Repository navigation
feat(dynamo): release model weights once their last engine is built - #4853
Open
cehongwang wants to merge 4 commits into
Open
cehongwang wants to merge 4 commits into
cehongwang wants to merge 4 commits into
Conversation
A new opt-in setting, release_weights_after_build. TensorRT holds a network's weights only until its engine is built, and a built engine carries its own copy, yet the model, the ExportedProgram and the partitioned graph modules keep every weight referenced until compilation ends. With several engines (resource partitioning, PyTorch fallbacks) compilation therefore holds all weights while building each engine. With the setting on, compile_module counts, for each weight storage, the partitions that read it. After each TensorRT partition is built it releases the storages no remaining partition reads, and before the first build the lowered module's parameters that no partition reads (for example a model head the traced function never calls). Storages read by PyTorch-run partitions are counted but never released. The storage itself is released (resize_(0)), because dropping references cannot free what the caller's model still points to, so the source module must not be used afterwards. Buffers are never released as unread: mutable buffers are lifted into engine inputs and inlined back into the compiled module. A release-handler hook lets an owner release its own storages (for example memory-mapped weights) instead. Measured on pi0.5 (lerobot, bf16, Anthony Shoumikhin's ExecuTorch export, CPU-resident model, resource partitioner with 9 engines, A40): host + GPU peak 25.7 -> 18.9 GiB (4.85 GiB released engine by engine, 1.90 GiB of never-read weights, mostly the two lm_heads, before the first engine).
This was referenced Oct 12, 2026
…orchTensorRTModule, release on the single-engine path
cehongwang
added a commit
that referenced
this pull request
Oct 13, 2026
cehongwang
added a commit
that referenced
this pull request
Oct 13, 2026
cehongwang
added this pull request to stack #4859
October 13, 2026 08:04
cehongwang
marked this pull request as ready for review
October 13, 2026 08:04
5 tasks
narendasan
reviewed
Oct 15, 2026
narendasan
left a comment
Collaborator
There was a problem hiding this comment.
Think this one looks mostly good, non invasive from a user level, do you think in a future version this should become the default?
Collaborator
Author
|
This ruins the original model, so users cannot run the pytorch model anymore. Making it default could lead to some confusion |
Collaborator
Author
|
We can make it automatically when weights are mapped from safetensors though. That's safe |
This branch has not been deployed
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.
Description
Adds an opt-in compilation setting,
release_weights_after_build, that frees each model weight once the last TensorRT engine that reads it is built.TensorRT holds a network's weights only until its engine is built, and a built engine carries its own copy. Yet the model, the
ExportedProgramand the partitioned graph modules keep every weight referenced until compilation ends. With several engines (resource partitioning, PyTorch fallbacks), compilation therefore holds all of a model's weights while it builds each engine. Torch-TensorRT already frees constant folding's_frozen_paramtensors after each build (conversion/_conversion.py); the model's own parameters are not covered.With the setting on,
compile_module:get_attr), including partitions that run in PyTorch;Storages read by PyTorch partitions or by the partitioned module's own graph are never released. Buffers are never released as unread: mutable buffers are lifted into engine inputs and inlined back into the compiled module. A storage that wasn't seen at partition time (for example, a weight rebound since) is kept.
The storage itself is released (
resize_(0)), because dropping references can't free what the caller's model still points to. So the source module, and anExportedProgramexported from it, must not be used after compilation, even if compilation fails; hence opt-in. Storages that can't be resized, such as views of an external buffer, are skipped. #4854, stacked on this PR, adds memory-mapped weights, which are released by dropping their pages instead.Also:
require_full_compilationwith every op supported) releases too;MutableTorchTensorRTModulerejects the setting, because it refits and recompiles from the source weights;strip_engine_weights, a warning says to refit from a freshly loaded checkpoint;torch.compile, where parameters arrive as graph inputs; the docstrings say so.Measurements
pi0.5 (lerobot, bf16, Anthony Shoumikhin's ExecuTorch export), model kept on the CPU through compilation, resource partitioner (9 engines), A40, TensorRT 11.0; host RSS + GPU memory, load phase excluded:
4.85 GiB was released engine by engine, and 1.90 GiB of weights the graph never reads (mostly the two
lm_heads, 1004 + 502 MiB) before the first engine. Engines and latency are unchanged.Testing
tests/py/dynamo/models/test_release_weights_after_build.py(13 tests):MutableTorchTensorRTModulerejects the setting.tests/py/dynamo/runtime/test_mutable_torchtrt_module.pypasses.tests/py/dynamo/partitioning/also passes, excepttest_000_fallback_reporting.py::test_fallback_is_reported_1_complex_dtype_refusal, which fails the same way onmainin my environment (torch 2.13).Type of change
Checklist: