Repository navigation
fix(dynamo): keep, move and refit the tensors that PyTorch nodes read after global partitioning - #4966
Merged
micwill755 merged 3 commits intoNov 3, 2026
Conversation
…r global partitioning
After partitioning, the compiler deletes every _frozen_param attribute
from the top graph module to free CPU memory. The global partitioner
leaves the nodes that run in PyTorch in that graph, so a PyTorch node
that reads a folded constant then fails at run time:
AttributeError: 'GraphModule' object has no attribute '_frozen_param0'
Keep the frozen parameters that the top graph still reads, and delete
the rest as before. The fast partitioner leaves no read of a folded
constant in the top graph, so nothing changes there.
_move_attr tolerated a missing target only because of that deletion.
Every target the top graph reads now exists, so it reads the attribute
directly and a missing one fails where the state is moved.
The new test checks that the constant the PyTorch node reads is kept,
that the one only TensorRT reads is deleted, and that the outputs
match. It fails without this change with that AttributeError.
…ter global partitioning
With offload_module_to_cpu=True, compile() moves the lowered module to
the CPU before partitioning, and constant folding creates its constants
on the CPU. compile_module moves PyTorch-side state back to the device
only for the fast partitioner's PyTorch submodules and for a graph where
nothing converts. The global partitioner leaves the PyTorch nodes in the
top graph, so the state they read stays on the CPU and the first call
fails with a device mismatch:
RuntimeError: Expected all tensors to be on the same device, but
found at least two devices, cuda:0 and cpu!
After the engines are built, call pin_torch_executed_state on the
partitioned module when the global partitioner ran, the same helper the
no-conversion path already uses. This covers folded constants and plain
parameters that the top graph reads.
The offload case of the test fails without this change with that error.
…ule after global partitioning refit_module_weights refits each TensorRT engine and loads the state of every other child module. With the global partitioner, the nodes that run in PyTorch stay in the top graph, and they can read state set on the top module itself: a folded constant, or a parameter of the top model. Refit never touched that state, so after a refit those nodes kept computing with the old weights while the engines used the new ones, and nothing reported it. When the global partitioner is used, copy every tensor that the new top graph reads from the top module into the compiled module, in place. The copy keeps the device and the tensor object, so a source model that shares those parameters keeps sharing them. A tensor whose shape changed fails with the same incompatibility error refit uses elsewhere, rather than being broadcast into the old one. The fast partitioner and modules loaded from an exported program are unchanged. The new test refits a module whose PyTorch nodes read a folded constant and a top-level parameter, with and without offload. It fails without this change because the refitted module still returns the old outputs.
shoumikhin
force-pushed
the
trt-global-partitioner-frozen-params
branch
from
November 2, 2026 17:30
ba98542 to
baf7e66
Compare
lanluo-nvidia
pushed a commit
that referenced
this pull request
Nov 3, 2026
bowang007
added a commit
that referenced
this pull request
Nov 4, 2026
torch_executed_modules was not honored on several paths. The torch.compile backend's AOT retrace dropped nn_module_stack, and Dynamo records module classes where torch.export records qualified names. Lowering passes that rewrite or fuse operators (unsupported-attention decomposition, pad and convolution fusion, fused RMSNorm replacement, dynamic slice_scatter decomposition, associative scan lowering and the legacy complex rewrite) either dropped the provenance of the nodes they create or moved an excluded operator into TensorRT, and ignored torch_executed_ops the same way. Buffer lifting predicted engine aliasing for a cache write of an excluded module, which made compilation fail. - Retrace the Dynamo graph through an fx.Interpreter under preserve_node_meta, so decomposed ATen nodes keep their module stack. - Match module classes and qualified names alike, both in graph metadata and in user entries, and pickle class entries by name. When an entry matches nothing, list the module classes present in the graph. - Add node_is_torch_executed (by operator or by module) and skip such nodes in the rewriting passes above, so PyTorch runs the original operator. For attention, the fused kernel stays exact on fully masked rows, where the decomposition gives NaN. Add inherit_provenance for the nodes the passes create. - After CPU offload, move the state that runs in PyTorch back to the device for both partitioners, in one call merged with the one from #4966. - aten.scalar_tensor: return a rank-0 tensor that is float32 unless a dtype is given, also for a SymInt argument. A rank-1 output did not match the next engine's rank-0 input, and an int32 SymInt gave wrong values in the engine. - Take the fused RMSNorm output index from the getitem, not from the order of the users. Tests: rename the partitioning tests to test_000_* so the standard CI suites run them, use an RTX-aware tolerance (TensorRT-RTX ignores disable_tf32), and add a regression test for each fix. Co-Authored-By: Claude Opus 5.5 <[email protected]>
6 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.
Problem
With the global partitioner (
use_fast_partitioner=False), operators that run in PyTorch stay in the top graph module and read tensors from it, such as folded constants (_frozen_param*) and top-level parameters. The compiler and refit mishandled that state:After partitioning, the compiler deletes every folded constant from the top module to free CPU memory. A PyTorch node that reads one then fails at run time:
For example,
torch_executed_ops={"torch.ops.aten.mul.Tensor"}on a model that computesx * (self.w + 1)hits this, becauseself.w + 1is folded into a constant that the PyTorchmulreads.With
offload_module_to_cpu=True, that state stays on the CPU, so the first call fails withExpected all tensors to be on the same device.refit_module_weightsrefits the engines and the other child modules, but never the tensors on the top module. After a refit, those PyTorch nodes kept using the old weights, with no error.Change
_move_attrno longer has to allow a missing attribute, so it reads it directly.pin_torch_executed_statehelper.Each change only affects the global partitioner (the second also needs offload), so the default fast partitioner path is unchanged. Refit of a module loaded back with
torch_tensorrt.loadstill leaves the tensors that PyTorch nodes read unchanged, with either partitioner. That older gap is out of scope here.Testing
On Linux x86_64 (H100):
test_pytorch_node_keeps_its_folded_constantruns with and without offload,. Its PyTorch nodes read a folded constant and a top-level parameter. It checks that the folded constant is kept, that the one only TensorRT reads is deleted, and that the outputs match. It fails without the first commit, and its offload case fails without the second.test_refit_global_partitioner_updates_top_module_staterefits the same kind of module, with and without offload, and checks that a parameter with a different shape is rejected. It fails without the third commit.