Gradient Decent / layout
Can page layout run a language model?
The browser's layout engine as a calculator. This is the same tiny Shakespeare-babbling model as the
css page, but every number is the size of a box and the page's layout does all of the
model's arithmetic: aspect-ratio multiplies, a row of boxes adds, the space left over subtracts, a
staircase of floats rolls the dice, and the longest bar in a grid column is the next letter. There is no
calc() in the machine and no sums in the script. Layout does the maths because it has to put the
boxes somewhere.
Yes, very slowly. About 45,000 boxes get laid out for every character, about 0.7 seconds each in headless Chrome, some 20,000 times slower than the same model as a JavaScript loop. That makes it the slowest page here per character. It is right, though: the dot landed on the same character as plain JavaScript 20 times out of 20, with every score within 0.005. The race below measures both on your machine.
Run it
The pick: 37 bars in one grid column. The longest one has no space left beside it, and that is where the dot appears.
Layer 2: one row per character. adds takes away what is left is the score
The dice: one per character. the curve, drawn in floats dropped in at a random height temperature
Layer 1: 192 neurons. Each strip is 145 boxes whose heights are the inputs and whose shapes are the weights.
This is the machine itself, shrunk with a CSS transform so it fits (transforms don't change layout), and cropped where a strip runs long. At full size a layer-1 strip is up to 5,000 pixels wide, a layer-2 row up to 80,000, and each die is about 28,000 pixels tall.
Race it in your browser
Runs 20 random 8-character contexts through both: the boxes, and a plain JavaScript loop over the same
weights. That loop is the only place on the page where script multiplies, and it only runs here, as the
checker. Same as window.selfTest().
How one character gets made
Nine steps. The script writes the weights into boxes once, then only carries widths from one stage to the next.
-
CSS
Embed.
The last 8 characters become class names. Each class sets lengths: "character 12 at position 3" is nine heights, such as
--p3_0: 1.61em. A negative number goes in a second variable, because a box can't be less than nothing tall. One unit of the model is 1em, and the font size is 256 pixels. -
Layout
Multiply.
One box per weight. Its height is an input and its
aspect-ratiois the weight, so layout has to make it input × weight wide. Nobody wrote a multiply. -
Layout
Add.
The boxes for one neuron sit side by side in a flex row. The row is as wide as all of them together, which is the sum. Terms that should count against the neuron go in a second row.
-
Layout
Subtract and clamp.
Under the adding row, the taking-away row is placed in the same width, and a stretchy box gets whatever is left. That is the sum minus the rest. If the taking-away row is longer there is nothing left, and nothing is as low as a width goes, which is exactly ReLU.
-
Layout
Roll the dice.
Sampling needs a random amount with a particular shape added to every score. Each die is 256 floats stacked into a staircase that draws that shape. The script pushes a block down by a random whole number of steps, and layout squeezes the block into the room left beside the step it lands on. That width is the roll. The die's own width comes from a box whose
aspect-ratiois the temperature, and the steps are percentages of it, so the same layout pass also multiplies the roll by the temperature. -
JS
Carry.
A
ResizeObserverreports the 192 leftover widths and the 37 rolls, and the script writes each one back as a size for layer 2. It copies the number; it does no sums. -
Layout
Layer 2.
The same three moves again, one row per character. Each row also gets a fixed box so no score falls below zero, and its roll.
-
Layout
Find the largest.
The observer carries the 37 scores to 37 bars in one grid column. The column is as wide as the longest bar, so that bar is the only one with no room left beside it. A container query on that leftover room gives a dot a size only where the room is zero.
-
JS
Write.
The observer fires again, for the one dot that just appeared. Which dot it was is the character. Append it, change the class names, and wait for the next frame.
What a neuron is made of
<div class="n"> one grid column, as wide as its longest row
<div class="row p"> the adding row
<i class="x3_0p" style="aspect-ratio:0.318"> height: var(--p3_0), so width = that input × 0.318
…144 more
</div>
<div class="d">
<div class="r"></div> flex: 1, min-width: 0. Its width is the answer
<div class="row m">…</div> the taking-away row, never shrinks
</div>
</div>
Every forward pass happens inside one frame. The observer's callbacks run during layout, each one changes something deeper in the page, and the browser lays out again and calls back again until nothing moves.
JS what the script still does
- Writes each weight into a box as text. Which row it goes in is decided by whether the text starts with a minus sign
- Characters to class names
- Copies 192 + 37 + 37 widths from one stage to the next
- Asks the browser for 37 random bytes and writes each one in front of
00px - Appends the character the dot landed on
- Counts characters and runs a stopwatch for the readout
- The plain JavaScript model in the race, as the checker
Layout all of the arithmetic
- Every multiply (
aspect-ratio) - Every add (boxes in a flex row)
- Every subtract and both ReLUs (leftover space)
- Bias (a box of fixed size)
- The dice and the temperature (floats, percentages and one more
aspect-ratio) - Argmax (a grid column and a container query)
What it says
-
Layout is a calculator if you hold it right. It multiplies, adds, subtracts, clamps, looks
a value up on a curve and finds a maximum without a single
calc(). Run the race for the numbers on your machine. - Floats are a lookup table. Any curve can be drawn as a staircase of floats, and a block dropped beside it reads off the value at that height. Here the curve is the one sampling needs, but it could be any function of one number.
- Sizes are fixed-point. Chrome lays out in 64ths of a pixel, so every box is rounded. One unit of the model is 256 pixels, which keeps the rounding well below the gaps between scores.
- The clamp is free. ReLU usually needs its own step. Here it falls out of the fact that a box can't have negative width, which is the same limit that makes negative numbers awkward everywhere else on this page.
- The observer is the loop. Nothing polls. A character is finished when the browser has nothing left to resize.
Gotchas found on the way
-
An
aspect-ratioof 0 means "no ratio", and the box ends up with no width. That happens to be the right answer for a weight of zero, and it is how temperature 0 turns the dice off. - The dice have 256 faces, so the random amounts come in 256 sizes. The largest is about 6 units, where the true curve has no upper limit.
-
A
ResizeObserverstays silent when a size doesn't change. If the same character wins twice its dot would not report, so the dot is 40 pixels tall on even steps and 41 on odd ones. - Within one frame the observer only reports on elements deeper in the page than the ones it just reported, so the stages have to be nested deeper and deeper in the markup.
- If a stage really doesn't change, nothing fires. A timer reads the dots directly after 700 ms; the race reports how often that happened.
- Tested in Chrome only.