Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: setting torch frontend tensor 'data' attribute #28473

Merged
merged 4 commits into from
Mar 4, 2024

Conversation

Sam-Armstrong
Copy link
Contributor

Fixes setting the data attribute of a frontend tensor to match the behaviour of native torch, such as this minimal example which prior to this fix would throw AttributeError: can't set attribute 'data' for the frontend:

import torch
import ivy.functional.frontends.torch as torch_frontend

x = torch.randn((1, 3, 3))
y = torch.randn((1, 3, 3))
x.__setattr__('data', y)
print(x)

x = torch_frontend.randn((1, 3, 3))
y = torch_frontend.randn((1, 3, 3))
x.__setattr__('data', y)
print(x)

Does this look ok to you @Ishticode?

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

PR Compliance Checks

Thank you for your Pull Request! We have run several checks on this pull request in order to make sure it's suitable for merging into this project. The results are listed in the following section.

Issue Reference

In order to be considered for merging, the pull request description must refer to a specific issue number. This is described in our contributing guide and our PR template.
This check is looking for a phrase similar to: "Fixes #XYZ" or "Resolves #XYZ" where XYZ is the issue number that this PR is meant to address.

@Sam-Armstrong
Copy link
Contributor Author

@Ishticode also, if this looks ok is there a way to stop the linter changing the code? 😅

@ivy-leaves ivy-leaves added the PyTorch Frontend Developing the PyTorch Frontend, checklist triggered by commenting add_frontend_checklist label Mar 3, 2024
Copy link
Contributor

@Ishticode Ishticode left a comment

Choose a reason for hiding this comment

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

was just playing with this.
One question i have is why do we want to just not do anything when data attribute is being updated. torch's native behaviour shows that the data changes. You can slightly modify this script to see. Whereas torch frontend data remains same before and after this.

print("native torch \n")
x = torch.randn((1, 3, 3))
y = torch.randn((1, 3, 3))
print("x.data before setattr:\n", x.data)
x.__setattr__('data', y)
print("x.data after setattr:\n", x.data)

print("ivy torch frontend\n")
x = torch_frontend.randn((1, 3, 3))
y = torch_frontend.randn((1, 3, 3))
print("x.data before setattr:\n")
print(x.data)
x.__setattr__('data', y)
print("x.data after setattr:\n")
print(x.data)

hmmm it does fix the issues you mentioned but it leads to this new subtle issue (which existed before ig but was explicit) which might be more troublesome elsewhere

@Sam-Armstrong
Copy link
Contributor Author

Sam-Armstrong commented Mar 4, 2024

@Ishticode my fix didn't have the pass, the linter added that for some reason. I was updating self

@Sam-Armstrong Sam-Armstrong merged commit ea04b7a into Transpile-AI:main Mar 4, 2024
137 of 141 checks passed
@Sam-Armstrong Sam-Armstrong deleted the tensor-set-attr branch March 4, 2024 17:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PyTorch Frontend Developing the PyTorch Frontend, checklist triggered by commenting add_frontend_checklist
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants