Positional Encoding

Positional encoding injects order or location into a model that otherwise treats inputs as a set. It is essential for Transformers because plain self-attention is permutation-equivariant.

A common absolute sinusoidal encoding is:

The input token state is then:

where is a token embedding and is the positional encoding.

Relative position bias modifies the attention logits directly:

ALiBi is a fixed relative bias variant. For causal attention, it adds a head-specific distance penalty:

Rotary position embeddings apply a position-dependent rotation to query and key vectors:

This makes attention depend on relative offsets through the rotated inner product.

Variants

  • Absolute learned positions attach one vector to each index.
  • Relative position bias modifies attention scores based on distance.
  • ALiBi adds a fixed head-specific linear distance bias to attention logits.
  • Rotary position embeddings rotate query/key features as a function of position.
  • Structural encodings can use graph distance, residue separation, 3D distance, or chain identity.

Position Contract

EncodingEncodesGood ForRisk
absolute indextoken position fixed-length sequencesweak length extrapolation
relative biasoffset local and long sequence patternsoffset bucket choices matter
ALiBilinear distance penaltytrain-short test-long language modelingfixed recency prior may not fit all tasks
rotaryrelative phase in query/key spacelong-context language modelsextrapolation depends on scaling
segment/chain idboundary or component identitypaired sequences, proteins, documentsmissing boundaries cause leakage
graph distanceshortest path or edge distancegraphs and moleculesgraph construction affects meaning
3D distance/framespatial relationstructures and geometrymust respect rotation/translation

The right encoding depends on the symmetry of the object. A set model, sequence model, graph model, and coordinate model should not use the same position story by default.

Sequence Length Extrapolation

If a model is trained up to length and evaluated at , the paper should explain:

Absolute learned embeddings may be undefined or extrapolated poorly. Relative or rotary methods often behave better, but still need empirical checks at the target context length.

ALiBi avoids a learned position lookup for unseen indices because the distance rule:

is defined for arbitrary distance . This helps length extrapolation, but dense attention still has the usual long-context compute and memory cost.

Structured Position

For protein, molecule, and graph inputs, position can mean several different things:

ObjectPosition Type
protein sequenceresidue index, chain id, MSA row/column
protein structureresidue index, 3D coordinate, local frame
molecule graphatom index, graph distance, bond type, conformer coordinate
protein-ligand complexreceptor frame, ligand frame, pair distance
document retrievalchunk order, source document, citation span

Do not treat arbitrary input order as meaningful unless the representation contract says so.

Checks

  • Does the task need order, distance, geometry, or only set membership?
  • Can the model extrapolate beyond the training context length?
  • Are padding, chain breaks, and multiple sequences handled correctly?
  • For protein or molecular structures, is sequence position enough, or is geometry also needed?
  • Are absolute coordinates being used where an invariant or equivariant representation is required?
  • Does the benchmark test longer contexts or different graph/structure sizes than training?