Skip to content

Use PyObject_GetOptionalAttrString in PyObject_FastGetAttrString when available#164624

Closed
swolchok wants to merge 1 commit intogh/swolchok/850/basefrom
gh/swolchok/850/head
Closed

Use PyObject_GetOptionalAttrString in PyObject_FastGetAttrString when available#164624
swolchok wants to merge 1 commit intogh/swolchok/850/basefrom
gh/swolchok/850/head

Conversation

@swolchok
Copy link
Copy Markdown
Contributor

@swolchok swolchok commented Oct 4, 2025

Stack from ghstack (oldest at bottom):

Python 3.13 added PyObject_GetOptionalAttrString. I'm not 100% certain that it is strictly better than the old approach in all cases, but based on documentation/comments it seems to be meant for this type of use, and it's faster when I profile torchtitan training (which gets to the "check for the __torch_function__ attr on some object" part of maybe_has_torch_function frequently enough to notice, but wastes a bunch of time generating exceptions that we then suppressed here).

… available

Python 3.13 added PyObject_GetOptionalAttrString. I'm not 100% certain that it is strictly better than the old approach in all cases, but based on documentation/comments it seems to be meant for this type of use, and it's faster when I profile torchtitan training (which gets to checking for the __torch_function__ attr on some object frequently enough to notice, but wastes a bunch of time generating exceptions that we then suppressed here).

[ghstack-poisoned]
@pytorch-bot
Copy link
Copy Markdown

pytorch-bot Bot commented Oct 4, 2025

🔗 Helpful Links

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

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

✅ No Failures

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

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


inline py::object PyObject_FastGetAttrString(PyObject* obj, const char* name) {
#if IS_PYTHON_3_13_PLUS
PyObject* res = (PyObject*)nullptr;
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.

Huh, so the cast from nulptr necessary? Probably a holdover from C to C++ copy paste but probably better to save for a file wide cleanup

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.

not sure, but didn't want to wait a full CI round-trip just to find out the old one was there for good reason

@swolchok swolchok added the topic: not user facing topic category label Oct 6, 2025
@swolchok
Copy link
Copy Markdown
Contributor Author

swolchok commented Oct 6, 2025

@pytorchbot merge

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Oct 6, 2025
@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 pushed a commit that referenced this pull request Oct 21, 2025
…164625)

Pybind's API entails a small unnecessary overhead when working with args. (Similarly, we should probably be using vectorcall, but that's a bigger change for both us and pybind11.)

Pull Request resolved: #164625
Approved by: https://github.com/albanD
ghstack dependencies: #164624
Chao1Han pushed a commit to Chao1Han/pytorch that referenced this pull request Oct 21, 2025
… available (pytorch#164624)

Python 3.13 added PyObject_GetOptionalAttrString. I'm not 100% certain that it is strictly better than the old approach in all cases, but based on documentation/comments it seems to be meant for this type of use, and it's faster when I profile torchtitan training (which gets to the "check for the `__torch_function__` attr on some object" part of maybe_has_torch_function frequently enough to notice, but wastes a bunch of time generating exceptions that we then suppressed here).
Pull Request resolved: pytorch#164624
Approved by: https://github.com/Skylion007
Chao1Han pushed a commit to Chao1Han/pytorch that referenced this pull request Oct 21, 2025
…ytorch#164625)

Pybind's API entails a small unnecessary overhead when working with args. (Similarly, we should probably be using vectorcall, but that's a bigger change for both us and pybind11.)

Pull Request resolved: pytorch#164625
Approved by: https://github.com/albanD
ghstack dependencies: pytorch#164624
zhudada0120 pushed a commit to zhudada0120/pytorch that referenced this pull request Oct 22, 2025
…ytorch#164625)

Pybind's API entails a small unnecessary overhead when working with args. (Similarly, we should probably be using vectorcall, but that's a bigger change for both us and pybind11.)

Pull Request resolved: pytorch#164625
Approved by: https://github.com/albanD
ghstack dependencies: pytorch#164624
@github-actions github-actions Bot deleted the gh/swolchok/850/head branch November 6, 2025 02:15
Khanaksahu pushed a commit to Khanaksahu/pytorch that referenced this pull request Nov 17, 2025
… available

Python 3.13 added PyObject_GetOptionalAttrString. I'm not 100% certain that it is strictly better than the old approach in all cases, but based on documentation/comments it seems to be meant for this type of use, and it's faster when I profile torchtitan training (which gets to checking for the __torch_function__ attr on some object frequently enough to notice, but wastes a bunch of time generating exceptions that we then suppressed here).

ghstack-source-id: 2215645
Pull Request resolved: pytorch/pytorch#164624
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 topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants