Introduction
Gated DeltaNet (GDN) [1] and Kimi Delta Attention (KDA) [2] keep a matrix state per head and update it once per token with a decayed delta rule,
where the decay is one scalar per head and token for GDN and one value per channel for KDA. Prefill must apply this recurrence to every token of a packed batch of variable-length sequences. Run token by token it is hopelessly serial; the standard remedy is the chunked form [3]: within a chunk of tokens the product of rank-one updates is written in WY form, which needs the inverse of a unit lower-triangular matrix, and only the boundary states between chunks remain sequential.
How should that chunked computation be cut into GPU work so that latency stays low for every batch shape? The difficulty is that the right cut depends on the batch. With one short sequence the GPU is nearly idle and every extra kernel launch or materialised intermediate is pure cost; with a packed batch of thousands of tokens there is plenty of parallelism across sequences and heads, but the boundary-state pass is long and must not be slowed by work that does not depend on it. The existing kernels each sit at one point of that trade-off: the starter's FLA pipeline runs several kernels for every batch; FlashInfer's warp-specialised GDN kernel [4] and the FlashKDA kernel are strongest on large batches; the contest-winning Kachua kernel tiles the value dimension so that short GDN batches fill the GPU. Our starting point was the measurement that Kachua's value-tiled formulation cut GDN short-batch latency far below the starter while nothing comparable existed for KDA, and our method is an attempt to carry that decomposition over to channel-wise decay and to long batches.
Method
Our method has two parts: a three-kernel WY decomposition (prepare, state, output) that we wrote for both operators, whose last two kernels are fused into one for KDA batches of fewer than 512 tokens, and a dispatch rule that decides from the batch layout alone which kernel to run.
The chunk transform, and what moves out of the serial pass
Fix one head and one chunk of tokens, and let be the cumulative log-decay inside the chunk (a scalar for GDN, a vector over channels for KDA). With decayed keys , keys decayed to the chunk end and the strictly lower-triangular matrix
the chunk's contribution follows from one triangular solve. The usual pipeline solves it inside the serial loop, against the right-hand side that depends on the carried state . Because the solve is linear, we split it:
and depend only on the chunk's own inputs, so they are computed in the token-parallel prepass, and the serial pass is left with two products per chunk:
with no triangular solve and no loads of values, gates or . The output needs only the boundary state entering each chunk and the intra-chunk attention matrix for , so it is computed afterwards in a third kernel that is parallel over chunks:
The serial pass stores the boundary state (in bf16) and for every chunk, and keeps its own accumulation in fp32.
for chunk in tl.range(0, tl.cdiv(tl.maximum(eos - bos, 0), CHUNK), num_stages=4):
record = base_record + chunk
tokens = bos + chunk * CHUNK + rows
active = tokens < eos
base = (record * H + head) * CHUNK
vector_offsets = base * D + rows[:, None] * D + channels[None, :]
kd_tile = tl.load(kd + vector_offsets)
kt_tile = tl.load(kt + vector_offsets)
y_offsets = base * D + rows[:, None] * D + values[None, :]
u_tile = tl.load(inv + y_offsets)
decay_base = (record * H + head) * 2 * D
if SCALAR_DECAY:
scaled_state = s
state_bf = scaled_state.to(tl.bfloat16)
y = u_tile - tl.dot(kd_tile, tl.trans(state_bf), out_dtype=tl.float32)
y = tl.where(active[:, None], y, 0.0).to(tl.bfloat16)
tail_decay = tl.load(decay + decay_base + D)
s = scaled_state * tail_decay + tl.dot(tl.trans(y), kt_tile, out_dtype=tl.float32)
else:
initial_decay = tl.load(decay + decay_base + channels)
tail_decay = tl.load(decay + decay_base + D + channels)
scaled_state = s * initial_decay[None, :]
state_bf = scaled_state.to(tl.bfloat16)
y = u_tile - tl.dot(kd_tile, tl.trans(state_bf), out_dtype=tl.float32)
y = tl.where(active[:, None], y, 0.0).to(tl.bfloat16)
s = scaled_state * tail_decay[None, :] + tl.dot(tl.trans(y), kt_tile, out_dtype=tl.float32)
h_offsets = (record * H + head) * D * D + values[:, None] * D + channels[None, :]
tl.store(h_out + h_offsets, state_bf)
tl.store(y_out + y_offsets, y)
tl.store(new_state + state_offsets, s) base = (record * H + head) * CHUNK
vector_offsets = base * D + rows[:, None] * D + channels[None, :]
matrix_offsets = base * CHUNK + rows[:, None] * CHUNK + tl.arange(0, CHUNK)[None, :]
qd_tile = tl.load(qd + vector_offsets)
mqk_tile = tl.load(mqk + matrix_offsets)
y_offsets = base * D + rows[:, None] * D + values[None, :]
y_tile = tl.load(y + y_offsets)
h_offsets = (record * H + head) * D * D + values[:, None] * D + channels[None, :]
h_tile = tl.load(h + h_offsets)
o = tl.dot(qd_tile, tl.trans(h_tile), out_dtype=tl.float32)
o += tl.dot(mqk_tile, y_tile, out_dtype=tl.float32)
output_offsets = tokens[:, None] * (H * D) + head * D + values[None, :]
tl.store(output + output_offsets, o.to(tl.bfloat16), mask=active[:, None])The serial pass is parallel only over sequences, heads and value tiles. We split the 128 value channels into tiles chosen from the number of sequence-head pairs, so that short batches still fill the GPU, and cap the tile width at 32 channels for the state pass, because wider tiles with a pipelined loop ran out of registers on packed batches in our measurements.
Channel-wise decay with a mid-chunk anchor
For GDN the decay ratio is a scalar per pair of tokens, so and are computed once per query/key head and shared by its two value heads, each of which applies its own ratio elementwise.
qv = tl.load(q + offsets, mask=mask, other=0.0)
kv = tl.load(k + offsets, mask=mask, other=0.0)
kt_factors = tl.trans(kv)
gram_kk = tl.dot(kv, kt_factors, out_dtype=tl.float32)
gram_qk = tl.dot(qv, kt_factors, out_dtype=tl.float32)
lower = rows[:, None] > cols[None, :]
lower_eq = rows[:, None] >= cols[None, :]
for subhead in tl.static_range(0, 2):
head = 2 * qk_head + subhead
gate_offsets = tokens * 8 + head
av = tl.load(a + gate_offsets, mask=active, other=0.0).to(tl.float32)
x = av + tl.load(dt_bias + head)
softplus = tl.where(x > 20.0, x, tl.log2(1.0 + tl.exp2(x * 1.4426950408889634)) * 0.6931471805599453)
log_gate = tl.where(active, -tl.exp(tl.load(A_log + head)) * softplus, 0.0)
cumulative = tl.cumsum(log_gate, axis=0)
total = tl.sum(log_gate, axis=0)
ratio = tl.where(lower_eq, tl.exp(cumulative[:, None] - cumulative[None, :]), 0.0)
beta = tl.sigmoid(tl.load(b + gate_offsets, mask=active, other=0.0).to(tl.float32))
nil = tl.where(lower, ratio * gram_kk * beta[:, None], 0.0)
inv = _unit_lower_inverse(nil, CHUNK=CHUNK)
mqk = ratio * gram_qk * scale
kd = (tl.exp(cumulative)[:, None] * kv).to(tl.bfloat16)
qd = (tl.exp(cumulative)[:, None] * qv * scale).to(tl.bfloat16)
kt = (tl.exp(total - cumulative)[:, None] * kv).to(tl.bfloat16)
value_offsets = tokens[:, None] * (8 * D) + head * D + channels[None, :]
vv = tl.load(v + value_offsets, mask=mask, other=0.0).to(tl.float32)
u = tl.dot(inv.to(tl.float16), (beta[:, None] * vv).to(tl.float16), out_dtype=tl.float32)
w = tl.dot(inv.to(tl.float16), (beta[:, None] * kd.to(tl.float32)).to(tl.float16), out_dtype=tl.float32)For KDA the ratio differs per channel and cannot be pulled out of the inner product. We factor it instead into a decayed and a restored key, and , so that every decay-weighted Gram entry is an ordinary dot product, , and . The anchor is the cumulative decay at the middle of the chunk. In the bounded gate used here each token's log-decay lies in , so with a 32-token chunk and the anchor at token 16 the exponents of the decayed and restored keys stay within , which fp32 and bf16 represent; anchoring at the chunk start would require exponents twice as large. The carried state is first scaled by and later by , which is why the prepass stores both factors.
gv = tl.load(g + offsets, mask=mask, other=0.0).to(tl.float32)
gv += tl.load(dt_bias + head * D + channels)[None, :]
log_decay = -5.0 * tl.sigmoid(tl.exp(tl.load(A_log + head)) * gv)
log_decay = tl.where(mask, log_decay, 0.0)
cumulative = tl.cumsum(log_decay, axis=0)
total = tl.sum(log_decay, axis=0)
if CHUNK >= 32:
anchor = tl.sum(tl.where(rows[:, None] == CHUNK // 2 - 1, cumulative, 0.0), axis=0)
else:
anchor = tl.zeros([D], dtype=tl.float32)
initial_decay = tl.exp(anchor)
tail_decay = tl.exp(total - anchor)
kd = (tl.exp(cumulative - anchor[None, :]) * kv).to(tl.bfloat16)
qd = (tl.exp(cumulative - anchor[None, :]) * qv * scale).to(tl.bfloat16)
kr = (tl.exp(anchor[None, :] - cumulative) * kv).to(tl.bfloat16)
kt = (tl.exp(total[None, :] - cumulative) * kv).to(tl.bfloat16)
beta = tl.sigmoid(tl.load(b + tokens * H + head, mask=active, other=0.0).to(tl.float32))
gram = tl.dot(kd, tl.trans(kr), out_dtype=tl.float32)
lower = rows[:, None] > cols[None, :]
nil = tl.where(lower, gram * beta[:, None], 0.0)
inv = _unit_lower_inverse(nil, CHUNK=CHUNK)
mqk = tl.dot(qd, tl.trans(kr), out_dtype=tl.float32)
mqk = tl.where(rows[:, None] >= cols[None, :], mqk, 0.0)
vv = tl.load(v + offsets, mask=mask, other=0.0).to(tl.float32)
u = tl.dot(inv.to(tl.bfloat16), (beta[:, None] * vv).to(tl.bfloat16), out_dtype=tl.float32)
w = tl.dot(inv.to(tl.bfloat16), (beta[:, None] * kd.to(tl.float32)).to(tl.bfloat16), out_dtype=tl.float32)The triangular inverse by Neumann doubling
is strictly lower triangular, hence nilpotent, so
and factors give the exact inverse with a handful of tensor-core products. Kachua already computes its inverse this way. We start from by a subtraction instead of a product with the identity, and run the products in fp16 with fp32 accumulation.
@triton.jit
def _unit_lower_inverse(n, CHUNK: tl.constexpr):
eye = (tl.arange(0, CHUNK)[:, None] == tl.arange(0, CHUNK)[None, :]).to(tl.float32)
x = eye - n
p = tl.dot(n.to(tl.float16), n.to(tl.float16), out_dtype=tl.float32)
if CHUNK >= 4:
x += tl.dot(p.to(tl.float16), x.to(tl.float16), out_dtype=tl.float32)
p = tl.dot(p.to(tl.float16), p.to(tl.float16), out_dtype=tl.float32)
if CHUNK >= 8:
x += tl.dot(p.to(tl.float16), x.to(tl.float16), out_dtype=tl.float32)
p = tl.dot(p.to(tl.float16), p.to(tl.float16), out_dtype=tl.float32)
if CHUNK >= 16:
x += tl.dot(p.to(tl.float16), x.to(tl.float16), out_dtype=tl.float32)
if CHUNK >= 32:
p = tl.dot(p.to(tl.float16), p.to(tl.float16), out_dtype=tl.float32)
x += tl.dot(p.to(tl.float16), x.to(tl.float16), out_dtype=tl.float32)
return xDispatch from the layout
The decomposition above wins where state parallelism is scarce and the serial depth is moderate;
the warp-specialised kernels win on deep or crowded batches. The entry points therefore choose a
kernel from the number of sequences , the total token count , the longest sequence, its
position in the batch and, for KDA, the head count. All of these are derived from cu_seqlens once
and cached on it, so a captured call reads nothing from the GPU. For KDA with 8 heads every batch
runs our kernel, with 32-token chunks once the longest sequence reaches 256 tokens; for larger head
counts shallow batches run a variant that solves inside the serial pass, and the rest run FlashKDA,
to which we pass a cached chunk-prefix tensor so it no longer rebuilds it on every call. For GDN our
kernel covers single sequences of 160 to 2048 tokens, two-sequence batches of up to 3072 tokens whose
longer sequence has at least 128 tokens, and part of the batches with more sequences, mostly three to
eight, with 64-token chunks in one medium regime; very short batches go to Kachua, and deep or crowded
ones to FlashInfer or FlashQLA.
@torch.no_grad()
def gdn_prefill(q, k, v, state, A_log, a, dt_bias, b, cu_seqlens, scale):
T, N = q.shape[0], cu_seqlens.shape[0] - 1
if scale is None or scale == 0.0:
scale = 1.0 / math.sqrt(q.shape[-1])
has_empty = _has_empty_seq(cu_seqlens)
if N >= 9 and (N >= 16 or T >= 192) and not has_empty:
return _gdn_flashinfer(q, k, v, state, A_log, a, dt_bias, b, cu_seqlens, scale)
if T >= 512:
if (N == 1 and T <= 2048) or (N == 2 and T <= 3072) or has_empty:
return gdn_wy(q, k, v, state, A_log, a, dt_bias, b, cu_seqlens, scale)
max_len = _max_seq_len(cu_seqlens)
if 5 <= N <= 8 and (1280 if N == 6 else 1408) <= max_len <= 2048 and T < 2048:
if (N == 5 and cu_seqlens._prefill_max_seq_pos > 2) or \
(N == 6 and cu_seqlens._prefill_max_seq_pos > 0):
return gdn_wy(q, k, v, state, A_log, a, dt_bias, b, cu_seqlens, scale, chunk=64)
return _gdn_flashqla(q, k, v, state, A_log, a, dt_bias, b, cu_seqlens, scale)
if 3 <= N <= 8 and T >= (2304 if N == 4 else 2048) and max_len <= 2048 and \
(N == 3 or 4 * max_len <= 3 * T):
return _gdn_flashqla(q, k, v, state, A_log, a, dt_bias, b, cu_seqlens, scale)
if (N == 3 and max_len <= 1408) or \
(N == 4 and max_len <= 1920) or (5 <= N <= 8 and T < 2048):
return gdn_wy(q, k, v, state, A_log, a, dt_bias, b, cu_seqlens, scale)
if T >= 1024:
return _gdn_flashinfer(q, k, v, state, A_log, a, dt_bias, b, cu_seqlens, scale)
if (N > 1 and _max_seq_len(cu_seqlens) >= 128) or (N == 1 and _max_seq_len(cu_seqlens) >= 160):
return gdn_wy(q, k, v, state, A_log, a, dt_bias, b, cu_seqlens, scale)
return _gdn_kachua(q, k, v, state, A_log, a, dt_bias, b, cu_seqlens, scale)Results
| Method | ① | ② | ③ | ④ | ⑤ | ⑥ | ||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Correctness ↑ | Speed-up over starter ↑ | Correctness ↑ | Speed-up over starter ↑ | Correctness ↑ | Speed-up over starter ↑ | Correctness ↑ | Speed-up over starter ↑ | Correctness ↑ | Speed-up over starter ↑ | Correctness ↑ | Speed-up over starter ↑ | |
| Kimi K3.1 Pro | 1.000 | 3.220 | 0 | 1.489 | 1.000 | 3.158 | 1.000 | 1.448 | 1.000 | 1.725 | 1.000 | 1.943 |
| FLA chunk kernels | 1.000 | 1.004 | 1.000 | 1.000 | 1.000 | 1.005 | 1.000 | 0.999 | 1.000 | 1.000 | 1.000 | 0.999 |
| FLA fused recurrent | 1.000 | 0.624 | 1.000 | 0.067 | 1.000 | 0.613 | 1.000 | 0.104 | 1.000 | 0.198 | 1.000 | 0.229 |
| Kachua | 1.000 | 3.172 | 0 | 1.059 | 1.000 | 1.001 | 1.000 | 0.999 | 1.000 | 1.000 | 1.000 | 1.000 |
| FlashInfer | 1.000 | 0.873 | 1.000 | 1.293 | 1.000 | 0.997 | 1.000 | 1.007 | 1.000 | 1.001 | 1.000 | 1.000 |
| FlashQLA | 1.000 | 1.146 | 1.000 | 1.142 | 1.000 | 1.001 | 1.000 | 1.010 | 1.000 | 1.000 | 1.000 | 0.999 |
| FlashKDA, single kernel | 1.000 | 1.003 | 1.000 | 0.999 | 1.000 | 1.320 | 1.000 | 0.882 | 1.000 | 1.713 | 1.000 | 1.937 |
| FlashKDA | 1.000 | 3.167 | 1.000 | 1.332 | 1.000 | 1.327 | 1.000 | 1.002 | 1.000 | 1.708 | 1.000 | 1.952 |
- ① Gated DeltaNet prefill (Qwen3-Next layer, TP4) · short batches, at most 512 tokens
- ② Gated DeltaNet prefill · long batches, 513 to 8K tokens
- ③ Kimi Delta Attention prefill · Kimi Linear layer, TP4, 8 heads · short batches
- ④ Kimi Delta Attention prefill · 8 heads · long batches
- ⑤ Kimi Delta Attention prefill · 64 heads · long batches
- ⑥ Kimi Delta Attention prefill · 96 heads · long batches
On five of the six settings the outputs pass the tolerance check, and on those five our submission is ahead of the starter everywhere. The clearest gains are on KDA-short, a speedup of 3.158 against 1.327 for the published per-batch dispatch and 1.320 for FlashKDA, and on KDA-long, 1.448 against 1.002 for the dispatch baseline. These are the settings where our margin over every kernel the dispatcher can fall back on is largest, which points to the decomposition itself rather than to the choice between existing kernels. On GDN-short we reach 3.220, level with or slightly above Kachua's 3.172 and the dispatch baseline's 3.167. On KDA-h64-long and KDA-h96-long our speedups of 1.725 and 1.943 are level with FlashKDA's 1.713 and 1.937.
On GDN-long the submission fails: its correctness value is 0.000, so its measured speedup of 1.489 is not a valid result. Kachua fails the same setting (0.000), while every other baseline passes it. Two properties of our GDN kernel are candidates, and we name both in the limitations: the 64-token chunks fall outside the range in which our inverse is exact, and in development we found an input family on which the GDN chunk solve loses accuracy at any chunk size.
How we got here
We began by timing the starter and the reference kernels on short, mixed, single-long and packed batches. In those development measurements Kachua's value-tiled formulation reduced the GDN short-batch geometric mean from 35.4 µs to 13.6 µs, while short KDA on FlashKDA stayed at 38.4 µs. That gap set the plan: generalise the value-tiled decomposition to channel-wise decay.
- A single fused consumer serialised the output. Our first KDA kernel computed outputs inside the serial chunk loop. Profiling one long sequence showed that loop at 222 µs, against 82 µs and 24 µs for FLA's separate state and output phases. Because no output token depends on later states, we split the output into its own chunk-parallel kernel; the output phase then took 15 µs in the same development measurement.
- 16-token chunks took too many serial steps on long sequences. Extending to 32 tokens needed the mid-chunk anchor to keep the decay exponents in range; it cut single-sequence latency by about 26% in development, while short packed batches kept 16-token chunks.
- The solve belonged in the prepass. Moving and out of the serial loop, together with a fourth pipeline stage and 32-token chunks for every batch of 512 tokens or more, lowered our development KDA long-batch geometric mean from 191.9 µs (after three-stage pipelining of the state loop) to 164.5 µs, against 227.3 µs for FLA on the same batches. Capping the state tiles at 32 channels then removed a register-pressure collapse on packed batches.
- For head-rich KDA, Triton code generation was the limit. On a packed batch with many heads FlashKDA's recurrence took 531 µs while the equivalent Triton work took over 1.3 ms, so we kept FlashKDA there. After the later improvements a re-test showed our kernel only marginally ahead on isolated sequences and behind elsewhere, and the boundary stayed.
- Several kernel-level ideas did not pay. A block-recursive inverse with two 16-token diagonal blocks had fewer operations but was slower because of layout conversions; two- and eight-warp prepares, a joint right-hand side for and , removing FlashKDA's beta transpose (by strided loads, a two-dimensional TMA descriptor that faulted, or a Triton transpose) and exact grid sizing were all slower or neutral and were dropped.
- Context parallelism over chunks was abandoned. We prototyped an associative merge of chunk summaries for long sequences. After fixing a missing anchor factor in the transition and missing decay factors in the local summary, the prototype was still numerically fragile and about twice as slow; we attribute the cost to composing dense transitions instead of compact WY products.
- The dispatch grew from sweeps. Most of the later session went into randomized and grid sweeps of batch layouts against the starter, each of which moved a boundary: sequence-count thresholds for FlashInfer and FlashQLA, a three-quarters balance guard for FlashQLA, a 160-token boundary for single sequences, and a position test on the longest sequence after we found that FlashQLA is slow when the longest sequence comes late. A longest-first reordering of FlashQLA's blocks did not fix that, and forcing its intra-card context parallelism was slower. A late change replaced that fallback with 64-token chunks of our own kernel, which in development lowered two such layouts from about 86 µs and 82 µs to about 72 µs and 69 µs, with outputs that passed the check.
- A numerical probe exposed a failure we did not resolve. With identical queries and keys, near-unit decay and close to one, our GDN kernel failed the tolerance check on sequences of 256 tokens and more, while KDA passed. TF32 products did not help and 16-token chunks were worse, so we concluded that the chunk system itself is ill-conditioned on such inputs and left it. The ordinary development inputs passed.
Limitations
- GDN-long fails the correctness check. Whatever the cause, a GDN route of the submission produces output outside the tolerance there, so no speedup is claimed on that setting. The dispatch baseline passes the same setting with 1.332.
- The inverse is exact only up to 32-token chunks. The doubling in
_unit_lower_inversestops at , so it sums for . The 64-token GDN route therefore drops the terms ; this is an approximation whose error depends on the decay and on how correlated the keys are within a chunk, and our development checks did not bound it. - The GDN chunk solve is ill-conditioned on correlated keys. Our own probe shows failures with strongly aligned keys and weak decay, and we found no precision change that cured them.
- Part of the speed comes from the dispatch, and the dispatch is fitted. The GDN rule contains thresholds on exact sequence counts, a balance guard and a test on the position of the longest sequence, each set from development sweeps. They are functions of the layout, but they are many, and a layout family the sweeps did not sample may land on a slower kernel. On KDA-h64-long and KDA-h96-long our result is essentially FlashKDA's.
- The serial depth is unchanged. Every route of our kernel still walks the boundary states of each sequence one chunk at a time; our attempt at context parallelism failed, so deep single sequences remain on other kernels.
- Mixed precision is a choice per operator. The products that form and run in fp16 for GDN, whose decay factors are at most one, and in bf16 for KDA, whose anchored decayed keys need the wider range; the KDA path also relies on the anchor keeping exponents within , which holds for the bounded gate of this task and not for an unbounded one.
References
- Songlin Yang, Jan Kautz, Ali Hatamizadeh (2025). Gated Delta Networks: Improving Mamba2 with Delta Rule. ICLR.
- Kimi Team, Yu Zhang, Zongyu Lin et al. (2025). Kimi Linear: An Expressive, Efficient Attention Architecture.
- Songlin Yang, Bailin Wang, Yu Zhang et al. (2024). Parallelizing Linear Transformers with the Delta Rule over Sequence Length. NeurIPS.
- Zihao Ye, Lequn Chen, Ruihang Lai et al. (2025). FlashInfer: Efficient and Customizable Attention Engine for LLM Inference Serving.