4.2.4 · HinglishTokenization & Language Modeling

Vocabulary size tradeoffs

2,673 words12 min readRead in English

4.2.4 · AI-ML › Tokenization & Language Modeling

Why vocabulary size matters

Jab hum text ko tokenize karte hain, toh hum ek compression-granularity tradeoff kar rahe hote hain. Chhoti vocabulary (jaise, byte-level ke liye 256 tokens) har character ko alag se treat karti hai. Badi vocabulary (100K+ tokens) common words ya phrases poore ke poore ek hi token mein represent kar sakti hai.

YEH kyun matter karta hai:

  1. Sequence length directly transformer compute ko impact karti hai: self-attention ke liye, jahan sequence length hai
  2. Embedding table size parameters hoti hai, jahan vocabulary size hai
  3. Token granularity affect karti hai ki model rare/unseen words pe kitna generalize kar sakta hai

The mathematics of the tradeoff

Chalo resource costs ko first principles se derive karte hain.

Embedding table cost

Vocabulary size aur embedding dimension ke liye:

KYU: tokens mein se har ek ko ek -dimensional dense vector representation chahiye.

ISKA matlab: Agar aur (BERT-base), toh yeh parameters hain sirf embedding table ke liye. (Agar instead ho, toh yeh hoga.) Yeh ke saath linearly badhta hai.

Sequence processing cost

Sequence length , embedding dimension , aur transformer layers ke liye:

Self-attention cost per layer:

KYU: Attention mechanism compute karta hai jo hai. Phir attention-weighted values: . term dominate karta hai.

Total forward pass cost:

term (attention) aur term (feedforward) dono sequence length ke saath scale karte hain.

ISKA matlab: Sequence length ko double karne se attention cost chaar guni ho jaati hai lekin feedforward cost sirf double hoti hai. Long documents ke liye, attention dominate karta hai.

The breakeven analysis

Kab ek badi vocabulary overall resources bachati hai?

Memory cost comparison:

  • Chhoti vocab (): Embedding cost , sequence length , attention cost
  • Badi vocab (): Embedding cost , sequence length , attention cost

Badi vocab jeetati hai agar:

YEH formula kyun: Left side badi vocabulary ka permanent parameter overhead hai. Right side chhoti sequences se per-batch activation memory savings hai. Agar aap bahut saare batches process karte ho (training, high-throughput inference), toh right side accumulate hoti hai aur left ko outweigh kar sakti hai.

Figure — Vocabulary size tradeoffs

Common vocabulary size choices

Vocabulary Size Typical Use Case Pros Cons
256–512 Byte-level, multilingual Universal, koi UNK tokens nahi Bahut lambi sequences
8K-16K Early neural MT, chhote models Balanced, manageable embedding size Kuch rare words split ho jaate hain
32K-50K GPT-2, BERT, RoBERTa Standard tradeoff, proven Rare technical terms se struggle
100K-250K Newer LMs, code models Chhoti sequences, domain coverage Badi embedding tables

Practical guidelines

Apne use case ke liye vocabulary size chunna:

  1. General-domain text (web, books): 32K-50K subword vocab (BPE/WordPiece)

    • KYU: Common word compression aur rare word splitting ke beech balance karta hai
  2. Code models: 50K-100K vocab

    • KYU: Code mein bahut saare unique identifiers, API names hote hain. Badi vocab get_user_profileget, _user, _profile jaise function names ki splitting reduce karti hai
  3. Multilingual (100+ languages): 250K+ vocab YA byte-level

    • KYU: Har language ko apne frequent tokens chahiye. Byte-level explosion avoid karta hai lekin sequence-length cost pay karta hai.
  4. Domain-specific (medical, legal): 50K-100K pe domain-specific vocab train karo

    • KYU: "Myocardial infarction" 1-2 tokens hona chahiye, 6 nahi. Efficiency aur performance dono improve hoti hai.
  5. Extremely long context (100K+ tokens): Chhoti vocab (16K-32K) ya hierarchical tokenization

    • KYU: attention ke saath, har token count karta hai. Sequences chhoti rakhna aur rare words split karna better hai.

Connections

  • 4.2.02-Byte-pair-encoding — BPE training merges ki number ke hisaab se alag vocab sizes produce karta hai
  • 4.2.03-WordPiece-and-SentencePiece — SentencePiece vocab size hyperparameter ka direct control allow karta hai
  • 4.3.01-Transformer-attention-complexity cost jo vocabulary size indirectly impact karti hai
  • 4.5.02-Embedding-layer-design — Embedding tables kaise implement aur optimize hoti hain
  • 5.1.03-Memory-optimization-techniques — Flash Attention, gradient checkpointing cost reduce karte hain
  • 6.2.01-Multilingual-models — 100+ languages ke liye massive vocab sizes kyun zaroori hain

Flashcards

#flashcards/ai-ml

Tokenization mein vocabulary size tradeoff kya hai?
Chhoti vocabulary → lambi sequences (zyada attention cost) lekin kam embedding parameters. Badi vocabulary → chhoti sequences (kam attention cost) lekin zyada embedding parameters. Yeh ek resource allocation decision hai.
Attention cost ke liye vocab size se sequence length zyada kyun matter karta hai?
Attention cost hai jahan sequence length hai. Sequence length double karne se attention cost chaar guni ho jaati hai. Embedding cost sirf hai, vocab size mein linear hai.
Embedding table parameter count ka formula kya hai?
Embedding parameters = jahan vocabulary size hai aur embedding dimension hai. GPT-2 small ke liye (, ), yeh 38.6M parameters hai.
Character-level models vocabulary tradeoff kyun solve nahi karte?
Woh embedding table cost eliminate karte hain lekin sequence length explode ho jaati hai (8-10x lambi). Attention hai, isliye compute ~64x badh jaata hai. Tradeoff sirf parameters se compute pe shift ho jaata hai.
Badi vocabulary resources kab bachati hai?
Jab ho. Extra embedding params ko bahut saare batches ke accumulated attention memory savings se outweigh hona chahiye.
Code models vs. general text ke liye typical vocabulary size kya hai?
Code models 50K-100K use karte hain (bahut saare unique identifiers). General text 32K-50K use karta hai. Code ko function/variable names ko bahut saare tokens mein split hone se bachane ke liye badi vocab chahiye.
Vocabulary mein frequency ke hisaab se bottom 10% kyun important hai?
Rare tokens (bottom 10%) ko good embeddings ke liye training mein phir bhi 50-100+ baar aana chahiye. Warna aap noisy embeddings pe parameters waste karte ho jo generalize nahi karte. Yeh vocab size choice guide karta hai.
Recall Ek 12-saal ke bacche ko explain karo

Imagine karo tum ek dost ko text kar rahe ho, lekin tumhare paas sirf ek limited "dictionary" of pre-approved text shortcuts hai.

Chhoti dictionary (256 shortcuts): Tum sirf individual letters bhej sakte ho jaise "h", "e", "l", "o". "hello" kehne ke liye tum 5 messages bhejte ho. Yeh slow hai!

Medium dictionary (32,000 shortcuts): Tumhare paas common words ke shortcuts hain. "hello" ek shortcut hai, "how" doosra hai, "are" teesra hai. Ab "hello how are you" sirf 4 shortcuts hai. Kitna fast!

Huge dictionary (200,000 shortcuts): Tumhare paas poore phrases ke shortcuts hain jaise "hello how are you doing today". Super fast! Lekin ab tumhare phone ko 200,000 shortcuts memory mein store karne pad rahe hain. Yeh bahut space leta hai.

The tradeoff: Chhoti dictionary = BAHUT SAARE messages bhejne padte hain (slow). Huge dictionary = tumhara phone shortcuts se bhar jaata hai (memory khaata hai). Tum "goldilocks" size chahte ho: itne shortcuts ki fast ho, lekin itne nahi ki phone mein space na rahe.

AI models ke liye, "messages bhejna" aise hai jaise computer har token ko process karta hai. Zyada tokens = zyada kaam (especially kyunki computer ko har token ko baaki har token se compare karna padta hai, jo bahut slow ho jaata hai). Lekin shortcuts store karna (vocabulary) bhi memory leta hai. Toh hum ek size chunte hain jo task ke liye "just right" ho!

Concept Map

larger V shortens

scales linearly

drives

quadratic with n

grows

affects

more per token

too large hurts

balanced by

balanced by

constrains

Vocabulary size V

Sequence length n

Embedding table V x d

Attention cost O n squared d

Rare word generalization

Information density per token

Goldilocks tradeoff

Inference compute

Parameter memory