tinyGPT Gym

See how a language model learns from a training document: text becomes tokens, tokens become prediction examples, and each training step nudges the model toward better next-token guesses.

Back to gym index
Loading tinyGPT artifacts...
You start with raw AI-book text; this tab turns it into the vocabulary and token IDs tinyGPT can learn from.
Act 1 — The problem → Act 2 — BPE builds the vocabulary → Act 3 — Text becomes IDs
Training document

Training Document: AIClub Introduction to Artificial Intelligence

This fixed AI textbook corpus is the only text tinyGPT learns from in this gym.

            

Act 1 — The problem: words or characters, both break

Two approaches to tokenizing the same sentence — and why each falls short.
Approach A — whole words
One token per word
Easy to read, but the vocabulary explodes. Every name, typo, and domain term needs its own entry. Unseen words fail at test time.
7 tokens
but vocab can reach 500k+ words
vs
▾
subwords
balance both
Approach B — characters only
One token per character
Tiny vocabulary, no unknowns — but every sentence explodes into dozens of pieces. The context window fills fast.
47 tokens
for one sentence alone

Act 2 — BPE builds the vocabulary

Byte-level BPE repeatedly merges common adjacent pieces, then uses that learned vocabulary to encode the selected snippet.

Vocabulary is learned from repeated text patterns.

The tokenizer starts from bytes, finds frequent neighboring pieces, creates merged tokens, and eventually represents the AI book as a shorter stream of token IDs.

1 Count neighbors Scan the training text and count adjacent token pieces.
2 Add one piece The most repeated pair becomes a new vocabulary token.
3 Replace everywhere Every occurrence is re-written using the new shorter piece.
Current merge candidate
Where this appears in the document
step 1 120 merges
As BPE adds vocabulary pieces, the same document is represented with fewer token pieces.

Act 3 — Text becomes a stream of IDs

The learned vocabulary encodes the selected text as colored token blocks with numeric IDs underneath.
So what? Notice common AI-book phrases can become compact tokens, while rarer wording may split into smaller pieces. BPE reflects what is frequent in this specific corpus.
You now have a stream of token IDs; this tab turns that stream into fixed-size X and y batches so the model has inputs and next-token answers.
Act 1 — One window: x and y → Act 2 — Stack into a batch → Act 3 — Train / val split

How the token stream becomes training examples

A training step samples 32 windows from the encoded document. Each window is 128 tokens wide and becomes one row in the batch.
One window of 5 tokens from the stream gives one training example — automatically, no human labeling needed.
stream
668 664 143 106 1053
input x
668 664 143 106 —
target y
— 664 143 106 1053
y is x shifted one position to the right — 4 free prediction tasks from 4 tokens.
1. Encode documentThe AI book becomes one long token stream.
2. Pick windowsChoose random start positions in the train split.
3. Stack rowsEach 128-token window becomes one row of X.
4. Provide labelsy is the next-token answer sheet, shifted one step ahead.

One batch is a 32 x 128 grid of token IDs.

The model sees 32 independent samples at once. Each sample is a fixed-length row of 128 real tokens. The target row y is also 128 tokens: one next-token answer for each X position.

Train
Val
You have a 32 x 128 batch of X and y tensors; this tab shows what happens when those tensors enter GPT.forward() and how 3,000 training steps produce a working model.
Act 1 — Loss: measuring how wrong → Act 2 — The training loop → Act 3 — Validation and perplexity

Inside the forward pass (Tab 2 → Tab 3 connection)

Click any step to see the Python code and tensor shapes. Embedding dimension is 256 — every token becomes a 256-value vector.
idx / X [32, 128] Input token IDs. The model receives this tensor. feeds GPT.forward
targets / y [32, 128] Not an input to the forward pass; used only when cross-entropy computes loss. loss only
You now have trained weights; this tab uses the tokenizer and forward pass to turn a prompt into last-position logits, sample one token, and feed it back as generated text.

Generate With The Trained tinyGPT Checkpoint

Runs the converted PyTorch model in the browser. No backend, no server state, no browser training.
Ready. The ONNX model loads on the first generation.

Generated Text

Each new token comes from the last-position logits over the 6,090-token vocabulary.

              
Why last-position logits? Because of the causal mask you saw in Tab 3, position 127 has seen all 128 input tokens, so its logits are the only ones used to predict the next token.

Real Inference Path

This is the honest path to a prompt box that behaves like the Colab notebook.
ids = tokenizer.encode(prompt)
idx = ids[-128:] # block_size context
logits = tinygpt.onnx(idx)
next_id = sample_top_k(logits[-1], temperature, k)
ids.append(next_id)

Next-token Candidates

The latest model call exposes the highest-probability next tokens before sampling.

Browser Model

Converted from the supplied PyTorch checkpoint.