text
stringlengths 1
2.05k
|
---|
import transform
from tvm.relay.testing |
import run_infer_type |
import tvm.topi.testing |
import tvm.testing
executor_kind = tvm.testing.parameter("debug", "vm")
def test_resize2d_infer_type():
n, c, h, w = te.size_var("n"), te.size_var("c"), te.size_var("h"), te.size_var("w")
x = relay.var("x", relay.TensorType((n, c, h, w), "int8"))
size = relay.var("size", relay.TensorType((2,), "int8"))
z = relay.image.resize2d(x, size)
zz = run_infer_type(z)
assert zz.checked_type == relay.TensorType((n, c, relay.Any(), relay.Any()), "int8")
@tvm.testing.uses_gpu
def test_resize2d(executor_kind):
def verify_resize2d(dshape, scale, method, layout):
if layout == "NHWC":
size = (dshape[1] * scale, dshape[2] * scale)
else:
size = (dshape[2] * scale, dshape[3] * scale)
size = np.array(size).astype("int64")
x_data = np.random.uniform(size=dshape).astype("float32")
x = relay.var("x", relay.TensorType(dshape, "float32"))
size_var = relay.var("size", relay.TensorType((2,), "int64"))
coord_trans = "asymmetric" if method == "nearest_neighbor" else "align_corners"
z = relay.image.resize2d(
x, size_var, None, layout, method, coordinate_transformation_mode=coord_trans
)
zz = run_infer_type(z)
func = relay.Function([x, size_var], z)
ref_res = tvm.topi.testing.resize2d_python(
x_data, (scale, scale), layout, method, coord_trans
)
for target, dev in tvm.testing.enabled_targets():
mod = tvm.ir.IRModule.from_expr(func)
op_res = relay.create_executor(
executor_kind, mod=mod, device=dev, target=target
).evaluate()(x_data, size)
tvm.testing.assert_allclose(op_res.numpy(), ref_res, rtol=1e-4, atol=1e-6)
for method in ["linear", "nearest_neighbor"]:
for layout in ["NCHW", "NHWC"]:
verify_resize2d((1, 4, 4, 4), 2, method, layout)
verify_resize2d((2, 8, 17, 20), 7, method, layout)
if __name__ == "__main__":
test_resize2d_infer_type()
t |
est_resize2d() |
""" Support level6 operator test cases.
""" |
import numpy as np |
import tvm
from tvm |
import te
from tvm |
import relay |
import tvm.testing
executor_kind = tvm.testing.parameter("debug", "vm")
@tvm.testing.uses_gpu
def test_dynamic_topk(executor_kind):
def verify_topk(k, axis, ret_type, is_ascend, dtype):
shape = (20, 100)
x = relay.var("x", relay.TensorType(shape, "float32"))
k_var = relay.var("x", relay.TensorType((1,), "float32"))
out = relay.topk(x, k_var, axis, ret_type, is_ascend, dtype)
if isinstance(out, relay.expr.TupleWrapper):
out = out.astuple()
func = relay.Function([x, k_var], out)
np_data = np.random.uniform(size=shape).astype("float32")
if is_ascend:
np_indices = np.argsort(np_data, axis=axis)
else:
np_indices = np.argsort(-np_data, axis=axis)
kk = k if k >= 1 else shape[axis]
if axis == 0:
np_indices = np_indices[:kk, :]
np_values = np.zeros(np_indices.shape).astype("float32")
for i in range(shape[1]):
np_values[:, i] = np_data[np_indices[:, i], i]
else:
np_indices = np_indices[:, :kk]
np_values = np.zeros(np_indices.shape).astype("float32")
for i in range(shape[0]):
np_values[i, :] = np_data[i, np_indices[i, :]]
np_indices = np_indices.astype(dtype)
for target, dev in tvm.testing.enabled_targets():
mod = tvm.ir.IRModule.from_expr(func)
op_res = relay.create_executor(
executor_kind, mod=mod, device=dev, target=target
).evaluate()(np_data, np.array([k]).astype("float32"))
if ret_type == "both":
tvm.testing.assert_allclose(op_res[0].numpy(), np_values)
tvm.testing.assert_allclose(op_res[1].numpy(), np_indices)
elif ret_type == "values":
tvm.testing.assert_allclose(op_res.numpy(), np_values)
else:
tvm.testing.assert_allclose(op_res.numpy(), np_indices)
np.random.seed(0)
for k in [0, 1, 5]:
for ax |
is in [0, -1, 1]:
for ret_type in ["both", "values", "indices"]:
verify_topk(k, axis, ret_type, True, "int64")
verify_topk(k, axis, ret_type, False, "float32")
if __name__ == "__main__":
test_dynamic_topk() |
"""Unit tests for annotations.""" |
import tvm |
import tvm.testing
from tvm |
import relay |
import pytest
def test_on_device_via_string():
x = relay.Var("x")
call = relay.annotation.on_device(x, "cuda")
assert isinstance(call, relay.Call)
assert len(call.args) == 1
assert call.args[0] == x
assert call.attrs.virtual_device.device_type_int == 2
assert call.attrs.virtual_device.virtual_device_id == 0
assert call.attrs.virtual_device.target is None
assert call.attrs.virtual_device.memory_scope == ""
assert call.attrs.constrain_body
assert not call.attrs.constrain_result
def test_on_device_via_device():
x = relay.Var("x")
call = relay.annotation.on_device(x, tvm.device("cpu"))
assert call.attrs.virtual_device.device_type_int == 1
def test_on_device_invalid_device():
x = relay.Var("x")
pytest.raises(ValueError, lambda: relay.annotation.on_device(x, "bogus"))
def test_on_device_fixed():
x = relay.Var("x")
call = relay.annotation.on_device(x, "cuda", constrain_result=True)
assert call.attrs.virtual_device.device_type_int == 2
assert call.attrs.constrain_body
assert call.attrs.constrain_result
def test_on_device_free():
x = relay.Var("x")
call = relay.annotation.on_device(x, "cuda", constrain_result=False, constrain_body=False)
assert call.attrs.virtual_device.device_type_int == -1
assert not call.attrs.constrain_body
assert not call.attrs.constrain_result
if __name__ == "__main__":
tvm.testing.main() |
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
"""Unit tests for tensor helpers."""
import tvm
import tvm.testing
from tvm import relay
import pytest
def test_device_copy_via_string():
x = relay.var("x")
call = relay.op.device_copy(x, "cuda", "cpu")
assert isinstance(call, relay.Call)
assert len(call.args) == 1
assert call.args[0] == x
assert call.attrs.src_virtual_device.device_type_int == 2 # ie kDLCUDA
assert call.attrs.src_virtual_device.virtual_device_id == 0
assert call.attrs.src_virtual_device.target is None
assert call.attrs.src_virtual_device.memory_scope == ""
assert call.attrs.dst_virtual_device.device_type_int == 1 # ie kDLCPU
assert call.attrs.dst_virtual_device.virtual_device_id == 0
assert call.attrs.dst_virtual_device.target is None
assert call.attrs.dst_virtual_device.memory_scope == ""
def test_device_copy_via_device():
x = relay.var("x")
call = relay.op.device_copy(x, tvm.device("cuda"), tvm.device("cpu"))
assert isinstance(call, relay.Call)
assert len(call.args) == 1
assert call.args[0] == x
assert call.attrs.src_virtual_device.device_type_int == 2 # ie kDLCUDA
assert call.attrs.dst_virtual_device.device_type_int == 1 # ie kDLCPU
if __name__ == "__main__":
tvm.testing.main()
|
import re |
import tvm |
import numpy as np
from tvm |
import relay
from tvm.relay |
import testing
from tvm.contrib |
import utils
from utils.adreno_utils |
import gpu_preprocess, build_run_compare |
import pytest
dtype = tvm.testing.parameter("float32")
@tvm.testing.requires_opencl
@tvm.testing.parametrize_targets("opencl -device=adreno")
def test_conv2d_inceptionv3_64x35x35_96x64x3x3_nopad(target, dtype):
input_shape = (1, 32, 42, 42)
filter_shape = (96, 32, 3, 3)
bias_shape = (1, 96, 1, 1)
A = relay.var("data", shape=input_shape, dtype=dtype)
B = relay.var("weight", shape=filter_shape, dtype=dtype)
bias = relay.var("bias", shape=bias_shape, dtype=dtype)
conv = relay.nn.conv2d(
A,
B,
data_layout="NCHW",
kernel_layout="OIHW",
padding=[0, 0, 0, 0],
strides=[2, 2],
out_dtype=dtype,
channels=96,
kernel_size=(3, 3),
)
D = relay.op.add(conv, bias)
D = relay.op.nn.relu(D)
mod = relay.Function([A, B, bias], D)
np.random.seed(0)
initializer = relay.testing.init.Xavier()
filter_data = np.zeros(filter_shape).astype(dtype)
bias_data = np.zeros(bias_shape).astype(dtype)
initializer("weight", filter_data)
initializer("bias", bias_data)
params1 = {
"weight": tvm.nd.array(filter_data),
"bias": tvm.nd.array(bias_data),
}
build_run_compare(mod, params1, {"data": input_shape}, dtype, target, [], gpu_preprocess)
@tvm.testing.requires_opencl
@tvm.testing.parametrize_targets("opencl -device=adreno")
def test_conv2d_inceptionv3_64x35x35_96x64x3x3_nopad_pass(target, dtype):
input_shape = (1, 32, 40, 40)
filter_shape = (96, 32, 2, 2)
bias_shape = (1, 96, 1, 1)
A = relay.var("data", shape=input_shape, dtype=dtype)
B = relay.var("weight", shape=filter_shape, dtype=dtype)
bias = relay.var("bias", shape=bias_shape, dtype=dtype)
conv = relay.nn.conv2d(
A,
B,
data_layout="NCHW",
kernel_layout="OIHW",
padding=[0, 0, 0, 0],
strides=[2, 2],
out_dtype=dtype,
channels=96,
kernel_size=(2, 2),
)
D = relay.op.add(conv, bias)
D = relay.op.nn.relu(D) |
mod = relay.Function([A, B, bias], D)
np.random.seed(0)
initializer = relay.testing.init.Xavier()
filter_data = np.zeros(filter_shape).astype(dtype)
bias_data = np.zeros(bias_shape).astype(dtype)
initializer("weight", filter_data)
initializer("bias", bias_data)
params1 = {
"weight": tvm.nd.array(filter_data),
"bias": tvm.nd.array(bias_data),
}
build_run_compare(mod, params1, {"data": input_shape}, dtype, target, [], gpu_preprocess)
@tvm.testing.requires_opencl
@tvm.testing.parametrize_targets("opencl -device=adreno")
def test_conv2d_inceptionv3_35_35_strides(target, dtype):
input_shape = (1, 48, 35, 35)
filter_shape = (64, 48, 5, 5)
bias_shape = (1, 64, 1, 1)
A = relay.var("data", shape=input_shape, dtype=dtype)
B = relay.var("weight", shape=filter_shape, dtype=dtype)
bias = relay.var("bias", shape=bias_shape, dtype=dtype)
conv = relay.nn.conv2d(
A,
B,
data_layout="NCHW",
kernel_layout="OIHW",
padding=[2, 2, 2, 2],
strides=[1, 1],
out_dtype=dtype,
channels=64,
kernel_size=(5, 5),
)
D = relay.op.add(conv, bias)
D = relay.op.nn.relu(D)
mod = relay.Function([A, B, bias], D)
np.random.seed(0)
initializer = relay.testing.init.Xavier()
filter_data = np.zeros(filter_shape).astype(dtype)
bias_data = np.zeros(bias_shape).astype(dtype)
initializer("weight", filter_data)
initializer("bias", bias_data)
params1 = {
"weight": tvm.nd.array(filter_data),
"bias": tvm.nd.array(bias_data),
}
build_run_compare(mod, params1, {"data": input_shape}, dtype, target, [], gpu_preprocess)
@tvm.testing.requires_opencl
@tvm.testing.parametrize_targets("opencl -device=adreno")
def test_conv2d_resnet50_v2_nchw_3c(target, dtype):
input_shape = (1, 3, 224, 224)
filter_shape = (64, 3, 7, 7)
bias_shape = (1, 64, 1, 1)
A = relay.var("data", shape=input_shape, dtype=dtype)
B = relay.var("weight", sh |
ape=filter_shape, dtype=dtype)
bias = relay.var("bias", shape=bias_shape, dtype=dtype)
conv = relay.nn.conv2d(
A,
B,
data_layout="NCHW",
kernel_layout="OIHW",
padding=[3, 3, 3, 3],
strides=[2, 2],
out_dtype=dtype,
channels=64,
kernel_size=(7, 7),
)
D = relay.op.add(conv, bias)
D = relay.op.nn.relu(D)
mod = relay.Function([A, B, bias], D)
np.random.seed(1)
initializer = relay.testing.init.Xavier()
filter_data = np.zeros(filter_shape).astype(dtype)
bias_data = np.zeros(bias_shape).astype(dtype)
initializer("weight", filter_data)
initializer("bias", bias_data)
params1 = {
"weight": tvm.nd.array(filter_data),
"bias": tvm.nd.array(bias_data),
}
build_run_compare(mod, params1, {"data": input_shape}, dtype, target)
@tvm.testing.requires_opencl
@tvm.testing.parametrize_targets("opencl -device=adreno")
def test_conv2d_inceptionv3_nchw_3c(target, dtype):
input_shape = (1, 3, 299, 299)
filter_shape = (64, 3, 3, 3)
bias_shape = (1, 64, 1, 1)
A = relay.var("data", shape=input_shape, dtype=dtype)
B = relay.var("weight", shape=filter_shape, dtype=dtype)
bias = relay.var("bias", shape=bias_shape, dtype=dtype)
conv = relay.nn.conv2d(
A,
B,
data_layout="NCHW",
kernel_layout="OIHW",
padding=[0, 0, 0, 0],
strides=[2, 2],
out_dtype=dtype,
channels=64,
kernel_size=(3, 3),
)
D = relay.op.add(conv, bias)
D = relay.op.nn.relu(D)
mod = relay.Function([A, B, bias], D)
np.random.seed(0)
initializer = relay.testing.init.Xavier()
filter_data = np.zeros(filter_shape).astype(dtype)
bias_data = np.zeros(bias_shape).astype(dtype)
initializer("weight", filter_data)
initializer("bias", bias_data)
params1 = {
"weight": tvm.nd.array(filter_data),
"bias": tvm.nd.array(bias_data),
}
build_run_compare(mod, params1, {"dat |
a": input_shape}, dtype, target)
@tvm.testing.requires_opencl
@tvm.testing.parametrize_targets("opencl -device=adreno")
def test_conv2d_1x1_16c16spatial(target, dtype):
input_shape = (1, 16, 256, 256)
filter_shape = (32, 16, 4, 4)
bias_shape = (1, 32, 1, 1)
A = relay.var("data", shape=input_shape, dtype=dtype)
B = relay.var("weight", shape=filter_shape, dtype=dtype)
bias = relay.var("bias", shape=bias_shape, dtype=dtype)
conv = relay.nn.conv2d(
A,
B,
data_layout="NCHW",
kernel_layout="OIHW",
padding=[0, 0, 0, 0],
strides=[2, 2],
out_dtype=dtype,
channels=32,
kernel_size=(4, 4),
)
D = relay.op.add(conv, bias)
D = relay.op.nn.relu(D)
mod = relay.Function([A, B, bias], D)
np.random.seed(0)
initializer = relay.testing.init.Xavier()
filter_data = np.zeros(filter_shape).astype(dtype)
bias_data = np.zeros(bias_shape).astype(dtype)
initializer("weight", filter_data)
initializer("bias", bias_data)
params1 = {
"weight": tvm.nd.array(filter_data),
"bias": tvm.nd.array(bias_data),
}
build_run_compare(mod, params1, {"data": input_shape}, dtype, target)
@tvm.testing.requires_opencl
@tvm.testing.parametrize_targets("opencl -device=adreno")
def test_conv2d_4x4_16c16pad(target, dtype):
input_shape = (1, 32, 256, 256)
filter_shape = (32, 32, 4, 4)
bias_shape = (1, 32, 1, 1)
A = relay.var("data", shape=input_shape, dtype=dtype)
B = relay.var("weight", shape=filter_shape, dtype=dtype)
bias = relay.var("bias", shape=bias_shape, dtype=dtype)
conv = relay.nn.conv2d(
A,
B,
data_layout="NCHW",
kernel_layout="OIHW",
padding=[3, 3, 0, 0],
strides=[2, 2],
out_dtype=dtype,
channels=32,
kernel_size=(4, 4),
)
D = relay.op.add(conv, bias)
D = relay.op.nn.relu(D)
mod = relay.Function([A, B, bias], D)
np.random.seed(0)
initializer = relay.te |
sting.init.Xavier()
filter_data = np.zeros(filter_shape).astype(dtype)
bias_data = np.zeros(bias_shape).astype(dtype)
initializer("weight", filter_data)
initializer("bias", bias_data)
params1 = {
"weight": tvm.nd.array(filter_data),
"bias": tvm.nd.array(bias_data),
}
build_run_compare(mod, params1, {"data": input_shape}, dtype, target)
@tvm.testing.requires_opencl
@tvm.testing.parametrize_targets("opencl -device=adreno")
def test_conv2d_4x4x4_16c16pad(target, dtype):
input_shape = (1, 32, 256, 256)
filter_shape = (4, 32, 4, 4)
bias_shape = (1, 4, 1, 1)
A = relay.var("data", shape=input_shape, dtype=dtype)
B = relay.var("weight", shape=filter_shape, dtype=dtype)
bias = relay.var("bias", shape=bias_shape, dtype=dtype)
conv = relay.nn.conv2d(
A,
B,
data_layout="NCHW",
kernel_layout="OIHW",
padding=[3, 3, 0, 0],
strides=[2, 2],
out_dtype=dtype,
channels=4,
kernel_size=(4, 4),
)
D = relay.op.add(conv, bias)
D = relay.op.nn.relu(D)
mod = relay.Function([A, B, bias], D)
np.random.seed(0)
initializer = relay.testing.init.Xavier()
filter_data = np.zeros(filter_shape).astype(dtype)
bias_data = np.zeros(bias_shape).astype(dtype)
initializer("weight", filter_data)
initializer("bias", bias_data)
params1 = {
"weight": tvm.nd.array(filter_data),
"bias": tvm.nd.array(bias_data),
}
build_run_compare(mod, params1, {"data": input_shape}, dtype, target)
@tvm.testing.requires_opencl
@tvm.testing.parametrize_targets("opencl -device=adreno")
def test_conv2d_yolov3_v2_nchw_3c(target, dtype):
input_shape = (1, 1024, 13, 13)
filter_shape = (255, 1024, 1, 1)
A = relay.var("data", shape=input_shape, dtype=dtype)
B = relay.var("weight", shape=filter_shape, dtype=dtype)
conv = relay.nn.conv2d(
A,
B,
data_layout="NCHW",
kernel_layout="OIHW",
padding=[0, 0, 0, 0], |
strides=[1, 1],
out_dtype=dtype,
channels=255,
kernel_size=(1, 1),
)
mod = relay.Function([A, B], conv)
np.random.seed(0)
initializer = relay.testing.init.Xavier()
filter_data = np.zeros(filter_shape).astype(dtype)
initializer("weight", filter_data)
params = {
"weight": tvm.nd.array(filter_data),
}
build_run_compare(mod, params, {"data": input_shape}, dtype, target)
@tvm.testing.requires_opencl
@tvm.testing.parametrize_targets("opencl -device=adreno")
def test_conv2d_vgg16_winograd_4d(target, dtype):
input_shape = (1, 512, 28, 28)
filter_shape = (512, 512, 3, 3)
bias_shape = (1, 512, 1, 1)
A = relay.var("data", shape=input_shape, dtype=dtype)
B = relay.var("weight", shape=filter_shape, dtype=dtype)
bias = relay.var("bias", shape=bias_shape, dtype=dtype)
conv = relay.nn.conv2d(
A,
B,
data_layout="NCHW",
kernel_layout="OIHW",
padding=[1, 1, 1, 1],
channels=512,
kernel_size=[3, 3],
out_dtype=dtype,
)
D = relay.op.add(conv, bias)
D = relay.op.nn.relu(D)
mod = relay.Function([A, B, bias], D)
np.random.seed(0)
initializer = relay.testing.init.Xavier()
filter_data = np.zeros(filter_shape).astype(dtype)
bias_data = np.zeros(bias_shape).astype(dtype)
initializer("weight", filter_data)
initializer("bias", bias_data)
params1 = {
"weight": tvm.nd.array(filter_data),
"bias": tvm.nd.array(bias_data),
}
temp = utils.tempdir()
stat_file = temp.relpath("stat.log")
with open(stat_file, "w") as f:
f.write(
f'{{"input": ["opencl -keys=adreno,opencl,gpu -device=adreno -max_num_threads=256", "conv2d_nchw_winograd.image2d", [["TENSOR", [1, 512, 28, 28], "{dtype}"], ["TENSOR", [512, 512, 3, 3], "{dtype}"], [1, 1], [1, 1, 1, 1], [1, 1], "{dtype}"], {{}}], "config": {{"index": 1591, "code_hash": null, "entity": [["auto_unroll_max_step", "ot", 4], ["tile_y", "sp", [-1 |
, 1, 32]], ["tile_x", "sp", [-1, 4, 2]], ["tile_rc", "sp", [-1, 8]]]}}, "result": [[0.0037244], 0, 7.06374192237854, 1653898629.7427933], "version": 0.2, "tvm_version": "0.8.dev0"}}\n'
)
graph = build_run_compare(
mod, params1, {"data": input_shape}, dtype, target, stat_file=stat_file
)
matches = re.findall("winograd", graph)
assert len(matches) > 0
@tvm.testing.requires_opencl
@tvm.testing.parametrize_targets("opencl -device=adreno")
def test_conv2d_winograd_conv(target, dtype):
input_shape = (1, 4, 3, 3)
A = relay.var("data", shape=input_shape, dtype=dtype)
filter_shape3 = (8, 4, 3, 3)
bias_shape3 = (8,)
B3 = relay.var("weight3", shape=filter_shape3, dtype=dtype)
D = relay.nn.conv2d(
A, B3, padding=[1, 1, 1, 1], channels=8, kernel_size=[3, 3], out_dtype=dtype
)
filter_shape4 = (8, 8, 3, 3)
bias_shape4 = (8,)
B4 = relay.var("weight4", shape=filter_shape4, dtype=dtype)
D = relay.nn.conv2d(
D, B4, padding=[1, 1, 1, 1], channels=8, kernel_size=[3, 3], out_dtype=dtype
)
mod = relay.Function([A, B3, B4], D)
np.random.seed(1)
initializer = relay.testing.init.Xavier()
filter_data3 = np.zeros(filter_shape3).astype(dtype)
bias_data3 = np.zeros(bias_shape3).astype(dtype)
filter_data4 = np.zeros(filter_shape4).astype(dtype)
bias_data4 = np.zeros(bias_shape4).astype(dtype)
initializer("weight", filter_data3)
initializer("bias", bias_data3)
initializer("weight", filter_data4)
initializer("bias", bias_data4)
params1 = {
"weight3": tvm.nd.array(filter_data3),
"weight4": tvm.nd.array(filter_data4),
}
temp = utils.tempdir()
stat_file = temp.relpath("stat.log")
with open(stat_file, "w") as f:
f.write(
f'{{"input": ["opencl -keys=adreno,opencl,gpu -device=adreno -max_num_threads=256", "conv2d_nchw_winograd.image2d", [["TENSOR", [1, 4, 3, 3], "{dtype}"], ["TENSOR", [8, 4, 3, 3], "{dtype}"], [1, 1], [1, 1, 1, 1], [1, 1], "{dtype}"], {{} |
}], "config": {{"index": 1591, "code_hash": null, "entity": [["auto_unroll_max_step", "ot", 4], ["tile_y", "sp", [-1, 1, 32]], ["tile_x", "sp", [-1, 4, 2]], ["tile_rc", "sp", [-1, 8]]]}}, "result": [[0.0037244], 0, 7.06374192237854, 1653898629.7427933], "version": 0.2, "tvm_version": "0.8.dev0"}}\n'
)
graph = build_run_compare(
mod, params1, {"data": input_shape}, dtype, target, stat_file=stat_file
)
matches = re.findall("winograd", graph)
assert len(matches) > 0
@tvm.testing.requires_opencl
@tvm.testing.parametrize_targets("opencl -device=adreno")
def test_residual_block(target, dtype):
"""
- some kind of residual block followed by convolution to have texture after residual block
- scalar data type verification which should be mapped to global memory scope
layout_transform (NCHW->NCHW4c)
| <- buffer
conv2d (1) <- to get textures as output
/ \
conv2d (2) |
\ /
add <- add should be fused into conv2d (2)
multiply to scalar <- buffer to the input of multiply scalar value
relu
| <- texture in intermediate tensor
conv2d (3)
relu
| <- buffer
layout_transform (NCHW4c->NCHW)
"""
input_shape = (1, 32, 40, 40)
filter_shape1 = (32, 32, 2, 2)
filter_shape2 = (32, 32, 1, 1)
filter_shape3 = (32, 32, 2, 2)
bias_shape1 = (1, 32, 1, 1)
A = relay.var("data", shape=input_shape, dtype=dtype)
W1 = relay.var("weight1", shape=filter_shape1, dtype=dtype)
B1 = relay.var("bias1", shape=bias_shape1, dtype=dtype)
W2 = relay.var("weight2", shape=filter_shape2, dtype=dtype)
W3 = relay.var("weight3", shape=filter_shape3, dtype=dtype)
conv1 = relay.nn.conv2d(
A,
W1,
data_l |
ayout="NCHW",
kernel_layout="OIHW",
padding=[0, 0, 0, 0],
strides=[2, 2],
out_dtype=dtype,
channels=32,
kernel_size=(2, 2),
)
D = relay.op.add(conv1, B1)
D = relay.op.nn.relu(D)
conv2 = relay.nn.conv2d(
D,
W2,
data_layout="NCHW",
kernel_layout="OIHW",
padding=[0, 0, 0, 0],
strides=[1, 1],
out_dtype=dtype,
channels=32,
kernel_size=(1, 1),
)
D = relay.op.add(conv2, D)
D = D * relay.const(0.15, dtype)
D = relay.op.nn.relu(D)
conv3 = relay.nn.conv2d(
D,
W3,
data_layout="NCHW",
kernel_layout="OIHW",
padding=[0, 0, 0, 0],
strides=[2, 2],
out_dtype=dtype,
channels=32,
kernel_size=(2, 2),
)
D = relay.op.nn.relu(conv3)
mod = relay.Function([A, W1, B1, W2, W3], D)
np.random.seed(0)
initializer = relay.testing.init.Xavier()
filter_data1 = np.zeros(filter_shape1).astype(dtype)
bias_data1 = np.zeros(bias_shape1).astype(dtype)
initializer("weight", filter_data1)
initializer("bias", bias_data1)
filter_data2 = np.zeros(filter_shape2).astype(dtype)
initializer("weight", filter_data2)
filter_data3 = np.zeros(filter_shape3).astype(dtype)
initializer("weight", filter_data3)
params1 = {
"weight1": tvm.nd.array(filter_data1),
"bias1": tvm.nd.array(bias_data1),
"weight2": tvm.nd.array(filter_data2),
"weight3": tvm.nd.array(filter_data3),
}
if dtype == "float16":
static_memory_scope = [
"global",
"global.texture",
"global.texture-weight",
"global.texture-weight",
"global.texture",
"global.texture-weight",
"global",
"global.texture",
"global.texture-weight",
"",
"",
]
else:
static_memory_scope = [
"global",
"global.texture", |
"global.texture-weight",
"global.texture-weight",
"global.texture",
"global.texture-weight",
"global.texture",
"global.texture-weight",
"",
"",
]
build_run_compare(mod, params1, {"data": input_shape}, dtype, target, static_memory_scope)
@tvm.testing.requires_opencl
@tvm.testing.parametrize_targets("opencl -device=adreno")
def test_concat(target, dtype):
"""
layout_transform (NCHW->NCHW4c)
| <- buffer
conv2d (1) <- to get textures as output
/ \
conv2d (2) conv2d (3)
\ / <- concat does not support textures, there we should have buffers
concatenation
| <- buffer
layout_transform (NCHW4c->NCHW)
"""
input_shape = (1, 32, 40, 40)
filter_shape1 = (96, 32, 2, 2)
filter_shape2 = (32, 96, 2, 2)
filter_shape3 = (5, 96, 2, 2)
bias_shape1 = (1, 96, 1, 1)
bias_shape2 = (1, 32, 1, 1)
A = relay.var("data", shape=input_shape, dtype=dtype)
W1 = relay.var("weight1", shape=filter_shape1, dtype=dtype)
B1 = relay.var("bias1", shape=bias_shape1, dtype=dtype)
W2 = relay.var("weight2", shape=filter_shape2, dtype=dtype)
W3 = relay.var("weight3", shape=filter_shape3, dtype=dtype)
B2 = relay.var("bias2", shape=bias_shape2, dtype=dtype)
conv1 = relay.nn.conv2d(
A,
W1,
data_layout="NCHW",
kernel_layout="OIHW",
padding=[0, 0, 0, 0],
strides=[2, 2],
out_dtype=dtype,
channels=96,
kernel_size=(2, 2),
)
D = relay.op.add(conv1, B1)
D = relay.op.nn.relu(D)
conv2 = relay.nn.conv2d(
D,
W2,
data_layout="NCHW",
kernel_layout="OIHW",
padding=[0, 0, 0, 0],
strides=[2, 2],
out_dtype=dtype,
channels=32,
kernel_size=(2, |
2),
)
conv2 = relay.op.add(conv2, B2)
conv2 = relay.op.nn.relu(conv2)
conv3 = relay.nn.conv2d(
D,
W3,
data_layout="NCHW",
kernel_layout="OIHW",
padding=[0, 0, 0, 0],
strides=[2, 2],
out_dtype=dtype,
channels=5,
kernel_size=(2, 2),
)
t = relay.Tuple([conv2, conv3])
c = relay.op.concatenate(t, axis=1)
mod = relay.Function([A, W1, B1, W2, B2, W3], c)
np.random.seed(0)
initializer = relay.testing.init.Xavier()
filter_data1 = np.zeros(filter_shape1).astype(dtype)
bias_data1 = np.zeros(bias_shape1).astype(dtype)
initializer("weight", filter_data1)
initializer("bias", bias_data1)
filter_data2 = np.zeros(filter_shape2).astype(dtype)
bias_data2 = np.zeros(bias_shape2).astype(dtype)
initializer("weight", filter_data2)
initializer("bias", bias_data2)
filter_data3 = np.zeros(filter_shape3).astype(dtype)
initializer("weight", filter_data3)
params1 = {
"weight1": tvm.nd.array(filter_data1),
"bias1": tvm.nd.array(bias_data1),
"weight2": tvm.nd.array(filter_data2),
"bias2": tvm.nd.array(bias_data2),
"weight3": tvm.nd.array(filter_data3),
}
static_memory_scope = [
"",
"global",
"global.texture-weight",
"global.texture-weight",
"global",
"global.texture-weight",
"global.texture-weight",
"",
"",
"",
"",
"",
]
static_memory_scope = []
build_run_compare(mod, params1, {"data": input_shape}, dtype, target, static_memory_scope)
@tvm.testing.requires_opencl
@tvm.testing.parametrize_targets("opencl -device=adreno")
def test_pooling_branching_texture_params(target, dtype):
"""
Verification of the pooling and many branches having textures
layout_transform (NCHW->NCHW4c)
| <- buffer
conv2d (0) <- to get textures |
| <- textures
pooling
/ \ \ <- textures
conv2d (1) conv2d (2) conv2d (3)
\ / |
add | <- to have the only one output, will be fused
\ /
add <- to have the only one output, will be fused
| <- buffer
layout_transform (NCHW4c->NCHW)
"""
input_shape = (1, 32, 40, 40)
filter_shape0 = (32, 32, 1, 1)
filter_shape1 = (32, 32, 2, 2)
filter_shape2 = (32, 32, 1, 1)
filter_shape3 = (32, 32, 2, 2)
bias_shape1 = (1, 32, 1, 1)
A = relay.var("data", shape=input_shape, dtype=dtype)
W0 = relay.var("weight0", shape=filter_shape0, dtype=dtype)
W1 = relay.var("weight1", shape=filter_shape1, dtype=dtype)
B1 = relay.var("bias1", shape=bias_shape1, dtype=dtype)
W2 = relay.var("weight2", shape=filter_shape2, dtype=dtype)
W3 = relay.var("weight3", shape=filter_shape3, dtype=dtype)
conv0 = relay.nn.conv2d(
A,
W0,
data_layout="NCHW",
kernel_layout="OIHW",
padding=[0, 0, 0, 0],
strides=[1, 1],
out_dtype=dtype,
channels=32,
kernel_size=(1, 1),
)
pool = relay.nn.avg_pool2d(conv0, pool_size=(2, 2), strides=(2, 2))
conv1 = relay.nn.conv2d(
pool,
W1,
data_layout="NCHW",
kernel_layout="OIHW",
padding=[0, 0, 1, 1],
strides=[1, 1],
out_dtype=dtype,
channels=32,
kernel_size=(2, 2),
)
conv1 = relay.op.add(conv1, B1)
conv1 = relay.op.nn.relu(conv1)
conv2 = relay.nn.conv2d(
pool,
W2,
data_layout="NCHW",
kernel_layout="OIHW",
padding=[0, 0, 0, 0],
strides=[1, 1],
out_dtype=dtype,
channels=32,
kernel |
_size=(1, 1),
)
conv3 = relay.nn.conv2d(
pool,
W3,
data_layout="NCHW",
kernel_layout="OIHW",
padding=[0, 1, 1, 0],
strides=[1, 1],
out_dtype=dtype,
channels=32,
kernel_size=(2, 2),
)
conv3 = relay.op.nn.relu(conv3)
res = relay.op.add(conv1, conv2)
res = relay.op.add(res, conv3)
mod = relay.Function([A, W0, W1, B1, W2, W3], res)
np.random.seed(0)
initializer = relay.testing.init.Xavier()
filter_data0 = np.zeros(filter_shape0).astype(dtype)
filter_data1 = np.zeros(filter_shape1).astype(dtype)
bias_data1 = np.zeros(bias_shape1).astype(dtype)
initializer("weight", filter_data1)
initializer("bias", bias_data1)
filter_data2 = np.zeros(filter_shape2).astype(dtype)
initializer("weight", filter_data2)
filter_data3 = np.zeros(filter_shape3).astype(dtype)
initializer("weight", filter_data3)
params1 = {
"weight0": tvm.nd.array(filter_data0),
"weight1": tvm.nd.array(filter_data1),
"bias1": tvm.nd.array(bias_data1),
"weight2": tvm.nd.array(filter_data2),
"weight3": tvm.nd.array(filter_data3),
}
static_memory_scope = [
"global",
"global.texture",
"global.texture-weight",
"global.texture",
"global.texture",
"global.texture-weight",
"global.texture-weight",
"global.texture-weight",
"global.texture",
"global.texture-weight",
"global.texture",
"",
"",
]
build_run_compare(mod, params1, {"data": input_shape}, dtype, target, static_memory_scope)
@tvm.testing.requires_opencl
@tvm.testing.parametrize_targets("opencl -device=adreno")
def test_branching_texture_params(target, dtype):
"""
Verification of passing texture to several consumers markup of relay variables in
primary functions + on_device
layout_transform (NCHW->NCHW4c)
| <- buffer |
conv2d (0) <- to get textures
/ \ \ <- here should be textures and textures in params
conv2d (1) conv2d (2) conv2d (3)
\ / |
add | <- to have the only one output
\ /
add <- to have the only one output
| <- buffer
layout_transform (NCHW4c->NCHW)
"""
input_shape = (1, 32, 40, 40)
filter_shape0 = (32, 32, 1, 1)
filter_shape1 = (32, 32, 2, 2)
filter_shape2 = (32, 32, 1, 1)
filter_shape3 = (32, 32, 2, 2)
bias_shape1 = (1, 32, 1, 1)
A = relay.var("data", shape=input_shape, dtype=dtype)
W0 = relay.var("weight0", shape=filter_shape0, dtype=dtype)
W1 = relay.var("weight1", shape=filter_shape1, dtype=dtype)
B1 = relay.var("bias1", shape=bias_shape1, dtype=dtype)
W2 = relay.var("weight2", shape=filter_shape2, dtype=dtype)
W3 = relay.var("weight3", shape=filter_shape3, dtype=dtype)
conv0 = relay.nn.conv2d(
A,
W0,
data_layout="NCHW",
kernel_layout="OIHW",
padding=[0, 0, 0, 0],
strides=[1, 1],
out_dtype=dtype,
channels=32,
kernel_size=(1, 1),
)
conv1 = relay.nn.conv2d(
conv0,
W1,
data_layout="NCHW",
kernel_layout="OIHW",
padding=[0, 0, 1, 1],
strides=[1, 1],
out_dtype=dtype,
channels=32,
kernel_size=(2, 2),
)
conv1 = relay.op.add(conv1, B1)
conv1 = relay.op.nn.relu(conv1)
conv2 = relay.nn.conv2d(
conv0,
W2,
data_layout="NCHW",
kernel_layout="OIHW",
padding=[0, 0, 0, 0],
strides=[1, 1],
out_dtype=dtype,
channels=32,
kernel_size=(1, 1),
)
conv3 = relay.nn.conv2d(
conv0,
W3,
data_layout="NCHW", |
kernel_layout="OIHW",
padding=[0, 1, 1, 0],
strides=[1, 1],
out_dtype=dtype,
channels=32,
kernel_size=(2, 2),
)
conv3 = relay.op.nn.relu(conv3)
res = relay.op.add(conv1, conv2)
res = relay.op.add(res, conv3)
mod = relay.Function([A, W0, W1, B1, W2, W3], res)
np.random.seed(0)
initializer = relay.testing.init.Xavier()
filter_data0 = np.zeros(filter_shape0).astype(dtype)
filter_data1 = np.zeros(filter_shape1).astype(dtype)
bias_data1 = np.zeros(bias_shape1).astype(dtype)
initializer("weight", filter_data1)
initializer("bias", bias_data1)
filter_data2 = np.zeros(filter_shape2).astype(dtype)
initializer("weight", filter_data2)
filter_data3 = np.zeros(filter_shape3).astype(dtype)
initializer("weight", filter_data3)
params1 = {
"weight0": tvm.nd.array(filter_data0),
"weight1": tvm.nd.array(filter_data1),
"bias1": tvm.nd.array(bias_data1),
"weight2": tvm.nd.array(filter_data2),
"weight3": tvm.nd.array(filter_data3),
}
static_memory_scope = [
"global",
"global.texture",
"global.texture-weight",
"global.texture",
"global.texture-weight",
"global.texture-weight",
"global.texture-weight",
"global.texture",
"global.texture-weight",
"global.texture",
"",
"",
]
build_run_compare(mod, params1, {"data": input_shape}, dtype, target, static_memory_scope)
@tvm.testing.requires_opencl
@tvm.testing.parametrize_targets("opencl -device=adreno")
def test_conv2d_different_lowering_same_op(target, dtype):
"""
Use case for verification of caching compiled functions
Three convolutions following by each other in this case should be
compiled in three different entities and lowered differently because
they are differ in input param memory scopes and in output memory scope
layout_transform (NCHW->NCHW4c)
| |
<- buffer
conv2d (1) <- buffer as input tensor and texture as output
| <- texture
conv2d (2) <- texture as input and texture as output
| <- texture
conv2d (3) <- texture as input and buffer as output
| <- buffer
layout_transform (NCHW4c->NCHW)
"""
input_shape = (1, 32, 40, 40)
filter_shape1 = (32, 32, 1, 1)
A = relay.var("data", shape=input_shape, dtype=dtype)
W1 = relay.var("weight1", shape=filter_shape1, dtype=dtype)
conv1 = relay.nn.conv2d(
A,
W1,
data_layout="NCHW",
kernel_layout="OIHW",
padding=[0, 0, 0, 0],
strides=[1, 1],
out_dtype=dtype,
channels=32,
kernel_size=(1, 1),
)
conv2 = relay.nn.conv2d(
conv1,
W1,
data_layout="NCHW",
kernel_layout="OIHW",
padding=[0, 0, 0, 0],
strides=[1, 1],
out_dtype=dtype,
channels=32,
kernel_size=(1, 1),
)
conv3 = relay.nn.conv2d(
conv2,
W1,
data_layout="NCHW",
kernel_layout="OIHW",
padding=[0, 0, 0, 0],
strides=[1, 1],
out_dtype=dtype,
channels=32,
kernel_size=(1, 1),
)
mod = relay.Function([A, W1], conv3)
np.random.seed(0)
initializer = relay.testing.init.Xavier()
filter_data1 = np.zeros(filter_shape1).astype(dtype)
params1 = {
"weight1": tvm.nd.array(filter_data1),
}
static_memory_scope = [
"global",
"global.texture",
"global.texture-weight",
"global.texture",
"global.texture",
"",
"",
]
build_run_compare(mod, params1, {"data": input_shape}, dtype, target, static_memory_scope)
@tvm.testing.requires_opencl
@tvm.testing.parametrize_targets("ope |
ncl -device=adreno")
def test_conv2d_winograd_non_rect(target, dtype):
input_shape = (1, 771, 36, 64)
A = relay.var("data", shape=input_shape, dtype=dtype)
filter_shape = (128, 771, 3, 3)
B = relay.var("weight", shape=filter_shape, dtype=dtype)
D = relay.nn.conv2d(
A, B, padding=[1, 1, 1, 1], channels=128, kernel_size=[3, 3], out_dtype=dtype
)
mod = relay.Function([A, B], D)
np.random.seed(1)
initializer = relay.testing.init.Xavier()
filter_data = np.zeros(filter_shape).astype(dtype)
initializer("weight", filter_data)
params1 = {
"weight": tvm.nd.array(filter_data),
}
temp = utils.tempdir()
stat_file = temp.relpath("stat.log")
with open(stat_file, "w") as f:
f.write(
f'{{"input": ["opencl -keys=adreno,opencl,gpu -device=adreno -max_num_threads=256 -texture_spatial_limit=16384 -thread_warp_size=1", "conv2d_nchw_winograd.image2d", [["TENSOR", [1, 771, 36, 64], "{dtype}"], ["TENSOR", [128, 771, 3, 3], "{dtype}"], [1, 1], [1, 1, 1, 1], [1, 1], "{dtype}"], {{}}], "config": {{"index": 5399, "code_hash": null, "entity": [["auto_unroll_max_step", "ot", 16], ["tile_y", "sp", [-1, 1, 32]], ["tile_x", "sp", [-1, 4, 8]], ["tile_rc", "sp", [-1, 193]]]}}, "result": [[0.0037244], 0, 7.06374192237854, 1653898629.7427933], "version": 0.2, "tvm_version": "0.8.dev0"}}\n'
)
graph = build_run_compare(
mod, params1, {"data": input_shape}, dtype, target, stat_file=stat_file
)
matches = re.findall("winograd", graph)
assert len(matches) > 0
@tvm.testing.requires_opencl
@tvm.testing.parametrize_targets("opencl -device=adreno")
def test_injective_nwo_inputs1(target, dtype):
"""
Use case for verification of stability of annotation primary functions
having several ops accepting data outside of Primary function
The visiting of ops during traversing of graph inside primary function
can depend on order of relay graph creation. Thus the annotation mechanism
should be reliable for graph t |
raversal order
The current decision if Prim Function support textures or not depend on
*any* op accepting input of the function and if op support textures
Input
/ \
layout_transform (NCHW->NCHW4c) |
| /
conv2d (1) /
| /
conv2d (2) mean /
/ \ / <- Primary function several head ops
(1)add (2)layout_transform |
| (NCHW4c->NCHW) |
| | \ /
| | (3) add
| | |
layout_transform \ /
(NCHW4c->NCHW) \ /
\ mul
\ /
add
This test verifies a case when the latest op which is visited is (3) and does not
support textures, but there is (1) supporting textures, thus the whole func will
support textures
"""
input_shape = (1, 4, 40, 40)
filter_shape1 = (4, 4, 3, 3)
filter_shape2 = (4, 4, 3, 3)
filter_shape3 = (4, 4, 3, 3)
A = relay.var("data", shape=input_shape, dtype=dtype)
W1 = relay.var("weight1", shape=filter_shape1, dtype=dtype)
W2 = relay.var("weight2", shape=filter_shape2, dtype=dtype)
mean = relay.mean(A, axis=1, keepdims=True)
conv1 = relay.nn.conv2d(
A,
W1,
data_layout="NCHW",
kernel_layout="OIHW",
padding=[1, 1, 1, 1],
strides=[1, 1],
out_dtype=dtype,
channels=4,
kernel_size=(3, 3),
)
conv2 = relay.nn.conv2d(
conv1,
W2,
data_layout="NCHW",
kernel_layout="OIHW",
padding=[1, 1, 1, 1],
strides=[1, 1],
out_dtype=dtype,
channels=4,
kernel_s |
ize=(3, 3),
)
ad3 = relay.op.add(conv1, conv2)
ad1 = relay.op.add(mean, conv1)
ad2 = relay.op.multiply(ad1, conv2)
ad4 = relay.op.add(ad3, ad2)
mod = relay.Function([A, W1, W2], ad4)
np.random.seed(0)
initializer = relay.testing.init.Xavier()
filter_data1 = np.zeros(filter_shape1).astype(dtype)
filter_data2 = np.zeros(filter_shape2).astype(dtype)
initializer("weight", filter_data1)
initializer("weight", filter_data2)
params1 = {
"weight1": tvm.nd.array(filter_data1),
"weight2": tvm.nd.array(filter_data2),
}
static_memory_scope = [
"global",
"global.texture",
"global.texture-nhwc",
"global.texture",
"global.texture-nhwc",
"global.texture",
"global",
"global",
]
build_run_compare(mod, params1, {"data": input_shape}, dtype, target, static_memory_scope)
@tvm.testing.requires_opencl
@tvm.testing.parametrize_targets("opencl -device=adreno")
def test_injective_nwo_inputs2(target, dtype):
"""
Use case for verification of stability of annotation primary functions
having several ops accepting data outside of Primary function
The visiting of ops during traversing of graph inside primary function
can depend on order of relay graph creation. Thus the annotation mechanism
should be reliable for graph traversal order
The current decision if Prim Function support textures or not depend on
*any* op accepting input of the function and if op support textures
Input
/ \
layout_transform (NCHW->NCHW4c) |
| /
conv2d (1) /
| /
conv2d (2) mean /
/ \ / <- Primary function several head ops
(1)add (2)layout_transform | |
| (NCHW4c->NCHW) |
| | \ /
| | (3) add
| | |
layout_transform \ /
(NCHW4c->NCHW) \ /
\ mul
\ /
add
This test verifies a case when the latest op which is (1), it supports textures
an whole prim function is considered as a func working with textures
"""
input_shape = (1, 4, 40, 40)
filter_shape1 = (4, 4, 3, 3)
filter_shape2 = (4, 4, 3, 3)
filter_shape3 = (4, 4, 3, 3)
A = relay.var("data", shape=input_shape, dtype=dtype)
W1 = relay.var("weight1", shape=filter_shape1, dtype=dtype)
W2 = relay.var("weight2", shape=filter_shape2, dtype=dtype)
mean = relay.mean(A, axis=1, keepdims=True)
conv1 = relay.nn.conv2d(
A,
W1,
data_layout="NCHW",
kernel_layout="OIHW",
padding=[1, 1, 1, 1],
strides=[1, 1],
out_dtype=dtype,
channels=4,
kernel_size=(3, 3),
)
conv2 = relay.nn.conv2d(
conv1,
W2,
data_layout="NCHW",
kernel_layout="OIHW",
padding=[1, 1, 1, 1],
strides=[1, 1],
out_dtype=dtype,
channels=4,
kernel_size=(3, 3),
)
ad3 = relay.op.add(conv1, conv2)
ad1 = relay.op.add(mean, conv1)
ad2 = relay.op.multiply(ad1, conv2)
ad4 = relay.op.add(ad2, ad3)
mod = relay.Function([A, W1, W2], ad4)
np.random.seed(0)
initializer = relay.testing.init.Xavier()
filter_data1 = np.zeros(filter_shape1).astype(dtype)
filter_data2 = np.zeros(filter_shape2).astype(dtype)
initializer("weight", filter_data1)
initializer("weight", filter_data2)
params1 = {
"weight1": tvm.nd.array(filter_data1),
"weight2": tvm.nd.array(filter_data2),
}
static_memory_scope = [
"global",
"global.texture",
"global.texture-nhwc", |
"global.texture",
"global",
"global.texture-nhwc",
"global.texture",
"global",
]
build_run_compare(mod, params1, {"data": input_shape}, dtype, target, static_memory_scope) |
import os |
import re |
import tvm |
import numpy as np
from tvm |
import relay
from tvm.relay |
import testing
from tvm.contrib |
import utils
from utils.adreno_utils |
import gpu_preprocess, build_run_compare |
import pytest
dtype = tvm.testing.parameter("float32")
@tvm.testing.requires_opencl
@tvm.testing.parametrize_targets("opencl -device=adreno")
def test_conv2d_deeplabv3_1_257_257_32x1_1_32_16(target, dtype):
input_shape = (1, 257, 257, 32)
filter_shape = (1, 1, 32, 16)
bias_shape = (filter_shape[-1],)
A = relay.var("data", shape=input_shape, dtype=dtype)
B = relay.var("weight", shape=filter_shape, dtype=dtype)
bias = relay.var("bias", shape=bias_shape, dtype=dtype)
conv = relay.nn.conv2d(
A,
B,
data_layout="NHWC",
kernel_layout="HWIO",
out_dtype=dtype,
channels=filter_shape[-1],
kernel_size=(1, 1),
)
D = relay.op.add(conv, bias)
D = relay.op.nn.relu(D)
mod = relay.Function([A, B, bias], D)
np.random.seed(1)
initializer = relay.testing.init.Xavier()
filter_data = np.zeros(filter_shape).astype(dtype)
bias_data = np.zeros(bias_shape).astype(dtype)
initializer("weight", filter_data)
initializer("bias", bias_data)
params1 = {
"weight": tvm.nd.array(filter_data),
"bias": tvm.nd.array(bias_data),
}
build_run_compare(mod, params1, {"data": input_shape}, dtype, target)
@tvm.testing.requires_opencl
@tvm.testing.parametrize_targets("opencl -device=adreno")
def test_conv2d_deeplabv3_1_257_257_32x1_1_32_16_with_padding(target, dtype):
input_shape = (1, 257, 257, 32)
filter_shape = (1, 1, 32, 16)
bias_shape = (filter_shape[-1],)
A = relay.var("data", shape=input_shape, dtype=dtype)
B = relay.var("weight", shape=filter_shape, dtype=dtype)
bias = relay.var("bias", shape=bias_shape, dtype=dtype)
conv = relay.nn.conv2d(
A,
B,
data_layout="NHWC",
kernel_layout="HWIO",
padding=[3, 3, 3, 3],
strides=[2, 2],
out_dtype=dtype,
channels=filter_shape[-1],
kernel_size=(1, 1),
)
D = relay.op.add(conv, bias)
D = relay.op.nn.relu(D)
mod = relay.Function([A, B, bias], D) |
np.random.seed(1)
initializer = relay.testing.init.Xavier()
filter_data = np.zeros(filter_shape).astype(dtype)
bias_data = np.zeros(bias_shape).astype(dtype)
initializer("weight", filter_data)
initializer("bias", bias_data)
params1 = {
"weight": tvm.nd.array(filter_data),
"bias": tvm.nd.array(bias_data),
}
build_run_compare(mod, params1, {"data": input_shape}, dtype, target)
@tvm.testing.requires_opencl
@tvm.testing.parametrize_targets("opencl -device=adreno")
def test_conv2d_4_35_35_32x3_3_144_16(target, dtype):
input_shape = (4, 35, 35, 32)
filter_shape = (3, 3, 32, 16)
bias_shape = (filter_shape[-1],)
kernel_size = (filter_shape[0], filter_shape[1])
A = relay.var("data", shape=input_shape, dtype=dtype)
B = relay.var("weight", shape=filter_shape, dtype=dtype)
bias = relay.var("bias", shape=bias_shape, dtype=dtype)
conv = relay.nn.conv2d(
A,
B,
data_layout="NHWC",
kernel_layout="HWIO",
out_dtype=dtype,
channels=filter_shape[-1],
kernel_size=kernel_size,
)
D = relay.op.add(conv, bias)
D = relay.op.nn.relu(D)
mod = relay.Function([A, B, bias], D)
np.random.seed(1)
initializer = relay.testing.init.Xavier()
filter_data = np.zeros(filter_shape).astype(dtype)
bias_data = np.zeros(bias_shape).astype(dtype)
initializer("weight", filter_data)
initializer("bias", bias_data)
params1 = {
"weight": tvm.nd.array(filter_data),
"bias": tvm.nd.array(bias_data),
}
build_run_compare(mod, params1, {"data": input_shape}, dtype, target)
@tvm.testing.requires_opencl
@tvm.testing.parametrize_targets("opencl -device=adreno")
def test_conv2d_deeplabv3_1_513_513_3x3_3_3_32(target, dtype):
input_shape = (1, 513, 513, 3)
filter_shape = (3, 3, 3, 32)
bias_shape = (filter_shape[-1],)
kernel_size = (filter_shape[0], filter_shape[1])
A = relay.var("data", shape=input_shape, dtype=dtype)
B = relay.var("w |
eight", shape=filter_shape, dtype=dtype)
bias = relay.var("bias", shape=bias_shape, dtype=dtype)
conv = relay.nn.conv2d(
A,
B,
data_layout="NHWC",
kernel_layout="HWIO",
out_dtype=dtype,
channels=filter_shape[-1],
kernel_size=kernel_size,
)
D = relay.op.add(conv, bias)
D = relay.op.nn.relu(D)
mod = relay.Function([A, B, bias], D)
np.random.seed(1)
initializer = relay.testing.init.Xavier()
filter_data = np.ones(filter_shape).astype(dtype)
bias_data = np.ones(bias_shape).astype(dtype)
initializer("weight", filter_data)
initializer("bias", bias_data)
params1 = {
"weight": tvm.nd.array(filter_data),
"bias": tvm.nd.array(bias_data),
}
build_run_compare(mod, params1, {"data": input_shape}, dtype, target)
@tvm.testing.requires_opencl
@tvm.testing.parametrize_targets("opencl -device=adreno")
def test_conv2d_inceptionv3_64x35x35_96x64x3x3_nopad(target, dtype):
input_shape = (1, 42, 42, 32)
filter_shape = (3, 3, 32, 96)
bias_shape = (1, 1, 1, 96)
A = relay.var("data", shape=input_shape, dtype=dtype)
B = relay.var("weight", shape=filter_shape, dtype=dtype)
bias = relay.var("bias", shape=bias_shape, dtype=dtype)
conv = relay.nn.conv2d(
A,
B,
data_layout="NHWC",
kernel_layout="HWIO",
padding=[0, 0, 0, 0],
strides=[2, 2],
out_dtype=dtype,
channels=96,
kernel_size=(3, 3),
)
D = relay.op.add(conv, bias)
D = relay.op.nn.relu(D)
mod = relay.Function([A, B, bias], D)
np.random.seed(0)
initializer = relay.testing.init.Xavier()
filter_data = np.zeros(filter_shape).astype(dtype)
bias_data = np.zeros(bias_shape).astype(dtype)
initializer("weight", filter_data)
initializer("bias", bias_data)
params1 = {
"weight": tvm.nd.array(filter_data),
"bias": tvm.nd.array(bias_data),
}
build_run_compare(mod, params1, {"data": input_shape}, dt |
ype, target, [], gpu_preprocess)
@tvm.testing.requires_opencl
@tvm.testing.parametrize_targets("opencl -device=adreno")
def test_conv2d_inceptionv3_64x35x35_96x64x3x3_nopad_pass(target, dtype):
input_shape = (1, 40, 40, 32)
filter_shape = (2, 2, 32, 96)
bias_shape = (1, 1, 1, 96)
A = relay.var("data", shape=input_shape, dtype=dtype)
B = relay.var("weight", shape=filter_shape, dtype=dtype)
bias = relay.var("bias", shape=bias_shape, dtype=dtype)
conv = relay.nn.conv2d(
A,
B,
data_layout="NHWC",
kernel_layout="HWIO",
padding=[0, 0, 0, 0],
strides=[2, 2],
out_dtype=dtype,
channels=96,
kernel_size=(2, 2),
)
D = relay.op.add(conv, bias)
D = relay.op.nn.relu(D)
mod = relay.Function([A, B, bias], D)
np.random.seed(0)
initializer = relay.testing.init.Xavier()
filter_data = np.zeros(filter_shape).astype(dtype)
bias_data = np.zeros(bias_shape).astype(dtype)
initializer("weight", filter_data)
initializer("bias", bias_data)
params1 = {
"weight": tvm.nd.array(filter_data),
"bias": tvm.nd.array(bias_data),
}
build_run_compare(mod, params1, {"data": input_shape}, dtype, target, [], gpu_preprocess)
@tvm.testing.requires_opencl
@tvm.testing.parametrize_targets("opencl -device=adreno")
def test_conv2d_inceptionv3_35_35_strides(target, dtype):
input_shape = (1, 35, 35, 48)
filter_shape = (5, 5, 48, 64)
bias_shape = (1, 1, 1, 64)
A = relay.var("data", shape=input_shape, dtype=dtype)
B = relay.var("weight", shape=filter_shape, dtype=dtype)
bias = relay.var("bias", shape=bias_shape, dtype=dtype)
conv = relay.nn.conv2d(
A,
B,
data_layout="NHWC",
kernel_layout="HWIO",
padding=[2, 2, 2, 2],
strides=[1, 1],
out_dtype=dtype,
channels=64,
kernel_size=(5, 5),
)
D = relay.op.add(conv, bias)
D = relay.op.nn.relu(D)
mod = relay.Function([A, B, bias |
], D)
np.random.seed(0)
initializer = relay.testing.init.Xavier()
filter_data = np.zeros(filter_shape).astype(dtype)
bias_data = np.zeros(bias_shape).astype(dtype)
initializer("weight", filter_data)
initializer("bias", bias_data)
params1 = {
"weight": tvm.nd.array(filter_data),
"bias": tvm.nd.array(bias_data),
}
build_run_compare(mod, params1, {"data": input_shape}, dtype, target, [], gpu_preprocess)
@tvm.testing.requires_opencl
@tvm.testing.parametrize_targets("opencl -device=adreno")
def test_conv2d_resnet50_v2_nhwc_3c(target, dtype):
input_shape = (1, 224, 224, 3)
filter_shape = (7, 7, 3, 64)
bias_shape = (1, 1, 1, 64)
A = relay.var("data", shape=input_shape, dtype=dtype)
B = relay.var("weight", shape=filter_shape, dtype=dtype)
bias = relay.var("bias", shape=bias_shape, dtype=dtype)
conv = relay.nn.conv2d(
A,
B,
data_layout="NHWC",
kernel_layout="HWIO",
padding=[3, 3, 3, 3],
strides=[2, 2],
out_dtype=dtype,
channels=64,
kernel_size=(7, 7),
)
D = relay.op.add(conv, bias)
D = relay.op.nn.relu(D)
mod = relay.Function([A, B, bias], D)
np.random.seed(1)
initializer = relay.testing.init.Xavier()
filter_data = np.zeros(filter_shape).astype(dtype)
bias_data = np.zeros(bias_shape).astype(dtype)
initializer("weight", filter_data)
initializer("bias", bias_data)
params1 = {
"weight": tvm.nd.array(filter_data),
"bias": tvm.nd.array(bias_data),
}
build_run_compare(mod, params1, {"data": input_shape}, dtype, target)
@tvm.testing.requires_opencl
@tvm.testing.parametrize_targets("opencl -device=adreno")
def test_conv2d_inceptionv3_nhwc_3c(target, dtype):
input_shape = (1, 299, 299, 3)
filter_shape = (3, 3, 3, 64)
bias_shape = (1, 1, 1, 64)
A = relay.var("data", shape=input_shape, dtype=dtype)
B = relay.var("weight", shape=filter_shape, dtype=dtype)
bias = relay.var("bia |
s", shape=bias_shape, dtype=dtype)
conv = relay.nn.conv2d(
A,
B,
data_layout="NHWC",
kernel_layout="HWIO",
padding=[0, 0, 0, 0],
strides=[2, 2],
out_dtype=dtype,
channels=64,
kernel_size=(3, 3),
)
D = relay.op.add(conv, bias)
D = relay.op.nn.relu(D)
mod = relay.Function([A, B, bias], D)
np.random.seed(0)
initializer = relay.testing.init.Xavier()
filter_data = np.zeros(filter_shape).astype(dtype)
bias_data = np.zeros(bias_shape).astype(dtype)
initializer("weight", filter_data)
initializer("bias", bias_data)
params1 = {
"weight": tvm.nd.array(filter_data),
"bias": tvm.nd.array(bias_data),
}
build_run_compare(mod, params1, {"data": input_shape}, dtype, target)
@tvm.testing.requires_opencl
@tvm.testing.parametrize_targets("opencl -device=adreno")
def test_conv2d_1x1_16c16spatial(target, dtype):
input_shape = (1, 128, 128, 16)
filter_shape = (4, 4, 16, 32)
bias_shape = (1, 1, 1, 32)
A = relay.var("data", shape=input_shape, dtype=dtype)
B = relay.var("weight", shape=filter_shape, dtype=dtype)
bias = relay.var("bias", shape=bias_shape, dtype=dtype)
conv = relay.nn.conv2d(
A,
B,
data_layout="NHWC",
kernel_layout="HWIO",
padding=[0, 0, 0, 0],
strides=[2, 2],
out_dtype=dtype,
channels=32,
kernel_size=(4, 4),
)
D = relay.op.add(conv, bias)
D = relay.op.nn.relu(D)
mod = relay.Function([A, B, bias], D)
np.random.seed(0)
initializer = relay.testing.init.Xavier()
filter_data = np.zeros(filter_shape).astype(dtype)
bias_data = np.zeros(bias_shape).astype(dtype)
initializer("weight", filter_data)
initializer("bias", bias_data)
params1 = {
"weight": tvm.nd.array(filter_data),
"bias": tvm.nd.array(bias_data),
}
build_run_compare(mod, params1, {"data": input_shape}, dtype, target)
@tvm.testing.requires_openc |
l
@tvm.testing.parametrize_targets("opencl -device=adreno")
def test_conv2d_4x4_16c16pad(target, dtype):
input_shape = (1, 256, 256, 32)
filter_shape = (4, 4, 32, 32)
bias_shape = (1, 1, 1, 32)
A = relay.var("data", shape=input_shape, dtype=dtype)
B = relay.var("weight", shape=filter_shape, dtype=dtype)
bias = relay.var("bias", shape=bias_shape, dtype=dtype)
conv = relay.nn.conv2d(
A,
B,
data_layout="NHWC",
kernel_layout="HWIO",
padding=[3, 3, 0, 0],
strides=[2, 2],
out_dtype=dtype,
channels=32,
kernel_size=(4, 4),
)
D = relay.op.add(conv, bias)
D = relay.op.nn.relu(D)
mod = relay.Function([A, B, bias], D)
np.random.seed(0)
initializer = relay.testing.init.Xavier()
filter_data = np.zeros(filter_shape).astype(dtype)
bias_data = np.zeros(bias_shape).astype(dtype)
initializer("weight", filter_data)
initializer("bias", bias_data)
params1 = {
"weight": tvm.nd.array(filter_data),
"bias": tvm.nd.array(bias_data),
}
build_run_compare(mod, params1, {"data": input_shape}, dtype, target)
@tvm.testing.requires_opencl
@tvm.testing.parametrize_targets("opencl -device=adreno")
def test_conv2d_4x4x4_16c16pad(target, dtype):
input_shape = (1, 256, 256, 32)
filter_shape = (4, 4, 32, 4)
bias_shape = (1, 1, 1, 4)
A = relay.var("data", shape=input_shape, dtype=dtype)
B = relay.var("weight", shape=filter_shape, dtype=dtype)
bias = relay.var("bias", shape=bias_shape, dtype=dtype)
conv = relay.nn.conv2d(
A,
B,
data_layout="NHWC",
kernel_layout="HWIO",
padding=[3, 3, 0, 0],
strides=[2, 2],
out_dtype=dtype,
channels=4,
kernel_size=(4, 4),
)
D = relay.op.add(conv, bias)
D = relay.op.nn.relu(D)
mod = relay.Function([A, B, bias], D)
np.random.seed(0)
initializer = relay.testing.init.Xavier()
filter_data = np.zeros(filter_shape).astype(dtyp |
e)
bias_data = np.zeros(bias_shape).astype(dtype)
initializer("weight", filter_data)
initializer("bias", bias_data)
params1 = {
"weight": tvm.nd.array(filter_data),
"bias": tvm.nd.array(bias_data),
}
build_run_compare(mod, params1, {"data": input_shape}, dtype, target)
@tvm.testing.requires_opencl
@tvm.testing.parametrize_targets("opencl -device=adreno")
def test_conv2d_yolov3_v2_nhwc_3c(target, dtype):
input_shape = (1, 13, 13, 1024)
filter_shape = (1, 1, 1024, 255)
A = relay.var("data", shape=input_shape, dtype=dtype)
B = relay.var("weight", shape=filter_shape, dtype=dtype)
conv = relay.nn.conv2d(
A,
B,
data_layout="NHWC",
kernel_layout="HWIO",
padding=[0, 0, 0, 0],
strides=[1, 1],
out_dtype=dtype,
channels=255,
kernel_size=(1, 1),
)
mod = relay.Function([A, B], conv)
np.random.seed(0)
initializer = relay.testing.init.Xavier()
filter_data = np.zeros(filter_shape).astype(dtype)
initializer("weight", filter_data)
params = {
"weight": tvm.nd.array(filter_data),
}
build_run_compare(mod, params, {"data": input_shape}, dtype, target)
@tvm.testing.requires_opencl
@tvm.testing.parametrize_targets("opencl -device=adreno")
def test_conv2d_vgg16_winograd_4d(target, dtype):
input_shape = (1, 28, 28, 512)
filter_shape = (3, 3, 512, 512)
bias_shape = (1, 1, 1, 512)
A = relay.var("data", shape=input_shape, dtype=dtype)
B = relay.var("weight", shape=filter_shape, dtype=dtype)
bias = relay.var("bias", shape=bias_shape, dtype=dtype)
conv = relay.nn.conv2d(
A,
B,
data_layout="NHWC",
kernel_layout="HWIO",
padding=[1, 1, 1, 1],
channels=512,
kernel_size=[3, 3],
out_dtype=dtype,
)
D = relay.op.add(conv, bias)
D = relay.op.nn.relu(D)
mod = relay.Function([A, B, bias], D)
np.random.seed(0)
initializer = relay.testing.init.Xavier() |
filter_data = np.zeros(filter_shape).astype(dtype)
bias_data = np.zeros(bias_shape).astype(dtype)
initializer("weight", filter_data)
initializer("bias", bias_data)
params1 = {
"weight": tvm.nd.array(filter_data),
"bias": tvm.nd.array(bias_data),
}
temp = utils.tempdir()
stat_file = temp.relpath("stat.log")
with open(stat_file, "w") as f:
f.write(
f'{{"input": ["opencl -keys=adreno,opencl,gpu -device=adreno -max_num_threads=256", "conv2d_nhwc_winograd.image2d", [["TENSOR", [1, 28, 28, 512], "{dtype}"], ["TENSOR", [3, 3, 512, 512], "{dtype}"], [1, 1], [1, 1, 1, 1], [1, 1], "{dtype}"], {{}}], "config": {{"index": 1591, "code_hash": null, "entity": [["auto_unroll_max_step", "ot", 4], ["tile_y", "sp", [-1, 1, 32]], ["tile_x", "sp", [-1, 4, 2]], ["tile_rc", "sp", [-1, 8]]]}}, "result": [[0.0037244], 0, 7.06374192237854, 1653898629.7427933], "version": 0.2, "tvm_version": "0.8.dev0"}}\n'
)
graph = build_run_compare(
mod, params1, {"data": input_shape}, dtype, target, stat_file=stat_file
)
matches = re.findall("winograd", graph)
assert len(matches) > 0
@tvm.testing.requires_opencl
@tvm.testing.parametrize_targets("opencl -device=adreno")
def test_conv2d_winograd_conv(target, dtype):
input_shape = (1, 3, 3, 4)
A = relay.var("data", shape=input_shape, dtype=dtype)
filter_shape3 = (3, 3, 4, 8)
bias_shape3 = (1, 1, 1, 8)
B3 = relay.var("weight3", shape=filter_shape3, dtype=dtype)
D = relay.nn.conv2d(
A,
B3,
data_layout="NHWC",
kernel_layout="HWIO",
padding=[1, 1, 1, 1],
channels=8,
kernel_size=[3, 3],
out_dtype=dtype,
)
filter_shape4 = (3, 3, 8, 8)
bias_shape4 = (1, 1, 1, 8)
B4 = relay.var("weight4", shape=filter_shape4, dtype=dtype)
D = relay.nn.conv2d(
D,
B4,
data_layout="NHWC",
kernel_layout="HWIO",
padding=[1, 1, 1, 1],
channels=8,
kernel_size=[3, 3], |
out_dtype=dtype,
)
mod = relay.Function([A, B3, B4], D)
np.random.seed(1)
initializer = relay.testing.init.Xavier()
filter_data3 = np.zeros(filter_shape3).astype(dtype)
bias_data3 = np.zeros(bias_shape3).astype(dtype)
filter_data4 = np.zeros(filter_shape4).astype(dtype)
bias_data4 = np.zeros(bias_shape4).astype(dtype)
initializer("weight", filter_data3)
initializer("bias", bias_data3)
initializer("weight", filter_data4)
initializer("bias", bias_data4)
params1 = {
"weight3": tvm.nd.array(filter_data3),
"weight4": tvm.nd.array(filter_data4),
}
temp = utils.tempdir()
stat_file = temp.relpath("stat.log")
with open(stat_file, "w") as f:
f.write(
f'{{"input": ["opencl -keys=adreno,opencl,gpu -device=adreno -max_num_threads=256", "conv2d_nhwc_winograd.image2d", [["TENSOR", [1, 3, 3, 4], "{dtype}"], ["TENSOR", [3, 3, 4, 8], "{dtype}"], [1, 1], [1, 1, 1, 1], [1, 1], "{dtype}"], {{}}], "config": {{"index": 1591, "code_hash": null, "entity": [["auto_unroll_max_step", "ot", 4], ["tile_y", "sp", [-1, 1, 32]], ["tile_x", "sp", [-1, 4, 2]], ["tile_rc", "sp", [-1, 8]]]}}, "result": [[0.0037244], 0, 7.06374192237854, 1653898629.7427933], "version": 0.2, "tvm_version": "0.8.dev0"}}\n'
)
graph = build_run_compare(
mod, params1, {"data": input_shape}, dtype, target, stat_file=stat_file
)
matches = re.findall("winograd", graph)
assert len(matches) > 0
@tvm.testing.requires_opencl
@tvm.testing.parametrize_targets("opencl -device=adreno")
def test_conv2d_winograd_non_rect(target, dtype):
input_shape = (1, 36, 64, 771)
A = relay.var("data", shape=input_shape, dtype=dtype)
filter_shape = (3, 3, 771, 128)
B = relay.var("weight", shape=filter_shape, dtype=dtype)
D = relay.nn.conv2d(
A,
B,
data_layout="NHWC",
kernel_layout="HWIO",
padding=[1, 1, 1, 1],
channels=128,
kernel_size=[3, 3],
out_dtype=dtype,
)
mod |
= relay.Function([A, B], D)
np.random.seed(1)
initializer = relay.testing.init.Xavier()
filter_data = np.zeros(filter_shape).astype(dtype)
initializer("weight", filter_data)
params1 = {
"weight": tvm.nd.array(filter_data),
}
temp = utils.tempdir()
stat_file = temp.relpath("stat.log")
with open(stat_file, "w") as f:
f.write(
f'{{"input": ["opencl -keys=adreno,opencl,gpu -device=adreno -max_num_threads=256 -texture_spatial_limit=16384 -thread_warp_size=1", "conv2d_nhwc_winograd.image2d", [["TENSOR", [1, 36, 64, 771], "{dtype}"], ["TENSOR", [3, 3, 771, 128], "{dtype}"], [1, 1], [1, 1, 1, 1], [1, 1], "{dtype}"], {{}}], "config": {{"index": 5399, "code_hash": null, "entity": [["auto_unroll_max_step", "ot", 16], ["tile_y", "sp", [-1, 1, 32]], ["tile_x", "sp", [-1, 4, 8]], ["tile_rc", "sp", [-1, 193]]]}}, "result": [[0.0037244], 0, 7.06374192237854, 1653898629.7427933], "version": 0.2, "tvm_version": "0.8.dev0"}}\n'
)
graph = build_run_compare(
mod, params1, {"data": input_shape}, dtype, target, stat_file=stat_file
)
matches = re.findall("winograd", graph)
assert len(matches) > 0 |
import os |
import tvm |
import numpy as np
from tvm |
import relay
from tvm.relay |
import testing
from utils.adreno_utils |
import gpu_preprocess, build_run_compare
dtype = tvm.testing.parameter("float32")
@tvm.testing.requires_opencl
@tvm.testing.parametrize_targets("opencl -device=adreno")
def test_depthwise_conv2d_bias_nchwc(target, dtype):
input_shape = (1, 64, 112, 112)
filter_shape = (64, 1, 3, 3)
bias_shape = (1, 64, 1, 1)
A = relay.var("data", shape=input_shape, dtype=dtype)
B = relay.var("weight", shape=filter_shape, dtype=dtype)
bias = relay.var("bias", shape=bias_shape, dtype=dtype)
conv = relay.nn.conv2d(
A,
B,
data_layout="NCHW",
kernel_layout="OIHW",
padding=[1, 1, 1, 1],
strides=[2, 2],
out_dtype=dtype,
channels=64,
groups=64,
kernel_size=(3, 3),
)
D = relay.op.add(conv, bias)
D = relay.op.nn.relu(D)
mod = relay.Function([A, B, bias], D)
np.random.seed(1)
initializer = relay.testing.init.Xavier()
filter_data = np.zeros(filter_shape).astype(dtype)
bias_data = np.zeros(bias_shape).astype(dtype)
initializer("weight", filter_data)
initializer("bias", bias_data)
params1 = {
"weight": tvm.nd.array(filter_data),
"bias": tvm.nd.array(bias_data),
}
build_run_compare(mod, params1, {"data": input_shape}, dtype, target, [], gpu_preprocess)
@tvm.testing.requires_opencl
@tvm.testing.parametrize_targets("opencl -device=adreno")
def test_depthwise_conv2d_nchwc(target, dtype):
input_shape = (1, 64, 112, 112)
filter_shape = (64, 1, 3, 3)
bias_shape = (1, 64, 1, 1)
A = relay.var("data", shape=input_shape, dtype=dtype)
B = relay.var("weight", shape=filter_shape, dtype=dtype)
conv = relay.nn.conv2d(
A,
B,
data_layout="NCHW",
kernel_layout="OIHW",
padding=[1, 1, 1, 1],
strides=[2, 2],
out_dtype=dtype,
channels=64,
groups=64,
kernel_size=(3, 3),
)
mod = relay.Function([A, B], conv)
np.random.seed(1)
initializer = relay.tes |
ting.init.Xavier()
filter_data = np.zeros(filter_shape).astype(dtype)
bias_data = np.zeros(bias_shape).astype(dtype)
initializer("weight", filter_data)
params1 = {
"weight": tvm.nd.array(filter_data),
}
build_run_compare(mod, params1, {"data": input_shape}, dtype, target, [], gpu_preprocess)
@tvm.testing.requires_opencl
@tvm.testing.parametrize_targets("opencl -device=adreno")
def test_depthwise_conv2d_bias_nchw(target, dtype):
input_shape = (1, 64, 112, 112)
filter_shape = (64, 1, 3, 3)
bias_shape = (1, 64, 1, 1)
A = relay.var("data", shape=input_shape, dtype=dtype)
B = relay.var("weight", shape=filter_shape, dtype=dtype)
bias = relay.var("bias", shape=bias_shape, dtype=dtype)
conv = relay.nn.conv2d(
A,
B,
data_layout="NCHW",
kernel_layout="OIHW",
padding=[1, 1, 1, 1],
strides=[2, 2],
out_dtype=dtype,
channels=64,
groups=64,
kernel_size=(3, 3),
)
D = relay.op.add(conv, bias)
D = relay.op.nn.relu(D)
mod = relay.Function([A, B, bias], D)
np.random.seed(1)
initializer = relay.testing.init.Xavier()
filter_data = np.zeros(filter_shape).astype(dtype)
bias_data = np.zeros(bias_shape).astype(dtype)
initializer("weight", filter_data)
initializer("bias", bias_data)
params1 = {
"weight": tvm.nd.array(filter_data),
"bias": tvm.nd.array(bias_data),
}
build_run_compare(mod, params1, {"data": input_shape}, dtype, target)
@tvm.testing.requires_opencl
@tvm.testing.parametrize_targets("opencl -device=adreno")
def test_depthwise_conv2d_repack_bias_nchw(target, dtype):
input_shape = (1, 63, 112, 112)
filter_shape = (63, 1, 3, 3)
bias_shape = (1, 63, 1, 1)
A = relay.var("data", shape=input_shape, dtype=dtype)
B = relay.var("weight", shape=filter_shape, dtype=dtype)
bias = relay.var("bias", shape=bias_shape, dtype=dtype)
conv = relay.nn.conv2d(
A,
B,
data |
_layout="NCHW",
kernel_layout="OIHW",
padding=[1, 1, 1, 1],
strides=[2, 2],
out_dtype=dtype,
channels=63,
groups=63,
kernel_size=(3, 3),
)
D = relay.op.add(conv, bias)
D = relay.op.nn.relu(D)
mod = relay.Function([A, B, bias], D)
np.random.seed(1)
initializer = relay.testing.init.Xavier()
filter_data = np.zeros(filter_shape).astype(dtype)
bias_data = np.zeros(bias_shape).astype(dtype)
initializer("weight", filter_data)
initializer("bias", bias_data)
params1 = {
"weight": tvm.nd.array(filter_data),
"bias": tvm.nd.array(bias_data),
}
build_run_compare(mod, params1, {"data": input_shape}, dtype, target) |
import os |
import tvm |
import numpy as np
from tvm |
import relay
from tvm.relay |
import testing
from utils.adreno_utils |
import build_run_compare
dtype = tvm.testing.parameter("float32")
@tvm.testing.requires_opencl
@tvm.testing.parametrize_targets("opencl -device=adreno")
def test_depthwise_conv2d_deeplabv3_1_129_129_144x3_3_144_1(target, dtype):
input_shape = (1, 129, 129, 144)
filter_shape = (3, 3, 144, 1)
kernel_size = (filter_shape[0], filter_shape[1])
bias_shape = (filter_shape[2],)
A = relay.var("data", shape=input_shape, dtype=dtype)
B = relay.var("weight", shape=filter_shape, dtype=dtype)
bias = relay.var("bias", shape=bias_shape, dtype=dtype)
conv = relay.nn.conv2d(
A,
B,
data_layout="NHWC",
kernel_layout="HWOI",
out_dtype=dtype,
groups=filter_shape[2],
channels=filter_shape[2],
kernel_size=kernel_size,
)
D = relay.op.add(conv, bias)
D = relay.op.nn.relu(D)
mod = relay.Function([A, B, bias], D)
mod = relay.Function([A, B, bias], conv)
np.random.seed(1)
initializer = relay.testing.init.Xavier()
filter_data = np.zeros(filter_shape).astype(dtype)
bias_data = np.zeros(bias_shape).astype(dtype)
initializer("weight", filter_data)
initializer("bias", bias_data)
params1 = {
"weight": tvm.nd.array(filter_data),
"bias": tvm.nd.array(bias_data),
}
build_run_compare(mod, params1, {"data": input_shape}, dtype, target)
@tvm.testing.requires_opencl
@tvm.testing.parametrize_targets("opencl -device=adreno")
def test_depthwise_conv2d_deeplabv3_4_35_35_576x3_3_576_1(target, dtype):
input_shape = (4, 35, 35, 576)
filter_shape = (3, 3, 576, 1)
kernel_size = (filter_shape[0], filter_shape[1])
bias_shape = (filter_shape[2],)
A = relay.var("data", shape=input_shape, dtype=dtype)
B = relay.var("weight", shape=filter_shape, dtype=dtype)
bias = relay.var("bias", shape=bias_shape, dtype=dtype)
conv = relay.nn.conv2d(
A,
B,
data_layout="NHWC",
kernel_layout="HWOI",
out_dtype=dtype,
groups=filter_sh |
ape[2],
channels=filter_shape[2],
kernel_size=kernel_size,
)
D = relay.op.add(conv, bias)
D = relay.op.nn.relu(D)
mod = relay.Function([A, B, bias], D)
mod = relay.Function([A, B, bias], conv)
np.random.seed(1)
initializer = relay.testing.init.Xavier()
filter_data = np.zeros(filter_shape).astype(dtype)
bias_data = np.zeros(bias_shape).astype(dtype)
initializer("weight", filter_data)
initializer("bias", bias_data)
params1 = {
"weight": tvm.nd.array(filter_data),
"bias": tvm.nd.array(bias_data),
}
build_run_compare(mod, params1, {"data": input_shape}, dtype, target)
@tvm.testing.requires_opencl
@tvm.testing.parametrize_targets("opencl -device=adreno")
def test_depthwise_conv2d_deeplabv3_1_129_129_144x3_3_144_1_with_padding(target, dtype):
input_shape = (1, 129, 129, 144)
filter_shape = (3, 3, 144, 1)
kernel_size = (filter_shape[0], filter_shape[1])
bias_shape = (filter_shape[2],)
A = relay.var("data", shape=input_shape, dtype=dtype)
B = relay.var("weight", shape=filter_shape, dtype=dtype)
bias = relay.var("bias", shape=bias_shape, dtype=dtype)
conv = relay.nn.conv2d(
A,
B,
data_layout="NHWC",
kernel_layout="HWOI",
padding=[3, 3, 3, 3],
strides=[2, 2],
out_dtype=dtype,
groups=filter_shape[2],
channels=filter_shape[2],
kernel_size=kernel_size,
)
D = relay.op.add(conv, bias)
D = relay.op.nn.relu(D)
mod = relay.Function([A, B, bias], D)
np.random.seed(1)
initializer = relay.testing.init.Xavier()
filter_data = np.zeros(filter_shape).astype(dtype)
bias_data = np.zeros(bias_shape).astype(dtype)
initializer("weight", filter_data)
initializer("bias", bias_data)
params1 = {
"weight": tvm.nd.array(filter_data),
"bias": tvm.nd.array(bias_data),
}
build_run_compare(mod, params1, {"data": input_shape}, dtype, target)
@tvm.testing.requires_opencl
@tvm.t |
esting.parametrize_targets("opencl -device=adreno")
def test_depthwise_conv2d_1_513_513_7x3_3_7_1(target, dtype):
input_shape = (1, 513, 513, 7)
filter_shape = (3, 3, 7, 1)
bias_shape = (filter_shape[2],)
kernel_size = (filter_shape[0], filter_shape[1])
A = relay.var("data", shape=input_shape, dtype=dtype)
B = relay.var("weight", shape=filter_shape, dtype=dtype)
bias = relay.var("bias", shape=bias_shape, dtype=dtype)
conv = relay.nn.conv2d(
A,
B,
data_layout="NHWC",
kernel_layout="HWOI",
out_dtype=dtype,
channels=filter_shape[2],
groups=filter_shape[2],
kernel_size=kernel_size,
)
D = relay.op.add(conv, bias)
D = relay.op.nn.relu(D)
mod = relay.Function([A, B, bias], D)
np.random.seed(1)
initializer = relay.testing.init.Xavier()
filter_data = np.ones(filter_shape).astype(dtype)
bias_data = np.ones(bias_shape).astype(dtype)
initializer("weight", filter_data)
initializer("bias", bias_data)
params1 = {
"weight": tvm.nd.array(filter_data),
"bias": tvm.nd.array(bias_data),
}
build_run_compare(mod, params1, {"data": input_shape}, dtype, target)
@tvm.testing.requires_opencl
@tvm.testing.parametrize_targets("opencl -device=adreno")
def test_depthwise_conv2d_1_513_513_3x3_3_3_1(target, dtype):
input_shape = (1, 513, 513, 3)
filter_shape = (3, 3, 3, 1)
bias_shape = (filter_shape[2],)
kernel_size = (filter_shape[0], filter_shape[1])
A = relay.var("data", shape=input_shape, dtype=dtype)
B = relay.var("weight", shape=filter_shape, dtype=dtype)
bias = relay.var("bias", shape=bias_shape, dtype=dtype)
conv = relay.nn.conv2d(
A,
B,
data_layout="NHWC",
kernel_layout="HWOI",
out_dtype=dtype,
channels=filter_shape[2],
groups=filter_shape[2],
kernel_size=kernel_size,
)
D = relay.op.add(conv, bias)
D = relay.op.nn.relu(D)
mod = relay.Function([A, B, bias], D |
)
np.random.seed(1)
initializer = relay.testing.init.Xavier()
filter_data = np.ones(filter_shape).astype(dtype)
bias_data = np.ones(bias_shape).astype(dtype)
initializer("weight", filter_data)
initializer("bias", bias_data)
params1 = {
"weight": tvm.nd.array(filter_data),
"bias": tvm.nd.array(bias_data),
}
build_run_compare(mod, params1, {"data": input_shape}, dtype, target) |
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import re
import tvm
import numpy as np
from tvm import relay
from tvm.relay import testing
from tvm.contrib import utils
from utils.adreno_utils import gpu_preprocess, build_run_compare
dtype = tvm.testing.parameter("float32")
@tvm.testing.requires_opencl
@tvm.testing.parametrize_targets("opencl -device=adreno")
def test_mean(target, dtype):
# NCHW
input_shape = (1, 3, 720, 1280)
A = relay.var("data", shape=input_shape, dtype=dtype)
mean = relay.mean(A, axis=1, keepdims=True)
mod = relay.Function([A], mean)
build_run_compare(mod, {}, {"data": input_shape}, dtype, target)
@tvm.testing.requires_opencl
@tvm.testing.parametrize_targets("opencl -device=adreno")
def test_argmax(target, dtype):
# NCHW
input_shape = (1, 3, 720, 1280)
A = relay.var("data", shape=input_shape, dtype=dtype)
argmax = relay.op.argmax(A, axis=[1])
mod = relay.Function([A], argmax)
build_run_compare(mod, {}, {"data": input_shape}, dtype, target)
|
"""Utils for adreno compute/schedules""" |
import os |
import tvm |
import numpy as np
from tvm |
import relay
from tvm |
import autotvm
from tvm.relay |
import testing
from tvm.relay.transform |
import recast
from tvm.contrib |
import graph_runtime |
import json
def get_cpu_reference(mod, params1, input_shape, inputs):
mod_fp32 = recast(mod, "float32", "float32", ops=["nn.conv2d", "add", "nn.relu"])
with relay.build_config(opt_level=3):
graph, lib, params = relay.build(mod_fp32, "llvm", params=params1)
ctx = tvm.cpu()
m = graph_runtime.create(graph, lib, ctx)
if isinstance(input_shape, dict):
for key in input_shape:
m.set_input(key, inputs[-1])
else:
m.set_input("data", inputs[-1])
m.set_input(**params)
m.run()
return [
m.get_output(0).asnumpy(),
]
def build_run_compare(
tvm_mod,
params1,
input_shape,
dtype="float32",
target="llvm",
static_mem_scopes=[],
gpu_preprocess=None,
stat_file=None,
):
if "TVM_TRACKER_HOST" in os.environ and "TVM_TRACKER_PORT" in os.environ:
rpc_tracker_host = os.environ["TVM_TRACKER_HOST"]
rpc_tracker_port = os.environ["TVM_TRACKER_PORT"]
run_on_host = 0
target_host = "llvm -mtriple=arm64-linux-android"
rpc_tracker_port = int(rpc_tracker_port)
else:
run_on_host = 1
target_host = "llvm"
if gpu_preprocess:
tvm_mod_nchwc = gpu_preprocess(tvm_mod)
else:
tvm_mod_nchwc = tvm_mod
if stat_file is not None:
with autotvm.apply_history_best(stat_file):
with tvm.transform.PassContext(opt_level=3):
graph, lib, params = relay.build(
tvm_mod_nchwc, target_host=target_host, target=target, params=params1
)
else:
with tvm.transform.PassContext(opt_level=3):
graph, lib, params = relay.build(
tvm_mod_nchwc, target_host=target_host, target=target, params=params1
)
graph_json = json.loads(graph)
if "storage_scope" in graph_json["attrs"]:
assert (
len(static_mem_scopes) == len(graph_json["attrs"]["storage_scope"][1])
or len(static_mem_scopes) == 0
)
else:
as |
sert len(static_mem_scopes) == 0
for i in range(0, len(static_mem_scopes)):
assert static_mem_scopes[i] == graph_json["attrs"]["storage_scope"][1][i]
if run_on_host:
ctx = tvm.opencl()
m = graph_runtime.create(graph, lib, ctx)
else:
from tvm |
import rpc
from tvm.contrib |
import utils, ndk
rpc_key = "android"
tracker = rpc.connect_tracker(rpc_tracker_host, rpc_tracker_port)
remote = tracker.request(rpc_key, priority=0, session_timeout=600)
temp = utils.tempdir()
dso_binary = "dev_lib_cl.so"
dso_binary_path = temp.relpath(dso_binary)
ctx = remote.cl(0)
lib.export_library(dso_binary_path, ndk.create_shared)
remote.upload(dso_binary_path)
rlib = remote.load_module(dso_binary)
m = graph_runtime.create(graph, rlib, ctx)
m.set_input(**params)
inputs = []
if isinstance(input_shape, dict):
for key in input_shape:
inputs.append(np.random.normal(size=input_shape[key]).astype(dtype))
m.set_input(key, inputs[-1])
else:
inputs.append(np.random.normal(size=input_shape).astype(dtype))
m.set_input("data", inputs[-1])
m.run()
ref_outputs = get_cpu_reference(tvm_mod, params1, input_shape, inputs)
for i, ref_output in enumerate(ref_outputs):
tvm_output = m.get_output(i)
output = tvm_output.asnumpy()
np.testing.assert_allclose(output, ref_output, rtol=1e-1, atol=1e-1)
return graph
def gpu_preprocess(tvm_mod):
layout_config = relay.transform.LayoutConfig()
desired_layouts = {"nn.conv2d": ["NCHW4c", "OIHW4o"]}
with layout_config:
seq = tvm.transform.Sequential([relay.transform.ConvertLayout(desired_layouts)])
with tvm.transform.PassContext(opt_level=3):
mod = tvm.IRModule.from_expr(tvm_mod)
tvm_mod_nchwc = seq(mod)
return tvm_mod_nchwc |
from typing |
import Callable |
import numpy as np
from tvm |
import relay
from tvm.relay.qnn.op |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.