Repository navigation
Getattr access for subclasses in pre-dispatch - #143671
tugsbayasgalan wants to merge 5 commits into
Conversation
[ghstack-poisoned]
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/143671
Note: Links to docs will display an error until the docs builds have been completed. This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
@tugsbayasgalan has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
This is a draft PR that tries to prototype how to capture attribute access in pre-dispatch IR. The motivating use case is: https://github.com/pytorch/ao/blob/039cef4ad546716aa04cd54c461feb173f7fe403/tutorials/developer_api_guide/export_to_executorch.py#L54 where TorchAO overrides Embedding.weight with a tensor subclass and then do attribute access inside it. We have to solve this problem in both strict and non-strict because even when dynamo translates subclass.inner_tensor into a fx Graph, the underlying tracer that converts torch IR to aten IR will need to handle subclass.inner_tensor as well. I think there are roughly two ways to implement this: 1) Override getattr on tensor in export to monkey patch inner tensors as properties and add torch function handler 2) When we first see a subclass tensor in make_fx, attach proxy to its' call inner tensors. I tried implementing (1) here: #143518, but it turned out to be quite intrusive. So I prefer (2) for now. Only downside for (2) is we are creating proxy for unnecessarily many inner tensors but i think it is ok because: 1) torch.compile will never see tensor subclass in make_fx so it won't regress any tracing speed 2) Hopefully tensor subclasses that are used in export are not that nest-y. I also noticed a small bug in tensor subclass unwrapping logic. cc IvanKobzarev cc ezyang SherlockNoMad EikanWang jgong5 wenzhe-nrv [ghstack-poisoned]
|
@tugsbayasgalan has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
|
@tugsbayasgalan has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
1 similar comment
|
@tugsbayasgalan has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
This is a draft PR that tries to prototype how to capture attribute access in pre-dispatch IR. The motivating use case is: https://github.com/pytorch/ao/blob/039cef4ad546716aa04cd54c461feb173f7fe403/tutorials/developer_api_guide/export_to_executorch.py#L54 where TorchAO overrides Embedding.weight with a tensor subclass and then do attribute access inside it. We have to solve this problem in both strict and non-strict because even when dynamo translates subclass.inner_tensor into a fx Graph, the underlying tracer that converts torch IR to aten IR will need to handle subclass.inner_tensor as well. I think there are roughly two ways to implement this: 1) Override getattr on tensor in export to monkey patch inner tensors as properties and add torch function handler 2) When we first see a subclass tensor in make_fx, attach proxy to its' call inner tensors. I tried implementing (1) here: #143518, but it turned out to be quite intrusive. So I prefer (2) for now. Only downside for (2) is we are creating proxy for unnecessarily many inner tensors but i think it is ok because: 1) torch.compile will never see tensor subclass in make_fx so it won't regress any tracing speed 2) Hopefully tensor subclasses that are used in export are not that nest-y. I also noticed a small bug in tensor subclass unwrapping logic. cc IvanKobzarev cc ezyang SherlockNoMad EikanWang jgong5 wenzhe-nrv Differential Revision: [D67531713](https://our.internmc.facebook.com/intern/diff/D67531713) [ghstack-poisoned]
|
@tugsbayasgalan has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
| todo = todo[:-num_children] | ||
| rebuilt = tp.__tensor_unflatten__(d, meta, None, None) # type: ignore[attr-defined] | ||
| todo.append(rebuilt) | ||
| todo.insert(0, rebuilt) |
There was a problem hiding this comment.
Let's say you have TwoTensor(TwoTensor_a, TwoTensor_b), then the way we put stuff into rebuilt queue looks like: [TwoTensor_b, TwoTensor_a, TwoTensor]. As a result, when we first build TwoTensor_b and if we put it in the end of the todos(before my PR), the original structure is lost as it would like sth like: [plain_tensor, plain_tensor, TwoTensor_b] and when we build TwoTensor_a, it will think plain_tensor and TwoTensor_b are the elements of TwoTensor_a. So the new structure would like sth like: TwoTensor(plain_tensor, TwoTensor(plain_tensor, TwoTensor))
This is a draft PR that tries to prototype how to capture attribute access in pre-dispatch IR. The motivating use case is: https://github.com/pytorch/ao/blob/039cef4ad546716aa04cd54c461feb173f7fe403/tutorials/developer_api_guide/export_to_executorch.py#L54 where TorchAO overrides Embedding.weight with a tensor subclass and then do attribute access inside it. We have to solve this problem in both strict and non-strict because even when dynamo translates subclass.inner_tensor into a fx Graph, the underlying tracer that converts torch IR to aten IR will need to handle subclass.inner_tensor as well. I think there are roughly two ways to implement this: 1) Override getattr on tensor in export to monkey patch inner tensors as properties and add torch function handler 2) When we first see a subclass tensor in make_fx, attach proxy to its' call inner tensors. I tried implementing (1) here: #143518, but it turned out to be quite intrusive. So I prefer (2) for now. Only downside for (2) is we are creating proxy for unnecessarily many inner tensors but i think it is ok because: 1) torch.compile will never see tensor subclass in make_fx so it won't regress any tracing speed 2) Hopefully tensor subclasses that are used in export are not that nest-y. I also noticed a small bug in tensor subclass unwrapping logic. cc IvanKobzarev cc ezyang SherlockNoMad EikanWang jgong5 wenzhe-nrv Differential Revision: [D67531713](https://our.internmc.facebook.com/intern/diff/D67531713) [ghstack-poisoned]
This is a draft PR that tries to prototype how to capture attribute access in pre-dispatch IR. The motivating use case is: https://github.com/pytorch/ao/blob/039cef4ad546716aa04cd54c461feb173f7fe403/tutorials/developer_api_guide/export_to_executorch.py#L54 where TorchAO overrides Embedding.weight with a tensor subclass and then do attribute access inside it. We have to solve this problem in both strict and non-strict because even when dynamo translates subclass.inner_tensor into a fx Graph, the underlying tracer that converts torch IR to aten IR will need to handle subclass.inner_tensor as well. I think there are roughly two ways to implement this: 1) Override getattr on tensor in export to monkey patch inner tensors as properties and add torch function handler 2) When we first see a subclass tensor in make_fx, attach proxy to its' call inner tensors. I tried implementing (1) here: #143518, but it turned out to be quite intrusive. So I prefer (2) for now. Only downside for (2) is we are creating proxy for unnecessarily many inner tensors but i think it is ok because: 1) torch.compile will never see tensor subclass in make_fx so it won't regress any tracing speed 2) Hopefully tensor subclasses that are used in export are not that nest-y. I also noticed a small bug in tensor subclass unwrapping logic. cc IvanKobzarev It seems easier if we just implement it recursively so that it is easier to track the inner attrs to corresponding plain tensors and both aot_autograd and fake_tensor implement subclass unwrapping recursively. cc ezyang SherlockNoMad EikanWang jgong5 wenzhe-nrv Differential Revision: [D67531713](https://our.internmc.facebook.com/intern/diff/D67531713) [ghstack-poisoned]
|
@tugsbayasgalan has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
|
@tugsbayasgalan and I talked offline. My take at the summary: (1) this PR solves the problem only for inner tensor attributes, not constant metadata subclass attributes (2) If we can figure out a less hacky way to override We discussed a version of overriding |
…re-dispatch export via patching tensor.getattr" Previous discussion: #143671 (comment) and #143671 Differential Revision: [D67693609](https://our.internmc.facebook.com/intern/diff/D67693609) [ghstack-poisoned]
…rt via patching tensor.getattr" Previous discussion: #143671 (comment) and #143671 Differential Revision: [D67693609](https://our.internmc.facebook.com/intern/diff/D67693609) [ghstack-poisoned]
…re-dispatch export via patching tensor.getattr" Previous discussion: #143671 (comment) and #143671 Differential Revision: [D67693609](https://our.internmc.facebook.com/intern/diff/D67693609) [ghstack-poisoned]
…rt via patching tensor.getattr" Previous discussion: #143671 (comment) and #143671 Differential Revision: [D67693609](https://our.internmc.facebook.com/intern/diff/D67693609) [ghstack-poisoned]
…re-dispatch export via patching tensor.getattr" Previous discussion: #143671 (comment) and #143671 Differential Revision: [D67693609](https://our.internmc.facebook.com/intern/diff/D67693609) [ghstack-poisoned]
…rt via patching tensor.getattr" Previous discussion: #143671 (comment) and #143671 Differential Revision: [D67693609](https://our.internmc.facebook.com/intern/diff/D67693609) [ghstack-poisoned]
…re-dispatch export via patching tensor.getattr" Previous discussion: #143671 (comment) and #143671 Differential Revision: [D67693609](https://our.internmc.facebook.com/intern/diff/D67693609) [ghstack-poisoned]
…rt via patching tensor.getattr" Previous discussion: #143671 (comment) and #143671 Differential Revision: [D67693609](https://our.internmc.facebook.com/intern/diff/D67693609) [ghstack-poisoned]
…re-dispatch export via patching tensor.getattr" Previous discussion: #143671 (comment) and #143671 Differential Revision: [D67693609](https://our.internmc.facebook.com/intern/diff/D67693609) [ghstack-poisoned]
…rt via patching tensor.getattr" Previous discussion: #143671 (comment) and #143671 Differential Revision: [D67693609](https://our.internmc.facebook.com/intern/diff/D67693609) [ghstack-poisoned]
…re-dispatch export via patching tensor.getattr" Previous discussion: #143671 (comment) and #143671 Differential Revision: [D67693609](https://our.internmc.facebook.com/intern/diff/D67693609) [ghstack-poisoned]
…rt via patching tensor.getattr" Previous discussion: #143671 (comment) and #143671 Differential Revision: [D67693609](https://our.internmc.facebook.com/intern/diff/D67693609) [ghstack-poisoned]
…re-dispatch export via patching tensor.getattr" Previous discussion: #143671 (comment) and #143671 Differential Revision: [D67693609](https://our.internmc.facebook.com/intern/diff/D67693609) [ghstack-poisoned]
…rt via patching tensor.getattr" Previous discussion: #143671 (comment) and #143671 Differential Revision: [D67693609](https://our.internmc.facebook.com/intern/diff/D67693609) [ghstack-poisoned]
…hing tensor.getattr (#143946) Previous discussion: #143671 (comment) and #143671 Differential Revision: [D67693609](https://our.internmc.facebook.com/intern/diff/D67693609) Pull Request resolved: #143946 Approved by: https://github.com/bdhirsh
|
Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as |
Stack from ghstack (oldest at bottom):
This is a draft PR that tries to prototype how to capture attribute access in pre-dispatch IR. The motivating use case is: https://github.com/pytorch/ao/blob/039cef4ad546716aa04cd54c461feb173f7fe403/tutorials/developer_api_guide/export_to_executorch.py#L54 where TorchAO overrides Embedding.weight with a tensor subclass and then do attribute access inside it. We have to solve this problem in both strict and non-strict because even when dynamo translates subclass.inner_tensor into a fx Graph, the underlying tracer that converts torch IR to aten IR will need to handle subclass.inner_tensor as well.
I think there are roughly two ways to implement this:
I tried implementing (1) here: #143518, but it turned out to be quite intrusive. So I prefer (2) for now. Only downside for (2) is we are creating proxy for unnecessarily many inner tensors but i think it is ok because:
I also noticed a small bug in tensor subclass unwrapping logic. cc @ezyang @SherlockNoMad @EikanWang @jgong5 @wenzhe-nrv @IvanKobzarev
It seems easier if we just implement it recursively so that it is easier to track the inner attrs to corresponding plain tensors and both aot_autograd and fake_tensor implement subclass unwrapping recursively.
cc @ezyang @SherlockNoMad @EikanWang @jgong5 @wenzhe-nrv
Differential Revision: D67531713