Relative Position Encoding › Key Parameters
Language AI HandbookAll chapters Relative Position Encoding › Key ParametersOne section · Sections in this chapter13 / 16 ↓
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
When implementing relative position encoding, several parameters control the behavior and efficiency of the mechanism. Getting these right matters more for relative encoding than for absolute encoding, because the position embeddings are learned and their training dynamics depend on initialization and capacity choices.
The key parameters are:
-
max_distance(k): Maximum relative position to distinguish. Positions beyond this distance share the same "far away" embedding. Typical values range from 16 to 128. Smaller values reduce memory but lose fine-grained distance information for distant positions. Larger values preserve more detail but require more parameters and training data. The right value depends on the task: sentence-level tasks rarely need , while document-level tasks may benefit from . -
d_k: Dimension of query and key vectors, and key-side relative position embeddings. Must match between queries and keys for dot product compatibility. Common values are 64 for single-head attention orembed_dim / num_headsfor multi-head attention. Larger gives the position embeddings more capacity to encode complex distance patterns. -
d_v: Dimension of value vectors and value-side relative position embeddings. Determines the output dimension of each attention head. Often set equal tod_kfor simplicity, though they can differ. -
Embedding initialization scale: Relative position embeddings are typically initialized with small random values. Xavier/Glorot initialization scales by to maintain stable gradient magnitudes during training. If initialization is too large, position biases dominate the attention scores from the start and the model struggles to learn content-based patterns. If too small, position information takes many epochs to develop.
The total number of relative position parameters is . With and , this adds 16,512 parameters per attention head, which is modest compared to the QKV projection matrices (which have parameters). In a 12-head transformer with , the QKV projections total about 1.77M parameters per layer, while relative position embeddings add only about 200K. The overhead is real but small.