Inside the Transformer
Day 1-এ শিখেছো LLM মানে দুটো file, Day 2-এ শিখেছো token আর embedding। আজ সেই embedding vector transformer block-এর ভেতরে ঢোকার পর কী হয় — সেটাই finally খুলে দেখবে। Q, K, V, attention-এর ৪ ধাপ, multi-head, positional encoding, residual, causal mask — সব এক এক করে ৫টা interactive demo দিয়ে। Module 1-এর শেষ দিন এটাই।
- •একটা transformer block-এর ভেতরে কী হয় — “input vector → attention → FFN → refined output” — এই picture পরিষ্কার বলতে পারা।
- •Q, K, V কেন তিনটা আলাদা projection — এবং attention-এর ৪ ধাপ (score → scale → softmax → blend) নিজে কাগজে চালাতে পারা।
- •Multi-head কেন — “different heads, different relationships” — সেটা একটা বাক্যে বুঝিয়ে বলতে পারা।
- •কেন positional encoding দরকার (permutation problem) এবং RoPE কী — অন্তত নাম + এক বাক্যে intuition।
- •Causal mask কেন decoder-only model-কে autoregressive বানায় — সেটা বুঝতে পারা।
- •Day 1 + Day 2 শেষ। Token, embedding, vector, cosine similarity — এই concepts চেনা।
- •একটু matrix operation চেনা থাকলে ভালো (dot product, matrix multiplication — কিন্তু আমরা picture দিয়েই বুঝাব)।
- •Math phobia? No problem — formula শুধু একটা পাশে রাখব, intuition আগে।
The Big Picture
পুরো journey — input থেকে next token।
Day 1-এ দেখেছো — LLM মানে parameters file + run code। Day 2-এ দেখেছো — text কীভাবে token → ID → embedding হয়। আজ সেই embedding vector গুলো transformer block-এর ভেতরে ঢোকার পর কী হয়, সেটাই finally খুলে দেখব।
এক block একটাই কাজ করে — vector নেয়, context দিয়ে refine করে, refined vector ফেরত দেয়। GPT-3 এই কাজটা ৯৬ বার পরপর করে। আজ এক block-এর ভেতর কী ঘটে — self-attention, multi-head, positional encoding, residual, causal mask — এই সব এক এক করে ৫টা interactive demo দিয়ে। কাল থেকে Module 2 শুরু (prompting আর evals) — কিন্তু এই foundation আজ একবার বসিয়ে নিলে বাকি সব lesson সহজ লাগবে।
Transformer blockthe refinement station
এক block = এক refinement step। দর্জির দোকানে পাঞ্জাবি বানানোর মতো — প্রতিবার ফিটিং-এ একটু একটু নিখুঁত হয়। Block input হিসেবে কিছু vector নেয়, ভেতরে দুটো sublayer (self-attention + FFN) চালায়, output হিসেবে same shape-এর refined vector দেয়। Refined মানে — এখন vector-গুলোতে আশেপাশের context-এর তথ্য মেশানো আছে। GPT-3: ৯৬টা block, Llama-3 70B: ৮০টা, GPT-2 small: ১২টা — মানে ৯৬ বার পর্যন্ত ফিটিং।
যখন কেউ বলে “transformer” বা “layer”, সে আসলে এই block-এর কথা বলছে। এই abstraction পরিষ্কার থাকলে “96-layer model” শুনে সঙ্গে সঙ্গে বুঝবে — মানে ৯৬ বার refinement।
Attention — The Heart of the Block
Q, K, V → score → softmax → blend।
Query, Key, Valuethe three projections
এক token-এর embedding × তিনটা আলাদা learned weight matrix (W_Q, W_K, W_V) → Q, K, V vector পাও। তিনটাই same input-এর তিনটা আলাদা angle: Q = “আমি কী খুঁজছি?”, K = “আমি কী offer করি?”, V = “আমি কী contribute করব?”। তিনটা আলাদা matrix কারণ model freely শিখতে পারবে “looking for” আর “advertising”-এ different signal থাকা দরকার।
এই তিনটা আলাদা projection — attention-এর সবচেয়ে non-obvious part। বুঝলে বাকি সব formula সহজ লাগবে।
Scaled attentionthe 4-step formula
Attention(Q, K, V) = softmax(Q · Kᵀ / √d_k) · V। ৪ ধাপ — (১) Q · Kᵀ = প্রতি query-key পেয়ারের জন্য similarity score। (২) ÷ √d_k = score-গুলো scale down (softmax stability)। (৩) softmax = score → probability (sum=1)। (৪) × V = weighted blend of values। Output = current token-এর জন্য নতুন refined vector।
এই formula 2017-এর paper থেকে এসেছে আর এখনো প্রায় unchanged। Attention বোঝা মানে এই ৪ ধাপ মুখস্থ করে কাগজে চালানো।
Multi-headdifferent heads, different perspectives
Single attention head এক relationship pattern-ই শিখতে পারে। Multi-head = parallel-এ multiple heads (original transformer: ৮টা, GPT-3: ৯৬টা)। প্রতিটা head-এর নিজস্ব W_Q, W_K, W_V — আলাদা attention pattern শেখে। শেষে concatenate + একটা output projection (W_O)। Same compute cost কারণ প্রতি head-এর dimension d_model / num_heads।
“The animal didn't cross the street because it was too tired” — এক head শিখে “it→animal” (anaphora), আরেক head “tired→animal” (state)। একসাথে multiple relationship।
The Supporting Cast
Positional encoding, residual, layer norm, FFN — এসব ছাড়া কিছুই train হয় না।
Positional encodingfixing the permutation problem
Self-attention permutation equivariant — token-এর order ignore করে। “Dog bites man” আর “Man bites dog” আলাদা vector দেবে না নিজে থেকে। Solution — position-এর তথ্য embedding-এ inject কর। দুটো প্রধান approach: (১) Sinusoidal (2017 original) — fixed sin/cos pattern, প্রতি position-এ unique। (২) RoPE (2021) — Q আর K-কে position-অনুযায়ী rotate কর; Llama, Mistral, Qwen, DeepSeek — সব এটা use করে।
Modern LLM paper পড়লে “rotary position embeddings” বা “RoPE” শুনবে। আজ অন্তত নাম + এক বাক্যে intuition থাকুক।
Residual connectionthe gradient highway
Each sublayer-এর output-এ input add কর: output = sublayer(x) + x। শুনে trivial মনে হয় কিন্তু এটা না থাকলে ৯৬-layer transformer train-ই হবে না — gradient back-propagation-এ vanish করে যাবে। ResNet (2015)-এর idea। Residual path gradient-এর জন্য একটা “highway” — directly pass through।
Engineering choice, not theory। কিন্তু ছাড়া transformer = unusable। তাই প্রতি সাবলেয়ার-এর চারপাশে এটা থাকে।
Layer normalization
Per-token, per-layer normalize — vector-এর mean 0, variance 1 করো। Training stability-র জন্য critical। Original paper post-norm (sublayer-এর পর); modern practice pre-norm (sublayer-এর আগে) — deeper network-এ training stable।
প্রায় সবসময় background-এ। কিন্তু “why doesn't my deep transformer train” debug করতে হলে এই concept-এ ফিরে আসতে হবে।
FFNfeed-forward network — the per-token compute
Each token-এর vector-এ independently এই MLP চলে: linear → activation (ReLU/GELU) → linear। Inner dimension সাধারণত 4× of d_model (যেমন d=512 হলে inner=2048)। Attention “context mix” করে; FFN “per-token computation” করে। Surprising fact — model-এর majority parameter এই FFN-এ থাকে, attention-এ না।
Mixture of Experts (MoE) models — DeepSeek, Mixtral — ঠিক এই FFN-কে replace করে multiple specialized experts দিয়ে। Modern architecture-এ এটা চেনা জরুরি।
Decoder Side — Masking and Final Softmax
কেন GPT এক token করে generate করে।
Causal maskno peeking at the future
Day 1-এ বলেছিলাম — পরীক্ষায় পাশের জনের খাতা দেখা নিষেধ। সেই নিয়মটাই এখানে matrix আকারে। Decoder-only (GPT) self-attention-এ একটা mask — position i শুধু position 0 থেকে i−1 দেখতে পারে। Future token-এর attention score = −∞ (softmax-এর পর 0)। Training-এ যাতে next-token prediction-এ cheating না হয়। Inference-এ এই mask-ই generation-কে autoregressive বানায় — এক token করে।
Encoder (BERT) সব দেখে; decoder (GPT) শুধু past দেখে। এই difference architectural — mask দিয়ে enforced।
Logits & softmaxpredicting the next token
শেষ block-এর শেষ position-এর vector → linear projection [d_model → vocab_size] → logits। Softmax(logits) → probability distribution over vocab। শীর্ষ probability-র token = greedy choice। অথবা sampling — temperature, top-p, top-k দিয়ে variation যোগ করে। যেটা চাই — সেটাই predicted next token।
Sampling parameters সামনে আসবে। আজকের জন্য — এই last step-টা পরিষ্কার হলে “কীভাবে model একটা token output করে” পুরো mechanism complete।
Check Your Understanding
ভুল হলে সমস্যা নেই — প্রতিটার ব্যাখ্যা আছে।
Q, K, V তিনটা completely আলাদা vector — একই input থেকে আসে না
Multi-head attention মানে multiple attention layer একটার পর একটা stack
Self-attention নিজে থেকে token order বোঝে
Residual connections accuracy বাড়ায়
Modern LLM (Llama, Mistral) sinusoidal positional encoding ব্যবহার করে
Causal mask শুধু inference-এ ব্যবহার হয়
Model-এর majority parameter attention layer-এ থাকে
একটা transformer block কী output করে?
ভুল হলেও সমস্যা নেই — প্রতিটা option-এর সাথে ব্যাখ্যা আছে।
Scaled dot-product attention-এ ÷ √d_k step কেন?
ভুল হলেও সমস্যা নেই — প্রতিটা option-এর সাথে ব্যাখ্যা আছে।
Multi-head attention multiple head ব্যবহার করার মূল কারণ?
ভুল হলেও সমস্যা নেই — প্রতিটা option-এর সাথে ব্যাখ্যা আছে।
Positional encoding না থাকলে কী হবে?
ভুল হলেও সমস্যা নেই — প্রতিটা option-এর সাথে ব্যাখ্যা আছে।
Decoder-only (GPT-জাতীয়) model-এ causal mask কেন critical?
ভুল হলেও সমস্যা নেই — প্রতিটা option-এর সাথে ব্যাখ্যা আছে।
Prove It to Yourself
চারটা ধারণা মাথায় বসাও। Module 1 শেষ।
চারটা ধারণা মনে রাখো — একটা block মানে refinement station: vector ঢোকে, context দিয়ে mix হয়ে refined vector বের হয়; N বার stack মানে N বার refinement, same shape in same shape out। Attention = score → scale → softmax → blend — Q·Kᵀ → ÷√d_k → softmax → ×V, এই ৪ ধাপ মুখস্থ করো, সব AI paper-এ এই pattern ফিরে আসবে। Multi-head মানে parallel perspectives — এক head এক relationship শেখে, multi-head একসাথে multiple lens (anaphora, syntax, semantics) দেয়। আর তিনটা trick ছাড়া deep transformer train-ই হয় না — positional encoding (order বোঝা), residual connections (gradient flow), layer norm (activation stability)।
আজকের শব্দগুলো — transformer block, self-attention, query/key/value, softmax, multi-head attention, positional encoding, RoPE, residual connection, layer norm, causal mask, logits — এখন থেকে English-ই থাকবে; paper, docs, অফিসের আলোচনায় এভাবেই দেখবে। Transformer paper বা model release note এখন তোমার পড়ার মতো — এই শব্দগুলো ওখানে literal এই অর্থেই বসে। পুরো তালিকা নিচের glossary-তে।
শব্দার্থ — পরে ফিরে দেখার জন্য
একনজরে সব key term। কোনো শব্দ ভুলে গেলে এখানে এসে খুঁজে নিও।
- Attention
- Mechanism যেটা প্রতি token-কে context-এর অন্য token-এ “weighted look” দেয়। 4 steps: score, scale, softmax, blend।
- Autoregressive
- এক token করে generate — পরের token predict করতে আগের সব token-কে input হিসেবে নেয়। Decoder-only LLM-এর mode।
- Causal mask
- Attention matrix-এর upper triangle (future positions) hide করার technique। Decoder-এ training + inference দুটোতেই use হয়।
- Cross-attention
- Decoder যখন encoder-এর output-এ attend করে (original encoder-decoder transformer)। GPT-জাতীয় decoder-only-এ নেই।
- d_model
- Embedding dimension — প্রতিটা token-এর vector-এর length। GPT-2: 768, GPT-3: 12288, Llama-3 8B: 4096।
- FFN
- Feed-Forward Network — প্রতি transformer block-এ attention-এর পরে চলা per-token MLP। 2 linear + activation। Inner dim ~4× of d_model।
- Head
- Multi-head attention-এর এক parallel attention computation। প্রতিটা head-এর নিজস্ব W_Q, W_K, W_V — আলাদা pattern শেখে।
- Layer normalization
- Per-token activation normalize (mean 0, variance 1)। Training stability-র জন্য essential।
- Logits
- Vocab_size-এর একটা vector — softmax-এর আগে প্রতি token-এর “score”। শেষ block-এর শেষ vector × output projection = logits।
- Multi-head attention
- Single attention-এর বদলে parallel multiple heads — concatenate + project। Different heads, different relationships।
- Permutation equivariant
- Operation যেটা input-এর order ignore করে। Self-attention by default এমনই — সেজন্য positional encoding লাগে।
- Positional encoding
- Token-এর position-এর তথ্য vector আকারে encode করার technique। Sinusoidal বা RoPE।
- Pre-norm vs post-norm
- Layer norm sublayer-এর আগে (pre, modern) বা পরে (post, original)। Pre-norm deeper network train করা সহজ করে।
- Q / K / V
- Query / Key / Value — same input-এর তিনটা আলাদা learned projection। Attention-এর basic building block।
- Residual connection
- Sublayer-এর output-এ input add: output = sublayer(x) + x। Gradient flow-এর জন্য critical।
- RoPE
- Rotary Position Embeddings (2021) — Q/K vectors-কে position-অনুযায়ী rotate করে। Llama, Mistral-সহ modern LLMs ব্যবহার করে।
- Scaled dot-product attention
- Attention(Q,K,V) = softmax(QKᵀ/√d_k)V। Standard attention formula।
- Self-attention
- Attention যেখানে Q, K, V সব same input থেকে আসে। Token নিজের context-এ অন্য token-দের দেখে।
- Sinusoidal positional encoding
- 2017 original transformer-এর position encoding — sin/cos waves of multiple frequencies।
- Softmax
- Score-গুলোকে probability distribution-এ convert (positive + sum to 1)। Attention এবং final next-token both-এ use হয়।
- Transformer block
- Self-attention + FFN, দুটোর চারপাশে residual + layer norm। Stacking-এর single unit।
Sources consulted to author this lesson. Citation style is informal — follow the links if you want to dig deeper.
- Hands-On Large Language Models — Ch. 3: Looking Inside Transformer LLMs — Jay Alammar & Maarten Grootendorst (2024)
- The Illustrated Transformer — Jay Alammar (2018)
- The Annotated Transformer — Harvard NLP (2018)
- Designing Positional Encoding — Hugging Face (2024)
- Let's Build GPT: From Scratch, in Code, Spelled Out — Andrej Karpathy (2023)
- Attention Is All You Need — Vaswani et al. (2017)
- RoFormer: Enhanced Transformer with Rotary Position Embedding — Su et al. (2021)
দুই বাক্যে নিজের ভাষায় লেখো
নিজের ভাষায় লেখো — long prompt-এর শেষে থাকা instruction-টা system message-এ সরালে output কেন বদলাতে পারে? উত্তরে Day 1, 2, 3-এ শেখা কোনো ধারণা (token, embedding, attention, positional encoding) ব্যবহার করো।