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().
How one character gets made
Seven steps per character. JavaScript does three of them and none of the arithmetic.
-
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 … s7t14means "token 1 at position 0" and so on. -
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,
--x0to--x23. -
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. -
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. -
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 withvar(), the same way, to make 13 logit colours. -
JS
Read back.
One
getComputedStyleand 13getPropertyValuecalls. Reading forces Chrome to resolve every expression in one style recalc. That's the whole forward pass. -
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
@propertycolours) - 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
-
Chrome keeps colour channels as unclamped floats.
color(srgb -4.8 7.12 52.5)survives a round trip, so a colour is a three-number register. Only alpha is clamped to 0 to 1, which makes rgba really rgb: three lanes, not four. - CSS costs about 1.6 µs per weight per character, flat. From 21K to 670K weights it stays linear. Nothing broke: a 14 MB stylesheet loaded in under 2 seconds. A one-million-weight model would be about 1.6 seconds per token.
- A JavaScript loop is 85 to 770 times faster in this test. The live race above can land higher, since a warm loop over a 21K model barely touches memory. The gap closes as the model grows because the JS loop starts missing the CPU cache, not because CSS gets better.
-
Every weight is about 30 bytes of text. A float, a multiply sign and a share of the
surrounding
calc(). Ternary weights would shrink each term to+ r,- gor nothing. - Precision is fine. Values come back with about 6 significant digits, and the logits match the JavaScript run to about 0.005.
Gotchas found on the way
-
tanh()isn't a CSS maths function. Using it makes the whole declaration invalid, and the colour silently falls back to black.exp(),pow(),sqrt(),max()andclamp()work. color-mix()premultiplies by alpha, so alpha has to stay at 1 or it scales the numbers.-
The vocabulary has 37 characters, which isn't a whole number of colours. The first version only had room for
36 logits, so it could never write
&. Now it pads to 39 with logits that can't win. - Tested in Chrome only. Safari has relative colour syntax, but nobody has checked whether it keeps out-of-range channels.