Transformer Math · Authored by Sandeep Belgavi

End-to-End Recipe: Train, Export, Validate, Serve

Run a bounded two-token bigram language-model workflow with actual fitting, held-out loss, quantized serialization, reload and a serving gate.

Experiment

Diagram labels stay full size. On narrow screens, scroll the diagram horizontally or focus it and use the arrow keys. Exact values follow below.

Paused · 0 / 60 teaching seconds

The timeline reveals computed states. Its sixty seconds describe presentation, not hardware speed.

End-to-End Recipe: Train, Export, Validate, Serve: experiment guide

This end-to-end workflow executes a miniature language-model recipe entirely in the browser: tokenize data, fit a conditional model, evaluate held-out transitions, quantize and serialize its table, reload and validate, then generate tokens when the validation gate passes. The executable model is a two-symbol bigram model, not a transformer or a CPU SLM training implementation. It is small enough that every count, probability and exported code can be verified. The distinction matters: the workflow teaches why artifacts must be evaluated after conversion; it does not assert that clicking a diagram launches PyTorch, creates GGUF or starts a server. The artifact has the explicit local format name math-helper-bigram-v1.

Worked details 1

Training corpus selects one of three fixed six-token sequences shown directly in the dropdown. Tokenization splits at spaces and maps a to zero and b to one, with no hidden vocabulary fitting or end-of-sequence marker. Only the five within-sequence adjacent pairs contribute training counts. The held-out sequence is always a a b b a, giving four scored transitions; it is never added to the training table. Additive smoothing alpha ranges from zero to two. Each conditional probability is (count+alpha)/(row total+2 alpha). An unseen context with alpha zero would receive an explicitly defined uniform fallback, although the supplied corpora include both contexts. This is count estimation, not stochastic gradient descent.

Worked details 2

Probability code bits chooses one through eight bits for each of four unsigned codes. With b bits the largest code is 2 to the b minus one. Each fitted probability is multiplied by this largest code and rounded to the nearest integer, with exact half cases rounded upward. Codes are serialized to JSON with the vocabulary, bit width and format tag. Reloaded rows are normalized by their integer row sums before evaluation or generation. Consequently the post-export distribution need not equal the fitted distribution. Integer code payload is four times the bit width; it excludes JSON text, vocabulary and all format overhead. The browser does not write a GGUF checkpoint or claim those four codes represent a practical transformer.

Worked details 3

Validation loss limit is the largest allowed mean held-out negative log probability in natural-log units, or nats. The gate accepts equality and requires a finite loss. An impossible held-out transition gives infinite loss and infinite perplexity and blocks serving regardless of a large finite limit. The table and inspector show probabilities and per-transition losses before and after quantization. Perplexity is the exponential of the mean loss. Prompt token chooses a or b, while Generated token count selects one through twelve additional tokens. Generation uses the reloaded distribution, takes the highest-probability next token and breaks exact ties in favor of a. These two controls affect serving only after the gate passes, and do not change fitting or validation.

Worked details 4

Reset parameters chooses alternating training data a b a b a b, alpha one, two code bits, loss limit one, prompt a and four generated tokens. The count rows are [0,3] and [2,0]. Fitted probabilities are [1/5,4/5] and [3/4,1/4]. Quantization produces code rows [1,2] and [2,1], so the reloaded rows are [1/3,2/3] and [2/3,1/3]. Each held-out transition type occurs once. Original loss is approximately 0.876639 nats; post-export loss is approximately 0.752039 nats, with perplexity approximately 2.121320. The gate passes and the full sequence is a b a b a. This tiny fixture happens to improve held-out loss after rounding; it is not evidence that quantization generally improves quality.

Worked details 5

For a failure experiment, keep the alternating corpus but set alpha to zero. Self-transitions receive probability zero, and the held-out a-to-a and b-to-b pairs make evaluation infinite. Serving is blocked and no extra tokens appear. Reset and choose one code bit: the rounded rows again become deterministic alternation and fail the same boundary despite smoothing before export. Reset and lower the loss limit to 0.7 to block a finite but unacceptable artifact. Raise it to one and switch the prompt to b to obtain b a b a b. Try the other training corpora and compare counts and held-out losses, holding the validation data fixed. A single tiny validation sequence does not establish generalization or justify tuning many choices against it.

Worked details 6

Play reveals six computed workflow stages over sixty teaching seconds. Pause freezes the current view, Next stage advances one step, Seek pauses at a chosen time, and Restart returns to tokenization while retaining inputs. Speed selects quarter, normal or four times the teaching presentation rate. Model edits recompute all dependent artifacts and pause; Reset parameters restores the defaults. Generation text is shown at the final stage, with the complete result also available in the inspector. Tables remain inspectable from the beginning. Blank, fractional count or out-of-range parameters produce an error and disable playback. The timeline is an explanation aid, not a training duration, benchmark, asynchronous deployment or production readiness claim.

Views are my own and do not represent any employer, past or present.