MCPcopy Create free account
hub / github.com/OpenMOSS/MOSS / trans_matmul_248_kernel

Function trans_matmul_248_kernel

models/quantization.py:168–243  ·  view source on GitHub ↗

Compute the matrix multiplication C = A x B. A is of shape (M, N) float16 B is of shape (K//8, N) int32 C is of shape (M, K) float16 scales is of shape (G, N) float16 zeros is of shape (G, N) float16 g_ptr is of shape (K) int32

(a_ptr, b_ptr, c_ptr,
                            scales_ptr, zeros_ptr, g_ptr,
                            M, N, K, bits, maxq,
                            stride_am, stride_ak,
                            stride_bk, stride_bn,
                            stride_cm, stride_cn,
                            stride_scales, stride_zeros,
                            BLOCK_SIZE_M: tl.constexpr, BLOCK_SIZE_N: tl.constexpr, BLOCK_SIZE_K: tl.constexpr,
                            GROUP_SIZE_M: tl.constexpr)

Source from the content-addressed store, hash-verified

166)
167@triton.jit
168def trans_matmul_248_kernel(a_ptr, b_ptr, c_ptr,
169 scales_ptr, zeros_ptr, g_ptr,
170 M, N, K, bits, maxq,
171 stride_am, stride_ak,
172 stride_bk, stride_bn,
173 stride_cm, stride_cn,
174 stride_scales, stride_zeros,
175 BLOCK_SIZE_M: tl.constexpr, BLOCK_SIZE_N: tl.constexpr, BLOCK_SIZE_K: tl.constexpr,
176 GROUP_SIZE_M: tl.constexpr):
177 """
178 Compute the matrix multiplication C = A x B.
179 A is of shape (M, N) float16
180 B is of shape (K//8, N) int32
181 C is of shape (M, K) float16
182 scales is of shape (G, N) float16
183 zeros is of shape (G, N) float16
184 g_ptr is of shape (K) int32
185 """
186 infearure_per_bits = 32 // bits
187
188 pid = tl.program_id(axis=0)
189 num_pid_m = tl.cdiv(M, BLOCK_SIZE_M)
190 num_pid_k = tl.cdiv(K, BLOCK_SIZE_K)
191 num_pid_n = tl.cdiv(N, BLOCK_SIZE_N)
192 num_pid_in_group = GROUP_SIZE_M * num_pid_k
193 group_id = pid // num_pid_in_group
194 first_pid_m = group_id * GROUP_SIZE_M
195 group_size_m = min(num_pid_m - first_pid_m, GROUP_SIZE_M)
196 pid_m = first_pid_m + (pid % group_size_m)
197 pid_k = (pid % num_pid_in_group) // group_size_m
198
199 offs_am = pid_m * BLOCK_SIZE_M + tl.arange(0, BLOCK_SIZE_M)
200 offs_bk = pid_k * BLOCK_SIZE_K + tl.arange(0, BLOCK_SIZE_K)
201 offs_n = tl.arange(0, BLOCK_SIZE_N)
202 a_ptrs = a_ptr + (offs_am[:, None] * stride_am + offs_n[None, :] * stride_ak) # (BLOCK_SIZE_M, BLOCK_SIZE_N)
203 a_mask = (offs_am[:, None] < M)
204 # b_ptrs is set up such that it repeats elements along the K axis 8 times
205 b_ptrs = b_ptr + ((offs_bk[:, None] // infearure_per_bits) * stride_bk + offs_n[None,
206 :] * stride_bn) # (BLOCK_SIZE_K, BLOCK_SIZE_N)
207 g_ptrs = g_ptr + offs_bk
208 g_idx = tl.load(g_ptrs)
209
210 # shifter is used to extract the N bits of each element in the 32-bit word from B
211 scales_ptrs = scales_ptr + offs_n[None, :] + g_idx[:, None] * stride_scales
212 zeros_ptrs = zeros_ptr + (offs_n[None, :] // infearure_per_bits) + g_idx[:, None] * stride_zeros
213
214 shifter = (offs_bk % infearure_per_bits) * bits
215 zeros_shifter = (offs_n % infearure_per_bits) * bits
216 accumulator = tl.zeros((BLOCK_SIZE_M, BLOCK_SIZE_K), dtype=tl.float32)
217
218 for k in range(0, num_pid_n):
219 # Fetch scales and zeros; these are per-outfeature and thus reused in the inner loop
220 scales = tl.load(scales_ptrs) # (BLOCK_SIZE_K, BLOCK_SIZE_N,)
221 zeros = tl.load(zeros_ptrs) # (BLOCK_SIZE_K, BLOCK_SIZE_N,)
222
223 zeros = (zeros >> zeros_shifter[None, :]) & maxq
224 zeros = (zeros + 1)
225

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected