Skip to content

fix(executorch): share tensors when copying a program for preprocess - #4775

Merged
lanluo-nvidia merged 1 commit into
pytorch:mainfrom
shoumikhin:executorch-share-constants-for-preprocess
Oct 2, 2026
Merged

lanluo-nvidia merged 1 commit into
pytorch:mainfrom
shoumikhin:executorch-share-constants-for-preprocess

Conversation

@shoumikhin

Copy link
Copy Markdown
Contributor

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 full copy.deepcopy, and that copies every tensor too.

For TensorRT, the partition holds the serialized engine as a uint8 tensor in its state dict. So lowering keeps a second copy of the engine alive while preprocess runs. For a large model that is several gigabytes of host memory, for nothing: TensorRTBackend.preprocess only 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 TensorRTBackend with 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.

tensor_memo = {
    id(t): t
    for values in (edge_program.state_dict, edge_program.constants)
    for t in values.values()
    if isinstance(t, torch.Tensor)
}
return copy.deepcopy(edge_program, tensor_memo)

preprocess reads the engine tensor and never writes to it, so sharing is safe.

ExecuTorch uses this hook in to_edge_transform_and_lower, which is what torch_tensorrt.save(..., output_format="executorch") calls.

What was tested

  • New unit test: the copy shares the state dict tensor with the source program, and still has its own graph module and state dict mapping. It fails before this change and passes after it.
  • test_backend.py passes.
  • Saved a large model with a 5.9 GiB engine on an aarch64 board with unified memory, with this change and the companion change that copies the engine only once in preprocess. The copy before preprocess used 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.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants