GPU
Behind the Pod IP
How a CPU asks a GPU to do anything
To the network a model server is one address in a pool. Behind that address there are at least two computers: the host, with its CPU and DRAM, and the GPU, with its own processors, memory, page tables and firmware. They share no memory. Everything between them crosses a PCIe link, and that link has rules. This lesson follows one small job across it: a 4 MiB block of KV cache that sits in host DRAM and has to be in GPU memory before the model can use it. The CPU never copies a byte. It writes down what it wants, points at it, and rings a bell.
- The Job
- move 4 MiB of KV cache from host DRAM to GPU memory
- The link
- PCIe Gen5 x16 · about 64 GB/s each way
- Asking takes
- about 350 ns
- Doing takes
- about 130 µs, with no CPU time
Read the whole lesson as text
1. One address
A request arrives and the endpoint picker sends it to gpu-2. The reason is something no load balancer can see in an address: the start of this prompt was processed before, and the result, its KV cache, is still on gpu-2. It is not in GPU memory any more. It was moved down into the server's ordinary memory to make room. So picking gpu-2 means choosing a particular price: a few copies across PCIe before the model can start answering. To see what that price is made of, go below the Pod IP.
- TMM sends the request to the worker the picker chose. From here on the network sees nothing.
2. Two computers
A GPU like the H100 is a separate machine at the far end of a PCIe link. It has its own processors, 80 GB of memory the host CPU cannot address as its own, its own page tables, and a small RISC-V core running firmware. The host has its CPU, its DRAM and its own page tables. An address that means something on one side means nothing on the other until someone sets up a matching mapping. The block of KV cache the request needs is on the host side. The model runs on the GPU side.
- The model server finds the cached block in host memory. The GPU can't use it there.
3. The slow way in
The GPU shows the host a few windows into itself, called BARs. Two matter here. One of them, which NVIDIA calls BAR1, is a view of GPU memory. Through it the CPU can write to GPU memory as if it were its own. So why not copy the block that way? Because stores through that window go out as small PCIe transactions. This is memory-mapped I/O, MMIO. Moving one megabyte takes 262,144 4-byte stores, and the whole path tops out near 1 GB/s. That is fine for flipping a control bit. For a model's weights or a KV cache it is useless.
- Copying through the memory window would mean over a million separate 4-byte transactions. Nobody moves data this way.
4. Write down the copy
Bulk data goes the other way round. The GPU has copy engines, hardware built to reach into host memory and move data at tens of GB/s. The CPU's job is only to describe the copy. It writes a short list of commands, called methods, into a buffer in its own memory, the pushbuffer. Source address, destination address, length, go. Then where to report when it is done, what value to report, and go again. Seven methods, sixty bytes in all. Nothing has crossed PCIe yet. This was the CPU writing into its own memory.
- The CPU writes sixty bytes that describe the copy into a buffer in its own memory.
- The CPU makes sure those bytes are really written before it does anything else.
5. Point at it
The GPU doesn't know the pushbuffer exists. It learns about work from a ring both sides agreed on at setup, the GPFIFO. Each entry is eight bytes: the address of a pushbuffer and its length. The CPU writes one entry, then moves its write index, GPPut, forward by one. The GPU has a read index of its own, GPGet. When the two are equal the ring is empty. Now they differ by one. The GPU still doesn't know, because it isn't watching host memory.
- The CPU writes an eight-byte entry into the ring that points at the commands.
- The CPU moves its write index forward by one. There is now one entry the GPU hasn't read.
6. Ring the doorbell
The CPU now tells the GPU to look. It does that with one 32-bit store to a register on the GPU called the doorbell. The value is a token that names the channel with new work. This is MMIO, the slow kind of access from two steps ago, and that is fine: it is four bytes, once. It is also a write, so the CPU sends it and moves on without waiting. The GPU's front end wakes up, reads the ring entry out of host memory, then reads the sixty bytes the entry points at. Its read index catches up and the ring is empty again.
- One four-byte write crosses PCIe and tells the GPU that this channel has work.
- The token tells the GPU's front end which channel to serve.
- The GPU reaches into host memory and reads the entry the CPU left there.
- It follows the entry to the pushbuffer and reads the sixty bytes of commands.
7. The copy engine does the work
The front end hands the methods to a copy engine. Now the data moves, and the direction of control is the opposite of the slow way: the GPU is the initiator. The copy engine reads the source straight out of host DRAM, in large transfers, and writes it into GPU memory. This is direct memory access, DMA. The host CPU executes nothing while it happens. The whole 4 MiB block takes about 130 microseconds. Asking for it took about 350 nanoseconds, roughly one part in 370.
- The front end passes the commands to the copy engine, which starts on the first launch.
- The copy engine pulls the block out of host memory by itself, at tens of gigabytes per second.
- About 130 microseconds later the KV cache block is in GPU memory, where the model can read it.
8. Done, and provably so
The CPU must not use the block before it has really arrived, and it must not ask. Reading a GPU register across PCIe would cost up to a microsecond of waiting, on every copy. So the answer comes the cheap way: the GPU writes it. The last two methods named a spot in host memory and a value. When the copy is finished the copy engine writes that value there. That is the semaphore. A flag on the release guarantees every earlier write of the copy has landed first. The CPU only ever reads its own memory.
- The copy engine writes the agreed value to the agreed spot in host memory.
- The CPU notices the value in its own memory and knows the block is ready.
9. Leave out one fence
Some of the mappings the CPU writes through let it buffer stores and send them in a different order. That is good for speed and dangerous here. Suppose the fence after the ring entry is missing. Most of the time nothing happens. Now and then, under load, the doorbell overtakes the entry. The front end wakes up, reads an entry that is only half written, and fetches garbage. The GPU reports it with an error code, an Xid, in the kernel log. Without a driver to explain what went wrong, those codes are the debugger.
- With no fence, the doorbell write gets out before the ring entry is fully written.
- The GPU reads a half-written entry, follows it to the wrong place and reports a bad command stream.
10. Pay once
None of this works until someone has created the channel, built the ring, mapped the doorbell page into the process, pinned the memory and bound a copy engine. That is a lot of privileged work, done by the kernel modules and by firmware on the GPU. Counted on an H100 for a program that sets up, copies twelve times and tears down: setup makes 685 calls into the kernel and the firmware. The twelve copies make none. Twelve doorbells, and the kernel never learns the copies happened. A big, slow, careful control path, and a small, boring data path that never calls back into it.
- Once, at startup, the process asks the kernel modules for everything a copy will need.
- The kernel module passes much of that work on to firmware running on the GPU itself.
Summary
Behind a Pod IP there are two computers and a link with rules: someone has to start every transfer, writes are cheap, reads are expensive, and order is not guaranteed without a fence. So the CPU never copies data to a GPU and never asks whether it is done. It writes sixty bytes of commands into its own memory, adds eight bytes to a ring, and makes one four-byte write to a doorbell. The GPU fetches the work, a copy engine moves the data at tens of gigabytes per second, and the GPU writes a value into host memory to say so. All the expensive, privileged work happened once, at setup. That is why a cache hit in host DRAM is cheap and still not free, and why the endpoint picker has to know which tier a prefix is in. The Pod IP is where the network ends. It is not where the cost ends.
Next lesson: One request, five prices