~/fba-lab/lab/qwen-c/05-tokenizer

FBALab

Code · architecture · study mode

AboutRoadmapSpeedrun
FBALab

Study mode — no GPU required.

Interactive LLM training & inference lab.

Qwen CAboutContactTermsPrivacyCookiesCommunity

© 2026 FBA Lab

Contact · contact@bubblspace.com · +91 75061 55016

INFERENCE SIMULATION

Qwen3 Pure C — 5 · Tokenizer

running
◷run_viz.c▸load_vocab()◎learner⌁05-tokenizer$no GPU
weights
Load the model
The 4 GB weight file is mapped into memory in milliseconds. No copying — the OS pages in only what inference needs.
run_viz.c · stage 01–02
load
runtime
Run inference
Your prompt flows through 28 layers of attention and computation, one token at a time.
stage 03–04
forward
active
output
Generate text
The model scores all 151,936 possible next words and picks one. Then repeats.
stage 05–06
1/6
Blocks
Quick summary

The model never sees words or letters — only integers. The vocabulary is the lookup table that maps every possible token (a text chunk, usually 3-4 characters) to a unique integer ID and back.

Full explanation below the code →

fba-lab — run_viz.c · load_vocab()executing
$ load_vocab("vocab.txt")151936 token strings → IDsvocab table loaded ✓
Explanation

What is load_vocab()? When you type "Hello" to the model, it doesn't process letters H, e, l, l, o. It processes token IDs — integers. A token is a chunk of text that the model treats as an indivisible unit. "Hello" might be a single token (ID: 9906) or split across two tokens depending on context. The vocabulary is the complete list of all 151,936 possible tokens, each assigned a permanent integer ID.

Why does this exist? The model only does math — matrix multiplications, additions, softmax. It can't multiply a letter. Token IDs are the bridge: each integer maps to a row in the embedding table (a vector of 1024 floats) that the model can actually process.

What load_vocab() does: It reads vocab.txt, where each line is one token string. Line N becomes token ID N. The reverse is trivial: vocab[token_id] returns the string. The forward direction (string → ID) is more complex and happens during encode().

What's in Qwen3's vocabulary: - Regular subword tokens (common word pieces like Ġhello, world) - Special control tokens like <|im_start|> and `` that structure the conversation - Byte-level fallback tokens for rare characters - Characters from many languages

The critical constraint: vocab_size in Config must exactly match the number of lines in vocab.txt. A mismatch means the embedding table (which has vocab_size rows) and the logits vector (which has vocab_size scores) are sized differently from what the tokenizer produces — silent corruption.

Why it matters

Token IDs are the integer bridge between UTF-8 text and floating-point embeddings.

Think about

Why does the model use token IDs (integers) instead of processing characters or words directly?

// architecture

Live diagram

100%
Text ↔ token IDsvocab.txtmerges.txtbyte mapbuild_tokenizerUTF-8 textpretokenizeBPE mergestoken IDsencode()prompt → IDsdecode_token_id()ID → UTF-8
← 4 · Forward pass6 · Generate loop →
Step 5 of 10Skill: The Forward Pass
01Blueprint02Load weights03Buffers04Forward pass05Tokenizer06Generate loop07GEMV, not GEMM08Every op a kernel09Attention & memory10Prefill vs decode