"""Kimi Delta Attention (KDA) forward, chunk form — custom Triton kernel. Matches the naive reference in reference.py (chunk-parallel KDA forward): intra-chunk: g = cumsum(g) # within chunk e = exp(g); a = k*e; b = k/e; qq = (q*scale)*e L[i,j] = -beta[i] * (b[i] . a[j]) for j < i (strictly lower) Ainv = (I - L)^{-1} w = Ainv @ (beta*a) ; u = Ainv @ (beta*v) Aqk[i,j] = qq[i] . b[j] for j <= i qqp = qq - Aqk @ w ; ou = Aqk @ u inter-chunk recurrence: v_i = u - w @ S o = ou + qqp @ S (reduced form) S = e_last * (S + b^T @ v_i) Two kernels: * kda_intra: one program per (b, h, chunk). The 64x64 inversion is done via 16x16 sub-chunk block forward substitution (doubling on the diagonal blocks + merged off-diagonal inverse), which keeps register pressure low and cuts inversion FLOPs ~3x. * kda_recurrent: one program per (b, h, v-slice) — sequential scan over chunks. w@S and qqp@S are issued as two independent dots (they overlap on the tensor cores), and the next chunk's loads are software-prefetched one chunk ahead. """ from __future__ import annotations import torch import torch.nn as nn import triton import triton.language as tl # --------------------------------------------------------------------------- # Kernel 1: intra-chunk prepare (16x16 sub-chunk block forward substitution) # --------------------------------------------------------------------------- @triton.jit def kda_intra_kernel( q_ptr, k_ptr, v_ptr, g_ptr, beta_ptr, w_ptr, u_ptr, qqp_ptr, ou_ptr, b_ptr, elast_ptr, scale, T, H, K: tl.constexpr, V: tl.constexpr, BT: tl.constexpr, BC: tl.constexpr, ): pid = tl.program_id(0) NT = T // BT n = pid % NT bh = pid // NT b = bh // H h = bh % H t16 = tl.arange(0, BC) d = tl.arange(0, K) dv = tl.arange(0, V) q_base = b * (T * H * K) + h * K + (n * BT) * (H * K) v_base = b * (T * H * V) + h * V + (n * BT) * (H * V) beta_base = b * (T * H) + h + (n * BT) * H r0 = t16 r1 = BC + t16 r2 = 2 * BC + t16 r3 = 3 * BC + t16 q0 = tl.load(q_ptr + q_base + r0[:, None] * (H * K) + d[None, :]).to(tl.float16) q1 = tl.load(q_ptr + q_base + r1[:, None] * (H * K) + d[None, :]).to(tl.float16) q2 = tl.load(q_ptr + q_base + r2[:, None] * (H * K) + d[None, :]).to(tl.float16) q3 = tl.load(q_ptr + q_base + r3[:, None] * (H * K) + d[None, :]).to(tl.float16) k0 = tl.load(k_ptr + q_base + r0[:, None] * (H * K) + d[None, :]).to(tl.float16) k1 = tl.load(k_ptr + q_base + r1[:, None] * (H * K) + d[None, :]).to(tl.float16) k2 = tl.load(k_ptr + q_base + r2[:, None] * (H * K) + d[None, :]).to(tl.float16) k3 = tl.load(k_ptr + q_base + r3[:, None] * (H * K) + d[None, :]).to(tl.float16) g0 = tl.load(g_ptr + q_base + r0[:, None] * (H * K) + d[None, :]).to(tl.float32) g1 = tl.load(g_ptr + q_base + r1[:, None] * (H * K) + d[None, :]).to(tl.float32) g2 = tl.load(g_ptr + q_base + r2[:, None] * (H * K) + d[None, :]).to(tl.float32) g3 = tl.load(g_ptr + q_base + r3[:, None] * (H * K) + d[None, :]).to(tl.float32) v0 = tl.load(v_ptr + v_base + r0[:, None] * (H * V) + dv[None, :]).to(tl.float16) v1 = tl.load(v_ptr + v_base + r1[:, None] * (H * V) + dv[None, :]).to(tl.float16) v2 = tl.load(v_ptr + v_base + r2[:, None] * (H * V) + dv[None, :]).to(tl.float16) v3 = tl.load(v_ptr + v_base + r3[:, None] * (H * V) + dv[None, :]).to(tl.float16) b0 = tl.load(beta_ptr + beta_base + r0 * H).to(tl.float32) b1 = tl.load(beta_ptr + beta_base + r1 * H).to(tl.float32) b2 = tl.load(beta_ptr + beta_base + r2 * H).to(tl.float32) b3 = tl.load(beta_ptr + beta_base + r3 * H).to(tl.float32) s0 = tl.sum(g0, axis=0) e0 = tl.exp(tl.cumsum(g0, axis=0)) s1 = s0 + tl.sum(g1, axis=0) e1 = tl.exp(s0[None, :] + tl.cumsum(g1, axis=0)) s2 = s1 + tl.sum(g2, axis=0) e2 = tl.exp(s1[None, :] + tl.cumsum(g2, axis=0)) s3 = s2 + tl.sum(g3, axis=0) e3 = tl.exp(s2[None, :] + tl.cumsum(g3, axis=0)) e_last = tl.exp(s3) bb0 = (k0 / e0).to(tl.float16) bb1 = (k1 / e1).to(tl.float16) bb2 = (k2 / e2).to(tl.float16) bb3 = (k3 / e3).to(tl.float16) qq0 = (q0 * scale * e0).to(tl.float16) qq1 = (q1 * scale * e1).to(tl.float16) qq2 = (q2 * scale * e2).to(tl.float16) qq3 = (q3 * scale * e3).to(tl.float16) ba0 = (b0[:, None] * k0 * e0).to(tl.float16) ba1 = (b1[:, None] * k1 * e1).to(tl.float16) ba2 = (b2[:, None] * k2 * e2).to(tl.float16) ba3 = (b3[:, None] * k3 * e3).to(tl.float16) bv0 = (b0[:, None] * v0).to(tl.float16) bv1 = (b1[:, None] * v1).to(tl.float16) bv2 = (b2[:, None] * v2).to(tl.float16) bv3 = (b3[:, None] * v3).to(tl.float16) mask_strict = t16[:, None] > t16[None, :] mask_incl = t16[:, None] >= t16[None, :] # L blocks (strictly lower triangular 64x64, 16x16 tiles) # L[i,j] = -beta[i] * (a[i].bb[j]) = -ba[i].bb[j] L00 = tl.where(mask_strict, -tl.dot(ba0, tl.trans(bb0)), 0.0) L11 = tl.where(mask_strict, -tl.dot(ba1, tl.trans(bb1)), 0.0) L22 = tl.where(mask_strict, -tl.dot(ba2, tl.trans(bb2)), 0.0) L33 = tl.where(mask_strict, -tl.dot(ba3, tl.trans(bb3)), 0.0) L10 = -tl.dot(ba1, tl.trans(bb0)) L20 = -tl.dot(ba2, tl.trans(bb0)) L21 = -tl.dot(ba2, tl.trans(bb1)) L30 = -tl.dot(ba3, tl.trans(bb0)) L31 = -tl.dot(ba3, tl.trans(bb1)) L32 = -tl.dot(ba3, tl.trans(bb2)) # diagonal 16x16 inverses via doubling I16 = tl.where(t16[:, None] == t16[None, :], 1.0, 0.0) D2 = tl.dot(L00.to(tl.float16), L00.to(tl.float16)) D4 = tl.dot(D2.to(tl.float16), D2.to(tl.float16)) D8 = tl.dot(D4.to(tl.float16), D4.to(tl.float16)) Di = tl.dot((I16 + L00).to(tl.float16), (I16 + D2).to(tl.float16)) Di = tl.dot(Di.to(tl.float16), (I16 + D4).to(tl.float16)) Ai00 = tl.dot(Di.to(tl.float16), (I16 + D8).to(tl.float16)) D2 = tl.dot(L11.to(tl.float16), L11.to(tl.float16)) D4 = tl.dot(D2.to(tl.float16), D2.to(tl.float16)) D8 = tl.dot(D4.to(tl.float16), D4.to(tl.float16)) Di = tl.dot((I16 + L11).to(tl.float16), (I16 + D2).to(tl.float16)) Di = tl.dot(Di.to(tl.float16), (I16 + D4).to(tl.float16)) Ai11 = tl.dot(Di.to(tl.float16), (I16 + D8).to(tl.float16)) D2 = tl.dot(L22.to(tl.float16), L22.to(tl.float16)) D4 = tl.dot(D2.to(tl.float16), D2.to(tl.float16)) D8 = tl.dot(D4.to(tl.float16), D4.to(tl.float16)) Di = tl.dot((I16 + L22).to(tl.float16), (I16 + D2).to(tl.float16)) Di = tl.dot(Di.to(tl.float16), (I16 + D4).to(tl.float16)) Ai22 = tl.dot(Di.to(tl.float16), (I16 + D8).to(tl.float16)) D2 = tl.dot(L33.to(tl.float16), L33.to(tl.float16)) D4 = tl.dot(D2.to(tl.float16), D2.to(tl.float16)) D8 = tl.dot(D4.to(tl.float16), D4.to(tl.float16)) Di = tl.dot((I16 + L33).to(tl.float16), (I16 + D2).to(tl.float16)) Di = tl.dot(Di.to(tl.float16), (I16 + D4).to(tl.float16)) Ai33 = tl.dot(Di.to(tl.float16), (I16 + D8).to(tl.float16)) # off-diagonal merged inverse Ai10 = tl.dot(Ai11.to(tl.float16), tl.dot(L10.to(tl.float16), Ai00.to(tl.float16)).to(tl.float16)) Ai21 = tl.dot(Ai22.to(tl.float16), tl.dot(L21.to(tl.float16), Ai11.to(tl.float16)).to(tl.float16)) Ai20 = tl.dot(Ai22.to(tl.float16), (tl.dot(L20.to(tl.float16), Ai00.to(tl.float16)) + tl.dot(L21.to(tl.float16), Ai10.to(tl.float16))).to(tl.float16)) Ai32 = tl.dot(Ai33.to(tl.float16), tl.dot(L32.to(tl.float16), Ai22.to(tl.float16)).to(tl.float16)) Ai31 = tl.dot(Ai33.to(tl.float16), (tl.dot(L31.to(tl.float16), Ai11.to(tl.float16)) + tl.dot(L32.to(tl.float16), Ai21.to(tl.float16))).to(tl.float16)) Ai30 = tl.dot(Ai33.to(tl.float16), (tl.dot(L30.to(tl.float16), Ai00.to(tl.float16)) + tl.dot(L31.to(tl.float16), Ai10.to(tl.float16)) + tl.dot(L32.to(tl.float16), Ai20.to(tl.float16))).to(tl.float16)) # Aqk = tril(qq @ bb^T) blocks (lower-inclusive) — computed before w/u so bb # tiles can be stored and freed from registers early. Aqk00 = tl.where(mask_incl, tl.dot(qq0, tl.trans(bb0)), 0.0) Aqk10 = tl.dot(qq1, tl.trans(bb0)) Aqk11 = tl.where(mask_incl, tl.dot(qq1, tl.trans(bb1)), 0.0) Aqk20 = tl.dot(qq2, tl.trans(bb0)) Aqk21 = tl.dot(qq2, tl.trans(bb1)) Aqk22 = tl.where(mask_incl, tl.dot(qq2, tl.trans(bb2)), 0.0) Aqk30 = tl.dot(qq3, tl.trans(bb0)) Aqk31 = tl.dot(qq3, tl.trans(bb1)) Aqk32 = tl.dot(qq3, tl.trans(bb2)) Aqk33 = tl.where(mask_incl, tl.dot(qq3, tl.trans(bb3)), 0.0) slab = bh * NT + n tl.store(b_ptr + slab * (BT * K) + r0[:, None] * K + d[None, :], bb0) tl.store(b_ptr + slab * (BT * K) + r1[:, None] * K + d[None, :], bb1) tl.store(b_ptr + slab * (BT * K) + r2[:, None] * K + d[None, :], bb2) tl.store(b_ptr + slab * (BT * K) + r3[:, None] * K + d[None, :], bb3) tl.store(elast_ptr + slab * K + d, e_last) # w = Ainv @ ba ; qqp = qq - Aqk @ w (computed + stored together) w0 = tl.dot(Ai00.to(tl.float16), ba0) w1 = tl.dot(Ai11.to(tl.float16), ba1) + tl.dot(Ai10.to(tl.float16), ba0) w2 = tl.dot(Ai22.to(tl.float16), ba2) + tl.dot(Ai21.to(tl.float16), ba1) + tl.dot(Ai20.to(tl.float16), ba0) w3 = tl.dot(Ai33.to(tl.float16), ba3) + tl.dot(Ai32.to(tl.float16), ba2) + tl.dot(Ai31.to(tl.float16), ba1) + tl.dot(Ai30.to(tl.float16), ba0) qqp0 = qq0.to(tl.float32) - tl.dot(Aqk00.to(tl.float16), w0.to(tl.float16)) qqp1 = qq1.to(tl.float32) - tl.dot(Aqk10.to(tl.float16), w0.to(tl.float16)) - tl.dot(Aqk11.to(tl.float16), w1.to(tl.float16)) qqp2 = qq2.to(tl.float32) - tl.dot(Aqk20.to(tl.float16), w0.to(tl.float16)) - tl.dot(Aqk21.to(tl.float16), w1.to(tl.float16)) - tl.dot(Aqk22.to(tl.float16), w2.to(tl.float16)) qqp3 = qq3.to(tl.float32) - tl.dot(Aqk30.to(tl.float16), w0.to(tl.float16)) - tl.dot(Aqk31.to(tl.float16), w1.to(tl.float16)) - tl.dot(Aqk32.to(tl.float16), w2.to(tl.float16)) - tl.dot(Aqk33.to(tl.float16), w3.to(tl.float16)) tl.store(w_ptr + slab * (BT * K) + r0[:, None] * K + d[None, :], w0.to(tl.float16)) tl.store(w_ptr + slab * (BT * K) + r1[:, None] * K + d[None, :], w1.to(tl.float16)) tl.store(w_ptr + slab * (BT * K) + r2[:, None] * K + d[None, :], w2.to(tl.float16)) tl.store(w_ptr + slab * (BT * K) + r3[:, None] * K + d[None, :], w3.to(tl.float16)) tl.store(qqp_ptr + slab * (BT * K) + r0[:, None] * K + d[None, :], qqp0.to(tl.float16)) tl.store(qqp_ptr + slab * (BT * K) + r1[:, None] * K + d[None, :], qqp1.to(tl.float16)) tl.store(qqp_ptr + slab * (BT * K) + r2[:, None] * K + d[None, :], qqp2.to(tl.float16)) tl.store(qqp_ptr + slab * (BT * K) + r3[:, None] * K + d[None, :], qqp3.to(tl.float16)) # u = Ainv @ bv ; ou = Aqk @ u u0 = tl.dot(Ai00.to(tl.float16), bv0) u1 = tl.dot(Ai11.to(tl.float16), bv1) + tl.dot(Ai10.to(tl.float16), bv0) u2 = tl.dot(Ai22.to(tl.float16), bv2) + tl.dot(Ai21.to(tl.float16), bv1) + tl.dot(Ai20.to(tl.float16), bv0) u3 = tl.dot(Ai33.to(tl.float16), bv3) + tl.dot(Ai32.to(tl.float16), bv2) + tl.dot(Ai31.to(tl.float16), bv1) + tl.dot(Ai30.to(tl.float16), bv0) ou0 = tl.dot(Aqk00.to(tl.float16), u0.to(tl.float16)) ou1 = tl.dot(Aqk10.to(tl.float16), u0.to(tl.float16)) + tl.dot(Aqk11.to(tl.float16), u1.to(tl.float16)) ou2 = tl.dot(Aqk20.to(tl.float16), u0.to(tl.float16)) + tl.dot(Aqk21.to(tl.float16), u1.to(tl.float16)) + tl.dot(Aqk22.to(tl.float16), u2.to(tl.float16)) ou3 = tl.dot(Aqk30.to(tl.float16), u0.to(tl.float16)) + tl.dot(Aqk31.to(tl.float16), u1.to(tl.float16)) + tl.dot(Aqk32.to(tl.float16), u2.to(tl.float16)) + tl.dot(Aqk33.to(tl.float16), u3.to(tl.float16)) tl.store(u_ptr + slab * (BT * V) + r0[:, None] * V + dv[None, :], u0.to(tl.float16)) tl.store(u_ptr + slab * (BT * V) + r1[:, None] * V + dv[None, :], u1.to(tl.float16)) tl.store(u_ptr + slab * (BT * V) + r2[:, None] * V + dv[None, :], u2.to(tl.float16)) tl.store(u_ptr + slab * (BT * V) + r3[:, None] * V + dv[None, :], u3.to(tl.float16)) tl.store(ou_ptr + slab * (BT * V) + r0[:, None] * V + dv[None, :], ou0.to(tl.float16)) tl.store(ou_ptr + slab * (BT * V) + r1[:, None] * V + dv[None, :], ou1.to(tl.float16)) tl.store(ou_ptr + slab * (BT * V) + r2[:, None] * V + dv[None, :], ou2.to(tl.float16)) tl.store(ou_ptr + slab * (BT * V) + r3[:, None] * V + dv[None, :], ou3.to(tl.float16)) # --------------------------------------------------------------------------- # Kernel 2: inter-chunk recurrence (sequential over chunks, V-sliced) # --------------------------------------------------------------------------- @triton.jit def kda_recurrent_kernel( w_ptr, u_ptr, qqp_ptr, ou_ptr, b_ptr, elast_ptr, o_ptr, T, H, K: tl.constexpr, V: tl.constexpr, BT: tl.constexpr, VS: tl.constexpr, ): pid = tl.program_id(0) NT = T // BT nvs = V // VS vslice = pid % nvs bh = pid // nvs b = bh // H h = bh % H t = tl.arange(0, BT) d = tl.arange(0, K) vd = vslice * VS + tl.arange(0, VS) S = tl.zeros((K, VS), dtype=tl.float32) o_base = b * (T * H * V) + h * V s0 = bh * NT w = tl.load(w_ptr + s0 * (BT * K) + t[:, None] * K + d[None, :]) qqp = tl.load(qqp_ptr + s0 * (BT * K) + t[:, None] * K + d[None, :]) u = tl.load(u_ptr + s0 * (BT * V) + t[:, None] * V + vd[None, :]) ou = tl.load(ou_ptr + s0 * (BT * V) + t[:, None] * V + vd[None, :]) bb = tl.load(b_ptr + s0 * (BT * K) + t[:, None] * K + d[None, :]) e_last = tl.load(elast_ptr + s0 * K + d) for n in range(NT): nn = n + 1 if n + 1 < NT else NT - 1 ns = bh * NT + nn nw = tl.load(w_ptr + ns * (BT * K) + t[:, None] * K + d[None, :]) nqqp = tl.load(qqp_ptr + ns * (BT * K) + t[:, None] * K + d[None, :]) nu = tl.load(u_ptr + ns * (BT * V) + t[:, None] * V + vd[None, :]) nou = tl.load(ou_ptr + ns * (BT * V) + t[:, None] * V + vd[None, :]) nbb = tl.load(b_ptr + ns * (BT * K) + t[:, None] * K + d[None, :]) nelast = tl.load(elast_ptr + ns * K + d) W = tl.dot(w.to(tl.float16), S.to(tl.float16)) # (BT, VS) op = tl.dot(qqp.to(tl.float16), S.to(tl.float16)) # (BT, VS) vi = u.to(tl.float32) - W o_n = ou.to(tl.float32) + op dS = tl.dot(tl.trans(bb).to(tl.float16), vi.to(tl.float16)) # (K, VS) S = e_last[:, None] * (S + dS) o_ptrs = o_ptr + o_base + (n * BT + t)[:, None] * (H * V) + vd[None, :] tl.store(o_ptrs, o_n.to(o_ptr.dtype.element_ty)) w = nw qqp = nqqp u = nu ou = nou bb = nbb e_last = nelast # --------------------------------------------------------------------------- # Python driver # --------------------------------------------------------------------------- def kda_forward(q, k, v, g, beta, scale, chunk_size, bufs): B, T, H, K = q.shape V = v.shape[-1] BT = chunk_size NT = T // BT assert T % BT == 0 NBH = B * H w, u, qqp, ou, bb, elast, o = bufs BC = 16 grid_intra = (NBH * NT,) kda_intra_kernel[grid_intra]( q, k, v, g, beta, w, u, qqp, ou, bb, elast, scale, T, H, K=K, V=V, BT=BT, BC=BC, num_warps=4, num_stages=1, ) VS = 16 if V % 16 == 0 else V grid_rec = (NBH * (V // VS),) kda_recurrent_kernel[grid_rec]( w, u, qqp, ou, bb, elast, o, T, H, K=K, V=V, BT=BT, VS=VS, num_warps=8, num_stages=1, ) 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) self._bufs = None def forward(self, q, k, v, g, beta): if self._bufs is None or self._bufs[0].shape[0] != self.B * self.H * (self.T // self.chunk_size): B, T, H, K = q.shape V = v.shape[-1] BT = self.chunk_size NT = T // BT NBH = B * H dev = q.device self._bufs = ( torch.empty(NBH * NT, BT, K, device=dev, dtype=torch.float16), torch.empty(NBH * NT, BT, V, device=dev, dtype=torch.float16), torch.empty(NBH * NT, BT, K, device=dev, dtype=torch.float16), torch.empty(NBH * NT, BT, V, device=dev, dtype=torch.float16), torch.empty(NBH * NT, BT, K, device=dev, dtype=torch.float16), torch.empty(NBH * NT, K, device=dev, dtype=torch.float32), torch.empty(B, T, H, V, device=dev, dtype=torch.bfloat16), ) return kda_forward(q, k, v, g, beta, self.scale, self.chunk_size, self._bufs) # 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]