Self-Attention Concept › From Cross-Attention to Self-Attention
Language AI HandbookAll chapters Self-Attention Concept › From Cross-Attention to Self-AttentionOne section · Sections in this chapter2 / 13 ↓
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
In encoder-decoder attention, we have two distinct sequences: the encoder outputs serve as keys and values, while the decoder state provides the query. The decoder asks, "Which parts of the input are relevant to what I'm generating right now?"
Self-attention simplifies this setup. Instead of attending across two different sequences, a single sequence attends to itself. Every token in the sequence serves simultaneously as a query, a key, and a value. The question becomes: "For each token in this sequence, which other tokens are most relevant to understanding its meaning in context?"
Self-attention is an attention mechanism where a sequence attends to itself. Each position can directly interact with every other position in the same sequence, enabling the model to capture dependencies regardless of their distance.
Consider the sentence "The animal didn't cross the street because it was too tired." To understand what "it" refers to, you need to connect it back to "animal" rather than "street." Self-attention enables this by allowing "it" to attend strongly to "animal," building a representation that captures this coreference relationship.
The key insight is that the meaning of a word depends heavily on its context. The word "bank" means something different in "river bank" versus "bank account." Self-attention lets each word gather information from surrounding words to disambiguate and enrich its representation.
Think of self-attention as a mechanism where every word in a sentence gets to vote on how much it contributes to every other word's meaning. The word "tired" votes heavily for "animal" when "it" is being interpreted, because "tired" is more naturally predicated of animals than streets. The word "cross" votes for "street" because streets are things you cross. All of this happens simultaneously, in a single computational pass over the sequence. The result is a set of enriched representations where each word's embedding combines its inherent meaning with its meaning in this specific sentence.
The transition from cross-attention to self-attention also has architectural implications. In an encoder-decoder model with cross-attention, the encoder and decoder are separate modules. The encoder reads the full source sequence, and the decoder generates the target sequence one token at a time, querying the encoder at each step. Self-attention enables a different design: a single stack of layers that can read a sequence, build increasingly rich representations, and either produce a classification or generate output. This is the architecture underlying BERT, GPT, and the entire modern transformer ecosystem.
The self-attention mechanism was introduced in the 2017 paper "Attention Is All You Need" by Vaswani et al. at Google. The paper demonstrated that a model built entirely from attention, with no recurrence or convolution, could outperform existing sequence-to-sequence architectures on machine translation benchmarks. The transformer architecture they described, built around self-attention, became the foundation for BERT (2018), GPT-2 (2019), T5 (2019), and nearly every major language model since. The title of the paper proved prophetic: attention, and specifically self-attention, did turn out to be all you need for language modeling at scale.