Model and Inference EngineeringEnd-to-end serving and modalitiesStaff
How would you diagnose wrong-request outputs during CUDA graph replay?
By Amit Kumar3 min readWorked answer includedReviewed 28 Sept 2026
The question
Interview question
A serving engine captures common decode batches into CUDA graphs. Replay reduces launch overhead, but concurrent requests sometimes receive tokens matching another request’s prefix. Eager execution is correct with the same weights. Explain capture and replay semantics, design a minimal reproduction, and trace the memory ownership and synchronization checks required before release.
Draft your approach first
Take a few minutes before you read the answer. Name the constraint that matters most, the failure you are designing for and how you would know it worked.
Reveal the worked answerWorked answer
A CUDA graph records GPU operations for replay. In the usual captured PyTorch path, replay reads and writes persistent memory addresses, while the application updates the values at those addresses for each new request. Faster launch does not validate those values. If the engine leaves a previous request’s token IDs, positions or cache-page mapping in the buffers, correct kernels can produce an answer for the wrong request. Treat that as a correctness and possible disclosure incident, and route the affected class through a known-correct path during investigation.
NVIDIA's CUDA graph guidance describes the static-address requirement and the danger of rebinding a tensor instead of copying new data into persistent graph inputs. In a serving engine, there are more moving parts than a plain tensor batch. The scheduler may reuse slots as requests finish, and the captured decode step may read metadata that identifies which KV pages belong to which sequence. I would inspect every input that crosses the capture boundary, including pointer arrays and device-side scalar tensors, not just the visible input_ids tensor.
My smallest reproduction would use two unmistakably different prompts, deterministic sampling and a fixed batch shape that alternates slot membership. Run eager and graph modes from the same cache state and compare logits before sampling, then compare token IDs. If logits diverge, check the actual input and metadata bytes immediately before replay and after synchronization. If logits agree but sampled tokens differ, inspect sampling seed and per-request RNG state, output slot mapping and any CPU-side postprocessing. Make the failing shape repeat at low concurrency first, then add cancellation, preemption, prefix cache hits and request replacement one at a time.
The order of operations is part of the bug search. The host must copy each new input into the persistent buffers, ensure those copies complete on the relevant stream before replay reads them, and not reuse output storage until the consumer has finished. A graph key should represent every shape and mode that changes captured work or memory interpretation. If the scheduler pads a smaller batch into a captured bucket, masks and valid lengths must say which slots are real. A stale KV block table can mix contexts even with a correct token ID. Look for an alias to a freed or recycled allocation and for a metadata update made after replay was queued.
I would add a canary that compares graph and eager logits on representative transitions, including a batch that loses one request and gains another. It need not run on every production token. Keep per-request traces of graph key, slot generation, KV ownership and output mapping with privacy-safe IDs, so a future mismatch is diagnosable. The release gate is exact behavior for deterministic fixtures, no cross-request content, and acceptable differences only where floating-point execution order reasonably explains small logit variation. A speedup is real only if it preserves the request boundary. A generic eager fallback is a valid temporary outcome.
Related questions
Keep practising model and inference engineering
Follow another question in this area, or search the complete library.
Get my essays on AI engineering and distributed systems by email. Subscribe to ArchCrux