Skip to content

Conversation

lezcano
Copy link
Collaborator

@lezcano lezcano commented Nov 27, 2023

Stack from ghstack (oldest at bottom):

A less general version of this wrapper was used in the keynote on
torch.compile(numpy). We expose a generic version of the wrapper
that works seamlessly with torch.compile.

cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @peterbell10 @ipiszy @yf225 @chenyang78 @kadeng @muchulee8 @aakhundov @ColinPeppler

A less general version of this wrapper was used in the keynote on
`torch.compile(numpy)`. We expose a generic version of the wrapper
that works seamlessly with `torch.compile`.

[ghstack-poisoned]
Copy link

pytorch-bot bot commented Nov 27, 2023

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/114610

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit 79c2403 with merge base a81edf9 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@lezcano
Copy link
Collaborator Author

lezcano commented Nov 27, 2023

Tagging a few reviewers as this PR exposes a new API to the public.
This API allows the user to execute NumPy code on CUDA / compute gradients through NumPy code, use NumPy code with torch.func, so I think it is well worth to have it in core and start advertising it.

… function"

A less general version of this wrapper was used in the keynote on
`torch.compile(numpy)`. We expose a generic version of the wrapper
that works seamlessly with `torch.compile`.

cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx peterbell10 ipiszy yf225 chenyang78 kadeng muchulee8 aakhundov ColinPeppler

[ghstack-poisoned]
… function"

A less general version of this wrapper was used in the keynote on
`torch.compile(numpy)`. We expose a generic version of the wrapper
that works seamlessly with `torch.compile`.

cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx peterbell10 ipiszy yf225 chenyang78 kadeng muchulee8 aakhundov ColinPeppler

[ghstack-poisoned]
… function"

A less general version of this wrapper was used in the keynote on
`torch.compile(numpy)`. We expose a generic version of the wrapper
that works seamlessly with `torch.compile`.

cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx peterbell10 ipiszy yf225 chenyang78 kadeng muchulee8 aakhundov ColinPeppler

[ghstack-poisoned]
lezcano added a commit that referenced this pull request Nov 27, 2023
A less general version of this wrapper was used in the keynote on
`torch.compile(numpy)`. We expose a generic version of the wrapper
that works seamlessly with `torch.compile`.

ghstack-source-id: 105ddf6
Pull Request resolved: #114610
@lezcano lezcano requested a review from soumith November 27, 2023 22:20
.. code-block:: python
@torch.compile
@torch.compiler.wrap_fn
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this (and below) be wrap_np? Doesn't seem to match code.

… function"

A less general version of this wrapper was used in the keynote on
`torch.compile(numpy)`. We expose a generic version of the wrapper
that works seamlessly with `torch.compile`.

cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx peterbell10 ipiszy yf225 chenyang78 kadeng muchulee8 aakhundov ColinPeppler

[ghstack-poisoned]
lezcano added a commit that referenced this pull request Nov 28, 2023
A less general version of this wrapper was used in the keynote on
`torch.compile(numpy)`. We expose a generic version of the wrapper
that works seamlessly with `torch.compile`.

ghstack-source-id: d0259ae
Pull Request resolved: #114610
Copy link
Contributor

@jansel jansel left a comment

Choose a reason for hiding this comment

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

Code changes look good to me, though I'd like others to weigh in on if this should be a public API and what it should be called.

@albanD
Copy link
Collaborator

albanD commented Dec 5, 2023

I'm not sure the semantic of this transformation is very clear to me and when I should use it?

Maybe adding type annotation to the functions will make it easier to see what happens?

@lezcano
Copy link
Collaborator Author

lezcano commented Dec 11, 2023

The type annotation would be Callable[Any, Any] -> Callable[Any, Any], so I don't think it would add all that much...

@albanD
Copy link
Collaborator

albanD commented Dec 11, 2023

I mean in terms of Tensor vs array.

… function"

A less general version of this wrapper was used in the keynote on
`torch.compile(numpy)`. We expose a generic version of the wrapper
that works seamlessly with `torch.compile`.

cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx peterbell10 ipiszy yf225 chenyang78 kadeng muchulee8 aakhundov ColinPeppler

[ghstack-poisoned]
lezcano added a commit that referenced this pull request Dec 22, 2023
A less general version of this wrapper was used in the keynote on
`torch.compile(numpy)`. We expose a generic version of the wrapper
that works seamlessly with `torch.compile`.

ghstack-source-id: 5b49d4d
Pull Request resolved: #114610
@lezcano
Copy link
Collaborator Author

lezcano commented Dec 22, 2023

Sorry, I completely dropped the ball on this one. Added type annotations to all functions. It's not 100% clear to me whether this makes things simpler or not, given that the decorator changes the types, but yeah...

@ezyang ezyang removed their request for review January 1, 2024 01:42
Copy link
Collaborator

@albanD albanD left a comment

Choose a reason for hiding this comment

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

I think that there is a pretty big constraint here that we end up with a function that can ONLY be used in the context of compile-full-graph and will NOT work in eager.
This is a new enough concept that I think we should say it explicitly in the faq and docstring.

… function"

A less general version of this wrapper was used in the keynote on
`torch.compile(numpy)`. We expose a generic version of the wrapper
that works seamlessly with `torch.compile`.

cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx peterbell10 ipiszy yf225 chenyang78 kadeng muchulee8 aakhundov ColinPeppler

[ghstack-poisoned]
lezcano added a commit that referenced this pull request Jan 2, 2024
A less general version of this wrapper was used in the keynote on
`torch.compile(numpy)`. We expose a generic version of the wrapper
that works seamlessly with `torch.compile`.

ghstack-source-id: 43cce18
Pull Request resolved: #114610
@lezcano
Copy link
Collaborator Author

lezcano commented Jan 2, 2024

Addressed the review. In particular:

  • I have streamlined the examples in the FAQ, using only wrap_numpy as it's the preferred approach
  • I have added a paragraph discussing graph breaks explicitly.
  • I have added a note discussing graph breaks explicitly to the docs.
    • Note that the docs are not currently rendered, as the torch.compiler page of the docs is currently being used as a page linking to other pages.

to avoid all these rather expensive memory copies. To do so, we just need
to tweak our ``numpy_fn`` so that it accepts cuda Tensors and returns tensors:
to tweak our ``numpy_fn`` so that it accepts cuda Tensors and returns tensors.
We can do so by using ``torch.compiler.wrap_numpy``:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks for the update, it looks quite good.
Should this piece be split into two questions in the FAQ?

  • How to speed up NumPy code with torch.compile?
    • Use the fast cpu backend by enabling torch.compile
    • Run things on CUDA
  • How to use NumPy code within your PyTorch code?
    • The wrap_numpy function

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think splitting it is too much given how little information there is. I will add one example where we simply run things on CPU, implementing your first point "Use the fast cpu backend by enabling torch.compile"

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Nevermind, after writing it I chose to split it into two sections, although they are different to the ones you proposed.

… function"

A less general version of this wrapper was used in the keynote on
`torch.compile(numpy)`. We expose a generic version of the wrapper
that works seamlessly with `torch.compile`.

cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx peterbell10 ipiszy yf225 chenyang78 kadeng muchulee8 aakhundov ColinPeppler

[ghstack-poisoned]
lezcano added a commit that referenced this pull request Jan 2, 2024
A less general version of this wrapper was used in the keynote on
`torch.compile(numpy)`. We expose a generic version of the wrapper
that works seamlessly with `torch.compile`.

ghstack-source-id: 2e17422
Pull Request resolved: #114610
Copy link
Collaborator

@albanD albanD left a comment

Choose a reason for hiding this comment

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

Thanks for the update!

@lezcano
Copy link
Collaborator Author

lezcano commented Jan 2, 2024

@pytorchbot merge

@pytorch-bot pytorch-bot bot added the ciflow/trunk Trigger trunk jobs on your pull request label Jan 2, 2024
@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@facebook-github-bot facebook-github-bot deleted the gh/Lezcano/256/head branch January 6, 2024 15:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants