Self-attention বিস্তারিত
এই পাঠে যা শিখবেন
- Cross-attention vs self-attention — পার্থক্য
- Q, K, V intuition — database analogy
- Scaled dot-product — কেন $\sqrt{d}$ দিয়ে scale
- Causal mask — autoregressive generation-এ
- PyTorch দিয়ে scratch থেকে self-attention
- Self-attention complexity ও property
১ · Self-attention vs cross-attention
L30-এ যে attention দেখলাম — encoder ও decoder-এর মধ্যে। Decoder-এর state $s_t$, encoder-এর hidden states $h_i$ — দু'টি আলাদা source থেকে। একে বলে cross-attention।
Self-attention-এ — sequence-এর প্রতিটি token নিজের সব sequence-এ attend করে। Source ও target same — "self"।
- "আমি ঢাকায় থাকি" — "ঢাকায়" token তার নিজের context (আমি, ঢাকায়, থাকি) দেখে।
- "থাকি"-র নিজের meaning বুঝতে আগের word "ঢাকায়" দরকার (location of staying)।
- প্রতিটি token-এর representation enrich হয় full context থেকে।
একই sequence থেকে — প্রতিটি token query হয়, প্রতিটি token key ও value হয়। Token i token j-কে query করে — relevance বুঝে — j-এর value নেয়।
২ · Query, Key, Value — database analogy
ভাবুন একটি library। আপনি একটি বই খুঁজছেন — "Bangladesh history" (query)। প্রতিটি বইয়ের একটি title (key), একটি content (value)। Query ও key match করে বই খুঁজি; relevant বই-এর content নিই।
- Query ($q$): "এই token কী খুঁজছে"। Question।
- Key ($k$): "এই token কী offer করে"। Index/title।
- Value ($v$): "এই token-এর actual information"। Content।
Linear projection দিয়ে input থেকে $Q, K, V$ obtain:
$$Q = X W_Q, \quad K = X W_K, \quad V = X W_V$$
যেখানে $X \in \mathbb{R}^{T \times d}$ — input embedding sequence। $W_Q, W_K, W_V \in \mathbb{R}^{d \times d_k}$ — learned projections।
৩ · Scaled dot-product attention
$$\text{Attention}(Q, K, V) = \text{softmax}\left( \frac{QK^\top}{\sqrt{d_k}} \right) V$$
Step by step:
- $QK^\top$: shape $(T \times T)$ — pairwise score (token i কতটা attends token j)।
- $/\sqrt{d_k}$: scaling — gradient stability।
- softmax (row-wise): probability distribution।
- $\cdot V$: weighted sum of value — output shape $(T \times d_v)$।
Output — প্রতিটি token-এর "context-enriched" representation। শব্দটির আগের meaning + relevant context থেকে info।
৪ · কেন $\sqrt{d_k}$ দিয়ে scale
$Q, K$-এর dimension $d_k$ বড় হলে — $QK^\top$-এর dot products variance বাড়ে। Softmax saturate হয় — gradient near-zero, training slow। $\sqrt{d_k}$ scale — variance ১-এ থাকে।
Mathematically — $q, k$ independent zero-mean unit-variance হলে — $q \cdot k$-এর variance $d_k$। $\sqrt{d_k}$ দিয়ে ভাগে — variance ১। Softmax stable।
৫ · PyTorch implementation — scratch থেকে
import torch
import torch.nn as nn
import torch.nn.functional as F
import math
class SelfAttention(nn.Module):
def __init__(self, d_model, d_k=None):
super().__init__()
self.d_k = d_k or d_model
self.W_Q = nn.Linear(d_model, self.d_k, bias=False)
self.W_K = nn.Linear(d_model, self.d_k, bias=False)
self.W_V = nn.Linear(d_model, self.d_k, bias=False)
def forward(self, x, mask=None):
# x: (B, T, d_model)
Q = self.W_Q(x) # (B, T, d_k)
K = self.W_K(x)
V = self.W_V(x)
# Scaled dot-product
scores = Q @ K.transpose(-2, -1) / math.sqrt(self.d_k)
# scores: (B, T, T)
if mask is not None:
scores = scores.masked_fill(mask == 0, -1e9)
alpha = F.softmax(scores, dim=-1)
out = alpha @ V # (B, T, d_k)
return out, alpha
attn = SelfAttention(d_model=64)
x = torch.randn(2, 10, 64)
out, a = attn(x)
print(out.shape, a.shape) # (2, 10, 64), (2, 10, 10)
print(a.sum(-1)[0, 0]) # ~1.0
৬ · Causal mask — autoregressive generation
Language model-এ — token $t$ predict-এ শুধু $\le t$ token দেখা legal (future leak ভুল)। Causal mask — upper-triangular matrix।
def causal_mask(T):
"""Lower triangular mask — token t can see ≤ t"""
mask = torch.tril(torch.ones(T, T))
return mask # (T, T) with 1 on/below diagonal
# Usage in self-attention
T = 5
mask = causal_mask(T)
print(mask)
# tensor([[1, 0, 0, 0, 0],
# [1, 1, 0, 0, 0],
# [1, 1, 1, 0, 0],
# [1, 1, 1, 1, 0],
# [1, 1, 1, 1, 1]])
# Mask 0 → -inf in scores → softmax 0
attn = SelfAttention(d_model=64)
x = torch.randn(2, T, 64)
out, _ = attn(x, mask=mask)
BERT-style — bidirectional, no mask। GPT-style — causal mask, autoregressive। মূল architecture একই।
৭ · Self-attention property
- Permutation invariance: token order matter না (sets-এর মতো)। তাই positional encoding লাগে। (পরের module L34-এ)।
- Parallelism: সব position simultaneously compute। RNN-এর সাথে বিপরীত। GPU-এ huge speedup।
- Long-range: token i token j-এ direct connection — distance যাই হোক। Vanishing gradient সমস্যা নেই।
- Compute: $O(T^2 d)$ — quadratic in sequence length।
- Memory: $O(T^2)$ attention matrix। Long sequence-এ bottleneck।
৮ · Comparison — RNN vs CNN vs Self-attention
- Path length (token i ↔ j gradient flow):
- RNN: $O(T)$ — vanishing gradient।
- CNN: $O(\log_k T)$ — kernel size $k$।
- Self-attention: $O(1)$ — direct।
- Compute per layer:
- RNN: $O(T d^2)$।
- CNN: $O(k T d^2)$।
- Self-attention: $O(T^2 d)$।
- Parallelize: RNN sequential, others fully parallel।
- Long sequence-এ RNN slow, self-attention memory-hungry।
ভাবনার প্রশ্ন
প্রতিটি প্রশ্ন নিজে কিছুক্ষণ ভাবুন — তারপর "→ উত্তর" চাপুন।
প্র ০১ কেন Q, K, V আলাদা projection? একই তিনবার ব্যবহার করলে কী হবে? Each-এর role intuitively?
Q, K, V separation — Transformer-এর genius। তিন আলাদা projection-এর deep design rationale।
Same input তিনবার:
- Q = K = V = X — naive case।
- $X X^\top$ — symmetric matrix। Diagonal high (self-similarity)।
- Each token strongly attend itself।
- Useful information extract limited।
Why three separate views:
- Q (asking): "এই token কী জানতে চায়?"
- K (matching): "এই token কী match করে?"
- V (carrying): "এই token কী information দেয়?"
- Three roles different — separate learned projection।
Database analogy detailed:
- Library — query "history book"।
- Each book — title (key) + content (value)।
- Title-content same not — "Bangladesh History" title vs ৫০০-page content।
- Match by title, retrieve content।
- Same separation Q-K-V।
K vs V difference:
- K — what's useful for matching।
- V — what's useful as information।
- Could differ — "about" vs "contents"।
- Same dimensionality not required।
Q vs K relationship:
- Q project — query space।
- K project — key space।
- Same dimension (matmul require)।
- Compatible for dot-product compute।
Asymmetry vs symmetry:
- Naive $X X^\top$ — symmetric attention।
- Q, K separate — asymmetric possible।
- Token i to j weight ≠ j to i weight।
- Real language asymmetric (verb-subject relationship)।
Empirical evidence:
- Ablation — Q=K=V model significantly worse।
- Q=K (shared) — slight degradation।
- K=V (shared) — moderate degradation।
- All separate — best।
Capacity perspective:
- Three projection — ৩ × $d^2$ parameters।
- Different "views" of same input।
- Increased model capacity।
- Same input multiple representations।
Geometric interpretation:
- X — original embedding space।
- $X W_Q$ — rotate/project to "query space"।
- $X W_K$ — different projection "key space"।
- Compatibility metric task-specific।
Multi-head extension:
- Multiple Q, K, V projections — different head।
- Each head — different aspect attention।
- L32-এ বিস্তারিত।
Cross-attention case:
- Q from decoder, K, V from encoder।
- Asymmetric — different source for Q vs K, V।
- Same Q, K, V machinery।
Modern variants:
- Linear attention: Q, K kernel feature map।
- Performer: random feature approximation।
- Linformer: K, V low-rank।
- Q, K, V structure preserved।
মূল উপলব্ধি: Q, K, V three roles — asking, matching, carrying। Same input three views। Asymmetric attention possible। Capacity gain। Database analogy intuitive। Without this separation — attention much weaker। Modern Transformer foundation। Architecture genius — simple but profound design।
প্র ০২ Self-attention permutation invariant — token order এ matter করে না। তবে language তো order-sensitive। কীভাবে handle?
এটি Transformer-এর critical design issue। Solution — positional encoding। কিন্তু why permutation invariance, কীভাবে injection?
Permutation invariance — proof:
- $\text{Attn}(P X) = P \cdot \text{Attn}(X)$ — output permutes same way।
- Attention computation set operation effectively।
- Token "order" intrinsically not used।
Language order matter কেন:
- "আমি ভাত খাই না" vs "না ভাত খাই আমি" — different।
- "The dog bit the man" vs "The man bit the dog" — opposite।
- Syntax encoded in order।
- Without order — bag of words।
Solution 1 — Sinusoidal positional encoding (Vaswani):
- $PE_{pos, 2i} = \sin(pos / 10000^{2i/d})$।
- $PE_{pos, 2i+1} = \cos(pos / 10000^{2i/d})$।
- Different frequency — position uniquely identify।
- Add to input embedding।
- Fixed, not learned।
Why sinusoidal:
- Smooth — close positions similar encoding।
- Linear projection → relative position — $\sin(a+b) = \sin a \cos b + \cos a \sin b$।
- Extrapolate longer — wave property।
- No additional parameter।
Solution 2 — Learned positional embedding (BERT, GPT-2):
- Position 0, 1, 2, ... — learned embedding।
- Max sequence length fixed।
- Cannot extrapolate।
- Marginal accuracy benefit।
Solution 3 — Relative positional encoding (Shaw, T5):
- $i - j$ relative distance encode।
- Translation invariant।
- Better generalization।
- Computational overhead।
Solution 4 — RoPE (Rotary, ২০২১):
- Rotation matrix multiplication on Q, K।
- Implicit relative position।
- Long context excellent extrapolation।
- Modern LLM standard (LLaMA, GPT-NeoX)।
Solution 5 — ALiBi (Attention with Linear Biases):
- Distance-based attention bias।
- No positional embedding।
- Strong extrapolation।
- Press et al. (২০২১)।
Comparison study:
- Sinusoidal — simple, works।
- Learned — slight accuracy gain, length-limited।
- Relative — better long-range।
- RoPE — current best practice।
Why no positional encoding fail:
- Order information completely lost।
- Bag of word equivalent।
- Syntax impossible learn।
- Order-sensitive task fail।
Bangla-specific consideration:
- Word order flexible (free word order language)।
- SOV typical, but variations common।
- Honorific position significant।
- Standard positional encoding sufficient।
Image patches — same issue:
- ViT (Vision Transformer) — image patches।
- Patch position 2D — flatten to 1D positional encoding।
- Or — 2D sinusoidal, learnable।
Audio:
- Time-frequency 2D — similar 2D positional।
- Or — 1D temporal positional।
Encoder-decoder:
- Both side positional encoding।
- Cross-attention — encoder positional helps source alignment।
Theoretical depth:
- Without PE — Transformer = bag of word + interaction।
- PE injection — order-sensitive।
- Inductive bias formalized।
মূল উপলব্ধি: Self-attention permutation-invariant — language requires order। Positional encoding inject order information। Sinusoidal classic, RoPE modern। Without PE — order completely lost। Architecture-data alignment design challenge। Modern LLM RoPE standard। Subtle but critical detail।
প্র ০৩ Self-attention $O(T^2)$ — Bangladesh-এ Bangla document analysis-এ practical issue। ১০০ পাতার legal document — কীভাবে handle?
Long document Bangla legal/medical/research — Bangladesh-এর জন্য practical use case। $O(T^2)$ scaling-এর সাথে battle।
Document size analysis:
- ১০০ পাতা — ৪০-৬০K word।
- Bangla token-এ ৫০-৭০K।
- Standard Transformer — ৫১২-২K context।
- Direct fit impossible।
Strategy 1 — Chunking:
- Document chunks-এ break — ৫১২ token each।
- Overlap (e.g., ১০%) — boundary information preserve।
- Each chunk independently process।
- Loss — cross-chunk information।
Strategy 2 — Hierarchical:
- Sentence-level encoding — Transformer।
- Document-level — sentence vectors দিয়ে আরেকটি Transformer।
- Two-level attention।
- Long document handle।
Strategy 3 — Sparse attention:
- Longformer — local window + global token।
- BigBird — random + window + global।
- $O(T)$ effective।
- Long document feasible।
Strategy 4 — Linear attention:
- Performer, Linformer।
- $O(T d)$ compute।
- Approximate but practical।
- Quality slight drop।
Strategy 5 — Flash attention:
- $O(T^2)$ compute — same।
- $O(T)$ memory!
- Tile-based, GPU memory hierarchy exploit।
- ৪-৮x longer context same hardware।
Strategy 6 — Retrieval-augmented:
- Document chunks vector store।
- Query-time relevant chunks retrieve।
- Small context, large knowledge base।
- RAG architecture।
Practical implementation:
from transformers import LongformerTokenizer, LongformerModel
tokenizer = LongformerTokenizer.from_pretrained(
'allenai/longformer-base-4096')
model = LongformerModel.from_pretrained(
'allenai/longformer-base-4096')
# 4096 token context!
text = open('legal_doc.txt').read()
inputs = tokenizer(text, return_tensors='pt',
truncation=True, max_length=4096)
outputs = model(**inputs)
Bangla-specific resources:
- BanglaBERT — ৫১২ context।
- Multilingual Longformer — ৪K।
- Bangla LLM (limited) — context varies।
- Custom train — practical option।
Use case-specific design:
Legal contract review:
- Section-wise chunking।
- Cross-reference — retrieval।
- Key clause attention।
- Hierarchical helpful।
Medical record analysis:
- Patient timeline — chronological chunks।
- Finding cross-reference।
- Privacy concern — local processing।
Research paper:
- Section-wise (abstract, intro, method, ...)।
- Citation aware।
- Domain-specific terminology।
Hardware budget:
- GPU memory — context length × batch।
- ৪K context Longformer — ১৬GB GPU comfortable।
- ৩২K Flash attention — ২৪GB।
- Cloud — A100 40GB, H100 80GB।
Cost-benefit:
- Chunking — cheap, lossy।
- Sparse — moderate cost, good quality।
- Flash — best quality, hardware investment।
- RAG — most flexible, additional infrastructure।
Implementation suggestion (Bangladesh):
- Start — chunking + BanglaBERT।
- Improve — Longformer fine-tune।
- Production — RAG with vector store।
- Future — Mamba/state-space।
Evaluation:
- Holistic understanding required।
- Cross-document inference।
- Long-range dependency capture।
- Domain-specific benchmark।
Future direction:
- Mamba — linear scaling, document-friendly।
- Hybrid — Transformer + retrieval।
- 1M context Gemini-style।
- Bangladesh — gradual adoption।
মূল উপলব্ধি: Long Bangla document — chunking, sparse, Flash, RAG strategy। ১০০ পাতা — multiple approach combine। Hierarchical practical। Cost-quality trade-off। Bangladesh — BanglaBERT + chunking pragmatic start, gradually evolve। Long context — modern AI's frontier challenge।
প্র ০৪ Self-attention "set operation" — কেন তবু sequence model-এ এত effective? Inductive bias বিশ্লেষণ।
Surprising paradox — set-like operation কীভাবে sequence task-এ excel? Deep theoretical question।
Inductive bias spectrum:
- Strong bias — RNN, CNN। Order/locality assumption।
- Weak bias — Transformer। Minimal assumption।
- "Bitter lesson" (Sutton) — less bias + more data win।
Self-attention bias:
- Pairwise interaction — every token to every other।
- Order-agnostic — positional encoding inject।
- No locality assumption।
- Permutation equivariant।
Strong RNN bias:
- Sequential — left-to-right।
- Recent prefer over distant।
- Implicit Markov assumption।
- Linguistic structure — bias right!
Why Transformer wins despite weak bias:
(১) Scale:
- Massive data + parameter — bias-free model overcome।
- RNN scale poorly (sequential)।
- Transformer scale beautifully (parallel)।
(২) Optimization:
- Path length $O(1)$ — gradient flow easy।
- RNN gradient vanish — weak bias hurt training।
- Optimization friendly architecture।
(৩) Information access:
- Direct connection any pair।
- RNN — bottleneck through hidden state।
- Information density Transformer-এ higher।
"Set" misconception:
- Pure set — bag of word।
- + Positional encoding — order-aware।
- + Multi-layer — composition।
- Effectively sequence model।
Empirical perspective:
- BERT, GPT — language understanding excel।
- Long-range dependency capture।
- Syntactic structure emerge।
- Without explicit grammar bias।
Linguistic bias from data:
- Order, hierarchy, agreement — pattern in data।
- Model learn from examples।
- Grammar implicit — emergent।
- Universal grammar debate (Chomsky)।
Where Transformer struggles:
- Counting — exactly know how many?
- Compositional — novel composition learn।
- Algorithmic — multi-step reasoning।
- Strong bias model — sometimes better।
Hybrid approach:
- Convolution + attention — local + global।
- Conformer — speech recognition state-of-art।
- Vision Transformer + convolutional inductive bias।
- Best both world।
Permutation invariance benefit:
- Image — patch order arbitrary।
- Set learning — point cloud, molecule।
- Multi-modal — modality order flexible।
- Beyond sequence — broader applicability।
Modern context:
- Vision — ViT competitive CNN।
- Speech — Conformer dominant।
- Multi-modal — Flamingo, CLIP।
- RL — Decision Transformer।
- Universal architecture trend।
Theoretical understanding:
- Universal approximator — Transformer।
- Implicit kernel learning।
- Attention as soft routing।
- Active research।
Bias-data trade-off:
- Strong bias — small data, fast train।
- Weak bias — large data, eventual win।
- Modern era — large data possible।
- Therefore Transformer dominate।
Bangladesh context:
- Limited Bangla data — hybrid approach।
- Pretrain global, fine-tune local।
- Transfer learning critical।
- Modern Transformer + Bangla data।
মূল উপলব্ধি: Self-attention "set-like" — কিন্তু positional encoding + scale + optimization-এর কারণে sequence-এ excel। Weak inductive bias + large data — modern paradigm। "Bitter lesson" — engineering bias-এর চেয়ে scale better। Hybrid — best balance। Transformer's success — minimal sufficient assumption। Architecture design philosophy lesson।
অনুশীলন
-
Compute: $X = \begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix}$, $W_Q = W_K = W_V = I$ (identity)। Attention output ($d=2$, $\sqrt{d} = \sqrt{2}$)।
$Q = K = V = X$।
$QK^\top = X X^\top = I$।
$/\sqrt{2}$: $\begin{pmatrix} 0.707 & 0 \\ 0 & 0.707 \end{pmatrix}$।
Softmax row-wise: $\begin{pmatrix} 0.67 & 0.33 \\ 0.33 & 0.67 \end{pmatrix}$।
Output = softmax · V = same matrix (V = X = I)।
-
Causal mask: $T = 4$ — mask matrix লিখুন। Token ৩ predict-এ কোন position attend possible?
mask = [[1, 0, 0, 0], [1, 1, 0, 0], [1, 1, 1, 0], [1, 1, 1, 1]]Token ৩ (index 2) — position ০, ১, ২ attend possible (lower triangle)। Future (position ৩) blocked।
-
Implement: PyTorch-এ একটি বাস্তব self-attention layer — input
(2, 5, 64)।উপরের
SelfAttentionclass দেখুন। Test:attn = SelfAttention(d_model=64) x = torch.randn(2, 5, 64) out, alpha = attn(x) # out: (2, 5, 64), alpha: (2, 5, 5)
আরও পড়ুন · ABCL TECH-এ আপনার পরবর্তী পদক্ষেপ
- পাঠ ৩২ · Multi-head attention পরবর্তী পাঠ একাধিক self-attention parallel — diverse perspective।
- পাঠ ৩০ · Attention mechanism আগের পাঠ Cross-attention foundation।
- পাঠ ৩৩ · Transformer architecture M5-এ Self-attention দিয়ে complete model।
- সব AI Courses দেখুন ABCL TECH Python, ML, DL, NLP, CV, GenAI, RL, MLOps — সব AI কোর্স একসাথে।