Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Relaxing type requirements for slice_like op #14097

Merged
merged 4 commits into from
Feb 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/operator/tensor/matrix_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,16 @@ Example::
return std::vector<std::string>{"data", "shape_like"};
})
.set_attr<nnvm::FInferShape>("FInferShape", SliceLikeShape)
.set_attr<nnvm::FInferType>("FInferType", ElemwiseType<2, 1>)
.set_attr<nnvm::FInferType>("FInferType", [](const nnvm::NodeAttrs& attrs,
std::vector<int> *in_attrs,
std::vector<int> *out_attrs) {
CHECK_EQ(in_attrs->size(), 2) << " in operator " << attrs.name;
std::vector<int> 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<nnvm::FGradient>("FGradient", ElemwiseGradUseNone{"_backward_slice_like"})
.set_attr<FCompute>("FCompute<cpu>", SliceLikeForward<cpu>)
.add_argument("data", "NDArray-or-Symbol", "Source input")
Expand Down
14 changes: 14 additions & 0 deletions tests/python/unittest/test_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though this example looks good, I am afraid it is specific. Do you mind using randomized shape, size and NDarray initalization to ensure this is generic?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those parameters do not matter for this test (and they are already tested in the dedicated test of slice_like op functionality) - the values for x and y were taken from the example from slice_like documentation. The only thing that matters is the type of y in this test. Before this PR this code would not work - it would throw an exception during type inference. With this PR, this code works (as it should).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright. Makes sense. Thanks for the explanation.

[ 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)
assert_allclose(z.asnumpy(), [[1,2,3],[5,6,7]])

@with_seed()
def test_flip():
for ndim in range(1, 6):
Expand Down