You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2018/09/05 04:00:10 UTC

[GitHub] haojin2 closed pull request #12458: Add extra check for illegal case in reshape

haojin2 closed pull request #12458: Add extra check for illegal case in reshape
URL: https://github.com/apache/incubator-mxnet/pull/12458
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/src/c_api/c_api.cc b/src/c_api/c_api.cc
index 1ef3f0fca9f..80ed6468159 100644
--- a/src/c_api/c_api.cc
+++ b/src/c_api/c_api.cc
@@ -452,6 +452,7 @@ MXNET_DLL int MXNDArrayReshape64(NDArrayHandle handle,
   CHECK_GT(arr->shape().Size(), 0) << "Source ndarray's shape is undefined. Input shape: "
     << arr->shape();
   TShape new_shape = mxnet::op::InferReshapeShape(shape, arr->shape(), reverse);
+  mxnet::op::CheckSizeSame(arr->shape(), new_shape);
   *ptr = arr->ReshapeWithRecord(new_shape);
   *out = ptr;
   API_END_HANDLE_ERROR(delete ptr);
diff --git a/src/operator/tensor/matrix_op-inl.h b/src/operator/tensor/matrix_op-inl.h
index 78e1fa1d9c6..5e44c9468db 100644
--- a/src/operator/tensor/matrix_op-inl.h
+++ b/src/operator/tensor/matrix_op-inl.h
@@ -69,6 +69,13 @@ struct ReshapeParam : public dmlc::Parameter<ReshapeParam> {
   }
 };
 
+inline void CheckSizeSame(const TShape& a, const TShape& b) {
+  CHECK_EQ(a.Size(), b.Size())
+    << "Target shape size is different to source. "
+    << "Target: " << b
+    << "\nSource: " << a;
+}
+
 template<typename IType>
 inline TShape InferReshapeShape(const nnvm::Tuple<IType>& shape,
                                 const TShape& dshape, bool reverse) {
@@ -208,10 +215,7 @@ inline bool ReshapeShape(const nnvm::NodeAttrs& attrs,
     return (*out_attrs)[0].ndim() && ReverseReshapeInferShape(&(*in_attrs)[0], (*out_attrs)[0]);
   }
   ReverseReshapeInferShape(&dshape, oshape);
-  CHECK_EQ(oshape.Size(), dshape.Size())
-    << "Target shape size is different to source. "
-    << "Target: " << oshape
-    << "\nSource: " << dshape;
+  CheckSizeSame(dshape, oshape);
   SHAPE_ASSIGN_CHECK(*out_attrs, 0, oshape);
   return ReverseReshapeInferShape(&(*in_attrs)[0], (*out_attrs)[0]);
 }
diff --git a/tests/python/unittest/test_operator.py b/tests/python/unittest/test_operator.py
index ca358ef02b1..3c6554dfe09 100644
--- a/tests/python/unittest/test_operator.py
+++ b/tests/python/unittest/test_operator.py
@@ -2109,6 +2109,10 @@ def test_reshape_new(src_shape, shape_args, reverse, dst_shape):
                 'Output Shape = %s' %(str(holdout_src_shape), str(shape_args), str(reverse),
                                       str(dst_shape), str(output_shape[0]))
 
+    def test_invalid_reshape():
+        data = mx.nd.arange(16).reshape((4, 4))
+        assert_exception(mx.nd.reshape, MXNetError, data, (1, -2))
+
     # Test new api (Using shape)
     test_cases = [
         [(2, 3, 5, 5),  (0, -1),          False, (2, 75)],
@@ -2157,6 +2161,9 @@ def test_reshape_new(src_shape, shape_args, reverse, dst_shape):
     exe.backward(out_grads=[mx.nd.array(out_grad_npy, ctx=default_context())])
     assert_allclose(exe.grad_arrays[0].asnumpy(), out_grad_npy.reshape((5, 4, 3, 7)))
 
+    # Test for invalid cases
+    test_invalid_reshape()
+
 
 @with_seed()
 def test_reshape_like():


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services