Gradient Decent / stories

Can blend modes write a story?

A 2D canvas, the browser's drawing surface, used as a calculator. It runs tiny-tiny-stories, a one-million-weight story model whose weights are only ever +1, 0 or −1, so each weight is one pixel: red, black or green. Every matrix multiply is colours being blended together. No WebGPU, no WebGL, no WASM, just a goofy way to do the maths with a tool that was built for pictures.

Yes, at 20 to 30 tokens a second, and it gets the maths right: over a 60-token run it picks the same token as a plain numpy version of the model every time. The stories wobble because the model has a million weights, not because of the canvas. A plain JavaScript loop is still much faster; the race below measures by how much on your machine.

Run it

Give it the start of a children's story.

The activation function (SiLU), stored as a gradient. Red is positive, green is negative.

Every layer's weights multiplied by its current input, live.

Rotary positions, turned by CSS rotate. One clock per frequency, at token position 0.

query pairs before, after, key pairs after. Under each clock: tokens per full turn.

The tiles aren't a visualisation added afterwards. Each one is the working canvas for that multiply, copied to the page. The clocks are the same: every coloured dot is the element whose position gets read back as the rotated numbers. They show the last layer's pairs.

Race it in your browser

Feeds the same run of tokens through both engines: the canvas with CSS turning the positions, and a plain JavaScript loop over the same weights read back out of the pictures. They should pick the same next token at every position.

Canvas, per token–
JavaScript, per token–
Canvas is slower by–
Same top token / largest logit gap–

How one token gets made

36 matrix multiplies per token, every one of them on the canvas. The rotation that tells attention where each token sits is done by CSS transforms. JavaScript does the bookkeeping around them.

  1. JS
    Tokenise.

    The text is cut into pieces from a 192-piece vocabulary, choosing the split with the best total score (a SentencePiece unigram tokenizer).

  2. JS
    Embed and normalise.

    Look up the token's row of numbers, then scale it so its average size is 1 (RMSNorm). This is the input vector for the layer.

  3. Canvas
    Multiply.

    The input becomes a 2-pixel-tall strip of colours. It's stretched over the weight picture with the multiply blend mode, so every pixel becomes one weight times one input.

  4. Canvas
    Sum.

    The product is drawn at half its width, again and again, with smoothing on. Each halving averages neighbouring pixels, so when a row is one pixel wide that pixel is the row's average, which is the sum divided by the width.

  5. JS
    Read back.

    One getImageData on a 1-pixel-wide column returns every output of the multiply at once. Red plus green from the top half, minus the same from the bottom half, times a scale.

  6. CSS
    Rotate for position.

    Three of those multiplies give a query, a key and a value. The query and key are cut into pairs of numbers, and each pair is turned by an angle that grows with the token's position, at 16 different speeds (rotary position embeddings). Each pair is a zero-size element placed with translate inside an element with rotate set to that angle. Where getBoundingClientRect() says it ended up is the rotated pair.

  7. JS
    Attention.

    The rotated key and the value are cached. The query is compared with every cached key, the scores go through a softmax, and the values are mixed in those proportions. This is how it remembers earlier tokens.

  8. Canvas
    The gradient.

    The feed-forward part needs SiLU, a smooth curve. It's stored as a canvas linear gradient with 1,025 colour stops, rendered once. Applying it is reading the colour at the right position, like an eyedropper.

  9. JS
    Repeat, then sample.

    5 layers of seven multiplies each, then one more against the vocabulary picture to score every possible next token. Pick one, show it, and go back to step 1.

What one multiply looks like

strip.putImageData(input, 0, 0)                  // the vector, as a 2-pixel-tall strip
work.globalCompositeOperation = 'copy'
work.drawImage(weights, 0, 0)                    // the matrix, as a picture
work.globalCompositeOperation = 'multiply'
work.drawImage(strip, 0, 0, w, 2, 0, 0, w, h)    // every weight times its input, in one draw
for (half of halvings) half.drawImage(prev, 0, 0, pw, h, 0, 0, pw / 2, h)   // sum by squashing
answer = last.getImageData(0, 0, 1, h)           // one pixel per output

That's the real sequence from lib/blend.js, trimmed. Negative numbers can't be colours, so positives go in the red channel and negatives in green, and the strip is drawn twice (once swapped) so all four sign combinations get multiplied.

JS the bookkeeping

  • The tokenizer
  • Norms
  • Attention scores and softmax over the cache
  • Adding results back into the running vector
  • Sampling

Canvas every matrix multiply

  • Query, key, value and attention output
  • Gate, up and down in the feed-forward part
  • The output head over the whole vocabulary
  • The SiLU curve, as a gradient
  • The live tiles on this page are those products

CSS the rotary positions

  • Turning every query and key pair by its position angle, with rotate and translate
  • The dots on the clocks are those pairs

The weights themselves

One pixel is one weight: red is +1, green is −1, black is 0. The head is full precision, so its picture has shades. Every picture in a row is drawn at the same scale. Click one to see it up close.

What it says

Gotchas found on the way