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 hooked linear init #1011

Merged
merged 5 commits into from
Jan 4, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions gptqmodel/nn_modules/hooked_linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@


class HookedLinear(torch.nn.Linear):
def __init__(self, in_features: int, out_features: int, bias: bool = True, device=None, dtype=None) -> None:
super().__init__(in_features, out_features, bias, device, dtype)
def __init__(self, in_features: int, out_features: int) -> None:
# avoid calling super().__init__() as it would allocate memory based on in/out features
torch.nn.Module.__init__(self)
self.in_features = in_features
self.out_features = out_features

self.forward_hook = None

@staticmethod
Expand All @@ -16,15 +20,13 @@ def replace_linear_with_hooked_linear(module):

@staticmethod
def from_linear(linear: torch.nn.Linear):
custom_linear = HookedLinear(linear.in_features, linear.out_features, bias=linear.bias is not None,
device=linear.weight.device, dtype=linear.weight.dtype)
custom_linear = HookedLinear(linear.in_features, linear.out_features)
custom_linear.weight = linear.weight
if linear.bias is not None:
custom_linear.bias = linear.bias
custom_linear.bias = linear.bias
return custom_linear

def forward(self, input: torch.Tensor) -> torch.Tensor:
output = super().forward(input)
if self.forward_hook:
self.forward_hook(self, input, output)
return output
return output
Loading