What this is about
Open any language model and look at where the work goes. Most of it is one kind of sum, called matrix multiplication. You take two tables of numbers and combine them by a fixed rule. The model does this over and over, with different tables, for every word it reads and writes.
If you make that one sum faster, you make the model faster. That is why people write special programs, called kernels, just for it. This article explains the sum with a tiny example, then shows what happened when we wrote our own for a gaming graphics card.
Why it matters
The speed of a chat model on your own computer comes down to how quickly the graphics card can do this sum. The numbers inside the model, its weights, are stored as tables. To make the model say one word, the card multiplies the tables by the numbers that stand for the text so far.
So when you hear "this card runs the model at 100 tokens per second", most of that speed is matrix multiplication. A library that does it well gives you a fast model. A library that does it badly on your card leaves speed unused. Large vendors tune their libraries mostly for their data center cards. Gaming cards often get less attention. That is the gap we looked at.
How it works, with a tiny example
A matrix is a table of numbers with rows and columns. Take two small ones.
Table A has two rows: 1 2 and 3 4. Table B has two rows: 5 6 and 7 8.
To get the first number of the answer, take the first row of A and the first column of B. Multiply the numbers that line up, then add: 1 x 5 + 2 x 7 = 19. That is one cell. Do the same for each row and column pairing and you get four cells: 19 22 and 43 50.
That is all matrix multiplication is: many small multiply-and-add steps. A real model uses tables with thousands of rows and thousands of columns. The count of steps explodes, but the rule is the same one you just did by hand.
Two things follow.
First, the work is very regular. Every cell is independent of the others, so a graphics card, which has thousands of small workers, can compute many cells at once.
Second, the work is mostly about moving numbers. The card has to bring the numbers from memory to the workers, and bring the results back. A fast kernel is mostly a plan for who loads what, when, and where it waits in between.
How we measured our own version
The card in this article is an RX 7900 XTX, a gaming card with 24 GB of memory. We wrote a kernel in the Mojo language and tested it against hipBLASLt, AMD's own library for this sum. We used square tables, from 256 by 256 up to 4096 by 4096, and 16-bit numbers.
The unit is GFLOP/s: billions of multiply-or-add operations per second. A bigger number is faster. Here are the results for three of the ten sizes, from our post on the test.
| size | ours (GFLOP/s) | hipBLASLt (GFLOP/s) | ratio |
|---|---|---|---|
| 256 | 6,372 | 6,288 | 1.01 |
| 1536 | 93,632 | 69,332 | 1.35 |
| 4096 | 90,705 | 82,437 | 1.10 |
Our kernel was ahead at all ten sizes. That is the whole claim. It is one card, one kind of number, and square tables. It does not say our kernel is the fastest sum in the world. It does not say the result carries over to other cards.
Two lessons from that work
A first win can be a measuring mistake. An earlier version of our notes said a different kernel was twice as fast as the vendor library. That was wrong. We had not given the vendor library a fair set-up. After we fixed the set-up, the lead was gone. We keep the retraction in the record. If a vendor baseline looks too easy to beat, suspect your own test first.
Cards need a warm-up. A graphics card slowly raises its speed when it gets work. At a 1-second warm-up, the same program read 66,000 GFLOP/s at the largest size. At 10 seconds, it read 90,705. The program had not changed. The card just had not reached full speed. Every timing we do now warms up for 10 seconds first.
And in a real chat model?
There is a catch. A chat model that writes one word at a time does not multiply two big square tables. It multiplies one thin row of numbers against a big table. That is a different shape, and a kernel tuned on squares does not carry over. We wrote about that separately, and the next article in this series covers it.
Words you may not know
- Matrix: a table of numbers in rows and columns.
- Matrix multiplication: combining two tables by multiplying rows against columns and adding.
- Kernel: a small program that runs on the graphics card.
- GFLOP/s: billions of simple operations per second. Higher is faster.
- hipBLASLt: AMD's library of ready-made matrix multiplications.
- fp16: a way to store a number in 16 bits, so tables take less space.
Try it
Our engine for AMD graphics cards, with every kernel in Mojo, is described on the mojo-baro project page. The kernel results above live in the repository's bench/ folder.
To see where a kernel waits on the card, instruction by instruction, look at wavewait. It reads a trace from the card and says why each wait happened. The code is on GitHub.
Read next
Beating hipBLASLt on a Gaming GPU has the full ten-size table, the tile-size fix and the retraction in detail.

Comentarios
Aún no hay comentarios.
Iniciar sesión para comentar.