4.2.10 · HinglishTokenization & Language Modeling

Context window and sequence length

1,852 words8 min readRead in English

4.2.10 · AI-ML › Tokenization & Language Modeling


YEH HAI KYA?

WHY distinction matter karti hai: sequence length tumhari choice hai per request; context window model ki hard ceiling hai. Inhe confuse karne se silent failures hoti hain jahan model lambi document ki shuruat "bhool" jaata hai.


LIMIT HAI KYO BILKUL?

Yeh limit arbitrary nahi hai — yeh self-attention cost se aati hai.

Cost ko scratch se derive karna

Step 1 — comparisons count karo. Length ki ek sequence ke liye, attention ek attention matrix banata hai jahan entry = token kitna token ko attend karta hai.

Yeh step kyun? Kyunki attention sabhi positions pe ek weighted sum ki tarah define hota hai, isliye har (query, key) pair ka ek weight chahiye.

Entries ki sankhya hai:

Step 2 — banane ki cost compute karo. Har entry dimension (model width) ke do vectors ka dot product hai. Ek dot product multiply-adds cost karta hai. entries hain, isliye:

Step 3 — memory. Softmax apply karne ke liye full attention matrix store karni padti hai. Iske entries hain (har ek ek single scalar weight hai), isliye: (Alag se, Q/K/V tensors sirf cost karti hain — mein linear. Dominant, quadratic term woh attention matrix hai.)


MODEL "POSITIONS" KAISE JAANTA HAI?

Agar attention sirf tokens pe sum kare, toh order kho jaata hai ("dog bites man" = "man bites dog"). Isliye hum positional information add karte hain.

WHY yeh purane models mein context cap karta hai: classic learned/sinusoidal position tables ek fixed length tak banaye gaye the. Position maango aur uske liye simply koi encoding nahi → model sharply degrade ho jaata hai. Modern tricks (RoPE, ALiBi, interpolation) ise extend karte hain, lekin trained window hi woh jagah hai jahan quality guaranteed hai.


Figure — Context window and sequence length

Worked examples


Common mistakes (steel-manned)


Active recall

Recall Quick self-test (answers chhupao, pehle forecast karo)
  • Sequence length aur context window mein kya fark hai?
  • Attention compute kyun hai lekin attention memory ?
  • Agar 1k → 4k jaaye, kitna zyada attention compute?
  • Prompt kahan khatam hota hai aur output budget kahan shuru hota hai?

Answers: seq length = current input tokens (tumhari choice, ceiling); context window = trained max (fixed). Compute mein har pairwise dot product se extra aata hai; stored weights single scalars hain isliye memory sirf hai (plus Q/K/V ke liye ). 16×. Dono same window share karte hain: .

Recall Feynman: ek 12-saal ke bachche ko samjhao

Socho tum ek story ek paper tube se padh rahe ho — ek baar mein sirf kuch words dikh sakte hain. Context window matlab tumhari tube kitni chaudi hai. Agar story tube se lambi hai, toh shuruat view se bahar khisak jaati hai aur tum usse bhool jaate ho. Zyada dekhne ke liye, tumhe chaudi tube chahiye — lekin tube ko do guna chauda karna tumhari aankhon ke liye chaar guna kaam hai, kyunki ab tumhe har word ko har doosre word se compare karna padta hai jo tum dekh sakte ho.


Flashcards

Ek language model ka context window kya hota hai?
Woh maximum number of tokens jo model ek baar mein process karne ke liye built/trained hua hai — ek fixed architectural ceiling ().
Sequence length vs context window kya hai?
Sequence length = current input mein tokens (tumhari per-request choice); context window = woh fixed maximum jo model support karta hai. Requirement: .
Self-attention compute kyun hai?
Har token har doosre token ko attend karta hai ( query–key pairs) aur har pair ek -dimensional dot product cost karta hai, jo deta hai.
Self-attention memory kyun hai ( nahi)?
Stored attention matrix har (query,key) pair ke liye ek scalar weight rakhti hai — scalars. factor sirf compute mein aata hai; Q/K/V storage alag term hai.
Agar context length 2048 se 8192 jaaye, kitna zyada attention compute?
.
Kya context window sirf prompt rakhta hai?
Nahi — prompt tokens AUR generated output tokens ek hi window share karte hain: .
Kya context windows words mein measure hote hain?
Nahi, tokens (subword units) mein; English ~1.3 tokens/word hai, code/doosri languages ke liye zyada.
"Lost in the middle" effect kya hai?
Window ke andar bhi, models shuruat aur ant ki information ko beech ki information se kahin behtar use karte hain.
Purane models apni trained length ke baad kyun fail ho jaate hain?
Positional encodings sirf tak exist karti hain; usse aage ki positions ka koi learned/defined encoding nahi hota, isliye quality collapse ho jaati hai.
6000-word English essay roughly kitne tokens ki hogi?
Lagbhag tokens.

Connections

  • Tokenization — window tokens mein count hoti hai, isliye tokenizer efficiency directly affect karti hai ki kitna text fit hoga.
  • Self-Attention — woh mechanism jo limit banata hai.
  • Positional Encoding — RoPE/ALiBi/interpolation decide karte hain ki window kitni extend ho sakti hai.
  • Transformer Architecture — jahan model width aur depth ke saath interact karte hain.
  • Truncation and Chunking — practical strategies jab ho.
  • KV Cache — stored keys/values ki memory bhi generation ke dauran sequence length ke saath badhti hai.

Concept Map

must satisfy L <= n_ctx

counts

hard ceiling set at

every token compares all

L^2 dot products of dim d

store scalar weights

combine into

combine into

doubling context quadruples cost

forces

Sequence length L

Context window n_ctx

Tokens

Self-attention

Attention matrix L x L

Compute O of L^2 times d

Memory O of L^2

Quadratic law

Finite context limit

Truncate chunk or summarize