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.
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.
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.
| gated | plain | |
|---|---|---|
| final loss | 0.0378 | 0.0457 |
| sink mass per layer | [0.045, 0.042, 0.041] | [0.040, 0.038, 0.037] |
| peak |hidden| | 22.8 | 29.3 |
| gate sparsity (trained) | [0.058, 0.086, 0.162] | – |
| gate sparsity (at init) | [0.000, 0.000, 0.000] | – |
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).