From 52c3ba1b93c57092fb18b5a423aaa792e33b5911 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Thu, 7 Jul 2022 07:37:04 +0000 Subject: [PATCH 001/110] [CI] Apply linting rules to tf tests --- .../frontend/tensorflow/test_forward.py | 118 ++++++++++++------ 1 file changed, 77 insertions(+), 41 deletions(-) diff --git a/tests/python/frontend/tensorflow/test_forward.py b/tests/python/frontend/tensorflow/test_forward.py index 70a137479fe2..dcb1d3a0fd13 100644 --- a/tests/python/frontend/tensorflow/test_forward.py +++ b/tests/python/frontend/tensorflow/test_forward.py @@ -14,17 +14,28 @@ # 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, unused-variable, redefined-builtin """ Tensorflow testcases ==================== 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 numpy as np import pytest +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 + +import tvm +import tvm.relay.testing.tf as tf_testing +import tvm.testing + try: import tensorflow.compat.v1 as tf @@ -35,32 +46,9 @@ # 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() - -from tensorflow.python.framework import constant_op -from tensorflow.python.framework import graph_util -from tensorflow.python.ops import nn_ops -from tensorflow.python.ops import nn -from tensorflow.python.ops import array_ops -from tensorflow.python.ops import math_ops -from tensorflow.python.ops import variable_scope -from tensorflow.python.ops import variables -from tensorflow.python.ops import init_ops -from tensorflow.python.framework import function -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 +gpu_sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) +gpu_sess.close() -import tvm.testing ####################################################################### # Generic run functions for TVM & tensorflow @@ -87,6 +75,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): @@ -281,9 +270,9 @@ 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)): + for i, _ in enumerate(tf_output): if not isinstance(tf_output[i], np.ndarray): - assert len(tvm_output[i].shape) == 0 + assert len(tvm_output[i].shape) == 0 # pylint: disable=len-as-condition tvm.testing.assert_allclose(tf_output[i], tvm_output[i], atol=1e-5, rtol=1e-5) sess.close() @@ -294,7 +283,7 @@ def is_gpu_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 len(gpu_list) > 0: # pylint: disable=len-as-condition print("Tensorflow GPU:", gpu_list) return True else: @@ -498,7 +487,7 @@ def _test_convolution( add_shapes_to_graph_def=True, ): """One iteration of convolution with given shapes and attributes""" - + # pylint: disable=dangerous-default-value total_size_1 = np.prod(tensor_in_sizes) total_size_2 = np.prod(filter_in_sizes) # Initializes the input tensor with array containing incrementing @@ -569,6 +558,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") @@ -945,7 +935,7 @@ def _test_convolution3d( add_shapes_to_graph_def=True, ): """One iteration of 3D convolution with given shapes and attributes""" - + # pylint: disable=dangerous-default-value total_size_1 = np.prod(tensor_in_sizes) total_size_2 = np.prod(filter_in_sizes) # Initializes the input tensor with array containing incrementing @@ -984,6 +974,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" @@ -1070,6 +1061,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], @@ -1181,6 +1173,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") @@ -1241,6 +1234,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]]) @@ -1279,6 +1273,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]]) @@ -1355,6 +1350,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]) @@ -1457,6 +1453,8 @@ 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] @@ -1480,6 +1478,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] @@ -1517,6 +1517,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] @@ -1534,6 +1536,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] @@ -1562,6 +1566,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] @@ -1584,6 +1590,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") @@ -1607,6 +1614,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") @@ -1628,6 +1637,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") @@ -1796,7 +1807,7 @@ def test_read_variable_op(target, dev): out_names=out_name, num_output=len(out_name), ) - for i in range(len(tf_output)): + for i, _ in enumerate(tf_output): tvm.testing.assert_allclose(tf_output[i], tvm_output[i], atol=1e-4, rtol=1e-5) sess.close() @@ -1909,6 +1920,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 @@ -2423,7 +2435,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" @@ -2440,6 +2452,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), @@ -2586,7 +2599,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") @@ -2602,7 +2615,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") @@ -2896,9 +2909,9 @@ 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") @@ -3048,6 +3061,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") @@ -3070,6 +3084,7 @@ 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") @@ -3095,7 +3110,7 @@ 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)): + for i, _ in enumerate(tf_output): tvm.testing.assert_allclose(tf_output[i], tvm_output[i], atol=1e-5, rtol=1e-5) @@ -3664,7 +3679,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) @@ -4015,7 +4030,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 @@ -4444,6 +4459,8 @@ def test_forward_pow_exp(): 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) @@ -4621,6 +4638,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(): @@ -4639,6 +4658,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) @@ -4661,6 +4682,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": @@ -4676,6 +4699,7 @@ def _check_op(tf_op, ishape, axis, keepdims, dtype="float32"): compare_tf_with_tvm([np_data], ["in_data:0"], reduce_op.name) def _test_math_op(op, dtypes=["int32", "float32"]): + # pylint: disable=dangerous-default-value for dtype in dtypes: _check_op(op, (3, 10), axis=(-1), keepdims=False, dtype=dtype) _check_op(op, (8, 16, 32), axis=(-1), keepdims=False, dtype=dtype) @@ -4700,6 +4724,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": @@ -4717,6 +4743,7 @@ def _check_op(tf_op, ishape, axis, keepdims, range_axis=False, dtype="float32"): compare_tf_with_tvm([np_data], ["in_data:0"], reduce_op.name) def _test_raw_reduce_op(op, dtypes=["int32", "float32"]): + # pylint: disable=dangerous-default-value for dtype in dtypes: _check_op(op, (3, 10), axis=(-1), keepdims=False, dtype=dtype) _check_op(op, (8, 16, 32), axis=(-1), keepdims=False, dtype=dtype) @@ -4820,6 +4847,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") @@ -4874,6 +4902,7 @@ def _test_forward_add_n(inputs): 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) @@ -4932,6 +4961,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]) @@ -4984,6 +5014,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") @@ -5049,6 +5080,7 @@ def _test_identityn(data_np_list): ], ) def test_forward_identityn(data_np_list): + """Identityn""" _test_identityn(data_np_list) @@ -5060,7 +5092,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) @@ -5433,6 +5465,7 @@ def resourceVariablesTest(x, y): def test_forward_spop(): + """Spop""" _test_spop_stateful() _test_spop_device_assignment() # tensorflow version upgrade support @@ -5465,6 +5498,7 @@ 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(): @@ -5497,6 +5531,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 @@ -5588,7 +5623,7 @@ def lstm_cell(): ) # Compare result - for i in range(len(tf_output)): + for i, _ in enumerate(tf_output): tvm.testing.assert_allclose(tf_output[i], tvm_output[i], atol=1e-5, rtol=1e-5) @@ -5666,6 +5701,7 @@ def test_forward_unique_with_counts(): def test_moments(): + """NN.moments""" g = tf.Graph() shape = [4, 176, 8, 8] dtype = "float32" From e17708edd2bab636614817c07d24d039917951d3 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Thu, 7 Jul 2022 06:46:24 +0000 Subject: [PATCH 002/110] [CI] Apply linting rules to tflite tests --- tests/python/frontend/tflite/test_forward.py | 171 +++++++++++-------- 1 file changed, 96 insertions(+), 75 deletions(-) diff --git a/tests/python/frontend/tflite/test_forward.py b/tests/python/frontend/tflite/test_forward.py index 42848783967c..4465f9638324 100644 --- a/tests/python/frontend/tflite/test_forward.py +++ b/tests/python/frontend/tflite/test_forward.py @@ -14,7 +14,8 @@ # 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, unused-variable, singleton-comparison +# pylint: disable=redefined-builtin, no-else-return, inconsistent-return-statements, import-outside-toplevel """ TFLite testcases ================ @@ -22,13 +23,21 @@ """ from __future__ import print_function from functools import partial +from distutils.version import LooseVersion + +import os +import tempfile import pytest import numpy as np -import tvm -import tempfile -from tvm import te + +from PIL import Image +from packaging import version as package_version +from tvm.contrib.download import download_testdata from tvm import relay +import tvm +import tvm.relay.testing.tf as tf_testing + try: import tensorflow.compat.v1 as tf @@ -48,19 +57,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 +88,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) @@ -114,6 +117,7 @@ def get_real_image_object_detection(im_height, im_width): def vmobj_to_list(o): + """Converts TVM objects returned by VM execution to Python List.""" if isinstance(o, tvm.nd.NDArray): return [o.numpy().tolist()] elif isinstance(o, tvm.runtime.container.ADT): @@ -259,13 +263,13 @@ 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)): + for i, _ in enumerate(input_details): interpreter.resize_tensor_input(input_details[i]["index"], input_data[i].shape) interpreter.allocate_tensors() # set input assert len(input_data) == len(input_details) - for i in range(len(input_details)): + for i, _ in enumerate(input_details): interpreter.set_tensor(input_details[i]["index"], input_data[i]) # Run @@ -273,7 +277,7 @@ def run_tflite_graph(tflite_model_buf, input_data): # get output tflite_output = list() - for i in range(len(output_details)): + for i, _ in enumerate(output_details): tflite_output.append(interpreter.get_tensor(output_details[i]["index"])) return tflite_output @@ -298,7 +302,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: @@ -354,21 +358,23 @@ 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) 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 ) 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": @@ -907,7 +913,7 @@ 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") @@ -957,6 +963,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 +1007,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") @@ -1161,6 +1168,7 @@ def _test_convolution( def test_forward_convolution(): + """Convolution""" for quantized in [False, True]: for fp16_quantized in [False, True]: _test_convolution( @@ -1365,6 +1373,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 @@ -1830,7 +1839,7 @@ def _test_shape(dtype): out = tf.shape(r, out_type=dtype) out = tf.add(out, tf.constant([1], dtype=dtype)) compare_tflite_with_tvm( - [x for x in np.nditer(data)], + [x for x in np.nditer(data)], # pylint: disable=unnecessary-comprehension ["start", "limit", "delta"], [start, limit, delta], [out], @@ -1887,6 +1896,7 @@ def test_forward_concatenation(): def _test_unary_elemwise(math_op, data, quantized, quant_range=[-6, 6], int_quant_dtype=tf.int8): """One iteration of unary elemwise""" + # pylint: disable= dangerous-default-value if quantized: with tf.Graph().as_default(): quant_min, quant_max = quant_range @@ -2185,6 +2195,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 +2244,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 +2261,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] != 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] != None else tf.quantization.fake_quant_with_min_max_args( data[1], min=out_min, max=out_max, name="const_tensor1" ), @@ -2268,50 +2279,37 @@ 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] != None } if math_op is math_ops.equal: out = math_op(inq_data[0], inq_data[1]) 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 + ":0" for x in input_range.keys()], - [x[1] for x in zip(in_data, inq_data) if None != x[0]], - [out], - ) - else: - out = math_op(inq_data[0], inq_data[1]) - out = with_fused_activation_function(out, fused_activation_function) - 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 + ":0" for x in input_range.keys()], - [x[1] for x in zip(in_data, inq_data) if None != x[0]], - [out], - quantized=True, - input_range=input_range, - experimental_new_converter=same_qnn_params, - ) + # 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 x[0] != None], + [x + ":0" for x in input_range.keys()], + [x[1] for x in zip(in_data, inq_data) if x[0] != None], + [out], + quantized=True, + input_range=input_range, + experimental_new_converter=same_qnn_params, + ) else: out = math_op( in_data[0] - if None != in_data[0] + if in_data[0] != None else ops.convert_to_tensor(data[0], dtype=data[0].dtype), in_data[1] - if None != in_data[1] + if in_data[1] != 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] != None], + [x[1] for x in zip(in_data, ("in_0:0", "in_1:0")) if x[0] != None], + [x for x in in_data if x != None], [out], ) @@ -2565,6 +2563,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,14 +2612,15 @@ 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], + [each for each in inputs], # pylint: disable=unnecessary-comprehension [each.name for each in temp], - [each for each in temp], + [each for each in temp], # pylint: disable=unnecessary-comprehension [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) @@ -2652,7 +2652,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 +2912,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,6 +2931,7 @@ def test_forward_arg_min_max(): def test_forward_select(): + """Select""" with tf.Graph().as_default(): with tf.Session() as sess: input1 = tf.placeholder(tf.int32, shape=[1, 4, 4, 3], name="input1") @@ -2949,6 +2951,7 @@ def test_forward_select(): "value, min, max", [[-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): + """Fake quant""" with tf.Graph().as_default(): with tf.Session() as sess: input = tf.placeholder(tf.float32, shape=[1], name="input") @@ -3788,7 +3791,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( @@ -4257,6 +4261,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", @@ -4373,10 +4378,10 @@ 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)): + 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: @@ -4508,7 +4513,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 +4531,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 +4550,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 +4569,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 +4594,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 +4619,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 +4678,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 +4700,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 +4722,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 +4744,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 +4774,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 +4819,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 +4857,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", ) @@ -4904,7 +4924,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", ) From 8aeb47b4a7496fd1c78ba053e5d208fff6baf6db Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Fri, 17 Jun 2022 11:46:58 +0000 Subject: [PATCH 003/110] [CI] Apply linting rules to coreml tests --- tests/python/frontend/caffe/test_forward.py | 4 ++++ tests/python/frontend/coreml/model_zoo/__init__.py | 9 ++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/tests/python/frontend/caffe/test_forward.py b/tests/python/frontend/caffe/test_forward.py index 0e94148b3d06..f48965b9dbb7 100644 --- a/tests/python/frontend/caffe/test_forward.py +++ b/tests/python/frontend/caffe/test_forward.py @@ -221,7 +221,11 @@ def _run_tvm(data, proto_file, blob_file): def _compare_caffe_tvm(caffe_out, tvm_out, is_network=False): +<<<<<<< HEAD for i in range(len(caffe_out)): +======= + for i, _ in enumerate(caffe_out): +>>>>>>> 2b7ec741f... fix error 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) diff --git a/tests/python/frontend/coreml/model_zoo/__init__.py b/tests/python/frontend/coreml/model_zoo/__init__.py index bf270b8dff75..9645d5ca891e 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,11 +36,14 @@ def get_resnet50(): def get_cat_image(): - url = "https://gist.githubusercontent.com/zhreshold/bcda4716699ac97ea44f791c24310193/raw/fa7ef0e9c9a5daea686d6473a62aacd1a5885849/cat.png" + 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)) # CoreML's standard model image format is BGR img_bgr = np.array(img)[:, :, ::-1] img = np.transpose(img_bgr, (2, 0, 1))[np.newaxis, :] - return np.asarray(img) + return np.asarray(img) \ No newline at end of file From 0d37d8f1e0611035549c08ad028e99ebc166f051 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Tue, 14 Jun 2022 13:24:21 +0000 Subject: [PATCH 004/110] [CI] Apply linting rules to caffe tests --- tests/python/frontend/caffe/test_forward.py | 25 +++++++++------------ 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/tests/python/frontend/caffe/test_forward.py b/tests/python/frontend/caffe/test_forward.py index f48965b9dbb7..749609b6e5a2 100644 --- a/tests/python/frontend/caffe/test_forward.py +++ b/tests/python/frontend/caffe/test_forward.py @@ -21,14 +21,9 @@ 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): @@ -221,11 +221,7 @@ def _run_tvm(data, proto_file, blob_file): def _compare_caffe_tvm(caffe_out, tvm_out, is_network=False): -<<<<<<< HEAD - for i in range(len(caffe_out)): -======= for i, _ in enumerate(caffe_out): ->>>>>>> 2b7ec741f... fix error 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) @@ -965,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) @@ -1197,4 +1194,4 @@ def test_forward_Inceptionv1(): test_forward_Mobilenetv2() test_forward_Alexnet() test_forward_Resnet50() - test_forward_Inceptionv1() + test_forward_Inceptionv1() \ No newline at end of file From b0b8755660d36b02c4531d9c95fe86e0390c51b0 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Thu, 7 Jul 2022 08:53:44 +0000 Subject: [PATCH 005/110] [CI] Apply linting rules to caffe2 tests --- tests/python/frontend/caffe2/test_forward.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/tests/python/frontend/caffe2/test_forward.py b/tests/python/frontend/caffe2/test_forward.py index 3d7ad230dec0..09ee8a4a3e67 100644 --- a/tests/python/frontend/caffe2/test_forward.py +++ b/tests/python/frontend/caffe2/test_forward.py @@ -14,15 +14,22 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. +# pylint: disable=no-else-return +""" +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 +110,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 +154,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 +199,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" From ab9c9e26dfe0a03c803ede70f8fa060e43051ff9 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Thu, 7 Jul 2022 08:54:20 +0000 Subject: [PATCH 006/110] reformat by black --- tests/python/frontend/caffe/test_forward.py | 2 +- tests/python/frontend/tflite/test_forward.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/python/frontend/caffe/test_forward.py b/tests/python/frontend/caffe/test_forward.py index 749609b6e5a2..4199e27df7ed 100644 --- a/tests/python/frontend/caffe/test_forward.py +++ b/tests/python/frontend/caffe/test_forward.py @@ -1194,4 +1194,4 @@ def test_forward_Inceptionv1(): test_forward_Mobilenetv2() test_forward_Alexnet() test_forward_Resnet50() - test_forward_Inceptionv1() \ No newline at end of file + test_forward_Inceptionv1() diff --git a/tests/python/frontend/tflite/test_forward.py b/tests/python/frontend/tflite/test_forward.py index 4465f9638324..3dd61363fabd 100644 --- a/tests/python/frontend/tflite/test_forward.py +++ b/tests/python/frontend/tflite/test_forward.py @@ -1839,7 +1839,7 @@ def _test_shape(dtype): out = tf.shape(r, out_type=dtype) out = tf.add(out, tf.constant([1], dtype=dtype)) compare_tflite_with_tvm( - [x for x in np.nditer(data)], # pylint: disable=unnecessary-comprehension + [x for x in np.nditer(data)], # pylint: disable=unnecessary-comprehension ["start", "limit", "delta"], [start, limit, delta], [out], @@ -2612,9 +2612,9 @@ 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], # pylint: disable=unnecessary-comprehension + [each for each in inputs], # pylint: disable=unnecessary-comprehension [each.name for each in temp], - [each for each in temp], # pylint: disable=unnecessary-comprehension + [each for each in temp], # pylint: disable=unnecessary-comprehension [output], ) From 6e158679633496d8462601a7efdbcfe82dd281d4 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Thu, 7 Jul 2022 08:56:01 +0000 Subject: [PATCH 007/110] reformat by black --- tests/python/frontend/coreml/model_zoo/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python/frontend/coreml/model_zoo/__init__.py b/tests/python/frontend/coreml/model_zoo/__init__.py index 9645d5ca891e..c137a5d71a05 100644 --- a/tests/python/frontend/coreml/model_zoo/__init__.py +++ b/tests/python/frontend/coreml/model_zoo/__init__.py @@ -46,4 +46,4 @@ def get_cat_image(): # CoreML's standard model image format is BGR img_bgr = np.array(img)[:, :, ::-1] img = np.transpose(img_bgr, (2, 0, 1))[np.newaxis, :] - return np.asarray(img) \ No newline at end of file + return np.asarray(img) From f94842a9504d6ab2b90809d5ee0b660f756482dc Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Thu, 7 Jul 2022 09:33:40 +0000 Subject: [PATCH 008/110] [CI] Apply linting rules to coreml tests --- tests/python/frontend/coreml/test_forward.py | 148 +++++++++++-------- 1 file changed, 83 insertions(+), 65 deletions(-) diff --git a/tests/python/frontend/coreml/test_forward.py b/tests/python/frontend/coreml/test_forward.py index 8892018a79e9..4530c4261952 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. +# pylint: disable=invalid-name, redefined-builtin +""" +CoreML testcases +==================== +This article is a test script to test CoreML operator with Relay. +""" +from os import path +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): + """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): + """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): + """Concat_layer_params""" dtype = "float32" a_np1 = np.random.uniform(size=input1_dim).astype(dtype) @@ -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_UpsampleLayerParams(input_dim, scale, mode): dtype = "float32" a_np = np.full(input_dim, 1, dtype=dtype) @@ -241,11 +244,12 @@ 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") + """UpsampleLayerParams""" + _verify_UpsampleLayerParams((1, 16, 32, 32), 2, "NN") + _verify_UpsampleLayerParams((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) @@ -264,10 +268,10 @@ 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) @@ -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): + """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): + """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): + """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): + """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): + """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): + """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): + """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): + """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,6 +579,7 @@ def test_forward_unary(): @tvm.testing.uses_gpu def test_forward_reduce(): + """Reduce""" from enum import Enum class ReduceAxis(Enum): @@ -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. @@ -749,22 +766,23 @@ def test_forward_image_scaler(): def verify_convolution(input_dim, filter, padding): + """Convolution""" dtype = "float32" - N, C, H, W = input_dim - OC, _, KH, KW = filter + _, c, h, w = input_dim + oc, _, kh, kw = 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=(oc, c, kh, kw)).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))] + inputs = [("input1", datatypes.Array(c, h, w))] output = [("output", datatypes.Array(*b_np.shape))] builder = NeuralNetworkBuilder(inputs, output) builder.add_convolution( name="conv", kernel_channels=3, - output_channels=OC, - height=KH, - width=KW, + output_channels=oc, + height=kh, + width=kw, stride_height=1, stride_width=1, border_mode=padding.lower(), @@ -828,7 +846,7 @@ def test_can_build_keras_to_coreml_to_relay(): if __name__ == "__main__": test_forward_AddLayerParams() - test_forward_ConcatLayerParams() + test_forward_concat_layer_params() test_forward_MultiplyLayerParams() test_forward_UpsampleLayerParams() test_forward_l2_normalize() From e2e33d584ef13ea543d295966572bf9b8b221382 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Thu, 7 Jul 2022 09:37:42 +0000 Subject: [PATCH 009/110] [CI] Apply linting rules to darknet tests --- tests/python/frontend/darknet/test_forward.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/python/frontend/darknet/test_forward.py b/tests/python/frontend/darknet/test_forward.py index 116748a4f78a..887482904daa 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, invalid-name """ Test Darknet Models =================== @@ -23,16 +24,16 @@ """ 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 +download_testdata.__test__ = False + REPO_URL = "https://github.com/dmlc/web-data/blob/main/darknet/" DARKNET_LIB = "libdarknet2.0.so" DARKNETLIB_URL = REPO_URL + "lib/" + DARKNET_LIB + "?raw=true" @@ -75,7 +76,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 From 6b8db8023bccc9779d8730cd6acdb2228fc49c5a Mon Sep 17 00:00:00 2001 From: Black <823036806@qq.com> Date: Thu, 7 Jul 2022 18:42:31 +0800 Subject: [PATCH 010/110] Update tests/python/frontend/coreml/test_forward.py Co-authored-by: Sebastian Boblest --- tests/python/frontend/coreml/test_forward.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python/frontend/coreml/test_forward.py b/tests/python/frontend/coreml/test_forward.py index 4530c4261952..b13493bc8e8e 100644 --- a/tests/python/frontend/coreml/test_forward.py +++ b/tests/python/frontend/coreml/test_forward.py @@ -210,7 +210,7 @@ def test_forward_concat_layer_params(): 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) From 034e20aab74327c62de8a1ea763e5ef04c4379fb Mon Sep 17 00:00:00 2001 From: Black <823036806@qq.com> Date: Thu, 7 Jul 2022 18:42:46 +0800 Subject: [PATCH 011/110] Update tests/python/frontend/tflite/test_forward.py Co-authored-by: Sebastian Boblest --- tests/python/frontend/tflite/test_forward.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python/frontend/tflite/test_forward.py b/tests/python/frontend/tflite/test_forward.py index 3dd61363fabd..66db23b5a520 100644 --- a/tests/python/frontend/tflite/test_forward.py +++ b/tests/python/frontend/tflite/test_forward.py @@ -4382,7 +4382,7 @@ def convert_sub_dummy(self, op): output_tensors = [out] in_node = [0] * 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] + in_node[i] = in_name[i].split(":")[0] with tf.Session() as sess: converter = tf.lite.TFLiteConverter.from_session(sess, input_tensors, output_tensors) From 4fa9ad05023b1cac8c88c76a80bbd12a8a3dbfe0 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Thu, 7 Jul 2022 10:46:20 +0000 Subject: [PATCH 012/110] Update tests/python/frontend/coreml/test_forward.py --- tests/python/frontend/coreml/test_forward.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python/frontend/coreml/test_forward.py b/tests/python/frontend/coreml/test_forward.py index b13493bc8e8e..6645bba3ec40 100644 --- a/tests/python/frontend/coreml/test_forward.py +++ b/tests/python/frontend/coreml/test_forward.py @@ -21,6 +21,7 @@ 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 @@ -580,7 +581,6 @@ def test_forward_unary(): @tvm.testing.uses_gpu def test_forward_reduce(): """Reduce""" - from enum import Enum class ReduceAxis(Enum): CHW = 0 From ef3c43d8e87de7436e5a750037a54021678bb829 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Thu, 7 Jul 2022 10:48:16 +0000 Subject: [PATCH 013/110] Update tests/python/frontend/tflite/test_forward.py --- tests/python/frontend/tflite/test_forward.py | 24 ++++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/tests/python/frontend/tflite/test_forward.py b/tests/python/frontend/tflite/test_forward.py index 66db23b5a520..d0e5099ff172 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, unused-variable, singleton-comparison +# pylint: disable=import-self, invalid-name, unused-argument, unused-variable # pylint: disable=redefined-builtin, no-else-return, inconsistent-return-statements, import-outside-toplevel """ TFLite testcases @@ -2261,14 +2261,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 in_data[0] != None + 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 in_data[1] != None + 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" ), @@ -2279,7 +2279,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 x[0] != None + if x[0] is not None } if math_op is math_ops.equal: @@ -2288,9 +2288,9 @@ def __test_elemwise(in_data): # 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 x[0] != None], + [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 x[0] != None], + [x[1] for x in zip(in_data, inq_data) if x[0] is not None], [out], quantized=True, input_range=input_range, @@ -2299,17 +2299,17 @@ def __test_elemwise(in_data): else: out = math_op( in_data[0] - if in_data[0] != None + if in_data[0] is not None else ops.convert_to_tensor(data[0], dtype=data[0].dtype), in_data[1] - if in_data[1] != None + 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 x[0] != None], - [x[1] for x in zip(in_data, ("in_0:0", "in_1:0")) if x[0] != None], - [x for x in in_data if x != None], + [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], ) @@ -3953,7 +3953,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], From 04f45b9e732fc0f4d0026d90494209b038432588 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Thu, 7 Jul 2022 13:55:42 +0000 Subject: [PATCH 014/110] fix test errors --- tests/python/frontend/coreml/test_forward.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/python/frontend/coreml/test_forward.py b/tests/python/frontend/coreml/test_forward.py index 6645bba3ec40..8d0a0326ce9b 100644 --- a/tests/python/frontend/coreml/test_forward.py +++ b/tests/python/frontend/coreml/test_forward.py @@ -244,10 +244,10 @@ def _verify_upsample_layer_params(input_dim, scale, mode): @tvm.testing.uses_gpu -def test_forward_UpsampleLayerParams(): +def test_forward_upsample_layer_params(): """UpsampleLayerParams""" - _verify_UpsampleLayerParams((1, 16, 32, 32), 2, "NN") - _verify_UpsampleLayerParams((1, 4, 6, 6), 3, "BILINEAR") + _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): @@ -845,10 +845,10 @@ def test_can_build_keras_to_coreml_to_relay(): if __name__ == "__main__": - test_forward_AddLayerParams() + test_forward_add_layer_params test_forward_concat_layer_params() - test_forward_MultiplyLayerParams() - test_forward_UpsampleLayerParams() + test_forward_multiply_layer_params() + test_forward_upsample_layer_params() test_forward_l2_normalize() test_forward_lrn() test_forward_average() From 54dd2df23a991e52a9dc39a0bebbd921b8713701 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Thu, 7 Jul 2022 14:03:14 +0000 Subject: [PATCH 015/110] fix ci test errors --- .../frontend/tensorflow/test_forward.py | 18 ++++++++-- tests/python/frontend/tflite/test_forward.py | 34 +++++++++++++------ 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/tests/python/frontend/tensorflow/test_forward.py b/tests/python/frontend/tensorflow/test_forward.py index dcb1d3a0fd13..ac42186d507a 100644 --- a/tests/python/frontend/tensorflow/test_forward.py +++ b/tests/python/frontend/tensorflow/test_forward.py @@ -36,6 +36,20 @@ 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 +from tensorflow.python.ops import nn_ops +from tensorflow.python.ops import nn +from tensorflow.python.ops import array_ops +from tensorflow.python.ops import math_ops +from tensorflow.python.ops import variable_scope +from tensorflow.python.ops import variables +from tensorflow.python.ops import init_ops +from tensorflow.python.framework import function +from tensorflow.python.framework import ops +from tensorflow.python.framework import dtypes +from tensorflow.python.ops import gen_functional_ops + try: import tensorflow.compat.v1 as tf @@ -4699,7 +4713,7 @@ def _check_op(tf_op, ishape, axis, keepdims, dtype="float32"): compare_tf_with_tvm([np_data], ["in_data:0"], reduce_op.name) def _test_math_op(op, dtypes=["int32", "float32"]): - # pylint: disable=dangerous-default-value + # pylint: disable=dangerous-default-value, redefined-outer-name for dtype in dtypes: _check_op(op, (3, 10), axis=(-1), keepdims=False, dtype=dtype) _check_op(op, (8, 16, 32), axis=(-1), keepdims=False, dtype=dtype) @@ -4743,7 +4757,7 @@ def _check_op(tf_op, ishape, axis, keepdims, range_axis=False, dtype="float32"): compare_tf_with_tvm([np_data], ["in_data:0"], reduce_op.name) def _test_raw_reduce_op(op, dtypes=["int32", "float32"]): - # pylint: disable=dangerous-default-value + # pylint: disable=dangerous-default-value, redefined-outer-name for dtype in dtypes: _check_op(op, (3, 10), axis=(-1), keepdims=False, dtype=dtype) _check_op(op, (8, 16, 32), axis=(-1), keepdims=False, dtype=dtype) diff --git a/tests/python/frontend/tflite/test_forward.py b/tests/python/frontend/tflite/test_forward.py index d0e5099ff172..b83c9167f0e2 100644 --- a/tests/python/frontend/tflite/test_forward.py +++ b/tests/python/frontend/tflite/test_forward.py @@ -2286,16 +2286,28 @@ def __test_elemwise(in_data): out = math_op(inq_data[0], inq_data[1]) out = with_fused_activation_function(out, fused_activation_function) - # 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 x[0] is not None], - [x + ":0" for x in input_range.keys()], - [x[1] for x in zip(in_data, inq_data) if x[0] is not None], - [out], - quantized=True, - input_range=input_range, - experimental_new_converter=same_qnn_params, - ) + compare_tflite_with_tvm( + [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 x[0] is not None], + [out], + ) + else: + out = math_op(inq_data[0], inq_data[1]) + out = with_fused_activation_function(out, fused_activation_function) + 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 x[0] is not None], + [x + ":0" for x in input_range.keys()], + [x[1] for x in zip(in_data, inq_data) if x[0] is not None], + [out], + quantized=True, + input_range=input_range, + experimental_new_converter=same_qnn_params, + ) else: out = math_op( in_data[0] @@ -5055,6 +5067,8 @@ def test_forward_nms_v5(): # Main # ---- if __name__ == "__main__": + test_all_elemwise() + assert 0 # BatchToSpaceND test_forward_batch_to_space_nd() From 736d14c01a5cd6a0e00b12c144943a431102ca31 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Fri, 8 Jul 2022 07:32:28 +0000 Subject: [PATCH 016/110] [CI] Apply linting rules to keras tests --- tests/python/frontend/keras/test_forward.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/python/frontend/keras/test_forward.py b/tests/python/frontend/keras/test_forward.py index ff9e20cff24b..6a2c949a0d0d 100644 --- a/tests/python/frontend/keras/test_forward.py +++ b/tests/python/frontend/keras/test_forward.py @@ -14,13 +14,14 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. +# pylint: disable=invalid-name, redefined-outer-name, missing-function-docstring +"""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 +import keras try: import tensorflow.compat.v1 as tf @@ -28,7 +29,6 @@ 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 if tf.executing_eagerly(): @@ -81,7 +81,7 @@ 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"): + def get_keras_output(xs): return keras_model.predict(xs) def get_tvm_output(xs, target, dev, dtype="float32"): @@ -125,7 +125,7 @@ def get_mobilenet(keras): @tvm.testing.uses_gpu -class TestKeras: +class TestKeras: # pylint: disable=missing-class-docstring scenarios = [using_classic_keras, using_tensorflow_keras] def test_forward_merge(self, keras): From a1a1c55317fdd3fe5f17605e3bea0510a122a352 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Fri, 8 Jul 2022 07:53:26 +0000 Subject: [PATCH 017/110] [CI] Apply linting rules to oneflow tests --- tests/python/frontend/oneflow/test_forward.py | 12 +++--------- tests/python/frontend/oneflow/test_vision_models.py | 5 ++++- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/tests/python/frontend/oneflow/test_forward.py b/tests/python/frontend/oneflow/test_forward.py index 0d18a2fb5c21..41ca08c7ab5a 100644 --- a/tests/python/frontend/oneflow/test_forward.py +++ b/tests/python/frontend/oneflow/test_forward.py @@ -14,19 +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 -# pylint: disable=arguments-differ, unused-argument, unused-import +# pylint: disable=import-self, invalid-name, missing-function-docstring +# pylint: disable=arguments-differ, unused-argument, consider-using-f-string """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 @@ -679,9 +676,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 +684,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() diff --git a/tests/python/frontend/oneflow/test_vision_models.py b/tests/python/frontend/oneflow/test_vision_models.py index e8d0627001ca..d8f5aa6b6001 100644 --- a/tests/python/frontend/oneflow/test_vision_models.py +++ b/tests/python/frontend/oneflow/test_vision_models.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 +# pylint: disable=import-self, invalid-name, consider-using-f-string # pylint: disable=arguments-differ, unused-argument, unused-import """Unit tests for various models and operators""" import os @@ -81,6 +81,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 +106,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 +127,7 @@ def verify_model( @tvm.testing.uses_gpu def test_vision_models(): + """Vision models test""" if os.path.exists(MODEL_HOME): rmdir(MODEL_HOME) From d94029936e02ba294ae0cff40553c5f327ab33e7 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Fri, 8 Jul 2022 11:19:31 +0000 Subject: [PATCH 018/110] [CI] Apply linting rules to onnx tests --- tests/python/frontend/onnx/test_forward.py | 108 ++++++++++----------- 1 file changed, 52 insertions(+), 56 deletions(-) diff --git a/tests/python/frontend/onnx/test_forward.py b/tests/python/frontend/onnx/test_forward.py index d68b76751184..a8bf85b5e3a3 100644 --- a/tests/python/frontend/onnx/test_forward.py +++ b/tests/python/frontend/onnx/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. +# pylint: disable=import-self, invalid-name, unused-argument, unused-variable, missing-function-docstring, redefined-builtin, consider-using-f-string +""" +ONNX testcases +================ +This article is a test script to test ONNX operator with Relay. +""" import glob import os 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 @@ -30,7 +36,13 @@ 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): @@ -105,13 +117,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))) @@ -131,8 +141,6 @@ def get_tvm_output( def get_onnxruntime_output(model, inputs): - import onnxruntime.backend - rep = onnxruntime.backend.prepare(model.SerializeToString(), "CPU") if isinstance(inputs, list) and len(inputs) == 1: inp = inputs[0] @@ -232,11 +240,10 @@ 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 - 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( @@ -256,7 +263,9 @@ def get_next(self): model_fp32 = os.path.join(d.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()) + quantized_model = quantize_static( + model_fp32, model_quant, RandomDataReader() + ) # pylint: disable=assignment-from-no-return # opt_level=1 will cause error with qnn lowering model = onnx.load(model_quant) verify_with_ort_with_inputs( @@ -376,7 +385,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( @@ -1442,7 +1451,7 @@ def test_lrn(target, dev): 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 @@ -1778,7 +1787,7 @@ 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) @@ -1802,7 +1811,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) @@ -2720,9 +2729,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,7 +2778,7 @@ def verify_conv( ) def repeat(N, D): - return tuple([N for _ in range(D)]) + return tuple(N for _ in range(D)) for D in [1, 2, 3]: # Convolution with padding @@ -3006,7 +3015,7 @@ def verify_convtranspose(x_shape, w_shape, y_shape, p, group=1): 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)]) + return tuple(N for _ in range(D)) # Once onnxruntime update is complete for D in [1, 2, 3]: @@ -3104,14 +3113,10 @@ def repeat(N, D): @tvm.testing.parametrize_targets def test_unsqueeze_constant(target, dev): - from torch.nn import Linear, Module, Sequential - class Flatten(Module): def forward(self, input): return input.view(input.size(0), -1) - import tempfile - with tempfile.NamedTemporaryFile() as fp: file_name = fp.name input_size = (1, 16, 32, 32) @@ -3804,7 +3809,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") @@ -3820,7 +3825,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") @@ -4869,8 +4874,10 @@ 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], rtol=1e-05, atol=1e-05 + ) # pylint: disable=unnecessary-list-index-lookup # Confirm that if works with cond as an array or scalar. verify_if(cond_array=False, num_outputs=1) @@ -5130,13 +5137,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") @@ -5398,7 +5403,7 @@ def verify_embedding_bag(num_embedding, embedding_dim, data_shape, num_bags=None def test_index_put(target, dev): class _index_put_model(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 @@ -5448,8 +5453,8 @@ 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) @@ -5849,9 +5854,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( @@ -5887,7 +5892,7 @@ def verify_qlinearconv( 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)]) + return tuple(N for _ in range(D)) # only support QLinearConv2d because only support qnn.conv2d D = 2 @@ -5997,9 +6002,8 @@ 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) @@ -6039,7 +6043,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( @@ -6071,7 +6074,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( @@ -6124,8 +6126,6 @@ def verify_qlinearsigmoid(a_shape): 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], @@ -6376,9 +6376,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( @@ -6414,7 +6414,7 @@ def verify_convinteger( 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)]) + return tuple(N for _ in range(D)) # only support 2D ConvInteger because we only support qnn.conv2d for now. D = 2 @@ -6507,7 +6507,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 @@ -6542,9 +6541,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] ) From 35c97ec513ec44705a9695dfa083fa1bcf4be84e Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Wed, 13 Jul 2022 07:48:53 +0000 Subject: [PATCH 019/110] replace with tvm.testing.main() --- tests/python/frontend/coreml/test_forward.py | 19 +------------------ 1 file changed, 1 insertion(+), 18 deletions(-) diff --git a/tests/python/frontend/coreml/test_forward.py b/tests/python/frontend/coreml/test_forward.py index 8d0a0326ce9b..f10093ab3330 100644 --- a/tests/python/frontend/coreml/test_forward.py +++ b/tests/python/frontend/coreml/test_forward.py @@ -845,21 +845,4 @@ def test_can_build_keras_to_coreml_to_relay(): if __name__ == "__main__": - test_forward_add_layer_params - test_forward_concat_layer_params() - test_forward_multiply_layer_params() - test_forward_upsample_layer_params() - 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() From 3f4cc01286c169e24f335af04809f7b3dc2141b3 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Wed, 13 Jul 2022 08:10:32 +0000 Subject: [PATCH 020/110] fix conflict --- tests/python/frontend/tflite/test_forward.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/python/frontend/tflite/test_forward.py b/tests/python/frontend/tflite/test_forward.py index b83c9167f0e2..fc296ea7590f 100644 --- a/tests/python/frontend/tflite/test_forward.py +++ b/tests/python/frontend/tflite/test_forward.py @@ -5067,8 +5067,6 @@ def test_forward_nms_v5(): # Main # ---- if __name__ == "__main__": - test_all_elemwise() - assert 0 # BatchToSpaceND test_forward_batch_to_space_nd() From bcf23c8aaee432d079503e3dfb694212bcfba8e9 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Wed, 13 Jul 2022 08:17:18 +0000 Subject: [PATCH 021/110] Update as @areusch suggest --- tests/python/frontend/coreml/test_forward.py | 29 ++++++++++---------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/tests/python/frontend/coreml/test_forward.py b/tests/python/frontend/coreml/test_forward.py index f10093ab3330..855441fb3cd9 100644 --- a/tests/python/frontend/coreml/test_forward.py +++ b/tests/python/frontend/coreml/test_forward.py @@ -122,7 +122,7 @@ def run_tvm_graph( def verify_add_layer_params(input_dim, alpha=2): - """Add_layer_params""" + """Verify add layer params""" dtype = "float32" a_np1 = np.random.uniform(size=input_dim).astype(dtype) @@ -151,7 +151,7 @@ def test_forward_add_layer_params(): def verify_multiply_layer_params(input_dim, alpha): - """Multiply_layer_params""" + """Verify multiply layer params""" dtype = "float32" a_np1 = np.random.uniform(size=input_dim).astype(dtype) @@ -184,7 +184,7 @@ def test_forward_multiply_layer_params(): def verify_concat_layer_params(input1_dim, input2_dim): - """Concat_layer_params""" + """Verify concat layer params""" dtype = "float32" a_np1 = np.random.uniform(size=input1_dim).astype(dtype) @@ -245,7 +245,7 @@ def _verify_upsample_layer_params(input_dim, scale, mode): @tvm.testing.uses_gpu def test_forward_upsample_layer_params(): - """UpsampleLayerParams""" + """Upsample Layer Params""" _verify_upsample_layer_params((1, 16, 32, 32), 2, "NN") _verify_upsample_layer_params((1, 4, 6, 6), 3, "BILINEAR") @@ -410,7 +410,7 @@ def test_forward_min(): def verify_unary_sqrt(input_dim): - """Unary sqrt""" + """Verify unary sqrt""" dtype = "float32" a_np = np.random.uniform(size=input_dim).astype(dtype) @@ -428,7 +428,7 @@ def verify_unary_sqrt(input_dim): def verify_unary_rsqrt(input_dim, epsilon=0): - """Unary rsqrt""" + """Verify unary rsqrt""" dtype = "float32" a_np = np.random.uniform(size=input_dim).astype(dtype) @@ -448,7 +448,7 @@ def verify_unary_rsqrt(input_dim, epsilon=0): def verify_unary_inverse(input_dim, epsilon=0): - """Unary inverse""" + """Verify unary inverse""" dtype = "float32" a_np = np.random.uniform(size=input_dim).astype(dtype) @@ -468,7 +468,7 @@ def verify_unary_inverse(input_dim, epsilon=0): def verify_unary_power(input_dim, alpha): - """Unary power""" + """Verify unary power""" dtype = "float32" a_np = np.random.uniform(size=input_dim).astype(dtype) @@ -488,7 +488,7 @@ def verify_unary_power(input_dim, alpha): def verify_unary_exp(input_dim): - """Unary exp""" + """Verify unary exp""" dtype = "float32" a_np = np.random.uniform(size=input_dim).astype(dtype) @@ -506,7 +506,7 @@ def verify_unary_exp(input_dim): def verify_unary_log(input_dim): - """Unary log""" + """Verify unary log""" dtype = "float32" a_np = np.random.uniform(size=input_dim).astype(dtype) @@ -524,7 +524,7 @@ def verify_unary_log(input_dim): def verify_unary_abs(input_dim): - """Unary abs""" + """Verify unary abs""" dtype = "float32" a_np = np.random.uniform(-100.0, 100.0, size=input_dim).astype(dtype) @@ -542,7 +542,7 @@ def verify_unary_abs(input_dim): def verify_unary_threshold(input_dim, alpha): - """Unary threshold""" + """Verify unary threshold""" dtype = "float32" a_np = np.random.uniform(-100.0, 100.0, size=input_dim).astype(dtype) @@ -766,7 +766,7 @@ def test_forward_image_scaler(): def verify_convolution(input_dim, filter, padding): - """Convolution""" + """Verify convolution""" dtype = "float32" _, c, h, w = input_dim oc, _, kh, kw = filter @@ -807,8 +807,7 @@ def test_forward_convolution(): 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( From f41e8f0cf14ca47955dc294be784d89a4d9441d5 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Wed, 13 Jul 2022 13:20:34 +0000 Subject: [PATCH 022/110] pylint pytorch/test_forward.py --- tests/python/frontend/pytorch/test_forward.py | 181 +++++++++--------- 1 file changed, 93 insertions(+), 88 deletions(-) diff --git a/tests/python/frontend/pytorch/test_forward.py b/tests/python/frontend/pytorch/test_forward.py index 4332f3efe59f..a2290f2f8a04 100644 --- a/tests/python/frontend/pytorch/test_forward.py +++ b/tests/python/frontend/pytorch/test_forward.py @@ -14,34 +14,33 @@ # 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, missing-function-docstring """Unit tests for various models and operators""" -from contextlib import suppress import os 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(): torch.backends.cuda.matmul.allow_tf32 = False torch.backends.cudnn.allow_tf32 = False - +# pylint: disable=arguments-renamed def list_ops(expr): class OpLister(tvm.relay.ExprVisitor): def visit_op(self, expr): @@ -93,6 +92,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() @@ -108,16 +108,18 @@ 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") +# pylint: disable=dangerous-default-value def verify_model( model_name, input_data=[], custom_convert_map={}, rtol=1e-5, atol=1e-5, expected_ops=[] ): @@ -156,7 +158,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)]: @@ -209,9 +211,10 @@ def verify_model_with_input( atol=1e-5, assert_shape_only=False, ): + """Generic function to generate and compare Pytorch and TVM output""" 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): @@ -227,13 +230,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(): + """PixelShuffle""" torch.set_grad_enabled(False) input_shape = [1, 144, 16, 16] @@ -805,18 +809,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) @@ -869,7 +873,7 @@ def test_forward_split(): 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 @@ -959,7 +963,7 @@ def test_forward_conv(): 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() @@ -968,7 +972,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() @@ -977,7 +981,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() @@ -986,7 +990,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() @@ -995,7 +999,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() @@ -1004,7 +1008,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() @@ -1418,7 +1422,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 @@ -1655,7 +1660,7 @@ def test_forward_dense(): 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): @@ -1663,7 +1668,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): @@ -1674,11 +1679,11 @@ 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 @@ -1686,17 +1691,14 @@ def 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)) @@ -2149,8 +2151,8 @@ def forward(self, *args): def 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) @@ -2165,7 +2167,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, @@ -2175,7 +2177,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 ) @@ -2234,6 +2236,7 @@ def test_mobilenet_v2(): 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 @@ -2355,15 +2358,16 @@ 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 +# pylint: disable=dangerous-default-value def verify_model_vm(input_model, ishapes, idtype=None, idata=None, targets=["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)))) @@ -2376,8 +2380,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 @@ -2409,9 +2413,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 @@ -2468,7 +2472,7 @@ 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 @@ -2477,7 +2481,7 @@ def forward(self, inp): class LoopWithIf(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 b = a + b if b.sum() > 0.0: @@ -2546,7 +2550,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) @@ -2968,17 +2972,17 @@ def 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 @@ -3663,8 +3667,8 @@ 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 @@ -3933,10 +3937,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 @@ -4056,8 +4060,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}") def test_convert_torch_script_with_input_types(): @@ -4090,7 +4094,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) @@ -4111,16 +4115,16 @@ def test_fn(x, weights=None): def 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(): 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(): @@ -4316,18 +4320,18 @@ 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(): 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) @@ -4412,21 +4416,21 @@ 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 @@ -4510,6 +4514,7 @@ 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(): def test_ior(x, y): @@ -4612,4 +4617,4 @@ def test_fn(x, y, w): if __name__ == "__main__": - pytest.main([__file__]) + tvm.testing.main() From 94c9c13f6095b487d4b54f2b191471b9847f7b23 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Tue, 26 Jul 2022 08:48:43 +0000 Subject: [PATCH 023/110] Update as @areusch suggest --- tests/python/frontend/darknet/test_forward.py | 1 - tests/python/frontend/keras/test_forward.py | 647 +++++++++--------- .../frontend/oneflow/test_vision_models.py | 7 +- tests/python/frontend/pytorch/test_forward.py | 13 +- .../frontend/tensorflow/test_forward.py | 9 +- tests/python/frontend/tflite/test_forward.py | 9 +- 6 files changed, 341 insertions(+), 345 deletions(-) diff --git a/tests/python/frontend/darknet/test_forward.py b/tests/python/frontend/darknet/test_forward.py index 887482904daa..f8a30a4e10cb 100644 --- a/tests/python/frontend/darknet/test_forward.py +++ b/tests/python/frontend/darknet/test_forward.py @@ -32,7 +32,6 @@ from tvm.relay.frontend.darknet import ACTIVATION from tvm import relay -download_testdata.__test__ = False REPO_URL = "https://github.com/dmlc/web-data/blob/main/darknet/" DARKNET_LIB = "libdarknet2.0.so" diff --git a/tests/python/frontend/keras/test_forward.py b/tests/python/frontend/keras/test_forward.py index 6a2c949a0d0d..87837ccf1d9d 100644 --- a/tests/python/frontend/keras/test_forward.py +++ b/tests/python/frontend/keras/test_forward.py @@ -14,14 +14,13 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. -# pylint: disable=invalid-name, redefined-outer-name, missing-function-docstring +# pylint: disable=invalid-name, missing-docstring """Unit tests for various models and operators""" import numpy as np import tvm from tvm import relay from tvm.contrib import graph_executor import tvm.testing -import keras try: import tensorflow.compat.v1 as tf @@ -31,6 +30,7 @@ from tensorflow import keras as tf_keras # 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: @@ -113,34 +113,35 @@ def to_channels_last(arr): tvm.testing.assert_allclose(kout, tout, rtol=1e-5, atol=1e-5) -def get_mobilenet(keras): +def get_mobilenet(keras_): if hasattr(keras.applications, "MobileNet"): # Keras 2.4.x and older - MobileNet = keras.applications.MobileNet + MobileNet = keras_.applications.MobileNet else: # Keras 2.6.x and newer - MobileNet = keras.applications.mobilenet.MobileNet + MobileNet = keras_.applications.mobilenet.MobileNet return MobileNet @tvm.testing.uses_gpu -class TestKeras: # pylint: disable=missing-class-docstring +class TestKeras: + '''Keras test''' scenarios = [using_classic_keras, using_tensorflow_keras] - 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) + 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) merge_funcs = [ - keras.layers.Add(), - keras.layers.Subtract(), - keras.layers.Multiply(), - keras.layers.Maximum(), - keras.layers.Minimum(), - keras.layers.Average(), - keras.layers.Concatenate(), + keras_.layers.Add(), + keras_.layers.Subtract(), + keras_.layers.Multiply(), + keras_.layers.Maximum(), + keras_.layers.Minimum(), + keras_.layers.Average(), + keras_.layers.Concatenate(), ] for merge_func in merge_funcs: class_name = type(merge_func).__name__ @@ -148,138 +149,138 @@ def test_forward_merge(self, keras): out = merge_func([x, y]) else: out = merge_func([x, y, z]) - keras_model = keras.models.Model(data, out) + keras_model = keras_.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_): + data1 = keras_.layers.Input(shape=(2, 2)) + data2 = keras_.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_.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), ] for merge_func in merge_funcs: out = merge_func([data1, data2]) keras_model = keras.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_): + data = keras_.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_.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), ] for act_func in act_funcs: x = act_func(data) - keras_model = keras.models.Model(data, x) + keras_model = keras_.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_): + 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) 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_.layers.Input(shape=(1, 32)) + x = keras_.layers.Dense(32, activation="relu", kernel_initializer="uniform")(data) + keras_model = keras_.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_): + data = keras_.layers.Input(shape=(2, 3, 4)) + x = keras_.layers.Permute([2, 3, 1])(data) + keras_model = keras_.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_): + keras_model = keras_.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_.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"), ] ) 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_): + data = keras_.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_.layers.MaxPooling2D((3, 3), strides=(1, 1), padding="same")(data) + keras_model = keras_.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_.layers.AveragePooling2D((3, 3), strides=(1, 1), padding="same")(data) + keras_model = keras_.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_): + data = keras_.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_.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"), # Enable when relay conv1dtranspose handles NWC - # keras.layers.Conv1DTranspose(filters=10, kernel_size=(3), padding="valid"), + # 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_.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_): + data = keras_.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_.layers.Conv2D(filters=10, kernel_size=(3, 3), strides=(2, 2), padding="same"), + keras_.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_.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"), ] for conv_func in conv_funcs: x = conv_func(data) - keras_model = keras.models.Model(data, x) + keras_model = keras_.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_): + data = keras_.layers.Input(shape=(32, 32, 3)) batch_norm_funcs = [ - keras.layers.BatchNormalization( + keras_.layers.BatchNormalization( axis=-1, momentum=0.99, epsilon=0.001, @@ -290,7 +291,7 @@ def test_forward_batch_norm(self, keras): moving_mean_initializer="zeros", moving_variance_initializer="ones", ), - keras.layers.BatchNormalization( + keras_.layers.BatchNormalization( axis=-1, momentum=0.99, epsilon=0.001, @@ -301,7 +302,7 @@ def test_forward_batch_norm(self, keras): moving_mean_initializer="zeros", moving_variance_initializer="ones", ), - keras.layers.BatchNormalization( + keras_.layers.BatchNormalization( axis=-1, momentum=0.99, epsilon=0.001, @@ -312,7 +313,7 @@ def test_forward_batch_norm(self, keras): moving_mean_initializer="zeros", moving_variance_initializer="ones", ), - keras.layers.BatchNormalization( + keras_.layers.BatchNormalization( axis=-1, momentum=0.99, epsilon=0.001, @@ -326,143 +327,145 @@ 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_.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_, 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) verify_keras_frontend(keras_model) - def test_forward_reshape(self, keras): + def test_forward_reshape(self, keras_): # 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_.layers.Input(shape=(32, 32, 3)) + x = keras_.layers.Reshape(target_shape=(16, 64, 3))(data) + keras_model = keras_.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_.layers.Input(shape=(32, 8, 3)) + x = keras_.layers.Reshape(target_shape=(256, 3))(data) + keras_model = keras_.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_.layers.Input(shape=(256, 3)) + x = keras_.layers.Reshape(target_shape=(8, 32, 3))(data) + keras_model = keras_.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_.layers.Input(shape=(2, 8)) + x = keras_.layers.Reshape(target_shape=(16,))(data) + keras_model = keras_.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_.layers.Input(shape=(16,)) + x = keras_.layers.Reshape(target_shape=(4, 4))(data) + keras_model = keras_.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_.layers.Input(shape=(2, 8)) + x = keras_.layers.Reshape(target_shape=(4, 4))(data) + keras_model = keras_.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_.layers.Input(shape=(15,)) + x = keras_.layers.Reshape(target_shape=(5, 3))(data) + keras_model = keras_.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_.layers.Input(shape=(3, 2, 4)) + x = keras_.layers.Reshape(target_shape=(3, 8))(data) + keras_model = keras_.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_): + 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) 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_): + 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) 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_): + 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]) verify_keras_frontend(keras_model) - def test_forward_reuse_layers(self, keras): + def test_forward_reuse_layers(self, keras_): # reuse conv2d - data = keras.layers.Input(shape=(32, 32, 3)) - conv2d = keras.layers.Conv2D(8, (3, 3), padding="same") + data = keras_.layers.Input(shape=(32, 32, 3)) + conv2d = keras_.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) + z = keras_.layers.Add()([x, y]) + z = keras_.layers.GlobalAveragePooling2D()(z) + keras_model = keras_.models.Model(data, z) 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_.layers.Input(shape=(32, 32, 3)) + x = keras_.layers.Conv2D(8, (3, 3), padding="same")(data) + add = keras_.layers.Add() x = add([x, x]) x = add([x, x]) - z = keras.layers.GlobalAveragePooling2D()(x) - keras_model = keras.models.Model(data, z) + z = keras_.layers.GlobalAveragePooling2D()(x) + keras_model = keras_.models.Model(data, z) verify_keras_frontend(keras_model) - def test_forward_lstm(self, keras): - data = keras.layers.Input(shape=(10, 32)) + def test_forward_lstm(self, keras_): + data = keras_.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_.layers.LSTM(16), + keras_.layers.LSTM(16, return_sequences=True), + keras_.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_.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_): + data = keras_.layers.Input(shape=(1, 32)) rnn_funcs = [ - keras.layers.LSTM( + keras_.layers.LSTM( units=16, return_state=False, recurrent_activation="sigmoid", activation="tanh" ), - keras.layers.LSTM( + keras_.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_.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( units=16, return_state=False, recurrent_activation="sigmoid", activation="tanh", reset_after=False, ), - keras.layers.GRU( + keras_.layers.GRU( units=16, return_state=False, recurrent_activation="sigmoid", @@ -473,220 +476,220 @@ 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_.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_, layout="NCHW"): + if hasattr(keras_.applications, "VGG16"): # Keras 2.4.x and older - VGG16 = keras.applications.VGG16 + VGG16 = keras_.applications.VGG16 else: # Keras 2.6.x and newer - VGG16 = keras.applications.vgg16.VGG16 + VGG16 = keras_.applications.vgg16.VGG16 keras_model = VGG16( 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_, layout="NCHW"): + if hasattr(keras_.applications, "Xception"): # Keras 2.4.x and older - Xception = keras.applications.Xception + Xception = keras_.applications.Xception else: # Keras 2.6.x and newer - Xception = keras.applications.xception.Xception + Xception = keras_.applications.xception.Xception keras_model = Xception( 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_, layout="NCHW"): + if hasattr(keras_.applications, "ResNet50"): # Keras 2.4.x and older - ResNet50 = keras.applications.ResNet50 + ResNet50 = keras_.applications.ResNet50 else: # Keras 2.6.x and newer - ResNet50 = keras.applications.resnet.ResNet50 + ResNet50 = keras_.applications.resnet.ResNet50 keras_model = ResNet50( 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_, layout="NCHW"): + MobileNet = get_mobilenet(keras_) keras_model = MobileNet( 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_): + data = keras_.layers.Input(shape=(32, 32, 32, 3)) conv_funcs = [ - keras.layers.Conv3D( + keras_.layers.Conv3D( filters=10, kernel_size=(3, 3, 3), strides=(2, 2, 2), padding="same" ), - keras.layers.Conv3D( + keras_.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_.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"), ] for conv_func in conv_funcs: x = conv_func(data) - keras_model = keras.models.Model(data, x) + keras_model = keras_.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_): + data = keras_.layers.Input(shape=(32, 32, 32, 3)) conv_funcs = [ - keras.layers.Conv3DTranspose( + keras_.layers.Conv3DTranspose( filters=10, kernel_size=(3, 3, 3), strides=(2, 2, 2), padding="same" ), - keras.layers.Conv3DTranspose( + keras_.layers.Conv3DTranspose( filters=10, kernel_size=(1, 1, 1), dilation_rate=(1, 1, 1), padding="same" ), - keras.layers.Conv3DTranspose( + keras_.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_.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_.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_): + data = keras_.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_.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"), # 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_.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"), ] for pool_func in pool_funcs: x = pool_func(data) - keras_model = keras.models.Model(data, x) + keras_model = keras_.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_): + 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) 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_): + data = keras_.layers.Input(shape=(32, 32, 32, 3)) pad_funcs = [ # Integer - keras.layers.ZeroPadding3D(padding=2), + keras_.layers.ZeroPadding3D(padding=2), # tuple of 3 ints - keras.layers.ZeroPadding3D(padding=(1, 2, 3)), + keras_.layers.ZeroPadding3D(padding=(1, 2, 3)), # tuple of 3 tuples of 2 ints - keras.layers.ZeroPadding3D(padding=((1, 1), (2, 2), (2, 2))), + keras_.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_.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_.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_): + data = keras_.layers.Input(shape=(2, 4), dtype="int32") + x = keras_.layers.Embedding(10, 3)(data) + keras_model = keras_.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_.layers.Input(shape=(2, 3, 4), dtype="int32") + x = keras_.layers.Embedding(4, 5)(data) + keras_model = keras_.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_.layers.Input(shape=(6, 2, 3, 4), dtype="int32") + x = keras_.layers.Embedding(4, 5)(data) + keras_model = keras_.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_): + data = keras_.layers.Input(shape=(5,), dtype="float32") + x = keras_.layers.Dense(6)(data) + x = keras_.layers.RepeatVector(2)(x) - keras_model = keras.models.Model(data, x) + keras_model = keras_.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_.layers.Input(shape=(10,), dtype="float32") + x = keras_.layers.RepeatVector(3)(data) + keras_model = keras_.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_.layers.Input(shape=(4,), dtype="float32") + x = keras_.layers.RepeatVector(1)(data) + keras_model = keras_.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_): + data = keras_.layers.Input(shape=(32, 32, 32, 1)) pool_funcs = [ # global maxpool - keras.layers.GlobalMaxPooling3D(), + keras_.layers.GlobalMaxPooling3D(), # global avgpool - keras.layers.GlobalAveragePooling3D(), + keras_.layers.GlobalAveragePooling3D(), ] for pool_func in pool_funcs: x = pool_func(data) - keras_model = keras.models.Model(data, x) + keras_model = keras_.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_): + MobileNet = get_mobilenet(keras_) sub_model = MobileNet(include_top=False, weights="imagenet", input_shape=(224, 224, 3)) - keras_model = keras.Sequential( + keras_model = keras_.Sequential( [ sub_model, - keras.layers.GlobalAveragePooling2D(), - keras.layers.Dense(1024, activation="relu"), - keras.layers.Dense(2, activation="sigmoid"), + keras_.layers.GlobalAveragePooling2D(), + keras_.layers.Dense(1024, activation="relu"), + keras_.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_): + data = keras_.layers.Input(shape=(16, 12, 8)) + K = keras_.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_.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])), ] for l2_func in l2_funcs: x = l2_func(data) - keras_model = keras.models.Model(data, x) + keras_model = keras_.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_): + 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) ) 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_.Input(shape=(5, 1)) + dense_layer = keras_.layers.Dense(1) + dense_model = keras_.models.Model( + dense_inputs, keras_.layers.TimeDistributed(dense_layer)(dense_inputs) ) verify_keras_frontend(dense_model, need_transpose=False) @@ -694,39 +697,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_=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) diff --git a/tests/python/frontend/oneflow/test_vision_models.py b/tests/python/frontend/oneflow/test_vision_models.py index d8f5aa6b6001..f82277f98195 100644 --- a/tests/python/frontend/oneflow/test_vision_models.py +++ b/tests/python/frontend/oneflow/test_vision_models.py @@ -14,19 +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, consider-using-f-string -# pylint: disable=arguments-differ, unused-argument, unused-import +# pylint: disable=import-self, invalid-name +# 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 diff --git a/tests/python/frontend/pytorch/test_forward.py b/tests/python/frontend/pytorch/test_forward.py index a2290f2f8a04..cd49dde619cf 100644 --- a/tests/python/frontend/pytorch/test_forward.py +++ b/tests/python/frontend/pytorch/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, missing-function-docstring +# pylint: disable=import-self, invalid-name, unused-argument, missing-docstring """Unit tests for various models and operators""" import os import sys @@ -40,13 +40,12 @@ torch.backends.cuda.matmul.allow_tf32 = False torch.backends.cudnn.allow_tf32 = False -# pylint: disable=arguments-renamed def list_ops(expr): 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) + 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 = {} @@ -130,7 +129,7 @@ def verify_model( 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: diff --git a/tests/python/frontend/tensorflow/test_forward.py b/tests/python/frontend/tensorflow/test_forward.py index ac42186d507a..eb6b23250ad3 100644 --- a/tests/python/frontend/tensorflow/test_forward.py +++ b/tests/python/frontend/tensorflow/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, unused-variable, redefined-builtin +# pylint: disable=import-self, invalid-name, unused-argument, unused-variable """ Tensorflow testcases ==================== @@ -297,7 +297,7 @@ def is_gpu_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: # pylint: disable=len-as-condition + if gpu_list: print("Tensorflow GPU:", gpu_list) return True else: @@ -501,7 +501,6 @@ def _test_convolution( add_shapes_to_graph_def=True, ): """One iteration of convolution with given shapes and attributes""" - # pylint: disable=dangerous-default-value total_size_1 = np.prod(tensor_in_sizes) total_size_2 = np.prod(filter_in_sizes) # Initializes the input tensor with array containing incrementing @@ -4682,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)) diff --git a/tests/python/frontend/tflite/test_forward.py b/tests/python/frontend/tflite/test_forward.py index fc296ea7590f..bc5ac2e4fe4f 100644 --- a/tests/python/frontend/tflite/test_forward.py +++ b/tests/python/frontend/tflite/test_forward.py @@ -1839,7 +1839,7 @@ def _test_shape(dtype): out = tf.shape(r, out_type=dtype) out = tf.add(out, tf.constant([1], dtype=dtype)) compare_tflite_with_tvm( - [x for x in np.nditer(data)], # pylint: disable=unnecessary-comprehension + list(np.nditer(data)), ["start", "limit", "delta"], [start, limit, delta], [out], @@ -1894,9 +1894,8 @@ 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""" - # pylint: disable= dangerous-default-value if quantized: with tf.Graph().as_default(): quant_min, quant_max = quant_range @@ -2624,9 +2623,9 @@ 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], # pylint: disable=unnecessary-comprehension + list(inputs), [each.name for each in temp], - [each for each in temp], # pylint: disable=unnecessary-comprehension + list(temp), [output], ) From 9eb3f28ea00ed5c8b916423a02e558b993168fcf Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Thu, 28 Jul 2022 06:28:19 +0000 Subject: [PATCH 024/110] reformat by black --- tests/python/frontend/pytorch/test_forward.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/python/frontend/pytorch/test_forward.py b/tests/python/frontend/pytorch/test_forward.py index cd49dde619cf..87106dd8ad48 100644 --- a/tests/python/frontend/pytorch/test_forward.py +++ b/tests/python/frontend/pytorch/test_forward.py @@ -40,6 +40,7 @@ torch.backends.cuda.matmul.allow_tf32 = False torch.backends.cudnn.allow_tf32 = False + def list_ops(expr): class OpLister(tvm.relay.ExprVisitor): def visit_op(self, op): From 502b300d07d52346a73a11e8aa0c3bd05b86ffeb Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Thu, 28 Jul 2022 06:32:12 +0000 Subject: [PATCH 025/110] fix redefined-outer-name lint errors --- tests/python/frontend/keras/test_forward.py | 658 ++++++++++---------- 1 file changed, 334 insertions(+), 324 deletions(-) diff --git a/tests/python/frontend/keras/test_forward.py b/tests/python/frontend/keras/test_forward.py index 87837ccf1d9d..b9583b2cf53f 100644 --- a/tests/python/frontend/keras/test_forward.py +++ b/tests/python/frontend/keras/test_forward.py @@ -31,6 +31,7 @@ # 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: @@ -61,8 +62,8 @@ 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_classic_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"): @@ -113,35 +114,36 @@ 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 = keras_mod.applications.MobileNet else: # Keras 2.6.x and newer - MobileNet = keras_.applications.mobilenet.MobileNet + MobileNet = keras_mod.applications.mobilenet.MobileNet return MobileNet @tvm.testing.uses_gpu class TestKeras: - '''Keras test''' + """Keras test""" + scenarios = [using_classic_keras, using_tensorflow_keras] - 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) + def test_forward_merge(self, keras_mod): + data = keras_mod.layers.Input(shape=(32, 32, 3)) + x = keras_mod.layers.Conv2D(8, (3, 3), padding="same")(data) + y = keras_mod.layers.Conv2D(8, (3, 3), padding="same")(x) + z = keras_mod.layers.Conv2D(8, (3, 3), padding="same")(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__ @@ -149,138 +151,140 @@ def test_forward_merge(self, keras_): out = merge_func([x, y]) else: out = merge_func([x, y, z]) - keras_model = keras_.models.Model(data, out) + 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): + 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): + 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): + 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): + 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): + 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"), + # 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): + 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): + 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, @@ -291,7 +295,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, @@ -302,7 +306,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, @@ -313,7 +317,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, @@ -327,145 +331,145 @@ 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): # 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): + 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) + z = keras_mod.layers.Average()([x, y]) + z = keras_mod.layers.GlobalAveragePooling2D()(z) + keras_model = keras_mod.models.Model([data1, data2], z) 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): # 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) + z = keras_mod.layers.Add()([x, y]) + z = keras_mod.layers.GlobalAveragePooling2D()(z) + keras_model = keras_mod.models.Model(data, z) 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) + z = keras_mod.layers.GlobalAveragePooling2D()(x) + keras_model = keras_mod.models.Model(data, z) 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): + 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): + 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( + 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_.layers.GRU( + 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", @@ -476,220 +480,226 @@ 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"): + if hasattr(keras_mod.applications, "VGG16"): # Keras 2.4.x and older - VGG16 = keras_.applications.VGG16 + VGG16 = keras_mod.applications.VGG16 else: # Keras 2.6.x and newer - VGG16 = keras_.applications.vgg16.VGG16 + VGG16 = keras_mod.applications.vgg16.VGG16 keras_model = VGG16( 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"): + if hasattr(keras_mod.applications, "Xception"): # Keras 2.4.x and older - Xception = keras_.applications.Xception + Xception = keras_mod.applications.Xception else: # Keras 2.6.x and newer - Xception = keras_.applications.xception.Xception + Xception = keras_mod.applications.xception.Xception keras_model = Xception( 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"): + if hasattr(keras_mod.applications, "ResNet50"): # Keras 2.4.x and older - ResNet50 = keras_.applications.ResNet50 + ResNet50 = keras_mod.applications.ResNet50 else: # Keras 2.6.x and newer - ResNet50 = keras_.applications.resnet.ResNet50 + ResNet50 = keras_mod.applications.resnet.ResNet50 keras_model = ResNet50( 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 = get_mobilenet(keras_mod) keras_model = MobileNet( 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): + 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): + 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): + 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): + 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): + 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): + 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): + 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): + MobileNet = get_mobilenet(keras_mod) sub_model = MobileNet(include_top=False, weights="imagenet", input_shape=(224, 224, 3)) - keras_model = keras_.Sequential( + 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): + data = keras_mod.layers.Input(shape=(16, 12, 8)) + K = 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.l2_normalize(v, axis=-2)), + keras_mod.layers.Lambda(lambda v: K.l2_normalize(x=v, axis=-1)), + keras_mod.layers.Lambda(lambda v: K.l2_normalize(axis=1, x=v)), + keras_mod.layers.Lambda(lambda v: K.l2_normalize(v, 2)), + keras_mod.layers.Lambda(lambda v: K.l2_normalize(v, axis=3)), + keras_mod.layers.Lambda(lambda v: K.l2_normalize(v, axis=(2, 3))), + keras_mod.layers.Lambda(lambda v: K.l2_normalize(v, (1, 2))), + keras_mod.layers.Lambda(lambda v: K.l2_normalize(v, axis=[-2, -1])), + keras_mod.layers.Lambda(lambda v: K.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): + 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) @@ -697,39 +707,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) From d870f5dc30fbfec56a5b6bdcdd60d289617e6296 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Thu, 28 Jul 2022 06:48:46 +0000 Subject: [PATCH 026/110] remove rules --- tests/python/frontend/caffe2/test_forward.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/python/frontend/caffe2/test_forward.py b/tests/python/frontend/caffe2/test_forward.py index 09ee8a4a3e67..88849ba20f24 100644 --- a/tests/python/frontend/caffe2/test_forward.py +++ b/tests/python/frontend/caffe2/test_forward.py @@ -14,7 +14,6 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. -# pylint: disable=no-else-return """ Caffe2 testcases ==================== From a6560d702d0fdcd0dac447ff7b57f37482d550a8 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Thu, 28 Jul 2022 07:23:09 +0000 Subject: [PATCH 027/110] remove rules & fix pylint errors --- tests/python/frontend/keras/test_forward.py | 180 +++++++++++--------- 1 file changed, 104 insertions(+), 76 deletions(-) diff --git a/tests/python/frontend/keras/test_forward.py b/tests/python/frontend/keras/test_forward.py index b9583b2cf53f..181c566f9949 100644 --- a/tests/python/frontend/keras/test_forward.py +++ b/tests/python/frontend/keras/test_forward.py @@ -14,7 +14,6 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. -# pylint: disable=invalid-name, missing-docstring """Unit tests for various models and operators""" import numpy as np import tvm @@ -45,10 +44,12 @@ 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: @@ -67,6 +68,7 @@ def pytest_generate_tests(metafunc): 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" @@ -82,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): - 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())] @@ -102,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: @@ -117,12 +119,12 @@ def to_channels_last(arr): def get_mobilenet(keras_mod): if hasattr(keras_mod.applications, "MobileNet"): # Keras 2.4.x and older - MobileNet = keras_mod.applications.MobileNet + mobilenet_mod = keras_mod.applications.MobileNet else: # Keras 2.6.x and newer - MobileNet = keras_mod.applications.mobilenet.MobileNet + mobilenet_mod = keras_mod.applications.mobilenet.MobileNet - return MobileNet + return mobilenet_mod @tvm.testing.uses_gpu @@ -132,10 +134,11 @@ class TestKeras: scenarios = [using_classic_keras, using_tensorflow_keras] def test_forward_merge(self, keras_mod): + """test_forward_merge""" data = keras_mod.layers.Input(shape=(32, 32, 3)) - x = keras_mod.layers.Conv2D(8, (3, 3), padding="same")(data) - y = keras_mod.layers.Conv2D(8, (3, 3), padding="same")(x) - z = keras_mod.layers.Conv2D(8, (3, 3), padding="same")(y) + 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_mod.layers.Add(), keras_mod.layers.Subtract(), @@ -148,13 +151,14 @@ def test_forward_merge(self, keras_mod): 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]) + 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_mod): + """test_forward_merge_dot""" data1 = keras_mod.layers.Input(shape=(2, 2)) data2 = keras_mod.layers.Input(shape=(2, 2)) merge_funcs = [ @@ -171,6 +175,7 @@ def test_forward_merge_dot(self, keras_mod): verify_keras_frontend(keras_model) def test_forward_activations(self, keras_mod): + """test_forward_activations""" data = keras_mod.layers.Input(shape=(32, 32, 3)) act_funcs = [ keras_mod.layers.Activation("softmax"), @@ -206,6 +211,7 @@ def test_forward_activations(self, keras_mod): verify_keras_frontend(keras_model, need_transpose=False, layout="NHWC") 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) @@ -225,6 +231,7 @@ def test_forward_permute(self, keras_mod): verify_keras_frontend(keras_model, need_transpose=False) def test_forward_sequential(self, keras_mod): + """test_forward_sequential""" keras_model = keras_mod.models.Sequential( [ keras_mod.layers.Dense(16, input_dim=32, activation="relu"), @@ -248,6 +255,7 @@ def test_forward_pool(self, keras_mod): verify_keras_frontend(keras_model) def test_forward_conv1d(self, keras_mod): + """test_forward_conv1d""" data = keras_mod.layers.Input(shape=(32, 3)) conv_funcs = [ keras_mod.layers.Conv1D(filters=10, kernel_size=(3,), strides=(2,), padding="same"), @@ -265,6 +273,7 @@ def test_forward_conv1d(self, keras_mod): verify_keras_frontend(keras_model, layout="NWC") def test_forward_conv(self, keras_mod): + """test_forward_conv""" data = keras_mod.layers.Input(shape=(32, 32, 3)) conv_funcs = [ keras_mod.layers.Conv2D(filters=10, kernel_size=(3, 3), strides=(2, 2), padding="same"), @@ -282,6 +291,7 @@ def test_forward_conv(self, keras_mod): verify_keras_frontend(keras_model) 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_mod.layers.BatchNormalization( @@ -341,6 +351,7 @@ def test_forward_upsample(self, keras_mod, interpolation="nearest"): verify_keras_frontend(keras_model) def test_forward_reshape(self, keras_mod): + """test_forward_reshape""" # input_shape len is 3, target_shape len is 3 data = keras_mod.layers.Input(shape=(32, 32, 3)) x = keras_mod.layers.Reshape(target_shape=(16, 64, 3))(data) @@ -383,6 +394,7 @@ def test_forward_reshape(self, keras_mod): verify_keras_frontend(keras_model) 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) @@ -399,9 +411,9 @@ def test_forward_multi_inputs(self, keras_mod): 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) - z = keras_mod.layers.Average()([x, y]) - z = keras_mod.layers.GlobalAveragePooling2D()(z) - keras_model = keras_mod.models.Model([data1, data2], z) + 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_mod): @@ -414,14 +426,15 @@ def test_forward_multi_outputs(self, keras_mod): verify_keras_frontend(keras_model) def test_forward_reuse_layers(self, keras_mod): + """test_forward_reuse_layers""" # reuse conv2d 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_mod.layers.Add()([x, y]) - z = keras_mod.layers.GlobalAveragePooling2D()(z) - keras_model = keras_mod.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_mod.layers.Input(shape=(32, 32, 3)) @@ -429,11 +442,12 @@ def test_forward_reuse_layers(self, keras_mod): add = keras_mod.layers.Add() x = add([x, x]) x = add([x, x]) - z = keras_mod.layers.GlobalAveragePooling2D()(x) - keras_model = keras_mod.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_mod): + """test_forward_lstm""" data = keras_mod.layers.Input(shape=(10, 32)) rnn_funcs = [ keras_mod.layers.LSTM(16), @@ -446,6 +460,7 @@ def test_forward_lstm(self, keras_mod): verify_keras_frontend(keras_model, need_transpose=False) def test_forward_rnn(self, keras_mod): + """test_forward_rnn""" data = keras_mod.layers.Input(shape=(1, 32)) rnn_funcs = [ keras_mod.layers.LSTM( @@ -484,53 +499,57 @@ def test_forward_rnn(self, keras_mod): verify_keras_frontend(keras_model, need_transpose=False) 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_mod.applications.VGG16 + vgg16_mod = keras_mod.applications.VGG16 else: # Keras 2.6.x and newer - VGG16 = keras_mod.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_mod, layout="NCHW"): + """test_forward_vgg16""" if hasattr(keras_mod.applications, "Xception"): # Keras 2.4.x and older - Xception = keras_mod.applications.Xception + xception_mod = keras_mod.applications.Xception else: # Keras 2.6.x and newer - Xception = keras_mod.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_mod, layout="NCHW"): + """test_forward_resnet50""" if hasattr(keras_mod.applications, "ResNet50"): # Keras 2.4.x and older - ResNet50 = keras_mod.applications.ResNet50 + resnet50_mod = keras_mod.applications.ResNet50 else: # Keras 2.6.x and newer - ResNet50 = keras_mod.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_mod, layout="NCHW"): - MobileNet = get_mobilenet(keras_mod) + 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_mod): + """test_forward_conv3d""" data = keras_mod.layers.Input(shape=(32, 32, 32, 3)) conv_funcs = [ keras_mod.layers.Conv3D( @@ -550,6 +569,7 @@ def test_forward_conv3d(self, keras_mod): verify_keras_frontend(keras_model, layout="NDHWC") 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_mod.layers.Conv3DTranspose( @@ -569,6 +589,7 @@ def test_forward_conv3d_transpose(self, keras_mod): verify_keras_frontend(keras_model, layout="NDHWC") def test_forward_pool3d(self, keras_mod): + """test_forward_pool3d""" data = keras_mod.layers.Input(shape=(32, 32, 32, 1)) pool_funcs = [ # maxpool keras_mod.layers.MaxPooling3D(pool_size=(2, 2, 2), strides=(1, 1, 1), padding="same"), @@ -593,6 +614,7 @@ def test_forward_upsample3d(self, keras_mod): verify_keras_frontend(keras_model, layout="NDHWC") 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_mod.layers.ZeroPadding3D(padding=2), @@ -609,6 +631,7 @@ def test_forward_zero_padding3d(self, keras_mod): verify_keras_frontend(keras_model, layout="NDHWC") 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) @@ -625,6 +648,7 @@ def test_forward_embedding(self, keras_mod): verify_keras_frontend(keras_model, need_transpose=False) 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) @@ -643,6 +667,7 @@ def test_forward_repeat_vector(self, keras_mod): verify_keras_frontend(keras_model, need_transpose=False) 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_mod.layers.GlobalMaxPooling3D(), @@ -655,9 +680,10 @@ def test_forward_global_pool3d(self, keras_mod): verify_keras_frontend(keras_model, layout="NDHWC") def test_forward_nested_layers(self, keras_mod): - MobileNet = get_mobilenet(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)) + sub_model = mobilenet_mod(include_top=False, weights="imagenet", input_shape=(224, 224, 3)) keras_model = keras_mod.Sequential( [ sub_model, @@ -669,18 +695,19 @@ def test_forward_nested_layers(self, keras_mod): verify_keras_frontend(keras_model) def test_forward_l2_normalize(self, keras_mod): + """test_forward_l2_normalize""" data = keras_mod.layers.Input(shape=(16, 12, 8)) - K = keras_mod.backend + k_backend = keras_mod.backend l2_funcs = [ - keras_mod.layers.Lambda(lambda v: K.l2_normalize(v, axis=-2)), - keras_mod.layers.Lambda(lambda v: K.l2_normalize(x=v, axis=-1)), - keras_mod.layers.Lambda(lambda v: K.l2_normalize(axis=1, x=v)), - keras_mod.layers.Lambda(lambda v: K.l2_normalize(v, 2)), - keras_mod.layers.Lambda(lambda v: K.l2_normalize(v, axis=3)), - keras_mod.layers.Lambda(lambda v: K.l2_normalize(v, axis=(2, 3))), - keras_mod.layers.Lambda(lambda v: K.l2_normalize(v, (1, 2))), - keras_mod.layers.Lambda(lambda v: K.l2_normalize(v, axis=[-2, -1])), - keras_mod.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) @@ -689,6 +716,7 @@ def test_forward_l2_normalize(self, keras_mod): verify_keras_frontend(keras_model, layout="NHWC") 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( @@ -707,30 +735,30 @@ def test_forward_time_distributed(self, keras_mod): if __name__ == "__main__": for k in [keras, tf_keras]: sut = TestKeras() - 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_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) From ec4fa1232f2556ddea89048d82a74dd509fca962 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Thu, 28 Jul 2022 08:10:51 +0000 Subject: [PATCH 028/110] Remove all unused_var and fix other pylint errors --- .../frontend/tensorflow/test_forward.py | 188 +++++++++--------- 1 file changed, 92 insertions(+), 96 deletions(-) diff --git a/tests/python/frontend/tensorflow/test_forward.py b/tests/python/frontend/tensorflow/test_forward.py index eb6b23250ad3..3bfa8dc10ce7 100644 --- a/tests/python/frontend/tensorflow/test_forward.py +++ b/tests/python/frontend/tensorflow/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, unused-variable +# pylint: disable=import-self, invalid-name, unused-argument """ Tensorflow testcases ==================== @@ -24,13 +24,17 @@ from distutils.version import LooseVersion import threading +import os.path import numpy as np import pytest +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 import tvm import tvm.relay.testing.tf as tf_testing @@ -110,9 +114,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( @@ -185,7 +189,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 @@ -199,7 +202,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 @@ -259,14 +262,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( @@ -284,17 +287,16 @@ 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 enumerate(tf_output): - if not isinstance(tf_output[i], np.ndarray): + 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_output[i], tvm_output[i], atol=1e-5, rtol=1e-5) + 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 gpu_list: @@ -1472,7 +1474,7 @@ 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( @@ -1480,8 +1482,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"]: @@ -1501,20 +1503,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) @@ -1540,8 +1542,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"]: @@ -1563,11 +1565,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") @@ -1594,7 +1596,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"]: @@ -1611,14 +1613,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"]: @@ -1641,7 +1643,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") @@ -1661,8 +1663,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") @@ -1714,7 +1716,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") @@ -1799,7 +1801,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 ) @@ -1820,8 +1822,8 @@ def test_read_variable_op(target, dev): out_names=out_name, num_output=len(out_name), ) - for i, _ in enumerate(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() @@ -2928,7 +2930,7 @@ def _test_split(in_shape, axis, num_or_size_splits, dtype): 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 ) split = tf.split(in_data, num_or_size_splits, axis=axis) @@ -3083,7 +3085,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( @@ -3104,8 +3106,8 @@ def test_forward_multi_output(): 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"] @@ -3123,8 +3125,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 enumerate(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) ####################################################################### @@ -3300,7 +3302,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") @@ -3745,7 +3747,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: @@ -3775,7 +3777,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") @@ -3785,7 +3787,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") @@ -3795,7 +3797,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") @@ -3804,7 +3806,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") @@ -3822,7 +3824,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 @@ -3866,17 +3868,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() @@ -3944,9 +3941,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 @@ -3977,13 +3974,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, @@ -4086,7 +4083,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) @@ -4161,11 +4157,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) @@ -4465,8 +4461,8 @@ 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") @@ -4783,7 +4779,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") @@ -4870,7 +4866,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 @@ -4911,7 +4907,7 @@ 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(): @@ -4947,7 +4943,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") @@ -4961,7 +4957,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"): @@ -5115,7 +5111,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(): @@ -5168,7 +5164,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( @@ -5296,7 +5292,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, @@ -5316,7 +5312,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 ) @@ -5340,7 +5336,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, @@ -5361,7 +5357,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( @@ -5380,7 +5376,7 @@ def constantsFn(x, y): a = tf.constant(20000, name="a") b = tf.constant(40000, name="b") - spopFn = gen_functional_ops.StatefulPartitionedCall( + _ = gen_functional_ops.StatefulPartitionedCall( args=[a, b], Tout=[tf.int32], f=constantsFn ) @@ -5443,7 +5439,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: @@ -5469,11 +5465,12 @@ 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") @@ -5516,18 +5513,18 @@ def test_forward_dynamic_input_shape(): 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, @@ -5571,12 +5568,10 @@ def generateData(): state_per_layer_list = tf.unstack(init_state, axis=0) rnn_tuple_state = tuple( - [ - tf.nn.rnn_cell.LSTMStateTuple( + list(tf.nn.rnn_cell.LSTMStateTuple( state_per_layer_list[idx][0], state_per_layer_list[idx][1] ) - for idx in range(num_layers) - ] + for idx in range(num_layers)) ) # Forward passes @@ -5592,7 +5587,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 @@ -5625,7 +5620,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"], @@ -5636,8 +5631,8 @@ def lstm_cell(): ) # Compare result - for i, _ in enumerate(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) ####################################################################### @@ -5720,9 +5715,9 @@ def test_moments(): 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 = """ @@ -5760,4 +5755,5 @@ def test_invert_permutation(): if __name__ == "__main__": + test_tensor_array_scatter() pytest.main([__file__]) From 0124341f98e18d8ea20f85fc97e972f8196adc79 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Thu, 28 Jul 2022 08:58:38 +0000 Subject: [PATCH 029/110] reformatted by black --- tests/python/frontend/tensorflow/test_forward.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/tests/python/frontend/tensorflow/test_forward.py b/tests/python/frontend/tensorflow/test_forward.py index 3bfa8dc10ce7..faa9ec82f698 100644 --- a/tests/python/frontend/tensorflow/test_forward.py +++ b/tests/python/frontend/tensorflow/test_forward.py @@ -1515,7 +1515,7 @@ def run(dtype_str, infer_shape): 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)] # pylint: disable=unnecessary-comprehension + 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) @@ -2930,9 +2930,7 @@ def _test_split(in_shape, axis, num_or_size_splits, dtype): tf.reset_default_graph() with tf.Graph().as_default(): in_data = tf.placeholder(dtype, in_shape, name="in_data") - _ = ( - 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] @@ -5376,9 +5374,7 @@ def constantsFn(x, y): a = tf.constant(20000, name="a") b = tf.constant(40000, name="b") - _ = 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 @@ -5568,10 +5564,12 @@ def generateData(): state_per_layer_list = tf.unstack(init_state, axis=0) rnn_tuple_state = tuple( - list(tf.nn.rnn_cell.LSTMStateTuple( + list( + tf.nn.rnn_cell.LSTMStateTuple( state_per_layer_list[idx][0], state_per_layer_list[idx][1] ) - for idx in range(num_layers)) + for idx in range(num_layers) + ) ) # Forward passes From de1b9a525fbd9ad835b35bb6c4c5dcee00718b64 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Thu, 28 Jul 2022 10:18:55 +0000 Subject: [PATCH 030/110] [CI] Apply linting rules to onnx tests --- tests/python/frontend/onnx/test_forward.py | 815 ++++++++++++--------- 1 file changed, 463 insertions(+), 352 deletions(-) diff --git a/tests/python/frontend/onnx/test_forward.py b/tests/python/frontend/onnx/test_forward.py index a8bf85b5e3a3..24dfe90458a4 100644 --- a/tests/python/frontend/onnx/test_forward.py +++ b/tests/python/frontend/onnx/test_forward.py @@ -46,6 +46,7 @@ def get_input_data_shape_dict(graph_def, input_data): + """Get input data shape""" if isinstance(input_data, list): input_names = {} shape_dict = {} @@ -141,6 +142,7 @@ def get_tvm_output( def get_onnxruntime_output(model, inputs): + """Generic function to generate onnxruntime output""" rep = onnxruntime.backend.prepare(model.SerializeToString(), "CPU") if isinstance(inputs, list) and len(inputs) == 1: inp = inputs[0] @@ -169,6 +171,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 @@ -221,6 +224,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, @@ -240,8 +244,8 @@ def verify_with_ort( def quantize_and_verify_with_ort( onnx_model, input_names, input_shapes, target, dev, rtol=1e-5, atol=1e-5 ): + """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): @@ -259,13 +263,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_quant = os.path.join(t_dir.temp_dir, "model.quant.onnx") + _ = quantize_static( # pylint: disable=assignment-from-no-return model_fp32, model_quant, RandomDataReader() - ) # pylint: disable=assignment-from-no-return + ) # opt_level=1 will cause error with qnn lowering model = onnx.load(model_quant) verify_with_ort_with_inputs( @@ -290,6 +294,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) @@ -323,6 +328,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) @@ -358,6 +364,7 @@ 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": @@ -424,9 +431,10 @@ 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"], block_size=block_size ) graph = helper.make_graph( @@ -443,14 +451,15 @@ 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"], block_size=block_size ) graph = helper.make_graph( @@ -469,6 +478,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) @@ -504,6 +514,7 @@ 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] @@ -537,6 +548,7 @@ 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, @@ -572,6 +584,7 @@ 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) @@ -593,6 +606,7 @@ 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) @@ -612,6 +626,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) @@ -630,6 +645,7 @@ 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") @@ -665,6 +681,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 @@ -710,6 +727,7 @@ 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") @@ -744,6 +762,7 @@ 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") @@ -776,6 +795,7 @@ 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) @@ -1021,6 +1041,7 @@ def test_ceil(target, dev): @tvm.testing.parametrize_targets def test_clip(target, dev): + """test_clip""" _test_onnx_op_elementwise( target, dev, @@ -1060,6 +1081,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]), @@ -1112,11 +1134,13 @@ 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") @@ -1165,6 +1189,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 @@ -1195,6 +1220,7 @@ 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) @@ -1246,6 +1272,7 @@ 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]] @@ -1276,6 +1303,7 @@ 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") @@ -1324,6 +1352,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] @@ -1357,6 +1386,7 @@ 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" @@ -1397,6 +1427,7 @@ 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") @@ -1431,7 +1462,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]) @@ -1448,6 +1479,7 @@ 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) @@ -1476,6 +1508,7 @@ 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) @@ -1511,6 +1544,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) @@ -1531,6 +1565,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) @@ -1554,6 +1589,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) @@ -1574,6 +1610,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) @@ -1619,6 +1656,7 @@ 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 @@ -1669,6 +1707,7 @@ 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" @@ -1698,6 +1737,7 @@ 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" @@ -1727,6 +1767,7 @@ 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" @@ -1756,6 +1797,7 @@ 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" @@ -1783,6 +1825,7 @@ 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) @@ -1826,6 +1869,7 @@ 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", @@ -1860,6 +1904,7 @@ 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 @@ -1961,6 +2006,7 @@ 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 @@ -2036,6 +2082,7 @@ 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] @@ -2064,7 +2111,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, ) @@ -2079,7 +2126,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)) ], @@ -2130,14 +2177,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), @@ -2154,41 +2202,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)), @@ -2270,6 +2319,7 @@ 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"]) @@ -2302,8 +2352,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 @@ -2312,7 +2362,7 @@ def ThresholdedRelu_x(x, alpha): target, dev, (2, 4, 5, 6), - ThresholdedRelu_x, + thresholded_relu_x, {"alpha": 0.25}, "float32", "ThresholdedRelu", @@ -2321,7 +2371,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, @@ -2336,7 +2386,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) @@ -2387,14 +2437,15 @@ 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) @@ -2424,6 +2475,7 @@ 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) @@ -2476,6 +2528,7 @@ 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( @@ -2495,12 +2548,13 @@ 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( @@ -2513,12 +2567,13 @@ 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"] @@ -2586,6 +2641,7 @@ 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) @@ -2638,6 +2694,7 @@ 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"] @@ -2670,6 +2727,7 @@ 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( @@ -2703,6 +2761,7 @@ 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, @@ -2777,112 +2836,113 @@ 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, @@ -3014,67 +3074,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 @@ -3089,36 +3149,37 @@ 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(N, dims), + repeat(4, dims), + repeat(2, dims), + repeat(1, dims), auto_pad="SAME_LOWER", ) @tvm.testing.parametrize_targets def test_unsqueeze_constant(target, dev): + """test_unsqueeze_constant""" class Flatten(Module): - def forward(self, input): - return input.view(input.size(0), -1) + 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)) @@ -3130,15 +3191,16 @@ 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 @@ -3255,6 +3317,7 @@ 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) @@ -3263,7 +3326,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"]) @@ -3300,6 +3363,7 @@ 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" ): @@ -3412,6 +3476,7 @@ 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) @@ -3446,6 +3511,7 @@ 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) @@ -3496,6 +3562,7 @@ 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") @@ -3527,6 +3594,7 @@ 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( @@ -3573,6 +3641,7 @@ 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: @@ -3699,6 +3768,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"], @@ -3719,7 +3789,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) @@ -3758,6 +3828,7 @@ def verify_rnn( target=None, dev=None, ): + """verify_rnn""" if rnn_type == "LSTM": multiplier = 4 elif rnn_type == "GRU": @@ -3766,7 +3837,7 @@ def verify_rnn( raise NotImplementedError(f"{rnn_type} RNNs not yet supported.") if directions not in [1, 2]: - raise ValueError(f"Direction should be either 1 or 2 (for bidirectional LSTMs)") + raise ValueError("Direction should be either 1 or 2 (for bidirectional LSTMs)") def get_inputs(): input_names = [] @@ -3883,6 +3954,7 @@ def register(name, shape, proto_type): @tvm.testing.parametrize_targets def test_lstm(target, dev): + """test_lstm""" for directions in [1, 2]: # No bias. verify_rnn( @@ -4033,6 +4105,7 @@ def test_lstm(target, dev): @tvm.testing.parametrize_targets def test_gru(target, dev): + """test_gru""" # Set seed for test reproduction np.random.seed(137) for directions in [1, 2]: @@ -4190,6 +4263,7 @@ 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,), []), @@ -4337,6 +4411,7 @@ 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", @@ -4368,9 +4443,10 @@ 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 @@ -4399,7 +4475,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]: @@ -4414,6 +4490,7 @@ 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, @@ -4483,6 +4560,7 @@ 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 ): @@ -4576,6 +4654,7 @@ 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]) @@ -4634,7 +4713,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], @@ -4700,7 +4779,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], @@ -4813,6 +4892,7 @@ 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. @@ -4838,12 +4918,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) @@ -4876,8 +4956,8 @@ def append_constant_nodes(nodes, outputs, expected, name): tvm_out = [tvm_out] for i, _ in enumerate(tvm_out): tvm.testing.assert_allclose( - correct_out[i], tvm_out[i], rtol=1e-05, atol=1e-05 - ) # pylint: disable=unnecessary-list-index-lookup + correct_out[i], tvm_out[i], rtol=1e-05, atol=1e-05 # pylint: disable=unnecessary-list-index-lookup + ) # Confirm that if works with cond as an array or scalar. verify_if(cond_array=False, num_outputs=1) @@ -4888,6 +4968,7 @@ 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", @@ -4917,6 +4998,7 @@ 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 = [ @@ -4970,23 +5052,24 @@ 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", @@ -5017,7 +5100,8 @@ 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"], @@ -5033,11 +5117,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, @@ -5051,7 +5135,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 = ( @@ -5085,17 +5169,18 @@ 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"] @@ -5285,6 +5370,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""" target_kind = tvm.target.Target(target).kind.name if onnx_test in unsupported_onnx_tests: @@ -5337,6 +5423,7 @@ def test_onnx_nodes(target, dev, onnx_test): def test_wrong_input(): + """test_wrong_input""" node = helper.make_node( "Softplus", inputs=["X"], @@ -5363,10 +5450,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, @@ -5401,7 +5489,8 @@ 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().__init__() self.indices = indices @@ -5412,7 +5501,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, @@ -5429,7 +5518,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) @@ -5458,7 +5547,7 @@ def verify_index_put_slice(data_shape, value_shape, accumulate): 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) @@ -5472,6 +5561,7 @@ 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", @@ -5509,6 +5599,7 @@ 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", @@ -5535,6 +5626,7 @@ 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", @@ -5567,6 +5659,7 @@ def verify_biasgelu(x, bias): @tvm.testing.parametrize_targets def test_embedlayernormalization(target, dev): + """test_embedlayernormalization""" def verify_embedlayernormalization( input_ids, segment_ids, @@ -5675,7 +5768,8 @@ 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"], @@ -5690,7 +5784,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( @@ -5698,7 +5792,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) ), @@ -5711,8 +5805,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, @@ -5726,17 +5820,18 @@ 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"], @@ -5750,20 +5845,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 @@ -5771,18 +5866,19 @@ 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, @@ -5891,113 +5987,114 @@ 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 = [] @@ -6030,10 +6127,11 @@ 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)), @@ -6061,10 +6159,11 @@ 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)), @@ -6094,6 +6193,7 @@ 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") @@ -6120,9 +6220,10 @@ 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))] @@ -6144,6 +6245,7 @@ def verify_qlinearsigmoid(a_shape): @tvm.testing.parametrize_targets 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( @@ -6202,7 +6304,8 @@ def get_random_uniform(shape, dtype="float32", high=1.0, low=0.0, seed=None): @tvm.testing.parametrize_targets 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) @@ -6214,7 +6317,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( @@ -6224,33 +6327,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, @@ -6270,6 +6373,7 @@ def get_random_uniform_like(input, shape, dtype=None, high=1.0, low=0.0, seed=No @tvm.testing.parametrize_targets 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( @@ -6308,7 +6412,8 @@ def get_random_normal(shape, dtype="float32", scale=1.0, mean=0.0, seed=None): @tvm.testing.parametrize_targets 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 @@ -6324,20 +6429,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) @@ -6345,6 +6452,7 @@ def get_random_normal_like(input, shape, dtype="float32", scale=1.0, mean=0.0, s @tvm.testing.parametrize_targets def test_convinteger(target, dev): + """test_convinteger""" def verify_convinteger( x_shape, w_shape, @@ -6413,90 +6521,91 @@ 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, @@ -6658,8 +6767,9 @@ 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) @@ -6693,15 +6803,16 @@ 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 = [] @@ -6711,7 +6822,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. From dff98bad9a73b9adb7b755151afd35a9dd109b73 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Thu, 28 Jul 2022 10:34:12 +0000 Subject: [PATCH 031/110] Disable unused-argument is for some unused-argument in function can not be removed --- tests/python/frontend/onnx/test_forward.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python/frontend/onnx/test_forward.py b/tests/python/frontend/onnx/test_forward.py index 24dfe90458a4..dba65a939513 100644 --- a/tests/python/frontend/onnx/test_forward.py +++ b/tests/python/frontend/onnx/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, unused-variable, missing-function-docstring, redefined-builtin, consider-using-f-string +# pylint: disable=unused-argument """ ONNX testcases ================ From 043ea1c3511bde9d7722a1872588c3aea19d80a1 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Thu, 28 Jul 2022 12:09:54 +0000 Subject: [PATCH 032/110] Fix ci errors --- tests/python/frontend/tensorflow/test_forward.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/python/frontend/tensorflow/test_forward.py b/tests/python/frontend/tensorflow/test_forward.py index faa9ec82f698..deb5df7e2651 100644 --- a/tests/python/frontend/tensorflow/test_forward.py +++ b/tests/python/frontend/tensorflow/test_forward.py @@ -53,6 +53,7 @@ from tensorflow.python.framework import ops from tensorflow.python.framework import dtypes from tensorflow.python.ops import gen_functional_ops +from tensorflow.python.client import device_lib try: import tensorflow.compat.v1 as tf @@ -5109,7 +5110,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"], "f{name}:0") + compare_tf_with_tvm([data], ["in_data:0"], f"{name}:0") def test_forward_isinf(): From 9e8a4684e53595762d2f3728071a729935e30765 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Thu, 28 Jul 2022 13:32:12 +0000 Subject: [PATCH 033/110] reformatted by black --- tests/python/frontend/onnx/test_forward.py | 92 +++++++++++++++++++++- 1 file changed, 89 insertions(+), 3 deletions(-) diff --git a/tests/python/frontend/onnx/test_forward.py b/tests/python/frontend/onnx/test_forward.py index dba65a939513..c14b373fe04f 100644 --- a/tests/python/frontend/onnx/test_forward.py +++ b/tests/python/frontend/onnx/test_forward.py @@ -246,6 +246,7 @@ def quantize_and_verify_with_ort( ): """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): @@ -267,7 +268,7 @@ def get_next(self): model_fp32 = os.path.join(t_dir.temp_dir, "model.onnx") onnx.save_model(onnx_model, model_fp32) model_quant = os.path.join(t_dir.temp_dir, "model.quant.onnx") - _ = quantize_static( # pylint: disable=assignment-from-no-return + _ = quantize_static( # pylint: disable=assignment-from-no-return model_fp32, model_quant, RandomDataReader() ) # opt_level=1 will cause error with qnn lowering @@ -365,6 +366,7 @@ 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": @@ -432,6 +434,7 @@ def _test_expand(name, data, shape, ref_data, dtype="int32"): @tvm.testing.parametrize_targets def test_depth_to_space(target, dev): """test_depth_to_space""" + def verify_depth_to_space(inshape, outshape, mode, block_size): node = onnx.helper.make_node( "DepthToSpace", inputs=["x"], outputs=["y"], block_size=block_size @@ -457,6 +460,7 @@ def verify_depth_to_space(inshape, outshape, mode, block_size): @tvm.testing.parametrize_targets def test_space_to_depth(target, dev): """test_space_to_depth""" + def verify_space_to_depth(inshape, outshape, block_size): node = onnx.helper.make_node( "SpaceToDepth", inputs=["x"], outputs=["y"], block_size=block_size @@ -515,6 +519,7 @@ 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] @@ -549,6 +554,7 @@ 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, @@ -585,6 +591,7 @@ 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) @@ -607,6 +614,7 @@ 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) @@ -646,6 +654,7 @@ 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") @@ -728,6 +737,7 @@ 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") @@ -763,6 +773,7 @@ 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") @@ -796,6 +807,7 @@ 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) @@ -1141,6 +1153,7 @@ def test_isnan(target, dev): @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") @@ -1221,6 +1234,7 @@ 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) @@ -1273,6 +1287,7 @@ 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]] @@ -1304,6 +1319,7 @@ 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") @@ -1387,6 +1403,7 @@ 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" @@ -1428,6 +1445,7 @@ 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") @@ -1480,6 +1498,7 @@ 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) @@ -1509,6 +1528,7 @@ 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) @@ -1657,6 +1677,7 @@ def test_upsample3d_trilinear(target, dev): @tvm.testing.parametrize_targets def test_softmax(target, dev): """test_softmax""" + def verify_softmax(inshape, axis, opset=None, dynamic=False): opname = "Softmax" outshape = inshape @@ -1708,6 +1729,7 @@ 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" @@ -1738,6 +1760,7 @@ 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" @@ -1768,6 +1791,7 @@ 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" @@ -1798,6 +1822,7 @@ 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" @@ -1826,6 +1851,7 @@ def verify_hardsigmoid(input_dim, alpha, beta): @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) @@ -1870,6 +1896,7 @@ 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", @@ -1905,6 +1932,7 @@ 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 @@ -2007,6 +2035,7 @@ 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 @@ -2083,6 +2112,7 @@ 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] @@ -2320,6 +2350,7 @@ 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"]) @@ -2446,6 +2477,7 @@ def sign_x(x): @tvm.testing.parametrize_targets def test_not(target, dev): """test_not""" + def verify_not(indata, dtype): x = indata.astype(dtype) @@ -2476,6 +2508,7 @@ 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) @@ -2529,6 +2562,7 @@ 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( @@ -2555,6 +2589,7 @@ def verify_tile_v6(indata, repeats, outdata): @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( @@ -2574,6 +2609,7 @@ def verify_erf(indata, outdata): @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"] @@ -2642,6 +2678,7 @@ 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) @@ -2695,6 +2732,7 @@ 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"] @@ -2728,6 +2766,7 @@ 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( @@ -2762,6 +2801,7 @@ 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, @@ -2943,6 +2983,7 @@ def repeat(num, dims): @tvm.testing.parametrize_targets def test_convtranspose(target, dev): """test_convtranspose""" + def verify_convtranspose_with_output_shape( x_shape, w_shape, @@ -3174,6 +3215,7 @@ def repeat(num, dims): @tvm.testing.parametrize_targets def test_unsqueeze_constant(target, dev): """test_unsqueeze_constant""" + class Flatten(Module): def forward(self, input_): return input_.view(input_.size(0), -1) @@ -3192,6 +3234,7 @@ 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"): _ = np.random.uniform(size=x_shape).astype("float32") @@ -3318,6 +3361,7 @@ 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) @@ -3364,6 +3408,7 @@ def verify_global_pooling(x_shape, mode): @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" ): @@ -3477,6 +3522,7 @@ 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) @@ -3512,6 +3558,7 @@ 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) @@ -3563,6 +3610,7 @@ 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") @@ -3595,6 +3643,7 @@ 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( @@ -3641,7 +3690,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""" + """test_lppool""" + def verify_lppool(x_shape, kernel_shape, p, strides, pads, out_shape, auto_pad="NOTSET"): kwargs = {} if p is not None: @@ -4264,6 +4314,7 @@ 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,), []), @@ -4412,6 +4463,7 @@ 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", @@ -4444,6 +4496,7 @@ def verify_nonzero(indata, outdata, dtype): @tvm.testing.parametrize_targets def test_topk(target, dev): """test_topk""" + def verify_topk(input_dims, k, axis=-1): output_dims = list(input_dims) output_dims[axis] = k @@ -4491,6 +4544,7 @@ 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, @@ -4561,6 +4615,7 @@ 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 ): @@ -4655,6 +4710,7 @@ 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]) @@ -4893,6 +4949,7 @@ 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. @@ -4956,7 +5013,10 @@ def append_constant_nodes(nodes, outputs, expected, name): tvm_out = [tvm_out] for i, _ in enumerate(tvm_out): tvm.testing.assert_allclose( - correct_out[i], tvm_out[i], rtol=1e-05, atol=1e-05 # pylint: disable=unnecessary-list-index-lookup + 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. @@ -4969,6 +5029,7 @@ 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", @@ -4999,6 +5060,7 @@ 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 = [ @@ -5070,6 +5132,7 @@ def verify_maxunpool(data, indices, kernel_shape, strides, output_shape=None, pa @tvm.testing.parametrize_targets def test_softplus(target, dev): """test_softplus""" + def verify_softplus(indata): node = helper.make_node( "Softplus", @@ -5101,6 +5164,7 @@ def verify_softplus(indata): @tvm.testing.parametrize_targets def test_cumsum(target, dev): """test_cumsum""" + def verify_cumsum(indata, axis, exclusive=0, reverse=0, dtype="float32"): cumsum_node = onnx.helper.make_node( "CumSum", @@ -5181,6 +5245,7 @@ def verify_cumsum(indata, axis, exclusive=0, reverse=0, dtype="float32"): @tvm.testing.parametrize_targets def test_eyelike(target, dev): """test_eyelike""" + def verify_eyelike(indata, dynamic=False): node_list = [] eyelike_inputs = ["X"] @@ -5490,6 +5555,7 @@ def verify_embedding_bag(num_embedding, embedding_dim, data_shape, num_bags=None @tvm.testing.parametrize_targets def test_index_put(target, dev): """test_index_put""" + class IndexPutModel(torch.nn.Module): def __init__(self, indices, values, accumulate): super().__init__() @@ -5562,6 +5628,7 @@ 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", @@ -5600,6 +5667,7 @@ 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", @@ -5627,6 +5695,7 @@ 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", @@ -5660,6 +5729,7 @@ def verify_biasgelu(x, bias): @tvm.testing.parametrize_targets def test_embedlayernormalization(target, dev): """test_embedlayernormalization""" + def verify_embedlayernormalization( input_ids, segment_ids, @@ -5769,6 +5839,7 @@ def verify_embedlayernormalization( @tvm.testing.parametrize_targets def test_attention(target, dev): """test_attention""" + def verify_attention(input_, weight, bias, mask_index, num_heads): node = onnx.helper.make_node( "Attention", @@ -5831,6 +5902,7 @@ def verify_attention(input_, weight, bias, mask_index, num_heads): @tvm.testing.parametrize_targets def test_skiplayernormalization(target, dev): """test_skiplayernormalization""" + def verify_skiplayernormalization(input_, skip, gamma, beta, bias): node = onnx.helper.make_node( "SkipLayerNormalization", @@ -5879,6 +5951,7 @@ def verify_skiplayernormalization(input_, skip, gamma, beta, bias): @tvm.testing.parametrize_targets def test_qlinearconv(target, dev): """test_qlinearconv""" + def verify_qlinearconv( x_shape, w_shape, @@ -6095,6 +6168,7 @@ def repeat(num, dims): @tvm.testing.parametrize_targets def test_qlinearconcat(target, dev): """test_qlinearconcat""" + def verify_qlinearconcat(shapes, out_shape, axis=None): input_names = [] input_values = [] @@ -6128,6 +6202,7 @@ 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): _ = np.random.random(a_shape).astype("float32") @@ -6160,6 +6235,7 @@ 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): _ = np.random.random(a_shape).astype("float32") @@ -6194,6 +6270,7 @@ def verify_qlinearmul(a_shape, b_shape, c_shape): @tvm.testing.parametrize_targets def test_qlinearleakyrelu(target, dev): """test_qlinearleakyrelu""" + def verify_qlinearleakyrelu(inshape, kwargs): in_array = np.random.random(inshape).astype("float32") @@ -6221,6 +6298,7 @@ def verify_qlinearleakyrelu(inshape, kwargs): @tvm.testing.parametrize_targets def test_qlinearsigmoid(target, dev): """test_qlinearsigmoid""" + def verify_qlinearsigmoid(a_shape): _ = np.random.random(a_shape).astype("float32") @@ -6246,6 +6324,7 @@ def verify_qlinearsigmoid(a_shape): @tvm.testing.parametrize_targets 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( @@ -6305,6 +6384,7 @@ def get_random_uniform(shape, dtype="float32", high=1.0, low=0.0, seed=None): @tvm.testing.parametrize_targets def test_random_uniform_like(target, dev): """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: @@ -6374,6 +6454,7 @@ def get_random_uniform_like(input_, shape, dtype=None, high=1.0, low=0.0, seed=N @tvm.testing.parametrize_targets 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( @@ -6413,6 +6494,7 @@ def get_random_normal(shape, dtype="float32", scale=1.0, mean=0.0, seed=None): @tvm.testing.parametrize_targets def test_random_normal_like(target, dev): """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( @@ -6453,6 +6535,7 @@ def get_random_normal_like(input_, shape, dtype="float32", scale=1.0, mean=0.0, @tvm.testing.parametrize_targets def test_convinteger(target, dev): """test_convinteger""" + def verify_convinteger( x_shape, w_shape, @@ -6606,6 +6689,7 @@ def repeat(num, dims): @tvm.testing.parametrize_targets def test_scan(target, dev): """test_scan""" + def verify_scan( input_shapes, output_shapes, @@ -6769,6 +6853,7 @@ def verify_scan( @tvm.testing.parametrize_targets 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) @@ -6813,6 +6898,7 @@ def verify_linear_regressor(a_shape, c_shape, i_shape, targets=1, batch=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 = [] From 93c0494aa2f265b12ecc5023055b394ecd0b9033 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Fri, 29 Jul 2022 02:17:32 +0000 Subject: [PATCH 034/110] Fix ci errors --- tests/python/frontend/onnx/test_forward.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/python/frontend/onnx/test_forward.py b/tests/python/frontend/onnx/test_forward.py index c14b373fe04f..74a2b1b9cdfc 100644 --- a/tests/python/frontend/onnx/test_forward.py +++ b/tests/python/frontend/onnx/test_forward.py @@ -437,7 +437,7 @@ def test_depth_to_space(target, dev): def verify_depth_to_space(inshape, outshape, mode, block_size): node = onnx.helper.make_node( - "DepthToSpace", inputs=["x"], outputs=["y"], block_size=block_size + "DepthToSpace", inputs=["x"], outputs=["y"], blocksize=block_size ) graph = helper.make_graph( @@ -463,7 +463,7 @@ def test_space_to_depth(target, dev): def verify_space_to_depth(inshape, outshape, block_size): node = onnx.helper.make_node( - "SpaceToDepth", inputs=["x"], outputs=["y"], block_size=block_size + "SpaceToDepth", inputs=["x"], outputs=["y"], blocksize=block_size ) graph = helper.make_graph( @@ -3204,7 +3204,7 @@ def repeat(num, dims): verify_convtranspose_with_output_shape( (1, 1) + repeat(32, dims), (1, 1) + repeat(4, dims), - repeat(N, dims), + repeat(num, dims), repeat(4, dims), repeat(2, dims), repeat(1, dims), From c6e938b06e23e2aa32d25cf1f9094122cb0cb68d Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Fri, 29 Jul 2022 02:43:05 +0000 Subject: [PATCH 035/110] Fix invalid-name/unused-variable,redefined-builtin --- tests/python/frontend/tflite/test_forward.py | 226 ++++++++++--------- 1 file changed, 115 insertions(+), 111 deletions(-) diff --git a/tests/python/frontend/tflite/test_forward.py b/tests/python/frontend/tflite/test_forward.py index bc5ac2e4fe4f..67751a30ea16 100644 --- a/tests/python/frontend/tflite/test_forward.py +++ b/tests/python/frontend/tflite/test_forward.py @@ -14,8 +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, unused-variable -# pylint: disable=redefined-builtin, no-else-return, inconsistent-return-statements, import-outside-toplevel +# pylint: disable=unused-argument, disable=import-outside-toplevel, inconsistent-return-statements """ TFLite testcases ================ @@ -34,6 +33,8 @@ from packaging import version as package_version from tvm.contrib.download import download_testdata from tvm import relay +from tvm.contrib import graph_executor +from tflite.BuiltinOperator import BuiltinOperator import tvm import tvm.relay.testing.tf as tf_testing @@ -116,31 +117,31 @@ def get_real_image_object_detection(im_height, im_width): return data -def vmobj_to_list(o): +def vmobj_to_list(obj): """Converts TVM objects returned by VM execution to Python List.""" - if isinstance(o, tvm.nd.NDArray): - return [o.numpy().tolist()] - elif isinstance(o, tvm.runtime.container.ADT): + 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( @@ -199,17 +200,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 @@ -236,18 +237,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) @@ -263,22 +263,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 enumerate(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 enumerate(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 enumerate(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 @@ -292,7 +292,7 @@ def compare_tflite_with_tvm( out_names=None, quantized=False, input_range=None, - mode="graph_executor", + mode="vm", experimental_new_converter=False, fp16_quantized=False, int_quant_dtype=tf.int8, @@ -330,7 +330,7 @@ def compare_tflite_with_tvm( try: quant_scale = 255 / (input_range[i][1] - input_range[i][0]) except ZeroDivisionError: - raise ZeroDivisionError( + print( "Min and max of the input range for tensor " + i + " can't be equal" ) mean = -input_range[i][0] * quant_scale @@ -344,9 +344,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( @@ -365,11 +365,11 @@ def compare_tflite_with_tvm( if quantized and not fp16_quantized: 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], tvm_output[i], atol=1, rtol=1e-5) # pylint: disable=unnecessary-list-index-lookup else: 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], tvm_output[i], atol=1e-5, rtol=1e-5 # pylint: disable=unnecessary-list-index-lookup ) @@ -385,7 +385,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): @@ -513,11 +513,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(): @@ -719,15 +719,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(): @@ -915,7 +915,7 @@ def _test_tflite2_quantized_convolution( """One iteration of TFLite2 quantized convolution with given shapes and attributes""" 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( @@ -929,7 +929,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( @@ -948,8 +948,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() @@ -1026,7 +1026,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( @@ -1046,8 +1046,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() @@ -1741,7 +1741,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( @@ -1835,8 +1834,8 @@ 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( list(np.nditer(data)), @@ -1864,11 +1863,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]) @@ -1933,9 +1932,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.") @@ -2635,14 +2634,14 @@ def test_forward_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) @@ -2944,7 +2943,7 @@ 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 @@ -2961,17 +2960,17 @@ def test_forward_select(): @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]] ) -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 @@ -3021,7 +3020,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) @@ -3048,7 +3047,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) @@ -3351,8 +3350,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( @@ -3366,12 +3365,14 @@ def _test_expand_dims(input_shape, input_type, axis, quantized=False): [input], ["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(): @@ -3440,17 +3441,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 ) @@ -3973,12 +3974,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], ) @@ -4158,13 +4161,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(): @@ -4187,8 +4190,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" ) @@ -4205,7 +4208,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], @@ -4213,10 +4216,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" ) @@ -4224,7 +4229,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] ) @@ -4308,10 +4313,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 @@ -4352,9 +4357,8 @@ def test_custom_op_converter(): 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} @@ -4884,10 +4888,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 @@ -4952,10 +4956,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 @@ -5023,7 +5027,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( From b122e38b38f2bab761eb96307bbed4fb9c45e861 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Fri, 29 Jul 2022 06:35:36 +0000 Subject: [PATCH 036/110] [CI] Apply linting rules to caffe tests --- tests/python/frontend/caffe/test_forward.py | 26 ++++++++++----------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/tests/python/frontend/caffe/test_forward.py b/tests/python/frontend/caffe/test_forward.py index 4199e27df7ed..5dc21387ba64 100644 --- a/tests/python/frontend/caffe/test_forward.py +++ b/tests/python/frontend/caffe/test_forward.py @@ -89,7 +89,7 @@ def _gen_filename_str(op_name, data_shape, *args, **kwargs): def _save_prototxt(n_netspec, f_path): """Generate .prototxt file according to caffe.NetSpec""" s = n_netspec.to_proto() - with open(f_path, "w") as f: + with open(f_path, "wb") as f: f.write(str(s)) @@ -109,7 +109,7 @@ def _save_solver(solver_file, proto_file, blob_file): s.snapshot = 100000 s.snapshot_prefix = blob_file_prefix - with open(solver_file, "w") as f: + with open(solver_file, "wb") as f: f.write(str(s)) @@ -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() @@ -181,14 +181,14 @@ def _run_tvm(data, proto_file, blob_file): predict_net = pb.NetParameter() # load model - with open(proto_file, "r") as f: + with open(proto_file, "rb") as f: text_format.Merge(f.read(), predict_net) # load blob 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()) @@ -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) @@ -1084,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") @@ -1145,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") From 3828f7b13e115b263a1bf989f7a9108bd7266ccb Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Fri, 29 Jul 2022 06:36:00 +0000 Subject: [PATCH 037/110] [CI] Apply linting rules to darknet tests --- tests/python/frontend/darknet/test_forward.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/python/frontend/darknet/test_forward.py b/tests/python/frontend/darknet/test_forward.py index f8a30a4e10cb..ed3d86710a5c 100644 --- a/tests/python/frontend/darknet/test_forward.py +++ b/tests/python/frontend/darknet/test_forward.py @@ -31,7 +31,7 @@ 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" @@ -171,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 From bd124105c403f5ce80b48e686c5eb9ff707aa615 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Fri, 29 Jul 2022 06:36:16 +0000 Subject: [PATCH 038/110] [CI] Apply linting rules to pytorch tests --- tests/python/frontend/pytorch/test_forward.py | 211 +++++++++++++++++- 1 file changed, 209 insertions(+), 2 deletions(-) diff --git a/tests/python/frontend/pytorch/test_forward.py b/tests/python/frontend/pytorch/test_forward.py index 87106dd8ad48..a74e89a412e1 100644 --- a/tests/python/frontend/pytorch/test_forward.py +++ b/tests/python/frontend/pytorch/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, missing-docstring +# pylint: disable=import-self, invalid-name, unused-argument """Unit tests for various models and operators""" import os import sys @@ -42,7 +42,9 @@ def list_ops(expr): + """list_ops""" class OpLister(tvm.relay.ExprVisitor): + """OpLister inherits from ExprVisitor""" def visit_op(self, op): if op not in self.node_set: self.node_list.append(op) @@ -58,6 +60,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)) @@ -237,7 +240,7 @@ def verify_model_with_input( # Single operator tests @tvm.testing.uses_gpu def test_forward_pixel_shuffle(): - """PixelShuffle""" + """test_forward_pixel_shuffle""" torch.set_grad_enabled(False) input_shape = [1, 144, 16, 16] @@ -249,6 +252,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] @@ -283,6 +287,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_subtract(): + """test_forward_subtract""" torch.set_grad_enabled(False) input_shape = [10] @@ -317,6 +322,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_multiply(): + """test_forward_multiply""" torch.set_grad_enabled(False) input_shape = [10] @@ -351,6 +357,7 @@ 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) @@ -401,6 +408,7 @@ 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) @@ -417,6 +425,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] @@ -430,6 +439,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] @@ -453,6 +463,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] @@ -481,6 +492,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] @@ -502,6 +514,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] @@ -520,6 +533,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_arange(): + """test_forward_arange""" torch.set_grad_enabled(False) class Arange1(Module): @@ -596,6 +610,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): @@ -618,6 +633,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] @@ -631,6 +647,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] @@ -652,6 +669,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() @@ -660,6 +678,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() @@ -672,6 +691,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() @@ -685,6 +705,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() @@ -696,6 +717,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() @@ -707,6 +729,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() @@ -715,6 +738,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() @@ -723,6 +747,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() @@ -731,6 +756,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() @@ -739,6 +765,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() @@ -749,6 +776,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() @@ -757,6 +785,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() @@ -765,6 +794,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() @@ -778,6 +808,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() @@ -791,6 +822,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() @@ -830,6 +862,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() @@ -849,6 +882,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() @@ -868,6 +902,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_split(): + """test_forward_split""" torch.set_grad_enabled(False) input_shape = [4, 10] @@ -889,6 +924,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] @@ -906,6 +942,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] @@ -932,6 +969,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] @@ -949,6 +987,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() @@ -957,6 +996,7 @@ 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] @@ -1042,6 +1082,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 @@ -1094,10 +1135,13 @@ 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 @@ -1126,6 +1170,7 @@ def forward(self, x, w, s): def test_forward_deform_conv(): + """test_forward_deform_conv""" torch.set_grad_enabled(False) def test_run( @@ -1206,6 +1251,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() @@ -1214,6 +1260,7 @@ def test_forward_threshold(): @tvm.testing.uses_gpu def test_forward_contiguous(): + """test_forward_contiguous""" torch.set_grad_enabled(False) input_shape = [10] @@ -1227,6 +1274,7 @@ 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) @@ -1241,6 +1289,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)) @@ -1253,6 +1302,7 @@ 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) @@ -1266,6 +1316,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() @@ -1288,6 +1339,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] @@ -1313,6 +1365,7 @@ 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) @@ -1323,6 +1376,7 @@ 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) @@ -1349,6 +1403,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] @@ -1372,6 +1427,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] @@ -1384,6 +1440,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] @@ -1397,6 +1454,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] @@ -1436,6 +1494,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] @@ -1460,6 +1519,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] @@ -1492,6 +1552,7 @@ def forward(self, index): @tvm.testing.uses_gpu def test_forward_clone(): + """test_forward_clone""" torch.set_grad_enabled(False) input_shape = [10] @@ -1505,6 +1566,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_gather(): + """test_forward_gather""" torch.set_grad_enabled(False) class Gather1(Module): @@ -1548,6 +1610,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] @@ -1561,6 +1624,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] @@ -1619,6 +1683,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] @@ -1647,6 +1712,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() @@ -1655,6 +1721,7 @@ 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] @@ -1688,6 +1755,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_linear(): + """test_forward_linear""" torch.set_grad_enabled(False) class Linear(Module): @@ -1735,6 +1803,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() @@ -1746,6 +1815,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] @@ -1790,6 +1860,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] @@ -1815,6 +1886,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] @@ -1828,6 +1900,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_expand(): + """test_forward_expand""" torch.set_grad_enabled(False) class Expand1(Module): @@ -1849,6 +1922,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): @@ -1871,6 +1945,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] @@ -1884,6 +1959,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] @@ -1898,6 +1974,7 @@ 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__() @@ -1968,6 +2045,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) @@ -1980,6 +2058,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) @@ -2000,6 +2079,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) @@ -2007,6 +2087,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) @@ -2016,6 +2097,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) @@ -2023,6 +2105,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) @@ -2030,6 +2113,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) @@ -2040,6 +2124,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) @@ -2050,6 +2135,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) @@ -2060,6 +2146,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) @@ -2070,6 +2157,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) @@ -2080,6 +2168,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) @@ -2149,6 +2238,7 @@ 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) @@ -2160,6 +2250,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( @@ -2190,48 +2281,56 @@ 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) @@ -2258,6 +2357,7 @@ 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) @@ -2293,6 +2393,7 @@ 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__() @@ -2313,22 +2414,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) @@ -2364,6 +2469,7 @@ def convert_pt_to_tvm_type(idtype): # pylint: disable=dangerous-default-value def verify_model_vm(input_model, ishapes, idtype=None, idata=None, targets=["llvm"]): + """verify_model_vm""" if not idtype: idtype = torch.float @@ -2425,7 +2531,9 @@ 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)) @@ -2438,11 +2546,13 @@ 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 @@ -2457,7 +2567,9 @@ 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 @@ -2479,6 +2591,7 @@ def forward(self, inp): return a class LoopWithIf(torch.nn.Module): + """LoopWithIf module""" def forward(self, inp): a = inp for _ in range(inp.size(0)): @@ -2500,7 +2613,9 @@ 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): @@ -2539,6 +2654,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): @@ -2559,6 +2675,7 @@ 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) @@ -2577,6 +2694,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] @@ -2610,6 +2728,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] @@ -2633,6 +2752,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] @@ -2656,6 +2776,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] @@ -2679,6 +2800,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] @@ -2732,6 +2854,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] @@ -2785,6 +2908,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] @@ -2838,6 +2962,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_rsub(): + """test_forward_rsub""" torch.set_grad_enabled(False) class Rsub1(Module): @@ -2866,6 +2991,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() @@ -2880,6 +3006,7 @@ def test_forward_embedding(): @tvm.testing.uses_gpu def test_forward_onehot(): + """test_forward_onehot""" torch.set_grad_enabled(False) class OneHot1(Module): @@ -2899,6 +3026,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_isfinite(): + """test_forward_isfinite""" torch.set_grad_enabled(False) class IsFinite1(Module): @@ -2911,6 +3039,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_isnan(): + """test_forward_isnan""" torch.set_grad_enabled(False) class IsNan1(Module): @@ -2923,6 +3052,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_isinf(): + """test_forward_isinf""" torch.set_grad_enabled(False) class IsInf1(Module): @@ -2935,6 +3065,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] @@ -2969,6 +3100,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_clamp_(): + """test_forward_clamp_""" torch.set_grad_enabled(False) class ClampInPlace(Module): @@ -2987,6 +3119,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_ones(): + """test_forward_ones""" torch.set_grad_enabled(False) class Ones1(Module): @@ -2998,6 +3131,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] @@ -3021,6 +3155,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] @@ -3032,6 +3167,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): @@ -3043,6 +3179,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] @@ -3066,6 +3203,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_full(): + """test_forward_full""" torch.set_grad_enabled(False) class Full1(Module): @@ -3082,6 +3220,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] @@ -3105,6 +3244,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] @@ -3123,6 +3263,7 @@ def test_func(x): @tvm.testing.uses_gpu def test_forward_linspace(): + """test_forward_linspace""" torch.set_grad_enabled(False) class Linspace1(Module): @@ -3169,6 +3310,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_take(): + """test_forward_take""" torch.set_grad_enabled(False) class Take1(Module): @@ -3192,6 +3334,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_topk(): + """test_forward_topk""" torch.set_grad_enabled(False) class Topk1(Module): @@ -3230,6 +3373,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): @@ -3251,6 +3395,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): @@ -3269,6 +3414,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): @@ -3296,6 +3442,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): @@ -3323,6 +3470,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_unary(): + """test_forward_unary""" torch.set_grad_enabled(False) class Sqrt1(Module): @@ -3451,6 +3599,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): @@ -3480,6 +3629,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): @@ -3509,6 +3659,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): @@ -3539,6 +3690,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_addcdiv(): + """test_forward_addcdiv""" torch.set_grad_enabled(False) class Addcdiv1(Module): @@ -3563,6 +3715,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_addcmul(): + """test_forward_addcmul""" torch.set_grad_enabled(False) class Addcmul1(Module): @@ -3587,6 +3740,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) @@ -3609,6 +3763,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): @@ -3632,6 +3787,7 @@ def forward(self, arg): @tvm.testing.uses_gpu def test_forward_traced_function(): + """test_forward_traced_function""" def fn(t1, t2): return t1 + t2 @@ -3642,6 +3798,7 @@ def fn(t1, t2): @tvm.testing.uses_gpu def test_forward_dtypes(): + """test_forward_dtypes""" def fn(t1, t2): return 2.5 * t1 + t2 @@ -3673,6 +3830,7 @@ def test_weight_names(): @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 @@ -3691,6 +3849,7 @@ def forward(self, x): @tvm.testing.uses_gpu def test_forward_matmul(): + """test_forward_matmul""" torch.set_grad_enabled(False) class MatMul1(Module): @@ -3736,6 +3895,7 @@ def forward(self, *args): def test_forward_index(): + """test_forward_index""" torch.set_grad_enabled(False) input_shape = [3, 4, 5, 6] @@ -3755,6 +3915,7 @@ def forward(self, x): def test_logsumexp(): + """test_logsumexp""" class Logsumexp(Module): def __init__(self, dim, keepdim=False): super().__init__() @@ -3774,6 +3935,7 @@ def forward(self, x): def test_stack(): + """test_stack""" class Stack(torch.nn.Module): def __init__(self, axis=0): super().__init__() @@ -3790,6 +3952,7 @@ def forward(self, x): def test_stack_dynamic(): + """test_stack_dynamic""" class Stack(torch.nn.Module): def forward(self, x): tensor_list = [] @@ -3803,6 +3966,7 @@ def forward(self, x): def test_forward_unbind(): + """test_forward_unbind""" class Unbind(torch.nn.Module): def __init__(self, axis=0): super().__init__() @@ -3818,6 +3982,7 @@ def forward(self, x): def test_forward_nonzero(): + """test_forward_nonzero""" class Nonzero(Module): def __init__(self, as_tuple=False): super().__init__() @@ -3831,6 +3996,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) @@ -3855,6 +4021,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( @@ -3887,6 +4054,7 @@ def test_fn_index_put3a(): def test_numel(): + """test_numel""" class Numel(Module): def forward(self, data): return torch.tensor(torch.numel(data)) @@ -3898,6 +4066,7 @@ def forward(self, data): def test_empty(): + """Test for aten::empty""" def test_func(): return torch.empty([1, 3, 10, 10]) @@ -3905,6 +4074,7 @@ def test_func(): def test_empty_like(): + """Test for aten::empty_like""" def test_func(data): return torch.empty_like(data) @@ -3912,6 +4082,7 @@ def test_func(data): def test_randn(): + """Test for aten::randn""" def test_func(): return torch.randn([1, 3, 10, 10]) @@ -4065,6 +4236,7 @@ def test_forward_pretrained_bert_base_uncased(): 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 @@ -4102,6 +4274,7 @@ def expected(x_shape, y_shape): def test_bincount(): + """test_bincount""" def test_fn(x, weights=None): return torch.bincount(x, weights=weights) @@ -4114,6 +4287,7 @@ 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_data in examples: verify_model(torch.nn.Hardswish().eval(), input_data=input_data) @@ -4121,6 +4295,7 @@ def test_hard_swish(): 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_data in examples: verify_model(torch.nn.Hardsigmoid().eval(), input_data=input_data) @@ -4128,6 +4303,7 @@ def test_hard_sigmoid(): def test_cumsum(): + """test_cumsum""" def test_fn(dim, dtype=None): return lambda x: torch.cumsum(x, dim=dim, dtype=dtype) @@ -4145,6 +4321,7 @@ 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) @@ -4154,6 +4331,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)) @@ -4162,6 +4340,7 @@ def test_transformer(): def test_argsort(): + """test_argsort""" def test_fn(dim, descending): return lambda x: torch.argsort(x, dim=dim, descending=descending) @@ -4177,6 +4356,7 @@ 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) @@ -4192,6 +4372,7 @@ def test_fn(dim, descending): def test_logical_and(): + """test_logical_and""" def test_fn(x, y): return torch.logical_and(x, y) @@ -4205,6 +4386,7 @@ def test_fn(x, y): def test_masked_select(): + """test_masked_select""" def test_fn(x, mask): return torch.masked_select(x, mask) @@ -4215,6 +4397,7 @@ 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) @@ -4232,6 +4415,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() @@ -4255,6 +4439,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 @@ -4273,6 +4458,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() @@ -4291,6 +4477,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() @@ -4310,6 +4497,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): @@ -4328,6 +4516,7 @@ def forward(self, x): 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() @@ -4339,6 +4528,7 @@ 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) @@ -4351,6 +4541,7 @@ 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) @@ -4369,6 +4560,7 @@ 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) @@ -4381,6 +4573,7 @@ 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) @@ -4392,6 +4585,7 @@ 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) @@ -4403,6 +4597,7 @@ 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, @@ -4435,6 +4630,7 @@ def test_fn(n_fft, hop_length, win_length, center, pad_mode, normalized, oneside @tvm.testing.uses_gpu def test_dot(): + """Test for aten::dot""" def test_fn(x): return x.dot(x) @@ -4444,6 +4640,7 @@ def test_fn(x): @tvm.testing.uses_gpu def test_mv(): + """Test for aten::mv""" def test_fn(m, v): return m.mv(v) @@ -4453,6 +4650,7 @@ 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__() @@ -4497,7 +4695,9 @@ 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): @@ -4517,6 +4717,7 @@ def forward(self, x): # 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) @@ -4535,6 +4736,7 @@ 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 @@ -4550,6 +4752,7 @@ 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) @@ -4562,8 +4765,10 @@ 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) @@ -4571,6 +4776,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) @@ -4603,6 +4809,7 @@ 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) From 2c05b1250284035b1c54bec6726da7b92a5fcc65 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Fri, 29 Jul 2022 06:36:28 +0000 Subject: [PATCH 039/110] [CI] Apply linting rules to tflite tests --- tests/python/frontend/tflite/test_forward.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/tests/python/frontend/tflite/test_forward.py b/tests/python/frontend/tflite/test_forward.py index 67751a30ea16..c0b94f7eb6ce 100644 --- a/tests/python/frontend/tflite/test_forward.py +++ b/tests/python/frontend/tflite/test_forward.py @@ -330,9 +330,7 @@ def compare_tflite_with_tvm( try: quant_scale = 255 / (input_range[i][1] - input_range[i][0]) except ZeroDivisionError: - print( - "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 @@ -365,11 +363,19 @@ def compare_tflite_with_tvm( if quantized and not fp16_quantized: 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) # pylint: disable=unnecessary-list-index-lookup + 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 enumerate(tflite_output): tvm.testing.assert_allclose( - tflite_output[i], tvm_output[i], atol=1e-5, rtol=1e-5 # pylint: disable=unnecessary-list-index-lookup + tflite_output[i], # pylint: disable=unnecessary-list-index-lookup + tvm_output[i], + atol=1e-5, + rtol=1e-5, ) @@ -4357,6 +4363,7 @@ def test_custom_op_converter(): class DummyOperatorConverter(relay.frontend.tflite.OperatorConverter): """Operator Converter for converting TFLite ops to relay ops""" + def __init__(self, model, subgraph, exp_tab): super().__init__(model, subgraph, exp_tab) self.allow_custom_ops = True From 943965c27140ecdf9afd51db7bd30db79d909f80 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Fri, 29 Jul 2022 06:37:44 +0000 Subject: [PATCH 040/110] reformat by black --- tests/python/frontend/pytorch/test_forward.py | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/tests/python/frontend/pytorch/test_forward.py b/tests/python/frontend/pytorch/test_forward.py index a74e89a412e1..0fb317206225 100644 --- a/tests/python/frontend/pytorch/test_forward.py +++ b/tests/python/frontend/pytorch/test_forward.py @@ -43,8 +43,10 @@ def list_ops(expr): """list_ops""" + class OpLister(tvm.relay.ExprVisitor): """OpLister inherits from ExprVisitor""" + def visit_op(self, op): if op not in self.node_set: self.node_list.append(op) @@ -358,6 +360,7 @@ 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) @@ -409,6 +412,7 @@ 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) @@ -1140,6 +1144,7 @@ def test_forward_conv2d_transpose_group(): class ModulatedConvTranspose2D(torch.nn.Module): """ModulatedConvTranspose2D module""" + def forward(self, x, w, s): """forward""" B, C, H, W = x.shape @@ -1275,6 +1280,7 @@ 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) @@ -1303,6 +1309,7 @@ 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) @@ -1366,6 +1373,7 @@ 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) @@ -1377,6 +1385,7 @@ 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) @@ -1975,6 +1984,7 @@ 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__() @@ -2358,6 +2368,7 @@ 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) @@ -2394,6 +2405,7 @@ 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__() @@ -2532,8 +2544,10 @@ 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)) @@ -2547,6 +2561,7 @@ def forward(self, inp): class NestedIf(torch.nn.Module): """NestedIf module""" + def __init__(self, N, M): super().__init__() self.weight = torch.nn.Parameter(torch.rand(N, M)) @@ -2568,6 +2583,7 @@ def forward(self, inp): class ScalarLoop(torch.nn.Module): """ScalarLoop module""" + def forward(self, inp): """forward""" a = 0 @@ -2592,6 +2608,7 @@ def forward(self, inp): class LoopWithIf(torch.nn.Module): """LoopWithIf module""" + def forward(self, inp): a = inp for _ in range(inp.size(0)): @@ -2614,6 +2631,7 @@ def forward(self, inp): class SimpleScalarWhileLoop(torch.nn.Module): """SimpleScalarWhileLoop module""" + def forward(self, inp): """forward""" a = 1 @@ -2676,6 +2694,7 @@ def forward(self, x, h): class RNNLoop(torch.nn.Module): """Pytorch RNNLoop module""" + def __init__(self): super().__init__() x = torch.rand(10, 4, dtype=torch.float) @@ -3788,6 +3807,7 @@ def forward(self, arg): @tvm.testing.uses_gpu def test_forward_traced_function(): """test_forward_traced_function""" + def fn(t1, t2): return t1 + t2 @@ -3799,6 +3819,7 @@ def fn(t1, t2): @tvm.testing.uses_gpu def test_forward_dtypes(): """test_forward_dtypes""" + def fn(t1, t2): return 2.5 * t1 + t2 @@ -3916,6 +3937,7 @@ def forward(self, x): def test_logsumexp(): """test_logsumexp""" + class Logsumexp(Module): def __init__(self, dim, keepdim=False): super().__init__() @@ -3936,6 +3958,7 @@ def forward(self, x): def test_stack(): """test_stack""" + class Stack(torch.nn.Module): def __init__(self, axis=0): super().__init__() @@ -3953,6 +3976,7 @@ def forward(self, x): def test_stack_dynamic(): """test_stack_dynamic""" + class Stack(torch.nn.Module): def forward(self, x): tensor_list = [] @@ -3967,6 +3991,7 @@ def forward(self, x): def test_forward_unbind(): """test_forward_unbind""" + class Unbind(torch.nn.Module): def __init__(self, axis=0): super().__init__() @@ -3983,6 +4008,7 @@ def forward(self, x): def test_forward_nonzero(): """test_forward_nonzero""" + class Nonzero(Module): def __init__(self, as_tuple=False): super().__init__() @@ -4055,6 +4081,7 @@ def test_fn_index_put3a(): def test_numel(): """test_numel""" + class Numel(Module): def forward(self, data): return torch.tensor(torch.numel(data)) @@ -4067,6 +4094,7 @@ def forward(self, data): def test_empty(): """Test for aten::empty""" + def test_func(): return torch.empty([1, 3, 10, 10]) @@ -4075,6 +4103,7 @@ def test_func(): def test_empty_like(): """Test for aten::empty_like""" + def test_func(data): return torch.empty_like(data) @@ -4083,6 +4112,7 @@ def test_func(data): def test_randn(): """Test for aten::randn""" + def test_func(): return torch.randn([1, 3, 10, 10]) @@ -4237,6 +4267,7 @@ def test_forward_pretrained_bert_base_uncased(): 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 @@ -4275,6 +4306,7 @@ def expected(x_shape, y_shape): def test_bincount(): """test_bincount""" + def test_fn(x, weights=None): return torch.bincount(x, weights=weights) @@ -4304,6 +4336,7 @@ def test_hard_sigmoid(): def test_cumsum(): """test_cumsum""" + def test_fn(dim, dtype=None): return lambda x: torch.cumsum(x, dim=dim, dtype=dtype) @@ -4322,6 +4355,7 @@ 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) @@ -4341,6 +4375,7 @@ def test_transformer(): def test_argsort(): """test_argsort""" + def test_fn(dim, descending): return lambda x: torch.argsort(x, dim=dim, descending=descending) @@ -4357,6 +4392,7 @@ 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) @@ -4373,6 +4409,7 @@ def test_fn(dim, descending): def test_logical_and(): """test_logical_and""" + def test_fn(x, y): return torch.logical_and(x, y) @@ -4387,6 +4424,7 @@ def test_fn(x, y): def test_masked_select(): """test_masked_select""" + def test_fn(x, mask): return torch.masked_select(x, mask) @@ -4398,6 +4436,7 @@ 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) @@ -4529,6 +4568,7 @@ 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) @@ -4542,6 +4582,7 @@ 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) @@ -4561,6 +4602,7 @@ 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) @@ -4574,6 +4616,7 @@ 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) @@ -4586,6 +4629,7 @@ 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) @@ -4598,6 +4642,7 @@ 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, @@ -4631,6 +4676,7 @@ def test_fn(n_fft, hop_length, win_length, center, pad_mode, normalized, oneside @tvm.testing.uses_gpu def test_dot(): """Test for aten::dot""" + def test_fn(x): return x.dot(x) @@ -4641,6 +4687,7 @@ def test_fn(x): @tvm.testing.uses_gpu def test_mv(): """Test for aten::mv""" + def test_fn(m, v): return m.mv(v) @@ -4651,6 +4698,7 @@ 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__() @@ -4696,6 +4744,7 @@ def test_list_tuple(): class List_tuple(Module): """List_tuple""" + def forward(self, x): """forward""" merged = [] @@ -4718,6 +4767,7 @@ def forward(self, x): @tvm.testing.uses_gpu def test_binary_bitwise(): """Test for binary bitwise""" + def test_ior(x, y): return x.__ior__(y) @@ -4737,6 +4787,7 @@ 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 @@ -4753,6 +4804,7 @@ 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) @@ -4769,6 +4821,7 @@ def 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) @@ -4810,6 +4863,7 @@ 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) From a2df6cec66c66994bd26aef7bafa08a0014900e6 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Fri, 29 Jul 2022 07:14:09 +0000 Subject: [PATCH 041/110] [Pylint] Making frontend tests pylint compliant Part 1 of N --- tests/lint/pylint.sh | 13 ++++ tests/python/frontend/caffe/test_forward.py | 42 ++----------- .../frontend/caffe2/model_zoo/__init__.py | 2 +- .../frontend/caffe2/model_zoo/squeezenet.py | 10 ++-- tests/python/frontend/caffe2/test_forward.py | 7 +-- .../frontend/coreml/model_zoo/__init__.py | 1 + tests/python/frontend/coreml/test_forward.py | 33 +++++------ tests/python/frontend/darknet/test_forward.py | 32 ++-------- tests/python/frontend/keras/test_forward.py | 48 +++++++-------- tests/python/frontend/oneflow/test_forward.py | 59 +++++++++++++++---- .../frontend/oneflow/test_vision_models.py | 2 +- .../frontend/tensorflow/test_forward.py | 1 - tests/python/frontend/tflite/test_forward.py | 3 +- 13 files changed, 119 insertions(+), 134 deletions(-) diff --git a/tests/lint/pylint.sh b/tests/lint/pylint.sh index 0ead015f9350..81e6a838ac18 100755 --- a/tests/lint/pylint.sh +++ b/tests/lint/pylint.sh @@ -32,3 +32,16 @@ python3 -m pylint tests/python/contrib/test_hexagon/conv2d/test_conv2d_blocked.p python3 -m pylint tests/python/contrib/test_hexagon/conv2d/test_conv2d_conv2d.py --rcfile="$(dirname "$0")"/pylintrc python3 -m pylint tests/python/contrib/test_hexagon/infrastructure.py --rcfile="$(dirname "$0")"/pylintrc python3 -m pylint tests/python/contrib/test_hexagon/test_2d_physical_buffers.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 5dc21387ba64..f4a41aeeb7d8 100644 --- a/tests/python/frontend/caffe/test_forward.py +++ b/tests/python/frontend/caffe/test_forward.py @@ -89,7 +89,7 @@ def _gen_filename_str(op_name, data_shape, *args, **kwargs): def _save_prototxt(n_netspec, f_path): """Generate .prototxt file according to caffe.NetSpec""" s = n_netspec.to_proto() - with open(f_path, "wb") as f: + with open(f_path, "w") as f: f.write(str(s)) @@ -109,7 +109,7 @@ def _save_solver(solver_file, proto_file, blob_file): s.snapshot = 100000 s.snapshot_prefix = blob_file_prefix - with open(solver_file, "wb") as f: + with open(solver_file, "w") as f: f.write(str(s)) @@ -181,7 +181,7 @@ def _run_tvm(data, proto_file, blob_file): predict_net = pb.NetParameter() # load model - with open(proto_file, "rb") as f: + with open(proto_file, "r") as f: text_format.Merge(f.read(), predict_net) # load blob with open(blob_file, "rb") as f: @@ -1160,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 88849ba20f24..9758d937c254 100644 --- a/tests/python/frontend/caffe2/test_forward.py +++ b/tests/python/frontend/caffe2/test_forward.py @@ -254,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 c137a5d71a05..ea2f3478fde4 100644 --- a/tests/python/frontend/coreml/model_zoo/__init__.py +++ b/tests/python/frontend/coreml/model_zoo/__init__.py @@ -36,6 +36,7 @@ def get_resnet50(): def get_cat_image(): + """Get cat image""" url = ( "https://gist.githubusercontent.com/zhreshold/" + "bcda4716699ac97ea44f791c24310193/raw/fa7ef0e9c9a5daea686d6473a62aacd1a5885849/cat.png" diff --git a/tests/python/frontend/coreml/test_forward.py b/tests/python/frontend/coreml/test_forward.py index 855441fb3cd9..e5e337570c1c 100644 --- a/tests/python/frontend/coreml/test_forward.py +++ b/tests/python/frontend/coreml/test_forward.py @@ -14,7 +14,6 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. -# pylint: disable=invalid-name, redefined-builtin """ CoreML testcases ==================== @@ -225,9 +224,9 @@ def _verify_upsample_layer_params(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, @@ -256,9 +255,9 @@ def _verify_l2_normalize(input_dim, eps): 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) @@ -278,9 +277,9 @@ def _verify_lrn(input_dim, size, bias, alpha, beta): 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", @@ -765,24 +764,24 @@ def test_forward_image_scaler(): ) -def verify_convolution(input_dim, filter, padding): +def verify_convolution(input_dim, filter_, padding): """Verify convolution""" dtype = "float32" - _, 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))] + inputs = [("input1", datatypes.Array(c, h, width))] output = [("output", datatypes.Array(*b_np.shape))] 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(), @@ -802,8 +801,8 @@ 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(): diff --git a/tests/python/frontend/darknet/test_forward.py b/tests/python/frontend/darknet/test_forward.py index ed3d86710a5c..ffaa773fc1bd 100644 --- a/tests/python/frontend/darknet/test_forward.py +++ b/tests/python/frontend/darknet/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=unused-argument, invalid-name +# pylint: disable=unused-argument """ Test Darknet Models =================== @@ -460,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 @@ -475,7 +475,7 @@ def test_forward_activation_logistic(): layer_1 = LIB.make_convolutional_layer( batch, h, - w, + width, c, n, groups, @@ -489,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) @@ -518,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 181c566f9949..2ee451f3d234 100644 --- a/tests/python/frontend/keras/test_forward.py +++ b/tests/python/frontend/keras/test_forward.py @@ -735,30 +735,30 @@ def test_forward_time_distributed(self, keras_mod): if __name__ == "__main__": for k in [keras, tf_keras]: sut = TestKeras() - # 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_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) diff --git a/tests/python/frontend/oneflow/test_forward.py b/tests/python/frontend/oneflow/test_forward.py index 41ca08c7ab5a..5c68985670ea 100644 --- a/tests/python/frontend/oneflow/test_forward.py +++ b/tests/python/frontend/oneflow/test_forward.py @@ -14,8 +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, missing-function-docstring -# pylint: disable=arguments-differ, unused-argument, consider-using-f-string +# pylint: disable=arguments-differ, unused-argument """Unit tests for various models and operators""" import os @@ -38,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): @@ -66,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 @@ -101,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) @@ -123,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() @@ -151,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": @@ -180,6 +182,7 @@ def verify_conv( ), device="llvm", ): + """verify_conv""" if device == "cuda": model.to(device) inputs = inputs.to(device) @@ -209,6 +212,7 @@ def verify_pool( ), device="llvm", ): + """verify_pool""" if device == "cuda": model.to(device) inputs = inputs.to(device) @@ -238,6 +242,7 @@ def verify_normalization( ), device="llvm", ): + """verify_normalization""" if device == "cuda": model.to(device) inputs = inputs.to(device) @@ -268,6 +273,7 @@ def verify_upsample( ), device="llvm", ): + """verify_upsample""" if device == "cuda": model.to(device) inputs = inputs.to(device) @@ -297,6 +303,7 @@ def verify_convtran( ), device="llvm", ): + """verify_convtran""" if device == "cuda": model.to(device) inputs = inputs.to(device) @@ -326,6 +333,7 @@ def verify_activation( ), device="llvm", ): + """verify_activation""" if device == "cuda": model.to(device) inputs = inputs.to(device) @@ -355,6 +363,7 @@ def verify_math( ), device="llvm", ): + """verify_math""" if device == "cuda": model.to(device) inputs = inputs.to(device) @@ -382,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) @@ -410,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) @@ -433,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__() @@ -454,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__() @@ -496,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__() @@ -516,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__() @@ -547,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__() @@ -567,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__() @@ -719,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) @@ -779,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]] @@ -795,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() @@ -808,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) @@ -851,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 f82277f98195..8a573e0f51e3 100644 --- a/tests/python/frontend/oneflow/test_vision_models.py +++ b/tests/python/frontend/oneflow/test_vision_models.py @@ -44,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): diff --git a/tests/python/frontend/tensorflow/test_forward.py b/tests/python/frontend/tensorflow/test_forward.py index deb5df7e2651..37baef1e00d5 100644 --- a/tests/python/frontend/tensorflow/test_forward.py +++ b/tests/python/frontend/tensorflow/test_forward.py @@ -5754,5 +5754,4 @@ def test_invert_permutation(): if __name__ == "__main__": - test_tensor_array_scatter() pytest.main([__file__]) diff --git a/tests/python/frontend/tflite/test_forward.py b/tests/python/frontend/tflite/test_forward.py index c0b94f7eb6ce..011b7d46e203 100644 --- a/tests/python/frontend/tflite/test_forward.py +++ b/tests/python/frontend/tflite/test_forward.py @@ -2964,7 +2964,8 @@ 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_value, max_value, quant_bits): """Fake quant""" From 56697fa5d0b603bd5c8324ff795aa5e2f35c06fd Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Fri, 29 Jul 2022 07:33:57 +0000 Subject: [PATCH 042/110] Fix ci errors --- tests/python/frontend/coreml/test_forward.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/python/frontend/coreml/test_forward.py b/tests/python/frontend/coreml/test_forward.py index e5e337570c1c..d99630f5f865 100644 --- a/tests/python/frontend/coreml/test_forward.py +++ b/tests/python/frontend/coreml/test_forward.py @@ -191,7 +191,7 @@ def verify_concat_layer_params(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" @@ -774,7 +774,7 @@ def verify_convolution(input_dim, filter_, padding): 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, width))] - 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_convolution( name="conv", From 1cae4bdd55caf3f524b69f3f6cc5e59d3564c2cd Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Fri, 29 Jul 2022 08:09:41 +0000 Subject: [PATCH 043/110] Fix invalid-name pylint errors --- tests/python/frontend/coreml/test_forward.py | 24 ++++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/tests/python/frontend/coreml/test_forward.py b/tests/python/frontend/coreml/test_forward.py index d99630f5f865..6ca03eb436ed 100644 --- a/tests/python/frontend/coreml/test_forward.py +++ b/tests/python/frontend/coreml/test_forward.py @@ -582,26 +582,26 @@ def test_forward_reduce(): """Reduce""" class ReduceAxis(Enum): - CHW = 0 - HW = 1 - C = 2 - H = 3 - W = 4 + AXIS_CHW = 0 + AXIS_HW = 1 + AXIS_C = 2 + AXIS_H = 3 + AXIS_W = 4 def _verify_reduce(input_dim, mode, axis, ref_func, dtype="float32"): print(input_dim, mode, axis) a_np = np.random.uniform(size=input_dim).astype(dtype) # translate to axis from coreml format - if axis == ReduceAxis.CHW: + if axis == ReduceAxis.AXIS_CHW: np_axis = (-3, -2, -1) - elif axis == ReduceAxis.HW: + elif axis == ReduceAxis.AXIS_HW: np_axis = (-2, -1) - elif axis == ReduceAxis.C: + elif axis == ReduceAxis.AXIS_C: np_axis = -3 - elif axis == ReduceAxis.H: + elif axis == ReduceAxis.AXIS_H: np_axis = -2 - elif axis == ReduceAxis.W: + elif axis == ReduceAxis.AXIS_W: np_axis = -1 if ref_func is np.argmax: @@ -624,7 +624,7 @@ def _verify_reduce(input_dim, mode, axis, ref_func, dtype="float32"): dshapes = [[10, 10], [1, 10, 10], [1, 3, 10, 10]] for dshape in dshapes: for axis in ReduceAxis: - if len(dshape) < 3 and axis in [ReduceAxis.CHW, ReduceAxis.C]: + if len(dshape) < 3 and axis in [ReduceAxis.AXIS_CHW, ReduceAxis.AXIS_C]: # input must have rank at least 3 continue _verify_reduce(dshape, "sum", axis, np.sum) @@ -632,7 +632,7 @@ def _verify_reduce(input_dim, mode, axis, ref_func, dtype="float32"): _verify_reduce(dshape, "prod", axis, np.prod) _verify_reduce(dshape, "min", axis, np.min) _verify_reduce(dshape, "max", axis, np.max) - if axis in [ReduceAxis.C, ReduceAxis.H, ReduceAxis.W]: + if axis in [ReduceAxis.AXIS_C, ReduceAxis.AXIS_H, ReduceAxis.AXIS_W]: # For mode ArgMax, axis must be [-1] or [-2] or [-3] _verify_reduce(dshape, "argmax", axis, np.argmax, dtype="int32") From 45b2d39d8fce7a11a596d03c6b914aa47fc2f952 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Fri, 29 Jul 2022 08:21:06 +0000 Subject: [PATCH 044/110] Fix invalid-name pylint errors --- tests/python/frontend/keras/test_forward.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/python/frontend/keras/test_forward.py b/tests/python/frontend/keras/test_forward.py index 2ee451f3d234..f39ca0662e5e 100644 --- a/tests/python/frontend/keras/test_forward.py +++ b/tests/python/frontend/keras/test_forward.py @@ -32,15 +32,15 @@ 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): @@ -63,8 +63,8 @@ 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_mod": keras}) -using_tensorflow_keras = ("tf_keras", {"keras_mod": 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"): @@ -131,7 +131,7 @@ def get_mobilenet(keras_mod): class TestKeras: """Keras test""" - scenarios = [using_classic_keras, using_tensorflow_keras] + scenarios = [USING_CALSSIC_KERAS, USING_TENSORFLOW_KERAS] def test_forward_merge(self, keras_mod): """test_forward_merge""" From 611a40e236e3ac62ac34ff6f7371ed48000bdde5 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Fri, 29 Jul 2022 09:21:36 +0000 Subject: [PATCH 045/110] Disabale temporarily --- tests/lint/pylint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/lint/pylint.sh b/tests/lint/pylint.sh index 81e6a838ac18..303d0f9e20b2 100755 --- a/tests/lint/pylint.sh +++ b/tests/lint/pylint.sh @@ -41,7 +41,7 @@ python3 -m pylint tests/python/frontend/coreml/*.py --rcfile="$(dirname "$0")"/p 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/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 From f0f745ddc6dc66926db765cd23cfb610ab3c9c3b Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Fri, 29 Jul 2022 09:35:38 +0000 Subject: [PATCH 046/110] Fix dangerous-default-value --- tests/python/frontend/tensorflow/test_forward.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/python/frontend/tensorflow/test_forward.py b/tests/python/frontend/tensorflow/test_forward.py index 37baef1e00d5..81fd273c1211 100644 --- a/tests/python/frontend/tensorflow/test_forward.py +++ b/tests/python/frontend/tensorflow/test_forward.py @@ -500,10 +500,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 @@ -947,11 +948,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""" - # pylint: disable=dangerous-default-value + 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 From 00efafc000131cb1547d3a1066626a1d3e733d8a Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Fri, 29 Jul 2022 09:45:27 +0000 Subject: [PATCH 047/110] Fix typo errors --- tests/python/frontend/tflite/test_forward.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python/frontend/tflite/test_forward.py b/tests/python/frontend/tflite/test_forward.py index 011b7d46e203..919f15bfb5ea 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=unused-argument, disable=import-outside-toplevel, inconsistent-return-statements +# pylint: disable=unused-argument, import-outside-toplevel, inconsistent-return-statements """ TFLite testcases ================ From 4dcfe092337cd0e35e8fc8fb63c2060d3d0ba1a7 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Fri, 29 Jul 2022 11:45:07 +0000 Subject: [PATCH 048/110] Fix ci errors --- tests/python/frontend/coreml/test_forward.py | 25 ++++++++++---------- tests/python/frontend/tflite/test_forward.py | 7 +++++- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/tests/python/frontend/coreml/test_forward.py b/tests/python/frontend/coreml/test_forward.py index 6ca03eb436ed..75879f3e582a 100644 --- a/tests/python/frontend/coreml/test_forward.py +++ b/tests/python/frontend/coreml/test_forward.py @@ -582,26 +582,27 @@ def test_forward_reduce(): """Reduce""" class ReduceAxis(Enum): - AXIS_CHW = 0 - AXIS_HW = 1 - AXIS_C = 2 - AXIS_H = 3 - AXIS_W = 4 + # pylint: disable=invalid-name + CHW = 0 + HW = 1 + C = 2 + H = 3 + W = 4 def _verify_reduce(input_dim, mode, axis, ref_func, dtype="float32"): print(input_dim, mode, axis) a_np = np.random.uniform(size=input_dim).astype(dtype) # translate to axis from coreml format - if axis == ReduceAxis.AXIS_CHW: + if axis == ReduceAxis.CHW: np_axis = (-3, -2, -1) - elif axis == ReduceAxis.AXIS_HW: + elif axis == ReduceAxis.HW: np_axis = (-2, -1) - elif axis == ReduceAxis.AXIS_C: + elif axis == ReduceAxis.C: np_axis = -3 - elif axis == ReduceAxis.AXIS_H: + elif axis == ReduceAxis.H: np_axis = -2 - elif axis == ReduceAxis.AXIS_W: + elif axis == ReduceAxis.W: np_axis = -1 if ref_func is np.argmax: @@ -624,7 +625,7 @@ def _verify_reduce(input_dim, mode, axis, ref_func, dtype="float32"): dshapes = [[10, 10], [1, 10, 10], [1, 3, 10, 10]] for dshape in dshapes: for axis in ReduceAxis: - if len(dshape) < 3 and axis in [ReduceAxis.AXIS_CHW, ReduceAxis.AXIS_C]: + if len(dshape) < 3 and axis in [ReduceAxis.CHW, ReduceAxis.C]: # input must have rank at least 3 continue _verify_reduce(dshape, "sum", axis, np.sum) @@ -632,7 +633,7 @@ def _verify_reduce(input_dim, mode, axis, ref_func, dtype="float32"): _verify_reduce(dshape, "prod", axis, np.prod) _verify_reduce(dshape, "min", axis, np.min) _verify_reduce(dshape, "max", axis, np.max) - if axis in [ReduceAxis.AXIS_C, ReduceAxis.AXIS_H, ReduceAxis.AXIS_W]: + if axis in [ReduceAxis.C, ReduceAxis.H, ReduceAxis.W]: # For mode ArgMax, axis must be [-1] or [-2] or [-3] _verify_reduce(dshape, "argmax", axis, np.argmax, dtype="int32") diff --git a/tests/python/frontend/tflite/test_forward.py b/tests/python/frontend/tflite/test_forward.py index 919f15bfb5ea..83c470ee72a5 100644 --- a/tests/python/frontend/tflite/test_forward.py +++ b/tests/python/frontend/tflite/test_forward.py @@ -3369,7 +3369,12 @@ 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_array = np.random.uniform(-100, 100, input_shape).astype(input_type) From 9cf4dac4dfedb9de102467ecb07c8715e2db07e7 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Mon, 1 Aug 2022 06:12:58 +0000 Subject: [PATCH 049/110] [CI] Apply linting rules to tensorflow tests --- tests/lint/pylint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/lint/pylint.sh b/tests/lint/pylint.sh index 303d0f9e20b2..81e6a838ac18 100755 --- a/tests/lint/pylint.sh +++ b/tests/lint/pylint.sh @@ -41,7 +41,7 @@ python3 -m pylint tests/python/frontend/coreml/*.py --rcfile="$(dirname "$0")"/p 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/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 From 4ffdb30f2a94c356d530c477aa77f6de42b7b419 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Wed, 3 Aug 2022 03:07:53 +0000 Subject: [PATCH 050/110] Fix dangerous-default-value --- tests/python/frontend/pytorch/test_forward.py | 10 ++++---- .../frontend/tensorflow/test_forward.py | 24 +++++++++---------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/tests/python/frontend/pytorch/test_forward.py b/tests/python/frontend/pytorch/test_forward.py index 0fb317206225..90d4f2c9cf9c 100644 --- a/tests/python/frontend/pytorch/test_forward.py +++ b/tests/python/frontend/pytorch/test_forward.py @@ -124,12 +124,14 @@ def load_model(model_name): raise RuntimeError("Model not supported") -# pylint: disable=dangerous-default-value def verify_model( - model_name, input_data=[], custom_convert_map={}, rtol=1e-5, atol=1e-5, expected_ops=[] + model_name, input_data=None, custom_convert_map=None, rtol=1e-5, atol=1e-5, expected_ops=None ): """Assert that the output of a compiled model matches with that of its baseline.""" + input_data = input_data or [] + 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): @@ -2479,9 +2481,9 @@ def convert_pt_to_tvm_type(idtype): return curr_dtype -# pylint: disable=dangerous-default-value -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 diff --git a/tests/python/frontend/tensorflow/test_forward.py b/tests/python/frontend/tensorflow/test_forward.py index 81fd273c1211..4e19f26e28b1 100644 --- a/tests/python/frontend/tensorflow/test_forward.py +++ b/tests/python/frontend/tensorflow/test_forward.py @@ -4707,22 +4707,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"]): - # pylint: disable=dangerous-default-value, redefined-outer-name - 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) @@ -4751,9 +4751,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"]): - # pylint: disable=dangerous-default-value, redefined-outer-name - 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) @@ -4764,7 +4764,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) From bc0793db5efe3e78efb20e9f5f2ca355c0b73115 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Wed, 3 Aug 2022 03:10:14 +0000 Subject: [PATCH 051/110] Fix ungrouped-imports --- tests/python/frontend/tflite/test_forward.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/python/frontend/tflite/test_forward.py b/tests/python/frontend/tflite/test_forward.py index 83c470ee72a5..573ff39e194d 100644 --- a/tests/python/frontend/tflite/test_forward.py +++ b/tests/python/frontend/tflite/test_forward.py @@ -31,13 +31,14 @@ from PIL import Image from packaging import version as package_version + +import tvm +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 -import tvm -import tvm.relay.testing.tf as tf_testing try: import tensorflow.compat.v1 as tf From 8fd3764a4ed0d48d6095b5bf28de1b242fa6c13c Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Thu, 7 Jul 2022 07:37:04 +0000 Subject: [PATCH 052/110] [CI] Apply linting rules to tf tests --- .../frontend/tensorflow/test_forward.py | 118 ++++++++++++------ 1 file changed, 77 insertions(+), 41 deletions(-) diff --git a/tests/python/frontend/tensorflow/test_forward.py b/tests/python/frontend/tensorflow/test_forward.py index 8c0b32465f11..540caa12914a 100755 --- a/tests/python/frontend/tensorflow/test_forward.py +++ b/tests/python/frontend/tensorflow/test_forward.py @@ -14,18 +14,29 @@ # 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, unused-variable, redefined-builtin """ Tensorflow testcases ==================== 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 numpy as np import pytest +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 + +import tvm +import tvm.relay.testing.tf as tf_testing +import tvm.testing + try: import tensorflow.compat.v1 as tf @@ -36,32 +47,9 @@ # 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() - -from tensorflow.python.framework import constant_op -from tensorflow.python.framework import graph_util -from tensorflow.python.ops import nn_ops -from tensorflow.python.ops import nn -from tensorflow.python.ops import array_ops -from tensorflow.python.ops import math_ops -from tensorflow.python.ops import variable_scope -from tensorflow.python.ops import variables -from tensorflow.python.ops import init_ops -from tensorflow.python.framework import function -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 +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 +76,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): @@ -282,9 +271,9 @@ 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)): + for i, _ in enumerate(tf_output): if not isinstance(tf_output[i], np.ndarray): - assert len(tvm_output[i].shape) == 0 + assert len(tvm_output[i].shape) == 0 # pylint: disable=len-as-condition tvm.testing.assert_allclose(tf_output[i], tvm_output[i], atol=1e-5, rtol=1e-5) sess.close() @@ -295,7 +284,7 @@ def is_gpu_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 len(gpu_list) > 0: # pylint: disable=len-as-condition print("Tensorflow GPU:", gpu_list) return True else: @@ -499,7 +488,7 @@ def _test_convolution( add_shapes_to_graph_def=True, ): """One iteration of convolution with given shapes and attributes""" - + # pylint: disable=dangerous-default-value 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 +559,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") @@ -946,7 +936,7 @@ def _test_convolution3d( add_shapes_to_graph_def=True, ): """One iteration of 3D convolution with given shapes and attributes""" - + # pylint: disable=dangerous-default-value 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 +975,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 +1062,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 +1174,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 +1235,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 +1274,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 +1351,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,6 +1454,8 @@ 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] @@ -1481,6 +1479,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] @@ -1518,6 +1518,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] @@ -1535,6 +1537,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] @@ -1563,6 +1567,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] @@ -1585,6 +1591,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") @@ -1608,6 +1615,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") @@ -1629,6 +1638,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") @@ -1797,7 +1808,7 @@ def test_read_variable_op(target, dev): out_names=out_name, num_output=len(out_name), ) - for i in range(len(tf_output)): + for i, _ in enumerate(tf_output): tvm.testing.assert_allclose(tf_output[i], tvm_output[i], atol=1e-4, rtol=1e-5) sess.close() @@ -1910,6 +1921,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 +2436,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 +2453,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 +2600,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 +2616,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,9 +2910,9 @@ 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") @@ -3049,6 +3062,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") @@ -3071,6 +3085,7 @@ 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") @@ -3096,7 +3111,7 @@ 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)): + for i, _ in enumerate(tf_output): tvm.testing.assert_allclose(tf_output[i], tvm_output[i], atol=1e-5, rtol=1e-5) @@ -3668,7 +3683,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) @@ -4019,7 +4034,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 @@ -4448,6 +4463,8 @@ def test_forward_pow_exp(): 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 +4642,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 +4662,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) @@ -4665,6 +4686,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": @@ -4680,6 +4703,7 @@ def _check_op(tf_op, ishape, axis, keepdims, dtype="float32"): compare_tf_with_tvm([np_data], ["in_data:0"], reduce_op.name) def _test_math_op(op, dtypes=["int32", "float32"]): + # pylint: disable=dangerous-default-value for dtype in dtypes: _check_op(op, (3, 10), axis=(-1), keepdims=False, dtype=dtype) _check_op(op, (8, 16, 32), axis=(-1), keepdims=False, dtype=dtype) @@ -4704,6 +4728,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": @@ -4721,6 +4747,7 @@ def _check_op(tf_op, ishape, axis, keepdims, range_axis=False, dtype="float32"): compare_tf_with_tvm([np_data], ["in_data:0"], reduce_op.name) def _test_raw_reduce_op(op, dtypes=["int32", "float32"]): + # pylint: disable=dangerous-default-value for dtype in dtypes: _check_op(op, (3, 10), axis=(-1), keepdims=False, dtype=dtype) _check_op(op, (8, 16, 32), axis=(-1), keepdims=False, dtype=dtype) @@ -4824,6 +4851,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") @@ -4878,6 +4906,7 @@ def _test_forward_add_n(inputs): 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) @@ -4936,6 +4965,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 +5018,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 +5084,7 @@ def _test_identityn(data_np_list): ], ) def test_forward_identityn(data_np_list): + """Identityn""" _test_identityn(data_np_list) @@ -5064,7 +5096,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) @@ -5437,6 +5469,7 @@ def resourceVariablesTest(x, y): def test_forward_spop(): + """Spop""" _test_spop_stateful() _test_spop_device_assignment() # tensorflow version upgrade support @@ -5469,6 +5502,7 @@ 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(): @@ -5501,6 +5535,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 @@ -5592,7 +5627,7 @@ def lstm_cell(): ) # Compare result - for i in range(len(tf_output)): + for i, _ in enumerate(tf_output): tvm.testing.assert_allclose(tf_output[i], tvm_output[i], atol=1e-5, rtol=1e-5) @@ -5670,6 +5705,7 @@ def test_forward_unique_with_counts(): def test_moments(): + """NN.moments""" g = tf.Graph() shape = [4, 176, 8, 8] dtype = "float32" From 7199127502187909e5b6115a4f474897f2ba69cd Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Thu, 7 Jul 2022 06:46:24 +0000 Subject: [PATCH 053/110] [CI] Apply linting rules to tflite tests --- tests/python/frontend/tflite/test_forward.py | 171 +++++++++++-------- 1 file changed, 96 insertions(+), 75 deletions(-) diff --git a/tests/python/frontend/tflite/test_forward.py b/tests/python/frontend/tflite/test_forward.py index 42848783967c..4465f9638324 100644 --- a/tests/python/frontend/tflite/test_forward.py +++ b/tests/python/frontend/tflite/test_forward.py @@ -14,7 +14,8 @@ # 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, unused-variable, singleton-comparison +# pylint: disable=redefined-builtin, no-else-return, inconsistent-return-statements, import-outside-toplevel """ TFLite testcases ================ @@ -22,13 +23,21 @@ """ from __future__ import print_function from functools import partial +from distutils.version import LooseVersion + +import os +import tempfile import pytest import numpy as np -import tvm -import tempfile -from tvm import te + +from PIL import Image +from packaging import version as package_version +from tvm.contrib.download import download_testdata from tvm import relay +import tvm +import tvm.relay.testing.tf as tf_testing + try: import tensorflow.compat.v1 as tf @@ -48,19 +57,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 +88,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) @@ -114,6 +117,7 @@ def get_real_image_object_detection(im_height, im_width): def vmobj_to_list(o): + """Converts TVM objects returned by VM execution to Python List.""" if isinstance(o, tvm.nd.NDArray): return [o.numpy().tolist()] elif isinstance(o, tvm.runtime.container.ADT): @@ -259,13 +263,13 @@ 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)): + for i, _ in enumerate(input_details): interpreter.resize_tensor_input(input_details[i]["index"], input_data[i].shape) interpreter.allocate_tensors() # set input assert len(input_data) == len(input_details) - for i in range(len(input_details)): + for i, _ in enumerate(input_details): interpreter.set_tensor(input_details[i]["index"], input_data[i]) # Run @@ -273,7 +277,7 @@ def run_tflite_graph(tflite_model_buf, input_data): # get output tflite_output = list() - for i in range(len(output_details)): + for i, _ in enumerate(output_details): tflite_output.append(interpreter.get_tensor(output_details[i]["index"])) return tflite_output @@ -298,7 +302,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: @@ -354,21 +358,23 @@ 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) 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 ) 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": @@ -907,7 +913,7 @@ 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") @@ -957,6 +963,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 +1007,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") @@ -1161,6 +1168,7 @@ def _test_convolution( def test_forward_convolution(): + """Convolution""" for quantized in [False, True]: for fp16_quantized in [False, True]: _test_convolution( @@ -1365,6 +1373,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 @@ -1830,7 +1839,7 @@ def _test_shape(dtype): out = tf.shape(r, out_type=dtype) out = tf.add(out, tf.constant([1], dtype=dtype)) compare_tflite_with_tvm( - [x for x in np.nditer(data)], + [x for x in np.nditer(data)], # pylint: disable=unnecessary-comprehension ["start", "limit", "delta"], [start, limit, delta], [out], @@ -1887,6 +1896,7 @@ def test_forward_concatenation(): def _test_unary_elemwise(math_op, data, quantized, quant_range=[-6, 6], int_quant_dtype=tf.int8): """One iteration of unary elemwise""" + # pylint: disable= dangerous-default-value if quantized: with tf.Graph().as_default(): quant_min, quant_max = quant_range @@ -2185,6 +2195,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 +2244,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 +2261,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] != 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] != None else tf.quantization.fake_quant_with_min_max_args( data[1], min=out_min, max=out_max, name="const_tensor1" ), @@ -2268,50 +2279,37 @@ 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] != None } if math_op is math_ops.equal: out = math_op(inq_data[0], inq_data[1]) 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 + ":0" for x in input_range.keys()], - [x[1] for x in zip(in_data, inq_data) if None != x[0]], - [out], - ) - else: - out = math_op(inq_data[0], inq_data[1]) - out = with_fused_activation_function(out, fused_activation_function) - 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 + ":0" for x in input_range.keys()], - [x[1] for x in zip(in_data, inq_data) if None != x[0]], - [out], - quantized=True, - input_range=input_range, - experimental_new_converter=same_qnn_params, - ) + # 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 x[0] != None], + [x + ":0" for x in input_range.keys()], + [x[1] for x in zip(in_data, inq_data) if x[0] != None], + [out], + quantized=True, + input_range=input_range, + experimental_new_converter=same_qnn_params, + ) else: out = math_op( in_data[0] - if None != in_data[0] + if in_data[0] != None else ops.convert_to_tensor(data[0], dtype=data[0].dtype), in_data[1] - if None != in_data[1] + if in_data[1] != 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] != None], + [x[1] for x in zip(in_data, ("in_0:0", "in_1:0")) if x[0] != None], + [x for x in in_data if x != None], [out], ) @@ -2565,6 +2563,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,14 +2612,15 @@ 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], + [each for each in inputs], # pylint: disable=unnecessary-comprehension [each.name for each in temp], - [each for each in temp], + [each for each in temp], # pylint: disable=unnecessary-comprehension [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) @@ -2652,7 +2652,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 +2912,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,6 +2931,7 @@ def test_forward_arg_min_max(): def test_forward_select(): + """Select""" with tf.Graph().as_default(): with tf.Session() as sess: input1 = tf.placeholder(tf.int32, shape=[1, 4, 4, 3], name="input1") @@ -2949,6 +2951,7 @@ def test_forward_select(): "value, min, max", [[-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): + """Fake quant""" with tf.Graph().as_default(): with tf.Session() as sess: input = tf.placeholder(tf.float32, shape=[1], name="input") @@ -3788,7 +3791,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( @@ -4257,6 +4261,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", @@ -4373,10 +4378,10 @@ 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)): + 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: @@ -4508,7 +4513,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 +4531,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 +4550,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 +4569,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 +4594,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 +4619,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 +4678,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 +4700,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 +4722,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 +4744,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 +4774,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 +4819,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 +4857,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", ) @@ -4904,7 +4924,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", ) From 31bdaeadd34d8c6bc12c2a96784dc7349863a3d8 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Fri, 17 Jun 2022 11:46:58 +0000 Subject: [PATCH 054/110] [CI] Apply linting rules to coreml tests --- tests/python/frontend/caffe/test_forward.py | 4 ++++ tests/python/frontend/coreml/model_zoo/__init__.py | 9 ++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/tests/python/frontend/caffe/test_forward.py b/tests/python/frontend/caffe/test_forward.py index 0e94148b3d06..f48965b9dbb7 100644 --- a/tests/python/frontend/caffe/test_forward.py +++ b/tests/python/frontend/caffe/test_forward.py @@ -221,7 +221,11 @@ def _run_tvm(data, proto_file, blob_file): def _compare_caffe_tvm(caffe_out, tvm_out, is_network=False): +<<<<<<< HEAD for i in range(len(caffe_out)): +======= + for i, _ in enumerate(caffe_out): +>>>>>>> 2b7ec741f... fix error 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) diff --git a/tests/python/frontend/coreml/model_zoo/__init__.py b/tests/python/frontend/coreml/model_zoo/__init__.py index bf270b8dff75..9645d5ca891e 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,11 +36,14 @@ def get_resnet50(): def get_cat_image(): - url = "https://gist.githubusercontent.com/zhreshold/bcda4716699ac97ea44f791c24310193/raw/fa7ef0e9c9a5daea686d6473a62aacd1a5885849/cat.png" + 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)) # CoreML's standard model image format is BGR img_bgr = np.array(img)[:, :, ::-1] img = np.transpose(img_bgr, (2, 0, 1))[np.newaxis, :] - return np.asarray(img) + return np.asarray(img) \ No newline at end of file From 9522223ff0f8e1e3bb45cff2bfb3b9f83cbd725c Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Tue, 14 Jun 2022 13:24:21 +0000 Subject: [PATCH 055/110] [CI] Apply linting rules to caffe tests --- tests/python/frontend/caffe/test_forward.py | 25 +++++++++------------ 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/tests/python/frontend/caffe/test_forward.py b/tests/python/frontend/caffe/test_forward.py index f48965b9dbb7..749609b6e5a2 100644 --- a/tests/python/frontend/caffe/test_forward.py +++ b/tests/python/frontend/caffe/test_forward.py @@ -21,14 +21,9 @@ 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): @@ -221,11 +221,7 @@ def _run_tvm(data, proto_file, blob_file): def _compare_caffe_tvm(caffe_out, tvm_out, is_network=False): -<<<<<<< HEAD - for i in range(len(caffe_out)): -======= for i, _ in enumerate(caffe_out): ->>>>>>> 2b7ec741f... fix error 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) @@ -965,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) @@ -1197,4 +1194,4 @@ def test_forward_Inceptionv1(): test_forward_Mobilenetv2() test_forward_Alexnet() test_forward_Resnet50() - test_forward_Inceptionv1() + test_forward_Inceptionv1() \ No newline at end of file From 336fba27c546c3b80669e0eef3f14914aaa48c10 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Thu, 7 Jul 2022 08:53:44 +0000 Subject: [PATCH 056/110] [CI] Apply linting rules to caffe2 tests --- tests/python/frontend/caffe2/test_forward.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/tests/python/frontend/caffe2/test_forward.py b/tests/python/frontend/caffe2/test_forward.py index 3d7ad230dec0..09ee8a4a3e67 100644 --- a/tests/python/frontend/caffe2/test_forward.py +++ b/tests/python/frontend/caffe2/test_forward.py @@ -14,15 +14,22 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. +# pylint: disable=no-else-return +""" +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 +110,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 +154,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 +199,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" From 78c1099adcb059fcdb9670f40b06ef48eff5ae38 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Thu, 7 Jul 2022 08:54:20 +0000 Subject: [PATCH 057/110] reformat by black --- tests/python/frontend/caffe/test_forward.py | 2 +- tests/python/frontend/tflite/test_forward.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/python/frontend/caffe/test_forward.py b/tests/python/frontend/caffe/test_forward.py index 749609b6e5a2..4199e27df7ed 100644 --- a/tests/python/frontend/caffe/test_forward.py +++ b/tests/python/frontend/caffe/test_forward.py @@ -1194,4 +1194,4 @@ def test_forward_Inceptionv1(): test_forward_Mobilenetv2() test_forward_Alexnet() test_forward_Resnet50() - test_forward_Inceptionv1() \ No newline at end of file + test_forward_Inceptionv1() diff --git a/tests/python/frontend/tflite/test_forward.py b/tests/python/frontend/tflite/test_forward.py index 4465f9638324..3dd61363fabd 100644 --- a/tests/python/frontend/tflite/test_forward.py +++ b/tests/python/frontend/tflite/test_forward.py @@ -1839,7 +1839,7 @@ def _test_shape(dtype): out = tf.shape(r, out_type=dtype) out = tf.add(out, tf.constant([1], dtype=dtype)) compare_tflite_with_tvm( - [x for x in np.nditer(data)], # pylint: disable=unnecessary-comprehension + [x for x in np.nditer(data)], # pylint: disable=unnecessary-comprehension ["start", "limit", "delta"], [start, limit, delta], [out], @@ -2612,9 +2612,9 @@ 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], # pylint: disable=unnecessary-comprehension + [each for each in inputs], # pylint: disable=unnecessary-comprehension [each.name for each in temp], - [each for each in temp], # pylint: disable=unnecessary-comprehension + [each for each in temp], # pylint: disable=unnecessary-comprehension [output], ) From 126d1bbf90824e848089cb57e72919e81b09ea6d Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Thu, 7 Jul 2022 08:56:01 +0000 Subject: [PATCH 058/110] reformat by black --- tests/python/frontend/coreml/model_zoo/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python/frontend/coreml/model_zoo/__init__.py b/tests/python/frontend/coreml/model_zoo/__init__.py index 9645d5ca891e..c137a5d71a05 100644 --- a/tests/python/frontend/coreml/model_zoo/__init__.py +++ b/tests/python/frontend/coreml/model_zoo/__init__.py @@ -46,4 +46,4 @@ def get_cat_image(): # CoreML's standard model image format is BGR img_bgr = np.array(img)[:, :, ::-1] img = np.transpose(img_bgr, (2, 0, 1))[np.newaxis, :] - return np.asarray(img) \ No newline at end of file + return np.asarray(img) From 52b9153a93bb0fa5e8ed503a40e66cc01c29dd8d Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Thu, 7 Jul 2022 09:33:40 +0000 Subject: [PATCH 059/110] [CI] Apply linting rules to coreml tests --- tests/python/frontend/coreml/test_forward.py | 148 +++++++++++-------- 1 file changed, 83 insertions(+), 65 deletions(-) diff --git a/tests/python/frontend/coreml/test_forward.py b/tests/python/frontend/coreml/test_forward.py index 8892018a79e9..4530c4261952 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. +# pylint: disable=invalid-name, redefined-builtin +""" +CoreML testcases +==================== +This article is a test script to test CoreML operator with Relay. +""" +from os import path +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): + """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): + """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): + """Concat_layer_params""" dtype = "float32" a_np1 = np.random.uniform(size=input1_dim).astype(dtype) @@ -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_UpsampleLayerParams(input_dim, scale, mode): dtype = "float32" a_np = np.full(input_dim, 1, dtype=dtype) @@ -241,11 +244,12 @@ 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") + """UpsampleLayerParams""" + _verify_UpsampleLayerParams((1, 16, 32, 32), 2, "NN") + _verify_UpsampleLayerParams((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) @@ -264,10 +268,10 @@ 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) @@ -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): + """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): + """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): + """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): + """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): + """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): + """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): + """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): + """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,6 +579,7 @@ def test_forward_unary(): @tvm.testing.uses_gpu def test_forward_reduce(): + """Reduce""" from enum import Enum class ReduceAxis(Enum): @@ -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. @@ -749,22 +766,23 @@ def test_forward_image_scaler(): def verify_convolution(input_dim, filter, padding): + """Convolution""" dtype = "float32" - N, C, H, W = input_dim - OC, _, KH, KW = filter + _, c, h, w = input_dim + oc, _, kh, kw = 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=(oc, c, kh, kw)).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))] + inputs = [("input1", datatypes.Array(c, h, w))] output = [("output", datatypes.Array(*b_np.shape))] builder = NeuralNetworkBuilder(inputs, output) builder.add_convolution( name="conv", kernel_channels=3, - output_channels=OC, - height=KH, - width=KW, + output_channels=oc, + height=kh, + width=kw, stride_height=1, stride_width=1, border_mode=padding.lower(), @@ -828,7 +846,7 @@ def test_can_build_keras_to_coreml_to_relay(): if __name__ == "__main__": test_forward_AddLayerParams() - test_forward_ConcatLayerParams() + test_forward_concat_layer_params() test_forward_MultiplyLayerParams() test_forward_UpsampleLayerParams() test_forward_l2_normalize() From 6c715b4a0ba43235cd98c212e3415be3861308dd Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Thu, 7 Jul 2022 09:37:42 +0000 Subject: [PATCH 060/110] [CI] Apply linting rules to darknet tests --- tests/python/frontend/darknet/test_forward.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/python/frontend/darknet/test_forward.py b/tests/python/frontend/darknet/test_forward.py index 116748a4f78a..887482904daa 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, invalid-name """ Test Darknet Models =================== @@ -23,16 +24,16 @@ """ 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 +download_testdata.__test__ = False + REPO_URL = "https://github.com/dmlc/web-data/blob/main/darknet/" DARKNET_LIB = "libdarknet2.0.so" DARKNETLIB_URL = REPO_URL + "lib/" + DARKNET_LIB + "?raw=true" @@ -75,7 +76,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 From 908ed287e368d33035acf32424a2ab07bdaf4158 Mon Sep 17 00:00:00 2001 From: Black <823036806@qq.com> Date: Thu, 7 Jul 2022 18:42:31 +0800 Subject: [PATCH 061/110] Update tests/python/frontend/coreml/test_forward.py Co-authored-by: Sebastian Boblest --- tests/python/frontend/coreml/test_forward.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python/frontend/coreml/test_forward.py b/tests/python/frontend/coreml/test_forward.py index 4530c4261952..b13493bc8e8e 100644 --- a/tests/python/frontend/coreml/test_forward.py +++ b/tests/python/frontend/coreml/test_forward.py @@ -210,7 +210,7 @@ def test_forward_concat_layer_params(): 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) From 51ab028fdc240ef7d04f4ad9b50ae1ea26a6fcc9 Mon Sep 17 00:00:00 2001 From: Black <823036806@qq.com> Date: Thu, 7 Jul 2022 18:42:46 +0800 Subject: [PATCH 062/110] Update tests/python/frontend/tflite/test_forward.py Co-authored-by: Sebastian Boblest --- tests/python/frontend/tflite/test_forward.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python/frontend/tflite/test_forward.py b/tests/python/frontend/tflite/test_forward.py index 3dd61363fabd..66db23b5a520 100644 --- a/tests/python/frontend/tflite/test_forward.py +++ b/tests/python/frontend/tflite/test_forward.py @@ -4382,7 +4382,7 @@ def convert_sub_dummy(self, op): output_tensors = [out] in_node = [0] * 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] + in_node[i] = in_name[i].split(":")[0] with tf.Session() as sess: converter = tf.lite.TFLiteConverter.from_session(sess, input_tensors, output_tensors) From 81c275023ba7404c369a4f25378c1ae3585ad57a Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Thu, 7 Jul 2022 10:46:20 +0000 Subject: [PATCH 063/110] Update tests/python/frontend/coreml/test_forward.py --- tests/python/frontend/coreml/test_forward.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python/frontend/coreml/test_forward.py b/tests/python/frontend/coreml/test_forward.py index b13493bc8e8e..6645bba3ec40 100644 --- a/tests/python/frontend/coreml/test_forward.py +++ b/tests/python/frontend/coreml/test_forward.py @@ -21,6 +21,7 @@ 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 @@ -580,7 +581,6 @@ def test_forward_unary(): @tvm.testing.uses_gpu def test_forward_reduce(): """Reduce""" - from enum import Enum class ReduceAxis(Enum): CHW = 0 From 9390cb616fae64d791658cb8852889ab91b235a6 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Thu, 7 Jul 2022 10:48:16 +0000 Subject: [PATCH 064/110] Update tests/python/frontend/tflite/test_forward.py --- tests/python/frontend/tflite/test_forward.py | 24 ++++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/tests/python/frontend/tflite/test_forward.py b/tests/python/frontend/tflite/test_forward.py index 66db23b5a520..d0e5099ff172 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, unused-variable, singleton-comparison +# pylint: disable=import-self, invalid-name, unused-argument, unused-variable # pylint: disable=redefined-builtin, no-else-return, inconsistent-return-statements, import-outside-toplevel """ TFLite testcases @@ -2261,14 +2261,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 in_data[0] != None + 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 in_data[1] != None + 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" ), @@ -2279,7 +2279,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 x[0] != None + if x[0] is not None } if math_op is math_ops.equal: @@ -2288,9 +2288,9 @@ def __test_elemwise(in_data): # 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 x[0] != None], + [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 x[0] != None], + [x[1] for x in zip(in_data, inq_data) if x[0] is not None], [out], quantized=True, input_range=input_range, @@ -2299,17 +2299,17 @@ def __test_elemwise(in_data): else: out = math_op( in_data[0] - if in_data[0] != None + if in_data[0] is not None else ops.convert_to_tensor(data[0], dtype=data[0].dtype), in_data[1] - if in_data[1] != None + 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 x[0] != None], - [x[1] for x in zip(in_data, ("in_0:0", "in_1:0")) if x[0] != None], - [x for x in in_data if x != None], + [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], ) @@ -3953,7 +3953,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], From ddd2b2b2b4c9ad3e91ebf3e948ec7065ddea9bb9 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Thu, 7 Jul 2022 13:55:42 +0000 Subject: [PATCH 065/110] fix test errors --- tests/python/frontend/coreml/test_forward.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/python/frontend/coreml/test_forward.py b/tests/python/frontend/coreml/test_forward.py index 6645bba3ec40..8d0a0326ce9b 100644 --- a/tests/python/frontend/coreml/test_forward.py +++ b/tests/python/frontend/coreml/test_forward.py @@ -244,10 +244,10 @@ def _verify_upsample_layer_params(input_dim, scale, mode): @tvm.testing.uses_gpu -def test_forward_UpsampleLayerParams(): +def test_forward_upsample_layer_params(): """UpsampleLayerParams""" - _verify_UpsampleLayerParams((1, 16, 32, 32), 2, "NN") - _verify_UpsampleLayerParams((1, 4, 6, 6), 3, "BILINEAR") + _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): @@ -845,10 +845,10 @@ def test_can_build_keras_to_coreml_to_relay(): if __name__ == "__main__": - test_forward_AddLayerParams() + test_forward_add_layer_params test_forward_concat_layer_params() - test_forward_MultiplyLayerParams() - test_forward_UpsampleLayerParams() + test_forward_multiply_layer_params() + test_forward_upsample_layer_params() test_forward_l2_normalize() test_forward_lrn() test_forward_average() From a0cfc67a5322f4d21c43ccf27a6030db844435cb Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Thu, 7 Jul 2022 14:03:14 +0000 Subject: [PATCH 066/110] fix ci test errors --- .../frontend/tensorflow/test_forward.py | 18 ++++++++-- tests/python/frontend/tflite/test_forward.py | 34 +++++++++++++------ 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/tests/python/frontend/tensorflow/test_forward.py b/tests/python/frontend/tensorflow/test_forward.py index 540caa12914a..6f6c0a2cbd02 100755 --- a/tests/python/frontend/tensorflow/test_forward.py +++ b/tests/python/frontend/tensorflow/test_forward.py @@ -37,6 +37,20 @@ 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 +from tensorflow.python.ops import nn_ops +from tensorflow.python.ops import nn +from tensorflow.python.ops import array_ops +from tensorflow.python.ops import math_ops +from tensorflow.python.ops import variable_scope +from tensorflow.python.ops import variables +from tensorflow.python.ops import init_ops +from tensorflow.python.framework import function +from tensorflow.python.framework import ops +from tensorflow.python.framework import dtypes +from tensorflow.python.ops import gen_functional_ops + try: import tensorflow.compat.v1 as tf @@ -4703,7 +4717,7 @@ def _check_op(tf_op, ishape, axis, keepdims, dtype="float32"): compare_tf_with_tvm([np_data], ["in_data:0"], reduce_op.name) def _test_math_op(op, dtypes=["int32", "float32"]): - # pylint: disable=dangerous-default-value + # pylint: disable=dangerous-default-value, redefined-outer-name for dtype in dtypes: _check_op(op, (3, 10), axis=(-1), keepdims=False, dtype=dtype) _check_op(op, (8, 16, 32), axis=(-1), keepdims=False, dtype=dtype) @@ -4747,7 +4761,7 @@ def _check_op(tf_op, ishape, axis, keepdims, range_axis=False, dtype="float32"): compare_tf_with_tvm([np_data], ["in_data:0"], reduce_op.name) def _test_raw_reduce_op(op, dtypes=["int32", "float32"]): - # pylint: disable=dangerous-default-value + # pylint: disable=dangerous-default-value, redefined-outer-name for dtype in dtypes: _check_op(op, (3, 10), axis=(-1), keepdims=False, dtype=dtype) _check_op(op, (8, 16, 32), axis=(-1), keepdims=False, dtype=dtype) diff --git a/tests/python/frontend/tflite/test_forward.py b/tests/python/frontend/tflite/test_forward.py index d0e5099ff172..b83c9167f0e2 100644 --- a/tests/python/frontend/tflite/test_forward.py +++ b/tests/python/frontend/tflite/test_forward.py @@ -2286,16 +2286,28 @@ def __test_elemwise(in_data): out = math_op(inq_data[0], inq_data[1]) out = with_fused_activation_function(out, fused_activation_function) - # 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 x[0] is not None], - [x + ":0" for x in input_range.keys()], - [x[1] for x in zip(in_data, inq_data) if x[0] is not None], - [out], - quantized=True, - input_range=input_range, - experimental_new_converter=same_qnn_params, - ) + compare_tflite_with_tvm( + [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 x[0] is not None], + [out], + ) + else: + out = math_op(inq_data[0], inq_data[1]) + out = with_fused_activation_function(out, fused_activation_function) + 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 x[0] is not None], + [x + ":0" for x in input_range.keys()], + [x[1] for x in zip(in_data, inq_data) if x[0] is not None], + [out], + quantized=True, + input_range=input_range, + experimental_new_converter=same_qnn_params, + ) else: out = math_op( in_data[0] @@ -5055,6 +5067,8 @@ def test_forward_nms_v5(): # Main # ---- if __name__ == "__main__": + test_all_elemwise() + assert 0 # BatchToSpaceND test_forward_batch_to_space_nd() From bf88603c4ff83ab7672701901988f2b6a8c48a9e Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Fri, 8 Jul 2022 07:32:28 +0000 Subject: [PATCH 067/110] [CI] Apply linting rules to keras tests --- tests/python/frontend/keras/test_forward.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/python/frontend/keras/test_forward.py b/tests/python/frontend/keras/test_forward.py index ff9e20cff24b..6a2c949a0d0d 100644 --- a/tests/python/frontend/keras/test_forward.py +++ b/tests/python/frontend/keras/test_forward.py @@ -14,13 +14,14 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. +# pylint: disable=invalid-name, redefined-outer-name, missing-function-docstring +"""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 +import keras try: import tensorflow.compat.v1 as tf @@ -28,7 +29,6 @@ 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 if tf.executing_eagerly(): @@ -81,7 +81,7 @@ 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"): + def get_keras_output(xs): return keras_model.predict(xs) def get_tvm_output(xs, target, dev, dtype="float32"): @@ -125,7 +125,7 @@ def get_mobilenet(keras): @tvm.testing.uses_gpu -class TestKeras: +class TestKeras: # pylint: disable=missing-class-docstring scenarios = [using_classic_keras, using_tensorflow_keras] def test_forward_merge(self, keras): From 4a7797953c7a3683511de8590a62439868c9630a Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Fri, 8 Jul 2022 07:53:26 +0000 Subject: [PATCH 068/110] [CI] Apply linting rules to oneflow tests --- tests/python/frontend/oneflow/test_forward.py | 12 +++--------- tests/python/frontend/oneflow/test_vision_models.py | 5 ++++- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/tests/python/frontend/oneflow/test_forward.py b/tests/python/frontend/oneflow/test_forward.py index 0d18a2fb5c21..41ca08c7ab5a 100644 --- a/tests/python/frontend/oneflow/test_forward.py +++ b/tests/python/frontend/oneflow/test_forward.py @@ -14,19 +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 -# pylint: disable=arguments-differ, unused-argument, unused-import +# pylint: disable=import-self, invalid-name, missing-function-docstring +# pylint: disable=arguments-differ, unused-argument, consider-using-f-string """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 @@ -679,9 +676,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 +684,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() diff --git a/tests/python/frontend/oneflow/test_vision_models.py b/tests/python/frontend/oneflow/test_vision_models.py index e8d0627001ca..d8f5aa6b6001 100644 --- a/tests/python/frontend/oneflow/test_vision_models.py +++ b/tests/python/frontend/oneflow/test_vision_models.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 +# pylint: disable=import-self, invalid-name, consider-using-f-string # pylint: disable=arguments-differ, unused-argument, unused-import """Unit tests for various models and operators""" import os @@ -81,6 +81,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 +106,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 +127,7 @@ def verify_model( @tvm.testing.uses_gpu def test_vision_models(): + """Vision models test""" if os.path.exists(MODEL_HOME): rmdir(MODEL_HOME) From f33905f336a8b9c3c91b4ec5f253bfffc3789857 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Fri, 8 Jul 2022 11:19:31 +0000 Subject: [PATCH 069/110] [CI] Apply linting rules to onnx tests --- tests/python/frontend/onnx/test_forward.py | 108 ++++++++++----------- 1 file changed, 52 insertions(+), 56 deletions(-) diff --git a/tests/python/frontend/onnx/test_forward.py b/tests/python/frontend/onnx/test_forward.py index e500f0902c83..25b52114119e 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=import-self, invalid-name, unused-argument, unused-variable, missing-function-docstring, redefined-builtin, consider-using-f-string +""" +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,7 +37,13 @@ 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): @@ -106,13 +118,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 +142,6 @@ def get_tvm_output( def get_onnxruntime_output(model, inputs): - import onnxruntime.backend - rep = onnxruntime.backend.prepare(model.SerializeToString(), "CPU") if isinstance(inputs, list) and len(inputs) == 1: inp = inputs[0] @@ -233,11 +241,10 @@ 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 - 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( @@ -257,7 +264,9 @@ def get_next(self): model_fp32 = os.path.join(d.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()) + quantized_model = quantize_static( + model_fp32, model_quant, RandomDataReader() + ) # pylint: disable=assignment-from-no-return # opt_level=1 will cause error with qnn lowering model = onnx.load(model_quant) verify_with_ort_with_inputs( @@ -377,7 +386,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( @@ -1443,7 +1452,7 @@ def test_lrn(target, dev): 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 @@ -1779,7 +1788,7 @@ 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 +1812,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) @@ -2721,9 +2730,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( @@ -2770,7 +2779,7 @@ def verify_conv( ) def repeat(N, D): - return tuple([N for _ in range(D)]) + return tuple(N for _ in range(D)) for D in [1, 2, 3]: # Convolution with padding @@ -3007,7 +3016,7 @@ def verify_convtranspose(x_shape, w_shape, y_shape, p, group=1): 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)]) + return tuple(N for _ in range(D)) # Once onnxruntime update is complete for D in [1, 2, 3]: @@ -3105,14 +3114,10 @@ def repeat(N, D): @tvm.testing.parametrize_targets def test_unsqueeze_constant(target, dev): - from torch.nn import Linear, Module, Sequential - class Flatten(Module): def forward(self, input): return input.view(input.size(0), -1) - import tempfile - with tempfile.NamedTemporaryFile() as fp: file_name = fp.name input_size = (1, 16, 32, 32) @@ -3805,7 +3810,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") @@ -3821,7 +3826,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") @@ -4870,8 +4875,10 @@ 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], rtol=1e-05, atol=1e-05 + ) # pylint: disable=unnecessary-list-index-lookup # Confirm that if works with cond as an array or scalar. verify_if(cond_array=False, num_outputs=1) @@ -5131,13 +5138,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") @@ -5386,7 +5391,7 @@ def verify_embedding_bag(num_embedding, embedding_dim, data_shape, num_bags=None def test_index_put(target, dev): class _index_put_model(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 @@ -5436,8 +5441,8 @@ 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) @@ -5837,9 +5842,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( @@ -5875,7 +5880,7 @@ def verify_qlinearconv( 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)]) + return tuple(N for _ in range(D)) # only support QLinearConv2d because only support qnn.conv2d D = 2 @@ -5985,9 +5990,8 @@ 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) @@ -6027,7 +6031,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( @@ -6059,7 +6062,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( @@ -6112,8 +6114,6 @@ def verify_qlinearsigmoid(a_shape): 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], @@ -6364,9 +6364,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( @@ -6402,7 +6402,7 @@ def verify_convinteger( 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)]) + return tuple(N for _ in range(D)) # only support 2D ConvInteger because we only support qnn.conv2d for now. D = 2 @@ -6495,7 +6495,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 @@ -6530,9 +6529,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] ) From cd86891b57abfe6be010d79a73dada29cedde720 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Wed, 13 Jul 2022 07:48:53 +0000 Subject: [PATCH 070/110] replace with tvm.testing.main() --- tests/python/frontend/coreml/test_forward.py | 19 +------------------ 1 file changed, 1 insertion(+), 18 deletions(-) diff --git a/tests/python/frontend/coreml/test_forward.py b/tests/python/frontend/coreml/test_forward.py index 8d0a0326ce9b..f10093ab3330 100644 --- a/tests/python/frontend/coreml/test_forward.py +++ b/tests/python/frontend/coreml/test_forward.py @@ -845,21 +845,4 @@ def test_can_build_keras_to_coreml_to_relay(): if __name__ == "__main__": - test_forward_add_layer_params - test_forward_concat_layer_params() - test_forward_multiply_layer_params() - test_forward_upsample_layer_params() - 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() From 2a4a55972fd33ae31e742b9decd0b30c7a762432 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Wed, 13 Jul 2022 08:10:32 +0000 Subject: [PATCH 071/110] fix conflict --- tests/python/frontend/tflite/test_forward.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/python/frontend/tflite/test_forward.py b/tests/python/frontend/tflite/test_forward.py index b83c9167f0e2..fc296ea7590f 100644 --- a/tests/python/frontend/tflite/test_forward.py +++ b/tests/python/frontend/tflite/test_forward.py @@ -5067,8 +5067,6 @@ def test_forward_nms_v5(): # Main # ---- if __name__ == "__main__": - test_all_elemwise() - assert 0 # BatchToSpaceND test_forward_batch_to_space_nd() From 9651514e1174cb47ddec2b83a46994de3aaf87d1 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Wed, 13 Jul 2022 08:17:18 +0000 Subject: [PATCH 072/110] Update as @areusch suggest --- tests/python/frontend/coreml/test_forward.py | 29 ++++++++++---------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/tests/python/frontend/coreml/test_forward.py b/tests/python/frontend/coreml/test_forward.py index f10093ab3330..855441fb3cd9 100644 --- a/tests/python/frontend/coreml/test_forward.py +++ b/tests/python/frontend/coreml/test_forward.py @@ -122,7 +122,7 @@ def run_tvm_graph( def verify_add_layer_params(input_dim, alpha=2): - """Add_layer_params""" + """Verify add layer params""" dtype = "float32" a_np1 = np.random.uniform(size=input_dim).astype(dtype) @@ -151,7 +151,7 @@ def test_forward_add_layer_params(): def verify_multiply_layer_params(input_dim, alpha): - """Multiply_layer_params""" + """Verify multiply layer params""" dtype = "float32" a_np1 = np.random.uniform(size=input_dim).astype(dtype) @@ -184,7 +184,7 @@ def test_forward_multiply_layer_params(): def verify_concat_layer_params(input1_dim, input2_dim): - """Concat_layer_params""" + """Verify concat layer params""" dtype = "float32" a_np1 = np.random.uniform(size=input1_dim).astype(dtype) @@ -245,7 +245,7 @@ def _verify_upsample_layer_params(input_dim, scale, mode): @tvm.testing.uses_gpu def test_forward_upsample_layer_params(): - """UpsampleLayerParams""" + """Upsample Layer Params""" _verify_upsample_layer_params((1, 16, 32, 32), 2, "NN") _verify_upsample_layer_params((1, 4, 6, 6), 3, "BILINEAR") @@ -410,7 +410,7 @@ def test_forward_min(): def verify_unary_sqrt(input_dim): - """Unary sqrt""" + """Verify unary sqrt""" dtype = "float32" a_np = np.random.uniform(size=input_dim).astype(dtype) @@ -428,7 +428,7 @@ def verify_unary_sqrt(input_dim): def verify_unary_rsqrt(input_dim, epsilon=0): - """Unary rsqrt""" + """Verify unary rsqrt""" dtype = "float32" a_np = np.random.uniform(size=input_dim).astype(dtype) @@ -448,7 +448,7 @@ def verify_unary_rsqrt(input_dim, epsilon=0): def verify_unary_inverse(input_dim, epsilon=0): - """Unary inverse""" + """Verify unary inverse""" dtype = "float32" a_np = np.random.uniform(size=input_dim).astype(dtype) @@ -468,7 +468,7 @@ def verify_unary_inverse(input_dim, epsilon=0): def verify_unary_power(input_dim, alpha): - """Unary power""" + """Verify unary power""" dtype = "float32" a_np = np.random.uniform(size=input_dim).astype(dtype) @@ -488,7 +488,7 @@ def verify_unary_power(input_dim, alpha): def verify_unary_exp(input_dim): - """Unary exp""" + """Verify unary exp""" dtype = "float32" a_np = np.random.uniform(size=input_dim).astype(dtype) @@ -506,7 +506,7 @@ def verify_unary_exp(input_dim): def verify_unary_log(input_dim): - """Unary log""" + """Verify unary log""" dtype = "float32" a_np = np.random.uniform(size=input_dim).astype(dtype) @@ -524,7 +524,7 @@ def verify_unary_log(input_dim): def verify_unary_abs(input_dim): - """Unary abs""" + """Verify unary abs""" dtype = "float32" a_np = np.random.uniform(-100.0, 100.0, size=input_dim).astype(dtype) @@ -542,7 +542,7 @@ def verify_unary_abs(input_dim): def verify_unary_threshold(input_dim, alpha): - """Unary threshold""" + """Verify unary threshold""" dtype = "float32" a_np = np.random.uniform(-100.0, 100.0, size=input_dim).astype(dtype) @@ -766,7 +766,7 @@ def test_forward_image_scaler(): def verify_convolution(input_dim, filter, padding): - """Convolution""" + """Verify convolution""" dtype = "float32" _, c, h, w = input_dim oc, _, kh, kw = filter @@ -807,8 +807,7 @@ def test_forward_convolution(): 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( From f0c1d0c450ae46ec7131819575f4bf94e7f6b13b Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Wed, 13 Jul 2022 13:20:34 +0000 Subject: [PATCH 073/110] pylint pytorch/test_forward.py --- tests/python/frontend/pytorch/test_forward.py | 181 +++++++++--------- 1 file changed, 93 insertions(+), 88 deletions(-) diff --git a/tests/python/frontend/pytorch/test_forward.py b/tests/python/frontend/pytorch/test_forward.py index 1d07c780b76b..c11b65b13522 100755 --- a/tests/python/frontend/pytorch/test_forward.py +++ b/tests/python/frontend/pytorch/test_forward.py @@ -14,35 +14,34 @@ # 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, missing-function-docstring """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(): torch.backends.cuda.matmul.allow_tf32 = False torch.backends.cudnn.allow_tf32 = False - +# pylint: disable=arguments-renamed def list_ops(expr): class OpLister(tvm.relay.ExprVisitor): def visit_op(self, expr): @@ -94,6 +93,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,16 +109,18 @@ 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") +# pylint: disable=dangerous-default-value def verify_model( model_name, input_data=[], custom_convert_map={}, rtol=1e-5, atol=1e-5, expected_ops=[] ): @@ -157,7 +159,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)]: @@ -210,9 +212,10 @@ def verify_model_with_input( atol=1e-5, assert_shape_only=False, ): + """Generic function to generate and compare Pytorch and TVM output""" 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): @@ -228,13 +231,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(): + """PixelShuffle""" torch.set_grad_enabled(False) input_shape = [1, 144, 16, 16] @@ -806,18 +810,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) @@ -870,7 +874,7 @@ def test_forward_split(): 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 @@ -960,7 +964,7 @@ def test_forward_conv(): 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() @@ -969,7 +973,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() @@ -978,7 +982,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() @@ -987,7 +991,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() @@ -996,7 +1000,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() @@ -1005,7 +1009,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() @@ -1419,7 +1423,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 @@ -1656,7 +1661,7 @@ def test_forward_dense(): 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): @@ -1664,7 +1669,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): @@ -1675,11 +1680,11 @@ 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 @@ -1687,17 +1692,14 @@ def 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)) @@ -2150,8 +2152,8 @@ def forward(self, *args): def 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) @@ -2166,7 +2168,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, @@ -2176,7 +2178,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 ) @@ -2235,6 +2237,7 @@ def test_mobilenet_v2(): 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 @@ -2356,15 +2359,16 @@ 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 +# pylint: disable=dangerous-default-value def verify_model_vm(input_model, ishapes, idtype=None, idata=None, targets=["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)))) @@ -2377,8 +2381,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 @@ -2410,9 +2414,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 @@ -2469,7 +2473,7 @@ 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 @@ -2478,7 +2482,7 @@ def forward(self, inp): class LoopWithIf(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 b = a + b if b.sum() > 0.0: @@ -2547,7 +2551,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) @@ -2969,17 +2973,17 @@ def 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 @@ -3664,8 +3668,8 @@ 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 @@ -3934,10 +3938,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 @@ -4057,8 +4061,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( @@ -4095,7 +4099,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) @@ -4116,16 +4120,16 @@ def test_fn(x, weights=None): def 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(): 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(): @@ -4321,18 +4325,18 @@ 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(): 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) @@ -4417,21 +4421,21 @@ 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 @@ -4515,6 +4519,7 @@ 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(): def test_ior(x, y): @@ -4627,4 +4632,4 @@ def _test_trilu(op, diagonal): if __name__ == "__main__": - pytest.main([__file__]) + tvm.testing.main() From b6e561ece9b8e52638236c0f9ad4923ece3d0da4 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Tue, 26 Jul 2022 08:48:43 +0000 Subject: [PATCH 074/110] Update as @areusch suggest --- tests/python/frontend/darknet/test_forward.py | 1 - tests/python/frontend/keras/test_forward.py | 647 +++++++++--------- .../frontend/oneflow/test_vision_models.py | 7 +- tests/python/frontend/pytorch/test_forward.py | 13 +- .../frontend/tensorflow/test_forward.py | 9 +- tests/python/frontend/tflite/test_forward.py | 9 +- 6 files changed, 341 insertions(+), 345 deletions(-) diff --git a/tests/python/frontend/darknet/test_forward.py b/tests/python/frontend/darknet/test_forward.py index 887482904daa..f8a30a4e10cb 100644 --- a/tests/python/frontend/darknet/test_forward.py +++ b/tests/python/frontend/darknet/test_forward.py @@ -32,7 +32,6 @@ from tvm.relay.frontend.darknet import ACTIVATION from tvm import relay -download_testdata.__test__ = False REPO_URL = "https://github.com/dmlc/web-data/blob/main/darknet/" DARKNET_LIB = "libdarknet2.0.so" diff --git a/tests/python/frontend/keras/test_forward.py b/tests/python/frontend/keras/test_forward.py index 6a2c949a0d0d..87837ccf1d9d 100644 --- a/tests/python/frontend/keras/test_forward.py +++ b/tests/python/frontend/keras/test_forward.py @@ -14,14 +14,13 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. -# pylint: disable=invalid-name, redefined-outer-name, missing-function-docstring +# pylint: disable=invalid-name, missing-docstring """Unit tests for various models and operators""" import numpy as np import tvm from tvm import relay from tvm.contrib import graph_executor import tvm.testing -import keras try: import tensorflow.compat.v1 as tf @@ -31,6 +30,7 @@ from tensorflow import keras as tf_keras # 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: @@ -113,34 +113,35 @@ def to_channels_last(arr): tvm.testing.assert_allclose(kout, tout, rtol=1e-5, atol=1e-5) -def get_mobilenet(keras): +def get_mobilenet(keras_): if hasattr(keras.applications, "MobileNet"): # Keras 2.4.x and older - MobileNet = keras.applications.MobileNet + MobileNet = keras_.applications.MobileNet else: # Keras 2.6.x and newer - MobileNet = keras.applications.mobilenet.MobileNet + MobileNet = keras_.applications.mobilenet.MobileNet return MobileNet @tvm.testing.uses_gpu -class TestKeras: # pylint: disable=missing-class-docstring +class TestKeras: + '''Keras test''' scenarios = [using_classic_keras, using_tensorflow_keras] - 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) + 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) merge_funcs = [ - keras.layers.Add(), - keras.layers.Subtract(), - keras.layers.Multiply(), - keras.layers.Maximum(), - keras.layers.Minimum(), - keras.layers.Average(), - keras.layers.Concatenate(), + keras_.layers.Add(), + keras_.layers.Subtract(), + keras_.layers.Multiply(), + keras_.layers.Maximum(), + keras_.layers.Minimum(), + keras_.layers.Average(), + keras_.layers.Concatenate(), ] for merge_func in merge_funcs: class_name = type(merge_func).__name__ @@ -148,138 +149,138 @@ def test_forward_merge(self, keras): out = merge_func([x, y]) else: out = merge_func([x, y, z]) - keras_model = keras.models.Model(data, out) + keras_model = keras_.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_): + data1 = keras_.layers.Input(shape=(2, 2)) + data2 = keras_.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_.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), ] for merge_func in merge_funcs: out = merge_func([data1, data2]) keras_model = keras.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_): + data = keras_.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_.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), ] for act_func in act_funcs: x = act_func(data) - keras_model = keras.models.Model(data, x) + keras_model = keras_.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_): + 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) 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_.layers.Input(shape=(1, 32)) + x = keras_.layers.Dense(32, activation="relu", kernel_initializer="uniform")(data) + keras_model = keras_.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_): + data = keras_.layers.Input(shape=(2, 3, 4)) + x = keras_.layers.Permute([2, 3, 1])(data) + keras_model = keras_.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_): + keras_model = keras_.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_.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"), ] ) 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_): + data = keras_.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_.layers.MaxPooling2D((3, 3), strides=(1, 1), padding="same")(data) + keras_model = keras_.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_.layers.AveragePooling2D((3, 3), strides=(1, 1), padding="same")(data) + keras_model = keras_.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_): + data = keras_.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_.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"), # Enable when relay conv1dtranspose handles NWC - # keras.layers.Conv1DTranspose(filters=10, kernel_size=(3), padding="valid"), + # 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_.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_): + data = keras_.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_.layers.Conv2D(filters=10, kernel_size=(3, 3), strides=(2, 2), padding="same"), + keras_.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_.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"), ] for conv_func in conv_funcs: x = conv_func(data) - keras_model = keras.models.Model(data, x) + keras_model = keras_.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_): + data = keras_.layers.Input(shape=(32, 32, 3)) batch_norm_funcs = [ - keras.layers.BatchNormalization( + keras_.layers.BatchNormalization( axis=-1, momentum=0.99, epsilon=0.001, @@ -290,7 +291,7 @@ def test_forward_batch_norm(self, keras): moving_mean_initializer="zeros", moving_variance_initializer="ones", ), - keras.layers.BatchNormalization( + keras_.layers.BatchNormalization( axis=-1, momentum=0.99, epsilon=0.001, @@ -301,7 +302,7 @@ def test_forward_batch_norm(self, keras): moving_mean_initializer="zeros", moving_variance_initializer="ones", ), - keras.layers.BatchNormalization( + keras_.layers.BatchNormalization( axis=-1, momentum=0.99, epsilon=0.001, @@ -312,7 +313,7 @@ def test_forward_batch_norm(self, keras): moving_mean_initializer="zeros", moving_variance_initializer="ones", ), - keras.layers.BatchNormalization( + keras_.layers.BatchNormalization( axis=-1, momentum=0.99, epsilon=0.001, @@ -326,143 +327,145 @@ 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_.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_, 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) verify_keras_frontend(keras_model) - def test_forward_reshape(self, keras): + def test_forward_reshape(self, keras_): # 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_.layers.Input(shape=(32, 32, 3)) + x = keras_.layers.Reshape(target_shape=(16, 64, 3))(data) + keras_model = keras_.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_.layers.Input(shape=(32, 8, 3)) + x = keras_.layers.Reshape(target_shape=(256, 3))(data) + keras_model = keras_.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_.layers.Input(shape=(256, 3)) + x = keras_.layers.Reshape(target_shape=(8, 32, 3))(data) + keras_model = keras_.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_.layers.Input(shape=(2, 8)) + x = keras_.layers.Reshape(target_shape=(16,))(data) + keras_model = keras_.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_.layers.Input(shape=(16,)) + x = keras_.layers.Reshape(target_shape=(4, 4))(data) + keras_model = keras_.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_.layers.Input(shape=(2, 8)) + x = keras_.layers.Reshape(target_shape=(4, 4))(data) + keras_model = keras_.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_.layers.Input(shape=(15,)) + x = keras_.layers.Reshape(target_shape=(5, 3))(data) + keras_model = keras_.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_.layers.Input(shape=(3, 2, 4)) + x = keras_.layers.Reshape(target_shape=(3, 8))(data) + keras_model = keras_.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_): + 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) 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_): + 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) 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_): + 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]) verify_keras_frontend(keras_model) - def test_forward_reuse_layers(self, keras): + def test_forward_reuse_layers(self, keras_): # reuse conv2d - data = keras.layers.Input(shape=(32, 32, 3)) - conv2d = keras.layers.Conv2D(8, (3, 3), padding="same") + data = keras_.layers.Input(shape=(32, 32, 3)) + conv2d = keras_.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) + z = keras_.layers.Add()([x, y]) + z = keras_.layers.GlobalAveragePooling2D()(z) + keras_model = keras_.models.Model(data, z) 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_.layers.Input(shape=(32, 32, 3)) + x = keras_.layers.Conv2D(8, (3, 3), padding="same")(data) + add = keras_.layers.Add() x = add([x, x]) x = add([x, x]) - z = keras.layers.GlobalAveragePooling2D()(x) - keras_model = keras.models.Model(data, z) + z = keras_.layers.GlobalAveragePooling2D()(x) + keras_model = keras_.models.Model(data, z) verify_keras_frontend(keras_model) - def test_forward_lstm(self, keras): - data = keras.layers.Input(shape=(10, 32)) + def test_forward_lstm(self, keras_): + data = keras_.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_.layers.LSTM(16), + keras_.layers.LSTM(16, return_sequences=True), + keras_.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_.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_): + data = keras_.layers.Input(shape=(1, 32)) rnn_funcs = [ - keras.layers.LSTM( + keras_.layers.LSTM( units=16, return_state=False, recurrent_activation="sigmoid", activation="tanh" ), - keras.layers.LSTM( + keras_.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_.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( units=16, return_state=False, recurrent_activation="sigmoid", activation="tanh", reset_after=False, ), - keras.layers.GRU( + keras_.layers.GRU( units=16, return_state=False, recurrent_activation="sigmoid", @@ -473,220 +476,220 @@ 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_.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_, layout="NCHW"): + if hasattr(keras_.applications, "VGG16"): # Keras 2.4.x and older - VGG16 = keras.applications.VGG16 + VGG16 = keras_.applications.VGG16 else: # Keras 2.6.x and newer - VGG16 = keras.applications.vgg16.VGG16 + VGG16 = keras_.applications.vgg16.VGG16 keras_model = VGG16( 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_, layout="NCHW"): + if hasattr(keras_.applications, "Xception"): # Keras 2.4.x and older - Xception = keras.applications.Xception + Xception = keras_.applications.Xception else: # Keras 2.6.x and newer - Xception = keras.applications.xception.Xception + Xception = keras_.applications.xception.Xception keras_model = Xception( 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_, layout="NCHW"): + if hasattr(keras_.applications, "ResNet50"): # Keras 2.4.x and older - ResNet50 = keras.applications.ResNet50 + ResNet50 = keras_.applications.ResNet50 else: # Keras 2.6.x and newer - ResNet50 = keras.applications.resnet.ResNet50 + ResNet50 = keras_.applications.resnet.ResNet50 keras_model = ResNet50( 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_, layout="NCHW"): + MobileNet = get_mobilenet(keras_) keras_model = MobileNet( 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_): + data = keras_.layers.Input(shape=(32, 32, 32, 3)) conv_funcs = [ - keras.layers.Conv3D( + keras_.layers.Conv3D( filters=10, kernel_size=(3, 3, 3), strides=(2, 2, 2), padding="same" ), - keras.layers.Conv3D( + keras_.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_.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"), ] for conv_func in conv_funcs: x = conv_func(data) - keras_model = keras.models.Model(data, x) + keras_model = keras_.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_): + data = keras_.layers.Input(shape=(32, 32, 32, 3)) conv_funcs = [ - keras.layers.Conv3DTranspose( + keras_.layers.Conv3DTranspose( filters=10, kernel_size=(3, 3, 3), strides=(2, 2, 2), padding="same" ), - keras.layers.Conv3DTranspose( + keras_.layers.Conv3DTranspose( filters=10, kernel_size=(1, 1, 1), dilation_rate=(1, 1, 1), padding="same" ), - keras.layers.Conv3DTranspose( + keras_.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_.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_.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_): + data = keras_.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_.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"), # 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_.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"), ] for pool_func in pool_funcs: x = pool_func(data) - keras_model = keras.models.Model(data, x) + keras_model = keras_.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_): + 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) 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_): + data = keras_.layers.Input(shape=(32, 32, 32, 3)) pad_funcs = [ # Integer - keras.layers.ZeroPadding3D(padding=2), + keras_.layers.ZeroPadding3D(padding=2), # tuple of 3 ints - keras.layers.ZeroPadding3D(padding=(1, 2, 3)), + keras_.layers.ZeroPadding3D(padding=(1, 2, 3)), # tuple of 3 tuples of 2 ints - keras.layers.ZeroPadding3D(padding=((1, 1), (2, 2), (2, 2))), + keras_.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_.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_.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_): + data = keras_.layers.Input(shape=(2, 4), dtype="int32") + x = keras_.layers.Embedding(10, 3)(data) + keras_model = keras_.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_.layers.Input(shape=(2, 3, 4), dtype="int32") + x = keras_.layers.Embedding(4, 5)(data) + keras_model = keras_.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_.layers.Input(shape=(6, 2, 3, 4), dtype="int32") + x = keras_.layers.Embedding(4, 5)(data) + keras_model = keras_.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_): + data = keras_.layers.Input(shape=(5,), dtype="float32") + x = keras_.layers.Dense(6)(data) + x = keras_.layers.RepeatVector(2)(x) - keras_model = keras.models.Model(data, x) + keras_model = keras_.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_.layers.Input(shape=(10,), dtype="float32") + x = keras_.layers.RepeatVector(3)(data) + keras_model = keras_.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_.layers.Input(shape=(4,), dtype="float32") + x = keras_.layers.RepeatVector(1)(data) + keras_model = keras_.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_): + data = keras_.layers.Input(shape=(32, 32, 32, 1)) pool_funcs = [ # global maxpool - keras.layers.GlobalMaxPooling3D(), + keras_.layers.GlobalMaxPooling3D(), # global avgpool - keras.layers.GlobalAveragePooling3D(), + keras_.layers.GlobalAveragePooling3D(), ] for pool_func in pool_funcs: x = pool_func(data) - keras_model = keras.models.Model(data, x) + keras_model = keras_.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_): + MobileNet = get_mobilenet(keras_) sub_model = MobileNet(include_top=False, weights="imagenet", input_shape=(224, 224, 3)) - keras_model = keras.Sequential( + keras_model = keras_.Sequential( [ sub_model, - keras.layers.GlobalAveragePooling2D(), - keras.layers.Dense(1024, activation="relu"), - keras.layers.Dense(2, activation="sigmoid"), + keras_.layers.GlobalAveragePooling2D(), + keras_.layers.Dense(1024, activation="relu"), + keras_.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_): + data = keras_.layers.Input(shape=(16, 12, 8)) + K = keras_.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_.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])), ] for l2_func in l2_funcs: x = l2_func(data) - keras_model = keras.models.Model(data, x) + keras_model = keras_.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_): + 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) ) 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_.Input(shape=(5, 1)) + dense_layer = keras_.layers.Dense(1) + dense_model = keras_.models.Model( + dense_inputs, keras_.layers.TimeDistributed(dense_layer)(dense_inputs) ) verify_keras_frontend(dense_model, need_transpose=False) @@ -694,39 +697,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_=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) diff --git a/tests/python/frontend/oneflow/test_vision_models.py b/tests/python/frontend/oneflow/test_vision_models.py index d8f5aa6b6001..f82277f98195 100644 --- a/tests/python/frontend/oneflow/test_vision_models.py +++ b/tests/python/frontend/oneflow/test_vision_models.py @@ -14,19 +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, consider-using-f-string -# pylint: disable=arguments-differ, unused-argument, unused-import +# pylint: disable=import-self, invalid-name +# 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 diff --git a/tests/python/frontend/pytorch/test_forward.py b/tests/python/frontend/pytorch/test_forward.py index c11b65b13522..179f8d3c311e 100755 --- a/tests/python/frontend/pytorch/test_forward.py +++ b/tests/python/frontend/pytorch/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, missing-function-docstring +# pylint: disable=import-self, invalid-name, unused-argument, missing-docstring """Unit tests for various models and operators""" import os import platform @@ -41,13 +41,12 @@ torch.backends.cuda.matmul.allow_tf32 = False torch.backends.cudnn.allow_tf32 = False -# pylint: disable=arguments-renamed def list_ops(expr): 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) + 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 = {} @@ -131,7 +130,7 @@ def verify_model( 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: diff --git a/tests/python/frontend/tensorflow/test_forward.py b/tests/python/frontend/tensorflow/test_forward.py index 6f6c0a2cbd02..f425d78e886f 100755 --- a/tests/python/frontend/tensorflow/test_forward.py +++ b/tests/python/frontend/tensorflow/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, unused-variable, redefined-builtin +# pylint: disable=import-self, invalid-name, unused-argument, unused-variable """ Tensorflow testcases ==================== @@ -298,7 +298,7 @@ def is_gpu_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: # pylint: disable=len-as-condition + if gpu_list: print("Tensorflow GPU:", gpu_list) return True else: @@ -502,7 +502,6 @@ def _test_convolution( add_shapes_to_graph_def=True, ): """One iteration of convolution with given shapes and attributes""" - # pylint: disable=dangerous-default-value total_size_1 = np.prod(tensor_in_sizes) total_size_2 = np.prod(filter_in_sizes) # Initializes the input tensor with array containing incrementing @@ -4686,8 +4685,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)) diff --git a/tests/python/frontend/tflite/test_forward.py b/tests/python/frontend/tflite/test_forward.py index fc296ea7590f..bc5ac2e4fe4f 100644 --- a/tests/python/frontend/tflite/test_forward.py +++ b/tests/python/frontend/tflite/test_forward.py @@ -1839,7 +1839,7 @@ def _test_shape(dtype): out = tf.shape(r, out_type=dtype) out = tf.add(out, tf.constant([1], dtype=dtype)) compare_tflite_with_tvm( - [x for x in np.nditer(data)], # pylint: disable=unnecessary-comprehension + list(np.nditer(data)), ["start", "limit", "delta"], [start, limit, delta], [out], @@ -1894,9 +1894,8 @@ 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""" - # pylint: disable= dangerous-default-value if quantized: with tf.Graph().as_default(): quant_min, quant_max = quant_range @@ -2624,9 +2623,9 @@ 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], # pylint: disable=unnecessary-comprehension + list(inputs), [each.name for each in temp], - [each for each in temp], # pylint: disable=unnecessary-comprehension + list(temp), [output], ) From 0092dbb103eda6f32c4d14040494a3bc6d6ded57 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Thu, 28 Jul 2022 06:28:19 +0000 Subject: [PATCH 075/110] reformat by black --- tests/python/frontend/pytorch/test_forward.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/python/frontend/pytorch/test_forward.py b/tests/python/frontend/pytorch/test_forward.py index 179f8d3c311e..2e8a7f31e96a 100755 --- a/tests/python/frontend/pytorch/test_forward.py +++ b/tests/python/frontend/pytorch/test_forward.py @@ -41,6 +41,7 @@ torch.backends.cuda.matmul.allow_tf32 = False torch.backends.cudnn.allow_tf32 = False + def list_ops(expr): class OpLister(tvm.relay.ExprVisitor): def visit_op(self, op): From 75aaee79296d32c480156600378ca0558f35e948 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Thu, 28 Jul 2022 06:32:12 +0000 Subject: [PATCH 076/110] fix redefined-outer-name lint errors --- tests/python/frontend/keras/test_forward.py | 658 ++++++++++---------- 1 file changed, 334 insertions(+), 324 deletions(-) diff --git a/tests/python/frontend/keras/test_forward.py b/tests/python/frontend/keras/test_forward.py index 87837ccf1d9d..b9583b2cf53f 100644 --- a/tests/python/frontend/keras/test_forward.py +++ b/tests/python/frontend/keras/test_forward.py @@ -31,6 +31,7 @@ # 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: @@ -61,8 +62,8 @@ 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_classic_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"): @@ -113,35 +114,36 @@ 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 = keras_mod.applications.MobileNet else: # Keras 2.6.x and newer - MobileNet = keras_.applications.mobilenet.MobileNet + MobileNet = keras_mod.applications.mobilenet.MobileNet return MobileNet @tvm.testing.uses_gpu class TestKeras: - '''Keras test''' + """Keras test""" + scenarios = [using_classic_keras, using_tensorflow_keras] - 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) + def test_forward_merge(self, keras_mod): + data = keras_mod.layers.Input(shape=(32, 32, 3)) + x = keras_mod.layers.Conv2D(8, (3, 3), padding="same")(data) + y = keras_mod.layers.Conv2D(8, (3, 3), padding="same")(x) + z = keras_mod.layers.Conv2D(8, (3, 3), padding="same")(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__ @@ -149,138 +151,140 @@ def test_forward_merge(self, keras_): out = merge_func([x, y]) else: out = merge_func([x, y, z]) - keras_model = keras_.models.Model(data, out) + 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): + 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): + 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): + 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): + 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): + 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"), + # 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): + 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): + 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, @@ -291,7 +295,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, @@ -302,7 +306,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, @@ -313,7 +317,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, @@ -327,145 +331,145 @@ 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): # 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): + 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) + z = keras_mod.layers.Average()([x, y]) + z = keras_mod.layers.GlobalAveragePooling2D()(z) + keras_model = keras_mod.models.Model([data1, data2], z) 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): # 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) + z = keras_mod.layers.Add()([x, y]) + z = keras_mod.layers.GlobalAveragePooling2D()(z) + keras_model = keras_mod.models.Model(data, z) 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) + z = keras_mod.layers.GlobalAveragePooling2D()(x) + keras_model = keras_mod.models.Model(data, z) 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): + 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): + 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( + 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_.layers.GRU( + 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", @@ -476,220 +480,226 @@ 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"): + if hasattr(keras_mod.applications, "VGG16"): # Keras 2.4.x and older - VGG16 = keras_.applications.VGG16 + VGG16 = keras_mod.applications.VGG16 else: # Keras 2.6.x and newer - VGG16 = keras_.applications.vgg16.VGG16 + VGG16 = keras_mod.applications.vgg16.VGG16 keras_model = VGG16( 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"): + if hasattr(keras_mod.applications, "Xception"): # Keras 2.4.x and older - Xception = keras_.applications.Xception + Xception = keras_mod.applications.Xception else: # Keras 2.6.x and newer - Xception = keras_.applications.xception.Xception + Xception = keras_mod.applications.xception.Xception keras_model = Xception( 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"): + if hasattr(keras_mod.applications, "ResNet50"): # Keras 2.4.x and older - ResNet50 = keras_.applications.ResNet50 + ResNet50 = keras_mod.applications.ResNet50 else: # Keras 2.6.x and newer - ResNet50 = keras_.applications.resnet.ResNet50 + ResNet50 = keras_mod.applications.resnet.ResNet50 keras_model = ResNet50( 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 = get_mobilenet(keras_mod) keras_model = MobileNet( 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): + 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): + 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): + 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): + 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): + 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): + 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): + 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): + MobileNet = get_mobilenet(keras_mod) sub_model = MobileNet(include_top=False, weights="imagenet", input_shape=(224, 224, 3)) - keras_model = keras_.Sequential( + 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): + data = keras_mod.layers.Input(shape=(16, 12, 8)) + K = 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.l2_normalize(v, axis=-2)), + keras_mod.layers.Lambda(lambda v: K.l2_normalize(x=v, axis=-1)), + keras_mod.layers.Lambda(lambda v: K.l2_normalize(axis=1, x=v)), + keras_mod.layers.Lambda(lambda v: K.l2_normalize(v, 2)), + keras_mod.layers.Lambda(lambda v: K.l2_normalize(v, axis=3)), + keras_mod.layers.Lambda(lambda v: K.l2_normalize(v, axis=(2, 3))), + keras_mod.layers.Lambda(lambda v: K.l2_normalize(v, (1, 2))), + keras_mod.layers.Lambda(lambda v: K.l2_normalize(v, axis=[-2, -1])), + keras_mod.layers.Lambda(lambda v: K.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): + 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) @@ -697,39 +707,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) From c1f459e2bd110510c3c9efc9b275d282141cfd32 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Thu, 28 Jul 2022 06:48:46 +0000 Subject: [PATCH 077/110] remove rules --- tests/python/frontend/caffe2/test_forward.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/python/frontend/caffe2/test_forward.py b/tests/python/frontend/caffe2/test_forward.py index 09ee8a4a3e67..88849ba20f24 100644 --- a/tests/python/frontend/caffe2/test_forward.py +++ b/tests/python/frontend/caffe2/test_forward.py @@ -14,7 +14,6 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. -# pylint: disable=no-else-return """ Caffe2 testcases ==================== From 92f4114cfdff021cbd56a5f918cb007a03c1944d Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Thu, 28 Jul 2022 07:23:09 +0000 Subject: [PATCH 078/110] remove rules & fix pylint errors --- tests/python/frontend/keras/test_forward.py | 180 +++++++++++--------- 1 file changed, 104 insertions(+), 76 deletions(-) diff --git a/tests/python/frontend/keras/test_forward.py b/tests/python/frontend/keras/test_forward.py index b9583b2cf53f..181c566f9949 100644 --- a/tests/python/frontend/keras/test_forward.py +++ b/tests/python/frontend/keras/test_forward.py @@ -14,7 +14,6 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. -# pylint: disable=invalid-name, missing-docstring """Unit tests for various models and operators""" import numpy as np import tvm @@ -45,10 +44,12 @@ 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: @@ -67,6 +68,7 @@ def pytest_generate_tests(metafunc): 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" @@ -82,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): - 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())] @@ -102,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: @@ -117,12 +119,12 @@ def to_channels_last(arr): def get_mobilenet(keras_mod): if hasattr(keras_mod.applications, "MobileNet"): # Keras 2.4.x and older - MobileNet = keras_mod.applications.MobileNet + mobilenet_mod = keras_mod.applications.MobileNet else: # Keras 2.6.x and newer - MobileNet = keras_mod.applications.mobilenet.MobileNet + mobilenet_mod = keras_mod.applications.mobilenet.MobileNet - return MobileNet + return mobilenet_mod @tvm.testing.uses_gpu @@ -132,10 +134,11 @@ class TestKeras: scenarios = [using_classic_keras, using_tensorflow_keras] def test_forward_merge(self, keras_mod): + """test_forward_merge""" data = keras_mod.layers.Input(shape=(32, 32, 3)) - x = keras_mod.layers.Conv2D(8, (3, 3), padding="same")(data) - y = keras_mod.layers.Conv2D(8, (3, 3), padding="same")(x) - z = keras_mod.layers.Conv2D(8, (3, 3), padding="same")(y) + 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_mod.layers.Add(), keras_mod.layers.Subtract(), @@ -148,13 +151,14 @@ def test_forward_merge(self, keras_mod): 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]) + 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_mod): + """test_forward_merge_dot""" data1 = keras_mod.layers.Input(shape=(2, 2)) data2 = keras_mod.layers.Input(shape=(2, 2)) merge_funcs = [ @@ -171,6 +175,7 @@ def test_forward_merge_dot(self, keras_mod): verify_keras_frontend(keras_model) def test_forward_activations(self, keras_mod): + """test_forward_activations""" data = keras_mod.layers.Input(shape=(32, 32, 3)) act_funcs = [ keras_mod.layers.Activation("softmax"), @@ -206,6 +211,7 @@ def test_forward_activations(self, keras_mod): verify_keras_frontend(keras_model, need_transpose=False, layout="NHWC") 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) @@ -225,6 +231,7 @@ def test_forward_permute(self, keras_mod): verify_keras_frontend(keras_model, need_transpose=False) def test_forward_sequential(self, keras_mod): + """test_forward_sequential""" keras_model = keras_mod.models.Sequential( [ keras_mod.layers.Dense(16, input_dim=32, activation="relu"), @@ -248,6 +255,7 @@ def test_forward_pool(self, keras_mod): verify_keras_frontend(keras_model) def test_forward_conv1d(self, keras_mod): + """test_forward_conv1d""" data = keras_mod.layers.Input(shape=(32, 3)) conv_funcs = [ keras_mod.layers.Conv1D(filters=10, kernel_size=(3,), strides=(2,), padding="same"), @@ -265,6 +273,7 @@ def test_forward_conv1d(self, keras_mod): verify_keras_frontend(keras_model, layout="NWC") def test_forward_conv(self, keras_mod): + """test_forward_conv""" data = keras_mod.layers.Input(shape=(32, 32, 3)) conv_funcs = [ keras_mod.layers.Conv2D(filters=10, kernel_size=(3, 3), strides=(2, 2), padding="same"), @@ -282,6 +291,7 @@ def test_forward_conv(self, keras_mod): verify_keras_frontend(keras_model) 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_mod.layers.BatchNormalization( @@ -341,6 +351,7 @@ def test_forward_upsample(self, keras_mod, interpolation="nearest"): verify_keras_frontend(keras_model) def test_forward_reshape(self, keras_mod): + """test_forward_reshape""" # input_shape len is 3, target_shape len is 3 data = keras_mod.layers.Input(shape=(32, 32, 3)) x = keras_mod.layers.Reshape(target_shape=(16, 64, 3))(data) @@ -383,6 +394,7 @@ def test_forward_reshape(self, keras_mod): verify_keras_frontend(keras_model) 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) @@ -399,9 +411,9 @@ def test_forward_multi_inputs(self, keras_mod): 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) - z = keras_mod.layers.Average()([x, y]) - z = keras_mod.layers.GlobalAveragePooling2D()(z) - keras_model = keras_mod.models.Model([data1, data2], z) + 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_mod): @@ -414,14 +426,15 @@ def test_forward_multi_outputs(self, keras_mod): verify_keras_frontend(keras_model) def test_forward_reuse_layers(self, keras_mod): + """test_forward_reuse_layers""" # reuse conv2d 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_mod.layers.Add()([x, y]) - z = keras_mod.layers.GlobalAveragePooling2D()(z) - keras_model = keras_mod.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_mod.layers.Input(shape=(32, 32, 3)) @@ -429,11 +442,12 @@ def test_forward_reuse_layers(self, keras_mod): add = keras_mod.layers.Add() x = add([x, x]) x = add([x, x]) - z = keras_mod.layers.GlobalAveragePooling2D()(x) - keras_model = keras_mod.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_mod): + """test_forward_lstm""" data = keras_mod.layers.Input(shape=(10, 32)) rnn_funcs = [ keras_mod.layers.LSTM(16), @@ -446,6 +460,7 @@ def test_forward_lstm(self, keras_mod): verify_keras_frontend(keras_model, need_transpose=False) def test_forward_rnn(self, keras_mod): + """test_forward_rnn""" data = keras_mod.layers.Input(shape=(1, 32)) rnn_funcs = [ keras_mod.layers.LSTM( @@ -484,53 +499,57 @@ def test_forward_rnn(self, keras_mod): verify_keras_frontend(keras_model, need_transpose=False) 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_mod.applications.VGG16 + vgg16_mod = keras_mod.applications.VGG16 else: # Keras 2.6.x and newer - VGG16 = keras_mod.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_mod, layout="NCHW"): + """test_forward_vgg16""" if hasattr(keras_mod.applications, "Xception"): # Keras 2.4.x and older - Xception = keras_mod.applications.Xception + xception_mod = keras_mod.applications.Xception else: # Keras 2.6.x and newer - Xception = keras_mod.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_mod, layout="NCHW"): + """test_forward_resnet50""" if hasattr(keras_mod.applications, "ResNet50"): # Keras 2.4.x and older - ResNet50 = keras_mod.applications.ResNet50 + resnet50_mod = keras_mod.applications.ResNet50 else: # Keras 2.6.x and newer - ResNet50 = keras_mod.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_mod, layout="NCHW"): - MobileNet = get_mobilenet(keras_mod) + 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_mod): + """test_forward_conv3d""" data = keras_mod.layers.Input(shape=(32, 32, 32, 3)) conv_funcs = [ keras_mod.layers.Conv3D( @@ -550,6 +569,7 @@ def test_forward_conv3d(self, keras_mod): verify_keras_frontend(keras_model, layout="NDHWC") 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_mod.layers.Conv3DTranspose( @@ -569,6 +589,7 @@ def test_forward_conv3d_transpose(self, keras_mod): verify_keras_frontend(keras_model, layout="NDHWC") def test_forward_pool3d(self, keras_mod): + """test_forward_pool3d""" data = keras_mod.layers.Input(shape=(32, 32, 32, 1)) pool_funcs = [ # maxpool keras_mod.layers.MaxPooling3D(pool_size=(2, 2, 2), strides=(1, 1, 1), padding="same"), @@ -593,6 +614,7 @@ def test_forward_upsample3d(self, keras_mod): verify_keras_frontend(keras_model, layout="NDHWC") 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_mod.layers.ZeroPadding3D(padding=2), @@ -609,6 +631,7 @@ def test_forward_zero_padding3d(self, keras_mod): verify_keras_frontend(keras_model, layout="NDHWC") 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) @@ -625,6 +648,7 @@ def test_forward_embedding(self, keras_mod): verify_keras_frontend(keras_model, need_transpose=False) 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) @@ -643,6 +667,7 @@ def test_forward_repeat_vector(self, keras_mod): verify_keras_frontend(keras_model, need_transpose=False) 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_mod.layers.GlobalMaxPooling3D(), @@ -655,9 +680,10 @@ def test_forward_global_pool3d(self, keras_mod): verify_keras_frontend(keras_model, layout="NDHWC") def test_forward_nested_layers(self, keras_mod): - MobileNet = get_mobilenet(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)) + sub_model = mobilenet_mod(include_top=False, weights="imagenet", input_shape=(224, 224, 3)) keras_model = keras_mod.Sequential( [ sub_model, @@ -669,18 +695,19 @@ def test_forward_nested_layers(self, keras_mod): verify_keras_frontend(keras_model) def test_forward_l2_normalize(self, keras_mod): + """test_forward_l2_normalize""" data = keras_mod.layers.Input(shape=(16, 12, 8)) - K = keras_mod.backend + k_backend = keras_mod.backend l2_funcs = [ - keras_mod.layers.Lambda(lambda v: K.l2_normalize(v, axis=-2)), - keras_mod.layers.Lambda(lambda v: K.l2_normalize(x=v, axis=-1)), - keras_mod.layers.Lambda(lambda v: K.l2_normalize(axis=1, x=v)), - keras_mod.layers.Lambda(lambda v: K.l2_normalize(v, 2)), - keras_mod.layers.Lambda(lambda v: K.l2_normalize(v, axis=3)), - keras_mod.layers.Lambda(lambda v: K.l2_normalize(v, axis=(2, 3))), - keras_mod.layers.Lambda(lambda v: K.l2_normalize(v, (1, 2))), - keras_mod.layers.Lambda(lambda v: K.l2_normalize(v, axis=[-2, -1])), - keras_mod.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) @@ -689,6 +716,7 @@ def test_forward_l2_normalize(self, keras_mod): verify_keras_frontend(keras_model, layout="NHWC") 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( @@ -707,30 +735,30 @@ def test_forward_time_distributed(self, keras_mod): if __name__ == "__main__": for k in [keras, tf_keras]: sut = TestKeras() - 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_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) From 37815c5566a43cf63a8e1966865ce29a3f9f33e2 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Thu, 28 Jul 2022 08:10:51 +0000 Subject: [PATCH 079/110] Remove all unused_var and fix other pylint errors --- .../frontend/tensorflow/test_forward.py | 188 +++++++++--------- 1 file changed, 92 insertions(+), 96 deletions(-) diff --git a/tests/python/frontend/tensorflow/test_forward.py b/tests/python/frontend/tensorflow/test_forward.py index f425d78e886f..a86c446d5919 100755 --- a/tests/python/frontend/tensorflow/test_forward.py +++ b/tests/python/frontend/tensorflow/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, unused-variable +# pylint: disable=import-self, invalid-name, unused-argument """ Tensorflow testcases ==================== @@ -25,13 +25,17 @@ import threading import platform +import os.path import numpy as np import pytest +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 import tvm import tvm.relay.testing.tf as tf_testing @@ -111,9 +115,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( @@ -186,7 +190,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 @@ -200,7 +203,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 @@ -260,14 +263,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( @@ -285,17 +288,16 @@ 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 enumerate(tf_output): - if not isinstance(tf_output[i], np.ndarray): + 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_output[i], tvm_output[i], atol=1e-5, rtol=1e-5) + 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 gpu_list: @@ -1473,7 +1475,7 @@ 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( @@ -1481,8 +1483,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"]: @@ -1502,20 +1504,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) @@ -1541,8 +1543,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"]: @@ -1564,11 +1566,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") @@ -1595,7 +1597,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"]: @@ -1612,14 +1614,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"]: @@ -1642,7 +1644,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") @@ -1662,8 +1664,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") @@ -1715,7 +1717,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") @@ -1800,7 +1802,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 ) @@ -1821,8 +1823,8 @@ def test_read_variable_op(target, dev): out_names=out_name, num_output=len(out_name), ) - for i, _ in enumerate(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() @@ -2929,7 +2931,7 @@ def _test_split(in_shape, axis, num_or_size_splits, dtype): 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 ) split = tf.split(in_data, num_or_size_splits, axis=axis) @@ -3084,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( @@ -3105,8 +3107,8 @@ def test_forward_multi_output(): 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"] @@ -3124,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 enumerate(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) ####################################################################### @@ -3301,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") @@ -3749,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: @@ -3779,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") @@ -3789,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") @@ -3799,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") @@ -3808,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") @@ -3826,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 @@ -3870,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() @@ -3948,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 @@ -3981,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, @@ -4090,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) @@ -4165,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) @@ -4469,8 +4465,8 @@ 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") @@ -4787,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") @@ -4874,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 @@ -4915,7 +4911,7 @@ 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(): @@ -4951,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") @@ -4965,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"): @@ -5119,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(): @@ -5172,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( @@ -5300,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, @@ -5320,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 ) @@ -5344,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, @@ -5365,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( @@ -5384,7 +5380,7 @@ def constantsFn(x, y): a = tf.constant(20000, name="a") b = tf.constant(40000, name="b") - spopFn = gen_functional_ops.StatefulPartitionedCall( + _ = gen_functional_ops.StatefulPartitionedCall( args=[a, b], Tout=[tf.int32], f=constantsFn ) @@ -5447,7 +5443,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: @@ -5473,11 +5469,12 @@ 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") @@ -5520,18 +5517,18 @@ def test_forward_dynamic_input_shape(): 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, @@ -5575,12 +5572,10 @@ def generateData(): state_per_layer_list = tf.unstack(init_state, axis=0) rnn_tuple_state = tuple( - [ - tf.nn.rnn_cell.LSTMStateTuple( + list(tf.nn.rnn_cell.LSTMStateTuple( state_per_layer_list[idx][0], state_per_layer_list[idx][1] ) - for idx in range(num_layers) - ] + for idx in range(num_layers)) ) # Forward passes @@ -5596,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 @@ -5629,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"], @@ -5640,8 +5635,8 @@ def lstm_cell(): ) # Compare result - for i, _ in enumerate(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) ####################################################################### @@ -5724,9 +5719,9 @@ def test_moments(): 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 = """ @@ -5764,4 +5759,5 @@ def test_invert_permutation(): if __name__ == "__main__": + test_tensor_array_scatter() pytest.main([__file__]) From 89e7b0e28429ae6f915005102b5d42d92e3b282b Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Thu, 28 Jul 2022 08:58:38 +0000 Subject: [PATCH 080/110] reformatted by black --- tests/python/frontend/tensorflow/test_forward.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/tests/python/frontend/tensorflow/test_forward.py b/tests/python/frontend/tensorflow/test_forward.py index a86c446d5919..80c70bb03f57 100755 --- a/tests/python/frontend/tensorflow/test_forward.py +++ b/tests/python/frontend/tensorflow/test_forward.py @@ -1516,7 +1516,7 @@ def run(dtype_str, infer_shape): 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)] # pylint: disable=unnecessary-comprehension + 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) @@ -2931,9 +2931,7 @@ def _test_split(in_shape, axis, num_or_size_splits, dtype): tf.reset_default_graph() with tf.Graph().as_default(): in_data = tf.placeholder(dtype, in_shape, name="in_data") - _ = ( - 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] @@ -5380,9 +5378,7 @@ def constantsFn(x, y): a = tf.constant(20000, name="a") b = tf.constant(40000, name="b") - _ = 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 @@ -5572,10 +5568,12 @@ def generateData(): state_per_layer_list = tf.unstack(init_state, axis=0) rnn_tuple_state = tuple( - list(tf.nn.rnn_cell.LSTMStateTuple( + list( + tf.nn.rnn_cell.LSTMStateTuple( state_per_layer_list[idx][0], state_per_layer_list[idx][1] ) - for idx in range(num_layers)) + for idx in range(num_layers) + ) ) # Forward passes From 454189e019259e5715cd2c80d40563444fffdfdd Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Thu, 28 Jul 2022 10:18:55 +0000 Subject: [PATCH 081/110] [CI] Apply linting rules to onnx tests --- tests/python/frontend/onnx/test_forward.py | 815 ++++++++++++--------- 1 file changed, 463 insertions(+), 352 deletions(-) diff --git a/tests/python/frontend/onnx/test_forward.py b/tests/python/frontend/onnx/test_forward.py index 25b52114119e..94f7598f3264 100644 --- a/tests/python/frontend/onnx/test_forward.py +++ b/tests/python/frontend/onnx/test_forward.py @@ -47,6 +47,7 @@ def get_input_data_shape_dict(graph_def, input_data): + """Get input data shape""" if isinstance(input_data, list): input_names = {} shape_dict = {} @@ -142,6 +143,7 @@ def get_tvm_output( def get_onnxruntime_output(model, inputs): + """Generic function to generate onnxruntime output""" rep = onnxruntime.backend.prepare(model.SerializeToString(), "CPU") if isinstance(inputs, list) and len(inputs) == 1: inp = inputs[0] @@ -170,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 @@ -222,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, @@ -241,8 +245,8 @@ def verify_with_ort( def quantize_and_verify_with_ort( onnx_model, input_names, input_shapes, target, dev, rtol=1e-5, atol=1e-5 ): + """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): @@ -260,13 +264,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_quant = os.path.join(t_dir.temp_dir, "model.quant.onnx") + _ = quantize_static( # pylint: disable=assignment-from-no-return model_fp32, model_quant, RandomDataReader() - ) # pylint: disable=assignment-from-no-return + ) # opt_level=1 will cause error with qnn lowering model = onnx.load(model_quant) verify_with_ort_with_inputs( @@ -291,6 +295,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) @@ -324,6 +329,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) @@ -359,6 +365,7 @@ 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": @@ -425,9 +432,10 @@ 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"], block_size=block_size ) graph = helper.make_graph( @@ -444,14 +452,15 @@ 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"], block_size=block_size ) graph = helper.make_graph( @@ -470,6 +479,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) @@ -505,6 +515,7 @@ 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] @@ -538,6 +549,7 @@ 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, @@ -573,6 +585,7 @@ 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) @@ -594,6 +607,7 @@ 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) @@ -613,6 +627,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) @@ -631,6 +646,7 @@ 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") @@ -666,6 +682,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 @@ -711,6 +728,7 @@ 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") @@ -745,6 +763,7 @@ 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") @@ -777,6 +796,7 @@ 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) @@ -1022,6 +1042,7 @@ def test_ceil(target, dev): @tvm.testing.parametrize_targets def test_clip(target, dev): + """test_clip""" _test_onnx_op_elementwise( target, dev, @@ -1061,6 +1082,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]), @@ -1113,11 +1135,13 @@ 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") @@ -1166,6 +1190,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 @@ -1196,6 +1221,7 @@ 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) @@ -1247,6 +1273,7 @@ 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]] @@ -1277,6 +1304,7 @@ 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") @@ -1325,6 +1353,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] @@ -1358,6 +1387,7 @@ 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" @@ -1398,6 +1428,7 @@ 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") @@ -1432,7 +1463,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]) @@ -1449,6 +1480,7 @@ 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) @@ -1477,6 +1509,7 @@ 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) @@ -1512,6 +1545,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) @@ -1532,6 +1566,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) @@ -1555,6 +1590,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) @@ -1575,6 +1611,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) @@ -1620,6 +1657,7 @@ 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 @@ -1670,6 +1708,7 @@ 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" @@ -1699,6 +1738,7 @@ 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" @@ -1728,6 +1768,7 @@ 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" @@ -1757,6 +1798,7 @@ 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" @@ -1784,6 +1826,7 @@ 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) @@ -1827,6 +1870,7 @@ 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", @@ -1861,6 +1905,7 @@ 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 @@ -1962,6 +2007,7 @@ 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 @@ -2037,6 +2083,7 @@ 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] @@ -2065,7 +2112,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, ) @@ -2080,7 +2127,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)) ], @@ -2131,14 +2178,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), @@ -2155,41 +2203,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)), @@ -2271,6 +2320,7 @@ 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"]) @@ -2303,8 +2353,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 @@ -2313,7 +2363,7 @@ def ThresholdedRelu_x(x, alpha): target, dev, (2, 4, 5, 6), - ThresholdedRelu_x, + thresholded_relu_x, {"alpha": 0.25}, "float32", "ThresholdedRelu", @@ -2322,7 +2372,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, @@ -2337,7 +2387,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) @@ -2388,14 +2438,15 @@ 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) @@ -2425,6 +2476,7 @@ 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) @@ -2477,6 +2529,7 @@ 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( @@ -2496,12 +2549,13 @@ 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( @@ -2514,12 +2568,13 @@ 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"] @@ -2587,6 +2642,7 @@ 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) @@ -2639,6 +2695,7 @@ 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"] @@ -2671,6 +2728,7 @@ 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( @@ -2704,6 +2762,7 @@ 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, @@ -2778,112 +2837,113 @@ 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, @@ -3015,67 +3075,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 @@ -3090,36 +3150,37 @@ 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(N, dims), + repeat(4, dims), + repeat(2, dims), + repeat(1, dims), auto_pad="SAME_LOWER", ) @tvm.testing.parametrize_targets def test_unsqueeze_constant(target, dev): + """test_unsqueeze_constant""" class Flatten(Module): - def forward(self, input): - return input.view(input.size(0), -1) + 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)) @@ -3131,15 +3192,16 @@ 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 @@ -3256,6 +3318,7 @@ 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) @@ -3264,7 +3327,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"]) @@ -3301,6 +3364,7 @@ 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" ): @@ -3413,6 +3477,7 @@ 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) @@ -3447,6 +3512,7 @@ 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) @@ -3497,6 +3563,7 @@ 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") @@ -3528,6 +3595,7 @@ 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( @@ -3574,6 +3642,7 @@ 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: @@ -3700,6 +3769,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"], @@ -3720,7 +3790,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) @@ -3759,6 +3829,7 @@ def verify_rnn( target=None, dev=None, ): + """verify_rnn""" if rnn_type == "LSTM": multiplier = 4 elif rnn_type == "GRU": @@ -3767,7 +3838,7 @@ def verify_rnn( raise NotImplementedError(f"{rnn_type} RNNs not yet supported.") if directions not in [1, 2]: - raise ValueError(f"Direction should be either 1 or 2 (for bidirectional LSTMs)") + raise ValueError("Direction should be either 1 or 2 (for bidirectional LSTMs)") def get_inputs(): input_names = [] @@ -3884,6 +3955,7 @@ def register(name, shape, proto_type): @tvm.testing.parametrize_targets def test_lstm(target, dev): + """test_lstm""" for directions in [1, 2]: # No bias. verify_rnn( @@ -4034,6 +4106,7 @@ def test_lstm(target, dev): @tvm.testing.parametrize_targets def test_gru(target, dev): + """test_gru""" # Set seed for test reproduction np.random.seed(137) for directions in [1, 2]: @@ -4191,6 +4264,7 @@ 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,), []), @@ -4338,6 +4412,7 @@ 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", @@ -4369,9 +4444,10 @@ 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 @@ -4400,7 +4476,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]: @@ -4415,6 +4491,7 @@ 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, @@ -4484,6 +4561,7 @@ 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 ): @@ -4577,6 +4655,7 @@ 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]) @@ -4635,7 +4714,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], @@ -4701,7 +4780,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], @@ -4814,6 +4893,7 @@ 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. @@ -4839,12 +4919,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) @@ -4877,8 +4957,8 @@ def append_constant_nodes(nodes, outputs, expected, name): tvm_out = [tvm_out] for i, _ in enumerate(tvm_out): tvm.testing.assert_allclose( - correct_out[i], tvm_out[i], rtol=1e-05, atol=1e-05 - ) # pylint: disable=unnecessary-list-index-lookup + correct_out[i], tvm_out[i], rtol=1e-05, atol=1e-05 # pylint: disable=unnecessary-list-index-lookup + ) # Confirm that if works with cond as an array or scalar. verify_if(cond_array=False, num_outputs=1) @@ -4889,6 +4969,7 @@ 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", @@ -4918,6 +4999,7 @@ 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 = [ @@ -4971,23 +5053,24 @@ 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", @@ -5018,7 +5101,8 @@ 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"], @@ -5034,11 +5118,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, @@ -5052,7 +5136,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 = ( @@ -5086,17 +5170,18 @@ 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"] @@ -5270,6 +5355,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") @@ -5325,6 +5411,7 @@ def test_onnx_nodes(target, dev, onnx_test): def test_wrong_input(): + """test_wrong_input""" node = helper.make_node( "Softplus", inputs=["X"], @@ -5351,10 +5438,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, @@ -5389,7 +5477,8 @@ 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().__init__() self.indices = indices @@ -5400,7 +5489,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, @@ -5417,7 +5506,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) @@ -5446,7 +5535,7 @@ def verify_index_put_slice(data_shape, value_shape, accumulate): 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) @@ -5460,6 +5549,7 @@ 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", @@ -5497,6 +5587,7 @@ 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", @@ -5523,6 +5614,7 @@ 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", @@ -5555,6 +5647,7 @@ def verify_biasgelu(x, bias): @tvm.testing.parametrize_targets def test_embedlayernormalization(target, dev): + """test_embedlayernormalization""" def verify_embedlayernormalization( input_ids, segment_ids, @@ -5663,7 +5756,8 @@ 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"], @@ -5678,7 +5772,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( @@ -5686,7 +5780,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) ), @@ -5699,8 +5793,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, @@ -5714,17 +5808,18 @@ 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"], @@ -5738,20 +5833,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 @@ -5759,18 +5854,19 @@ 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, @@ -5879,113 +5975,114 @@ 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 = [] @@ -6018,10 +6115,11 @@ 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)), @@ -6049,10 +6147,11 @@ 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)), @@ -6082,6 +6181,7 @@ 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") @@ -6108,9 +6208,10 @@ 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))] @@ -6132,6 +6233,7 @@ def verify_qlinearsigmoid(a_shape): @tvm.testing.parametrize_targets 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( @@ -6190,7 +6292,8 @@ def get_random_uniform(shape, dtype="float32", high=1.0, low=0.0, seed=None): @tvm.testing.parametrize_targets 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) @@ -6202,7 +6305,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( @@ -6212,33 +6315,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, @@ -6258,6 +6361,7 @@ def get_random_uniform_like(input, shape, dtype=None, high=1.0, low=0.0, seed=No @tvm.testing.parametrize_targets 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( @@ -6296,7 +6400,8 @@ def get_random_normal(shape, dtype="float32", scale=1.0, mean=0.0, seed=None): @tvm.testing.parametrize_targets 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 @@ -6312,20 +6417,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) @@ -6333,6 +6440,7 @@ def get_random_normal_like(input, shape, dtype="float32", scale=1.0, mean=0.0, s @tvm.testing.parametrize_targets def test_convinteger(target, dev): + """test_convinteger""" def verify_convinteger( x_shape, w_shape, @@ -6401,90 +6509,91 @@ 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, @@ -6646,8 +6755,9 @@ 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) @@ -6681,15 +6791,16 @@ 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 = [] @@ -6699,7 +6810,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. From 86d475caf72d0cef42924c4adf0fde0b3c38ac22 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Thu, 28 Jul 2022 10:34:12 +0000 Subject: [PATCH 082/110] Disable unused-argument is for some unused-argument in function can not be removed --- tests/python/frontend/onnx/test_forward.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python/frontend/onnx/test_forward.py b/tests/python/frontend/onnx/test_forward.py index 94f7598f3264..2016aea7ad0a 100644 --- a/tests/python/frontend/onnx/test_forward.py +++ b/tests/python/frontend/onnx/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, unused-variable, missing-function-docstring, redefined-builtin, consider-using-f-string +# pylint: disable=unused-argument """ ONNX testcases ================ From 6660b1db75065b1421fa7c917ddb592412fdc62d Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Thu, 28 Jul 2022 12:09:54 +0000 Subject: [PATCH 083/110] Fix ci errors --- tests/python/frontend/tensorflow/test_forward.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/python/frontend/tensorflow/test_forward.py b/tests/python/frontend/tensorflow/test_forward.py index 80c70bb03f57..3892a4239e53 100755 --- a/tests/python/frontend/tensorflow/test_forward.py +++ b/tests/python/frontend/tensorflow/test_forward.py @@ -54,6 +54,7 @@ from tensorflow.python.framework import ops from tensorflow.python.framework import dtypes from tensorflow.python.ops import gen_functional_ops +from tensorflow.python.client import device_lib try: import tensorflow.compat.v1 as tf @@ -5113,7 +5114,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"], "f{name}:0") + compare_tf_with_tvm([data], ["in_data:0"], f"{name}:0") def test_forward_isinf(): From 0b2d83f348d9167f6ee83d65a9852ada769ec264 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Thu, 28 Jul 2022 13:32:12 +0000 Subject: [PATCH 084/110] reformatted by black --- tests/python/frontend/onnx/test_forward.py | 92 +++++++++++++++++++++- 1 file changed, 89 insertions(+), 3 deletions(-) diff --git a/tests/python/frontend/onnx/test_forward.py b/tests/python/frontend/onnx/test_forward.py index 2016aea7ad0a..4afcddf9a4d8 100644 --- a/tests/python/frontend/onnx/test_forward.py +++ b/tests/python/frontend/onnx/test_forward.py @@ -247,6 +247,7 @@ def quantize_and_verify_with_ort( ): """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): @@ -268,7 +269,7 @@ def get_next(self): model_fp32 = os.path.join(t_dir.temp_dir, "model.onnx") onnx.save_model(onnx_model, model_fp32) model_quant = os.path.join(t_dir.temp_dir, "model.quant.onnx") - _ = quantize_static( # pylint: disable=assignment-from-no-return + _ = quantize_static( # pylint: disable=assignment-from-no-return model_fp32, model_quant, RandomDataReader() ) # opt_level=1 will cause error with qnn lowering @@ -366,6 +367,7 @@ 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": @@ -433,6 +435,7 @@ def _test_expand(name, data, shape, ref_data, dtype="int32"): @tvm.testing.parametrize_targets def test_depth_to_space(target, dev): """test_depth_to_space""" + def verify_depth_to_space(inshape, outshape, mode, block_size): node = onnx.helper.make_node( "DepthToSpace", inputs=["x"], outputs=["y"], block_size=block_size @@ -458,6 +461,7 @@ def verify_depth_to_space(inshape, outshape, mode, block_size): @tvm.testing.parametrize_targets def test_space_to_depth(target, dev): """test_space_to_depth""" + def verify_space_to_depth(inshape, outshape, block_size): node = onnx.helper.make_node( "SpaceToDepth", inputs=["x"], outputs=["y"], block_size=block_size @@ -516,6 +520,7 @@ 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] @@ -550,6 +555,7 @@ 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, @@ -586,6 +592,7 @@ 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) @@ -608,6 +615,7 @@ 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) @@ -647,6 +655,7 @@ 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") @@ -729,6 +738,7 @@ 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") @@ -764,6 +774,7 @@ 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") @@ -797,6 +808,7 @@ 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) @@ -1142,6 +1154,7 @@ def test_isnan(target, dev): @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") @@ -1222,6 +1235,7 @@ 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) @@ -1274,6 +1288,7 @@ 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]] @@ -1305,6 +1320,7 @@ 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") @@ -1388,6 +1404,7 @@ 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" @@ -1429,6 +1446,7 @@ 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") @@ -1481,6 +1499,7 @@ 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) @@ -1510,6 +1529,7 @@ 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) @@ -1658,6 +1678,7 @@ def test_upsample3d_trilinear(target, dev): @tvm.testing.parametrize_targets def test_softmax(target, dev): """test_softmax""" + def verify_softmax(inshape, axis, opset=None, dynamic=False): opname = "Softmax" outshape = inshape @@ -1709,6 +1730,7 @@ 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" @@ -1739,6 +1761,7 @@ 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" @@ -1769,6 +1792,7 @@ 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" @@ -1799,6 +1823,7 @@ 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" @@ -1827,6 +1852,7 @@ def verify_hardsigmoid(input_dim, alpha, beta): @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) @@ -1871,6 +1897,7 @@ 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", @@ -1906,6 +1933,7 @@ 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 @@ -2008,6 +2036,7 @@ 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 @@ -2084,6 +2113,7 @@ 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] @@ -2321,6 +2351,7 @@ 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"]) @@ -2447,6 +2478,7 @@ def sign_x(x): @tvm.testing.parametrize_targets def test_not(target, dev): """test_not""" + def verify_not(indata, dtype): x = indata.astype(dtype) @@ -2477,6 +2509,7 @@ 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) @@ -2530,6 +2563,7 @@ 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( @@ -2556,6 +2590,7 @@ def verify_tile_v6(indata, repeats, outdata): @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( @@ -2575,6 +2610,7 @@ def verify_erf(indata, outdata): @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"] @@ -2643,6 +2679,7 @@ 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) @@ -2696,6 +2733,7 @@ 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"] @@ -2729,6 +2767,7 @@ 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( @@ -2763,6 +2802,7 @@ 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, @@ -2944,6 +2984,7 @@ def repeat(num, dims): @tvm.testing.parametrize_targets def test_convtranspose(target, dev): """test_convtranspose""" + def verify_convtranspose_with_output_shape( x_shape, w_shape, @@ -3175,6 +3216,7 @@ def repeat(num, dims): @tvm.testing.parametrize_targets def test_unsqueeze_constant(target, dev): """test_unsqueeze_constant""" + class Flatten(Module): def forward(self, input_): return input_.view(input_.size(0), -1) @@ -3193,6 +3235,7 @@ 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"): _ = np.random.uniform(size=x_shape).astype("float32") @@ -3319,6 +3362,7 @@ 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) @@ -3365,6 +3409,7 @@ def verify_global_pooling(x_shape, mode): @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" ): @@ -3478,6 +3523,7 @@ 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) @@ -3513,6 +3559,7 @@ 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) @@ -3564,6 +3611,7 @@ 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") @@ -3596,6 +3644,7 @@ 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( @@ -3642,7 +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""" + """test_lppool""" + def verify_lppool(x_shape, kernel_shape, p, strides, pads, out_shape, auto_pad="NOTSET"): kwargs = {} if p is not None: @@ -4265,6 +4315,7 @@ 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,), []), @@ -4413,6 +4464,7 @@ 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", @@ -4445,6 +4497,7 @@ def verify_nonzero(indata, outdata, dtype): @tvm.testing.parametrize_targets def test_topk(target, dev): """test_topk""" + def verify_topk(input_dims, k, axis=-1): output_dims = list(input_dims) output_dims[axis] = k @@ -4492,6 +4545,7 @@ 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, @@ -4562,6 +4616,7 @@ 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 ): @@ -4656,6 +4711,7 @@ 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]) @@ -4894,6 +4950,7 @@ 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. @@ -4957,7 +5014,10 @@ def append_constant_nodes(nodes, outputs, expected, name): tvm_out = [tvm_out] for i, _ in enumerate(tvm_out): tvm.testing.assert_allclose( - correct_out[i], tvm_out[i], rtol=1e-05, atol=1e-05 # pylint: disable=unnecessary-list-index-lookup + 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. @@ -4970,6 +5030,7 @@ 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", @@ -5000,6 +5061,7 @@ 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 = [ @@ -5071,6 +5133,7 @@ def verify_maxunpool(data, indices, kernel_shape, strides, output_shape=None, pa @tvm.testing.parametrize_targets def test_softplus(target, dev): """test_softplus""" + def verify_softplus(indata): node = helper.make_node( "Softplus", @@ -5102,6 +5165,7 @@ def verify_softplus(indata): @tvm.testing.parametrize_targets def test_cumsum(target, dev): """test_cumsum""" + def verify_cumsum(indata, axis, exclusive=0, reverse=0, dtype="float32"): cumsum_node = onnx.helper.make_node( "CumSum", @@ -5182,6 +5246,7 @@ def verify_cumsum(indata, axis, exclusive=0, reverse=0, dtype="float32"): @tvm.testing.parametrize_targets def test_eyelike(target, dev): """test_eyelike""" + def verify_eyelike(indata, dynamic=False): node_list = [] eyelike_inputs = ["X"] @@ -5478,6 +5543,7 @@ def verify_embedding_bag(num_embedding, embedding_dim, data_shape, num_bags=None @tvm.testing.parametrize_targets def test_index_put(target, dev): """test_index_put""" + class IndexPutModel(torch.nn.Module): def __init__(self, indices, values, accumulate): super().__init__() @@ -5550,6 +5616,7 @@ 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", @@ -5588,6 +5655,7 @@ 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", @@ -5615,6 +5683,7 @@ 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", @@ -5648,6 +5717,7 @@ def verify_biasgelu(x, bias): @tvm.testing.parametrize_targets def test_embedlayernormalization(target, dev): """test_embedlayernormalization""" + def verify_embedlayernormalization( input_ids, segment_ids, @@ -5757,6 +5827,7 @@ def verify_embedlayernormalization( @tvm.testing.parametrize_targets def test_attention(target, dev): """test_attention""" + def verify_attention(input_, weight, bias, mask_index, num_heads): node = onnx.helper.make_node( "Attention", @@ -5819,6 +5890,7 @@ def verify_attention(input_, weight, bias, mask_index, num_heads): @tvm.testing.parametrize_targets def test_skiplayernormalization(target, dev): """test_skiplayernormalization""" + def verify_skiplayernormalization(input_, skip, gamma, beta, bias): node = onnx.helper.make_node( "SkipLayerNormalization", @@ -5867,6 +5939,7 @@ def verify_skiplayernormalization(input_, skip, gamma, beta, bias): @tvm.testing.parametrize_targets def test_qlinearconv(target, dev): """test_qlinearconv""" + def verify_qlinearconv( x_shape, w_shape, @@ -6083,6 +6156,7 @@ def repeat(num, dims): @tvm.testing.parametrize_targets def test_qlinearconcat(target, dev): """test_qlinearconcat""" + def verify_qlinearconcat(shapes, out_shape, axis=None): input_names = [] input_values = [] @@ -6116,6 +6190,7 @@ 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): _ = np.random.random(a_shape).astype("float32") @@ -6148,6 +6223,7 @@ 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): _ = np.random.random(a_shape).astype("float32") @@ -6182,6 +6258,7 @@ def verify_qlinearmul(a_shape, b_shape, c_shape): @tvm.testing.parametrize_targets def test_qlinearleakyrelu(target, dev): """test_qlinearleakyrelu""" + def verify_qlinearleakyrelu(inshape, kwargs): in_array = np.random.random(inshape).astype("float32") @@ -6209,6 +6286,7 @@ def verify_qlinearleakyrelu(inshape, kwargs): @tvm.testing.parametrize_targets def test_qlinearsigmoid(target, dev): """test_qlinearsigmoid""" + def verify_qlinearsigmoid(a_shape): _ = np.random.random(a_shape).astype("float32") @@ -6234,6 +6312,7 @@ def verify_qlinearsigmoid(a_shape): @tvm.testing.parametrize_targets 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( @@ -6293,6 +6372,7 @@ def get_random_uniform(shape, dtype="float32", high=1.0, low=0.0, seed=None): @tvm.testing.parametrize_targets def test_random_uniform_like(target, dev): """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: @@ -6362,6 +6442,7 @@ def get_random_uniform_like(input_, shape, dtype=None, high=1.0, low=0.0, seed=N @tvm.testing.parametrize_targets 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( @@ -6401,6 +6482,7 @@ def get_random_normal(shape, dtype="float32", scale=1.0, mean=0.0, seed=None): @tvm.testing.parametrize_targets def test_random_normal_like(target, dev): """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( @@ -6441,6 +6523,7 @@ def get_random_normal_like(input_, shape, dtype="float32", scale=1.0, mean=0.0, @tvm.testing.parametrize_targets def test_convinteger(target, dev): """test_convinteger""" + def verify_convinteger( x_shape, w_shape, @@ -6594,6 +6677,7 @@ def repeat(num, dims): @tvm.testing.parametrize_targets def test_scan(target, dev): """test_scan""" + def verify_scan( input_shapes, output_shapes, @@ -6757,6 +6841,7 @@ def verify_scan( @tvm.testing.parametrize_targets 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) @@ -6801,6 +6886,7 @@ def verify_linear_regressor(a_shape, c_shape, i_shape, targets=1, batch=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 = [] From 880b7f66dbf853ddb701952ec73d24ec15f5c397 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Fri, 29 Jul 2022 02:17:32 +0000 Subject: [PATCH 085/110] Fix ci errors --- tests/python/frontend/onnx/test_forward.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/python/frontend/onnx/test_forward.py b/tests/python/frontend/onnx/test_forward.py index 4afcddf9a4d8..3cdb0f41c2f1 100644 --- a/tests/python/frontend/onnx/test_forward.py +++ b/tests/python/frontend/onnx/test_forward.py @@ -438,7 +438,7 @@ def test_depth_to_space(target, dev): def verify_depth_to_space(inshape, outshape, mode, block_size): node = onnx.helper.make_node( - "DepthToSpace", inputs=["x"], outputs=["y"], block_size=block_size + "DepthToSpace", inputs=["x"], outputs=["y"], blocksize=block_size ) graph = helper.make_graph( @@ -464,7 +464,7 @@ def test_space_to_depth(target, dev): def verify_space_to_depth(inshape, outshape, block_size): node = onnx.helper.make_node( - "SpaceToDepth", inputs=["x"], outputs=["y"], block_size=block_size + "SpaceToDepth", inputs=["x"], outputs=["y"], blocksize=block_size ) graph = helper.make_graph( @@ -3205,7 +3205,7 @@ def repeat(num, dims): verify_convtranspose_with_output_shape( (1, 1) + repeat(32, dims), (1, 1) + repeat(4, dims), - repeat(N, dims), + repeat(num, dims), repeat(4, dims), repeat(2, dims), repeat(1, dims), From 11b2a604f4e1f20ac0f020681b77eba95ec4fa02 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Fri, 29 Jul 2022 02:43:05 +0000 Subject: [PATCH 086/110] Fix invalid-name/unused-variable,redefined-builtin --- tests/python/frontend/tflite/test_forward.py | 226 ++++++++++--------- 1 file changed, 115 insertions(+), 111 deletions(-) diff --git a/tests/python/frontend/tflite/test_forward.py b/tests/python/frontend/tflite/test_forward.py index bc5ac2e4fe4f..67751a30ea16 100644 --- a/tests/python/frontend/tflite/test_forward.py +++ b/tests/python/frontend/tflite/test_forward.py @@ -14,8 +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, unused-variable -# pylint: disable=redefined-builtin, no-else-return, inconsistent-return-statements, import-outside-toplevel +# pylint: disable=unused-argument, disable=import-outside-toplevel, inconsistent-return-statements """ TFLite testcases ================ @@ -34,6 +33,8 @@ from packaging import version as package_version from tvm.contrib.download import download_testdata from tvm import relay +from tvm.contrib import graph_executor +from tflite.BuiltinOperator import BuiltinOperator import tvm import tvm.relay.testing.tf as tf_testing @@ -116,31 +117,31 @@ def get_real_image_object_detection(im_height, im_width): return data -def vmobj_to_list(o): +def vmobj_to_list(obj): """Converts TVM objects returned by VM execution to Python List.""" - if isinstance(o, tvm.nd.NDArray): - return [o.numpy().tolist()] - elif isinstance(o, tvm.runtime.container.ADT): + 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( @@ -199,17 +200,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 @@ -236,18 +237,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) @@ -263,22 +263,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 enumerate(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 enumerate(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 enumerate(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 @@ -292,7 +292,7 @@ def compare_tflite_with_tvm( out_names=None, quantized=False, input_range=None, - mode="graph_executor", + mode="vm", experimental_new_converter=False, fp16_quantized=False, int_quant_dtype=tf.int8, @@ -330,7 +330,7 @@ def compare_tflite_with_tvm( try: quant_scale = 255 / (input_range[i][1] - input_range[i][0]) except ZeroDivisionError: - raise ZeroDivisionError( + print( "Min and max of the input range for tensor " + i + " can't be equal" ) mean = -input_range[i][0] * quant_scale @@ -344,9 +344,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( @@ -365,11 +365,11 @@ def compare_tflite_with_tvm( if quantized and not fp16_quantized: 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], tvm_output[i], atol=1, rtol=1e-5) # pylint: disable=unnecessary-list-index-lookup else: 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], tvm_output[i], atol=1e-5, rtol=1e-5 # pylint: disable=unnecessary-list-index-lookup ) @@ -385,7 +385,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): @@ -513,11 +513,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(): @@ -719,15 +719,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(): @@ -915,7 +915,7 @@ def _test_tflite2_quantized_convolution( """One iteration of TFLite2 quantized convolution with given shapes and attributes""" 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( @@ -929,7 +929,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( @@ -948,8 +948,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() @@ -1026,7 +1026,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( @@ -1046,8 +1046,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() @@ -1741,7 +1741,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( @@ -1835,8 +1834,8 @@ 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( list(np.nditer(data)), @@ -1864,11 +1863,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]) @@ -1933,9 +1932,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.") @@ -2635,14 +2634,14 @@ def test_forward_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) @@ -2944,7 +2943,7 @@ 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 @@ -2961,17 +2960,17 @@ def test_forward_select(): @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]] ) -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 @@ -3021,7 +3020,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) @@ -3048,7 +3047,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) @@ -3351,8 +3350,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( @@ -3366,12 +3365,14 @@ def _test_expand_dims(input_shape, input_type, axis, quantized=False): [input], ["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(): @@ -3440,17 +3441,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 ) @@ -3973,12 +3974,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], ) @@ -4158,13 +4161,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(): @@ -4187,8 +4190,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" ) @@ -4205,7 +4208,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], @@ -4213,10 +4216,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" ) @@ -4224,7 +4229,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] ) @@ -4308,10 +4313,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 @@ -4352,9 +4357,8 @@ def test_custom_op_converter(): 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} @@ -4884,10 +4888,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 @@ -4952,10 +4956,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 @@ -5023,7 +5027,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( From 2501ce0519e8a544a1201e4ee711eec486302536 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Fri, 29 Jul 2022 06:35:36 +0000 Subject: [PATCH 087/110] [CI] Apply linting rules to caffe tests --- tests/python/frontend/caffe/test_forward.py | 26 ++++++++++----------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/tests/python/frontend/caffe/test_forward.py b/tests/python/frontend/caffe/test_forward.py index 4199e27df7ed..5dc21387ba64 100644 --- a/tests/python/frontend/caffe/test_forward.py +++ b/tests/python/frontend/caffe/test_forward.py @@ -89,7 +89,7 @@ def _gen_filename_str(op_name, data_shape, *args, **kwargs): def _save_prototxt(n_netspec, f_path): """Generate .prototxt file according to caffe.NetSpec""" s = n_netspec.to_proto() - with open(f_path, "w") as f: + with open(f_path, "wb") as f: f.write(str(s)) @@ -109,7 +109,7 @@ def _save_solver(solver_file, proto_file, blob_file): s.snapshot = 100000 s.snapshot_prefix = blob_file_prefix - with open(solver_file, "w") as f: + with open(solver_file, "wb") as f: f.write(str(s)) @@ -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() @@ -181,14 +181,14 @@ def _run_tvm(data, proto_file, blob_file): predict_net = pb.NetParameter() # load model - with open(proto_file, "r") as f: + with open(proto_file, "rb") as f: text_format.Merge(f.read(), predict_net) # load blob 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()) @@ -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) @@ -1084,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") @@ -1145,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") From f979f2f154125aa57ddf084081dfa3a1e6d81d39 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Fri, 29 Jul 2022 06:36:00 +0000 Subject: [PATCH 088/110] [CI] Apply linting rules to darknet tests --- tests/python/frontend/darknet/test_forward.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/python/frontend/darknet/test_forward.py b/tests/python/frontend/darknet/test_forward.py index f8a30a4e10cb..ed3d86710a5c 100644 --- a/tests/python/frontend/darknet/test_forward.py +++ b/tests/python/frontend/darknet/test_forward.py @@ -31,7 +31,7 @@ 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" @@ -171,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 From 16484edf7b85317ac06e391df4ef09d4ddcf9aeb Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Fri, 29 Jul 2022 06:36:16 +0000 Subject: [PATCH 089/110] [CI] Apply linting rules to pytorch tests --- tests/python/frontend/pytorch/test_forward.py | 211 +++++++++++++++++- 1 file changed, 209 insertions(+), 2 deletions(-) diff --git a/tests/python/frontend/pytorch/test_forward.py b/tests/python/frontend/pytorch/test_forward.py index 2e8a7f31e96a..ea5a83ddd99f 100755 --- a/tests/python/frontend/pytorch/test_forward.py +++ b/tests/python/frontend/pytorch/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, missing-docstring +# pylint: disable=import-self, invalid-name, unused-argument """Unit tests for various models and operators""" import os import platform @@ -43,7 +43,9 @@ def list_ops(expr): + """list_ops""" class OpLister(tvm.relay.ExprVisitor): + """OpLister inherits from ExprVisitor""" def visit_op(self, op): if op not in self.node_set: self.node_list.append(op) @@ -59,6 +61,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)) @@ -238,7 +241,7 @@ def verify_model_with_input( # Single operator tests @tvm.testing.uses_gpu def test_forward_pixel_shuffle(): - """PixelShuffle""" + """test_forward_pixel_shuffle""" torch.set_grad_enabled(False) input_shape = [1, 144, 16, 16] @@ -250,6 +253,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] @@ -284,6 +288,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_subtract(): + """test_forward_subtract""" torch.set_grad_enabled(False) input_shape = [10] @@ -318,6 +323,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_multiply(): + """test_forward_multiply""" torch.set_grad_enabled(False) input_shape = [10] @@ -352,6 +358,7 @@ 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) @@ -402,6 +409,7 @@ 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) @@ -418,6 +426,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] @@ -431,6 +440,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] @@ -454,6 +464,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] @@ -482,6 +493,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] @@ -503,6 +515,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] @@ -521,6 +534,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_arange(): + """test_forward_arange""" torch.set_grad_enabled(False) class Arange1(Module): @@ -597,6 +611,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): @@ -619,6 +634,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] @@ -632,6 +648,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] @@ -653,6 +670,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() @@ -661,6 +679,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() @@ -673,6 +692,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() @@ -686,6 +706,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() @@ -697,6 +718,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() @@ -708,6 +730,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() @@ -716,6 +739,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() @@ -724,6 +748,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() @@ -732,6 +757,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() @@ -740,6 +766,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() @@ -750,6 +777,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() @@ -758,6 +786,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() @@ -766,6 +795,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() @@ -779,6 +809,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() @@ -792,6 +823,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() @@ -831,6 +863,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() @@ -850,6 +883,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() @@ -869,6 +903,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_split(): + """test_forward_split""" torch.set_grad_enabled(False) input_shape = [4, 10] @@ -890,6 +925,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] @@ -907,6 +943,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] @@ -933,6 +970,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] @@ -950,6 +988,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() @@ -958,6 +997,7 @@ 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] @@ -1043,6 +1083,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 @@ -1095,10 +1136,13 @@ 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 @@ -1127,6 +1171,7 @@ def forward(self, x, w, s): def test_forward_deform_conv(): + """test_forward_deform_conv""" torch.set_grad_enabled(False) def test_run( @@ -1207,6 +1252,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() @@ -1215,6 +1261,7 @@ def test_forward_threshold(): @tvm.testing.uses_gpu def test_forward_contiguous(): + """test_forward_contiguous""" torch.set_grad_enabled(False) input_shape = [10] @@ -1228,6 +1275,7 @@ 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) @@ -1242,6 +1290,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)) @@ -1254,6 +1303,7 @@ 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) @@ -1267,6 +1317,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() @@ -1289,6 +1340,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] @@ -1314,6 +1366,7 @@ 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) @@ -1324,6 +1377,7 @@ 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) @@ -1350,6 +1404,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] @@ -1373,6 +1428,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] @@ -1385,6 +1441,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] @@ -1398,6 +1455,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] @@ -1437,6 +1495,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] @@ -1461,6 +1520,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] @@ -1493,6 +1553,7 @@ def forward(self, index): @tvm.testing.uses_gpu def test_forward_clone(): + """test_forward_clone""" torch.set_grad_enabled(False) input_shape = [10] @@ -1506,6 +1567,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_gather(): + """test_forward_gather""" torch.set_grad_enabled(False) class Gather1(Module): @@ -1549,6 +1611,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] @@ -1562,6 +1625,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] @@ -1620,6 +1684,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] @@ -1648,6 +1713,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() @@ -1656,6 +1722,7 @@ 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] @@ -1689,6 +1756,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_linear(): + """test_forward_linear""" torch.set_grad_enabled(False) class Linear(Module): @@ -1736,6 +1804,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() @@ -1747,6 +1816,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] @@ -1791,6 +1861,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] @@ -1816,6 +1887,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] @@ -1829,6 +1901,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_expand(): + """test_forward_expand""" torch.set_grad_enabled(False) class Expand1(Module): @@ -1850,6 +1923,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): @@ -1872,6 +1946,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] @@ -1885,6 +1960,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] @@ -1899,6 +1975,7 @@ 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__() @@ -1969,6 +2046,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) @@ -1981,6 +2059,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) @@ -2001,6 +2080,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) @@ -2008,6 +2088,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) @@ -2017,6 +2098,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) @@ -2024,6 +2106,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) @@ -2031,6 +2114,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) @@ -2041,6 +2125,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) @@ -2051,6 +2136,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) @@ -2061,6 +2147,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) @@ -2071,6 +2158,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) @@ -2081,6 +2169,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) @@ -2150,6 +2239,7 @@ 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) @@ -2161,6 +2251,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( @@ -2191,48 +2282,56 @@ 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) @@ -2259,6 +2358,7 @@ 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) @@ -2294,6 +2394,7 @@ 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__() @@ -2314,22 +2415,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) @@ -2365,6 +2470,7 @@ def convert_pt_to_tvm_type(idtype): # pylint: disable=dangerous-default-value def verify_model_vm(input_model, ishapes, idtype=None, idata=None, targets=["llvm"]): + """verify_model_vm""" if not idtype: idtype = torch.float @@ -2426,7 +2532,9 @@ 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)) @@ -2439,11 +2547,13 @@ 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 @@ -2458,7 +2568,9 @@ 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 @@ -2480,6 +2592,7 @@ def forward(self, inp): return a class LoopWithIf(torch.nn.Module): + """LoopWithIf module""" def forward(self, inp): a = inp for _ in range(inp.size(0)): @@ -2501,7 +2614,9 @@ 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): @@ -2540,6 +2655,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): @@ -2560,6 +2676,7 @@ 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) @@ -2578,6 +2695,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] @@ -2611,6 +2729,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] @@ -2634,6 +2753,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] @@ -2657,6 +2777,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] @@ -2680,6 +2801,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] @@ -2733,6 +2855,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] @@ -2786,6 +2909,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] @@ -2839,6 +2963,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_rsub(): + """test_forward_rsub""" torch.set_grad_enabled(False) class Rsub1(Module): @@ -2867,6 +2992,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() @@ -2881,6 +3007,7 @@ def test_forward_embedding(): @tvm.testing.uses_gpu def test_forward_onehot(): + """test_forward_onehot""" torch.set_grad_enabled(False) class OneHot1(Module): @@ -2900,6 +3027,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_isfinite(): + """test_forward_isfinite""" torch.set_grad_enabled(False) class IsFinite1(Module): @@ -2912,6 +3040,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_isnan(): + """test_forward_isnan""" torch.set_grad_enabled(False) class IsNan1(Module): @@ -2924,6 +3053,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_isinf(): + """test_forward_isinf""" torch.set_grad_enabled(False) class IsInf1(Module): @@ -2936,6 +3066,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] @@ -2970,6 +3101,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_clamp_(): + """test_forward_clamp_""" torch.set_grad_enabled(False) class ClampInPlace(Module): @@ -2988,6 +3120,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_ones(): + """test_forward_ones""" torch.set_grad_enabled(False) class Ones1(Module): @@ -2999,6 +3132,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] @@ -3022,6 +3156,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] @@ -3033,6 +3168,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): @@ -3044,6 +3180,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] @@ -3067,6 +3204,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_full(): + """test_forward_full""" torch.set_grad_enabled(False) class Full1(Module): @@ -3083,6 +3221,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] @@ -3106,6 +3245,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] @@ -3124,6 +3264,7 @@ def test_func(x): @tvm.testing.uses_gpu def test_forward_linspace(): + """test_forward_linspace""" torch.set_grad_enabled(False) class Linspace1(Module): @@ -3170,6 +3311,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_take(): + """test_forward_take""" torch.set_grad_enabled(False) class Take1(Module): @@ -3193,6 +3335,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_topk(): + """test_forward_topk""" torch.set_grad_enabled(False) class Topk1(Module): @@ -3231,6 +3374,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): @@ -3252,6 +3396,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): @@ -3270,6 +3415,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): @@ -3297,6 +3443,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): @@ -3324,6 +3471,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_unary(): + """test_forward_unary""" torch.set_grad_enabled(False) class Sqrt1(Module): @@ -3452,6 +3600,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): @@ -3481,6 +3630,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): @@ -3510,6 +3660,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): @@ -3540,6 +3691,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_addcdiv(): + """test_forward_addcdiv""" torch.set_grad_enabled(False) class Addcdiv1(Module): @@ -3564,6 +3716,7 @@ def forward(self, *args): @tvm.testing.uses_gpu def test_forward_addcmul(): + """test_forward_addcmul""" torch.set_grad_enabled(False) class Addcmul1(Module): @@ -3588,6 +3741,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) @@ -3610,6 +3764,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): @@ -3633,6 +3788,7 @@ def forward(self, arg): @tvm.testing.uses_gpu def test_forward_traced_function(): + """test_forward_traced_function""" def fn(t1, t2): return t1 + t2 @@ -3643,6 +3799,7 @@ def fn(t1, t2): @tvm.testing.uses_gpu def test_forward_dtypes(): + """test_forward_dtypes""" def fn(t1, t2): return 2.5 * t1 + t2 @@ -3674,6 +3831,7 @@ def test_weight_names(): @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 @@ -3692,6 +3850,7 @@ def forward(self, x): @tvm.testing.uses_gpu def test_forward_matmul(): + """test_forward_matmul""" torch.set_grad_enabled(False) class MatMul1(Module): @@ -3737,6 +3896,7 @@ def forward(self, *args): def test_forward_index(): + """test_forward_index""" torch.set_grad_enabled(False) input_shape = [3, 4, 5, 6] @@ -3756,6 +3916,7 @@ def forward(self, x): def test_logsumexp(): + """test_logsumexp""" class Logsumexp(Module): def __init__(self, dim, keepdim=False): super().__init__() @@ -3775,6 +3936,7 @@ def forward(self, x): def test_stack(): + """test_stack""" class Stack(torch.nn.Module): def __init__(self, axis=0): super().__init__() @@ -3791,6 +3953,7 @@ def forward(self, x): def test_stack_dynamic(): + """test_stack_dynamic""" class Stack(torch.nn.Module): def forward(self, x): tensor_list = [] @@ -3804,6 +3967,7 @@ def forward(self, x): def test_forward_unbind(): + """test_forward_unbind""" class Unbind(torch.nn.Module): def __init__(self, axis=0): super().__init__() @@ -3819,6 +3983,7 @@ def forward(self, x): def test_forward_nonzero(): + """test_forward_nonzero""" class Nonzero(Module): def __init__(self, as_tuple=False): super().__init__() @@ -3832,6 +3997,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) @@ -3856,6 +4022,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( @@ -3888,6 +4055,7 @@ def test_fn_index_put3a(): def test_numel(): + """test_numel""" class Numel(Module): def forward(self, data): return torch.tensor(torch.numel(data)) @@ -3899,6 +4067,7 @@ def forward(self, data): def test_empty(): + """Test for aten::empty""" def test_func(): return torch.empty([1, 3, 10, 10]) @@ -3906,6 +4075,7 @@ def test_func(): def test_empty_like(): + """Test for aten::empty_like""" def test_func(data): return torch.empty_like(data) @@ -3913,6 +4083,7 @@ def test_func(data): def test_randn(): + """Test for aten::randn""" def test_func(): return torch.randn([1, 3, 10, 10]) @@ -4070,6 +4241,7 @@ 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 @@ -4107,6 +4279,7 @@ def expected(x_shape, y_shape): def test_bincount(): + """test_bincount""" def test_fn(x, weights=None): return torch.bincount(x, weights=weights) @@ -4119,6 +4292,7 @@ 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_data in examples: verify_model(torch.nn.Hardswish().eval(), input_data=input_data) @@ -4126,6 +4300,7 @@ def test_hard_swish(): 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_data in examples: verify_model(torch.nn.Hardsigmoid().eval(), input_data=input_data) @@ -4133,6 +4308,7 @@ def test_hard_sigmoid(): def test_cumsum(): + """test_cumsum""" def test_fn(dim, dtype=None): return lambda x: torch.cumsum(x, dim=dim, dtype=dtype) @@ -4150,6 +4326,7 @@ 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) @@ -4159,6 +4336,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)) @@ -4167,6 +4345,7 @@ def test_transformer(): def test_argsort(): + """test_argsort""" def test_fn(dim, descending): return lambda x: torch.argsort(x, dim=dim, descending=descending) @@ -4182,6 +4361,7 @@ 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) @@ -4197,6 +4377,7 @@ def test_fn(dim, descending): def test_logical_and(): + """test_logical_and""" def test_fn(x, y): return torch.logical_and(x, y) @@ -4210,6 +4391,7 @@ def test_fn(x, y): def test_masked_select(): + """test_masked_select""" def test_fn(x, mask): return torch.masked_select(x, mask) @@ -4220,6 +4402,7 @@ 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) @@ -4237,6 +4420,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() @@ -4260,6 +4444,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 @@ -4278,6 +4463,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() @@ -4296,6 +4482,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() @@ -4315,6 +4502,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): @@ -4333,6 +4521,7 @@ def forward(self, x): 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() @@ -4344,6 +4533,7 @@ 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) @@ -4356,6 +4546,7 @@ 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) @@ -4374,6 +4565,7 @@ 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) @@ -4386,6 +4578,7 @@ 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) @@ -4397,6 +4590,7 @@ 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) @@ -4408,6 +4602,7 @@ 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, @@ -4440,6 +4635,7 @@ def test_fn(n_fft, hop_length, win_length, center, pad_mode, normalized, oneside @tvm.testing.uses_gpu def test_dot(): + """Test for aten::dot""" def test_fn(x): return x.dot(x) @@ -4449,6 +4645,7 @@ def test_fn(x): @tvm.testing.uses_gpu def test_mv(): + """Test for aten::mv""" def test_fn(m, v): return m.mv(v) @@ -4458,6 +4655,7 @@ 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__() @@ -4502,7 +4700,9 @@ 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): @@ -4522,6 +4722,7 @@ def forward(self, x): # 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) @@ -4540,6 +4741,7 @@ 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 @@ -4555,6 +4757,7 @@ 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) @@ -4567,8 +4770,10 @@ 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) @@ -4576,6 +4781,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) @@ -4608,6 +4814,7 @@ 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) From 5f906ee3924ca9f7bf8ac50940d5515a34ef25b1 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Fri, 29 Jul 2022 06:36:28 +0000 Subject: [PATCH 090/110] [CI] Apply linting rules to tflite tests --- tests/python/frontend/tflite/test_forward.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/tests/python/frontend/tflite/test_forward.py b/tests/python/frontend/tflite/test_forward.py index 67751a30ea16..c0b94f7eb6ce 100644 --- a/tests/python/frontend/tflite/test_forward.py +++ b/tests/python/frontend/tflite/test_forward.py @@ -330,9 +330,7 @@ def compare_tflite_with_tvm( try: quant_scale = 255 / (input_range[i][1] - input_range[i][0]) except ZeroDivisionError: - print( - "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 @@ -365,11 +363,19 @@ def compare_tflite_with_tvm( if quantized and not fp16_quantized: 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) # pylint: disable=unnecessary-list-index-lookup + 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 enumerate(tflite_output): tvm.testing.assert_allclose( - tflite_output[i], tvm_output[i], atol=1e-5, rtol=1e-5 # pylint: disable=unnecessary-list-index-lookup + tflite_output[i], # pylint: disable=unnecessary-list-index-lookup + tvm_output[i], + atol=1e-5, + rtol=1e-5, ) @@ -4357,6 +4363,7 @@ def test_custom_op_converter(): class DummyOperatorConverter(relay.frontend.tflite.OperatorConverter): """Operator Converter for converting TFLite ops to relay ops""" + def __init__(self, model, subgraph, exp_tab): super().__init__(model, subgraph, exp_tab) self.allow_custom_ops = True From a147937ffc37133f0d0c0d87662e05dd1fa63257 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Fri, 29 Jul 2022 06:37:44 +0000 Subject: [PATCH 091/110] reformat by black --- tests/python/frontend/pytorch/test_forward.py | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/tests/python/frontend/pytorch/test_forward.py b/tests/python/frontend/pytorch/test_forward.py index ea5a83ddd99f..42ca8d8e32bb 100755 --- a/tests/python/frontend/pytorch/test_forward.py +++ b/tests/python/frontend/pytorch/test_forward.py @@ -44,8 +44,10 @@ def list_ops(expr): """list_ops""" + class OpLister(tvm.relay.ExprVisitor): """OpLister inherits from ExprVisitor""" + def visit_op(self, op): if op not in self.node_set: self.node_list.append(op) @@ -359,6 +361,7 @@ 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) @@ -410,6 +413,7 @@ 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) @@ -1141,6 +1145,7 @@ def test_forward_conv2d_transpose_group(): class ModulatedConvTranspose2D(torch.nn.Module): """ModulatedConvTranspose2D module""" + def forward(self, x, w, s): """forward""" B, C, H, W = x.shape @@ -1276,6 +1281,7 @@ 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) @@ -1304,6 +1310,7 @@ 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) @@ -1367,6 +1374,7 @@ 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) @@ -1378,6 +1386,7 @@ 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) @@ -1976,6 +1985,7 @@ 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__() @@ -2359,6 +2369,7 @@ 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) @@ -2395,6 +2406,7 @@ 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__() @@ -2533,8 +2545,10 @@ 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)) @@ -2548,6 +2562,7 @@ def forward(self, inp): class NestedIf(torch.nn.Module): """NestedIf module""" + def __init__(self, N, M): super().__init__() self.weight = torch.nn.Parameter(torch.rand(N, M)) @@ -2569,6 +2584,7 @@ def forward(self, inp): class ScalarLoop(torch.nn.Module): """ScalarLoop module""" + def forward(self, inp): """forward""" a = 0 @@ -2593,6 +2609,7 @@ def forward(self, inp): class LoopWithIf(torch.nn.Module): """LoopWithIf module""" + def forward(self, inp): a = inp for _ in range(inp.size(0)): @@ -2615,6 +2632,7 @@ def forward(self, inp): class SimpleScalarWhileLoop(torch.nn.Module): """SimpleScalarWhileLoop module""" + def forward(self, inp): """forward""" a = 1 @@ -2677,6 +2695,7 @@ def forward(self, x, h): class RNNLoop(torch.nn.Module): """Pytorch RNNLoop module""" + def __init__(self): super().__init__() x = torch.rand(10, 4, dtype=torch.float) @@ -3789,6 +3808,7 @@ def forward(self, arg): @tvm.testing.uses_gpu def test_forward_traced_function(): """test_forward_traced_function""" + def fn(t1, t2): return t1 + t2 @@ -3800,6 +3820,7 @@ def fn(t1, t2): @tvm.testing.uses_gpu def test_forward_dtypes(): """test_forward_dtypes""" + def fn(t1, t2): return 2.5 * t1 + t2 @@ -3917,6 +3938,7 @@ def forward(self, x): def test_logsumexp(): """test_logsumexp""" + class Logsumexp(Module): def __init__(self, dim, keepdim=False): super().__init__() @@ -3937,6 +3959,7 @@ def forward(self, x): def test_stack(): """test_stack""" + class Stack(torch.nn.Module): def __init__(self, axis=0): super().__init__() @@ -3954,6 +3977,7 @@ def forward(self, x): def test_stack_dynamic(): """test_stack_dynamic""" + class Stack(torch.nn.Module): def forward(self, x): tensor_list = [] @@ -3968,6 +3992,7 @@ def forward(self, x): def test_forward_unbind(): """test_forward_unbind""" + class Unbind(torch.nn.Module): def __init__(self, axis=0): super().__init__() @@ -3984,6 +4009,7 @@ def forward(self, x): def test_forward_nonzero(): """test_forward_nonzero""" + class Nonzero(Module): def __init__(self, as_tuple=False): super().__init__() @@ -4056,6 +4082,7 @@ def test_fn_index_put3a(): def test_numel(): """test_numel""" + class Numel(Module): def forward(self, data): return torch.tensor(torch.numel(data)) @@ -4068,6 +4095,7 @@ def forward(self, data): def test_empty(): """Test for aten::empty""" + def test_func(): return torch.empty([1, 3, 10, 10]) @@ -4076,6 +4104,7 @@ def test_func(): def test_empty_like(): """Test for aten::empty_like""" + def test_func(data): return torch.empty_like(data) @@ -4084,6 +4113,7 @@ def test_func(data): def test_randn(): """Test for aten::randn""" + def test_func(): return torch.randn([1, 3, 10, 10]) @@ -4242,6 +4272,7 @@ def test_forward_pretrained_bert_base_uncased(): ) 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 @@ -4280,6 +4311,7 @@ def expected(x_shape, y_shape): def test_bincount(): """test_bincount""" + def test_fn(x, weights=None): return torch.bincount(x, weights=weights) @@ -4309,6 +4341,7 @@ def test_hard_sigmoid(): def test_cumsum(): """test_cumsum""" + def test_fn(dim, dtype=None): return lambda x: torch.cumsum(x, dim=dim, dtype=dtype) @@ -4327,6 +4360,7 @@ 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) @@ -4346,6 +4380,7 @@ def test_transformer(): def test_argsort(): """test_argsort""" + def test_fn(dim, descending): return lambda x: torch.argsort(x, dim=dim, descending=descending) @@ -4362,6 +4397,7 @@ 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) @@ -4378,6 +4414,7 @@ def test_fn(dim, descending): def test_logical_and(): """test_logical_and""" + def test_fn(x, y): return torch.logical_and(x, y) @@ -4392,6 +4429,7 @@ def test_fn(x, y): def test_masked_select(): """test_masked_select""" + def test_fn(x, mask): return torch.masked_select(x, mask) @@ -4403,6 +4441,7 @@ 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) @@ -4534,6 +4573,7 @@ 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) @@ -4547,6 +4587,7 @@ 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) @@ -4566,6 +4607,7 @@ 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) @@ -4579,6 +4621,7 @@ 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) @@ -4591,6 +4634,7 @@ 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) @@ -4603,6 +4647,7 @@ 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, @@ -4636,6 +4681,7 @@ def test_fn(n_fft, hop_length, win_length, center, pad_mode, normalized, oneside @tvm.testing.uses_gpu def test_dot(): """Test for aten::dot""" + def test_fn(x): return x.dot(x) @@ -4646,6 +4692,7 @@ def test_fn(x): @tvm.testing.uses_gpu def test_mv(): """Test for aten::mv""" + def test_fn(m, v): return m.mv(v) @@ -4656,6 +4703,7 @@ 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__() @@ -4701,6 +4749,7 @@ def test_list_tuple(): class List_tuple(Module): """List_tuple""" + def forward(self, x): """forward""" merged = [] @@ -4723,6 +4772,7 @@ def forward(self, x): @tvm.testing.uses_gpu def test_binary_bitwise(): """Test for binary bitwise""" + def test_ior(x, y): return x.__ior__(y) @@ -4742,6 +4792,7 @@ 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 @@ -4758,6 +4809,7 @@ 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) @@ -4774,6 +4826,7 @@ def 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) @@ -4815,6 +4868,7 @@ 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) From e9abd849507cd9f523ad769928dffeb5ceb49cc4 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Fri, 29 Jul 2022 07:14:09 +0000 Subject: [PATCH 092/110] [Pylint] Making frontend tests pylint compliant Part 1 of N --- tests/lint/pylint.sh | 13 ++++ tests/python/frontend/caffe/test_forward.py | 42 ++----------- .../frontend/caffe2/model_zoo/__init__.py | 2 +- .../frontend/caffe2/model_zoo/squeezenet.py | 10 ++-- tests/python/frontend/caffe2/test_forward.py | 7 +-- .../frontend/coreml/model_zoo/__init__.py | 1 + tests/python/frontend/coreml/test_forward.py | 33 +++++------ tests/python/frontend/darknet/test_forward.py | 32 ++-------- tests/python/frontend/keras/test_forward.py | 48 +++++++-------- tests/python/frontend/oneflow/test_forward.py | 59 +++++++++++++++---- .../frontend/oneflow/test_vision_models.py | 2 +- .../frontend/tensorflow/test_forward.py | 1 - tests/python/frontend/tflite/test_forward.py | 3 +- 13 files changed, 119 insertions(+), 134 deletions(-) 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 5dc21387ba64..f4a41aeeb7d8 100644 --- a/tests/python/frontend/caffe/test_forward.py +++ b/tests/python/frontend/caffe/test_forward.py @@ -89,7 +89,7 @@ def _gen_filename_str(op_name, data_shape, *args, **kwargs): def _save_prototxt(n_netspec, f_path): """Generate .prototxt file according to caffe.NetSpec""" s = n_netspec.to_proto() - with open(f_path, "wb") as f: + with open(f_path, "w") as f: f.write(str(s)) @@ -109,7 +109,7 @@ def _save_solver(solver_file, proto_file, blob_file): s.snapshot = 100000 s.snapshot_prefix = blob_file_prefix - with open(solver_file, "wb") as f: + with open(solver_file, "w") as f: f.write(str(s)) @@ -181,7 +181,7 @@ def _run_tvm(data, proto_file, blob_file): predict_net = pb.NetParameter() # load model - with open(proto_file, "rb") as f: + with open(proto_file, "r") as f: text_format.Merge(f.read(), predict_net) # load blob with open(blob_file, "rb") as f: @@ -1160,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 88849ba20f24..9758d937c254 100644 --- a/tests/python/frontend/caffe2/test_forward.py +++ b/tests/python/frontend/caffe2/test_forward.py @@ -254,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 c137a5d71a05..ea2f3478fde4 100644 --- a/tests/python/frontend/coreml/model_zoo/__init__.py +++ b/tests/python/frontend/coreml/model_zoo/__init__.py @@ -36,6 +36,7 @@ def get_resnet50(): def get_cat_image(): + """Get cat image""" url = ( "https://gist.githubusercontent.com/zhreshold/" + "bcda4716699ac97ea44f791c24310193/raw/fa7ef0e9c9a5daea686d6473a62aacd1a5885849/cat.png" diff --git a/tests/python/frontend/coreml/test_forward.py b/tests/python/frontend/coreml/test_forward.py index 855441fb3cd9..e5e337570c1c 100644 --- a/tests/python/frontend/coreml/test_forward.py +++ b/tests/python/frontend/coreml/test_forward.py @@ -14,7 +14,6 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. -# pylint: disable=invalid-name, redefined-builtin """ CoreML testcases ==================== @@ -225,9 +224,9 @@ def _verify_upsample_layer_params(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, @@ -256,9 +255,9 @@ def _verify_l2_normalize(input_dim, eps): 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) @@ -278,9 +277,9 @@ def _verify_lrn(input_dim, size, bias, alpha, beta): 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", @@ -765,24 +764,24 @@ def test_forward_image_scaler(): ) -def verify_convolution(input_dim, filter, padding): +def verify_convolution(input_dim, filter_, padding): """Verify convolution""" dtype = "float32" - _, 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))] + inputs = [("input1", datatypes.Array(c, h, width))] output = [("output", datatypes.Array(*b_np.shape))] 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(), @@ -802,8 +801,8 @@ 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(): diff --git a/tests/python/frontend/darknet/test_forward.py b/tests/python/frontend/darknet/test_forward.py index ed3d86710a5c..ffaa773fc1bd 100644 --- a/tests/python/frontend/darknet/test_forward.py +++ b/tests/python/frontend/darknet/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=unused-argument, invalid-name +# pylint: disable=unused-argument """ Test Darknet Models =================== @@ -460,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 @@ -475,7 +475,7 @@ def test_forward_activation_logistic(): layer_1 = LIB.make_convolutional_layer( batch, h, - w, + width, c, n, groups, @@ -489,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) @@ -518,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 181c566f9949..2ee451f3d234 100644 --- a/tests/python/frontend/keras/test_forward.py +++ b/tests/python/frontend/keras/test_forward.py @@ -735,30 +735,30 @@ def test_forward_time_distributed(self, keras_mod): if __name__ == "__main__": for k in [keras, tf_keras]: sut = TestKeras() - # 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_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) diff --git a/tests/python/frontend/oneflow/test_forward.py b/tests/python/frontend/oneflow/test_forward.py index 41ca08c7ab5a..5c68985670ea 100644 --- a/tests/python/frontend/oneflow/test_forward.py +++ b/tests/python/frontend/oneflow/test_forward.py @@ -14,8 +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, missing-function-docstring -# pylint: disable=arguments-differ, unused-argument, consider-using-f-string +# pylint: disable=arguments-differ, unused-argument """Unit tests for various models and operators""" import os @@ -38,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): @@ -66,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 @@ -101,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) @@ -123,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() @@ -151,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": @@ -180,6 +182,7 @@ def verify_conv( ), device="llvm", ): + """verify_conv""" if device == "cuda": model.to(device) inputs = inputs.to(device) @@ -209,6 +212,7 @@ def verify_pool( ), device="llvm", ): + """verify_pool""" if device == "cuda": model.to(device) inputs = inputs.to(device) @@ -238,6 +242,7 @@ def verify_normalization( ), device="llvm", ): + """verify_normalization""" if device == "cuda": model.to(device) inputs = inputs.to(device) @@ -268,6 +273,7 @@ def verify_upsample( ), device="llvm", ): + """verify_upsample""" if device == "cuda": model.to(device) inputs = inputs.to(device) @@ -297,6 +303,7 @@ def verify_convtran( ), device="llvm", ): + """verify_convtran""" if device == "cuda": model.to(device) inputs = inputs.to(device) @@ -326,6 +333,7 @@ def verify_activation( ), device="llvm", ): + """verify_activation""" if device == "cuda": model.to(device) inputs = inputs.to(device) @@ -355,6 +363,7 @@ def verify_math( ), device="llvm", ): + """verify_math""" if device == "cuda": model.to(device) inputs = inputs.to(device) @@ -382,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) @@ -410,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) @@ -433,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__() @@ -454,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__() @@ -496,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__() @@ -516,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__() @@ -547,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__() @@ -567,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__() @@ -719,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) @@ -779,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]] @@ -795,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() @@ -808,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) @@ -851,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 f82277f98195..8a573e0f51e3 100644 --- a/tests/python/frontend/oneflow/test_vision_models.py +++ b/tests/python/frontend/oneflow/test_vision_models.py @@ -44,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): diff --git a/tests/python/frontend/tensorflow/test_forward.py b/tests/python/frontend/tensorflow/test_forward.py index 3892a4239e53..11894075b950 100755 --- a/tests/python/frontend/tensorflow/test_forward.py +++ b/tests/python/frontend/tensorflow/test_forward.py @@ -5758,5 +5758,4 @@ def test_invert_permutation(): if __name__ == "__main__": - test_tensor_array_scatter() pytest.main([__file__]) diff --git a/tests/python/frontend/tflite/test_forward.py b/tests/python/frontend/tflite/test_forward.py index c0b94f7eb6ce..011b7d46e203 100644 --- a/tests/python/frontend/tflite/test_forward.py +++ b/tests/python/frontend/tflite/test_forward.py @@ -2964,7 +2964,8 @@ 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_value, max_value, quant_bits): """Fake quant""" From 614f34b963a33224192e098337124b4264f98d12 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Fri, 29 Jul 2022 07:33:57 +0000 Subject: [PATCH 093/110] Fix ci errors --- tests/python/frontend/coreml/test_forward.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/python/frontend/coreml/test_forward.py b/tests/python/frontend/coreml/test_forward.py index e5e337570c1c..d99630f5f865 100644 --- a/tests/python/frontend/coreml/test_forward.py +++ b/tests/python/frontend/coreml/test_forward.py @@ -191,7 +191,7 @@ def verify_concat_layer_params(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" @@ -774,7 +774,7 @@ def verify_convolution(input_dim, filter_, padding): 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, width))] - 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_convolution( name="conv", From b6cd734b88f116451905ad64bbd1c904dc98b1a1 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Fri, 29 Jul 2022 08:09:41 +0000 Subject: [PATCH 094/110] Fix invalid-name pylint errors --- tests/python/frontend/coreml/test_forward.py | 24 ++++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/tests/python/frontend/coreml/test_forward.py b/tests/python/frontend/coreml/test_forward.py index d99630f5f865..6ca03eb436ed 100644 --- a/tests/python/frontend/coreml/test_forward.py +++ b/tests/python/frontend/coreml/test_forward.py @@ -582,26 +582,26 @@ def test_forward_reduce(): """Reduce""" class ReduceAxis(Enum): - CHW = 0 - HW = 1 - C = 2 - H = 3 - W = 4 + AXIS_CHW = 0 + AXIS_HW = 1 + AXIS_C = 2 + AXIS_H = 3 + AXIS_W = 4 def _verify_reduce(input_dim, mode, axis, ref_func, dtype="float32"): print(input_dim, mode, axis) a_np = np.random.uniform(size=input_dim).astype(dtype) # translate to axis from coreml format - if axis == ReduceAxis.CHW: + if axis == ReduceAxis.AXIS_CHW: np_axis = (-3, -2, -1) - elif axis == ReduceAxis.HW: + elif axis == ReduceAxis.AXIS_HW: np_axis = (-2, -1) - elif axis == ReduceAxis.C: + elif axis == ReduceAxis.AXIS_C: np_axis = -3 - elif axis == ReduceAxis.H: + elif axis == ReduceAxis.AXIS_H: np_axis = -2 - elif axis == ReduceAxis.W: + elif axis == ReduceAxis.AXIS_W: np_axis = -1 if ref_func is np.argmax: @@ -624,7 +624,7 @@ def _verify_reduce(input_dim, mode, axis, ref_func, dtype="float32"): dshapes = [[10, 10], [1, 10, 10], [1, 3, 10, 10]] for dshape in dshapes: for axis in ReduceAxis: - if len(dshape) < 3 and axis in [ReduceAxis.CHW, ReduceAxis.C]: + if len(dshape) < 3 and axis in [ReduceAxis.AXIS_CHW, ReduceAxis.AXIS_C]: # input must have rank at least 3 continue _verify_reduce(dshape, "sum", axis, np.sum) @@ -632,7 +632,7 @@ def _verify_reduce(input_dim, mode, axis, ref_func, dtype="float32"): _verify_reduce(dshape, "prod", axis, np.prod) _verify_reduce(dshape, "min", axis, np.min) _verify_reduce(dshape, "max", axis, np.max) - if axis in [ReduceAxis.C, ReduceAxis.H, ReduceAxis.W]: + if axis in [ReduceAxis.AXIS_C, ReduceAxis.AXIS_H, ReduceAxis.AXIS_W]: # For mode ArgMax, axis must be [-1] or [-2] or [-3] _verify_reduce(dshape, "argmax", axis, np.argmax, dtype="int32") From 42026ae0ac06eabc5d03c03458ae88b146707bd6 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Fri, 29 Jul 2022 08:21:06 +0000 Subject: [PATCH 095/110] Fix invalid-name pylint errors --- tests/python/frontend/keras/test_forward.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/python/frontend/keras/test_forward.py b/tests/python/frontend/keras/test_forward.py index 2ee451f3d234..f39ca0662e5e 100644 --- a/tests/python/frontend/keras/test_forward.py +++ b/tests/python/frontend/keras/test_forward.py @@ -32,15 +32,15 @@ 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): @@ -63,8 +63,8 @@ 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_mod": keras}) -using_tensorflow_keras = ("tf_keras", {"keras_mod": 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"): @@ -131,7 +131,7 @@ def get_mobilenet(keras_mod): class TestKeras: """Keras test""" - scenarios = [using_classic_keras, using_tensorflow_keras] + scenarios = [USING_CALSSIC_KERAS, USING_TENSORFLOW_KERAS] def test_forward_merge(self, keras_mod): """test_forward_merge""" From 9e00554c828d4048e04515a245021f201cb06936 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Fri, 29 Jul 2022 09:21:36 +0000 Subject: [PATCH 096/110] Disabale temporarily --- tests/lint/pylint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/lint/pylint.sh b/tests/lint/pylint.sh index 2228e110c15e..c49764745157 100755 --- a/tests/lint/pylint.sh +++ b/tests/lint/pylint.sh @@ -49,7 +49,7 @@ python3 -m pylint tests/python/frontend/coreml/*.py --rcfile="$(dirname "$0")"/p 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/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 From 146ab4d2d2fbddcb24afd8bdfc3ba5b996be9afd Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Fri, 29 Jul 2022 09:35:38 +0000 Subject: [PATCH 097/110] Fix dangerous-default-value --- tests/python/frontend/tensorflow/test_forward.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/python/frontend/tensorflow/test_forward.py b/tests/python/frontend/tensorflow/test_forward.py index 11894075b950..f2fa1ac228f1 100755 --- a/tests/python/frontend/tensorflow/test_forward.py +++ b/tests/python/frontend/tensorflow/test_forward.py @@ -501,10 +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 @@ -948,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""" - # pylint: disable=dangerous-default-value + 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 From 73ec6abca9a147fd989583660ecda0dc55d50549 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Fri, 29 Jul 2022 09:45:27 +0000 Subject: [PATCH 098/110] Fix typo errors --- tests/python/frontend/tflite/test_forward.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python/frontend/tflite/test_forward.py b/tests/python/frontend/tflite/test_forward.py index 011b7d46e203..919f15bfb5ea 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=unused-argument, disable=import-outside-toplevel, inconsistent-return-statements +# pylint: disable=unused-argument, import-outside-toplevel, inconsistent-return-statements """ TFLite testcases ================ From d5d9e91f6154a8df7f4a12675a2a23e58ba01f9a Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Fri, 29 Jul 2022 11:45:07 +0000 Subject: [PATCH 099/110] Fix ci errors --- tests/python/frontend/coreml/test_forward.py | 25 ++++++++++---------- tests/python/frontend/tflite/test_forward.py | 7 +++++- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/tests/python/frontend/coreml/test_forward.py b/tests/python/frontend/coreml/test_forward.py index 6ca03eb436ed..75879f3e582a 100644 --- a/tests/python/frontend/coreml/test_forward.py +++ b/tests/python/frontend/coreml/test_forward.py @@ -582,26 +582,27 @@ def test_forward_reduce(): """Reduce""" class ReduceAxis(Enum): - AXIS_CHW = 0 - AXIS_HW = 1 - AXIS_C = 2 - AXIS_H = 3 - AXIS_W = 4 + # pylint: disable=invalid-name + CHW = 0 + HW = 1 + C = 2 + H = 3 + W = 4 def _verify_reduce(input_dim, mode, axis, ref_func, dtype="float32"): print(input_dim, mode, axis) a_np = np.random.uniform(size=input_dim).astype(dtype) # translate to axis from coreml format - if axis == ReduceAxis.AXIS_CHW: + if axis == ReduceAxis.CHW: np_axis = (-3, -2, -1) - elif axis == ReduceAxis.AXIS_HW: + elif axis == ReduceAxis.HW: np_axis = (-2, -1) - elif axis == ReduceAxis.AXIS_C: + elif axis == ReduceAxis.C: np_axis = -3 - elif axis == ReduceAxis.AXIS_H: + elif axis == ReduceAxis.H: np_axis = -2 - elif axis == ReduceAxis.AXIS_W: + elif axis == ReduceAxis.W: np_axis = -1 if ref_func is np.argmax: @@ -624,7 +625,7 @@ def _verify_reduce(input_dim, mode, axis, ref_func, dtype="float32"): dshapes = [[10, 10], [1, 10, 10], [1, 3, 10, 10]] for dshape in dshapes: for axis in ReduceAxis: - if len(dshape) < 3 and axis in [ReduceAxis.AXIS_CHW, ReduceAxis.AXIS_C]: + if len(dshape) < 3 and axis in [ReduceAxis.CHW, ReduceAxis.C]: # input must have rank at least 3 continue _verify_reduce(dshape, "sum", axis, np.sum) @@ -632,7 +633,7 @@ def _verify_reduce(input_dim, mode, axis, ref_func, dtype="float32"): _verify_reduce(dshape, "prod", axis, np.prod) _verify_reduce(dshape, "min", axis, np.min) _verify_reduce(dshape, "max", axis, np.max) - if axis in [ReduceAxis.AXIS_C, ReduceAxis.AXIS_H, ReduceAxis.AXIS_W]: + if axis in [ReduceAxis.C, ReduceAxis.H, ReduceAxis.W]: # For mode ArgMax, axis must be [-1] or [-2] or [-3] _verify_reduce(dshape, "argmax", axis, np.argmax, dtype="int32") diff --git a/tests/python/frontend/tflite/test_forward.py b/tests/python/frontend/tflite/test_forward.py index 919f15bfb5ea..83c470ee72a5 100644 --- a/tests/python/frontend/tflite/test_forward.py +++ b/tests/python/frontend/tflite/test_forward.py @@ -3369,7 +3369,12 @@ 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_array = np.random.uniform(-100, 100, input_shape).astype(input_type) From 9bda0d690e062a2a39f884e02fcb8f78e7d79ee4 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Mon, 1 Aug 2022 06:12:58 +0000 Subject: [PATCH 100/110] [CI] Apply linting rules to tensorflow tests --- tests/lint/pylint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/lint/pylint.sh b/tests/lint/pylint.sh index c49764745157..2228e110c15e 100755 --- a/tests/lint/pylint.sh +++ b/tests/lint/pylint.sh @@ -49,7 +49,7 @@ python3 -m pylint tests/python/frontend/coreml/*.py --rcfile="$(dirname "$0")"/p 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/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 From 3a49e239d38c6eaa8c079601369becdaa66e3314 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Wed, 3 Aug 2022 03:07:53 +0000 Subject: [PATCH 101/110] Fix dangerous-default-value --- tests/python/frontend/pytorch/test_forward.py | 10 ++++---- .../frontend/tensorflow/test_forward.py | 24 +++++++++---------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/tests/python/frontend/pytorch/test_forward.py b/tests/python/frontend/pytorch/test_forward.py index 42ca8d8e32bb..98569f491c50 100755 --- a/tests/python/frontend/pytorch/test_forward.py +++ b/tests/python/frontend/pytorch/test_forward.py @@ -125,12 +125,14 @@ def load_model(model_name): raise RuntimeError("Model not supported") -# pylint: disable=dangerous-default-value def verify_model( - model_name, input_data=[], custom_convert_map={}, rtol=1e-5, atol=1e-5, expected_ops=[] + model_name, input_data=None, custom_convert_map=None, rtol=1e-5, atol=1e-5, expected_ops=None ): """Assert that the output of a compiled model matches with that of its baseline.""" + input_data = input_data or [] + 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): @@ -2480,9 +2482,9 @@ def convert_pt_to_tvm_type(idtype): return curr_dtype -# pylint: disable=dangerous-default-value -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 diff --git a/tests/python/frontend/tensorflow/test_forward.py b/tests/python/frontend/tensorflow/test_forward.py index f2fa1ac228f1..c679425beab1 100755 --- a/tests/python/frontend/tensorflow/test_forward.py +++ b/tests/python/frontend/tensorflow/test_forward.py @@ -4711,22 +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"]): - # pylint: disable=dangerous-default-value, redefined-outer-name - 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) @@ -4755,9 +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"]): - # pylint: disable=dangerous-default-value, redefined-outer-name - 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) @@ -4768,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) From 8bcae5a16da00727671c182a3bb5a7e532b01aa4 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Wed, 3 Aug 2022 03:10:14 +0000 Subject: [PATCH 102/110] Fix ungrouped-imports --- tests/python/frontend/tflite/test_forward.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/python/frontend/tflite/test_forward.py b/tests/python/frontend/tflite/test_forward.py index 83c470ee72a5..573ff39e194d 100644 --- a/tests/python/frontend/tflite/test_forward.py +++ b/tests/python/frontend/tflite/test_forward.py @@ -31,13 +31,14 @@ from PIL import Image from packaging import version as package_version + +import tvm +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 -import tvm -import tvm.relay.testing.tf as tf_testing try: import tensorflow.compat.v1 as tf From 225f5e610ec0ecbbae479b535e57e565bb78d584 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Wed, 3 Aug 2022 06:34:05 +0000 Subject: [PATCH 103/110] Fix boolean value of Tensor with more than one value is ambiguous --- tests/python/frontend/pytorch/test_forward.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/python/frontend/pytorch/test_forward.py b/tests/python/frontend/pytorch/test_forward.py index 98569f491c50..427a078c71c1 100755 --- a/tests/python/frontend/pytorch/test_forward.py +++ b/tests/python/frontend/pytorch/test_forward.py @@ -130,7 +130,6 @@ def verify_model( ): """Assert that the output of a compiled model matches with that of its baseline.""" - input_data = input_data or [] custom_convert_map = custom_convert_map or {} expected_ops = expected_ops or [] if isinstance(model_name, str): @@ -143,7 +142,7 @@ def verify_model( baseline_input = [input_data] else: assert False, "Unexpected input format" - + baseline_input = baseline_input or [] if torch.cuda.is_available(): if isinstance(baseline_model, torch.nn.Module): baseline_model = baseline_model.cuda() From 9bd5189814dbea8ac6924e8cb230b78142ee3e9f Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Wed, 3 Aug 2022 08:25:11 +0000 Subject: [PATCH 104/110] Fix 'NoneType' object has no attribute 'shape' --- tests/python/frontend/pytorch/test_forward.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/python/frontend/pytorch/test_forward.py b/tests/python/frontend/pytorch/test_forward.py index 427a078c71c1..23c719572a06 100755 --- a/tests/python/frontend/pytorch/test_forward.py +++ b/tests/python/frontend/pytorch/test_forward.py @@ -137,12 +137,14 @@ def verify_model( elif isinstance(input_data, list): baseline_model = model_name baseline_input = input_data - elif isinstance(input_data, torch.Tensor) or not input_data.shape: + elif isinstance(input_data, torch.Tensor) or not input_data: + if not input_data: + baseline_input = [] + else: + baseline_input = [input_data] baseline_model = model_name - baseline_input = [input_data] else: assert False, "Unexpected input format" - baseline_input = baseline_input or [] if torch.cuda.is_available(): if isinstance(baseline_model, torch.nn.Module): baseline_model = baseline_model.cuda() From 56e6d04fcf7ec78182a5ad8f6228c22544c2803d Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Wed, 3 Aug 2022 14:47:55 +0000 Subject: [PATCH 105/110] Fix boolean value of Tensor with more than one value is ambiguous --- tests/python/frontend/pytorch/test_forward.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/tests/python/frontend/pytorch/test_forward.py b/tests/python/frontend/pytorch/test_forward.py index 23c719572a06..2c276c2b9d59 100755 --- a/tests/python/frontend/pytorch/test_forward.py +++ b/tests/python/frontend/pytorch/test_forward.py @@ -130,6 +130,7 @@ def verify_model( ): """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): @@ -137,12 +138,9 @@ def verify_model( elif isinstance(input_data, list): baseline_model = model_name baseline_input = input_data - elif isinstance(input_data, torch.Tensor) or not input_data: - if not input_data: - baseline_input = [] - else: - baseline_input = [input_data] + 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(): From 5fba4ab0c4eaf501a5cfdcccc99c21eaab9983f4 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Thu, 11 Aug 2022 02:08:59 +0000 Subject: [PATCH 106/110] reformatted by black --- tests/python/frontend/onnx/test_forward.py | 1 + tests/python/frontend/pytorch/test_forward.py | 11 +++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/tests/python/frontend/onnx/test_forward.py b/tests/python/frontend/onnx/test_forward.py index 03bc7c4e7df6..c7f6bdb90cbc 100644 --- a/tests/python/frontend/onnx/test_forward.py +++ b/tests/python/frontend/onnx/test_forward.py @@ -4212,6 +4212,7 @@ def test_lstm(target, dev): dev=dev, ) + @tvm.testing.parametrize_targets def test_resize(target, dev): """test_resize""" diff --git a/tests/python/frontend/pytorch/test_forward.py b/tests/python/frontend/pytorch/test_forward.py index 2333d09de822..b7548106d871 100755 --- a/tests/python/frontend/pytorch/test_forward.py +++ b/tests/python/frontend/pytorch/test_forward.py @@ -126,8 +126,15 @@ def load_model(model_name): def verify_model( - model_name, input_data=None, custom_convert_map=None, rtol=1e-5, atol=1e-5, expected_ops=None, - kind="graph", check_correctness=True, cpu_only=False + model_name, + input_data=None, + custom_convert_map=None, + rtol=1e-5, + atol=1e-5, + 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.""" From 258dc72c6519c10ffa3a6dd3be0baa2eca2f9e25 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Thu, 11 Aug 2022 07:07:52 +0000 Subject: [PATCH 107/110] [CI] Apply linting rules to caffe tests --- tests/python/frontend/caffe/test_forward.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python/frontend/caffe/test_forward.py b/tests/python/frontend/caffe/test_forward.py index f4a41aeeb7d8..21e4c9d03181 100644 --- a/tests/python/frontend/caffe/test_forward.py +++ b/tests/python/frontend/caffe/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=import-self, invalid-name, unused-argument, unspecified-encoding """ Caffe testcases ==================== From 6c48c54f9c0db9a4aa098f4c6ddb67a7f4cd7742 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Thu, 11 Aug 2022 07:10:21 +0000 Subject: [PATCH 108/110] Fix dangerous-default-value --- tests/python/frontend/pytorch/test_forward.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/python/frontend/pytorch/test_forward.py b/tests/python/frontend/pytorch/test_forward.py index b7548106d871..843d9a4e5a30 100755 --- a/tests/python/frontend/pytorch/test_forward.py +++ b/tests/python/frontend/pytorch/test_forward.py @@ -226,13 +226,15 @@ 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 = [f"input{idx}" for idx, _ in enumerate(input_data)] From d11c25fbba5c317ca4b718a27743c635831d8ab0 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Thu, 11 Aug 2022 07:11:30 +0000 Subject: [PATCH 109/110] Fix conflict & fix pylint error --- tests/python/frontend/onnx/test_forward.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/tests/python/frontend/onnx/test_forward.py b/tests/python/frontend/onnx/test_forward.py index c7f6bdb90cbc..e1e5c3ea8bc5 100644 --- a/tests/python/frontend/onnx/test_forward.py +++ b/tests/python/frontend/onnx/test_forward.py @@ -3891,7 +3891,7 @@ def verify_rnn( raise NotImplementedError(f"{rnn_type} RNNs not yet supported.") if directions not in [1, 2]: - raise ValueError("Direction should be either 1 or 2 (for bidirectional LSTMs)") + raise ValueError(f"Direction should be either 1 or 2 (for bidirectional LSTMs)") def get_inputs(): input_names = [] @@ -4029,10 +4029,7 @@ def register(name, shape, proto_type): ) -@pytest.mark.parametrize("rnn_type", ["RNN", "LSTM", "GRU"]) -@tvm.testing.parametrize_targets -def test_lstm(target, dev): - """test_lstm""" +def verify_rnn_helper(target, dev, rnn_type): num_activations = 1 if rnn_type == "GRU": num_activations = 2 @@ -4213,6 +4210,21 @@ def test_lstm(target, dev): ) +@tvm.testing.parametrize_targets +def test_rnn(target, dev): + verify_rnn_helper(target, dev, "RNN") + + +@tvm.testing.parametrize_targets +def test_lstm(target, dev): + verify_rnn_helper(target, dev, "LSTM") + + +@tvm.testing.parametrize_targets +def test_gru(target, dev): + verify_rnn_helper(target, dev, "GRU") + + @tvm.testing.parametrize_targets def test_resize(target, dev): """test_resize""" From c26c82dbc309c1832ac8fe9f7db72580f750d457 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Thu, 11 Aug 2022 08:10:47 +0000 Subject: [PATCH 110/110] restore --- tests/python/frontend/tflite/test_forward.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python/frontend/tflite/test_forward.py b/tests/python/frontend/tflite/test_forward.py index 573ff39e194d..9121721d8ea2 100644 --- a/tests/python/frontend/tflite/test_forward.py +++ b/tests/python/frontend/tflite/test_forward.py @@ -293,7 +293,7 @@ def compare_tflite_with_tvm( out_names=None, quantized=False, input_range=None, - mode="vm", + mode="graph_executor", experimental_new_converter=False, fp16_quantized=False, int_quant_dtype=tf.int8,