Positional Encoding advanced 7 min read 10 flashcards

Contextual Position Encoding

Every standard position scheme counts tokens, which is why a model cannot reliably attend to "the previous sentence"; CoPE makes the position counter itself a function of content, incrementing only on tokens the model decides matter.

Ask a transformer for the third-to-last word and it can do it. Ask for the third-to-last sentence and it struggles, because the only ruler it owns measures tokens. A sentence is 8 tokens here and 40 there, so "three sentences back" corresponds to no fixed token offset, and a position scheme built on token counts cannot express it. The same gap explains why counting tasks, selective copying, and the Flip-Flop language-modelling task defeat models that handle far harder-looking problems.

A learned, fractional counter

CoPE keeps RoPE's premise that position modulates attention but replaces the counter (Golovneva et al., 2024, Contextual Position Encoding, arXiv:2405.18719). For query \(i\) and each earlier key \(j\), a gate decides whether \(j\) should be counted at all:

\[g_{ij} = \sigma(\mathbf{q}_i \cdot \mathbf{k}_j)\]

The position of \(j\) relative to \(i\) is then the running sum of gates between them:

\[p_{ij} = \sum_{k=j}^{i} g_{ik}\]

If the gate fires only on full stops, \(p_{ij}\) counts sentences. If it fires on every token, CoPE reduces to ordinary relative position. Crucially the gate is query-dependent, so different heads in the same layer can measure in different units simultaneously.

Because the sum is over sigmoids, \(p_{ij}\) is a real number rather than an integer, and there is no embedding row to look up. CoPE interpolates between learned embeddings at the two adjacent integer positions, which keeps the whole path differentiable.

What it buys

The paper reports that CoPE solves selective copy, counting, and Flip-Flop, tasks on which standard position embeddings fail outright, and improves perplexity on language modelling and code. Code is the intuitive win: nesting depth, block boundaries, and argument index are all content-defined units, and a token-counting ruler measures none of them.

The broader point is a reframing. Absolute, relative, rotary, and ALiBi differ in how they encode a position, but agree that position means token index. CoPE is the first widely-discussed scheme to make position an addressable, learned quantity, which is why it belongs in the same conversation as retrieval rather than only in the same conversation as RoPE.

When it breaks

The cost model is the problem. Standard RoPE applies a rotation per token, \(O(n)\) work outside the attention matrix. CoPE's gate is a function of every query-key pair, so computing \(p_{ij}\) is \(O(n^2)\) and, worse, produces an \(n \times n\) tensor of distinct positions rather than one vector of angles. That is architecturally hostile to FlashAttention, whose entire advantage comes from never materialising the score matrix. As of mid-2026 no frontier model has shipped CoPE, and the kernel story is the reason.

Interpolating between embeddings adds a second cost. The method needs a table of position embeddings up to some maximum count, which reintroduces the bounded-range problem RoPE was adopted to escape, and the interpolation weights depend on gate values that shift during training.

The published evidence is small-scale. Gains are reported on algorithmic tasks and modest language-modelling setups, not on a frontier pretraining run, and the tasks CoPE solves are ones designed to expose exactly its motivating weakness. That is legitimate science and weak evidence for deployment; whether contextual position survives contact with a 100B-token budget is genuinely unknown.

Treat CoPE as the clearest available statement of what token-counting position encodings cannot do, rather than as a drop-in replacement. The diagnosis has held up better than the cure.

Check yourself

10 flashcards for this concept

Click a card to reveal the answer.

Drill the whole track