Tokens & Embeddings Deep Dive
Day 1-এ শিখেছো একটা LLM আসলে দুটো file। আজ ভেতরে নামবে — text কীভাবে token, token কীভাবে integer ID, আর ID কীভাবে meaning-carrying embedding vector-এ পরিণত হয়, সেই পুরো pipeline-টা। BPE algorithm নিজে হাতে চালাবে, cosine similarity নিজে টেনে দেখবে, আর কেন বাংলা prompt-এ বিল বেশি আসে — সেটাও বুঝবে।
- •Text কীভাবে number হয় — “token → ID → embedding” এই pipeline-টা নিজে বুঝিয়ে বলতে পারা।
- •BPE training algorithm-টা ৩টা merge-এর উদাহরণ দিয়ে নিজে কাগজে চালাতে পারা।
- •একই sentence English vs Bengali-এ কেন আলাদা token count দেয় — সেটা বুঝে cost-aware decision নিতে পারা।
- •Cosine similarity-র geometric মানে নিজের ভাষায় ব্যাখ্যা করতে পারা — কেন magnitude নয়, direction।
- •Static আর contextual embedding-এর পার্থক্য — কখন কোনটা use হয়।
- •Day 1 শেষ করেছো। Token, embedding, attention — এই তিনটা শব্দ কান-এ পরিচিত।
- •কোনো math background দরকার নেই — শুধু “vector মানে সংখ্যার সারি” এটুকু চিনলেই হবে।
- •Day 1-এর tokenizer demo নিজে একবার try করেছো।
The 2-Stage Pipeline
পুরো lesson এই চারটা box-এর মধ্যেই ঘুরবে।
Day 1-এ তুমি দেখেছো একটা LLM আসলে দুটো file — parameters আর run code। আজ ভেতরে নামব একটা step — সেই run code-টা তোমার text-কে কী করে। Spoiler: দুবার convert করে। প্রথমে tokens + IDs-এ (text → integer)। তারপর embeddings-এ (integer → meaning-carrying vector)।
এই pipeline-টা মূল — কারণ RAG, semantic search, agent memory, classification — Module 4 আর Module 5-এর প্রায় সব topic এই দুটো step-এর উপর বসে। আজকের ৬টা demo এই pipeline-টা হাতে কলমে অনুভব করানোর জন্য।
Stage 1: Tokenization
Text → discrete subword units → integer IDs।
Tokendeeper than Day 1
Day 1-এ বলেছিলাম token মানে “text-এর ছোট টুকরো”। আজ সংজ্ঞা শক্ত করি — token হলো model-এর vocabulary-র একটা entry, যার একটা integer ID আছে। Model word দেখে না, ID দেখে। তাই API-এর pricing word-এ না, token-এ। GPT-4 tokenizer-এ “Hello” = 1 token (ID 9906), “unbelievable” = 3 token, “আমি” = 5 token।
প্রতিটা cost decision token দিয়ে হয় — কতটা context পাঠাবে, কোথায় কাটবে, কোথায় cache করবে। Token বুঝলে বুঝবে টাকাটা কোথায় যাচ্ছে।
একটা শব্দকে অর্থপূর্ণ subword-এ ভাগ করে — vocabulary ছোট থাকে, rare word handle হয়।
Tokenizer stages4 steps most skip 3 of
Tokenizer-এর কাজ ৪ ধাপে: (১) Normalization — Unicode form (NFKC), control char strip। (২) Pre-tokenization — whitespace + punctuation দিয়ে word-এ ভাগ। (৩) Splitting — প্রতিটা word-কে character/byte-এ ভাগ। (৪) Merging — learned merge rule গুলো order-অনুযায়ী apply। বেশিরভাগ ধাপ ৪ নং-ই শুধু দেখে। ১-৩ নং miss হলে তুমি বুঝবে না কেন একই text কখনো ভিন্ন token দেয়।
“Café” আর “Cafe” কেন কখনো একই tokenize হয়, কখনো না — উত্তর normalization step-এ।
Vocabulary
Tokenizer-এর pre-built dictionary। প্রতিটা token-এর একটা unique integer ID (0 থেকে vocab_size − 1)। GPT-2: 50,257 · Llama-2: 32,000 · Llama-3: 128,000 · GPT-4o: ~200,000 · Claude: ~100,000+। বড় vocab = বেশি English word “as is” represent করা যায়, কিন্তু embedding matrix-ও বড় হয়। ছোট vocab = সব subword-এ ভাঙতে হয়, কিন্তু compact।
Vocab size একটা design choice — speed/quality trade-off। নতুন tokenizer (Llama-3) বড় vocab নিয়েছে কারণ multilingual better করতে চেয়েছিল।
BPE trainingthe merge-frequent-pairs game
Byte-Pair Encoding — একটা greedy frequency game। Start করো শুধু character দিয়ে। তারপর iteratively সবচেয়ে বেশি একসাথে আসা পেয়ার (pair) মিলিয়ে দাও — একটা নতুন token হলো। যতক্ষণ না target vocab size-এ পৌঁছায়, repeat। নিচের interactive demo-তে নিজে চালিয়ে দেখো — তিনটা merge step এ পুরো idea মাথায় বসে যাবে।
যেকোনো modern tokenizer (GPT, Llama, Claude সবই) এই pattern-এ trained — কিছু variation (WordPiece, SentencePiece) আছে, কিন্তু core idea একই।
Byte-level BPEGPT-2's 2019 trick
Unicode-এ ~150,000 character আছে। সবগুলোকে base vocab-এ রাখলে vocab বিশাল হবে। GPT-2-এর সমাধান — character না, byte ব্যবহার করো। Base vocab fixed 256 (এক byte = এক value)। যেকোনো Unicode character UTF-8-এ 1-4 byte-এ ভাঙে — সব representable। Result: [UNK] token-এর দরকার নেই। কিন্তু price — Bengali, Hindi, Chinese-এ প্রতি character 2-4 byte লাগে, তাই token-cost English-এর চেয়ে 2-6× বেশি।
API bill কেন Bengali prompt-এ বেশি আসে — উত্তর এখানে। এটা bug না, design।
Token IDs
Tokenizer-এর output আসলে text না — integer-এর সারি। GPT-4-এর কাছে “Hello world” = [9906, 1917]। এই মুহূর্তেই text শেষ, math শুরু। ID-গুলোর সংখ্যাগত মান-এর কোনো meaning নেই — 9906 আর 9907 কাছাকাছি ID হলেও meaning কাছাকাছি না। ID শুধু lookup index।
API request-এ তুমি text পাঠাও, কিন্তু model received করে ID। Tokenizer mismatch হলে (e.g., GPT-3.5 tokenizer দিয়ে GPT-4-এ পাঠালে) — silent corruption।
Special tokens
Tokenizer-এর কাছে শুধু “real text” না — কিছু engineered token আছে যেগুলো structure mark করে। [CLS] = sentence start (BERT)। [SEP] = separator। [PAD] = batching-এর জন্য padding। <|endoftext|> = document boundary (GPT-2)। <|system|>, <|user|>, <|assistant|> = chat role markers। Chat model কীভাবে জানে কোথায় তোমার message শেষ, কোথায় system instruction — এই special token দিয়ে।
Prompt injection attack অনেক সময় special token impersonate করে কাজ করে। সুরক্ষিত prompt লিখতে হলে এদের চেনা দরকার।
Non-English costthe bill nobody warns you about
“Hello world” = 2 token। “আমি ভালো আছি” = 13-17 token (একই অর্থ!)। কারণ — byte-level BPE-এর base vocab byte, আর Bengali প্রতি character UTF-8-এ 3 byte। তাছাড়া BPE training data বেশিরভাগ English, তাই Bengali subword-এর merge rare। ফলে Bengali প্রায় byte-level-এ tokenize হয়। Practical consequence: API cost 4-6× বেশি Bengali prompt-এ।
Day 1-এর রাফির কথা মনে আছে? ওর team Bengali complaint গুলো সরাসরি API-তে পাঠিয়ে দেখলো — bill English-এর প্রায় পাঁচগুণ। তখন থেকে ওরা English instruction-এর সাথে Bengali example mix করে। তুমিও Bengali data নিয়ে কাজ করলে budget আগে হিসাব করো, cache aggressively।
Stage 2: Embeddings
Integer IDs → meaning-carrying vectors।
Embeddingthe bridge from integers to meaning
Token ID হলো শুধু “এই subword-এর dictionary-তে index”। ID-র সংখ্যাগত মান-এর কোনো অর্থ নেই। Meaning encode করতে প্রতিটা ID-কে একটা dense float vector দিয়ে replace করা হয় — সেটাই embedding। Dimension typical: GPT-2: 768, Llama-3 8B: 4096, OpenAI text-embedding-3-small: 1536, large: 3072। সব embedding store থাকে একটা বিশাল embedding matrix-এ (shape: vocab_size × embedding_dim)। Training-এ ওই matrix-এর সব entry learned।
RAG, semantic search, classification — সবকিছুর foundation এই vector। Day 2 শেষে তুমি জানবে কেন “similar meaning → similar vector” — তাই কী।
Cosine similarity
দুটো vector-এর similarity measure করার সবচেয়ে common পদ্ধতি। Formula: cos(θ) = (a · b) / (|a| × |b|)। কিন্তু formula মুখস্থ না করে picture-টা ধরো — দুটো arrow origin থেকে। মাঝখানের angle। cos(0°) = 1 (same direction = same meaning)। cos(90°) = 0 (orthogonal = unrelated)। cos(180°) = −1 (opposite)। Magnitude (লম্বা vs ছোট vector) ignore করে — শুধু direction দেখে।
Vector search, recommendation, document similarity — সব cosine similarity-এর উপর দাঁড়িয়ে। উপরের draggable demo-তে নিজে হাত দিয়ে দেখো।
Vector arithmeticking − man + woman ≈ queen
Word2vec-এর সবচেয়ে বিস্ময়কর finding — semantic relationship embedding space-এ direction হিসেবে stored। Gender axis, royalty axis, capital-city axis — এগুলো আলাদা fact না, geometry। king vector থেকে man vector বাদ দিন → woman vector যোগ করো → result queen-এর কাছাকাছি landing করে। Paris − France + Italy ≈ Rome। walking − walk + swim ≈ swimming।
Model আলাদা করে “queen is the female of king” শেখেনি — শুধু context predict করতে গিয়ে এই geometry তৈরি হয়েছে। Emergence-এর প্রথম example।
Static/contextualdeeper than Day 1
Static (word2vec, GloVe) — প্রতি token-এর একটাই vector, context যাই হোক। “bank” word সর্বদা একই vector — financial bank, river bank, blood bank সব একই। Contextual (BERT, GPT, যেকোনো modern LLM) — vector context অনুযায়ী compute হয়। Transformer-এর self-attention layer-by-layer প্রতিটা token-এর vector re-shape করে আশেপাশের token দিয়ে। শেষ layer-এ “river bank”-এর bank-এর vector “money bank”-এর bank থেকে geometrically আলাদা।
Cheap retrieval-এ static embedding (cached, fast) এখনো use হয়। কিন্তু nuance চাইলে contextual embedding (slower, smarter) দরকার। এই trade-off Module 4 (RAG)-এ ফেরত আসবে।
Check Your Understanding
ভুল হলে সমস্যা নেই — প্রতিটার ব্যাখ্যা আছে।
Tokenizer শুধু whitespace আর punctuation দিয়ে text কাটে
১ token = ১ word
GPT-4 আর Llama-3 একই text-এর জন্য একই token দেবে
Embedding বলে দেয় একটা word-এর meaning কী
Cosine similarity আর Euclidean distance একই জিনিস measure করে
Contextual embedding আসার পর static embedding obsolete
Bigger embedding dimension সবসময় better
একটা BPE tokenizer-এ merge rules শেখা হয়েছে: (u,g)→ug, (h,ug)→hug। “hugging” word-টা কীভাবে tokenize হবে?
ভুল হলেও সমস্যা নেই — প্রতিটা option-এর সাথে ব্যাখ্যা আছে।
GPT-2 কেন character-level BPE-র বদলে byte-level BPE use করে?
ভুল হলেও সমস্যা নেই — প্রতিটা option-এর সাথে ব্যাখ্যা আছে।
একটা model-এর embedding dimension 1536। এই সংখ্যাটার physical meaning কী?
ভুল হলেও সমস্যা নেই — প্রতিটা option-এর সাথে ব্যাখ্যা আছে।
একই অর্থের English vs Bengali sentence-এ Bengali কেন বেশি token নেয়?
ভুল হলেও সমস্যা নেই — প্রতিটা option-এর সাথে ব্যাখ্যা আছে।
Embedding-এ Euclidean distance-এর বদলে cosine similarity কেন বেশি use হয়?
ভুল হলেও সমস্যা নেই — প্রতিটা option-এর সাথে ব্যাখ্যা আছে।
Prove It To Yourself
চারটা ধারণা মনে রাখো। নিজের ভাষায় লেখো। Day 2 শেষ।
চারটা ধারণা মনে রাখো — Text → Tokens → IDs → Vectors, এই pipeline-টা মুখস্থ করো, প্রতিটা concept এই চার stage-এর কোনো না কোনোটার সাথে attach করে। BPE = greedy frequency game — সবচেয়ে frequent adjacent pair merge করো, নতুন token যোগ করো, repeat, vocab full না হওয়া পর্যন্ত; এটাই পুরো algorithm। Cosine = direction, না distance — vector search, RAG, recommendation, সবকিছুর নিচে cosine similarity, এই geometric intuition আজ build করে নিলে Module 4 (RAG)-এ confusion হবে না। আর Bengali costs ~4× বেশি token — byte-level BPE + English-dominated training → non-Latin script ব্যয়বহুল; cache aggressively, prompt shorter রাখো, English instruction-এর সাথে Bengali example mix করো।
আজকের শব্দগুলো — tokenizer, vocabulary, BPE, merge rule, token ID, special token, embedding, vector, cosine similarity, dimension — এখন থেকে English-ই থাকবে; paper, docs, অফিসের আলোচনায় এভাবেই দেখবে। কাল কেউ “token limit” বললে তুমি জানো সেটা কী। পুরো তালিকা নিচের glossary-তে।
শব্দার্থ — পরে ফিরে দেখার জন্য
একনজরে সব key term। কোনো শব্দ ভুলে গেলে এখানে এসে খুঁজে নিও।
- BPE
- Byte-Pair Encoding — সবচেয়ে frequent adjacent pair iteratively merge করে subword vocabulary বানানোর algorithm। Modern tokenizer-এর core।
- Byte-level BPE
- BPE যেখানে base alphabet character না, byte (256টা)। GPT-2 introduce করেছে। কোনো [UNK] হয় না।
- Contextual embedding
- Vector context অনুযায়ী compute হয় (transformer-এর self-attention দিয়ে)। BERT, GPT — সব modern LLM।
- Cosine similarity
- দুটো vector-এর direction কতটা কাছাকাছি তার measure। Range: −1 (opposite) থেকে +1 (identical)। Embedding-এ similarity-র default metric।
- Embedding
- Token ID-কে replace করে যে dense float vector — meaning carry করে। Dimension typical 384-4096।
- Embedding matrix
- Shape [vocab_size × embedding_dim]। Each row = এক token-এর embedding। Training-এ এর entry শেখা হয়।
- Merge rule
- BPE training-এ শেখা rule — দুটো adjacent token-কে এক token-এ পরিণত কর। Order-অনুযায়ী apply হয়।
- Normalization
- Tokenization-এর প্রথম step — Unicode form (NFKC), case folding (optional), control char strip।
- Pre-tokenization
- Whitespace + punctuation দিয়ে text-কে word-এ ভাগ করার step (BPE merge চালানোর আগে)।
- Special token
- Structure mark করার engineered token: [CLS], [SEP], [PAD], <|endoftext|>, <|user|>, <|assistant|>, ইত্যাদি।
- Static embedding
- প্রতি token-এর fixed একটা vector, context যাই হোক। word2vec, GloVe ইত্যাদি।
- Token ID
- Vocabulary-তে একটা token-এর integer index। Model এই ID দেখে, text দেখে না।
- Vector arithmetic
- Embedding-এ semantic operation — যেমন king − man + woman ≈ queen। Direction = relationship।
- Vocabulary
- Tokenizer যত token চেনে তাদের complete list (with IDs)। Size typical 30k-200k।
- word2vec
- 2013-র প্রথম সফল word-embedding model। Skip-gram + negative sampling-এ training। king − man + woman ≈ queen।
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. 2: Tokens and Embeddings — Jay Alammar & Maarten Grootendorst (2024)
- LLM Course — Byte-Pair Encoding Tokenization — Hugging Face (2024)
- The Illustrated Word2Vec — Jay Alammar (2019)
- Let's Build the GPT Tokenizer — Andrej Karpathy (2024)
- OpenAI Tokenizer Playground — OpenAI (2024)
- Neural Machine Translation of Rare Words with Subword Units — Sennrich, Haddow & Birch (2016)
- Efficient Estimation of Word Representations in Vector Space — Mikolov, Chen, Corrado & Dean (2013)
দুই বাক্যে নিজের ভাষায় লেখো
“Hello world” আর “আমি ভালো আছি” — দুটো বাক্য GPT-4-এ পাঠালে কেন আলাদা cost হয়? নিজের ভাষায় explain করো (যেন একজন colleague-কে বোঝাচ্ছো)।