Training Tiny Models on a Google TPU for the Browser: Without the Magic

4 min read

  • #ai
  • #jax
  • #tpu
  • #typescript
  • #colab
  • #flow-matching
On this page
  1. Why pure TypeScript instead of ONNX or Transformers.js?
  2. Model 1: A 15.7M-parameter LLM pretrained on FineWeb-Edu, fine-tuned on this blog
  3. Model 2: A 1.29M-parameter cyberpunk flow-matching U-Net
  4. What this proves

I wanted to know what happens when you stop calling an API and actually train the models yourself.

Not a 70-billion-parameter model behind a billing key. Not a 200 MB WebGPU download that stalls your browser tab. Two tiny neural networks written in pure JAX, trained in bfloat16 on a Google TPU v5e in Colab, quantized to Int8, and shipped as static files right next to this post.

No server. No API key. No WASM runtime. Just Float32Array math running inside your browser tab.

Why pure TypeScript instead of ONNX or Transformers.js?

This site runs a strict Content Security Policy (default-src 'self', no 'wasm-unsafe-eval', no third-party script CDNs). That immediately rules out onnxruntime-web and transformers.js unless I poke holes in the security headers.

So I didn’t. Instead, I constrained both architectures to operations you can write in five lines of TypeScript: matrix multiplication, RMSNorm, RoPE, SwiGLU, 3x3 convolutions, GroupNorm, nearest-neighbor upsampling, and Euler integration.

The payoff is two-fold. The weights load fast, and every intermediate step stays small enough to inspect live on the page.

Model 1: A 15.7M-parameter LLM pretrained on FineWeb-Edu, fine-tuned on this blog

In What Claude Is Actually Doing: LLMs Without the Magic, I used hand-authored numbers to show how next-token prediction works. This time, the widget below is an actual Transformer.

My first attempt trained directly on my 18 blog posts alone (254,189 characters, 42,790 words). Predictably, a model that has only ever seen 42k words cannot learn English grammar — it just memorizes fragments and outputs gibberish.

So I switched to the real two-stage recipe in JAX on a TPU v5e:

  1. Pretraining: 9,000 steps (bfloat16) on 14.6M tokens from FineWeb-Edu (filtered to clean sentence-ending documents) so the 8-layer Transformer (d_model=384, 6 heads, 4,096-token BPE, RoPE, RMSNorm, SwiGLU, weight tying) learns real expository English. Loss dropped from 8.38 to 2.64.
  2. Fine-tuning: 120 gentle steps on my own 68k blog tokens so it picks up my vocabulary and phrasing.

The weights export to 15.0 MB in symmetric per-tensor Int8.

Play with it. Pick a prompt, adjust the token count or pace slider to watch each step happen, or step through one token at a time to inspect the model’s top-5 candidates:

Live Browser Model · 15.7M Params (15.0 MB Int8)
The model

Notice two honest things about what you just saw:

  1. 15.7M parameters is still tiny. It now writes complete, grammatical sentences and holds a coherent thought for a sentence or two before drifting. That is what 15 MB of weights looks like without a giant retrieval layer.
  2. A single forward pass takes ~36 ms in plain JS. Quantizing from fp32 to int8 shifts the final logits by a mean absolute difference of 0.065 against JAX — completely negligible for sampling, with zero WASM or server backend.

Model 2: A 1.29M-parameter cyberpunk flow-matching U-Net

Autoregressive text models predict one discrete token at a time. Image models work differently: they learn a continuous vector field from pure Gaussian noise (t = 0.00) to a finished image (t = 1.00).

Instead of a classic DDPM noise schedule, this second model uses Rectified Flow Matching. During training, you linearly interpolate between random noise x0 and a target 32x32 sprite x1:

x_t = (1 - t) * x_0 + t * x_1
target_velocity = x_1 - x_0

A 1.29M-parameter conditional U-Net (base=48) learns to predict that velocity given (x_t, t, class_id) across 6 procedural cyberpunk classes (visor, skull, drone, chip, katana, terminal), trained for 80 epochs on 24,000 generated sprites on the TPU v5e (mse 0.0552, with EMA weights and 10% unconditional dropout for Classifier-Free Guidance).

The exported Int8 weights weigh 1.2 MB.

Pick a class and hit Generate sprite, then drag the trajectory slider back and forth to watch how noise turns into neon pixels over 16 Euler steps:

Live Browser Model · 1.29M Params (1.2 MB Int8)
t = 0.00 (step 0/16)

Each Euler step runs two U-Net passes (conditional + unconditional for CFG), and 3x3 convolutions on CPU in plain JS take ~490 ms per pass — so 16 steps take a few seconds on your CPU. Zero GPU, zero external runtime, and you can scrub every single step of the ODE trajectory.

What this proves

Big models feel mysterious because their scale hides the plumbing. Train a tiny one on a TPU yourself, export the weights as raw int8 bytes, and write the forward pass in plain TypeScript: suddenly there is nowhere for the magic to hide.

An LLM is still just linear algebra, normalization, and a weighted die over a vocabulary. A diffusion model is still just a small U-Net nudging noise along a learned direction, sixteen times in a row.

Now you can run both in your own browser tab.