Positional encoding is the method of adding information about the absolute or relative order of tokens to the input embeddings of a transformer model. Since the core self-attention mechanism operates on sets of tokens without inherent sequence, these encodings are essential for the model to understand language structure, enabling tasks like next-word prediction and machine translation. The most common implementation, introduced in the original "Attention Is All You Need" paper, uses a fixed, pre-defined pattern of sine and cosine functions.
Glossary
Positional Encoding

What is Positional Encoding?
A foundational technique in transformer models that injects sequential order information into otherwise permutation-invariant input embeddings.
These sinusoidal encodings create a unique, continuous signal for each token position that the model can learn to interpret. Alternatives include learnable positional embeddings, where vectors for each position are trained parameters. This technique is a critical component of context window management, as it defines how order is represented within the model's fixed processing limit, directly impacting performance on tasks requiring precise sequential reasoning.
Key Features of Positional Encoding
Positional encoding is the critical method for injecting sequential order information into transformer models, which otherwise lack an inherent sense of token position due to their permutation-invariant self-attention mechanism.
Absolute vs. Relative Encoding
Absolute positional encoding assigns a unique, fixed vector to each position index (e.g., position 1, position 2). The original Transformer paper uses sinusoidal functions for this. Relative positional encoding represents distances between tokens (e.g., 'token A is 5 positions before token B'). This is often more effective for tasks where relative order matters more than absolute position, such as parsing or machine translation, and is implemented in models like T5 and Transformer-XL.
Sinusoidal Encoding Formula
The classic method uses a deterministic, non-learnable function of sine and cosine waves with geometrically increasing frequencies:
- For even dimensions:
PE(pos, 2i) = sin(pos / 10000^(2i/d_model)) - For odd dimensions:
PE(pos, 2i+1) = cos(pos / 10000^(2i/d_model))Whereposis the position,iis the dimension index, andd_modelis the embedding dimension. Key properties: It allows the model to easily attend to relative positions via linear transformations and can extrapolate to sequence lengths longer than those seen during training.
Learned Positional Embeddings
An alternative to sinusoidal functions where a learnable embedding matrix is used, similar to token embeddings. Each position index has a corresponding vector that is updated during training via backpropagation.
- Pros: Can potentially learn optimal position representations for a specific task or dataset.
- Cons: Is fixed to a maximum sequence length defined during training and cannot generalize to longer sequences without retraining or heuristic extension. Used in models like BERT and the original GPT.
Additive Combination with Token Embeddings
Positional information is added element-wise to the token's content embedding before being fed into the transformer layers: Input = TokenEmbedding(token) + PositionalEncoding(position). This preserves the dimensionality of the model while allowing the self-attention mechanism to disambiguate tokens based on both their semantic meaning and their position in the sequence. The model learns to attend to and interpret this combined signal throughout its layers.
Aliasing and Extrapolation
A core challenge is handling sequences longer than the model was trained on (length extrapolation). Sinusoidal encodings theoretically support this but models often struggle in practice due to attention pattern shifts. Learned embeddings fail completely beyond their trained length. Advanced methods like Rotary Position Embedding (RoPE) and Attention with Linear Biases (ALiBi) are explicitly designed for better extrapolation by enforcing a decay in attention scores with relative distance.
Modern Variants: RoPE and ALiBi
Rotary Position Embedding (RoPE): Applies a rotation matrix to token embeddings based on their absolute positions. This encodes relative position information directly in the attention score calculation. It's the dominant method in models like LLaMA and GPT-J due to its performance and extrapolation capabilities. Attention with Linear Biases (ALiBi): Adds a static, non-learned bias that penalizes the attention score between distant tokens. It requires no positional embeddings at all, trains faster, and demonstrates superior extrapolation to long contexts, as seen in models like MosaicML's MPT.
Positional Encoding: Sinusoidal vs. Learned
A comparison of the two primary methods for injecting positional information into transformer models, which lack inherent sequence order awareness.
| Feature | Sinusoidal Encoding | Learned (Absolute) Encoding | Learned (Relative) Encoding |
|---|---|---|---|
Core Mechanism | Predefined, deterministic mathematical function using sine and cosine waves | Embedding layer trained from random initialization | Trainable parameters that model pairwise token distances |
Generalization to Longer Sequences | Theoretically extrapolates to arbitrary lengths via function continuity | Cannot generalize beyond maximum sequence length seen during training | Can generalize to longer sequences if distance patterns are consistent |
Trainable Parameters | 0 (fixed, non-parametric) | N x d_model, where N is max training length | Typically O(d_model) or O(d_model^2) for relative bias tables |
Inductive Bias | Strong: encodes smooth, periodic positional relationships | None: positions are independent categories | Learns to prioritize local attention and specific distance relationships |
Common Use Cases | Original Transformer, BERT (early versions), models requiring strong length extrapolation | GPT family, BERT (later versions), T5 | Transformer-XL, DeBERTa, models optimized for long-context tasks |
Computational Overhead | Negligible (pre-computed or computed on-the-fly) | Small (lookup from embedding table) | Moderate (requires adding biases to attention scores) |
Information Content | Absolute position only | Absolute position only | Relative positional relationships between token pairs |
Integration with Attention | Added to token embeddings before the first layer | Added to token embeddings before the first layer | Integrated directly into the attention score calculation |
Positional Encoding in Major Models & Frameworks
Positional encoding is a critical component for injecting sequence order into transformer models. Different architectures and frameworks implement this concept in distinct ways to optimize for length, efficiency, or specific data types.
Original Transformer Sinusoidal Encoding
The seminal 2017 "Attention Is All You Need" paper introduced sinusoidal positional encodings. These are deterministic, pre-computed vectors added to token embeddings.
- Function: Uses sine and cosine waves of varying frequencies:
PE(pos, 2i) = sin(pos / 10000^(2i/d_model))andPE(pos, 2i+1) = cos(pos / 10000^(2i/d_model)). - Properties: Theoretically allows the model to attend to relative positions via linear transformations. It generalizes to sequence lengths longer than those seen during training.
- Limitation: Being fixed and non-trainable, it cannot adapt to the specific data distribution.
Learned Positional Embeddings (BERT, GPT-2)
Many early transformer models replaced sinusoidal functions with learned positional embeddings.
- Mechanism: A lookup table (embedding layer) where each absolute position index (0, 1, 2,...) has a corresponding trainable vector, just like word tokens.
- Advantage: Can theoretically learn optimal position representations for the task and dataset.
- Key Limitation: The model cannot extrapolate to sequence positions longer than the maximum seen during training (e.g., BERT's 512 tokens). This necessitates model retraining or clever workarounds for longer contexts.
Rotary Position Embedding (RoPE)
Rotary Position Embedding (RoPE) is the dominant method in modern LLMs like LLaMA, GPT-NeoX, and PaLM. It encodes absolute position with a rotation matrix that naturally incorporates relative position in the attention score.
- Core Idea: Instead of adding a positional vector, it rotates the query and key vectors using a rotation matrix defined by the position index.
- Key Benefit: The dot product between a query at position
mand a key at positionndepends only on the relative distancem-n. This provides strong relative positional bias while allowing for extrapolation to longer contexts than seen in training. - Efficiency: Integrates seamlessly into the attention computation without adding extra parameters.
ALiBi (Attention with Linear Biases)
ALiBi dispenses with adding embeddings altogether. It directly modifies the attention scores by adding a static, non-learned bias that penalizes attention between distant tokens.
- Mechanism: Adds a negative bias
-m * |i-j|to the attention score between queryiand keyj, wheremis a head-specific slope. - Advantages: Extremely efficient (no extra compute for positional embeddings). Demonstrates remarkable length extrapolation, allowing models trained on, e.g., 1024 tokens to perform well on 8000+ token sequences with no fine-tuning.
- Usage: Used in models like MPT from MosaicML (now Databricks).
Relative Positional Encodings (T5)
Relative positional encodings bias the attention mechanism based on the relative distance between tokens, not their absolute positions.
- T5's Implementation: Introduces a set of learned embeddings for each possible relative distance bucket (e.g., -32 to +32). During attention, a bias based on the relative distance
i-jis added to the score for each query-key pair. - Benefit: Inherently translation invariant—the model understands "the previous word" regardless of where in the sequence it occurs. Often leads to better generalization.
- Complexity: Slightly more computationally intensive than absolute methods, as the bias is computed per attention pair.
No Positional Encodings (Decoder-Only)
Some recent architectures, particularly in image and code generation, experiment with removing explicit positional encodings altogether.
- Premise: The model infers position from the data itself. In images, the arrangement of patches provides implicit structure. In code, indentation and syntax offer strong positional cues.
- Example: Models like ImageGPT and certain vision transformers (ViTs) have shown that with carefully designed attention patterns (e.g., axial attention), convolutional stem, or other inductive biases, explicit positional signals can be less critical.
- Trade-off: Removes a potential source of inductive bias, placing greater burden on the model's capacity to learn structure from data alone.
Frequently Asked Questions
Positional encoding is a foundational technique in transformer models that injects information about the order of tokens into the model's input, enabling it to understand sequence. This FAQ addresses its core mechanisms, variations, and practical implications.
Positional encoding is the method of injecting information about the absolute or relative order of tokens into a transformer model's input embeddings. It is necessary because the transformer's core self-attention mechanism is fundamentally permutation-invariant; it treats a sequence as an unordered set of tokens. Without positional information, the model cannot distinguish between "dog bites man" and "man bites dog," as the attention weights would be identical for both sequences. By adding a unique positional signal to each token's embedding, the model gains the ability to process language, code, or any other data with a sequential dependency.
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
Related Terms
Positional encoding is a foundational component of the transformer architecture. Understanding its related concepts is essential for designing efficient models and managing context.
Self-Attention Mechanism
The self-attention mechanism is the core operation in a transformer that computes a weighted sum of all tokens in a sequence, allowing each token to integrate information from every other token. It is inherently permutation-invariant, meaning it treats a sequence as an unordered set. Positional encoding is the critical add-on that injects order information, enabling the model to understand sequence structure like word order or time steps.
- Key Operation: Computes Query, Key, and Value vectors for each token.
- Without Position: The sentences 'The dog bit the man' and 'The man bit the dog' would be processed identically.
- With Position: Positional vectors allow the model to distinguish between these semantically opposite sequences.
Rotary Position Embedding (RoPE)
Rotary Position Embedding (RoPE) is an advanced positional encoding method that injects positional information by rotating the Query and Key vectors using a rotation matrix. This method provides relative positional information that decays with distance, is distance-aware, and is particularly effective for long sequences.
- Core Idea: Applies a rotation to token embeddings based on their absolute position, making the dot product between queries and keys dependent on their relative distance.
- Advantages: Offers better extrapolation to sequence lengths longer than those seen during training compared to fixed sinusoidal encodings. Used in models like LLaMA and GPT-NeoX.
- Mathematical Property: Ensures that the inner product of two vectors encodes their relative position difference.
Absolute vs. Relative Positioning
This distinction defines how a model understands token order.
- Absolute Positional Encoding (e.g., original sinusoidal, learned embeddings): Assigns a unique vector to each position index (e.g., position 1, position 2). The model knows a token's exact location but may struggle to generalize patterns based on relative distances.
- Relative Positional Encoding: Encodes the distance between tokens (e.g., token A is 5 positions before token B). This is often more flexible for tasks where relative order matters more than absolute position, such as parsing or coreference resolution.
Modern architectures like T5 and Transformer-XL use forms of relative positioning, which can improve performance on tasks requiring understanding of long-range dependencies.
Transformer Architecture
The transformer architecture is a neural network design based entirely on the self-attention mechanism, dispensing with recurrence and convolution. Positional encoding is a mandatory component of its encoder and decoder blocks to process sequential data.
- Key Components: Multi-Head Attention, Feed-Forward Networks, Layer Normalization, and Positional Encoding.
- Input Pipeline: Token Embeddings + Positional Encoding = Input to the first transformer layer.
- Impact: This architecture, introduced in the 'Attention Is All You Need' paper, enabled the scalable training of large language models (LLMs) by allowing full parallelization during training.
Permutation Invariance
Permutation invariance is a mathematical property of the core self-attention operation: shuffling the input tokens does not change the output of the attention calculation. This is because self-attention computes relationships between all pairs independently of order.
- The Problem: For language, video frames, or time-series data, order is semantically critical. 'Cat eats mouse' ≠'Mouse eats cat'.
- The Solution: Positional encoding breaks this invariance by making the model's computations sensitive to the original sequence order.
- Analogy: Without positional encoding, a transformer's view of a sentence is like a bag of words; with it, the model sees an ordered sequence.
Sinusoidal Encoding
Sinusoidal encoding is the fixed, non-learnable positional encoding scheme introduced in the original transformer paper. It uses a series of sine and cosine functions of different frequencies to create a unique positional vector for each token index.
- Formula: For each dimension i of the embedding,
PE(pos, 2i) = sin(pos / 10000^(2i/d_model))andPE(pos, 2i+1) = cos(pos / 10000^(2i/d_model)). - Properties: It is deterministic, smooth, and allows the model to attend to relative positions through linear transformations. It can theoretically extrapolate to sequence lengths not seen during training.
- Use Case: Serves as the canonical example of positional encoding and is still used in models like the original BERT and GPT architectures.

About the author
Prasad Kumkar
CEO & MD, Inference Systems
Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.
His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us