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

Commit

Permalink
[Op] Fix reshape and mean (#20058)
Browse files Browse the repository at this point in the history
    Operator mean falls back to old api
    Operator reshape falls back to old api as it occurs twice in mx.nd.np.all
    Remove reverse keyword in np.reshape to achieve numpy compatibility. Please use npx.reshape if reverse is needed.
  • Loading branch information
barry-jin committed Mar 22, 2021
1 parent 4d706e8 commit 5722f8b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
12 changes: 10 additions & 2 deletions python/mxnet/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,7 @@ def write_all_str(module_file, module_all_list):

_NP_OP_PREFIX = '_np_'
_NP_OP_SUBMODULE_LIST = ['_random_', '_linalg_']
_NP_OP_IMPLEMENTED_SET = {'_np_reshape'}

_NP_EXT_OP_PREFIX = '_npx_'
_NP_EXT_OP_SUBMODULE_LIST = ['_image_', '_random_']
Expand Down Expand Up @@ -850,12 +851,15 @@ def _init_np_op_module(root_module_name, np_module_name, mx_module_name, make_op
if np_module_name == 'numpy':
op_name_prefix = _NP_OP_PREFIX
submodule_name_list = _NP_OP_SUBMODULE_LIST
op_implemented_set = _NP_OP_IMPLEMENTED_SET
elif np_module_name == 'numpy_extension':
op_name_prefix = _NP_EXT_OP_PREFIX
submodule_name_list = _NP_EXT_OP_SUBMODULE_LIST
op_implemented_set = set()
elif np_module_name == 'numpy._internal':
op_name_prefix = _NP_INTERNAL_OP_PREFIX
submodule_name_list = []
op_implemented_set = set()
else:
raise ValueError('unsupported np module name {}'.format(np_module_name))

Expand All @@ -865,8 +869,12 @@ def _init_np_op_module(root_module_name, np_module_name, mx_module_name, make_op
op_names = []
for i in range(size.value):
name = py_str(plist[i])
if name.startswith(op_name_prefix):
op_names.append(name)
if mx_module_name != 'symbol':
if name.startswith(op_name_prefix) and name not in op_implemented_set:
op_names.append(name)
else:
if name.startswith(op_name_prefix):
op_names.append(name)

if mx_module_name is None:
# register np/npx ops for imperative programming
Expand Down
4 changes: 2 additions & 2 deletions python/mxnet/ndarray/numpy/_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -9476,7 +9476,7 @@ def cumsum(a, axis=None, dtype=None, out=None):
return _api_internal.cumsum(a, axis, dtype, out)

@set_module('mxnet.ndarray.numpy')
def reshape(a, newshape, reverse=False, order='C'):
def reshape(a, newshape, order='C'):
"""
Gives a new shape to an array without changing its data.
This function always returns a copy of the input array if
Expand Down Expand Up @@ -9537,7 +9537,7 @@ def reshape(a, newshape, reverse=False, order='C'):
[3., 4.],
[5., 6.]])
"""
return _api_internal.reshape(a, newshape, reverse, order)
return _api_internal.reshape(a, newshape, False, order)

@set_module('mxnet.ndarray.numpy')
def moveaxis(a, source, destination):
Expand Down
12 changes: 6 additions & 6 deletions python/mxnet/numpy/multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ def __getitem__(self, key):
if not unsupported:
new_shape += (-4,)
sliced = _npi.slice(self, begin, end, step)
return _npi.reshape(sliced, new_shape)
return _mx_nd_np.reshape(sliced, new_shape)

# Special handling for cases only supported in imperative mode
if dc.is_deferred_compute():
Expand Down Expand Up @@ -1635,9 +1635,9 @@ def reshape(self, *args, **kwargs): # pylint: disable=arguments-differ
if len(args) == 0:
raise TypeError('reshape() takes exactly 1 argument (0 given)')
if len(args) == 1 and isinstance(args[0], tuple):
return _mx_np_op.reshape(self, newshape=args[0], order=order)
return _mx_nd_np.reshape(self, newshape=args[0], order=order)
else:
return _mx_np_op.reshape(self, newshape=args, order=order)
return _mx_nd_np.reshape(self, newshape=args, order=order)

def reshape_like(self, *args, **kwargs):
"""Convenience fluent method for :py:func:`reshape_like`.
Expand Down Expand Up @@ -7620,7 +7620,7 @@ def mean(a, axis=None, dtype=None, out=None, keepdims=False): # pylint: disable
>>> np.mean(a, dtype=np.float64)
array(0.55, dtype=float64)
"""
return _npi.mean(a, axis=axis, dtype=dtype, keepdims=keepdims, out=out)
return _mx_nd_np.mean(a, axis=axis, dtype=dtype, keepdims=keepdims, out=out)
# pylint: enable=redefined-outer-name


Expand Down Expand Up @@ -11810,7 +11810,7 @@ def cumsum(a, axis=None, dtype=None, out=None):
return _mx_nd_np.cumsum(a, axis=axis, dtype=dtype, out=out)

@set_module('mxnet.numpy')
def reshape(a, newshape, reverse, order='C'):
def reshape(a, newshape, order='C'):
"""
Gives a new shape to an array without changing its data.
This function always returns a copy of the input array if
Expand Down Expand Up @@ -11871,7 +11871,7 @@ def reshape(a, newshape, reverse, order='C'):
[3., 4.],
[5., 6.]])
"""
return _mx_nd_np.reshape(a, newshape, reverse, order)
return _mx_nd_np.reshape(a, newshape, order)

@set_module('mxnet.numpy')
def moveaxis(a, source, destination):
Expand Down
2 changes: 2 additions & 0 deletions src/api/operator/numpy/np_broadcast_reduce_op_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ MXNET_REGISTER_API("_npi.mean")
op::NumpyReduceAxesParam param;
if (args[1].type_code() == kNull) {
param.axis = dmlc::optional<mxnet::Tuple<int>>();
} else if (args[1].type_code() == kDLInt) {
param.axis = Tuple<int>(1, args[1].operator int64_t());
} else {
param.axis = mxnet::Tuple<int>(args[1].operator ObjectRef());
}
Expand Down

0 comments on commit 5722f8b

Please sign in to comment.