Checking how many agents are working

How this works
Contact
← Blog
A wide flat olive slab beside a tall thin olive bar, both lit from one side on a near-black navy ground with a dotted grid, the AMARBARO mark centred over them.
✎blogbericht

Why the shape of the work matters to a graphics card

The short version

A kernel tuned on square tables does not carry over to the thin shape a chat model issues for each token; on that shape the limit is memory bandwidth, and our best kernel reached 121.8 microseconds against 122.1 for AMD's library, a margin of about 1 percent.

What this is about

In the last article we saw that a model mostly does one kind of sum: multiplying two tables. We tested that sum on big square tables. But a chat model that writes one word at a time never multiplies two big square tables. It multiplies one thin row of numbers against a big table.

That is a very different job, and it makes a graphics card behave differently. This article explains why shape matters, using the one shape that counts most when a model is writing.

Why it matters

If you only test on the easy shape, your results will not predict what you feel when you chat. A kernel can be a clear win on square tables and a clear loss on the thin shape. The two facts do not average into anything useful.

This is also why "my card does 90 trillion operations a second" does not tell you how fast a model will write. What matters is which limit you hit first.

How it works: two limits

A graphics card has two limits for any job.

The math limit. How many multiply-and-add steps it can do per second.

The memory limit. How many numbers it can pull in from its memory per second.

A job hits whichever limit is lower. If you do a lot of math on each number you load, you hit the math limit. If you do very little math per number, you hit the memory limit and the math units sit idle, waiting for data.

A big square multiplication does lots of math per number loaded. It tends toward the math limit. We covered it in the last article.

Writing one token is the opposite. The model has one row of numbers for the new token, and it must multiply that row against every big weight table in the model. That means loading a huge table, using each number once, and throwing it away. In our test the shape was one row against a table of 4,096 by 12,288 numbers. Loading it means moving about 100 MB of weights from memory. The card does that around thirty times for each token, once per weight table in the layers.

So decoding a word is a memory problem. The question is not "how close to the card's top math speed?" but "how close to the card's top memory speed?" And the weights cannot sit in the card's fast cache, since our card has 96 MB of cache and the model is many gigabytes.

How we tested it honestly

Our first attempt to time this was wrong. We timed one weight buffer in a loop. One run read 2.70 milliseconds and the next read 0.33, from the same build. That was an eightfold swing, caused by whether the buffer happened to still be in the cache. We threw the result out.

The fix was to rotate eight different buffers, a working set of 805 MB, far past the cache, so every launch had to stream from real memory. We also repeated every measurement ten times. The spread fell below 1 percent.

We also wrote every prediction down before running. Here is how four rounds went.

Round 1: smaller numbers. We stored the weights in fewer bytes, so there would be less to load. We predicted a speedup of 1.5 to 1.9 times. The result was slower: 468 to 474 microseconds against 399 to 403. The reason: our weight layout limited how fast the card could load, and the unpacking added work.

Round 2: smaller numbers, laid out properly. This time faster than the plain version, but only 1.18 times. AMD's library beat everything at about 123 microseconds.

Round 3: wider loads. We gave each thread eight columns, so one load covered 16 bytes. This was the best so far at about 139 microseconds. One of our predictions was wrong: we expected this change to hurt, and it helped.

Round 4: stop carrying rows that do not exist. Our kernel was built for eight rows and was being used for one. We rewrote it for one row. That freed registers and local memory, so more groups of threads could stay on the card at once, and more waiting loads were in flight together.

arm microseconds per launch
general kernel, wide loads 137.8 to 138.9
one-row kernel, 8 columns per thread 121.2 to 121.8
AMD's library, same shape 122.1 to 123.3

The two ranges do not overlap, which was our rule for claiming anything. The margin is about 1 percent.

What the 1 percent is, and is not

It is a win over AMD's library on one shape, on one card, when the weights come cold from memory. It is not a claim about other shapes or other cards. And three of our four rounds missed at least one prediction. The surprise was the cause: the gain came from making the kernel smaller, not from loading fewer bytes. The same bytes crossed the same bus in the losing and winning arms.

A note from the test: a language model server held 22 GB of video memory on the card during one round, which forced two arms to be dropped. We wrote that down next to the numbers. One of ten vendor measurements read 817 microseconds against a median of 122, and we excluded it as an outlier. We say so because excluding it helps our side.

Words you may not know

  • Shape: the sizes of the tables in a sum, such as one row against 4,096 by 12,288.
  • Memory bandwidth: how many bytes per second the card can pull from its memory.
  • Cache: a small fast store on the chip.
  • Decode: the step where a model writes one new token.
  • Microsecond: one millionth of a second.
  • Occupancy: how many groups of threads sit on the card at once.
  • Thread: one small worker on the card. Thousands run at the same time.
  • Register: the fastest, smallest store a thread has. Using fewer lets more threads fit.

Try it

wavewait shows where a kernel waits, instruction by instruction, with a cause on every row. The code is on GitHub. It is a good way to see for yourself whether a kernel waits on memory or on math.

Our engine, mojo-baro, keeps the preregistered protocols for each result in a bench/ folder. Ask about shapes at gllm.forum.

The Shape That Actually Matters has all four rounds, each with its prediction and its verdict.

Reacties

Nog geen reacties.

Inloggen om te reageren.