10000 DOC: Initialize numpy compatibility note by HaoZeke · Pull Request #71688 · pytorch/pytorch · GitHub
[go: up one dir, main page]

Skip to content

DOC: Initialize numpy compatibility note #71688

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 11 commits into from
Closed

Conversation

HaoZeke
Copy link
Collaborator
@HaoZeke HaoZeke commented Jan 24, 2022

Fixes #48628, #46829.

@rgommers

@pytorch-bot
Copy link
pytorch-bot bot commented Jan 24, 2022
CI Flow Status

⚛️ CI Flow

Ruleset - Version: v1
Ruleset - File: https://github.com/HaoZeke/pytorch/blob/9f3d9f6ce1d42806197fb10500a3086a25a3e39a/.github/generated-ciflow-ruleset.json
PR ciflow labels: ciflow/default
Add ciflow labels to this PR to trigger more builds:

Workflows Labels (bold enabled) Status
Triggered Workflows
linux-binary-conda ciflow/binaries, ciflow/binaries_conda, ciflow/default ✅ triggered
linux-binary-libtorch-cxx11-abi ciflow/binaries, ciflow/binaries_libtorch, ciflow/default ✅ triggered
linux-binary-libtorch-pre-cxx11 ciflow/binaries, ciflow/binaries_libtorch, ciflow/default ✅ triggered
linux-binary-manywheel ciflow/binaries, ciflow/binaries_wheel, ciflow/default ✅ triggered
linux-bionic-py3.7-clang9 ciflow/all, ciflow/cpu, ciflow/default, ciflow/linux, ciflow/noarch, ciflow/trunk, ciflow/xla ✅ triggered
linux-docs ciflow/all, ciflow/cpu, ciflow/default, ciflow/docs, ciflow/linux, ciflow/trunk ✅ triggered
linux-vulkan-bionic-py3.7-clang9 ciflow/all, ciflow/cpu, ciflow/default, ciflow/linux, ciflow/trunk, ciflow/vulkan ✅ triggered
linux-xenial-cuda11.3-py3.7-gcc7 ciflow/all, ciflow/cuda, ciflow/default, ciflow/linux, ciflow/trunk ✅ triggered
linux-xenial-cuda11.3-py3.7-gcc7-bazel-test ciflow/all, ciflow/bazel, ciflow/cpu, ciflow/default, ciflow/linux, ciflow/trunk ✅ triggered
linux-xenial-py3-clang5-mobile-build ciflow/all, ciflow/default, ciflow/linux, ciflow/mobile, ciflow/trunk ✅ triggered
linux-xenial-py3-clang5-mobile-custom-build-static ciflow/all, ciflow/default, ciflow/linux, ciflow/mobile, ciflow/trunk ✅ triggered
linux-xenial-py3.7-clang7-asan ciflow/all, ciflow/cpu, ciflow/default, ciflow/linux, ciflow/sanitizers, ciflow/trunk ✅ triggered
linux-xenial-py3.7-clang7-onnx ciflow/all, ciflow/cpu, ciflow/default, ciflow/linux, ciflow/onnx, ciflow/trunk ✅ triggered
linux-xenial-py3.7-gcc5.4 ciflow/all, ciflow/cpu, ciflow/default, ciflow/linux, ciflow/trunk ✅ triggered
linux-xenial-py3.7-gcc7 ciflow/all, ciflow/cpu, ciflow/default, ciflow/linux, ciflow/trunk ✅ triggered
linux-xenial-py3.7-gcc7-no-ops ciflow/all, ciflow/cpu, ciflow/default, ciflow/linux, ciflow/trunk ✅ triggered
pytorch-linux-xenial-py3-clang5-android-ndk-r19c-gradle-custom-build-single ciflow/all, ciflow/android, ciflow/cpu, ciflow/default, ciflow/linux, ciflow/trunk ✅ triggered
pytorch-linux-xenial-py3-clang5-android-ndk-r19c-gradle-custom-build-single-full-jit ciflow/all, ciflow/android, ciflow/cpu, ciflow/default, ciflow/linux, ciflow/trunk ✅ triggered
win-vs2019-cpu-py3 ciflow/all, ciflow/cpu, ciflow/default, ciflow/trunk, ciflow/win ✅ triggered
win-vs2019-cuda11.3-py3 ciflow/all, ciflow/cuda, ciflow/default, ciflow/trunk, ciflow/win ✅ triggered
Skipped Workflows
caffe2-linux-xenial-py3.7-gcc5.4 ciflow/all, ciflow/cpu, ciflow/linux, ciflow/trunk 🚫 skipped
docker-builds ciflow/all, ciflow/trunk 🚫 skipped
ios-12-5-1-arm64 ciflow/all, ciflow/ios, ciflow/macos, ciflow/trunk 🚫 skipped
ios-12-5-1-arm64-coreml ciflow/all, ciflow/ios, ciflow/macos, ciflow/trunk 🚫 skipped
ios-12-5-1-arm64-custom-ops ciflow/all, ciflow/ios, ciflow/macos, ciflow/trunk 🚫 skipped
ios-12-5-1-arm64-full-jit ciflow/all, ciflow/ios, ciflow/macos, ciflow/trunk 🚫 skipped
ios-12-5-1-arm64-metal ciflow/all, ciflow/ios, ciflow/macos, ciflow/trunk 🚫 skipped
ios-12-5-1-x86-64 ciflow/all, ciflow/ios, ciflow/macos, ciflow/trunk 🚫 skipped
ios-12-5-1-x86-64-coreml ciflow/all, ciflow/ios, ciflow/macos, ciflow/trunk 🚫 skipped
ios-12-5-1-x86-64-full-jit ciflow/all, ciflow/ios, ciflow/macos, ciflow/trunk 🚫 skipped
libtorch-linux-xenial-cuda10.2-py3.7-gcc7 ciflow/all, ciflow/cuda, ciflow/libtorch, ciflow/linux, ciflow/trunk 🚫 skipped
libtorch-linux-xenial-cuda11.3-py3.7-gcc7 ciflow/all, ciflow/cuda, ciflow/libtorch, ciflow/linux, ciflow/trunk 🚫 skipped
linux-bionic-cuda10.2-py3.9-gcc7 ciflow/all, ciflow/cuda, ciflow/linux, ciflow/slow, ciflow/trunk 🚫 skipped
linux-bionic-rocm4.5-py3.7 ciflow/all, ciflow/linux, ciflow/rocm, ciflow/trunk 🚫 skipped
linux-docs-push ciflow/all, ciflow/cpu, ciflow/linux, ciflow/scheduled 🚫 skipped
linux-xenial-cuda11.3-py3.7-gcc7-no-ops ciflow/all, ciflow/cuda, ciflow/linux, ciflow/trunk 🚫 skipped
macos-10-15-py3-arm64 ciflow/all, ciflow/macos, ciflow/trunk 🚫 skipped
macos-10-15-py3-lite-interpreter-x86-64 ciflow/all, ciflow/macos, ciflow/trunk 🚫 skipped
macos-11-py3-x86-64 ciflow/all, ciflow/macos, ciflow/trunk 🚫 skipped
parallelnative-linux-xenial-py3.7-gcc5.4 ciflow/all, ciflow/cpu, ciflow/linux, ciflow/trunk 🚫 skipped
periodic-libtorch-linux-bionic-cuda11.5-py3.7-gcc7 ciflow/all, ciflow/cuda, ciflow/libtorch, ciflow/linux, ciflow/scheduled 🚫 skipped
periodic-libtorch-linux-xenial-cuda11.1-py3.7-gcc7 ciflow/all, ciflow/cuda, ciflow/libtorch, ciflow/linux, ciflow/scheduled 🚫 skipped
periodic-linux-bionic-cuda11.5-py3.7-gcc7 ciflow/all, ciflow/cuda, ciflow/linux, ciflow/scheduled 🚫 skipped
periodic-linux-xenial-cuda10.2-py3-gcc7-slow-gradcheck ciflow/all, ciflow/cuda, ciflow/linux, ciflow/scheduled, ciflow/slow, ciflow/slow-gradcheck 🚫 skipped
periodic-linux-xenial-cuda11.1-py3.7-gcc7-debug ciflow/all, ciflow/cuda, ciflow/linux, ciflow/scheduled 🚫 skipped
periodic-win-vs2019-cuda11.1-py3 ciflow/all, ciflow/cuda, ciflow/scheduled, ciflow/win 🚫 skipped
periodic-win-vs2019-cuda11.5-py3 ciflow/all, ciflow/cuda, ciflow/scheduled, ciflow/win 🚫 skipped
pytorch-linux-xenial-py3-clang5-android-ndk-r19c-build ciflow/all, ciflow/android, ciflow/cpu, ciflow/linux, ciflow/trunk 🚫 skipped

@facebook-github-bot
Copy link
Contributor

Hi @HaoZeke!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@fb.com. Thanks!

@facebook-github-bot
Copy link
Contributor
facebook-github-bot commented Jan 24, 2022

🔗 Helpful links

❌ 1 Flaky Failures

As of commit 40af0c7 (more details on the Dr. CI page):

Expand to see more

None of the CI failures appear to be your fault 💚



❄️ 1 failure tentatively classified as flaky

but reruns have not yet been triggered to confirm:

See GitHub Actions build pull / linux-focal-py3.7-gcc7 / test (docs_test, 1, 1, linux.2xlarge) (1/1)

Step: "Test" (full log | diagnosis details | 🔁 rerun) ❄️

2022-06-23T19:10:13.9666960Z SyntaxError: unexpected EOF while parsing
2022-06-23T19:10:13.9662946Z     with torch.no_grad():
2022-06-23T19:10:13.9663142Z Exception raised:
2022-06-23T19:10:13.9663341Z     Traceback (most recent call last):
2022-06-23T19:10:13.9663617Z       File "/opt/conda/lib/python3.7/doctest.py", line 1337, in __run
2022-06-23T19:10:13.9664011Z         compileflags, 1), test.globs)
2022-06-23T19:10:13.9664612Z       File "/opt/conda/lib/python3.7/site-packages/sphinx/ext/doctest.py", line 476, in compile
2022-06-23T19:10:13.9665702Z         return compile(code, name, self.type, flags, dont_inherit)
2022-06-23T19:10:13.9666171Z       File "<doctest default[3]>", line 1
2022-06-23T19:10:13.9666545Z         with torch.no_grad():
2022-06-23T19:10:13.9666749Z                             ^
2022-06-23T19:10:13.9666960Z     SyntaxError: unexpected EOF while parsing
2022-06-23T19:10:13.9773552Z **********************************************************************
2022-06-23T19:10:13.9773936Z 1 items had failures:
2022-06-23T19:10:13.9774289Z    1 of  70 in default
2022-06-23T19:10:13.9774540Z 70 tests in 1 items.
2022-06-23T19:10:13.9774721Z 69 passed and 1 failed.
2022-06-23T19:10:13.9774922Z ***Test Failed*** 1 failures.
2022-06-23T19:10:14.5678485Z 
2022-06-23T19:10:14.5678865Z Document: jit_language_reference_v2
2022-06-23T19:10:14.5679433Z -----------------------------------
2022-06-23T19:10:14.5863561Z 1 items passed all tests:

This comment was automatically generated by Dr. CI (expand for details).

Please report bugs/suggestions to the (internal) Dr. CI Users group.

Click here to manually regenerate this comment.

@HaoZeke HaoZeke marked this pull request as draft January 24, 2022 00:13
@rgommers rgommers self-requested a review January 24, 2022 08:52
@HaoZeke HaoZeke force-pushed the numpyDoc branch 5 times, most recently from 6679cd3 to 935c282 Compare January 25, 2022 22:16
Copy link
Collaborator
@rgommers rgommers left a comment

Choose a reason for hiding this comment

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

Thanks @HaoZeke, this is a good start.

A section "Using NumPy functions directly on PyTorch tensors" still seems to be missing. (concrete example: np.sin(t) works. let's explain why). The reductions special-case mentioned in gh-48628 is also missing.

Also note #46829 (comment). Here you use + as the operator, but it'd be good to copy that example and mention that numpy may behave unexpectedly for different operators.

@HaoZeke HaoZeke force-pushed the numpyDoc branch 4 times, most recently from e0ca917 to 218d0a1 Compare January 31, 2022 00:56
@HaoZeke HaoZeke marked this pull request as ready for review January 31, 2022 00:57
@HaoZeke HaoZeke requested a review from rgommers January 31, 2022 00:57
@mrshenli mrshenli added module: docs Related to our documentation, both in docs/ and docblocks module: numpy Related to numpy support, and also numpy compatibility of our operators triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module labels Jan 31, 2022
@mrshenli mrshenli requested a review from mruberry January 31, 2022 03:50
@HaoZeke HaoZeke force-pushed the numpyDoc branch 5 times, most recently from 8a45130 to dd86ad9 Compare February 4, 2022 17:51
@facebook-github-bot
Copy link
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!


**TL;DR:**

- Be explicit about obtaining and using NumPy arrays
Copy link
Collaborator

Choose a reason for hiding this comment

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

Having now read the document, I like being clear about converting between torch tensors and NumPy arrays. I think we should just tell people if they want to call NumPy operations on PyTorch tensors how they can do that (like creating a NumPy view of the data and running in a no grad context). We should let people know that PyTorch has a very "NumPy compatible" API and try to note the biggest divergences. We should talk about our Python Array API support (which is still in development) and let the user know if they can acquire the Python array API namespace from a torch tensor (not at this time).

@rgommers
Copy link
Collaborator

@HaoZeke the build-docs job is failing because of

/var/lib/jenkins/workspace/docs/source/notes/numpy_compatibility.rst:50: WARNING: undefined label: :std:doc:reference/generated/numpy.shares_memory

warnings get turned into errors in CI. That's why there's no working link to the rendered docs here. Can you just put it in double backticks if it's not easy to figure out? The reference isn't essential I'd say.

@HaoZeke HaoZeke force-pushed the numpyDoc branch 2 times, most recently from c78a030 to 6ba3626 Compare March 15, 2022 15:55
@svekars svekars requested a review from mruberry May 5, 2022 20:35
pytorchmergebot pushed a commit that referenced this pull request Jun 6, 2022
**Reopened** to help with merge issues. See #59790 for full context.

Fixes #20778. Helps #71688.

Finalizes @martinPasen's force argument for `Tensor.numpy()`. It is set to False by default. If it's set to True then we:
1. detatch the Tensor, if requires_grad == True
2. move to cpu, if not on cpu already
3. Uses .resolve_conj() if .is_conj() == True
4. Uses .resolve_neg() if .is_neg() == True

cc @albanD
Pull Request resolved: #78564
Approved by: https://github.com/albanD
facebook-github-bot pushed a commit that referenced this pull request Jun 7, 2022
Summary:
**Reopened** to help with merge issues. See #59790 for full context.

Fixes #20778. Helps #71688.

Finalizes martinPasen's force argument for `Tensor.numpy()`. It is set to False by default. If it's set to True then we:
1. detatch the Tensor, if requires_grad == True
2. move to cpu, if not on cpu already
3. Uses .resolve_conj() if .is_conj() == True
4. Uses .resolve_neg() if .is_neg() == True

cc albanD

Pull Request resolved: #78564
Approved by: https://github.com/albanD

Test Plan: contbuild & OSS CI, see https://hud.pytorch.org/commit/pytorch/pytorch/3f58dd18dc6fc18ed82fb1632cea48373c0a7798

Reviewed By: seemethere

Differential Revision: D36935606

Pulled By: seemethere

fbshipit-source-id: dc2dd7f569feb8da29add55db3d1625241ff8d77
HaoZeke and others added 8 commits June 23, 2022 19:23
Also address most review comments, add doctests

Co-authored-by: Ralf Gommers <ralf.gommers@gmail.com>
Co-authored-by: Ralf Gommers <ralf.gommers@gmail.com>
Co-authored-by: Mike Ruberry <mruberry@fb.com>
Co-authored-by: Ralf Gommers <ralf.gommers@gmail.com>
Co-authored-by: Mike Ruberry <mruberry@fb.com>
Co-authored-by: Ralf Gommers <ralf.gommers@gmail.com>
@HaoZeke
Copy link
Collaborator Author
HaoZeke commented Jun 23, 2022

@rgommers and @mruberry, this has now been revised in light of #59790 being merged.

Does it read alright?

Copy link
Collaborator
@rgommers rgommers left a comment

Choose a reason for hiding this comment

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

This now recommends in a couple of places to use .numpy(force=True), which is not a good idea. That feature is something that is meant for a use case like described in one of the issues: Napari wants to provide generic support for array/tensor libraries, and as a visualization library doesn't need to worry about performance as much or the implications of implicit device transfer or .detach(). For the average user on the other hand, we don't want to teach such bad habits and sweep all the details under the rug.

This still doesn't read well, it still feels like a long list of weird corner cases. Concrete example of that: there's a red warning box near the top with "Do not use id to check for shared memory", which just feels like a big distraction to the story (I have never seen any user do this in practice).

Most of the content is there, it just needs to be polished up with a focus on teaching, and hiding all the corner cases as notes in dropdown boxes instead of giving them center stage.

@HaoZeke I propose to put this on hold for now, for two reasons: (1) after multiple iterations it still feels off the mark, and (2) there are higher-prio things you have started on (primtorch). With a few hours to refactor this I think it's mergable, but that's probably something @mruberry or I should do.

@mruberry's main review comment from before was:

"Having now read the document, I like being clear about converting between torch tensors and NumPy arrays. I think we should just tell people if they want to call NumPy operations on PyTorch tensors how they can do that (like creating a NumPy view of the data and running in a no grad context). We should let people know that PyTorch has a very "NumPy compatible" API and try to note the biggest divergences. We should talk about our Python Array API support (which is still in development) and let the user know if they can acquire the Python array API namespace from a torch tensor (not at this time)."

It doesn't feel like that was addressed.

@github-actions
Copy link
Contributor

Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as Stale.
Feel free to remove the Stale label if you feel this was a mistake.
If you are unable to remove the Stale label please contact a maintainer in order to do so.
If you want the bot to never mark this PR stale again, add the no-stale label.
Stale pull requests will automatically be closed after 30 days of inactivity.

@github-actions github-actions bot added the Stale label Aug 28, 2022
@rgommers
Copy link
Collaborator

I will close this for now. The content here can be reused in case someone is picking up the linked doc issue.

Thanks for the effort @HaoZeke and @mruberry

@rgommers rgommers closed this Aug 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla signed module: docs Related to our documentation, both in docs/ and docblocks module: numpy Related to numpy support, and also numpy compatibility of our operators open source Stale triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add docs on PyTorch - NumPy interaction
7 participants
0