gated-deltanet

The gated delta rule in recurrent form, loadable through kernels. The reference baseline is transformers' torch_recurrent_gated_delta_rule, which this kernel matches to 5.5e-7 relative and replaces via @use_kernel_forward_from_hub("Qwen3_5GatedDeltaNet").

Three of every four layers in a hybrid-attention model are not attention: they carry a fixed-size state per head and update it once per token, so memory does not grow with context. The catch is speed: written in framework ops the recurrence is a Python loop that reads and writes the whole state several times per token. This kernel holds the entire state in registers for the whole sequence, one block per (batch, head) pair, reading it from memory once and writing it once whatever the length, which makes prefill and decode the same kernel and the eager loop about twenty times slower.

The 128 by 128 recurrent state blooms, wipes under the retention gate, and re-blooms while the memory line stays flat against attention's climbing KV curve

One head's 128 x 128 state filmed live across 2,048 tokens of updates, including periodic retention-gate wipes (g large and negative erases the past, then the state re-blooms). At 48 layers the recurrent memory is 144 MB at any context; attention's KV cache at the same geometry passes 155 GB by 131,072 tokens. Output matches the transformers reference to 1e-6.

Usage

import torch
from kernels import get_kernel

gdn = get_kernel("phanerozoic/gated-deltanet", version=1, trust_remote_code=True)

state = gdn.new_state(batch, heads, key_dim, value_dim)     # [B, H, Dk, Dv] fp32

# prefill and decode are the same call; the state carries
out = gdn.gated_delta_rule(q, k, v, g, beta, state, use_qk_l2norm=True)

version selects the release branch; trust_remote_code is required by kernels for publishers without the trusted-publisher mark. g is the log decay: the kernel applies exp(g), matching the reference. q, k are [B, T, H, Dk]; v is [B, T, H, Dv]; g, beta are [B, T, H]; returns [B, T, H, Dv].

API

Symbol Purpose
gated_delta_rule(q, k, v, g, beta, state, use_qk_l2norm) run the recurrence, updating state in place
new_state(batch, heads, key_dim, value_dim) a zeroed [B, H, Dk, Dv] fp32 state
GatedDeltaNet(batch, heads, key_dim, value_dim, use_qk_l2norm) module holding the state across steps, one per layer

Method

Per token the update is a decay, a read, a rank-one correction and a read:

S    <- S * exp(g)      gated decay, one scalar per head
m    <- k^T S           what the state already predicts for this key
d    <- (v - m) * beta  the delta, scaled by the write gate
S    <- S + k d^T       rank-one write
out  <- q^T S           read with the query

The delta rule corrects the state by the difference between the value and what the state already holds for that key, so writing the same key twice does not double its contribution. The state is the whole cost, 64 KiB per head, and here one block owns one (batch, head) pair with the state in its threads' registers for the whole sequence: a thread owns one value column and a slice of the key rows. Every intermediate token touches registers only.

Measured

RTX 6000 Ada, 48 heads, 128 x 128 state, against the eager reference on the same inputs:

batch tokens eager this speedup
1 1 0.435 ms 0.028 ms 15.3x
1 64 7.451 ms 0.347 ms 21.4x
1 512 61.114 ms 2.787 ms 21.9x
4 256 40.268 ms 2.468 ms 16.3x

State footprint, Bonsai 27B geometry: 3.0 MB per layer per sequence, 144 MB across all 48 linear layers, constant in context length.

Correctness

Verified against transformers 5.13:

  • Output and final state agree with the reference to 5.5e-7 relative across batch 1 to 3, 8 to 256 tokens, 2 to 8 heads, 64 and 128 dimensions, with and without the in-kernel L2 normalization.
  • Chunked equals whole: a 96-token sequence in six 16-token calls, carrying the state, is torch.equal to one call; decoding token by token equals the batched pass, state included.
  • Gate semantics: beta = 0 leaves the state exactly untouched; a large negative g shrinks it by nine orders of magnitude; g = 0 with beta = 0 is the exact identity.
  • Deterministic: repeated runs are bitwise identical.
  • The delta rule is stable only while beta * |k|^2 stays near or below 2; real models L2-normalize keys (use_qk_l2norm), and on out-of-region input this kernel reproduces the reference's divergence to five significant figures.

Requirements and limits

  • NVIDIA GPU with compute capability 8.0+; fp32 arithmetic (the state is a long product chain).
  • Dv must divide the 256-thread block, Dk divisible by 256 / Dv, Dk / (256 / Dv) <= 64; Dk <= 256. The 128 x 128 and 64 x 64 geometries satisfy all constraints.
  • Recurrent form only: long prefills would run faster in the chunked formulation; this kernel keeps the exact recurrence, which is what makes chunked and token-by-token bitwise identical.
  • Grouped heads are the caller's: repeat key and query heads before calling, as the reference layer does. Forward only.

References

Yang et al., "Gated Delta Networks: Improving Mamba2 with Delta Rule" (2024); Yang et al., "Parallelizing Linear Transformers with the Delta Rule over Sequence Length" (2024); Schlag et al., "Linear Transformers Are Secretly Fast Weight Programmers" (2021); the reference implementation in transformers' Qwen3_5GatedDeltaNet.

License

Apache-2.0.

Downloads last month
-
apache-2.0
Supported hardwares new
CUDA
8.08.68.99.010.012.0
GPU
B300
288GB
NVIDIA SXM
B200
192GB
NVIDIA SXM
H200
141GB
NVIDIA SXM
H100
80GB
GPU
H800
80GB
GPU
H20
96GB
GPU
L40s
48GB
GPU
L40
48GB
GPU
L20
48GB
GPU
L4
24GB
DGX Spark
GB10
128GB
GPU
RTX PRO 6000 WS
96GB
GPU
RTX PRO 6000 Max-Q
96GB
GPU
RTX PRO 5000
48GB
GPU
RTX PRO 4500 WS
32GB
GPU
RTX PRO 4000
24GB
GPU
RTX PRO 4000 SFF
24GB
GPU
RTX PRO 2000
16GB
GPU
RTX 6000 Ada
48GB
GPU
RTX 5880 Ada
48GB
RTX
RTX 5000 Ada
32GB
GPU
RTX 4500 Ada
24GB
RTX
RTX 4000 Ada
20GB
RTX
RTX 4000 SFF Ada
20GB
GPU
RTX 3500 Ada Mobile
12GB
GPU
RTX 2000 Ada
16GB
GPU
RTX A6000
48GB
GPU
RTX A5000
8GB
GPU
RTX A5000 Max-Q
16GB
GPU
RTX A5000 Mobile
16GB
GPU
RTX A4000
16GB
GPU
RTX A4000 Max-Q
8GB
GPU
RTX A4000 Mobile
8GB
GPU
RTX A3000 Mobile
6GB
GPU
RTX A2000
6GB
GPU
RTX A2000 Embedded
4GB
GPU
RTX A2000 Max-Q
4GB
GPU
RTX A2000 Mobile
4GB
GPU
A800
40GB
GPU
A100
80GB
GPU
A40
48GB
GPU
A30
24GB
GPU
A10
24GB
GPU
A2
16GB
RTX
RTX 5090
32GB
RTX
RTX 5090 D
32GB
RTX
RTX 5090 Mobile
24GB
RTX
RTX 5080
16GB
RTX
RTX 5080 Mobile
16GB
RTX
RTX 5070
12GB
RTX
RTX 5070 Mobile
8GB
RTX
RTX 5070 Ti
16GB
RTX
RTX 5070 Ti Mobile
12GB
RTX
RTX 5060 Ti
16GB
RTX
RTX 5060
8GB
RTX
RTX 5060 Mobile
8GB
RTX
RTX 5050
8GB
RTX
RTX 5050 Mobile
8GB
RTX
RTX 4090
24GB
RTX
RTX 4090D
24GB
RTX
RTX 4090 Mobile
16GB
RTX
RTX 4080 SUPER
16GB
RTX
RTX 4080
16GB
RTX
RTX 4080 Mobile
12GB
RTX
RTX 4070
12GB
RTX
RTX 4070 Mobile
8GB
RTX
RTX 4070 Ti
12GB
RTX
RTX 4070 Super
12GB
RTX
RTX 4070 Ti Super
16GB
RTX
RTX 4060
8GB
RTX
RTX 4060 Ti
8GB
RTX
RTX 4090 Laptop
16GB
RTX
RTX 4080 Laptop
12GB
RTX
RTX 4070 Laptop
8GB
RTX
RTX 4060 Laptop
8GB
RTX
RTX 4050 Laptop
6GB
RTX
RTX 3090
24GB
RTX
RTX 3090 Ti
24GB
RTX
RTX 3080
12GB
RTX
RTX 3080 Ti
12GB
RTX
RTX 3080 Mobile
16GB
RTX
RTX 3070
8GB
RTX
RTX 3070 Ti
8GB
RTX
RTX 3070 Ti Mobile
8GB
RTX
RTX 3060 Ti
8GB
RTX
RTX 3060
12GB
RTX
RTX 3060 Mobile
6GB
RTX
RTX 3050 Mobile
4GB
GPU
RTX 2050 Mobile
4GB
Jetson
Jetson AGX Orin 64GB
64GB
Jetson
Jetson AGX Orin 32GB
32GB
Jetson
Jetson Orin NX 16GB
16GB
Jetson
Jetson Orin NX 8GB
8GB
Jetson
Jetson Orin Nano 8GB
8GB
Jetson
Jetson Orin Nano 4GB
4GB
OS
linux
Arch
x86_64
Kernel Builder
19aaa64
Free AI Image Generator No sign-up. Instant results. Open Now