Long-context architectures
6.1.10· AI-ML › Scaling & Efficient Architectures
Modern AI systems ko increasingly long sequences process karni padti hain—poore documents, code repositories, ya conversation histories. Traditional Transformers ko sequence length mein quadratic complexity face karni padti hai, jo unhe ~2k-8k tokens se aage ke contexts ke liye impractical bana deti hai. Long-context architectures is problem ko attention mechanisms aur memory structures ko redesign karke solve karti hain, taaki 100k+ token contexts efficiently handle ho sakein.
Why Long Context Matters
Standard Transformer attention har token pair ke beech similarity compute karta hai:
tokens ke liye, is mein memory aur compute lagti hai. 100k tokens par, yeh 10 billion comparisons per layer ho jaate hain.
Architectural Approaches
1. Sparse Attention Patterns
Longformer ka Sliding Window Derive Karna:
Token ke liye full attention cost se shuru karo:
Size ki local window tak restrict karo:
tokens ke liye:
Agar constant hai (jaise, 512), complexity ban jaati hai:
Lekin local-only attention global context kho deta hai. Longformer ka solution: global attention tokens add karo jo sab kuch attend karte hain:
- Zyaadatar tokens: local window ( comparisons)
- special tokens: full attention ( comparisons each)
- Total:
Agar hai, toh yeh hai with global information preserved.
Yeh step kyun? Global tokens (jaise [CLS]) document-level information aggregate karte hain, jabki local windows nearby dependencies capture karti hain. Yeh thodi si full attention ko mostly-local attention ke saath trade karta hai.
2. Linear Attention Mechanisms
Softmax expand karo:
Iske liye saare dot products compute karne padte hain → .
Linear attention ko explicit feature maps se approximate karta hai:
Tab:
Key insight: Sum ko associativity se rearrange karo:
Ab hum compute karte hain (maano = feature map dimension, = value dimension):
- ek baar saare tokens ke liye →
- ek baar →
- Har query ke liye: numerator aur denominator → per query, yaani se independent
queries mein total: — sequence length mein linear!
Yeh step kyun? Key-value aggregation aur normalizer precompute karke, har query sirf in fixed-size summaries ko touch karta hai. Per-query cost par depend karti hai, par nahi. Feature map ko quality maintain karne ke liye softmax kernel ko itna achha approximate karna chahiye.
Yeh step kyun? Keys par linear scan globally ek baar hota hai ( aur build karte waqt), har query ke liye baar baar nahi. Phir har query un summaries ko constant time mein reuse karta hai w.r.t. , aur se division softmax-style normalization enforce karta hai.
3. State Space Models (SSMs)
Tokens ke liye Discretization: Continuous time ko discrete steps (tokens) se replace karo:
jahan aur step size ke liye.
SSMs long context ke liye kyun?
- State ki dimension fixed hai (jaise, 256), sequence length ke saath nahi badhti
- Update per token hai jahan state dimension hai
- Total: with → effectively
ko diagonal plus low-rank choose karo:
Yeh fast matrix exponential allow karta hai:
Diagonal ke saath, exponential hai:
Low-rank correction Woodbury identity ke zariye efficient updates allow karta hai.
Recurrent form:
Convolutional form (training parallelization ke liye):
jahan ek learnable convolution kernel hai.
Yeh step kyun? State space formulation poori history ko ek fixed vector mein compress karta hai, RNN hidden states ki tarah lekin better training stability ke saath. Structured matrices ise computationally feasible banate hain.
4. Memory-Augmented Architectures
Memorizing Transformers architecture:
- Local attention: Recent tokens ko attend karo (sliding window)
- Memory attention: External memory se nearest neighbors ko attend karo
- Memory update: Current layer ke key-value pairs memory bank mein store karo
Memory lookup approximate nearest neighbor (ANN) search use karta hai:
FAISS ya similar use karke, yeh hai jahan memory size hai.
Total complexity: ≈ jab
Yeh step kyun? Saare past tokens equally relevant nahi hote. Retrieval computation ko wahan focus karti hai jahan current token ke liye matter karta hai, human selective attention ko mimic karte hue.
Comparison Table
| Architecture | Complexity | Context Length | Training | Key Tradeoff |
|---|---|---|---|---|
| Sparse Attention (Longformer) | 16k-32k | Moderate | Local bias, global tokens zaroori | |
| Linear Attention (Performer) | 100k+ | Fast | Approximation quality | |
| State Space (S4) | 1M+ | Fast (convolution) | Fixed state capacity | |
| Memory-Augmented | Unlimited | Slow (retrieval) | Memory management overhead |
Kyun galat hai: Feature map softmax kernel ko approximate karta hai. Tasks ke liye jinhein precise token interactions chahiye (jaise, exact words copy karna, arithmetic), approximation quality degrade kar deti hai. Empirically, linear attention often standard attention se underperform karta hai un tasks par jo exact attention patterns maangti hain.
Fix: Linear attention encoder layers ke liye use karo jo long context process karti hain, lekin decoder layers ke liye standard attention rakho jahan output generate karte waqt precision matter karti hai. Ya hybrid approaches use karo (local + linear).
Kyun galat hai: Transformers mein multiple layers hoti hain. Information network ke through propagate hoti hai. size ki sliding window ke saath, receptive field har layer mein ~ tokens badhta hai, toh ek token roughly layers ke baad distant context tak pahunch sakta hai—full attention se kaafi sasta.
Example: Position 1000 par token, window ke saath (har taraf ±50 attend karta hai)
- Layer 1: receptive field ≈ 100 tokens (950–1050)
- Layer 2: un mein se har ek ne ±50 dekhe → field ≈ 200 tokens (900–1100)
- Layer : receptive field ≈ tokens
- Pure 1000-token span cover karne ke liye tumhe layers chahiye, nahi 4–5.
Fix: Itni depth do ( layers) ki information propagate ho sake, ya global tokens / dilated windows add karo taaki kam layers mein long-range access shortcut ho.
Key Formulas Summary
Recall Baarah Saal ke Bachche ko Samjhao
Socho tum ek bahut badi kitaab padh rahe ho jisme 100,000 words hain. Tumhara dimaag har ek word yaad nahi rakh sakta jo tumne padha, hai na?
Full attention matlab hai ki har naye word ke liye tum poori kitaab mein waapas jaake check karte ho ki yeh word har pichle word se kaise related hai. Yeh bahut thaka dene wala hai! Agar kitaab 1,000 pages ki hai, toh tum ek million page flips karoge.
Sparse attention kehta hai: "Sirf pichle kuch pages dekho (local), plus table of contents aur chapter summaries (global tokens)." Bahut faster! Tum gist phir bhi samajh jaate ho bina sab kuch dobara padhe. (Ek catch: bahut door ke pages ko sirf local jhaankon se connect karne ke liye, tumhe kitaab ke kaafi "passes" chahiye.)
Linear attention matlab hai ki padhte waqt ek cheat sheet banana—tum key points likhte ho jo automatically update hoti hain. Jab tum naya word dekhte ho, poori kitaab ki jagah sirf apna ek-page cheat sheet check karte ho. Cheat sheet check karna utna hi chhota time leta hai chahe kitaab kitni bhi lambi ho.
Memory banks matlab hai 100 sabse important sentences highlight karna, phir jab kuch yaad karna ho sirf un highlights ko dekhna.
Har method sab kuch dobara padhne se bachne ka ek smart shortcut dhundhta hai, taaki tum bahut, bahut lambi kitaabein (ya AI mein, bahut lambi conversations aur documents) handle kar sako.
Connections
- Transformer Architecture - woh foundation jo optimize ho rahi hai
- Attention Mechanisms - core operation jo re-engineer ho rahi hai
- Computational Complexity - bottleneck samajhna
- Retrieval-Augmented Generation - related memory approach
- Recurrent Neural Networks - SSMs recurrent ideas ko better scaling ke saath revive karte hain
- Kernel Methods - linear attention kernel approximation use karta hai
- Information Bottleneck - state space models fixed capacity se compress karte hain
#flashcards/ai-ml
Standard Transformer attention mein O(n²) complexity kyun hoti hai? :: Kyunki yeh har token pair ke beech similarity (QKᵀ) compute karta hai — n queries mein se har ek n keys se compare hoti hai.
Longformer jaise sparse attention patterns ka key idea kya hai?
Linear attention O(n) complexity kaise achieve karta hai?
Sparse local attention mein full sequence cover karne ke liye kitni layers chahiye?
Attention aur state space models mein fundamental difference kya hai?
Memory-augmented architectures ka tradeoff kya hai? :: Yeh external memory se retrieve karke unlimited context achieve karte hain, lekin retrieval overhead (O(log M) per query) add hoti hai aur kya store karna hai yeh manage karna padta hai.