Paper recreation · interactive demo

Gated Attention for Large Language Models: Non-linearity, Sparsity, and Attention-Sink-Free

Qiu et al., Qwen Team, Alibaba — arXiv:2505.06708

Normal attention mixes the values with a softmax and passes the result straight on. This paper adds one thing: a sigmoid gate, computed from the current token, multiplied into each head's output channel by channel. A gate near 0 lets a head say nothing for that token. The paper reports three effects: a non-linearity in the value path, sparse gates, and no attention sink.

\[ \begin{aligned} G &= \sigma\!\left(X W_\theta + b\right) && \text{query-dependent, one value per (head, channel)} \\ \text{out} &= W_O\left(\operatorname{SDPA}(Q, K, V) \odot G\right) && \text{the paper's G1 variant} \end{aligned} \]

What the gate does

One causal attention layer with random weights, run on your text. Drag the gate bias. At +8 every gate is about 1, so the gated layer matches plain attention. At −8 every gate is about 0, so the layer outputs almost nothing. In between, each token decides, channel by channel, how much of each head gets through.

4 heads × 8 channels, model width 32. Every character gets a fixed random embedding plus a position vector, like the repo's lm.py.

Attention (rows = queries, color = √weight)
Gate \(G\) per channel
Hover over a cell to read its value.
Layer output size \(\lVert \text{out}_t \rVert\) per token, all heads
plain (gate removed, same weights)gated

The attention map doesn't change when you move the bias, because the gate acts after the softmax. What changes is how much of each head's result reaches the output. That's the paper's point: a head with nothing useful to add can close its gate. Without a gate, the only way to say nothing is to send its attention to a harmless token, usually position 0. That is the attention sink.

At \(b = 0\) every gate starts near 0.5, which is why the repo's default init halves the layer output. The README suggests \(b = 4\) (\(\sigma(4) \approx 0.98\)) when you add the gate to an already-trained checkpoint.

What training did

These numbers come from the repo's train.py and are not computed on this page. It trains two 3-layer character LMs with the same seed, one with the gate and one without.

gatedplain
final loss0.03780.0457
sink mass per layer[0.045, 0.042, 0.041][0.040, 0.038, 0.037]
peak |hidden|22.829.3
gate sparsity (trained)[0.058, 0.086, 0.162]–
gate sparsity (at init)[0.000, 0.000, 0.000]–
Share of gate values below 0.1, per layer
at init (all zero)after 600 steps
Reproduces: learned sparsityNo gate is below 0.1 at the start. After training, 4–20% of channels are switched off, and the share grows with depth. The same happened on all 5 seeds.
Reproduces loosely: smaller activationsThe gated model's peak hidden value was 8–20% lower on 5 of 5 seeds. That matches the paper's finding on very large activations, but this model is far too small to confirm it.
Does not reproduce: the attention sinkNeither model forms a sink. Attention to position 0 stays close to uniform (about \(1/T\)) in both. Sinks appear in large-scale pretraining, so this run can't confirm or refute the paper's main claim.
Not a loss comparisonThe gated model had the lower loss on 3 of 5 seeds. At this scale, that is noise.

What this demo simplifies

The weights above are random, not trained, so the gate patterns show how the mechanism works, not what a real model learns. It's one layer with no positional encoding beyond a position vector (no RoPE), no grouped-query attention, and no KV cache. Only the G1 variant is implemented: a sigmoid gate after SDPA, separate for each head and channel. That's the one the paper recommends, out of several placements it tests (on Q, K or V, before SDPA, shared across heads, other activations).