GPU
Measure, don't assume
Same hardware, same commands, two thirds of the speed
A GPU copy is sixty bytes of commands and one doorbell write. So it should be possible to write those bytes yourself, ring the doorbell yourself, and match CUDA's speed with no CUDA at all. Public research did exactly that, with a small library that replaces CUDA for copies. It worked, and it ran at two thirds of the speed, on the same GPU, over the same link, with almost the same commands. This lesson follows the hunt for the missing third. The cause was something nobody thought to check, and it is the same mistake a load balancer makes when it treats identical GPUs as equal.
- Hand-built copy
- 38 GB/s
- Cuda, same machine
- 55 GB/s
- Same
- copy engine, PCIe link, memory, nearly the same commands
- Different
- one thing nobody wrote down
Read the whole lesson as text
1. Three identical workers
Three GPU workers serve one model. Same GPU, same image, same settings, all healthy. Round robin was built for a pool like this: every server is the same, every request is one unit of work, so deal them out in turn. Hold on to that assumption while we go inside gpu-2 and try something that should be simple.
- Dealt out in turn, the next request goes to gpu-2 for no better reason than that it is next.
2. A copy without CUDA
Inside gpu-2, a small library sets up its own channel through the kernel module, maps the ring and the doorbell, and writes a copy command by hand. It comes to 18 words of 4 bytes, 72 bytes, a little longer than CUDA's 60 because it names the copy engine explicitly instead of relying on earlier state. Then the ring entry: 8 bytes pointing at those 72.
- The library writes its own copy command into the pushbuffer, 72 bytes long.
3. The first fault
The first run doesn't copy anything. With no driver to say what went wrong, the GPU's own error codes are all there is. NVIDIA calls them Xids and they land in the kernel log. This one is Xid 31, a fault in the GPU's memory management. The cause is in the ring entry. Commands are always 4-byte aligned, so the entry stores the pushbuffer's address shifted right by two bits. The library stored it unshifted, and the GPU went looking for commands at four times the address.
- The library rings the doorbell for its hand-built entry.
- The GPU reads the entry, multiplies the address by four as the format says, and faults on an address that was never mapped.
4. It works. Now measure it
With the address shifted the copy works. Data arrives, the semaphore fires, everything checks out. Then comes the measurement. A 256 MiB copy from the hand-built path runs at 38 GB/s. CUDA on the same machine runs at 55. Every obvious suspect is cleared. It is the same copy engine, the same PCIe link, the same memory. The 12 extra bytes of command can't cost a third of the bandwidth, and commands aren't on the critical path once the engine is running.
- The copy is correct and a third slower than the same copy through CUDA.
5. The clock nobody checked
The answer has nothing to do with the copy. The GPU's compute cores have a clock, and it was idling at 345 MHz. When CUDA starts up on a device it quietly sends one control command that raises the clocks to 1755 MHz. Nothing in the copy path asks for it. It is a side effect of CUDA's setup, invisible unless every control call is being traced. The hand-built library never sent it, so the GPU stayed in its idle power state, and even the copy engine ran slower.
- Add the one control command CUDA sends at startup: a request to boost performance.
- The kernel module hands the request to the firmware on the GPU.
- The firmware raises the clock from its idle rate to full speed.
6. Measure again
With that one control call added to setup the hand-built copy goes from 38 to 54.8 GB/s, against CUDA's 54.7. Across sizes the two now match. What remains is a fixed cost of about 15 microseconds per copy, for submitting, the semaphore round trip and waking up. It is 11% of a 4 MiB copy and 1% of a 64 MiB one. Small transfers pay for setup and big ones don't notice.
- The same copy, with the clock raised, now runs as fast as CUDA's.
7. The flaky one
The second problem was harder because it only happened sometimes. With the CPU and the GPU using different addresses for the same buffer, between 25 and 34% of 128 MiB round trips came back wrong. No crash and no Xid. The data was simply wrong some of the time. The fix was to stop having two addresses. Reserve one window of address space, about 4 GiB starting at 0x200000000, and map each buffer at the same address on both sides. One pointer, valid on both processors. After that, 1,000 runs out of 1,000 passed.
- With two different addresses for one buffer, about a third of the round trips return wrong data and nothing reports it.
- Map every buffer at the same address on both sides, and every run passes.
8. Round robin trusts the spec sheet
Back at the pool. The wrong assumption inside the GPU was: same hardware, same work, same speed. Round robin makes exactly that assumption on every request. Requests aren't equal. A 128K-token summary and a two-word greeting are both one request. Servers aren't equal either, even when the hardware is. One may be decoding long answers for a dozen users, one may be throttling, one may have the right prefix warm. From outside they look the same. Least connections is a little better and still counts requests instead of work.
- Round robin sends the next request to gpu-2 because it is next, though gpu-2 is clearing its work at a third of the others' rate.
9. Read the register
The F5 endpoint picker is built on the lesson of the clock: read the register, don't trust the spec sheet. It weights load by tokens, so a long prompt counts for what it costs. It measures how fast each worker is really clearing its decode work, as seconds to drain its queue. If a worker is slow right now, for any reason at all, the picker sees it in the numbers without having to know why. And it values a cache hit by the measured cost of fetching it. Weights someone tuned by hand once are their own kind of spec sheet. They are right for the day they were set.
- The picker sends the request to the worker that is really fastest right now.
Summary
A copy built by hand ran a third slower than CUDA's on the same GPU, and every usual check said it should be fine. The cause was a clock left at its idle rate, raised by a control call CUDA makes during setup and never mentions. A second bug returned wrong data a third of the time with no error at all, until both processors used one address for each buffer. Neither fix is in an API reference. Both were found by measuring what was really happening. Routing has the same shape. Things that should behave the same don't, the reason isn't in the configuration, and the only reliable answer is to measure the thing that decides the outcome.
Next lesson: Seeing without slowing down