Bidirectional Attention

An attention pattern that lets each token read context on both sides when the model is allowed to see the full sequence.

Bidirectional attention lets a token use context from both earlier and later positions, which makes it the standard attention pattern for encoder-style models that need to understand a whole sequence instead of predicting only the next token.

At a glance

Released

June 2017

Authors

Ashish Vaswani, Noam Shazeer, Niki Parmar, et al.

Optimizes

  • Full Sequence Context
  • Representation Quality

What It Is

Bidirectional attention is the full-context version of self-attention. When the mask allows it, each token can compare itself with tokens to its left and right instead of looking in only one direction.

Why It Exists

Bidirectional attention optimizes for representation quality when the model's job is to understand an entire sequence at once. It helps each position build a richer hidden state before any downstream prediction head reads it.

How It Works

Queries, keys, and values are computed as in ordinary self-attention, but the attention mask does not block later positions. In a masked language model or other encoder setting, the score matrix stays open across the whole visible sequence, so one token can gather evidence from earlier words, later words, and its own position.

Math Or Compute Schema

The formulas below contrast causal attention with bidirectional attention. The projection math stays the same, but the mask changes from next-token-only reach to a full-context mask that keeps both left and right context available.
Causal attention
Attention(Qi,Ki,Vi)=softmax ⁣(Mcausal⊙QiKi⊤dk)Vi\text{Attention}(Q_i, K_i, V_i) = \mathrm{softmax}\!\left(M_{\mathrm{causal}} \odot \frac{Q_i K_i^{\top}}{\sqrt{d_k}}\right) V_i
QQ
Query vectors for head i.
KK
Key vectors for head i.
VV
Value vectors for head i.
HH
Number of query heads.
dkd_k
Key dimension per head.
ii
Query head index.
Bidirectional attention
Attention(Qi,Ki,Vi)=softmax ⁣(Mbi⊙QiKi⊤dk)Vi\text{Attention}(Q_i, K_i, V_i) = \mathrm{softmax}\!\left(M_{\mathrm{bi}} \odot \frac{Q_i K_i^{\top}}{\sqrt{d_k}}\right) V_i
QQ
Query vectors for head i.
KK
Key vectors for head i.
VV
Value vectors for head i.
HH
Number of query heads.
nn
Visible sequence length.
dkd_k
Key dimension per head.
ii
Query head index.
MbiM_{\mathrm{bi}}
Bidirectional mask that keeps both left and right context available inside the visible sequence.

Compared To Nearby Modules

Compared with causal attention, bidirectional attention removes the next-token-only restriction and uses full-sequence context. Compared with cross-attention, it still reads from the same sequence rather than a separate encoder memory. Compared with the broader attention page, this page focuses on the specific mask pattern that encoder-style models use.
Comparison dimensionBidirectional AttentionAttention OverviewMulti-Head Attention
Visible context per queryBoth earlier and later tokens in the same visible sequenceDepends on the architecture and mask pattern chosen for the attention blockDense attention over whatever positions the chosen mask leaves visible
Best-fit task styleEncoder-style understanding and masked language model objectivesGeneral attention lookup across many Transformer designsBaseline per-head attention when the model keeps one key-value pair per query head
Where keys and values come fromThe same sequence as the query positionUsually the same sequence for self-attention, but the overview spans broader patternsThe same sequence as the query position in self-attention blocks

Example Architectures

Bidirectional attention appears in encoder stacks and on the encoder side of encoder-decoder systems, where the model must build a contextual representation of the input before a later component consumes it. That is why it is the natural fit for reading-style tasks, masked language models, and many Transformer pipelines that separate understanding from generation.

Limitations And Tradeoffs

Because tokens can read future positions, plain bidirectional attention does not match next-token generation. Decoder-only language models therefore use causal masks instead when they must predict text one token at a time.

Why It Still Matters

Many readers first meet transformer attention through autoregressive chat models, so a dedicated bidirectional page prevents full-context encoder behavior from being hidden behind decoder-first explanations.

Tags

References

  1. Vaswani, Ashish, et al. "Attention Is All You Need." arXiv, 2017, https://arxiv.org/abs/1706.03762.