From b8f5edba6704650e9e6351b05a7d9dbc328020ec Mon Sep 17 00:00:00 2001 From: Przemek Tredak Date: Fri, 8 Feb 2019 10:49:20 -0800 Subject: [PATCH 1/4] Relaxing types for slice_like op --- src/operator/tensor/matrix_op.cc | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/operator/tensor/matrix_op.cc b/src/operator/tensor/matrix_op.cc index e5d354be629f..847e8d0ff7a9 100644 --- a/src/operator/tensor/matrix_op.cc +++ b/src/operator/tensor/matrix_op.cc @@ -661,7 +661,16 @@ Example:: return std::vector{"data", "shape_like"}; }) .set_attr("FInferShape", SliceLikeShape) -.set_attr("FInferType", ElemwiseType<2, 1>) +.set_attr("FInferType", [](const nnvm::NodeAttrs& attrs, + std::vector *in_attrs, + std::vector *out_attrs) { + CHECK_EQ(in_attrs->size(), 2) << " in operator " << attrs.name; + std::vector checked_in_attrs = { (*in_attrs)[0] }; + bool ret = !type_is_none((*in_attrs)[1]) && + ElemwiseType<1,1>(attrs, &checked_in_attrs, out_attrs); + (*in_attrs)[0] = checked_in_attrs[0]; + return ret; + }) .set_attr("FGradient", ElemwiseGradUseNone{"_backward_slice_like"}) .set_attr("FCompute", SliceLikeForward) .add_argument("data", "NDArray-or-Symbol", "Source input") From d8d9aff53507fefe4986a08e520f7066f6788074 Mon Sep 17 00:00:00 2001 From: Przemek Tredak Date: Fri, 8 Feb 2019 11:24:03 -0800 Subject: [PATCH 2/4] Added test --- tests/python/unittest/test_operator.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/python/unittest/test_operator.py b/tests/python/unittest/test_operator.py index 7b5b9ebf3be4..154859ba5976 100644 --- a/tests/python/unittest/test_operator.py +++ b/tests/python/unittest/test_operator.py @@ -2515,6 +2515,20 @@ def test_slice_like(): assert_allclose(xx, xgrad.asnumpy()) assert_allclose(xgrad1.asnumpy(), mx.nd.zeros_like(xgrad1).asnumpy()) +@with_seed() +def test_slice_like_different_types(): + x = [[ 1., 2., 3., 4.], + [ 5., 6., 7., 8.], + [ 9., 10., 11., 12.]] + + y = [[ 0., 0., 0.], + [ 0., 0., 0.]] + + x = mx.nd.array(x) + y = mx.nd.array(y).astype('int32') + z = mx.nd.slice_like(x, y).asnumpy() + assert_allclose(z.asnumpy(), [[1,2,3],[5,6,7]]) + @with_seed() def test_flip(): for ndim in range(1, 6): From 2011735d8c365140c503736791b30cf4ce1dea13 Mon Sep 17 00:00:00 2001 From: Przemek Tredak Date: Fri, 8 Feb 2019 14:20:19 -0800 Subject: [PATCH 3/4] Fix typo in test --- tests/python/unittest/test_operator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python/unittest/test_operator.py b/tests/python/unittest/test_operator.py index 154859ba5976..08dd1d514fe9 100644 --- a/tests/python/unittest/test_operator.py +++ b/tests/python/unittest/test_operator.py @@ -2526,7 +2526,7 @@ def test_slice_like_different_types(): x = mx.nd.array(x) y = mx.nd.array(y).astype('int32') - z = mx.nd.slice_like(x, y).asnumpy() + z = mx.nd.slice_like(x, y) assert_allclose(z.asnumpy(), [[1,2,3],[5,6,7]]) @with_seed() From 1645101fed0b5eb470a30fec2b56fdb5f49d48d2 Mon Sep 17 00:00:00 2001 From: Przemek Tredak Date: Fri, 8 Feb 2019 20:17:27 -0800 Subject: [PATCH 4/4] Fix lint --- src/operator/tensor/matrix_op.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/operator/tensor/matrix_op.cc b/src/operator/tensor/matrix_op.cc index 847e8d0ff7a9..3a244ac89790 100644 --- a/src/operator/tensor/matrix_op.cc +++ b/src/operator/tensor/matrix_op.cc @@ -667,7 +667,7 @@ Example:: CHECK_EQ(in_attrs->size(), 2) << " in operator " << attrs.name; std::vector checked_in_attrs = { (*in_attrs)[0] }; bool ret = !type_is_none((*in_attrs)[1]) && - ElemwiseType<1,1>(attrs, &checked_in_attrs, out_attrs); + ElemwiseType<1, 1>(attrs, &checked_in_attrs, out_attrs); (*in_attrs)[0] = checked_in_attrs[0]; return ret; })