Skip to content

fix(executorch): copy only the moving bindings through CUDA graph buffers - #5044

Closed
shoumikhin wants to merge 1 commit into
pytorch:mainfrom
shoumikhin:cuda-graph-moving-bindings
Closed

shoumikhin wants to merge 1 commit into
pytorch:mainfrom
shoumikhin:cuda-graph-moving-bindings

Conversation

@shoumikhin

@shoumikhin shoumikhin commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

What is wrong today

With CUDA graphs on, the ExecuTorch TensorRT delegate replays on the caller's buffers while they stay put, and moves all of them to buffers of its own once they keep moving. So if only 1 buffer moves, every input and output is copied on every call.

This hurts programs that pass weights as engine inputs while outputs move on each call, as through the ExecuTorch Python runtime: every call copies all the weights, and each program keeps a second copy of them on the GPU.

What this change does

The graph now tracks each input and output separately:

  • One that moves twice goes through a buffer of the graph's own, as before.
  • One that moves before a graph is recorded does so until its address repeats.
  • Every other one stays in place and is not copied. When it moves, the graph records again.
  • A graph buffer nothing reads is freed at the next recording.
  • A call from another regular CUDA context replays if it brings the same addresses for the buffers read in place. Otherwise it records (and can fail) where it often replayed before, and then every plain-stream call copies everything until the shapes or the profile change. Green context calls keep their own graphs.

When all buffers move together and no other context calls, nothing changes. Otherwise N buffers moving at different calls can cost up to 3N+1 recordings per set of shapes in each graph, or more if the shared scratch grows, another context calls, or a recording fails. Some patterns record more often than before, and a buffer that moved twice stays copied after it stops moving.

What was tested

  • New tests in test_execution_graph.cpp cover weights that stay put, one output that moves alone (also on a green context), settling back in place, new shapes, a failed allocation, and calls from another CUDA context. All but the alternating-sets test (a guard for kept behavior) fail without this change.
  • The delegate's C++ suites pass on an A100 against the ExecuTorch 1.6 and TensorRT 11.4 wheels (2-GPU cases skipped).
  • CI has not run the C++ suite yet: the wheel build fails on a c10d header the torch nightly removed, which fix: build, test and lower on PyTorch nightly, and let Debugger work again #5059 fixes.
  • With 1.7 GiB of fp16 weights passed as inputs and shared by 2 programs, device memory went from about 7.8 GiB to 4.4 GiB, same outputs.

@meta-cla meta-cla Bot added the cla signed label Nov 14, 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 14, 2026
@github-actions
github-actions Bot requested a review from narendasan November 14, 2026 22:41
@shoumikhin
shoumikhin force-pushed the cuda-graph-moving-bindings branch 2 times, most recently from cd2503b to cdcf5e9 Compare November 15, 2026 07:03
@shoumikhin
shoumikhin force-pushed the cuda-graph-moving-bindings branch 2 times, most recently from de08eb1 to 3627a49 Compare November 18, 2026 19:31
@shoumikhin
shoumikhin force-pushed the cuda-graph-moving-bindings branch from 3627a49 to 3593e72 Compare November 19, 2026 00:01
@shoumikhin
shoumikhin force-pushed the cuda-graph-moving-bindings branch 2 times, most recently from 560edaa to 3086079 Compare November 21, 2026 12:25
…fers

With CUDA graph replay on, the TensorRT delegate records the graph on the
caller's buffers while they stay put. Once they keep moving, it switches
every binding to buffers of its own, and every call copies all inputs in
and all outputs out.

That is costly when a program takes large inputs that never move, such as
weights passed in as engine inputs, while its outputs arrive at a new
address on each call, as they do through the ExecuTorch Python runtime.
Every call then copies all the weights, and each program keeps a second
copy of them on the device.

The graph now tracks each binding on its own. A binding that moves twice
goes through a buffer of the graph's own, as before. A binding that moves
while no graph is recorded does so until it repeats its address. Every
other binding stays on the caller's address and is not copied, and when
one of them moves, the graph records again on its new address. A buffer
no recording reads any more is freed at the next recording. When all
bindings move at the same calls and no call comes from another regular
CUDA context, this is the old behavior. Bindings that move at different
calls each count their own moves, so N of them can cost up to 3N+1
recordings for one set of shapes in each graph. A growth of the shared
scratch, a call from another regular CUDA context, or a recording that
fails or is skipped can add more. Some patterns record more often than
before, and a binding that moved twice while another waited for a
repeat stays copied after it stops moving.

A call from another regular CUDA context replays the graph when it brings
the same addresses for the bindings the graph reads and writes in place.
Otherwise it records on buffers of the graph's own for every binding, and
from then on every call on a plain stream copies every binding until the
shapes or the profile change. Such a call used to replay whenever the
graph already copied every binding, so it can now record, or fail, where
it used to replay. If that recording fails, the engine keeps its own
graph, as before. Calls from a green context are not affected, since
each green context keeps a graph of its own. The backend README, the user
guide and the documentation of the CUDA graphs option describe the new
behavior.

Tests in test_execution_graph.cpp:
- AnInputThatStaysPutIsReadInPlaceWhileTheOutputMoves: the input stays
  put and the output moves. The input is read in place, only one buffer
  is allocated, a replay copies only the output, and moving the input
  records again on its new address.
- AWeightInputStaysInPlaceWhileAnActivationInputMoves: with 3 inputs,
  a weight input stays put next to a longer activation input that moves
  twice. Only the activation is copied, in full, and the weight, the
  other input and every output stay in place. When the other input then
  moves twice too, both moving inputs are copied and the weight still is
  not.
- AnOutputThatMovesTwiceIsCopiedAloneWhileTheOtherOutputsStayPut: with
  3 outputs, only output_1 moves twice. It alone is copied out, in full
  although it is longer than output_0, and every input and the other
  outputs stay in place.
- AnInputThatMovedBeforeTheFirstRecordingGoesBackInPlaceOnceItRepeats:
  the input goes back in place once it repeats and its buffer is freed,
  while the moving output is still copied.
- AStagedInputThatRepeatsWhileTheOutputMovesGoesBackInPlaceInOneRecording:
  an input that repeats on the call where the output moves goes back in
  place in that call's recording, so 4 calls record twice, and one that
  moves again on that call stays staged.
- NewShapesKeepOnlyTheBuffersThatMovedTwiceStaged: under new shapes the
  output that moved twice stays staged and the input stays in place.
- StagingCarriesAcrossShapeChanges: across 2 shape changes, the output
  that moved twice stays staged, the input that moved once does not, and
  an input staged until it repeats settles without the output.
- AlternatingSetsUnderNewShapesRecordOnceAfterAnOutputWasStaged: a caller
  that alternates 2 whole sets of buffers still records once for each
  set of shapes and first replays on the 3rd call.
- AFailedAllocationForAnInputKeepsAnOutputThatMovedTwiceStaged: when
  the buffer for an input that waits for a repeat cannot be allocated,
  that input goes back in place, and an output that moved twice stays
  staged, so later calls still record and replay.
- AnOutputThatWaitsForARepeatDoesNotCountTheMovesOfAnInput: an output
  staged until it repeats does not count the moves of the input next to
  it, so it goes back in place when it repeats.
- FailedRecordingsFromAnotherRegularContextLeaveAnOwnGraphThatCopiesOnlyItsOutput:
  a failed recording for another context keeps the engine's own graph,
  which still reads its input in place.
- ACallFromAnotherRegularContextReplaysAPartlyCopiedGraphOnItsInPlaceAddresses:
  a call from another context that brings the address the graph reads in
  place replays the graph without recording and copies only the output.
- CallsAlternatingWithAnotherRegularContextReplayOneGraph: calls that
  alternate with another context record once, then replay one graph
  instead of recording at every switch, and both contexts get the right
  outputs.
- AGreenContextGraphReadsAnInputThatStaysPutInPlaceWhileTheOutputMoves:
  on a green context stream too, the input is read in place while the
  output moves, and a replay copies only the output.

All new tests but the AlternatingSets one fail without this change. That
one guards behavior the change must keep.

Signed-off-by: Anthony Shoumikhin <[email protected]>
@shoumikhin
shoumikhin force-pushed the cuda-graph-moving-bindings branch from 3086079 to fd29a62 Compare November 22, 2026 18:38
@shoumikhin

Copy link
Copy Markdown
Contributor Author

Folded into #5075 to keep the number of PRs down. This PR's commits are in #5075.

@shoumikhin shoumikhin closed this Nov 23, 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