Skip to content

Commit 6546323

Browse files
committed
[dev] support AWQ/GPTQ quantization for dense models
1 parent 75d0bda commit 6546323

5 files changed

Lines changed: 412 additions & 2 deletions

File tree

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ blake3==1.0.5
99
cachetools==6.1.0
1010
cbor2==5.7.0
1111
cloudpickle==3.1.1
12-
compressed-tensors==0.10.2
12+
compressed-tensors==0.11.0
1313
diskcache==5.6.3
1414
gguf==0.17.1
1515
mistral_common==1.8.3

vllm_kunlun/ops/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@
1616
#
1717

1818
import vllm_kunlun.ops.rotary_embedding
19-
import vllm_kunlun.ops.layernorm
19+
import vllm_kunlun.ops.layernorm
20+
import vllm_kunlun.ops.quantization.awq
21+
import vllm_kunlun.ops.quantization.gptq
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
#
2+
# Copyright (c) 2025 Baidu, Inc. All Rights Reserved.
3+
# Author: Li Wei, Pan Xiakai, You Zeyu
4+
# Email: liwei157@baidu.com
5+
# This file is a part of the vllm-kunlun project.
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
19+
import torch
20+
21+
from typing import Optional
22+
from vllm.model_executor.layers.quantization.awq import AWQLinearMethod
23+
24+
25+
def repack_int4_for_kunlun(self, packed: torch.Tensor, num_bits: int = 4):
26+
"""Convert AWQ-packed int4 weights to Kunlun XPU format.
27+
Input: packed[N, K], dtype=int32, saved as AWQ order
28+
Output: packed_reordered[N, K], dtype=int32, saved as Kunlun order
29+
"""
30+
N, K = packed.shape
31+
self.align_type = 1 if K % 8 == 0 else 0
32+
assert num_bits == 4, "Only int4 supported now"
33+
shifts = torch.arange(0, 32, num_bits, device=packed.device, dtype=torch.int32)
34+
35+
if self.align_type == 0: # NORMAL MODE
36+
# Unpack AWQ order:[0, 2, 4, 6, 1, 3, 5, 7]
37+
unpacked_awq = (packed.unsqueeze(-1) >> shifts) & 0xF # [N, K, 8]
38+
39+
# Reverse AWQ order and convert to KUNLUN order
40+
AWQ_TO_KUNLUN_ORDER_NORMAL = [4, 0, 5, 1, 6, 2, 7, 3]
41+
# [0,2,4,6,1,3,5,7] --> [1, 0, 3, 2, 5, 4, 7, 6]
42+
unpacked_kunlun = unpacked_awq[..., AWQ_TO_KUNLUN_ORDER_NORMAL] # [N, K, 8]
43+
44+
# Pack to int32, order[6, 7, 4, 5, 2, 3, 0, 1]
45+
packed_kunlun = (unpacked_kunlun << shifts).sum(
46+
dim=-1, dtype=torch.int32
47+
) # [N, K]
48+
elif self.align_type == 1: # FAST MODEL
49+
# Unpack AWQ order
50+
unpacked_awq = (
51+
packed.view(N, K // 8, 8).unsqueeze(-1) >> shifts
52+
) & 0xF # [N, K//8, 8, 8]
53+
54+
# Reverse AWQ order and convert to KUNLUN order
55+
AWQ_TO_KUNLUN_ORDER_FAST = [
56+
32, 0, 36, 4, 33, 1, 37, 5,
57+
34, 2, 38, 6, 35, 3, 39, 7,
58+
40, 8, 44, 12, 41, 9, 45, 13,
59+
42, 10, 46, 14, 43, 11, 47, 15,
60+
48, 16, 52, 20, 49, 17, 53, 21,
61+
50, 18, 54, 22, 51, 19, 55, 23,
62+
56, 24, 60, 28, 57, 25, 61, 29,
63+
58, 26, 62, 30, 59, 27, 63, 31
64+
]
65+
unpacked_awq = unpacked_awq.reshape(N, K // 8, 64)
66+
unpacked_kunlun = unpacked_awq[..., AWQ_TO_KUNLUN_ORDER_FAST] # [N, K//8, 64]
67+
68+
# Pack to int32
69+
unpacked_kunlun = unpacked_kunlun.reshape(N, K // 8, 8, 8)
70+
packed_kunlun = (
71+
(unpacked_kunlun << shifts).sum(dim=-1, dtype=torch.int32).reshape(N, K)
72+
) # [N, K]
73+
else:
74+
raise NotImplementedError
75+
76+
return packed_kunlun
77+
78+
79+
def process_weights_after_loading(self, layer: torch.nn.Module) -> None:
80+
layer.qweight = torch.nn.Parameter(
81+
(
82+
self.repack_int4_for_kunlun(layer.qweight.data)
83+
if layer.qweight.data.dtype == torch.int32
84+
else layer.qweight.data
85+
),
86+
requires_grad=False,
87+
)
88+
layer.qzeros = torch.nn.Parameter(
89+
(
90+
self.repack_int4_for_kunlun(layer.qzeros.data)
91+
if layer.qzeros.data.dtype == torch.int32
92+
else layer.qzeros.data
93+
),
94+
requires_grad=False,
95+
)
96+
layer.scales = torch.nn.Parameter(layer.scales.data, requires_grad=False)
97+
98+
99+
def apply(
100+
self, layer: torch.nn.Module, x: torch.Tensor, bias: Optional[torch.Tensor] = None
101+
) -> torch.Tensor:
102+
qweight = layer.qweight
103+
scales = layer.scales
104+
qzeros = layer.qzeros
105+
pack_factor = self.quant_config.pack_factor
106+
out_shape = x.shape[:-1] + (qweight.shape[-1] * pack_factor,)
107+
reshaped_x = x.reshape(-1, x.shape[-1])
108+
109+
# num_tokens >= threshold
110+
FP16_MATMUL_HEURISTIC_CONDITION = x.shape[:-1].numel() >= 256
111+
112+
if FP16_MATMUL_HEURISTIC_CONDITION:
113+
out = torch.ops._C.awq_dequantize(
114+
qweight, scales, qzeros, quant_type=0, align_type=self.align_type
115+
)
116+
out = torch.matmul(reshaped_x, out)
117+
else:
118+
out = torch.ops._C.awq_gemm(
119+
reshaped_x, qweight, scales, qzeros, align_type=self.align_type
120+
)
121+
if bias is not None:
122+
out.add_(bias)
123+
return out.reshape(out_shape)
124+
125+
126+
AWQLinearMethod.repack_int4_for_kunlun = repack_int4_for_kunlun
127+
AWQLinearMethod.process_weights_after_loading = process_weights_after_loading
128+
AWQLinearMethod.apply = apply
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#
2+
# Copyright (c) 2025 Baidu, Inc. All Rights Reserved.
3+
# Author: Li Wei, You Zeyu
4+
# Email: liwei157@baidu.com, youzeyu@baidu.com
5+
# This file is a part of the vllm-kunlun project.
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
19+
import torch
20+
21+
from torch.nn.parameter import Parameter
22+
from typing import Optional
23+
from vllm.model_executor.layers.quantization.gptq import GPTQLinearMethod, ExllamaState
24+
25+
26+
def process_weights_after_loading(self, layer: torch.nn.Module) -> None:
27+
# for torch.compile
28+
layer.qzeros = Parameter(
29+
self.repack_int4_for_kunlun(layer.qzeros.data, self.quant_config.weight_bits)
30+
if self.quant_config.weight_bits == 4 else layer.qzeros.data,
31+
requires_grad=False
32+
)
33+
layer.qweight = Parameter(layer.qweight.data, requires_grad=False)
34+
layer.g_idx = Parameter(layer.g_idx.data, requires_grad=False)
35+
layer.scales = Parameter(layer.scales.data, requires_grad=False)
36+
37+
# exllama needs to shuffle the weight after the weight is loaded
38+
# here we do the shuffle on first forward pass
39+
if layer.exllama_state == ExllamaState.UNINITIALIZED:
40+
if self.quant_config.desc_act:
41+
layer.g_idx.data = torch.argsort(layer.g_idx).to(torch.int)
42+
else:
43+
layer.g_idx.data = torch.empty((0, ),
44+
dtype=torch.int,
45+
device=layer.g_idx.device)
46+
layer.exllama_state = ExllamaState.READY
47+
48+
# No need shuffle on xpu
49+
# ops.gptq_shuffle(layer.qweight, layer.g_idx,
50+
# self.quant_config.weight_bits)
51+
52+
53+
def repack_int4_for_kunlun(self, packed: torch.Tensor, num_bits: int = 4):
54+
N, K = packed.shape
55+
assert num_bits == 4, "Only int4 supported now"
56+
shifts = torch.arange(0, 32, num_bits, device=packed.device, dtype=torch.int32)
57+
58+
# Unpack int32 to int4 values
59+
unpacked_gptq = (
60+
packed.view(N, K // 8, 8).unsqueeze(-1) >> shifts
61+
) & 0xF # [N, K//8, 8, 8]
62+
63+
# Convert to KUNLUN order
64+
GPTQ_TO_KUNLUN_ORDER_FAST = [
65+
32, 0, 33, 1, 34, 2, 35, 3,
66+
36, 4, 37, 5, 38, 6, 39, 7,
67+
40, 8, 41, 9, 42, 10, 43, 11,
68+
44, 12, 45, 13, 46, 14, 47, 15,
69+
48, 16, 49, 17, 50, 18, 51, 19,
70+
52, 20, 53, 21, 54, 22, 55, 23,
71+
56, 24, 57, 25, 58, 26, 59, 27,
72+
60, 28, 61, 29, 62, 30, 63, 31,
73+
]
74+
unpacked_gptq = unpacked_gptq.reshape(N, K // 8, 64)
75+
unpacked_kunlun = unpacked_gptq[..., GPTQ_TO_KUNLUN_ORDER_FAST] # [N, K//8, 64]
76+
77+
# Pack to int32
78+
unpacked_kunlun = unpacked_kunlun.reshape(N, K // 8, 8, 8)
79+
packed_kunlun = (
80+
(unpacked_kunlun << shifts).sum(dim=-1, dtype=torch.int32).reshape(N, K)
81+
) # [N, K]
82+
83+
return packed_kunlun
84+
85+
86+
def apply(
87+
self, layer: torch.nn.Module, x: torch.Tensor, bias: Optional[torch.Tensor] = None
88+
) -> torch.Tensor:
89+
out_shape = x.shape[:-1] + (layer.qweight.shape[-1], )
90+
reshaped_x = x.reshape(-1, x.shape[-1])
91+
92+
output = torch.ops.xspeedgate_ops.gptq_gemm(
93+
reshaped_x,
94+
layer.qweight,
95+
layer.qzeros,
96+
layer.scales,
97+
layer.g_idx,
98+
layer.exllama_state == ExllamaState.READY,
99+
self.quant_config.weight_bits,
100+
)
101+
if bias is not None:
102+
output.add_(bias)
103+
return output.reshape(out_shape)
104+
105+
106+
GPTQLinearMethod.repack_int4_for_kunlun = repack_int4_for_kunlun
107+
GPTQLinearMethod.process_weights_after_loading = process_weights_after_loading
108+
GPTQLinearMethod.apply = apply

0 commit comments

Comments
 (0)