From 00b861fb5241c29604e36c54faab972669293b41 Mon Sep 17 00:00:00 2001 From: electriclilies Date: Wed, 12 May 2021 18:29:26 -0700 Subject: [PATCH 01/24] Fix topi test case and docs (tvm was returning inverse_indices and claiming it was indices) --- python/tvm/topi/cuda/unique.py | 35 ++++++++++------ python/tvm/topi/unique.py | 42 +++++++++++++------- tests/python/topi/python/test_topi_unique.py | 31 +++++++++++---- 3 files changed, 73 insertions(+), 35 deletions(-) diff --git a/python/tvm/topi/cuda/unique.py b/python/tvm/topi/cuda/unique.py index 2bca3c447c4c..bbb0dacb5784 100644 --- a/python/tvm/topi/cuda/unique.py +++ b/python/tvm/topi/cuda/unique.py @@ -294,10 +294,18 @@ def unique(data, is_sorted=True, return_counts=False): Returns ------- output : tvm.te.Tensor - A 1-D tensor containing the unique elements of the input data tensor. + A 1-D tensor containing the unique elements of the input data tensor. The same size as + the input data. If there are less unique elements than input data, the end of the tensor + is padded with zeros. indices : tvm.te.Tensor - A 1-D tensor containing the index of each data element in the output tensor. + A 1-D tensor. The same size as output. For each entry in output, it contains + the index of its first occurence in the input data. The end of the tensor is padded + with the length of the input data. + + inverse_indices : tvm.te.Tensor + A 1-D tensor. For each entry in data, it contains the index of that entry in output. + (Note that inverse_indices is very similar to indices if output is not sorted.) num_unique : tvm.te.Tensor A 1-D tensor with size=1 containing the number of unique elements in the input data tensor. @@ -309,20 +317,23 @@ def unique(data, is_sorted=True, return_counts=False): -------- .. code-block:: python [output, indices, num_unique] = unique([4, 5, 1, 2, 3, 3, 4, 5], False, False) - output = [4, 5, 1, 2, 3, ?, ?, ?] - indices = [0, 1, 2, 3, 4, 4, 0, 1] - num_unique = [5] + output = [4, 5, 1, 2, 3, ?, ?, ?] + indices = [0, 1, 2, 3, 4, ?, ?, ?] + inverse_indices = [0, 1, 2, 3, 4, 4, 0, 1] + num_unique = [5] [output, indices, num_unique, counts] = unique([4, 5, 1, 2, 3, 3, 4, 5], False, True) - output = [4, 5, 1, 2, 3, ?, ?, ?] - indices = [0, 1, 2, 3, 4, 4, 0, 1] - num_unique = [5] - counts = [2, 2, 1, 1, 2, ?, ?, ?] + output = [4, 5, 1, 2, 3, ?, ?, ?] + indices = [0, 1, 2, 3, 4, ?, ?, ?] + inverse_indices = [0, 1, 2, 3, 4, 4, 0, 1] + num_unique = [5] + counts = [2, 2, 1, 1, 2, ?, ?, ?] [output, indices, num_unique] = unique([4, 5, 1, 2, 3, 3, 4, 5], True) - output = [1, 2, 3, 4, 5, ?, ?, ?] - indices = [3, 4, 0, 1, 2, 2, 3, 4] - num_unique = [5] + output = [1, 2, 3, 4, 5, ?, ?, ?] + indices = [2, 3, 4, 0, 1, ?, ?, ?] + inverse_indices = [3, 4, 0, 1, 2, 2, 3, 4] + num_unique = [5] """ sorted_data = sort(data) argsorted_indices = argsort(data, dtype="int32") diff --git a/python/tvm/topi/unique.py b/python/tvm/topi/unique.py index e7256551d7b6..b225dbdfacf4 100644 --- a/python/tvm/topi/unique.py +++ b/python/tvm/topi/unique.py @@ -194,7 +194,7 @@ def _calc_first_occurence(argsorted_indices, inc_scan): """ first_occurence = output_tensor(argsorted_indices.shape, "int32") for i in parallel(argsorted_indices.shape[0]): - first_occurence[i] = argsorted_indices.shape[0] + first_occurence[i] = argsorted_indices.shape[0] # TODO: why isn't this 0? ah because if 0 is in last spot is problem for i in parallel(argsorted_indices.shape[0]): if i == 0 or inc_scan[i] != inc_scan[i - 1]: first_occurence[inc_scan[i]] = argsorted_indices[i] @@ -220,10 +220,18 @@ def unique(data, is_sorted=True, return_counts=False): Returns ------- output : tvm.te.Tensor - A 1-D tensor containing the unique elements of the input data tensor. + A 1-D tensor containing the unique elements of the input data tensor. The same size as + the input data. If there are less unique elements than input data, the end of the tensor + is padded with zeros. indices : tvm.te.Tensor - A 1-D tensor containing the index of each data element in the output tensor. + A 1-D tensor. The same size as output. For each entry in output, it contains + the index of its first occurence in the input data. The end of the tensor is padded + with the length of the input data. + + inverse_indices : tvm.te.Tensor + A 1-D tensor. For each entry in data, it contains the index of that entry in output. + (Note that inverse_indices is very similar to indices if output is not sorted.) num_unique : tvm.te.Tensor A 1-D tensor with size=1 containing the number of unique elements in the input data tensor. @@ -235,20 +243,23 @@ def unique(data, is_sorted=True, return_counts=False): -------- .. code-block:: python [output, indices, num_unique] = unique([4, 5, 1, 2, 3, 3, 4, 5], False, False) - output = [4, 5, 1, 2, 3, ?, ?, ?] - indices = [0, 1, 2, 3, 4, 4, 0, 1] - num_unique = [5] + output = [4, 5, 1, 2, 3, ?, ?, ?] + indices = [0, 1, 2, 3, 4, ?, ?, ?] + inverse_indices = [0, 1, 2, 3, 4, 4, 0, 1] + num_unique = [5] [output, indices, num_unique, counts] = unique([4, 5, 1, 2, 3, 3, 4, 5], False, True) - output = [4, 5, 1, 2, 3, ?, ?, ?] - indices = [0, 1, 2, 3, 4, 4, 0, 1] - num_unique = [5] - counts = [2, 2, 1, 1, 2, ?, ?, ?] + output = [4, 5, 1, 2, 3, ?, ?, ?] + indices = [0, 1, 2, 3, 4, ?, ?, ?] + inverse_indices = [0, 1, 2, 3, 4, 4, 0, 1] + num_unique = [5] + counts = [2, 2, 1, 1, 2, ?, ?, ?] [output, indices, num_unique] = unique([4, 5, 1, 2, 3, 3, 4, 5], True) - output = [1, 2, 3, 4, 5, ?, ?, ?] - indices = [3, 4, 0, 1, 2, 2, 3, 4] - num_unique = [5] + output = [1, 2, 3, 4, 5, ?, ?, ?] + indices = [2, 3, 4, 0, 1, ?, ?, ?] + inverse_indices = [3, 4, 0, 1, 2, 2, 3, 4] + num_unique = [5] """ sorted_data = sort(data) argsorted_indices = argsort(data, dtype="int32") @@ -272,6 +283,7 @@ def unique(data, is_sorted=True, return_counts=False): fcompute = lambda ins, outs: _calc_unique_ir(*ins, None, *outs) else: fcompute = lambda ins, outs: _calc_unique_ir(*ins, None, *outs, None) + first_occurence = _calc_first_occurence(argsorted_indices, inc_scan) else: # calculate the index converter if the unique elements should not be sorted # calculate first occurence @@ -293,5 +305,5 @@ def unique(data, is_sorted=True, return_counts=False): tag="_calc_unique_cpu", ) if return_counts: - return [outs[0], outs[1], num_unique_elements, outs[2]] - return [*outs, num_unique_elements] + return [outs[0], first_occurence, outs[1], num_unique_elements, outs[2]] + return [outs[0], first_occurence, outs[1], num_unique_elements] diff --git a/tests/python/topi/python/test_topi_unique.py b/tests/python/topi/python/test_topi_unique.py index a97b95029862..26727b5ae943 100644 --- a/tests/python/topi/python/test_topi_unique.py +++ b/tests/python/topi/python/test_topi_unique.py @@ -34,11 +34,11 @@ def calc_numpy_unique(data, is_sorted=False): uniq = uniq[order].astype(data.dtype) inverse = np.array([reverse_order[i] for i in inverse]).astype("int32") counts = counts[order].astype("int32") - return [uniq.astype(data.dtype), inverse.astype("int32"), counts, num_uniq] + return [uniq.astype(data.dtype), index.astype("int32"), inverse.astype("int32"), counts, num_uniq] def check_unique(data, is_sorted=False): # numpy reference - np_unique, np_indices, np_counts, np_num_unique = calc_numpy_unique(data, is_sorted) + np_unique, np_indices, np_inverse_indices, np_counts, np_num_unique = calc_numpy_unique(data, is_sorted) num_unique = np_num_unique[0] implementations = { @@ -59,6 +59,7 @@ def check_unique(data, is_sorted=False): tvm_data = tvm.nd.array(data, device=dev) tvm_unique = tvm.nd.array(np.zeros(data.shape).astype(data.dtype), device=dev) tvm_indices = tvm.nd.array(np.zeros(data.shape).astype("int32"), device=dev) + tvm_inverse_indices = tvm.nd.array(np.zeros(data.shape).astype("int32"), device=dev) tvm_num_unique = tvm.nd.array(np.zeros([1]).astype("int32"), device=dev) # without counts @@ -67,13 +68,26 @@ def check_unique(data, is_sorted=False): outs = fcompute(te_input, False) s = fschedule(outs) func = tvm.build(s, [te_input, *outs]) - func(tvm_data, tvm_unique, tvm_indices, tvm_num_unique) + func(tvm_data, tvm_unique, tvm_indices, tvm_inverse_indices, tvm_num_unique) + + print("TVM_DATA: ", tvm_data) + print("TVM_UNIQUE: ", tvm_unique) + print("TVM_INDICES: ", tvm_indices) + print("TVM_INVERSE_INDICES: ", tvm_inverse_indices) + print("TVM_NUM_UNIQUE: ", tvm_num_unique) + + print("NP_DATA: ", data) + print("NP_UNIQUE: ", np_unique) + print("NP_INDICES: ", np_indices) + print("NP_INVERSE_INDICES: ", np_inverse_indices) + print("NP_NUM_UNIQUE: ", np_num_unique) assert tvm_num_unique.asnumpy()[0] == np_num_unique np.testing.assert_allclose( tvm_unique.asnumpy()[:num_unique], np_unique, atol=1e-5, rtol=1e-5 ) - np.testing.assert_allclose(tvm_indices.asnumpy(), np_indices, atol=1e-5, rtol=1e-5) + + np.testing.assert_allclose(tvm_indices.asnumpy()[:num_unique], np_indices, atol=1e-5, rtol=1e-5) # with counts tvm_counts = tvm.nd.array(np.zeros(data.shape).astype("int32"), device=dev) @@ -82,15 +96,16 @@ def check_unique(data, is_sorted=False): outs = fcompute(te_input, True) s = fschedule(outs) func = tvm.build(s, [te_input, *outs]) - func(tvm_data, tvm_unique, tvm_indices, tvm_num_unique, tvm_counts) - - np_unique, np_indices, _, np_num_unique = calc_numpy_unique(data, is_sorted) + print("Trying func") + func(tvm_data, tvm_unique, tvm_indices, tvm_inverse_indices, tvm_num_unique, tvm_counts) + print("Succeeded") + np_unique, np_indices, np_inverse_indices, _, np_num_unique = calc_numpy_unique(data, is_sorted) # TODO this is incorrect?? num_unique = np_num_unique[0] assert tvm_num_unique.asnumpy()[0] == np_num_unique np.testing.assert_allclose( tvm_unique.asnumpy()[:num_unique], np_unique, atol=1e-5, rtol=1e-5 ) - np.testing.assert_allclose(tvm_indices.asnumpy(), np_indices, atol=1e-5, rtol=1e-5) + np.testing.assert_allclose(tvm_indices.asnumpy()[:num_unique], np_indices, atol=1e-5, rtol=1e-5) np.testing.assert_allclose( tvm_counts.asnumpy()[:num_unique], np_counts, atol=1e-5, rtol=1e-5 ) From a4c9242e5eb5dcf4c3e13c9466770bb8f2ea3582 Mon Sep 17 00:00:00 2001 From: electriclilies Date: Fri, 14 May 2021 15:53:48 -0700 Subject: [PATCH 02/24] Passes on CPU, fix unique op test --- python/tvm/relay/op/_transform.py | 8 ++++++-- python/tvm/relay/op/transform.py | 4 ++-- src/relay/op/tensor/transform.cc | 1 + tests/python/relay/test_op_level3.py | 15 +++++++++------ 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/python/tvm/relay/op/_transform.py b/python/tvm/relay/op/_transform.py index 76adee477a1a..77b643c5d398 100644 --- a/python/tvm/relay/op/_transform.py +++ b/python/tvm/relay/op/_transform.py @@ -1043,24 +1043,28 @@ def ensure_tensor(tensor): def _unique_shape(data_shape): unique_shape = output_tensor((1,), "int64") indices_shape = output_tensor((1,), "int64") + inverse_indices_shape = output_tensor((1,), "int64") num_unique_shape = output_tensor((1,), "int64") unique_shape[0] = data_shape[0] indices_shape[0] = data_shape[0] + inverse_indices_shape[0] = data_shape[0] num_unique_shape[0] = int64(1) - return (unique_shape, indices_shape, num_unique_shape) + return (unique_shape, indices_shape, inverse_indices_shape, num_unique_shape) @script def _unique_with_counts_shape(data_shape): unique_shape = output_tensor((1,), "int64") indices_shape = output_tensor((1,), "int64") + inverse_indices_shape = output_tensor((1,), "int64") num_unique_shape = output_tensor((1,), "int64") counts_shape = output_tensor((1,), "int64") unique_shape[0] = data_shape[0] indices_shape[0] = data_shape[0] + inverse_indices_shape[0] = data_shape[0] num_unique_shape[0] = int64(1) counts_shape[0] = data_shape[0] - return (unique_shape, indices_shape, num_unique_shape, counts_shape) + return (unique_shape, indices_shape, inverse_indices_shape, num_unique_shape, counts_shape) @_reg.register_shape_func("unique", False) diff --git a/python/tvm/relay/op/transform.py b/python/tvm/relay/op/transform.py index 4e406e81ef68..15659f9da99f 100644 --- a/python/tvm/relay/op/transform.py +++ b/python/tvm/relay/op/transform.py @@ -1688,5 +1688,5 @@ def unique(data, is_sorted=True, return_counts=False): num_unique = [5] """ if return_counts: - return TupleWrapper(_make.unique(data, is_sorted, return_counts), 4) - return TupleWrapper(_make.unique(data, is_sorted, return_counts), 3) + return TupleWrapper(_make.unique(data, is_sorted, return_counts), 5) + return TupleWrapper(_make.unique(data, is_sorted, return_counts), 4) diff --git a/src/relay/op/tensor/transform.cc b/src/relay/op/tensor/transform.cc index 9111fa529e5b..525ce6a4cb57 100644 --- a/src/relay/op/tensor/transform.cc +++ b/src/relay/op/tensor/transform.cc @@ -3955,6 +3955,7 @@ bool UniqueRel(const Array& types, int num_inputs, const Attrs& attrs, std::vector fields; fields.push_back(TensorType(data->shape, data->dtype)); // unique fields.push_back(TensorType(data->shape, DataType::Int(32))); // indices + fields.push_back(TensorType(data->shape, DataType::Int(32))); // inverse_indices fields.push_back(TensorType(Array{1}, DataType::Int(32))); // num_unique const auto* param = attrs.as(); if (param->return_counts) { diff --git a/tests/python/relay/test_op_level3.py b/tests/python/relay/test_op_level3.py index e84b22b30ce1..3ac141746e3c 100644 --- a/tests/python/relay/test_op_level3.py +++ b/tests/python/relay/test_op_level3.py @@ -1928,7 +1928,7 @@ def calc_numpy_unique(data, is_sorted=False): uniq = uniq[order].astype(data.dtype) inverse = np.array([reverse_order[i] for i in inverse]).astype("int32") counts = counts[order].astype("int32") - return [uniq.astype(data.dtype), inverse.astype("int32"), counts, num_uniq] + return [uniq.astype(data.dtype), index.astype("int32"), inverse.astype("int32"), num_uniq, counts] def verify_unique(n, dtype, is_dyn=False, is_sorted=False, return_counts=False): if is_dyn: @@ -1949,18 +1949,21 @@ def verify_unique(n, dtype, is_dyn=False, is_sorted=False, return_counts=False): for kind in backends: mod = tvm.ir.IRModule.from_expr(func) intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) - tvm_res = intrp.evaluate()(x_data) - np_res = calc_numpy_unique(x_data, is_sorted) + tvm_res = intrp.evaluate()(x_data) # unique, indices, inverse_indices, num_unique, (counts) + np_res = calc_numpy_unique(x_data, is_sorted) # unique, indices, inverse_indices, num_unique, counts num_unique = np_res[3][0] - assert num_unique == tvm_res[2].asnumpy()[0] + # num_unique + assert num_unique == tvm_res[3].asnumpy()[0] # unique tvm.testing.assert_allclose(tvm_res[0].asnumpy()[:num_unique], np_res[0], rtol=1e-5) + # indices + tvm.testing.assert_allclose(tvm_res[1].asnumpy()[:num_unique], np_res[1], rtol=1e-5) # inverse_indices - tvm.testing.assert_allclose(tvm_res[1].asnumpy(), np_res[1], rtol=1e-5) + tvm.testing.assert_allclose(tvm_res[2].asnumpy(), np_res[2], rtol=1e-5) # counts if return_counts: tvm.testing.assert_allclose( - tvm_res[3].asnumpy()[:num_unique], np_res[2], rtol=1e-5 + tvm_res[4].asnumpy()[:num_unique], np_res[4], rtol=1e-5 ) for dtype in ["int32", "int64"]: From e7cd0514d0a01312f5cdd205b90f440e6651b80d Mon Sep 17 00:00:00 2001 From: electriclilies Date: Tue, 18 May 2021 12:20:53 -0700 Subject: [PATCH 03/24] more changes --- python/tvm/relay/expr.py | 2 +- python/tvm/relay/frontend/onnx.py | 28 ++++++++++++++++++++++ python/tvm/relay/op/_transform.py | 2 +- python/tvm/relay/op/transform.py | 7 ++++-- python/tvm/topi/cuda/unique.py | 8 +++---- python/tvm/topi/unique.py | 4 ++-- tests/python/frontend/onnx/test_forward.py | 4 ++-- 7 files changed, 43 insertions(+), 12 deletions(-) diff --git a/python/tvm/relay/expr.py b/python/tvm/relay/expr.py index 8d73a090ed6f..f5b2850eaac0 100644 --- a/python/tvm/relay/expr.py +++ b/python/tvm/relay/expr.py @@ -405,7 +405,7 @@ def __init__(self, tuple_value, size): def astuple(self): """Returns the underlying Relay tuple if this wrapper is passed - as an argument to an FFI function.""" + as an argumentn to an FFI function.""" return self.tuple_value def astext(self): diff --git a/python/tvm/relay/frontend/onnx.py b/python/tvm/relay/frontend/onnx.py index f3282f03c813..d04a4f9677e6 100644 --- a/python/tvm/relay/frontend/onnx.py +++ b/python/tvm/relay/frontend/onnx.py @@ -2844,6 +2844,28 @@ def _impl_v11(cls, inputs, attr, params): raise ValueError("Unsupported Shift Direction: " + direction) return out +class Unique(OnnxOpConverter): + """Operator converter for unique""" + + @classmethod + def _impl_v11(cls, inputs, attr, params): + if len(inputs) != 1: + raise ValueError("Unique expects 1 input") + + data = inputs[0] + axis = attr.get("axis", None) + if axis is None: # If axis is None, flatten the input before calling unique + data = _op.reshape(data, _op.const([-1])) + else: + data_shape = infer_shape(data) + if len(data_shape) != 1: + raise ValueError("TVM only supports 1D Unique operator.") + sorted = attr.get("sorted", 1) # sorted is 0 or 1, 1 by default + + # ONNX documentation doesn't list return_counts as an attribute, but it lists return_counts as optional. + # Therefore we'll just always return it. + print(type(data)) + return _op.unique(data, is_sorted=(sorted == 1), return_counts=True) # compatible operators that do NOT require any conversion. _identity_list = [] @@ -3008,6 +3030,7 @@ def _get_convert_map(opset): "NonZero": NonZero.get_converter(opset), "Range": Range.get_converter(opset), "CumSum": CumSum.get_converter(opset), + "Unique": Unique.get_converter(opset), # defs/control_flow "Loop": Loop.get_converter(opset), "If": If.get_converter(opset), @@ -3229,6 +3252,11 @@ def from_onnx(self, graph, opset, get_output_expr=False): if outputs_num == 1: self._nodes[node_output[0]] = fold_constant(op) else: + print("op: ", op) + print() + print("op.astuple(): ", op.astuple()) + print() + print("type of op.astuple(): ", type(op.astuple())) op = _expr.TupleWrapper(fold_constant(op.astuple()), len(op)) for k, i in zip(list(node_output), range(len(node_output))): self._nodes[k] = op[i] diff --git a/python/tvm/relay/op/_transform.py b/python/tvm/relay/op/_transform.py index 77b643c5d398..30554a706628 100644 --- a/python/tvm/relay/op/_transform.py +++ b/python/tvm/relay/op/_transform.py @@ -174,7 +174,7 @@ def compute_cumprod(attrs, inputs, output_type): @_reg.register_compute("unique") def compute_unique(attrs, inputs, output_type): """Compute definition of unique""" - return topi.unique(inputs[0], attrs.sorted, attrs.return_counts) + topi.unique(inputs[0], attrs.sorted, attrs.return_counts) _reg.register_strategy("unique", strategy.unique_strategy) diff --git a/python/tvm/relay/op/transform.py b/python/tvm/relay/op/transform.py index 15659f9da99f..f7e4eb4cdc25 100644 --- a/python/tvm/relay/op/transform.py +++ b/python/tvm/relay/op/transform.py @@ -1648,7 +1648,7 @@ def unique(data, is_sorted=True, return_counts=False): data : relay.Expr A 1-D tensor of integers. - sorted : bool + is_sorted : bool Whether to sort the unique elements in ascending order before returning as output. return_counts : bool @@ -1656,12 +1656,15 @@ def unique(data, is_sorted=True, return_counts=False): Returns ------- - output : relay.Expr + unique : relay.Expr A 1-D tensor containing the unique elements of the input data tensor. indices : relay.Expr A 1-D tensor containing the index of each data element in the output tensor. + inverse_indices : relay.Expr + A 1-D tensor. For each entry in data, it contains the index of that data element in the unique array. + num_unique : relay.Expr A 1-D tensor with size=1 containing the number of unique elements in the input data tensor. diff --git a/python/tvm/topi/cuda/unique.py b/python/tvm/topi/cuda/unique.py index bbb0dacb5784..caedfde2a027 100644 --- a/python/tvm/topi/cuda/unique.py +++ b/python/tvm/topi/cuda/unique.py @@ -293,7 +293,7 @@ def unique(data, is_sorted=True, return_counts=False): Returns ------- - output : tvm.te.Tensor + unique : tvm.te.Tensor A 1-D tensor containing the unique elements of the input data tensor. The same size as the input data. If there are less unique elements than input data, the end of the tensor is padded with zeros. @@ -304,7 +304,7 @@ def unique(data, is_sorted=True, return_counts=False): with the length of the input data. inverse_indices : tvm.te.Tensor - A 1-D tensor. For each entry in data, it contains the index of that entry in output. + A 1-D tensor. For each entry in data, it contains the index of that data element in the unique array. (Note that inverse_indices is very similar to indices if output is not sorted.) num_unique : tvm.te.Tensor @@ -412,5 +412,5 @@ def unique(data, is_sorted=True, return_counts=False): tag="_calc_unique_gpu", ) if return_counts: - return [outs[0], outs[1], num_unique_elements, outs[2]] - return [*outs, num_unique_elements] + return [outs[0], first_occurence, outs[1], num_unique_elements, outs[2]] + return [outs[0], first_occurence, outs[1], num_unique_elements] diff --git a/python/tvm/topi/unique.py b/python/tvm/topi/unique.py index b225dbdfacf4..01991d5c1d40 100644 --- a/python/tvm/topi/unique.py +++ b/python/tvm/topi/unique.py @@ -219,7 +219,7 @@ def unique(data, is_sorted=True, return_counts=False): Returns ------- - output : tvm.te.Tensor + unique : tvm.te.Tensor A 1-D tensor containing the unique elements of the input data tensor. The same size as the input data. If there are less unique elements than input data, the end of the tensor is padded with zeros. @@ -230,7 +230,7 @@ def unique(data, is_sorted=True, return_counts=False): with the length of the input data. inverse_indices : tvm.te.Tensor - A 1-D tensor. For each entry in data, it contains the index of that entry in output. + A 1-D tensor. For each entry in data, it contains the index of that data element in the unique array. (Note that inverse_indices is very similar to indices if output is not sorted.) num_unique : tvm.te.Tensor diff --git a/tests/python/frontend/onnx/test_forward.py b/tests/python/frontend/onnx/test_forward.py index 3ffeb3e4f788..7a021fb9cdad 100644 --- a/tests/python/frontend/onnx/test_forward.py +++ b/tests/python/frontend/onnx/test_forward.py @@ -4246,11 +4246,11 @@ def verify_cumsum(indata, axis, exclusive=0, reverse=0, type="float32"): "test_tfidfvectorizer_tf_onlybigrams_levelempty/", "test_tfidfvectorizer_tf_onlybigrams_skip5/", "test_tfidfvectorizer_tf_uniandbigrams_skip5/", - "test_unique_not_sorted_without_axis/", + #"test_unique_not_sorted_without_axis/", "test_unique_sorted_with_axis/", "test_unique_sorted_with_axis_3d/", "test_unique_sorted_with_negative_axis/", - "test_unique_sorted_without_axis/", + #"test_unique_sorted_without_axis/", "test_upsample_nearest/", ] From f5ae586e22da1b53a36dc2183f3f0dbba977951d Mon Sep 17 00:00:00 2001 From: electriclilies Date: Tue, 18 May 2021 13:04:04 -0700 Subject: [PATCH 04/24] mtrying to fix optional outputs in onnx importer --- python/tvm/relay/frontend/onnx.py | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/python/tvm/relay/frontend/onnx.py b/python/tvm/relay/frontend/onnx.py index d04a4f9677e6..a2b72901d072 100644 --- a/python/tvm/relay/frontend/onnx.py +++ b/python/tvm/relay/frontend/onnx.py @@ -3218,6 +3218,15 @@ def from_onnx(self, graph, opset, get_output_expr=False): outputs_num = 1 else: outputs_num = len(op) + + if outputs_num == 1: + node_output[0] = fold_constant(op) + elif len(outputs) != outputs_num: + op = _expr.TupleWrapper(fold_constant(op.astuple()), len(op)) + for k, i in zip(list(node_output), range(len(node_output))): + self._nodes[k] = op[i] + + if outputs_num > 1: # ONNX supports optional outputs for some nodes. # This block searches for missing outputs in the ONNX graph @@ -3239,7 +3248,7 @@ def from_onnx(self, graph, opset, get_output_expr=False): # Create the new op with valid outputs if len(outputs) == 1: op = outputs[0] - else: + elif len(outputs) != outputs_num: op = _expr.TupleWrapper(outputs, len(outputs)) # Drop invalid outputs for the onnx node outputs_num = len(outputs) @@ -3249,17 +3258,6 @@ def from_onnx(self, graph, opset, get_output_expr=False): ), "Number of output mismatch {} vs {} in {}.".format( len(node_output), outputs_num, op_name ) - if outputs_num == 1: - self._nodes[node_output[0]] = fold_constant(op) - else: - print("op: ", op) - print() - print("op.astuple(): ", op.astuple()) - print() - print("type of op.astuple(): ", type(op.astuple())) - op = _expr.TupleWrapper(fold_constant(op.astuple()), len(op)) - for k, i in zip(list(node_output), range(len(node_output))): - self._nodes[k] = op[i] # now return the outputs outputs = [self._nodes[self._parse_value_proto(i)] for i in graph.output] From 78d208712500d629e7991c1b390fb1f7f3fd7489 Mon Sep 17 00:00:00 2001 From: electriclilies Date: Tue, 18 May 2021 14:44:31 -0700 Subject: [PATCH 05/24] TupleGetItem is being passed a stringgit add python/tvm/relay/frontend/onnx.py debugging print statements --- python/tvm/relay/expr.py | 2 ++ python/tvm/relay/frontend/onnx.py | 21 +++++++++++++++------ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/python/tvm/relay/expr.py b/python/tvm/relay/expr.py index f5b2850eaac0..d1a6ac0a359a 100644 --- a/python/tvm/relay/expr.py +++ b/python/tvm/relay/expr.py @@ -419,6 +419,8 @@ def astext(self): return self.tuple_value.astext() def __getitem__(self, index): + print("INdex is: ", index) + print("Type of index is: ", type(index)) if index >= len(self): raise IndexError("Tuple index out of range") return TupleGetItem(self.tuple_value, index) diff --git a/python/tvm/relay/frontend/onnx.py b/python/tvm/relay/frontend/onnx.py index 8d3eaf8ba482..df0cfbc0c12b 100644 --- a/python/tvm/relay/frontend/onnx.py +++ b/python/tvm/relay/frontend/onnx.py @@ -3324,12 +3324,21 @@ def from_onnx(self, graph, opset, get_output_expr=False): outputs_num = len(op) if outputs_num == 1: - node_output[0] = fold_constant(op) - elif len(outputs) != outputs_num: - op = _expr.TupleWrapper(fold_constant(op.astuple()), len(op)) - for k, i in zip(list(node_output), range(len(node_output))): - self._nodes[k] = op[i] - + op = fold_constant(op) + else: + #elif len(outputs) != outputs_num: + print("op before: ", op) + tuple_list = [] + for i in range(len(node_output)): + print("index: ", type(i)) + folded_op_i = fold_constant(op[i]) + print("folded op[i]: ", folded_op_i) + tuple_list.append(folded_op_i) + + op = _expr.TupleWrapper(_expr.Tuple(tuple_list), len(node_output)) + print("op after: ", op) + # somehow tuplegetitem is getting a string?? + self._nodes = op if outputs_num > 1: # ONNX supports optional outputs for some nodes. From 13d9af82fc0d27e56ac6d5d426df8d8f048b4bb6 Mon Sep 17 00:00:00 2001 From: electriclilies Date: Thu, 20 May 2021 17:14:18 -0700 Subject: [PATCH 06/24] Unique is passing onnx unit tests --- python/tvm/relay/frontend/onnx.py | 38 ++++++++++++++++++------------- python/tvm/topi/unique.py | 27 ++++++++++++---------- src/relay/op/tensor/transform.cc | 2 +- 3 files changed, 38 insertions(+), 29 deletions(-) diff --git a/python/tvm/relay/frontend/onnx.py b/python/tvm/relay/frontend/onnx.py index df0cfbc0c12b..6dc0419a0d3e 100644 --- a/python/tvm/relay/frontend/onnx.py +++ b/python/tvm/relay/frontend/onnx.py @@ -2967,8 +2967,19 @@ def _impl_v11(cls, inputs, attr, params): # ONNX documentation doesn't list return_counts as an attribute, but it lists return_counts as optional. # Therefore we'll just always return it. - print(type(data)) - return _op.unique(data, is_sorted=(sorted == 1), return_counts=True) + unique = _op.unique(data, is_sorted=(sorted == 1), return_counts=True) + unique_shape = infer_type(unique[0]).checked_type.shape + num_unique = unique[3] + + trim_unique_lambda = lambda input: _op.strided_slice(input, _op.const([0]), num_unique) + + unique_vals = trim_unique_lambda(unique[0]) + indices = trim_unique_lambda(unique[1]) + inverse_indices = unique[2] + counts = trim_unique_lambda(unique[4]) + # ONNX unique returns unique, indices, inverse_indices, (optional) counts + return _expr.TupleWrapper(_expr.Tuple([unique_vals, indices, inverse_indices, counts]), 4) + # compatible operators that do NOT require any conversion. _identity_list = [] @@ -3326,19 +3337,7 @@ def from_onnx(self, graph, opset, get_output_expr=False): if outputs_num == 1: op = fold_constant(op) else: - #elif len(outputs) != outputs_num: - print("op before: ", op) - tuple_list = [] - for i in range(len(node_output)): - print("index: ", type(i)) - folded_op_i = fold_constant(op[i]) - print("folded op[i]: ", folded_op_i) - tuple_list.append(folded_op_i) - - op = _expr.TupleWrapper(_expr.Tuple(tuple_list), len(node_output)) - print("op after: ", op) - # somehow tuplegetitem is getting a string?? - self._nodes = op + op = _expr.TupleWrapper(fold_constant(op.astuple()), len(op)) if outputs_num > 1: # ONNX supports optional outputs for some nodes. @@ -3362,7 +3361,7 @@ def from_onnx(self, graph, opset, get_output_expr=False): if len(outputs) == 1: op = outputs[0] elif len(outputs) != outputs_num: - op = _expr.TupleWrapper(outputs, len(outputs)) + op = _expr.TupleWrapper(_expr.Tuple(outputs), len(outputs)) # Drop invalid outputs for the onnx node outputs_num = len(outputs) node_output = [output for output in node_output if output != ""] @@ -3372,6 +3371,13 @@ def from_onnx(self, graph, opset, get_output_expr=False): len(node_output), outputs_num, op_name ) + if outputs_num == 1: + self._nodes[node_output[0]] = op + else: + for k, i in zip(list(node_output), range(len(node_output))): + self._nodes[k] = op[i] + + # now return the outputs outputs = [self._nodes[self._parse_value_proto(i)] for i in graph.output] outputs = outputs[0] if len(outputs) == 1 else _expr.Tuple(outputs) diff --git a/python/tvm/topi/unique.py b/python/tvm/topi/unique.py index 01991d5c1d40..7d389e0e8324 100644 --- a/python/tvm/topi/unique.py +++ b/python/tvm/topi/unique.py @@ -93,7 +93,7 @@ def _calc_num_unique(inc_scan): def _calc_unique_ir( - data, argsorted_indices, inc_scan, index_converter, unique_elements, indices, counts + data, argsorted_indices, inc_scan, index_converter, unique_elements, inverse_indices, counts ): """Low level IR to calculate unique elements, inverse indices, and counts (optional) of unique elements of 1-D array. @@ -117,7 +117,7 @@ def _calc_unique_ir( unique_elements : Buffer A buffer that stores the unique elements. - indices : Buffer + inverse_indices : Buffer A buffer that stores the the index of each input data element in the unique element array. counts (optional) : Buffer @@ -128,7 +128,7 @@ def _calc_unique_ir( argsorted_indices_ptr = ib.buffer_ptr(argsorted_indices) inc_scan_ptr = ib.buffer_ptr(inc_scan) unique_elements_ptr = ib.buffer_ptr(unique_elements) - indices_ptr = ib.buffer_ptr(indices) + inverse_indices_ptr = ib.buffer_ptr(inverse_indices) index_converter_ptr = None if isinstance(index_converter, tir.Buffer): @@ -137,7 +137,7 @@ def _calc_unique_ir( if isinstance(counts, tir.Buffer): counts_ptr = ib.buffer_ptr(counts) # use indices_ptr as a tmp buffer to store tids with inc_scan[tid] != inc_scan[tid-1] - unique_seq_indices_ptr = ib.buffer_ptr(indices) + unique_seq_indices_ptr = ib.buffer_ptr(inverse_indices) data_length = data.shape[0] @@ -167,7 +167,7 @@ def _calc_unique_ir( unique_idx = ( inc_scan_ptr[i] if not index_converter_ptr else index_converter_ptr[inc_scan_ptr[i]] ) - indices_ptr[data_idx] = unique_idx + inverse_indices_ptr[data_idx] = unique_idx with ib.if_scope(i == 0): unique_elements_ptr[unique_idx] = data_ptr[data_idx] with ib.else_scope(): @@ -194,7 +194,7 @@ def _calc_first_occurence(argsorted_indices, inc_scan): """ first_occurence = output_tensor(argsorted_indices.shape, "int32") for i in parallel(argsorted_indices.shape[0]): - first_occurence[i] = argsorted_indices.shape[0] # TODO: why isn't this 0? ah because if 0 is in last spot is problem + first_occurence[i] = argsorted_indices.shape[0] for i in parallel(argsorted_indices.shape[0]): if i == 0 or inc_scan[i] != inc_scan[i - 1]: first_occurence[inc_scan[i]] = argsorted_indices[i] @@ -277,17 +277,17 @@ def unique(data, is_sorted=True, return_counts=False): out_data_shape = [data.shape] * 2 out_dtypes = [data.dtype, "int32"] # prepare inputs and fcompute + + first_occurence = _calc_first_occurence(argsorted_indices, inc_scan) if is_sorted: in_data = [data, argsorted_indices, inc_scan] if return_counts: fcompute = lambda ins, outs: _calc_unique_ir(*ins, None, *outs) else: fcompute = lambda ins, outs: _calc_unique_ir(*ins, None, *outs, None) - first_occurence = _calc_first_occurence(argsorted_indices, inc_scan) + + indices = first_occurence else: - # calculate the index converter if the unique elements should not be sorted - # calculate first occurence - first_occurence = _calc_first_occurence(argsorted_indices, inc_scan) # calculate index converter by sorting unique elements by their first occurence argsorted_first_occurence = argsort(first_occurence, dtype="int32") index_converter = argsort(argsorted_first_occurence, dtype="int32") @@ -296,6 +296,9 @@ def unique(data, is_sorted=True, return_counts=False): fcompute = lambda ins, outs: _calc_unique_ir(*ins, *outs) else: fcompute = lambda ins, outs: _calc_unique_ir(*ins, *outs, None) + # First occurence is in order of sorted unique output, if we sort the first_occurence array we get the correct result + indices = sort(first_occurence) + outs = te.extern( out_data_shape, in_data, @@ -305,5 +308,5 @@ def unique(data, is_sorted=True, return_counts=False): tag="_calc_unique_cpu", ) if return_counts: - return [outs[0], first_occurence, outs[1], num_unique_elements, outs[2]] - return [outs[0], first_occurence, outs[1], num_unique_elements] + return [outs[0], indices, outs[1], num_unique_elements, outs[2]] + return [outs[0], indices, outs[1], num_unique_elements] diff --git a/src/relay/op/tensor/transform.cc b/src/relay/op/tensor/transform.cc index b9e710251efd..44933cd0154c 100644 --- a/src/relay/op/tensor/transform.cc +++ b/src/relay/op/tensor/transform.cc @@ -3952,7 +3952,7 @@ bool UniqueRel(const Array& types, int num_inputs, const Attrs& attrs, } const int ndim = static_cast(data->shape.size()); ICHECK_EQ(ndim, 1) << "Unique: input must be 1-D tensor"; - ICHECK_EQ(data->dtype.is_int(), true) << "Unique: input must have int32 or int64 dtype"; + std::vector fields; fields.push_back(TensorType(data->shape, data->dtype)); // unique fields.push_back(TensorType(data->shape, DataType::Int(32))); // indices From d8cbe25e5460adf3356396bbe0658b56608bf487 Mon Sep 17 00:00:00 2001 From: electriclilies Date: Thu, 20 May 2021 17:19:44 -0700 Subject: [PATCH 07/24] fix indices --- python/tvm/topi/cuda/unique.py | 45 +++++++++++++++++----------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/python/tvm/topi/cuda/unique.py b/python/tvm/topi/cuda/unique.py index caedfde2a027..c71c61e79bac 100644 --- a/python/tvm/topi/cuda/unique.py +++ b/python/tvm/topi/cuda/unique.py @@ -119,7 +119,7 @@ def _calc_num_unique(inc_scan): def _calc_unique_ir( - data, argsorted_indices, inc_scan, index_converter, unique_elements, indices, counts + data, argsorted_indices, inc_scan, index_converter, unique_elements, inverse_indices, counts ): """Low level IR to calculate unique elements, inverse indices, and counts (optional) of unique elements of 1-D array. @@ -143,7 +143,7 @@ def _calc_unique_ir( unique_elements : Buffer A buffer that stores the unique elements. - indices : Buffer + inverse_indices : Buffer A buffer that stores the the index of each input data element in the unique element array. counts (optional) : Buffer @@ -154,7 +154,7 @@ def _calc_unique_ir( argsorted_indices_ptr = ib.buffer_ptr(argsorted_indices) inc_scan_ptr = ib.buffer_ptr(inc_scan) unique_elements_ptr = ib.buffer_ptr(unique_elements) - indices_ptr = ib.buffer_ptr(indices) + inverse_indices_ptr = ib.buffer_ptr(inverse_indices) index_converter_ptr = None if isinstance(index_converter, tir.Buffer): @@ -163,7 +163,7 @@ def _calc_unique_ir( if isinstance(counts, tir.Buffer): counts_ptr = ib.buffer_ptr(counts) # use indices_ptr as a tmp buffer to store tids with inc_scan[tid] != inc_scan[tid-1] - unique_seq_indices_ptr = ib.buffer_ptr(indices) + unique_seq_indices_ptr = ib.buffer_ptr(inverse_indices) batch_size = data.shape[0] max_threads = _get_max_threads(batch_size) @@ -218,7 +218,7 @@ def _calc_unique_ir( if not index_converter_ptr else index_converter_ptr[inc_scan_ptr[tid]] ) - indices_ptr[data_idx] = unique_idx + inverse_indices_ptr[data_idx] = unique_idx with ib.if_scope(tid == 0): unique_elements_ptr[unique_idx] = data_ptr[data_idx] with ib.else_scope(): @@ -366,6 +366,20 @@ def unique(data, is_sorted=True, return_counts=False): out_buffers = [unique_elements_buf, inverse_indices_buf] out_dtypes = [data.dtype, "int32"] # prepare inputs and fcompute + # calculate first occurence + first_occurence_buf = tir.decl_buffer( + data.shape, "int32", "first_occurence_buf", data_alignment=8 + ) + first_occurence = te.extern( + [data.shape], + [argsorted_indices, inc_scan], + lambda ins, outs: _calc_first_occurence_ir(ins[0], ins[1], outs[0]), + dtype=["int32"], + in_buffers=[argsorted_indices_buf, inc_scan_buf], + out_buffers=[first_occurence_buf], + name="_calc_first_occurence", + tag="_calc_first_occurence_gpu", + ) if is_sorted: in_data = [data, argsorted_indices, inc_scan] in_buffers = [data_buf, argsorted_indices_buf, inc_scan_buf] @@ -373,22 +387,8 @@ def unique(data, is_sorted=True, return_counts=False): fcompute = lambda ins, outs: _calc_unique_ir(*ins, None, *outs) else: fcompute = lambda ins, outs: _calc_unique_ir(*ins, None, *outs, None) + indices = first_occurence else: - # calculate the index converter if the unique elements should not be sorted - # calculate first occurence - first_occurence_buf = tir.decl_buffer( - data.shape, "int32", "first_occurence_buf", data_alignment=8 - ) - first_occurence = te.extern( - [data.shape], - [argsorted_indices, inc_scan], - lambda ins, outs: _calc_first_occurence_ir(ins[0], ins[1], outs[0]), - dtype=["int32"], - in_buffers=[argsorted_indices_buf, inc_scan_buf], - out_buffers=[first_occurence_buf], - name="_calc_first_occurence", - tag="_calc_first_occurence_gpu", - ) # calculate index converter by sorting unique elements by their first occurence argsorted_first_occurence = argsort(first_occurence, dtype="int32") index_converter = argsort(argsorted_first_occurence, dtype="int32") @@ -401,6 +401,7 @@ def unique(data, is_sorted=True, return_counts=False): fcompute = lambda ins, outs: _calc_unique_ir(*ins, *outs) else: fcompute = lambda ins, outs: _calc_unique_ir(*ins, *outs, None) + indices = sort(first_occurence) outs = te.extern( out_data_shape, in_data, @@ -412,5 +413,5 @@ def unique(data, is_sorted=True, return_counts=False): tag="_calc_unique_gpu", ) if return_counts: - return [outs[0], first_occurence, outs[1], num_unique_elements, outs[2]] - return [outs[0], first_occurence, outs[1], num_unique_elements] + return [outs[0], indices, outs[1], num_unique_elements, outs[2]] + return [outs[0], indices, outs[1], num_unique_elements] From d7853cfae1eff44e1d1ca6cb00e838007e68d958 Mon Sep 17 00:00:00 2001 From: electriclilies Date: Thu, 20 May 2021 17:33:02 -0700 Subject: [PATCH 08/24] change comment --- python/tvm/relay/frontend/onnx.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/python/tvm/relay/frontend/onnx.py b/python/tvm/relay/frontend/onnx.py index 6dc0419a0d3e..a98faa68df49 100644 --- a/python/tvm/relay/frontend/onnx.py +++ b/python/tvm/relay/frontend/onnx.py @@ -2965,10 +2965,9 @@ def _impl_v11(cls, inputs, attr, params): raise ValueError("TVM only supports 1D Unique operator.") sorted = attr.get("sorted", 1) # sorted is 0 or 1, 1 by default - # ONNX documentation doesn't list return_counts as an attribute, but it lists return_counts as optional. + # ONNX documentation lists return_counts as optional but there is no input to specify whether it is returned. # Therefore we'll just always return it. unique = _op.unique(data, is_sorted=(sorted == 1), return_counts=True) - unique_shape = infer_type(unique[0]).checked_type.shape num_unique = unique[3] trim_unique_lambda = lambda input: _op.strided_slice(input, _op.const([0]), num_unique) From 0c1544eff7f0880b78e179702d93c6deeebc2e75 Mon Sep 17 00:00:00 2001 From: electriclilies Date: Thu, 20 May 2021 17:41:30 -0700 Subject: [PATCH 09/24] fix return of compute unique --- python/tvm/relay/expr.py | 4 +--- python/tvm/relay/op/_transform.py | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/python/tvm/relay/expr.py b/python/tvm/relay/expr.py index d1a6ac0a359a..8d73a090ed6f 100644 --- a/python/tvm/relay/expr.py +++ b/python/tvm/relay/expr.py @@ -405,7 +405,7 @@ def __init__(self, tuple_value, size): def astuple(self): """Returns the underlying Relay tuple if this wrapper is passed - as an argumentn to an FFI function.""" + as an argument to an FFI function.""" return self.tuple_value def astext(self): @@ -419,8 +419,6 @@ def astext(self): return self.tuple_value.astext() def __getitem__(self, index): - print("INdex is: ", index) - print("Type of index is: ", type(index)) if index >= len(self): raise IndexError("Tuple index out of range") return TupleGetItem(self.tuple_value, index) diff --git a/python/tvm/relay/op/_transform.py b/python/tvm/relay/op/_transform.py index 30554a706628..77b643c5d398 100644 --- a/python/tvm/relay/op/_transform.py +++ b/python/tvm/relay/op/_transform.py @@ -174,7 +174,7 @@ def compute_cumprod(attrs, inputs, output_type): @_reg.register_compute("unique") def compute_unique(attrs, inputs, output_type): """Compute definition of unique""" - topi.unique(inputs[0], attrs.sorted, attrs.return_counts) + return topi.unique(inputs[0], attrs.sorted, attrs.return_counts) _reg.register_strategy("unique", strategy.unique_strategy) From 5c8f86c9764ae054c478b3f2af689ee27bdf2552 Mon Sep 17 00:00:00 2001 From: electriclilies Date: Thu, 20 May 2021 17:46:06 -0700 Subject: [PATCH 10/24] black --- python/tvm/relay/frontend/onnx.py | 6 ++--- tests/python/frontend/onnx/test_forward.py | 2 -- tests/python/relay/test_op_level3.py | 16 ++++++++++--- tests/python/topi/python/test_topi_unique.py | 24 ++++++++++++++++---- 4 files changed, 35 insertions(+), 13 deletions(-) diff --git a/python/tvm/relay/frontend/onnx.py b/python/tvm/relay/frontend/onnx.py index a98faa68df49..e95091e1711c 100644 --- a/python/tvm/relay/frontend/onnx.py +++ b/python/tvm/relay/frontend/onnx.py @@ -2947,6 +2947,7 @@ def _impl_v11(cls, inputs, attr, params): raise ValueError("Unsupported Shift Direction: " + direction) return out + class Unique(OnnxOpConverter): """Operator converter for unique""" @@ -2957,13 +2958,13 @@ def _impl_v11(cls, inputs, attr, params): data = inputs[0] axis = attr.get("axis", None) - if axis is None: # If axis is None, flatten the input before calling unique + if axis is None: # If axis is None, flatten the input before calling unique data = _op.reshape(data, _op.const([-1])) else: data_shape = infer_shape(data) if len(data_shape) != 1: raise ValueError("TVM only supports 1D Unique operator.") - sorted = attr.get("sorted", 1) # sorted is 0 or 1, 1 by default + sorted = attr.get("sorted", 1) # sorted is 0 or 1, 1 by default # ONNX documentation lists return_counts as optional but there is no input to specify whether it is returned. # Therefore we'll just always return it. @@ -3376,7 +3377,6 @@ def from_onnx(self, graph, opset, get_output_expr=False): for k, i in zip(list(node_output), range(len(node_output))): self._nodes[k] = op[i] - # now return the outputs outputs = [self._nodes[self._parse_value_proto(i)] for i in graph.output] outputs = outputs[0] if len(outputs) == 1 else _expr.Tuple(outputs) diff --git a/tests/python/frontend/onnx/test_forward.py b/tests/python/frontend/onnx/test_forward.py index fc399dfa9577..1c0160b58bea 100644 --- a/tests/python/frontend/onnx/test_forward.py +++ b/tests/python/frontend/onnx/test_forward.py @@ -4256,11 +4256,9 @@ def verify_cumsum(indata, axis, exclusive=0, reverse=0, type="float32"): "test_tfidfvectorizer_tf_onlybigrams_levelempty/", "test_tfidfvectorizer_tf_onlybigrams_skip5/", "test_tfidfvectorizer_tf_uniandbigrams_skip5/", - #"test_unique_not_sorted_without_axis/", "test_unique_sorted_with_axis/", "test_unique_sorted_with_axis_3d/", "test_unique_sorted_with_negative_axis/", - #"test_unique_sorted_without_axis/", "test_upsample_nearest/", ] diff --git a/tests/python/relay/test_op_level3.py b/tests/python/relay/test_op_level3.py index 3ac141746e3c..4e0de5afa325 100644 --- a/tests/python/relay/test_op_level3.py +++ b/tests/python/relay/test_op_level3.py @@ -1928,7 +1928,13 @@ def calc_numpy_unique(data, is_sorted=False): uniq = uniq[order].astype(data.dtype) inverse = np.array([reverse_order[i] for i in inverse]).astype("int32") counts = counts[order].astype("int32") - return [uniq.astype(data.dtype), index.astype("int32"), inverse.astype("int32"), num_uniq, counts] + return [ + uniq.astype(data.dtype), + index.astype("int32"), + inverse.astype("int32"), + num_uniq, + counts, + ] def verify_unique(n, dtype, is_dyn=False, is_sorted=False, return_counts=False): if is_dyn: @@ -1949,8 +1955,12 @@ def verify_unique(n, dtype, is_dyn=False, is_sorted=False, return_counts=False): for kind in backends: mod = tvm.ir.IRModule.from_expr(func) intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) - tvm_res = intrp.evaluate()(x_data) # unique, indices, inverse_indices, num_unique, (counts) - np_res = calc_numpy_unique(x_data, is_sorted) # unique, indices, inverse_indices, num_unique, counts + tvm_res = intrp.evaluate()( + x_data + ) # unique, indices, inverse_indices, num_unique, (counts) + np_res = calc_numpy_unique( + x_data, is_sorted + ) # unique, indices, inverse_indices, num_unique, counts num_unique = np_res[3][0] # num_unique assert num_unique == tvm_res[3].asnumpy()[0] diff --git a/tests/python/topi/python/test_topi_unique.py b/tests/python/topi/python/test_topi_unique.py index 26727b5ae943..f4fdd1e1de91 100644 --- a/tests/python/topi/python/test_topi_unique.py +++ b/tests/python/topi/python/test_topi_unique.py @@ -34,11 +34,19 @@ def calc_numpy_unique(data, is_sorted=False): uniq = uniq[order].astype(data.dtype) inverse = np.array([reverse_order[i] for i in inverse]).astype("int32") counts = counts[order].astype("int32") - return [uniq.astype(data.dtype), index.astype("int32"), inverse.astype("int32"), counts, num_uniq] + return [ + uniq.astype(data.dtype), + index.astype("int32"), + inverse.astype("int32"), + counts, + num_uniq, + ] def check_unique(data, is_sorted=False): # numpy reference - np_unique, np_indices, np_inverse_indices, np_counts, np_num_unique = calc_numpy_unique(data, is_sorted) + np_unique, np_indices, np_inverse_indices, np_counts, np_num_unique = calc_numpy_unique( + data, is_sorted + ) num_unique = np_num_unique[0] implementations = { @@ -87,7 +95,9 @@ def check_unique(data, is_sorted=False): tvm_unique.asnumpy()[:num_unique], np_unique, atol=1e-5, rtol=1e-5 ) - np.testing.assert_allclose(tvm_indices.asnumpy()[:num_unique], np_indices, atol=1e-5, rtol=1e-5) + np.testing.assert_allclose( + tvm_indices.asnumpy()[:num_unique], np_indices, atol=1e-5, rtol=1e-5 + ) # with counts tvm_counts = tvm.nd.array(np.zeros(data.shape).astype("int32"), device=dev) @@ -99,13 +109,17 @@ def check_unique(data, is_sorted=False): print("Trying func") func(tvm_data, tvm_unique, tvm_indices, tvm_inverse_indices, tvm_num_unique, tvm_counts) print("Succeeded") - np_unique, np_indices, np_inverse_indices, _, np_num_unique = calc_numpy_unique(data, is_sorted) # TODO this is incorrect?? + np_unique, np_indices, np_inverse_indices, _, np_num_unique = calc_numpy_unique( + data, is_sorted + ) # TODO this is incorrect?? num_unique = np_num_unique[0] assert tvm_num_unique.asnumpy()[0] == np_num_unique np.testing.assert_allclose( tvm_unique.asnumpy()[:num_unique], np_unique, atol=1e-5, rtol=1e-5 ) - np.testing.assert_allclose(tvm_indices.asnumpy()[:num_unique], np_indices, atol=1e-5, rtol=1e-5) + np.testing.assert_allclose( + tvm_indices.asnumpy()[:num_unique], np_indices, atol=1e-5, rtol=1e-5 + ) np.testing.assert_allclose( tvm_counts.asnumpy()[:num_unique], np_counts, atol=1e-5, rtol=1e-5 ) From e37a97be878fe95864a837b4ad49a2a998be2159 Mon Sep 17 00:00:00 2001 From: electriclilies Date: Thu, 20 May 2021 17:54:04 -0700 Subject: [PATCH 11/24] fix lint --- python/tvm/relay/frontend/onnx.py | 8 ++++---- python/tvm/topi/cuda/unique.py | 4 ++-- python/tvm/topi/unique.py | 8 +++++--- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/python/tvm/relay/frontend/onnx.py b/python/tvm/relay/frontend/onnx.py index e95091e1711c..69e2e791bdd6 100644 --- a/python/tvm/relay/frontend/onnx.py +++ b/python/tvm/relay/frontend/onnx.py @@ -2964,11 +2964,11 @@ def _impl_v11(cls, inputs, attr, params): data_shape = infer_shape(data) if len(data_shape) != 1: raise ValueError("TVM only supports 1D Unique operator.") - sorted = attr.get("sorted", 1) # sorted is 0 or 1, 1 by default + is_sorted = attr.get("sorted", 1) # sorted is 0 or 1, 1 by default - # ONNX documentation lists return_counts as optional but there is no input to specify whether it is returned. - # Therefore we'll just always return it. - unique = _op.unique(data, is_sorted=(sorted == 1), return_counts=True) + # ONNX documentation lists return_counts as optional but there is no input to specify + # whether it is returned. Therefore we'll just always return it. + unique = _op.unique(data, is_sorted=(is_sorted == 1), return_counts=True) num_unique = unique[3] trim_unique_lambda = lambda input: _op.strided_slice(input, _op.const([0]), num_unique) diff --git a/python/tvm/topi/cuda/unique.py b/python/tvm/topi/cuda/unique.py index c71c61e79bac..6fd2feea6c5f 100644 --- a/python/tvm/topi/cuda/unique.py +++ b/python/tvm/topi/cuda/unique.py @@ -304,8 +304,8 @@ def unique(data, is_sorted=True, return_counts=False): with the length of the input data. inverse_indices : tvm.te.Tensor - A 1-D tensor. For each entry in data, it contains the index of that data element in the unique array. - (Note that inverse_indices is very similar to indices if output is not sorted.) + A 1-D tensor. For each entry in data, it contains the index of that data element in the + unique array. (Note that inverse_indices is very similar to indices if output is not sorted.) num_unique : tvm.te.Tensor A 1-D tensor with size=1 containing the number of unique elements in the input data tensor. diff --git a/python/tvm/topi/unique.py b/python/tvm/topi/unique.py index 7d389e0e8324..49869c2ecda4 100644 --- a/python/tvm/topi/unique.py +++ b/python/tvm/topi/unique.py @@ -230,8 +230,9 @@ def unique(data, is_sorted=True, return_counts=False): with the length of the input data. inverse_indices : tvm.te.Tensor - A 1-D tensor. For each entry in data, it contains the index of that data element in the unique array. - (Note that inverse_indices is very similar to indices if output is not sorted.) + A 1-D tensor. For each entry in data, it contains the index of that data element in + the unique array. (Note that inverse_indices is very similar to indices if output is not + sorted.) num_unique : tvm.te.Tensor A 1-D tensor with size=1 containing the number of unique elements in the input data tensor. @@ -296,7 +297,8 @@ def unique(data, is_sorted=True, return_counts=False): fcompute = lambda ins, outs: _calc_unique_ir(*ins, *outs) else: fcompute = lambda ins, outs: _calc_unique_ir(*ins, *outs, None) - # First occurence is in order of sorted unique output, if we sort the first_occurence array we get the correct result + # First occurence is in order of sorted unique output, if we sort the first_occurence array + # we get the correct result indices = sort(first_occurence) outs = te.extern( From 7bc6545611c4d490ffec125d44bcd0d044236725 Mon Sep 17 00:00:00 2001 From: electriclilies Date: Fri, 21 May 2021 11:06:15 -0700 Subject: [PATCH 12/24] Some stray .asnumpy()s got through my merge, fix) --- python/tvm/contrib/sparse.py | 2 +- python/tvm/relay/frontend/mxnet.py | 6 +- python/tvm/runtime/ndarray.py | 2 +- tests/python/frontend/mxnet/test_forward.py | 124 +++++++++--------- tests/python/topi/python/test_topi_prng.py | 4 +- .../python/topi/python/test_topi_transform.py | 4 +- tests/python/topi/python/test_topi_unique.py | 10 +- tutorials/frontend/deploy_quantized.py | 2 +- tutorials/frontend/deploy_ssd_gluoncv.py | 2 +- tutorials/frontend/from_mxnet.py | 2 +- web/tests/python/webgpu_rpc_test.py | 4 +- web/tests/python/websock_rpc_test.py | 2 +- 12 files changed, 82 insertions(+), 82 deletions(-) diff --git a/python/tvm/contrib/sparse.py b/python/tvm/contrib/sparse.py index d515f58f9d2f..3e985ab4a25c 100644 --- a/python/tvm/contrib/sparse.py +++ b/python/tvm/contrib/sparse.py @@ -85,7 +85,7 @@ def asnumpy(self): """Construct a full matrix and convert it to numpy array. This API will be deprecated in TVM v0.8 release. Please use `numpy` instead.""" warnings.warn( - "CSRNDArray.asnumpy() will be deprecated in TVM v0.8 release. " + "CSRNDArray.numpy() will be deprecated in TVM v0.8 release. " "Please use CSRNDArray.numpy() instead.", DeprecationWarning, ) diff --git a/python/tvm/relay/frontend/mxnet.py b/python/tvm/relay/frontend/mxnet.py index 3b940bd15f5b..71f288e73e59 100644 --- a/python/tvm/relay/frontend/mxnet.py +++ b/python/tvm/relay/frontend/mxnet.py @@ -2894,9 +2894,9 @@ def from_mxnet(symbol, shape=None, dtype="float32", arg_params=None, aux_params= arg_params = arg_params if arg_params else {} aux_params = aux_params if aux_params else {} for k, v in arg_params.items(): - params[k] = _nd.array(v.asnumpy()) + params[k] = _nd.array(v.numpy()) for k, v in aux_params.items(): - params[k] = _nd.array(v.asnumpy()) + params[k] = _nd.array(v.numpy()) shape, dtype = _update_shape_dtype(shape, dtype, params) func = _from_mxnet_impl(symbol, shape, dtype, params, mod) elif isinstance(symbol, mx.gluon.HybridBlock): @@ -2904,7 +2904,7 @@ def from_mxnet(symbol, shape=None, dtype="float32", arg_params=None, aux_params= raise ValueError("arg_params and aux_params ae not used when importing HybridBlock") params = {} for k, v in symbol.collect_params().items(): - params[k] = _nd.array(v.data().asnumpy()) + params[k] = _nd.array(v.data().numpy()) inputs = [] for name in shape: inputs.append(mx.sym.Variable(name)) diff --git a/python/tvm/runtime/ndarray.py b/python/tvm/runtime/ndarray.py index e19221c9f186..9ba9c241d0a7 100644 --- a/python/tvm/runtime/ndarray.py +++ b/python/tvm/runtime/ndarray.py @@ -186,7 +186,7 @@ def asnumpy(self): """Convert this array to numpy array. This API will be deprecated in TVM v0.8 release. Please use `numpy` instead.""" warnings.warn( - "NDArray.asnumpy() will be deprecated in TVM v0.8 release. " + "NDArray.numpy() will be deprecated in TVM v0.8 release. " "Please use NDArray.numpy() instead.", DeprecationWarning, ) diff --git a/tests/python/frontend/mxnet/test_forward.py b/tests/python/frontend/mxnet/test_forward.py index 362a9b623d25..db4bcfe025bd 100644 --- a/tests/python/frontend/mxnet/test_forward.py +++ b/tests/python/frontend/mxnet/test_forward.py @@ -51,7 +51,7 @@ def get_gluon_output(name, x): inputs=mx.sym.var("data"), params=net.collect_params(), ) - out = net_sym(mx.nd.array(x.astype(dtype))).asnumpy() + out = net_sym(mx.nd.array(x.astype(dtype))).numpy() return out, net_sym else: @@ -64,7 +64,7 @@ def get_mxnet_output(symbol, x, dtype="float32"): mod.bind(data_shapes=[("data", x.shape)], for_training=False) mod.init_params() mod.forward(Batch([mx.nd.array(x.astype(dtype))])) - out = mod.get_outputs()[0].asnumpy() + out = mod.get_outputs()[0].numpy() args, auxs = mod.get_params() return out, args, auxs @@ -330,7 +330,7 @@ def test_forward_where(): mod.bind(data_shapes=shapes.items(), for_training=False) mod.init_params() args, auxs = mod.get_params() - mx_out = mx.nd.where(mx_cond, mx_x, mx_y).asnumpy() + mx_out = mx.nd.where(mx_cond, mx_x, mx_y).numpy() mod, _ = relay.frontend.from_mxnet(mx_sym, shapes, args, auxs) for target, dev in tvm.testing.enabled_targets(): @@ -354,7 +354,7 @@ def _mx_symbol(F, start, stop, step): return sym def verify(start, stop, step): - ref_res = _mx_symbol(mx.nd, start, stop, step).asnumpy() + ref_res = _mx_symbol(mx.nd, start, stop, step).numpy() mx_sym = _mx_symbol(mx.sym, start, stop, step) mod, _ = relay.frontend.from_mxnet(mx_sym, {}) for target, dev in tvm.testing.enabled_targets(): @@ -420,7 +420,7 @@ def test_forward_broadcast_ops(): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(a_np, b_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) @tvm.testing.uses_gpu @@ -455,7 +455,7 @@ def test_forward_elemwise_ops(): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(a_np, b_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) @tvm.testing.uses_gpu @@ -504,7 +504,7 @@ def test_forward_unary_ops(): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(a_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-5, atol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-5, atol=1e-5) @tvm.testing.uses_gpu @@ -534,7 +534,7 @@ def test_forward_scalar_ops(): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(a_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) for op in ["maximum", "minimum"]: dtype = "float32" a_shape = (3, 4, 5) @@ -548,7 +548,7 @@ def test_forward_scalar_ops(): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(a_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) @tvm.testing.uses_gpu @@ -562,7 +562,7 @@ def verify(shape, axis, begin, end): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(data_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) verify((3, 4), 0, 1, 2) verify((3, 4), 0, 1, None) @@ -587,7 +587,7 @@ def verify(x_shape, y_shape, axes): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(x_np, y_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) verify((3, 4), (2, 3), None) verify((3, 4), (2, 3), (0, 1)) @@ -621,7 +621,7 @@ def verify(shape, seq_lengths, use_seq_lengths, seq_axis): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(*in_data) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) verify((3, 4), [1, 2, 3, 1], True, 0) verify((3, 4), None, False, 0) @@ -657,7 +657,7 @@ def test_forward_logistic_regression_output(): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(data_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) @tvm.testing.uses_gpu @@ -675,7 +675,7 @@ def verify(a_shape, b_shape, transpose_b=False): intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(a_np, b_np) tvm.testing.assert_allclose( - op_res.numpy(), ref_res.asnumpy(), rtol=1e-05, atol=1e-05 + op_res.numpy(), ref_res.numpy(), rtol=1e-05, atol=1e-05 ) verify((1, 256), (256, 1)) @@ -693,7 +693,7 @@ def verify(shape): for kind in ["debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(x_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) verify((1,)) verify((3, 4, 5)) @@ -715,7 +715,7 @@ def verify(shape, axis): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(x_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) verify((1, 3, 1), None) verify((1, 3, 1), 0) @@ -735,7 +735,7 @@ def verify(shape, axis, size): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(x_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) verify((1, 2, 1), 2, 3) verify((1, 2, 1), (0, 2), (2, 3)) @@ -752,7 +752,7 @@ def verify(input_shape, shape): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(x_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) verify((1, 2, 3), (3, 2, 3)) verify((4, 1, 32, 32), (4, 8, 32, 32)) @@ -770,7 +770,7 @@ def verify(input_shape, like_shape): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(x_np, y_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) verify((1, 2, 3), (3, 2, 3)) verify((4, 1, 32, 32), (4, 8, 32, 32)) @@ -789,7 +789,7 @@ def test_forward_logical_not(): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(a_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) @tvm.testing.uses_gpu @@ -805,7 +805,7 @@ def verify(val, shape, dtype): for kind in ["debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()() - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) verify(2, (3, 4), "float32") verify(2, (3, 4), "int32") @@ -829,7 +829,7 @@ def verify(data_shape, weight_shape): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(x=x_np, w=w_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) verify((2, 2), (4, 5)) verify((2, 3, 4), (4, 5)) @@ -856,7 +856,7 @@ def verify(shape, indices_src, axis, mode="clip"): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(x_np, indices_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) verify((2, 2), [[[1, 0], [0, 1]]], 0) verify((2, 2), [[[1, 0], [0, 1]]], 1) @@ -880,7 +880,7 @@ def verify(xshape, yshape, y_data, error=False): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(x_data, y_data) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) verify((2, 2), (2, 3), [[1, 1, 0], [0, 1, 0]]) verify((2, 2, 2), (2, 2), [[0, 1], [1, 0]]) @@ -909,7 +909,7 @@ def verify(shape, transform_type, target_shape): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(x) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-5, atol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-5, atol=1e-5) verify((4, 6), "affine", (16, 32)) verify((4, 2, 16, 16), "warp", None) @@ -929,7 +929,7 @@ def verify(data_shape, grid_shape): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(data, grid) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-5, atol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-5, atol=1e-5) verify((4, 4, 16, 32), (4, 2, 8, 8)) verify((4, 4, 16, 32), (4, 2, 32, 32)) @@ -995,9 +995,9 @@ def verify( if init_states: assert len(op_res) == len(mx_res) for i, val in enumerate(op_res): - tvm.testing.assert_allclose(val.numpy(), mx_res[i].asnumpy(), rtol=1e-3) + tvm.testing.assert_allclose(val.numpy(), mx_res[i].numpy(), rtol=1e-3) else: - tvm.testing.assert_allclose(op_res.numpy(), mx_res.asnumpy(), rtol=1e-3) + tvm.testing.assert_allclose(op_res.numpy(), mx_res.numpy(), rtol=1e-3) for mode in ["rnn", "gru", "lstm"]: verify(mode, 1, 64, 64, 1) @@ -1029,7 +1029,7 @@ def verify(xshape, yshape, offset=None): op_res = intrp.evaluate()(x_data, y_data) else: op_res = intrp.evaluate()(x_data) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) verify((1, 3, 40, 40), (1, 3, 20, 20)) verify((1, 3, 40, 40), (1, 3, 20, 20), (0, 0)) @@ -1049,7 +1049,7 @@ def verify(shape, axis, is_ascend, dtype="float32"): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(x_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) verify((2, 3, 4), axis=0, is_ascend=False) verify((1, 4, 6), axis=1, is_ascend=True) @@ -1083,9 +1083,9 @@ def verify(shape, k, axis, ret_type, is_ascend=None, dtype="float32"): if isinstance(ref_res, list): assert len(op_res) == len(ref_res) for i, t in enumerate(op_res): - tvm.testing.assert_allclose(t.numpy(), ref_res[i].asnumpy()) + tvm.testing.assert_allclose(t.numpy(), ref_res[i].numpy()) else: - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) verify((3, 4), k=1, axis=0, ret_type="both") verify((3, 4), k=1, axis=-1, ret_type="indices") @@ -1140,7 +1140,7 @@ def verify(shape, use_sequence_length, value, axis, dtype, itype): op_res = intrp.evaluate()(data_np, valid_length_np) else: op_res = intrp.evaluate()(data_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) verify((5, 10), True, 0.0, 0, "float32", "float32") verify((5, 4, 3), True, 1.0, 1, "float32", "float32") @@ -1159,7 +1159,7 @@ def verify(shape): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(x_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) verify((3, 4)) verify((3, 4, 5)) @@ -1207,7 +1207,7 @@ def verify(shape, axis=1, fix_gamma=False): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(x, gamma, beta, moving_mean, moving_var) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-3) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-3) verify((2, 3, 4, 5)) verify((2, 3, 4, 5), axis=0) @@ -1231,7 +1231,7 @@ def verify(shape, axis=1, epsilon=1e-5): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(x, gamma, beta) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-5, atol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-5, atol=1e-5) verify((2, 3, 4, 5)) verify((32, 64, 80, 64)) @@ -1255,7 +1255,7 @@ def verify(shape, axis=-1): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(x, gamma, beta) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-3, atol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-3, atol=1e-5) verify((2, 5)) verify((2, 5), axis=0) @@ -1283,7 +1283,7 @@ def verify(shape, num_groups=1): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(x, gamma, beta) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-3, atol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-3, atol=1e-5) verify((1, 4, 2), num_groups=4) # TODO(trevmorr): MXNet GroupNorm implementation is bugged for cases when num_groups != num_channels @@ -1304,7 +1304,7 @@ def verify(indices_shape, depth, on_value, off_value, dtype): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(x.astype("float32")) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-3, atol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-3, atol=1e-5) verify((3,), 3, 1, 0, "int32") verify((3,), 3, 1.0, 0.0, "float32") @@ -1430,7 +1430,7 @@ def verify(data_shape, kernel_size, stride, pad, num_filter, is_depthwise=False) for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(x, weight, bias) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-3) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-3) verify(data_shape=(1, 1, 1024 * 16), kernel_size=(17,), stride=(2,), pad=(8,), num_filter=4) verify(data_shape=(20, 1, 1024 * 16), kernel_size=(17,), stride=(2,), pad=(8,), num_filter=4) @@ -1511,7 +1511,7 @@ def verify(data_shape, kernel_size, stride, pad, num_filter): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(x, weight, bias) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-3, atol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-3, atol=1e-5) verify(data_shape=(1, 1, 1024 * 16), kernel_size=(17,), stride=(2,), pad=(8,), num_filter=4) verify(data_shape=(20, 1, 1024 * 16), kernel_size=(17,), stride=(2,), pad=(8,), num_filter=4) @@ -1544,7 +1544,7 @@ def verify(a_np, b_np): for kind in ["debug", "vm"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(a_np, b_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-3) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-3) verify(np.asarray([1.0], "float32"), np.asarray([2.0], "float32")) verify(np.asarray([4.0], "float32"), np.asarray([3.0], "float32")) @@ -1611,7 +1611,7 @@ def verify(x, shape, dtype): for kind in ["graph", "vm", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(a_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) for dtype in ["int32", "int64"]: verify([0, 1, 2, 3], [2, 2], dtype) @@ -1652,7 +1652,7 @@ def verify(shape, blocksize=2): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(x) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-3, atol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-3, atol=1e-5) verify((1, 18, 3, 3), 3) @@ -1671,7 +1671,7 @@ def verify(shape, blocksize=2): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(x) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-3, atol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-3, atol=1e-5) verify((1, 1, 9, 9), 3) @@ -1707,7 +1707,7 @@ def verify(data_shape, kernel_size, max_displacement, stride1, stride2, pad_size for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(data1, data2) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-3, atol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-3, atol=1e-5) verify( (1, 3, 10, 10), @@ -1812,7 +1812,7 @@ def verify(data_shape, start=None, step=None, axis=None): for kind in ["graph"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()() - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) verify(data_shape=(3,), start=0.0, step=1.0) verify(data_shape=(3, 4, 5), start=0.0, step=1.0) @@ -1834,7 +1834,7 @@ def verify(batch, seq_length, num_heads, head_dim): for kind in ["graph"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(data_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-5) verify(1, 10, 3, 16) verify(3, 10, 6, 8) @@ -1859,7 +1859,7 @@ def verify(batch, seq_length, num_heads, head_dim): for kind in ["graph"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(data=data_np, weight=weight_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-5) verify(1, 10, 4, 16) verify(3, 10, 6, 8) @@ -1916,7 +1916,7 @@ def verify( for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(data) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-3, atol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-3, atol=1e-5) verify((1, 10, 6)) # No valid boxes @@ -1955,7 +1955,7 @@ def verify(data_shape, anchor_shape, stds=[1, 1, 1, 1], clip=-1, in_format="corn for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(data, anchors) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-3, atol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-3, atol=1e-5) verify((1, 10, 4), (1, 10, 4)) verify((4, 10, 4), (1, 10, 4)) @@ -1999,7 +1999,7 @@ def verify(data_shape, axis, use_length, length): else: op_res = intrp.evaluate()(x) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-3, atol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-3, atol=1e-5) verify((2, 3, 5), -1, False, None) verify((2, 3, 5), 2, False, None) @@ -2054,7 +2054,7 @@ def test_forward_npi_transpose(data_shape, axes, dtype, target, dev, kind): mod, _ = relay.frontend.from_mxnet(mx_sym, {"data": data_shape}, dtype=dtype) intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(data_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-5) @pytest.mark.parametrize( @@ -2082,7 +2082,7 @@ def test_forward_npi_concatenate(data_shape1, data_shape2, axis, dtype, target, ) intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(data_np1, data_np2) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-5) @pytest.mark.parametrize( @@ -2110,7 +2110,7 @@ def test_forward_npi_stack(data_shape1, data_shape2, axis, dtype, target, dev, k ) intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(data_np1, data_np2) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-5) @pytest.mark.parametrize("data_shape", [(2, 2, 2), (2, 7, 2), (2, 2, 2, 1, 2, 3, 1), (1, 8)]) @@ -2125,7 +2125,7 @@ def test_forward_np_copy(data_shape, dtype, target, dev, kind): mod, _ = relay.frontend.from_mxnet(mx_sym, {"data": data_shape}, dtype=dtype) intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(data_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-5) @pytest.mark.parametrize("dtype", ["float64", "float32", "int64", "int32", "bool"]) @@ -2153,7 +2153,7 @@ def test_forward_npx_reshape(data_shape, out_shape, dtype, target, reverse, dev, mod, _ = relay.frontend.from_mxnet(mx_sym, {"data": data_shape}, dtype=dtype) intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(data_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-5) @pytest.mark.parametrize( @@ -2188,7 +2188,7 @@ def test_forward_npi_binary(data_shape, dtype, target, dev, kind): ) intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(data_np1, data_np2) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-5) @pytest.mark.parametrize( @@ -2220,7 +2220,7 @@ def test_forward_npi_binary_scalar(data_shape, dtype, scalar, target, dev, kind) mod, _ = relay.frontend.from_mxnet(mx_sym, shape={"lhs": data_shape}, dtype=dtype) intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(data_np1) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-5) @pytest.mark.parametrize( @@ -2237,7 +2237,7 @@ def test_forward_npi_tanh(data_shape, dtype, target, dev, kind): mod, _ = relay.frontend.from_mxnet(mx_sym, shape={"data": data_shape}, dtype=dtype) intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(data_np1) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-5) @pytest.mark.skipif(not hasattr(mx.np, "where"), reason="mx.np.where hasn't been publish yet") @@ -2269,7 +2269,7 @@ def test_forward_npi_where_rscalar( ) intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(cond_np, data_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-5) @pytest.mark.parametrize("dtype", ["float64", "float32", "int64", "int32", "bool"]) @@ -2303,7 +2303,7 @@ def test_forward_split_v2( op_res_.append(arr.numpy().tolist()) ref_res_ = [] for arr in ref_res: - ref_res_.append(arr.asnumpy().tolist()) + ref_res_.append(arr.numpy().tolist()) tvm.testing.assert_allclose(op_res_, ref_res_, rtol=1e-5) diff --git a/tests/python/topi/python/test_topi_prng.py b/tests/python/topi/python/test_topi_prng.py index 1be32e6ea1d1..63b8be259ada 100644 --- a/tests/python/topi/python/test_topi_prng.py +++ b/tests/python/topi/python/test_topi_prng.py @@ -57,7 +57,7 @@ def uniform(target, dev, gen, low, high, size, dtype): out_gen = tvm.nd.array(np.zeros(gen.shape, dtype="uint64")) rands = tvm.nd.array(np.zeros(size, dtype=dtype)) f(tvm.nd.array(gen), tvm.nd.array(low), tvm.nd.array(high), out_gen, rands) - return out_gen.asnumpy(), rands.asnumpy() + return out_gen.numpy(), rands.numpy() @tvm.testing.parametrize_targets @@ -137,7 +137,7 @@ def test_threefry_wrapping(target, dev): @tvm.testing.parametrize_targets def test_uniform(target, dev): - gen = tvm.relay.random.threefry_key(0).data.asnumpy() + gen = tvm.relay.random.threefry_key(0).data.numpy() m = 1024 n = 1024 dtypes = ["float32", "float64"] diff --git a/tests/python/topi/python/test_topi_transform.py b/tests/python/topi/python/test_topi_transform.py index 20172f07fd9e..f7f5474769b4 100644 --- a/tests/python/topi/python/test_topi_transform.py +++ b/tests/python/topi/python/test_topi_transform.py @@ -880,7 +880,7 @@ def test_transpose_unfused_schedule(target, dev): kind="graph", mod=tvm.IRModule.from_expr(relay.Function([x], f)), device=dev, target=target ) r = np.random.rand(*shape) - tvm.testing.assert_allclose(ex.evaluate()(r).asnumpy(), np.transpose(r)) + tvm.testing.assert_allclose(ex.evaluate()(r).numpy(), np.transpose(r)) # We want to make sure schedule does not fire here, but there is no way of # inspecting which schedules were used. @@ -893,7 +893,7 @@ def test_transpose_unfused_schedule(target, dev): device=dev, target=target, ) - tvm.testing.assert_allclose(ex.evaluate()(r, r).asnumpy(), np.transpose(r + r)) + tvm.testing.assert_allclose(ex.evaluate()(r, r).numpy(), np.transpose(r + r)) @tvm.testing.uses_gpu diff --git a/tests/python/topi/python/test_topi_unique.py b/tests/python/topi/python/test_topi_unique.py index 74e867a12e9a..1252aaf0b781 100644 --- a/tests/python/topi/python/test_topi_unique.py +++ b/tests/python/topi/python/test_topi_unique.py @@ -83,22 +83,22 @@ def check_unique(data, is_sorted=False, with_counts=False): func(tvm_data, tvm_unique, tvm_indices, tvm_inverse_indices, tvm_num_unique) num_unique = np_num_unique[0] - assert tvm_num_unique.asnumpy()[0] == np_num_unique + assert tvm_num_unique.numpy()[0] == np_num_unique np.testing.assert_allclose( - tvm_unique.asnumpy()[:num_unique], np_unique, atol=1e-5, rtol=1e-5 + tvm_unique.numpy()[:num_unique], np_unique, atol=1e-5, rtol=1e-5 ) np.testing.assert_allclose( - tvm_indices.asnumpy()[:num_unique], np_indices, atol=1e-5, rtol=1e-5 + tvm_indices.numpy()[:num_unique], np_indices, atol=1e-5, rtol=1e-5 ) np.testing.assert_allclose( - tvm_inverse_indices.asnumpy()[:num_unique], np_inverse_indices, atol=1e-5, rtol=1e-5 + tvm_inverse_indices.numpy()[:num_unique], np_inverse_indices, atol=1e-5, rtol=1e-5 ) if with_counts: np.testing.assert_allclose( - tvm_counts.asnumpy()[:num_unique], np_counts, atol=1e-5, rtol=1e-5 + tvm_counts.numpy()[:num_unique], np_counts, atol=1e-5, rtol=1e-5 ) for in_dtype in ["int32", "int64"]: diff --git a/tutorials/frontend/deploy_quantized.py b/tutorials/frontend/deploy_quantized.py index b2210b8ab69b..fc7ce6dc44f0 100644 --- a/tutorials/frontend/deploy_quantized.py +++ b/tutorials/frontend/deploy_quantized.py @@ -57,7 +57,7 @@ def get_val_data(num_workers=4): std_rgb = [58.393, 57.12, 57.375] def batch_fn(batch): - return batch.data[0].asnumpy(), batch.label[0].asnumpy() + return batch.data[0].numpy(), batch.label[0].numpy() img_size = 299 if model_name == "inceptionv3" else 224 val_data = mx.io.ImageRecordIter( diff --git a/tutorials/frontend/deploy_ssd_gluoncv.py b/tutorials/frontend/deploy_ssd_gluoncv.py index ebe18670c6a3..7e54c4cf212c 100644 --- a/tutorials/frontend/deploy_ssd_gluoncv.py +++ b/tutorials/frontend/deploy_ssd_gluoncv.py @@ -103,7 +103,7 @@ def build(target): def run(lib, dev): # Build TVM runtime m = graph_executor.GraphModule(lib["default"](dev)) - tvm_input = tvm.nd.array(x.asnumpy(), device=dev) + tvm_input = tvm.nd.array(x.numpy(), device=dev) m.set_input("data", tvm_input) # execute m.run() diff --git a/tutorials/frontend/from_mxnet.py b/tutorials/frontend/from_mxnet.py index 0ce610a2cdd6..8a69ea29a20a 100644 --- a/tutorials/frontend/from_mxnet.py +++ b/tutorials/frontend/from_mxnet.py @@ -129,7 +129,7 @@ def block2symbol(block): args = {} auxs = {} for k, v in block.collect_params().items(): - args[k] = mx.nd.array(v.data().asnumpy()) + args[k] = mx.nd.array(v.data().numpy()) return sym, args, auxs diff --git a/web/tests/python/webgpu_rpc_test.py b/web/tests/python/webgpu_rpc_test.py index e26937038fa2..c326f9fc1a16 100644 --- a/web/tests/python/webgpu_rpc_test.py +++ b/web/tests/python/webgpu_rpc_test.py @@ -70,11 +70,11 @@ def check(remote): a = tvm.nd.array(adata, dev) b = tvm.nd.array(np.zeros(n, dtype=A.dtype), dev) - np.testing.assert_equal(a.asnumpy(), adata) + np.testing.assert_equal(a.numpy(), adata) f1 = remote.system_lib() addone = f1.get_function("addone") addone(a, b) - np.testing.assert_equal(b.asnumpy(), a.asnumpy() + 1) + np.testing.assert_equal(b.numpy(), a.numpy() + 1) print("Test pass..") check(remote) diff --git a/web/tests/python/websock_rpc_test.py b/web/tests/python/websock_rpc_test.py index 84a0c9f134d5..ee94e40a678c 100644 --- a/web/tests/python/websock_rpc_test.py +++ b/web/tests/python/websock_rpc_test.py @@ -82,7 +82,7 @@ def check(remote): time_f(a, b) cost = time_f(a, b).mean print("%g secs/op" % cost) - np.testing.assert_equal(b.asnumpy(), a.asnumpy() + 1) + np.testing.assert_equal(b.numpy(), a.numpy() + 1) check(remote) From 72a42d85ab8270787db1982be114cc1d87c4f383 Mon Sep 17 00:00:00 2001 From: electriclilies Date: Fri, 21 May 2021 11:11:33 -0700 Subject: [PATCH 13/24] fix lint --- python/tvm/relay/op/transform.py | 3 ++- python/tvm/topi/cuda/unique.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/python/tvm/relay/op/transform.py b/python/tvm/relay/op/transform.py index 43a704fcf0cd..7a4f608f3826 100644 --- a/python/tvm/relay/op/transform.py +++ b/python/tvm/relay/op/transform.py @@ -1669,7 +1669,8 @@ def unique(data, is_sorted=True, return_counts=False): A 1-D tensor containing the index of each data element in the output tensor. inverse_indices : relay.Expr - A 1-D tensor. For each entry in data, it contains the index of that data element in the unique array. + A 1-D tensor. For each entry in data, it contains the index of that data element in the + unique array. num_unique : relay.Expr A 1-D tensor with size=1 containing the number of unique elements in the input data tensor. diff --git a/python/tvm/topi/cuda/unique.py b/python/tvm/topi/cuda/unique.py index 6fd2feea6c5f..911ee71a0057 100644 --- a/python/tvm/topi/cuda/unique.py +++ b/python/tvm/topi/cuda/unique.py @@ -305,7 +305,8 @@ def unique(data, is_sorted=True, return_counts=False): inverse_indices : tvm.te.Tensor A 1-D tensor. For each entry in data, it contains the index of that data element in the - unique array. (Note that inverse_indices is very similar to indices if output is not sorted.) + unique array. (Note that inverse_indices is very similar to indices if output is not + sorted) num_unique : tvm.te.Tensor A 1-D tensor with size=1 containing the number of unique elements in the input data tensor. From d6a013aaa93fd99a64e17c4f25683666523b7b26 Mon Sep 17 00:00:00 2001 From: electriclilies Date: Fri, 21 May 2021 12:16:38 -0700 Subject: [PATCH 14/24] revert changed .numpys --- python/tvm/contrib/sparse.py | 2 +- python/tvm/relay/frontend/mxnet.py | 6 +- python/tvm/runtime/ndarray.py | 2 +- tests/python/frontend/mxnet/test_forward.py | 124 ++++++++++---------- tests/python/topi/python/test_topi_prng.py | 4 +- tutorials/frontend/deploy_quantized.py | 2 +- tutorials/frontend/deploy_ssd_gluoncv.py | 2 +- tutorials/frontend/from_mxnet.py | 2 +- web/tests/python/webgpu_rpc_test.py | 4 +- web/tests/python/websock_rpc_test.py | 2 +- 10 files changed, 75 insertions(+), 75 deletions(-) diff --git a/python/tvm/contrib/sparse.py b/python/tvm/contrib/sparse.py index 3e985ab4a25c..d515f58f9d2f 100644 --- a/python/tvm/contrib/sparse.py +++ b/python/tvm/contrib/sparse.py @@ -85,7 +85,7 @@ def asnumpy(self): """Construct a full matrix and convert it to numpy array. This API will be deprecated in TVM v0.8 release. Please use `numpy` instead.""" warnings.warn( - "CSRNDArray.numpy() will be deprecated in TVM v0.8 release. " + "CSRNDArray.asnumpy() will be deprecated in TVM v0.8 release. " "Please use CSRNDArray.numpy() instead.", DeprecationWarning, ) diff --git a/python/tvm/relay/frontend/mxnet.py b/python/tvm/relay/frontend/mxnet.py index 71f288e73e59..3b940bd15f5b 100644 --- a/python/tvm/relay/frontend/mxnet.py +++ b/python/tvm/relay/frontend/mxnet.py @@ -2894,9 +2894,9 @@ def from_mxnet(symbol, shape=None, dtype="float32", arg_params=None, aux_params= arg_params = arg_params if arg_params else {} aux_params = aux_params if aux_params else {} for k, v in arg_params.items(): - params[k] = _nd.array(v.numpy()) + params[k] = _nd.array(v.asnumpy()) for k, v in aux_params.items(): - params[k] = _nd.array(v.numpy()) + params[k] = _nd.array(v.asnumpy()) shape, dtype = _update_shape_dtype(shape, dtype, params) func = _from_mxnet_impl(symbol, shape, dtype, params, mod) elif isinstance(symbol, mx.gluon.HybridBlock): @@ -2904,7 +2904,7 @@ def from_mxnet(symbol, shape=None, dtype="float32", arg_params=None, aux_params= raise ValueError("arg_params and aux_params ae not used when importing HybridBlock") params = {} for k, v in symbol.collect_params().items(): - params[k] = _nd.array(v.data().numpy()) + params[k] = _nd.array(v.data().asnumpy()) inputs = [] for name in shape: inputs.append(mx.sym.Variable(name)) diff --git a/python/tvm/runtime/ndarray.py b/python/tvm/runtime/ndarray.py index 9ba9c241d0a7..e19221c9f186 100644 --- a/python/tvm/runtime/ndarray.py +++ b/python/tvm/runtime/ndarray.py @@ -186,7 +186,7 @@ def asnumpy(self): """Convert this array to numpy array. This API will be deprecated in TVM v0.8 release. Please use `numpy` instead.""" warnings.warn( - "NDArray.numpy() will be deprecated in TVM v0.8 release. " + "NDArray.asnumpy() will be deprecated in TVM v0.8 release. " "Please use NDArray.numpy() instead.", DeprecationWarning, ) diff --git a/tests/python/frontend/mxnet/test_forward.py b/tests/python/frontend/mxnet/test_forward.py index db4bcfe025bd..1cb237b1074d 100644 --- a/tests/python/frontend/mxnet/test_forward.py +++ b/tests/python/frontend/mxnet/test_forward.py @@ -51,7 +51,7 @@ def get_gluon_output(name, x): inputs=mx.sym.var("data"), params=net.collect_params(), ) - out = net_sym(mx.nd.array(x.astype(dtype))).numpy() + out = net_sym(mx.nd.array(x.astype(dtype))).asnumpy() return out, net_sym else: @@ -64,7 +64,7 @@ def get_mxnet_output(symbol, x, dtype="float32"): mod.bind(data_shapes=[("data", x.shape)], for_training=False) mod.init_params() mod.forward(Batch([mx.nd.array(x.astype(dtype))])) - out = mod.get_outputs()[0].numpy() + out = mod.get_outputs()[0].asnumpy() args, auxs = mod.get_params() return out, args, auxs @@ -330,7 +330,7 @@ def test_forward_where(): mod.bind(data_shapes=shapes.items(), for_training=False) mod.init_params() args, auxs = mod.get_params() - mx_out = mx.nd.where(mx_cond, mx_x, mx_y).numpy() + mx_out = mx.nd.where(mx_cond, mx_x, mx_y).asnumpy() mod, _ = relay.frontend.from_mxnet(mx_sym, shapes, args, auxs) for target, dev in tvm.testing.enabled_targets(): @@ -361,7 +361,7 @@ def verify(start, stop, step): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()() - tvm.testing.assert_allclose(op_res.numpy(), ref_res) + tvm.testing.assert_allclose(op_res.asnumpy(), ref_res) verify(0, 20, None) verify(0, 20, 2) @@ -420,7 +420,7 @@ def test_forward_broadcast_ops(): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(a_np, b_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) @tvm.testing.uses_gpu @@ -455,7 +455,7 @@ def test_forward_elemwise_ops(): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(a_np, b_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) @tvm.testing.uses_gpu @@ -504,7 +504,7 @@ def test_forward_unary_ops(): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(a_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-5, atol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-5, atol=1e-5) @tvm.testing.uses_gpu @@ -534,7 +534,7 @@ def test_forward_scalar_ops(): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(a_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) for op in ["maximum", "minimum"]: dtype = "float32" a_shape = (3, 4, 5) @@ -548,7 +548,7 @@ def test_forward_scalar_ops(): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(a_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) @tvm.testing.uses_gpu @@ -562,7 +562,7 @@ def verify(shape, axis, begin, end): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(data_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) verify((3, 4), 0, 1, 2) verify((3, 4), 0, 1, None) @@ -587,7 +587,7 @@ def verify(x_shape, y_shape, axes): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(x_np, y_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) verify((3, 4), (2, 3), None) verify((3, 4), (2, 3), (0, 1)) @@ -621,7 +621,7 @@ def verify(shape, seq_lengths, use_seq_lengths, seq_axis): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(*in_data) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) verify((3, 4), [1, 2, 3, 1], True, 0) verify((3, 4), None, False, 0) @@ -657,7 +657,7 @@ def test_forward_logistic_regression_output(): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(data_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) @tvm.testing.uses_gpu @@ -675,7 +675,7 @@ def verify(a_shape, b_shape, transpose_b=False): intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(a_np, b_np) tvm.testing.assert_allclose( - op_res.numpy(), ref_res.numpy(), rtol=1e-05, atol=1e-05 + op_res.numpy(), ref_res.asnumpy(), rtol=1e-05, atol=1e-05 ) verify((1, 256), (256, 1)) @@ -693,7 +693,7 @@ def verify(shape): for kind in ["debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(x_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) verify((1,)) verify((3, 4, 5)) @@ -715,7 +715,7 @@ def verify(shape, axis): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(x_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) verify((1, 3, 1), None) verify((1, 3, 1), 0) @@ -735,7 +735,7 @@ def verify(shape, axis, size): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(x_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) verify((1, 2, 1), 2, 3) verify((1, 2, 1), (0, 2), (2, 3)) @@ -752,7 +752,7 @@ def verify(input_shape, shape): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(x_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) verify((1, 2, 3), (3, 2, 3)) verify((4, 1, 32, 32), (4, 8, 32, 32)) @@ -770,7 +770,7 @@ def verify(input_shape, like_shape): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(x_np, y_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) verify((1, 2, 3), (3, 2, 3)) verify((4, 1, 32, 32), (4, 8, 32, 32)) @@ -789,7 +789,7 @@ def test_forward_logical_not(): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(a_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) @tvm.testing.uses_gpu @@ -805,7 +805,7 @@ def verify(val, shape, dtype): for kind in ["debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()() - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) verify(2, (3, 4), "float32") verify(2, (3, 4), "int32") @@ -829,7 +829,7 @@ def verify(data_shape, weight_shape): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(x=x_np, w=w_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) verify((2, 2), (4, 5)) verify((2, 3, 4), (4, 5)) @@ -856,7 +856,7 @@ def verify(shape, indices_src, axis, mode="clip"): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(x_np, indices_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) verify((2, 2), [[[1, 0], [0, 1]]], 0) verify((2, 2), [[[1, 0], [0, 1]]], 1) @@ -880,7 +880,7 @@ def verify(xshape, yshape, y_data, error=False): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(x_data, y_data) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) verify((2, 2), (2, 3), [[1, 1, 0], [0, 1, 0]]) verify((2, 2, 2), (2, 2), [[0, 1], [1, 0]]) @@ -909,7 +909,7 @@ def verify(shape, transform_type, target_shape): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(x) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-5, atol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-5, atol=1e-5) verify((4, 6), "affine", (16, 32)) verify((4, 2, 16, 16), "warp", None) @@ -929,7 +929,7 @@ def verify(data_shape, grid_shape): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(data, grid) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-5, atol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-5, atol=1e-5) verify((4, 4, 16, 32), (4, 2, 8, 8)) verify((4, 4, 16, 32), (4, 2, 32, 32)) @@ -995,9 +995,9 @@ def verify( if init_states: assert len(op_res) == len(mx_res) for i, val in enumerate(op_res): - tvm.testing.assert_allclose(val.numpy(), mx_res[i].numpy(), rtol=1e-3) + tvm.testing.assert_allclose(val.numpy(), mx_res[i].asnumpy(), rtol=1e-3) else: - tvm.testing.assert_allclose(op_res.numpy(), mx_res.numpy(), rtol=1e-3) + tvm.testing.assert_allclose(op_res.numpy(), mx_res.asnumpy(), rtol=1e-3) for mode in ["rnn", "gru", "lstm"]: verify(mode, 1, 64, 64, 1) @@ -1029,7 +1029,7 @@ def verify(xshape, yshape, offset=None): op_res = intrp.evaluate()(x_data, y_data) else: op_res = intrp.evaluate()(x_data) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) verify((1, 3, 40, 40), (1, 3, 20, 20)) verify((1, 3, 40, 40), (1, 3, 20, 20), (0, 0)) @@ -1049,7 +1049,7 @@ def verify(shape, axis, is_ascend, dtype="float32"): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(x_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) verify((2, 3, 4), axis=0, is_ascend=False) verify((1, 4, 6), axis=1, is_ascend=True) @@ -1083,9 +1083,9 @@ def verify(shape, k, axis, ret_type, is_ascend=None, dtype="float32"): if isinstance(ref_res, list): assert len(op_res) == len(ref_res) for i, t in enumerate(op_res): - tvm.testing.assert_allclose(t.numpy(), ref_res[i].numpy()) + tvm.testing.assert_allclose(t.numpy(), ref_res[i].asnumpy()) else: - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) verify((3, 4), k=1, axis=0, ret_type="both") verify((3, 4), k=1, axis=-1, ret_type="indices") @@ -1140,7 +1140,7 @@ def verify(shape, use_sequence_length, value, axis, dtype, itype): op_res = intrp.evaluate()(data_np, valid_length_np) else: op_res = intrp.evaluate()(data_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) verify((5, 10), True, 0.0, 0, "float32", "float32") verify((5, 4, 3), True, 1.0, 1, "float32", "float32") @@ -1159,7 +1159,7 @@ def verify(shape): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(x_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) verify((3, 4)) verify((3, 4, 5)) @@ -1207,7 +1207,7 @@ def verify(shape, axis=1, fix_gamma=False): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(x, gamma, beta, moving_mean, moving_var) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-3) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-3) verify((2, 3, 4, 5)) verify((2, 3, 4, 5), axis=0) @@ -1231,7 +1231,7 @@ def verify(shape, axis=1, epsilon=1e-5): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(x, gamma, beta) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-5, atol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-5, atol=1e-5) verify((2, 3, 4, 5)) verify((32, 64, 80, 64)) @@ -1255,7 +1255,7 @@ def verify(shape, axis=-1): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(x, gamma, beta) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-3, atol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-3, atol=1e-5) verify((2, 5)) verify((2, 5), axis=0) @@ -1283,7 +1283,7 @@ def verify(shape, num_groups=1): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(x, gamma, beta) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-3, atol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-3, atol=1e-5) verify((1, 4, 2), num_groups=4) # TODO(trevmorr): MXNet GroupNorm implementation is bugged for cases when num_groups != num_channels @@ -1304,7 +1304,7 @@ def verify(indices_shape, depth, on_value, off_value, dtype): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(x.astype("float32")) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-3, atol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-3, atol=1e-5) verify((3,), 3, 1, 0, "int32") verify((3,), 3, 1.0, 0.0, "float32") @@ -1430,7 +1430,7 @@ def verify(data_shape, kernel_size, stride, pad, num_filter, is_depthwise=False) for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(x, weight, bias) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-3) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-3) verify(data_shape=(1, 1, 1024 * 16), kernel_size=(17,), stride=(2,), pad=(8,), num_filter=4) verify(data_shape=(20, 1, 1024 * 16), kernel_size=(17,), stride=(2,), pad=(8,), num_filter=4) @@ -1511,7 +1511,7 @@ def verify(data_shape, kernel_size, stride, pad, num_filter): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(x, weight, bias) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-3, atol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-3, atol=1e-5) verify(data_shape=(1, 1, 1024 * 16), kernel_size=(17,), stride=(2,), pad=(8,), num_filter=4) verify(data_shape=(20, 1, 1024 * 16), kernel_size=(17,), stride=(2,), pad=(8,), num_filter=4) @@ -1544,7 +1544,7 @@ def verify(a_np, b_np): for kind in ["debug", "vm"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(a_np, b_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-3) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-3) verify(np.asarray([1.0], "float32"), np.asarray([2.0], "float32")) verify(np.asarray([4.0], "float32"), np.asarray([3.0], "float32")) @@ -1611,7 +1611,7 @@ def verify(x, shape, dtype): for kind in ["graph", "vm", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(a_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) for dtype in ["int32", "int64"]: verify([0, 1, 2, 3], [2, 2], dtype) @@ -1652,7 +1652,7 @@ def verify(shape, blocksize=2): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(x) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-3, atol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-3, atol=1e-5) verify((1, 18, 3, 3), 3) @@ -1671,7 +1671,7 @@ def verify(shape, blocksize=2): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(x) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-3, atol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-3, atol=1e-5) verify((1, 1, 9, 9), 3) @@ -1707,7 +1707,7 @@ def verify(data_shape, kernel_size, max_displacement, stride1, stride2, pad_size for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(data1, data2) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-3, atol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-3, atol=1e-5) verify( (1, 3, 10, 10), @@ -1812,7 +1812,7 @@ def verify(data_shape, start=None, step=None, axis=None): for kind in ["graph"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()() - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy()) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy()) verify(data_shape=(3,), start=0.0, step=1.0) verify(data_shape=(3, 4, 5), start=0.0, step=1.0) @@ -1834,7 +1834,7 @@ def verify(batch, seq_length, num_heads, head_dim): for kind in ["graph"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(data_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-5) verify(1, 10, 3, 16) verify(3, 10, 6, 8) @@ -1859,7 +1859,7 @@ def verify(batch, seq_length, num_heads, head_dim): for kind in ["graph"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(data=data_np, weight=weight_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-5) verify(1, 10, 4, 16) verify(3, 10, 6, 8) @@ -1916,7 +1916,7 @@ def verify( for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(data) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-3, atol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-3, atol=1e-5) verify((1, 10, 6)) # No valid boxes @@ -1955,7 +1955,7 @@ def verify(data_shape, anchor_shape, stds=[1, 1, 1, 1], clip=-1, in_format="corn for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(data, anchors) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-3, atol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-3, atol=1e-5) verify((1, 10, 4), (1, 10, 4)) verify((4, 10, 4), (1, 10, 4)) @@ -1999,7 +1999,7 @@ def verify(data_shape, axis, use_length, length): else: op_res = intrp.evaluate()(x) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-3, atol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-3, atol=1e-5) verify((2, 3, 5), -1, False, None) verify((2, 3, 5), 2, False, None) @@ -2054,7 +2054,7 @@ def test_forward_npi_transpose(data_shape, axes, dtype, target, dev, kind): mod, _ = relay.frontend.from_mxnet(mx_sym, {"data": data_shape}, dtype=dtype) intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(data_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-5) @pytest.mark.parametrize( @@ -2082,7 +2082,7 @@ def test_forward_npi_concatenate(data_shape1, data_shape2, axis, dtype, target, ) intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(data_np1, data_np2) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-5) @pytest.mark.parametrize( @@ -2110,7 +2110,7 @@ def test_forward_npi_stack(data_shape1, data_shape2, axis, dtype, target, dev, k ) intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(data_np1, data_np2) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-5) @pytest.mark.parametrize("data_shape", [(2, 2, 2), (2, 7, 2), (2, 2, 2, 1, 2, 3, 1), (1, 8)]) @@ -2125,7 +2125,7 @@ def test_forward_np_copy(data_shape, dtype, target, dev, kind): mod, _ = relay.frontend.from_mxnet(mx_sym, {"data": data_shape}, dtype=dtype) intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(data_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-5) @pytest.mark.parametrize("dtype", ["float64", "float32", "int64", "int32", "bool"]) @@ -2153,7 +2153,7 @@ def test_forward_npx_reshape(data_shape, out_shape, dtype, target, reverse, dev, mod, _ = relay.frontend.from_mxnet(mx_sym, {"data": data_shape}, dtype=dtype) intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(data_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-5) @pytest.mark.parametrize( @@ -2188,7 +2188,7 @@ def test_forward_npi_binary(data_shape, dtype, target, dev, kind): ) intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(data_np1, data_np2) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-5) @pytest.mark.parametrize( @@ -2220,7 +2220,7 @@ def test_forward_npi_binary_scalar(data_shape, dtype, scalar, target, dev, kind) mod, _ = relay.frontend.from_mxnet(mx_sym, shape={"lhs": data_shape}, dtype=dtype) intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(data_np1) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-5) @pytest.mark.parametrize( @@ -2237,7 +2237,7 @@ def test_forward_npi_tanh(data_shape, dtype, target, dev, kind): mod, _ = relay.frontend.from_mxnet(mx_sym, shape={"data": data_shape}, dtype=dtype) intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(data_np1) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-5) @pytest.mark.skipif(not hasattr(mx.np, "where"), reason="mx.np.where hasn't been publish yet") @@ -2269,7 +2269,7 @@ def test_forward_npi_where_rscalar( ) intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()(cond_np, data_np) - tvm.testing.assert_allclose(op_res.numpy(), ref_res.numpy(), rtol=1e-5) + tvm.testing.assert_allclose(op_res.numpy(), ref_res.asnumpy(), rtol=1e-5) @pytest.mark.parametrize("dtype", ["float64", "float32", "int64", "int32", "bool"]) @@ -2303,7 +2303,7 @@ def test_forward_split_v2( op_res_.append(arr.numpy().tolist()) ref_res_ = [] for arr in ref_res: - ref_res_.append(arr.numpy().tolist()) + ref_res_.append(arr.asnumpy().tolist()) tvm.testing.assert_allclose(op_res_, ref_res_, rtol=1e-5) diff --git a/tests/python/topi/python/test_topi_prng.py b/tests/python/topi/python/test_topi_prng.py index 63b8be259ada..9b5ca4893d64 100644 --- a/tests/python/topi/python/test_topi_prng.py +++ b/tests/python/topi/python/test_topi_prng.py @@ -40,7 +40,7 @@ def threefry_generate(target, dev, gen, size): out_gen = tvm.nd.array(np.zeros(gen.shape, dtype="uint64")) rands = tvm.nd.array(np.zeros(size, dtype="uint64")) f(tvm.nd.array(gen), out_gen, rands) - return out_gen.numpy(), rands.numpy() + return out_gen.asnumpy(), rands.asnumpy() def uniform(target, dev, gen, low, high, size, dtype): @@ -63,7 +63,7 @@ def uniform(target, dev, gen, low, high, size, dtype): @tvm.testing.parametrize_targets def test_threefry_split(target, dev): # test that results of split do not equal eachother or the input - gen = tvm.relay.random.threefry_key(0).data.numpy() + gen = tvm.relay.random.threefry_key(0).data.asnumpy() a, b = threefry_split(target, dev, gen) assert (a != b).any() and ( a != gen diff --git a/tutorials/frontend/deploy_quantized.py b/tutorials/frontend/deploy_quantized.py index fc7ce6dc44f0..b2210b8ab69b 100644 --- a/tutorials/frontend/deploy_quantized.py +++ b/tutorials/frontend/deploy_quantized.py @@ -57,7 +57,7 @@ def get_val_data(num_workers=4): std_rgb = [58.393, 57.12, 57.375] def batch_fn(batch): - return batch.data[0].numpy(), batch.label[0].numpy() + return batch.data[0].asnumpy(), batch.label[0].asnumpy() img_size = 299 if model_name == "inceptionv3" else 224 val_data = mx.io.ImageRecordIter( diff --git a/tutorials/frontend/deploy_ssd_gluoncv.py b/tutorials/frontend/deploy_ssd_gluoncv.py index 7e54c4cf212c..ebe18670c6a3 100644 --- a/tutorials/frontend/deploy_ssd_gluoncv.py +++ b/tutorials/frontend/deploy_ssd_gluoncv.py @@ -103,7 +103,7 @@ def build(target): def run(lib, dev): # Build TVM runtime m = graph_executor.GraphModule(lib["default"](dev)) - tvm_input = tvm.nd.array(x.numpy(), device=dev) + tvm_input = tvm.nd.array(x.asnumpy(), device=dev) m.set_input("data", tvm_input) # execute m.run() diff --git a/tutorials/frontend/from_mxnet.py b/tutorials/frontend/from_mxnet.py index 8a69ea29a20a..0ce610a2cdd6 100644 --- a/tutorials/frontend/from_mxnet.py +++ b/tutorials/frontend/from_mxnet.py @@ -129,7 +129,7 @@ def block2symbol(block): args = {} auxs = {} for k, v in block.collect_params().items(): - args[k] = mx.nd.array(v.data().numpy()) + args[k] = mx.nd.array(v.data().asnumpy()) return sym, args, auxs diff --git a/web/tests/python/webgpu_rpc_test.py b/web/tests/python/webgpu_rpc_test.py index c326f9fc1a16..e26937038fa2 100644 --- a/web/tests/python/webgpu_rpc_test.py +++ b/web/tests/python/webgpu_rpc_test.py @@ -70,11 +70,11 @@ def check(remote): a = tvm.nd.array(adata, dev) b = tvm.nd.array(np.zeros(n, dtype=A.dtype), dev) - np.testing.assert_equal(a.numpy(), adata) + np.testing.assert_equal(a.asnumpy(), adata) f1 = remote.system_lib() addone = f1.get_function("addone") addone(a, b) - np.testing.assert_equal(b.numpy(), a.numpy() + 1) + np.testing.assert_equal(b.asnumpy(), a.asnumpy() + 1) print("Test pass..") check(remote) diff --git a/web/tests/python/websock_rpc_test.py b/web/tests/python/websock_rpc_test.py index ee94e40a678c..84a0c9f134d5 100644 --- a/web/tests/python/websock_rpc_test.py +++ b/web/tests/python/websock_rpc_test.py @@ -82,7 +82,7 @@ def check(remote): time_f(a, b) cost = time_f(a, b).mean print("%g secs/op" % cost) - np.testing.assert_equal(b.numpy(), a.numpy() + 1) + np.testing.assert_equal(b.asnumpy(), a.asnumpy() + 1) check(remote) From e654b272eeba2376cd92a25d19e67c6a427ab665 Mon Sep 17 00:00:00 2001 From: electriclilies Date: Fri, 21 May 2021 12:20:48 -0700 Subject: [PATCH 15/24] missed a few --- tests/python/topi/python/test_topi_prng.py | 8 ++++---- tests/python/topi/python/test_topi_transform.py | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/python/topi/python/test_topi_prng.py b/tests/python/topi/python/test_topi_prng.py index 9b5ca4893d64..1be32e6ea1d1 100644 --- a/tests/python/topi/python/test_topi_prng.py +++ b/tests/python/topi/python/test_topi_prng.py @@ -40,7 +40,7 @@ def threefry_generate(target, dev, gen, size): out_gen = tvm.nd.array(np.zeros(gen.shape, dtype="uint64")) rands = tvm.nd.array(np.zeros(size, dtype="uint64")) f(tvm.nd.array(gen), out_gen, rands) - return out_gen.asnumpy(), rands.asnumpy() + return out_gen.numpy(), rands.numpy() def uniform(target, dev, gen, low, high, size, dtype): @@ -57,13 +57,13 @@ def uniform(target, dev, gen, low, high, size, dtype): out_gen = tvm.nd.array(np.zeros(gen.shape, dtype="uint64")) rands = tvm.nd.array(np.zeros(size, dtype=dtype)) f(tvm.nd.array(gen), tvm.nd.array(low), tvm.nd.array(high), out_gen, rands) - return out_gen.numpy(), rands.numpy() + return out_gen.asnumpy(), rands.asnumpy() @tvm.testing.parametrize_targets def test_threefry_split(target, dev): # test that results of split do not equal eachother or the input - gen = tvm.relay.random.threefry_key(0).data.asnumpy() + gen = tvm.relay.random.threefry_key(0).data.numpy() a, b = threefry_split(target, dev, gen) assert (a != b).any() and ( a != gen @@ -137,7 +137,7 @@ def test_threefry_wrapping(target, dev): @tvm.testing.parametrize_targets def test_uniform(target, dev): - gen = tvm.relay.random.threefry_key(0).data.numpy() + gen = tvm.relay.random.threefry_key(0).data.asnumpy() m = 1024 n = 1024 dtypes = ["float32", "float64"] diff --git a/tests/python/topi/python/test_topi_transform.py b/tests/python/topi/python/test_topi_transform.py index f7f5474769b4..20172f07fd9e 100644 --- a/tests/python/topi/python/test_topi_transform.py +++ b/tests/python/topi/python/test_topi_transform.py @@ -880,7 +880,7 @@ def test_transpose_unfused_schedule(target, dev): kind="graph", mod=tvm.IRModule.from_expr(relay.Function([x], f)), device=dev, target=target ) r = np.random.rand(*shape) - tvm.testing.assert_allclose(ex.evaluate()(r).numpy(), np.transpose(r)) + tvm.testing.assert_allclose(ex.evaluate()(r).asnumpy(), np.transpose(r)) # We want to make sure schedule does not fire here, but there is no way of # inspecting which schedules were used. @@ -893,7 +893,7 @@ def test_transpose_unfused_schedule(target, dev): device=dev, target=target, ) - tvm.testing.assert_allclose(ex.evaluate()(r, r).numpy(), np.transpose(r + r)) + tvm.testing.assert_allclose(ex.evaluate()(r, r).asnumpy(), np.transpose(r + r)) @tvm.testing.uses_gpu From e27ff5ed158c40cfc67ec1a855477eeba4757877 Mon Sep 17 00:00:00 2001 From: electriclilies Date: Fri, 21 May 2021 12:27:54 -0700 Subject: [PATCH 16/24] fix more .asnumpy --- tests/python/frontend/mxnet/test_forward.py | 4 ++-- tests/python/relay/test_op_level3.py | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/python/frontend/mxnet/test_forward.py b/tests/python/frontend/mxnet/test_forward.py index 1cb237b1074d..362a9b623d25 100644 --- a/tests/python/frontend/mxnet/test_forward.py +++ b/tests/python/frontend/mxnet/test_forward.py @@ -354,14 +354,14 @@ def _mx_symbol(F, start, stop, step): return sym def verify(start, stop, step): - ref_res = _mx_symbol(mx.nd, start, stop, step).numpy() + ref_res = _mx_symbol(mx.nd, start, stop, step).asnumpy() mx_sym = _mx_symbol(mx.sym, start, stop, step) mod, _ = relay.frontend.from_mxnet(mx_sym, {}) for target, dev in tvm.testing.enabled_targets(): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, mod=mod, device=dev, target=target) op_res = intrp.evaluate()() - tvm.testing.assert_allclose(op_res.asnumpy(), ref_res) + tvm.testing.assert_allclose(op_res.numpy(), ref_res) verify(0, 20, None) verify(0, 20, 2) diff --git a/tests/python/relay/test_op_level3.py b/tests/python/relay/test_op_level3.py index 9f5dc4e580c1..96e014fa3cb4 100644 --- a/tests/python/relay/test_op_level3.py +++ b/tests/python/relay/test_op_level3.py @@ -2012,17 +2012,17 @@ def verify_unique(n, dtype, is_dyn=False, is_sorted=False, return_counts=False): ) # unique, indices, inverse_indices, num_unique, counts num_unique = np_res[3][0] # num_unique - assert num_unique == tvm_res[3].asnumpy()[0] + assert num_unique == tvm_res[3].numpy()[0] # unique - tvm.testing.assert_allclose(tvm_res[0].asnumpy()[:num_unique], np_res[0], rtol=1e-5) + tvm.testing.assert_allclose(tvm_res[0].numpy()[:num_unique], np_res[0], rtol=1e-5) # indices - tvm.testing.assert_allclose(tvm_res[1].asnumpy()[:num_unique], np_res[1], rtol=1e-5) + tvm.testing.assert_allclose(tvm_res[1].numpy()[:num_unique], np_res[1], rtol=1e-5) # inverse_indices - tvm.testing.assert_allclose(tvm_res[2].asnumpy(), np_res[2], rtol=1e-5) + tvm.testing.assert_allclose(tvm_res[2].numpy(), np_res[2], rtol=1e-5) # counts if return_counts: tvm.testing.assert_allclose( - tvm_res[4].asnumpy()[:num_unique], np_res[4], rtol=1e-5 + tvm_res[4].numpy()[:num_unique], np_res[4], rtol=1e-5 ) for dtype in ["int32", "int64"]: From 3d50aef83c9969d1db95981acca35c2a1732dfcf Mon Sep 17 00:00:00 2001 From: electriclilies Date: Fri, 21 May 2021 12:31:04 -0700 Subject: [PATCH 17/24] fix black --- tests/python/topi/python/test_topi_unique.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/tests/python/topi/python/test_topi_unique.py b/tests/python/topi/python/test_topi_unique.py index 1252aaf0b781..e67b9aa74003 100644 --- a/tests/python/topi/python/test_topi_unique.py +++ b/tests/python/topi/python/test_topi_unique.py @@ -78,16 +78,21 @@ def check_unique(data, is_sorted=False, with_counts=False): if with_counts: tvm_counts = tvm.nd.array(np.zeros(data.shape).astype("int32"), device=dev) - func(tvm_data, tvm_unique, tvm_indices, tvm_inverse_indices, tvm_num_unique, tvm_counts) + func( + tvm_data, + tvm_unique, + tvm_indices, + tvm_inverse_indices, + tvm_num_unique, + tvm_counts, + ) else: func(tvm_data, tvm_unique, tvm_indices, tvm_inverse_indices, tvm_num_unique) - + num_unique = np_num_unique[0] assert tvm_num_unique.numpy()[0] == np_num_unique - np.testing.assert_allclose( - tvm_unique.numpy()[:num_unique], np_unique, atol=1e-5, rtol=1e-5 - ) + np.testing.assert_allclose(tvm_unique.numpy()[:num_unique], np_unique, atol=1e-5, rtol=1e-5) np.testing.assert_allclose( tvm_indices.numpy()[:num_unique], np_indices, atol=1e-5, rtol=1e-5 ) @@ -95,7 +100,7 @@ def check_unique(data, is_sorted=False, with_counts=False): np.testing.assert_allclose( tvm_inverse_indices.numpy()[:num_unique], np_inverse_indices, atol=1e-5, rtol=1e-5 ) - + if with_counts: np.testing.assert_allclose( tvm_counts.numpy()[:num_unique], np_counts, atol=1e-5, rtol=1e-5 From 03926558336ef0a437496095d32bb70b4ebdc154 Mon Sep 17 00:00:00 2001 From: electriclilies Date: Mon, 24 May 2021 11:24:34 -0700 Subject: [PATCH 18/24] Fix op level 3 test --- tests/python/relay/test_op_level3.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/python/relay/test_op_level3.py b/tests/python/relay/test_op_level3.py index 96e014fa3cb4..653dc7be9cce 100644 --- a/tests/python/relay/test_op_level3.py +++ b/tests/python/relay/test_op_level3.py @@ -1977,6 +1977,7 @@ def calc_numpy_unique(data, is_sorted=False): uniq = uniq[order].astype(data.dtype) inverse = np.array([reverse_order[i] for i in inverse]).astype("int32") counts = counts[order].astype("int32") + index = np.sort(index) # In unsorted case, need to sort the index of first occurence return [ uniq.astype(data.dtype), index.astype("int32"), @@ -2011,6 +2012,12 @@ def verify_unique(n, dtype, is_dyn=False, is_sorted=False, return_counts=False): x_data, is_sorted ) # unique, indices, inverse_indices, num_unique, counts num_unique = np_res[3][0] + + print("Data: ", x_data) + print("Numpy unique: ", np_res[0]) + print("Unique: ", tvm_res[0].numpy()) + print("Indices: ", tvm_res[1].numpy()) + print("Numpy indices: ", np_res[1]) # num_unique assert num_unique == tvm_res[3].numpy()[0] # unique From baaa97b1653d7b3336c205a41fdb631af3d3698d Mon Sep 17 00:00:00 2001 From: electriclilies Date: Mon, 24 May 2021 11:25:10 -0700 Subject: [PATCH 19/24] remove prints --- tests/python/relay/test_op_level3.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/tests/python/relay/test_op_level3.py b/tests/python/relay/test_op_level3.py index 653dc7be9cce..454beff59bfd 100644 --- a/tests/python/relay/test_op_level3.py +++ b/tests/python/relay/test_op_level3.py @@ -2013,11 +2013,6 @@ def verify_unique(n, dtype, is_dyn=False, is_sorted=False, return_counts=False): ) # unique, indices, inverse_indices, num_unique, counts num_unique = np_res[3][0] - print("Data: ", x_data) - print("Numpy unique: ", np_res[0]) - print("Unique: ", tvm_res[0].numpy()) - print("Indices: ", tvm_res[1].numpy()) - print("Numpy indices: ", np_res[1]) # num_unique assert num_unique == tvm_res[3].numpy()[0] # unique From 0181f57fbab532bf4558836ccda714c1a7bfcdfd Mon Sep 17 00:00:00 2001 From: electriclilies Date: Mon, 24 May 2021 11:46:55 -0700 Subject: [PATCH 20/24] Fix pytorch and tf importers --- python/tvm/relay/frontend/pytorch.py | 8 ++++---- python/tvm/relay/frontend/tensorflow.py | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/python/tvm/relay/frontend/pytorch.py b/python/tvm/relay/frontend/pytorch.py index b5cfcf5e3bac..4be12922cd84 100644 --- a/python/tvm/relay/frontend/pytorch.py +++ b/python/tvm/relay/frontend/pytorch.py @@ -2294,16 +2294,16 @@ def unique(self, inputs, input_types): logging.warning("TVM always assumes sorted=True for torch.unique") is_sorted = True if return_counts: - [unique, indices, num_uniq, counts] = _op.unique( + [unique, indices, inverse_indices, num_uniq, counts] = _op.unique( data, is_sorted=is_sorted, return_counts=True ) unique_sliced = _op.strided_slice(unique, begin=[0], end=num_uniq, slice_mode="size") counts_sliced = _op.strided_slice(counts, begin=[0], end=num_uniq, slice_mode="size") - return (unique_sliced, indices, counts_sliced) + return (unique_sliced, inverse_indices, counts_sliced) else: - [unique, indices, num_uniq] = _op.unique(data, is_sorted=is_sorted, return_counts=False) + [unique, indices, inverse_indices, num_uniq] = _op.unique(data, is_sorted=is_sorted, return_counts=False) unique_sliced = _op.strided_slice(unique, begin=[0], end=num_uniq, slice_mode="size") - return (unique_sliced, indices) + return (unique_sliced, inverse_indices) # Operator mappings def create_convert_map(self): diff --git a/python/tvm/relay/frontend/tensorflow.py b/python/tvm/relay/frontend/tensorflow.py index 4af73702ad9c..4ac1e2b44f75 100644 --- a/python/tvm/relay/frontend/tensorflow.py +++ b/python/tvm/relay/frontend/tensorflow.py @@ -2702,19 +2702,19 @@ def _impl(inputs, attr, params, mod): assert len(inputs) == 1 data = inputs[0] if return_counts: - [unique, indices, num_uniq, counts] = _op.unique( + [unique, indices, inverse_indices, num_uniq, counts] = _op.unique( data, is_sorted=False, return_counts=True ) unique_sliced = _op.strided_slice(unique, begin=[0], end=num_uniq, slice_mode="size") counts_sliced = _op.strided_slice(counts, begin=[0], end=num_uniq, slice_mode="size") return _expr.TupleWrapper( - _expr.Tuple([unique_sliced, indices, counts_sliced]), + _expr.Tuple([unique_sliced, inverse_indices, counts_sliced]), 3, ) - [unique, indices, num_uniq] = _op.unique(data, is_sorted=False, return_counts=False) + [unique, indices, inverse_indices, num_uniq] = _op.unique(data, is_sorted=False, return_counts=False) unique_sliced = _op.strided_slice(unique, begin=[0], end=num_uniq, slice_mode="size") return _expr.TupleWrapper( - _expr.Tuple([unique_sliced, indices]), + _expr.Tuple([unique_sliced, inverse_indices]), 2, ) From 00f989b8cbaa265763b5fe717409f6981b40fb33 Mon Sep 17 00:00:00 2001 From: electriclilies Date: Mon, 24 May 2021 11:53:32 -0700 Subject: [PATCH 21/24] black --- python/tvm/relay/frontend/pytorch.py | 4 +++- python/tvm/relay/frontend/tensorflow.py | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/python/tvm/relay/frontend/pytorch.py b/python/tvm/relay/frontend/pytorch.py index 4be12922cd84..f0ba99291727 100644 --- a/python/tvm/relay/frontend/pytorch.py +++ b/python/tvm/relay/frontend/pytorch.py @@ -2301,7 +2301,9 @@ def unique(self, inputs, input_types): counts_sliced = _op.strided_slice(counts, begin=[0], end=num_uniq, slice_mode="size") return (unique_sliced, inverse_indices, counts_sliced) else: - [unique, indices, inverse_indices, num_uniq] = _op.unique(data, is_sorted=is_sorted, return_counts=False) + [unique, indices, inverse_indices, num_uniq] = _op.unique( + data, is_sorted=is_sorted, return_counts=False + ) unique_sliced = _op.strided_slice(unique, begin=[0], end=num_uniq, slice_mode="size") return (unique_sliced, inverse_indices) diff --git a/python/tvm/relay/frontend/tensorflow.py b/python/tvm/relay/frontend/tensorflow.py index 4ac1e2b44f75..8f3ad7d840b9 100644 --- a/python/tvm/relay/frontend/tensorflow.py +++ b/python/tvm/relay/frontend/tensorflow.py @@ -2711,7 +2711,9 @@ def _impl(inputs, attr, params, mod): _expr.Tuple([unique_sliced, inverse_indices, counts_sliced]), 3, ) - [unique, indices, inverse_indices, num_uniq] = _op.unique(data, is_sorted=False, return_counts=False) + [unique, indices, inverse_indices, num_uniq] = _op.unique( + data, is_sorted=False, return_counts=False + ) unique_sliced = _op.strided_slice(unique, begin=[0], end=num_uniq, slice_mode="size") return _expr.TupleWrapper( _expr.Tuple([unique_sliced, inverse_indices]), From d7676bae62d748f93d4c809cb5afb0a3aaf709f4 Mon Sep 17 00:00:00 2001 From: electriclilies Date: Mon, 24 May 2021 12:07:35 -0700 Subject: [PATCH 22/24] fix lint --- python/tvm/relay/frontend/tensorflow.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/tvm/relay/frontend/tensorflow.py b/python/tvm/relay/frontend/tensorflow.py index 8f3ad7d840b9..040f8384dbe0 100644 --- a/python/tvm/relay/frontend/tensorflow.py +++ b/python/tvm/relay/frontend/tensorflow.py @@ -2702,7 +2702,7 @@ def _impl(inputs, attr, params, mod): assert len(inputs) == 1 data = inputs[0] if return_counts: - [unique, indices, inverse_indices, num_uniq, counts] = _op.unique( + [unique, _, inverse_indices, num_uniq, counts] = _op.unique( data, is_sorted=False, return_counts=True ) unique_sliced = _op.strided_slice(unique, begin=[0], end=num_uniq, slice_mode="size") @@ -2711,7 +2711,7 @@ def _impl(inputs, attr, params, mod): _expr.Tuple([unique_sliced, inverse_indices, counts_sliced]), 3, ) - [unique, indices, inverse_indices, num_uniq] = _op.unique( + [unique, _, inverse_indices, num_uniq] = _op.unique( data, is_sorted=False, return_counts=False ) unique_sliced = _op.strided_slice(unique, begin=[0], end=num_uniq, slice_mode="size") From 8fcd5a7a2881b497edb31150a91898d0afe86173 Mon Sep 17 00:00:00 2001 From: electriclilies Date: Tue, 25 May 2021 14:07:54 -0700 Subject: [PATCH 23/24] fix indentation --- python/tvm/relay/frontend/onnx.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/python/tvm/relay/frontend/onnx.py b/python/tvm/relay/frontend/onnx.py index 8be5eaab433f..6a72d76576a5 100644 --- a/python/tvm/relay/frontend/onnx.py +++ b/python/tvm/relay/frontend/onnx.py @@ -3376,11 +3376,11 @@ def from_onnx(self, graph, opset, get_output_expr=False): len(node_output), outputs_num, op_name ) - if outputs_num == 1: - self._nodes[node_output[0]] = op - else: - for k, i in zip(list(node_output), range(len(node_output))): - self._nodes[k] = op[i] + if outputs_num == 1: + self._nodes[node_output[0]] = op + else: + for k, i in zip(list(node_output), range(len(node_output))): + self._nodes[k] = op[i] # now return the outputs outputs = [self._nodes[self._parse_value_proto(i)] for i in graph.output] From 9f42cfebb898c016bb52995e6a085a9c7bd5772b Mon Sep 17 00:00:00 2001 From: electriclilies Date: Fri, 28 May 2021 13:33:53 -0700 Subject: [PATCH 24/24] fix topi test --- tests/python/topi/python/test_topi_unique.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/python/topi/python/test_topi_unique.py b/tests/python/topi/python/test_topi_unique.py index e67b9aa74003..3e26241cea94 100644 --- a/tests/python/topi/python/test_topi_unique.py +++ b/tests/python/topi/python/test_topi_unique.py @@ -30,6 +30,7 @@ def calc_numpy_unique(data, is_sorted=False): num_uniq = np.array([len(uniq)]).astype("int32") if not is_sorted: order = np.argsort(index) + index = np.sort(index) reverse_order = np.argsort(order) uniq = uniq[order].astype(data.dtype) inverse = np.array([reverse_order[i] for i in inverse]).astype("int32") @@ -98,7 +99,7 @@ def check_unique(data, is_sorted=False, with_counts=False): ) np.testing.assert_allclose( - tvm_inverse_indices.numpy()[:num_unique], np_inverse_indices, atol=1e-5, rtol=1e-5 + tvm_inverse_indices.numpy(), np_inverse_indices, atol=1e-5, rtol=1e-5 ) if with_counts: