Gradient Decent / cards

Can CSS transforms multiply matrices?

The browser's 3D transform engine as a calculator. A tiny Shakespeare-babbling model whose two matrix multiplies are done by the part of the browser that tilts and slides elements: every pair of neurons is a card with a long chain of transforms, and the answer is how far the card ends up tilted.

Yes. A transform list is a product of 4×4 matrices, and with the right matrices that product is a running sum of input × weight. Chrome multiplies each list out and hands back one matrix3d() with two neurons in it. It matches a plain JavaScript run of the same weights to about and picks the same top character every time. It is also around a thousand times slower than the JavaScript loop, and it can't do the ReLU: that is one CSS max() per number, with a script carrying the numbers from the first wall of cards to the second.

Run it

Hidden layer: 192 neurons on 96 cards. Each card's left-to-right tilt is one neuron and its top-to-bottom tilt is the next.

Logits: 37 characters on 19 cards, two to a card. The left character is the left-to-right tilt, the right one is the top-to-bottom tilt. A card tipping steeply away is a character the model has ruled out.

The cards aren't a picture of the maths drawn afterwards. Each card's transform is the calculation, and the script reads the result out of that same property. What you see is flattened so it stays on screen: the walls show 15% of the real tilt for the hidden layer and 4% for the logits (a scaleZ() on the wall, which doesn't change what the script reads), and both walls lean back a little so the tilts are easier to see. Each wall also drifts towards you by the sum of its inputs, which is a by-product.

Race it in your browser

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

Cards, per character–
JavaScript, per character–
Cards are slower by–
Same top character / largest logit gap–

How a transform list adds up input × weight

Every matrix in every list has the same shape. It leaves x and y alone, slides the card towards you by a, and has two numbers p and q in the bottom row, the one normally used for perspective. Multiply two of them and the first one's a gets multiplied by the second one's p and q, and lands in the row that sets how z depends on x and y. That is a tilt.

│ 1  0  0  0 │   │ 1   0   0  0  │   │ 1     0     0  0    │
│ 0  1  0  0 │ × │ 0   1   0  0  │ = │ 0     1     0  0    │
│ 0  0  1  a │   │ 0   0   1  a' │   │ a·p'  a·q'  1  a+a' │   ← tilt, and depth
│ p  q  0  1 │   │ p'  q'  0  1  │   │ p+p'  q+q'  0  1    │

Keep multiplying and the tilt keeps adding: each new matrix contributes (everything in the a slots so far) × (its own p and q). So the inputs go in the a slots as var()s, and the p, q slots hold constants chosen so each input ends up multiplied by its own weight. Since an input meets every p after it, those constants are the differences between neighbouring weights, worked out when the page is built. The bias is one more input whose value is always 1. At the end the bottom row has cancelled back to zero and the matrix is a plain tilt: z = s₁·x + s₂·y, where s₁ and s₂ are two neurons.

How one character gets made

  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 nine custom properties for one token at one position. The 8 positions × 9 numbers are the 72 inputs, --x0 to --x71. This is a selector lookup, not transform maths.

  3. CSS
    Layer 1: 72 inputs to 192 neurons, by transforms.

    Each of the cards has a transform of matrix3d() functions: one for the bias, one per input, one to close. Chrome multiplies the list into a single matrix. All 13,824 multiplies and the adds, biases included, happen in that product.

  4. JS
    Carry 192 numbers across.

    Read each card's computed transform, which comes back as one matrix3d(…), cut out the 3rd and 7th numbers (the two tilts), and set them as --h0 to --h191 on the second wall. The text is copied as it is; the script does no arithmetic on it.

  5. CSS
    ReLU.

    Layer 2 uses each number as max(0, var(--h5)). That is a CSS maths function, not the transform engine. A transform list is linear all the way through, so it can't clamp anything, and this is why the two layers can't be one long list.

  6. CSS
    Layer 2: 192 neurons to 37 logits, by transforms.

    The same trick again: cards of matrices each, two logits to a card (the 38th is a spare).

  7. JS
    Read back and sample.

    Read the 19 computed transforms, take the two tilts from each as numbers, 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 nine inputs */
.s0t12 { --x0:-1.965094; --x1:.421589; --x2:-.557773; --x3:.622969; --x4:-.054166; --x5:-.485587; --x6:.088103; --x7:-1.000168; --x8:1.369296; } /* "k" */

/* the first card of layer 1: 74 matrices, of which the first four and the last are */
#w1 i:nth-child(1) { /* outputs 0 and 1 */
  transform:
    matrix3d(1,0,0,.647662,0,1,0,.573428,0,0,1,0,0,0,1,1)
    matrix3d(1,0,0,-.91647,0,1,0,-.601982,0,0,1,0,0,0,var(--x0),1)
    matrix3d(1,0,0,.184715,0,1,0,.05994,0,0,1,0,0,0,var(--x1),1)
    matrix3d(1,0,0,-.217795,0,1,0,-.036069,0,0,1,0,0,0,var(--x2),1)
    …
    matrix3d(1,0,0,-.594731,0,1,0,-.492266,0,0,1,0,0,0,0,1);
}

/* the first card of layer 2 starts like this */
#w2 i:nth-child(1) { /* outputs 0 and 1 */
  transform:
    matrix3d(1,0,0,.788217,0,1,0,-.294879,0,0,1,0,0,0,1,1)
    matrix3d(1,0,0,-1.235272,0,1,0,.17394,0,0,1,0,0,0,max(0,var(--h0)),1)
    …

Real lines from this page's stylesheet. matrix3d() lists its 16 numbers column by column, so p and q are the 4th and 8th and a is the 15th. The two layers are matrices in KB of CSS for weights. View the page source to read all of it.

JS no arithmetic until the softmax

  • Characters to class names
  • Copy 192 numbers from the first wall's computed transforms into variables on the second, as text
  • Read 37 logits from 19 computed transforms
  • Softmax, temperature and sampling
  • The loop, one character every 16 ms

CSS the matrix maths

  • Transform engine: every multiply and add in both layers, and both bias vectors
  • Transform engine: the cards you see, which are the same elements
  • Class selectors: the embedding lookup
  • max(): the ReLU
  • Build time, in Python: the differences between neighbouring weights

What it says

Gotchas found on the way