I Removed the CPU from the LLM Decode Loop. It Didn't Make It Faster.
I wondered whether letting the GPU control the decode loop would speed up generation. Asynchronous CUDA Graph replay already kept the GPU busy, and a step-level EOS check reduced the remaining gap to about a millisecond when starting the next job in the same slot.
I wondered whether decoding would get faster if the GPU decided when to run the next token. The current token, the KV cache, and the stopping state already lived on the device. Why have the CPU drive the loop?
I tried it with Qwen2.5-0.5B-Instruct on a 4 GiB RTX 3050 Laptop GPU under WSL2. The runs used batch size one, greedy decoding, and the same model weights and kernels. I prepared the prompt state before timing. The CPU still set up and submitted each job. In the device-controlled version, the GPU ran every decode step and stopped on its own.
How far ahead can the CPU queue?
Every decode step reads the state produced by the previous one. With the token ID and state already in device memory, the host can queue the next step before the previous one finishes. CUDA runs the graph instances in order on the same stream.
I captured one decode step as a CUDA graph. In asynchronous replay, the host queued one instance per token up to the generation budget. In the device-controlled version, I put the same step inside a Conditional WHILE node. The last kernel in the step updated the state and set the predicate for the next iteration. NVIDIA CUDA Graphs
The host-side submission reduces to this for the two asynchronous modes. replay contains one decode step. autonomous contains the same step inside the device loop.
1
2
3
4
5
6
7
if (mode == Mode::kAutonomous) {
CheckCuda(cudaGraphLaunch(autonomous, stream));
return;
}
for (int step = 0; step < steps; ++step) {
CheckCuda(cudaGraphLaunch(replay, stream));
}
| Mode | Submission pattern | Throughput, 32 input and 64 output tokens |
|---|---|---|
| Eager | The CPU launches each kernel | 136.4 tokens/s |
| Synchronous replay | The CPU waits after each graph launch | 145.7 tokens/s |
| Asynchronous replay | The CPU queues all 64 graph launches | 148.6 tokens/s |
| Conditional WHILE | The CPU launches once and the GPU runs the loop | 148.7 tokens/s |
Across the 16 combinations of prompt and output length, WHILE ranged from 0.41% slower to 0.53% faster than asynchronous replay. In the 64-token case, the replay submissions used about 169 µs of process CPU time, compared with 62 µs for the WHILE submission. The whole decode took about 431 ms. In the replay runs, the next step was already queued while the GPU was still executing the current one.
The asynchronous replay and WHILE curves nearly overlap across the fixed-work matrix.
I also paused the submitting process with SIGSTOP after it queued its work. Both the WHILE job and the prequeued replay completed during the pause in three trials each. The submitting process could stay asleep once the GPU had its work.
What happens to the queue after EOS?
A fixed output length tells the host exactly how many graphs to queue. I tried a more awkward case with a budget of 256 tokens and the prompt “What is 2 + 2? Reply with only the number.” The model produced 4 and then EOS, its end-of-sequence token.
In my initial replay, each kernel checked the finished flag and returned immediately after EOS. The stream still dispatched the nodes in each of the 254 graph instances already queued behind those two useful steps. The result was ready on the device at about 12.9 ms. The buffered path waited for the stream before collecting it, so the measured completion time was 81.63 ms. WHILE ended its loop as soon as it committed EOS and finished in 13.11 ms.
I tried two changes to replay. First, I queued eight steps at a time and checked the finished flag between windows. Then I kept the full queue and put a single guard at the start of each step. A small GPU kernel reads the flag and sets a Conditional IF predicate. Once EOS arrives, the IF skips the entire decode graph body. The remaining 254 instances each execute one guard in stream order and skip the model kernels. CUDA Conditional IF nodes
Replay evaluates this guard at the entrance to each prequeued graph. WHILE updates the same kind of predicate at the end of its loop body, after committing the token.
1
2
3
4
__global__ void SetReplayCondition(const DecodeState* state,
cudaGraphConditionalHandle condition) {
cudaGraphSetConditional(condition, state->status == JobStatus::kRunning);
}
| Actual output, including EOS | Full-budget replay | Replay in windows of 8 | Replay with a step-level IF | Conditional WHILE |
|---|---|---|---|---|
| 2 tokens | 81.63 ms | 14.98 ms | 14.20 ms | 13.11 ms |
| 24 tokens | 229.11 ms | 159.81 ms | 162.12 ms | 159.86 ms |
| 40 tokens | 331.30 ms | 269.60 ms | 268.83 ms | 268.26 ms |
These are medians from seven runs per case, measured from submission to stream completion. For the two-token answer, the device finished the useful work at about 12.9 ms in every mode. The median interval from committing EOS to completing the stream fell from 68.04 ms with per-kernel checks to 1.07 ms with the step-level IF. WHILE had no extra graph instances to drain. That is where the roughly 1 ms gap came from.
Can the reader see that millisecond?
The previous test waits for the whole stream before collecting the result. With streaming, both modes published each committed token to the same mapped host buffer. A CPU relay sent the tokens through a local pipe, and an independent process timed their arrival. I also prequeued the first step of a second job on the same CUDA stream to see when it could start reusing the original GPU slot.
For the arithmetic prompt, the visible token arrived at 6.79 ms with IF Replay and 6.78 ms with WHILE. Across 15 prompt and budget combinations, the gap in median arrival time for the last non-EOS token stayed below 0.9%. The reader had its token while the replay stream continued through its queue.
The next job in the same slot still waited for that queue. In the three early-EOS cases with a 256-token budget, the median interval from the final token commit to the next job’s start was 1.05–1.10 ms with IF Replay and 8–9 µs with WHILE. With a 64-token budget, the savings fell to 0.11–0.28 ms. When the job used the full budget, the gap was about 9 µs in both modes.
The left panel measures arrival at a local reader. The right panel measures the same-slot handoff with a GPU clock.
What stayed on the critical path?
Moving the loop to the device reduced work the CPU could already overlap with GPU execution. With a step-level guard and streaming, the difference I could still measure was an earlier handoff after an unusually early EOS. The model kernels still took roughly the same time to produce the tokens.
These measurements cover one slot and local token delivery. In a batched service or a server with several streams, the relevant question would be whether earlier slot reuse advances another request.
Cover photograph by Fritzchens Fritz, CC0 1.0.
