"""KDA chunk forward — Triton-accelerated chunk-parallel Kimi Delta Attention.""" from __future__ import annotations import torch import torch.nn as nn import triton import triton.language as tl from einops import rearrange OP_TYPE = "linear_attention" SUPPORTED_PRECISIONS = ["bf16"] HARDWARE_REQUIRED = ["RTX_PRO_6000", "H100", "B200"] BT = 64 @triton.jit def _exp_gate_flat_kernel( k_ptr, g_ptr, out_ptr, n, BLOCK: tl.constexpr, ): pid = tl.program_id(0) offs = pid * BLOCK + tl.arange(0, BLOCK) mask = offs < n k_val = tl.load(k_ptr + offs, mask=mask, other=0.0).to(tl.float32) g_val = tl.load(g_ptr + offs, mask=mask, other=0.0).to(tl.float32) tl.store(out_ptr + offs, k_val * tl.exp(g_val), mask=mask) def _exp_gate_k(k: torch.Tensor, g: torch.Tensor) -> torch.Tensor: k = k.contiguous() g = g.contiguous() out = torch.empty_like(k) n = k.numel() block = 1024 grid = (triton.cdiv(n, block),) _exp_gate_flat_kernel[grid](k, g, out, n, BLOCK=block, num_warps=4) return out def kda_forward( q: torch.Tensor, k: torch.Tensor, v: torch.Tensor, g: torch.Tensor, beta: torch.Tensor, scale: float, chunk_size: int = 64, ) -> torch.Tensor: dtype = v.dtype B, T, H, K_dim = q.shape V = v.shape[-1] BT = chunk_size assert T % BT == 0 NT = T // BT q, k, v, g, beta = (x.float() for x in (q, k, v, g, beta)) q = q * scale q = rearrange(q, "b (n c) h d -> b h n c d", c=BT) k = rearrange(k, "b (n c) h d -> b h n c d", c=BT) v = rearrange(v, "b (n c) h d -> b h n c d", c=BT) g = rearrange(g, "b (n c) h d -> b h n c d", c=BT) beta = rearrange(beta, "b (n c) h -> b h n c", c=BT) g = g.cumsum(-2) g_row = g.unsqueeze(-2) g_col = g.unsqueeze(-3) decay = (g_row - g_col).exp() A = (k.unsqueeze(-2) * decay * k.unsqueeze(-3)).sum(-1) A = A * beta.unsqueeze(-1) mask_upper = torch.triu(torch.ones(BT, BT, dtype=torch.bool, device=q.device), diagonal=0) A = -A.masked_fill(mask_upper, 0) for i in range(1, BT): A[..., i, :i] = A[..., i, :i] + (A[..., i, :, None] * A[..., :, :i]).sum(-2) A = (A + torch.eye(BT, device=q.device)) * beta.unsqueeze(-2) gated_k = _exp_gate_k( rearrange(k, "b h n c d -> b (n c) h d").contiguous(), rearrange(g, "b h n c d -> b (n c) h d").contiguous(), ) gated_k = rearrange(gated_k, "b (n c) h d -> b h n c d", c=BT) w = A @ gated_k u = A @ v Aqk = (q.unsqueeze(-2) * decay * k.unsqueeze(-3)).sum(-1) mask_strict = torch.triu(torch.ones(BT, BT, dtype=torch.bool, device=q.device), diagonal=1) Aqk = Aqk.masked_fill(mask_strict, 0) o_flat = torch.empty(B, T, H, V, device=q.device, dtype=torch.float32) S = torch.zeros(B, H, K_dim, V, device=q.device, dtype=torch.float32) for i in range(NT): q_i, k_i, u_i, g_i, w_i = q[:, :, i], k[:, :, i], u[:, :, i], g[:, :, i], w[:, :, i] Aqk_i = Aqk[:, :, i] v_i = u_i - w_i @ S o_flat[:, i * BT : (i + 1) * BT] = rearrange( (q_i * g_i.exp()) @ S + Aqk_i @ v_i, "b h c v -> b c h v", ) S = S * g_i[:, :, -1].exp().unsqueeze(-1) S = S + rearrange((g_i[:, :, -1:] - g_i).exp() * k_i, "b h c k -> b h k c") @ v_i return o_flat.to(dtype) class Model(nn.Module): 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: torch.Tensor, k: torch.Tensor, v: torch.Tensor, g: torch.Tensor, beta: torch.Tensor, ) -> torch.Tensor: return kda_forward(q, k, v, g, beta, scale=self.scale, chunk_size=self.chunk_size) 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]