What this is about
A graphics card was made to draw pictures. To draw a picture it has to compute the color of millions of dots, and it does that by doing a lot of small, similar sums at once. People later noticed that other jobs, such as running a language model, need exactly the same kind of work.
Here is the one idea to grasp first. On a processor, you write a loop that does a job one item after another. On a graphics card, you write the job for one item, and the card runs thousands of copies of it at the same time, one per item. The small function you write is called a kernel.
Why it matters
If you only learn one thing about GPUs, make it this. It explains why a card can be many times faster than a processor for some jobs and useless for others. A job that splits into many independent small pieces fits. A job where each step needs the answer of the last one does not.
It also explains why GPU code looks odd. You do not write the loop. You write the body of the loop and say how many copies to run.
How it works
Think of a classroom of 100 students and a stack of 100 sums. A single teacher works the sums one by one. That is the processor. Now give one sum to each student and say "go". That is the graphics card. Each student does a very small amount of work, but all of them work at once.
Three things follow from that picture.
- The function is small. Each thread does a tiny task. Our own micro-test had 16 million threads, and each did 16 exponential calculations in a row. The card handled all of them together.
- Copies must not need each other. If student 7 must wait for student 6, the class is no faster than one teacher. So kernels are written so that each copy works on its own piece.
- Moving data is the other half of the cost. A card has its own memory, called VRAM. Our card, an AMD Radeon RX 7900 XTX, has 24 GB of it. Often the time is spent reading numbers from memory, not adding them. In our exponential micro-test, the standard version came out near memory bound, while our own version was bound by arithmetic.
Where Mojo fits in
Mojo is one of a few languages that can write the kernel and the rest of your program together. In our engine, mojo-baro, every GPU kernel is Mojo. The kernels folder holds matrix-vector products for each weight format, flash attention, a router for expert models, a sampler on the card and persistent kernels. The command-line tools and the server's engine process are Mojo too.
The engine shows two styles of using the card. One style, which we call launch, runs one kernel for each operation in the model. It is the plain reference path. The other, mega, runs one long-lived kernel per token for the whole forward pass. That is the default for dense models. Both exist because the second is faster but harder to change. We report which path served each answer.
Mojo can also reach corners of the hardware that it has no official name for. We wrote a short probe that makes Mojo produce the instruction RDNA3 cards use to test a ray against a node of a ray-tracing tree. It compiled. We have never run it. Our write-up is titled with that fact: a compile proves the instruction is reachable, not that it works.
How to see where a kernel waits
A fast kernel is one that does not wait. To find out where it waits, we built wavewait. It reads the thread trace the card emits and shows, for each instruction, how many cycles a group of threads waited and why. For example, a memory load that waits 670 cycles can look like one innocent line in a listing, yet it is the cost that matters.
You do not need wavewait on day one. It is useful to know that a profile of a GPU kernel exists, and that it can name a cause for each wait.
What we do not claim
- We use one card, an RX 7900 XTX. We have not tested your card.
- A speed result for one model does not carry to every model.
- This article gives you the idea and no kernel code. The first real kernel is a topic for a later article.
Words you may not know
- GPU: graphics processing unit, the graphics card's chip.
- Kernel: a small function that runs on the GPU, in many copies at once.
- Thread: one running copy of a kernel.
- VRAM: the memory on the graphics card.
- Forward pass: one trip of the input through a language model to get the next word.
Try it
- Open the mojo-baro project page and read how its engine is split into parts.
- Visit the wavewait page to see what a per-instruction wait table looks like.
- Browse the
kernelsfolder in the mojo-baro repository and note the file names. Each one is a kernel.
Read next
- Same bits on the CPU and the GPU shows what it took to make both sides agree.
- Mojo can reach ray-tracing hardware it does not expose has the probe and its limits.

Komentarze
Brak komentarzy.
Zaloguj się aby skomentować.