Skip to content

Commit

Permalink
fix np.clip scalar input case (apache#17788)
Browse files Browse the repository at this point in the history
  • Loading branch information
xidulu authored and sxjscience committed Jul 1, 2020
1 parent 4e7c4f3 commit 2c7c746
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions python/mxnet/numpy/multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -6174,6 +6174,11 @@ def clip(a, a_min, a_max, out=None):
>>> np.clip(a, 3, 6, out=a)
array([3., 3., 3., 3., 4., 5., 6., 6., 6., 6.], dtype=float32)
"""
from numbers import Number
if isinstance(a, Number):
# In case input is a scalar, the computation would fall back to native numpy.
# The value returned would be a python scalar.
return _np.clip(a, a_min, a_max, out=None)
return _mx_nd_np.clip(a, a_min, a_max, out=out)


Expand Down
10 changes: 10 additions & 0 deletions tests/python/unittest/test_numpy_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -3644,6 +3644,16 @@ def __init__(self, a_min=None, a_max=None):

def hybrid_forward(self, F, x):
return x.clip(self._a_min, self._a_max)

# Test scalar case
for _, a_min, a_max, throw_exception in workloads:
a = _np.random.uniform() # A scalar
if throw_exception:
# No need to test the exception case here.
continue
mx_ret = np.clip(a, a_min, a_max)
np_ret = _np.clip(a, a_min, a_max)
assert_almost_equal(mx_ret, np_ret, atol=1e-4, rtol=1e-3, use_broadcast=False)

for shape, a_min, a_max, throw_exception in workloads:
for dtype in dtypes:
Expand Down

0 comments on commit 2c7c746

Please sign in to comment.