Repository navigation
perf(executorch): replay CUDA graphs on green context streams - #4984
Closed
shoumikhin wants to merge 2 commits into
Closed
shoumikhin wants to merge 2 commits into
shoumikhin wants to merge 2 commits into
Conversation
… unchanged bindings With use_cuda_graphs on, the TensorRT delegate recorded its graph on buffers it owns, so each call copied every input in and every output out, and set every tensor address again. Every call, with or without CUDA graphs, also gave TensorRT each input shape again and re-ran shape inference, even when nothing had changed. On small models the copies are a large share of the run time with CUDA graphs on. This change removes that work when the caller's buffers and shapes stay the same from call to call, which is the common case: - The graph is recorded on the caller's own input and output buffers. Later calls with the same buffers replay it with no copies. - If the buffers move, that call records again on the new ones. If they move a second time for the same shapes, the delegate falls back to its own buffers and copies, as before, so a caller that alternates between two sets of buffers still replays, with the copies. - execute() remembers the shapes and addresses it last gave the TensorRT context and skips setInputShape, setTensorAddress and inferShapes when they are unchanged, with or without CUDA graphs. A profile switch forgets them, as TensorRT requires. - Each input and output is checked with one cudaPointerGetAttributes call instead of two. The graph tests now cover replay on the caller's buffers with no copies, one move recording again, a second move falling back to the delegate's own buffers, a failed recording on those buffers restoring the caller's, and unchanged shapes not being set again, with and without graphs. The test that reads the wrong-device checks out of TensorRTBackend.cpp now looks for them through the new helper.
shoumikhin
force-pushed
the
et-graph-green-context
branch
from
November 5, 2026 12:52
aa5ed6d to
47f83a5
Compare
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 on a stream created in the current context, so it could not replay on a stream from a green context without running that work on every SM of the GPU. It took the ordinary path for such streams instead, so green contexts never got replay. The capture stream is now created in the caller stream's context: for a green context with cuGreenCtxStreamCreate, otherwise in the current context as before, so a green context's replays stay on its own SMs. Calls from green contexts keep a graph of their own, apart from the one for plain contexts, so an engine called in turn from a green and a plain stream replays on both instead of recording at every switch, and each graph counts its own failed recordings. A call from another green context than the last records again. Green contexts are told apart by their context id, since a destroyed one's handle can be handed out again. A green context stream on another device takes the ordinary path, as any stream on another device does. If the caller destroys a green context while the engine is still loaded, the capture stream left in it can only be destroyed, and that reports an error. The delegate clears that error, so the caller does not find it later.
shoumikhin
force-pushed
the
et-graph-green-context
branch
from
November 5, 2026 13:43
47f83a5 to
d996214
Compare
Contributor
Author
|
Folded into #4983 so both changes can be reviewed and picked together. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #4983. Only the last commit is new here.
What is wrong today
A green context gives its streams a fixed share of the GPU's streaming multiprocessors (SMs). A CUDA graph runs its kernels in the context it was recorded in, not that of the stream that launches it. The TensorRT delegate records on a stream of the current context, where a replay would use every SM, so it never replays on green context streams.
What this change does
cuGreenCtxStreamCreatefor a green context, otherwise in the current context as before. Replays then stay on the green context's SMs, including kernels on TensorRT's auxiliary streams.Benchmark
The synthetic models from #4983, on a 16-SM green context stream with CUDA graphs on. Median time per call (p50) of 3000 calls, then the median over 5 runs, on an H100. Outputs were byte for byte the same as on main.
Tests