Skip to content

Conversation

swolchok
Copy link
Contributor

@swolchok swolchok commented Sep 9, 2025

Move the entirety of `__new__` into C++, saving a layer of disable_dynamo and making progress toward all-C++.

[ghstack-poisoned]
Copy link

pytorch-bot bot commented Sep 9, 2025

🔗 Helpful Links

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

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

✅ No Failures

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

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

@pytorch-bot pytorch-bot bot added ciflow/inductor oncall: distributed Add this issue/PR to distributed oncall triage queue labels Sep 9, 2025
swolchok added a commit that referenced this pull request Sep 9, 2025
Move the entirety of `__new__` into C++, saving a layer of disable_dynamo and making progress toward all-C++.

ghstack-source-id: 87f5032
Pull Request resolved: #162508
@swolchok swolchok added the topic: not user facing topic category label Sep 9, 2025
Move the entirety of `__new__` into C++, saving a layer of disable_dynamo and making progress toward all-C++.

cc H-Huang awgu wanchaol fegin fduwjj wz337 wconstab d4l3k pragupta ezyang msaroufim dcci

[ghstack-poisoned]
swolchok added a commit that referenced this pull request Sep 9, 2025
Move the entirety of `__new__` into C++, saving a layer of disable_dynamo and making progress toward all-C++.

ghstack-source-id: e32eed6
Pull Request resolved: #162508
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.

Not really sure what the end goal here is and the level of optimization we're aiming for. But pybind is most likely costing you a lot, especially for things like setting a slot attribute?

extra_dispatch_keys = extra_dispatch_keys.add(c10::DispatchKey::Negative);
}

py::handle spec = py::handle(r.pyobject(2));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does .pyobject() return a new or borrowed reference?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

borrowed -- it just vends from the input args array, which is borrowed.

}

py::handle spec = py::handle(r.pyobject(2));
const auto tensor_meta = spec.attr(dtensor_interned_strings.tensor_meta);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Some error checking from the old code is lost here when this is None?
Also what happens here when the attribute doesn't exist?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

what happens here when the attribute doesn't exist?

PyObject_GetAttr() will set an error, and pybind11 will throw.

Some error checking from the old code is lost here when this is None

you can't get an attribute from None, so this change is safe. it's harder to debug, so I suppose we can TORCH_CHECK here.

Comment on lines +964 to +965
py_tensor.attr(dtensor_interned_strings._spec) = spec;
py_tensor.attr(dtensor_interned_strings._local_tensor) = local_tensor;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I've never seen this before. Is the refcounting acutally correct on this?!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

why wouldn't it be? it would be an obvious bug in pybind11 if this didn't work

@swolchok
Copy link
Contributor Author

swolchok commented Sep 11, 2025

Not really sure what the end goal here is and the level of optimization we're aiming for.

Per discussions with @ezyang, the end goal is C++ DTensor. I've chosen to move toward that incrementally, since 1) it would be better if we don't actually have to stop and do a full rewrite 2) incremental software development is faster in general 3) it is unlikely that the incremental pieces will be entirely different from what we would write if we were sitting down with a blank slate.

But pybind is most likely costing you a lot, especially for things like setting a slot attribute?

pybind11's C++ wrappers for the CPython API seem mostly OK . The cost seems to center around per-bound-C++-function-call overhead. I have a pair of upstream pybind11 PRs that reduce that overhead somewhat waiting for reviews (pybind/pybind11#5824 and pybind/pybind11#5830).

Move the entirety of `__new__` into C++, saving a layer of disable_dynamo and making progress toward all-C++.

cc H-Huang awgu wanchaol fegin fduwjj wz337 wconstab d4l3k pragupta ezyang msaroufim dcci

[ghstack-poisoned]
Move the entirety of `__new__` into C++, saving a layer of disable_dynamo and making progress toward all-C++.

cc H-Huang awgu wanchaol fegin fduwjj wz337 wconstab d4l3k pragupta ezyang msaroufim dcci

[ghstack-poisoned]
Move the entirety of `__new__` into C++, saving a layer of disable_dynamo and making progress toward all-C++.

cc H-Huang awgu wanchaol fegin fduwjj wz337 wconstab d4l3k pragupta ezyang msaroufim dcci

[ghstack-poisoned]
Move the entirety of `__new__` into C++, saving a layer of disable_dynamo and making progress toward all-C++.

cc H-Huang awgu wanchaol fegin fduwjj wz337 wconstab d4l3k pragupta ezyang msaroufim dcci

[ghstack-poisoned]
Move the entirety of `__new__` into C++, saving a layer of disable_dynamo and making progress toward all-C++.

cc H-Huang awgu wanchaol fegin fduwjj wz337 wconstab d4l3k pragupta ezyang msaroufim dcci

[ghstack-poisoned]
@swolchok swolchok added the release notes: distributed (dtensor) release notes category label Sep 17, 2025
@swolchok swolchok removed the topic: not user facing topic category label Sep 17, 2025
Move the entirety of `__new__` into C++, saving a layer of disable_dynamo and making progress toward all-C++.

cc H-Huang awgu wanchaol fegin fduwjj wz337 wconstab d4l3k pragupta ezyang msaroufim dcci

[ghstack-poisoned]
Move the entirety of `__new__` into C++, saving a layer of disable_dynamo and making progress toward all-C++.

cc H-Huang awgu wanchaol fegin fduwjj wz337 wconstab d4l3k pragupta ezyang msaroufim dcci

[ghstack-poisoned]
swolchok added a commit that referenced this pull request Sep 17, 2025
Move the entirety of `__new__` into C++, saving a layer of disable_dynamo and making progress toward all-C++.

ghstack-source-id: e1b6902
Pull Request resolved: #162508
@swolchok swolchok requested review from albanD and ezyang September 18, 2025 20:10
…ly native DTensor.__new__"

Move the entirety of `__new__` into C++, saving a layer of disable_dynamo and making progress toward all-C++.

cc H-Huang awgu wanchaol fegin fduwjj wz337 wconstab d4l3k pragupta ezyang msaroufim dcci

[ghstack-poisoned]
@swolchok
Copy link
Contributor Author

@pytorchbot merge

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

mansiag05 pushed a commit to mansiag05/pytorch that referenced this pull request Sep 22, 2025
Move the entirety of `__new__` into C++, saving a layer of disable_dynamo and making progress toward all-C++.

Pull Request resolved: pytorch#162508
Approved by: https://github.com/ezyang
ghstack dependencies: pytorch#161695
cleonard530 pushed a commit to cleonard530/pytorch that referenced this pull request Sep 22, 2025
Move the entirety of `__new__` into C++, saving a layer of disable_dynamo and making progress toward all-C++.

Pull Request resolved: pytorch#162508
Approved by: https://github.com/ezyang
ghstack dependencies: pytorch#161695
pytorchmergebot pushed a commit that referenced this pull request Sep 23, 2025
…nsor_info (#162968)

Next PR writes a C++ implementation. Seems good to have tests first.

Pull Request resolved: #162968
Approved by: https://github.com/ezyang
ghstack dependencies: #161695, #162508
dsashidh pushed a commit to dsashidh/pytorch that referenced this pull request Sep 26, 2025
Move the entirety of `__new__` into C++, saving a layer of disable_dynamo and making progress toward all-C++.

Pull Request resolved: pytorch#162508
Approved by: https://github.com/ezyang
ghstack dependencies: pytorch#161695
dsashidh pushed a commit to dsashidh/pytorch that referenced this pull request Sep 26, 2025
…nsor_info (pytorch#162968)

Next PR writes a C++ implementation. Seems good to have tests first.

Pull Request resolved: pytorch#162968
Approved by: https://github.com/ezyang
ghstack dependencies: pytorch#161695, pytorch#162508
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ciflow/inductor ciflow/trunk Trigger trunk jobs on your pull request Merged oncall: distributed Add this issue/PR to distributed oncall triage queue release notes: distributed (dtensor) release notes category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants