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
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
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().
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.
-
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.
-
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 / weightmakes 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. -
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.
-
JS
Remember.
The key and value are copied into the next slot of the ring: this page's memory is 2,048 CSS variables.
-
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. -
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.
-
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.
-
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.
-
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 (
cqwinside 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
-
A transformer fits in layout. Attention needed two new moves:
cqwto multiply two numbers that are both still being worked out, andflex-growto divide by a total. Run the race for the numbers on your machine. - It learned Latin, sort of. Trained on the five books of De Finibus (about 47,000 words) for 70 seconds on two CPU cores. On a stretch it never saw, it is about 6 times less surprised by each piece than a model that only knows how common each piece is (3.62 against 5.39 nats a piece). Most of what it writes is real Latin words in plausible order, which is also a fair description of lorem ipsum.
- The model was built for the page. No layer norms, because dividing by a square root needed another stage. Attention shares are score ÷ (1 + total) with negative scores clamped, not the usual exponential, because flex-grow does that directly. Rotation angles repeat every 64 pieces, so the 16-slot ring never has to shift.
Gotchas found on the way
- If the grow factors in a flex row add up to less than 1, flexbox only hands out that fraction of the room. The sink of 1 keeps the total above 1, so the shares stay exact.
- A size container can't take its size from its contents, so the product box can't be the container. The container is a wrapper, and the product is the height of the box inside.
-
Ten stages have to finish inside one frame, and a
ResizeObserveronly reports elements deeper in the page than the last ones it reported. The stages are nested one inside the next and drawn withdisplay: contents, so they can still sit in a grid. - The residual connections let a stage start on half-new inputs: the feed-forward output stage hears from the attention stage before the feed-forward input stage has finished. That sent early, wrong waves down the machine. The fix holds a few copies back until the stage before has reported, so each big stage is laid out once per piece. A piece is finished when a whole frame passes with nothing resized.
-
Chrome stops drawing a single box past about 16,000 pixels. The scores are 1,024 pixels a unit, so the
pick's bars arrive in
cqwof a 3-pixel-wide container instead: the same numbers, three hundredths of a pixel each. - Tested in Chrome, and it also runs in Safari on iOS. Drawing the shrunken machine costs time on top of the sums, so a phone is slower.