Repository navigation
perf(dynamo): offload modules to CPU without holding both copies - #4793
Merged
Merged
Conversation
This was referenced Oct 7, 2026
narendasan
force-pushed
the
narendasan/compile-mem-incremental-offload
branch
from
October 7, 2026 14:22
39ab39d to
4bc7340
Compare
narendasan
force-pushed
the
narendasan/compile-mem-incremental-offload
branch
from
October 8, 2026 17:57
4bc7340 to
06a6297
Compare
There was a problem hiding this comment.
There are some changes that do not conform to Python style guidelines:
--- /home/runner/work/TensorRT/TensorRT/tests/py/dynamo/runtime/test_compile_memory.py 2026-10-08 17:57:52.103951+00:00
+++ /home/runner/work/TensorRT/TensorRT/tests/py/dynamo/runtime/test_compile_memory.py 2026-10-08 17:58:20.566743+00:00
@@ -18,21 +18,23 @@
env = {
k: v
for k, v in os.environ.items()
if k != "TORCHTRT_ENABLE_BUILDER_MALLOC_TRIM"
}
- with mock.patch.object(
- utils, "_glibc_malloc_trim", return_value=malloc_trim
- ), mock.patch.dict(os.environ, env, clear=True):
+ with (
+ mock.patch.object(utils, "_glibc_malloc_trim", return_value=malloc_trim),
+ mock.patch.dict(os.environ, env, clear=True),
+ ):
utils.trim_host_heap()
malloc_trim.assert_called_once_with(0)
def test_env_var_disables_trimming(self):
malloc_trim = mock.Mock(return_value=1)
- with mock.patch.object(
- utils, "_glibc_malloc_trim", return_value=malloc_trim
- ), mock.patch.dict(os.environ, {"TORCHTRT_ENABLE_BUILDER_MALLOC_TRIM": "0"}):
+ with (
+ mock.patch.object(utils, "_glibc_malloc_trim", return_value=malloc_trim),
+ mock.patch.dict(os.environ, {"TORCHTRT_ENABLE_BUILDER_MALLOC_TRIM": "0"}),
+ ):
utils.trim_host_heap()
malloc_trim.assert_not_called()
def test_missing_malloc_trim_is_a_no_op(self):
with mock.patch.object(utils, "_glibc_malloc_trim", return_value=None):deallocate_module() moved the module with module.to("cpu") and emptied the
CUDA cache once at the end, so until the move finished the process held every
weight twice: on the CPU, and in CUDA blocks PyTorch had freed but not released.
TensorRT allocates with cudaMalloc and cannot use those blocks, and on unified
memory systems both copies come out of the same DRAM. Convert through the same
_apply that .to() uses, but empty the cache each time 1 GiB has moved, so the GPU
copy shrinks while the CPU copy grows.
With offload_module_to_cpu=True on a 3 GiB model, the move's peak drops from
1.0x to 0.33x the weight size (host and GPU combined).
Co-Authored-By: Claude Opus 5.5 <[email protected]>
narendasan
force-pushed
the
narendasan/compile-mem-incremental-offload
branch
from
October 8, 2026 17:58
06a6297 to
2f15b93
Compare
There was a problem hiding this comment.
There are some changes that do not conform to Python style guidelines:
--- /home/runner/work/TensorRT/TensorRT/tests/py/dynamo/runtime/test_compile_memory.py 2026-10-08 17:59:42.669826+00:00
+++ /home/runner/work/TensorRT/TensorRT/tests/py/dynamo/runtime/test_compile_memory.py 2026-10-08 18:00:22.522733+00:00
@@ -18,21 +18,23 @@
env = {
k: v
for k, v in os.environ.items()
if k != "TORCHTRT_ENABLE_BUILDER_MALLOC_TRIM"
}
- with mock.patch.object(
- utils, "_glibc_malloc_trim", return_value=malloc_trim
- ), mock.patch.dict(os.environ, env, clear=True):
+ with (
+ mock.patch.object(utils, "_glibc_malloc_trim", return_value=malloc_trim),
+ mock.patch.dict(os.environ, env, clear=True),
+ ):
utils.trim_host_heap()
malloc_trim.assert_called_once_with(0)
def test_env_var_disables_trimming(self):
malloc_trim = mock.Mock(return_value=1)
- with mock.patch.object(
- utils, "_glibc_malloc_trim", return_value=malloc_trim
- ), mock.patch.dict(os.environ, {"TORCHTRT_ENABLE_BUILDER_MALLOC_TRIM": "0"}):
+ with (
+ mock.patch.object(utils, "_glibc_malloc_trim", return_value=malloc_trim),
+ mock.patch.dict(os.environ, {"TORCHTRT_ENABLE_BUILDER_MALLOC_TRIM": "0"}),
+ ):
utils.trim_host_heap()
malloc_trim.assert_not_called()
def test_missing_malloc_trim_is_a_no_op(self):
with mock.patch.object(utils, "_glibc_malloc_trim", return_value=None):
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.
Summary
deallocate_module(used byoffload_module_to_cpu) moved the module withmodule.to("cpu")and emptied the CUDA cache once at the end. Until the move finished, the process held every weight twice: on the CPU, and in CUDA blocks PyTorch had freed but not released. TensorRT allocates withcudaMallocand can't use those blocks, and on unified memory systems both copies come out of the same DRAM._applythat.to()uses, but empties the cache each time 1 GiB has moved, so the GPU copy shrinks as the CPU copy grows. Parameter objects are rebound in place exactly as before.Measured
With
offload_module_to_cpu=Trueon a 3 GiB model, the move's host + GPU peak drops from 1.0W to 0.33W. That stage isn't the overall compile peak until later in this stack.How these numbers were measured
A 3 GiB fp16 model (24 ×
nn.Linear(8192, 8192)) exported withtorch.exportand compiled withtorch_tensorrt.dynamo.compile(..., min_block_size=1)on an RTX 5090 (TensorRT 11.3, torch 2.15 nightly). A sidecar process samples the compiling process's RSS from/procevery 5 ms andnvidia-smisamples GPU memory every 10 ms. Numbers are peak growth over the footprint before compilation starts, which already includes the model's own copy, in multiples of the weight size W. "Host + GPU" is the figure that matters on unified memory systems (Jetson, DGX Spark), where both come out of one pool.offload_module_to_cpu)mainTests
tests/py/dynamo/runtime/test_compile_memory.py::TestDeallocateModule(new): everything lands on the CPU in place, and GPU memory is released between tensors rather than once at the end (fails on the old implementation).nbytesis undefined for sparse ones (test_moves_sparse_parameters).Stack
2 of 5. Base: #4792. Next: #4794.
Together these remove the weight-sized copies compilation made between PyTorch and a running engine, so models compile closer to memory capacity, especially on unified memory systems, without splitting them into more engines. On a 3 GiB model, peak compile memory goes from 4.66W to 2.77W (2.16W with
offload_module_to_cpu), and memory held after deleting the PyTorch model from 1.67W to 0.22W.🤖 Generated with Claude Code