Skip to content

Getattr access for subclasses in pre-dispatch - #143671

Closed
tugsbayasgalan wants to merge 5 commits into
gh/tugsbayasgalan/282/basefrom
gh/tugsbayasgalan/282/head
Closed

tugsbayasgalan wants to merge 5 commits into
gh/tugsbayasgalan/282/basefrom
gh/tugsbayasgalan/282/head

Conversation

@tugsbayasgalan

@tugsbayasgalan tugsbayasgalan commented Dec 20, 2024 •

Copy link
Copy Markdown
Contributor

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:

  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 @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

@pytorch-bot

pytorch-bot Bot commented Dec 20, 2024

Copy link
Copy Markdown

🔗 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

Copy link
Copy Markdown
Contributor Author

@tugsbayasgalan has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Dec 20, 2024

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

Copy link
Copy Markdown
Contributor Author

@tugsbayasgalan has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@tugsbayasgalan

Copy link
Copy Markdown
Contributor Author

@tugsbayasgalan has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

1 similar comment
@tugsbayasgalan

Copy link
Copy Markdown
Contributor Author

@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 pushed a commit that referenced this pull request Dec 20, 2024
ghstack-source-id: aa53bbb
Pull Request resolved: #143671
@tugsbayasgalan

Copy link
Copy Markdown
Contributor Author

@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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

?

@tugsbayasgalan tugsbayasgalan Dec 20, 2024 •

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.

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))

Tugsbayasgalan Manlaibaatar added 2 commits December 22, 2024 15:31

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 pushed a commit that referenced this pull request Dec 22, 2024
ghstack-source-id: f418b13
Pull Request resolved: #143671
@tugsbayasgalan

Copy link
Copy Markdown
Contributor Author

@tugsbayasgalan has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@bdhirsh

bdhirsh commented Dec 23, 2024

Copy link
Copy Markdown
Collaborator

@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 __getattribute__ (some flavor of the prototype in this PR), then that work would subsume these changes to proxy tracing

We discussed a version of overriding __getattribute__ inside of export that looks something like this:

import torch
import contextlib
from torch.testing._internal.custom_tensor import ConstantExtraMetadataTensor
import torch.utils._pytree as pytree

@contextlib.contextmanager
def create_new_getattribute(x):
    tensor_attributes, constant_attributes = x.__tensor_flatten__()
    flattened_constant_attributes = pytree.tree_leaves(constant_attributes)
    old_getattribute = x.__class__.__getattribute__

    def new_getattribute(self, name):
        out = old_getattribute(self, name)
        if name in tensor_attributes or out in flattened_constant_attributes: # [4]
            print("PROXY")
            return Proxy("getattr(name")
        else:
            return out

    try:
        x.__class__.__getattribute__ = new_getattribute
        yield
    finally:
        x.__class__.__getattribute__ = old_getattribute


x = ConstantExtraMetadataTensor(torch.ones(2), 4)
with create_new_getattribute(x):
    attribute = y.constant_attribute
    return x.add(attribute)

tugsbayasgalan pushed a commit that referenced this pull request Dec 28, 2024
tugsbayasgalan pushed a commit that referenced this pull request Dec 28, 2024
tugsbayasgalan pushed a commit that referenced this pull request Dec 30, 2024
tugsbayasgalan pushed a commit that referenced this pull request Dec 30, 2024
tugsbayasgalan pushed a commit that referenced this pull request Jan 2, 2025
tugsbayasgalan pushed a commit that referenced this pull request Jan 2, 2025
tugsbayasgalan pushed a commit that referenced this pull request Jan 2, 2025
tugsbayasgalan pushed a commit that referenced this pull request Jan 2, 2025
tugsbayasgalan pushed a commit that referenced this pull request Jan 2, 2025
tugsbayasgalan pushed a commit that referenced this pull request Jan 2, 2025
tugsbayasgalan pushed a commit that referenced this pull request Jan 6, 2025
tugsbayasgalan pushed a commit that referenced this pull request Jan 6, 2025
tugsbayasgalan pushed a commit that referenced this pull request Jan 6, 2025
tugsbayasgalan pushed a commit that referenced this pull request Jan 6, 2025
@zou3519
zou3519 removed their request for review January 23, 2025 14:34
@github-actions

Copy link
Copy Markdown
Contributor

Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as Stale.
Feel free to remove the Stale label if you feel this was a mistake.
If you are unable to remove the Stale label please contact a maintainer in order to do so.
If you want the bot to never mark this PR stale again, add the no-stale label.
Stale pull requests will automatically be closed after 30 days of inactivity.

@github-actions github-actions Bot added the Stale label Mar 24, 2025
@github-actions github-actions Bot closed this Apr 23, 2025
@github-actions
github-actions Bot deleted the gh/tugsbayasgalan/282/head branch May 25, 2025 02:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants