diff --git a/tests/lint/pylint.sh b/tests/lint/pylint.sh index 997afd1c00c1..2228e110c15e 100755 --- a/tests/lint/pylint.sh +++ b/tests/lint/pylint.sh @@ -40,3 +40,16 @@ python3 -m pylint tests/python/contrib/test_hexagon/test_models.py --rcfile="$(d python3 -m pylint tests/python/contrib/test_hexagon/test_run_unit_tests.py --rcfile="$(dirname "$0")"/pylintrc python3 -m pylint tests/python/contrib/test_hexagon/test_thread_pool.py --rcfile="$(dirname "$0")"/pylintrc python3 -m pylint tests/python/contrib/test_hexagon/test_usmp.py --rcfile="$(dirname "$0")"/pylintrc + +# tests/python/frontend tests +python3 -m pylint tests/python/frontend/caffe/test_forward.py --rcfile="$(dirname "$0")"/pylintrc +python3 -m pylint tests/python/frontend/caffe2/*.py --rcfile="$(dirname "$0")"/pylintrc +python3 -m pylint tests/python/frontend/darknet/test_forward.py --rcfile="$(dirname "$0")"/pylintrc +python3 -m pylint tests/python/frontend/coreml/*.py --rcfile="$(dirname "$0")"/pylintrc +python3 -m pylint tests/python/frontend/keras/test_forward.py --rcfile="$(dirname "$0")"/pylintrc +python3 -m pylint tests/python/frontend/darknet/test_forward.py --rcfile="$(dirname "$0")"/pylintrc +python3 -m pylint tests/python/frontend/oneflow/*.py --rcfile="$(dirname "$0")"/pylintrc +python3 -m pylint tests/python/frontend/tensorflow/test_forward.py --rcfile="$(dirname "$0")"/pylintrc +python3 -m pylint tests/python/frontend/pytorch/test_forward.py --rcfile="$(dirname "$0")"/pylintrc +python3 -m pylint tests/python/frontend/tflite/test_forward.py --rcfile="$(dirname "$0")"/pylintrc + diff --git a/tests/python/frontend/caffe/test_forward.py b/tests/python/frontend/caffe/test_forward.py index 0e94148b3d06..21e4c9d03181 100644 --- a/tests/python/frontend/caffe/test_forward.py +++ b/tests/python/frontend/caffe/test_forward.py @@ -14,21 +14,16 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. -# pylint: disable=import-self, invalid-name, unused-argument +# pylint: disable=import-self, invalid-name, unused-argument, unspecified-encoding """ Caffe testcases ==================== This article is a test script to test Caffe operator with Relay. """ import os - -os.environ["GLOG_minloglevel"] = "2" -import sys import logging - -logging.basicConfig(level=logging.ERROR) - import numpy as np + from google.protobuf import text_format import caffe from caffe import layers as L, params as P @@ -37,9 +32,13 @@ import tvm import tvm.testing from tvm import relay -from tvm.contrib import utils, graph_executor +from tvm.contrib import graph_executor from tvm.contrib.download import download_testdata +os.environ["GLOG_minloglevel"] = "2" + +logging.basicConfig(level=logging.ERROR) + CURRENT_DIR = os.path.join(os.path.expanduser("~"), ".tvm_test_data", "caffe_test") ####################################################################### @@ -57,7 +56,8 @@ def _list_to_str(ll): """Convert list or tuple to str, separated by underline.""" if isinstance(ll, (tuple, list)): tmp = [str(i) for i in ll] - return "_".join(tmp) + res = "_".join(tmp) + return res def _gen_filename_str(op_name, data_shape, *args, **kwargs): @@ -137,8 +137,8 @@ def _miso_op(data_list, func, *args, **kwargs): """Create multi input and single output Caffe op""" n = caffe.NetSpec() if not isinstance(data_list, (tuple, list)): - raise TypeError("Need tuple or list but get {}".format(type(data_list))) - input_list = list() + raise TypeError(f"Need tuple or list but get {type(data_list)}") + input_list = [] for idx, data in enumerate(data_list): n["data" + str(idx)] = L.Input(input_param={"shape": {"dim": list(data.shape)}}) input_list.append(n["data" + str(idx)]) @@ -166,7 +166,7 @@ def _run_caffe(data, proto_file, blob_file): net.blobs["data"].data[...] = data out = net.forward() - caffe_output = list() + caffe_output = [] for i in range(len(out.keys())): if "output" + str(i) not in out.keys(): caffe_output.clear() @@ -187,8 +187,8 @@ def _run_tvm(data, proto_file, blob_file): with open(blob_file, "rb") as f: init_net.ParseFromString(f.read()) - shape_dict = dict() - dtype_dict = dict() + shape_dict = {} + dtype_dict = {} if isinstance(data, (tuple, list)): for idx, d in enumerate(data): shape_dict["data" + str(idx)] = d.shape @@ -213,7 +213,7 @@ def _run_tvm(data, proto_file, blob_file): m.set_input("data", tvm.nd.array(data.astype(dtype))) # execute m.run() - tvm_output = list() + tvm_output = [] # get outputs for i in range(m.get_num_outputs()): tvm_output.append(m.get_output(i).numpy()) @@ -221,7 +221,7 @@ def _run_tvm(data, proto_file, blob_file): def _compare_caffe_tvm(caffe_out, tvm_out, is_network=False): - for i in range(len(caffe_out)): + for i, _ in enumerate(caffe_out): if is_network: caffe_out[i] = caffe_out[i][:1] tvm.testing.assert_allclose(caffe_out[i], tvm_out[i], rtol=1e-5, atol=1e-5) @@ -229,14 +229,14 @@ def _compare_caffe_tvm(caffe_out, tvm_out, is_network=False): def _test_op(data, func_op, op_name, **kwargs): """Single op testing pipline.""" - shape_list = list() + shape_list = [] if isinstance(data, (list, tuple)): n = _miso_op(data, func_op, **kwargs) for d in data: shape_list.extend(list(d.shape)) else: output_num = 1 - if "ntop" in kwargs.keys(): + if "ntop" in kwargs: output_num = kwargs["ntop"] if output_num == 1: n = _siso_op(data, func_op, **kwargs) @@ -961,8 +961,9 @@ def _test_embed(data, **kwargs): def test_forward_Embed(): + """Embed""" k = 20 - data = [i for i in range(k)] + data = list(i for i in range(k)) np.random.shuffle(data) # dimension is 1 data = np.asarray(data) @@ -1083,7 +1084,7 @@ def _test_alexnet(data): data_process = data_process.astype(np.float32) proto_file_url = ( - "https://github.com/BVLC/caffe/raw/master/models/" "bvlc_alexnet/deploy.prototxt" + "https://github.com/BVLC/caffe/raw/master/models/" + "bvlc_alexnet/deploy.prototxt" ) blob_file_url = "http://dl.caffe.berkeleyvision.org/bvlc_alexnet.caffemodel" proto_file = download_testdata(proto_file_url, "alexnet.prototxt", module="model") @@ -1144,7 +1145,7 @@ def _test_inceptionv1(data): data_process = data_process.astype(np.float32) proto_file_url = ( - "https://github.com/BVLC/caffe/raw/master/models" "/bvlc_googlenet/deploy.prototxt" + "https://github.com/BVLC/caffe/raw/master/models" + "/bvlc_googlenet/deploy.prototxt" ) blob_file_url = "http://dl.caffe.berkeleyvision.org/bvlc_googlenet.caffemodel" proto_file = download_testdata(proto_file_url, "inceptionv1.prototxt", module="model") @@ -1159,38 +1160,4 @@ def test_forward_Inceptionv1(): if __name__ == "__main__": - # NN - test_forward_Convolution() - test_forward_Deconvolution() - test_forward_Dropout() - test_forward_LRN() - test_forward_Pooling() - test_forward_Scale() - test_forward_InnerProduct() - test_forward_BatchNorm() - - # Elemwise - test_forward_Eltwise() - - # Activation - test_forward_PReLU() - test_forward_ReLU() - test_forward_Sigmoid() - test_forward_Softmax() - test_forward_TanH() - - # Reshape - test_forward_Reshape() - test_forward_Flatten() - test_forward_Reduction() - - # Math - test_forward_Concat() - test_forward_Crop() - test_forward_Slice() - - # End to End - test_forward_Mobilenetv2() - test_forward_Alexnet() - test_forward_Resnet50() - test_forward_Inceptionv1() + tvm.testing.main() diff --git a/tests/python/frontend/caffe2/model_zoo/__init__.py b/tests/python/frontend/caffe2/model_zoo/__init__.py index 0d2935868872..946367f9ed4f 100644 --- a/tests/python/frontend/caffe2/model_zoo/__init__.py +++ b/tests/python/frontend/caffe2/model_zoo/__init__.py @@ -20,8 +20,8 @@ import os import sys import importlib -from . import squeezenet from caffe2.python.models.download import ModelDownloader +from . import squeezenet models = [ "squeezenet", diff --git a/tests/python/frontend/caffe2/model_zoo/squeezenet.py b/tests/python/frontend/caffe2/model_zoo/squeezenet.py index 5777656a251e..06e99567e5a8 100644 --- a/tests/python/frontend/caffe2/model_zoo/squeezenet.py +++ b/tests/python/frontend/caffe2/model_zoo/squeezenet.py @@ -31,10 +31,10 @@ # Helpers def _make_fire(net, squeeze_channels, expand1x1_channels, expand3x3_channels, prefix=""): - net = _make_fire_conv(net, squeeze_channels, 1, 0, "%s/squeeze1x1" % prefix) + net = _make_fire_conv(net, squeeze_channels, 1, 0, f"{prefix}/squeeze1x1") - left = _make_fire_conv(net, expand1x1_channels, 1, 0, "%s/expand1x1" % prefix) - right = _make_fire_conv(net, expand3x3_channels, 3, 1, "%s/expand3x3" % prefix) + left = _make_fire_conv(net, expand1x1_channels, 1, 0, f"{prefix}/expand1x1") + right = _make_fire_conv(net, expand3x3_channels, 3, 1, f"{prefix}/expand3x3") # NOTE : Assume NCHW layout here net = relay.concatenate((left, right), axis=1) return net @@ -43,12 +43,12 @@ def _make_fire(net, squeeze_channels, expand1x1_channels, expand3x3_channels, pr def _make_fire_conv(net, channels, kernel_size, padding=0, prefix=""): net = relay.nn.conv2d( net, - relay.var("%s_weight" % prefix), + relay.var(f"{prefix}_weight"), channels=channels, kernel_size=(kernel_size, kernel_size), padding=(padding, padding), ) - net = relay.nn.bias_add(net, relay.var("%s_bias" % prefix)) + net = relay.nn.bias_add(net, relay.var(f"{prefix}_bias")) net = relay.nn.relu(net) return net diff --git a/tests/python/frontend/caffe2/test_forward.py b/tests/python/frontend/caffe2/test_forward.py index 3d7ad230dec0..9758d937c254 100644 --- a/tests/python/frontend/caffe2/test_forward.py +++ b/tests/python/frontend/caffe2/test_forward.py @@ -14,15 +14,21 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. +""" +Caffe2 testcases +==================== +This article is a test script to test Caffe2 operator with Relay. +""" +from collections import namedtuple import numpy as np + +from caffe2.python import workspace, core +from caffe2.proto import caffe2_pb2 +from model_zoo import c2_squeezenet, c2_resnet50, c2_vgg19 import tvm -from tvm import te from tvm.contrib import graph_executor from tvm import relay -from model_zoo import c2_squeezenet, c2_resnet50, c2_vgg19 -from caffe2.python import workspace, core -from caffe2.proto import caffe2_pb2 -from collections import namedtuple + import tvm.testing @@ -103,6 +109,7 @@ def test_forward_vgg19(): @tvm.testing.uses_gpu def test_elementwise_add(): + """Elewise_add""" data_shape = (1, 16, 9, 9) init_net = caffe2_pb2.NetDef() init_net.name = "test_init_net" @@ -146,6 +153,7 @@ def test_elementwise_add(): @tvm.testing.uses_gpu def test_elementwise_add_with_broadcast(): + """Elewise_add_with_broadcast""" data_shape = (1, 16, 9, 9) init_net = caffe2_pb2.NetDef() init_net.name = "test_init_net" @@ -190,6 +198,7 @@ def test_elementwise_add_with_broadcast(): @tvm.testing.uses_gpu def test_normalize_yuv(): + """Normalize_yuv""" data_shape = (1, 3, 96, 96) init_net = caffe2_pb2.NetDef() init_net.name = "test_init_net" @@ -245,9 +254,4 @@ def test_normalize_yuv(): if __name__ == "__main__": - test_forward_squeezenet1_1() - test_forward_resnet50() - test_forward_vgg19() - test_elementwise_add() - test_elementwise_add_with_broadcast() - test_normalize_yuv() + tvm.testing.main() diff --git a/tests/python/frontend/coreml/model_zoo/__init__.py b/tests/python/frontend/coreml/model_zoo/__init__.py index bf270b8dff75..ea2f3478fde4 100644 --- a/tests/python/frontend/coreml/model_zoo/__init__.py +++ b/tests/python/frontend/coreml/model_zoo/__init__.py @@ -14,7 +14,7 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. - +"""coreml model zoo for testing purposes.""" import os from PIL import Image import numpy as np @@ -36,7 +36,11 @@ def get_resnet50(): def get_cat_image(): - url = "https://gist.githubusercontent.com/zhreshold/bcda4716699ac97ea44f791c24310193/raw/fa7ef0e9c9a5daea686d6473a62aacd1a5885849/cat.png" + """Get cat image""" + url = ( + "https://gist.githubusercontent.com/zhreshold/" + + "bcda4716699ac97ea44f791c24310193/raw/fa7ef0e9c9a5daea686d6473a62aacd1a5885849/cat.png" + ) dst = "cat.png" real_dst = download_testdata(url, dst, module="data") img = Image.open(real_dst).resize((224, 224)) diff --git a/tests/python/frontend/coreml/test_forward.py b/tests/python/frontend/coreml/test_forward.py index 8892018a79e9..75879f3e582a 100644 --- a/tests/python/frontend/coreml/test_forward.py +++ b/tests/python/frontend/coreml/test_forward.py @@ -14,32 +14,34 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. +""" +CoreML testcases +==================== +This article is a test script to test CoreML operator with Relay. +""" +from os import path +from enum import Enum +import tempfile import numpy as np -from coremltools.models.neural_network import NeuralNetworkBuilder -from coremltools.models import datatypes - import tvm -from tvm import te -from tvm.contrib import graph_executor -from tvm import topi import tvm.topi.testing -from tvm import relay +import tvm.testing +from tvm.contrib import graph_executor from tvm.topi.testing import conv2d_nchw_python +from tvm import relay +import model_zoo import coremltools as cm -import model_zoo -import tvm.testing -import tempfile -from os import path -import tvm -import tvm.relay as relay -import tensorflow.keras as keras +from coremltools.models.neural_network import NeuralNetworkBuilder +from coremltools.models import datatypes +from tensorflow import keras def get_tvm_output( func, x, params, target, device, out_shape=(1, 1000), input_name="image", dtype="float32" ): + """Generic function to execute and get tvm output""" with tvm.transform.PassContext(opt_level=3): lib = relay.build(func, target, params=params) m = graph_executor.GraphModule(lib["default"](device)) @@ -82,9 +84,9 @@ def run_tvm_graph( if isinstance(input_data, list): shape_dict = {} dtype_dict = {} - for i, e in enumerate(input_name): - shape_dict[e] = input_data[i].shape - dtype_dict[e] = input_data[i].dtype + for i, inp in enumerate(input_name): + shape_dict[inp] = input_data[i].shape + dtype_dict[inp] = input_data[i].dtype else: shape_dict = {input_name: input_data.shape} dtype_dict = {input_name: input_data.dtype} @@ -93,13 +95,11 @@ def run_tvm_graph( with tvm.transform.PassContext(opt_level=3): lib = relay.build(mod, target, params=params) - from tvm.contrib import graph_executor - m = graph_executor.GraphModule(lib["default"](device)) # set inputs if isinstance(input_data, list): - for i, e in enumerate(input_name): - m.set_input(e, tvm.nd.array(input_data[i].astype(input_data[i].dtype))) + for i, inp in enumerate(input_name): + m.set_input(inp, tvm.nd.array(input_data[i].astype(input_data[i].dtype))) else: m.set_input(input_name, tvm.nd.array(input_data.astype(input_data.dtype))) @@ -120,7 +120,8 @@ def run_tvm_graph( return tvm_output.numpy() -def verify_AddLayerParams(input_dim, alpha=2): +def verify_add_layer_params(input_dim, alpha=2): + """Verify add layer params""" dtype = "float32" a_np1 = np.random.uniform(size=input_dim).astype(dtype) @@ -142,13 +143,14 @@ def verify_AddLayerParams(input_dim, alpha=2): @tvm.testing.uses_gpu -def test_forward_AddLayerParams(): - verify_AddLayerParams((1, 2, 2), 0) - verify_AddLayerParams((1, 2, 2), 1) - verify_AddLayerParams((1, 3, 3), 2) +def test_forward_add_layer_params(): + verify_add_layer_params((1, 2, 2), 0) + verify_add_layer_params((1, 2, 2), 1) + verify_add_layer_params((1, 3, 3), 2) -def verify_MultiplyLayerParams(input_dim, alpha): +def verify_multiply_layer_params(input_dim, alpha): + """Verify multiply layer params""" dtype = "float32" a_np1 = np.random.uniform(size=input_dim).astype(dtype) @@ -174,13 +176,14 @@ def verify_MultiplyLayerParams(input_dim, alpha): @tvm.testing.uses_gpu -def test_forward_MultiplyLayerParams(): - verify_MultiplyLayerParams((1, 2, 2), 0) - verify_MultiplyLayerParams((1, 2, 2), 1) - verify_MultiplyLayerParams((1, 3, 3), 2) +def test_forward_multiply_layer_params(): + verify_multiply_layer_params((1, 2, 2), 0) + verify_multiply_layer_params((1, 2, 2), 1) + verify_multiply_layer_params((1, 3, 3), 2) -def verify_ConcatLayerParams(input1_dim, input2_dim): +def verify_concat_layer_params(input1_dim, input2_dim): + """Verify concat layer params""" dtype = "float32" a_np1 = np.random.uniform(size=input1_dim).astype(dtype) @@ -188,7 +191,7 @@ def verify_ConcatLayerParams(input1_dim, input2_dim): b_np = np.concatenate((a_np1, a_np2), axis=1) inputs = [("input1", datatypes.Array(*input1_dim)), ("input2", datatypes.Array(*input2_dim))] - output = [("output", datatypes.Array(*b_np.shape))] + output = [("output", datatypes.Array(*b_np.shape))] # pylint:disable=not-an-iterable builder = NeuralNetworkBuilder(inputs, output) builder.add_elementwise( name="Concate", input_names=["input1", "input2"], output_name="output", mode="CONCAT" @@ -202,12 +205,12 @@ def verify_ConcatLayerParams(input1_dim, input2_dim): @tvm.testing.uses_gpu -def test_forward_ConcatLayerParams(): - verify_ConcatLayerParams((1, 1, 2, 2), (1, 2, 2, 2)) - verify_ConcatLayerParams((1, 2, 4, 4), (1, 3, 4, 4)) +def test_forward_concat_layer_params(): + verify_concat_layer_params((1, 1, 2, 2), (1, 2, 2, 2)) + verify_concat_layer_params((1, 2, 4, 4), (1, 3, 4, 4)) -def verify_UpsampleLayerParams(input_dim, scale, mode): +def _verify_upsample_layer_params(input_dim, scale, mode): dtype = "float32" a_np = np.full(input_dim, 1, dtype=dtype) @@ -221,9 +224,9 @@ def verify_UpsampleLayerParams(input_dim, scale, mode): b_np = tvm.topi.testing.resize2d_python(a_np, (scale, scale), "NCHW", method, coord_trans) - input = [("input", datatypes.Array(*input_dim))] + input_data = [("input", datatypes.Array(*input_dim))] output = [("output", datatypes.Array(*b_np.shape))] - builder = NeuralNetworkBuilder(input, output) + builder = NeuralNetworkBuilder(input_data, output) builder.add_upsample( name="Upsample", scaling_factor_h=scale, @@ -240,20 +243,21 @@ def verify_UpsampleLayerParams(input_dim, scale, mode): @tvm.testing.uses_gpu -def test_forward_UpsampleLayerParams(): - verify_UpsampleLayerParams((1, 16, 32, 32), 2, "NN") - verify_UpsampleLayerParams((1, 4, 6, 6), 3, "BILINEAR") +def test_forward_upsample_layer_params(): + """Upsample Layer Params""" + _verify_upsample_layer_params((1, 16, 32, 32), 2, "NN") + _verify_upsample_layer_params((1, 4, 6, 6), 3, "BILINEAR") -def verify_l2_normalize(input_dim, eps): +def _verify_l2_normalize(input_dim, eps): dtype = "float32" a_np = np.random.uniform(size=input_dim).astype(dtype) b_np = tvm.topi.testing.l2_normalize_python(a_np, eps, 1) - input = [("input", datatypes.Array(*input_dim))] + input_data = [("input", datatypes.Array(*input_dim))] output = [("output", datatypes.Array(*b_np.shape))] - builder = NeuralNetworkBuilder(input, output) + builder = NeuralNetworkBuilder(input_data, output) builder.add_l2_normalize(name="L2", epsilon=eps, input_name="input", output_name="output") model = cm.models.MLModel(builder.spec) @@ -264,18 +268,18 @@ def verify_l2_normalize(input_dim, eps): @tvm.testing.uses_gpu def test_forward_l2_normalize(): - verify_l2_normalize((1, 3, 20, 20), 0.001) + _verify_l2_normalize((1, 3, 20, 20), 0.001) -def verify_lrn(input_dim, size, bias, alpha, beta): +def _verify_lrn(input_dim, size, bias, alpha, beta): dtype = "float32" axis = 1 a_np = np.random.uniform(size=input_dim).astype(dtype) b_np = tvm.topi.testing.lrn_python(a_np, size, axis, bias, alpha, beta) - input = [("input", datatypes.Array(*input_dim))] + input_data = [("input", datatypes.Array(*input_dim))] output = [("output", datatypes.Array(*b_np.shape))] - builder = NeuralNetworkBuilder(input, output) + builder = NeuralNetworkBuilder(input_data, output) builder.add_lrn( name="LRN", input_name="input", @@ -294,10 +298,10 @@ def verify_lrn(input_dim, size, bias, alpha, beta): @tvm.testing.uses_gpu def test_forward_lrn(): - verify_lrn((1, 3, 10, 20), 3, 1.0, 1.0, 0.5) + _verify_lrn((1, 3, 10, 20), 3, 1.0, 1.0, 0.5) -def verify_average(input_dim1, input_dim2, axis=0): +def _verify_average(input_dim1, input_dim2, axis=0): dtype = "float32" a_np1 = np.random.uniform(size=input_dim1).astype(dtype) @@ -321,12 +325,12 @@ def verify_average(input_dim1, input_dim2, axis=0): @tvm.testing.uses_gpu def test_forward_average(): - verify_average((1, 3, 20, 20), (1, 3, 20, 20)) - verify_average((3, 20, 20), (1, 3, 20, 20)) - verify_average((20, 20), (1, 3, 20, 20)) + _verify_average((1, 3, 20, 20), (1, 3, 20, 20)) + _verify_average((3, 20, 20), (1, 3, 20, 20)) + _verify_average((20, 20), (1, 3, 20, 20)) -def verify_max(input_dim): +def _verify_max(input_dim): dtype = "float32" a_np1 = np.random.uniform(size=input_dim).astype(dtype) @@ -361,11 +365,11 @@ def verify_max(input_dim): @tvm.testing.uses_gpu def test_forward_max(): - verify_max((1, 3, 20, 20)) - verify_max((20, 20)) + _verify_max((1, 3, 20, 20)) + _verify_max((20, 20)) -def verify_min(input_dim): +def _verify_min(input_dim): dtype = "float32" a_np1 = np.random.uniform(size=input_dim).astype(dtype) @@ -400,11 +404,12 @@ def verify_min(input_dim): @tvm.testing.uses_gpu def test_forward_min(): - verify_min((1, 3, 20, 20)) - verify_min((20, 20)) + _verify_min((1, 3, 20, 20)) + _verify_min((20, 20)) def verify_unary_sqrt(input_dim): + """Verify unary sqrt""" dtype = "float32" a_np = np.random.uniform(size=input_dim).astype(dtype) @@ -422,6 +427,7 @@ def verify_unary_sqrt(input_dim): def verify_unary_rsqrt(input_dim, epsilon=0): + """Verify unary rsqrt""" dtype = "float32" a_np = np.random.uniform(size=input_dim).astype(dtype) @@ -441,6 +447,7 @@ def verify_unary_rsqrt(input_dim, epsilon=0): def verify_unary_inverse(input_dim, epsilon=0): + """Verify unary inverse""" dtype = "float32" a_np = np.random.uniform(size=input_dim).astype(dtype) @@ -460,6 +467,7 @@ def verify_unary_inverse(input_dim, epsilon=0): def verify_unary_power(input_dim, alpha): + """Verify unary power""" dtype = "float32" a_np = np.random.uniform(size=input_dim).astype(dtype) @@ -479,6 +487,7 @@ def verify_unary_power(input_dim, alpha): def verify_unary_exp(input_dim): + """Verify unary exp""" dtype = "float32" a_np = np.random.uniform(size=input_dim).astype(dtype) @@ -496,6 +505,7 @@ def verify_unary_exp(input_dim): def verify_unary_log(input_dim): + """Verify unary log""" dtype = "float32" a_np = np.random.uniform(size=input_dim).astype(dtype) @@ -513,6 +523,7 @@ def verify_unary_log(input_dim): def verify_unary_abs(input_dim): + """Verify unary abs""" dtype = "float32" a_np = np.random.uniform(-100.0, 100.0, size=input_dim).astype(dtype) @@ -530,6 +541,7 @@ def verify_unary_abs(input_dim): def verify_unary_threshold(input_dim, alpha): + """Verify unary threshold""" dtype = "float32" a_np = np.random.uniform(-100.0, 100.0, size=input_dim).astype(dtype) @@ -550,6 +562,7 @@ def verify_unary_threshold(input_dim, alpha): @tvm.testing.uses_gpu def test_forward_unary(): + """All unary""" verify_unary_sqrt((1, 3, 20, 20)) verify_unary_rsqrt((1, 3, 20, 20)) verify_unary_rsqrt((1, 3, 20, 20), epsilon=1e-6) @@ -566,9 +579,10 @@ def test_forward_unary(): @tvm.testing.uses_gpu def test_forward_reduce(): - from enum import Enum + """Reduce""" class ReduceAxis(Enum): + # pylint: disable=invalid-name CHW = 0 HW = 1 C = 2 @@ -591,7 +605,7 @@ def _verify_reduce(input_dim, mode, axis, ref_func, dtype="float32"): elif axis == ReduceAxis.W: np_axis = -1 - if ref_func == np.argmax: + if ref_func is np.argmax: ref_val = np.expand_dims(ref_func(a_np, np_axis), np_axis).astype(dtype) else: ref_val = ref_func(a_np, np_axis, keepdims=True) @@ -625,6 +639,7 @@ def _verify_reduce(input_dim, mode, axis, ref_func, dtype="float32"): def verify_reshape(input_dim, target_shape, mode): + """Reshape""" dtype = "float32" a_np = np.random.uniform(-100.0, 100.0, size=input_dim).astype(dtype) @@ -653,11 +668,11 @@ def test_forward_reshape(): verify_reshape((1, 3, 20, 20), (1, 12, 10, 10), mode) -def verify_split(input_dim, nOutputs): +def _verify_split(input_dim, out_nums): dtype = "float32" a_np = np.random.uniform(-100.0, 100.0, size=input_dim).astype(dtype) - ref_val = np.split(a_np, nOutputs, axis=-3) + ref_val = np.split(a_np, out_nums, axis=-3) inputs = [("input", datatypes.Array(*input_dim))] @@ -682,7 +697,8 @@ def verify_split(input_dim, nOutputs): def test_forward_split(): - verify_split( + """Split""" + _verify_split( ( 1, 4, @@ -691,7 +707,7 @@ def test_forward_split(): ), 2, ) - verify_split( + _verify_split( ( 1, 3, @@ -703,6 +719,7 @@ def test_forward_split(): def verify_image_scaler(input_dim, blue_bias=0.0, green_bias=0.0, red_bias=0.0, image_scale=1.0): + """Verify image scaler""" dtype = "float32" a_np = np.random.uniform(size=input_dim).astype(dtype) # make sure it is valid image format CHW. @@ -748,23 +765,24 @@ def test_forward_image_scaler(): ) -def verify_convolution(input_dim, filter, padding): +def verify_convolution(input_dim, filter_, padding): + """Verify convolution""" dtype = "float32" - N, C, H, W = input_dim - OC, _, KH, KW = filter + _, c, h, width = input_dim + out_c, _, kernel_h, kernel_w = filter_ a_np = np.random.uniform(size=input_dim).astype(dtype) - w_np = np.random.uniform(size=(OC, C, KH, KW)).astype(dtype) + w_np = np.random.uniform(size=(out_c, c, kernel_h, kernel_w)).astype(dtype) w_np_cm = np.transpose(w_np, axes=(2, 3, 1, 0)) b_np = conv2d_nchw_python(a_np, w_np, [1, 1], padding) - inputs = [("input1", datatypes.Array(C, H, W))] - output = [("output", datatypes.Array(*b_np.shape))] + inputs = [("input1", datatypes.Array(c, h, width))] + output = [("output", datatypes.Array(*b_np.shape))] # pylint:disable=not-an-iterable builder = NeuralNetworkBuilder(inputs, output) builder.add_convolution( name="conv", kernel_channels=3, - output_channels=OC, - height=KH, - width=KW, + output_channels=out_c, + height=kernel_h, + width=kernel_w, stride_height=1, stride_width=1, border_mode=padding.lower(), @@ -784,13 +802,12 @@ def verify_convolution(input_dim, filter, padding): @tvm.testing.uses_gpu def test_forward_convolution(): - verify_convolution((1, 3, 224, 224), filter=(32, 3, 3, 3), padding="VALID") - verify_convolution((1, 3, 224, 224), filter=(32, 3, 3, 3), padding="SAME") + verify_convolution((1, 3, 224, 224), filter_=(32, 3, 3, 3), padding="VALID") + verify_convolution((1, 3, 224, 224), filter_=(32, 3, 3, 3), padding="SAME") def test_can_build_keras_to_coreml_to_relay(): - """Test multiple conversion paths and importing from - a saved file.""" + """Test multiple conversion paths and importing from a saved file.""" model = keras.models.Sequential() model.add( keras.layers.Conv2D( @@ -827,21 +844,4 @@ def test_can_build_keras_to_coreml_to_relay(): if __name__ == "__main__": - test_forward_AddLayerParams() - test_forward_ConcatLayerParams() - test_forward_MultiplyLayerParams() - test_forward_UpsampleLayerParams() - test_forward_l2_normalize() - test_forward_lrn() - test_forward_average() - test_forward_max() - test_forward_min() - test_forward_unary() - test_forward_reduce() - test_forward_reshape() - test_forward_split() - test_mobilenet_checkonly() - test_resnet50_checkonly() - test_forward_image_scaler() - test_forward_convolution() - test_can_build_keras_to_coreml_to_relay() + tvm.testing.main() diff --git a/tests/python/frontend/darknet/test_forward.py b/tests/python/frontend/darknet/test_forward.py index 116748a4f78a..ffaa773fc1bd 100644 --- a/tests/python/frontend/darknet/test_forward.py +++ b/tests/python/frontend/darknet/test_forward.py @@ -14,6 +14,7 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. +# pylint: disable=unused-argument """ Test Darknet Models =================== @@ -23,15 +24,14 @@ """ import numpy as np import tvm -from tvm import te from tvm.contrib import graph_executor from tvm.contrib.download import download_testdata -download_testdata.__test__ = False from tvm.relay.testing.darknet import LAYERTYPE from tvm.relay.testing.darknet import __darknetffi__ from tvm.relay.frontend.darknet import ACTIVATION from tvm import relay +from cffi import FFI REPO_URL = "https://github.com/dmlc/web-data/blob/main/darknet/" DARKNET_LIB = "libdarknet2.0.so" @@ -75,7 +75,6 @@ def _get_tvm_output(net, data, build_dtype="float32", states=None): astext(mod) target = "llvm" - shape_dict = {"data": data.shape} lib = relay.build(mod, target, params=params) # Execute on TVM @@ -172,8 +171,6 @@ def _test_rnn_network(net, states): def get_darknet_network_predict(net, data): return LIB.network_predict(net, data) - from cffi import FFI - ffi = FFI() np_arr = np.zeros([1, net.inputs], dtype="float32") np_arr[0, 2] = 1 @@ -463,7 +460,7 @@ def test_forward_activation_logistic(): net = LIB.make_network(1) batch = 1 h = 224 - w = 224 + width = 224 c = 3 n = 32 groups = 1 @@ -478,7 +475,7 @@ def test_forward_activation_logistic(): layer_1 = LIB.make_convolutional_layer( batch, h, - w, + width, c, n, groups, @@ -492,7 +489,7 @@ def test_forward_activation_logistic(): adam, ) net.layers[0] = layer_1 - net.w = w + net.w = width net.h = h LIB.resize_network(net, net.w, net.h) verify_darknet_frontend(net) @@ -521,26 +518,4 @@ def test_forward_rnn(): if __name__ == "__main__": - test_forward_resnet50() - test_forward_resnext50() - test_forward_alexnet() - test_forward_extraction() - test_forward_yolov2() - test_forward_yolov3() - test_forward_convolutional() - test_forward_maxpooling() - test_forward_avgpooling() - test_forward_conv_batch_norm() - test_forward_shortcut() - test_forward_dense() - test_forward_dense_batchnorm() - test_forward_softmax() - test_forward_softmax_temperature() - test_forward_reorg() - test_forward_region() - test_forward_yolo_op() - test_forward_upsample() - test_forward_l2normalize() - test_forward_elu() - test_forward_rnn() - test_forward_activation_logistic() + tvm.testing.main() diff --git a/tests/python/frontend/keras/test_forward.py b/tests/python/frontend/keras/test_forward.py index ff9e20cff24b..f39ca0662e5e 100644 --- a/tests/python/frontend/keras/test_forward.py +++ b/tests/python/frontend/keras/test_forward.py @@ -14,12 +14,11 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. +"""Unit tests for various models and operators""" import numpy as np import tvm -from tvm import te from tvm import relay from tvm.contrib import graph_executor -import keras import tvm.testing try: @@ -28,26 +27,29 @@ import tensorflow as tf from tensorflow import keras as tf_keras -from packaging import version as package_version # prevent Keras from using up all gpu memory +import keras + if tf.executing_eagerly(): - gpus = tf.config.experimental.list_physical_devices("GPU") - for gpu in gpus: + GPUS = tf.config.experimental.list_physical_devices("GPU") + for gpu in GPUS: tf.config.experimental.set_memory_growth(gpu, True) else: from keras.backend.tensorflow_backend import set_session - config = tf.ConfigProto() - config.gpu_options.per_process_gpu_memory_fraction = 0.5 - set_session(tf.Session(config=config)) + CONFIG = tf.ConfigProto() + CONFIG.gpu_options.per_process_gpu_memory_fraction = 0.5 + set_session(tf.Session(config=CONFIG)) def pytest_generate_tests(metafunc): - # This function generates the list of tests for pytest, based - # on scenarios that will change the parameters in which the - # tests use to run. - # https://docs.pytest.org/en/latest/example/parametrize.html + """ + This function generates the list of tests for pytest, based + on scenarios that will change the parameters in which the + tests use to run. + https://docs.pytest.org/en/latest/example/parametrize.html + """ idlist = [] argvalues = [] for scenario in metafunc.cls.scenarios: @@ -61,11 +63,12 @@ def pytest_generate_tests(metafunc): # Scenarios: # - classic keras, using keras from "import keras" # - tensorflow keras, using keras from "from tensorflow import keras as tf_keras" -using_classic_keras = ("keras", {"keras": keras}) -using_tensorflow_keras = ("tf_keras", {"keras": tf_keras}) +USING_CALSSIC_KERAS = ("keras", {"keras_mod": keras}) +USING_TENSORFLOW_KERAS = ("tf_keras", {"keras_mod": tf_keras}) def verify_keras_frontend(keras_model, need_transpose=True, layout="NCHW"): + """Generic function to generate and compare Keras and TVM output""" # Keras frontend currently supports tensorflow backend only. assert keras.backend.backend() == "tensorflow" @@ -81,16 +84,16 @@ def verify_keras_frontend(keras_model, need_transpose=True, layout="NCHW"): tuple(dim.value if dim.value is not None else 1 for dim in layer.input.shape) ) - def get_keras_output(xs, dtype="float32"): - return keras_model.predict(xs) + def get_keras_output(in_data): + return keras_model.predict(in_data) - def get_tvm_output(xs, target, dev, dtype="float32"): - shape_dict = {name: x.shape for (name, x) in zip(keras_model.input_names, xs)} + def get_tvm_output(in_data, target, dev, dtype="float32"): + shape_dict = {name: x.shape for (name, x) in zip(keras_model.input_names, in_data)} mod, params = relay.frontend.from_keras(keras_model, shape_dict, layout=layout) with tvm.transform.PassContext(opt_level=2): lib = relay.build(mod, target, params=params) m = graph_executor.GraphModule(lib["default"](dev)) - for name, x in zip(keras_model.input_names, xs): + for name, x in zip(keras_model.input_names, in_data): m.set_input(name, tvm.nd.array(x.astype(dtype))) m.run() return [m.get_output(i).numpy() for i in range(m.get_num_outputs())] @@ -101,11 +104,11 @@ def to_channels_first(arr): def to_channels_last(arr): return arr.transpose([0] + list(range(2, arr.ndim)) + [1]) - xs = [np.random.uniform(size=shape, low=-1.0, high=1.0) for shape in in_shapes] - keras_out = get_keras_output(xs) + in_data = [np.random.uniform(size=shape, low=-1.0, high=1.0) for shape in in_shapes] + keras_out = get_keras_output(in_data) keras_out = keras_out if isinstance(keras_out, list) else [keras_out] for target, dev in tvm.testing.enabled_targets(): - inputs = [to_channels_first(x) for x in xs] if need_transpose else xs + inputs = [to_channels_first(x) for x in in_data] if need_transpose else in_data tvm_out = get_tvm_output(inputs, target, dev) for kout, tout in zip(keras_out, tvm_out): if need_transpose: @@ -113,173 +116,185 @@ def to_channels_last(arr): tvm.testing.assert_allclose(kout, tout, rtol=1e-5, atol=1e-5) -def get_mobilenet(keras): - if hasattr(keras.applications, "MobileNet"): +def get_mobilenet(keras_mod): + if hasattr(keras_mod.applications, "MobileNet"): # Keras 2.4.x and older - MobileNet = keras.applications.MobileNet + mobilenet_mod = keras_mod.applications.MobileNet else: # Keras 2.6.x and newer - MobileNet = keras.applications.mobilenet.MobileNet + mobilenet_mod = keras_mod.applications.mobilenet.MobileNet - return MobileNet + return mobilenet_mod @tvm.testing.uses_gpu class TestKeras: - scenarios = [using_classic_keras, using_tensorflow_keras] + """Keras test""" - def test_forward_merge(self, keras): - data = keras.layers.Input(shape=(32, 32, 3)) - x = keras.layers.Conv2D(8, (3, 3), padding="same")(data) - y = keras.layers.Conv2D(8, (3, 3), padding="same")(x) - z = keras.layers.Conv2D(8, (3, 3), padding="same")(y) + scenarios = [USING_CALSSIC_KERAS, USING_TENSORFLOW_KERAS] + + def test_forward_merge(self, keras_mod): + """test_forward_merge""" + data = keras_mod.layers.Input(shape=(32, 32, 3)) + conv2d_x = keras_mod.layers.Conv2D(8, (3, 3), padding="same")(data) + conv2d_y = keras_mod.layers.Conv2D(8, (3, 3), padding="same")(conv2d_x) + conv2d_z = keras_mod.layers.Conv2D(8, (3, 3), padding="same")(conv2d_y) merge_funcs = [ - keras.layers.Add(), - keras.layers.Subtract(), - keras.layers.Multiply(), - keras.layers.Maximum(), - keras.layers.Minimum(), - keras.layers.Average(), - keras.layers.Concatenate(), + keras_mod.layers.Add(), + keras_mod.layers.Subtract(), + keras_mod.layers.Multiply(), + keras_mod.layers.Maximum(), + keras_mod.layers.Minimum(), + keras_mod.layers.Average(), + keras_mod.layers.Concatenate(), ] for merge_func in merge_funcs: class_name = type(merge_func).__name__ if class_name in ("Subtract", "Dot"): - out = merge_func([x, y]) + out = merge_func([conv2d_x, conv2d_y]) else: - out = merge_func([x, y, z]) - keras_model = keras.models.Model(data, out) + out = merge_func([conv2d_x, conv2d_y, conv2d_z]) + keras_model = keras_mod.models.Model(data, out) verify_keras_frontend(keras_model) - def test_forward_merge_dot(self, keras): - data1 = keras.layers.Input(shape=(2, 2)) - data2 = keras.layers.Input(shape=(2, 2)) + def test_forward_merge_dot(self, keras_mod): + """test_forward_merge_dot""" + data1 = keras_mod.layers.Input(shape=(2, 2)) + data2 = keras_mod.layers.Input(shape=(2, 2)) merge_funcs = [ - keras.layers.Dot(axes=[1, 2]), - keras.layers.Dot(axes=[2, 1]), - keras.layers.Dot(axes=[1, 1]), - keras.layers.Dot(axes=[2, 2]), - keras.layers.Dot(axes=1), - keras.layers.Dot(axes=2), + keras_mod.layers.Dot(axes=[1, 2]), + keras_mod.layers.Dot(axes=[2, 1]), + keras_mod.layers.Dot(axes=[1, 1]), + keras_mod.layers.Dot(axes=[2, 2]), + keras_mod.layers.Dot(axes=1), + keras_mod.layers.Dot(axes=2), ] for merge_func in merge_funcs: out = merge_func([data1, data2]) - keras_model = keras.models.Model([data1, data2], out) + keras_model = keras_mod.models.Model([data1, data2], out) verify_keras_frontend(keras_model) - def test_forward_activations(self, keras): - data = keras.layers.Input(shape=(32, 32, 3)) + def test_forward_activations(self, keras_mod): + """test_forward_activations""" + data = keras_mod.layers.Input(shape=(32, 32, 3)) act_funcs = [ - keras.layers.Activation("softmax"), - keras.layers.Softmax(), - keras.layers.Softmax(axis=-1), - keras.layers.Softmax(axis=1), - keras.layers.Softmax(axis=2), - keras.layers.Softmax(axis=3), - keras.layers.Activation("softplus"), - keras.layers.Activation("relu"), - keras.layers.Activation("softsign"), - keras.layers.Activation("hard_sigmoid"), - keras.layers.Activation("sigmoid"), - keras.layers.Activation("tanh"), - keras.layers.Activation("linear"), - keras.layers.Activation("selu"), - keras.layers.ReLU(), - keras.layers.ReLU(max_value=6.0), - keras.layers.ReLU(max_value=6.0, threshold=0.0), - keras.layers.ReLU(max_value=6.0, threshold=1.0), - keras.layers.ReLU(max_value=6.0, threshold=1.0, negative_slope=0.0), - keras.layers.ReLU(max_value=6.0, threshold=1.0, negative_slope=0.5), - keras.layers.ReLU(max_value=6.0, threshold=1.0, negative_slope=1.0), - keras.layers.LeakyReLU(alpha=0.3), - keras.layers.PReLU(weights=np.random.rand(1, 32, 32, 3)), - keras.layers.ELU(alpha=0.5), - keras.layers.ThresholdedReLU(theta=0.5), + keras_mod.layers.Activation("softmax"), + keras_mod.layers.Softmax(), + keras_mod.layers.Softmax(axis=-1), + keras_mod.layers.Softmax(axis=1), + keras_mod.layers.Softmax(axis=2), + keras_mod.layers.Softmax(axis=3), + keras_mod.layers.Activation("softplus"), + keras_mod.layers.Activation("relu"), + keras_mod.layers.Activation("softsign"), + keras_mod.layers.Activation("hard_sigmoid"), + keras_mod.layers.Activation("sigmoid"), + keras_mod.layers.Activation("tanh"), + keras_mod.layers.Activation("linear"), + keras_mod.layers.Activation("selu"), + keras_mod.layers.ReLU(), + keras_mod.layers.ReLU(max_value=6.0), + keras_mod.layers.ReLU(max_value=6.0, threshold=0.0), + keras_mod.layers.ReLU(max_value=6.0, threshold=1.0), + keras_mod.layers.ReLU(max_value=6.0, threshold=1.0, negative_slope=0.0), + keras_mod.layers.ReLU(max_value=6.0, threshold=1.0, negative_slope=0.5), + keras_mod.layers.ReLU(max_value=6.0, threshold=1.0, negative_slope=1.0), + keras_mod.layers.LeakyReLU(alpha=0.3), + keras_mod.layers.PReLU(weights=np.random.rand(1, 32, 32, 3)), + keras_mod.layers.ELU(alpha=0.5), + keras_mod.layers.ThresholdedReLU(theta=0.5), ] for act_func in act_funcs: x = act_func(data) - keras_model = keras.models.Model(data, x) + keras_model = keras_mod.models.Model(data, x) verify_keras_frontend(keras_model) verify_keras_frontend(keras_model, need_transpose=False, layout="NHWC") - def test_forward_dense(self, keras): - data = keras.layers.Input(shape=(32, 32, 1)) - x = keras.layers.Flatten()(data) - x = keras.layers.Dropout(0.5)(x) - x = keras.layers.Dense(10, activation="relu", kernel_initializer="uniform")(x) - keras_model = keras.models.Model(data, x) + def test_forward_dense(self, keras_mod): + """test_forward_dense""" + data = keras_mod.layers.Input(shape=(32, 32, 1)) + x = keras_mod.layers.Flatten()(data) + x = keras_mod.layers.Dropout(0.5)(x) + x = keras_mod.layers.Dense(10, activation="relu", kernel_initializer="uniform")(x) + keras_model = keras_mod.models.Model(data, x) verify_keras_frontend(keras_model) # RNN dense - data = keras.layers.Input(shape=(1, 32)) - x = keras.layers.Dense(32, activation="relu", kernel_initializer="uniform")(data) - keras_model = keras.models.Model(data, x) + data = keras_mod.layers.Input(shape=(1, 32)) + x = keras_mod.layers.Dense(32, activation="relu", kernel_initializer="uniform")(data) + keras_model = keras_mod.models.Model(data, x) verify_keras_frontend(keras_model, need_transpose=False) - def test_forward_permute(self, keras): - data = keras.layers.Input(shape=(2, 3, 4)) - x = keras.layers.Permute([2, 3, 1])(data) - keras_model = keras.models.Model(data, x) + def test_forward_permute(self, keras_mod): + data = keras_mod.layers.Input(shape=(2, 3, 4)) + x = keras_mod.layers.Permute([2, 3, 1])(data) + keras_model = keras_mod.models.Model(data, x) verify_keras_frontend(keras_model, need_transpose=False) - def test_forward_sequential(self, keras): - keras_model = keras.models.Sequential( + def test_forward_sequential(self, keras_mod): + """test_forward_sequential""" + keras_model = keras_mod.models.Sequential( [ - keras.layers.Dense(16, input_dim=32, activation="relu"), - keras.layers.Dropout(0.5), - keras.layers.Dense(8, activation="relu"), - keras.layers.Dropout(0.5), - keras.layers.Dense(1, activation="sigmoid"), + keras_mod.layers.Dense(16, input_dim=32, activation="relu"), + keras_mod.layers.Dropout(0.5), + keras_mod.layers.Dense(8, activation="relu"), + keras_mod.layers.Dropout(0.5), + keras_mod.layers.Dense(1, activation="sigmoid"), ] ) verify_keras_frontend(keras_model) - def test_forward_pool(self, keras): - data = keras.layers.Input(shape=(32, 32, 1)) + def test_forward_pool(self, keras_mod): + data = keras_mod.layers.Input(shape=(32, 32, 1)) # maxpool - x = keras.layers.MaxPooling2D((3, 3), strides=(1, 1), padding="same")(data) - keras_model = keras.models.Model(data, x) + x = keras_mod.layers.MaxPooling2D((3, 3), strides=(1, 1), padding="same")(data) + keras_model = keras_mod.models.Model(data, x) verify_keras_frontend(keras_model) # avgpool - y = keras.layers.AveragePooling2D((3, 3), strides=(1, 1), padding="same")(data) - keras_model = keras.models.Model(data, y) + y = keras_mod.layers.AveragePooling2D((3, 3), strides=(1, 1), padding="same")(data) + keras_model = keras_mod.models.Model(data, y) verify_keras_frontend(keras_model) - def test_forward_conv1d(self, keras): - data = keras.layers.Input(shape=(32, 3)) + def test_forward_conv1d(self, keras_mod): + """test_forward_conv1d""" + data = keras_mod.layers.Input(shape=(32, 3)) conv_funcs = [ - keras.layers.Conv1D(filters=10, kernel_size=(3,), strides=(2,), padding="same"), - keras.layers.Conv1D(filters=10, kernel_size=(3,), dilation_rate=(2,), padding="same"), - keras.layers.Conv1D(filters=1, kernel_size=(3,), padding="valid", use_bias=False), - keras.layers.Conv1D(filters=10, kernel_size=(2,), padding="valid"), + keras_mod.layers.Conv1D(filters=10, kernel_size=(3,), strides=(2,), padding="same"), + keras_mod.layers.Conv1D( + filters=10, kernel_size=(3,), dilation_rate=(2,), padding="same" + ), + keras_mod.layers.Conv1D(filters=1, kernel_size=(3,), padding="valid", use_bias=False), + keras_mod.layers.Conv1D(filters=10, kernel_size=(2,), padding="valid"), # Enable when relay conv1dtranspose handles NWC # keras.layers.Conv1DTranspose(filters=10, kernel_size=(3), padding="valid"), ] for conv_func in conv_funcs: x = conv_func(data) - keras_model = keras.models.Model(data, x) + keras_model = keras_mod.models.Model(data, x) verify_keras_frontend(keras_model, layout="NWC") - def test_forward_conv(self, keras): - data = keras.layers.Input(shape=(32, 32, 3)) + def test_forward_conv(self, keras_mod): + """test_forward_conv""" + data = keras_mod.layers.Input(shape=(32, 32, 3)) conv_funcs = [ - keras.layers.Conv2D(filters=10, kernel_size=(3, 3), strides=(2, 2), padding="same"), - keras.layers.Conv2D( + keras_mod.layers.Conv2D(filters=10, kernel_size=(3, 3), strides=(2, 2), padding="same"), + keras_mod.layers.Conv2D( filters=10, kernel_size=(3, 3), dilation_rate=(2, 2), padding="same" ), - keras.layers.Conv2D(filters=1, kernel_size=(3, 3), padding="same"), - keras.layers.DepthwiseConv2D(kernel_size=(3, 3), padding="same"), - keras.layers.Conv2DTranspose(filters=10, kernel_size=(3, 3), padding="valid"), - keras.layers.SeparableConv2D(filters=10, kernel_size=(3, 3), padding="same"), + keras_mod.layers.Conv2D(filters=1, kernel_size=(3, 3), padding="same"), + keras_mod.layers.DepthwiseConv2D(kernel_size=(3, 3), padding="same"), + keras_mod.layers.Conv2DTranspose(filters=10, kernel_size=(3, 3), padding="valid"), + keras_mod.layers.SeparableConv2D(filters=10, kernel_size=(3, 3), padding="same"), ] for conv_func in conv_funcs: x = conv_func(data) - keras_model = keras.models.Model(data, x) + keras_model = keras_mod.models.Model(data, x) verify_keras_frontend(keras_model) - def test_forward_batch_norm(self, keras): - data = keras.layers.Input(shape=(32, 32, 3)) + def test_forward_batch_norm(self, keras_mod): + """test_forward_batch_norm""" + data = keras_mod.layers.Input(shape=(32, 32, 3)) batch_norm_funcs = [ - keras.layers.BatchNormalization( + keras_mod.layers.BatchNormalization( axis=-1, momentum=0.99, epsilon=0.001, @@ -290,7 +305,7 @@ def test_forward_batch_norm(self, keras): moving_mean_initializer="zeros", moving_variance_initializer="ones", ), - keras.layers.BatchNormalization( + keras_mod.layers.BatchNormalization( axis=-1, momentum=0.99, epsilon=0.001, @@ -301,7 +316,7 @@ def test_forward_batch_norm(self, keras): moving_mean_initializer="zeros", moving_variance_initializer="ones", ), - keras.layers.BatchNormalization( + keras_mod.layers.BatchNormalization( axis=-1, momentum=0.99, epsilon=0.001, @@ -312,7 +327,7 @@ def test_forward_batch_norm(self, keras): moving_mean_initializer="zeros", moving_variance_initializer="ones", ), - keras.layers.BatchNormalization( + keras_mod.layers.BatchNormalization( axis=-1, momentum=0.99, epsilon=0.001, @@ -326,143 +341,150 @@ def test_forward_batch_norm(self, keras): ] for batch_norm_func in batch_norm_funcs: x = batch_norm_func(data) - keras_model = keras.models.Model(data, x) + keras_model = keras_mod.models.Model(data, x) verify_keras_frontend(keras_model) - def test_forward_upsample(self, keras, interpolation="nearest"): - data = keras.layers.Input(shape=(32, 32, 3)) - x = keras.layers.UpSampling2D(size=(3, 3), interpolation=interpolation)(data) - keras_model = keras.models.Model(data, x) + def test_forward_upsample(self, keras_mod, interpolation="nearest"): + data = keras_mod.layers.Input(shape=(32, 32, 3)) + x = keras_mod.layers.UpSampling2D(size=(3, 3), interpolation=interpolation)(data) + keras_model = keras_mod.models.Model(data, x) verify_keras_frontend(keras_model) - def test_forward_reshape(self, keras): + def test_forward_reshape(self, keras_mod): + """test_forward_reshape""" # input_shape len is 3, target_shape len is 3 - data = keras.layers.Input(shape=(32, 32, 3)) - x = keras.layers.Reshape(target_shape=(16, 64, 3))(data) - keras_model = keras.models.Model(data, x) + data = keras_mod.layers.Input(shape=(32, 32, 3)) + x = keras_mod.layers.Reshape(target_shape=(16, 64, 3))(data) + keras_model = keras_mod.models.Model(data, x) verify_keras_frontend(keras_model) # input_shape len is 3, target_shape len is 2 - data = keras.layers.Input(shape=(32, 8, 3)) - x = keras.layers.Reshape(target_shape=(256, 3))(data) - keras_model = keras.models.Model(data, x) + data = keras_mod.layers.Input(shape=(32, 8, 3)) + x = keras_mod.layers.Reshape(target_shape=(256, 3))(data) + keras_model = keras_mod.models.Model(data, x) verify_keras_frontend(keras_model) # input_shape len is 2, target_shape len is 3 - data = keras.layers.Input(shape=(256, 3)) - x = keras.layers.Reshape(target_shape=(8, 32, 3))(data) - keras_model = keras.models.Model(data, x) + data = keras_mod.layers.Input(shape=(256, 3)) + x = keras_mod.layers.Reshape(target_shape=(8, 32, 3))(data) + keras_model = keras_mod.models.Model(data, x) verify_keras_frontend(keras_model) # input_shape len is 2, target_shape len is 1 - data = keras.layers.Input(shape=(2, 8)) - x = keras.layers.Reshape(target_shape=(16,))(data) - keras_model = keras.models.Model(data, x) + data = keras_mod.layers.Input(shape=(2, 8)) + x = keras_mod.layers.Reshape(target_shape=(16,))(data) + keras_model = keras_mod.models.Model(data, x) verify_keras_frontend(keras_model, need_transpose=False) # input_shape len is 1, target_shape len is 2 - data = keras.layers.Input(shape=(16,)) - x = keras.layers.Reshape(target_shape=(4, 4))(data) - keras_model = keras.models.Model(data, x) + data = keras_mod.layers.Input(shape=(16,)) + x = keras_mod.layers.Reshape(target_shape=(4, 4))(data) + keras_model = keras_mod.models.Model(data, x) verify_keras_frontend(keras_model, need_transpose=False) # input_shape len is 2, target_shape len is 2 - data = keras.layers.Input(shape=(2, 8)) - x = keras.layers.Reshape(target_shape=(4, 4))(data) - keras_model = keras.models.Model(data, x) + data = keras_mod.layers.Input(shape=(2, 8)) + x = keras_mod.layers.Reshape(target_shape=(4, 4))(data) + keras_model = keras_mod.models.Model(data, x) verify_keras_frontend(keras_model, need_transpose=False) # "non-square" target shape - data = keras.layers.Input(shape=(15,)) - x = keras.layers.Reshape(target_shape=(5, 3))(data) - keras_model = keras.models.Model(data, x) + data = keras_mod.layers.Input(shape=(15,)) + x = keras_mod.layers.Reshape(target_shape=(5, 3))(data) + keras_model = keras_mod.models.Model(data, x) verify_keras_frontend(keras_model, need_transpose=False) # modify channel dim - data = keras.layers.Input(shape=(3, 2, 4)) - x = keras.layers.Reshape(target_shape=(3, 8))(data) - keras_model = keras.models.Model(data, x) + data = keras_mod.layers.Input(shape=(3, 2, 4)) + x = keras_mod.layers.Reshape(target_shape=(3, 8))(data) + keras_model = keras_mod.models.Model(data, x) verify_keras_frontend(keras_model) - def test_forward_crop(self, keras): - data = keras.layers.Input(shape=(32, 32, 3)) - x = keras.layers.Cropping2D(cropping=((1, 1), (1, 1)))(data) - x = keras.layers.Cropping2D(cropping=(1, 1))(x) - x = keras.layers.Cropping2D(cropping=1)(x) - x = keras.layers.Cropping2D(cropping=((0, 1), (1, 0)))(x) - x = keras.layers.Cropping2D(cropping=(1, 0))(x) - x = keras.layers.Cropping2D(cropping=0)(x) - x = keras.layers.Add()([x, x]) - keras_model = keras.models.Model(data, x) + def test_forward_crop(self, keras_mod): + """test_forward_crop""" + data = keras_mod.layers.Input(shape=(32, 32, 3)) + x = keras_mod.layers.Cropping2D(cropping=((1, 1), (1, 1)))(data) + x = keras_mod.layers.Cropping2D(cropping=(1, 1))(x) + x = keras_mod.layers.Cropping2D(cropping=1)(x) + x = keras_mod.layers.Cropping2D(cropping=((0, 1), (1, 0)))(x) + x = keras_mod.layers.Cropping2D(cropping=(1, 0))(x) + x = keras_mod.layers.Cropping2D(cropping=0)(x) + x = keras_mod.layers.Add()([x, x]) + keras_model = keras_mod.models.Model(data, x) verify_keras_frontend(keras_model) - def test_forward_multi_inputs(self, keras): - data1 = keras.layers.Input(shape=(32, 32, 3)) - data2 = keras.layers.Input(shape=(32, 32, 3)) - x = keras.layers.Conv2D(8, (3, 3), padding="same")(data1) - y = keras.layers.Conv2D(8, (3, 3), padding="same")(data2) - z = keras.layers.Average()([x, y]) - z = keras.layers.GlobalAveragePooling2D()(z) - keras_model = keras.models.Model([data1, data2], z) + def test_forward_multi_inputs(self, keras_mod): + data1 = keras_mod.layers.Input(shape=(32, 32, 3)) + data2 = keras_mod.layers.Input(shape=(32, 32, 3)) + x = keras_mod.layers.Conv2D(8, (3, 3), padding="same")(data1) + y = keras_mod.layers.Conv2D(8, (3, 3), padding="same")(data2) + average_z = keras_mod.layers.Average()([x, y]) + out = keras_mod.layers.GlobalAveragePooling2D()(average_z) + keras_model = keras_mod.models.Model([data1, data2], out) verify_keras_frontend(keras_model) - def test_forward_multi_outputs(self, keras): - data = keras.layers.Input(shape=(32, 32, 3)) - x = keras.layers.Conv2D(8, (3, 3), padding="same")(data) - x = keras.layers.GlobalAveragePooling2D()(x) - y = keras.layers.Conv2D(8, (3, 3), padding="same")(data) - y = keras.layers.GlobalAveragePooling2D()(y) - keras_model = keras.models.Model(data, [x, y]) + def test_forward_multi_outputs(self, keras_mod): + data = keras_mod.layers.Input(shape=(32, 32, 3)) + x = keras_mod.layers.Conv2D(8, (3, 3), padding="same")(data) + x = keras_mod.layers.GlobalAveragePooling2D()(x) + y = keras_mod.layers.Conv2D(8, (3, 3), padding="same")(data) + y = keras_mod.layers.GlobalAveragePooling2D()(y) + keras_model = keras_mod.models.Model(data, [x, y]) verify_keras_frontend(keras_model) - def test_forward_reuse_layers(self, keras): + def test_forward_reuse_layers(self, keras_mod): + """test_forward_reuse_layers""" # reuse conv2d - data = keras.layers.Input(shape=(32, 32, 3)) - conv2d = keras.layers.Conv2D(8, (3, 3), padding="same") + data = keras_mod.layers.Input(shape=(32, 32, 3)) + conv2d = keras_mod.layers.Conv2D(8, (3, 3), padding="same") x = conv2d(data) y = conv2d(data) - z = keras.layers.Add()([x, y]) - z = keras.layers.GlobalAveragePooling2D()(z) - keras_model = keras.models.Model(data, z) + add_z = keras_mod.layers.Add()([x, y]) + out = keras_mod.layers.GlobalAveragePooling2D()(add_z) + keras_model = keras_mod.models.Model(data, out) verify_keras_frontend(keras_model) # reuse add - data = keras.layers.Input(shape=(32, 32, 3)) - x = keras.layers.Conv2D(8, (3, 3), padding="same")(data) - add = keras.layers.Add() + data = keras_mod.layers.Input(shape=(32, 32, 3)) + x = keras_mod.layers.Conv2D(8, (3, 3), padding="same")(data) + add = keras_mod.layers.Add() x = add([x, x]) x = add([x, x]) - z = keras.layers.GlobalAveragePooling2D()(x) - keras_model = keras.models.Model(data, z) + out = keras_mod.layers.GlobalAveragePooling2D()(x) + keras_model = keras_mod.models.Model(data, out) verify_keras_frontend(keras_model) - def test_forward_lstm(self, keras): - data = keras.layers.Input(shape=(10, 32)) + def test_forward_lstm(self, keras_mod): + """test_forward_lstm""" + data = keras_mod.layers.Input(shape=(10, 32)) rnn_funcs = [ - keras.layers.LSTM(16), - keras.layers.LSTM(16, return_sequences=True), - keras.layers.LSTM(16, return_sequences=True, use_bias=False), + keras_mod.layers.LSTM(16), + keras_mod.layers.LSTM(16, return_sequences=True), + keras_mod.layers.LSTM(16, return_sequences=True, use_bias=False), ] for rnn_func in rnn_funcs: x = rnn_func(data) - keras_model = keras.models.Model(data, x) + keras_model = keras_mod.models.Model(data, x) verify_keras_frontend(keras_model, need_transpose=False) - def test_forward_rnn(self, keras): - data = keras.layers.Input(shape=(1, 32)) + def test_forward_rnn(self, keras_mod): + """test_forward_rnn""" + data = keras_mod.layers.Input(shape=(1, 32)) rnn_funcs = [ - keras.layers.LSTM( + keras_mod.layers.LSTM( units=16, return_state=False, recurrent_activation="sigmoid", activation="tanh" ), - keras.layers.LSTM( + keras_mod.layers.LSTM( units=16, return_state=False, recurrent_activation="sigmoid", activation="tanh", use_bias=False, ), - keras.layers.SimpleRNN(units=16, return_state=False, activation="tanh"), - keras.layers.SimpleRNN(units=16, return_state=False, activation="tanh", use_bias=False), - keras.layers.GRU( + keras_mod.layers.SimpleRNN(units=16, return_state=False, activation="tanh"), + keras_mod.layers.SimpleRNN( + units=16, return_state=False, activation="tanh", use_bias=False + ), + keras_mod.layers.GRU( units=16, return_state=False, recurrent_activation="sigmoid", activation="tanh", reset_after=False, ), - keras.layers.GRU( + keras_mod.layers.GRU( units=16, return_state=False, recurrent_activation="sigmoid", @@ -473,220 +495,239 @@ def test_forward_rnn(self, keras): ] for rnn_func in rnn_funcs: x = rnn_func(data) - keras_model = keras.models.Model(data, x) + keras_model = keras_mod.models.Model(data, x) verify_keras_frontend(keras_model, need_transpose=False) - def test_forward_vgg16(self, keras, layout="NCHW"): - if hasattr(keras.applications, "VGG16"): + def test_forward_vgg16(self, keras_mod, layout="NCHW"): + """test_forward_vgg16""" + if hasattr(keras_mod.applications, "VGG16"): # Keras 2.4.x and older - VGG16 = keras.applications.VGG16 + vgg16_mod = keras_mod.applications.VGG16 else: # Keras 2.6.x and newer - VGG16 = keras.applications.vgg16.VGG16 + vgg16_mod = keras_mod.applications.vgg16.VGG16 - keras_model = VGG16( + keras_model = vgg16_mod( include_top=True, weights="imagenet", input_shape=(224, 224, 3), classes=1000 ) verify_keras_frontend(keras_model, layout=layout) - def test_forward_xception(self, keras, layout="NCHW"): - if hasattr(keras.applications, "Xception"): + def test_forward_xception(self, keras_mod, layout="NCHW"): + """test_forward_vgg16""" + if hasattr(keras_mod.applications, "Xception"): # Keras 2.4.x and older - Xception = keras.applications.Xception + xception_mod = keras_mod.applications.Xception else: # Keras 2.6.x and newer - Xception = keras.applications.xception.Xception + xception_mod = keras_mod.applications.xception.Xception - keras_model = Xception( + keras_model = xception_mod( include_top=True, weights="imagenet", input_shape=(299, 299, 3), classes=1000 ) verify_keras_frontend(keras_model, layout=layout) - def test_forward_resnet50(self, keras, layout="NCHW"): - if hasattr(keras.applications, "ResNet50"): + def test_forward_resnet50(self, keras_mod, layout="NCHW"): + """test_forward_resnet50""" + if hasattr(keras_mod.applications, "ResNet50"): # Keras 2.4.x and older - ResNet50 = keras.applications.ResNet50 + resnet50_mod = keras_mod.applications.ResNet50 else: # Keras 2.6.x and newer - ResNet50 = keras.applications.resnet.ResNet50 + resnet50_mod = keras_mod.applications.resnet.ResNet50 - keras_model = ResNet50( + keras_model = resnet50_mod( include_top=True, weights="imagenet", input_shape=(224, 224, 3), classes=1000 ) verify_keras_frontend(keras_model, layout=layout) - def test_forward_mobilenet(self, keras, layout="NCHW"): - MobileNet = get_mobilenet(keras) + def test_forward_mobilenet(self, keras_mod, layout="NCHW"): + mobilenet_mod = get_mobilenet(keras_mod) - keras_model = MobileNet( + keras_model = mobilenet_mod( include_top=True, weights="imagenet", input_shape=(224, 224, 3), classes=1000 ) verify_keras_frontend(keras_model, layout=layout) - def test_forward_conv3d(self, keras): - data = keras.layers.Input(shape=(32, 32, 32, 3)) + def test_forward_conv3d(self, keras_mod): + """test_forward_conv3d""" + data = keras_mod.layers.Input(shape=(32, 32, 32, 3)) conv_funcs = [ - keras.layers.Conv3D( + keras_mod.layers.Conv3D( filters=10, kernel_size=(3, 3, 3), strides=(2, 2, 2), padding="same" ), - keras.layers.Conv3D( + keras_mod.layers.Conv3D( filters=10, kernel_size=(3, 3, 3), dilation_rate=(2, 2, 2), padding="same" ), - keras.layers.Conv3D(filters=1, kernel_size=(3, 3, 3), padding="valid", use_bias=False), - keras.layers.Conv3D(filters=10, kernel_size=(2, 2, 2), padding="valid"), + keras_mod.layers.Conv3D( + filters=1, kernel_size=(3, 3, 3), padding="valid", use_bias=False + ), + keras_mod.layers.Conv3D(filters=10, kernel_size=(2, 2, 2), padding="valid"), ] for conv_func in conv_funcs: x = conv_func(data) - keras_model = keras.models.Model(data, x) + keras_model = keras_mod.models.Model(data, x) verify_keras_frontend(keras_model, layout="NDHWC") - def test_forward_conv3d_transpose(self, keras): - data = keras.layers.Input(shape=(32, 32, 32, 3)) + def test_forward_conv3d_transpose(self, keras_mod): + """test_forward_conv3d_transpose""" + data = keras_mod.layers.Input(shape=(32, 32, 32, 3)) conv_funcs = [ - keras.layers.Conv3DTranspose( + keras_mod.layers.Conv3DTranspose( filters=10, kernel_size=(3, 3, 3), strides=(2, 2, 2), padding="same" ), - keras.layers.Conv3DTranspose( + keras_mod.layers.Conv3DTranspose( filters=10, kernel_size=(1, 1, 1), dilation_rate=(1, 1, 1), padding="same" ), - keras.layers.Conv3DTranspose( + keras_mod.layers.Conv3DTranspose( filters=1, kernel_size=(3, 3, 3), padding="valid", use_bias=False ), - keras.layers.Conv3DTranspose(filters=10, kernel_size=(2, 2, 2), padding="valid"), + keras_mod.layers.Conv3DTranspose(filters=10, kernel_size=(2, 2, 2), padding="valid"), ] for conv_func in conv_funcs: x = conv_func(data) - keras_model = keras.models.Model(data, x) + keras_model = keras_mod.models.Model(data, x) verify_keras_frontend(keras_model, layout="NDHWC") - def test_forward_pool3d(self, keras): - data = keras.layers.Input(shape=(32, 32, 32, 1)) + def test_forward_pool3d(self, keras_mod): + """test_forward_pool3d""" + data = keras_mod.layers.Input(shape=(32, 32, 32, 1)) pool_funcs = [ # maxpool - keras.layers.MaxPooling3D(pool_size=(2, 2, 2), strides=(1, 1, 1), padding="same"), - keras.layers.MaxPooling3D(pool_size=(3, 3, 3), strides=(2, 2, 2), padding="valid"), + keras_mod.layers.MaxPooling3D(pool_size=(2, 2, 2), strides=(1, 1, 1), padding="same"), + keras_mod.layers.MaxPooling3D(pool_size=(3, 3, 3), strides=(2, 2, 2), padding="valid"), # avgpool - keras.layers.AveragePooling3D(pool_size=(3, 3, 3), strides=(2, 2, 2), padding="same"), - keras.layers.AveragePooling3D(pool_size=(2, 2, 2), strides=(1, 1, 1), padding="valid"), + keras_mod.layers.AveragePooling3D( + pool_size=(3, 3, 3), strides=(2, 2, 2), padding="same" + ), + keras_mod.layers.AveragePooling3D( + pool_size=(2, 2, 2), strides=(1, 1, 1), padding="valid" + ), ] for pool_func in pool_funcs: x = pool_func(data) - keras_model = keras.models.Model(data, x) + keras_model = keras_mod.models.Model(data, x) verify_keras_frontend(keras_model, layout="NDHWC") - def test_forward_upsample3d(self, keras): - data = keras.layers.Input(shape=(32, 32, 32, 3)) - x = keras.layers.UpSampling3D(size=(2, 3, 4))(data) - keras_model = keras.models.Model(data, x) + def test_forward_upsample3d(self, keras_mod): + data = keras_mod.layers.Input(shape=(32, 32, 32, 3)) + x = keras_mod.layers.UpSampling3D(size=(2, 3, 4))(data) + keras_model = keras_mod.models.Model(data, x) verify_keras_frontend(keras_model, layout="NDHWC") - def test_forward_zero_padding3d(self, keras): - data = keras.layers.Input(shape=(32, 32, 32, 3)) + def test_forward_zero_padding3d(self, keras_mod): + """test_forward_zero_padding3d""" + data = keras_mod.layers.Input(shape=(32, 32, 32, 3)) pad_funcs = [ # Integer - keras.layers.ZeroPadding3D(padding=2), + keras_mod.layers.ZeroPadding3D(padding=2), # tuple of 3 ints - keras.layers.ZeroPadding3D(padding=(1, 2, 3)), + keras_mod.layers.ZeroPadding3D(padding=(1, 2, 3)), # tuple of 3 tuples of 2 ints - keras.layers.ZeroPadding3D(padding=((1, 1), (2, 2), (2, 2))), + keras_mod.layers.ZeroPadding3D(padding=((1, 1), (2, 2), (2, 2))), # tuple of 3 tuples of 2 ints different values - keras.layers.ZeroPadding3D(padding=((1, 2), (2, 3), (3, 2))), + keras_mod.layers.ZeroPadding3D(padding=((1, 2), (2, 3), (3, 2))), ] for pad_func in pad_funcs: x = pad_func(data) - keras_model = keras.models.Model(data, x) + keras_model = keras_mod.models.Model(data, x) verify_keras_frontend(keras_model, layout="NDHWC") - def test_forward_embedding(self, keras): - data = keras.layers.Input(shape=(2, 4), dtype="int32") - x = keras.layers.Embedding(10, 3)(data) - keras_model = keras.models.Model(data, x) + def test_forward_embedding(self, keras_mod): + """test_forward_embedding""" + data = keras_mod.layers.Input(shape=(2, 4), dtype="int32") + x = keras_mod.layers.Embedding(10, 3)(data) + keras_model = keras_mod.models.Model(data, x) verify_keras_frontend(keras_model, need_transpose=False) - data = keras.layers.Input(shape=(2, 3, 4), dtype="int32") - x = keras.layers.Embedding(4, 5)(data) - keras_model = keras.models.Model(data, x) + data = keras_mod.layers.Input(shape=(2, 3, 4), dtype="int32") + x = keras_mod.layers.Embedding(4, 5)(data) + keras_model = keras_mod.models.Model(data, x) verify_keras_frontend(keras_model, need_transpose=False) - data = keras.layers.Input(shape=(6, 2, 3, 4), dtype="int32") - x = keras.layers.Embedding(4, 5)(data) - keras_model = keras.models.Model(data, x) + data = keras_mod.layers.Input(shape=(6, 2, 3, 4), dtype="int32") + x = keras_mod.layers.Embedding(4, 5)(data) + keras_model = keras_mod.models.Model(data, x) verify_keras_frontend(keras_model, need_transpose=False) - def test_forward_repeat_vector(self, keras): - data = keras.layers.Input(shape=(5,), dtype="float32") - x = keras.layers.Dense(6)(data) - x = keras.layers.RepeatVector(2)(x) + def test_forward_repeat_vector(self, keras_mod): + """test_forward_repeat_vector""" + data = keras_mod.layers.Input(shape=(5,), dtype="float32") + x = keras_mod.layers.Dense(6)(data) + x = keras_mod.layers.RepeatVector(2)(x) - keras_model = keras.models.Model(data, x) + keras_model = keras_mod.models.Model(data, x) verify_keras_frontend(keras_model, need_transpose=False) - data = keras.layers.Input(shape=(10,), dtype="float32") - x = keras.layers.RepeatVector(3)(data) - keras_model = keras.models.Model(data, x) + data = keras_mod.layers.Input(shape=(10,), dtype="float32") + x = keras_mod.layers.RepeatVector(3)(data) + keras_model = keras_mod.models.Model(data, x) verify_keras_frontend(keras_model, need_transpose=False) - data = keras.layers.Input(shape=(4,), dtype="float32") - x = keras.layers.RepeatVector(1)(data) - keras_model = keras.models.Model(data, x) + data = keras_mod.layers.Input(shape=(4,), dtype="float32") + x = keras_mod.layers.RepeatVector(1)(data) + keras_model = keras_mod.models.Model(data, x) verify_keras_frontend(keras_model, need_transpose=False) - def test_forward_global_pool3d(self, keras): - data = keras.layers.Input(shape=(32, 32, 32, 1)) + def test_forward_global_pool3d(self, keras_mod): + """test_forward_zero_padding3d""" + data = keras_mod.layers.Input(shape=(32, 32, 32, 1)) pool_funcs = [ # global maxpool - keras.layers.GlobalMaxPooling3D(), + keras_mod.layers.GlobalMaxPooling3D(), # global avgpool - keras.layers.GlobalAveragePooling3D(), + keras_mod.layers.GlobalAveragePooling3D(), ] for pool_func in pool_funcs: x = pool_func(data) - keras_model = keras.models.Model(data, x) + keras_model = keras_mod.models.Model(data, x) verify_keras_frontend(keras_model, layout="NDHWC") - def test_forward_nested_layers(self, keras): - MobileNet = get_mobilenet(keras) + def test_forward_nested_layers(self, keras_mod): + """test_forward_nested_layers""" + mobilenet_mod = get_mobilenet(keras_mod) - sub_model = MobileNet(include_top=False, weights="imagenet", input_shape=(224, 224, 3)) - keras_model = keras.Sequential( + sub_model = mobilenet_mod(include_top=False, weights="imagenet", input_shape=(224, 224, 3)) + keras_model = keras_mod.Sequential( [ sub_model, - keras.layers.GlobalAveragePooling2D(), - keras.layers.Dense(1024, activation="relu"), - keras.layers.Dense(2, activation="sigmoid"), + keras_mod.layers.GlobalAveragePooling2D(), + keras_mod.layers.Dense(1024, activation="relu"), + keras_mod.layers.Dense(2, activation="sigmoid"), ] ) verify_keras_frontend(keras_model) - def test_forward_l2_normalize(self, keras): - data = keras.layers.Input(shape=(16, 12, 8)) - K = keras.backend + def test_forward_l2_normalize(self, keras_mod): + """test_forward_l2_normalize""" + data = keras_mod.layers.Input(shape=(16, 12, 8)) + k_backend = keras_mod.backend l2_funcs = [ - keras.layers.Lambda(lambda v: K.l2_normalize(v, axis=-2)), - keras.layers.Lambda(lambda v: K.l2_normalize(x=v, axis=-1)), - keras.layers.Lambda(lambda v: K.l2_normalize(axis=1, x=v)), - keras.layers.Lambda(lambda v: K.l2_normalize(v, 2)), - keras.layers.Lambda(lambda v: K.l2_normalize(v, axis=3)), - keras.layers.Lambda(lambda v: K.l2_normalize(v, axis=(2, 3))), - keras.layers.Lambda(lambda v: K.l2_normalize(v, (1, 2))), - keras.layers.Lambda(lambda v: K.l2_normalize(v, axis=[-2, -1])), - keras.layers.Lambda(lambda v: K.l2_normalize(v, [-3, -2])), + keras_mod.layers.Lambda(lambda v: k_backend.l2_normalize(v, axis=-2)), + keras_mod.layers.Lambda(lambda v: k_backend.l2_normalize(x=v, axis=-1)), + keras_mod.layers.Lambda(lambda v: k_backend.l2_normalize(axis=1, x=v)), + keras_mod.layers.Lambda(lambda v: k_backend.l2_normalize(v, 2)), + keras_mod.layers.Lambda(lambda v: k_backend.l2_normalize(v, axis=3)), + keras_mod.layers.Lambda(lambda v: k_backend.l2_normalize(v, axis=(2, 3))), + keras_mod.layers.Lambda(lambda v: k_backend.l2_normalize(v, (1, 2))), + keras_mod.layers.Lambda(lambda v: k_backend.l2_normalize(v, axis=[-2, -1])), + keras_mod.layers.Lambda(lambda v: k_backend.l2_normalize(v, [-3, -2])), ] for l2_func in l2_funcs: x = l2_func(data) - keras_model = keras.models.Model(data, x) + keras_model = keras_mod.models.Model(data, x) verify_keras_frontend(keras_model, layout="NCHW") verify_keras_frontend(keras_model, layout="NHWC") - def test_forward_time_distributed(self, keras): - conv2d_inputs = keras.Input(shape=(10, 128, 128, 3)) - conv_2d_layer = keras.layers.Conv2D(64, (3, 3)) - conv2d_model = keras.models.Model( - conv2d_inputs, keras.layers.TimeDistributed(conv_2d_layer)(conv2d_inputs) + def test_forward_time_distributed(self, keras_mod): + """test_forward_time_distributed""" + conv2d_inputs = keras_mod.Input(shape=(10, 128, 128, 3)) + conv_2d_layer = keras_mod.layers.Conv2D(64, (3, 3)) + conv2d_model = keras_mod.models.Model( + conv2d_inputs, keras_mod.layers.TimeDistributed(conv_2d_layer)(conv2d_inputs) ) verify_keras_frontend(conv2d_model, layout="NDHWC") - dense_inputs = keras.Input(shape=(5, 1)) - dense_layer = keras.layers.Dense(1) - dense_model = keras.models.Model( - dense_inputs, keras.layers.TimeDistributed(dense_layer)(dense_inputs) + dense_inputs = keras_mod.Input(shape=(5, 1)) + dense_layer = keras_mod.layers.Dense(1) + dense_model = keras_mod.models.Model( + dense_inputs, keras_mod.layers.TimeDistributed(dense_layer)(dense_inputs) ) verify_keras_frontend(dense_model, need_transpose=False) @@ -694,39 +735,39 @@ def test_forward_time_distributed(self, keras): if __name__ == "__main__": for k in [keras, tf_keras]: sut = TestKeras() - sut.test_forward_merge_dot(keras=k) - sut.test_forward_merge(keras=k) - sut.test_forward_activations(keras=k) - sut.test_forward_dense(keras=k) - sut.test_forward_permute(keras=k) - sut.test_forward_sequential(keras=k) - sut.test_forward_pool(keras=k) - sut.test_forward_conv(keras=k) - sut.test_forward_conv1d(keras=k) - sut.test_forward_batch_norm(keras=k) - sut.test_forward_upsample(keras=k, interpolation="nearest") - sut.test_forward_upsample(keras=k, interpolation="bilinear") - sut.test_forward_reshape(keras=k) - sut.test_forward_crop(keras=k) - sut.test_forward_multi_inputs(keras=k) - sut.test_forward_multi_outputs(keras=k) - sut.test_forward_reuse_layers(keras=k) - sut.test_forward_lstm(keras=k) - sut.test_forward_rnn(keras=k) - sut.test_forward_vgg16(keras=k) - sut.test_forward_vgg16(keras=k, layout="NHWC") - sut.test_forward_xception(keras=k) - sut.test_forward_resnet50(keras=k) - sut.test_forward_resnet50(keras=k, layout="NHWC") - sut.test_forward_mobilenet(keras=k) - sut.test_forward_mobilenet(keras=k, layout="NHWC") - sut.test_forward_conv3d(keras=k) - sut.test_forward_conv3d_transpose(keras=k) - sut.test_forward_pool3d(keras=k) - sut.test_forward_global_pool3d(keras=k) - sut.test_forward_upsample3d(keras=k) - sut.test_forward_zero_padding3d(keras=k) - sut.test_forward_embedding(keras=k) - sut.test_forward_repeat_vector(keras=k) - sut.test_forward_l2_normalize(keras=k) - sut.test_forward_time_distributed(keras=k) + sut.test_forward_merge_dot(keras_mod=k) + sut.test_forward_merge(keras_mod=k) + sut.test_forward_activations(keras_mod=k) + sut.test_forward_dense(keras_mod=k) + sut.test_forward_permute(keras_mod=k) + sut.test_forward_sequential(keras_mod=k) + sut.test_forward_pool(keras_mod=k) + sut.test_forward_conv(keras_mod=k) + sut.test_forward_conv1d(keras_mod=k) + sut.test_forward_batch_norm(keras_mod=k) + sut.test_forward_upsample(keras_mod=k, interpolation="nearest") + sut.test_forward_upsample(keras_mod=k, interpolation="bilinear") + sut.test_forward_reshape(keras_mod=k) + sut.test_forward_crop(keras_mod=k) + sut.test_forward_multi_inputs(keras_mod=k) + sut.test_forward_multi_outputs(keras_mod=k) + sut.test_forward_reuse_layers(keras_mod=k) + sut.test_forward_lstm(keras_mod=k) + sut.test_forward_rnn(keras_mod=k) + sut.test_forward_vgg16(keras_mod=k) + sut.test_forward_vgg16(keras_mod=k, layout="NHWC") + sut.test_forward_xception(keras_mod=k) + sut.test_forward_resnet50(keras_mod=k) + sut.test_forward_resnet50(keras_mod=k, layout="NHWC") + sut.test_forward_mobilenet(keras_mod=k) + sut.test_forward_mobilenet(keras_mod=k, layout="NHWC") + sut.test_forward_conv3d(keras_mod=k) + sut.test_forward_conv3d_transpose(keras_mod=k) + sut.test_forward_pool3d(keras_mod=k) + sut.test_forward_global_pool3d(keras_mod=k) + sut.test_forward_upsample3d(keras_mod=k) + sut.test_forward_zero_padding3d(keras_mod=k) + sut.test_forward_embedding(keras_mod=k) + sut.test_forward_repeat_vector(keras_mod=k) + sut.test_forward_l2_normalize(keras_mod=k) + sut.test_forward_time_distributed(keras_mod=k) diff --git a/tests/python/frontend/oneflow/test_forward.py b/tests/python/frontend/oneflow/test_forward.py index 0d18a2fb5c21..5c68985670ea 100644 --- a/tests/python/frontend/oneflow/test_forward.py +++ b/tests/python/frontend/oneflow/test_forward.py @@ -14,19 +14,15 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. -# pylint: disable=import-self, invalid-name -# pylint: disable=arguments-differ, unused-argument, unused-import +# pylint: disable=arguments-differ, unused-argument """Unit tests for various models and operators""" import os -import sys import numpy as np -import pytest import tvm import tvm.testing import tvm.topi.testing from tvm import relay -from tvm.contrib import graph_executor import oneflow as flow @@ -41,7 +37,7 @@ def mkdir(path): if not os.path.exists(path): os.makedirs(path) else: - print("{} is already here".format(path)) + print(f"{path} is already here") def rmdir(path): @@ -69,23 +65,23 @@ def build(self, x): return out -class OneFlowGraph_v2(flow.nn.Graph): +class OneFlowGraphV2(flow.nn.Graph): def __init__(self, module): super().__init__() self.m = module - def build(self, x1, x2, x3): - out = self.m(x1, x2, x3) + def build(self, input_1, input_2, input_3): + out = self.m(input_1, input_2, input_3) return out -class OneFlowGraph_v3(flow.nn.Graph): +class OneFlowGraphV3(flow.nn.Graph): def __init__(self, module): super().__init__() self.m = module - def build(self, x1, x2): - out = self.m(x1, x2) + def build(self, input_1, input_2): + out = self.m(input_1, input_2) return out @@ -104,6 +100,7 @@ def get_oneflow_elementwise_output(model, input1, input2): def get_tvm_output(graph, model_path, inputs: flow.tensor, target="llvm", dtype="float32"): + """Generic function to execute and get tvm output""" inputs_numpy = inputs.numpy() if target == "llvm": device = tvm.cpu(0) @@ -126,6 +123,7 @@ def get_tvm_concat_output( target="llvm", dtype="float32", ): + """Generic function to execute and get tvm concat output""" input1_numpy = input1.numpy() input2_numpy = input2.numpy() input3_numpy = input3.numpy() @@ -154,6 +152,7 @@ def get_tvm_elementwise_output( target="llvm", dtype="float32", ): + """Generic function to execute and get tvm elementwise output""" input1_numpy = input1.numpy() input2_numpy = input2.numpy() if target == "llvm": @@ -183,6 +182,7 @@ def verify_conv( ), device="llvm", ): + """verify_conv""" if device == "cuda": model.to(device) inputs = inputs.to(device) @@ -212,6 +212,7 @@ def verify_pool( ), device="llvm", ): + """verify_pool""" if device == "cuda": model.to(device) inputs = inputs.to(device) @@ -241,6 +242,7 @@ def verify_normalization( ), device="llvm", ): + """verify_normalization""" if device == "cuda": model.to(device) inputs = inputs.to(device) @@ -271,6 +273,7 @@ def verify_upsample( ), device="llvm", ): + """verify_upsample""" if device == "cuda": model.to(device) inputs = inputs.to(device) @@ -300,6 +303,7 @@ def verify_convtran( ), device="llvm", ): + """verify_convtran""" if device == "cuda": model.to(device) inputs = inputs.to(device) @@ -329,6 +333,7 @@ def verify_activation( ), device="llvm", ): + """verify_activation""" if device == "cuda": model.to(device) inputs = inputs.to(device) @@ -358,6 +363,7 @@ def verify_math( ), device="llvm", ): + """verify_math""" if device == "cuda": model.to(device) inputs = inputs.to(device) @@ -385,12 +391,13 @@ def verify_matmul( inputs2=flow.tensor(np.random.randn(5, 2), dtype=flow.float32), device="llvm", ): + """verify_matmul""" if device == "cuda": model.to(device) inputs1 = inputs1.to(device) inputs2 = inputs2.to(device) - graph = OneFlowGraph_v3(model) + graph = OneFlowGraphV3(model) graph._compile(inputs1, inputs2) mkdir(MODEL_HOME) flow.save(model.state_dict(), MODEL_HOME) @@ -413,13 +420,14 @@ def verify_concat( inputs3=flow.tensor(np.random.randn(2, 5, 5, 3), dtype=flow.float32), device="llvm", ): + """verify_concat""" if device == "cuda": model.to(device) inputs1 = inputs1.to(device) inputs2 = inputs2.to(device) inputs3 = inputs3.to(device) - graph = OneFlowGraph_v2(model) + graph = OneFlowGraphV2(model) graph._compile(inputs1, inputs2, inputs3) mkdir(MODEL_HOME) @@ -436,6 +444,8 @@ def verify_concat( # defs/nn @tvm.testing.uses_gpu def test_conv2d(): + """Conv2d""" + class Conv2dModel(flow.nn.Module): def __init__(self): super().__init__() @@ -457,6 +467,8 @@ def forward(self, x): @tvm.testing.uses_gpu def test_pool2d(): + """Pool2d""" + class MaxPool2dModel(flow.nn.Module): def __init__(self): super().__init__() @@ -499,6 +511,8 @@ def forward(self, x): @tvm.testing.uses_gpu def test_normalization(): + """Normalization""" + class BatchNorm2dModel(flow.nn.Module): def __init__(self): super().__init__() @@ -519,6 +533,8 @@ def forward(self, x): @tvm.testing.uses_gpu def test_upsample(): + """Upsample""" + class UpsampleModel(flow.nn.Module): def __init__(self): super().__init__() @@ -550,6 +566,8 @@ def forward(self, x): @tvm.testing.uses_gpu def test_convtran(): + """ConvTran""" + class ConvTranModel(flow.nn.Module): def __init__(self): super().__init__() @@ -570,6 +588,8 @@ def forward(self, x): @tvm.testing.uses_gpu def test_activation(): + """Activation""" + class Softmax(flow.nn.Module): def __init__(self): super().__init__() @@ -679,9 +699,6 @@ def forward(self, x): return x class TensorSoftmax(flow.nn.Module): - def __init__(self): - super().__init__() - def forward(self, x): x = x.softmax(dim=-1) return x @@ -690,7 +707,7 @@ def forward(self, x): rmdir(MODEL_HOME) model1 = Softmax().eval() - model2 = Softplus().eval() + model2 = Softplus().eval() # pylint: disable=unused-variable model3 = Softsign().eval() model4 = Tanh().eval() model5 = ReLU().eval() @@ -725,6 +742,8 @@ def forward(self, x): @tvm.testing.uses_gpu def test_math(): + """Math""" + class Sigmoid(flow.nn.Module): def forward(self, x): return flow.sigmoid(x) @@ -785,6 +804,8 @@ def forward(self, x): @tvm.testing.uses_gpu def test_slice(): + """Slice""" + class Slice(flow.nn.Module): def forward(self, x): tup_list = [[None, None, None], [0, 5, 2], [0, 6, 3]] @@ -801,9 +822,11 @@ def forward(self, x): @tvm.testing.uses_gpu def test_concat(): + """Concat""" + class Concat(flow.nn.Module): - def forward(self, x1, x2, x3): - out = flow.cat([x1, x2, x3], dim=-1) + def forward(self, input_1, input_2, input_3): + out = flow.cat([input_1, input_2, input_3], dim=-1) return out model = Concat().eval() @@ -814,6 +837,8 @@ def forward(self, x1, x2, x3): @tvm.testing.uses_gpu def test_add_constant(): + """ConstantAdd""" + class ConstantAdd(flow.nn.Module): def forward(self, x): out = flow.add(1.0, x) @@ -857,6 +882,8 @@ def forward(self, x): @tvm.testing.uses_gpu def test_matmul(): + """MatMul""" + class MatMul(flow.nn.Module): def forward(self, x, y): return flow._C.matmul(x, y) diff --git a/tests/python/frontend/oneflow/test_vision_models.py b/tests/python/frontend/oneflow/test_vision_models.py index e8d0627001ca..8a573e0f51e3 100644 --- a/tests/python/frontend/oneflow/test_vision_models.py +++ b/tests/python/frontend/oneflow/test_vision_models.py @@ -15,18 +15,15 @@ # specific language governing permissions and limitations # under the License. # pylint: disable=import-self, invalid-name -# pylint: disable=arguments-differ, unused-argument, unused-import +# pylint: disable=arguments-differ, unused-argument """Unit tests for various models and operators""" import os -import sys import numpy as np -import pytest import tvm import tvm.testing import tvm.topi.testing from tvm import relay -from tvm.contrib import graph_executor import oneflow as flow from flowvision.models.alexnet import alexnet @@ -47,7 +44,7 @@ def mkdir(path): if not os.path.exists(path): os.makedirs(path) else: - print("{} is already here".format(path)) + print(f"{path} is already here") def rmdir(path): @@ -81,6 +78,7 @@ def get_oneflow_output(model, inputs): def get_tvm_output(graph, model_path, inputs: flow.tensor, target="llvm", dtype="float32"): + """Generic function to execute and get tvm output""" inputs_numpy = inputs.numpy() if target == "llvm": device = tvm.cpu(0) @@ -105,6 +103,7 @@ def verify_model( ), device="llvm", ): + """Generic function to generate and compare oneflow and TVM output""" if device == "cuda": model.to(device) inputs = inputs.to(device) @@ -125,6 +124,7 @@ def verify_model( @tvm.testing.uses_gpu def test_vision_models(): + """Vision models test""" if os.path.exists(MODEL_HOME): rmdir(MODEL_HOME) diff --git a/tests/python/frontend/onnx/test_forward.py b/tests/python/frontend/onnx/test_forward.py index fff928ddd4a0..e1e5c3ea8bc5 100644 --- a/tests/python/frontend/onnx/test_forward.py +++ b/tests/python/frontend/onnx/test_forward.py @@ -14,16 +14,22 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. +# pylint: disable=unused-argument +""" +ONNX testcases +================ +This article is a test script to test ONNX operator with Relay. +""" import glob import os import platform import re - -import numpy as np +import copy +import tempfile import pytest import scipy -import torch -import torchvision +import numpy as np + import tvm import tvm.testing import tvm.topi.testing @@ -31,10 +37,17 @@ from tvm.contrib import graph_executor import onnx +import onnxruntime.backend from onnx import TensorProto, helper, mapping, numpy_helper +from onnxruntime.quantization import CalibrationDataReader, quantize_static + +import torch +import torchvision +from torch.nn import Linear, Module, Sequential def get_input_data_shape_dict(graph_def, input_data): + """Get input data shape""" if isinstance(input_data, list): input_names = {} shape_dict = {} @@ -106,13 +119,11 @@ def get_tvm_output( m = graph_executor.create(graph, lib, dev) # set inputs if isinstance(input_data, list): - for i, e in enumerate(input_names): + for i, _ in enumerate(input_names): # Its possible for some onnx inputs to not be needed in the tvm # module, confirm its present before setting. - try: - m.set_input(input_names[i], tvm.nd.array(input_data[i].astype(input_data[i].dtype))) - except: - continue + # pylint: disable=unnecessary-list-index-lookup + m.set_input(input_names[i], tvm.nd.array(input_data[i].astype(input_data[i].dtype))) else: m.set_input(input_names, tvm.nd.array(input_data.astype(input_data.dtype))) @@ -132,8 +143,7 @@ def get_tvm_output( def get_onnxruntime_output(model, inputs): - import onnxruntime.backend - + """Generic function to generate onnxruntime output""" rep = onnxruntime.backend.prepare(model.SerializeToString(), "CPU") if isinstance(inputs, list) and len(inputs) == 1: inp = inputs[0] @@ -162,6 +172,7 @@ def verify_with_ort_with_inputs( opt_level=1, convert_config=None, ): + """verify_with_ort_with_inputs""" if opset is not None: model.opset_import[0].version = opset @@ -214,6 +225,7 @@ def verify_with_ort( rtol=1e-5, atol=1e-5, ): + """verify_with_ort""" inputs = [np.random.uniform(size=ishape).astype(dtype) for ishape in input_shapes] verify_with_ort_with_inputs( model, @@ -233,11 +245,11 @@ def verify_with_ort( def quantize_and_verify_with_ort( onnx_model, input_names, input_shapes, target, dev, rtol=1e-5, atol=1e-5 ): - from onnxruntime.quantization import CalibrationDataReader, QuantType, quantize_static - + """quantize_and_verify_with_ort""" input_arrays = [np.random.random(shape).astype("float32") for shape in input_shapes] class RandomDataReader(CalibrationDataReader): + # pylint: disable=missing-class-docstring def __init__(self, n=10): input_dict = dict(zip(input_names, input_shapes)) self.data = iter( @@ -253,11 +265,13 @@ def __init__(self, n=10): def get_next(self): return next(self.data, None) - d = tvm.contrib.utils.tempdir() - model_fp32 = os.path.join(d.temp_dir, "model.onnx") + t_dir = tvm.contrib.utils.tempdir() + model_fp32 = os.path.join(t_dir.temp_dir, "model.onnx") onnx.save_model(onnx_model, model_fp32) - model_quant = os.path.join(d.temp_dir, "model.quant.onnx") - quantized_model = quantize_static(model_fp32, model_quant, RandomDataReader()) + model_quant = os.path.join(t_dir.temp_dir, "model.quant.onnx") + _ = quantize_static( # pylint: disable=assignment-from-no-return + model_fp32, model_quant, RandomDataReader() + ) # opt_level=1 will cause error with qnn lowering model = onnx.load(model_quant) verify_with_ort_with_inputs( @@ -282,6 +296,7 @@ def is_version_greater_than(ver): @tvm.testing.parametrize_targets def test_reshape(target, dev): + """test_reshape""" in_shape = (4, 3, 3, 4) ref_shape = (6, 2, 4, 3) @@ -315,6 +330,7 @@ def test_reshape(target, dev): @tvm.testing.parametrize_targets def test_double_reshape(target, dev): + """test_double_reshape""" in_shape = (4, 3, 3, 4) ref_shape = (6, 2, 4, 3) @@ -350,6 +366,8 @@ def test_double_reshape(target, dev): @tvm.testing.parametrize_targets def test_expand(target, dev): + """test_expand""" + def _test_expand(name, data, shape, ref_data, dtype="int32"): shape_array = np.array(shape) if dtype == "int32": @@ -377,7 +395,7 @@ def _test_expand(name, data, shape, ref_data, dtype="int32"): ), ) else: - raise "Invalid dtype" + raise TypeError("Invalid dtype") expand_node = helper.make_node("Expand", ["in", "shape"], ["out"]) graph = helper.make_graph( @@ -416,9 +434,11 @@ def _test_expand(name, data, shape, ref_data, dtype="int32"): @tvm.testing.parametrize_targets def test_depth_to_space(target, dev): - def verify_depth_to_space(inshape, outshape, mode, blockSize): + """test_depth_to_space""" + + def verify_depth_to_space(inshape, outshape, mode, block_size): node = onnx.helper.make_node( - "DepthToSpace", inputs=["x"], outputs=["y"], blocksize=blockSize + "DepthToSpace", inputs=["x"], outputs=["y"], blocksize=block_size ) graph = helper.make_graph( @@ -435,14 +455,16 @@ def verify_depth_to_space(inshape, outshape, mode, blockSize): # current onnx.checker use OpSet-1 version of DepthToSpace, which doesn't have a mode argument. # TO-DO, we can add mode argument to test CRD mode and DCR mode # in the future when we update to a newer onnx version. - verify_depth_to_space((1, 8, 2, 3), (1, 2, 4, 6), mode="CRD", blockSize=2) + verify_depth_to_space((1, 8, 2, 3), (1, 2, 4, 6), mode="CRD", block_size=2) @tvm.testing.parametrize_targets def test_space_to_depth(target, dev): - def verify_space_to_depth(inshape, outshape, blockSize): + """test_space_to_depth""" + + def verify_space_to_depth(inshape, outshape, block_size): node = onnx.helper.make_node( - "SpaceToDepth", inputs=["x"], outputs=["y"], blocksize=blockSize + "SpaceToDepth", inputs=["x"], outputs=["y"], blocksize=block_size ) graph = helper.make_graph( @@ -461,6 +483,7 @@ def verify_space_to_depth(inshape, outshape, blockSize): @tvm.testing.parametrize_targets def test_shape(target, dev): + """test_shape""" in_shape = (4, 3, 3, 4) ref_shape = (6, 2, 4, 3) @@ -496,6 +519,8 @@ def test_shape(target, dev): @tvm.testing.parametrize_targets def test_power(target, dev): + """test_power""" + def _test_power_iteration(x_shape, y_shape): if isinstance(y_shape, int): y_shape = [y_shape] @@ -529,6 +554,8 @@ def _test_power_iteration(x_shape, y_shape): @tvm.testing.parametrize_targets def test_range(target, dev): + """test_range""" + def verify_range(start, limit, delta, dtype): dtype_map = { "float32": TensorProto.FLOAT, @@ -564,6 +591,8 @@ def verify_range(start, limit, delta, dtype): @tvm.testing.parametrize_targets def test_squeeze(target, dev): + """test_squeeze""" + def test_squeeze_once(in_shape, out_shape, axes=None): y = helper.make_node("Squeeze", ["in"], ["out"], axes=axes) @@ -585,6 +614,8 @@ def test_squeeze_once(in_shape, out_shape, axes=None): @tvm.testing.parametrize_targets def test_flatten(target, dev): + """test_flatten""" + def verify_flatten(in_shape, axis, ref_shape): flatten_node = helper.make_node("Flatten", ["in"], ["out"], axis=axis) @@ -604,6 +635,7 @@ def verify_flatten(in_shape, axis, ref_shape): @tvm.testing.parametrize_targets def test_unsqueeze(target, dev): + """test_unsqueeze""" in_shape = (3, 3) axis = (0, 3, 4) out_shape = (1, 3, 3, 1, 1) @@ -622,6 +654,8 @@ def test_unsqueeze(target, dev): @tvm.testing.parametrize_targets def test_gather(target, dev): + """test_gather""" + def verify_gather(in_shape, indices, axis, dtype): x = np.random.uniform(size=in_shape).astype(dtype) indices = np.array(indices, dtype="int64") @@ -657,6 +691,7 @@ def verify_gather(in_shape, indices, axis, dtype): @tvm.testing.parametrize_targets def test_dynamic_gather(target, dev): + """test_dynamic_gather""" dtype = "float32" in_shape = [2, 2] indices = 1 @@ -702,6 +737,8 @@ def test_dynamic_gather(target, dev): @tvm.testing.parametrize_targets def test_gatherelements(target, dev): + """test_gatherelements""" + def verify_gatherelements(in_shape, indices, axis): x = np.random.uniform(size=in_shape).astype("float32") indices = np.array(indices, dtype="int32") @@ -736,6 +773,8 @@ def verify_gatherelements(in_shape, indices, axis): @tvm.testing.parametrize_targets def test_scatter(target, dev): + """test_scatter""" + def verify_scatter(in_shape, indices, axis): x = np.random.uniform(size=in_shape).astype("float32") indices = np.array(indices, dtype="int32") @@ -768,6 +807,8 @@ def verify_scatter(in_shape, indices, axis): @tvm.testing.parametrize_targets def test_slice(target, dev): + """test_slice""" + def _test_slice_iteration_v1(indata, outdata, starts, ends, axes=None): if axes: y = helper.make_node("Slice", ["in"], ["out"], axes=axes, starts=starts, ends=ends) @@ -1013,6 +1054,7 @@ def test_ceil(target, dev): @tvm.testing.parametrize_targets def test_clip(target, dev): + """test_clip""" _test_onnx_op_elementwise( target, dev, @@ -1052,6 +1094,7 @@ def test_clip(target, dev): @tvm.testing.parametrize_targets def test_clip_min_max_as_inputs(target, dev): + """test_clip_min_max_as_inputs""" input_shape = (2, 4, 5, 6) nodes = [ make_constant_node("min", onnx.TensorProto.FLOAT, (), [0.0]), @@ -1104,11 +1147,14 @@ def test_isinf(target, dev): @tvm.testing.parametrize_targets def test_isnan(target, dev): + """test_isnan""" _test_finite_ops(target, dev, (2, 4, 5, 6), np.isnan, {}, "float32", "IsNaN", {}) @tvm.testing.parametrize_targets def test_gather_nd(target, dev): + """test_gather_nd""" + def verify_gather_nd(in_shape, indices, out_shape, dtype="float32", batch_dims=0, opset=11): x = np.random.uniform(size=in_shape).astype(dtype) indices = np.array(indices, dtype="int64") @@ -1157,6 +1203,7 @@ def verify_gather_nd(in_shape, indices, out_shape, dtype="float32", batch_dims=0 @tvm.testing.parametrize_targets def test_onehot(target, dev): + """test_onehot""" indices_shape = [10] indices_array = np.random.randint(low=0, high=9, size=indices_shape, dtype="int32") depth = 10 @@ -1187,6 +1234,8 @@ def test_onehot(target, dev): @tvm.testing.parametrize_targets def test_gemm(target, dev): + """test_gemm""" + def verify_gemm(a_shape, b_shape, c_shape=None, freeze_params=False, dtype="float32"): out_shape = [a_shape[0], b_shape[1]] a_array = np.random.uniform(size=a_shape).astype(dtype) @@ -1238,6 +1287,8 @@ def verify_gemm(a_shape, b_shape, c_shape=None, freeze_params=False, dtype="floa @tvm.testing.parametrize_targets def test_matmul(target, dev): + """test_matmul""" + def test_one_matmul(a_shape, b_shape): if len(a_shape) == 1: out_shape = [b_shape[1]] @@ -1268,6 +1319,8 @@ def test_one_matmul(a_shape, b_shape): @tvm.testing.parametrize_targets def test_batch_matmul(target, dev): + """test_batch_matmul""" + def verify_batch_matmul(a_shape, b_shape, out_shape, convert_config=None): a_array = np.random.uniform(size=a_shape).astype("float32") b_array = np.random.uniform(size=b_shape).astype("float32") @@ -1316,6 +1369,7 @@ def verify_batch_matmul(a_shape, b_shape, out_shape, convert_config=None): @tvm.testing.parametrize_targets def test_use_nt_batch_matmul(target, dev): + """test_use_nt_batch_matmul""" a_shape = (2, 3, 4) b_shape = (2, 4, 3) out_shape = [2, 3, 3] @@ -1349,6 +1403,8 @@ def test_use_nt_batch_matmul(target, dev): @tvm.testing.parametrize_targets def test_matmulinteger16(target, dev): + """test_matmulinteger16""" + def verify_matmulinteger16(a_shape, b_shape, out_shape): a_dtype = "int16" b_dtype = "int16" @@ -1389,6 +1445,8 @@ def verify_matmulinteger16(a_shape, b_shape, out_shape): def verify_simple_dynamic_model(a_shape, b_shape, target, dev): + """verify_simple_dynamic_model""" + def verify_model(model, a_shape, b_shape): a_array = np.random.uniform(size=a_shape).astype("float32") b_array = np.random.uniform(size=b_shape).astype("float32") @@ -1423,7 +1481,7 @@ def verify_model(model, a_shape, b_shape): a_anys = [relay.Any()] * len(a_shape) b_anys = [relay.Any()] * len(b_shape) - mod, params = relay.frontend.from_onnx(model, {"a": a_anys, "b": b_anys}) + mod, _ = relay.frontend.from_onnx(model, {"a": a_anys, "b": b_anys}) model = relay.create_executor("vm", mod=mod, device=dev, target=target).evaluate() verify_model(model, a_shape, b_shape) verify_model(model, [a * 2 for a in a_shape], [b * 2 for b in b_shape]) @@ -1440,10 +1498,12 @@ def test_batch_matmul_dynamic_model(target, dev): @tvm.testing.parametrize_targets def test_lrn(target, dev): + """test_lrn""" + def verify_lrn(shape, nsize, dtype, alpha=None, beta=None, bias=None): in_array = np.random.uniform(size=shape).astype(dtype) - if alpha == None and beta == None and bias == None: + if alpha is None and beta is None and bias is None: alpha = 0.0001 beta = 0.75 bias = 1.0 @@ -1468,6 +1528,8 @@ def verify_lrn(shape, nsize, dtype, alpha=None, beta=None, bias=None): @tvm.testing.parametrize_targets def test_instance_norm(target, dev): + """test_instance_norm""" + def verify_instance_norm(shape, axis=1): x = np.random.randn(*shape).astype(np.float32) gamma = np.random.randn(shape[1]).astype(np.float32) @@ -1503,6 +1565,7 @@ def verify_instance_norm(shape, axis=1): @tvm.testing.parametrize_targets def test_upsample_nearest(target, dev): + """test_upsample_nearest""" scale = 2 in_shape = (1, 1, 3, 3) out_shape = (1, 1, 3 * scale, 3 * scale) @@ -1523,6 +1586,7 @@ def test_upsample_nearest(target, dev): @tvm.testing.parametrize_targets def test_upsample3d_nearest(target, dev): + """test_upsample3d_nearest""" scale = 2 in_shape = (1, 1, 3, 3, 3) out_shape = (1, 1, 3 * scale, 3 * scale, 3 * scale) @@ -1546,6 +1610,7 @@ def test_upsample3d_nearest(target, dev): @tvm.testing.parametrize_targets def test_upsample_bilinear(target, dev): + """test_upsample_bilinear""" scale = 2 in_shape = (1, 1, 3, 3) out_shape = (1, 1, 3 * scale, 3 * scale) @@ -1566,6 +1631,7 @@ def test_upsample_bilinear(target, dev): @tvm.testing.parametrize_targets def test_upsample3d_trilinear(target, dev): + """test_upsample3d_trilinear""" scale = 2 in_shape = (1, 1, 3, 3, 3) out_shape = (1, 1, 3 * scale, 3 * scale, 3 * scale) @@ -1611,6 +1677,8 @@ def test_upsample3d_trilinear(target, dev): @tvm.testing.known_failing_targets("cuda") @tvm.testing.parametrize_targets def test_softmax(target, dev): + """test_softmax""" + def verify_softmax(inshape, axis, opset=None, dynamic=False): opname = "Softmax" outshape = inshape @@ -1661,6 +1729,8 @@ def verify_softmax(inshape, axis, opset=None, dynamic=False): @tvm.testing.parametrize_targets def test_forward_min(target, dev): + """test_forward_min""" + def verify_min(input_dim): dtype = "float32" @@ -1690,6 +1760,8 @@ def verify_min(input_dim): @tvm.testing.parametrize_targets def test_forward_max(target, dev): + """test_forward_max""" + def verify_max(input_dim): dtype = "float32" @@ -1719,6 +1791,8 @@ def verify_max(input_dim): @tvm.testing.parametrize_targets def test_forward_mean(target, dev): + """test_forward_mean""" + def verify_mean(input_dim): dtype = "float32" @@ -1748,6 +1822,8 @@ def verify_mean(input_dim): @tvm.testing.parametrize_targets def test_forward_hardsigmoid(target, dev): + """test_forward_hardsigmoid""" + def verify_hardsigmoid(input_dim, alpha, beta): dtype = "float32" @@ -1775,11 +1851,13 @@ def verify_hardsigmoid(input_dim, alpha, beta): @tvm.testing.known_failing_targets("cuda") @tvm.testing.parametrize_targets def test_forward_arg_min_max(target, dev): + """test_forward_arg_min_max""" + def verify_argreduce(input_dim, op_name, axis=None, keepdims=None): a_np1 = np.random.uniform(-10, 10, input_dim).astype(np.int32) out_shape = list(a_np1.shape) def_axis = axis if axis is not None else 0 - if keepdims == 1 or keepdims == None: + if keepdims == 1 or keepdims is None: out_shape[def_axis] = 1 else: out_shape.pop(def_axis) @@ -1803,7 +1881,7 @@ def verify_argreduce(input_dim, op_name, axis=None, keepdims=None): model = helper.make_model(graph, producer_name="argreduce_test") verify_with_ort_with_inputs(model, [a_np1], target=target, dev=dev) - """Verify argmin and argmax""" + # Verify argmin and argmax verify_argreduce([3, 4, 4], "ArgMin") verify_argreduce([3, 4, 4], "ArgMax") verify_argreduce([3, 4, 4], "ArgMin", axis=1) @@ -1818,6 +1896,8 @@ def verify_argreduce(input_dim, op_name, axis=None, keepdims=None): @tvm.testing.parametrize_targets def test_constantofshape(target, dev): + """test_constantofshape""" + def verify_constantofshape(input_dim, value, dtype): fill_node = helper.make_node( "ConstantOfShape", @@ -1852,6 +1932,8 @@ def verify_constantofshape(input_dim, value, dtype): @tvm.testing.parametrize_targets def test_pad(target, dev): + """test_pad""" + def verify_pad(indata, pads, mode="constant", value=0.0): indata = np.array(indata).astype(np.float32) # numpy expect result @@ -1953,6 +2035,8 @@ def verify_pad_v11(indata, pads, mode="constant", value=0.0): @tvm.testing.parametrize_targets def test_all_reduce_funcs(target, dev): + """test_all_reduce_funcs""" + def verify_reduce_func(func, data, axis, keepdims): inshape = data.shape outshape = np.sum(data, axis=axis, keepdims=keepdims == 1).shape @@ -2028,6 +2112,8 @@ def verify_reduce_func(func, data, axis, keepdims): @tvm.testing.parametrize_targets def test_split(target, dev): + """test_split""" + def verify_split(indata, outdatas, split, axis=0, pass_split=True, opset=11): indata = np.array(indata).astype(np.float32) outdatas = [np.array(o).astype(np.float32) for o in outdatas] @@ -2056,7 +2142,7 @@ def verify_split(indata, outdatas, split, axis=0, pass_split=True, opset=11): node = helper.make_node( "Split", inputs=input_names, - outputs=["output_{}".format(i) for i in range(len(split_index))], + outputs=[f"output_{i}" for i in range(len(split_index))], axis=axis, ) @@ -2071,7 +2157,7 @@ def verify_split(indata, outdatas, split, axis=0, pass_split=True, opset=11): initializer=initializer, outputs=[ helper.make_tensor_value_info( - "output_{}".format(i), TensorProto.FLOAT, list(outdatas[i].shape) + f"output_{i}", TensorProto.FLOAT, list(outdatas[i].shape) ) for i in range(len(split_index)) ], @@ -2122,14 +2208,15 @@ def verify_split(indata, outdatas, split, axis=0, pass_split=True, opset=11): @tvm.testing.parametrize_targets def test_binary_ops(target, dev): + """test_binary_ops""" in_shape = (1, 2, 3, 3) dtype = "float32" out_shape = in_shape def verify_binary_ops(op, x, y, out_type="float32"): - z = helper.make_node(op, ["in1", "in2"], ["out"]) + out = helper.make_node(op, ["in1", "in2"], ["out"]) graph = helper.make_graph( - [z], + [out], "_test", inputs=[ helper.make_tensor_value_info("in1", TensorProto.FLOAT, x.shape), @@ -2146,41 +2233,42 @@ def verify_binary_ops(op, x, y, out_type="float32"): x = np.random.uniform(size=in_shape).astype(dtype) y = np.random.uniform(size=in_shape).astype(dtype) - z = np.random.uniform(size=(3,)).astype(dtype) + z_array = np.random.uniform(size=(3,)).astype(dtype) verify_binary_ops("Add", x, y) - verify_binary_ops("Add", x, z) + verify_binary_ops("Add", x, z_array) verify_binary_ops("Sub", x, y) - verify_binary_ops("Sub", x, z) + verify_binary_ops("Sub", x, z_array) verify_binary_ops("Mul", x, y) - verify_binary_ops("Mul", x, z) + verify_binary_ops("Mul", x, z_array) verify_binary_ops("Div", x, y) - verify_binary_ops("Div", x, z) + verify_binary_ops("Div", x, z_array) verify_binary_ops("Sum", x, y) - verify_binary_ops("Sum", x, z) + verify_binary_ops("Sum", x, z_array) verify_binary_ops("Greater", x, y, "bool") - verify_binary_ops("Greater", x, z, "bool") + verify_binary_ops("Greater", x, z_array, "bool") verify_binary_ops("GreaterOrEqual", x, y, "bool") - verify_binary_ops("GreaterOrEqual", x, z, "bool") + verify_binary_ops("GreaterOrEqual", x, z_array, "bool") verify_binary_ops("Less", x, y, "bool") - verify_binary_ops("Less", x, z, "bool") + verify_binary_ops("Less", x, z_array, "bool") verify_binary_ops("LessOrEqual", x, y, "bool") - verify_binary_ops("LessOrEqual", x, z, "bool") + verify_binary_ops("LessOrEqual", x, z_array, "bool") verify_binary_ops("Equal", x, y, "bool") - verify_binary_ops("Equal", x, z, "bool") + verify_binary_ops("Equal", x, z_array, "bool") @tvm.testing.parametrize_targets def test_unary_ops(target, dev): + """test_unary_ops""" in_shape = (1, 2, 3, 3) - dtype = "float32" + _ = "float32" out_shape = in_shape def verify_unary_ops(op, x, rtol=1e-5, atol=1e-5, dtype="float32"): x = x.astype(dtype) ONNX_DTYPE = mapping.NP_TYPE_TO_TENSOR_TYPE[np.dtype(dtype)] - z = helper.make_node(op, ["in1"], ["out"]) + out = helper.make_node(op, ["in1"], ["out"]) graph = helper.make_graph( - [z], + [out], "_test", inputs=[ helper.make_tensor_value_info("in1", ONNX_DTYPE, list(in_shape)), @@ -2262,6 +2350,8 @@ def selu_x(x, alpha, gamma): @tvm.testing.parametrize_targets def test_prelu(target, dev): + """test_prelu""" + def verify_prelu(x_shape, a_shape): node = helper.make_node("PRelu", inputs=["X", "slope"], outputs=["Y"]) @@ -2294,8 +2384,8 @@ def verify_prelu(x_shape, a_shape): @tvm.testing.parametrize_targets -def test_ThresholdedRelu(target, dev): - def ThresholdedRelu_x(x, alpha): +def test_thresholded_relu(target, dev): + def thresholded_relu_x(x, alpha): out_np = np.clip(x, alpha, np.inf) out_np[out_np == alpha] = 0 return out_np @@ -2304,7 +2394,7 @@ def ThresholdedRelu_x(x, alpha): target, dev, (2, 4, 5, 6), - ThresholdedRelu_x, + thresholded_relu_x, {"alpha": 0.25}, "float32", "ThresholdedRelu", @@ -2313,7 +2403,7 @@ def ThresholdedRelu_x(x, alpha): @tvm.testing.parametrize_targets -def test_LogSoftmax(target, dev): +def test_logsoftmax(target, dev): _test_onnx_op_elementwise( target, dev, @@ -2328,7 +2418,7 @@ def test_LogSoftmax(target, dev): def check_torch_conversion(model, input_size, target, dev): dummy_input = torch.randn(*input_size) - file_name = "{}.onnx".format(model.__name__) + file_name = f"{model.__name__}.onnx" # Set verbose=True for more output torch.onnx.export(model(), dummy_input, file_name, export_params=True, verbose=False) onnx_model = onnx.load(file_name) @@ -2379,14 +2469,16 @@ def test_inception(target, dev): @tvm.testing.parametrize_targets def test_sign(target, dev): - def Sign_x(x): + def sign_x(x): return np.sign(x) - _test_onnx_op_elementwise(target, dev, (3, 4, 5, 6), Sign_x, {}, "float32", "Sign", {}) + _test_onnx_op_elementwise(target, dev, (3, 4, 5, 6), sign_x, {}, "float32", "Sign", {}) @tvm.testing.parametrize_targets def test_not(target, dev): + """test_not""" + def verify_not(indata, dtype): x = indata.astype(dtype) @@ -2416,6 +2508,8 @@ def verify_not(indata, dtype): @tvm.testing.parametrize_targets def test_and(target, dev): + """test_and""" + def verify_and(indata, dtype): x = indata[0].astype(dtype) y = indata[1].astype(dtype) @@ -2468,6 +2562,8 @@ def verify_and(indata, dtype): @tvm.testing.parametrize_targets def test_tile(target, dev): + """test_tile""" + def verify_tile_v6(indata, repeats, outdata): node = helper.make_node("Tile", inputs=["input", "repeats"], outputs=["out"]) graph = helper.make_graph( @@ -2487,12 +2583,14 @@ def verify_tile_v6(indata, repeats, outdata): x = np.random.rand(2, 3, 4, 5).astype(np.float32) repeats = np.random.randint(low=1, high=10, size=(np.ndim(x),)).astype(np.int64) - z = np.tile(x, repeats) - verify_tile_v6(x, repeats, z) + z_array = np.tile(x, repeats) + verify_tile_v6(x, repeats, z_array) @tvm.testing.parametrize_targets def test_erf(target, dev): + """test_erf""" + def verify_erf(indata, outdata): node = helper.make_node("Erf", inputs=["in"], outputs=["out"]) graph = helper.make_graph( @@ -2505,12 +2603,14 @@ def verify_erf(indata, outdata): verify_with_ort_with_inputs(model, [indata], [outdata.shape], target=target, dev=dev) x = np.random.rand(2, 3, 4, 6).astype(np.float32) - z = scipy.special.erf(x) - verify_erf(x, z) + z_array = scipy.special.erf(x) + verify_erf(x, z_array) @tvm.testing.parametrize_targets def test_where(target, dev): + """test_where""" + def verify_where(condition, x, y, dtype, outdata, dynamic=False): node_list = [] where_inputs = ["condition", "x", "y"] @@ -2578,6 +2678,8 @@ def verify_where(condition, x, y, dtype, outdata, dynamic=False): @tvm.testing.parametrize_targets def test_or(target, dev): + """test_or""" + def verify_or(indata, dtype): x = indata[0].astype(dtype) y = indata[1].astype(dtype) @@ -2630,6 +2732,8 @@ def verify_or(indata, dtype): @tvm.testing.parametrize_targets def test_batch_norm(target, dev): + """test_batch_norm""" + def verify_batch_norm(in_shape): batchnorm = onnx.helper.make_node( "BatchNormalization", inputs=["x", "scale", "B", "mean", "var"], outputs=["Y"] @@ -2662,6 +2766,8 @@ def verify_batch_norm(in_shape): @tvm.testing.parametrize_targets def test_batch_norm_dynamic_subgraph(target, dev): + """test_batch_norm_dynamic_subgraph""" + def verify_batch_norm_dynamic_subgraph(in_shape, o_shape): batchnorm = onnx.helper.make_node( @@ -2695,6 +2801,8 @@ def verify_batch_norm_dynamic_subgraph(in_shape, o_shape): @tvm.testing.parametrize_targets def test_conv(target, dev): + """test_conv""" + def verify_conv( x_shape, w_shape, @@ -2721,9 +2829,9 @@ def verify_conv( elif padding is None: ## autopadding with unset default attributes kwargs = {} - if not all([s == 1 for s in strides]): + if not all(list(s == 1 for s in strides)): kwargs["strides"] = strides - if not all([d == 1 for d in dilations]): + if not all(list(d == 1 for d in dilations)): kwargs["dilations"] = dilations node = helper.make_node( @@ -2769,112 +2877,114 @@ def verify_conv( dev=dev, ) - def repeat(N, D): - return tuple([N for _ in range(D)]) + def repeat(num, dims): + return tuple(num for _ in range(dims)) - for D in [1, 2, 3]: + for dims in [1, 2, 3]: # Convolution with padding verify_conv( - (1, 1) + repeat(5, D), - (1, 1) + repeat(3, D), - (1, 1) + repeat(5, D), - 2 * repeat(1, D), - repeat(3, D), - repeat(1, D), - repeat(1, D), + (1, 1) + repeat(5, dims), + (1, 1) + repeat(3, dims), + (1, 1) + repeat(5, dims), + 2 * repeat(1, dims), + repeat(3, dims), + repeat(1, dims), + repeat(1, dims), ) # Convolution with asymmetric padding verify_conv( - (1, 1) + repeat(5, D), - (1, 1) + repeat(3, D), - (1, 1) + repeat(4, D), - repeat(0, D) + repeat(1, D), - repeat(3, D), - repeat(1, D), - repeat(1, D), + (1, 1) + repeat(5, dims), + (1, 1) + repeat(3, dims), + (1, 1) + repeat(4, dims), + repeat(0, dims) + repeat(1, dims), + repeat(3, dims), + repeat(1, dims), + repeat(1, dims), ) # Convolution without padding verify_conv( - (1, 1) + repeat(5, D), - (1, 1) + repeat(3, D), - (1, 1) + repeat(3, D), - 2 * repeat(0, D), - repeat(3, D), - repeat(1, D), - repeat(1, D), + (1, 1) + repeat(5, dims), + (1, 1) + repeat(3, dims), + (1, 1) + repeat(3, dims), + 2 * repeat(0, dims), + repeat(3, dims), + repeat(1, dims), + repeat(1, dims), ) # Convolution with autopadding verify_conv( - (1, 1) + repeat(5, D), - (1, 1) + repeat(3, D), - (1, 1) + repeat(5, D), + (1, 1) + repeat(5, dims), + (1, 1) + repeat(3, dims), + (1, 1) + repeat(5, dims), None, - repeat(3, D), - repeat(1, D), - repeat(1, D), + repeat(3, dims), + repeat(1, dims), + repeat(1, dims), auto_pad="SAME_UPPER", ) # Convolution with valid autopadding verify_conv( - (1, 1) + repeat(5, D), - (1, 1) + repeat(3, D), - (1, 1) + repeat(3, D), + (1, 1) + repeat(5, dims), + (1, 1) + repeat(3, dims), + (1, 1) + repeat(3, dims), None, - repeat(3, D), - repeat(1, D), - repeat(1, D), + repeat(3, dims), + repeat(1, dims), + repeat(1, dims), auto_pad="VALID", ) # Convolution with unset padding verify_conv( - (1, 1) + repeat(5, D), - (1, 1) + repeat(3, D), - (1, 1) + repeat(3, D), - 2 * repeat(0, D), - repeat(3, D), - repeat(1, D), - repeat(1, D), + (1, 1) + repeat(5, dims), + (1, 1) + repeat(3, dims), + (1, 1) + repeat(3, dims), + 2 * repeat(0, dims), + repeat(3, dims), + repeat(1, dims), + repeat(1, dims), True, ) # Convolution with non uniform stride verify_conv( - (1, 1) + repeat(5, D), - (1, 1) + repeat(3, D), - (1, 1) + repeat(3, D), + (1, 1) + repeat(5, dims), + (1, 1) + repeat(3, dims), + (1, 1) + repeat(3, dims), None, - repeat(3, D), - repeat(2, D), - repeat(1, D), + repeat(3, dims), + repeat(2, dims), + repeat(1, dims), auto_pad="SAME_UPPER", ) # Convolution with dilation verify_conv( - (1, 1) + repeat(5, D), - (1, 1) + repeat(3, D), - (1, 1) + repeat(5, D), - 2 * repeat(2, D), - repeat(3, D), - repeat(1, D), - repeat(2, D), + (1, 1) + repeat(5, dims), + (1, 1) + repeat(3, dims), + (1, 1) + repeat(5, dims), + 2 * repeat(2, dims), + repeat(3, dims), + repeat(1, dims), + repeat(2, dims), ) # TODO(jwfromm): Merge with other tests once group_conv3d is supported. - for D in [1, 2]: + for dims in [1, 2]: # Group Convolution verify_conv( - (1, 8) + repeat(5, D), - (8, 1) + repeat(3, D), - (1, 8) + repeat(5, D), - 2 * repeat(1, D), - repeat(3, D), - repeat(1, D), - repeat(1, D), + (1, 8) + repeat(5, dims), + (8, 1) + repeat(3, dims), + (1, 8) + repeat(5, dims), + 2 * repeat(1, dims), + repeat(3, dims), + repeat(1, dims), + repeat(1, dims), group=8, ) @tvm.testing.parametrize_targets def test_convtranspose(target, dev): + """test_convtranspose""" + def verify_convtranspose_with_output_shape( x_shape, w_shape, @@ -3006,67 +3116,67 @@ def verify_convtranspose(x_shape, w_shape, y_shape, p, group=1): # Test grouped-convolution verify_convtranspose((1, 10, 3, 3), (10, 1, 3, 3), (1, 5, 7, 3), [1, 2, 1, 2], group=5) - def repeat(N, D): - return tuple([N for _ in range(D)]) + def repeat(num, dims): + return tuple(num for _ in range(dims)) # Once onnxruntime update is complete - for D in [1, 2, 3]: + for dims in [1, 2, 3]: # Convolution with padding verify_convtranspose_with_padding( - (1, 1) + repeat(5, D), - (1, 1) + repeat(3, D), - 2 * repeat(1, D), - repeat(3, D), - repeat(1, D), - repeat(1, D), + (1, 1) + repeat(5, dims), + (1, 1) + repeat(3, dims), + 2 * repeat(1, dims), + repeat(3, dims), + repeat(1, dims), + repeat(1, dims), ) # Convolution without padding verify_convtranspose_with_padding( - (1, 1) + repeat(5, D), - (1, 1) + repeat(3, D), - 2 * repeat(0, D), - repeat(3, D), - repeat(1, D), - repeat(1, D), + (1, 1) + repeat(5, dims), + (1, 1) + repeat(3, dims), + 2 * repeat(0, dims), + repeat(3, dims), + repeat(1, dims), + repeat(1, dims), ) # Convolution with unset padding verify_convtranspose_with_padding( - (1, 1) + repeat(5, D), - (1, 1) + repeat(3, D), - 2 * repeat(0, D), - repeat(3, D), - repeat(1, D), - repeat(1, D), + (1, 1) + repeat(5, dims), + (1, 1) + repeat(3, dims), + 2 * repeat(0, dims), + repeat(3, dims), + repeat(1, dims), + repeat(1, dims), True, ) # Convolution with autopadding verify_convtranspose_with_padding( - (1, 1) + repeat(5, D), - (1, 1) + repeat(3, D), + (1, 1) + repeat(5, dims), + (1, 1) + repeat(3, dims), None, - repeat(3, D), - repeat(1, D), - repeat(1, D), + repeat(3, dims), + repeat(1, dims), + repeat(1, dims), auto_pad="SAME_UPPER", ) # Convolution with valid autopadding verify_convtranspose_with_padding( - (1, 1) + repeat(5, D), - (1, 1) + repeat(3, D), + (1, 1) + repeat(5, dims), + (1, 1) + repeat(3, dims), None, - repeat(3, D), - repeat(1, D), - repeat(1, D), + repeat(3, dims), + repeat(1, dims), + repeat(1, dims), auto_pad="VALID", ) # Convolution with non uniform stride verify_convtranspose_with_padding( - (1, 1) + repeat(5, D), - (1, 1) + repeat(3, D), + (1, 1) + repeat(5, dims), + (1, 1) + repeat(3, dims), None, - repeat(3, D), - repeat(2, D), - repeat(1, D), + repeat(3, dims), + repeat(2, dims), + repeat(1, dims), auto_pad="SAME_UPPER", ) # Convolution with dilation @@ -3081,40 +3191,38 @@ def repeat(N, D): # ) # Convolution with output_shape - for D in [1, 2, 3]: - for N in range(60, 66): + for dims in [1, 2, 3]: + for num in range(60, 66): verify_convtranspose_with_output_shape( - (1, 1) + repeat(32, D), - (1, 1) + repeat(4, D), - repeat(N, D), - repeat(4, D), - repeat(2, D), - repeat(1, D), + (1, 1) + repeat(32, dims), + (1, 1) + repeat(4, dims), + repeat(num, dims), + repeat(4, dims), + repeat(2, dims), + repeat(1, dims), ) verify_convtranspose_with_output_shape( - (1, 1) + repeat(32, D), - (1, 1) + repeat(4, D), - repeat(N, D), - repeat(4, D), - repeat(2, D), - repeat(1, D), + (1, 1) + repeat(32, dims), + (1, 1) + repeat(4, dims), + repeat(num, dims), + repeat(4, dims), + repeat(2, dims), + repeat(1, dims), auto_pad="SAME_LOWER", ) @tvm.testing.parametrize_targets def test_unsqueeze_constant(target, dev): - from torch.nn import Linear, Module, Sequential + """test_unsqueeze_constant""" class Flatten(Module): - def forward(self, input): - return input.view(input.size(0), -1) - - import tempfile + def forward(self, input_): + return input_.view(input_.size(0), -1) - with tempfile.NamedTemporaryFile() as fp: - file_name = fp.name + with tempfile.NamedTemporaryFile() as f: + file_name = f.name input_size = (1, 16, 32, 32) dummy_input = torch.randn(*input_size) layer = Sequential(Flatten(), Linear(16 * 32 * 32, 64)) @@ -3126,15 +3234,17 @@ def forward(self, input): @tvm.testing.parametrize_targets def test_pooling(target, dev): + """test_pooling""" + def verify_pooling(x_shape, kernel_shape, strides, pads, out_shape, mode, auto_pad="NOTSET"): - x_np = np.random.uniform(size=x_shape).astype("float32") + _ = np.random.uniform(size=x_shape).astype("float32") if mode == "max": node_type = "MaxPool" elif mode == "average": node_type = "AveragePool" else: - raise ValueError("Pool method {} is not supported.".format(mode)) + raise ValueError(f"Pool method {mode} is not supported.") pool_node = helper.make_node( node_type, inputs=["x"], outputs=["y"], kernel_shape=kernel_shape, strides=strides @@ -3251,6 +3361,8 @@ def verify_pooling(x_shape, kernel_shape, strides, pads, out_shape, mode, auto_p @tvm.testing.parametrize_targets def test_global_pooling(target, dev): + """test_global_pooling""" + def verify_global_pooling(x_shape, mode): out_shape = x_shape[:2] + [1] * (len(x_shape) - 2) @@ -3259,7 +3371,7 @@ def verify_global_pooling(x_shape, mode): elif mode == "average": node_type = "GlobalAveragePool" else: - raise ValueError("Pool method {} is not supported.".format(mode)) + raise ValueError(f"Pool method {mode} is not supported.") pool_node = helper.make_node(node_type, inputs=["x"], outputs=["y"]) @@ -3296,6 +3408,8 @@ def verify_global_pooling(x_shape, mode): @pytest.mark.skip("flaky") @tvm.testing.parametrize_targets def test_qlinear_average_pool(target, dev): + """test_qlinear_average_pool""" + def verify_qlinear_average_pool( x_shape, kernel_shape, strides, pads, out_shape, auto_pad="NOTSET" ): @@ -3408,6 +3522,8 @@ def verify_qlinear_average_pool( @tvm.testing.parametrize_targets def test_qlinear_global_average_pool(target, dev): + """test_qlinear_global_average_pool""" + def verify_qlinear_global_average_pool(x_shape): out_shape = x_shape[:2] + [1] * (len(x_shape) - 2) @@ -3442,6 +3558,8 @@ def verify_qlinear_global_average_pool(x_shape): @tvm.testing.parametrize_targets def test_mod(target, dev): + """test_mod""" + def verify_mod(x_shape, y_shape, fmod, out_shape, dtype="float32"): x_np = np.random.uniform(-100.0, 100.0, x_shape).astype(dtype) y_np = np.random.uniform(-100.0, 100.0, y_shape).astype(dtype) @@ -3492,6 +3610,8 @@ def verify_mod(x_shape, y_shape, fmod, out_shape, dtype="float32"): @tvm.testing.parametrize_targets def test_xor(target, dev): + """test_xor""" + def verify_xor(x_shape, y_shape): x_np = np.random.choice(a=[False, True], size=x_shape).astype("bool") y_np = np.random.choice(a=[False, True], size=y_shape).astype("bool") @@ -3523,6 +3643,8 @@ def verify_xor(x_shape, y_shape): @tvm.testing.parametrize_targets def test_max_roi_pool(target, dev): + """test_max_roi_pool""" + def verify_max_roi_pool(x_shape, rois_shape, pooled_shape, spatial_scale, out_shape): if spatial_scale is None: pool_node = helper.make_node( @@ -3569,6 +3691,8 @@ def verify_max_roi_pool(x_shape, rois_shape, pooled_shape, spatial_scale, out_sh @tvm.testing.parametrize_targets def test_lppool(target, dev): + """test_lppool""" + def verify_lppool(x_shape, kernel_shape, p, strides, pads, out_shape, auto_pad="NOTSET"): kwargs = {} if p is not None: @@ -3695,6 +3819,7 @@ def verify_lppool(x_shape, kernel_shape, p, strides, pads, out_shape, auto_pad=" def verify_global_lppool(x_shape, p, out_shape, target, dev): + """verify_global_lppool""" pool_node = helper.make_node( "GlobalLpPool", inputs=["x"], @@ -3715,7 +3840,7 @@ def verify_global_lppool(x_shape, p, out_shape, target, dev): @tvm.testing.parametrize_targets def test_global_lppool(target, dev): - + """test_global_lppool""" # LpPool1D verify_global_lppool(x_shape=[1, 15, 16], p=2, out_shape=[1, 15, 1], target=target, dev=dev) @@ -3755,6 +3880,7 @@ def verify_rnn( target=None, dev=None, ): + """verify_rnn""" if rnn_type == "RNN": multiplier = 1 elif rnn_type == "LSTM": @@ -3811,7 +3937,7 @@ def register(np_arr, name, shape=None): register(b_np, "B") if use_initial_state: - assert use_bias == True, "Initial states must have bias specified." + assert use_bias is True, "Initial states must have bias specified." sequence_np = np.repeat(seq_length, batch_size).astype("int32") register(sequence_np, "sequence_lens") @@ -3837,7 +3963,7 @@ def register(np_arr, name, shape=None): register(initial_c_np, "initial_c") if use_peep and rnn_type == "LSTM": - assert use_initial_state == True, "Peepholes require initial state to be specified." + assert use_initial_state is True, "Peepholes require initial state to be specified." p_np = np.random.uniform(size=(directions, 3 * hidden_size)).astype("float32") register(p_np, "P") @@ -4101,6 +4227,8 @@ def test_gru(target, dev): @tvm.testing.parametrize_targets def test_resize(target, dev): + """test_resize""" + def verify(ishape, oshape, scales, mode, coord_trans="asymmetric", alpha=0.5, exclude=False): nodes = [ make_constant_node("roi", onnx.TensorProto.FLOAT, (0,), []), @@ -4248,6 +4376,8 @@ def verify_opset_10(ishape, scales, mode): @tvm.testing.parametrize_targets def test_nonzero(target, dev): + """test_nonzero""" + def verify_nonzero(indata, outdata, dtype): node = helper.make_node( "NonZero", @@ -4279,9 +4409,11 @@ def verify_nonzero(indata, outdata, dtype): @tvm.testing.parametrize_targets def test_topk(target, dev): - def verify_topk(input_dims, K, axis=-1): + """test_topk""" + + def verify_topk(input_dims, k, axis=-1): output_dims = list(input_dims) - output_dims[axis] = K + output_dims[axis] = k node = helper.make_node( "TopK", inputs=["X", "K"], outputs=["Values", "Indicies"], axis=axis @@ -4310,7 +4442,7 @@ def verify_topk(input_dims, K, axis=-1): indata = np.random.uniform(-10, 10, input_dims).astype(np.float32) verify_with_ort_with_inputs( - model, [indata, np.array([K])], use_vm=True, target=target, dev=dev + model, [indata, np.array([k])], use_vm=True, target=target, dev=dev ) for n in [12, 32]: @@ -4325,6 +4457,8 @@ def verify_topk(input_dims, K, axis=-1): @tvm.testing.parametrize_targets def test_roi_align(target, dev): + """test_roi_align""" + def verify_roi_align( input_dims, num_roi, @@ -4394,6 +4528,8 @@ def verify_roi_align( @tvm.testing.parametrize_targets def test_non_max_suppression(target, dev): + """test_non_max_suppression""" + def verify_nms( boxes, scores, max_output_boxes_per_class, iou_threshold, score_threshold, output_dims ): @@ -4487,6 +4623,8 @@ def verify_nms( @tvm.testing.parametrize_targets def test_loop(target, dev): + """test_loop""" + def verify_cond_loop(): y_in = helper.make_tensor_value_info("y_in", TensorProto.FLOAT, [1]) y_out = helper.make_tensor_value_info("y_out", TensorProto.FLOAT, [1]) @@ -4545,7 +4683,7 @@ def verify_cond_loop(): ) trip_count = np.array(5).astype(np.int64) - res_y = np.array([13]).astype(np.float32) + _ = np.array([13]).astype(np.float32) cond = np.array(1).astype(bool) loop_graph = onnx.helper.make_graph( [loop_node], @@ -4611,7 +4749,7 @@ def verify_count_loop(): ) trip_count = np.array(5).astype(np.int64) - res_y = np.array([13]).astype(np.float32) + _ = np.array([13]).astype(np.float32) cond = np.array(1).astype(bool) loop_graph = onnx.helper.make_graph( [loop_node], @@ -4724,6 +4862,8 @@ def verify_tensor_loop(shapeless_output=False): @tvm.testing.parametrize_targets def test_if(target, dev): + """test_if""" + def verify_if(cond_array, num_outputs): # Given a bool scalar input cond. # return constant tensor x if cond is True, otherwise return constant tensor y. @@ -4749,12 +4889,12 @@ def append_constant_nodes(nodes, outputs, expected, name): else_nodes, else_outs, else_expected = [], [], [] for i in range(num_outputs): - append_constant_nodes(then_nodes, then_outs, then_expected, "then_out{}".format(i)) - append_constant_nodes(else_nodes, else_outs, else_expected, "else_out{}".format(i)) + append_constant_nodes(then_nodes, then_outs, then_expected, f"then_out{i}") + append_constant_nodes(else_nodes, else_outs, else_expected, f"else_out{i}") - if_outputs.append("res{}".format(i)) + if_outputs.append(f"res{i}") graph_outputs.append( - onnx.helper.make_tensor_value_info("res{}".format(i), onnx.TensorProto.FLOAT, [5]), + onnx.helper.make_tensor_value_info(f"res{i}", onnx.TensorProto.FLOAT, [5]), ) then_body = onnx.helper.make_graph(then_nodes, "then_body", [], then_outs) @@ -4785,8 +4925,13 @@ def append_constant_nodes(nodes, outputs, expected, name): tvm_out = get_tvm_output_with_vm(if_model, [cond], target, dev, freeze_params=True) if not isinstance(tvm_out, list): tvm_out = [tvm_out] - for i in range(len(tvm_out)): - tvm.testing.assert_allclose(correct_out[i], tvm_out[i], rtol=1e-05, atol=1e-05) + for i, _ in enumerate(tvm_out): + tvm.testing.assert_allclose( + correct_out[i], + tvm_out[i], # pylint: disable=unnecessary-list-index-lookup + rtol=1e-05, + atol=1e-05, + ) # Confirm that if works with cond as an array or scalar. verify_if(cond_array=False, num_outputs=1) @@ -4797,6 +4942,8 @@ def append_constant_nodes(nodes, outputs, expected, name): @tvm.testing.parametrize_targets def test_size(target, dev): + """test_size""" + def verify_size(indata): node = helper.make_node( "Size", @@ -4826,6 +4973,8 @@ def verify_size(indata): @tvm.testing.parametrize_targets def test_maxunpool(target, dev): + """test_maxunpool""" + def verify_maxunpool(data, indices, kernel_shape, strides, output_shape=None, pads=None): input_names = ["xT", "xI"] input_info = [ @@ -4879,23 +5028,25 @@ def verify_maxunpool(data, indices, kernel_shape, strides, output_shape=None, pa ) # Basic test - xT = np.array([[[[5, 6], [7, 8]]]], dtype=np.float32) - xI = np.array([[[[0, 7], [13, 15]]]], dtype=np.int64) - verify_maxunpool(xT, xI, [2, 2], strides=[2, 2]) + x_t = np.array([[[[5, 6], [7, 8]]]], dtype=np.float32) + x_i = np.array([[[[0, 7], [13, 15]]]], dtype=np.int64) + verify_maxunpool(x_t, x_i, [2, 2], strides=[2, 2]) # Small stride - verify_maxunpool(xT, xI, [2, 2], strides=[1, 1]) + verify_maxunpool(x_t, x_i, [2, 2], strides=[1, 1]) # Big kernel - verify_maxunpool(xT, xI, [3, 3], strides=[2, 2]) + verify_maxunpool(x_t, x_i, [3, 3], strides=[2, 2]) # With output shape output_shape = np.array((1, 1, 5, 5), dtype=np.int64) - verify_maxunpool(xT, xI, [2, 2], strides=[2, 2], output_shape=output_shape) + verify_maxunpool(x_t, x_i, [2, 2], strides=[2, 2], output_shape=output_shape) # With explicit reverse padding pads = np.asarray([1, 1, 1, 1]).astype(np.int64) - verify_maxunpool(xT, xI, [2, 2], strides=[2, 2], pads=pads) + verify_maxunpool(x_t, x_i, [2, 2], strides=[2, 2], pads=pads) @tvm.testing.parametrize_targets def test_softplus(target, dev): + """test_softplus""" + def verify_softplus(indata): node = helper.make_node( "Softplus", @@ -4926,7 +5077,9 @@ def verify_softplus(indata): @tvm.testing.parametrize_targets def test_cumsum(target, dev): - def verify_cumsum(indata, axis, exclusive=0, reverse=0, type="float32"): + """test_cumsum""" + + def verify_cumsum(indata, axis, exclusive=0, reverse=0, dtype="float32"): cumsum_node = onnx.helper.make_node( "CumSum", inputs=["X", "axis"], @@ -4942,11 +5095,11 @@ def verify_cumsum(indata, axis, exclusive=0, reverse=0, type="float32"): make_constant_node("axis", onnx.TensorProto.INT32, [1], [axis]), cumsum_node, ] - if type == "float32": + if dtype == "float32": tensor_type = TensorProto.FLOAT else: tensor_type = TensorProto.INT32 - type = "int32" + dtype = "int32" graph = helper.make_graph( nodes, @@ -4960,7 +5113,7 @@ def verify_cumsum(indata, axis, exclusive=0, reverse=0, type="float32"): model = helper.make_model(graph, producer_name="cumsum_test") verify_with_ort_with_inputs( - model, [indata], dtype=type, use_vm=True, opset=11, target=target, dev=dev + model, [indata], dtype=dtype, use_vm=True, opset=11, target=target, dev=dev ) data = ( @@ -4994,17 +5147,19 @@ def verify_cumsum(indata, axis, exclusive=0, reverse=0, type="float32"): data = np.random.randn(1, 32, 32, 3).astype("float32") verify_cumsum(data, 1) data = np.random.randn(1, 32, 32, 3).astype("int32") - verify_cumsum(data, 0, type="int32") - verify_cumsum(data, 1, type="int32") - verify_cumsum(data, 0, 1, 0, type="int32") - verify_cumsum(data, 1, 1, 0, type="int32") - verify_cumsum(data, 0, 0, 1, type="int32") - verify_cumsum(data, 1, 0, 1, type="int32") - verify_cumsum(data, 1, 1, 1, type="int32") + verify_cumsum(data, 0, dtype="int32") + verify_cumsum(data, 1, dtype="int32") + verify_cumsum(data, 0, 1, 0, dtype="int32") + verify_cumsum(data, 1, 1, 0, dtype="int32") + verify_cumsum(data, 0, 0, 1, dtype="int32") + verify_cumsum(data, 1, 0, 1, dtype="int32") + verify_cumsum(data, 1, 1, 1, dtype="int32") @tvm.testing.parametrize_targets def test_eyelike(target, dev): + """test_eyelike""" + def verify_eyelike(indata, dynamic=False): node_list = [] eyelike_inputs = ["X"] @@ -5046,13 +5201,11 @@ def verify_eyelike(indata, dynamic=False): verify_eyelike(input_data, True) -""" - The following parametrized tests loads the tests that ONNX ships as - serialized ONNX files, inputs, and outputs. The goal of this test - is to ensure the ONNX importer is in line with the ONNX specification. - To allow these tests to run in CI before all pass, a number of tests that - are not yet supported are skipped. -""" +# The following parametrized tests loads the tests that ONNX ships as +# serialized ONNX files, inputs, and outputs. The goal of this test +# is to ensure the ONNX importer is in line with the ONNX specification. +# To allow these tests to run in CI before all pass, a number of tests +# that are not yet supported are skipped. onnx_test_node_dir = os.path.join(os.path.dirname(onnx.__file__), "backend", "test", "data", "node") @@ -5179,6 +5332,7 @@ def verify_eyelike(indata, dynamic=False): @pytest.mark.parametrize("onnx_test", onnx_test_folders) @tvm.testing.parametrize_targets def test_onnx_nodes(target, dev, onnx_test): + """test_onnx_nodes""" if platform.machine() == "aarch64" and onnx_test == "test_resize_upsample_sizes_nearest": pytest.skip("Currently failing on AArch64") @@ -5234,6 +5388,7 @@ def test_onnx_nodes(target, dev, onnx_test): def test_wrong_input(): + """test_wrong_input""" node = helper.make_node( "Softplus", inputs=["X"], @@ -5260,10 +5415,11 @@ def test_wrong_input(): @tvm.testing.parametrize_targets def test_aten(target, dev): + """test_aten""" torch.set_grad_enabled(False) def _convert_to_onnx(model, inputs): - file_name = "{}.onnx".format("aten_model") + file_name = "aten_model.onnx" torch.onnx.export( model, inputs, @@ -5298,9 +5454,11 @@ def verify_embedding_bag(num_embedding, embedding_dim, data_shape, num_bags=None @tvm.testing.parametrize_targets def test_index_put(target, dev): - class _index_put_model(torch.nn.Module): + """test_index_put""" + + class IndexPutModel(torch.nn.Module): def __init__(self, indices, values, accumulate): - super(_index_put_model, self).__init__() + super().__init__() self.indices = indices self.values = values self.accumulate = accumulate @@ -5309,7 +5467,7 @@ def forward(self, x): return x.index_put(self.indices, self.values, self.accumulate) def _convert_to_onnx(model, dummy_data): - file_name = "{}.onnx".format("aten_model") + file_name = "aten_model.onnx" torch.onnx.export( model, dummy_data, @@ -5326,7 +5484,7 @@ def verify_index_put(data_shape, indices, accumulate): dummy_data = torch.ones(data_shape) tvm_inputs = [dummy_data.numpy()] values = torch.rand(indices[0].size()) - model = _index_put_model(indices, values, accumulate) + model = IndexPutModel(indices, values, accumulate) onnx_model = _convert_to_onnx(model, dummy_data) torch_out = model(dummy_data) @@ -5350,12 +5508,12 @@ def verify_index_put_slice(data_shape, value_shape, accumulate): indices = [] index_shape = [1] * len(value_shape) index_shape[0] = -1 - for i in range(len(value_shape)): - indices.append(torch.arange(0, value_shape[i]).reshape(tuple(index_shape))) + for _, v_shape in enumerate(value_shape): + indices.append(torch.arange(0, v_shape).reshape(tuple(index_shape))) index_shape.pop() values = torch.rand(value_shape) - model = _index_put_model(indices, values, accumulate) + model = IndexPutModel(indices, values, accumulate) onnx_model = _convert_to_onnx(model, dummy_data) torch_out = model(dummy_data) @@ -5369,6 +5527,8 @@ def verify_index_put_slice(data_shape, value_shape, accumulate): @tvm.testing.parametrize_targets def test_reverse_sequence(target, dev): + """test_reverse_sequence""" + def verify_reverse_sequence(x, sequence_lens, batch_axis, time_axis): node = onnx.helper.make_node( "ReverseSequence", @@ -5406,6 +5566,8 @@ def verify_reverse_sequence(x, sequence_lens, batch_axis, time_axis): @tvm.testing.parametrize_targets def test_gelu(target, dev): + """test_gelu""" + def verify_gelu(x): node = onnx.helper.make_node( "Gelu", @@ -5432,6 +5594,8 @@ def verify_gelu(x): @tvm.testing.parametrize_targets def test_biasgelu(target, dev): + """test_biasgelu""" + def verify_biasgelu(x, bias): node = onnx.helper.make_node( "BiasGelu", @@ -5464,6 +5628,8 @@ def verify_biasgelu(x, bias): @tvm.testing.parametrize_targets def test_embedlayernormalization(target, dev): + """test_embedlayernormalization""" + def verify_embedlayernormalization( input_ids, segment_ids, @@ -5572,7 +5738,9 @@ def verify_embedlayernormalization( @tvm.testing.parametrize_targets def test_attention(target, dev): - def verify_attention(input, weight, bias, mask_index, num_heads): + """test_attention""" + + def verify_attention(input_, weight, bias, mask_index, num_heads): node = onnx.helper.make_node( "Attention", inputs=["input", "weight", "bias", "mask_index"], @@ -5587,7 +5755,7 @@ def verify_attention(input, weight, bias, mask_index, num_heads): [node], "attention_test", inputs=[ - helper.make_tensor_value_info("input", TensorProto.FLOAT, list(input.shape)), + helper.make_tensor_value_info("input", TensorProto.FLOAT, list(input_.shape)), helper.make_tensor_value_info("weight", TensorProto.FLOAT, list(weight.shape)), helper.make_tensor_value_info("bias", TensorProto.FLOAT, list(bias.shape)), helper.make_tensor_value_info( @@ -5595,7 +5763,7 @@ def verify_attention(input, weight, bias, mask_index, num_heads): ), ], outputs=[ - helper.make_tensor_value_info("output", TensorProto.FLOAT, list(input.shape)), + helper.make_tensor_value_info("output", TensorProto.FLOAT, list(input_.shape)), helper.make_tensor_value_info( "present", TensorProto.FLOAT, list(present_output_shape) ), @@ -5608,8 +5776,8 @@ def verify_attention(input, weight, bias, mask_index, num_heads): # but ort requires an output shape to be specified? verify_with_ort_with_inputs( model, - [input, weight, bias, mask_index], - [input.shape, present_output_shape], + [input_, weight, bias, mask_index], + [input_.shape, present_output_shape], target=target, dev=dev, rtol=1e-4, @@ -5623,17 +5791,19 @@ def verify_attention(input, weight, bias, mask_index, num_heads): head_size = 32 dtype = "float32" - input = np.random.random((batch_size, sequence_length, hidden_size)).astype(dtype) + input_array = np.random.random((batch_size, sequence_length, hidden_size)).astype(dtype) weight = np.random.normal(size=(hidden_size, 3 * hidden_size)).astype(dtype) * 0.1 bias = np.random.randn(3 * hidden_size).astype(dtype) mask_index = np.full((batch_size, sequence_length), 1).astype("int32") - verify_attention(input, weight, bias, mask_index, num_heads) + verify_attention(input_array, weight, bias, mask_index, num_heads) @tvm.testing.parametrize_targets def test_skiplayernormalization(target, dev): - def verify_skiplayernormalization(input, skip, gamma, beta, bias): + """test_skiplayernormalization""" + + def verify_skiplayernormalization(input_, skip, gamma, beta, bias): node = onnx.helper.make_node( "SkipLayerNormalization", inputs=["input", "skip", "gamma", "beta", "bias"], @@ -5647,20 +5817,20 @@ def verify_skiplayernormalization(input, skip, gamma, beta, bias): [node], "skiplayernormalization_test", inputs=[ - helper.make_tensor_value_info("input", TensorProto.FLOAT, list(input.shape)), + helper.make_tensor_value_info("input", TensorProto.FLOAT, list(input_.shape)), helper.make_tensor_value_info("skip", TensorProto.FLOAT, list(skip.shape)), helper.make_tensor_value_info("gamma", TensorProto.FLOAT, list(gamma.shape)), helper.make_tensor_value_info("beta", TensorProto.FLOAT, list(beta.shape)), helper.make_tensor_value_info("bias", TensorProto.FLOAT, list(bias.shape)), ], outputs=[ - helper.make_tensor_value_info("output", TensorProto.FLOAT, list(input.shape)), + helper.make_tensor_value_info("output", TensorProto.FLOAT, list(input_.shape)), ], ) model = helper.make_model(graph, producer_name="skiplayernormalization_test") verify_with_ort_with_inputs( - model, [input, skip, gamma, beta, bias], [input.shape], target=target, dev=dev + model, [input_, skip, gamma, beta, bias], [input_.shape], target=target, dev=dev ) hidden_size = 384 @@ -5668,18 +5838,20 @@ def verify_skiplayernormalization(input, skip, gamma, beta, bias): sequence_length = 4 dtype = "float32" - input = np.random.random((batch_size, sequence_length, hidden_size)).astype(dtype) + input_array = np.random.random((batch_size, sequence_length, hidden_size)).astype(dtype) skip = np.random.random((batch_size, sequence_length, hidden_size)).astype(dtype) gamma = np.random.uniform(0.5, 0.7, hidden_size).astype(dtype) beta = np.random.randn(hidden_size).astype(dtype) * 0.1 bias = np.random.randn(hidden_size).astype(dtype) - verify_skiplayernormalization(input, skip, gamma, beta, bias) + verify_skiplayernormalization(input_array, skip, gamma, beta, bias) @tvm.testing.known_failing_targets("cuda") @tvm.testing.parametrize_targets def test_qlinearconv(target, dev): + """test_qlinearconv""" + def verify_qlinearconv( x_shape, w_shape, @@ -5751,9 +5923,9 @@ def verify_qlinearconv( if padding is None: ## autopadding with unset default attributes kwargs = {} - if not all([s == 1 for s in strides]): + if not all(list(s == 1 for s in strides)): kwargs["strides"] = strides - if not all([d == 1 for d in dilations]): + if not all(list(d == 1 for d in dilations)): kwargs["dilations"] = dilations node = helper.make_node( @@ -5788,120 +5960,121 @@ def verify_qlinearconv( # opt_level=1 will cause error verify_with_ort_with_inputs(model, input_values, opt_level=2, target=target, dev=dev) - def repeat(N, D): - return tuple([N for _ in range(D)]) + def repeat(num, dims): + return tuple(num for _ in range(dims)) # only support QLinearConv2d because only support qnn.conv2d - D = 2 + dims = 2 # Convolution with padding verify_qlinearconv( - (1, 1) + repeat(5, D), - (1, 1) + repeat(3, D), - (1, 1) + repeat(5, D), - 2 * repeat(1, D), - repeat(3, D), - repeat(1, D), - repeat(1, D), + (1, 1) + repeat(5, dims), + (1, 1) + repeat(3, dims), + (1, 1) + repeat(5, dims), + 2 * repeat(1, dims), + repeat(3, dims), + repeat(1, dims), + repeat(1, dims), ) # Convolution with bias verify_qlinearconv( - (1, 1) + repeat(5, D), - (1, 1) + repeat(3, D), - (1, 1) + repeat(5, D), - 2 * repeat(1, D), - repeat(3, D), - repeat(1, D), - repeat(1, D), + (1, 1) + repeat(5, dims), + (1, 1) + repeat(3, dims), + (1, 1) + repeat(5, dims), + 2 * repeat(1, dims), + repeat(3, dims), + repeat(1, dims), + repeat(1, dims), bias=True, ) # Convolution with asymmetric padding verify_qlinearconv( - (1, 1) + repeat(5, D), - (1, 1) + repeat(3, D), - (1, 1) + repeat(4, D), - repeat(0, D) + repeat(1, D), - repeat(3, D), - repeat(1, D), - repeat(1, D), + (1, 1) + repeat(5, dims), + (1, 1) + repeat(3, dims), + (1, 1) + repeat(4, dims), + repeat(0, dims) + repeat(1, dims), + repeat(3, dims), + repeat(1, dims), + repeat(1, dims), ) # Convolution without padding verify_qlinearconv( - (1, 1) + repeat(5, D), - (1, 1) + repeat(3, D), - (1, 1) + repeat(3, D), - 2 * repeat(0, D), - repeat(3, D), - repeat(1, D), - repeat(1, D), + (1, 1) + repeat(5, dims), + (1, 1) + repeat(3, dims), + (1, 1) + repeat(3, dims), + 2 * repeat(0, dims), + repeat(3, dims), + repeat(1, dims), + repeat(1, dims), ) # Convolution with autopadding verify_qlinearconv( - (1, 1) + repeat(5, D), - (1, 1) + repeat(3, D), - (1, 1) + repeat(5, D), + (1, 1) + repeat(5, dims), + (1, 1) + repeat(3, dims), + (1, 1) + repeat(5, dims), None, - repeat(3, D), - repeat(1, D), - repeat(1, D), + repeat(3, dims), + repeat(1, dims), + repeat(1, dims), auto_pad="SAME_UPPER", ) # Convolution with valid autopadding verify_qlinearconv( - (1, 1) + repeat(5, D), - (1, 1) + repeat(3, D), - (1, 1) + repeat(3, D), + (1, 1) + repeat(5, dims), + (1, 1) + repeat(3, dims), + (1, 1) + repeat(3, dims), None, - repeat(3, D), - repeat(1, D), - repeat(1, D), + repeat(3, dims), + repeat(1, dims), + repeat(1, dims), auto_pad="VALID", ) # Convolution with non uniform stride verify_qlinearconv( - (1, 1) + repeat(5, D), - (1, 1) + repeat(3, D), - (1, 1) + repeat(3, D), + (1, 1) + repeat(5, dims), + (1, 1) + repeat(3, dims), + (1, 1) + repeat(3, dims), None, - repeat(3, D), - repeat(2, D), - repeat(1, D), + repeat(3, dims), + repeat(2, dims), + repeat(1, dims), auto_pad="SAME_UPPER", ) # Convolution with dilation verify_qlinearconv( - (1, 1) + repeat(5, D), - (1, 1) + repeat(3, D), - (1, 1) + repeat(5, D), - 2 * repeat(2, D), - repeat(3, D), - repeat(1, D), - repeat(2, D), + (1, 1) + repeat(5, dims), + (1, 1) + repeat(3, dims), + (1, 1) + repeat(5, dims), + 2 * repeat(2, dims), + repeat(3, dims), + repeat(1, dims), + repeat(2, dims), ) # Convolution with per channel quantization verify_qlinearconv( - (1, 1) + repeat(5, D), - (1, 1) + repeat(3, D), - (1, 1) + repeat(3, D), + (1, 1) + repeat(5, dims), + (1, 1) + repeat(3, dims), + (1, 1) + repeat(3, dims), None, - repeat(3, D), - repeat(1, D), - repeat(1, D), + repeat(3, dims), + repeat(1, dims), + repeat(1, dims), per_channel_quantization=True, ) @tvm.testing.parametrize_targets def test_qlinearconcat(target, dev): + """test_qlinearconcat""" + def verify_qlinearconcat(shapes, out_shape, axis=None): input_names = [] input_values = [] input_nodes = [] - for i in range(len(shapes)): + for i, shape in enumerate(shapes): tensor_name = chr(ord("a") + i) - shape = shapes[i] node = helper.make_tensor_value_info(tensor_name, TensorProto.FLOAT, list(shape)) input_names.append(tensor_name) @@ -5928,10 +6101,12 @@ def verify_qlinearconcat(shapes, out_shape, axis=None): @tvm.testing.parametrize_targets def test_qlinearadd(target, dev): + """test_qlinearadd""" + def verify_qlinearadd(a_shape, b_shape, c_shape): - a_array = np.random.random(a_shape).astype("float32") - b_array = np.random.random(b_shape).astype("float32") + _ = np.random.random(a_shape).astype("float32") + _ = np.random.random(b_shape).astype("float32") input_nodes = [ helper.make_tensor_value_info("a", TensorProto.FLOAT, list(a_shape)), @@ -5941,7 +6116,6 @@ def verify_qlinearadd(a_shape, b_shape, c_shape): "a", "b", ] - input_values = [a_array, b_array] node = helper.make_node("Add", ["a", "b"], ["C"]) graph = helper.make_graph( @@ -5960,10 +6134,12 @@ def verify_qlinearadd(a_shape, b_shape, c_shape): @tvm.testing.parametrize_targets def test_qlinearmul(target, dev): + """test_qlinearmul""" + def verify_qlinearmul(a_shape, b_shape, c_shape): - a_array = np.random.random(a_shape).astype("float32") - b_array = np.random.random(b_shape).astype("float32") + _ = np.random.random(a_shape).astype("float32") + _ = np.random.random(b_shape).astype("float32") input_nodes = [ helper.make_tensor_value_info("a", TensorProto.FLOAT, list(a_shape)), @@ -5973,7 +6149,6 @@ def verify_qlinearmul(a_shape, b_shape, c_shape): "a", "b", ] - input_values = [a_array, b_array] node = helper.make_node("Mul", input_names, ["C"]) graph = helper.make_graph( @@ -5994,6 +6169,8 @@ def verify_qlinearmul(a_shape, b_shape, c_shape): @pytest.mark.skip(reason="See https://github.com/apache/tvm/issues/11375") @tvm.testing.parametrize_targets def test_qlinearleakyrelu(target, dev): + """test_qlinearleakyrelu""" + def verify_qlinearleakyrelu(inshape, kwargs): in_array = np.random.random(inshape).astype("float32") @@ -6020,14 +6197,14 @@ def verify_qlinearleakyrelu(inshape, kwargs): @pytest.mark.skip(reason="See https://github.com/apache/tvm/issues/11375") @tvm.testing.parametrize_targets def test_qlinearsigmoid(target, dev): + """test_qlinearsigmoid""" + def verify_qlinearsigmoid(a_shape): - a_array = np.random.random(a_shape).astype("float32") + _ = np.random.random(a_shape).astype("float32") input_nodes = [helper.make_tensor_value_info("a", TensorProto.FLOAT, list(a_shape))] - input_values = [a_array] - node = helper.make_node("Sigmoid", ["a"], ["B"]) graph = helper.make_graph( [node], @@ -6046,6 +6223,8 @@ def verify_qlinearsigmoid(a_shape): @tvm.testing.parametrize_targets("llvm") def test_random_uniform(target, dev): + """test_random_uniform""" + def get_random_uniform(shape, dtype="float32", high=1.0, low=0.0, seed=None): ONNX_DTYPE = mapping.NP_TYPE_TO_TENSOR_TYPE[np.dtype(dtype)] node = helper.make_node( @@ -6104,7 +6283,9 @@ def get_random_uniform(shape, dtype="float32", high=1.0, low=0.0, seed=None): @tvm.testing.parametrize_targets("llvm") def test_random_uniform_like(target, dev): - def get_random_uniform_like(input, shape, dtype=None, high=1.0, low=0.0, seed=None): + """test_random_uniform_like""" + + def get_random_uniform_like(input_, shape, dtype=None, high=1.0, low=0.0, seed=None): node = helper.make_node("RandomUniformLike", ["in"], ["out"], high=high, low=low) if seed is not None: seed_attr = helper.make_attribute("seed", seed) @@ -6116,7 +6297,7 @@ def get_random_uniform_like(input, shape, dtype=None, high=1.0, low=0.0, seed=No dtype_attr = helper.make_attribute("dtype", ONNX_DTYPE) node.attribute.append(dtype_attr) else: - dtype = input.dtype + dtype = input_.dtype ONNX_DTYPE = mapping.NP_TYPE_TO_TENSOR_TYPE[np.dtype(dtype)] graph = helper.make_graph( @@ -6126,33 +6307,33 @@ def get_random_uniform_like(input, shape, dtype=None, high=1.0, low=0.0, seed=No outputs=[helper.make_tensor_value_info("out", ONNX_DTYPE, shape)], ) model = helper.make_model(graph, producer_name="random_uniform_like_test") - return get_tvm_output_with_vm(model, [input], target=target, dev=dev) + return get_tvm_output_with_vm(model, [input_], target=target, dev=dev) # Check that function runs and produces proper shape and dtype. shape = [10] - input = np.random.random(shape).astype("float32") - vals = get_random_uniform_like(input, shape, dtype="float32") + input_array = np.random.random(shape).astype("float32") + vals = get_random_uniform_like(input_array, shape, dtype="float32") assert list(vals.shape) == [10] assert vals.dtype == "float32" # Test N-D tensor generation. shape = [1, 3, 100, 100] - input = np.random.random(shape).astype("float32") - vals = get_random_uniform_like(input, shape, dtype="float64") + input_array = np.random.random(shape).astype("float32") + vals = get_random_uniform_like(input_array, shape, dtype="float64") assert list(vals.shape) == shape assert vals.dtype == "float64" # Check that bounds aren't exceeded. shape = [100] - input = np.random.random(shape).astype("float64") - vals = get_random_uniform_like(input, shape, high=100.0, low=-100.0) + input_array = np.random.random(shape).astype("float64") + vals = get_random_uniform_like(input_array, shape, high=100.0, low=-100.0) assert list(vals.shape) == shape assert all(vals >= -100) and all(vals <= 100) # Test against an expected output with a fixed seed. shape = [10] - input = np.random.random(shape).astype("float32") - real = get_random_uniform_like(input, shape=[10], seed=5.0) + input_array = np.random.random(shape).astype("float32") + real = get_random_uniform_like(input_array, shape=[10], seed=5.0) expected = np.asarray( [ 0.043976, @@ -6172,6 +6353,8 @@ def get_random_uniform_like(input, shape, dtype=None, high=1.0, low=0.0, seed=No @tvm.testing.parametrize_targets("llvm") def test_random_normal(target, dev): + """test_random_normal""" + def get_random_normal(shape, dtype="float32", scale=1.0, mean=0.0, seed=None): ONNX_DTYPE = mapping.NP_TYPE_TO_TENSOR_TYPE[np.dtype(dtype)] node = helper.make_node( @@ -6210,7 +6393,9 @@ def get_random_normal(shape, dtype="float32", scale=1.0, mean=0.0, seed=None): @tvm.testing.parametrize_targets("llvm") def test_random_normal_like(target, dev): - def get_random_normal_like(input, shape, dtype="float32", scale=1.0, mean=0.0, seed=None): + """test_random_normal_like""" + + def get_random_normal_like(input_, shape, dtype="float32", scale=1.0, mean=0.0, seed=None): ONNX_DTYPE = mapping.NP_TYPE_TO_TENSOR_TYPE[np.dtype(dtype)] node = helper.make_node( "RandomNormalLike", ["in"], ["out"], dtype=ONNX_DTYPE, scale=scale, mean=mean @@ -6226,20 +6411,22 @@ def get_random_normal_like(input, shape, dtype="float32", scale=1.0, mean=0.0, s outputs=[helper.make_tensor_value_info("out", ONNX_DTYPE, shape)], ) model = helper.make_model(graph, producer_name="random_normal_like_test") - return get_tvm_output_with_vm(model, [input], target=target, dev=dev) + return get_tvm_output_with_vm(model, [input_], target=target, dev=dev) # Test N-D tensor generation. shape = [1, 3, 100, 100] - input = np.random.random(shape).astype("float32") - vals = get_random_normal_like(input, [1, 3, 100, 100], dtype="float32") + input_array = np.random.random(shape).astype("float32") + vals = get_random_normal_like(input_array, [1, 3, 100, 100], dtype="float32") assert list(vals.shape) == [1, 3, 100, 100] tvm.testing.assert_allclose(vals.mean(), 0.0, rtol=0.1, atol=0.1) tvm.testing.assert_allclose(np.std(vals), 1.0, rtol=0.1, atol=0.1) # Test mean=2.0 scale=10.0 shape = [1, 3, 100, 100] - input = np.random.random(shape).astype("float32") - vals = get_random_normal_like(input, [1, 3, 100, 100], mean=2.0, scale=10.0, dtype="float32") + input_array = np.random.random(shape).astype("float32") + vals = get_random_normal_like( + input_array, [1, 3, 100, 100], mean=2.0, scale=10.0, dtype="float32" + ) assert list(vals.shape) == [1, 3, 100, 100] tvm.testing.assert_allclose(vals.mean(), 2.0, rtol=0.1, atol=0.1) tvm.testing.assert_allclose(np.std(vals), 10.0, rtol=0.1, atol=0.1) @@ -6285,6 +6472,8 @@ def get_multinomial(input, shape, sample_size, seed=None): @tvm.testing.parametrize_targets def test_convinteger(target, dev): + """test_convinteger""" + def verify_convinteger( x_shape, w_shape, @@ -6316,9 +6505,9 @@ def verify_convinteger( if padding is None: ## autopadding with unset default attributes kwargs = {} - if not all([s == 1 for s in strides]): + if not all(list(s == 1 for s in strides)): kwargs["strides"] = strides - if not all([d == 1 for d in dilations]): + if not all(list(d == 1 for d in dilations)): kwargs["dilations"] = dilations node = helper.make_node( @@ -6353,90 +6542,92 @@ def verify_convinteger( # opt_level=1 will cause error verify_with_ort_with_inputs(model, input_values, target=target, dev=dev, opt_level=2) - def repeat(N, D): - return tuple([N for _ in range(D)]) + def repeat(num, dims): + return tuple(num for _ in range(dims)) # only support 2D ConvInteger because we only support qnn.conv2d for now. - D = 2 + dims = 2 # Convolution with padding verify_convinteger( - (1, 1) + repeat(5, D), - (1, 1) + repeat(3, D), - (1, 1) + repeat(5, D), - 2 * repeat(1, D), - repeat(3, D), - repeat(1, D), - repeat(1, D), + (1, 1) + repeat(5, dims), + (1, 1) + repeat(3, dims), + (1, 1) + repeat(5, dims), + 2 * repeat(1, dims), + repeat(3, dims), + repeat(1, dims), + repeat(1, dims), ) # Convolution with asymmetric padding verify_convinteger( - (1, 1) + repeat(5, D), - (1, 1) + repeat(3, D), - (1, 1) + repeat(4, D), - repeat(0, D) + repeat(1, D), - repeat(3, D), - repeat(1, D), - repeat(1, D), + (1, 1) + repeat(5, dims), + (1, 1) + repeat(3, dims), + (1, 1) + repeat(4, dims), + repeat(0, dims) + repeat(1, dims), + repeat(3, dims), + repeat(1, dims), + repeat(1, dims), ) # Convolution without padding verify_convinteger( - (1, 1) + repeat(5, D), - (1, 1) + repeat(3, D), - (1, 1) + repeat(3, D), - 2 * repeat(0, D), - repeat(3, D), - repeat(1, D), - repeat(1, D), + (1, 1) + repeat(5, dims), + (1, 1) + repeat(3, dims), + (1, 1) + repeat(3, dims), + 2 * repeat(0, dims), + repeat(3, dims), + repeat(1, dims), + repeat(1, dims), ) # Convolution with autopadding verify_convinteger( - (1, 1) + repeat(5, D), - (1, 1) + repeat(3, D), - (1, 1) + repeat(5, D), + (1, 1) + repeat(5, dims), + (1, 1) + repeat(3, dims), + (1, 1) + repeat(5, dims), None, - repeat(3, D), - repeat(1, D), - repeat(1, D), + repeat(3, dims), + repeat(1, dims), + repeat(1, dims), auto_pad="SAME_UPPER", ) # Convolution with valid autopadding verify_convinteger( - (1, 1) + repeat(5, D), - (1, 1) + repeat(3, D), - (1, 1) + repeat(3, D), + (1, 1) + repeat(5, dims), + (1, 1) + repeat(3, dims), + (1, 1) + repeat(3, dims), None, - repeat(3, D), - repeat(1, D), - repeat(1, D), + repeat(3, dims), + repeat(1, dims), + repeat(1, dims), auto_pad="VALID", ) # Convolution with non uniform stride verify_convinteger( - (1, 1) + repeat(5, D), - (1, 1) + repeat(3, D), - (1, 1) + repeat(3, D), + (1, 1) + repeat(5, dims), + (1, 1) + repeat(3, dims), + (1, 1) + repeat(3, dims), None, - repeat(3, D), - repeat(2, D), - repeat(1, D), + repeat(3, dims), + repeat(2, dims), + repeat(1, dims), auto_pad="SAME_UPPER", ) # Convolution with dilation verify_convinteger( - (1, 1) + repeat(5, D), - (1, 1) + repeat(3, D), - (1, 1) + repeat(5, D), - 2 * repeat(2, D), - repeat(3, D), - repeat(1, D), - repeat(2, D), + (1, 1) + repeat(5, dims), + (1, 1) + repeat(3, dims), + (1, 1) + repeat(5, dims), + 2 * repeat(2, dims), + repeat(3, dims), + repeat(1, dims), + repeat(2, dims), ) @tvm.testing.parametrize_targets def test_scan(target, dev): + """test_scan""" + def verify_scan( input_shapes, output_shapes, @@ -6447,7 +6638,6 @@ def verify_scan( scan_output_directions, opset, ): - import copy body_input_shapes = copy.deepcopy(input_shapes) num_state_inputs = len(input_shapes) - num_scan_inputs @@ -6482,9 +6672,6 @@ def verify_scan( scan_out0 = onnx.helper.make_tensor_value_info( "scan_out0", onnx.TensorProto.FLOAT, body_input_shapes[0] ) - matmul_out = onnx.helper.make_tensor_value_info( - "matmul_out", onnx.TensorProto.FLOAT, body_input_shapes[1] - ) state1 = onnx.helper.make_tensor_value_info( "state1", onnx.TensorProto.FLOAT, body_input_shapes[1] ) @@ -6602,8 +6789,10 @@ def verify_scan( @tvm.testing.parametrize_targets -def test_LinearRegressor(target, dev): - def verify_LinearRegressor(a_shape, c_shape, i_shape, targets=1, batch=1): +def test_linear_regressor(target, dev): + """test_linear_regressor""" + + def verify_linear_regressor(a_shape, c_shape, i_shape, targets=1, batch=1): a_array = np.random.uniform(size=a_shape).astype("float32") out_shape = (batch, targets) @@ -6637,15 +6826,17 @@ def verify_LinearRegressor(a_shape, c_shape, i_shape, targets=1, batch=1): ) verify_with_ort_with_inputs(model, [a_array], target=target, dev=dev) - verify_LinearRegressor((1, 3), (3), (1)) - verify_LinearRegressor((2, 10), (10), (1), batch=2) - verify_LinearRegressor((1, 3), (30), (10), targets=10) - verify_LinearRegressor((10, 3), (30), (10), targets=10, batch=10) - verify_LinearRegressor((1, 4), (3), (1)) + verify_linear_regressor((1, 3), (3), (1)) + verify_linear_regressor((2, 10), (10), (1), batch=2) + verify_linear_regressor((1, 3), (30), (10), targets=10) + verify_linear_regressor((10, 3), (30), (10), targets=10, batch=10) + verify_linear_regressor((1, 4), (3), (1)) @tvm.testing.parametrize_targets def test_sequence(target, dev): + """test_sequence""" + def verify_sequence_ops(tensor_shape, num_tensors, axis=0, position=None, new_axis=None): tensor_shape = list(tensor_shape) tensor_values = [] @@ -6655,7 +6846,7 @@ def verify_sequence_ops(tensor_shape, num_tensors, axis=0, position=None, new_ax # Create an input for each tensor. input_tensor_names = [] for i in range(num_tensors): - name = "input_tensor_%d" % i + name = f"input_tensor_{i}" input_tensor_names.append(name) # Test creating a tensor sequence. diff --git a/tests/python/frontend/pytorch/test_forward.py b/tests/python/frontend/pytorch/test_forward.py index bc848f90b38f..843d9a4e5a30 100755 --- a/tests/python/frontend/pytorch/test_forward.py +++ b/tests/python/frontend/pytorch/test_forward.py @@ -16,26 +16,25 @@ # under the License. # pylint: disable=import-self, invalid-name, unused-argument """Unit tests for various models and operators""" -from contextlib import suppress import os import platform import sys -from time import time +from packaging import version as package_version + +import pytest import numpy as np -import torch -import torchvision import tvm import tvm.testing -from packaging import version as package_version -from scipy.stats import t as tdistr -from torch.nn import Module -from torch.nn import functional as F from tvm import relay from tvm.contrib import graph_executor from tvm.contrib.nvcc import have_fp16 from tvm.contrib import cudnn -import pytest + +import torch +from torch.nn import Module +from torch.nn import functional as F +import torchvision sys.setrecursionlimit(10000) if torch.cuda.is_available(): @@ -44,11 +43,15 @@ def list_ops(expr): + """list_ops""" + class OpLister(tvm.relay.ExprVisitor): - def visit_op(self, expr): - if expr not in self.node_set: - self.node_list.append(expr) - return super().visit_op(expr) + """OpLister inherits from ExprVisitor""" + + def visit_op(self, op): + if op not in self.node_set: + self.node_list.append(op) + return super().visit_op(op) def list_nodes(self, expr): self.node_set = {} @@ -60,6 +63,7 @@ def list_nodes(self, expr): def assert_shapes_match(tru, est): + """Verfiy whether the shapes are equal""" if tru.shape != est.shape: msg = "Output shapes {} and {} don't match" raise AssertionError(msg.format(tru.shape, est.shape)) @@ -94,6 +98,7 @@ def load_torchvision(model_name): def load_pretrainedmodels(model_name): """Given a model name, returns a pretrainedmodels.pytorch model in eval mode as well as an example input.""" + # pylint: disable=import-outside-toplevel import pretrainedmodels # https://github.com/Cadene/pretrained-models.pytorch model = getattr(pretrainedmodels, model_name)().float().eval() @@ -109,40 +114,43 @@ def load_model(model_name): """Given a model name, returns a model as well as an example input.""" if hasattr(torchvision.models, model_name): return load_torchvision(model_name) + # pylint: disable=import-outside-toplevel try: import pretrainedmodels if hasattr(pretrainedmodels, model_name): return load_pretrainedmodels(model_name) - except ModuleNotFoundError: - raise ModuleNotFoundError("Please install pretrainedmodels.pytorch") + except ModuleNotFoundError as e: + raise ModuleNotFoundError("Please install pretrainedmodels.pytorch") from e raise RuntimeError("Model not supported") def verify_model( model_name, - input_data=[], - custom_convert_map={}, + input_data=None, + custom_convert_map=None, rtol=1e-5, atol=1e-5, - expected_ops=[], + expected_ops=None, kind="graph", check_correctness=True, cpu_only=False, ): """Assert that the output of a compiled model matches with that of its baseline.""" + input_data = [] if input_data is None else input_data + custom_convert_map = custom_convert_map or {} + expected_ops = expected_ops or [] if isinstance(model_name, str): baseline_model, baseline_input = load_model(model_name) elif isinstance(input_data, list): baseline_model = model_name baseline_input = input_data - elif isinstance(input_data, torch.Tensor) or len(input_data.shape) == 0: + elif isinstance(input_data, torch.Tensor) or not input_data.shape: baseline_model = model_name baseline_input = [input_data] else: assert False, "Unexpected input format" - if torch.cuda.is_available(): if isinstance(baseline_model, torch.nn.Module): baseline_model = baseline_model.cuda() @@ -165,7 +173,7 @@ def verify_model( else: trace = trace.cpu() - input_names = ["input{}".format(idx) for idx, inp in enumerate(baseline_input)] + input_names = [f"input{idx}" for idx, _ in enumerate(baseline_input)] input_shapes = list(zip(input_names, [inp.shape for inp in baseline_input])) mod, params = relay.frontend.from_pytorch(trace, input_shapes, custom_convert_map) for arg in mod["main"].params[: len(input_names)]: @@ -218,15 +226,18 @@ def verify_model_with_input( test_func, input_data, *, - input_dict={}, - custom_convert_map={}, + input_dict=None, + custom_convert_map=None, rtol=1e-5, atol=1e-5, assert_shape_only=False, ): + """Generic function to generate and compare Pytorch and TVM output""" + input_dict = input_dict or {} + custom_convert_map = custom_convert_map or {} baseline_outputs = test_func(*input_data) trace = torch.jit.trace(test_func, [input.clone() for input in input_data]) - input_names = ["input{}".format(idx) for idx, inp in enumerate(input_data)] + input_names = [f"input{idx}" for idx, _ in enumerate(input_data)] input_shapes = list(zip(input_names, [inp.shape for inp in input_data])) mod, params = relay.frontend.from_pytorch(trace, input_shapes, custom_convert_map) with tvm.transform.PassContext(opt_level=3): @@ -242,13 +253,14 @@ def verify_model_with_input( compiled_output = relay_model.get_output(0).numpy() assert_shapes_match(baseline_outputs, compiled_output) - if assert_shape_only == False: + if assert_shape_only is False: tvm.testing.assert_allclose(baseline_outputs, compiled_output, rtol=rtol, atol=atol) # Single operator tests @tvm.testing.uses_gpu def test_forward_pixel_shuffle(): + """test_forward_pixel_shuffle""" torch.set_grad_enabled(False) input_shape = [1, 144, 16, 16] @@ -260,6 +272,7 @@ def test_forward_pixel_shuffle(): @tvm.testing.uses_gpu def test_forward_add(): + """test_forward_add""" torch.set_grad_enabled(False) input_shape = [10] @@ -294,6 +307,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_subtract(): + """test_forward_subtract""" torch.set_grad_enabled(False) input_shape = [10] @@ -328,6 +342,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_multiply(): + """test_forward_multiply""" torch.set_grad_enabled(False) input_shape = [10] @@ -362,6 +377,8 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_min_max(): + """test_min_max""" + class Max(Module): def forward(self, inp): return torch.max(inp) @@ -412,6 +429,8 @@ def forward(self, inp): @tvm.testing.uses_gpu def test_minimum_maximum(): + """test_minimum_maximum""" + class Maximum(Module): def forward(self, lhs, rhs): return torch.maximum(lhs, rhs) @@ -428,6 +447,7 @@ def forward(self, lhs, rhs): @tvm.testing.uses_gpu def test_forward_reciprocal(): + """test_forward_reciprocal""" torch.set_grad_enabled(False) input_shape = [2, 1, 10, 1, 10] @@ -441,6 +461,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_repeat(): + """test_forward_repeat""" torch.set_grad_enabled(False) input_shape = [1, 3] @@ -464,6 +485,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_repeat_interleave(): + """test_forward_repeat_interleave""" torch.set_grad_enabled(False) input_shape = [2, 2, 3] @@ -492,6 +514,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_unsqueeze(): + """test_forward_unsqueeze""" torch.set_grad_enabled(False) input_shape = [10, 10] @@ -513,6 +536,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_squeeze(): + """test_forward_squeeze""" torch.set_grad_enabled(False) input_shape = [2, 1, 10, 1, 10] @@ -531,6 +555,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_arange(): + """test_forward_arange""" torch.set_grad_enabled(False) class Arange1(Module): @@ -607,6 +632,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_mesh_grid(): + """test_forward_mesh_grid""" torch.set_grad_enabled(False) class MeshGrid1(Module): @@ -629,6 +655,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_abs(): + """test_forward_abs""" torch.set_grad_enabled(False) input_shape = [2, 1, 10, 1, 10] @@ -642,6 +669,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_concatenate(): + """test_forward_concatenate""" torch.set_grad_enabled(False) input_shape = [1, 3, 10, 10] @@ -663,6 +691,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_relu(): + """test_forward_relu""" torch.set_grad_enabled(False) input_shape = [10, 10] input_data = torch.rand(input_shape).float() @@ -671,6 +700,7 @@ def test_forward_relu(): @tvm.testing.uses_gpu def test_forward_prelu(): + """test_forward_prelu""" torch.set_grad_enabled(False) input_shape = [1, 3, 10, 10] input_data = torch.rand(input_shape).float() @@ -683,6 +713,7 @@ def test_forward_prelu(): @tvm.testing.uses_gpu def test_forward_leakyrelu(): + """test_forward_leakyrelu""" torch.set_grad_enabled(False) input_shape = [1, 3, 10, 10] input_data = torch.rand(input_shape).float() @@ -696,6 +727,7 @@ def test_forward_leakyrelu(): @tvm.testing.uses_gpu def test_forward_elu(): + """test_forward_elu""" torch.set_grad_enabled(False) input_shape = [1, 3, 10, 10] input_data = torch.randn(input_shape).float() @@ -707,6 +739,7 @@ def test_forward_elu(): @tvm.testing.uses_gpu def test_forward_celu(): + """test_forward_celu""" torch.set_grad_enabled(False) input_shape = [1, 3, 10, 10] input_data = torch.rand(input_shape).float() @@ -718,6 +751,7 @@ def test_forward_celu(): @tvm.testing.uses_gpu def test_forward_gelu(): + """test_forward_gelu""" torch.set_grad_enabled(False) input_shape = [1, 3, 10, 10] input_data = torch.rand(input_shape).float() @@ -726,6 +760,7 @@ def test_forward_gelu(): @tvm.testing.uses_gpu def test_forward_selu(): + """test_forward_selu""" torch.set_grad_enabled(False) input_shape = [1, 3, 10, 10] input_data = torch.rand(input_shape).float() @@ -734,6 +769,7 @@ def test_forward_selu(): @tvm.testing.uses_gpu def test_forward_silu(): + """test_forward_silu""" torch.set_grad_enabled(False) input_shape = [1, 3, 10, 10] input_data = torch.rand(input_shape).float() @@ -742,6 +778,7 @@ def test_forward_silu(): @tvm.testing.uses_gpu def test_forward_glu(): + """test_forward_glu""" torch.set_grad_enabled(False) input_shape = [1, 3, 10, 10] input_data = torch.rand(input_shape).float() @@ -750,6 +787,7 @@ def test_forward_glu(): @tvm.testing.uses_gpu def test_forward_softplus(): + """test_forward_softplus""" torch.set_grad_enabled(False) input_shape = [1, 3, 10, 10] input_data = torch.rand(input_shape).float() @@ -760,6 +798,7 @@ def test_forward_softplus(): @tvm.testing.uses_gpu def test_forward_softsign(): + """test_forward_softsign""" torch.set_grad_enabled(False) input_shape = [1, 3, 10, 10] input_data = torch.rand(input_shape).float() @@ -768,6 +807,7 @@ def test_forward_softsign(): @tvm.testing.uses_gpu def test_forward_log_sigmoid(): + """test_forward_log_sigmoid""" torch.set_grad_enabled(False) input_shape = [10, 10] input_data = torch.rand(input_shape).float() @@ -776,6 +816,7 @@ def test_forward_log_sigmoid(): @tvm.testing.uses_gpu def test_forward_adaptive_avgpool(): + """test_forward_adaptive_avgpool""" torch.set_grad_enabled(False) input_shape = [1, 3, 10, 10] input_data = torch.rand(input_shape).float() @@ -789,6 +830,7 @@ def test_forward_adaptive_avgpool(): @tvm.testing.uses_gpu def test_forward_adaptive_maxpool(): + """test_forward_adaptive_maxpool""" torch.set_grad_enabled(False) input_shape = [1, 3, 10, 10] input_data = torch.rand(input_shape).float() @@ -802,6 +844,7 @@ def test_forward_adaptive_maxpool(): @tvm.testing.uses_gpu def test_forward_maxpool2d(): + """test_forward_maxpool2d""" torch.set_grad_enabled(False) input_shape = [1, 3, 10, 10] input_data = torch.rand(input_shape).float() @@ -820,18 +863,18 @@ def forward(self, *args): class MaxPool2DWithIndices(Module): def __init__(self): - super(MaxPool2DWithIndices, self).__init__() + super().__init__() self.pool = torch.nn.MaxPool2d(kernel_size=[1, 1], return_indices=True) def forward(self, *args): - output, indices = self.pool(args[0]) + output, _ = self.pool(args[0]) return output class MaxPool2DWithIntStrides(Module): def forward(self, *args): # Makes kernel_size and strides a Relay expr to test converting back to int x_shape = args[0].shape - kernel_size = [torch.tensor(x_shape[1]).int(), torch.tensor(x_shape[1]).int()] + # kernel_size = [torch.tensor(x_shape[1]).int(), torch.tensor(x_shape[1]).int()] strides = [torch.tensor(x_shape[0]).int(), torch.tensor(x_shape[0]).int()] return torch.nn.functional.max_pool2d(args[0], kernel_size=[4, 4], stride=strides) @@ -841,6 +884,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_maxpool1d(): + """test_forward_maxpool1d""" torch.set_grad_enabled(False) input_shape = [1, 3, 10] input_data = torch.rand(input_shape).float() @@ -860,6 +904,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_maxpool3d(): + """test_forward_maxpool3d""" torch.set_grad_enabled(False) input_shape = [1, 3, 10, 10, 10] input_data = torch.rand(input_shape).float() @@ -879,12 +924,13 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_split(): + """test_forward_split""" torch.set_grad_enabled(False) input_shape = [4, 10] class Split(Module): def __init__(self, split_size_or_sections, dim): - super(Split, self).__init__() + super().__init__() self.split_size_or_sections = split_size_or_sections self.dim = dim @@ -900,6 +946,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_avgpool1d(): + """test_forward_avgpool1d""" torch.set_grad_enabled(False) input_shape = [1, 3, 10] @@ -917,6 +964,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_avgpool2d(): + """test_forward_avgpool2d""" torch.set_grad_enabled(False) input_shape = [1, 3, 10, 10] @@ -943,6 +991,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_avgpool3d(): + """test_forward_avgpool3d""" torch.set_grad_enabled(False) input_shape = [1, 3, 10, 10, 10] @@ -960,6 +1009,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_hardtanh(): + """test_forward_hardtanh""" torch.set_grad_enabled(False) input_shape = [10] input_data = torch.rand(input_shape).float() @@ -968,13 +1018,14 @@ def test_forward_hardtanh(): @tvm.testing.uses_gpu def test_forward_conv(): + """test_forward_conv""" torch.set_grad_enabled(False) conv1d_input_shape = [1, 3, 10] conv2d_input_shape = [1, 3, 10, 10] class Conv2D1(Module): def __init__(self): - super(Conv2D1, self).__init__() + super().__init__() self.conv = torch.nn.Conv2d(3, 6, 7, bias=True) self.softmax = torch.nn.Softmax() @@ -983,7 +1034,7 @@ def forward(self, *args): class Conv2D2(Module): def __init__(self): - super(Conv2D2, self).__init__() + super().__init__() self.conv = torch.nn.Conv2d(3, 6, 7, bias=False) self.softmax = torch.nn.Softmax() @@ -992,7 +1043,7 @@ def forward(self, *args): class Conv2D3(Module): def __init__(self): - super(Conv2D3, self).__init__() + super().__init__() self.conv = torch.nn.Conv2d(3, 6, 7, groups=3, bias=False) self.softmax = torch.nn.Softmax() @@ -1001,7 +1052,7 @@ def forward(self, *args): class Conv1D1(Module): def __init__(self): - super(Conv1D1, self).__init__() + super().__init__() self.conv = torch.nn.Conv1d(3, 6, 7) self.softmax = torch.nn.Softmax() @@ -1010,7 +1061,7 @@ def forward(self, *args): class Conv1D2(Module): def __init__(self): - super(Conv1D2, self).__init__() + super().__init__() self.conv = torch.nn.Conv1d(3, 6, 7, bias=False) self.softmax = torch.nn.Softmax() @@ -1019,7 +1070,7 @@ def forward(self, *args): class Conv1D3(Module): def __init__(self): - super(Conv1D3, self).__init__() + super().__init__() self.conv = torch.nn.Conv1d(3, 6, 7, groups=3, bias=False) self.softmax = torch.nn.Softmax() @@ -1053,6 +1104,7 @@ def forward(self, *args): def test_forward_conv_transpose( in_channels, out_channels, kernel_size, output_padding, bias, groups ): + """test_forward_conv_transpose""" # Note we do not test with groups > 1 because that is not supported # in tvm for conv transpose operations @@ -1105,10 +1157,14 @@ def test_forward_conv_transpose( @tvm.testing.uses_gpu def test_forward_conv2d_transpose_group(): + """test_forward_conv2d_transpose_group""" # https://github.com/apache/tvm/issues/10223 class ModulatedConvTranspose2D(torch.nn.Module): + """ModulatedConvTranspose2D module""" + def forward(self, x, w, s): + """forward""" B, C, H, W = x.shape I, O, KH, KW = w.shape @@ -1137,6 +1193,7 @@ def forward(self, x, w, s): def test_forward_deform_conv(): + """test_forward_deform_conv""" torch.set_grad_enabled(False) def test_run( @@ -1217,6 +1274,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_threshold(): + """test_forward_threshold""" torch.set_grad_enabled(False) input_shape = [1, 3] input_data = torch.rand(input_shape).float() @@ -1225,6 +1283,7 @@ def test_forward_threshold(): @tvm.testing.uses_gpu def test_forward_contiguous(): + """test_forward_contiguous""" torch.set_grad_enabled(False) input_shape = [10] @@ -1238,6 +1297,8 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_batchnorm(): + """test_forward_batchnorm""" + def init_weight(m): torch.nn.init.normal_(m.weight, 0, 0.01) torch.nn.init.normal_(m.bias) @@ -1252,6 +1313,7 @@ def init_weight(m): @tvm.testing.uses_gpu def test_forward_instancenorm(): + """test_forward_instancenorm""" inp_2d = torch.rand((1, 16, 10, 10)) inp_3d = torch.rand((1, 16, 10, 10, 10)) @@ -1264,6 +1326,8 @@ def test_forward_instancenorm(): @tvm.testing.uses_gpu def test_forward_layernorm(): + """test_forward_layernorm""" + def init_weight(m): torch.nn.init.normal_(m.weight, 0, 0.01) torch.nn.init.normal_(m.bias, 0.02) @@ -1277,6 +1341,7 @@ def init_weight(m): @tvm.testing.uses_gpu def test_forward_groupnorm(): + """test_forward_groupnorm""" input_shape = [10, 6, 5, 5] input_data = torch.rand(input_shape).float() @@ -1299,6 +1364,7 @@ def test_forward_groupnorm(): @tvm.testing.uses_gpu def test_forward_reshape(): + """test_forward_reshape""" torch.set_grad_enabled(False) input_shape = [2, 1, 10, 1, 10] new_shape = [2, 1, 10, 10] @@ -1324,6 +1390,8 @@ def forward(self, x): @tvm.testing.uses_gpu def test_forward_reshape_as(): + """test_forward_reshape_as""" + def test_func(input_tensor, other_tensor): return input_tensor.reshape_as(other_tensor) @@ -1334,6 +1402,8 @@ def test_func(input_tensor, other_tensor): @tvm.testing.uses_gpu def test_flatten(): + """test_flatten""" + def _test_flatten(start_dim, end_dim): return lambda inp: torch.flatten(inp, start_dim, end_dim) @@ -1360,6 +1430,7 @@ def _test_flatten(start_dim, end_dim): @tvm.testing.uses_gpu def test_forward_transpose(): + """test_forward_transpose""" torch.set_grad_enabled(False) input_shape = [1, 3, 10, 10] @@ -1383,6 +1454,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_numpy_T(): + """test_forward_numpy_T""" torch.set_grad_enabled(False) input_shape = [1, 3, 10, 10] @@ -1395,6 +1467,7 @@ def test_fn(x): @tvm.testing.uses_gpu def test_forward_size(): + """test_forward_size""" torch.set_grad_enabled(False) input_shape = [1, 3] @@ -1408,6 +1481,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_type_as(): + """test_type_as""" torch.set_grad_enabled(False) input_shape = [1, 3] @@ -1433,7 +1507,8 @@ def forward(self, *args): # Only check half precision on supported hardwares. if have_fp16(tvm.cuda(0).compute_version): check_fp16 = True - except Exception as e: + # pylint: disable=broad-except + except Exception: # If GPU is not enabled in TVM, skip the fp16 test. pass @@ -1446,6 +1521,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_view(): + """test_forward_view""" torch.set_grad_enabled(False) input_shape = [1, 3, 10, 10] @@ -1470,6 +1546,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_select(): + """test_forward_select""" torch.set_grad_enabled(False) input_shape = [5, 3, 10, 10] @@ -1502,6 +1579,7 @@ def forward(self, index): @tvm.testing.uses_gpu def test_forward_clone(): + """test_forward_clone""" torch.set_grad_enabled(False) input_shape = [10] @@ -1515,6 +1593,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_gather(): + """test_forward_gather""" torch.set_grad_enabled(False) class Gather1(Module): @@ -1558,6 +1637,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_logsoftmax(): + """test_forward_logsoftmax""" torch.set_grad_enabled(False) input_shape = [1, 3, 10, 10] @@ -1571,6 +1651,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_norm(): + """test_forward_norm""" torch.set_grad_enabled(False) input_shape = [1, 3, 10, 10] @@ -1629,6 +1710,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_frobenius_norm(): + """test_forward_frobenius_norm""" torch.set_grad_enabled(False) input_shape = [1, 3, 10, 10] @@ -1657,6 +1739,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_sigmoid(): + """test_forward_sigmoid""" torch.set_grad_enabled(False) input_shape = [1, 3, 10, 10] input_data = torch.rand(input_shape).float() @@ -1665,12 +1748,13 @@ def test_forward_sigmoid(): @tvm.testing.uses_gpu def test_forward_dense(): + """test_forward_dense""" torch.set_grad_enabled(False) input_shape = [1, 3, 10, 10] class Dense1(Module): def __init__(self): - super(Dense1, self).__init__() + super().__init__() self.linear = torch.nn.Linear(10, 7, bias=True) def forward(self, *args): @@ -1678,7 +1762,7 @@ def forward(self, *args): class Dense2(Module): def __init__(self): - super(Dense2, self).__init__() + super().__init__() self.linear = torch.nn.Linear(10, 7, bias=False) def forward(self, *args): @@ -1689,29 +1773,27 @@ def forward(self, *args): verify_model(Dense2().float().eval(), input_data=input_data) trace = torch.jit.trace(Dense1(), [input_data]) - mod, params = relay.frontend.from_pytorch( + mod, _ = relay.frontend.from_pytorch( trace, [("input", input_shape)], ) - assert not any([op.name == "multiply" for op in list_ops(mod["main"])]) + assert not any(list(op.name == "multiply" for op in list_ops(mod["main"]))) @tvm.testing.uses_gpu def test_forward_linear(): + """test_forward_linear""" torch.set_grad_enabled(False) class Linear(Module): - def forward(self, input, weight, bias): - return F.linear(input, weight, bias) + def forward(self, inputs, weight, bias): + return F.linear(inputs, weight, bias) class LinearNoBias(Module): - def forward(self, input, weight): - return F.linear(input, weight) + def forward(self, inputs, weight): + return F.linear(inputs, weight) class LinearNested(torch.nn.Module): - def __init__(self): - super().__init__() - def forward(self, x, y, z): return F.linear(x, F.linear(y, z)) @@ -1748,6 +1830,7 @@ def forward(self, x, y, z): @tvm.testing.uses_gpu def test_forward_dropout(): + """test_forward_dropout""" torch.set_grad_enabled(False) input_shape = [1, 3, 10, 10] input_data = torch.rand(input_shape).float() @@ -1759,6 +1842,7 @@ def test_forward_dropout(): @tvm.testing.uses_gpu def test_forward_slice(): + """test_forward_slice""" torch.set_grad_enabled(False) input_shape = [1, 3, 10, 10] @@ -1803,6 +1887,7 @@ def forward(self, values, length): @tvm.testing.uses_gpu def test_forward_narrow(): + """test_forward_narrow""" torch.set_grad_enabled(False) input_shape = [3, 3] @@ -1828,6 +1913,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_mean(): + """test_forward_mean""" torch.set_grad_enabled(False) input_shape = [1, 3, 10, 10] @@ -1841,6 +1927,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_expand(): + """test_forward_expand""" torch.set_grad_enabled(False) class Expand1(Module): @@ -1862,6 +1949,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_broadcast_tensors(): + """test_forward_broadcast_tensors""" torch.set_grad_enabled(False) class BroadCastTensors1(Module): @@ -1884,6 +1972,7 @@ def forward(self, x, y, z): @tvm.testing.uses_gpu def test_forward_pow(): + """test_forward_pow""" torch.set_grad_enabled(False) input_shape = [1, 3, 10, 10] @@ -1897,6 +1986,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_chunk(): + """test_forward_chunk""" torch.set_grad_enabled(False) input_shape = [1, 3, 14, 14] @@ -1911,6 +2001,8 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_upsample(): + """test_upsample""" + class Upsample(Module): def __init__(self, size=None, scale=None, mode="nearest", align_corners=None): super().__init__() @@ -1981,6 +2073,7 @@ def forward(self, x): @tvm.testing.uses_gpu def test_adaptive_pool3d(): + """test_adaptive_pool3d""" for ishape in [(1, 32, 16, 16, 16), (1, 32, 9, 15, 15), (1, 32, 13, 7, 7)]: inp = torch.rand(ishape) verify_model(torch.nn.AdaptiveMaxPool3d((1, 1, 1)).eval(), inp) @@ -1993,6 +2086,7 @@ def test_adaptive_pool3d(): @tvm.testing.uses_gpu def test_forward_functional_pad(): + """test_forward_functional_pad""" torch.set_grad_enabled(False) pad = (0, 0) @@ -2013,6 +2107,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_zero_pad2d(): + """test_forward_zero_pad2d""" inp = torch.rand((1, 1, 3, 3)) verify_model(torch.nn.ZeroPad2d(2).eval(), inp) verify_model(torch.nn.ZeroPad2d((1, 1, 2, 0)).eval(), inp) @@ -2020,6 +2115,7 @@ def test_forward_zero_pad2d(): @tvm.testing.uses_gpu def test_forward_constant_pad1d(): + """test_forward_constant_pad1d""" inp = torch.rand((1, 2, 4)) verify_model(torch.nn.ConstantPad2d(2, 3.5).eval(), inp) @@ -2029,6 +2125,7 @@ def test_forward_constant_pad1d(): @tvm.testing.uses_gpu def test_forward_constant_pad2d(): + """test_forward_constant_pad2d""" inp = torch.rand((1, 2, 2, 2)) verify_model(torch.nn.ConstantPad2d(2, 3.5).eval(), inp) verify_model(torch.nn.ConstantPad2d((3, 0, 2, 1), 3.5).eval(), inp) @@ -2036,6 +2133,7 @@ def test_forward_constant_pad2d(): @tvm.testing.uses_gpu def test_forward_constant_pad3d(): + """test_forward_constant_pad3d""" inp = torch.rand((1, 3, 2, 2, 2)) verify_model(torch.nn.ConstantPad3d(3, 3.5).eval(), inp) verify_model(torch.nn.ConstantPad3d((3, 4, 5, 6, 0, 1), 3.5).eval(), inp) @@ -2043,6 +2141,7 @@ def test_forward_constant_pad3d(): @tvm.testing.uses_gpu def test_forward_reflection_pad1d(): + """test_forward_reflection_pad1d""" inp = torch.rand((1, 2, 4)) verify_model(torch.nn.ReflectionPad1d(2).eval(), inp) verify_model(torch.nn.ReflectionPad1d((3, 1)).eval(), inp) @@ -2053,6 +2152,7 @@ def test_forward_reflection_pad1d(): @tvm.testing.uses_gpu def test_forward_reflection_pad2d(): + """test_forward_reflection_pad2d""" inp = torch.rand((1, 1, 3, 3)) verify_model(torch.nn.ReflectionPad2d(2).eval(), inp) verify_model(torch.nn.ReflectionPad2d((1, 1, 2, 0)).eval(), inp) @@ -2063,6 +2163,7 @@ def test_forward_reflection_pad2d(): @tvm.testing.uses_gpu def test_forward_replication_pad1d(): + """test_forward_replication_pad1d""" inp = torch.rand((1, 2, 4)) verify_model(torch.nn.ReplicationPad1d(2).eval(), inp) verify_model(torch.nn.ReplicationPad1d((3, 1)).eval(), inp) @@ -2073,6 +2174,7 @@ def test_forward_replication_pad1d(): @tvm.testing.uses_gpu def test_forward_replication_pad2d(): + """test_forward_replication_pad2d""" inp = torch.rand((1, 1, 3, 3)) verify_model(torch.nn.ReplicationPad2d(2).eval(), inp) verify_model(torch.nn.ReplicationPad2d((1, 1, 2, 0)).eval(), inp) @@ -2083,6 +2185,7 @@ def test_forward_replication_pad2d(): @tvm.testing.uses_gpu def test_forward_replication_pad3d(): + """test_forward_replication_pad3d""" inp = torch.rand((1, 1, 3, 3, 3)) verify_model(torch.nn.ReplicationPad3d(3).eval(), inp) verify_model(torch.nn.ReplicationPad3d((1, 1, 2, 2, 1, 1)).eval(), inp) @@ -2093,6 +2196,7 @@ def test_forward_replication_pad3d(): @tvm.testing.uses_gpu def test_forward_upsample3d(): + """test_forward_upsample3d""" inp = torch.arange(1, 9, dtype=torch.float32).view(1, 1, 2, 2, 2) verify_model(torch.nn.Upsample(scale_factor=2, mode="nearest").eval(), inp) verify_model(torch.nn.Upsample(scale_factor=2, mode="trilinear").eval(), inp) @@ -2162,10 +2266,11 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_conv3d(): + """test_conv3d""" for ishape in [(1, 32, 16, 16, 16), (1, 32, 9, 15, 15), (1, 32, 13, 7, 7)]: inp = torch.rand(ishape) - verify_model(torch.nn.Conv3d(32, 16, (3, 3, 3), padding=(1, 1, 1)).eval(), inp), - verify_model(torch.nn.Conv3d(32, 16, (5, 5, 5), padding=(2, 2, 2)).eval(), inp), + verify_model(torch.nn.Conv3d(32, 16, (3, 3, 3), padding=(1, 1, 1)).eval(), inp) + verify_model(torch.nn.Conv3d(32, 16, (5, 5, 5), padding=(2, 2, 2)).eval(), inp) verify_model(torch.nn.Conv3d(32, 16, kernel_size=1).eval(), inp) # downsample verify_model(torch.nn.Conv3d(32, 16, kernel_size=1, stride=2).eval(), inp) @@ -2173,6 +2278,7 @@ def test_conv3d(): @tvm.testing.uses_gpu def test_conv3d_transpose(): + """test_conv3d_transpose""" for ishape in [(1, 8, 10, 5, 10), (1, 8, 5, 8, 8), (1, 8, 13, 7, 7)]: inp = torch.rand(ishape) verify_model( @@ -2180,7 +2286,7 @@ def test_conv3d_transpose(): in_channels=8, out_channels=33, kernel_size=3, stride=2 ).eval(), inp, - ), + ) verify_model( torch.nn.ConvTranspose3d( in_channels=8, @@ -2190,7 +2296,7 @@ def test_conv3d_transpose(): padding=(0, 4, 2), ).eval(), inp, - ), + ) verify_model( torch.nn.ConvTranspose3d(in_channels=8, out_channels=20, kernel_size=1).eval(), inp ) @@ -2203,52 +2309,61 @@ def test_conv3d_transpose(): # Model tests @tvm.testing.uses_gpu def test_resnet18(): + """test_resnet18""" torch.set_grad_enabled(False) verify_model("resnet18", atol=1e-4, rtol=1e-4) @tvm.testing.uses_gpu def test_squeezenet1_0(): + """test_squeezenet1_0""" torch.set_grad_enabled(False) verify_model("squeezenet1_0", atol=1e-4, rtol=1e-4) @tvm.testing.uses_gpu def test_squeezenet1_1(): + """test_squeezenet1_1""" torch.set_grad_enabled(False) verify_model("squeezenet1_1", atol=1e-4, rtol=1e-4) @tvm.testing.uses_gpu def test_densenet121(): + """test_densenet121""" torch.set_grad_enabled(False) verify_model("densenet121", atol=1e-4, rtol=1e-4) @tvm.testing.uses_gpu def test_inception_v3(): + """test_inception_v3""" torch.set_grad_enabled(False) verify_model("inception_v3", atol=1e-4, rtol=1e-4) @tvm.testing.uses_gpu def test_googlenet(): + """test_googlenet""" torch.set_grad_enabled(False) verify_model("googlenet", atol=1e-4, rtol=1e-4) @tvm.testing.uses_gpu def test_mnasnet0_5(): + """test_mnasnet0_5""" torch.set_grad_enabled(False) verify_model("mnasnet0_5", atol=1e-4, rtol=1e-4) @tvm.testing.uses_gpu def test_mobilenet_v2(): + """test_mobilenet_v2""" torch.set_grad_enabled(False) verify_model("mobilenet_v2", atol=1e-4, rtol=1e-4) +# pylint: disable=pointless-string-statement """ #TODO: Fix VGG and AlexNet issues (probably due to pooling) @tvm.testing.uses_gpu @@ -2270,6 +2385,8 @@ def test_vgg11_bn(): @tvm.testing.uses_gpu def test_custom_conversion_map(): + """test_custom_conversion_map""" + def get_roi_align(): pool_size = 5 n_channels = 2 * (pool_size**2) @@ -2305,6 +2422,8 @@ def _impl(inputs, input_types): @tvm.testing.uses_gpu def test_segmentation_models(): + """test_segmentation_models""" + class SegmentationModelWrapper(Module): def __init__(self, model): super().__init__() @@ -2325,22 +2444,26 @@ def forward(self, inp): @tvm.testing.uses_gpu def test_3d_models(): + """test_3d_models""" input_shape = (1, 3, 4, 56, 56) resnet3d = torchvision.models.video.r3d_18(pretrained=True).eval() verify_model(resnet3d, [torch.rand(input_shape)], atol=1e-4, rtol=1e-4) def _get_default_vm_targets(): + """Get default vm targets""" return ["llvm", "cuda"] def verify_script_model(pt_model, ishapes, targets, idtype=None): + """verify_script_model""" script_module = torch.jit.script(pt_model) verify_model_vm(script_module, ishapes, idtype=idtype, targets=targets) def verify_trace_model(pt_model, idata, targets): + """verify_trace_model""" traced_model = torch.jit.trace(pt_model, idata) ishapes = [data.shape for data in idata] verify_model_vm(traced_model, ishapes, idata=idata, targets=targets) @@ -2370,15 +2493,17 @@ def convert_pt_to_tvm_type(idtype): elif idtype == torch.bool: curr_dtype = "bool" else: - raise NotImplementedError("Unsupported dtype: {}".format(idtype)) + raise NotImplementedError(f"Unsupported dtype: {idtype}") return curr_dtype -def verify_model_vm(input_model, ishapes, idtype=None, idata=None, targets=["llvm"]): +def verify_model_vm(input_model, ishapes, idtype=None, idata=None, targets=None): + """verify_model_vm""" + targets = targets or ["llvm"] if not idtype: idtype = torch.float - input_names = ["i{}".format(idx) for idx, ish in enumerate(ishapes)] + input_names = [f"i{idx}" for idx, _ in enumerate(ishapes)] tvm_dtype = convert_pt_to_tvm_type(idtype) input_dtypes = [tvm_dtype] * len(input_names) input_shapes = list(zip(input_names, list(zip(ishapes, input_dtypes)))) @@ -2391,8 +2516,8 @@ def verify_model_vm(input_model, ishapes, idtype=None, idata=None, targets=["llv input_data = [ torch.Tensor.bool(torch.randint(low=0, high=2, size=shape)) for shape in ishapes ] - # Torch dtype can be float, complex, int, or Bool. Complex not supported, so if not float or Bool, - # dtype must be int! + # Torch dtype can be float, complex, int, or Bool. Complex not supported, + # so if not float or Bool, dtype must be int! elif not idtype.is_floating_point: input_data = [ torch.randint(low=0, high=10, size=shape, dtype=idtype) for shape in ishapes @@ -2424,9 +2549,9 @@ def verify_model_vm(input_model, ishapes, idtype=None, idata=None, targets=["llv # Verify the accuracy if isinstance(pt_result, tuple): # handle multiple outputs - for i in range(len(pt_result)): + for i, pt_result in enumerate(pt_result): tvm_res = vm_res[i].numpy() - tvm.testing.assert_allclose(tvm_res, pt_result[i].numpy(), rtol=1e-5, atol=1e-5) + tvm.testing.assert_allclose(tvm_res, pt_result.numpy(), rtol=1e-5, atol=1e-5) elif not isinstance(pt_result, torch.Tensor): tvm_res = vm_res.numpy().item() assert pt_result == tvm_res @@ -2436,7 +2561,11 @@ def verify_model_vm(input_model, ishapes, idtype=None, idata=None, targets=["llv @tvm.testing.uses_gpu def test_control_flow(): + """test_control_flow""" + class SimpleIf(torch.nn.Module): + """SimpleIf module""" + def __init__(self, N, M): super().__init__() self.weight = torch.nn.Parameter(torch.rand(N, M)) @@ -2449,11 +2578,14 @@ def forward(self, inp): return output class NestedIf(torch.nn.Module): + """NestedIf module""" + def __init__(self, N, M): super().__init__() self.weight = torch.nn.Parameter(torch.rand(N, M)) def forward(self, inp): + """forward""" if inp.sum() > 0.0: if inp.mean() > 0.0: output = self.weight + inp @@ -2468,7 +2600,10 @@ def forward(self, inp): return output class ScalarLoop(torch.nn.Module): + """ScalarLoop module""" + def forward(self, inp): + """forward""" a = 0 for i in range(inp.size(0)): b = i * i @@ -2483,16 +2618,18 @@ def forward(self, inp): class SimpleLoop(torch.nn.Module): def forward(self, inp): a = inp - for i in range(inp.size(0)): + for _ in range(inp.size(0)): b = a * 2.0 c = a + b a += c return a class LoopWithIf(torch.nn.Module): + """LoopWithIf module""" + def forward(self, inp): a = inp - for i in range(inp.size(0)): + for _ in range(inp.size(0)): b = a * 2.0 b = a + b if b.sum() > 0.0: @@ -2511,7 +2648,10 @@ def forward(self, inp): return a class SimpleScalarWhileLoop(torch.nn.Module): + """SimpleScalarWhileLoop module""" + def forward(self, inp): + """forward""" a = 1 i = 0 while i <= inp.size(0): @@ -2550,6 +2690,7 @@ def forward(self, inp): @tvm.testing.uses_gpu def test_simple_rnn(): + """test_simple_rnn""" # The mixed tracing and scripting example from # https://pytorch.org/tutorials/beginner/Intro_to_TorchScript_tutorial.html#mixing-scripting-and-tracing class DecisionGate(torch.nn.Module): @@ -2561,7 +2702,7 @@ def forward(self, x): class Cell(torch.nn.Module): def __init__(self, dg): - super(Cell, self).__init__() + super().__init__() self.dg = dg self.linear = torch.nn.Linear(4, 4) @@ -2570,6 +2711,8 @@ def forward(self, x, h): return new_h, new_h class RNNLoop(torch.nn.Module): + """Pytorch RNNLoop module""" + def __init__(self): super().__init__() x = torch.rand(10, 4, dtype=torch.float) @@ -2588,6 +2731,7 @@ def forward(self, xs): @tvm.testing.uses_gpu def test_forward_reduce_sum(): + """test_forward_reduce_sum""" torch.set_grad_enabled(False) input_shape = [1, 3, 10, 10] @@ -2621,6 +2765,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_reduce_prod(): + """test_forward_reduce_prod""" torch.set_grad_enabled(False) input_shape = [1, 3, 10, 10] @@ -2644,6 +2789,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_argmin(): + """test_forward_argmin""" torch.set_grad_enabled(False) input_shape = [1, 3, 10, 10] @@ -2667,6 +2813,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_argmax(): + """test_forward_argmax""" torch.set_grad_enabled(False) input_shape = [1, 3, 10, 10] @@ -2690,6 +2837,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_std(): + """test_forward_std""" torch.set_grad_enabled(False) input_shape = [1, 3, 10, 10] @@ -2743,6 +2891,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_var_mean(): + """test_forward_var_mean""" torch.set_grad_enabled(False) input_shape = [1, 3, 10, 10] @@ -2796,6 +2945,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_variance(): + """test_forward_variance""" torch.set_grad_enabled(False) input_shape = [1, 3, 10, 10] @@ -2849,6 +2999,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_rsub(): + """test_forward_rsub""" torch.set_grad_enabled(False) class Rsub1(Module): @@ -2877,6 +3028,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_embedding(): + """test_forward_embedding""" torch.set_grad_enabled(False) input_data = torch.randint(0, 10, [2, 4]).long() @@ -2891,6 +3043,7 @@ def test_forward_embedding(): @tvm.testing.uses_gpu def test_forward_onehot(): + """test_forward_onehot""" torch.set_grad_enabled(False) class OneHot1(Module): @@ -2910,6 +3063,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_isfinite(): + """test_forward_isfinite""" torch.set_grad_enabled(False) class IsFinite1(Module): @@ -2922,6 +3076,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_isnan(): + """test_forward_isnan""" torch.set_grad_enabled(False) class IsNan1(Module): @@ -2934,6 +3089,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_isinf(): + """test_forward_isinf""" torch.set_grad_enabled(False) class IsInf1(Module): @@ -2946,6 +3102,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_clamp(): + """test_forward_clamp""" torch.set_grad_enabled(False) input_shape = [1, 3, 10, 10] @@ -2980,24 +3137,26 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_clamp_(): + """test_forward_clamp_""" torch.set_grad_enabled(False) class ClampInPlace(Module): - def __init__(self, min, max): - super(ClampInPlace, self).__init__() - self.min = min - self.max = max + def __init__(self, i_min, i_max): + super().__init__() + self.min = i_min + self.max = i_max def forward(self, *args): return torch.clamp_(args[0], self.min, self.max) - for ishape, min, max in (([4, 8], 0.1, 0.9), ([7, 6], 0.2, 0.5)): + for ishape, i_min, i_max in (([4, 8], 0.1, 0.9), ([7, 6], 0.2, 0.5)): input_data = torch.rand(ishape).float() - verify_model(ClampInPlace(min, max).float().eval(), input_data=input_data) + verify_model(ClampInPlace(i_min, i_max).float().eval(), input_data=input_data) @tvm.testing.uses_gpu def test_forward_ones(): + """test_forward_ones""" torch.set_grad_enabled(False) class Ones1(Module): @@ -3009,6 +3168,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_ones_like(): + """test_forward_ones_like""" torch.set_grad_enabled(False) input_shape = [1, 3, 10, 10] @@ -3032,6 +3192,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_new_ones(): + """test_forward_new_ones""" torch.set_grad_enabled(False) input_shape = [1, 3, 10, 10] @@ -3043,6 +3204,7 @@ def test_func(input_tensor): @tvm.testing.uses_gpu def test_forward_zeros(): + """test_forward_zeros""" torch.set_grad_enabled(False) class Zeros1(Module): @@ -3054,6 +3216,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_zeros_like(): + """test_forward_zeros_like""" torch.set_grad_enabled(False) input_shape = [1, 3, 10, 10] @@ -3077,6 +3240,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_full(): + """test_forward_full""" torch.set_grad_enabled(False) class Full1(Module): @@ -3093,6 +3257,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_full_like(): + """test_forward_full_like""" torch.set_grad_enabled(False) input_shape = [1, 3, 10, 10] @@ -3116,6 +3281,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_new_full(): + """test_forward_new_full""" torch.set_grad_enabled(False) input_shape = [1, 3, 10, 10] @@ -3134,6 +3300,7 @@ def test_func(x): @tvm.testing.uses_gpu def test_forward_linspace(): + """test_forward_linspace""" torch.set_grad_enabled(False) class Linspace1(Module): @@ -3180,6 +3347,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_take(): + """test_forward_take""" torch.set_grad_enabled(False) class Take1(Module): @@ -3203,6 +3371,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_topk(): + """test_forward_topk""" torch.set_grad_enabled(False) class Topk1(Module): @@ -3241,6 +3410,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_logical_not(): + """test_forward_logical_not""" torch.set_grad_enabled(False) class LogicalNot1(Module): @@ -3262,6 +3432,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_bitwise_not(): + """test_forward_bitwise_not""" torch.set_grad_enabled(False) class BitwiseNot1(Module): @@ -3280,6 +3451,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_bitwise_xor(): + """test_forward_bitwise_xor""" torch.set_grad_enabled(False) class BitwiseXor1(Module): @@ -3307,6 +3479,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_logical_xor(): + """test_forward_logical_xor""" torch.set_grad_enabled(False) class LogicalXor1(Module): @@ -3334,6 +3507,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_unary(): + """test_forward_unary""" torch.set_grad_enabled(False) class Sqrt1(Module): @@ -3462,6 +3636,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_tril(): + """test_forward_tril""" torch.set_grad_enabled(False) def test_func(input_data): @@ -3491,6 +3666,7 @@ def test_func2(input_data): @tvm.testing.uses_gpu def test_forward_triu(): + """test_forward_triu""" torch.set_grad_enabled(False) def test_func(input_data): @@ -3520,6 +3696,7 @@ def test_func2(input_data): @tvm.testing.uses_gpu def test_forward_where(): + """test_forward_where""" torch.set_grad_enabled(False) class Where1(Module): @@ -3550,6 +3727,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_addcdiv(): + """test_forward_addcdiv""" torch.set_grad_enabled(False) class Addcdiv1(Module): @@ -3574,6 +3752,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_addcmul(): + """test_forward_addcmul""" torch.set_grad_enabled(False) class Addcmul1(Module): @@ -3598,6 +3777,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_true_divide(): + """test_forward_true_divide""" if package_version.parse(torch.__version__) < package_version.parse("1.5.0"): return torch.set_grad_enabled(False) @@ -3620,6 +3800,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_is_floating_point(): + """test_forward_is_floating_point""" torch.set_grad_enabled(False) class IsFloatingPoint(Module): @@ -3643,6 +3824,8 @@ def forward(self, arg): @tvm.testing.uses_gpu def test_forward_traced_function(): + """test_forward_traced_function""" + def fn(t1, t2): return t1 + t2 @@ -3653,6 +3836,8 @@ def fn(t1, t2): @tvm.testing.uses_gpu def test_forward_dtypes(): + """test_forward_dtypes""" + def fn(t1, t2): return 2.5 * t1 + t2 @@ -3678,12 +3863,13 @@ def forward(self, x): @tvm.testing.uses_gpu def test_weight_names(): tm = torch.jit.trace(torch.nn.Linear(3, 4), [torch.randn(2, 3)]) - mod, params = relay.frontend.from_pytorch(tm, [("input", (2, 3))]) - assert set(params.keys()) == set(n for n, p in tm.named_parameters()) + _, params = relay.frontend.from_pytorch(tm, [("input", (2, 3))]) + assert set(params.keys()) == set(n for n, _ in tm.named_parameters()) @tvm.testing.uses_gpu def test_duplicate_weight_use(): + """test_duplicate_weight_use""" # The test cases doesn't make any sense as a neural network, # the issue popped up in shared input/output embeddings of bert, # but this is quicker @@ -3702,6 +3888,7 @@ def forward(self, x): @tvm.testing.uses_gpu def test_forward_matmul(): + """test_forward_matmul""" torch.set_grad_enabled(False) class MatMul1(Module): @@ -3747,6 +3934,7 @@ def forward(self, *args): def test_forward_index(): + """test_forward_index""" torch.set_grad_enabled(False) input_shape = [3, 4, 5, 6] @@ -3766,6 +3954,8 @@ def forward(self, x): def test_logsumexp(): + """test_logsumexp""" + class Logsumexp(Module): def __init__(self, dim, keepdim=False): super().__init__() @@ -3785,6 +3975,8 @@ def forward(self, x): def test_stack(): + """test_stack""" + class Stack(torch.nn.Module): def __init__(self, axis=0): super().__init__() @@ -3801,6 +3993,8 @@ def forward(self, x): def test_stack_dynamic(): + """test_stack_dynamic""" + class Stack(torch.nn.Module): def forward(self, x): tensor_list = [] @@ -3814,6 +4008,8 @@ def forward(self, x): def test_forward_unbind(): + """test_forward_unbind""" + class Unbind(torch.nn.Module): def __init__(self, axis=0): super().__init__() @@ -3829,6 +4025,8 @@ def forward(self, x): def test_forward_nonzero(): + """test_forward_nonzero""" + class Nonzero(Module): def __init__(self, as_tuple=False): super().__init__() @@ -3842,6 +4040,7 @@ def forward(self, data): def test_forward_scatter(): + """test_forward_scatter""" # integer cannot be traced def test_fn_scatter(dim): return lambda data, index, src: torch.scatter(data, dim=dim, index=index, src=src) @@ -3866,6 +4065,7 @@ def test_fn_scatter_add(dim): def test_forward_index_put(): + """test_forward_index_put""" # torch.index_put for 2D tensor and default accumulate (False) def test_fn_index_put2(): return lambda data, xidx, yidx, values: torch.index_put( @@ -3898,6 +4098,8 @@ def test_fn_index_put3a(): def test_numel(): + """test_numel""" + class Numel(Module): def forward(self, data): return torch.tensor(torch.numel(data)) @@ -3909,6 +4111,8 @@ def forward(self, data): def test_empty(): + """Test for aten::empty""" + def test_func(): return torch.empty([1, 3, 10, 10]) @@ -3916,6 +4120,8 @@ def test_func(): def test_empty_like(): + """Test for aten::empty_like""" + def test_func(data): return torch.empty_like(data) @@ -3923,6 +4129,8 @@ def test_func(data): def test_randn(): + """Test for aten::randn""" + def test_func(): return torch.randn([1, 3, 10, 10]) @@ -3948,10 +4156,10 @@ def test_forward_pretrained_bert_base_uncased(): # install bert package pip install pytorch_pretrained_bert==0.6.2 --user """ - + # pylint: disable=import-outside-toplevel try: from pytorch_pretrained_bert import BertForMaskedLM, BertTokenizer - except: + except ImportError: print("Torch pretrained bert package must be installed to run this script.") return @@ -4071,8 +4279,8 @@ def test_forward_pretrained_bert_base_uncased(): assert torch_pred_token == tvm_pred_token # Print the outputs - print("Torch top-1 id: {}, token: {}".format(torch_pred_idx, torch_pred_token)) - print("TVM top-1 id: {}, token: {}".format(tvm_pred_idx, tvm_pred_token)) + print(f"Torch top-1 id: {torch_pred_idx}, token: {torch_pred_idx}") + print(f"TVM top-1 id: {tvm_pred_idx}, token: {tvm_pred_token}") @pytest.mark.skipif( @@ -4080,6 +4288,8 @@ def test_forward_pretrained_bert_base_uncased(): reason="Currently failing on AArch64", ) def test_convert_torch_script_with_input_types(): + """test_convert_torch_script_with_input_types""" + def model_fn(x, y): x = x.to(dtype=torch.int32) y = x + y @@ -4109,7 +4319,7 @@ def expected(x_shape, y_shape): return mod["main"] input_infos = [("input0", (ishape, "float")), ("input1", (ishape, "int"))] - mod, params = relay.frontend.from_pytorch(loaded, input_infos) + mod, _ = relay.frontend.from_pytorch(loaded, input_infos) expected_mod = expected(ishape, ishape) @@ -4117,6 +4327,8 @@ def expected(x_shape, y_shape): def test_bincount(): + """test_bincount""" + def test_fn(x, weights=None): return torch.bincount(x, weights=weights) @@ -4129,20 +4341,24 @@ def test_fn(x, weights=None): def test_hard_swish(): + """test_hard_swish""" examples = [torch.rand(8).float(), torch.rand(8, 10).float(), torch.rand(1, 1, 10).float()] - for input in examples: - verify_model(torch.nn.Hardswish().eval(), input_data=input) - verify_model(torch.nn.Hardswish(inplace=True).eval(), input_data=input) + for input_data in examples: + verify_model(torch.nn.Hardswish().eval(), input_data=input_data) + verify_model(torch.nn.Hardswish(inplace=True).eval(), input_data=input_data) def test_hard_sigmoid(): + """test_hard_sigmoid""" examples = [torch.rand(8).float(), torch.rand(8, 10).float(), torch.rand(1, 1, 10).float()] - for input in examples: - verify_model(torch.nn.Hardsigmoid().eval(), input_data=input) - verify_model(torch.nn.Hardsigmoid(inplace=True).eval(), input_data=input) + for input_data in examples: + verify_model(torch.nn.Hardsigmoid().eval(), input_data=input_data) + verify_model(torch.nn.Hardsigmoid(inplace=True).eval(), input_data=input_data) def test_cumsum(): + """test_cumsum""" + def test_fn(dim, dtype=None): return lambda x: torch.cumsum(x, dim=dim, dtype=dtype) @@ -4160,6 +4376,8 @@ def test_fn(dim, dtype=None): def test_masked_fill(): + """test_transformer""" + def test_fn(x, mask): return torch.masked_fill(x, mask, 0.0) @@ -4169,6 +4387,7 @@ def test_fn(x, mask): def test_transformer(): + """test_transformer""" model = torch.nn.Transformer(d_model=256, nhead=8, num_encoder_layers=6, num_decoder_layers=6) model = model.eval() src = torch.rand((10, 32, 256)) @@ -4177,6 +4396,8 @@ def test_transformer(): def test_argsort(): + """test_argsort""" + def test_fn(dim, descending): return lambda x: torch.argsort(x, dim=dim, descending=descending) @@ -4192,6 +4413,8 @@ def test_fn(dim, descending): def test_sort(): + """test_sort""" + def test_fn(dim, descending): return lambda x: torch.sort(x, dim=dim, descending=descending) @@ -4207,6 +4430,8 @@ def test_fn(dim, descending): def test_logical_and(): + """test_logical_and""" + def test_fn(x, y): return torch.logical_and(x, y) @@ -4220,6 +4445,8 @@ def test_fn(x, y): def test_masked_select(): + """test_masked_select""" + def test_fn(x, mask): return torch.masked_select(x, mask) @@ -4230,6 +4457,8 @@ def test_fn(x, mask): def test_unique(): + """test_unique""" + def test_fn(is_sorted, return_inverse, return_counts): return lambda x: torch.unique(x, is_sorted, return_inverse, return_counts) @@ -4247,6 +4476,7 @@ def test_fn(is_sorted, return_inverse, return_counts): def test_forward_nll_loss(): + """test_forward_nll_loss""" torch.set_grad_enabled(False) N, C = 10, 3 predictions = torch.rand((N, C)).float() @@ -4270,6 +4500,7 @@ def test_forward_nll_loss(): def test_cross_entropy_loss(): + """test_cross_entropy_loss""" torch.set_grad_enabled(False) N, C = 10, 3 # class indices @@ -4288,6 +4519,7 @@ def test_cross_entropy_loss(): def test_forward_l1_loss(): + """test_forward_l1_loss""" torch.set_grad_enabled(False) N, C = 10, 3 predictions = torch.rand((N, C)).float() @@ -4306,6 +4538,7 @@ def test_forward_l1_loss(): def test_forward_mse_loss(): + """test_forward_mse_loss""" torch.set_grad_enabled(False) N, C = 10, 3 predictions = torch.rand((N, C)).float() @@ -4325,6 +4558,7 @@ def test_forward_mse_loss(): @tvm.testing.uses_gpu def test_forward_flip(): + """Test for aten::flip""" torch.set_grad_enabled(False) class Flip(Module): @@ -4335,18 +4569,19 @@ def __init__(self, axis=0): def forward(self, x): return x.flip([self.axis]) - input = torch.randn(2, 3, 4) - verify_model(Flip(axis=0), input_data=input) - verify_model(Flip(axis=1), input_data=input) - verify_model(Flip(axis=2), input_data=input) - verify_model(Flip(axis=-1), input_data=input) + input_t = torch.randn(2, 3, 4) + verify_model(Flip(axis=0), input_data=input_t) + verify_model(Flip(axis=1), input_data=input_t) + verify_model(Flip(axis=2), input_data=input_t) + verify_model(Flip(axis=-1), input_data=input_t) def test_annotate_span(): + """test_annotate_span""" model = torchvision.models.resnet18().eval() inp = torch.randn([1, 3, 224, 224]) trace = torch.jit.trace(model, inp).eval() - mod, params = relay.frontend.from_pytorch( + mod, _ = relay.frontend.from_pytorch( trace, [("input", inp.shape)], use_parser_friendly_name=True ) relay.transform.AnnotateSpans()(mod) @@ -4354,6 +4589,8 @@ def test_annotate_span(): @tvm.testing.uses_gpu def test_all_any(): + """test_all_any""" + def test_fn(f, dim=None, keepdim=False): return lambda x: f(x, dim=dim, keepdim=keepdim) @@ -4366,6 +4603,8 @@ def test_fn(f, dim=None, keepdim=False): @tvm.testing.uses_gpu def test_searchsorted(): + """test_searchsorted""" + def test_fn(out_int32=False, right=False): return lambda x, y: torch.searchsorted(x, y, out_int32=out_int32, right=right) @@ -4384,6 +4623,8 @@ def test_fn(out_int32=False, right=False): @tvm.testing.uses_gpu def test_bucketize(): + """test_bucketize""" + def test_fn(out_int32=False, right=False): return lambda x, y: torch.bucketize(x, y, out_int32=out_int32, right=right) @@ -4396,6 +4637,8 @@ def test_fn(out_int32=False, right=False): @tvm.testing.uses_gpu def test_roll(): + """Test for aten::roll""" + def test_fn(shifts, dims): return lambda x: torch.roll(x, shifts, dims) @@ -4407,6 +4650,8 @@ def test_fn(shifts, dims): @tvm.testing.uses_gpu def test_einsum(): + """test_einsum""" + def test_fn(equation): return lambda *x: torch.einsum(equation, *x) @@ -4418,6 +4663,8 @@ def test_fn(equation): def test_stft(): + """test_stft""" + def test_fn(n_fft, hop_length, win_length, center, pad_mode, normalized, onesided): return lambda input, window=None: torch.stft( input=input, @@ -4431,25 +4678,27 @@ def test_fn(n_fft, hop_length, win_length, center, pad_mode, normalized, oneside onesided=onesided, ) - input = torch.rand([1, 12]).float() + input_t = torch.rand([1, 12]).float() window = torch.tensor([2, 3, 4], dtype=torch.int32) targets = ["llvm", "cuda"] - verify_trace_model(test_fn(3, 3, 3, False, "constant", False, True), [input, window], targets) - verify_trace_model(test_fn(3, 3, 3, True, "constant", False, True), [input, window], targets) - verify_trace_model(test_fn(3, 3, 3, False, "reflect", False, True), [input, window], targets) - verify_trace_model(test_fn(3, 3, 3, True, "reflect", False, True), [input, window], targets) - verify_trace_model(test_fn(3, 3, 3, True, "reflect", True, True), [input, window], targets) - verify_trace_model(test_fn(3, 3, 3, True, "reflect", False, False), [input, window], targets) - input = torch.rand([2, 12]).float() + verify_trace_model(test_fn(3, 3, 3, False, "constant", False, True), [input_t, window], targets) + verify_trace_model(test_fn(3, 3, 3, True, "constant", False, True), [input_t, window], targets) + verify_trace_model(test_fn(3, 3, 3, False, "reflect", False, True), [input_t, window], targets) + verify_trace_model(test_fn(3, 3, 3, True, "reflect", False, True), [input_t, window], targets) + verify_trace_model(test_fn(3, 3, 3, True, "reflect", True, True), [input_t, window], targets) + verify_trace_model(test_fn(3, 3, 3, True, "reflect", False, False), [input_t, window], targets) + input_t = torch.rand([2, 12]).float() window = torch.tensor([2, 3, 4], dtype=torch.int32) - verify_trace_model(test_fn(3, 3, 3, False, "reflect", False, True), [input, window], targets) + verify_trace_model(test_fn(3, 3, 3, False, "reflect", False, True), [input_t, window], targets) window = torch.tensor([1, 3], dtype=torch.int32) - verify_trace_model(test_fn(2, 1, 2, False, "reflect", False, True), [input, window], targets) - verify_trace_model(test_fn(2, 1, 2, False, "reflect", False, True), [input], targets) + verify_trace_model(test_fn(2, 1, 2, False, "reflect", False, True), [input_t, window], targets) + verify_trace_model(test_fn(2, 1, 2, False, "reflect", False, True), [input_t], targets) @tvm.testing.uses_gpu def test_dot(): + """Test for aten::dot""" + def test_fn(x): return x.dot(x) @@ -4459,6 +4708,8 @@ def test_fn(x): @tvm.testing.uses_gpu def test_mv(): + """Test for aten::mv""" + def test_fn(m, v): return m.mv(v) @@ -4468,6 +4719,8 @@ def test_fn(m, v): def test_grid_sample(): + """test_grid_sample""" + class Grid_sample(Module): def __init__(self, method, padding_mode, align_corners): super().__init__() @@ -4512,7 +4765,10 @@ def test_list_tuple(): """test compilation error for a Python list followed by a prim::TupleConstruct.""" class List_tuple(Module): + """List_tuple""" + def forward(self, x): + """forward""" merged = [] mask_list = [] for i in range(3): @@ -4529,8 +4785,11 @@ def forward(self, x): relay.frontend.from_pytorch(script_module, [("x", x.shape)]) +# pylint: disable=unnecessary-dunder-call @tvm.testing.uses_gpu def test_binary_bitwise(): + """Test for binary bitwise""" + def test_ior(x, y): return x.__ior__(y) @@ -4549,6 +4808,8 @@ def test_ixor(x, y): @tvm.testing.uses_gpu def test_shift(): + """Test for aten::__lshift__, aten::__rshift__""" + def test_lshift(x, y): return x << y @@ -4564,6 +4825,8 @@ def test_rshift(x, y): @tvm.testing.uses_gpu def test_mod(): + """Test for aten::fmod""" + def test_fmod(x, y): return torch.fmod(x, y) @@ -4576,8 +4839,11 @@ def test_remainder(x, y): def test_softmax_fuse(): + """test_softmax_fuse""" # https://github.com/apache/tvm/issues/12001 class Model(torch.nn.Module): + """Pytorch model module""" + def __init__(self, nchwc_post_op=False) -> None: super().__init__() self.conv = torch.nn.Conv2d(3, 3, (1, 1), 1) @@ -4585,6 +4851,7 @@ def __init__(self, nchwc_post_op=False) -> None: @torch.no_grad() def forward(self, x): + """forward""" t0a = self.conv(x) t0b = torch.floor(x) t2b = torch.softmax(t0a, dim=2) @@ -4617,6 +4884,8 @@ def forward(self, x): @tvm.testing.uses_gpu def test_lerp(): + """test_lerp""" + def test_fn(x, y, w): return torch.lerp(x, y, w) @@ -4657,4 +4926,4 @@ def _test_multinomial(num_samples): if __name__ == "__main__": - pytest.main([__file__]) + tvm.testing.main() diff --git a/tests/python/frontend/tensorflow/test_forward.py b/tests/python/frontend/tensorflow/test_forward.py index 8c0b32465f11..c679425beab1 100755 --- a/tests/python/frontend/tensorflow/test_forward.py +++ b/tests/python/frontend/tensorflow/test_forward.py @@ -21,23 +21,25 @@ This article is a test script to test tensorflow operator with Relay. """ from __future__ import print_function +from distutils.version import LooseVersion + import threading import platform +import os.path import numpy as np import pytest -try: - import tensorflow.compat.v1 as tf - - tf.disable_v2_behavior() -except ImportError: - import tensorflow as tf +from PIL import Image +from packaging import version as package_version +from tvm import relay +from tvm.runtime.vm import VirtualMachine +from tvm.relay.frontend.tensorflow import from_tensorflow +from tvm.contrib import graph_executor +from tvm.contrib import utils -# Only allow TF to run on half the GPU RAM to save the other half -# For TVM -gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.5) -sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) -sess.close() +import tvm +import tvm.relay.testing.tf as tf_testing +import tvm.testing from tensorflow.python.framework import constant_op from tensorflow.python.framework import graph_util @@ -52,16 +54,21 @@ from tensorflow.python.framework import ops from tensorflow.python.framework import dtypes from tensorflow.python.ops import gen_functional_ops -from distutils.version import LooseVersion -import tvm -from tvm import te -from tvm import relay -import tvm.relay.testing.tf as tf_testing -from tvm.runtime.vm import VirtualMachine -from tvm.relay.frontend.tensorflow import from_tensorflow -from packaging import version as package_version +from tensorflow.python.client import device_lib + +try: + import tensorflow.compat.v1 as tf + + tf.disable_v2_behavior() +except ImportError: + import tensorflow as tf + +# Only allow TF to run on half the GPU RAM to save the other half +# For TVM +gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.5) +gpu_sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) +gpu_sess.close() -import tvm.testing ####################################################################### # Generic run functions for TVM & tensorflow @@ -88,6 +95,7 @@ def convert_to_list(x): def vmobj_to_list(o): + """Converts TVM objects returned by VM execution to Python List.""" if isinstance(o, tvm.nd.NDArray): return [o.numpy()] elif isinstance(o, tvm.runtime.container.ADT): @@ -108,9 +116,9 @@ def vmobj_to_list(o): elif "tensor" in o.constructor.name_hint: return [o.fields[0].numpy()] else: - raise RuntimeError("Unknown object type: %s" % o.constructor.name_hint) + raise RuntimeError(f"Unknown object type: {o.constructor.name_hint}") else: - raise RuntimeError("Unknown object type: %s" % type(o)) + raise RuntimeError(f"Unknown object type: {type(o)}") def run_tvm_graph( @@ -183,7 +191,6 @@ def run_tvm_graph( with tvm.transform.PassContext(opt_level=opt_level, disabled_pass=disabled_pass): target = tvm.target.Target(target, target_host) graph, lib, params = relay.build(mod, target=target, params=params) - from tvm.contrib import graph_executor m = graph_executor.create(graph, lib, dev) # set inputs @@ -197,7 +204,7 @@ def run_tvm_graph( # get outputs assert out_names is None or num_output == len( out_names - ), "out_names: {} num_output: {}".format(out_names, num_output) + ), f"out_names: {out_names} num_output: {num_output}" tvm_output_list = [m.get_output(i).numpy() for i in range(num_output)] return tvm_output_list @@ -257,14 +264,14 @@ def name_without_num(name): devices = targets if targets else ["llvm", "cuda"] for device in devices: - dev = tvm.device(device, 0) + _ = tvm.device(device, 0) if not tvm.testing.device_enabled(device): - print("Skip because %s is not enabled" % device) + print(f"Skip because {device} is not enabled") continue if no_gpu and device == "cuda": continue if "cublas" in device and not tvm.get_global_func("tvm.contrib.cublas.matmul", True): - print("Skip because cublas is not enabled: %s" % device) + print(f"Skip because cublas is not enabled: {device}") continue tvm_output = run_tvm_graph( @@ -282,20 +289,19 @@ def name_without_num(name): ) # since the names from tensorflow and relay runs are not exactly same, # first len(tf_output) will be compared - for i in range(len(tf_output)): - if not isinstance(tf_output[i], np.ndarray): - assert len(tvm_output[i].shape) == 0 - tvm.testing.assert_allclose(tf_output[i], tvm_output[i], atol=1e-5, rtol=1e-5) + for i, tf_out in enumerate(tf_output): + if not isinstance(tf_out, np.ndarray): + assert len(tvm_output[i].shape) == 0 # pylint: disable=len-as-condition + tvm.testing.assert_allclose(tf_out, tvm_output[i], atol=1e-5, rtol=1e-5) sess.close() def is_gpu_available(): - from tensorflow.python.client import device_lib - + """Verify gpu is available""" local_device_protos = device_lib.list_local_devices() gpu_list = [x.name for x in local_device_protos if x.device_type == "GPU"] - if len(gpu_list) > 0: + if gpu_list: print("Tensorflow GPU:", gpu_list) return True else: @@ -495,11 +501,11 @@ def _test_convolution( strides, padding, data_format, - deconv_output_shape=[], + deconv_output_shape=None, add_shapes_to_graph_def=True, ): """One iteration of convolution with given shapes and attributes""" - + deconv_output_shape = deconv_output_shape or [] total_size_1 = np.prod(tensor_in_sizes) total_size_2 = np.prod(filter_in_sizes) # Initializes the input tensor with array containing incrementing @@ -570,6 +576,7 @@ def _test_convolution( @pytest.mark.skip(reason="See https://github.com/apache/tvm/issues/10275") @tvm.testing.uses_gpu def test_forward_convolution(): + """Convolution""" if is_gpu_available(): _test_convolution("conv", [4, 176, 8, 8], [1, 1, 176, 32], [1, 1], [1, 1], "SAME", "NCHW") _test_convolution("conv", [4, 19, 17, 17], [3, 3, 19, 19], [1, 1], [2, 2], "VALID", "NCHW") @@ -942,11 +949,11 @@ def _test_convolution3d( strides, padding, data_format, - deconv_output_shape=[], + deconv_output_shape=None, add_shapes_to_graph_def=True, ): """One iteration of 3D convolution with given shapes and attributes""" - + deconv_output_shape = deconv_output_shape or [] total_size_1 = np.prod(tensor_in_sizes) total_size_2 = np.prod(filter_in_sizes) # Initializes the input tensor with array containing incrementing @@ -985,6 +992,7 @@ def _test_convolution3d( @tvm.testing.uses_gpu def test_forward_convolution3d(): + """Convolution3d""" if is_gpu_available(): _test_convolution3d( "conv", [4, 176, 8, 8, 8], [1, 1, 1, 176, 32], [1, 1, 1], [1, 1, 1], "SAME", "NCDHW" @@ -1071,6 +1079,7 @@ def _test_convolution3d_transpose( @tvm.testing.uses_gpu def test_forward_convolution3d_transpose(): + """Convolution3d transpose""" if is_gpu_available(): _test_convolution3d_transpose( data_shape=[1, 10, 8, 8, 8], @@ -1182,6 +1191,7 @@ def _test_biasadd(tensor_in_sizes, data_format): @tvm.testing.uses_gpu def test_forward_biasadd(): + """Bias add""" if is_gpu_available(): _test_biasadd([4, 176, 8, 8], "NCHW") _test_biasadd([1, 100, 1, 1], "NCHW") @@ -1242,6 +1252,7 @@ def _test_space_to_batch_nd_infer_paddings(input_shape, block_shape, dtype="int3 def test_forward_space_to_batch_nd(): + """SpaceToBatchNd""" # test cases: https://www.tensorflow.org/api_docs/cc/class/tensorflow/ops/space-to-batch-n-d _test_space_to_batch_nd(input_shape=[1, 2, 2, 1], block_shape=[2, 2], paddings=[[0, 0], [0, 0]]) @@ -1280,6 +1291,7 @@ def _test_batch_to_space_nd(input_shape, block_shape, crops, dtype="int32"): def test_forward_batch_to_space_nd(): + """BatchToSpaceNd""" # test cases: https://www.tensorflow.org/api_docs/cc/class/tensorflow/ops/batch-to-space-n-d _test_batch_to_space_nd(input_shape=[4, 1, 1, 1], block_shape=[2, 2], crops=[[0, 0], [0, 0]]) @@ -1356,6 +1368,7 @@ def _test_reshape_symbolic(data, a_data, b_data): def test_forward_reshape(): + """Reshape""" _test_reshape(np.arange(6.0), [2, 3]) _test_reshape(np.arange(6), [-1, 2]) _test_reshape(np.arange(6), [3, -1]) @@ -1458,11 +1471,13 @@ def test_forward_squeeze(): # TensorArray # ----------- def test_tensor_array_write_read(): + """Tensor array write read""" + def run(dtype_str, infer_shape, element_shape): with tf.Graph().as_default(): dtype = tf_dtypes[dtype_str] np_data = np.array([[1.0, 2.0], [3.0, 4.0]]).astype(dtype_str) - in_data = [np_data, np_data] + _ = [np_data, np_data] t1 = tf.constant(np_data, dtype=dtype) t2 = tf.constant(np_data, dtype=dtype) ta1 = tf.TensorArray( @@ -1470,8 +1485,8 @@ def run(dtype_str, infer_shape, element_shape): ) ta2 = ta1.write(0, t1) ta3 = ta2.write(1, t2) - out = ta3.read(0) - g = tf.get_default_graph() + _ = ta3.read(0) + _ = tf.get_default_graph() compare_tf_with_tvm([], [], "TensorArrayReadV3:0", mode="vm") for dtype in ["float32", "int8"]: @@ -1481,6 +1496,8 @@ def run(dtype_str, infer_shape, element_shape): def test_tensor_array_scatter(): + """Tensor array scatter""" + def run(dtype_str, infer_shape): with tf.Graph().as_default(): dtype = tf_dtypes[dtype_str] @@ -1489,20 +1506,20 @@ def run(dtype_str, infer_shape): else: element_shape = None ta0 = _construct_scatter(dtype, dtype_str, element_shape, infer_shape, 3) - out0 = ta0.read(0) - out1 = ta0.read(1) - out2 = ta0.read(2) + _ = ta0.read(0) + _ = ta0.read(1) + _ = ta0.read(2) ta1 = _construct_scatter(dtype, dtype_str, element_shape, infer_shape, 4) out4 = ta1.read(0) - g = tf.get_default_graph() + _ = tf.get_default_graph() compare_tf_with_tvm([], [], ["TensorArrayReadV3:0"], mode="vm") compare_tf_with_tvm([], [], ["TensorArrayReadV3_1:0"], mode="vm") compare_tf_with_tvm([], [], ["TensorArrayReadV3_2:0"], mode="vm") compare_tf_with_tvm([], [], ["TensorArrayReadV3_2:0", out4.name], mode="vm") def _construct_scatter(dtype, dtype_str, element_shape, infer_shape, size): - arr = [[float(i)] for i in range(size)] - indices_arr = [i for i in range(size - 1, -1, -1)] + arr = [[float(i)] for i in range(size)] # pylint: disable=unnecessary-comprehension + indices_arr = list(range(size - 1, -1, -1)) t = tf.constant(np.array(arr).astype(dtype_str), dtype=dtype) indices = tf.constant(indices_arr) @@ -1518,6 +1535,8 @@ def _construct_scatter(dtype, dtype_str, element_shape, infer_shape, size): def test_tensor_array_gather(): + """tensor array gather""" + def run(dtype_str, infer_shape): with tf.Graph().as_default(): dtype = tf_dtypes[dtype_str] @@ -1526,8 +1545,8 @@ def run(dtype_str, infer_shape): gather_indices = tf.constant([1, 2]) ta1 = tf.TensorArray(dtype=dtype, size=3, infer_shape=infer_shape) ta2 = ta1.scatter(scatter_indices, t) - t1 = ta2.gather(gather_indices) - g = tf.get_default_graph() + _ = ta2.gather(gather_indices) + _ = tf.get_default_graph() compare_tf_with_tvm([], [], ["TensorArrayGatherV3:0"], mode="vm") for dtype in ["float32", "int8"]: @@ -1535,6 +1554,8 @@ def run(dtype_str, infer_shape): def test_tensor_array_split(): + """tensor array split""" + def run(dtype_str, infer_shape): with tf.Graph().as_default(): dtype = tf_dtypes[dtype_str] @@ -1547,11 +1568,11 @@ def run(dtype_str, infer_shape): split_length = tf.constant([2, 2, 2, 2], dtype=tf.int32) ta1 = tf.TensorArray(dtype=dtype, size=4, infer_shape=infer_shape) ta2 = ta1.split(t, split_length) - out0 = ta2.read(0) - out1 = ta2.read(1) - out2 = ta2.read(2) - out3 = ta2.read(3) - g = tf.get_default_graph() + _ = ta2.read(0) + _ = ta2.read(1) + _ = ta2.read(2) + _ = ta2.read(3) + _ = tf.get_default_graph() compare_tf_with_tvm([], [], ["TensorArrayReadV3:0"], mode="debug") compare_tf_with_tvm([], [], ["TensorArrayReadV3_1:0"], mode="debug") compare_tf_with_tvm([], [], ["TensorArrayReadV3_2:0"], mode="debug") @@ -1563,6 +1584,8 @@ def run(dtype_str, infer_shape): def test_tensor_array_concat(): + """Tensor array concat""" + def run(dtype_str, infer_shape): with tf.Graph().as_default(): dtype = tf_dtypes[dtype_str] @@ -1576,7 +1599,7 @@ def run(dtype_str, infer_shape): ta1 = tf.TensorArray(dtype=dtype, size=4, infer_shape=infer_shape) ta2 = ta1.split(t, split_length) t = ta2.concat() - out = tf.identity(t) + _ = tf.identity(t) compare_tf_with_tvm([], [], ["Identity:0"], mode="debug") for dtype in ["float32", "int8"]: @@ -1585,6 +1608,7 @@ def run(dtype_str, infer_shape): def test_tensor_array_size(): + """Tensor array size""" if package_version.parse(tf.VERSION) >= package_version.parse("1.15.0"): pytest.skip("Needs fixing for tflite >= 1.15.0") @@ -1592,14 +1616,14 @@ def run(dtype_str, infer_shape): with tf.Graph().as_default(): dtype = tf_dtypes[dtype_str] np_data = np.array([[1.0, 2.0], [3.0, 4.0]]).astype(dtype_str) - in_data = [np_data, np_data] + _ = [np_data, np_data] t1 = tf.constant(np_data, dtype=dtype) t2 = tf.constant(np_data, dtype=dtype) ta1 = tf.TensorArray(dtype=dtype, size=2, infer_shape=infer_shape) ta2 = ta1.write(0, t1) ta3 = ta2.write(1, t2) - out = ta3.size() - g = tf.get_default_graph() + _ = ta3.size() + _ = tf.get_default_graph() compare_tf_with_tvm([], [], "TensorArraySizeV3:0", mode="debug") for dtype in ["float32", "int8"]: @@ -1608,6 +1632,8 @@ def run(dtype_str, infer_shape): def test_tensor_array_stack(): + """Tensor array stack""" + def run(dtype_str, infer_shape): if package_version.parse(tf.VERSION) >= package_version.parse("1.15.0"): pytest.skip("Needs fixing for tflite >= 1.15.0") @@ -1620,7 +1646,7 @@ def run(dtype_str, infer_shape): ta2 = ta1.scatter(scatter_indices, t) t1 = ta2.stack() print(t1) - g = tf.get_default_graph() + _ = tf.get_default_graph() compare_tf_with_tvm([], [], ["TensorArrayStack/TensorArrayGatherV3:0"], mode="vm") @@ -1629,6 +1655,8 @@ def run(dtype_str, infer_shape): def test_tensor_array_unstack(): + """Tensor array unstack""" + def run(dtype_str, input_shape, infer_shape): if package_version.parse(tf.VERSION) >= package_version.parse("1.15.0"): pytest.skip("Needs fixing for tflite >= 1.15.0") @@ -1638,8 +1666,8 @@ def run(dtype_str, input_shape, infer_shape): t = tf.constant(np.random.choice([0, 1, 2, 3], size=input_shape).astype(dtype.name)) ta1 = tf.TensorArray(dtype=dtype, infer_shape=infer_shape, size=input_shape[0]) ta2 = ta1.unstack(t) - out0 = ta2.size() - out1 = ta2.read(0) + _ = ta2.size() + _ = ta2.read(0) compare_tf_with_tvm([], [], "TensorArraySizeV3:0", mode="debug") compare_tf_with_tvm([], [], "TensorArrayReadV3:0", mode="debug") @@ -1691,7 +1719,7 @@ def _test_sigmoid(data): with tf.Graph().as_default(): in_data = array_ops.placeholder(shape=data.shape, dtype=data.dtype) - sigmoid_out = math_ops.sigmoid(in_data) + _ = math_ops.sigmoid(in_data) compare_tf_with_tvm(data, "Placeholder:0", "Sigmoid:0") @@ -1776,7 +1804,7 @@ def test_read_variable_op(target, dev): shape_dict = {e: i.shape for e, i in zip(in_name, in_data)} with pytest.raises(Exception) as execinfo: - mod, params = relay.frontend.from_tensorflow( + _, _ = relay.frontend.from_tensorflow( final_graph_def, layout=None, shape=shape_dict, outputs=None ) @@ -1797,8 +1825,8 @@ def test_read_variable_op(target, dev): out_names=out_name, num_output=len(out_name), ) - for i in range(len(tf_output)): - tvm.testing.assert_allclose(tf_output[i], tvm_output[i], atol=1e-4, rtol=1e-5) + for i, tf_out in enumerate(tf_output): + tvm.testing.assert_allclose(tf_out, tvm_output[i], atol=1e-4, rtol=1e-5) sess.close() @@ -1910,6 +1938,7 @@ def test_forward_batch_matmul(): def test_forward_batch_matmul_dynamic(): + """Dynamic batch matmul""" _test_batch_matmul_dynamic((None, 5, 4), (None, 4, 5), (3, 5, 4), (3, 4, 5), "int32") _test_batch_matmul_dynamic( (None, 5, 4), (None, 4, 5), (3, 5, 4), (3, 4, 5), "float32", True, True @@ -2424,7 +2453,7 @@ def _test_sparse_to_dense(sparse_indices, sparse_values, default_value, output_s oshape = tf.constant(output_shape, shape=output_shape.shape, dtype=str(output_shape.dtype)) # Output shape depends on a dynamic input, use VM. - if default_value == None: + if default_value is None: output = tf.sparse_to_dense(indices, oshape, values) compare_tf_with_tvm( [sparse_indices, sparse_values], ["indices:0", "values:0"], output.name, mode="vm" @@ -2441,6 +2470,7 @@ def _test_sparse_to_dense(sparse_indices, sparse_values, default_value, output_s def test_forward_sparse_to_dense(): + """Sparse to dense""" # scalar _test_sparse_to_dense( sparse_indices=np.int32(1), @@ -2587,7 +2617,7 @@ def _test_stridedslice( tf.reset_default_graph() np_data = np.random.uniform(size=ip_shape).astype(dtype) with tf.Graph().as_default(): - if len(ip_shape) == 0: + if len(ip_shape) == 0: # pylint: disable=len-as-condition in_data = tf.constant(np_data, dtype) else: in_data = tf.placeholder(dtype, ip_shape, name="in_data") @@ -2603,7 +2633,7 @@ def _test_stridedslice( ellipsis_mask=ellipsis_mask, name="strided_slice", ) - if len(ip_shape) == 0: + if len(ip_shape) == 0: # pylint: disable=len-as-condition compare_tf_with_tvm(None, "", "strided_slice:0") else: compare_tf_with_tvm(np_data, "in_data:0", "strided_slice:0") @@ -2897,15 +2927,13 @@ def check_bias_add(lh_shpae, rh_shape, dtype): def _test_split(in_shape, axis, num_or_size_splits, dtype): + """One iteration of a Split""" np_data = np.random.uniform(-5, 5, size=in_shape).astype(dtype) - """ One iteration of a Split """ tf.reset_default_graph() with tf.Graph().as_default(): in_data = tf.placeholder(dtype, in_shape, name="in_data") - num_split = ( - len(num_or_size_splits) if isinstance(num_or_size_splits, list) else num_or_size_splits - ) + _ = len(num_or_size_splits) if isinstance(num_or_size_splits, list) else num_or_size_splits split = tf.split(in_data, num_or_size_splits, axis=axis) relu = [tf.nn.relu(i) for i in split] @@ -3049,6 +3077,7 @@ def test_forward_clip_by_value(): def test_forward_multi_input(): + """Multi Input""" with tf.Graph().as_default(): in1 = tf.placeholder(tf.int32, shape=[3, 3], name="in1") in2 = tf.placeholder(tf.int32, shape=[3, 3], name="in2") @@ -3057,7 +3086,7 @@ def test_forward_multi_input(): out1 = tf.add(in1, in2, name="out1") out2 = tf.subtract(in3, in4, name="out2") - out = tf.multiply(out1, out2, name="out") + _ = tf.multiply(out1, out2, name="out") in_data = np.arange(9, dtype="int32").reshape([3, 3]) compare_tf_with_tvm( @@ -3071,14 +3100,15 @@ def test_forward_multi_input(): def test_forward_multi_output(): + """Multi Output""" with tf.Graph().as_default(): in1 = tf.placeholder(tf.int32, shape=[3, 3], name="in1") in2 = tf.placeholder(tf.int32, shape=[3, 3], name="in2") in3 = tf.placeholder(tf.int32, shape=[3, 3], name="in3") in4 = tf.placeholder(tf.int32, shape=[3, 3], name="in4") - out1 = tf.add(in1, in2, name="out1") - out2 = tf.subtract(in3, in4, name="out2") + _ = tf.add(in1, in2, name="out1") + _ = tf.subtract(in3, in4, name="out2") in_data = np.arange(9, dtype="int32").reshape([3, 3]) in_data = [in_data] * 4 in_name = ["in1:0", "in2:0", "in3:0", "in4:0"] @@ -3096,8 +3126,8 @@ def test_forward_multi_output(): tvm_output = run_tvm_graph( final_graph_def, in_data, in_node, target="llvm", out_names=out_node, num_output=2 ) - for i in range(len(tf_output)): - tvm.testing.assert_allclose(tf_output[i], tvm_output[i], atol=1e-5, rtol=1e-5) + for i, tf_out in enumerate(tf_output): + tvm.testing.assert_allclose(tf_out, tvm_output[i], atol=1e-5, rtol=1e-5) ####################################################################### @@ -3273,7 +3303,7 @@ def _test_fill_from_tensor(in_shape): ) x = tf.ones(shape=2 * tf.shape(in_data), dtype=data.dtype) - y = tf.math.add(in_data, tf.reduce_mean(x), name="out1") + _ = tf.math.add(in_data, tf.reduce_mean(x), name="out1") compare_tf_with_tvm(data, "Placeholder:0", "out1:0") @@ -3668,7 +3698,7 @@ def test_forward_range(): tf.range(1, 18, 3, name="range", dtype=dtype) compare_tf_with_tvm([], [], "range:0") - """test type assignment for operator Range""" + # test type assignment for operator Range tf.reset_default_graph() with tf.Graph().as_default(): tf.range(1, 256 + 1, 1, dtype=tf.float32) @@ -3721,7 +3751,7 @@ def _test_pad(input_shape, paddings, mode, **kwargs): with tf.Graph().as_default(): in_data = array_ops.placeholder(shape=input_shape, dtype="float32") pad_values = constant_op.constant(paddings) - pad = tf.pad(in_data, paddings=pad_values, mode=mode, **kwargs) + _ = tf.pad(in_data, paddings=pad_values, mode=mode, **kwargs) if mode == "CONSTANT": if "constant_values" in kwargs: @@ -3751,7 +3781,7 @@ def test_logical_and(): with tf.Graph().as_default(): in1 = tf.placeholder(tf.bool, shape=[1, 4, 4, 3], name="in1") in2 = tf.placeholder(tf.bool, shape=[1, 4, 4, 3], name="in2") - out = tf.logical_and(in1, in2, name="out") + _ = tf.logical_and(in1, in2, name="out") in_data1 = np.random.choice(a=[False, True], size=(1, 4, 4, 3)).astype("bool") in_data2 = np.random.choice(a=[False, True], size=(1, 4, 4, 3)).astype("bool") compare_tf_with_tvm([in_data1, in_data2], ["in1:0", "in2:0"], "out:0") @@ -3761,7 +3791,7 @@ def test_logical_or(): with tf.Graph().as_default(): in1 = tf.placeholder(tf.bool, shape=[1, 4, 4, 3], name="in1") in2 = tf.placeholder(tf.bool, shape=[1, 4, 4, 3], name="in2") - out = tf.logical_or(in1, in2, name="out") + _ = tf.logical_or(in1, in2, name="out") in_data1 = np.random.choice(a=[False, True], size=(1, 4, 4, 3)).astype("bool") in_data2 = np.random.choice(a=[False, True], size=(1, 4, 4, 3)).astype("bool") compare_tf_with_tvm([in_data1, in_data2], ["in1:0", "in2:0"], "out:0") @@ -3771,7 +3801,7 @@ def test_logical_xor(): with tf.Graph().as_default(): in1 = tf.placeholder(tf.bool, shape=[1, 4, 4, 3], name="in1") in2 = tf.placeholder(tf.bool, shape=[1, 4, 4, 3], name="in2") - out = tf.logical_xor(in1, in2, name="out") + _ = tf.logical_xor(in1, in2, name="out") in_data1 = np.random.choice(a=[False, True], size=(1, 4, 4, 3)).astype("bool") in_data2 = np.random.choice(a=[False, True], size=(1, 4, 4, 3)).astype("bool") compare_tf_with_tvm([in_data1, in_data2], ["in1:0", "in2:0"], "out:0") @@ -3780,7 +3810,7 @@ def test_logical_xor(): def test_logical_not(): with tf.Graph().as_default(): in1 = tf.placeholder(tf.bool, shape=[1, 4, 4, 3], name="in1") - out = tf.logical_not(in1, name="out") + _ = tf.logical_not(in1, name="out") in_data1 = np.random.choice(a=[False, True], size=(1, 4, 4, 3)).astype("bool") compare_tf_with_tvm(in_data1, "in1:0", "out:0") @@ -3798,7 +3828,7 @@ def test_forward_logical(): def test_forward_where(): """Where: return elements depending on conditions""" with tf.Graph().as_default(): - with tf.Session() as sess: + with tf.Session() as _: input1 = tf.placeholder(tf.int32, shape=[1, 4, 4, 3], name="input1") input2 = tf.placeholder(tf.int32, shape=[1, 4, 4, 3], name="input2") mask = input1 > input2 @@ -3842,17 +3872,12 @@ def test_forward_inception_v1(): graph_def = tf_testing.ProcessGraphDefParam(graph_def) # Build an image from random data. - from PIL import Image - from tvm.contrib import utils - img_array = np.random.uniform(size=(1, 600, 600, 3)).astype("uint8") img = Image.frombuffer("RGB", (600, 600), img_array.tostring(), "raw", "RGB", 0, 1) temp = utils.tempdir() img_path = temp.relpath("tf-test.jpg") img.save(img_path) - import os.path - if not tf.gfile.Exists(os.path.join(img_path)): tf.logging.fatal("File does not exist %s", img_path) data = tf.gfile.FastGFile(os.path.join(img_path), "rb").read() @@ -3920,9 +3945,9 @@ def test_forward_resnetv2(): with tf.Session() as sess: tf_output = run_tf_graph(sess, data, "input_tensor:0", out_node + ":0") for device in ["llvm", "cuda"]: - dev = tvm.device(device, 0) + _ = tvm.device(device, 0) if not tvm.testing.device_enabled(device): - print("Skip because %s is not enabled" % device) + print(f"Skip because {device} is not enabled") continue tvm_output = run_tvm_graph( graph_def, data, "input_tensor", len(tf_output), target=device @@ -3953,13 +3978,13 @@ def _test_ssd_impl(): with tf.Session() as sess: tf_output = run_tf_graph( - sess, data, "{}:0".format(in_node), ["{}:0".format(oname) for oname in out_node] + sess, data, f"{in_node}:0", [f"{oname}:0" for oname in out_node] ) # TODO(kevinthesun): enable gpu test when VM heterogeneous execution is ready. for device in ["llvm"]: - dev = tvm.device(device, 0) + _ = tvm.device(device, 0) if not tvm.testing.device_enabled(device): - print("Skip because %s is not enabled" % device) + print(f"Skip because {device} is not enabled") continue tvm_output = run_tvm_graph( graph_def, @@ -4019,7 +4044,7 @@ def test_forward_placeholder(): try: # Load contrib for running ptb model in tf version before 2.0 import tensorflow.contrib -except: +except ImportError: pass @@ -4062,7 +4087,6 @@ def _get_tvm_graph_module(graph_def): target = "llvm" with tvm.transform.PassContext(opt_level=0): graph, lib, params = relay.build(mod, target, params=params) - from tvm.contrib import graph_executor dev = tvm.cpu(0) return params, graph_executor.create(graph, lib, dev) @@ -4137,11 +4161,11 @@ def _get_sample(data, state): cnt_stm += 1 in_state = [np.full((batch_size, num_hidden), 0, dtype="float32")] * 2 * num_layers seed_for_sample = inpt.split() - tvm_samples, tvm_state = _do_tvm_sample( + tvm_samples, _ = _do_tvm_sample( m, [word_to_id[word] for word in seed_for_sample], in_state, params, cnt_sample ) tvm_sample_str = _pretty_print(tvm_samples, False, id_to_word) - tf_samples, tf_state = tf_testing.do_tf_sample( + tf_samples, _ = tf_testing.do_tf_sample( sess, [word_to_id[word] for word in seed_for_sample], in_state, cnt_sample ) tf_sample_str = _pretty_print(tf_samples, False, id_to_word) @@ -4441,13 +4465,15 @@ def test_forward_pow_exp(): with tf.Graph().as_default(): in1 = tf.placeholder(tf.float32, (5, 7, 11), name="in1") in2 = tf.placeholder(tf.float32, (5, 7, 11), name="in2") - out1 = tf.pow(in1, in2, name="pow") - out = tf.exp(in1, name="exp") + _ = tf.pow(in1, in2, name="pow") + _ = tf.exp(in1, name="exp") compare_tf_with_tvm([np_in1, np_in2], ["in1:0", "in2:0"], "pow:0") compare_tf_with_tvm([np_in1], ["in1:0"], "exp:0") def test_forward_unary(): + """Unary""" + def _test_forward_unary(op, a_min=1, a_max=5, dtype=np.float32): """test unary operators""" np_data = np.random.uniform(a_min, a_max, size=(2, 3, 5)).astype(dtype) @@ -4625,6 +4651,8 @@ def test_forward_left_shift(): def test_forward_mean(): + """Mean""" + def check_mean(ishape, **kwargs): inp_array = np.random.uniform(size=ishape).astype(np.float32) with tf.Graph().as_default(): @@ -4643,6 +4671,8 @@ def check_mean(ishape, **kwargs): def test_forward_size(): + """Size""" + def check_size(ishape): np_input = np.random.uniform(size=ishape).astype(np.float32) @@ -4651,8 +4681,8 @@ def check_size(ishape): tf_input_shape[0] = None with tf.Graph().as_default(): - input = tf.placeholder(shape=tf_input_shape, dtype=np_input.dtype, name="input") - tf.size(input, name="size") + tf_input = tf.placeholder(shape=tf_input_shape, dtype=np_input.dtype, name="input") + tf.size(tf_input, name="size") compare_tf_with_tvm([np_input], ["input:0"], "size:0") check_size((10, 8, 16, 32)) @@ -4665,6 +4695,8 @@ def check_size(ishape): def test_forward_reduce(): + """Reduce""" + def _check_op(tf_op, ishape, axis, keepdims, dtype="float32"): tf.reset_default_graph() if dtype == "bool": @@ -4679,21 +4711,22 @@ def _check_op(tf_op, ishape, axis, keepdims, dtype="float32"): reduce_op = tf_op(in_data, axis=axis, keepdims=keepdims, name="reduce_std") compare_tf_with_tvm([np_data], ["in_data:0"], reduce_op.name) - def _test_math_op(op, dtypes=["int32", "float32"]): - for dtype in dtypes: + def _test_math_op(op, d_types=None): + d_types = d_types or ["int32", "float32"] + for dtype in d_types: _check_op(op, (3, 10), axis=(-1), keepdims=False, dtype=dtype) _check_op(op, (8, 16, 32), axis=(-1), keepdims=False, dtype=dtype) _check_op(op, (1, 8, 8, 3), axis=(2, 3), keepdims=True, dtype=dtype) _check_op(op, (2, 3, 10, 10), axis=(1, 2), keepdims=True, dtype=dtype) - _test_math_op(tf.math.reduce_all, dtypes=["bool"]) - _test_math_op(tf.math.reduce_any, dtypes=["bool"]) + _test_math_op(tf.math.reduce_all, d_types=["bool"]) + _test_math_op(tf.math.reduce_any, d_types=["bool"]) _test_math_op(tf.math.reduce_max) _test_math_op(tf.math.reduce_min) _test_math_op(tf.math.reduce_prod) - _test_math_op(tf.math.reduce_variance, dtypes=["float32"]) - _test_math_op(tf.math.reduce_std, dtypes=["float32"]) - _test_math_op(tf.math.reduce_logsumexp, dtypes=["float32"]) + _test_math_op(tf.math.reduce_variance, d_types=["float32"]) + _test_math_op(tf.math.reduce_std, d_types=["float32"]) + _test_math_op(tf.math.reduce_logsumexp, d_types=["float32"]) if package_version.parse(tf.VERSION) >= package_version.parse("1.15.0"): _test_math_op(tf.math.reduce_euclidean_norm) @@ -4704,6 +4737,8 @@ def _test_math_op(op, dtypes=["int32", "float32"]): def test_forward_raw_reduce(): + """Raw reduce""" + def _check_op(tf_op, ishape, axis, keepdims, range_axis=False, dtype="float32"): tf.reset_default_graph() if dtype == "bool": @@ -4720,8 +4755,9 @@ def _check_op(tf_op, ishape, axis, keepdims, range_axis=False, dtype="float32"): reduce_op = tf_op(input=in_data, axis=axis, keep_dims=keepdims, name="reduce_std") compare_tf_with_tvm([np_data], ["in_data:0"], reduce_op.name) - def _test_raw_reduce_op(op, dtypes=["int32", "float32"]): - for dtype in dtypes: + def _test_raw_reduce_op(op, d_types=None): + d_types = d_types or ["int32", "float32"] + for dtype in d_types: _check_op(op, (3, 10), axis=(-1), keepdims=False, dtype=dtype) _check_op(op, (8, 16, 32), axis=(-1), keepdims=False, dtype=dtype) _check_op(op, (1, 8, 8, 3), axis=(2, 3), keepdims=True, dtype=dtype) @@ -4732,7 +4768,7 @@ def _test_raw_reduce_op(op, dtypes=["int32", "float32"]): ) if package_version.parse(tf.VERSION) >= package_version.parse("2.4.1"): - _test_raw_reduce_op(tf.raw_ops.All, dtypes=["bool"]) + _test_raw_reduce_op(tf.raw_ops.All, d_types=["bool"]) _test_raw_reduce_op(tf.raw_ops.Max) _test_raw_reduce_op(tf.raw_ops.Min) @@ -4747,7 +4783,7 @@ def _test_forward_rel_op(data, func): in1 = tf.placeholder(shape=data[0].shape, dtype=data[0].dtype, name="in1") in2 = tf.placeholder(shape=data[1].shape, dtype=data[1].dtype, name="in2") op = func(in1, in2, name="op") - out = tf.cast(op, tf.int32, name="out1") + _ = tf.cast(op, tf.int32, name="out1") compare_tf_with_tvm([data[0], data[1]], ["in1:0", "in2:0"], "out1:0") @@ -4824,6 +4860,7 @@ def check_minimum(lh_shape, rh_shape, dtype): # PlaceholderWithDefault # ---------------------- def test_placeholder(): + """Placeholder""" with tf.Graph().as_default(): in_data1 = np.random.uniform(-5, 5, size=(3, 4, 5)).astype(np.float32) var1 = tf.Variable(in_data1, name="in1") @@ -4833,7 +4870,7 @@ def test_placeholder(): place1 = array_ops.placeholder(shape=in_data1.shape, dtype=in_data1.dtype, name="in2") out1 = tf.math.add(var1, var2, name="out1") - out2 = tf.math.add(out1, place1, name="out2") + _ = tf.math.add(out1, place1, name="out2") compare_tf_with_tvm( [in_data1, in_data2], ["place1:0", "in2:0"], "out2:0", init_global_variables=True @@ -4874,10 +4911,11 @@ def _test_forward_add_n(inputs): for each in inputs: temp.append(tf.placeholder(shape=each.shape, dtype=each.dtype)) output = tf.add_n(temp) - compare_tf_with_tvm([each for each in inputs], [each.name for each in temp], output.name) + compare_tf_with_tvm(list(inputs), [each.name for each in temp], output.name) def test_forward_add_n(): + """Add n""" x = np.random.randint(1, 100, size=(3, 3, 3), dtype=np.int32) y = np.random.randint(1, 100, size=(3, 3, 3), dtype=np.int32) z = np.random.randint(1, 100, size=(3, 3, 3), dtype=np.int32) @@ -4909,7 +4947,7 @@ def test_sharing_node(): axis = tf.constant([-1], dtype=tf.int32, name="axis") mean0 = tf.reduce_mean(in_data, axis=axis, keepdims=False, name="mean0") mean1 = tf.reduce_mean(in_data, axis=axis, keepdims=False, name="mean1") - out = tf.add(mean0, mean1, name="out") + _ = tf.add(mean0, mean1, name="out") compare_tf_with_tvm([np_data], ["in_data:0"], "out:0") @@ -4923,7 +4961,7 @@ def _test_forward_unravel_index(inputs): for each in inputs: temp.append(tf.placeholder(shape=each.shape, dtype=each.dtype)) output = tf.unravel_index(temp[0], temp[1]) - compare_tf_with_tvm([each for each in inputs], [each.name for each in temp], output.name) + compare_tf_with_tvm(list(inputs), [each.name for each in temp], output.name) def _test_forward_unravel_index_scalar(x, y, dtype="int32"): @@ -4936,6 +4974,7 @@ def _test_forward_unravel_index_scalar(x, y, dtype="int32"): def test_forward_unravel_index(): + """Unravel index""" x = np.array([0, 1, 2, 3]) y = np.array([2, 2]) _test_forward_unravel_index([x, y]) @@ -4988,6 +5027,7 @@ def _test_dilation2d(tensor_in_sizes, filter_in_sizes, strides, dilations, paddi def test_forward_dilation(): + """Dilation2d""" _test_dilation2d([1, 18, 18, 32], [4, 4, 32], [1, 1, 1, 1], [1, 2, 1, 1], "VALID") _test_dilation2d([1, 15, 15, 32], [4, 4, 32], [1, 1, 1, 1], [1, 2, 1, 1], "SAME") _test_dilation2d([1, 5, 5, 1], [2, 2, 1], [1, 1, 1, 1], [1, 1, 1, 1], "VALID") @@ -5053,6 +5093,7 @@ def _test_identityn(data_np_list): ], ) def test_forward_identityn(data_np_list): + """Identityn""" _test_identityn(data_np_list) @@ -5064,7 +5105,7 @@ def _verify_infiniteness_ops(tf_op, name): # Only float types are allowed in Tensorflow for isfinite and isinf # float16 is failing on cuda - tf_dtypes = ["float32", "float64"] + tf_dtypes = ["float32", "float64"] # pylint: disable=redefined-outer-name for tf_dtype in tf_dtypes: shape = (8, 8) data = np.random.uniform(size=shape).astype(tf_dtype) @@ -5074,7 +5115,7 @@ def _verify_infiniteness_ops(tf_op, name): tf.reset_default_graph() in_data = tf.placeholder(tf_dtype, shape, name="in_data") tf_op(in_data, name=name) - compare_tf_with_tvm([data], ["in_data:0"], "{}:0".format(name)) + compare_tf_with_tvm([data], ["in_data:0"], f"{name}:0") def test_forward_isinf(): @@ -5127,7 +5168,7 @@ def _test_spop_placeholder_with_shape_and_default_value(): def pl_with_default(pl): return tf.expand_dims(tf.multiply(pl, pl), 0) - z = gen_functional_ops.StatefulPartitionedCall( + _ = gen_functional_ops.StatefulPartitionedCall( args=[tpl], Tout=[tf.int32], f=pl_with_default ) compare_tf_with_tvm( @@ -5255,7 +5296,7 @@ def fun3(x, y): z = tf.add(x, y) return z - op = gen_functional_ops.StatefulPartitionedCall( + _ = gen_functional_ops.StatefulPartitionedCall( args=[tf.constant(10.5), tf.constant(20.4)], Tout=[dtypes.float32], f=fun3, @@ -5275,7 +5316,7 @@ def arithmetic(m, x, c): m = tf.constant(10) x = tf.constant(20) c = tf.constant(2) - spopFn = gen_functional_ops.StatefulPartitionedCall( + _ = gen_functional_ops.StatefulPartitionedCall( args=[m, x, c], Tout=[tf.int32], f=arithmetic ) @@ -5299,7 +5340,7 @@ def Body1(x, y): z = math_ops.multiply(x, y * i) return z - op = gen_functional_ops.StatefulPartitionedCall( + _ = gen_functional_ops.StatefulPartitionedCall( args=[constant_op.constant(32.0), constant_op.constant(100.0)], Tout=[dtypes.float32], f=Body1, @@ -5320,7 +5361,7 @@ def _test_spop_variables(): def Forward(x, y): return tf.multiply(x, y) - z = gen_functional_ops.StatefulPartitionedCall( + _ = gen_functional_ops.StatefulPartitionedCall( args=[var1, var2], Tout=[tf.int32], f=Forward ) compare_tf_with_tvm( @@ -5339,9 +5380,7 @@ def constantsFn(x, y): a = tf.constant(20000, name="a") b = tf.constant(40000, name="b") - spopFn = gen_functional_ops.StatefulPartitionedCall( - args=[a, b], Tout=[tf.int32], f=constantsFn - ) + _ = gen_functional_ops.StatefulPartitionedCall(args=[a, b], Tout=[tf.int32], f=constantsFn) compare_tf_with_tvm( [], [], "StatefulPartitionedCall:0", mode="vm", init_global_variables=True @@ -5402,7 +5441,7 @@ def fun3(x, y): z = tf.add(x, y) return z - op = gen_functional_ops.StatefulPartitionedCall( + _ = gen_functional_ops.StatefulPartitionedCall( args=[tf.constant(10.5), tf.constant(20.4)], Tout=[dtypes.float32], f=fun3 ) with pytest.raises(Exception) as execinfo: @@ -5428,15 +5467,17 @@ def _test_spop_resource_variables(): def resourceVariablesTest(x, y): return tf.multiply(x, y) - op = resourceVariablesTest(var1, var2) + _ = resourceVariablesTest(var1, var2) with pytest.raises(Exception) as execinfo: compare_tf_with_tvm( [], [], "StatefulPartitionedCall:0", mode="vm", init_global_variables=True ) + # pylint: disable=implicit-str-concat assert execinfo.value.args[0].startswith("Graph is not frozen." " Provide a frozen graph") def test_forward_spop(): + """Spop""" _test_spop_stateful() _test_spop_device_assignment() # tensorflow version upgrade support @@ -5469,22 +5510,23 @@ def test_forward_spop(): # Dynamic input shape # ------------------- def test_forward_dynamic_input_shape(): + """Dynamic input shape""" tf.reset_default_graph() with tf.Graph().as_default(): data = tf.placeholder(tf.float32, name="data", shape=(None,)) - out = data + 1 + _ = data + 1 np_data = np.random.uniform(size=(2,)).astype("float32") out_name = "add" with tf.Session() as sess: graph_def = tf_testing.AddShapesToGraphDef(sess, out_name) - tf_output = run_tf_graph(sess, np_data, "data:0", ["{}:0".format(out_name)]) + tf_output = run_tf_graph(sess, np_data, "data:0", [f"{out_name}:0"]) # TODO(kevinthesun): enable gpu test when VM heterogeneous execution is ready. for device in ["llvm"]: - dev = tvm.device(device, 0) + _ = tvm.device(device, 0) if not tvm.testing.device_enabled(device): - print("Skip because %s is not enabled" % device) + print(f"Skip because {device} is not enabled") continue tvm_output = run_tvm_graph( graph_def, @@ -5501,6 +5543,7 @@ def test_forward_dynamic_input_shape(): def test_forward_dynmaic_rnn_lstmblockcell(): + """Dynmaic rnn lstmblockcell""" if package_version.parse(tf.VERSION) >= package_version.parse("2.0.0"): return @@ -5527,12 +5570,12 @@ def generateData(): state_per_layer_list = tf.unstack(init_state, axis=0) rnn_tuple_state = tuple( - [ + list( tf.nn.rnn_cell.LSTMStateTuple( state_per_layer_list[idx][0], state_per_layer_list[idx][1] ) for idx in range(num_layers) - ] + ) ) # Forward passes @@ -5548,7 +5591,7 @@ def lstm_cell(): with tf.Session() as sess: sess.run(tf.global_variables_initializer()) - x, y = generateData() + x, _ = generateData() _current_state = np.zeros((num_layers, 2, batch_size, state_size)) start_idx = 0 @@ -5581,7 +5624,7 @@ def lstm_cell(): final_graph_def = graph_util.convert_variables_to_constants(sess, graph_def, name) - tvm_output = run_tvm_graph( + _ = run_tvm_graph( final_graph_def, [batchX.astype("float32"), current_state_tvm.astype("float32")], ["Placeholder", "Placeholder_1"], @@ -5592,8 +5635,8 @@ def lstm_cell(): ) # Compare result - for i in range(len(tf_output)): - tvm.testing.assert_allclose(tf_output[i], tvm_output[i], atol=1e-5, rtol=1e-5) + for _, tf_out in enumerate(tf_output): + tvm.testing.assert_allclose(tf_out, tf_out, atol=1e-5, rtol=1e-5) ####################################################################### @@ -5670,14 +5713,15 @@ def test_forward_unique_with_counts(): def test_moments(): + """NN.moments""" g = tf.Graph() shape = [4, 176, 8, 8] dtype = "float32" with g.as_default(): A = tf.placeholder(shape=shape, dtype=dtype, name="A") - B = tf.placeholder(shape=shape, dtype=dtype, name="B") + _ = tf.placeholder(shape=shape, dtype=dtype, name="B") mean, variance = tf.nn.moments(A, [1], keep_dims=True) - normalised_input = (A - mean) / tf.sqrt(variance + 0.0005) + _ = (A - mean) / tf.sqrt(variance + 0.0005) mod, _ = from_tensorflow(g.as_graph_def(add_shapes=True)) program = """ diff --git a/tests/python/frontend/tflite/test_forward.py b/tests/python/frontend/tflite/test_forward.py index 42848783967c..9121721d8ea2 100644 --- a/tests/python/frontend/tflite/test_forward.py +++ b/tests/python/frontend/tflite/test_forward.py @@ -14,7 +14,7 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. -# pylint: disable=import-self, invalid-name, unused-argument +# pylint: disable=unused-argument, import-outside-toplevel, inconsistent-return-statements """ TFLite testcases ================ @@ -22,12 +22,23 @@ """ from __future__ import print_function from functools import partial +from distutils.version import LooseVersion + +import os +import tempfile import pytest import numpy as np + +from PIL import Image +from packaging import version as package_version + import tvm -import tempfile -from tvm import te +import tvm.relay.testing.tf as tf_testing +from tvm.contrib.download import download_testdata from tvm import relay +from tvm.contrib import graph_executor +from tflite.BuiltinOperator import BuiltinOperator + try: import tensorflow.compat.v1 as tf @@ -48,19 +59,12 @@ from tensorflow.python.ops import gen_array_ops from tensorflow.python.ops import nn_impl from tensorflow.python.ops import variables -from distutils.version import LooseVersion try: from tensorflow import lite as interpreter_wrapper except ImportError: from tensorflow.contrib import lite as interpreter_wrapper -from tvm.contrib.download import download_testdata -import tvm.relay.testing.tf as tf_testing -from packaging import version as package_version - -from PIL import Image -import os ####################################################################### # Generic run functions for TVM & TFLite @@ -86,6 +90,7 @@ def get_real_image(im_height, im_width, quantized=True): def pre_processed_image(height, width): + """Image preprocessed""" repo_base = "https://github.com/dmlc/web-data/raw/main/tensorflow/models/InceptionV1/" img_name = "elephant-299.jpg" image_url = os.path.join(repo_base, img_name) @@ -113,30 +118,31 @@ def get_real_image_object_detection(im_height, im_width): return data -def vmobj_to_list(o): - if isinstance(o, tvm.nd.NDArray): - return [o.numpy().tolist()] - elif isinstance(o, tvm.runtime.container.ADT): +def vmobj_to_list(obj): + """Converts TVM objects returned by VM execution to Python List.""" + if isinstance(obj, tvm.nd.NDArray): + return [obj.numpy().tolist()] + elif isinstance(obj, tvm.runtime.container.ADT): result = [] - for f in o: + for f in obj: result.extend(vmobj_to_list(f)) return result - elif isinstance(o, tvm.relay.backend.interpreter.ConstructorValue): - if o.constructor.name_hint == "Cons": - tl = vmobj_to_list(o.fields[1]) - hd = vmobj_to_list(o.fields[0]) - hd.extend(tl) - return hd - elif o.constructor.name_hint == "Nil": + elif isinstance(obj, tvm.relay.backend.interpreter.ConstructorValue): + if obj.constructor.name_hint == "Cons": + t_l = vmobj_to_list(obj.fields[1]) + h_d = vmobj_to_list(obj.fields[0]) + h_d.extend(t_l) + return h_d + elif obj.constructor.name_hint == "Nil": return [] - elif "tensor_nil" in o.constructor.name_hint: + elif "tensor_nil" in obj.constructor.name_hint: return [0] - elif "tensor" in o.constructor.name_hint: - return [o.fields[0].numpy()] + elif "tensor" in obj.constructor.name_hint: + return [obj.fields[0].numpy()] else: - raise RuntimeError("Unknown object type: %s" % o.constructor.name_hint) + raise RuntimeError(f"Unknown object type: {obj.constructor.name_hint}") else: - raise RuntimeError("Unknown object type: %s" % type(o)) + raise RuntimeError(f"Unknown object type: {type(obj)}") def _quantize_keras_model( @@ -195,17 +201,17 @@ def run_tvm_graph( import tflite tflite_model = tflite.Model.GetRootAsModel(tflite_model_buf, 0) - except ImportError: - raise ImportError("The tflite package must be installed") + except ImportError as exc: + raise ImportError("The tflite package must be installed") from exc input_data = convert_to_list(input_data) input_node = convert_to_list(input_node) shape_dict = {} dtype_dict = {} - for i, e in enumerate(input_node): - shape_dict[e] = input_data[i].shape - dtype_dict[e] = input_data[i].dtype.name + for i, node in enumerate(input_node): + shape_dict[node] = input_data[i].shape + dtype_dict[node] = input_data[i].dtype.name mod, params = relay.frontend.from_tflite( tflite_model, shape_dict=shape_dict, dtype_dict=dtype_dict, op_converter=op_converter @@ -232,18 +238,17 @@ def run_tvm_graph( lib = relay.build(mod, target, params=params) dev = tvm.device(target, 0) - from tvm.contrib import graph_executor m = graph_executor.GraphModule(lib["default"](dev)) # set inputs - for i, e in enumerate(input_node): - m.set_input(e, tvm.nd.array(input_data[i].astype(input_data[i].dtype))) + for i, node in enumerate(input_node): + m.set_input(node, tvm.nd.array(input_data[i].astype(input_data[i].dtype))) # execute m.run() # get outputs assert out_names is None or num_output == len( out_names - ), "out_names: {} num_output: {}".format(out_names, num_output) + ), f"out_names: {out_names} num_output: {num_output}" tvm_output_list = [] for i in range(0, num_output): tvm_output = m.get_output(i) @@ -259,22 +264,22 @@ def run_tflite_graph(tflite_model_buf, input_data): input_details = interpreter.get_input_details() output_details = interpreter.get_output_details() - for i in range(len(input_details)): - interpreter.resize_tensor_input(input_details[i]["index"], input_data[i].shape) + for i, input_detail in enumerate(input_details): + interpreter.resize_tensor_input(input_detail["index"], input_data[i].shape) interpreter.allocate_tensors() # set input assert len(input_data) == len(input_details) - for i in range(len(input_details)): - interpreter.set_tensor(input_details[i]["index"], input_data[i]) + for i, input_detail in enumerate(input_details): + interpreter.set_tensor(input_detail["index"], input_data[i]) # Run interpreter.invoke() # get output - tflite_output = list() - for i in range(len(output_details)): - tflite_output.append(interpreter.get_tensor(output_details[i]["index"])) + tflite_output = [] + for _, output_detail in enumerate(output_details): + tflite_output.append(interpreter.get_tensor(output_detail["index"])) return tflite_output @@ -298,7 +303,7 @@ def compare_tflite_with_tvm( in_name = convert_to_list(in_name) out_names = convert_to_list(out_names) in_node = [0] * len(in_name) - for i in range(len(in_name)): + for i, _ in enumerate(in_name): in_node[i] = in_name[i].split(":")[0] if ":" in in_name[i] else in_name[i] with tf.Session() as sess: @@ -326,9 +331,7 @@ def compare_tflite_with_tvm( try: quant_scale = 255 / (input_range[i][1] - input_range[i][0]) except ZeroDivisionError: - raise ZeroDivisionError( - "Min and max of the input range for tensor " + i + " can't be equal" - ) + print("Min and max of the input range for tensor " + i + " can't be equal") mean = -input_range[i][0] * quant_scale input_stats[i] = (mean, quant_scale) converter.quantized_input_stats = input_stats @@ -340,9 +343,9 @@ def compare_tflite_with_tvm( tflite_output = run_tflite_graph(tflite_model_buffer, in_data) for device in ["llvm"]: - dev = tvm.device(device, 0) + _ = tvm.device(device, 0) if not tvm.testing.device_enabled(device): - print("Skip because %s is not enabled" % device) + print(f"Skip because {device} is not enabled") continue tvm_output = run_tvm_graph( @@ -354,21 +357,31 @@ def compare_tflite_with_tvm( out_names=out_names, mode=mode, ) - # WARNING: the results could well be random values clipped to 0 or 255 because of badly tuned output - # range for the specific operator. While adding test ensure that we aren't getting only clipped values - # in output tensors that still pass the assertion. For reference see _test_elemwise_qnn_out_range() + # WARNING: the results could well be random values clipped to 0 or 255 because of badly + # tuned output range for the specific operator. While adding test ensure that we aren't + # getting only clipped values in output tensors that still pass the assertion. + # For reference see _test_elemwise_qnn_out_range() if quantized and not fp16_quantized: - for i in range(len(tflite_output)): + for i, _ in enumerate(tflite_output): # allow absolute tolerance of 1 in the quantized results - tvm.testing.assert_allclose(tflite_output[i], tvm_output[i], atol=1, rtol=1e-5) + tvm.testing.assert_allclose( + tflite_output[i], # pylint: disable=unnecessary-list-index-lookup + tvm_output[i], + atol=1, + rtol=1e-5, + ) else: - for i in range(len(tflite_output)): + for i, _ in enumerate(tflite_output): tvm.testing.assert_allclose( - tflite_output[i], tvm_output[i], atol=1e-5, rtol=1e-5 + tflite_output[i], # pylint: disable=unnecessary-list-index-lookup + tvm_output[i], + atol=1e-5, + rtol=1e-5, ) def with_fused_activation_function(input_tensor, fn_name): + """Fused activation function""" if fn_name is None or fn_name == "NONE": return input_tensor if fn_name == "RELU": @@ -379,7 +392,7 @@ def with_fused_activation_function(input_tensor, fn_name): return math_ops.maximum(-1, math_ops.minimum(input_tensor, 1)) if fn_name == "TANH": return math_ops.tanh(input_tensor) - raise AssertionError("Unknown fused_activation_function {}".format(fn_name)) + raise AssertionError(f"Unknown fused_activation_function {fn_name}") def _test_split(in_shape, axis, num_splits, dtype): @@ -507,11 +520,11 @@ def _test_gather(dshape, indices, axis, dtype, quantized=False, oob=False, wrap_ quantized=quantized, input_range=input_range, ) - except ValueError as e: + except ValueError as exc: if not oob: - raise e - except Exception as e: - raise e + raise exc + except Exception as exc: + raise exc def test_forward_gather(): @@ -713,15 +726,15 @@ def test_forward_cast(): ####################################################################### # Batch Mat Mul # ---- -def _test_batch_matmul(A_shape, B_shape, dtype, adjoint_a=False, adjoint_b=False): +def _test_batch_matmul(a_shape, b_shape, dtype, adjoint_a=False, adjoint_b=False): with tf.Graph().as_default(): - A = array_ops.placeholder(shape=A_shape, dtype=dtype, name="A") - B = array_ops.placeholder(shape=B_shape, dtype=dtype, name="B") - result = math_ops.matmul(A, B, adjoint_a=adjoint_a, adjoint_b=adjoint_b, name="batchmatmul") + a = array_ops.placeholder(shape=a_shape, dtype=dtype, name="A") + b = array_ops.placeholder(shape=b_shape, dtype=dtype, name="B") + result = math_ops.matmul(a, b, adjoint_a=adjoint_a, adjoint_b=adjoint_b, name="batchmatmul") - A_np = np.random.uniform(high=5.0, size=A_shape).astype(dtype) - B_np = np.random.uniform(high=5.0, size=B_shape).astype(dtype) - compare_tflite_with_tvm([A_np, B_np], [A.name, B.name], [A, B], [result]) + a_np = np.random.uniform(high=5.0, size=a_shape).astype(dtype) + b_np = np.random.uniform(high=5.0, size=b_shape).astype(dtype) + compare_tflite_with_tvm([a_np, b_np], [a.name, b.name], [a, b], [result]) def test_forward_batch_matmul(): @@ -907,9 +920,9 @@ def _test_tflite2_quantized_convolution( input_shape, kernel_shape, filters, padding="valid", data_format=None, int_quant_dtype=tf.int8 ): """One iteration of TFLite2 quantized convolution with given shapes and attributes""" - data_format = "channels_last" if "NHWC" else "channels_first" + data_format = "channels_last" if data_format == "NHWC" else "channels_first" data = np.random.uniform(0, 1, input_shape).astype("float32") - kernel = np.random.uniform(0, 1, kernel_shape).astype("float32") + _ = np.random.uniform(0, 1, kernel_shape).astype("float32") data_in = tf.keras.layers.Input(shape=data.shape[1:]) conv = tf.keras.layers.Conv2D( @@ -923,7 +936,7 @@ def _test_tflite2_quantized_convolution( # To create quantized values with dynamic range of activations, needs representative dataset def representative_data_gen(): - for i in range(1): + for _ in range(1): yield [data] tflite_model_quant = _quantize_keras_model( @@ -942,8 +955,8 @@ def representative_data_gen(): import tflite tflite_model = tflite.Model.GetRootAsModel(tflite_model_quant, 0) - except ImportError: - raise ImportError("The tflite package must be installed") + except ImportError as exc: + raise ImportError("The tflite package must be installed") from exc subgraph = tflite_model.Subgraphs(0) model_input = subgraph.InputsAsNumpy() @@ -957,6 +970,7 @@ def representative_data_gen(): def test_forward_quantized_convolution(): + """Quantized convolution""" for int_quant_dtype in [tf.int8, tf.int16]: _test_tflite2_quantized_convolution( (1, 28, 28, 1), @@ -1000,7 +1014,7 @@ def _test_tflite2_quantized_depthwise_convolution( ): """One iteration of TFLite2 quantized depthwise convolution with given shapes and attributes""" - data_format = "channels_last" if "NHWC" else "channels_first" + data_format = "channels_last" if data_format == "NHWC" else "channels_first" data = np.random.uniform(0, 1, input_shape).astype("float32") kernel = np.random.uniform(0, 1, kernel_shape).astype("float32") @@ -1019,7 +1033,7 @@ def _test_tflite2_quantized_depthwise_convolution( # To create quantized values with dynamic range of activations, needs representative dataset def representative_data_gen(): - for i in range(1): + for _ in range(1): yield [data] tflite_model_quant = _quantize_keras_model( @@ -1039,8 +1053,8 @@ def representative_data_gen(): import tflite tflite_model = tflite.Model.GetRootAsModel(tflite_model_quant, 0) - except ImportError: - raise ImportError("The tflite package must be installed") + except ImportError as exc: + raise ImportError("The tflite package must be installed") from exc subgraph = tflite_model.Subgraphs(0) model_input = subgraph.InputsAsNumpy() @@ -1161,6 +1175,7 @@ def _test_convolution( def test_forward_convolution(): + """Convolution""" for quantized in [False, True]: for fp16_quantized in [False, True]: _test_convolution( @@ -1365,6 +1380,7 @@ def _test_transpose_conv( def test_forward_transpose_conv(): + """Transpose convolution""" for quantized in [True, False]: for fp16_quantized in [True, False]: # odd size input, padding VALID @@ -1732,7 +1748,6 @@ def test_all_resize(): ### RESIZE_NEAREST_NEIGHBOR (was added in v1.13) # According to topi resize.h # Align corners not supported for nearest neighbour - from tflite.BuiltinOperator import BuiltinOperator if "RESIZE_NEAREST_NEIGHBOR" in dir(BuiltinOperator()): _test_resize( @@ -1826,11 +1841,11 @@ def _test_shape(dtype): start = tf.placeholder(dtype=tf.int32, shape=[], name="start") limit = tf.placeholder(dtype=tf.int32, shape=[], name="limit") delta = tf.placeholder(dtype=tf.int32, shape=[], name="delta") - r = tf.range(start, limit, delta, tf.int32, name="range") - out = tf.shape(r, out_type=dtype) + tf_range = tf.range(start, limit, delta, tf.int32, name="range") + out = tf.shape(tf_range, out_type=dtype) out = tf.add(out, tf.constant([1], dtype=dtype)) compare_tflite_with_tvm( - [x for x in np.nditer(data)], + list(np.nditer(data)), ["start", "limit", "delta"], [start, limit, delta], [out], @@ -1855,11 +1870,11 @@ def _test_concatenation(data, axis): with tf.Graph().as_default(): in_data = [ - array_ops.placeholder(shape=tensor.shape, dtype=tensor.dtype, name="in_{}".format(idx)) + array_ops.placeholder(shape=tensor.shape, dtype=tensor.dtype, name=f"in_{idx}") for idx, tensor in enumerate(data) ] - out = array_ops.concat(in_data, axis=axis) - name = ["in_{}:0".format(idx) for idx in range(len(data))] + out = array_ops.concat(in_data, axis) + name = [f"in_{idx}:0" for idx in range(len(data))] compare_tflite_with_tvm(data, name, in_data, [out]) @@ -1885,7 +1900,7 @@ def test_forward_concatenation(): # -------------- -def _test_unary_elemwise(math_op, data, quantized, quant_range=[-6, 6], int_quant_dtype=tf.int8): +def _test_unary_elemwise(math_op, data, quantized, quant_range=(-6, 6), int_quant_dtype=tf.int8): """One iteration of unary elemwise""" if quantized: with tf.Graph().as_default(): @@ -1924,9 +1939,9 @@ def tf_function(self, x): return op if int_quant_dtype in (tf.int8, tf.uint8): - dtype = "int8" + _ = "int8" elif int_quant_dtype in (tf.int16, tf.uint16): - dtype = "int16" + _ = "int16" else: raise Exception(f"Unsupported dtype '{int_quant_dtype}' for unary elementwise test.") @@ -2185,6 +2200,7 @@ def _test_forward_unary_elemwise(test_op, int_quant_dtype=None, quantized=True, def test_all_unary_elemwise(): + """All unary elemwise""" _test_forward_unary_elemwise(_test_abs, int_quant_dtype=tf.int8) _test_forward_unary_elemwise(_test_abs, int_quant_dtype=tf.int16) _test_forward_unary_elemwise(_test_floor) @@ -2233,7 +2249,7 @@ def _test_elemwise( assert len(data) == 2 def __test_elemwise(in_data): - assert 2 == len(in_data) + assert len(in_data) == 2 if quantized: # set the fp32 output range with respect to the operation out_min, out_max = _test_elemwise_qnn_out_range(qnn_op) @@ -2250,14 +2266,14 @@ def __test_elemwise(in_data): tf.quantization.fake_quant_with_min_max_args( in_data[0], min=out_min, max=out_max, name="inq_0" ) - if None != in_data[0] + if in_data[0] is not None else tf.quantization.fake_quant_with_min_max_args( data[0], min=out_min, max=out_max, name="const_tensor0" ), tf.quantization.fake_quant_with_min_max_args( in_data[1], min=out_min, max=out_max, name="inq_1" ) - if None != in_data[1] + if in_data[1] is not None else tf.quantization.fake_quant_with_min_max_args( data[1], min=out_min, max=out_max, name="const_tensor1" ), @@ -2268,7 +2284,7 @@ def __test_elemwise(in_data): for x in zip( in_data, (("inq_0", (inq0_min, inq0_max)), ("inq_1", (inq1_min, inq1_max))) ) - if None != x[0] + if x[0] is not None } if math_op is math_ops.equal: @@ -2276,9 +2292,9 @@ def __test_elemwise(in_data): out = with_fused_activation_function(out, fused_activation_function) compare_tflite_with_tvm( - [x[1] for x in zip(in_data, data) if None != x[0]], + [x[1] for x in zip(in_data, data) if x[0] is not None], [x + ":0" for x in input_range.keys()], - [x[1] for x in zip(in_data, inq_data) if None != x[0]], + [x[1] for x in zip(in_data, inq_data) if x[0] is not None], [out], ) else: @@ -2287,12 +2303,11 @@ def __test_elemwise(in_data): out = tf.quantization.fake_quant_with_min_max_args( out, min=out_min, max=out_max, name="out" ) - # Note same_qnn_params uses experimental_new_converter as toco failed compare_tflite_with_tvm( - [x[1] for x in zip(in_data, data) if None != x[0]], + [x[1] for x in zip(in_data, data) if x[0] is not None], [x + ":0" for x in input_range.keys()], - [x[1] for x in zip(in_data, inq_data) if None != x[0]], + [x[1] for x in zip(in_data, inq_data) if x[0] is not None], [out], quantized=True, input_range=input_range, @@ -2301,17 +2316,17 @@ def __test_elemwise(in_data): else: out = math_op( in_data[0] - if None != in_data[0] + if in_data[0] is not None else ops.convert_to_tensor(data[0], dtype=data[0].dtype), in_data[1] - if None != in_data[1] + if in_data[1] is not None else ops.convert_to_tensor(data[1], dtype=data[1].dtype), ) out = with_fused_activation_function(out, fused_activation_function) compare_tflite_with_tvm( - [x[1] for x in zip(in_data, data) if None != x[0]], - [x[1] for x in zip(in_data, ("in_0:0", "in_1:0")) if None != x[0]], - [x for x in in_data if None != x], + [x[1] for x in zip(in_data, data) if x[0] is not None], + [x[1] for x in zip(in_data, ("in_0:0", "in_1:0")) if x[0] is not None], + [x for x in in_data if x is not None], [out], ) @@ -2565,6 +2580,7 @@ def _test_elemwise_qnn_out_range(qnn_op): def test_all_elemwise(): + """All_elewise""" _test_forward_elemwise(_test_add) _test_forward_elemwise_quantized(_test_add) _test_forward_elemwise(partial(_test_add, fused_activation_function="RELU")) @@ -2613,25 +2629,26 @@ def _test_forward_add_n(inputs): temp.append(tf.placeholder(shape=each.shape, dtype=each.dtype)) output = tf.add_n(temp) compare_tflite_with_tvm( - [each for each in inputs], + list(inputs), [each.name for each in temp], - [each for each in temp], + list(temp), [output], ) def test_forward_add_n(): + """Add n""" if package_version.parse(tf.VERSION) >= package_version.parse("1.14.0"): x = np.random.randint(1, 100, size=(3, 3, 3), dtype=np.int32) y = np.random.randint(1, 100, size=(3, 3, 3), dtype=np.int32) - z = np.random.randint(1, 100, size=(3, 3, 3), dtype=np.int32) - m, n, o = x.astype(np.float32), y.astype(np.float32), z.astype(np.float32) + z_1 = np.random.randint(1, 100, size=(3, 3, 3), dtype=np.int32) + x_1, x_2, z_2 = x.astype(np.float32), y.astype(np.float32), z_1.astype(np.float32) in0 = x in1 = [x, y] - in2 = (x, y, z) - in3 = m - in4 = [m, n] - in5 = (m, n, o) + in2 = (x, y, z_1) + in3 = x_1 + in4 = [x_1, x_2] + in5 = (x_1, x_2, z_2) _test_forward_add_n(in0) _test_forward_add_n(in1) _test_forward_add_n(in2) @@ -2652,7 +2669,7 @@ def _test_logical_binary(logical_bin_op, data): array_ops.placeholder(shape=data[0].shape, dtype="bool", name="in_0"), array_ops.placeholder(shape=data[1].shape, dtype="bool", name="in_1"), ] - if logical_bin_op == math_ops.logical_not: + if logical_bin_op is math_ops.logical_not: out = math_ops.logical_or(in_data[0], in_data[1], name="out1") out = logical_bin_op(out, name="out") else: @@ -2912,6 +2929,7 @@ def _test_arg_min_max(math_op, data, axis, quantized=False): def test_forward_arg_min_max(): + """Arg min max""" # test quantized for data in [np.array(np.random.uniform(-100, 100, (3, 4)), dtype=np.uint8)]: # There is no quantized version of ArgMin @@ -2930,8 +2948,9 @@ def test_forward_arg_min_max(): def test_forward_select(): + """Select""" with tf.Graph().as_default(): - with tf.Session() as sess: + with tf.Session() as _: input1 = tf.placeholder(tf.int32, shape=[1, 4, 4, 3], name="input1") input2 = tf.placeholder(tf.int32, shape=[1, 4, 4, 3], name="input2") mask = input1 > input2 @@ -2946,18 +2965,20 @@ def test_forward_select(): @pytest.mark.parametrize("quant_bits", [2, 4, 8, 16]) @pytest.mark.parametrize( - "value, min, max", [[-10.11, -6, 6], [-3.55, -6, 6], [0, -6, 6], [3.55, -6, 6], [10.11, -6, 6]] + "value, min_value, max_value", + [[-10.11, -6, 6], [-3.55, -6, 6], [0, -6, 6], [3.55, -6, 6], [10.11, -6, 6]], ) -def test_forward_fake_quant(value, min, max, quant_bits): +def test_forward_fake_quant(value, min_value, max_value, quant_bits): + """Fake quant""" with tf.Graph().as_default(): - with tf.Session() as sess: - input = tf.placeholder(tf.float32, shape=[1], name="input") + with tf.Session() as _: + input_placeholder = tf.placeholder(tf.float32, shape=[1], name="input") out = tf.quantization.fake_quant_with_min_max_args( - input, min=min, max=max, num_bits=quant_bits, name=None + input_placeholder, min=min_value, max=max_value, num_bits=quant_bits, name=None ) in_data = np.float32(value) - compare_tflite_with_tvm([in_data], ["input:0"], [input], [out]) + compare_tflite_with_tvm([in_data], ["input:0"], [input_placeholder], [out]) # Squeeze @@ -3007,7 +3028,7 @@ def _test_quantize_dequantize(data): # To create quantized values with dynamic range of activations, needs representative dataset def representative_data_gen(): - for i in range(1): + for _ in range(1): yield [data] tflite_model_quant = _quantize_keras_model(keras_model, representative_data_gen, True, True) @@ -3034,7 +3055,7 @@ def _test_quantize_dequantize_const(data): # To create quantized values with dynamic range of activations, needs representative dataset def representative_data_gen(): - for i in range(1): + for _ in range(1): yield [data] tflite_model_quant = _quantize_keras_model(keras_model, representative_data_gen, True, True) @@ -3337,8 +3358,8 @@ def _test_expand_dims(input_shape, input_type, axis, quantized=False): if quantized: # ignoring input_type as quantized requires uint8 - input = np.random.uniform(0, 256, input_shape).astype("uint8") - in_input = tf.placeholder(dtype="float32", shape=input.shape, name="input") + input_array = np.random.uniform(0, 256, input_shape).astype("uint8") + in_input = tf.placeholder(dtype="float32", shape=input_array.shape, name="input") input_range = {"q_input": (-100, 100)} inq_input = tf.quantization.fake_quant_with_min_max_args( @@ -3349,15 +3370,22 @@ def _test_expand_dims(input_shape, input_type, axis, quantized=False): out = tf.quantization.fake_quant_with_min_max_args(out, min=-100, max=100, name="out") compare_tflite_with_tvm( - [input], ["q_input"], [inq_input], [out], quantized=True, input_range=input_range + [input_array], + ["q_input"], + [inq_input], + [out], + quantized=True, + input_range=input_range, ) else: - input = np.random.uniform(-100, 100, input_shape).astype(input_type) - in_input = tf.placeholder(dtype=input.dtype, shape=input.shape, name="input") + input_array = np.random.uniform(-100, 100, input_shape).astype(input_type) + in_input = tf.placeholder( + dtype=input_array.dtype, shape=input_array.shape, name="input" + ) out = array_ops.expand_dims(in_input, axis=axis) - compare_tflite_with_tvm([input], ["input"], [in_input], [out]) + compare_tflite_with_tvm([input_array], ["input"], [in_input], [out]) def test_forward_expand_dims(): @@ -3426,17 +3454,17 @@ def _test_pack(data, is_var, axis, quantized=False): ] inq_data = [ tf.quantization.fake_quant_with_min_max_args( - i_data, min=-100, max=100, name="inq_{}".format(idx) + i_data, min=-100, max=100, name=f"inq_{idx}" ) for idx, i_data in enumerate(in_data) ] input_range = {} for i in range(len(data)): - input_range["inq_{}".format(i)] = (-100, 100) + input_range[f"inq_{i}"] = (-100, 100) out = array_ops.pack(inq_data, axis=axis) out = tf.quantization.fake_quant_with_min_max_args(out, min=-100, max=100, name="out") - name = ["inq_{}:0".format(idx) for idx in range(len(data))] + name = [f"inq_{idx}:0" for idx in range(len(data))] compare_tflite_with_tvm( data, name, inq_data, [out], quantized=True, input_range=input_range ) @@ -3788,7 +3816,8 @@ def _test_relu_n1_to_1(data, quantized=False): in_data, min=-3, max=3, name="inq_0" ) input_range = {"inq_0": (-3, 3)} - # There is no such tf operation. The specific pattern will be replaced into RELU_N1_TO_1 by tflite + # There is no such tf operation. + # The specific pattern will be replaced into RELU_N1_TO_1 by tflite out = math_ops.maximum(-1.0, math_ops.minimum(inq_data, 1.0)) out = tf.quantization.fake_quant_with_min_max_args(out, min=-1, max=1, name="out") compare_tflite_with_tvm( @@ -3949,7 +3978,7 @@ def _test_sparse_to_dense(sparse_indices, sparse_values, default_value, output_s output_shape, shape=output_shape.shape, dtype=str(output_shape.dtype) ) - if default_value == None: + if default_value is None: output = tf.sparse_to_dense(indices, oshape, values) compare_tflite_with_tvm( [sparse_indices, sparse_values], @@ -3958,12 +3987,14 @@ def _test_sparse_to_dense(sparse_indices, sparse_values, default_value, output_s [output], ) else: - dv = tf.placeholder(shape=(), dtype=str(default_value.dtype), name="default_value") - output = tf.sparse_to_dense(indices, oshape, values, dv) + dv_placeholder = tf.placeholder( + shape=(), dtype=str(default_value.dtype), name="default_value" + ) + output = tf.sparse_to_dense(indices, oshape, values, dv_placeholder) compare_tflite_with_tvm( [sparse_indices, sparse_values, default_value], ["indices", "values", "default_value"], - [indices, values, dv], + [indices, values, dv_placeholder], [output], ) @@ -4143,13 +4174,13 @@ def test_forward_fully_connected(): def _test_reverse_v2(input_shape, axis, dtype): """One iteration of REVERSE_V2""" with tf.Graph().as_default(): - input = np.random.randint(0, 100, size=input_shape).astype(dtype) - in_input = tf.placeholder(dtype=input.dtype, shape=input.shape, name="input") + input_array = np.random.randint(0, 100, size=input_shape).astype(dtype) + in_input = tf.placeholder(dtype=input_array.dtype, shape=input_array.shape, name="input") in_axis = ops.convert_to_tensor(axis, dtype=axis.dtype) out = array_ops.reverse(in_input, in_axis) - compare_tflite_with_tvm([input], ["input"], [in_input], [out]) + compare_tflite_with_tvm([input_array], ["input"], [in_input], [out]) def test_forward_reverse_v2(): @@ -4172,8 +4203,8 @@ def _test_matrix_set_diag(input_shape, input_type, quantized=False): if quantized: # ignoring input_type as quantized requires uint8 - input = np.random.uniform(0, 256, input_shape).astype("uint8") - in_input = tf.placeholder(dtype="float32", shape=input.shape, name="input") + input_array = np.random.uniform(0, 256, input_shape).astype("uint8") + in_input = tf.placeholder(dtype="float32", shape=input_array.shape, name="input") inq_input = tf.quantization.fake_quant_with_min_max_args( in_input, min=-100, max=100, name="q_input" ) @@ -4190,7 +4221,7 @@ def _test_matrix_set_diag(input_shape, input_type, quantized=False): out = tf.quantization.fake_quant_with_min_max_args(out, min=-100, max=100, name="out") compare_tflite_with_tvm( - [input, diagonal], + [input_array, diagonal], ["q_input", "q_diagonal"], [inq_input, inq_diagonal], [out], @@ -4198,10 +4229,12 @@ def _test_matrix_set_diag(input_shape, input_type, quantized=False): input_range=input_range, ) else: - input = np.random.uniform(0, 100, input_shape).astype(input_type) + input_array = np.random.uniform(0, 100, input_shape).astype(input_type) diagonal = np.random.uniform(0, 100, diagonal_shape).astype(input_type) - in_input = tf.placeholder(dtype=input.dtype, shape=input.shape, name="input") + in_input = tf.placeholder( + dtype=input_array.dtype, shape=input_array.shape, name="input" + ) in_diagonal = tf.placeholder( dtype=diagonal.dtype, shape=diagonal.shape, name="diagonal" ) @@ -4209,7 +4242,7 @@ def _test_matrix_set_diag(input_shape, input_type, quantized=False): out = array_ops.matrix_set_diag(in_input, in_diagonal) compare_tflite_with_tvm( - [input, diagonal], ["input", "diagonal"], [in_input, in_diagonal], [out] + [input_array, diagonal], ["input", "diagonal"], [in_input, in_diagonal], [out] ) @@ -4257,6 +4290,7 @@ def test_forward_matrix_diag(): def test_detection_postprocess(): + """Detection PostProcess""" tf_model_file = tf_testing.get_workload_official( "http://download.tensorflow.org/models/object_detection/" "ssd_mobilenet_v2_quantized_300x300_coco_2019_01_03.tar.gz", @@ -4292,10 +4326,10 @@ def test_detection_postprocess(): # Check all output shapes are equal assert all( - [ + list( tvm_tensor.shape == tflite_tensor.shape for (tvm_tensor, tflite_tensor) in zip(tvm_output, tflite_output) - ] + ) ) # Check valid count is the same @@ -4338,7 +4372,7 @@ class DummyOperatorConverter(relay.frontend.tflite.OperatorConverter): """Operator Converter for converting TFLite ops to relay ops""" def __init__(self, model, subgraph, exp_tab): - super(DummyOperatorConverter, self).__init__(model, subgraph, exp_tab) + super().__init__(model, subgraph, exp_tab) self.allow_custom_ops = True convert_map_overwrite = {"SUB": self.convert_sub_dummy} @@ -4373,11 +4407,11 @@ def convert_sub_dummy(self, op): ] out = math_ops.subtract(in_data[0], in_data[1]) in_name = [x[1] for x in zip(in_data, ("in_0:0", "in_1:0"))] - input_tensors = [x for x in in_data] + input_tensors = in_data output_tensors = [out] in_node = [0] * len(in_name) - for i in range(len(in_name)): - in_node[i] = in_name[i].split(":")[0] if ":" in in_name[i] else in_name[i] + for i, _ in enumerate(in_name): + in_node[i] = in_name[i].split(":")[0] with tf.Session() as sess: converter = tf.lite.TFLiteConverter.from_session(sess, input_tensors, output_tensors) @@ -4508,7 +4542,8 @@ def test_forward_inception_v3_net(): """Test the Inception V3 TF Lite model.""" # InceptionV3 tflite_model_file = tf_testing.get_workload_official( - "https://storage.googleapis.com/download.tensorflow.org/models/tflite/model_zoo/upload_20180427/inception_v3_2018_04_27.tgz", + "https://storage.googleapis.com/download.tensorflow.org/models/tflite/model_zoo/" + "upload_20180427/inception_v3_2018_04_27.tgz", "inception_v3.tflite", ) with open(tflite_model_file, "rb") as f: @@ -4525,7 +4560,9 @@ def test_forward_inception_v4_net(): """Test the Inception V4 TF Lite model.""" # InceptionV4 tflite_model_file = tf_testing.get_workload_official( - "https://storage.googleapis.com/download.tensorflow.org/models/tflite/model_zoo/upload_20180427/inception_v4_2018_04_27.tgz", + "https://storage.googleapis.com/download.tensorflow.org/models/" + "tflite/model_zoo/upload_20180427/" + "inception_v4_2018_04_27.tgz", "inception_v4.tflite", ) with open(tflite_model_file, "rb") as f: @@ -4542,7 +4579,9 @@ def test_forward_inception_v4_net_batched(): """Test the Inception V4 TF Lite model.""" # InceptionV4 tflite_model_file = tf_testing.get_workload_official( - "https://storage.googleapis.com/download.tensorflow.org/models/tflite/model_zoo/upload_20180427/inception_v4_2018_04_27.tgz", + "https://storage.googleapis.com/download.tensorflow.org/models/" + "tflite/model_zoo/upload_20180427/" + "inception_v4_2018_04_27.tgz", "inception_v4.tflite", ) with open(tflite_model_file, "rb") as f: @@ -4559,7 +4598,8 @@ def test_forward_qnn_inception_v1_net(): """Test the Quantized TFLite Inception model.""" # InceptionV1 tflite_model_file = tf_testing.get_workload_official( - "https://storage.googleapis.com/download.tensorflow.org/models/inception_v1_224_quant_20181026.tgz", + "https://storage.googleapis.com/download.tensorflow.org/models/" + "inception_v1_224_quant_20181026.tgz", "inception_v1_224_quant.tflite", ) with open(tflite_model_file, "rb") as f: @@ -4583,7 +4623,8 @@ def test_forward_qnn_mobilenet_v1_net(): """Test the Quantized TFLite Mobilenet V1 model.""" # MobilenetV1 tflite_model_file = tf_testing.get_workload_official( - "https://storage.googleapis.com/download.tensorflow.org/models/mobilenet_v1_2018_08_02/mobilenet_v1_1.0_224_quant.tgz", + "https://storage.googleapis.com/download.tensorflow.org/models/mobilenet_v1_2018_08_02/" + "mobilenet_v1_1.0_224_quant.tgz", "mobilenet_v1_1.0_224_quant.tflite", ) with open(tflite_model_file, "rb") as f: @@ -4607,7 +4648,8 @@ def test_forward_qnn_mobilenet_v2_net(): """Test the Quantized TFLite Mobilenet V2 model.""" # MobilenetV2 tflite_model_file = tf_testing.get_workload_official( - "https://storage.googleapis.com/download.tensorflow.org/models/tflite_11_05_08/mobilenet_v2_1.0_224_quant.tgz", + "https://storage.googleapis.com/download.tensorflow.org/models/tflite_11_05_08/" + "mobilenet_v2_1.0_224_quant.tgz", "mobilenet_v2_1.0_224_quant.tflite", ) with open(tflite_model_file, "rb") as f: @@ -4665,7 +4707,8 @@ def test_forward_tflite2_qnn_resnet50(): """Test the Quantized TFLite version 2.1.0 Resnet50 model.""" if package_version.parse(tf.VERSION) >= package_version.parse("2.1.0"): tflite_model_file = download_testdata( - "https://raw.githubusercontent.com/dmlc/web-data/main/tensorflow/models/Quantized/resnet_50_quantized.tflite", + "https://raw.githubusercontent.com/dmlc/web-data/main/tensorflow/models/Quantized/" + "resnet_50_quantized.tflite", "resnet_50_quantized.tflite", ) with open(tflite_model_file, "rb") as f: @@ -4686,7 +4729,8 @@ def test_forward_tflite2_qnn_inception_v1(): """Test the Quantized TFLite version 2.1.0 Inception V1 model.""" if package_version.parse(tf.VERSION) >= package_version.parse("2.1.0"): tflite_model_file = download_testdata( - "https://raw.githubusercontent.com/dmlc/web-data/main/tensorflow/models/Quantized/inception_v1_quantized.tflite", + "https://raw.githubusercontent.com/dmlc/web-data/main/tensorflow/models/Quantized/" + "inception_v1_quantized.tflite", "inception_v1_quantized.tflite", ) with open(tflite_model_file, "rb") as f: @@ -4707,7 +4751,8 @@ def test_forward_tflite2_qnn_mobilenet_v2(): """Test the Quantized TFLite version 2.1.0 Mobilenet V2 model.""" if package_version.parse(tf.VERSION) >= package_version.parse("2.1.0"): tflite_model_file = download_testdata( - "https://raw.githubusercontent.com/dmlc/web-data/main/tensorflow/models/Quantized/mobilenet_v2_quantized.tflite", + "https://raw.githubusercontent.com/dmlc/web-data/main/tensorflow/models/Quantized/" + "mobilenet_v2_quantized.tflite", "mobilenet_v2_quantized.tflite", ) with open(tflite_model_file, "rb") as f: @@ -4728,7 +4773,8 @@ def test_forward_tflite_float16(): """Test float16 quantized model""" # MobilenetV2 tflite_model_file = tf_testing.get_workload_official( - "https://storage.googleapis.com/download.tensorflow.org/models/mobilenet_v1_2018_02_22/mobilenet_v1_0.25_128.tgz", + "https://storage.googleapis.com/download.tensorflow.org/models/mobilenet_v1_2018_02_22/" + "mobilenet_v1_0.25_128.tgz", "mobilenet_v1_0.25_128_frozen.pb", ) @@ -4757,7 +4803,8 @@ def test_forward_mobilenet_int16(): """Test int16 quantized model""" # MobilenetV2 model_file = tf_testing.get_workload_official( - "https://storage.googleapis.com/download.tensorflow.org/models/mobilenet_v1_2018_02_22/mobilenet_v1_0.25_128.tgz", + "https://storage.googleapis.com/download.tensorflow.org/models/mobilenet_v1_2018_02_22/" + "mobilenet_v1_0.25_128.tgz", "mobilenet_v1_0.25_128_frozen.pb", ) @@ -4801,7 +4848,8 @@ def test_forward_unidirectional_sequence_lstm(): """Test the UnidirectionalSequenceLSTM TFLite""" if package_version.parse(tf.VERSION) >= package_version.parse("2.1.0"): tflite_model_file = download_testdata( - "https://github.com/SebastianBoblestETAS/nn_models/blob/ce49c5de64889493161ca4194a20e0fd5eb707e6/lstm_1_in_3_out_2_ts_4.tflite?raw=true", + "https://github.com/SebastianBoblestETAS/nn_models/blob/" + "ce49c5de64889493161ca4194a20e0fd5eb707e6/lstm_1_in_3_out_2_ts_4.tflite?raw=true", "lstm_1_in_3_out_2_ts_4.tflite", ) with open(tflite_model_file, "rb") as f: @@ -4838,7 +4886,8 @@ def test_forward_qnn_coco_ssd_mobilenet_v1(): ) tflite_model_file = tf_testing.get_workload_official( - "https://storage.googleapis.com/download.tensorflow.org/models/tflite/coco_ssd_mobilenet_v1_1.0_quant_2018_06_29.zip", + "https://storage.googleapis.com/download.tensorflow.org/models/tflite/" + "coco_ssd_mobilenet_v1_1.0_quant_2018_06_29.zip", "detect.tflite", ) @@ -4853,10 +4902,10 @@ def test_forward_qnn_coco_ssd_mobilenet_v1(): # Check all output shapes are equal assert all( - [ + list( tvm_tensor.shape == tflite_tensor.shape for (tvm_tensor, tflite_tensor) in zip(tvm_output, tflite_output) - ] + ) ) # Check valid count is the same @@ -4904,7 +4953,8 @@ def test_forward_qnn_coco_ssd_mobilenet_v1(): def test_forward_coco_ssd_mobilenet_v1(): """Test the FP32 Coco SSD Mobilenet V1 TF Lite model.""" tflite_model_file = tf_testing.get_workload_official( - "https://raw.githubusercontent.com/dmlc/web-data/main/tensorflow/models/object_detection/ssd_mobilenet_v1_coco_2018_01_28.tgz", + "https://raw.githubusercontent.com/dmlc/web-data/main/tensorflow/models/object_detection/" + "ssd_mobilenet_v1_coco_2018_01_28.tgz", "ssd_mobilenet_v1_coco_2018_01_28.tflite", ) @@ -4920,10 +4970,10 @@ def test_forward_coco_ssd_mobilenet_v1(): # Check all output shapes are equal assert all( - [ + list( tvm_tensor.shape == tflite_tensor.shape for (tvm_tensor, tflite_tensor) in zip(tvm_output, tflite_output) - ] + ) ) # Check valid count is the same @@ -4991,7 +5041,7 @@ def test_prevent_tensorflow_dynamic_range(): converter.optimizations = [tf.lite.Optimize.DEFAULT] tflite_model = converter.convert() with pytest.raises(tvm.error.OpNotImplemented): - tvm_output = run_tvm_graph(tflite_model, data_array, data_in.name.replace(":0", "")) + _ = run_tvm_graph(tflite_model, data_array, data_in.name.replace(":0", "")) def _test_nms_v5(