Rúben GalvãoThe Rabbit Hole

The Rabbit Hole / AI and machine learning / Language models / Inside attention

Attention gating: should this head add anything?

A head has collected information. How does the model decide how much of it to use?

Finding information is not the same as needing it

The head has done its work: compare queries and keys, apply softmax, then take a weighted sum of values.

For “the old rabbit”, that result might help rabbit pick up old. For another token, the same head may have little to contribute.

In attention sinks, we saw one way a model handles that pressure. But could it simply turn down the collected information before adding anything to the stream?

Let the token control what passes

We used a gate inside the feed-forward network. We can use the same idea here.

Take the current token’s representation through another learned matrix. Then apply sigmoid, giving a multiplier between zero and one for each coordinate of the collected vector.

Near one, the coordinate passes through. Near zero, it becomes quiet.

How much should pass through?

Interactive

Follow the current token through the learned gate. The collected vector stays the same.

Current tokenGate weightsSigmoid01CollectedGateGated output100%100%100%100%100%100%×=Gate bypassed: every coordinate passes unchanged.7,1687,168128128

Choose Let less through. The collected vector has not changed; the gate changes how much of it continues.

The token makes its gate alongside its query. It does not wait to read the collected result. Training adjusts the gate weights together with the query, key and value weights.

Put it inside the head

The gate sits after the weighted sum, before the output projection Wₒ.

Follow the extra branch from the token representations down to the multiplication. The gated result then takes the familiar route through Wₒ and back into the residual stream.

An output gate inside the attention headThe residual stream runs uninterrupted down the left. A branch reads its token vectors. Learned Wq, Wk and Wv matrices produce queries, keys and values. Query–key dot products produce scores; masking sets future scores to negative infinity; softmax produces the attention map. Its weights collect the values. A learned gate made from the current token multiplies the collected vector before the output projection. The output projection Wo expands the collected information. The update is added back to the residual stream.Residual streamToken representationsEarlier context is dimmed; outlined columns are current.Current token computation.7,168192WqEarlier context is dimmed; outlined columns are current.Current token computation.Queries7,168192WkEarlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.Current token computation.Keys7,168128WvEarlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.Current token computation.ValuesDot productsMask: −∞SoftmaxAttention mapWeighted sumof valuesEarlier context is dimmed; outlined columns are current.Current token computation.CollectedGate weightsWgSigmoidEarlier context is dimmed; outlined columns are current.Current token computation.Gate×Earlier context is dimmed; outlined columns are current.Current token computation.7,1681281287,168WₒUpdate+Enriched token representationsCLCLCL7,168192192128128
The extra branch reads the current token, makes a gate, and controls the collected information before Wₒ. The residual stream keeps flowing.

Each head gets its own gate weights. Here, each coordinate has its own multiplier; a simpler alternative uses one multiplier for the whole head.

The residual stream keeps its existing representation. Turning down this branch only turns down this head’s contribution.

What happens to the sink?

Multiplying the collected vector does not change the attention map that produced it. Its rows still add up to one.

But during training, the head now has a direct way to suppress an unhelpful result. It has less reason to route attention into a sink to achieve that behaviour.

Qiu and colleagues found much less attention going to the first token after training with head-specific output gates. Their experiments also found better training stability.

So the two questions are different: where should I gather information? And how much of what I gathered should pass through?

Sources

The gate widget multiplies token vectors by a fixed gate matrix, applies sigmoid, and multiplies the collected vector coordinate by coordinate. Its two inputs show stronger and weaker gates. The whole-head diagram follows the same attention calculation as the introductory article, with the extra output-gate branch added. The cells show compact samples; dimension labels follow the selected model.

← Browse all subjectsBack to the beginning ↑