From f365e85d1b5cbec9221770c86923d6c673cf6ad3 Mon Sep 17 00:00:00 2001 From: Qubitium Date: Sun, 26 Jul 2026 21:04:50 +0000 Subject: [PATCH] fix(qlinear): size pack_original qweight/qzeros by bits/32 for 3-bit GPTQ The previous pack_factor shortcut (32 // bits) gives 10 for 3-bit, but the 3-bit custom packing loop consumes 32 values per 3 rows. Using math.ceil(dim / pack_factor) over-allocated the output buffer and made the loop overrun. Size packed tensors by math.ceil(dim * bits / 32) instead, which is exact for 2/4/8/3-bit layouts. --- gptqmodel/nn_modules/qlinear/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gptqmodel/nn_modules/qlinear/__init__.py b/gptqmodel/nn_modules/qlinear/__init__.py index 242d04078..6c6c6ed91 100644 --- a/gptqmodel/nn_modules/qlinear/__init__.py +++ b/gptqmodel/nn_modules/qlinear/__init__.py @@ -1395,7 +1395,7 @@ def pack_original(self, linear: nn.Module, scales: t.Tensor, zeros: t.Tensor, g_ int_weight = int_weight.to(t.int32).T.contiguous() int_weight = int_weight.numpy().astype(self.pack_np_math_dtype) - qweight = np.zeros((math.ceil(int_weight.shape[0] / self.pack_factor), int_weight.shape[1]), + qweight = np.zeros((math.ceil(int_weight.shape[0] * self.bits / 32), int_weight.shape[1]), dtype=self.pack_np_math_dtype) if self.bits in [2, 4, 8]: for row in range(qweight.shape[0]): @@ -1428,7 +1428,7 @@ def pack_original(self, linear: nn.Module, scales: t.Tensor, zeros: t.Tensor, g_ self.register_buffer("qweight", t.from_numpy(qweight.astype(self.pack_np_dtype))) zeros = zeros.numpy().astype(self.pack_np_math_dtype) - qzeros = np.zeros((zeros.shape[0], math.ceil(zeros.shape[1] / self.pack_factor)), dtype=self.pack_np_math_dtype) + qzeros = np.zeros((zeros.shape[0], math.ceil(zeros.shape[1] * self.bits / 32)), dtype=self.pack_np_math_dtype) if self.bits in [2, 4, 8]: for col in range(qzeros.shape[1]): for j in range(self.pack_factor):