"""Kimi Delta Attention forward (chunk form) — custom Triton kernels for H100 (SM90). Chunk-parallel KDA forward written from scratch (no FLA imports): Kernel 1 `_kda_prep_kernel` (per chunk, fully parallel): g_cum = in-chunk cumsum of the per-channel log-decay (x 1/ln2, exp2 form) L = strict-lower(diag(beta) . (k e^{g}) (k e^{-g})^T) X = (I + L)^{-1} diag(beta) -- Newton-Schulz doubling (exact: L nilpotent) w = X (e^{g} k), u = X v kd = k e^{-g} (kg = kd * e^{g_last} is recovered in kernel 2) qg = q e^{g} * scale glast = g_cum[last] (fp32, per chunk) Kernel 2 `_kda_state_kernel` (sequential over chunks per (batch, head, V-block)), all tiles kept V-major so the register-resident operand is always the MMA "A" side: v_new_i^T = u_i^T - h_i^T @ w_i^T (h_i = state at chunk start, in registers) h_{i+1}^T = (h_i^T + v_new_i^T @ kd_i) .. e^{glast} stores h_i^T as (V, K) bf16 and v_new (T, V) bf16 Kernel 3 `_kda_out_kernel` (per chunk, fully parallel): Aqk = mask_lower(qg @ kd^T) o_i = qg_i @ h_i + Aqk @ v_new_i """ from __future__ import annotations import torch import torch.nn as nn import triton import triton.language as tl OP_TYPE = "linear_attention" SUPPORTED_PRECISIONS = ["bf16"] HARDWARE_REQUIRED = ["RTX_PRO_6000", "H100", "B200"] RCP_LN2 = tl.constexpr(1.4426950408889634) @triton.jit def _kda_prep_kernel( q, k, v, g, beta, w, u, kd, qg, glast, scale, T: tl.constexpr, H: tl.constexpr, K: tl.constexpr, V: tl.constexpr, BT: tl.constexpr, ): i_t, i_bh = tl.program_id(0), tl.program_id(1) i_b, i_h = i_bh // H, i_bh % H bos = i_b * T NT = T // BT # ---- gates ---- p_g = tl.make_block_ptr(g + (bos * H + i_h) * K, (T, K), (H * K, 1), (i_t * BT, 0), (BT, K), (1, 0)) b_g = tl.load(p_g).to(tl.float32) b_gl = tl.sum(b_g, 0) * RCP_LN2 # last row of the cumsum b_g = tl.cumsum(b_g, 0) * RCP_LN2 o_k = tl.arange(0, K) tl.store(glast + ((i_b * NT + i_t) * H + i_h) * K + o_k, b_gl) b_e = tl.exp2(b_g) p_k = tl.make_block_ptr(k + (bos * H + i_h) * K, (T, K), (H * K, 1), (i_t * BT, 0), (BT, K), (1, 0)) b_k = tl.load(p_k) b_kg = (b_k * b_e).to(tl.bfloat16) # k e^{g} b_kd = (b_k / b_e).to(tl.bfloat16) # k e^{-g} p_kd = tl.make_block_ptr(kd + (bos * H + i_h) * K, (T, K), (H * K, 1), (i_t * BT, 0), (BT, K), (1, 0)) tl.store(p_kd, b_kd) p_q = tl.make_block_ptr(q + (bos * H + i_h) * K, (T, K), (H * K, 1), (i_t * BT, 0), (BT, K), (1, 0)) b_q = tl.load(p_q) b_qg = (b_q * b_e * scale).to(tl.bfloat16) p_qg = tl.make_block_ptr(qg + (bos * H + i_h) * K, (T, K), (H * K, 1), (i_t * BT, 0), (BT, K), (1, 0)) tl.store(p_qg, b_qg) # ---- A = strict_lower(diag(beta) (k e^g)(k e^-g)^T) ---- p_beta = tl.make_block_ptr(beta + bos * H + i_h, (T,), (H,), (i_t * BT,), (BT,), (0,)) b_beta = tl.load(p_beta).to(tl.float32) b_A = tl.dot(b_kg, tl.trans(b_kd)) o_i = tl.arange(0, BT) m_lo = o_i[:, None] > o_i[None, :] b_L = tl.where(m_lo, b_A * b_beta[:, None], 0.0).to(tl.bfloat16) # ---- X = (I + L)^{-1} via Newton-Schulz: X <- X(2I - (I+L)X); exact after 5 iters ---- m_eye = o_i[:, None] == o_i[None, :] b_eye = tl.where(m_eye, 1.0, 0.0) b_X = (b_eye - b_L).to(tl.bfloat16) for _ in tl.static_range(4): b_t = (b_X + tl.dot(b_L, b_X)).to(tl.bfloat16) # (I+L) X b_X = (2.0 * b_X - tl.dot(b_X, b_t)).to(tl.bfloat16) b_t = (b_X + tl.dot(b_L, b_X)).to(tl.bfloat16) b_Xf = (2.0 * b_X.to(tl.float32) - tl.dot(b_X, b_t)) * b_beta[None, :] b_Xb = b_Xf.to(tl.bfloat16) # X <- X diag(beta) # ---- w = X (e^g k), u = X v ---- b_w = tl.dot(b_Xb, b_kg) p_w = tl.make_block_ptr(w + (bos * H + i_h) * K, (T, K), (H * K, 1), (i_t * BT, 0), (BT, K), (1, 0)) tl.store(p_w, b_w.to(w.dtype.element_ty)) p_v = tl.make_block_ptr(v + (bos * H + i_h) * V, (T, V), (H * V, 1), (i_t * BT, 0), (BT, V), (1, 0)) b_v = tl.load(p_v) b_u = tl.dot(b_Xb, b_v) p_u = tl.make_block_ptr(u + (bos * H + i_h) * V, (T, V), (H * V, 1), (i_t * BT, 0), (BT, V), (1, 0)) tl.store(p_u, b_u.to(u.dtype.element_ty)) @triton.jit def _kda_state_kernel( w, u, kd, glast, h, v_new, T: tl.constexpr, H: tl.constexpr, K: tl.constexpr, V: tl.constexpr, BT: tl.constexpr, BV: tl.constexpr, ): i_v, i_bh = tl.program_id(0), tl.program_id(1) i_b, i_h = i_bh // H, i_bh % H bos = i_b * T NT = T // BT # V-major state: b_h*[BV, 64] so it is always the MMA "A" (register) operand. b_h1 = tl.zeros([BV, 64], dtype=tl.float32) b_h2 = tl.zeros([BV, 64], dtype=tl.float32) w += (bos * H + i_h) * K u += (bos * H + i_h) * V kd += (bos * H + i_h) * K v_new += (bos * H + i_h) * V h += ((i_b * NT * H) + i_h) * K * V # h layout: (B, NT, H, V, K) glast += (i_b * NT * H + i_h) * K for i_t in range(NT): # store chunk-start state (V, K) p_h1 = tl.make_block_ptr(h + i_t * H * K * V, (V, K), (K, 1), (i_v * BV, 0), (BV, 64), (1, 0)) p_h2 = tl.make_block_ptr(h + i_t * H * K * V, (V, K), (K, 1), (i_v * BV, 64), (BV, 64), (1, 0)) tl.store(p_h1, b_h1.to(h.dtype.element_ty)) tl.store(p_h2, b_h2.to(h.dtype.element_ty)) # v_new^T = u^T - h^T @ w^T p_w1 = tl.make_block_ptr(w, (K, T), (1, H * K), (0, i_t * BT), (64, BT), (0, 1)) p_w2 = tl.make_block_ptr(w, (K, T), (1, H * K), (64, i_t * BT), (64, BT), (0, 1)) b_w1 = tl.load(p_w1) b_w2 = tl.load(p_w2) b_vn = tl.dot(b_h1.to(b_w1.dtype), b_w1) + tl.dot(b_h2.to(b_w2.dtype), b_w2) p_u = tl.make_block_ptr(u, (V, T), (1, H * V), (i_v * BV, i_t * BT), (BV, BT), (0, 1)) b_vn = tl.load(p_u).to(tl.float32) - b_vn p_vn = tl.make_block_ptr(v_new, (V, T), (1, H * V), (i_v * BV, i_t * BT), (BV, BT), (0, 1)) b_vnb = b_vn.to(v_new.dtype.element_ty) tl.store(p_vn, b_vnb) # h^T = (h^T + v_new^T @ kd) * e^{glast} o_k = tl.arange(0, 64) b_gl1 = tl.exp2(tl.load(glast + i_t * H * K + o_k)) b_gl2 = tl.exp2(tl.load(glast + i_t * H * K + 64 + o_k)) p_kd1 = tl.make_block_ptr(kd, (T, K), (H * K, 1), (i_t * BT, 0), (BT, 64), (1, 0)) p_kd2 = tl.make_block_ptr(kd, (T, K), (H * K, 1), (i_t * BT, 64), (BT, 64), (1, 0)) b_kd1 = tl.load(p_kd1) b_kd2 = tl.load(p_kd2) b_h1 = (b_h1 + tl.dot(b_vnb, b_kd1)) * b_gl1[None, :] b_h2 = (b_h2 + tl.dot(b_vnb, b_kd2)) * b_gl2[None, :] @triton.jit def _kda_out_kernel( qg, kd, v_new, h, o, T: tl.constexpr, H: tl.constexpr, K: tl.constexpr, V: tl.constexpr, BT: tl.constexpr, BV: tl.constexpr, ): i_v, i_t, i_bh = tl.program_id(0), tl.program_id(1), tl.program_id(2) i_b, i_h = i_bh // H, i_bh % H bos = i_b * T NT = T // BT p_qg1 = tl.make_block_ptr(qg + (bos * H + i_h) * K, (T, K), (H * K, 1), (i_t * BT, 0), (BT, 64), (1, 0)) p_qg2 = tl.make_block_ptr(qg + (bos * H + i_h) * K, (T, K), (H * K, 1), (i_t * BT, 64), (BT, 64), (1, 0)) b_qg1 = tl.load(p_qg1) b_qg2 = tl.load(p_qg2) # h stored (V, K); load transposed tiles (K, BV) h += ((i_b * NT + i_t) * H + i_h) * K * V p_h1 = tl.make_block_ptr(h, (K, V), (1, K), (0, i_v * BV), (64, BV), (0, 1)) p_h2 = tl.make_block_ptr(h, (K, V), (1, K), (64, i_v * BV), (64, BV), (0, 1)) b_h1 = tl.load(p_h1) b_h2 = tl.load(p_h2) b_o = tl.dot(b_qg1, b_h1) + tl.dot(b_qg2, b_h2) # Aqk = mask_lower_incl_diag(qg @ kd^T) p_kd1 = tl.make_block_ptr(kd + (bos * H + i_h) * K, (K, T), (1, H * K), (0, i_t * BT), (64, BT), (0, 1)) p_kd2 = tl.make_block_ptr(kd + (bos * H + i_h) * K, (K, T), (1, H * K), (64, i_t * BT), (64, BT), (0, 1)) b_kd1 = tl.load(p_kd1) b_kd2 = tl.load(p_kd2) b_A = tl.dot(b_qg1, b_kd1) + tl.dot(b_qg2, b_kd2) o_i = tl.arange(0, BT) b_A = tl.where(o_i[:, None] >= o_i[None, :], b_A, 0.0) p_vn = tl.make_block_ptr(v_new + (bos * H + i_h) * V, (T, V), (H * V, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0)) b_vn = tl.load(p_vn) b_o += tl.dot(b_A.to(b_vn.dtype), b_vn) p_o = tl.make_block_ptr(o + (bos * H + i_h) * V, (T, V), (H * V, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0)) tl.store(p_o, b_o.to(o.dtype.element_ty)) _WORKSPACE_CACHE: dict = {} def _get_workspace(B, T, H, K, V, NT, device): key = (B, T, H, K, V, device) ws = _WORKSPACE_CACHE.get(key) if ws is None: ws = dict( w=torch.empty(B, T, H, K, dtype=torch.bfloat16, device=device), u=torch.empty(B, T, H, V, dtype=torch.bfloat16, device=device), kd=torch.empty(B, T, H, K, dtype=torch.bfloat16, device=device), qg=torch.empty(B, T, H, K, dtype=torch.bfloat16, device=device), glast=torch.empty(B, NT, H, K, dtype=torch.float32, device=device), h=torch.empty(B, NT, H, V, K, dtype=torch.bfloat16, device=device), v_new=torch.empty(B, T, H, V, dtype=torch.bfloat16, device=device), ) _WORKSPACE_CACHE[key] = ws return ws def _kda_fwd(q, k, v, g, beta, scale, chunk_size=64): B, T, H, K = q.shape V = v.shape[-1] BT = chunk_size assert T % BT == 0 and BT == 64 and K == 128 and V == 128 NT = T // BT q = q.contiguous() k = k.contiguous() v = v.contiguous() g = g.contiguous() beta = beta.contiguous() ws = _get_workspace(B, T, H, K, V, NT, q.device) w, u, kd, qg, glast, h, v_new = ( ws["w"], ws["u"], ws["kd"], ws["qg"], ws["glast"], ws["h"], ws["v_new"] ) o = torch.empty(B, T, H, V, dtype=torch.bfloat16, device=q.device) _kda_prep_kernel[(NT, B * H)]( q, k, v, g, beta, w, u, kd, qg, glast, scale, T=T, H=H, K=K, V=V, BT=BT, num_warps=8, num_stages=1, ) BV = 64 _kda_state_kernel[(V // BV, B * H)]( w, u, kd, glast, h, v_new, T=T, H=H, K=K, V=V, BT=BT, BV=BV, num_warps=4, num_stages=3, ) _kda_out_kernel[(V // BV, NT, B * H)]( qg, kd, v_new, h, o, T=T, H=H, K=K, V=V, BT=BT, BV=BV, num_warps=4, num_stages=2, ) return o class Model(nn.Module): """KDA forward (chunk form). No learned parameters; all inputs are activations.""" def __init__(self, B: int, T: int, H: int, K: int, V: int, chunk_size: int = 64): super().__init__() self.B, self.T, self.H, self.K, self.V = B, T, H, K, V self.chunk_size = chunk_size self.scale = float(K) ** -0.5 self.register_buffer("_dummy", torch.zeros(1), persistent=False) def forward(self, q, k, v, g, beta): return _kda_fwd(q, k, v, g, beta, scale=self.scale, chunk_size=self.chunk_size) # Module-level shape shims (overridden by check.py / benchmark.py per shape). B = 2 T = 1024 H = 8 K = 128 V = 128 CHUNK_SIZE = 64 def get_inputs(): torch.manual_seed(0) q = torch.randn(B, T, H, K, dtype=torch.bfloat16) * 0.1 k = torch.randn(B, T, H, K, dtype=torch.bfloat16) * 0.1 v = torch.randn(B, T, H, V, dtype=torch.bfloat16) * 0.1 g = (torch.randn(B, T, H, K, dtype=torch.float32) * 0.1 - 0.05) beta = torch.sigmoid(torch.randn(B, T, H, dtype=torch.bfloat16)) return [q, k, v, g, beta] def get_init_inputs(): return [B, T, H, K, V, CHUNK_SIZE]