Skip to content

fix(dynamo): keep, move and refit the tensors that PyTorch nodes read after global partitioning - #4966

Merged
micwill755 merged 3 commits into
pytorch:mainfrom
shoumikhin:trt-global-partitioner-frozen-params
Nov 3, 2026
Merged

micwill755 merged 3 commits into
pytorch:mainfrom
shoumikhin:trt-global-partitioner-frozen-params

Conversation

@shoumikhin

@shoumikhin shoumikhin commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

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:

    AttributeError: 'GraphModule' object has no attribute '_frozen_param0'
    

    For example, torch_executed_ops={"torch.ops.aten.mul.Tensor"} on a model that computes x * (self.w + 1) hits this, because self.w + 1 is folded into a constant that the PyTorch mul reads.

  • With offload_module_to_cpu=True, that state stays on the CPU, so the first call fails with Expected all tensors to be on the same device.

  • refit_module_weights refits 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

  1. Keep the folded constants that the top graph still reads, and delete the rest as before. _move_attr no longer has to allow a missing attribute, so it reads it directly.
  2. With offload on, move the state the top graph reads back to the device after the engines are built, with the existing pin_torch_executed_state helper.
  3. In refit, copy every tensor that the new top graph reads from the top module into the compiled module, in place. A tensor whose shape changed is rejected with the usual incompatibility error.

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.load still 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_constant runs 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_state refits 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.
  • The partitioning, refit, mutable module and constant folding tests give the same results with and without this change.

@meta-cla meta-cla Bot added the cla signed label Nov 2, 2026
@github-actions github-actions Bot added component: tests Issues re: Tests 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 2, 2026
@github-actions
github-actions Bot requested a review from zewenli98 November 2, 2026 02:09
…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
shoumikhin force-pushed the trt-global-partitioner-frozen-params branch from ba98542 to baf7e66 Compare November 2, 2026 17:30
@shoumikhin shoumikhin changed the title fix(dynamo): keep frozen constants that PyTorch nodes still read after global partitioning fix(dynamo): keep, move and refit the tensors that PyTorch nodes read after global partitioning Nov 2, 2026

@5had3z 5had3z 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

@micwill755
micwill755 merged commit e50eb79 into pytorch:main Nov 3, 2026
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]>
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.

3 participants