Skip to content

Commit 7c3d80c

Browse files
authored
fix(qlinear): size pack_original qweight/qzeros by bits/32 for 3-bit GPTQ (#2984)
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.
1 parent 079edcc commit 7c3d80c

1 file changed

Lines changed: 2 additions & 2 deletions

File tree

gptqmodel/nn_modules/qlinear/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,7 +1395,7 @@ def pack_original(self, linear: nn.Module, scales: t.Tensor, zeros: t.Tensor, g_
13951395
int_weight = int_weight.to(t.int32).T.contiguous()
13961396
int_weight = int_weight.numpy().astype(self.pack_np_math_dtype)
13971397

1398-
qweight = np.zeros((math.ceil(int_weight.shape[0] / self.pack_factor), int_weight.shape[1]),
1398+
qweight = np.zeros((math.ceil(int_weight.shape[0] * self.bits / 32), int_weight.shape[1]),
13991399
dtype=self.pack_np_math_dtype)
14001400
if self.bits in [2, 4, 8]:
14011401
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_
14281428
self.register_buffer("qweight", t.from_numpy(qweight.astype(self.pack_np_dtype)))
14291429

14301430
zeros = zeros.numpy().astype(self.pack_np_math_dtype)
1431-
qzeros = np.zeros((zeros.shape[0], math.ceil(zeros.shape[1] / self.pack_factor)), dtype=self.pack_np_math_dtype)
1431+
qzeros = np.zeros((zeros.shape[0], math.ceil(zeros.shape[1] * self.bits / 32)), dtype=self.pack_np_math_dtype)
14321432
if self.bits in [2, 4, 8]:
14331433
for col in range(qzeros.shape[1]):
14341434
for j in range(self.pack_factor):

0 commit comments

Comments
 (0)