Stacey on Software

Where the numbers live

An illustration: a silver-haired woman in a dark blazer leans over a workbench at night, one finger on a row of numbers on a long strip of green-bar fanfold printout that runs past a worn 1981 Radio Shack Color Computer, while the beige CRT beside it shows a green screen with a table of short words and columns of numbers; an amber twenty-sided die rests on the desk beside the paper

Last post covered:

  • The loop. Guess the next token, measure how wrong the guess was, nudge every number a little in the direction that would have made it less wrong, repeat.
  • The training data. Eighteen vintage computer names, split into words, sorted and numbered: 29 tokens. COMMODORE is 13 because it is thirteenth in that list. The number is a label, not a quantity.
  • The window. Two tokens wide. The model’s only job is to guess the token that comes next, so the eighteen names become 58 examples.
  • The parameters. 290 numbers that training changes.
  • Twenty epochs. Enough training to invent plausible names, not so much that it repeats the training data.

This post is about the 290 numbers: where they live, what each one does, and what one training step does to them.

Token identifiers stay fixed during training. Each identifier selects a row of three learned values from a table.

Two tables

Two tables, because the context window is two tokens wide and each position gets its own.

Each table has 29 rows, one per token, and each row holds three numbers. When the window holds END then COMMODORE, the machine fetches the END row from the first table and the COMMODORE row from the second table, and adds them:

position 1, <END>       -0.0286   -0.0187   +0.0583
position 2, COMMODORE   -0.0009   -0.0266   -0.0168
                        -------   -------   -------
add them                -0.0295   -0.0453   +0.0415

Two tables side by side on the CoCo's green screen, in its pixel type, titled TWO TABLES, ONE PER WINDOW POSITION. Each lists tokens with three signed numbers beside them. In the position 1 table the END row is highlighted in amber; in the position 2 table the COMMODORE row is. Below, the two fetched rows are written out and added, with the sum in three black cells: -0.0295, -0.0453, +0.0415. A caption reads: these three numbers are the model's whole input, no arithmetic beyond the addition.

The export script took these rows from the reference model before training. It also extracts the other numerical examples in this post; the repository map explains where the experiment code and data live. Adding the two rows gives the three context values used for prediction.

Each row of three values is a token’s embedding. We can plot those values as coordinates in three-dimensional space, so each token becomes a point in a box. The embedding values start random. Training changes them according to which tokens follow each context.

Here is the second table after 20 epochs, having been nudged into clusters:

The position 2 table after 20 epochs, drawn on the CoCo's green screen as points inside a wireframe box, each with a dashed drop line to the floor. The six makers are larger amber points close together on the right, labelled ACORN, APPLE, ATARI, COMMODORE, SINCLAIR and TANDY. The model names are small black points bunched on the left, most of them labelled, with COLOR, BBC, ZX and MODEL high up on their own. A caption reads: tokens that are followed by the same kind of token end up near each other; END lies far above this box.

APPLE and ATARI are 0.12 apart. COMMODORE is 0.44 from ATARI and 1.13 from 64. Nothing in the numbers says what a maker is. The makers ended up near each other because the same kind of token follows them. COLOR, BBC, ZX and MODEL sit high up on their own. Each begins a multiword model name: COLOR COMPUTER, BBC MICRO, ZX SPECTRUM and MODEL 100. That is what this space records: not what the words mean, but how and where they are encountered together. END is left off the figure because its position 2 embedding lies far above the other points. In the training contexts, END in position 2 precedes the first word of a name.

COMMODORE has a separate embedding for each context-window position. Its position 1 row is +0.0781, +0.0250, +0.0173; its position 2 row is -0.0009, -0.0266, -0.0168. Selecting a different row lets the same token contribute different numbers depending on which window position it occupies. The token identifier selects the row; the arithmetic uses the row’s three values.

Two positions, 29 tokens, three numbers each: 174 numbers, most of the model.

Why three?

GPT-3’s embedding is 12,288 numbers long where ours is three. Same idea, more room.

Every extra number is another 87 multiplies per example: 29 to score the tokens and 58 more in the training step. The 6809 has no 16-bit multiply instruction. A routine built from its 8-bit MUL instruction performs each multiplication. I measured the routine at 133 cycles on the machine, about 150 microseconds. I priced the widths before choosing:

numbers per wordparametersmultiplies to traintime in multiplies
1116100,92015 s
2203201,84030 s
3290302,76045 s
4377403,68060 s
5464504,60075 s
6551605,52090 s

The 45-second entry estimates time in the multiply routine alone. The run shown last time also computes probabilities, updates parameters and draws the progress display.

Six would probably still have fit my three-minute budget, but three was enough. Three numbers per token told 29 tokens apart well enough that the extra width never earned its cost in clock time.

The other choice I priced was the one I built first and threw away. Make a token a single character instead of a whole word and the model has to predict every letter: 12,859,560 multiplies. Counting only the eleven cycles of each bare MUL instruction gives a lower bound of 158 seconds against a 180-second budget. Including the arithmetic needed around those instructions raises the estimate to half an hour.

What is a parameter?

A parameter is a numerical value that training is allowed to change.

 2 window positions x 29 tokens x 3 numbers  =  174   the two tables
                     29 tokens x 3 numbers  =   87   a row for every token it can predict
                     29 tokens              =   29   a bias per token
                                               ----
                                                290

I enjoy working at this small scale. With 290 parameters, I can inspect every value and trace an update through the assembly. The CoCo’s memory and clock budgets also make the cost of a wider model visible before I build it.

A score for every token

The scoreboard contains 87 weights and 29 biases. Each of the 29 possible output tokens has three weights and one bias, an offset added to its score. To score a token, multiply each context value by its corresponding weight, add the three products, then add the bias. Repeat for all 29 tokens.

Before training all 290 numbers are random and small, so the scores are all near zero. Here are the shares they turn into for the window END, COMMODORE, where the right answer is AMIGA:

TRS-80   3.49%    <- the largest share, barely
128      3.48%
ZX80     3.48%
ATARI    3.48%
...
AMIGA    3.45%    <- the right answer, in the middle of the pack

Scores become shares

Those percentages are what a score turns into once you push it through a softmax.

Suppose three tokens have raw scores of 3, 2 and 1. A score of 3 doesn’t mean 3%, or three votes. Softmax makes every score a positive weight, then divides each by the total, so the results are shares of 100%: about 67%, 24% and 9%. The largest score receives the largest share.

Why “soft”? A hard maximum would give the winner everything. Softmax lets the strongest choice lead while the others stay possible.

Softmax produces probabilities. A separate step selects the next token.

On the CoCo the shares add up to 256 instead of 100, so every token owns a stretch of a line from 0 to 255 as wide as its share. The machine draws one byte, and whatever stretch it lands on is the next token. That’s the die.

Training is a nudge

For the window END, COMMODORE, the training target is AMIGA and its predicted probability is 3.45%. One training step computes the prediction errors and uses their gradients to update the output parameters and the selected embedding rows. Here is the step, with the real numbers.

The training target assigns AMIGA a probability of 1, but its predicted probability was 0.0345. Target minus prediction is 0.9655. For each of AMIGA’s output weights, multiply the learning rate by that difference and by the corresponding context value. Add the result to the weight.

The context values are -0.0295, -0.0453 and +0.0415. They came from adding the two embedding rows at the top of this post. Each value multiplies one of AMIGA’s three output weights when computing its score.

I chose a learning rate of 0.0625. Multiplying that rate by the target-minus-prediction difference, 0.9655, gives the scale for the update. Multiply each context value by this scale and add the result to its corresponding weight. The two negative context values decrease their weights; the positive value increases its weight.

Three columns on the CoCo's green screen, titled THREE WEIGHTS AND ONE NUDGE EACH, with the formula beneath: change equals 0.0625, a rate I chose, times 0.9655, how wrong it was, times the context number. Each column has the context number in a navy box marked negative or positive, the weight before, the change in an amber box with a navy arrow pointing down for the two negative changes and up for the positive one, and the weight after: +0.1214 to +0.1197, -0.0705 to -0.0732, +0.0325 to +0.0350. The caption reads: each weight moves the way that would have raised the score for AMIGA.

The rate, 0.0625, is a sixteenth. I chose it because dividing by sixteen uses four shift pairs on a 6809.

Training also updates the two embedding rows used for this example: END in position 1 and COMMODORE in position 2. Their updates combine the prediction errors for all 29 tokens with the output weights that connect each context value to those scores. After the step, fetch and add those rows again and you get -0.0118, -0.0561, +0.0477. Repeated updates change which output tokens these rows favour.

After this training step, AMIGA’s predicted probability rises from 3.45% to 3.68%. The run contains 1,160 training steps. The output weights, biases and selected embedding rows are all updated using gradients from the same prediction.

Roll it

Here is the die being rolled, from the run you watched last time, after all 1,160 of those steps. Seed 6809 drew three bytes: 50, 12, 119.

The first draw, 50, landed on COMMODORE, which owned 27 of the 256. TANDY occupied 131 positions, more than half the line, but the draw did not select it. The second draw, 12, selected the token 128, whose share occupied 17 positions. PET had the largest share at that step, with 25 positions. The third draw, 119, landed on END, which by then owned 228 of 256.

Three horizontal lines from 0 to 255 on the CoCo's green screen, titled SEED 6809 DREW 50, 12, 119. Each line is divided into stretches, one per token, as wide as its share. On the first line a navy arrow at 50 lands on COMMODORE's stretch, highlighted amber, while TANDY's stretch covers half the line. On the second, an arrow at 12 lands on 128. On the third, an arrow at 119 lands on END, which covers almost the whole line. The bottom reads: THE NAME: COMMODORE 128.

COMMODORE 128. A real name; it’s in the training data. It’s also the first name on the screen at the end of the video.

Seed 6810 drew 177, 110 and 29 from the same table, and got TANDY COMPUTER. Same weights, same rows, different bytes. Each generated token changes the context, so the next draw uses a newly computed probability distribution.

Two things about the die. For the first two tokens, END’s probability share is transferred to the highest-probability non-END token, so a name is never one word long; that rule is in the code, not the weights. And you don’t have to roll. Take the widest stretch every time and you get the same name every time: greedy decoding, which is what temperature zero means. Temperature rescales the scores before they become shares. Higher temperature makes the probability distribution flatter; lower temperature concentrates more probability on the highest-scoring tokens. At temperature zero, greedy decoding selects the highest-scoring token.

The 174 embedding values and 116 output parameters determine the probabilities. Greedy decoding gives the same continuation from a fixed context and fixed weights. Sampling can produce different continuations from those same weights.

Next post: what 1,160 of those nudges do, epoch by epoch, and the 6809 instructions that do one of them.