Repository navigation
Conversation
* Support scalar and zero-size tensors with np.sum * Add sanity check when ndim is set
|
In my opinion, we can not use the switch. Instead, We can add a typecast from an 1-size NDArray to a scalar. |
|
@wkcn Can you elaborate why we cannot use a switch to guarantee backward compatibility and allow users to opt in new numpy compatible behavior? |
|
when set_numpy_comp(true), the output of some reduction operators is always a scalsr, breaking the construct of static graph and the deployment. |
|
Sorry that I clicked by mistake. |
|
when set_numpy_comp(True) is called, the output of some reduction operators Is always a scalar, which break the construct of computational graph, and not benefit for deployment. Example: x = mx.nd.arange(6)
x
[0. 1. 2. 3. 4. 5.]
<NDArray 6 @cpu(0)>
x[1]
[1.]
<NDArray 1 @cpu(0)>
float(x[1])
1.0
# 1-dim NDArray
mx.nd.sum(x)
[15.]
<NDArray 1 @cpu(0)>
# 0-dim NDArray
mx.numpy.sum(x)
15.0
<NDArray 1 @cpu(0)>
# built-in float
float(mx.numpy.sum(x))
15.0 |
wkcn
left a comment
There was a problem hiding this comment.
I see it.
Thank you for the contribution!
LGTM: )
|
@reminisce Could you please rebase to resolve merge conflicts and fix the CI failures? Thanks |
|
@mxnet-label-bot update [Numpy, pr-awaiting-response] |
This PR is implemented based upon the discussion with @eric-haibin-lin. It provides two APIs and NumPy-compatible NDArray indexing behavior, i.e.
y = x[i].set_numpy_comp(enable)This is a thread-safe utility function for users to enable or disable NumPy-compatible behaviors in MXNet. This will generally affect the behavior of the following two operations:
enable=False, NDArray indexing always returns a tensor withndim >= 1, which is the default behavior of MXNet kept for backward compatibility. Whenenable=True, NDArray indexing will return a tensor withndim >= 0, which is consistent with NumPy's indexing behavior. The result tensor withndim = 0is actually a scalar whose shape is(). For example:enable=True, the convenience fluent methods will dispatch the calls to NumPy operators if implemented in MXNet. For example, given an NDArraydata,data.sum()will callmxnet.ndarray.sum(data)whenenable=False(default behavior), and mxnet.numpy.sum(data)whenenable=True`._is_numpy_comp()This is a thread-safe utility function for checking whether NumPy-compatibility has been enabled or disabled. This is implemented for developers to use. Users are not expected to call this function.
@junrushao1994 @eric-haibin-lin @szha @zheng-da @yzhliu