Skip to content

fix(dynamo): release weights before serializing the engine, not after - #5015

Open
shoumikhin wants to merge 1 commit into
pytorch:mainfrom
shoumikhin:release-weights-before-serialize
Open

shoumikhin wants to merge 1 commit into
pytorch:mainfrom
shoumikhin:release-weights-before-serialize

Conversation

@shoumikhin

@shoumikhin shoumikhin commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

fix(dynamo): release weights before serializing the engine, not after

What is wrong today

With release_weights_after_build, compile frees a weight once no engine still to be built reads it. That runs after the engine is serialized. So during serialization, memory holds both the source weights and the serialized engine, which has its own copy of them.

For a large model built as one engine, that is about the size of the weights extra (host memory for a model on the CPU, device memory on the GPU). An engine cache hit has the same problem.

What this change does

  • convert_module takes an optional release_weights callback. The conversion calls it after the build, before serialize(), and the memory reclaim that already follows the build frees those weights too. On a cache hit, pull_cached_engine calls it after the refit and gives the weights back from PyTorch's CUDA cache before serializing.
  • WeightReleaser.release_once_built builds it. It releases at most once, and every call returns the bytes freed.
  • compile_module calls it again after convert_module returns. This releases the weights for builds that never call it (a tuning sweep, or a cache hit with strip_engine_weights=True). If the build released anything, it then frees what the build left behind, as before, once per engine. So cache hits stay as fast as before.

Limits: the TensorRT build can still be the peak. With version_compatible or TensorRT-RTX, the build already returns a serialized plan while the source weights are alive.

Nothing changes when release_weights_after_build is False. With the default None, mapped weight pages are now dropped before serializing too. With True, if a step after the release fails, that engine's weights are already gone, which the setting's docs allow. The error asking to reload the model is now also logged on Ctrl-C.

Testing

  • New tests check which weights are released, and their memory reclaimed, at each serialize (one engine, 2 engines sharing a weight, mapped weights). They fail without this change.
  • A cache hit test (whole-graph and partitioned): the refit engine is serialized after the release, and the full reclaim runs once.
  • Tests for a tuning sweep, and for the error after a failure or Ctrl-C.
  • Existing tests for weight release, low memory, weight mapping, the engine cache and compile_and_save pass (Linux x86_64, TensorRT 11.4).
  • On a large mixture-of-experts model built as one engine with compile_and_save, host memory during serialization dropped by about the size of the weights.

@meta-cla meta-cla Bot added the cla signed label Nov 12, 2026
@github-actions github-actions Bot added component: tests Issues re: Tests component: conversion Issues re: Conversion stage component: core Issues re: The core compiler component: api [Python] Issues re: Python API component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths labels Nov 12, 2026
@github-actions
github-actions Bot requested a review from apbose November 12, 2026 02:23
@shoumikhin
shoumikhin force-pushed the release-weights-before-serialize branch from 9990e9b to 352ef1a Compare November 14, 2026 17:18
shoumikhin added a commit to shoumikhin/TensorRT that referenced this pull request Nov 15, 2026
…5021 on main

Brings the grouped GEMM kernel on this branch to the current version of
pytorch#5021 on main. The kernel is opt-in through a grouped_mm_kernel setting (default
False). It declines, with one warning that names the reason, when
Triton, the driver's PTX limit, the GPU, hardware compatibility or
Windows cross-compilation rule it out. A build failure is cached and
logged once. Rows are masked so invalid offsets, past mat1 or negative,
never read or write outside mat1 or the output. The dense converter's
warning suggests the setting only when it is off and the kernel would
likely take the op, checked without starting Triton. The tests are the
same as on main, including engine-only exports run in a fresh process
that loads only TensorRT and PyTorch, and a .pte run by the ExecuTorch
runtime.

It adapts to the release branch's kernel helpers. The plugin launch is
still built in the kernel module, since this branch has no
_common.make_pointer_aot_fn. The 48 KiB shared memory limit is a named
constant in kernels/_common.py, used by the triton_op check, its error
message and this kernel, as on main. compile_triton_to_ptx here hints
every pointer as 16-byte aligned; it gets an optional per-pointer
pointer_alignment, with the old behaviour as the default, so the kernel
can leave the offsets unhinted as it does on main.

This branch is release/2.15 (5aca374) with these release/2.15 ports of
pull requests on main, oldest first:

- pytorch#5014: fix(conversion): leave large aten._grouped_mm to PyTorch instead of the dense lowering
- pytorch#5016: fix(kernels): reject AOT kernels that need more than 48 KiB of shared memory
- pytorch#5017: fix(kernels): give triton_op kernels Triton's pointer alignment hint
- pytorch#5021: feat(conversion): run aten._grouped_mm in TensorRT with a grouped GEMM kernel
- pytorch#5021: fix(conversion): build the grouped GEMM plugin launch directly on release/2.15
- pytorch#5020: feat(dynamo): convert stable sort of index tensors with a counting sort
- pytorch#5020: fix(dynamo): counting sort: add the key count to the engine cache key, convert single rows only
- pytorch#5019: fix(dynamo): keep a select of a uint8 tensor inside the TensorRT engine
- pytorch#5022: fix(dynamo): keep a cast to uint8 inside the TensorRT engine
- pytorch#5023: Compute antialiased resize weights like PyTorch's CUDA kernel
- pytorch#5024: Convert antialiased bilinear resize by default
- pytorch#5021: fix(conversion): compile the grouped GEMM kernel without tensor memory on sm_100 and newer
- pytorch#5015: fix(dynamo): release weights before serializing the engine, not after
- pytorch#5021: feat(conversion): make the grouped GEMM kernel opt-in, as in pytorch#5021 on main

Signed-off-by: Anthony Shoumikhin <[email protected]>
shoumikhin added a commit to shoumikhin/TensorRT that referenced this pull request Nov 15, 2026
…5021 on main

Brings the grouped GEMM kernel on this branch to the current version of
pytorch#5021 on main. The kernel is opt-in through a grouped_mm_kernel setting (default
False). It declines, with one warning that names the reason, when
Triton, the driver's PTX limit, the GPU, hardware compatibility or
Windows cross-compilation rule it out. A build failure is cached and
logged once. Rows are masked so invalid offsets, past mat1 or negative,
never read or write outside mat1 or the output. The dense converter's
warning suggests the setting only when it is off and the kernel would
likely take the op, checked without starting Triton. The tests are the
same as on main, including engine-only exports run in a fresh process
that loads only TensorRT and PyTorch, and a .pte run by the ExecuTorch
runtime.

It adapts to the release branch's kernel helpers. The plugin launch is
still built in the kernel module, since this branch has no
_common.make_pointer_aot_fn. The 48 KiB shared memory limit is a named
constant in kernels/_common.py, used by the triton_op check, its error
message and this kernel, as on main. compile_triton_to_ptx here hints
every pointer as 16-byte aligned; it gets an optional per-pointer
pointer_alignment, with the old behaviour as the default, so the kernel
can leave the offsets unhinted as it does on main.

This branch is release/2.15 (5aca374) with these release/2.15 ports of
pull requests on main, oldest first:

- pytorch#5014: fix(conversion): leave large aten._grouped_mm to PyTorch instead of the dense lowering
- pytorch#5016: fix(kernels): reject AOT kernels that need more than 48 KiB of shared memory
- pytorch#5017: fix(kernels): give triton_op kernels Triton's pointer alignment hint
- pytorch#5021: feat(conversion): run aten._grouped_mm in TensorRT with a grouped GEMM kernel
- pytorch#5021: fix(conversion): build the grouped GEMM plugin launch directly on release/2.15
- pytorch#5020: feat(dynamo): convert stable sort of index tensors with a counting sort
- pytorch#5020: fix(dynamo): counting sort: add the key count to the engine cache key, convert single rows only
- pytorch#5019: fix(dynamo): keep a select of a uint8 tensor inside the TensorRT engine
- pytorch#5022: fix(dynamo): keep a cast to uint8 inside the TensorRT engine
- pytorch#5023: Compute antialiased resize weights like PyTorch's CUDA kernel
- pytorch#5024: Convert antialiased bilinear resize by default
- pytorch#5021: fix(conversion): compile the grouped GEMM kernel without tensor memory on sm_100 and newer
- pytorch#5015: fix(dynamo): release weights before serializing the engine, not after
- pytorch#5021: feat(conversion): make the grouped GEMM kernel opt-in, as in pytorch#5021 on main

Signed-off-by: Anthony Shoumikhin <[email protected]>
@shoumikhin
shoumikhin force-pushed the release-weights-before-serialize branch 3 times, most recently from 8bdfc29 to 8e80752 Compare November 19, 2026 08:52
@micwill755
micwill755 self-requested a review November 19, 2026 23:04

@micwill755 micwill755 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

@shoumikhin
shoumikhin force-pushed the release-weights-before-serialize branch 3 times, most recently from 44f65da to 5b3bb52 Compare November 23, 2026 01:32
With release_weights_after_build, compile frees a model weight once no
engine still to be built reads it. Today that happens after
convert_module returns, which is after the engine was serialized. So
while the engine is serialized, memory holds both the source weights and
the serialized engine, which has its own copy of the same weights. For a
large model built as one engine, this holds about the size of the weights
more memory during serialization than needed. The TensorRT build itself
can still be the overall peak, so this lowers what serialization holds,
not always the peak. The source weights are host memory when the model is
on the CPU (offload_module_to_cpu or mapped weights), and device memory
when it is on the GPU.

The engine already holds its own copy of the weights, so serializing it
does not need the source copy. convert_module now takes an optional
release_weights callback. The conversion calls it right after the build,
before serialize(), and the memory reclaim that already follows the build
then frees the released weights too. On an engine cache hit,
pull_cached_engine calls it right after it refits the cached engine, then
gives the released weights back from PyTorch's CUDA cache, which TensorRT
cannot use, before it serializes the engine. With version_compatible or
TensorRT-RTX, the build itself returns a serialized plan, so that plan
still exists next to the source weights for a while.

The callback releases at most once, and every call returns the bytes
that release freed. compile_module calls it again after convert_module
returns. That releases the weights for builds that never call it (a
tuning sweep, or a cache hit with strip_engine_weights=True). It also
tells compile_module whether this build released anything, so it still
frees what the build left behind afterwards, as before. A cache hit
still runs that full memory reclaim once per engine, so it is as fast as
before.

Nothing changes when release_weights_after_build is False. With the
default None, the pages of weights mapped with map_weights are now
dropped before the serialize too. With True, if a step after the release
fails (cache write, serialize, engine setup) in a build or a refit cache
hit, that engine's weights are already released, which the setting's
docs already allow: the source model must not be used after a failed
compile. When this happens during conversion, the error that says to
reload the model is logged, now also when the compile is interrupted
with Ctrl-C.

New tests:
- which weights are already released, and their memory reclaimed, when
  each engine is serialized, for one engine and for 2 engines that share
  a weight;
- the same for mapped weights, and that their pages were really dropped
  where the platform can drop pages;
- an engine cache hit, on the whole-graph and the partitioned paths: the
  refit engine is serialized after its weights are released and given
  back from PyTorch's cache, with strip_engine_weights=True the weights
  are still released, and either way the full memory reclaim runs once,
  after the release;
- memory is freed after a build that released weights;
- a tuning sweep does not get the callback, so the weights stay alive
  through it, and they are released after the sweep;
- a failure or Ctrl-C after the release logs the error that says to
  reload the model.

Signed-off-by: Anthony Shoumikhin <[email protected]>
@shoumikhin
shoumikhin force-pushed the release-weights-before-serialize branch from 5b3bb52 to 8a58b96 Compare November 25, 2026 16:11

This branch has not been deployed

No deployments
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: conversion Issues re: Conversion stage 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