KEMBAR78
Hide torch_python symbols by cyyever · Pull Request #142214 · pytorch/pytorch · GitHub
Skip to content

Conversation

cyyever
Copy link
Collaborator

@cyyever cyyever commented Dec 6, 2024

Change symbols in torch_python to invisible by default on platforms other than Apple.

cc @albanD

@pytorch-bot pytorch-bot bot added the topic: not user facing topic category label Dec 6, 2024
@pytorch-bot
Copy link

pytorch-bot bot commented Dec 6, 2024

🔗 Helpful Links

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

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

✅ You can merge normally! (3 Unrelated Failures)

As of commit 20fae6f with merge base e0bdae7 (image):

BROKEN TRUNK - The following jobs failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

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

@cyyever cyyever marked this pull request as draft December 6, 2024 03:35
@netlify
Copy link

netlify bot commented Dec 6, 2024

Deploy Preview for chimerical-cranachan-793287 ready!

Name Link
🔨 Latest commit 525351ef2565dce6bfbd6fca88d309dee3a7e770
🔍 Latest deploy log https://app.netlify.com/sites/chimerical-cranachan-793287/deploys/675324397306200008c9f2ab
😎 Deploy Preview https://deploy-preview-142214--chimerical-cranachan-793287.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@cyyever cyyever force-pushed the torch_python_abi branch 2 times, most recently from 8b584ca to 525351e Compare December 6, 2024 16:20
@cyyever cyyever changed the title Hide torch_python_symbols Hide torch_python symbols Dec 7, 2024
@cyyever cyyever marked this pull request as ready for review December 7, 2024 05:12
@cyyever cyyever added the ciflow/periodic Trigger jobs ran periodically on master (periodic.yml) on the PR label Dec 8, 2024
@cyyever cyyever requested a review from ezyang December 8, 2024 13:22
@Skylion007
Copy link
Collaborator

What's the point of doing this and why weren't we hiding symbols in it before? Does this guard against any invalidly compiled PyTorch extensions silently failing?

@cyyever
Copy link
Collaborator Author

cyyever commented Dec 9, 2024

What's the point of doing this and why weren't we hiding symbols in it before? Does this guard against any invalidly compiled PyTorch extensions silently failing?

The torch_python exposed all symbols before. Recently it was wrapped into torch_compile_options with default visibility. This PR tries to mark public symbols explicitly. We should have a clear definition of public python API. Some extensions using private symbols will see linker failures.

@janeyx99 janeyx99 added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Dec 9, 2024
@ezyang ezyang requested review from albanD and malfet December 10, 2024 04:40
@albanD
Copy link
Collaborator

albanD commented Dec 10, 2024

I'm not sure how to test this :/
Can you share the PR that makes that visibility change?

@cyyever
Copy link
Collaborator Author

cyyever commented Dec 11, 2024

It is #136743

@ezyang ezyang requested a review from janeyx99 December 11, 2024 15:40
Copy link
Contributor

@ezyang ezyang left a comment

Choose a reason for hiding this comment

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

This is kind of risky but I guess we can give it a try.

if(NOT WIN32)
target_compile_options(torch_python PRIVATE
$<$<COMPILE_LANGUAGE:CXX>: -fvisibility=default>)
endif()
Copy link
Contributor

Choose a reason for hiding this comment

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

@cyyever how come this change undoes the previous change #136743?

And is it intentional that line 313 is commented out but not deleted?

Copy link
Collaborator Author

@cyyever cyyever Dec 12, 2024

Choose a reason for hiding this comment

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

torch_compile_options uses private visibility for torch targets. Line 313 is from merge conflicting that is now solved.

};

extern PyTypeObject THPLayoutType;
TORCH_PYTHON_API extern PyTypeObject THPLayoutType;
Copy link
Contributor

Choose a reason for hiding this comment

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

How did you decide which 3 APIs to start labeling with TORCH_PYTHON_API?

Will this change break extensions that use other APIs?

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 built it locally and fixed all import errors. The 3 APIs were found during that time.

@albanD
Copy link
Collaborator

albanD commented Dec 11, 2024

Curious what is the plan to test this to make sure we didn't miss others? Did you try on a set of third party extensions?

@cyyever
Copy link
Collaborator Author

cyyever commented Dec 12, 2024

Curious what is the plan to test this to make sure we didn't miss others? Did you try on a set of third party extensions?

Tried it on hugging face libraries and torchvison, it works fine.

@cyyever cyyever added the ciflow/s390 s390x-related CI jobs label Dec 12, 2024
@cyyever
Copy link
Collaborator Author

cyyever commented Dec 12, 2024

@pytorchmergebot merge

@pytorch-bot pytorch-bot bot added the ciflow/trunk Trigger trunk jobs on your pull request label Dec 12, 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

@pytorchmergebot
Copy link
Collaborator

Merge failed

Reason: 1 jobs have failed, first few of them are: trunk / macos-py3-arm64 / test (default, 1, 3, macos-m1-stable)

Details for Dev Infra team Raised by workflow job

@cyyever cyyever marked this pull request as ready for review December 16, 2024 00:48
@cyyever
Copy link
Collaborator Author

cyyever commented Dec 16, 2024

@pytorchmergebot merge -i

@pytorchmergebot
Copy link
Collaborator

@albanD
Copy link
Collaborator

albanD commented Dec 18, 2024

I actually would like to argue to revert this one again.
It broke a bunch of things including some internal use case that had to be fixed in #143380 (comment) and it is also breaking the WIP extension at https://github.com/pytorch/pytorch/tree/main/test/cpp_extensions/open_registration_extension (working on enabling it in CI but failing now).

I am afraid there are a lot more of these APIs that would need to be marked and as you mentioned in the other PR, we don't have a good way right now to make sure we caught all of them.
Also @atalman can you confirm this did NOT make it into the release. If it did, we should definitely remove it!

add_library(torch_python SHARED ${TORCH_PYTHON_SRCS})
torch_compile_options(torch_python) # see cmake/public/utils.cmake
if(NOT WIN32)
if(APPLE)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why this check here? Imo symbol visibility on Linux and Mac should be identical, shouldn't it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Apple has some hard to find linking errors that lead to test failures

@malfet
Copy link
Contributor

malfet commented Dec 18, 2024

Wow, I'm surprised we didn't do it before. I'm very supportive of the change, but also a bit scared of the things it could break. (Though, why keep default visibility on Mac?)

Can you confirm this did NOT make it into the release. If it did, we should definitely remove it!
It's not part of the release, according to

git log origin/release/2.6 -- torch/CMakeLists.txt

and https://github.com/pytorch/pytorch/commits/release/2.6/torch/CMakeLists.txt

@suo
Copy link
Member

suo commented Dec 19, 2024

I think this change makes sense directionally, but making a public symbol private is a breaking change so we should be careful about it. I think it would be good to revert and come up with a plan for how to roll it out.

@cyyever
Copy link
Collaborator Author

cyyever commented Dec 19, 2024

Another way is to gradually discover those symbols and mark them public.

@albanD
Copy link
Collaborator

albanD commented Dec 19, 2024

The problem with this approach @cyyever is that users based on the release will have a 3months delay where they're unable to use PyTorch until the next release comes out. Which is really not acceptable.

@cyyever
Copy link
Collaborator Author

cyyever commented Dec 19, 2024

@albanD Fully understand, seems that it should be reverted.

@atalman
Copy link
Contributor

atalman commented Dec 19, 2024

@albanD @malfet I confirm this is not part of the release 2.6:
https://github.com/pytorch/pytorch/blob/release/2.6/torch/CMakeLists.txt#L313

pytorchmergebot pushed a commit that referenced this pull request Mar 7, 2025
Fixes #148208. There are solutions for exposing symbols implicitly from inline functions (i.e., inline function A calls non-inline function B in foo.h. Code includes foo.h has to see the symbol B in DLL).

Solution 1: tag the entire struct where the inline functions are defined as member functions with TORCH_PYTHON_API --- this PR does this for python_arg_parser.h. An alternative solution exists but will slow down dispatching a lot --- drop inline keyword and move implementation to .cc file.

Solution 2: tag individual functions with TORCH_PYTHON_API. This PR does this for python_tensor.h.

Related discussion about hiding torch_python symbols: #142214

Pull Request resolved: #148213
Approved by: https://github.com/malfet
@malfet malfet added topic: bc breaking topic category module: python frontend For issues relating to PyTorch's Python frontend and removed topic: not user facing topic category labels Apr 9, 2025
@janeyx99 janeyx99 added the release notes: cpp release notes category label Apr 9, 2025
@cyyever cyyever deleted the torch_python_abi branch May 14, 2025 01:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci-no-td Do not run TD on this PR ciflow/periodic Trigger jobs ran periodically on master (periodic.yml) on the PR ciflow/s390 s390x-related CI jobs ciflow/trunk Trigger trunk jobs on your pull request Merged module: python frontend For issues relating to PyTorch's Python frontend open source release notes: cpp release notes category Reverted topic: bc breaking topic category 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.