From 9f9f078db9f4fe7693163ab8c94f2444320fd2b2 Mon Sep 17 00:00:00 2001 From: LRL-ModelCloud Date: Sat, 4 Jan 2025 13:45:45 +0800 Subject: [PATCH 1/5] fix hooked linear init --- gptqmodel/nn_modules/hooked_linear.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/gptqmodel/nn_modules/hooked_linear.py b/gptqmodel/nn_modules/hooked_linear.py index f1d36796c..490d6f174 100644 --- a/gptqmodel/nn_modules/hooked_linear.py +++ b/gptqmodel/nn_modules/hooked_linear.py @@ -1,9 +1,18 @@ import torch +from torch.nn import Parameter 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) + factory_kwargs = {"device": device, "dtype": dtype} + torch.nn.Module.__init__(self) + self.in_features = in_features + self.out_features = out_features + if bias: + self.bias = Parameter(torch.empty(out_features, **factory_kwargs)) + else: + self.register_parameter("bias", None) + self.forward_hook = None @staticmethod From 3c0fb142ecf8c3a9e5136912eee3b08f0d1b976c Mon Sep 17 00:00:00 2001 From: LRL-ModelCloud Date: Sat, 4 Jan 2025 14:09:08 +0800 Subject: [PATCH 2/5] cleanup --- gptqmodel/nn_modules/hooked_linear.py | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/gptqmodel/nn_modules/hooked_linear.py b/gptqmodel/nn_modules/hooked_linear.py index 490d6f174..f88e9ead7 100644 --- a/gptqmodel/nn_modules/hooked_linear.py +++ b/gptqmodel/nn_modules/hooked_linear.py @@ -1,17 +1,11 @@ import torch -from torch.nn import Parameter class HookedLinear(torch.nn.Linear): def __init__(self, in_features: int, out_features: int, bias: bool = True, device=None, dtype=None) -> None: - factory_kwargs = {"device": device, "dtype": dtype} torch.nn.Module.__init__(self) self.in_features = in_features self.out_features = out_features - if bias: - self.bias = Parameter(torch.empty(out_features, **factory_kwargs)) - else: - self.register_parameter("bias", None) self.forward_hook = None @@ -28,8 +22,7 @@ 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.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: From 228d14abe9aa1fb00fda4d5e0b13544fe5f4d115 Mon Sep 17 00:00:00 2001 From: Qubitium-ModelCloud Date: Sat, 4 Jan 2025 14:11:22 +0800 Subject: [PATCH 3/5] Update hooked_linear.py --- gptqmodel/nn_modules/hooked_linear.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/gptqmodel/nn_modules/hooked_linear.py b/gptqmodel/nn_modules/hooked_linear.py index f88e9ead7..fadf4e5a2 100644 --- a/gptqmodel/nn_modules/hooked_linear.py +++ b/gptqmodel/nn_modules/hooked_linear.py @@ -3,6 +3,7 @@ class HookedLinear(torch.nn.Linear): def __init__(self, in_features: int, out_features: int, bias: bool = True, device=None, dtype=None) -> None: + # avoid calling super().__init__() as it would allocate memory baased on in/out features torch.nn.Module.__init__(self) self.in_features = in_features self.out_features = out_features @@ -29,4 +30,4 @@ def forward(self, input: torch.Tensor) -> torch.Tensor: output = super().forward(input) if self.forward_hook: self.forward_hook(self, input, output) - return output \ No newline at end of file + return output From ba1d2a1b5f882b1c1741b0f92881259f09d0e1f3 Mon Sep 17 00:00:00 2001 From: Qubitium-ModelCloud Date: Sat, 4 Jan 2025 14:13:13 +0800 Subject: [PATCH 4/5] Update hooked_linear.py --- gptqmodel/nn_modules/hooked_linear.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/gptqmodel/nn_modules/hooked_linear.py b/gptqmodel/nn_modules/hooked_linear.py index fadf4e5a2..30f8cbfcf 100644 --- a/gptqmodel/nn_modules/hooked_linear.py +++ b/gptqmodel/nn_modules/hooked_linear.py @@ -2,7 +2,7 @@ class HookedLinear(torch.nn.Linear): - def __init__(self, in_features: int, out_features: int, bias: bool = True, device=None, dtype=None) -> None: + def __init__(self, in_features: int, out_features: int) -> None: # avoid calling super().__init__() as it would allocate memory baased on in/out features torch.nn.Module.__init__(self) self.in_features = in_features @@ -20,8 +20,7 @@ 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 custom_linear.bias = linear.bias return custom_linear From 8836de3639ff0819ac92abd6b2e39ad0fade7bc1 Mon Sep 17 00:00:00 2001 From: Qubitium-ModelCloud Date: Sat, 4 Jan 2025 14:14:53 +0800 Subject: [PATCH 5/5] Update hooked_linear.py --- gptqmodel/nn_modules/hooked_linear.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gptqmodel/nn_modules/hooked_linear.py b/gptqmodel/nn_modules/hooked_linear.py index 30f8cbfcf..1641db714 100644 --- a/gptqmodel/nn_modules/hooked_linear.py +++ b/gptqmodel/nn_modules/hooked_linear.py @@ -3,7 +3,7 @@ class HookedLinear(torch.nn.Linear): def __init__(self, in_features: int, out_features: int) -> None: - # avoid calling super().__init__() as it would allocate memory baased on in/out features + # 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