Repository navigation
fix(executorch): share tensors when copying a program for preprocess - #4775
Merged
lanluo-nvidia merged 1 commit intoOct 2, 2026
Merged
lanluo-nvidia merged 1 commit into
lanluo-nvidia merged 1 commit into
Conversation
ExecuTorch deep-copies each partition before it calls a backend's preprocess. By default that copies every tensor in the program, and for TensorRT the program holds the serialized engine as a uint8 tensor. So lowering kept a second copy of the engine alive, which for a large model is gigabytes of host memory. TensorRTBackend.preprocess only reads tensors. Override copy_exported_program_for_preprocess to share the program's tensors through the deepcopy memo, the same way the ExecuTorch CUDA backend does in its low-memory mode. The graph, the signature and the state-dict mapping are still copied.
3 of 7 tasks
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
Before ExecuTorch calls a backend's
preprocess, it makes a copy of each partition, so a backend that edits the graph cannot change the caller's program. The default copy is a fullcopy.deepcopy, and that copies every tensor too.For TensorRT, the partition holds the serialized engine as a
uint8tensor in its state dict. So lowering keeps a second copy of the engine alive whilepreprocessruns. For a large model that is several gigabytes of host memory, for nothing:TensorRTBackend.preprocessonly reads tensors.What this change does
ExecuTorch lets a backend choose how that copy is made, through
BackendDetails.copy_exported_program_for_preprocess. The ExecuTorch CUDA backend already overrides it in its low-memory mode.This change overrides it in
TensorRTBackendwith the same memo approach, but always, not only in a low-memory mode: tensors from the state dict and the constants go into the deepcopy memo, so the copy shares them. Everything else is still copied: the graph module, the graph signature, and the state dict mapping itself.preprocessreads the engine tensor and never writes to it, so sharing is safe.ExecuTorch uses this hook in
to_edge_transform_and_lower, which is whattorch_tensorrt.save(..., output_format="executorch")calls.What was tested
test_backend.pypasses.preprocess. The copy beforepreprocessused to add 5.9 GiB and now adds almost nothing. Both runs also freed the PyTorch model before saving and returned freed heap memory to the system. With that setup, the peak memory of the save fell from 40.9 GiB to 28.8 GiB, and the saved program was byte for byte the same as before.