Skip to content

[autograd] Thread-safe Python wrapping for Node#179767

Open
colesbury wants to merge 11 commits intogh/colesbury/6/basefrom
gh/colesbury/6/head
Open

[autograd] Thread-safe Python wrapping for Node#179767
colesbury wants to merge 11 commits intogh/colesbury/6/basefrom
gh/colesbury/6/head

Conversation

@colesbury
Copy link
Copy Markdown
Member

@colesbury colesbury commented Apr 8, 2026

Stack from ghstack (oldest at bottom):

Replace the raw PyObject* on Node with an atomic PyObjectSlot
and use the same strategy that we already use for Tensor and Storage
PyObject wrappers.

Authored with Claude.

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

pytorch-bot Bot commented Apr 8, 2026

🔗 Helpful Links

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

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

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

✅ No Failures

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

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

colesbury added a commit that referenced this pull request Apr 8, 2026
Replace the raw PyObject* on Node with an atomic PyObjectSlot
and use the same strategy that we already use for Tensor and Storage
PyObject wrappers.

Authored with Claude.


ghstack-source-id: e467af4
Pull-Request: #179767
@pytorch-bot
Copy link
Copy Markdown

pytorch-bot Bot commented Apr 8, 2026

This PR needs a release notes: label

If your changes are user facing and intended to be a part of release notes, please use a label starting with release notes:.

If not, please add the topic: not user facing label.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "topic: not user facing"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

@colesbury colesbury added the topic: not user facing topic category label Apr 8, 2026
@colesbury
Copy link
Copy Markdown
Member Author

Got Claude to run some benchmarks on this stack of PRs and didn't see a noticeable change, maybe a tiny bit faster for a few cases, but importantly doesn't look like a regression anywhere:

https://gist.github.com/colesbury/8ac150f3d6e43d3adff2f888e9aeb5f5

[ghstack-poisoned]
colesbury added a commit that referenced this pull request Apr 8, 2026
Replace the raw PyObject* on Node with an atomic PyObjectSlot
and use the same strategy that we already use for Tensor and Storage
PyObject wrappers.

Authored with Claude.

ghstack-source-id: 5fa9478
Pull-Request: #179767
[ghstack-poisoned]
colesbury added a commit that referenced this pull request Apr 9, 2026
Replace the raw PyObject* on Node with an atomic PyObjectSlot
and use the same strategy that we already use for Tensor and Storage
PyObject wrappers.

Authored with Claude.

ghstack-source-id: 872d766
Pull-Request: #179767
[ghstack-poisoned]
colesbury added a commit that referenced this pull request Apr 9, 2026
Replace the raw PyObject* on Node with an atomic PyObjectSlot
and use the same strategy that we already use for Tensor and Storage
PyObject wrappers.

Authored with Claude.

ghstack-source-id: 265cdaf
Pull-Request: #179767
[ghstack-poisoned]
colesbury added a commit that referenced this pull request Apr 9, 2026
Replace the raw PyObject* on Node with an atomic PyObjectSlot
and use the same strategy that we already use for Tensor and Storage
PyObject wrappers.

Authored with Claude.

ghstack-source-id: 165b906
Pull-Request: #179767
[ghstack-poisoned]
colesbury added a commit that referenced this pull request Apr 9, 2026
Replace the raw PyObject* on Node with an atomic PyObjectSlot
and use the same strategy that we already use for Tensor and Storage
PyObject wrappers.

Authored with Claude.

ghstack-source-id: 176276e
Pull-Request: #179767
colesbury added a commit to colesbury/pytorch that referenced this pull request Apr 10, 2026
Replace the raw PyObject* on Node with an atomic PyObjectSlot
and use the same strategy that we already use for Tensor and Storage
PyObject wrappers.

Authored with Claude.

ghstack-source-id: 176276e
Pull-Request: pytorch#179767
colesbury added a commit to colesbury/pytorch that referenced this pull request Apr 10, 2026
Replace the raw PyObject* on Node with an atomic PyObjectSlot
and use the same strategy that we already use for Tensor and Storage
PyObject wrappers.

Authored with Claude.

ghstack-source-id: 176276e
Pull-Request: pytorch#179767
[ghstack-poisoned]
[ghstack-poisoned]
Copy link
Copy Markdown
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.

Sounds good!


// Enable PyObject support for intrusive_ptr<Node> so that the refcount
// machinery calls incref_pyobject/decref_pyobject on transitions.
namespace c10::detail {
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.

Claude seems to take offence at this one:

  • Missing #ifndef C10_MOBILE guard on TargetTraits specialization —
    torch/csrc/autograd/node.h:707-715. Both TensorImpl.h:3096 and StorageImpl.h:407 wrap their
    TargetTraits specialization in #ifndef C10_MOBILE. The intrusive_ptr refcount machinery relies on
    this: in mobile builds, has_pyobject paths are disabled and guarded by
    TORCH_INTERNAL_ASSERT_DEBUG_ONLY(!detail::has_pyobject(combined)) (see intrusive_ptr.h:1191). If
    node.h is ever transitively included in a mobile build, can_have_pyobject = true for Node would fire
    that assertion. Even if autograd headers aren't used on mobile today, the guard should be present for
    consistency and safety.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'll add the C10_MOBILE ifdef guard

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

Sounds good!

[ghstack-poisoned]
[ghstack-poisoned]
pytorchmergebot pushed a commit that referenced this pull request Apr 21, 2026
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.14.0) (oldest at bottom):

* #179767
* -> #179766
* #179765
* #179764

Replace std::shared_ptr<Node> with c10::intrusive_ptr<Node> throughout
the autograd system. Node now inherits from c10::intrusive_ptr_target
instead of std::enable_shared_from_this<Node>.

Authored with Claude.

Pull Request resolved: #179766
Approved by: https://github.com/albanD
ghstack dependencies: #179764, #179765
Replace the raw PyObject* on Node with an atomic PyObjectSlot
and use the same strategy that we already use for Tensor and Storage
PyObject wrappers.

Authored with Claude.

[ghstack-poisoned]
colesbury added a commit that referenced this pull request Apr 22, 2026
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.14.0) (oldest at bottom):

* #179767
* -> #179766
* #179765
* #179764

Replace std::shared_ptr<Node> with c10::intrusive_ptr<Node> throughout
the autograd system. Node now inherits from c10::intrusive_ptr_target
instead of std::enable_shared_from_this<Node>.

Authored with Claude.

cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx kadeng chauhang amjames Lucaskabela jataylo azahed98 xmfan aditvenk

[ghstack-poisoned]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants