DL0053 Gated Attention

What is Gated Attention and how does it improve transformer architectures over standard scaled dot-product attention?

Answer

Gated Attention (arXiv:2505.06708) applies a head-specific sigmoid gate after Scaled Dot-Product Attention (SDPA) to dynamically modulate attention output. Unlike standard attention where all heads contribute equally, gated attention introduces query-dependent sparse gating that suppresses irrelevant heads and activates only salient ones. This mitigates the attention sink problem where standard transformers concentrate disproportionate attention on the first few tokens, and enhances long-context extrapolation by maintaining diverse attention patterns across sequence lengths.

Gated Attention Mechanism Diagram

Figure 1: Gated attention applies a head-specific sigmoid gate after SDPA to modulate attention output before the residual connection

(1) Post-SDPA Gating: The gate is applied after SDPA computation, not before — each attention head h_i is multiplied by a sigmoid gate g_i = \sigma(W_g \cdot q_i) where W_g is a head-specific projection.
(2) Sparsity Induction: The sigmoid gate produces values in [0, 1], and empirical measurements show mean gate activation of ~0.116, meaning most heads are heavily suppressed — introducing beneficial sparsity without hard pruning.
(3) Attention Sink Mitigation: Standard attention allocates ~46.7% of attention mass to the first token; gated attention reduces this to ~4.8%, distributing attention more uniformly across tokens.

Gate Activation Distribution

Figure 2: Gate activation distribution across 8 attention heads shows heavy suppression (mean ~0.116) with sparse high-activation regions

Mathematical Formulation:
\text{GatedAttn}(Q, K, V) = \text{Concat}(g_1 \odot h_1, \ldots, g_H \odot h_H) W_O
g_i = \sigma(W_g^{(i)} \cdot q_i + b_g^{(i)})

Where:

  •  h_i = \text{SDPA}(q_i, k_i, v_i) is the output of the i-th attention head
  •  g_i \in [0, 1] is the head-specific gate score
  •  W_g^{(i)} \in \mathbb{R}^{d_k \times 1} is a learned projection from query to scalar gate
  •  \sigma is the sigmoid function
  •  \odot denotes element-wise multiplication
  •  W_O is the standard output projection

The gate projection W_g^{(i)} adds only O(d_k) parameters per head — a negligible overhead of ~0.1% of total model parameters — yet significantly improves long-context performance. On the RULER benchmark at 128K context length, gated attention improves needle-in-haystack retrieval accuracy from ~72% to ~94% compared to standard attention.


Login to view more content

Did you solve the problem?

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *