What is the difference between bidirectional and causal attention?
Last updated
In bidirectional attention, every token attends to every other token, past and future. In causal attention, each token attends only to itself and earlier tokens. The first suits understanding tasks over a complete input; the second is required for left-to-right generation.
Bidirectional (encoder-style: BERT, T5 encoder, ViT):
There is no causal mask, only padding masking. Representations use full left and right context.
It can't be trained with plain next-token prediction, because the answer would be visible. It uses objectives such as masked language modeling or span corruption.
Best for classification, NER, retrieval embeddings, and encoding the source for seq2seq.
Causal (decoder-style: GPT, Llama):
A lower-triangular mask means token t sees only tokens ≤ t.
Trained with next-token prediction. Every position gives a training signal in a single pass.
Past states never change, so a KV cache makes generation incremental.
Trade-offs:
Bidirectional gives richer per-token context but generates poorly.
Causal is generative and scales well as a universal interface.
Both are O(n²). Causal kernels can skip the masked upper triangle, roughly halving compute.
Hybrids:
Prefix-LM: bidirectional over the prompt, causal over the continuation.
Encoder-decoder: a bidirectional encoder, a causal decoder, and cross-attention between them.
- https://www.mantramindai.in/courses/transformer-architecture-q-a/lessons/how-does-bidirectional-encoder-self-attention-differ-from-causal-decoder-self
- https://medium.com/@virenm.meena/deep-learning-fundamentals-interview-questions-answers-bb61958547ca
- https://huggingface.co/docs/transformers/attention_interface