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

[numpy] fix mix numpy scalar and MXNet numpy ndarray #18313

Merged
merged 1 commit into from
May 19, 2020

Conversation

yijunc
Copy link
Contributor

@yijunc yijunc commented May 14, 2020

Description

Attempt to resolve issue mentioned in #16743
It is done by casting the inputs into mx_np or np.
Current conversion rules:

Expression a type b type out type
a += b onp mx_np onp
a += b mx_np onp mx_np
c = a + b onp mx_np onp
c = a + b mx_np onp mx_np

However, the example code in the issue still can't run due to type issue.

import mxnet as mx
import numpy as np
mx.npx.set_np()
a = np.array(1) // the type in np is int64
a += mx.np.array(1) // the type in mx_np is float32

Checklist

Essentials

Please feel free to remove inapplicable items for your PR.

  • The PR title starts with [MXNET-$JIRA_ID], where $JIRA_ID refers to the relevant JIRA issue created (except PRs with tiny changes)
  • Changes are complete (i.e. I finished coding on this PR)
  • All changes have test coverage:
  • Unit tests are added for small changes to verify correctness (e.g. adding a new operator)
  • Nightly tests are added for complicated/long-running ones (e.g. changing distributed kvstore)
  • Build tests will be added for build configuration changes (e.g. adding a new build option with NCCL)
  • Code is well-documented:
  • For user-facing API changes, API doc string has been updated.
  • For new C++ functions in header files, their functionalities and arguments are documented.
  • For new examples, README.md is added to explain the what the example does, the source of the dataset, expected performance on test set and reference to the original paper if applicable
  • Check the API doc at https://mxnet-ci-doc.s3-accelerate.dualstack.amazonaws.com/PR-$PR_ID/$BUILD_ID/index.html
  • To the best of my knowledge, examples are either not affected by this change, or have been fixed to be compatible with this change

Changes

  • Feature1, tests, (and when applicable, API doc)
  • Feature2, tests, (and when applicable, API doc)

Comments

  • If this change is a backward incompatible change, why must this change be made.
  • Interesting edge cases to note here

@yijunc yijunc requested a review from szha as a code owner May 14, 2020 05:28
@mxnet-bot
Copy link

Hey @BenjaminCHEN2016 , Thanks for submitting the PR
All tests are already queued to run once. If tests fail, you can trigger one or more tests again with the following commands:

  • To trigger all jobs: @mxnet-bot run ci [all]
  • To trigger specific jobs: @mxnet-bot run ci [job1, job2]

CI supported jobs: [windows-gpu, centos-cpu, unix-cpu, website, windows-cpu, miscellaneous, centos-gpu, sanity, edge, clang, unix-gpu]


Note:
Only following 3 categories can trigger CI :PR Author, MXNet Committer, Jenkins Admin.
All CI tests must pass before the PR can be merged.

@yijunc yijunc force-pushed the fix_issue_16743 branch 5 times, most recently from 96b2362 to 5235944 Compare May 14, 2020 08:07
@hgt312
Copy link
Contributor

hgt312 commented May 14, 2020

@yzhliu @sxjscience Any thoughts on the conversion rules?

@yijunc yijunc force-pushed the fix_issue_16743 branch 2 times, most recently from 9a309cb to 2356ee0 Compare May 14, 2020 08:35
@sxjscience
Copy link
Member

import jax.numpy as jnp
import numpy as onp
# jnp += onp
a = jnp.ones((10,))
b = onp.ones((10,))
a += b
print(type(a))

# onp += jnp
a = onp.ones((10,))
b = jnp.ones((10,))
a += b
print(type(a))

Output:

<class 'jax.interpreters.xla.DeviceArray'>
<class 'jax.interpreters.xla.DeviceArray'>

@sxjscience
Copy link
Member

In Jax, if any ndarray is a Jax ndarray, the result will also be a Jax ndarray.

@hgt312
Copy link
Contributor

hgt312 commented May 14, 2020

Jax does not implement inplace op overload like __iadd__. It is not a real inplace operation.
In my opinion, inplace operation should keep its type, while the other should return a mx ndarray.

@sxjscience
Copy link
Member

Jax does not implement inplace op overload like __iadd__. It is not a real inplace operation.
In my opinion, inplace operation should keep its type, while the other should return a mx ndarray.

I agree with that.

@yijunc
Copy link
Contributor Author

yijunc commented May 15, 2020

Conversion rules:

Expression a type b type out type
a += b onp mx_np onp
a += b mx_np onp mx_np
c = a + b onp mx_np mx_np
c = a + b mx_np onp mx_np

@yijunc yijunc changed the title [Fix][Numpy][WIP] fix mix numpy scalar and MXNet numpy ndarray [numpy] fix mix numpy scalar and MXNet numpy ndarray May 15, 2020
@mxnet-bot
Copy link

Undefined action detected.
Permissible actions are : run ci [all], run ci [job1, job2]
Example : @mxnet-bot run ci [all]
Example : @mxnet-bot run ci [centos-cpu, clang]

1 similar comment
@mxnet-bot
Copy link

Undefined action detected.
Permissible actions are : run ci [all], run ci [job1, job2]
Example : @mxnet-bot run ci [all]
Example : @mxnet-bot run ci [centos-cpu, clang]

@yijunc
Copy link
Contributor Author

yijunc commented May 17, 2020

@mxnet-bot run ci [miscellaneous, unix-cpu]

@mxnet-bot
Copy link

Jenkins CI successfully triggered : [miscellaneous, unix-cpu]

@yijunc
Copy link
Contributor Author

yijunc commented May 18, 2020

@mxnet-bot run ci [windows-cpu, windows-gpu, centos-cpu]

@mxnet-bot
Copy link

Jenkins CI successfully triggered : [centos-cpu, windows-gpu, windows-cpu]

@hgt312
Copy link
Contributor

hgt312 commented May 18, 2020

LGTM now. @yzhliu

@yijunc
Copy link
Contributor Author

yijunc commented May 18, 2020

@mxnet-bot run ci [centos-cpu]

@mxnet-bot
Copy link

Jenkins CI successfully triggered : [centos-cpu]

@yijunc
Copy link
Contributor Author

yijunc commented May 18, 2020

@mxnet-bot run ci [unix-gpu]

@mxnet-bot
Copy link

Jenkins CI successfully triggered : [unix-gpu]

@yijunc
Copy link
Contributor Author

yijunc commented May 18, 2020

@mxnet-bot run ci [unix-gpu]

@mxnet-bot
Copy link

Jenkins CI successfully triggered : [unix-gpu]

@yijunc
Copy link
Contributor Author

yijunc commented May 19, 2020

@sxjscience Any thoughts on this pr?

@sxjscience sxjscience merged commit b214477 into apache:master May 19, 2020
AntiZpvoh pushed a commit to AntiZpvoh/incubator-mxnet that referenced this pull request Jul 6, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants