Cross-Attention › Self-Attention vs. Cross-Attention
Language AI HandbookAll chapters Cross-Attention › Self-Attention vs. Cross-AttentionOne section · Sections in this chapter2 / 14 ↓
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
Self-attention and cross-attention share the same mathematical formulation: QKV representations combined with scaled dot-product attention. The critical difference lies in where these components come from. To understand this distinction precisely, we need to see both forms side by side and appreciate what changes when you move from one to the other.
In self-attention, all three projections derive from the same sequence. The intuition is that every token in the sequence can act as a questioner (query), an advertiser (key), and a content provider (value) simultaneously. A token might ask "what context surrounds me?" while also answering the questions of other tokens by advertising its own content. This simultaneous questioning and answering within a single sequence is what lets self-attention capture rich intra-sequence dependencies: subject-verb agreement, pronoun resolution, long-range semantic coherence.
Given an input matrix containing token representations, self-attention computes QKV representations as:
where:
- : the input sequence matrix, with tokens each represented as a -dimensional vector
- : the learned query projection matrix
- : the learned key projection matrix
- : the learned value projection matrix
- : the resulting query and key matrices
- : the resulting value matrix
Every token attends to every other token (and itself) within the same sequence. The attention matrix is square with shape , and each entry describes how much token attends to token .
In cross-attention, the queries come from one sequence while keys and values come from a different sequence. This separation is the key distinction: the decoder generates queries to ask "what do I need?" while the encoder provides keys and values to answer "here's what I have." The decoder cannot answer its own questions by consulting only its own partial output. It needs access to the encoded representation of the source sequence. Cross-attention provides exactly that access, in a learnable and differentiable way.
where:
- : the decoder's current representations (what's been generated so far)
- : the encoder's output representations (the processed source sequence)
- : the learned projection matrices for queries and keys
- : the learned projection matrix for values
- : the number of tokens in the decoder sequence (target length so far)
- : the number of tokens in the encoder sequence (source length)
- : the model dimension (size of each token's representation)
The key insight is that the encoder output appears twice: once for the keys and once for the values. The keys tell the decoder "this is what I offer," and the values tell the decoder "this is what I'll send you if you attend to me." These two roles are separated because the model benefits from being able to project the encoder's information into two different spaces: one for matching (keys against queries) and one for content transmission (values).
Cross-attention allows tokens in one sequence (the decoder) to attend to tokens in a different sequence (the encoder). Queries come from the decoder, while keys and values come from the encoder, enabling information to flow from the source sequence to the target sequence during generation.
This asymmetry is what makes cross-attention powerful for tasks like translation. The decoder asks questions (queries) based on what it has generated so far, and the encoder's representations provide the answers (keys and values). The learned projection matrices , , and are trained end-to-end by gradient descent, so the model learns to ask good questions and provide informative answers entirely from the supervision signal (for translation, the signal is simply "produce the correct target sentence given the source").