Topic 7
Module 01 capstone: GPT-2 in your browser
This is the end of Module 01. You have read GPT-2's numbers in tables and labs. Now you run the model yourself, in your own browser, and use everything from the six lessons on prompts you choose.
Predict first
Your own GPT-2
Runs entirely in your browser · Xenova/gpt2 (quantized)
This downloads GPT-2 small (about 128 MB, quantized to 8 bits) from Hugging Face's servers, once, and runs it on your device with WebAssembly. Your prompts never leave the browser. On a phone or a slow connection, this may take a while.
The capstone tasks
Keep a short notes file as you go. For each task write the prompt, the settings (temperature, top-k, top-p), the numbers you saw on screen, and one or two sentences of explanation.
- Count tokens. Type a sentence with a rare word, a number and your own name. How many tokens does the tokens view show, and where did words split? (Lesson 1.)
- Make greedy loop. With temperature 0 (greedy), find a prompt whose continuation repeats itself. Note the repeated phrase, and compare a repeated token's probability the first and second time it appears. (Lesson 4.)
- Catch a hallucination. Find a factual prompt where GPT-2's top choice is wrong. Write down the top-5 probabilities and where the right answer ranks, if it appears. Was the wrong answer's probability high or low? (This lesson.)
- Ground it. Put one true sentence in front of your task 3 prompt. Does the right answer move up? By how much? (This lesson.)
- Explain a temperature change. Run one prompt at temperature 0 and again at 1 or higher. Find the first token where the two outputs differ. The bars only show the last step, and a new sampled run will not repeat the old one. So paste the prompt plus the tokens both runs share into the prompt box, set New tokens to 1 and temperature to 1, and generate: the bars now show that exact step. Use them to explain which rank each run picked, and with what probability. (Lesson 4.)
- Explain the whole path. In three sentences, explain how a prompt becomes text, using the words token, embedding, attention, logits and sampling. (Lessons 1 to 5.)
What "done" looks like
- Every task has a prompt, its settings, and numbers copied from the screen, not from this lesson.
- Task 3 names the wrong answer, its probability and the right answer's rank, and says whether the probability gave any warning.
- Task 5 points at one specific step and one specific candidate bar.
- Task 6 uses all five words correctly and in the right order: text → tokens → embeddings → attention layers → logits → sampling → next token, repeated.
Quick check
Summary
What Module 01 covered, from prompt to text:
- Tokenization. Models read token IDs, not text. Byte-pair encoding builds a vocabulary of subwords, so token counts depend on the tokenizer and the language. Count tokens before you send them.
- Embeddings. Each token ID selects one learned row of an embedding table. Vectors are compared by direction and distance, and similarity tracks topic, not truth.
- Attention. Each token builds a query, key and value; attention mixes information from earlier tokens (the causal mask) into each position, across many heads and stacked transformer blocks. Its cost grows with the square of the sequence length.
- Sampling. The last position's vector becomes one logit per vocabulary token; softmax gives probabilities. Generation is a loop of pick, append, repeat. Greedy, temperature, top-k and top-p decide how to pick.
- Context windows and the KV cache. The context window is a hard limit on positions, shared by the prompt and the output, and position information tells the model the order of the tokens. Generation splits into one parallel pass over the prompt (prefill) and one token at a time (decode). The KV cache keeps each layer's earlier keys and values so decode computes only the new token's, at a memory cost that grows with every token.
- Limits. The model is trained for likely text, not true text. It has no lookup, its knowledge is frozen, it copies patterns, and it builds on its own errors. Probability is a signal, not a guarantee. Grounding, constrained output, abstention and evaluation reduce hallucination; nothing in this module removes it.
Next
Module 02, Inference and Model Optimization, is in development. It picks up where lesson 5 left off: prefill versus decode, latency and throughput, quantization (the reason the browser model's numbers differ), batching and the cost of each request.