Skip to content

Enable CPP/CUDAExtension with py_limited_api for python agnosticism#138088

Closed
janeyx99 wants to merge 12 commits into
gh/janeyx99/196/basefrom
gh/janeyx99/196/head
Closed

Enable CPP/CUDAExtension with py_limited_api for python agnosticism#138088
janeyx99 wants to merge 12 commits into
gh/janeyx99/196/basefrom
gh/janeyx99/196/head

Conversation

@janeyx99

@janeyx99 janeyx99 commented Oct 16, 2024

Copy link
Copy Markdown
Contributor

Getting tested with ao, but now there is a real test i added.

What does this PR do?

We want to allow custom PyTorch extensions to be able to build one wheel for multiple Python versions, in other words, achieve python agnosticism. It turns out that there is such a way that setuptools/Python provides already! Namely, if the user promises to use only the Python limited API in their extension, they can pass in py_limited_api to their Extension class and to the bdist_wheel command (with a min python version) in order to build 1 wheel that will suffice across multiple Python versions.

Sounds lovely! Why don't people do that already with PyTorch? Well 2 things. This workflow is hardly documented (even searching for python agnostic specifically does not reveal many answers) so I'd expect that people simply don't know about it. But even if they did, PyTorch custom Extensions would still not work because we always link torch_python, which does not abide by py_limited_api rules.

So this is where this PR comes in! We respect when the user specifies py_limited_api and skip linking torch_python under that condition, allowing users to enroll in the provided functionality I just described.

How do I know this PR works?

I manually tested my silly little ultra_norm locally (with import python_agnostic) and wrote a test case for the extension showing that

  • torch_python doesn't show up in the ldd tree
  • no Py- symbols show up
    It may be a little confusing that our test case is actually python-free (more clean than python-agnostic) but it is sufficient (and not necessary) towards showing that this change works.

Stack from ghstack (oldest at bottom):

@pytorch-bot

pytorch-bot Bot commented Oct 16, 2024

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ No Failures

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

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

janeyx99 added a commit that referenced this pull request Oct 16, 2024
ghstack-source-id: 8eee609
Pull Request resolved: #138088
janeyx99 added a commit that referenced this pull request Oct 24, 2024
ghstack-source-id: 18e0041
Pull Request resolved: #138088
janeyx99 added a commit that referenced this pull request Nov 1, 2024
ghstack-source-id: 60cc2dc
Pull Request resolved: #138088
janeyx99 added a commit that referenced this pull request Nov 5, 2024
ghstack-source-id: 47e19e4
Pull Request resolved: #138088
janeyx99 added a commit that referenced this pull request Nov 7, 2024
ghstack-source-id: e51ed5a
Pull Request resolved: #138088
@janeyx99 janeyx99 added the release notes: python_frontend python frontend release notes category label Nov 7, 2024
@janeyx99 janeyx99 changed the title [DRAFT] do not look--testing cpp_extension no python Enable CPP/CUDAExtension with py_limited_api for python agnosticism Nov 12, 2024
janeyx99 added a commit that referenced this pull request Nov 13, 2024
ghstack-source-id: 228f648
Pull Request resolved: #138088
@janeyx99 janeyx99 requested a review from malfet November 13, 2024 19:44
…nosticism"

Getting tested with ao




[ghstack-poisoned]
@janeyx99 janeyx99 marked this pull request as ready for review November 14, 2024 07:22
@janeyx99 janeyx99 requested review from a team, ezyang, fmassa and soumith as code owners November 14, 2024 07:22
…nosticism"

Getting tested with ao, but now there is a real test i added.




[ghstack-poisoned]
janeyx99 added a commit that referenced this pull request Nov 14, 2024
ghstack-source-id: 8eeab57
Pull Request resolved: #138088
…nosticism"

Getting tested with ao, but now there is a real test i added.




[ghstack-poisoned]
Comment thread torch/utils/cpp_extension.py Outdated
constructor. Full list arguments can be found at
https://setuptools.pypa.io/en/latest/userguide/ext_modules.html#extension-api-reference

Note!

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Shouldn't this be ..note: or something like that?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I wasn't swayed either way, the cudaextension one had a big chunk of text so I stuck with it. but yea, can change

Comment thread torch/utils/cpp_extension.py Outdated
Comment on lines +1033 to +1040
If passing in the ``py_limited_api`` flag signifying that the extension uses
only the Python limited API, CUDAExtension will respect the flag and SKIP
linking torch_python with your custom extension!!!! This means that your
extension CANNOT rely on any APIs provided in torch_python!!!! Notably, your
extension cannot call PYBIND11_MODULE, which is commonly used to create a
Python module associated with the extension. The APIs in torch_python are
blatantly NOT within the Python limited API, which is why we avoid linking
torch_python when the ``py_limited_api`` is specified.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think we could make this shorter and to the point? I feel like there are only 2 ideas here:

  • PyTorch python API (as provided in libtorch_python) cannot be built with py_limited_api
  • When this flag is passed, it is the user responsibility in their library to not use APIs from libtorch_python (in particular pytorch/python bindings) and only use APIs from libtorch (aten objects, operators and registering operations through the dispatcher for access from python).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

oki let me try again!

…nosticism"

Getting tested with ao, but now there is a real test i added.

## What does this PR do?

We want to allow custom PyTorch extensions to be able to build one wheel for multiple Python versions, in other words, achieve python agnosticism. It turns out that there is such a way that setuptools/Python provides already! Namely, if the user promises to use only the Python limited API in their extension, they can pass in `py_limited_api` to their Extension class and to the bdist_wheel command (with a min python version) in order to build 1 wheel that will suffice across multiple Python versions.

Sounds lovely! Why don't people do that already with PyTorch? Well 2 things. This workflow is hardly documented (even searching for python agnostic specifically does not reveal many answers) so I'd expect that people simply don't know about it. But even if they did, _PyTorch_ custom Extensions would still not work because we always link torch_python, which does not abide by py_limited_api rules.

So this is where this PR comes in! We respect when the user specifies py_limited_api and skip linking torch_python under that condition, allowing users to enroll in the provided functionality I just described.

## How do I know this PR works?

I manually tested my silly little ultra_norm locally (with `import python_agnostic`) and wrote a test case for the extension showing that
- torch_python doesn't show up in the ldd tree
- no Py- symbols show up
It may be a little confusing that our test case is actually python-free (more clean than python-agnostic) but it is sufficient (and not necessary) towards showing that this change works.




[ghstack-poisoned]
janeyx99 added a commit that referenced this pull request Dec 11, 2024
ghstack-source-id: dcc0aee
Pull Request resolved: #138088

@albanD albanD left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

SGTM !

…nosticism"

Getting tested with ao, but now there is a real test i added.

## What does this PR do?

We want to allow custom PyTorch extensions to be able to build one wheel for multiple Python versions, in other words, achieve python agnosticism. It turns out that there is such a way that setuptools/Python provides already! Namely, if the user promises to use only the Python limited API in their extension, they can pass in `py_limited_api` to their Extension class and to the bdist_wheel command (with a min python version) in order to build 1 wheel that will suffice across multiple Python versions.

Sounds lovely! Why don't people do that already with PyTorch? Well 2 things. This workflow is hardly documented (even searching for python agnostic specifically does not reveal many answers) so I'd expect that people simply don't know about it. But even if they did, _PyTorch_ custom Extensions would still not work because we always link torch_python, which does not abide by py_limited_api rules.

So this is where this PR comes in! We respect when the user specifies py_limited_api and skip linking torch_python under that condition, allowing users to enroll in the provided functionality I just described.

## How do I know this PR works?

I manually tested my silly little ultra_norm locally (with `import python_agnostic`) and wrote a test case for the extension showing that
- torch_python doesn't show up in the ldd tree
- no Py- symbols show up
It may be a little confusing that our test case is actually python-free (more clean than python-agnostic) but it is sufficient (and not necessary) towards showing that this change works.




[ghstack-poisoned]
janeyx99 added a commit that referenced this pull request Dec 11, 2024
ghstack-source-id: c716a04
Pull Request resolved: #138088
@janeyx99

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
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

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: 1 mandatory check(s) failed. The first few are:

Dig deeper by viewing the failures on hud

Details for Dev Infra team Raised by workflow job

Failing merge rule: Core Maintainers

@janeyx99

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
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

@janeyx99

Copy link
Copy Markdown
Contributor Author

@pytorchbot cherry-pick --onto release/2.6 -c fixnewfeature

This fixes py_limited_api usage for torchao to be able to be python agnostic in their releases, as their releases depend on stable torch to build.

pytorchbot pushed a commit that referenced this pull request Dec 17, 2024
…138088)

Getting tested with ao, but now there is a real test i added.

## What does this PR do?

We want to allow custom PyTorch extensions to be able to build one wheel for multiple Python versions, in other words, achieve python agnosticism. It turns out that there is such a way that setuptools/Python provides already! Namely, if the user promises to use only the Python limited API in their extension, they can pass in `py_limited_api` to their Extension class and to the bdist_wheel command (with a min python version) in order to build 1 wheel that will suffice across multiple Python versions.

Sounds lovely! Why don't people do that already with PyTorch? Well 2 things. This workflow is hardly documented (even searching for python agnostic specifically does not reveal many answers) so I'd expect that people simply don't know about it. But even if they did, _PyTorch_ custom Extensions would still not work because we always link torch_python, which does not abide by py_limited_api rules.

So this is where this PR comes in! We respect when the user specifies py_limited_api and skip linking torch_python under that condition, allowing users to enroll in the provided functionality I just described.

## How do I know this PR works?

I manually tested my silly little ultra_norm locally (with `import python_agnostic`) and wrote a test case for the extension showing that
- torch_python doesn't show up in the ldd tree
- no Py- symbols show up
It may be a little confusing that our test case is actually python-free (more clean than python-agnostic) but it is sufficient (and not necessary) towards showing that this change works.

Pull Request resolved: #138088
Approved by: https://github.com/ezyang, https://github.com/albanD

(cherry picked from commit be27dbf)
@pytorchbot

Copy link
Copy Markdown
Collaborator

Cherry picking #138088

The cherry pick PR is at #143448 and it is recommended to link a fixnewfeature cherry pick PR with an issue. The following tracker issues are updated:

Details for Dev Infra team Raised by workflow job

malfet pushed a commit that referenced this pull request Dec 17, 2024
…143448)

Enable CPP/CUDAExtension with py_limited_api for python agnosticism (#138088)

Getting tested with ao, but now there is a real test i added.

## What does this PR do?

We want to allow custom PyTorch extensions to be able to build one wheel for multiple Python versions, in other words, achieve python agnosticism. It turns out that there is such a way that setuptools/Python provides already! Namely, if the user promises to use only the Python limited API in their extension, they can pass in `py_limited_api` to their Extension class and to the bdist_wheel command (with a min python version) in order to build 1 wheel that will suffice across multiple Python versions.

Sounds lovely! Why don't people do that already with PyTorch? Well 2 things. This workflow is hardly documented (even searching for python agnostic specifically does not reveal many answers) so I'd expect that people simply don't know about it. But even if they did, _PyTorch_ custom Extensions would still not work because we always link torch_python, which does not abide by py_limited_api rules.

So this is where this PR comes in! We respect when the user specifies py_limited_api and skip linking torch_python under that condition, allowing users to enroll in the provided functionality I just described.

## How do I know this PR works?

I manually tested my silly little ultra_norm locally (with `import python_agnostic`) and wrote a test case for the extension showing that
- torch_python doesn't show up in the ldd tree
- no Py- symbols show up
It may be a little confusing that our test case is actually python-free (more clean than python-agnostic) but it is sufficient (and not necessary) towards showing that this change works.

Pull Request resolved: #138088
Approved by: https://github.com/ezyang, https://github.com/albanD

(cherry picked from commit be27dbf)

Co-authored-by: Jane Xu <janeyx@meta.com>
@github-actions github-actions Bot deleted the gh/janeyx99/196/head branch January 18, 2025 02:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk Trigger trunk jobs on your pull request Merged release notes: python_frontend python frontend release notes category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants