Repository navigation
Description
Follow-up to #4983. That PR lets the ExecuTorch TensorRT delegate replay CUDA graphs on CUDA green context streams. This issue explains the solution, lists the parts of the design to review, and proposes benchmarks with VLA (vision-language-action) models.
The problem
A CUDA graph runs its kernels in the context it was recorded in, not in the context of the stream that launches it. The delegate recorded its graph on a stream of the regular context, so replaying it on a green context stream would have run on every SM and broken the partition. To avoid that, a call on a green context stream never replayed: it ran enqueueV3 instead. The results were correct, but green context users got no CUDA graph speedup.
The solution in #4983
- Record in the green context. For a call on a green context stream, the delegate creates a temporary capture stream inside that green context (
cuGreenCtxStreamCreate) and records there, so replays stay on its SMs.recording_context()reads the stream's context and green context throughcuStreamGetCtx_v2(CUDA 12.5+). - A graph per green context. An engine keeps a graph for each of up to 4 green contexts, plus one for regular streams, keyed by context id (
cuCtxGetId) because a destroyed green context's handle can be reused. A fifth green context evicts the one called longest ago. All green graphs share one record of the input shapes, so new shapes on any of them reset all of them. - Lifetimes. TensorRT requires a green context to outlive the graphs recorded in it, so the engine must be destroyed before its green contexts. An engine loaded while a green context is current is created in the device's primary context instead, so that green context can be destroyed first.
- Fallbacks. Some context combinations run without a graph (for example a green context stream while a regular context other than the device's primary one is current).
Validation so far
On an RTX 5090 (driver 595.99, TensorRT 11.4, #4983 at 588b8ae), a chain of 8 fp16 4096x4096 matmuls, 30 calls each, under Nsight Systems:
| Setup | Graph launches | Kernels ran in | GPU time per call |
|---|---|---|---|
| Normal stream, graphs off | 0 | regular context | 1.67 ms |
| Normal stream, graphs on | 29 | regular context | 1.66 ms |
| Green 32 SMs, graphs off | 0 | green context | 6.64 ms |
| Green 32 SMs, graphs on | 29 | green context | 6.63 ms |
| Green 8 SMs, graphs off | 0 | green context | 26.27 ms |
| Green 8 SMs, graphs on | 29 | green context | 26.27 ms |
Green context streams replay, every replayed kernel is tagged with the green context, and replay time matches the partition's own time. A replay that escaped the partition would take about 1.7 ms on 8 SMs. Not covered: one engine switching between green contexts, engines with auxiliary streams, and output values.
Design to review
- Multiple green graphs per engine. Which workloads call one engine from more than one green context? Is a cache of 4 with least-recently-used eviction the right policy, or would one green graph plus the regular one cover the real cases with less state?
- Shared shape record. New shapes on one green context reset every green graph. Should each graph track its own shapes?
- Memory. Each green graph may hold its own input and output buffers. What does that cost for large models?
- TensorRT's green context build constraint. On green context streams TensorRT 11.4 logs: "Optimization profile 0 was built without CUDA green context constraints. Enqueue will continue, but this configuration may cause enqueue crashes or other undefined behavior." Should Torch-TensorRT expose that build option for engines meant to run on green contexts?
- Auxiliary streams. Kernels TensorRT runs on its own auxiliary streams can run outside the partition. Should the delegate warn, or should export default to
max_aux_streams=0for green context use? - State machine.
ExecutionGraphnests oneExecutionGraphper green context and tracks many flags. Would an explicit mode and a separate green graph container be clearer?
Benchmarks with VLA models
VLA models are the main reason to use green contexts here: a heavy vision and language backbone runs at a few Hz, and a light action head runs at a high rate, and a partition keeps them from slowing each other down.
Models: pi0.5 (examples/torchtrt_executorch_example/export_lerobot_pi05.py), SmolVLA, GR00T N1.5, OpenVLA.
Setups:
- Whole model on one green context, graphs on and off, at a few partition sizes.
- Backbone and action head as separate methods on separate green contexts, running concurrently.
- Partition resized at runtime, so one engine switches green contexts.
- More than 4 green contexts in turn, to exercise eviction.
Metrics: p50 and p99 latency per call, action rate, number of recordings, extra device memory per green graph, latency interference from a concurrent workload on the rest of the GPU, and outputs compared with graphs off.
Test scripts for the validation above (reference runner, export, nsys summary) can be shared with anyone who wants to reproduce or extend them.
Activity
shoumikhin commentedon Nov 10, 2026
@micwill755 Thanks for testing this and writing it up. One note before you run setup 3: keep the old green context alive until the engine is destroyed, because the engine still holds a graph recorded in it. Destroying it first breaks TensorRT's lifetime rule. The other points are good follow-up topics, especially the build-time green context option and auxiliary streams.