long context llm

**Long Context LLMs and Context Window Extension** is the **set of techniques that enable language models to process sequences far exceeding their original training context length** — from the early 2K-4K token limits of GPT-3 to the 128K-2M token windows of modern models like GPT-4 Turbo, Claude, and Gemini, using methods such as RoPE frequency scaling, YaRN, ring attention, and positional interpolation to extend context without full retraining, while addressing the fundamental challenges of attention cost, positional encoding generalization, and the lost-in-the-middle phenomenon. **Context Length Evolution** | Model | Year | Context Length | Method | |-------|------|---------------|--------| | GPT-3 | 2020 | 2,048 | Absolute positions | | GPT-3.5 Turbo | 2023 | 16K | ALiBi | | GPT-4 | 2023 | 8K / 32K | Unknown | | GPT-4 Turbo | 2024 | 128K | Unknown | | Claude 3.5 | 2024 | 200K | Unknown | | Gemini 1.5 Pro | 2024 | 1M-2M | Ring attention variant | | Llama 3.1 | 2024 | 128K | RoPE scaling + continued pretraining | **Why Long Context Is Hard** ``` Problem 1: Attention is O(N²) 128K tokens → 16B attention entries per layer → 64GB per layer Solution: FlashAttention, ring attention, sparse attention Problem 2: Positional encoding doesn't generalize Trained on 4K → positions 4001+ are out-of-distribution Solution: RoPE scaling, YaRN, positional interpolation Problem 3: Lost in the middle Model attends to beginning and end, ignores middle content Solution: Better training with long documents, positional adjustments ``` **RoPE Scaling Methods** | Method | How It Works | Extension Factor | Quality | |--------|-------------|-----------------|--------| | Linear interpolation | Scale frequencies by training/target ratio | 4-8× | Good | | NTK-aware scaling | Scale high frequencies less than low | 4-16× | Better | | YaRN | NTK + attention scaling + temperature | 16-64× | Best open method | | Dynamic NTK | Adjust scaling based on actual sequence length | Adaptive | Good | | ABF (Llama 3) | Adjust base frequency of RoPE | 8-32× | Strong | **RoPE Positional Interpolation** ``` Original RoPE (trained for 4K): Position 0 → θ₀, Position 4096 → θ₄₀₉₆ Positions beyond 4096: unseen during training → garbage Linear interpolation (extend to 32K): Map [0, 32768] → [0, 4096] New position embedding = RoPE(position × 4096/32768) All positions now within trained range Trade-off: Nearby positions become harder to distinguish YaRN improvement: Different scaling per frequency dimension Low frequencies: Full interpolation (they capture long-range) High frequencies: No scaling (they capture local detail) + Attention temperature correction ``` **Ring Attention** ``` Problem: Single GPU can't hold attention for 1M tokens Ring Attention: - Distribute sequence across N GPUs (each holds L/N tokens) - Each GPU computes local attention block - Rotate KV blocks around the ring of GPUs - After N rotations, each GPU has attended to all tokens - Memory per GPU: O(L/N) instead of O(L) ``` **Lost-in-the-Middle Problem** - Studies show models retrieve information best from beginning and end of context. - Middle of long contexts: 10-30% accuracy drop on retrieval tasks. - Causes: Attention patterns shaped by training data distribution, positional biases. - Mitigations: Long-context fine-tuning with retrieval tasks throughout the document, attention sinks at beginning. **Needle-in-a-Haystack Evaluation** - Insert a specific fact at various positions in a long document. - Ask the model to retrieve the fact. - Measures: Retrieval accuracy as a function of context position and total length. - State-of-the-art models (GPT-4 Turbo, Claude 3): >95% across all positions at 128K. Long context LLMs are **enabling entirely new AI applications** — from processing entire codebases in a single prompt to analyzing full books, legal documents, and multi-hour recordings, context window extension transforms LLMs from short-message responders into comprehensive document understanding systems, while the ongoing research into efficient attention and positional encoding continues to push context boundaries toward millions of tokens.

Go deeper with CFSGPT

Get AI-powered deep-dives, save terms, and run advanced simulations — free account.

Create Free Account