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

Potential Gradient Error when Reloading Frozen Weights in qmodule.py _load_from_state_dict #293

Open
cjfghk5697 opened this issue Aug 24, 2024 · 4 comments

Comments

@cjfghk5697
Copy link

cjfghk5697 commented Aug 24, 2024

There is a potential issue in the _load_from_state_dict method where reloading frozen weights into a frozen module might cause a gradient-related error. The FIXME comment in the code points out this problem.

if type(self.weight.data) is not type(deserialized_weight):
    # Reloading frozen weights into unfrozen module: move to the correct device and force assignment
    self.weight = torch.nn.Parameter(deserialized_weight.to(self.weight.device))
else:
    # FIXME: here we should copy frozen weights into frozen module, but this leads to grad error
    self.weight = torch.nn.Parameter(deserialized_weight.to(self.weight.device))

Proposed Solution:

Explicitly set the requires_grad state of the reloaded parameters to False to prevent errors.
Alternatively, use the .data attribute to directly copy the values without altering the requires_grad state.

self.weight = torch.nn.Parameter(deserialized_weight.to(self.weight.device))
self.weight.requires_grad = False

or

self.weight.data.copy_(deserialized_weight.to(self.weight.device).data)

This fix would prevent potential gradient errors when reloading frozen weights into a frozen module, ensuring the operation is safe and consistent.

Copy link

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@github-actions github-actions bot added the Stale label Sep 24, 2024
Copy link

This issue was closed because it has been stalled for 5 days with no activity.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Sep 29, 2024
@dacorvo dacorvo reopened this Sep 30, 2024
@github-actions github-actions bot removed the Stale label Oct 1, 2024
Copy link

github-actions bot commented Nov 1, 2024

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@github-actions github-actions bot added the Stale label Nov 1, 2024
Copy link

github-actions bot commented Nov 7, 2024

This issue was closed because it has been stalled for 5 days with no activity.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 7, 2024
@dacorvo dacorvo reopened this Nov 10, 2024
@github-actions github-actions bot removed the Stale label Nov 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants