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().
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
-
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 nine custom properties for one token at one position. The 8 positions × 9 numbers are the 72 inputs,
--x0to--x71. This is a selector lookup, not transform maths. -
CSS
Layer 1: 72 inputs to 192 neurons, by transforms.
Each of the cards has a
transformofmatrix3d()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. -
JS
Carry 192 numbers across.
Read each card's computed
transform, which comes back as onematrix3d(…), cut out the 3rd and 7th numbers (the two tilts), and set them as--h0to--h191on the second wall. The text is copied as it is; the script does no arithmetic on it. -
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. -
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).
-
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
-
A transform list is a general 4×4 matrix product, and Chrome will give you the answer. The
computed
transformof an element is its whole list multiplied out to onematrix()ormatrix3d(). Nothing has to be drawn for that to happen: asking for the computed style is enough. - Four by four is enough for a dot product of any length. The matrices used here only ever add into each other or multiply once, so a list can be as long as the layer is wide. A list of 100,000 matrices worked in a test, with no sign of a limit.
- The answer comes back with six significant digits. Chrome multiplies in 64-bit floats (the leftovers in the bottom row cancel to about 1e-16) but prints the computed matrix with six digits. Six digits on the hidden layer turns into a logit error of about , on logits that run from about −270 to +13. The top character has matched the JavaScript run every time.
-
It is slow. ms a character in headless Chrome, about the same as
the same model done with CSS colours (about 40 ms), against a JavaScript loop's tens of microseconds. Layer
1 is about three quarters of it. The multiplying is not the slow part: a single list of 100,000 matrices
took 70 ms, parsing included. Most of the time goes on putting variables into
matrix3d()functions and parsing them again for every character. The same lists written out with plain numbers ran three to four times faster. -
Every weight is about bytes of text. A whole
matrix3d(), thirteen fixed numbers and all, carries two weights. As a weight format it is worse than the colour one. - ReLU is the wall. Without it the second layer's cards could sit inside the first's and the browser would chain the lot. A network with no activation function is just one matrix, though, so that would not be a model worth running.
Gotchas found on the way
-
If the result happens to be a flat 2D matrix, Chrome prints the short
matrix(a, b, c, d, e, f)form with six numbers instead of sixteen. A reader that always takes "the 3rd number" gets the wrong one. -
Arguments are clamped to the 32-bit float range, so
1e39quietly becomes3.40282e+38. Below that they are kept as 64-bit: 16777217 − 16777216 came out as 1 withmatrix3d(), and as 0 with twotranslateX()lengths. - Round the weights, then take the differences. Rounding the differences instead lets every rounding error ride along the rest of the list, which made the hidden layer about ten times less accurate.
- Read all of the first wall before writing anything to the second. Alternating a write to one wall with a read from the other made Chrome restyle 96 times a character: 2.7 seconds instead of 50 ms.
-
A
transitionon the cards would look lovely and break everything: straight after a change the computed transform is still the old pose, not the answer. - The inputs sit in the depth slot on purpose. With the weights there and the inputs in the bottom row, the sum of the inputs is left in the perspective row at the end, and a test card with −0.2 there drew as a shape several times its size.
-
getBoundingClientRect()also carries the answer, as where a transformed box lands. Through a fixed parent that turns depth into sideways movement it gave a tilt back to about seven digits, one more than the printed matrix, and the box's edges even do a ReLU for free (the right edge of a box stretched from 0 to s is max(0, s)). It isn't used here because it measures the box as drawn, so the camera and the lean on the walls would be mixed in, and the cards on show would have to be copies. -
A
ResizeObservercan't read any of this. It fires when layout size changes, and a transform never changes layout: it stayed silent in a test. A scroll box'sscrollWidthdoes follow a transformed child, but only to whole pixels.getBoxQuads()isn't in Chrome. - Tested in Chrome only. Other browsers may print the computed matrix differently.