Gradient Decent / css

Can a stylesheet run a language model?

The browser's style engine as a calculator. A tiny model that babbles lowercase Shakespeare one character at a time, where the whole forward pass is CSS colour maths: every number lives in a colour, every multiply is a relative colour and every sum is a color-mix(). A goofy experiment to see whether a stylesheet could do it at all.

Yes, and it gets the maths right: it matches a plain JavaScript run of the same weights to about 0.005, and picks the same top character every time. It's just very slow. The browser's style engine is hundreds of times slower than a plain JavaScript loop (85 to 770 times in the scaling test below, and about 1,000 times in the race on this tiny model; the race measures your machine), and every weight costs about 30 bytes of stylesheet. The babble is the 21,000-weight model's fault, not the CSS's.

Run it

Hidden layer: 192 neurons as 64 colours, live

Logits: 37 characters as 13 colours, live

Those squares aren't drawn by script. Each one's background is a CSS expression on the neuron's colour, so they change because the style engine just did the maths.

Race it in your browser

Runs 40 random 8-character contexts through both engines on this exact model: the stylesheet, and a plain JavaScript loop over the same weights from model.json. Same as window.selfTest().

CSS, per character–
JavaScript, per character–
CSS is slower by–
Same top character / largest logit gap–

How one character gets made

Seven steps per character. JavaScript does three of them and none of the arithmetic.

  1. JS
    Tokenise.

    Take the last 8 characters, look up each one's number in the 37-character vocabulary, and write them as class names on one div: s0t1 s1t20 … s7t14 means "token 1 at position 0" and so on.

  2. CSS
    Embed.

    Each of the class rules sets the colours for one token at one position. The 8 positions × 9 numbers come out as 24 colours, --x0 to --x23.

  3. CSS
    Layer 1: 72 inputs to 192 neurons.

    The weight matrix is cut into 3×3 blocks. Each block is a relative colour: take input colour i, and make a new colour whose red is r*a + g*b + b*c, and so on. That's a 3×3 matrix times a 3-vector, which is all a colour matrix is.

  4. CSS
    Sum.

    The blocks for one output colour are added with a binary tree of color-mix(). Mixing averages, so the weights are pre-multiplied by the number of leaves (32 here) and the average comes out as the sum. The bias is one more leaf.

  5. CSS
    ReLU, then layer 2.

    calc(max(r, 0)) on each channel. The result lands in a registered @property --h0 { syntax: '<color>' }, which the next layer reads with var(), the same way, to make 13 logit colours.

  6. JS
    Read back.

    One getComputedStyle and 13 getPropertyValue calls. Reading forces Chrome to resolve every expression in one style recalc. That's the whole forward pass.

  7. JS
    Sample.

    Softmax with temperature and pick one character. Then go back to step 1 with it on the end.

What the compiled stylesheet looks like

/* token 12 at position 0 sets three input colours */
.s0t12 { /* "k" */
  --x0: color(srgb -1.9651 0.42159 -0.55777);
  --x1: color(srgb 0.62297 -0.054166 -0.48559);
  --x2: color(srgb 0.088103 -1.0002 1.3693);
}

/* one 3x3 block of layer 1: input colour 0 -> output colour 0 */
color(from var(--x0) srgb calc(r*8.6019 + g*2.691 + b*9.6604) calc(r*0.91374 + g*-1.0043 + b*0.14985) calc(r*1.2529 + g*2.6966 + b*-4.2279)),

Real lines from this page's stylesheet. The two layers have blocks like that one, joined by color-mix() calls, in KB of CSS for weights. View the page source to read all of it.

JS about 20 lines

  • Characters to class names (the embedding table itself is CSS)
  • Read 13 computed colours back as numbers
  • Softmax, temperature and sampling
  • The loop, one character every 16 ms

CSS all of the maths

  • Embedding lookup (class selectors)
  • Every multiply and add in both layers (calc() in relative colours, color-mix())
  • Bias and ReLU
  • Passing activations between layers (registered @property colours)
  • The neuron swatches on this page

How it scales

The same two-layer shape with random weights, widening the hidden layer. Time per character, median of 15, headless Chrome 153 on CPU. JavaScript averaged over 1.5 seconds of runs. Log scale.

CSS   plain JavaScript loop

weights stylesheet CSS / char JS / char CSS slower by

What it says

Gotchas found on the way