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().

Layout, per character–
JavaScript, per character–
Layout is slower by–
Same top character / largest score gap–

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.

  1. 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.

  2. Layout
    Multiply.

    One box per weight. Its height is an input and its aspect-ratio is the weight, so layout has to make it input × weight wide. Nobody wrote a multiply.

  3. 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.

  4. 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.

  5. 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-ratio is the temperature, and the steps are percentages of it, so the same layout pass also multiplies the roll by the temperature.

  6. JS
    Carry.

    A ResizeObserver reports 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.

  7. 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.

  8. 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.

  9. 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

Gotchas found on the way