Streams, events and CUDA graphs: timing, overlap and launch overhead
How CUDA orders work with streams, how to overlap copies with compute using pinned memory and several streams, how to express dependencies with events, and how CUDA graphs remove per-launch CPU overhead, first in C and then the way inference engines use them through PyTorch, with static buffers and one graph per batch size.
By Sandip Maity, Applied AI Engineer
So far each lesson has been about one kernel. A real inference step is hundreds of them, plus copies between the CPU and GPU. How CUDA orders and launches all that work decides whether the GPU spends its time computing or waiting. The fusion and launch overhead lesson showed the cost; this one shows the tools.
#Streams
A stream is a queue of GPU work. Two rules:
- Within a stream, work runs in order. A kernel doesn't start until the previous operation in its stream has finished.
- Different streams may run at the same time. The GPU is free to overlap them if it has spare resources, or to interleave them.
If you don't name a stream, you get the default stream. Its legacy behaviour is to synchronise with every other stream, which quietly prevents overlap. Compile with --default-stream per-thread or create your own streams to avoid surprises.
cudaStream_t s1, s2;
cudaStreamCreate(&s1); cudaStreamCreate(&s2);
kernel_a<<<grid, block, 0, s1>>>(...); // the 4th launch argument is the stream
kernel_b<<<grid, block, 0, s2>>>(...); // may overlap with kernel_a#Overlapping copies with compute
A GPU has separate copy engines (DMA) that move data over PCIe while the SMs compute. To use them in parallel with kernels:
- Allocate host memory as pinned (page-locked) with
cudaMallocHost. With ordinarymallocmemory, the driver must stage the data through a pinned buffer, and the copy stops being truly asynchronous. - Use
cudaMemcpyAsyncwith a stream. - Split the work into chunks, and give each chunk its own stream: copy in, compute, copy out.
const int chunks = 4, n = 1 << 24, cn = n / chunks;
float *h, *d;
cudaMallocHost(&h, n * sizeof(float)); // pinned
cudaMalloc(&d, n * sizeof(float));
cudaStream_t st[chunks];
for (int i = 0; i < chunks; i++) cudaStreamCreate(&st[i]);
for (int i = 0; i < chunks; i++) {
float* hp = h + i * cn; float* dp = d + i * cn;
cudaMemcpyAsync(dp, hp, cn * sizeof(float), cudaMemcpyHostToDevice, st[i]);
process<<<(cn + 255) / 256, 256, 0, st[i]>>>(dp, cn);
cudaMemcpyAsync(hp, dp, cn * sizeof(float), cudaMemcpyDeviceToHost, st[i]);
}
cudaDeviceSynchronize();While chunk 2 is computing, chunk 3 can be copying in and chunk 1 copying out. Toggle between one stream and four and watch the timeline compress:
Streams (one chunk per stream)
7.00 time units versus 13 with one stream: 46% shorter. The floor is the longest stage, 5 units, plus a little fill and drain.
Copy in 4 units, compute 5, copy out 4, split into equal chunks. Copy engines and SMs work in parallel when chunks are in different streams and host memory is pinned. Bars are scaled to the one-stream total.
In inference this pattern shows up in KV cache offloading (moving blocks between GPU and CPU memory while the model runs), weight streaming for models that don't fit, and loading a model while the previous one is still serving. Engines also overlap the CPU's scheduling work for step with the GPU's execution of step . That's a different overlap, between CPU and GPU, but the principle is the same: keep every engine of the machine busy.
Pro content
Keep reading with Inference Academy Pro
Unlock the rest of this lesson, plus every advanced lesson, hands-on lab, the larger playground models and completion certificates.
Still to come in this lesson
- Events: timing and dependencies
- CUDA graphs
- How engines use graphs
- What you can now do
- Common mistakes
- Exercise
- Going deeper
You will be able to answer
- Two kernels are launched into the same stream. What does CUDA guarantee?
- Why must host memory be pinned for cudaMemcpyAsync to overlap with kernels?
- A decode step launches 400 small kernels, each taking 3 microseconds on the GPU. Eager launches cost about 10 microseconds each on the CPU. Roughly what does a CUDA graph save?
- Why do inference engines capture a separate CUDA graph for each of several batch sizes?
- Which operation silently breaks CUDA graph capture in PyTorch code?
Articles that build on this lesson