3.4. Linear attention (gated delta rule)¶
3.4.1. Where we are¶
The MLA page showed that when the same math has two equal forms, every mode has to make the same choice between them. Linear attention faces that choice in a sharper form. Training and prefill usually run a chunked algorithm, while decode runs a recurrent step. Here the result is also carried from token to token in a state, so what matters is where the chunk boundaries fall.
3.4.2. The idea: the chunk boundaries decide how the state's sum is grouped¶
A linear attention layer (here Gated DeltaNet) keeps a fixed-size state S for each sequence and head, and updates it with every token. The recurrent form applies one update per token. The chunked form takes C tokens at a time, combines them with matrix products, and carries the state from one chunk to the next.
Take ten tokens and chunks of 4:
- Ten tokens, in chunks of C = 4. The chunks decide how the state's sum is grouped.
- One prefill of tokens 0–9 uses the chunks {0–3}, {4–7} and {8–9}.
- Now prefill tokens 0–7 and then call decode once per token. Each call starts its own chunk, first {8} and then {9}.
- Token 9 is summed in a different grouping, so its output and the state differ in their last bits.
- The aligned version keeps the state at token 8, which is a chunk boundary, and recomputes the unfinished chunk when token 9 arrives.
- That gives {8–9} again, as in prefill, so the bits are the same.
- The recurrent form makes one update per token in every mode, so it gives the same bits at no extra cost.
In the second pattern the same terms are summed in a different grouping, so token 9's output and the state differ in their last bits.
Three facts follow from this:
- The batch is not the problem. Each program owns one sequence and one head, and no reduction crosses sequences, so both forms are batch-invariant by construction (L2).
- Mode invariance depends on the chunk boundaries. The recurrent form is mode-invariant at no cost, because every mode runs the same loop body. The chunked form is mode-invariant only when every chunk starts at an absolute multiple of C.
- The chunked and recurrent forms give different results, and the precision decides how different. They are equally accurate when the chunked matrix products run in fp32. With bf16 operands, which common kernels use, the chunked form carries a much larger error, and that error is most of the gap seen in practice.
3.4.3. The steps¶
1. Confirm that the layer is batch-invariant¶
Rule out the cheap failure first: run bi_check.check_batch from
tools/bi_check.py on a sequence whose last chunk
is only partly filled and whose initial state is not zero. Both forms should
pass, as they did on one GPU type
(report).
2. Choose one form that the trainer and the engine both use¶
Pick one of the options below so that the forward pass uses the same
arithmetic on both sides. The recurrent and chunked kernels are in
gdn_kernels.py, and the
aligned chunk wrapper AlignedChunked is in
gdn_bi.py; see
recipe 7.
Decode and every chunk size must then give an output and a state bitwise
equal to those of a full prefill; this was shown on one GPU type
(report).
| Option | What it gives | Cost class | When to choose it |
|---|---|---|---|
| (a) Run the recurrent forward everywhere | L1–L3, with the same kernel on both sides | trainer forward: moderate for short and medium sequences, large for long ones; decode is the cheapest | the default when you need parity |
| (b) Run the chunked form everywhere, and decode in aligned chunk steps | L1–L3, with the trainer unchanged | trainer: free; decode: large | long sequences, where (a) costs the most |
| (c) Accept the difference | not bitwise equal | free | parity is not required; then prefer fp32 for the chunked products |
Which of (a) and (b) is cheaper overall depends on how a workload divides its time between the trainer's forward pass and rollout decode.
3. Use the same kernel configuration on both sides¶
The configuration sets the kernel's internal reduction tree. For the recurrent kernel, use the same tile width and warp count in the trainer and in the engine; for the chunked kernel, the same chunk size and the same precision for the products. The two sides must give the same bits on the same input: different recurrent configurations each pass L1–L3 but differ from each other (report).
4. Verify each mode as it continues from a saved state¶
Run the verification matrix with each mode continuing from the saved fp32 state. Compare the fp32 outputs before the cast, and the final state, not only the bf16 output.
3.4.4. What can go wrong¶
A chunk of one token is not a recurrent step
If you decode with the chunked kernel one new token at a time, every call starts a new chunk, and the result is not bitwise equal to prefill. Keep the state at the last absolute chunk boundary and recompute the unfinished chunk instead.
Two configurations are two reduction trees
The tile width and warp count of the recurrent kernel change how the sum inside the state update is grouped. To reach L4, pin them to the same values on both sides.
A bf16 output hides differences in the state
Most differences disappear in the final cast to bf16 and reappear in later tokens. Compare the fp32 outputs and the state.
Not yet shown: a backward pass for the recurrent forward; decode batches for the aligned wrapper in which the sequences sit at different positions; and agreement with a production kernel library.
3.4.5. Summary¶
- Both forms are batch-invariant by construction; whether they are mode-invariant depends on where the chunk boundaries fall.
- The recurrent form is mode-invariant at no cost; the chunked form is mode-invariant when its boundaries sit at absolute multiples of C.
- Most of the practical gap between the chunked and the recurrent form comes from the precision of the chunked matrix products.
- Choose (a) recurrent or (b) aligned chunked according to where the cost falls, and pin the kernel configuration on both sides.
3.4.6. Go deeper¶
- Reports: linear attention (GDN) and kernel cases (plain linear attention).
- Code: examples/linear_attention/gdn_bi.py, gdn_kernels.py.
- Recipe: 7, linear attention; the cost that others have reported for training with the recurrent form only is listed in the references.
- Skill: batch-invariant-kernel.