Skip to content

perf(executorch): replay CUDA graphs on green context streams - #4984

Closed
shoumikhin wants to merge 2 commits into
pytorch:mainfrom
shoumikhin:et-graph-green-context
Closed

shoumikhin wants to merge 2 commits into
pytorch:mainfrom
shoumikhin:et-graph-green-context

Conversation

@shoumikhin

@shoumikhin shoumikhin commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

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

  • The delegate creates its capture stream in the caller stream's context: with cuGreenCtxStreamCreate for 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.
  • Green context calls keep a graph of their own, apart from the plain one. So an engine called in turn from a green and a plain stream replays on both, rather than recording at every switch. A call from another green context than the last records again there.
  • A green context stream on another device is refused, like any stream there, and the graph is kept.
  • If the caller destroys a green context while the engine is loaded, destroying the delegate's stream there reports an error. The delegate clears it for the caller.

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.

Model main with #4983 with this change
MLP, 1 input 33.8 µs 32.1 µs 28.8 µs
MLP, 8 inputs, 2 outputs 37.8 µs 30.2 µs 24.4 µs
4-layer conv net, 128x128 46.7 µs 45.8 µs 38.5 µs

Tests

  • New C++ tests: a green context stream records and replays in its own context, also through the delegate's own buffers when the caller's move; bursts alternating between a green and a plain stream replay on both; failed green recordings leave plain replay on; a destroyed green context leaves no CUDA error; a green context stream on another device keeps the graph (2 GPUs).
  • An Nsight Systems trace on a 16-SM green context shows every replayed kernel there, including those on auxiliary streams.
  • All ExecuTorch C++ test binaries pass on an H100 with 2 GPUs visible. The new tests also pass on a Jetson AGX Thor.

@meta-cla meta-cla Bot added the cla signed label Nov 5, 2026
@github-actions github-actions Bot added documentation Improvements or additions to documentation component: tests Issues re: Tests component: api [C++] Issues re: C++ API labels Nov 5, 2026
@github-actions
github-actions Bot requested a review from narendasan November 5, 2026 12:28
… 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
shoumikhin force-pushed the et-graph-green-context branch from aa5ed6d to 47f83a5 Compare November 5, 2026 12:52
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

Copy link
Copy Markdown
Contributor Author

Folded into #4983 so both changes can be reviewed and picked together.

@shoumikhin shoumikhin closed this Nov 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla signed component: api [C++] Issues re: C++ API component: tests Issues re: Tests documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant