[CPU] [Quantization] Add GPTQ/AWQ 4bits quantization support for CPU (#22685)
Co-authored-by: Ma Mingfei <mingfei.ma@intel.com>
This commit is contained in:
@@ -402,3 +402,39 @@ def unpack_and_dequant_awq(
|
||||
fp16_weight = qdq_weight_T.T
|
||||
|
||||
return fp16_weight, zeros
|
||||
|
||||
|
||||
def unpack_4bit_to_32bit_signed(qweight, qzeros):
|
||||
# Unpack 4-bit values and interpret them as signed integers
|
||||
unpacked_weights = torch.zeros(
|
||||
(qweight.shape[0] * 8, qweight.shape[1]),
|
||||
dtype=torch.int8,
|
||||
device=qweight.device,
|
||||
requires_grad=False,
|
||||
)
|
||||
unpacked_zeros = torch.zeros(
|
||||
(qzeros.shape[0], qzeros.shape[1] * 8),
|
||||
dtype=torch.int8,
|
||||
device=qzeros.device,
|
||||
requires_grad=False,
|
||||
)
|
||||
|
||||
for row in range(unpacked_weights.shape[0]):
|
||||
i = row % 8
|
||||
unpacked_weights[row, :] = (qweight[row // 8, :] >> (4 * i)) & 0xF
|
||||
|
||||
for col in range(unpacked_zeros.shape[1]):
|
||||
i = col % 8
|
||||
unpacked_zeros[:, col] = (qzeros[:, col // 8] >> (4 * i)) & 0xF
|
||||
|
||||
return unpacked_weights, unpacked_zeros + 1
|
||||
|
||||
|
||||
def unpack_and_dequant_gptq(qweight, qzeros, scales):
|
||||
unpacked_qweight, unpacked_qzeros = unpack_4bit_to_32bit_signed(qweight, qzeros)
|
||||
group_size = unpacked_qweight.shape[0] // scales.shape[0]
|
||||
scales = scales.repeat_interleave(group_size, dim=0)
|
||||
unpacked_qzeros = unpacked_qzeros.repeat_interleave(group_size, dim=0)
|
||||
unpacked_qweight = (unpacked_qweight - unpacked_qzeros) * scales
|
||||
|
||||
return unpacked_qweight.T
|
||||
|
||||
Reference in New Issue
Block a user