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

[numpy] Add a global switch to turn on/off numpy compatibility - #14361

Closed
reminisce wants to merge 2 commits into
apache:numpyfrom
reminisce:env_var_zero_dim_size_tensor
Closed

reminisce wants to merge 2 commits into
apache:numpyfrom
reminisce:env_var_zero_dim_size_tensor

Conversation

@reminisce

Copy link
Copy Markdown
Contributor

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].

  1. 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:

    1. NDArray indexing. When enable=False, NDArray indexing always returns a tensor with ndim >= 1, which is the default behavior of MXNet kept for backward compatibility. When enable=True, NDArray indexing will return a tensor with ndim >= 0, which is consistent with NumPy's indexing behavior. The result tensor with ndim = 0 is actually a scalar whose shape is (). For example:

    from mxnet.base import set_numpy_comp
    x = mx.nd.arange(6)
    x
    [0. 1. 2. 3. 4. 5.]
    <NDArray 6 @cpu(0)>
    x[1]
    [1.]
    <NDArray 1 @cpu(0)>
    set_numpy_comp(enable=True)
    x[1]
    1.0
    <NDArray @cpu(0)>
    set_numpy_comp(enable=False)
    x[1]
    [1.]
    <NDArray 1 @cpu(0)>

    1. NDArray's convenience fluent methods. When enable=True, the convenience fluent methods will dispatch the calls to NumPy operators if implemented in MXNet. For example, given an NDArray data, data.sum() will call mxnet.ndarray.sum(data) when enable=False (default behavior), and mxnet.numpy.sum(data)whenenable=True`.
  2. _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

* Support scalar and zero-size tensors with np.sum

* Add sanity check when ndim is set
@reminisce reminisce added the Numpy label Mar 7, 2019
@reminisce
reminisce requested a review from szha as a code owner March 7, 2019 23:27
@reminisce reminisce added the pr-awaiting-review PR is waiting for code review label Mar 7, 2019
@wkcn

wkcn commented Mar 16, 2019 •

Copy link
Copy Markdown
Member

In my opinion, we can not use the switch. Instead, We can add a typecast from an 1-size NDArray to a scalar.

@reminisce

Copy link
Copy Markdown
Contributor Author

@wkcn Can you elaborate why we cannot use a switch to guarantee backward compatibility and allow users to opt in new numpy compatible behavior?

@wkcn

wkcn commented Mar 17, 2019

Copy link
Copy Markdown
Member

when set_numpy_comp(true), the output of some reduction operators is always a scalsr, breaking the construct of static graph and the deployment.

@wkcn wkcn closed this Mar 17, 2019
@wkcn wkcn reopened this Mar 17, 2019
@wkcn

wkcn commented Mar 17, 2019

Copy link
Copy Markdown
Member

Sorry that I clicked by mistake.

@wkcn

wkcn commented Mar 17, 2019 •

Copy link
Copy Markdown
Member

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 wkcn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I see it.
Thank you for the contribution!
LGTM: )

@abhinavs95

Copy link
Copy Markdown
Contributor

@reminisce Could you please rebase to resolve merge conflicts and fix the CI failures? Thanks

@abhinavs95

Copy link
Copy Markdown
Contributor

@mxnet-label-bot update [Numpy, pr-awaiting-response]

@marcoabreu marcoabreu added pr-awaiting-response PR is reviewed and waiting for contributor to respond and removed pr-awaiting-review PR is waiting for code review labels Mar 27, 2019
@reminisce reminisce closed this Mar 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Numpy pr-awaiting-response PR is reviewed and waiting for contributor to respond

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants