import torch import torch.nn as nn import triton import triton.language as tl @triton.jit def fp8_gemm_kernel_fast( a_ptr, b_ptr, c_ptr, weight_scale_ptr, M, N, K, stride_am, stride_ak, stride_bn, stride_bk, stride_cm, stride_cn, BLOCK_SIZE_M: tl.constexpr, BLOCK_SIZE_N: tl.constexpr, BLOCK_SIZE_K: tl.constexpr, GROUP_SIZE_M: tl.constexpr, EVEN_M: tl.constexpr, EVEN_N: tl.constexpr, EVEN_K: tl.constexpr, ): pid = tl.program_id(axis=0) num_pid_m = tl.cdiv(M, BLOCK_SIZE_M) num_pid_n = tl.cdiv(N, BLOCK_SIZE_N) # Swizzling width = GROUP_SIZE_M * num_pid_n group_id = pid // width row_idx = group_id * GROUP_SIZE_M col_idx = pid % num_pid_n pid_m = row_idx + (pid % width) // num_pid_n pid_n = col_idx if pid_m >= num_pid_m: return offs_am = pid_m * BLOCK_SIZE_M + tl.arange(0, BLOCK_SIZE_M) offs_bn = pid_n * BLOCK_SIZE_N + tl.arange(0, BLOCK_SIZE_N) offs_k = tl.arange(0, BLOCK_SIZE_K) accumulator = tl.zeros((BLOCK_SIZE_M, BLOCK_SIZE_N), dtype=tl.float32) clamped_am = tl.where(offs_am < M, offs_am, 0) clamped_bn = tl.where(offs_bn < N, offs_bn, 0) if EVEN_K: # Fast path: pointers incremented outside loop a_ptrs = a_ptr + clamped_am[:, None] * stride_am + offs_k[None, :] * stride_ak b_ptrs = b_ptr + clamped_bn[None, :] * stride_bn + offs_k[:, None] * stride_bk for k in range(0, tl.cdiv(K, BLOCK_SIZE_K)): if not EVEN_M: mask_a = offs_am[:, None] < M a = tl.load(a_ptrs, mask=mask_a, other=0.0) else: a = tl.load(a_ptrs) if not EVEN_N: mask_b = offs_bn[None, :] < N b = tl.load(b_ptrs, mask=mask_b, other=0.0) else: b = tl.load(b_ptrs) accumulator = tl.dot(a, b, accumulator, out_dtype=tl.float32) a_ptrs += BLOCK_SIZE_K * stride_ak b_ptrs += BLOCK_SIZE_K * stride_bk else: # Safe path: pointers recomputed with clamping inside loop for k in range(0, tl.cdiv(K, BLOCK_SIZE_K)): offs_k_current = k * BLOCK_SIZE_K + tl.arange(0, BLOCK_SIZE_K) clamped_k = tl.where(offs_k_current < K, offs_k_current, 0) a_ptrs = a_ptr + clamped_am[:, None] * stride_am + clamped_k[None, :] * stride_ak b_ptrs = b_ptr + clamped_bn[None, :] * stride_bn + clamped_k[:, None] * stride_bk mask_a = (offs_am[:, None] < M) & (offs_k_current[None, :] < K) mask_b = (offs_bn[None, :] < N) & (offs_k_current[:, None] < K) a = tl.load(a_ptrs, mask=mask_a, other=0.0) b = tl.load(b_ptrs, mask=mask_b, other=0.0) accumulator = tl.dot(a, b, accumulator, out_dtype=tl.float32) if EVEN_N: scales = tl.load(weight_scale_ptr + offs_bn) else: scales = tl.load(weight_scale_ptr + clamped_bn, mask=offs_bn < N, other=1.0) accumulator = accumulator * scales[None, :] out = accumulator.to(tl.bfloat16) c_ptrs = c_ptr + clamped_am[:, None] * stride_cm + clamped_bn[None, :] * stride_cn if EVEN_M and EVEN_N: tl.store(c_ptrs, out) else: mask_c = (offs_am[:, None] < M) & (offs_bn[None, :] < N) tl.store(c_ptrs, out, mask=mask_c) def run_triton_gemm(x, weight, weight_scale): M, K = x.shape N, K_w = weight.shape c = torch.empty((M, N), dtype=torch.bfloat16, device=x.device) if M <= 64: BLOCK_SIZE_M = 32 BLOCK_SIZE_N = 128 BLOCK_SIZE_K = 64 GROUP_SIZE_M = 1 else: BLOCK_SIZE_M = 128 BLOCK_SIZE_N = 128 BLOCK_SIZE_K = 64 GROUP_SIZE_M = 8 EVEN_M = (M % BLOCK_SIZE_M == 0) EVEN_N = (N % BLOCK_SIZE_N == 0) EVEN_K = (K % BLOCK_SIZE_K == 0) grid = lambda META: (triton.cdiv(M, BLOCK_SIZE_M) * triton.cdiv(N, BLOCK_SIZE_N),) fp8_gemm_kernel_fast[grid]( x, weight, c, weight_scale, M, N, K, x.stride(0), x.stride(1), weight.stride(0), weight.stride(1), c.stride(0), c.stride(1), BLOCK_SIZE_M=BLOCK_SIZE_M, BLOCK_SIZE_N=BLOCK_SIZE_N, BLOCK_SIZE_K=BLOCK_SIZE_K, GROUP_SIZE_M=GROUP_SIZE_M, EVEN_M=EVEN_M, EVEN_N=EVEN_N, EVEN_K=EVEN_K, ) return c class Model(nn.Module): """y = ((x @ w.T) * weight_scale).to(bf16). x: fp8_e4m3 (M, K). w: fp8_e4m3 (N, K) normalized to the e4m3 range. weight_scale: (N,) per-output-channel dequant scale. """ def __init__(self, M: int, N: int, K: int): super().__init__() self.M, self.N, self.K = M, N, K self.register_buffer("weight", torch.empty(N, K, dtype=torch.float8_e4m3fn)) self.register_buffer("weight_scale", torch.empty(N, dtype=torch.float32)) def forward(self, x: torch.Tensor) -> torch.Tensor: return run_triton_gemm(x, self.weight, self.weight_scale) def get_inputs(): x = (torch.rand(4096, 4096) * 8 - 4).to(torch.float8_e4m3fn) return [x] def get_init_inputs(): return [4096, 4096, 4096]