Repository navigation
fix(dynamo): release weights before serializing the engine, not after - #5015
Open
shoumikhin wants to merge 1 commit into
Open
shoumikhin wants to merge 1 commit into
shoumikhin wants to merge 1 commit into
Conversation
shoumikhin
force-pushed
the
release-weights-before-serialize
branch
from
November 14, 2026 17:18
9990e9b to
352ef1a
Compare
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
force-pushed
the
release-weights-before-serialize
branch
3 times, most recently
from
November 19, 2026 08:52
8bdfc29 to
8e80752
Compare
micwill755
self-requested a review
November 19, 2026 23:04
shoumikhin
force-pushed
the
release-weights-before-serialize
branch
3 times, most recently
from
November 23, 2026 01:32
44f65da to
5b3bb52
Compare
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
force-pushed
the
release-weights-before-serialize
branch
from
November 25, 2026 16:11
5b3bb52 to
8a58b96
Compare
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.
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_moduletakes an optionalrelease_weightscallback. The conversion calls it after the build, beforeserialize(), and the memory reclaim that already follows the build frees those weights too. On a cache hit,pull_cached_enginecalls it after the refit and gives the weights back from PyTorch's CUDA cache before serializing.WeightReleaser.release_once_builtbuilds it. It releases at most once, and every call returns the bytes freed.compile_modulecalls it again afterconvert_modulereturns. This releases the weights for builds that never call it (a tuning sweep, or a cache hit withstrip_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_compatibleor TensorRT-RTX, the build already returns a serialized plan while the source weights are alive.Nothing changes when
release_weights_after_buildisFalse. With the defaultNone, mapped weight pages are now dropped before serializing too. WithTrue, 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
compile_and_savepass (Linux x86_64, TensorRT 11.4).compile_and_save, host memory during serialization dropped by about the size of the weights.