Self-attention, or auto-attention, is an operation in which elements of a sequence query other elements of the same sequence to compute a contextual representation. In text, each token can consider other tokens present in the input, not just the one immediately next to it.

The technique is central to Transformers, the architecture used in many language models. “Self” indicates that the source of information consulted belongs to the sequence itself. This differentiates it from forms of cross-attention, in which one sequence can query another, such as text querying image representations.

How it works

Each position is transformed into query, key and value. The query of a token is compared to the keys of the other tokens. Compatibility scores are normalized to form weights, and those weights combine the corresponding values. The result is a representation that incorporates information from other positions.

For example, when processing an ambiguous word, the model can use nearby nouns and verbs to form a contextual representation. In many architectures, there are multiple attention heads, which calculate relationships in different spaces. The system also uses additional layers, normalizations and neural network operations to produce its final representations.

The attention mask defines which positions can be queried. In causal generation, a token normally should not use future tokens that have not yet been generated. In other tasks, the model may have access to the entire sequence. The exact behavior depends on the architecture and the configuration.

Practical example

Consider “The key was inside the box, but it was locked.” To interpret “it”, the model can relate the pronoun to “box” and to the grammatical signals in the sentence. Self-attention provides a way to combine these elements, even if they are not immediate neighbors.

In code, a variable can appear far from its declaration. Self-attention allows considering distributed occurrences in the context. Still, the model can misinterpret scope, ignore part of the file or suggest an invalid change; tests remain necessary.

What it is for

Self-attention allows modeling dependencies between positions and forming representations conditioned on context. It is applied to text tasks and also to systems that process image, audio or combinations of modalities.

For those who use models, the concept helps understand how context and sequence participate in generation. It also explains why more context does not automatically mean a better answer: the model needs to select and combine the relevant elements.

Self-attention is a calculation, not a human capacity for paying attention. Its weights are not, by themselves, a complete justification for the answer. The result depends on the whole model, the data and the task being executed.