import torch import torch.nn as nn import triton import triton.language as tl @triton.jit def _pack_k_kernel(src, dst, R: tl.constexpr, K: tl.constexpr, KP: tl.constexpr, BR: tl.constexpr, BC: tl.constexpr): pid_r = tl.program_id(0) pid_c = tl.program_id(1) offs_r = pid_r * BR + tl.arange(0, BR) offs_c = pid_c * BC + tl.arange(0, BC) vals = tl.load(src + offs_r[:, None] * K + offs_c[None, :], mask=(offs_r[:, None] < R) & (offs_c[None, :] < K), other=0.0) tl.store(dst + offs_r[:, None] * KP + offs_c[None, :], vals, mask=(offs_r[:, None] < R) & (offs_c[None, :] < KP)) @triton.jit def _fp8_gemm_kernel(x, w, s, y, M: tl.constexpr, N: tl.constexpr, K: tl.constexpr, BM: tl.constexpr, BN: tl.constexpr, BK: tl.constexpr, GROUP_M: tl.constexpr): pid = tl.program_id(0) num_m = tl.cdiv(M, BM) num_n = tl.cdiv(N, BN) group = GROUP_M * num_n gid = pid // group first_m = gid * GROUP_M group_m = tl.minimum(num_m - first_m, GROUP_M) rem = pid - gid * group pid_m = first_m + (rem % group_m) pid_n = rem // group_m offs_m = (pid_m * BM + tl.arange(0, BM)) % M offs_n = (pid_n * BN + tl.arange(0, BN)) % N offs_k = tl.arange(0, BK) x_ptrs = x + offs_m[:, None] * K + offs_k[None, :] w_ptrs = w + offs_n[:, None] * K + offs_k[None, :] acc = tl.zeros((BM, BN), tl.float32) k_even: tl.constexpr = K - (K % BK) for _ in range(0, k_even, BK): a = tl.load(x_ptrs) b = tl.load(w_ptrs) acc = tl.dot(a, b.T, acc) x_ptrs += BK w_ptrs += BK if K % BK != 0: k = k_even + offs_k a = tl.load(x + offs_m[:, None] * K + k[None, :], mask=k[None, :] < K, other=0.0) b = tl.load(w + offs_n[:, None] * K + k[None, :], mask=k[None, :] < K, other=0.0) acc = tl.dot(a, b.T, acc) offs_mo = pid_m * BM + tl.arange(0, BM) offs_no = pid_n * BN + tl.arange(0, BN) scale = tl.load(s + offs_no, mask=offs_no < N, other=0.0).to(tl.float32) out = (acc * scale[None, :]).to(tl.bfloat16) tl.store(y + offs_mo[:, None] * N + offs_no[None, :], out, mask=(offs_mo[:, None] < M) & (offs_no[None, :] < N)) def _pick_config(M: int, N: int, K: int): if M <= 64: bm = 16 if M % 16 == 0 else triton.next_power_of_2(M) return dict(bm=bm, bn=32, bk=256, group_m=1, warps=4, stages=3) return dict(bm=128, bn=256, bk=128, group_m=8, warps=8, stages=3) class Model(nn.Module): 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)) self._cfg = _pick_config(M, N, K) self._w_pack = None self._w_pack_version = -1 self._kp = None self._y = None self._x_pack = None self._x_src = None self._x_pack_version = -1 self._graph = None self._static_in = None self._graph_wver = -1 def _kp_padded(self): bk = self._cfg["bk"] return triton.cdiv(self.K, bk) * bk def _ensure_wpack(self, device): kp = self._kp_padded() if self.K % self._cfg["bk"] != 0 and (self._w_pack is None or self._w_pack_version != self.weight._version): self._w_pack = torch.empty((self.N, kp), device=device, dtype=torch.float8_e4m3fn) self._w_pack_version = self.weight._version self._kp = kp _pack_k_kernel[(triton.cdiv(self.N, 16), triton.cdiv(kp, 128))]( self.weight, self._w_pack, self.N, self.K, kp, BR=16, BC=128, num_warps=4) def _launch(self, xbuf, w, K, y): c = self._cfg grid = (triton.cdiv(self.M, c["bm"]) * triton.cdiv(self.N, c["bn"]),) _fp8_gemm_kernel[grid](xbuf, w, self.weight_scale, y, self.M, self.N, K, BM=c["bm"], BN=c["bn"], BK=c["bk"], GROUP_M=c["group_m"], num_warps=c["warps"], num_stages=c["stages"]) def _operands(self, x): if self.K % self._cfg["bk"] != 0: self._ensure_wpack(x.device) kp = self._kp if (self._x_pack is None or self._x_src is not x or self._x_pack_version != x._version): if self._x_pack is None: self._x_pack = torch.empty((self.M, kp), device=x.device, dtype=torch.float8_e4m3fn) self._x_src = x self._x_pack_version = x._version _pack_k_kernel[(triton.cdiv(self.M, 16), triton.cdiv(kp, 128))]( x, self._x_pack, self.M, self.K, kp, BR=16, BC=128, num_warps=4) return self._x_pack, self._w_pack, kp return x, self.weight, self.K def forward(self, x: torch.Tensor) -> torch.Tensor: if self._y is None: self._y = torch.empty((self.M, self.N), device=x.device, dtype=torch.bfloat16) y = self._y if self.M <= 64 and self.K % self._cfg["bk"] == 0 and x.is_cuda: if self._graph is None or self._graph_wver != self.weight._version: self._static_in = torch.empty_like(x) self._static_in.copy_(x) s = torch.cuda.Stream() s.wait_stream(torch.cuda.current_stream()) with torch.cuda.stream(s): for _ in range(3): self._launch(self._static_in, self.weight, self.K, y) torch.cuda.current_stream().wait_stream(s) torch.cuda.synchronize() self._graph = torch.cuda.CUDAGraph() with torch.cuda.graph(self._graph): self._launch(self._static_in, self.weight, self.K, y) self._graph_wver = self.weight._version self._static_in.copy_(x) self._graph.replay() return y xb, wb, Ku = self._operands(x) self._launch(xb, wb, Ku, y) return y M = 4096 N = 4096 K = 4096 def get_inputs(): x = (torch.rand(M, K) * 8 - 4).to(torch.float8_e4m3fn) return [x] def get_init_inputs(): return [M, N, K]