Prompts Forms Resume Reports ★ Saved ★ Saved★ + Submit a tool
Core concepts

Positional Encoding and Prefixes

About 4167 characters Updated Nov 30, 2026 21 views AI wiki

Positional encoding is a vector representation attached to each element of a sequence to express its order information; a prefix is a segment of context additionally inserted at the front of the sequence that is learnable or generated by fixed rules. Both serve the same goal: letting models that lack a natural sense of order know who comes first and who comes after, and where a piece of control information begins.

Core Principles

A fundamental property of the self-attention mechanism is insensitivity to input order: after shuffling a sequence, the attention output is merely rearranged accordingly, and the model itself cannot distinguish positions. Position information must therefore be injected explicitly. There are two mainstream approaches to positional encoding: one is sine-cosine encoding generated by fixed formulas, with different dimensions corresponding to waveforms of different frequencies, so that the encoding of any position can be expressed in terms of another through a linear transformation of relative displacement; the other is learnable positional embedding, assigning a trainable vector to each position, simple and flexible but limited by the maximum length.

The prefix idea moves this information forward: a segment of special vectors is placed at the beginning of the input sequence; they do not come from real data but are context slots the model uses to control behavior. A typical example is the visual prefix in vision-language models, where an image is encoded and inserted before the text; another is the prompt prefix in contrastive learning, using a small number of learnable vectors to replace handcrafted prompts, so that only these parameters need tuning to adapt to a new task. There is also the idea of prefixing with key-value caches, computing the internal representation of a shared context once and reusing that cache across subsequent requests, thereby saving repeated forward computation and significantly reducing inference cost in long-prompt scenarios.

Key Points

Sine encoding: sine for even dimensions, cosine for odd dimensions, with frequencies decreasing geometrically; lower dimensions change faster and higher dimensions more gently, so the encoding contains both high-frequency detail and low-frequency macro position. Its advantages are that it needs no training and can extrapolate to longer sequences, while its weakness is that its ability to express relative position is relatively fixed. In practice it is often added directly to word embeddings, or added only at the input layer without repeated injection in intermediate layers.

Positional embedding: the same dimension as word embeddings, participating in training. Extrapolation is poor; beyond the training length there is no corresponding vector, so it can usually only be truncated or interpolated.

Rotary positional encoding: rotating query and key vectors by position angles so that attention scores depend only on relative position, combining extrapolability and relativity, and it is a common choice in current large language models. There are also techniques for length extrapolation through linear scaling or frequency adjustment.

Relative position and bias: adding a learnable relative position bias to attention scores, such as the relative position bucketed bias of T5, suited to long texts and local dependencies.

Prefix tuning: fixing the backbone parameters and only concatenating several learnable vectors before the keys and values of each attention layer; the parameter count is far smaller than full-parameter fine-tuning, suiting scenarios where multiple tasks share a backbone and single tasks switch quickly. The cost is that these vectors occupy part of the attention capacity's quota and may affect normal tokens in long sequences.

Common Misconceptions

First, confusing positional encoding with attention masks. The former provides order information and the latter controls visibility; they are not interchangeable, and removing either degrades the model's ability to model order. Second, believing learnable positional embeddings can extrapolate directly to any length, when in fact interpolation or retraining is needed, and copying training-time position vectors to longer sequences yields meaningless results. Third, equating prefix tuning with prompt engineering; the former is continuous vectors optimized by gradients and the latter is discrete text, and their interpretability and tuning methods are completely different. Fourth, mistakenly believing longer prefixes are better, when in fact an overly long prefix dilutes the weight of real input in attention.

Application Scenarios

Machine translation, long document question answering, vision-language models, speech sequence modeling, code completion, multi-task fine-tuning adaptation, heterogeneous modality fusion, and context cache acceleration for large models.