Skip to content

perf(dynamo): offload modules to CPU without holding both copies - #4793

Merged
narendasan merged 1 commit into
mainfrom
narendasan/compile-mem-incremental-offload
Oct 8, 2026
Merged

narendasan merged 1 commit into
mainfrom
narendasan/compile-mem-incremental-offload

Conversation

@narendasan

@narendasan narendasan commented Oct 7, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • deallocate_module (used by offload_module_to_cpu) moved the module with module.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 with cudaMalloc and can't use those blocks, and on unified memory systems both copies come out of the same DRAM.
  • It now converts through the same _apply that .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=True on 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 with torch.export and compiled with torch_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 /proc every 5 ms and nvidia-smi samples 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.

Stack position Peak host + GPU (default) Peak host + GPU (offload_module_to_cpu) Held after compile Held after deleting the PyTorch model
main 4.66W 4.66W 2.66W 1.67W
1 malloc_trim 4.16W 4.16W 2.16W 1.23W
2 offload 4.16W 4.16W 2.16W 1.22W
3 zero-copy engine setup 3.13W 3.13W 2.16W 1.23W
4 no retained plan 3.13W 3.13W 1.16W 0.24W
5 plan passthrough 2.77W 2.16W 1.16W 0.22W

Tests

  • 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).
  • Sparse parameters move like any other: release pacing only counts strided tensors, since nbytes is 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

@cehongwang cehongwang left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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):

Base automatically changed from narendasan/compile-mem-malloc-trim to main October 8, 2026 17:58
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
narendasan force-pushed the narendasan/compile-mem-incremental-offload branch from 06a6297 to 2f15b93 Compare October 8, 2026 17:58
@narendasan
narendasan merged commit 307ccb6 into main Oct 8, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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):

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla signed component: api [Python] Issues re: Python API component: core Issues re: The core compiler component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths component: tests Issues re: Tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants