Luong Attention › Global vs Local Attention
Language AI HandbookAll chapters 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 , we compute attention weights over all encoder hidden states by applying softmax to the alignment scores. The attention weight for source position at decoder timestep is:
where:
- : the attention weight for source position when decoding at timestep
- : the alignment score computed using any of the three score functions
- : the total number of encoder positions (source sequence length)
- : 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:
where:
- : the context vector at decoder timestep
- : the attention weight for position (how much to focus on that position)
- : the encoder hidden state at position
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 per decoder step, where 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 . This reduces computation and can also serve as an inductive bias when alignments are expected to be roughly monotonic.


Luong et al. propose two variants for determining the alignment point :
Monotonic alignment (local-m): The alignment point is simply set to , 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:
where:
- : the predicted alignment point (center of the attention window) at decoder timestep
- : the source sequence length, used to scale the output to a valid position
- : the sigmoid function, which maps any real number to
- : a learned weight matrix that transforms the decoder hidden state
- : a learned weight vector that projects to a scalar
- : the current decoder hidden state
The sigmoid ensures the output is between 0 and 1, and multiplying by 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 , attention weights are computed normally but multiplied by a Gaussian centered at :
where:
- : the final attention weight for position at decoder timestep
- : the base attention weight from softmax over the local window
- : the source position being attended to
- : the predicted center of the attention window
- : the standard deviation of the Gaussian (controls window sharpness), typically set to
- The exponential term is a Gaussian that peaks at 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 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.