Skip to content

fix(dynamo): a dry run moves no weights - #5069

Open
cehongwang wants to merge 1 commit into
mainfrom
cehongw/dry-run-moves-no-weights
Open

cehongwang wants to merge 1 commit into
mainfrom
cehongw/dry-run-moves-no-weights

Conversation

@cehongwang

Copy link
Copy Markdown
Collaborator

Description

A dry run builds and runs no engine, but with offload_module_to_cpu=True it still moved weights: compile offloaded the module to the CPU, and compile_module moved the parts that run in PyTorch back to the GPU. Both consequences are reproduced by the new tests on main:

  • Weights still on meta, which a dry run accepts on purpose (pre_export_lowering skips the meta-weights check, since a dry run only needs shapes), made the move back raise:
    NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty() ...
      File "py/torch_tensorrt/dynamo/_compiler.py", line 1830, in compile_module
        submodule.to(to_torch_device(settings.device))
    
  • A GPU model came out of a dry run with the weights of its TensorRT parts on the CPU, since only the PyTorch parts are moved back.

CompilationSettings.__post_init__ now turns offload_module_to_cpu off when dryrun is set, as _reporting.py already does for its own dry runs. compile and cross_compile_for_windows read the setting from settings instead of their argument, and skip the low-GPU-memory warning ("Consider setting offload_module_to_cpu=True") in a dry run, which builds no engine.

Found while testing low_memory(model_weights_on_meta=True) (#5005), whose compile_settings turn offload on: a dry run to inspect the partitions of a model built on meta crashed.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Testing

torch 2.16.0.dev20261029, TensorRT 11.4, A40, Python runtime:

  • New test_dry_run_moves_no_weights and test_dry_run_of_weights_on_meta in tests/py/dynamo/partitioning/test_000_fast_partitioning.py, each with the fast and the global partitioner: all 4 fail on main (a weight left on the CPU; Cannot copy out of meta tensor) and pass with the fix.
  • test_000_fast_partitioning.py: 15 passed. test_000_global_partitioning.py, test_000_require_full_compilation.py, test_dryrun_stats_observable.py: 30 passed. test_low_memory.py, test_buffer_lifting.py: 91 passed. test_reporting_stages.py: 6 passed. test_003_cross_compile_for_windows.py skips here (no TensorRT Windows library), so CI covers the cross_compile_for_windows change.
  • pre-commit: every hook passes except mypy's 4 "Untyped decorator" errors in _compiler.py, which main has at the same lines.

Checklist:

  • My code follows the style guidelines of this project (You can use the linters)
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas and hacks
  • I have made corresponding changes to the documentation
  • I have added tests to verify my fix or my feature
  • New and existing unit tests pass locally with my changes
  • I have added the relevant labels to my PR in so that relevant reviewers are notified

A dry run builds and runs no engine, but with offload_module_to_cpu it still moved
weights: compile offloaded the module to the CPU, and compile_module moved the parts that
run in PyTorch back to the GPU. On weights still on meta, which a dry run accepts, the
move back raised "Cannot copy out of meta tensor"; on a GPU model, the dry run left the
weights of its TensorRT parts on the CPU.

CompilationSettings now turns offload_module_to_cpu off for a dry run, as the reporting
path already does for its own dry runs, and compile and cross_compile_for_windows read it
from the settings and skip the low-GPU-memory warning in a dry run.
@cehongwang cehongwang added this to the v2.16.0 milestone Nov 18, 2026
@cehongwang cehongwang added component: tests Issues re: Tests component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths labels Nov 18, 2026
@meta-cla meta-cla Bot added the cla signed label Nov 18, 2026
@github-actions github-actions Bot added component: core Issues re: The core compiler component: api [Python] Issues re: Python API labels Nov 18, 2026

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

1 participant