ALiBi (Attention with Linear Biases) is a positional encoding technique for transformer models that replaces learned or sinusoidal position embeddings with a fixed, linearly decreasing bias penalty added to attention scores based on token distance. This simple mechanism allows the model to naturally attend more to nearby tokens and less to distant ones, enabling it to generalize to sequence lengths longer than those seen during training without any modification or fine-tuning. It eliminates the need for a positional embedding matrix, reducing parameters and memory usage.
Glossary
ALiBi (Attention with Linear Biases)

What is ALiBi (Attention with Linear Biases)?
A positional encoding method for transformer models that enables efficient extrapolation to longer sequences.
The method works by subtracting a sloped linear penalty from the query-key attention scores before the softmax, where the penalty magnitude is proportional to the distance between tokens. This inductive bias for locality is computationally trivial and is applied during the attention calculation itself. ALiBi is a key innovation in efficient transformer architectures, particularly for training models that must later handle extended contexts, as it provides robust length extrapolation capabilities critical for long-context inference on edge hardware.
Key Features of ALiBi
ALiBi (Attention with Linear Biases) is a method for enabling Transformer models to handle sequences longer than those seen during training by adding a fixed, non-learned bias to attention scores.
Linear Distance-Based Penalty
ALiBi works by adding a fixed, non-learned bias to the attention scores computed between query and key vectors. This bias is a negative linear penalty proportional to the distance between the tokens. For a query at position i and a key at position j (where j is in the past), the bias is -m * (i - j), where m is a head-specific slope. This simple mechanism inherently teaches the model to prioritize nearby tokens and discount distant ones, without requiring the model to learn this behavior from data.
Extrapolation to Longer Sequences
The primary advantage of ALiBi is its ability to generalize to longer sequence lengths at inference time than those used during training. Because the bias is defined as a linear function of relative distance, it can be applied to any pair of token positions, even if that specific distance was never observed during training. Models trained with ALiBi on, for example, 2048-token contexts can often perform immediately on 4096-token or longer sequences without any fine-tuning, a capability where traditional sinusoidal or learned positional embeddings typically fail.
No Trainable Position Parameters
Unlike sinusoidal embeddings or learned positional embeddings, ALiBi introduces zero additional trainable parameters for position information. The bias slopes m are predefined constants, typically set using a geometric progression (e.g., 1/2^1, 1/2^2, ...) across attention heads. This eliminates the need for the model to allocate capacity to learning positional relationships, potentially freeing parameters for learning linguistic content, and simplifies the model architecture.
Implementation and Computational Cost
Implementing ALiBi is computationally trivial and memory-efficient. The bias matrix is precomputed once and added to the (QK^T) attention score matrix before the softmax operation. This adds negligible overhead compared to standard attention. Its simplicity makes it a drop-in replacement for other positional encoding schemes in Transformer decoders, requiring minimal code changes while offering significant benefits for sequence length generalization.
Comparison to Rotary Positional Embedding (RoPE)
ALiBi and Rotary Positional Embedding (RoPE) are two leading methods for enabling length extrapolation.
- ALiBi adds a fixed penalty; it is non-learned and explicitly discourages long-range attention.
- RoPE rotates query and key vectors with a frequency-based transformation, injecting relative position information into the embeddings themselves. Empirically, ALiBi often demonstrates stronger out-of-the-box extrapolation capabilities, while RoPE can provide slightly better performance on in-distribution lengths. The choice depends on the target use case and need for robust long-context handling.
Use in Small Language Models (SLMs)
ALiBi is particularly valuable in the context of Small Language Model (SLM) engineering and efficient model architectures. For models destined for edge deployment with constrained memory, the ability to process longer contexts without retraining is a major advantage. It allows a compact model trained on manageable sequence lengths to be deployed for tasks requiring longer reasoning or document analysis, enhancing its utility without increasing its parameter count or computational footprint during inference.
ALiBi vs. Other Positional Encoding Methods
A technical comparison of ALiBi against other prominent methods for injecting positional information into transformer models, focusing on architectural properties, computational characteristics, and extrapolation behavior.
| Feature / Metric | ALiBi (Attention with Linear Biases) | Absolute Sinusoidal | Learned Absolute Embeddings | Rotary Positional Embedding (RoPE) |
|---|---|---|---|---|
Core Mechanism | Adds a fixed, linear bias penalty to attention scores based on token distance. | Adds a fixed sinusoidal signal to token embeddings based on absolute position. | Learns a lookup table of embedding vectors for each absolute position. | Applies a rotation matrix to query and key vectors based on absolute position. |
Position Representation | Relative (implicitly via bias) | Absolute | Absolute | Relative (via absolute encoding) |
Parameter Overhead | Zero learned parameters | Zero learned parameters | N × d_model parameters (N=max seq length) | Zero learned parameters |
Extrapolation to Longer Sequences | ||||
Inference-Time Computational Overhead | < 1% | < 1% | < 1% | ~2-5% |
Training Stability | No special initialization required | Stable by design | Requires careful initialization | Stable by design |
Attention Score Formulation | score = q·k + (m * -|i-j|) where m is a head-specific slope | score = (q + p_i)·(k + p_j) | score = (q + p_i)·(k + p_j) | score = (R_i q)·(R_j k) |
Key Architectural Benefit | Enables zero-shot generalization to sequences longer than those seen in training. | Simple, deterministic, and introduced in the original transformer. | Flexible; can theoretically learn any positional mapping. | Encodes relative position information naturally in the attention dot product. |
Models and Frameworks Using ALiBi
ALiBi's ability to enable length extrapolation has made it a key positional encoding method in several notable open-source models and research frameworks, particularly those focused on long-context efficiency.
Palmyra (Writer.ai Models)
Palmyra models, developed by Writer.ai for enterprise-grade language tasks, utilize ALiBi to power their long-context capabilities. This is particularly valuable for business applications involving lengthy documents, such as legal contract review, long-form report generation, and multi-document synthesis, where the model must maintain coherence and reference information across thousands of tokens.
xGen (Salesforce)
Salesforce Research's xGen models, designed for long-context tasks, employ ALiBi. Their research highlighted ALiBi's effectiveness in enabling 8k-token context windows from models trained on shorter sequences. This work demonstrated practical applications in long-document question answering and summarization, showcasing the method's production viability for enterprise use cases requiring extensive context.
Jais (MBZUAI & Core42)
Jais, a large Arabic-English bilingual language model, incorporates ALiBi. This choice supports the model's design goals for robust performance on Arabic linguistic structures and long-context understanding, which is essential for processing lengthy Arabic text common in legal, religious, and historical documents.
Research & Custom Implementation
Beyond specific models, ALiBi is a standard option in many transformer libraries and research codebases for exploring long-context efficiency.
- Hugging Face Transformers: Can be implemented via custom attention masks.
- FlashAttention-2: Includes support for ALiBi biases, allowing its efficiency gains to be combined with ALiBi's extrapolation benefits.
- Custom Architectures: Often the go-to choice for research prototypes requiring variable sequence lengths without retraining, due to its simple, additive bias mechanism.
Frequently Asked Questions About ALiBi
ALiBi (Attention with Linear Biases) is a key innovation in transformer architecture that enables models to handle sequences longer than those seen during training. This FAQ addresses its core mechanisms, advantages, and practical applications in efficient model design.
ALiBi (Attention with Linear Biases) is a positional encoding method for transformer models that replaces traditional additive embeddings with a fixed, linearly decreasing bias penalty applied to attention scores based on token distance. It works by adding a negative bias to the query-key dot product during the attention calculation, where the bias magnitude is proportional to the distance between the tokens, penalizing attention between distant tokens without requiring the model to learn positional embeddings from data.
The mechanism is defined by the formula added to the standard attention score: score = q * k^T + m * (i - j), where i and j are token positions, and m is a head-specific, fixed negative slope (e.g., m = 2^(-8/head)). This simple, non-learned approach provides the model with a strong inductive bias about relative positioning, enabling it to generalize to sequence lengths not encountered during training.
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 in Efficient Model Architectures
ALiBi is part of a broader toolkit for making transformer models more efficient and capable of handling long sequences. These related techniques address the core challenges of positional awareness and computational cost.
Rotary Positional Embedding (RoPE)
A positional encoding technique that injects absolute positional information by rotating query and key vectors with a frequency-based transformation. Unlike ALiBi's fixed bias, RoPE encodes position via multiplicative rotations in the vector space.
- Key Mechanism: Applies a rotation matrix to queries and keys based on their absolute positions, which inherently encodes relative position information in the attention scores.
- Advantages: Often yields stronger performance on standard benchmarks compared to learned embeddings and demonstrates good extrapolation to longer sequences, though typically not as robust as ALiBi for extreme lengths.
- Use Case: Foundational to models like LLaMA, GPT-NeoX, and PaLM.
Multi-Query Attention (MQA)
An attention mechanism designed for inference efficiency where all query heads share a single key head and a single value head. This drastically reduces the size of the key-value (KV) cache in autoregressive decoding.
- Core Benefit: Reduces memory bandwidth pressure during generation, a critical bottleneck for long sequences and high-throughput serving.
- Trade-off: Can lead to a slight degradation in model quality compared to standard Multi-Head Attention (MHA).
- Evolution: Often serves as a baseline for more refined mechanisms like Grouped-Query Attention (GQA), which groups queries to share KV heads, offering a better quality/efficiency balance.
FlashAttention
An I/O-aware, exact attention algorithm that recomputes attention scores on-the-fly within fast SRAM to avoid reading/writing the large attention matrix to slow high-bandwidth memory (HBM).
- Solves the Memory Bottleneck: Standard attention is memory-bound, limited by HBM bandwidth. FlashAttention makes it compute-bound, leveraging faster SRAM.
- Impact: Delivers 2-4x speedup in training and enables training transformers on sequences up to 16K tokens long. It is orthogonal to ALiBi and can be combined with it for maximum efficiency.
- Foundation: A core enabling technology for modern long-context LLMs, reducing both wall-clock time and hardware cost.
Sliding Window Attention
A sparse attention pattern where each token only attends to a fixed-size window of preceding tokens (e.g., the last 4096 tokens). This reduces attention complexity from O(n²) to O(n * w), where w is the window size.
- Efficiency vs. Context: Provides linear scaling for very long sequences but limits the model's theoretical effective context window. Useful for tasks with strong local dependencies.
- Combination with ALiBi: Often used together with ALiBi. The sliding window provides computational efficiency, while ALiBi's biases allow the model to meaningfully attend within that window and generalize to different window sizes at test time.
- Example: Used in models like Longformer and Mistral 7B.
Linear Attention
A class of attention mechanisms that reformulate the standard dot-product attention to achieve linear time and memory complexity O(n) with respect to sequence length.
- Core Idea: Uses kernel feature maps to decompose the attention computation, leveraging the associative property of matrix multiplication. Common approaches include random feature maps or explicit kernel approximations.
- Trade-offs: While computationally efficient, early linear attention methods often struggled to match the performance of full softmax attention on challenging language modeling tasks.
- Modern Variants: Newer approaches like FlashAttention-2 with its online softmax and recent state-space models (e.g., Mamba) offer compelling linear-time alternatives.
Relative Positional Encoding (T5 Bias)
A learnable alternative to ALiBi where the model learns a relative position bias matrix that is added to attention scores. Popularized by the T5 model.
- Mechanism: A trainable scalar bias
b_{i,j}for each possible relative distance(i-j)is added to the attention score between token i and token j. - Comparison to ALiBi: While more flexible, it requires learning parameters for a maximum clip distance and does not naturally extrapolate to unseen sequence lengths. ALiBi's fixed, linear bias is a deliberate simplification that enables its strong length generalization.
- Use Case: Effective when training and inference sequence lengths are fixed and known in advance.

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