Gradient Decent / lorem

Can page layout write lorem ipsum?

You always pour lorem ipsum into a page layout. So what if the page layout wrote the lorem ipsum? This page turns the browser's layout engine into a calculator and uses it to run a small language model that writes lorem ipsum. Every multiply, sum and choice is the size of a box. The model was trained ahead of time, in an ordinary script, on Cicero's De Finibus, the book lorem ipsum was chopped out of, and what it writes is typeset into the magazine below as filler.

Yes, slowly. Each piece of a word takes about 66,000 boxes and a bit over half a second, roughly 4,000 times slower than the same model in plain JavaScript. It's a goofy experiment, not a useful one, but the answers are right: in headless Chrome the boxes picked the same piece as a plain JavaScript copy of the model 24 times out of 24, with every score within 0.012. What it writes is mostly real Latin words in a plausible order, which is also a fair description of lorem ipsum.

Run it

LoremVol. XLV a.C.n. · Filler quarterly

De Finibus Bonorum et Malorum

M. Tullius Cicero, as the boxes remember him

“Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet.”

The context: 16 slots in a ring · each dial is that slot's position, eight hands turning at eight speeds

1 · Queries, keys and values · 96 sums of up to 64 boxes. Box widths are the piece's numbers, box shapes are the weights

The dice · one per piece of the vocabulary. Floats draw the curve, the yellow block is dropped in at random

2 · Rotation · each pair of numbers turned by the slot's angle: cos and sin are box shapes

3 · Attention scores · query times every key in the ring. Here both factors are live, so one is a width and the other a cqw height inside it

4 · Attention · two flex rows. Each slot grows by its score, the grey sink by 1, so each slot's width is its share

5 · Mixing the values · each slot's share times its value, summed

6 · Back into the stream · the word's own numbers plus what attention found

7 · Feed-forward, in · 96 neurons, clamped at zero by the leftover space

8 · Feed-forward, out · and added back

9 · Scores for all 320 pieces · each with a fixed box that keeps it above zero

10 · The pick · each bar is a score (green) plus that piece's roll of the dice (yellow), all in one grid column. Only the longest has no room beside it, and that's where the yellow mark appears

picked ·

Everything in the numbered stages is the machine itself, shrunk with CSS transforms so it fits (transforms don't change layout). At full size it is millions of pixels wide.

Race it in your browser

Feeds 24 random pieces through both: the boxes, and a plain JavaScript copy of the same model. That copy is the only place on the page where script multiplies, and it only runs here, as the checker. Same as window.selfTest().

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

How one piece gets made

The model reads pieces of words, not letters: dolor, em, ips. Each step makes one piece. The script builds the boxes once, then only carries sizes from one stage to the next.

  1. CSS
    Look up the piece.

    A class name on the first stage sets 64 lengths: the piece's 32 numbers, each split into a positive and a negative part. A class on the rotation stage sets the position: eight cos and eight sin values, split the same way.

  2. Layout
    Queries, keys and values.

    The same move as the layout page, turned on its side: a box is one number wide, and its aspect-ratio: 1 / weight makes it number × weight tall. A column of them is a sum; the taking-away column sits next to it and a stretchy box at the bottom of each gets the difference. Both stretchy boxes are kept, so a sum can come out negative.

  3. Layout
    Rotate.

    Rotary positions turn each pair of numbers by an angle that depends on where the piece sits. A turn is a sum of products with cos and sin, and cos and sin are constants for each position, so they are box shapes too. The angles repeat every 64 pieces, so a ring of 16 slots never has to be shifted.

  4. JS
    Remember.

    The key and value are copied into the next slot of the ring: this page's memory is 2,048 CSS variables.

  5. Layout
    Score every slot.

    Here both factors are live, so aspect-ratio can't help. The box is the query's number wide and is a size container; the box inside it is the key's number in cqw, a percentage of that width, so its height is the product.

  6. Layout
    Share out the attention.

    Each slot becomes a flex item that grows by its score (clamped at zero), next to a sink that grows by 1. Flexbox hands out the row in proportion, so each width is score ÷ (1 + all scores). Division, for free.

  7. Layout
    Mix, feed forward, score.

    Each slot's share times its value, summed; added back to the piece's own numbers; through 96 clamped neurons and back; then a score for each of the 320 pieces, with a fixed box that keeps every score above zero.

  8. Layout
    Pick.

    Each piece's bar is its score followed by its roll of the dice. 320 bars in one grid column. The longest leaves no room beside it, and a container query gives a mark a size only where the room is zero.

  9. JS
    Typeset.

    The observer sees which mark appeared. The piece is queued, and the letters are let out evenly at the pace the machine has been running, so the text flows instead of arriving in lumps.

JS what the script still does

  • Writes each weight into a box as text; a leading minus sign decides the column
  • Splits the prompt into pieces with the model's merge list
  • Copies sizes from one stage to the next, and keys and values into the ring
  • Counts positions and which ring slot is next
  • Asks for 320 random bytes per piece
  • Lets the letters out at an even pace, and counts and times for the readout
  • The plain JavaScript model in the race, as the checker

Layout all of the model's arithmetic

  • Multiplying by a weight, cos or sin (aspect-ratio)
  • Multiplying two live numbers (cqw inside a size container)
  • Adding (columns of boxes)
  • Subtracting and clamping (leftover space)
  • Dividing by the total (flex-grow)
  • The dice and the temperature (floats and aspect-ratio)
  • Picking the largest (a grid column and a container query)

What it says

Gotchas found on the way