@tduncan / Nvidia-Optimization-Stack-VLLM Mirror · read-only / commits / e5697d161c
Fork 0

A high-throughput and memory-efficient inference and serving engine for LLMs

Mirrored from https://github.com/tduncan-lum/Nvidia-Optimization-Stack---VLLM.git

Synced Nov 16, 18:15 UTC

[Kernel] [Triton] [AMD] Adding Triton implementations awq_dequantize and awq_gemm to support AWQ (#7386)

rasmith committed Aug 28, 2024 at 14:37 UTC e5697d161c132cd50d8ce560ece5b10931d74965
5 files changed +493 -1
tests/kernels/test_awq_triton.py new
+169
@@ -0,0 +1,169 @@
1 +"""Tests for the AWQ Triton kernel.
2 +
3 +Run `pytest tests/kernels/test_awq_triton.py`.
4 +"""
5 +import pytest
6 +import torch
7 +
8 +from vllm.model_executor.layers.quantization.awq_triton import (
9 + AWQ_TRITON_SUPPORTED_GROUP_SIZES, awq_dequantize_triton, awq_gemm_triton)
10 +
11 +device = "cuda"
12 +
13 +
14 +def reverse_awq_order(t: torch.Tensor):
15 + bits = 4
16 + AWQ_REVERSE_ORDER = [0, 4, 1, 5, 2, 6, 3, 7]
17 + reverse_order_tensor = torch.arange(
18 + t.shape[-1],
19 + dtype=torch.int32,
20 + device=t.device,
21 + )
22 + reverse_order_tensor = reverse_order_tensor.view(-1, 32 // bits)
23 + reverse_order_tensor = reverse_order_tensor[:, AWQ_REVERSE_ORDER]
24 + reverse_order_tensor = reverse_order_tensor.view(-1)
25 +
26 + t = t[:, reverse_order_tensor] & 0xF
27 + return t
28 +
29 +
30 +# qweights - [R , C // 8], int32
31 +# scales - [R // G, C ], float16
32 +# zeros - [R // G, C // 8], int32
33 +def awq_dequantize_torch(qweight: torch.Tensor, scales: torch.Tensor,
34 + qzeros: torch.Tensor,
35 + group_size: int) -> torch.Tensor:
36 +
37 + if group_size == -1:
38 + group_size = qweight.shape[0]
39 +
40 + bits = 4
41 + shifts = torch.arange(0, 32, bits, device=qzeros.device)
42 +
43 + iweights = torch.bitwise_right_shift(qweight[:, :, None],
44 + shifts[None, None, :]).to(torch.int8)
45 +
46 + iweights = iweights.view(iweights.shape[0], -1)
47 +
48 + zeros = torch.bitwise_right_shift(qzeros[:, :, None],
49 + shifts[None, None, :]).to(torch.int8)
50 + zeros = zeros.view(qzeros.shape[0], -1)
51 + zeros = reverse_awq_order(zeros)
52 +
53 + iweights = reverse_awq_order(iweights)
54 +
55 + iweights = torch.bitwise_and(iweights, (2**bits) - 1)
56 + zeros = torch.bitwise_and(zeros, (2**bits) - 1)
57 +
58 + scales = scales.repeat_interleave(group_size, dim=0)
59 + zeros = zeros.repeat_interleave(group_size, dim=0)
60 + return (iweights - zeros) * scales
61 +
62 +
63 +# qweights - [R , C // 8], int32
64 +# scales - [R // G, C ], float16
65 +# zeros - [R // G, C // 8], int32
66 +@pytest.mark.parametrize("qweight_rows", [3584, 18944, 128, 256, 512, 1024])
67 +@pytest.mark.parametrize("qweight_cols", [448, 576, 4736, 16, 32, 64, 128])
68 +@pytest.mark.parametrize("group_size", AWQ_TRITON_SUPPORTED_GROUP_SIZES)
69 +def test_dequantize(qweight_rows, qweight_cols, group_size):
70 +
71 + if group_size == -1:
72 + group_size = qweight_rows
73 +
74 + qweight_dtype = torch.int32
75 + scales_rows = qweight_rows // group_size
76 + scales_cols = qweight_cols * 8
77 + scales_dtype = torch.float16
78 + zeros_rows = scales_rows
79 + zeros_cols = qweight_cols
80 + zeros_dtype = torch.int32
81 +
82 + torch.manual_seed(0)
83 +
84 + qweight = torch.randint(0,
85 + torch.iinfo(torch.int32).max,
86 + (qweight_rows, qweight_cols),
87 + dtype=qweight_dtype,
88 + device=device)
89 + scales = torch.rand(scales_rows,
90 + scales_cols,
91 + dtype=scales_dtype,
92 + device=device)
93 + zeros = torch.randint(0,
94 + torch.iinfo(torch.int32).max,
95 + (zeros_rows, zeros_cols),
96 + dtype=zeros_dtype,
97 + device=device)
98 +
99 + iweights_triton = awq_dequantize_triton(qweight, scales, zeros)
100 +
101 + assert (not torch.any(torch.isinf(iweights_triton))
102 + and not torch.any(torch.isnan(iweights_triton)))
103 +
104 + iweights_torch = awq_dequantize_torch(qweight, scales, zeros, group_size)
105 +
106 + torch.testing.assert_close(iweights_triton, iweights_torch)
107 +
108 +
109 +# input - [N, K]
110 +# qweight - [K, M // 8]
111 +# qzeros - [K // G, M // 8]
112 +# scales - [K // G, M]
113 +@pytest.mark.parametrize("N", [1, 2, 4, 8, 14, 17, 23, 32])
114 +@pytest.mark.parametrize("K", [128])
115 +@pytest.mark.parametrize("M", [16, 24, 32])
116 +@pytest.mark.parametrize("group_size", AWQ_TRITON_SUPPORTED_GROUP_SIZES)
117 +@pytest.mark.parametrize("splitK", [1, 8])
118 +def test_gemm(N, K, M, splitK, group_size):
119 +
120 + if group_size == -1:
121 + group_size = K
122 +
123 + split_k_iters = splitK
124 +
125 + input_rows = N
126 + input_cols = K
127 + input_dtype = torch.float32
128 + qweight_rows = input_cols
129 + qweight_cols = M // 8
130 + scales_rows = qweight_rows // group_size
131 + scales_cols = M
132 + scales_dtype = torch.float32
133 + qzeros_rows = scales_rows
134 + qzeros_cols = qweight_cols
135 +
136 + torch.manual_seed(0)
137 +
138 + input = torch.rand((input_rows, input_cols),
139 + dtype=input_dtype,
140 + device=device)
141 + qweight = torch.randint(0,
142 + torch.iinfo(torch.int32).max,
143 + (qweight_rows, qweight_cols),
144 + device=device)
145 + qzeros = torch.randint(0,
146 + torch.iinfo(torch.int32).max,
147 + (qzeros_rows, qzeros_cols),
148 + device=device)
149 + scales = torch.rand((scales_rows, scales_cols),
150 + dtype=scales_dtype,
151 + device=device)
152 +
153 + output_triton = awq_gemm_triton(input, qweight, scales, qzeros,
154 + split_k_iters)
155 +
156 + assert (not torch.any(torch.isinf(output_triton))
157 + and not torch.any(torch.isnan(output_triton)))
158 +
159 + dequantized_weights = awq_dequantize_triton(qweight, scales, qzeros)
160 +
161 + output_torch = torch.matmul(input, dequantized_weights)
162 +
163 + assert (not torch.any(torch.isinf(output_torch))
164 + and not torch.any(torch.isnan(output_torch)))
165 +
166 + torch.testing.assert_close(output_triton.cpu(),
167 + output_torch.cpu(),
168 + atol=1e-1,
169 + rtol=1e-1)
vllm/_custom_ops.py
+9
@@ -4,6 +4,7 @@ from typing import List, Optional, Tuple, Union
4
5 import torch
6
7 +import vllm.envs as envs
8 from vllm._core_ext import ScalarType
9 from vllm.logger import init_logger
10 from vllm.platforms import current_platform
@@ -177,12 +178,20 @@ def advance_step(num_seqs: int, num_queries: int, block_size: int,
178 def awq_dequantize(qweight: torch.Tensor, scales: torch.Tensor,
179 zeros: torch.Tensor, split_k_iters: int, thx: int,
180 thy: int) -> torch.Tensor:
181 + if envs.VLLM_USE_TRITON_AWQ:
182 + from vllm.model_executor.layers.quantization.awq_triton import (
183 + awq_dequantize_triton)
184 + return awq_dequantize_triton(qweight, scales, zeros)
185 return torch.ops._C.awq_dequantize(qweight, scales, zeros, split_k_iters,
186 thx, thy)
187
188
189 def awq_gemm(input: torch.Tensor, qweight: torch.Tensor, qzeros: torch.Tensor,
190 scales: torch.Tensor, split_k_iters: int) -> torch.Tensor:
191 + if envs.VLLM_USE_TRITON_AWQ:
192 + from vllm.model_executor.layers.quantization.awq_triton import (
193 + awq_gemm_triton)
194 + return awq_gemm_triton(input, qweight, qzeros, scales, split_k_iters)
195 return torch.ops._C.awq_gemm(input, qweight, qzeros, scales, split_k_iters)
196
197
vllm/config.py
+7 -1
@@ -267,7 +267,7 @@ class ModelConfig:
267
268 def _verify_quantization(self) -> None:
269 supported_quantization = [*QUANTIZATION_METHODS]
270 - rocm_supported_quantization = ["gptq", "squeezellm", "fp8"]
270 + rocm_supported_quantization = ["awq", "gptq", "squeezellm", "fp8"]
271 optimized_quantization_methods = [
272 "fp8", "marlin", "gptq_marlin_24", "gptq_marlin", "awq_marlin",
273 "fbgemm_fp8", "compressed_tensors", "compressed-tensors",
@@ -322,6 +322,12 @@ class ModelConfig:
322 "%s quantization is not fully "
323 "optimized yet. The speed can be slower than "
324 "non-quantized models.", self.quantization)
325 + if (self.quantization == "awq" and is_hip()
326 + and not envs.VLLM_USE_TRITON_AWQ):
327 + logger.warning(
328 + "Using AWQ quantization with ROCm, but VLLM_USE_TRITON_AWQ"
329 + " is not set, enabling VLLM_USE_TRITON_AWQ.")
330 + envs.VLLM_USE_TRITON_AWQ = True
331
332 def _verify_cuda_graph(self) -> None:
333 if self.max_seq_len_to_capture is None:
vllm/envs.py
+4
@@ -400,6 +400,10 @@ environment_variables: Dict[str, Callable[[], Any]] = {
400 "VLLM_TORCH_PROFILER_DIR":
401 lambda: (None if os.getenv("VLLM_TORCH_PROFILER_DIR", None) is None else os
402 .path.expanduser(os.getenv("VLLM_TORCH_PROFILER_DIR", "."))),
403 +
404 + # If set, vLLM will use Triton implementations of AWQ.
405 + "VLLM_USE_TRITON_AWQ":
406 + lambda: bool(int(os.getenv("VLLM_USE_TRITON_AWQ", "0"))),
407 }
408
409 # end-env-vars-definition
vllm/model_executor/layers/quantization/awq_triton.py new
+304
@@ -0,0 +1,304 @@
1 +import torch
2 +import triton
3 +import triton.language as tl
4 +
5 +AWQ_TRITON_SUPPORTED_GROUP_SIZES = [-1, 32, 64, 128]
6 +
7 +
8 +@triton.jit
9 +def awq_dequantize_kernel(
10 + qweight_ptr, # quantized matrix
11 + scales_ptr, # scales, per group
12 + zeros_ptr, # zeros, per group
13 + group_size, # Should always be one of the supported group sizes
14 + result_ptr, # Output matrix
15 + num_cols, # input num cols in qweight
16 + num_rows, # input num rows in qweight
17 + BLOCK_SIZE_X: tl.constexpr,
18 + BLOCK_SIZE_Y: tl.constexpr):
19 + # Setup the pids.
20 + pid_x = tl.program_id(axis=0)
21 + pid_y = tl.program_id(axis=1)
22 +
23 + # Compute offsets and masks for qweight_ptr.
24 + offsets_y = pid_y * BLOCK_SIZE_Y + tl.arange(0, BLOCK_SIZE_Y)
25 + offsets_x = pid_x * BLOCK_SIZE_X + tl.arange(0, BLOCK_SIZE_X * 8) // 8
26 + offsets = num_cols * offsets_y[:, None] + offsets_x[None, :]
27 +
28 + masks_y = offsets_y < num_rows
29 + masks_x = offsets_x < num_cols
30 +
31 + masks = masks_y[:, None] & masks_x[None, :]
32 +
33 + # Compute offsets and masks for result output ptr.
34 + result_offsets_y = pid_y * BLOCK_SIZE_Y + tl.arange(0, BLOCK_SIZE_Y)
35 + result_offsets_x = pid_x * BLOCK_SIZE_X * 8 + tl.arange(
36 + 0, BLOCK_SIZE_X * 8)
37 + result_offsets = (8 * num_cols * result_offsets_y[:, None] +
38 + result_offsets_x[None, :])
39 +
40 + result_masks_y = result_offsets_y < num_rows
41 + result_masks_x = result_offsets_x < num_cols * 8
42 + result_masks = result_masks_y[:, None] & result_masks_x[None, :]
43 +
44 + # Load the weights.
45 + iweights = tl.load(qweight_ptr + offsets, masks)
46 +
47 + # Create reverse AWQ order as tensor: [0, 4, 1, 5, 2, 6, 3, 7]
48 + # that will map given indices to the correct order.
49 + reverse_awq_order_tensor = ((tl.arange(0, 2) * 4)[None, :] +
50 + tl.arange(0, 4)[:, None]).reshape(8)
51 +
52 + # Use this to compute a set of shifts that can be used to unpack and
53 + # reorder the values in iweights and zeros.
54 + shifts = reverse_awq_order_tensor * 4
55 + shifts = tl.broadcast_to(shifts[None, :], (BLOCK_SIZE_Y * BLOCK_SIZE_X, 8))
56 + shifts = tl.reshape(shifts, (BLOCK_SIZE_Y, BLOCK_SIZE_X * 8))
57 +
58 + # Unpack and reorder: shift out the correct 4-bit value and mask.
59 + iweights = (iweights >> shifts) & 0xF
60 +
61 + # Compute zero offsets and masks.
62 + zero_offsets_y = (pid_y * BLOCK_SIZE_Y // group_size +
63 + tl.arange(0, BLOCK_SIZE_Y) // group_size)
64 + zero_offsets_x = pid_x * BLOCK_SIZE_X + tl.arange(0, BLOCK_SIZE_X * 8) // 8
65 + zero_offsets = num_cols * zero_offsets_y[:, None] + zero_offsets_x[None, :]
66 +
67 + zero_masks_y = zero_offsets_y < num_rows // group_size
68 + zero_masks_x = zero_offsets_x < num_cols
69 + zero_masks = zero_masks_y[:, None] & zero_masks_x[None, :]
70 +
71 + # Load the zeros.
72 + zeros = tl.load(zeros_ptr + zero_offsets, zero_masks)
73 +
74 + # Unpack and reorder: shift out the correct 4-bit value and mask.
75 + zeros = (zeros >> shifts) & 0xF
76 +
77 + # Compute scale offsets and masks.
78 + scale_offsets_y = (pid_y * BLOCK_SIZE_Y // group_size +
79 + tl.arange(0, BLOCK_SIZE_Y) // group_size)
80 + scale_offsets_x = (pid_x * BLOCK_SIZE_X * 8 +
81 + tl.arange(0, BLOCK_SIZE_X * 8))
82 + scale_offsets = (num_cols * 8 * scale_offsets_y[:, None] +
83 + scale_offsets_x[None, :])
84 + scale_masks_y = scale_offsets_y < num_rows // group_size
85 + scale_masks_x = scale_offsets_x < num_cols * 8
86 + scale_masks = scale_masks_y[:, None] & scale_masks_x[None, :]
87 +
88 + # Load the scales.
89 + scales = tl.load(scales_ptr + scale_offsets, scale_masks)
90 +
91 + # Dequantize.
92 + iweights = (iweights - zeros) * scales
93 + iweights = iweights.to(result_ptr.type.element_ty)
94 +
95 + # Finally, store.
96 + tl.store(result_ptr + result_offsets, iweights, result_masks)
97 +
98 +
99 +@triton.jit
100 +def awq_gemm_kernel(a_ptr, b_ptr, c_ptr, zeros_ptr, scales_ptr, M, N, K,
101 + group_size, BLOCK_SIZE_M: tl.constexpr,
102 + BLOCK_SIZE_N: tl.constexpr, BLOCK_SIZE_K: tl.constexpr,
103 + SPLIT_K: tl.constexpr):
104 + pid = tl.program_id(axis=0)
105 + pid_z = tl.program_id(1)
106 +
107 + # NOTE: This doesn't work in TRITON_INTERPRET=1 mode. Use below instead.
108 + # num_pid_n = (N + BLOCK_SIZE_N - 1) // BLOCK_SIZE_N
109 + num_pid_n = tl.cdiv(N, BLOCK_SIZE_N)
110 +
111 + pid_m = pid // num_pid_n
112 + pid_n = pid % num_pid_n
113 +
114 + accumulator_dtype = c_ptr.type.element_ty
115 +
116 + # NOTE: This doesn't work in TRITON_INTERPRET=1 mode. Use below instead.
117 + # accumulator = tl.arange(0, BLOCK_SIZE_N)
118 + # accumulator = tl.broadcast_to(accumulator[None, :],
119 + # (BLOCK_SIZE_M, BLOCK_SIZE_N))
120 + # accumulator = accumulator & 0x0
121 + # accumulator = accumulator.to(accumulator_dtype)
122 + accumulator = tl.zeros((BLOCK_SIZE_M, BLOCK_SIZE_N),
123 + dtype=accumulator_dtype)
124 +
125 + # Create reverse AWQ order as tensor: [0, 4, 1, 5, 2, 6, 3, 7]
126 + # that will map given indices to the correct order.
127 + reverse_awq_order_tensor = ((tl.arange(0, 2) * 4)[None, :] +
128 + tl.arange(0, 4)[:, None]).reshape(8)
129 +
130 + # Create the necessary shifts to use to unpack.
131 + shifts = reverse_awq_order_tensor * 4
132 + shifts = tl.broadcast_to(shifts[None, :],
133 + (BLOCK_SIZE_K * (BLOCK_SIZE_N // 8), 8))
134 + shifts = tl.reshape(shifts, (BLOCK_SIZE_K, BLOCK_SIZE_N))
135 +
136 + # Offsets and masks.
137 + offsets_am = pid_m * BLOCK_SIZE_M + tl.arange(0, BLOCK_SIZE_M)
138 + masks_am = offsets_am < M
139 +
140 + offsets_bn = (pid_n * (BLOCK_SIZE_N // 8) +
141 + tl.arange(0, BLOCK_SIZE_N) // 8)
142 + masks_bn = offsets_bn < N // 8
143 +
144 + offsets_zn = (pid_n * (BLOCK_SIZE_N // 8) +
145 + tl.arange(0, BLOCK_SIZE_N) // 8)
146 + masks_zn = offsets_zn < N // 8
147 +
148 + offsets_sn = pid_n * BLOCK_SIZE_N + tl.arange(0, BLOCK_SIZE_N)
149 + masks_sn = offsets_sn < N
150 +
151 + offsets_k = pid_z * BLOCK_SIZE_K + tl.arange(0, BLOCK_SIZE_K)
152 + offsets_a = K * offsets_am[:, None] + offsets_k[None, :]
153 + offsets_b = (N // 8) * offsets_k[:, None] + offsets_bn[None, :]
154 +
155 + a_ptrs = a_ptr + offsets_a
156 + b_ptrs = b_ptr + offsets_b
157 +
158 + # NOTE: Use this in TRITON_INTERPRET=1 mode instead of tl.cdiv
159 + # block_offset = BLOCK_SIZE_K * SPLIT_K
160 + # for k in range(0, (K + block_offset - 1) // (block_offset)):
161 + for k in range(0, tl.cdiv(K, BLOCK_SIZE_K * SPLIT_K)):
162 + masks_k = offsets_k < K
163 + masks_a = masks_am[:, None] & masks_k[None, :]
164 + a = tl.load(a_ptrs, mask=masks_a)
165 +
166 + masks_b = masks_k[:, None] & masks_bn[None, :]
167 + b = tl.load(b_ptrs, mask=masks_b)
168 +
169 + # Dequantize b.
170 + offsets_szk = (
171 + (BLOCK_SIZE_K * SPLIT_K * k + pid_z * BLOCK_SIZE_K) // group_size +
172 + tl.arange(0, BLOCK_SIZE_K) // group_size)
173 + offsets_z = (N // 8) * offsets_szk[:, None] + offsets_zn[None, :]
174 + masks_zk = offsets_szk < K // group_size
175 + masks_z = masks_zk[:, None] & masks_zn[None, :]
176 + zeros_ptrs = zeros_ptr + offsets_z
177 + zeros = tl.load(zeros_ptrs, mask=masks_z)
178 +
179 + offsets_s = N * offsets_szk[:, None] + offsets_sn[None, :]
180 + masks_sk = offsets_szk < K // group_size
181 + masks_s = masks_sk[:, None] & masks_sn[None, :]
182 + scales_ptrs = scales_ptr + offsets_s
183 + scales = tl.load(scales_ptrs, mask=masks_s)
184 +
185 + b = (b >> shifts) & 0xF
186 + zeros = (zeros >> shifts) & 0xF
187 + b = (b - zeros) * scales
188 + b = b.to(c_ptr.type.element_ty)
189 +
190 + # Accumulate results.
191 + accumulator = tl.dot(a, b, accumulator, out_dtype=accumulator_dtype)
192 +
193 + offsets_k += BLOCK_SIZE_K * SPLIT_K
194 + a_ptrs += BLOCK_SIZE_K * SPLIT_K
195 + b_ptrs += BLOCK_SIZE_K * SPLIT_K * (N // 8)
196 +
197 + c = accumulator.to(c_ptr.type.element_ty)
198 + offs_cm = pid_m * BLOCK_SIZE_M + tl.arange(0, BLOCK_SIZE_M)
199 + offs_cn = pid_n * BLOCK_SIZE_N + tl.arange(0, BLOCK_SIZE_N)
200 + c_ptrs = c_ptr + N * offs_cm[:, None] + offs_cn[None, :]
201 + c_mask = (offs_cm[:, None] < M) & (offs_cn[None, :] < N)
202 + if SPLIT_K == 1:
203 + tl.store(c_ptrs, c, mask=c_mask)
204 + else:
205 + tl.atomic_add(c_ptrs, c, mask=c_mask)
206 +
207 +
208 +# qweights - [K , M // 8], int32
209 +# scales - [K // G, M ], float16
210 +# zeros - [K // G, M // 8], int32
211 +def awq_dequantize_triton(qweight: torch.Tensor,
212 + scales: torch.Tensor,
213 + zeros: torch.Tensor,
214 + block_size_x: int = 32,
215 + block_size_y: int = 32) -> torch.Tensor:
216 + K = qweight.shape[0]
217 + M = scales.shape[1]
218 + group_size = qweight.shape[0] // scales.shape[0]
219 +
220 + assert K > 0 and M > 0
221 + assert scales.shape[0] == K // group_size and scales.shape[1] == M
222 + assert zeros.shape[0] == K // group_size and zeros.shape[1] == M // 8
223 + assert group_size <= K
224 + assert group_size in AWQ_TRITON_SUPPORTED_GROUP_SIZES or group_size == K
225 +
226 + # Result tensor:
227 + # number of rows = same as input tensor
228 + # number of cols = 8 x input tensor num cols
229 + result = torch.empty(qweight.shape[0],
230 + qweight.shape[1] * 8,
231 + device=qweight.device,
232 + dtype=scales.dtype)
233 +
234 + Y = qweight.shape[0] # num rows
235 + X = qweight.shape[1] # num cols
236 +
237 + grid = lambda META: (
238 + triton.cdiv(X, META['BLOCK_SIZE_X']),
239 + triton.cdiv(Y, META['BLOCK_SIZE_Y']),
240 + )
241 + awq_dequantize_kernel[grid](qweight,
242 + scales,
243 + zeros,
244 + group_size,
245 + result,
246 + X,
247 + Y,
248 + BLOCK_SIZE_X=block_size_x,
249 + BLOCK_SIZE_Y=block_size_y)
250 +
251 + return result
252 +
253 +
254 +# input - [M, K]
255 +# qweight - [K, N // 8]
256 +# qzeros - [K // G, N // 8]
257 +# scales - [K // G, N]
258 +# split_k_iters - parallelism along K-dimension, int, power of 2.
259 +def awq_gemm_triton(input: torch.Tensor,
260 + qweight: torch.Tensor,
261 + scales: torch.Tensor,
262 + qzeros: torch.Tensor,
263 + split_k_iters: int,
264 + block_size_m: int = 32,
265 + block_size_n: int = 32,
266 + block_size_k: int = 32) -> torch.Tensor:
267 + M, K = input.shape
268 + N = qweight.shape[1] * 8
269 + group_size = qweight.shape[0] // qzeros.shape[0]
270 +
271 + assert N > 0 and K > 0 and M > 0
272 + assert qweight.shape[0] == K and qweight.shape[1] == N // 8
273 + assert qzeros.shape[0] == K // group_size and qzeros.shape[1] == N // 8
274 + assert scales.shape[0] == K // group_size and scales.shape[1] == N
275 + assert split_k_iters & (split_k_iters - 1) == 0 and split_k_iters != 0
276 + assert split_k_iters <= 32
277 + assert group_size <= K
278 + assert group_size in AWQ_TRITON_SUPPORTED_GROUP_SIZES or group_size == K
279 +
280 + grid = lambda META: (
281 + triton.cdiv(M, META['BLOCK_SIZE_M']) * triton.cdiv(
282 + N, META['BLOCK_SIZE_N']),
283 + split_k_iters,
284 + )
285 +
286 + result = torch.zeros((M, N), dtype=scales.dtype, device=input.device)
287 +
288 + # A = input, B = qweight, C = result
289 + # A = M x K, B = K x N, C = M x N
290 + awq_gemm_kernel[grid](input,
291 + qweight,
292 + result,
293 + qzeros,
294 + scales,
295 + M,
296 + N,
297 + K,
298 + group_size,
299 + BLOCK_SIZE_M=block_size_m,
300 + BLOCK_SIZE_N=block_size_n,
301 + BLOCK_SIZE_K=block_size_k,
302 + SPLIT_K=split_k_iters)
303 +
304 + return result