Repository navigation
fix(executorch): copy only the moving bindings through CUDA graph buffers - #5044
Closed
shoumikhin wants to merge 1 commit into
Closed
shoumikhin wants to merge 1 commit into
shoumikhin wants to merge 1 commit into
Conversation
shoumikhin
force-pushed
the
cuda-graph-moving-bindings
branch
2 times, most recently
from
November 15, 2026 07:03
cd2503b to
cdcf5e9
Compare
shoumikhin
force-pushed
the
cuda-graph-moving-bindings
branch
2 times, most recently
from
November 18, 2026 19:31
de08eb1 to
3627a49
Compare
This was referenced Nov 18, 2026
shoumikhin
force-pushed
the
cuda-graph-moving-bindings
branch
from
November 19, 2026 00:01
3627a49 to
3593e72
Compare
shoumikhin
force-pushed
the
cuda-graph-moving-bindings
branch
2 times, most recently
from
November 21, 2026 12:25
560edaa to
3086079
Compare
…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
force-pushed
the
cuda-graph-moving-bindings
branch
from
November 22, 2026 18:38
3086079 to
fd29a62
Compare
Contributor
Author
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.
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:
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
test_execution_graph.cppcover 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.