-
Notifications
You must be signed in to change notification settings - Fork 27.5k
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
Bugfix/inv freq #34525
base: main
Are you sure you want to change the base?
Bugfix/inv freq #34525
Conversation
Hi, I don't see the bug in the original code! |
@Rocketknight1 It is not due to any missing assignment, you can verify this with the following script: import torch # v2.3.1
x = torch.randn(size=(2, 4, 8)).to(device='cpu')
print(x.device) # cpu
x.to(device='cuda')
print(x.device) # cpu Hence, you need an assignment - be it with a new var or directly on Edit: Maybe for more clarification. The in-place movement for a device only happens when we call it on a import torch
class TestModel(torch.nn.Module):
def __init__(self):
super().__init__()
inv_freq = torch.tensor(1.0)
self.register_buffer("inv_freq", tensor=inv_freq, persistent=False)
def forward(self, x):
self.inv_freq.to('cuda')
print(self.inv_freq.device)
model = TestModel()
# you will see 2x cpu
print(model.inv_freq.device)
model('test')
# in-place movement
model.to('cuda')
# you will see 2x cuda:x
print(model.inv_freq.device)
model('test') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're totally right, my bad! That's an embarrassing mistake to make, and my only defence is that I had a lot of notifications that day. The new code is absolutely more correct!
cc @LysandreJik for core maintainer review
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
No worries! Happens to all of us :) and tbh, it is some niche behaviour on torch's side on how device movement is handled. |
I think cc @ArthurZucker would like to review this |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey! This was introduced by #30775, but TBH I am not convinced that we need to even put self.inv_freq
to device. See Llama model as it does not have this and there are no reports about failures in this case!
What does this PR do?
Fixes # (issue)
Before submitting
Pull Request section?
to it if that's the case.
documentation guidelines, and
here are tips on formatting docstrings.
Who can review?
@ArthurZucker
Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.