"""Fused single-kernel W4A16 decode for the Kimi-Linear hybrid unit (batch=1). The whole per-token forward (3 KDA + 1 MLA layer, each with a 64-expert MoE, all int4 dequant-GEMVs, short causal conv, KDA recurrent-state update, MLA latent-cache absorb attention, MoE router + expert GEMVs, both RMSNorms and residual adds) is one custom CUDA __global__ kernel built with load_inline and invoked exactly once in step(). No CUDA graph, no torch.compile, no per-op loop. int4 weights are streamed once through a fused dequant-GEMV (warp-per-output, out-major layout, per-group dequant in registers); the bf16 weight is never materialized. MLA uses the absorb form so kv_b is never materialized across the context. RMSNorm is folded into each GEMV's input load (sum-of-squares inline). Cross-block sync uses a monotonic-generation atomic barrier; the grid is sized to the GPU's max-resident block count so it is deadlock-free. """ from __future__ import annotations import os from dataclasses import dataclass, field os.environ.setdefault("CUDA_HOME", "/usr/local/cuda") import torch import torch.nn as nn from torch.utils.cpp_extension import load_inline OP_TYPE = "kimi_linear_w4a16_decode" HARDWARE_REQUIRED = ["RTX_PRO_6000"] EPS = 1.0e-6 GROUP = 128 H = 32; DK = 128; C = H * DK; HID = 2304 QK_NOPE = 128; QK_ROPE = 64; VHD = 128; KV_LORA = 512 M = 1024; NSH = 1 LMAX_ALLOC = 32768 # max context + slack for MLA cache buffers @dataclass(frozen=True) class Config: hidden: int = HID kda_heads: int = H kda_head_dim: int = DK short_conv: int = 4 mla_heads: int = H kv_lora: int = KV_LORA qk_nope: int = QK_NOPE qk_rope: int = QK_ROPE v_head: int = VHD rope_theta: float = 10000.0 n_experts: int = 64 n_active: int = 8 n_shared: int = NSH moe_inter: int = M routed_scaling: float = 2.446 group: int = GROUP pattern: tuple = ("K", "K", "K", "M") dtype: torch.dtype = field(default=torch.bfloat16) def build_config(shape): return Config(n_experts=int(shape.get("n_experts", 64))) # --------------------------------------------------------------------------- # # Module structure -- mirrors reference.py exactly (names/shapes) so the # reference state_dict loads with strict=True. # --------------------------------------------------------------------------- # class QuantLinear(nn.Module): def __init__(self, in_f, out_f, group=GROUP): super().__init__(); ng = in_f // group self.register_buffer("w_q", torch.zeros(in_f // 2, out_f, dtype=torch.uint8)) self.register_buffer("scales", torch.zeros(ng, out_f, dtype=torch.bfloat16)) self.register_buffer("zeros", torch.zeros(ng, out_f, dtype=torch.bfloat16)) class QuantExperts(nn.Module): def __init__(self, n, in_f, out_f, group=GROUP): super().__init__(); ng = in_f // group self.register_buffer("w_q", torch.zeros(n, in_f // 2, out_f, dtype=torch.uint8)) self.register_buffer("scales", torch.zeros(n, ng, out_f, dtype=torch.bfloat16)) self.register_buffer("zeros", torch.zeros(n, ng, out_f, dtype=torch.bfloat16)) class KDA(nn.Module): def __init__(self, cfg): super().__init__(); self.cfg = cfg self.q_proj = QuantLinear(HID, C); self.k_proj = QuantLinear(HID, C) self.v_proj = QuantLinear(HID, C); self.g_proj = QuantLinear(HID, C) self.beta_proj = nn.Linear(HID, H, bias=False, dtype=cfg.dtype) self.conv_w = nn.Parameter(torch.empty(3, C, cfg.short_conv, dtype=cfg.dtype)) self.o_proj = QuantLinear(C, HID); self.scale = DK ** -0.5 class MLA(nn.Module): def __init__(self, cfg): super().__init__(); self.cfg = cfg self.q_proj = QuantLinear(HID, H * (QK_NOPE + QK_ROPE)) self.kv_a = QuantLinear(HID, KV_LORA + QK_ROPE) self.kv_b = QuantLinear(KV_LORA, H * (QK_NOPE + VHD)) self.o_proj = QuantLinear(H * VHD, HID); self.scale = (QK_NOPE + QK_ROPE) ** -0.5 class MoE(nn.Module): def __init__(self, cfg): super().__init__(); E = cfg.n_experts self.router = nn.Linear(HID, E, bias=False, dtype=cfg.dtype) self.gate = QuantExperts(E, HID, M); self.up = QuantExperts(E, HID, M) self.down = QuantExperts(E, M, HID) self.s_gate = QuantExperts(NSH, HID, M); self.s_up = QuantExperts(NSH, HID, M) self.s_down = QuantExperts(NSH, M, HID) class Block(nn.Module): def __init__(self, cfg, kind): super().__init__(); self.kind = kind self.attn_norm = nn.Parameter(torch.ones(cfg.hidden, dtype=cfg.dtype)) self.moe_norm = nn.Parameter(torch.ones(cfg.hidden, dtype=cfg.dtype)) self.attn = KDA(cfg) if kind == "K" else MLA(cfg) self.moe = MoE(cfg) # --------------------------------------------------------------------------- # # Weight layout codegen (single source of truth). # --------------------------------------------------------------------------- # def _build_layout(): ql = [] for b in range(3): ql += [(f"K{b}Q", HID, C, 1), (f"K{b}K", HID, C, 1), (f"K{b}V", HID, C, 1), (f"K{b}G", HID, C, 1), (f"K{b}O", C, HID, 1)] ql += [("MQ", HID, H * (QK_NOPE + QK_ROPE), 1), ("MVA", HID, KV_LORA + QK_ROPE, 1), ("MO", H * VHD, HID, 1)] for b in range(4): ql += [(f"B{b}GATE", HID, M, 64), (f"B{b}UP", HID, M, 64), (f"B{b}DOWN", M, HID, 64), (f"B{b}SGATE", HID, M, NSH), (f"B{b}SUP", HID, M, NSH), (f"B{b}SDOWN", M, HID, NSH)] wq_off = sc_off = zs_off = 0 off_wq = {}; off_sc = {}; off_zs = {}; meta = {} lines = [] for name, in_f, out_f, e in ql: ng = in_f // GROUP off_wq[name] = wq_off; off_sc[name] = sc_off; off_zs[name] = zs_off meta[name] = (in_f, out_f, e) lines.append(f"#define WQ_{name} {wq_off}") lines.append(f"#define SC_{name} {sc_off}") lines.append(f"#define ZS_{name} {zs_off}") wq_off += e * (in_f // 2) * out_f sc_off += e * ng * out_f zs_off += e * ng * out_f return ql, off_wq, off_sc, off_zs, meta, "\n".join(lines), (wq_off, sc_off, zs_off) _QLIST, _OFFWQ, _OFFSC, _OFFZS, _META, _DEFINES, _WQSZ = _build_layout() def _bf_layout(): off = {}; lines = []; cur = 0 def emit(name, n): nonlocal cur off[name] = cur; lines.append(f"#define BF_{name} {cur}"); cur += n for b in range(4): emit(f"AN{b}", HID); emit(f"MN{b}", HID) for b in range(3): emit(f"BETA{b}", H * HID); emit(f"CONV{b}", 3 * C * 4) for b in range(4): emit(f"ROUT{b}", 64 * HID) return off, "\n".join(lines), cur _OFFBF, _BF_DEFINES, _BFSZ = _bf_layout() # --------------------------------------------------------------------------- # # CUDA helpers + main kernel # --------------------------------------------------------------------------- # _CONST = """ #define H 32 #define DK 128 #define C 4096 #define HID 2304 #define QK_NOPE 128 #define QK_ROPE 64 #define VHD 128 #define KV_LORA 512 #define M 1024 #define NSH 1 #define GROUP 128 #define WARPS 8 #define THREADS 256 #define KDA_SMEM 4368 #define EPSF 1.0e-6f #define ROUTED_SCALING 2.446f typedef unsigned int u32; typedef __nv_bfloat16 bf16; """ _HELPERS = r""" __device__ inline float b2f(bf16 v){return __bfloat162float(v);} __device__ inline bf16 f2b(float v){return __float2bfloat16(v);} __device__ inline float wsum(float v){for(int o=16;o>0;o>>=1)v+=__shfl_xor_sync(0xffffffffu,v,o);return v;} __device__ inline float bsum(float v,volatile float* red){ int tid=threadIdx.x; v=wsum(v); if((tid&31)==0)red[tid>>5]=v; __syncthreads(); v=(tid smem sx; fold rmsnorm if wnorm!=0 (sumsq inline) __device__ inline void load_fold(float* sx,const float* x,const bf16* wnorm,int K,volatile float* red){ int tid=threadIdx.x; if(wnorm){ float part=0.0f; for(int i=tid;i>(b<<3))&0xFFu; a += xb[b*2]*(((float)(by&0xF)-zz)*ss) + xb[b*2+1]*(((float)((by>>4)&0xF)-zz)*ss); } return a; } // vectorized dequant dot over one weight row (uint32 loads). 2 accumulators for ILP. __device__ inline float gemv_dot_v(const float* x,const uint8_t* wq_row,const bf16* sc,const bf16* zr,int K,int lane){ const uint32_t* r=(const uint32_t*)wq_row; int K8=K>>3; float acc0=0,acc1=0; int j=lane; for(; j+32>4]), b2f(zr[j>>4])); int j2=j+32; acc1 += dq_u32(r[j2], x+(j2<<3), b2f(sc[j2>>4]), b2f(zr[j2>>4])); } for(; j>4]), b2f(zr[j>>4])); return wsum(acc0+acc1); } // out-major dequant GEMV, warp-per-output. wq[N,K/2] sc/zr[N,ng]. __device__ inline void gemv_om(const float* sx,const uint8_t* wq,const bf16* sc,const bf16* zr,int K,int N,float* y,int bid){ int tid=threadIdx.x,warp=tid>>5,lane=tid&31,ng=K/GROUP,gw=bid*WARPS+warp,TW=gridDim.x*WARPS; for(int out=gw;out>5,lane=tid&31,ng=K/GROUP,gw=bid*WARPS+warp,TW=gridDim.x*WARPS; for(int out=gw;out>5,lane=tid&31,ng=K/GROUP,gw=bid*WARPS+warp,TW=gridDim.x*WARPS; for(int out=gw;out>5,lane=tid&31,gw=bid*WARPS+warp,TW=gridDim.x*WARPS; for(int out=gw;out>5,lane=tid&31,ng=K/GROUP,gw=bid*WARPS+warp,TW=gridDim.x*WARPS; for(int out=gw;out=H) return; int h=bid,tid=threadIdx.x; float scalef=rsqrtf((float)DK); float* shk=smem+KDA_SMEM; float* shv=shk+DK; float* shq=shv+DK; float* shd=shq+DK; float* shp=shd+DK; float* sho=shp+DK; for(int d=tid;d>7,d=jd&127; S[h*DK*DK+j*DK+d]*=shd[j];} __threadfence_block(); __syncthreads(); for(int d=tid;d>7,d=jd&127; S[h*DK*DK+j*DK+d]+=bh*shk[j]*(shv[d]-shp[d]);} __threadfence_block(); __syncthreads(); for(int d=tid;d bigbuf if(prime){ for(int i=tid;i>5,lane=tid&31,gw=bid*WARPS+warp,TW=gridDim.x*WARPS; int N=KV_LORA*H; for(int out=gw;out>4)&0xF)):((float)(by&0xF))); acc+=qn[d]*((nib-b2f(z[d]))*b2f(s[d])); } acc=wsum(acc); if(lane==0) qa[out]=acc; } } } // MLA scores: scores[l,h]=scale*( c_kv[l]@qa[:,h] + k_rope[l]@qrope[h] ) __device__ inline void mla_scores(const float* qfull,const float* qa,const bf16* ckv,const bf16* krc,float* scores,int L,int bid){ int tid=threadIdx.x,warp=tid>>5,lane=tid&31,gw=bid*WARPS+warp,TW=gridDim.x*WARPS; int Lp=L+1; float scalef=rsqrtf((float)(QK_NOPE+QK_ROPE)); for(int l=gw;l=H) return; int h=bid, tid=threadIdx.x, Lp=L+1; float mx=-1e30f; for(int l=tid;l= NT_I*NT_L) return; int it=bid%NT_I, lc=bid/NT_I, ibase=it*IW; int tid=threadIdx.x; int il=tid&31; int h0=tid>>5; int Lp=L+1, lper=(Lp+NT_L-1)/NT_L, l0=lc*lper, l1=l0+lper; if(l1>Lp)l1=Lp; float a0=0,a1=0,a2=0,a3=0; for(int l=l0;l>5,lane=tid&31,gw=bid*WARPS+warp,TW=gridDim.x*WARPS; int N=H*VHD; int ng=KV_LORA/GROUP; for(int out=gw;out idxw[0..7]=idx, idxw[8..15]=w (norm*scaling) __device__ inline void moe_topk(const float* rout,float* idxw_g,float* smem,int bid){ int tid=threadIdx.x; float* sh=smem+KDA_SMEM; // [64] probs + [16] out float* pr=sh; float* out=sh+64; if(tid<64){ float mx=-1e30f; for(int j=0;j<64;j++) mx=fmaxf(mx,rout[j]); // softmax: need sum -- do in two passes within thread0? do per-thread reduce pr[tid]=__expf(rout[tid]-mx); } __syncthreads(); if(tid==0){ float sm=0; for(int j=0;j<64;j++) sm+=pr[j]; for(int j=0;j<64;j++) pr[j]=pr[j]/sm; // top-8 for(int k=0;k<8;k++){ int bi=-1; float bv=-1e30f; for(int j=0;j<64;j++) if(pr[j]>bv){bv=pr[j];bi=j;} out[k]=(float)bi; out[8+k]=bv; pr[bi]=-1e30f; } float wsumw=0; for(int k=0;k<8;k++) wsumw+=out[8+k]; for(int k=0;k<8;k++){ out[8+k]=out[8+k]/(wsumw+1e-9f)*ROUTED_SCALING; idxw_g[k]=out[k]; idxw_g[8+k]=out[8+k]; } } __syncthreads(); } // routed gateup for 8 experts: warp-per-(j,m). hh[j*M+m]=silu(gate)*up __device__ inline void moe_routed_gateup(const float* sx,const uint8_t* gwq,const bf16* gsc,const bf16* gzr, const uint8_t* uwq,const bf16* usc,const bf16* uzr,const float* idxw,float* hh,int bid){ int tid=threadIdx.x,warp=tid>>5,lane=tid&31,ng=HID/GROUP,gw=bid*WARPS+warp,TW=gridDim.x*WARPS; int NA=8; int strideg = M*(HID/2); // per expert out-major for(int out=gw;out>5,gw=bid*WARPS+warp,TW=gridDim.x*WARPS; int ng=M/GROUP; int stride_e=HID*(M/2); for(int c=gw;c #include #include #include #include namespace cg = cooperative_groups; @CONST@ @DEFINES@ @BF_DEFINES@ enum { P_WQ,P_SC,P_ZS,P_BF, P_H0,P_H1, P_CNT, P_HIDDEN,P_OUT, P_Q,P_K,P_V,P_G,P_O,P_BETA, P_QFULL,P_KVFULL,P_OFULL,P_QABS,P_SCORES,P_P,P_VACC, P_HH,P_ROUT,P_IDXW, P_S0,P_CQ0,P_CK0,P_CV0, P_S1,P_CQ1,P_CK1,P_CV1, P_S2,P_CQ2,P_CK2,P_CV2, P_CKV,P_KRC,P_CKVIN,P_KRCIN, P_KVBUK_WQ,P_KVBUK_SC,P_KVBUK_ZR, P_KVBUV_WQ,P_KVBUV_SC,P_KVBUV_ZR, P_NPTRS }; @HELPERS@ extern "C" __global__ __launch_bounds__(THREADS, 4) void mega(void** P, int L, int gen, int prime, int stop){ int bid=blockIdx.x; extern __shared__ float smem[]; float* sx=smem; volatile float* red=smem+4096; uint8_t* WQ=(uint8_t*)P[P_WQ]; bf16* SC=(bf16*)P[P_SC]; bf16* ZS=(bf16*)P[P_ZS]; bf16* BF=(bf16*)P[P_BF]; u32* cnt=(u32*)P[P_CNT]; int phase=0; { bf16* hidden=(bf16*)P[P_HIDDEN]; float* h0=(float*)P[P_H0]; int tid=threadIdx.x; for(int i=tid;i(); mega<<>>((void**)p,(int)L,(int)gen,(int)prime,(int)stop); } """ _CUDA_SRC = (_KERN .replace("@CONST@", _CONST) .replace("@DEFINES@", _DEFINES) .replace("@BF_DEFINES@", _BF_DEFINES) .replace("@HELPERS@", _HELPERS) .replace("@BODY@", _kda(0, "P_H0", "P_H1", "P_S0", "P_CQ0", "P_CK0", "P_CV0") + _kda(1, "P_H1", "P_H0", "P_S1", "P_CQ1", "P_CK1", "P_CV1") + _kda(2, "P_H0", "P_H1", "P_S2", "P_CQ2", "P_CK2", "P_CV2") + _MLA_FIX)) _cpp = "void launch_mega(torch::Tensor ptrs, int64_t L, int64_t gen, int64_t prime, int64_t stop);" _ext = load_inline("kimi_mega", cpp_sources=[_cpp], cuda_sources=[_CUDA_SRC], functions=["launch_mega"], extra_cuda_cflags=["-O3", "--use_fast_math", "-lineinfo"], verbose=False) class Model(nn.Module): def __init__(self, cfg): super().__init__() self.cfg = cfg self.blocks = nn.ModuleList(Block(cfg, k) for k in cfg.pattern) self._prepared = False self._gen = 0 def _prepare(self): dev = next(self.parameters()).device wq = torch.zeros(_WQSZ[0], dtype=torch.uint8, device=dev) sc = torch.zeros(_WQSZ[1], dtype=torch.bfloat16, device=dev) zs = torch.zeros(_WQSZ[2], dtype=torch.bfloat16, device=dev) bf = torch.zeros(_BFSZ, dtype=torch.bfloat16, device=dev) def put(name): wqt, sct, zrt = self._wt[name] if wqt.dim() == 2: WqT = wqt.t().contiguous(); ScT = sct.t().contiguous(); ZrT = zrt.t().contiguous() else: WqT = wqt.transpose(1, 2).contiguous() ScT = sct.transpose(1, 2).contiguous() ZrT = zrt.transpose(1, 2).contiguous() ow = _OFFWQ[name]; wq[ow:ow + WqT.numel()].copy_(WqT.view(-1)) os = _OFFSC[name]; sc[os:os + ScT.numel()].copy_(ScT.view(-1)) oz = _OFFZS[name]; zs[oz:oz + ZrT.numel()].copy_(ZrT.view(-1)) for name, *_ in _QLIST: put(name) # bf16 params def putbf(name, tensor): o = _OFFBF[name]; n = tensor.numel() bf[o:o + n].copy_(tensor.reshape(-1).to(torch.bfloat16)) for b in range(4): putbf(f"AN{b}", self.blocks[b].attn_norm); putbf(f"MN{b}", self.blocks[b].moe_norm) for b in range(3): putbf(f"BETA{b}", self.blocks[b].attn.beta_proj.weight) # [H, HID] row-major putbf(f"CONV{b}", self.blocks[b].attn.conv_w) # [3, C, 4] for b in range(4): putbf(f"ROUT{b}", self.blocks[b].moe.router.weight) # [E, HID] # kvb_UK (in-major [H,256,128]) and kvb_UV_T (out-major [H,128,256]) from MLA kv_b. # Vectorized gather (avoid per-column GPU syncs). kvb = self.blocks[3].attn.kv_b wqb = kvb.w_q # [256, 8192] (in//2, out) scb = kvb.scales # [4, 8192] zrb = kvb.zeros hidx = torch.arange(H, device=dev) didx = torch.arange(VHD, device=dev) uk_cols = (hidx[:, None] * 256 + torch.arange(QK_NOPE, device=dev)[None, :]).reshape(-1) # [H*128] uv_cols = (hidx[:, None] * 256 + 128 + didx[None, :]).reshape(-1) # [H*128] uk_wq = wqb.index_select(1, uk_cols).reshape(256, H, QK_NOPE).permute(1, 0, 2).contiguous() uk_sc = scb.index_select(1, uk_cols).reshape(4, H, QK_NOPE).permute(1, 0, 2).contiguous() uk_zr = zrb.index_select(1, uk_cols).reshape(4, H, QK_NOPE).permute(1, 0, 2).contiguous() uv_wq = wqb.index_select(1, uv_cols).reshape(256, H, VHD).permute(1, 2, 0).contiguous() uv_sc = scb.index_select(1, uv_cols).reshape(4, H, VHD).permute(1, 2, 0).contiguous() uv_zr = zrb.index_select(1, uv_cols).reshape(4, H, VHD).permute(1, 2, 0).contiguous() self._wq = wq; self._sc = sc; self._zs = zs; self._bf = bf self._uk_wq = uk_wq; self._uk_sc = uk_sc; self._uk_zr = uk_zr self._uv_wq = uv_wq; self._uv_sc = uv_sc; self._uv_zr = uv_zr # scratch (fp32 except state) self._h0 = torch.zeros(HID, device=dev, dtype=torch.float32) self._h1 = torch.zeros(HID, device=dev, dtype=torch.float32) self._out = torch.zeros(HID, device=dev, dtype=torch.bfloat16) self._cnt = torch.zeros(128, device=dev, dtype=torch.int32) self._q = torch.zeros(C, device=dev, dtype=torch.float32) self._k = torch.zeros(C, device=dev, dtype=torch.float32) self._v = torch.zeros(C, device=dev, dtype=torch.float32) self._g = torch.zeros(C, device=dev, dtype=torch.float32) self._o = torch.zeros(C, device=dev, dtype=torch.float32) self._beta = torch.zeros(H, device=dev, dtype=torch.float32) self._qfull = torch.zeros(H * (QK_NOPE + QK_ROPE), device=dev, dtype=torch.float32) self._kvfull = torch.zeros(KV_LORA + QK_ROPE, device=dev, dtype=torch.float32) self._ofull = torch.zeros(H * VHD, device=dev, dtype=torch.float32) self._qabs = torch.zeros(KV_LORA * H, device=dev, dtype=torch.float32) self._scores = torch.zeros((LMAX_ALLOC + 64) * H, device=dev, dtype=torch.float32) self._p = torch.zeros((LMAX_ALLOC + 64) * H, device=dev, dtype=torch.float32) self._vacc = torch.zeros(H * KV_LORA, device=dev, dtype=torch.float32) self._hh = torch.zeros(9 * M, device=dev, dtype=torch.float32) self._rout = torch.zeros(64, device=dev, dtype=torch.float32) self._idxw = torch.zeros(16, device=dev, dtype=torch.float32) self._ckv_buf = None # MLA bigbuf, allocated on first step self._prepared = True def _build_wt(self): # build name -> (wq, sc, zr) accessor map wt = {} for b in range(3): a = self.blocks[b].attn wt[f"K{b}Q"] = (a.q_proj.w_q, a.q_proj.scales, a.q_proj.zeros) wt[f"K{b}K"] = (a.k_proj.w_q, a.k_proj.scales, a.k_proj.zeros) wt[f"K{b}V"] = (a.v_proj.w_q, a.v_proj.scales, a.v_proj.zeros) wt[f"K{b}G"] = (a.g_proj.w_q, a.g_proj.scales, a.g_proj.zeros) wt[f"K{b}O"] = (a.o_proj.w_q, a.o_proj.scales, a.o_proj.zeros) a = self.blocks[3].attn wt["MQ"] = (a.q_proj.w_q, a.q_proj.scales, a.q_proj.zeros) wt["MVA"] = (a.kv_a.w_q, a.kv_a.scales, a.kv_a.zeros) wt["MO"] = (a.o_proj.w_q, a.o_proj.scales, a.o_proj.zeros) for b in range(4): m = self.blocks[b].moe wt[f"B{b}GATE"] = (m.gate.w_q, m.gate.scales, m.gate.zeros) wt[f"B{b}UP"] = (m.up.w_q, m.up.scales, m.up.zeros) wt[f"B{b}DOWN"] = (m.down.w_q, m.down.scales, m.down.zeros) wt[f"B{b}SGATE"] = (m.s_gate.w_q, m.s_gate.scales, m.s_gate.zeros) wt[f"B{b}SUP"] = (m.s_up.w_q, m.s_up.scales, m.s_up.zeros) wt[f"B{b}SDOWN"] = (m.s_down.w_q, m.s_down.scales, m.s_down.zeros) self._wt = wt def step(self, hidden, state): if not self._prepared: self._build_wt(); self._prepare() dev = self._h0.device # MLA cache: detect whether the incoming cache is our own bigbuf (a # continued AR chain within one run) or a fresh state (new run/seed). # Only a fresh state needs the prime copy of the existing cache in. ckv_in = state[3]["c_kv"] # [L, 512] krc_in = state[3]["k_rope"] # [L, 64] L = ckv_in.shape[0] is_my_buf = (self._ckv_buf is not None and ckv_in.data_ptr() == self._ckv_buf.data_ptr()) if is_my_buf: prime = 0 else: prime = 1 cap = L + 64 if self._ckv_buf is None or self._ckv_buf.shape[0] < cap: self._ckv_buf = torch.zeros(cap, KV_LORA, device=dev, dtype=torch.bfloat16) self._krc_buf = torch.zeros(cap, QK_ROPE, device=dev, dtype=torch.bfloat16) # pointer table in fixed order (must match enum P_*) ptrs = torch.tensor([ self._wq.data_ptr(), self._sc.data_ptr(), self._zs.data_ptr(), self._bf.data_ptr(), self._h0.data_ptr(), self._h1.data_ptr(), self._cnt.data_ptr(), hidden.data_ptr(), self._out.data_ptr(), self._q.data_ptr(), self._k.data_ptr(), self._v.data_ptr(), self._g.data_ptr(), self._o.data_ptr(), self._beta.data_ptr(), self._qfull.data_ptr(), self._kvfull.data_ptr(), self._ofull.data_ptr(), self._qabs.data_ptr(), self._scores.data_ptr(), self._p.data_ptr(), self._vacc.data_ptr(), self._hh.data_ptr(), self._rout.data_ptr(), self._idxw.data_ptr(), state[0]["S"].data_ptr(), state[0]["cq"].data_ptr(), state[0]["ck"].data_ptr(), state[0]["cv"].data_ptr(), state[1]["S"].data_ptr(), state[1]["cq"].data_ptr(), state[1]["ck"].data_ptr(), state[1]["cv"].data_ptr(), state[2]["S"].data_ptr(), state[2]["cq"].data_ptr(), state[2]["ck"].data_ptr(), state[2]["cv"].data_ptr(), self._ckv_buf.data_ptr(), self._krc_buf.data_ptr(), ckv_in.data_ptr(), krc_in.data_ptr(), self._uk_wq.data_ptr(), self._uk_sc.data_ptr(), self._uk_zr.data_ptr(), self._uv_wq.data_ptr(), self._uv_sc.data_ptr(), self._uv_zr.data_ptr(), ], device=dev, dtype=torch.int64) _ext.launch_mega(ptrs, L, self._gen, 1 if prime else 0, getattr(self, "_stop", 4)) self._gen += 1 # reflect MLA cache into state (view of bigbuf) state[3]["c_kv"] = self._ckv_buf[:L + 1] state[3]["k_rope"] = self._krc_buf[:L + 1] return self._out, state