"""Fused W4A16 GEMM: unpack int4 weights + asymmetric dequant + bf16 matmul.""" from __future__ import annotations import torch import torch.nn as nn import triton import triton.language as tl GROUP_SIZE = 128 HALF_GROUP = GROUP_SIZE // 2 @triton.jit def w4a16_gemv_splitk_kernel( x_ptr, wq_ptr, scales_ptr, zeros_ptr, out_ptr, N, stride_xk, stride_wqk, stride_wqn, stride_sg, stride_sn, stride_zg, stride_zn, BLOCK_N: tl.constexpr, GROUP_SIZE: tl.constexpr, HALF_GROUP: tl.constexpr, ): g = tl.program_id(0) pid_n = tl.program_id(1) offs_n = pid_n * BLOCK_N + tl.arange(0, BLOCK_N) n_mask = offs_n < N g_base = g * GROUP_SIZE wq_base = g_base // 2 s_ptrs = scales_ptr + g * stride_sg + offs_n * stride_sn z_ptrs = zeros_ptr + g * stride_zg + offs_n * stride_zn scales = tl.load(s_ptrs, mask=n_mask, other=0.0).to(tl.float32) zeros = tl.load(z_ptrs, mask=n_mask, other=0.0).to(tl.float32) row_offs = tl.arange(0, HALF_GROUP) wq_ptrs = wq_ptr + (wq_base + row_offs)[:, None] * stride_wqk + offs_n[None, :] * stride_wqn packed = tl.load(wq_ptrs, mask=n_mask[None, :], other=0) lo = (packed & 0xF).to(tl.float32) hi = ((packed >> 4) & 0xF).to(tl.float32) lo_bf = (lo - zeros[None, :]) * scales[None, :] hi_bf = (hi - zeros[None, :]) * scales[None, :] even_k = g_base + 2 * row_offs odd_k = even_k + 1 x_even = tl.load(x_ptr + even_k * stride_xk).to(tl.float32) x_odd = tl.load(x_ptr + odd_k * stride_xk).to(tl.float32) partial = tl.dot(x_even[None, :], lo_bf, allow_tf32=False)[0, :] partial += tl.dot(x_odd[None, :], hi_bf, allow_tf32=False)[0, :] tl.atomic_add(out_ptr + offs_n, partial, mask=n_mask) @triton.jit def w4a16_gemm_kernel( x_ptr, wq_ptr, scales_ptr, zeros_ptr, out_ptr, M, N, K, stride_xm, stride_xk, stride_wqk, stride_wqn, stride_sg, stride_sn, stride_zg, stride_zn, stride_om, stride_on, BLOCK_M: tl.constexpr, BLOCK_N: tl.constexpr, GROUP_SIZE: tl.constexpr, HALF_GROUP: tl.constexpr, ): pid_m = tl.program_id(0) pid_n = tl.program_id(1) offs_m = pid_m * BLOCK_M + tl.arange(0, BLOCK_M) offs_n = pid_n * BLOCK_N + tl.arange(0, BLOCK_N) m_mask = offs_m < M n_mask = offs_n < N acc = tl.zeros((BLOCK_M, BLOCK_N), dtype=tl.float32) n_groups = K // GROUP_SIZE for g in range(0, n_groups): g_base = g * GROUP_SIZE wq_base = g_base // 2 s_ptrs = scales_ptr + g * stride_sg + offs_n * stride_sn z_ptrs = zeros_ptr + g * stride_zg + offs_n * stride_zn scales = tl.load(s_ptrs, mask=n_mask, other=0.0).to(tl.float32) zeros = tl.load(z_ptrs, mask=n_mask, other=0.0).to(tl.float32) row_offs = tl.arange(0, HALF_GROUP) wq_ptrs = wq_ptr + (wq_base + row_offs)[:, None] * stride_wqk + offs_n[None, :] * stride_wqn packed = tl.load(wq_ptrs, mask=n_mask[None, :], other=0) lo = (packed & 0xF).to(tl.float32) hi = ((packed >> 4) & 0xF).to(tl.float32) lo_bf = (lo - zeros[None, :]) * scales[None, :] hi_bf = (hi - zeros[None, :]) * scales[None, :] even_k = g_base + 2 * row_offs odd_k = even_k + 1 a_even_ptrs = x_ptr + offs_m[:, None] * stride_xm + even_k[None, :] * stride_xk a_odd_ptrs = x_ptr + offs_m[:, None] * stride_xm + odd_k[None, :] * stride_xk a_even = tl.load(a_even_ptrs, mask=m_mask[:, None], other=0.0).to(tl.float32) a_odd = tl.load(a_odd_ptrs, mask=m_mask[:, None], other=0.0).to(tl.float32) acc += tl.dot(a_even, lo_bf, allow_tf32=False) acc += tl.dot(a_odd, hi_bf, allow_tf32=False) out_ptrs = out_ptr + offs_m[:, None] * stride_om + offs_n[None, :] * stride_on tl.store( out_ptrs, acc.to(tl.bfloat16), mask=m_mask[:, None] & n_mask[None, :], ) def w4a16_gemm( x: torch.Tensor, w_q: torch.Tensor, scales: torch.Tensor, zeros: torch.Tensor, ) -> torch.Tensor: M, K = x.shape Kh, N = w_q.shape assert Kh * 2 == K n_groups = K // GROUP_SIZE if M == 1: BLOCK_N = 128 out_fp32 = torch.zeros(N, dtype=torch.float32, device=x.device) grid = (n_groups, triton.cdiv(N, BLOCK_N)) w4a16_gemv_splitk_kernel[grid]( x, w_q, scales, zeros, out_fp32, N, x.stride(1), w_q.stride(0), w_q.stride(1), scales.stride(0), scales.stride(1), zeros.stride(0), zeros.stride(1), BLOCK_N=BLOCK_N, GROUP_SIZE=GROUP_SIZE, HALF_GROUP=HALF_GROUP, num_warps=4, ) return out_fp32.to(torch.bfloat16).unsqueeze(0) out = torch.empty((M, N), dtype=torch.bfloat16, device=x.device) if M <= 16: BLOCK_M, BLOCK_N, num_warps = 16, 128, 4 elif M <= 32: BLOCK_M, BLOCK_N, num_warps = 32, 128, 4 else: BLOCK_M, BLOCK_N, num_warps = 64, 128, 8 grid = (triton.cdiv(M, BLOCK_M), triton.cdiv(N, BLOCK_N)) w4a16_gemm_kernel[grid]( x, w_q, scales, zeros, out, M, N, K, x.stride(0), x.stride(1), w_q.stride(0), w_q.stride(1), scales.stride(0), scales.stride(1), zeros.stride(0), zeros.stride(1), out.stride(0), out.stride(1), BLOCK_M=BLOCK_M, BLOCK_N=BLOCK_N, GROUP_SIZE=GROUP_SIZE, HALF_GROUP=HALF_GROUP, num_warps=num_warps, ) return out class Model(nn.Module): def __init__(self, M: int, N: int, K: int, group_size: int = GROUP_SIZE): super().__init__() assert group_size == GROUP_SIZE self.M, self.N, self.K = M, N, K self.group_size = group_size n_groups = K // group_size self.register_buffer("w_q", torch.empty(K // 2, N, dtype=torch.uint8)) self.register_buffer("scales", torch.empty(n_groups, N, dtype=torch.bfloat16)) self.register_buffer("zeros", torch.empty(n_groups, N, dtype=torch.bfloat16)) def forward(self, x: torch.Tensor) -> torch.Tensor: return w4a16_gemm(x, self.w_q, self.scales, self.zeros) M = 1 N = 12288 K = 4096 def get_inputs(): x = torch.randn(M, K, dtype=torch.bfloat16) return [x] def get_init_inputs(): return [M, N, K]