From 871efe0e640ec8853b9d23e5c5eed4021b822ee9 Mon Sep 17 00:00:00 2001 From: Siju Samuel Date: Thu, 18 Oct 2018 22:21:44 +0530 Subject: [PATCH 1/4] [RELAY]Reduce ops --- docs/langref/relay_op.rst | 10 ++ python/tvm/relay/op/reduce.py | 193 +++++++++++++++++++++++++++ src/relay/op/tensor/reduce.cc | 111 +++++++++++++++ tests/python/relay/test_op_level4.py | 43 ++++++ 4 files changed, 357 insertions(+) diff --git a/docs/langref/relay_op.rst b/docs/langref/relay_op.rst index 56558272f2a3..a36f8e6c71cf 100644 --- a/docs/langref/relay_op.rst +++ b/docs/langref/relay_op.rst @@ -108,6 +108,11 @@ This level enables additional math and transform operators. tvm.relay.where tvm.relay.argmax tvm.relay.argmin + tvm.relay.sum + tvm.relay.max + tvm.relay.min + tvm.relay.mean + tvm.relay.prod **Level 5: Vision/Image Operators** @@ -187,6 +192,11 @@ Level 4 Definitions .. autofunction:: tvm.relay.where .. autofunction:: tvm.relay.argmax .. autofunction:: tvm.relay.argmin +.. autofunction:: tvm.relay.sum +.. autofunction:: tvm.relay.max +.. autofunction:: tvm.relay.min +.. autofunction:: tvm.relay.mean +.. autofunction:: tvm.relay.prod Level 5 Definitions diff --git a/python/tvm/relay/op/reduce.py b/python/tvm/relay/op/reduce.py index a2a4519512ea..8f6da630312c 100644 --- a/python/tvm/relay/op/reduce.py +++ b/python/tvm/relay/op/reduce.py @@ -62,3 +62,196 @@ def argmin(data, axis=None, keepdims=False, exclude=False): """ return _make.argmin(data, axis, keepdims, exclude) + + +def sum(data, axis=None, keepdims=False, exclude=False): + """Computes the sum of array elements over given axes. + + Example:: + + data = [[[1,2],[2,3],[1,3]], + [[1,4],[4,3],[5,2]], + [[7,1],[7,2],[7,3]]] + + sum(data, axis=1) + [[ 4. 8.] + [ 10. 9.] + [ 21. 6.]] + + sum(data, axis=[1,2]) + [ 12. 19. 27.] + + Parameters + ---------- + data : relay.Expr + The input data + + axis : None or int or tuple of int + Axis or axes along which a argmin operation is performed. + The default, axis=None, will find the indices of minimum element all of the elements of + the input array. If axis is negative it counts from the last to the first axis. + + keepdims : bool + If this is set to True, the axes which are reduced are left in the result as dimensions + with size one. + With this option, the result will broadcast correctly against the input array. + + exclude : bool + If `exclude` is true, reduction will be performed on the axes that are + NOT in axis instead. + + Returns + ------- + result : relay.Expr + The computed result. + """ + + return _make.sum(data, axis, keepdims, exclude) + + +def max(data, axis=None, keepdims=False, exclude=False): + """ Computes the max of array elements over given axes. + + Parameters + ---------- + data : relay.Expr + The input data + + axis : None or int or tuple of int + Axis or axes along which a argmin operation is performed. + The default, axis=None, will find the indices of minimum element all of the elements of + the input array. If axis is negative it counts from the last to the first axis. + + keepdims : bool + If this is set to True, the axes which are reduced are left in the result as dimensions + with size one. + With this option, the result will broadcast correctly against the input array. + + exclude : bool + If `exclude` is true, reduction will be performed on the axes that are + NOT in axis instead. + + Returns + ------- + result : relay.Expr + The computed result. + """ + + return _make.max(data, axis, keepdims, exclude) + + +def min(data, axis=None, keepdims=False, exclude=False): + """Computes the min of array elements over given axes. + + Parameters + ---------- + data : relay.Expr + The input data + + axis : None or int or tuple of int + Axis or axes along which a argmin operation is performed. + The default, axis=None, will find the indices of minimum element all of the elements of + the input array. If axis is negative it counts from the last to the first axis. + + keepdims : bool + If this is set to True, the axes which are reduced are left in the result as dimensions + with size one. + With this option, the result will broadcast correctly against the input array. + + exclude : bool + If `exclude` is true, reduction will be performed on the axes that are + NOT in axis instead. + + Returns + ------- + result : relay.Expr + The computed result. + """ + + return _make.min(data, axis, keepdims, exclude) + + +def mean(data, axis=None, keepdims=False, exclude=False): + """Computes the mean of array elements over given axes. + + Example:: + + data = [[[1,2],[2,3],[1,3]], + [[1,4],[4,3],[5,2]], + [[7,1],[7,2],[7,3]]] + + mean(data) + [3.22] + + mean(data, axis=[1,2]) + [ 2. 3.16666667 4.5] + + Parameters + ---------- + data : relay.Expr + The input data + + axis : None or int or tuple of int + Axis or axes along which a argmin operation is performed. + The default, axis=None, will find the indices of minimum element all of the elements of + the input array. If axis is negative it counts from the last to the first axis. + + keepdims : bool + If this is set to True, the axes which are reduced are left in the result as dimensions + with size one. + With this option, the result will broadcast correctly against the input array. + + exclude : bool + If `exclude` is true, reduction will be performed on the axes that are + NOT in axis instead. + + Returns + ------- + result : relay.Expr + The computed result. + """ + + return _make.mean(data, axis, keepdims, exclude) + + +def prod(data, axis=None, keepdims=False, exclude=False): + """Computes the products of array elements over given axes. + + Example:: + + data = [[[1,2],[2,3],[1,3]], + [[1,4],[4,3],[5,2]], + [[7,1],[7,2],[7,3]]] + + mean(data, axis=1) + [35562240] + + mean(data, axis=[1,2]) + [ 36 480 2058] + + Parameters + ---------- + data : relay.Expr + The input data + + axis : None or int or tuple of int + Axis or axes along which a argmin operation is performed. + The default, axis=None, will find the indices of minimum element all of the elements of + the input array. If axis is negative it counts from the last to the first axis. + + keepdims : bool + If this is set to True, the axes which are reduced are left in the result as dimensions + with size one. + With this option, the result will broadcast correctly against the input array. + + exclude : bool + If `exclude` is true, reduction will be performed on the axes that are + NOT in axis instead. + + Returns + ------- + result : relay.Expr + The computed result. + """ + + return _make.prod(data, axis, keepdims, exclude) diff --git a/src/relay/op/tensor/reduce.cc b/src/relay/op/tensor/reduce.cc index 017ef1e5dfec..71e95f5dd118 100644 --- a/src/relay/op/tensor/reduce.cc +++ b/src/relay/op/tensor/reduce.cc @@ -172,6 +172,34 @@ bool ArgReduceRel(const Array& types, return true; } +/*! +* \brief ReduceRel Output type and shape relation evaluation function. +* \param num_inputs Number of input types in the args. +* \param attrs The additional attributes of the operator. +* \param reporter The reporter to report solution to. +* \return false if This relation cannot be resolved. true if this relation has been resolved. +*/ +bool ReduceRel(const Array& types, + int num_inputs, + const Attrs& attrs, + const TypeReporter& reporter) { + CHECK_EQ(types.size(), 2); + const auto* data = types[0].as(); + if (data == nullptr) return false; + CHECK(static_cast(data->shape.size()) != 0); + std::vector in_shape; + for (auto i : data->shape) { + in_shape.push_back(i); + } + + const ReduceAttrs* param = attrs.as(); + CHECK(param != nullptr); + + // assign output type and shape + auto oshape = ReduceShapeImpl(in_shape, param, reporter); + reporter->Assign(types[1], TensorTypeNode::make(oshape, data->dtype)); + return true; +} #define RELAY_REGISTER_REDUCE_OP(OpName) \ TVM_REGISTER_API("relay.op._make." OpName) \ @@ -213,5 +241,88 @@ values over a given axis. .set_support_level(4) .add_type_rel("ArgReduce", ArgReduceRel); + +RELAY_REGISTER_REDUCE_OP("sum") +.describe(R"code(Computes the sum of array elements over given axes. + +Example:: + + data = [[[1,2],[2,3],[1,3]], + [[1,4],[4,3],[5,2]], + [[7,1],[7,2],[7,3]]] + + sum(data, axis=1) + [[ 4. 8.] + [ 10. 9.] + [ 21. 6.]] + + sum(data, axis=[1,2]) + [ 12. 19. 27.] + +)code" TVM_ADD_FILELINE) +.set_num_inputs(1) +.set_support_level(4) +.add_type_rel("Reduce", ReduceRel); + + +RELAY_REGISTER_REDUCE_OP("max") +.describe(R"code(Computes the max of array elements over given axes. + +)code" TVM_ADD_FILELINE) +.set_num_inputs(1) +.set_support_level(4) +.add_type_rel("Reduce", ReduceRel); + + +RELAY_REGISTER_REDUCE_OP("min") +.describe(R"code(Computes the min of array elements over given axes. + +)code" TVM_ADD_FILELINE) +.set_num_inputs(1) +.set_support_level(4) +.add_type_rel("Reduce", ReduceRel); + + +RELAY_REGISTER_REDUCE_OP("mean") +.describe(R"code(Computes the mean of array elements over given axes. + +Example:: + + data = [[[1,2],[2,3],[1,3]], + [[1,4],[4,3],[5,2]], + [[7,1],[7,2],[7,3]]] + + mean(data) + [3.22] + + mean(data, axis=[1,2]) + [ 2. 3.16666667 4.5] + +)code" TVM_ADD_FILELINE) +.set_num_inputs(1) +.set_support_level(4) +.add_type_rel("Reduce", ReduceRel); + + +RELAY_REGISTER_REDUCE_OP("prod") +.describe(R"code(Computes the products of array elements over given axes. + +Example:: + + data = [[[1,2],[2,3],[1,3]], + [[1,4],[4,3],[5,2]], + [[7,1],[7,2],[7,3]]] + + mean(data, axis=1) + [35562240] + + mean(data, axis=[1,2]) + [ 36 480 2058] + +)code" TVM_ADD_FILELINE) +.set_num_inputs(1) +.set_support_level(4) +.add_type_rel("Reduce", ReduceRel); + } // namespace relay } // namespace tvm diff --git a/tests/python/relay/test_op_level4.py b/tests/python/relay/test_op_level4.py index c2b685affab4..cfa2d3be2f7b 100644 --- a/tests/python/relay/test_op_level4.py +++ b/tests/python/relay/test_op_level4.py @@ -65,6 +65,15 @@ def test_arg_reduce(): z = relay.argmax(x, axis=(2,), keepdims=True, exclude=True) zz = relay.ir_pass.infer_type(z) assert zz.checked_type == relay.ty.TensorType((1, 1 , h, 1), "int32") + ib = relay.ir_builder.IRBuilder() + x = ib.param("x", relay.TensorType((10, 4), "int32")) + y = ib.param("y", relay.TensorType((5, 10, 1), "int32")) + with ib.function(x, y) as func: + ib.ret(op(x, y)) + ib.ret(func) + func = relay.ir_pass.infer_type(ib.env, func.to_func()) + ftype = func.checked_type + assert ftype.ret_type == relay.TensorType((5, 10, 4), "int32") def test_where(): @@ -76,9 +85,43 @@ def test_where(): assert zz.checked_type == relay.TensorType((3, 4), "float32") +def verify_reduce(test_func, data, axis, keepdims, exclude, output): + ib = relay.ir_builder.IRBuilder() + x = ib.param("x", relay.ty.TensorType(data, "float32")) + with ib.function(x) as func: + ib.ret(test_func(x, axis, keepdims, exclude)) + ib.ret(func) + func = relay.ir_pass.infer_type(ib.env, func.to_func()) + ftype = func.checked_type + out_type = "int32" if test_func in [relay.argmin, relay.argmax] else "float32" + assert ftype.ret_type == relay.ty.TensorType(output, out_type) + +def test_reduce(): + d1, d2, d3, d4 = tvm.var("d1"), tvm.var("d2"), tvm.var("d3"), tvm.var("d4") + for func in [relay.sum, + relay.max, + relay.min, + relay.mean, + relay.prod, + relay.argmin, + relay.argmax]: + verify_reduce(func, (d1, d2, d3, d4), (2,), True, False, (d1, d2, 1, d4)) + verify_reduce(func, (2, 3, 4), (1,), True, False, (2, 1, 4)) + verify_reduce(func, (2, 3, 4), (0, 1, 2), False, False, ()) + verify_reduce(func, (4, 4, 3), None, True, False, (1, 1, 1)) + verify_reduce(func, (4, 4, 3), None, False, False, ()) + verify_reduce(func, (4, 4, 3), (0, 2), False, False, (4,)) + verify_reduce(func, (128, 24, 128), (0, 1), False, False, (128,)) + verify_reduce(func, (128, 24, 128), (0, 2), False, False, (24,)) + verify_reduce(func, (128, 24, 128), (0, 1), True, False, (1, 1, 128)) + verify_reduce(func, (128, 24, 128), (0, 2), True, False, (1, 24, 1)) + verify_reduce(func, (128, 24, 128), None, True, False, (1, 1, 1)) + if __name__ == "__main__": test_binary_op() test_cmp_type() test_binary_int_broadcast() test_where() test_arg_reduce() + test_multibox_prior() + test_reduce() From bf62afc4e9186d037261104ffaede1826154ccd8 Mon Sep 17 00:00:00 2001 From: Siju Samuel Date: Sat, 20 Oct 2018 08:26:35 +0530 Subject: [PATCH 2/4] Review Comments --- python/tvm/relay/op/reduce.py | 38 ----------------------------------- src/relay/op/tensor/reduce.cc | 18 +++++++++-------- 2 files changed, 10 insertions(+), 46 deletions(-) diff --git a/python/tvm/relay/op/reduce.py b/python/tvm/relay/op/reduce.py index 8f6da630312c..1ffbc15ade86 100644 --- a/python/tvm/relay/op/reduce.py +++ b/python/tvm/relay/op/reduce.py @@ -67,20 +67,6 @@ def argmin(data, axis=None, keepdims=False, exclude=False): def sum(data, axis=None, keepdims=False, exclude=False): """Computes the sum of array elements over given axes. - Example:: - - data = [[[1,2],[2,3],[1,3]], - [[1,4],[4,3],[5,2]], - [[7,1],[7,2],[7,3]]] - - sum(data, axis=1) - [[ 4. 8.] - [ 10. 9.] - [ 21. 6.]] - - sum(data, axis=[1,2]) - [ 12. 19. 27.] - Parameters ---------- data : relay.Expr @@ -174,18 +160,6 @@ def min(data, axis=None, keepdims=False, exclude=False): def mean(data, axis=None, keepdims=False, exclude=False): """Computes the mean of array elements over given axes. - Example:: - - data = [[[1,2],[2,3],[1,3]], - [[1,4],[4,3],[5,2]], - [[7,1],[7,2],[7,3]]] - - mean(data) - [3.22] - - mean(data, axis=[1,2]) - [ 2. 3.16666667 4.5] - Parameters ---------- data : relay.Expr @@ -217,18 +191,6 @@ def mean(data, axis=None, keepdims=False, exclude=False): def prod(data, axis=None, keepdims=False, exclude=False): """Computes the products of array elements over given axes. - Example:: - - data = [[[1,2],[2,3],[1,3]], - [[1,4],[4,3],[5,2]], - [[7,1],[7,2],[7,3]]] - - mean(data, axis=1) - [35562240] - - mean(data, axis=[1,2]) - [ 36 480 2058] - Parameters ---------- data : relay.Expr diff --git a/src/relay/op/tensor/reduce.cc b/src/relay/op/tensor/reduce.cc index 71e95f5dd118..8c9ea19c2f13 100644 --- a/src/relay/op/tensor/reduce.cc +++ b/src/relay/op/tensor/reduce.cc @@ -7,6 +7,7 @@ #include #include #include +#include "../op_common.h" #include "../type_relations.h" namespace tvm { @@ -158,10 +159,7 @@ bool ArgReduceRel(const Array& types, const auto* data = types[0].as(); if (data == nullptr) return false; CHECK(static_cast(data->shape.size()) != 0); - std::vector in_shape; - for (auto i : data->shape) { - in_shape.push_back(i); - } + std::vector&& in_shape = AsVector(data->shape); const ReduceAttrs* param = attrs.as(); CHECK(param != nullptr); @@ -187,10 +185,7 @@ bool ReduceRel(const Array& types, const auto* data = types[0].as(); if (data == nullptr) return false; CHECK(static_cast(data->shape.size()) != 0); - std::vector in_shape; - for (auto i : data->shape) { - in_shape.push_back(i); - } + std::vector&& in_shape = AsVector(data->shape); const ReduceAttrs* param = attrs.as(); CHECK(param != nullptr); @@ -229,6 +224,7 @@ values over a given axis. )code" TVM_ADD_FILELINE) .set_attrs_type_key("relay.attrs.ReduceAttrs") .set_support_level(4) +.set_attrs_type_key("relay.attrs.ReduceAttrs") .add_type_rel("ArgReduce", ArgReduceRel); @@ -239,6 +235,7 @@ values over a given axis. )code" TVM_ADD_FILELINE) .set_attrs_type_key("relay.attrs.ReduceAttrs") .set_support_level(4) +.set_attrs_type_key("relay.attrs.ReduceAttrs") .add_type_rel("ArgReduce", ArgReduceRel); @@ -262,6 +259,7 @@ Example:: )code" TVM_ADD_FILELINE) .set_num_inputs(1) .set_support_level(4) +.set_attrs_type_key("relay.attrs.ReduceAttrs") .add_type_rel("Reduce", ReduceRel); @@ -271,6 +269,7 @@ RELAY_REGISTER_REDUCE_OP("max") )code" TVM_ADD_FILELINE) .set_num_inputs(1) .set_support_level(4) +.set_attrs_type_key("relay.attrs.ReduceAttrs") .add_type_rel("Reduce", ReduceRel); @@ -280,6 +279,7 @@ RELAY_REGISTER_REDUCE_OP("min") )code" TVM_ADD_FILELINE) .set_num_inputs(1) .set_support_level(4) +.set_attrs_type_key("relay.attrs.ReduceAttrs") .add_type_rel("Reduce", ReduceRel); @@ -301,6 +301,7 @@ Example:: )code" TVM_ADD_FILELINE) .set_num_inputs(1) .set_support_level(4) +.set_attrs_type_key("relay.attrs.ReduceAttrs") .add_type_rel("Reduce", ReduceRel); @@ -322,6 +323,7 @@ Example:: )code" TVM_ADD_FILELINE) .set_num_inputs(1) .set_support_level(4) +.set_attrs_type_key("relay.attrs.ReduceAttrs") .add_type_rel("Reduce", ReduceRel); } // namespace relay From bd1d7e5fd16a43505654ecaa8159dccb2436976a Mon Sep 17 00:00:00 2001 From: Siju Samuel Date: Sun, 21 Oct 2018 07:45:58 +0530 Subject: [PATCH 3/4] Rebasing and testcases updated with astext asserts --- src/relay/op/tensor/reduce.cc | 19 ++++------ tests/python/relay/test_op_level4.py | 54 ++++++++-------------------- 2 files changed, 20 insertions(+), 53 deletions(-) diff --git a/src/relay/op/tensor/reduce.cc b/src/relay/op/tensor/reduce.cc index 8c9ea19c2f13..0a955fad631b 100644 --- a/src/relay/op/tensor/reduce.cc +++ b/src/relay/op/tensor/reduce.cc @@ -20,7 +20,7 @@ struct ReduceAttrs : public tvm::AttrsNode { bool exclude; TVM_DECLARE_ATTRS(ReduceAttrs, "relay.attrs.ReduceAttrs") { - TVM_ATTR_FIELD(axis).set_default(Array({})) + TVM_ATTR_FIELD(axis).set_default(NullValue>()) .describe(R"code(The axis or axes along which to perform the reduction. The default, `axis=()`, will compute over all elements into a @@ -224,7 +224,6 @@ values over a given axis. )code" TVM_ADD_FILELINE) .set_attrs_type_key("relay.attrs.ReduceAttrs") .set_support_level(4) -.set_attrs_type_key("relay.attrs.ReduceAttrs") .add_type_rel("ArgReduce", ArgReduceRel); @@ -235,7 +234,6 @@ values over a given axis. )code" TVM_ADD_FILELINE) .set_attrs_type_key("relay.attrs.ReduceAttrs") .set_support_level(4) -.set_attrs_type_key("relay.attrs.ReduceAttrs") .add_type_rel("ArgReduce", ArgReduceRel); @@ -257,9 +255,8 @@ Example:: [ 12. 19. 27.] )code" TVM_ADD_FILELINE) -.set_num_inputs(1) -.set_support_level(4) .set_attrs_type_key("relay.attrs.ReduceAttrs") +.set_support_level(4) .add_type_rel("Reduce", ReduceRel); @@ -267,9 +264,8 @@ RELAY_REGISTER_REDUCE_OP("max") .describe(R"code(Computes the max of array elements over given axes. )code" TVM_ADD_FILELINE) -.set_num_inputs(1) -.set_support_level(4) .set_attrs_type_key("relay.attrs.ReduceAttrs") +.set_support_level(4) .add_type_rel("Reduce", ReduceRel); @@ -277,9 +273,8 @@ RELAY_REGISTER_REDUCE_OP("min") .describe(R"code(Computes the min of array elements over given axes. )code" TVM_ADD_FILELINE) -.set_num_inputs(1) -.set_support_level(4) .set_attrs_type_key("relay.attrs.ReduceAttrs") +.set_support_level(4) .add_type_rel("Reduce", ReduceRel); @@ -299,9 +294,8 @@ Example:: [ 2. 3.16666667 4.5] )code" TVM_ADD_FILELINE) -.set_num_inputs(1) -.set_support_level(4) .set_attrs_type_key("relay.attrs.ReduceAttrs") +.set_support_level(4) .add_type_rel("Reduce", ReduceRel); @@ -321,9 +315,8 @@ Example:: [ 36 480 2058] )code" TVM_ADD_FILELINE) -.set_num_inputs(1) -.set_support_level(4) .set_attrs_type_key("relay.attrs.ReduceAttrs") +.set_support_level(4) .add_type_rel("Reduce", ReduceRel); } // namespace relay diff --git a/tests/python/relay/test_op_level4.py b/tests/python/relay/test_op_level4.py index cfa2d3be2f7b..74a2e35d41ab 100644 --- a/tests/python/relay/test_op_level4.py +++ b/tests/python/relay/test_op_level4.py @@ -46,36 +46,6 @@ def test_binary_int_broadcast(): assert zz.checked_type == relay.TensorType((5, 10, 4), "int32") -def test_arg_reduce(): - for op in [relay.argmax, relay.argmin]: - n, c , h, w = 10, 20, 3, 4 - x = relay.var("x", relay.ty.TensorType((n, c , h, w), "float32")) - z = relay.argmax(x, axis=(1,)) - "axis=" in z.astext() - zz = relay.ir_pass.infer_type(z) - assert zz.checked_type == relay.ty.TensorType((n, h, w), "int32") - n, c , h, w = tvm.var("n"), tvm.var("c"), tvm.var("h"), tvm.var("w") - x = relay.var("x", relay.ty.TensorType((n, c , h, w), "float32")) - z = relay.argmax(x, axis=(2,), keepdims=True) - zz = relay.ir_pass.infer_type(z) - assert zz.checked_type == relay.ty.TensorType((n, c , 1, w), "int32") - - n, c , h, w = tvm.var("n"), tvm.var("c"), tvm.var("h"), tvm.var("w") - x = relay.var("x", relay.ty.TensorType((n, c , h, w), "float32")) - z = relay.argmax(x, axis=(2,), keepdims=True, exclude=True) - zz = relay.ir_pass.infer_type(z) - assert zz.checked_type == relay.ty.TensorType((1, 1 , h, 1), "int32") - ib = relay.ir_builder.IRBuilder() - x = ib.param("x", relay.TensorType((10, 4), "int32")) - y = ib.param("y", relay.TensorType((5, 10, 1), "int32")) - with ib.function(x, y) as func: - ib.ret(op(x, y)) - ib.ret(func) - func = relay.ir_pass.infer_type(ib.env, func.to_func()) - ftype = func.checked_type - assert ftype.ret_type == relay.TensorType((5, 10, 4), "int32") - - def test_where(): cond = relay.var("cond", relay.TensorType((3, 4), "float32")) x = relay.var("x", relay.TensorType((3, 4), "float32")) @@ -86,15 +56,17 @@ def test_where(): def verify_reduce(test_func, data, axis, keepdims, exclude, output): - ib = relay.ir_builder.IRBuilder() - x = ib.param("x", relay.ty.TensorType(data, "float32")) - with ib.function(x) as func: - ib.ret(test_func(x, axis, keepdims, exclude)) - ib.ret(func) - func = relay.ir_pass.infer_type(ib.env, func.to_func()) - ftype = func.checked_type + x = relay.var("x", relay.TensorType(data, "float32")) + z = test_func(x, axis, keepdims, exclude) + zz = relay.ir_pass.infer_type(z) + if axis: + assert "axis=" in z.astext() + if keepdims: + assert "keepdims=" in z.astext() + if exclude: + assert "exclude=" in z.astext() out_type = "int32" if test_func in [relay.argmin, relay.argmax] else "float32" - assert ftype.ret_type == relay.ty.TensorType(output, out_type) + assert zz.checked_type == relay.ty.TensorType(output, out_type) def test_reduce(): d1, d2, d3, d4 = tvm.var("d1"), tvm.var("d2"), tvm.var("d3"), tvm.var("d4") @@ -106,16 +78,18 @@ def test_reduce(): relay.argmin, relay.argmax]: verify_reduce(func, (d1, d2, d3, d4), (2,), True, False, (d1, d2, 1, d4)) + verify_reduce(func, (d1, d2, d3), (1,), True, False, (d1, 1, d3)) + verify_reduce(func, (d1, d2, d3), None, True, False, (1, 1, 1)) + verify_reduce(func, (d1, d2, d3), (0, 1), True, False, (1, 1, d3)) verify_reduce(func, (2, 3, 4), (1,), True, False, (2, 1, 4)) verify_reduce(func, (2, 3, 4), (0, 1, 2), False, False, ()) verify_reduce(func, (4, 4, 3), None, True, False, (1, 1, 1)) - verify_reduce(func, (4, 4, 3), None, False, False, ()) + verify_reduce(func, (4, 4, 3), None, False, True, ()) verify_reduce(func, (4, 4, 3), (0, 2), False, False, (4,)) verify_reduce(func, (128, 24, 128), (0, 1), False, False, (128,)) verify_reduce(func, (128, 24, 128), (0, 2), False, False, (24,)) verify_reduce(func, (128, 24, 128), (0, 1), True, False, (1, 1, 128)) verify_reduce(func, (128, 24, 128), (0, 2), True, False, (1, 24, 1)) - verify_reduce(func, (128, 24, 128), None, True, False, (1, 1, 1)) if __name__ == "__main__": test_binary_op() From 755ae6738e255bdd625d1c2329154119eecf8392 Mon Sep 17 00:00:00 2001 From: Siju Samuel Date: Sun, 21 Oct 2018 12:57:48 +0530 Subject: [PATCH 4/4] argmin/max test cases as part of test_reduce --- python/tvm/relay/op/reduce.py | 7 ------- tests/python/relay/test_op_level4.py | 6 ++---- 2 files changed, 2 insertions(+), 11 deletions(-) diff --git a/python/tvm/relay/op/reduce.py b/python/tvm/relay/op/reduce.py index 1ffbc15ade86..73c5f270e8bf 100644 --- a/python/tvm/relay/op/reduce.py +++ b/python/tvm/relay/op/reduce.py @@ -30,7 +30,6 @@ def argmax(data, axis=None, keepdims=False, exclude=False): result : relay.Expr The computed result. """ - return _make.argmax(data, axis, keepdims, exclude) def argmin(data, axis=None, keepdims=False, exclude=False): @@ -60,7 +59,6 @@ def argmin(data, axis=None, keepdims=False, exclude=False): result : relay.Expr The computed result. """ - return _make.argmin(data, axis, keepdims, exclude) @@ -91,7 +89,6 @@ def sum(data, axis=None, keepdims=False, exclude=False): result : relay.Expr The computed result. """ - return _make.sum(data, axis, keepdims, exclude) @@ -122,7 +119,6 @@ def max(data, axis=None, keepdims=False, exclude=False): result : relay.Expr The computed result. """ - return _make.max(data, axis, keepdims, exclude) @@ -153,7 +149,6 @@ def min(data, axis=None, keepdims=False, exclude=False): result : relay.Expr The computed result. """ - return _make.min(data, axis, keepdims, exclude) @@ -184,7 +179,6 @@ def mean(data, axis=None, keepdims=False, exclude=False): result : relay.Expr The computed result. """ - return _make.mean(data, axis, keepdims, exclude) @@ -215,5 +209,4 @@ def prod(data, axis=None, keepdims=False, exclude=False): result : relay.Expr The computed result. """ - return _make.prod(data, axis, keepdims, exclude) diff --git a/tests/python/relay/test_op_level4.py b/tests/python/relay/test_op_level4.py index 74a2e35d41ab..2dc643cfd7e4 100644 --- a/tests/python/relay/test_op_level4.py +++ b/tests/python/relay/test_op_level4.py @@ -68,7 +68,7 @@ def verify_reduce(test_func, data, axis, keepdims, exclude, output): out_type = "int32" if test_func in [relay.argmin, relay.argmax] else "float32" assert zz.checked_type == relay.ty.TensorType(output, out_type) -def test_reduce(): +def test_reduce_functions(): d1, d2, d3, d4 = tvm.var("d1"), tvm.var("d2"), tvm.var("d3"), tvm.var("d4") for func in [relay.sum, relay.max, @@ -96,6 +96,4 @@ def test_reduce(): test_cmp_type() test_binary_int_broadcast() test_where() - test_arg_reduce() - test_multibox_prior() - test_reduce() + test_reduce_functions()