Luong Attention › Global vs Local Attention

Michael BrenndoerferMay 18, 20256 min read
Language AI HandbookAll chapters
Part 12 of 67Sequence-to-Sequence
Chapter 88Luong Attention › Global vs Local Attention
Reading section 3 of 11 · Global vs Local Attention · 6 minSection 3 of 11 · 6 min

Book contents

Section 3 of 11 · 6 min read

Luong Attention › Global vs Local AttentionOne section · Sections in this chapter3 / 11 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Beyond score functions, Luong et al. introduced an important architectural distinction: global attention attends to all encoder states, while local attention focuses on a subset of positions around a predicted alignment point.

Global Attention

Global attention is conceptually straightforward. At each decoder timestep tt, we compute attention weights over all encoder hidden states by applying softmax to the alignment scores. The attention weight for source position ss at decoder timestep tt is:

αts=exp⁡(score(ht,hˉs))∑s′=1Sexp⁡(score(ht,hˉs′))\alpha_{ts} = \frac{\exp(\text{score}(h_t, \bar{h}_s))}{\sum_{s'=1}^{S} \exp(\text{score}(h_t, \bar{h}_{s'}))}

where:

  • αts\alpha_{ts}: the attention weight for source position ss when decoding at timestep tt
  • score(ht,hˉs)\text{score}(h_t, \bar{h}_s): the alignment score computed using any of the three score functions
  • SS: the total number of encoder positions (source sequence length)
  • exp⁡(⋅)\exp(\cdot): the exponential function, which ensures all values are positive before normalization
  • The denominator normalizes the weights so they sum to 1 across all source positions

The context vector is then computed as the weighted sum of all encoder states:

ct=∑s=1Sαtshˉsc_t = \sum_{s=1}^{S} \alpha_{ts} \bar{h}_s

where:

  • ct∈Rdhˉc_t \in \mathbb{R}^{d_{\bar{h}}}: the context vector at decoder timestep tt
  • αts\alpha_{ts}: the attention weight for position ss (how much to focus on that position)
  • hˉs\bar{h}_s: the encoder hidden state at position ss

This weighted sum allows the decoder to "focus" on relevant source positions: positions with high attention weights contribute more to the context vector, while positions with low weights contribute less.

Global attention is what we typically mean when we say "attention" without qualification. It allows the model to attend to any position in the source sequence, which is essential for tasks like translation where word order can differ dramatically between languages.

The computational cost of global attention is O(S)O(S) per decoder step, where SS is the source sequence length. For most NLP tasks with sequences of hundreds or thousands of tokens, this is manageable. However, for very long sequences (documents, books, genomic data), global attention becomes a bottleneck.

Local Attention

Local attention restricts the attention window to a subset of encoder positions centered around an alignment point ptp_t. This reduces computation and can also serve as an inductive bias when alignments are expected to be roughly monotonic.

Out[6]:
Visualization
Heatmap showing attention weights across all encoder positions for global attention, with a diagonal bias.
Global attention weights over a 12-token source sequence for each of 10 target positions. The diagonal pattern shows that each target token attends most strongly to the corresponding source region, while still maintaining non-zero weights across all positions for maximum flexibility.
Heatmap showing attention weights concentrated in a narrow diagonal band for local attention.
Local attention with window half-width D=2 over the same source and target lengths. Attention weights are non-zero only within a narrow band around the predicted alignment point, reducing computation from O(S) to O(D) per step.

Luong et al. propose two variants for determining the alignment point ptp_t:

Monotonic alignment (local-m): The alignment point is simply set to pt=tp_t = t, assuming the source and target sequences are roughly aligned. This works well for tasks like speech recognition where the output follows the input order.

Predictive alignment (local-p): The model learns to predict the alignment point using a small neural network. The predicted alignment point is:

pt=S⋅σ(vp⊤tanh⁡(Wpht))p_t = S \cdot \sigma(v_p^\top \tanh(W_p h_t))

where:

  • ptp_t: the predicted alignment point (center of the attention window) at decoder timestep tt
  • SS: the source sequence length, used to scale the output to a valid position
  • σ(⋅)\sigma(\cdot): the sigmoid function, which maps any real number to [0,1][0, 1]
  • Wp∈Rdh×dhW_p \in \mathbb{R}^{d_h \times d_h}: a learned weight matrix that transforms the decoder hidden state
  • vp∈Rdhv_p \in \mathbb{R}^{d_h}: a learned weight vector that projects to a scalar
  • hth_t: the current decoder hidden state

The sigmoid ensures the output is between 0 and 1, and multiplying by SS scales it to a valid source position. This allows the model to learn non-monotonic alignments while still restricting attention to a local window.

Within the window [pt−D,pt+D][p_t - D, p_t + D], attention weights are computed normally but multiplied by a Gaussian centered at ptp_t:

αts=align(ht,hˉs)⋅exp⁡(−(s−pt)22σ2)\alpha_{ts} = \text{align}(h_t, \bar{h}_s) \cdot \exp\left(-\frac{(s - p_t)^2}{2\sigma^2}\right)

where:

  • αts\alpha_{ts}: the final attention weight for position ss at decoder timestep tt
  • align(ht,hˉs)\text{align}(h_t, \bar{h}_s): the base attention weight from softmax over the local window
  • ss: the source position being attended to
  • ptp_t: the predicted center of the attention window
  • σ\sigma: the standard deviation of the Gaussian (controls window sharpness), typically set to D/2D/2
  • The exponential term is a Gaussian that peaks at s=pts = p_t and decays for positions farther from the center

The Gaussian favors positions near the center of the window. This provides a soft boundary rather than a hard cutoff. Positions at the edge of the window receive lower weights even if their alignment scores are high.

When to Use Each

The choice between global and local attention depends on your task:

  • Global attention is the default choice for most NLP tasks. Translation, summarization, and question answering all benefit from the ability to attend to any position. The computational overhead is acceptable for typical sequence lengths.

  • Local attention shines when you have prior knowledge about alignment structure. Speech recognition, where phonemes appear in order, is a natural fit. Document-level tasks with very long sequences can also benefit from the reduced computation.

In practice, global attention dominates because modern hardware handles the O(S)O(S) computation efficiently, and the flexibility to attend anywhere is valuable. Local attention is more of a historical curiosity, though its ideas influenced later work on sparse attention patterns in transformers.