Skip to content

fix: preserve frozen input signatures and PyTorch export/runtime compatibility - #4952

Open
970334745 wants to merge 4 commits into
pytorch:mainfrom
970334745:fix/exporter-constant-input-signatures
Open

970334745 wants to merge 4 commits into
pytorch:mainfrom
970334745:fix/exporter-constant-input-signatures

Conversation

@970334745

@970334745 970334745 commented Oct 30, 2026 •

Copy link
Copy Markdown

Problem and resulting behavior

Fixes #4941. save(retrace=False) rebuilt frozen Python inputs as tensor arguments, so serializing an exported model with scale=2.0 or mask=None could raise KeyError. Preserve ConstantArgument after graph-state lifting, including positional and keyword constants, dynamic batches, and constants preceding parameters or buffers.

Current PyTorch also changes fake-metadata cache keys, stops re-exporting several TorchScript entry points, and attaches execution guards to unlifted modules. The compatibility changes below let these export/runtime paths use current PyTorch while preserving input validation and the older cache/API contracts.

Changes

  • Invalidate missing fake metadata by node in a node-keyed mapping, retaining hash invalidation for the older OrderedSet representation.
  • Resolve the original TorchScript script, trace, save, and load callables, preferring public exports when present. Original signatures, keyword filtering, extra files, and error propagation remain intact; the global torch.jit namespace is not modified. Legacy API fixtures use the same resolver.
  • Remove only exact PyTorch-generated GuardsFn modules when rebuilding a canonical ExportedProgram. Preserve authoritative range constraints and constant specs so the rebuilt program regenerates its input guards, while retaining upstream fake-mode discovery for integer-only inputs. Tests reject out-of-range batches and changed frozen values, and retain user-defined modules/subclasses even when named _guards_fn.
  • Recognize ModelOpt quantizers through their class hierarchy without importing an optional dependency to inspect an unrelated model. This includes user subclasses and the mutable-module path. Quantized export still uses its real ModelOpt export context and propagates dependency errors.
  • Run cache, TorchScript compatibility, and optional-dependency detection regressions in the existing CPU CI lane. Existing rejection checks remain active, including AssertionError from newer native input guards; no new skip guards or numerical tolerance changes are introduced.

Validation

Python 3.12.3, torch==2.16.0.dev20261116+cpu, CMake 4.4.5, pytest 9.2.0:

  • CPU API/cache/compatibility/detection suite: 230 passed, 0 failed, 27 skipped. The exact set of existing platform/package skips is unchanged from the matched baseline.
  • Engine-free exporter/lifting component suite: 27 passed, 0 skipped. This includes the original constant-input regressions, generated/user-guard cases, and two engine-independent lifting cases. Only unused native TensorRT import boundaries are isolated; PyTorch graph construction, guards, serialization and execution are real.
  • Before/after checks reproduce 12 original loader/cache failures on 50e8c516, and 12 optional-dependency detection plus 23 export-guard failures on 6ec631f0; the repaired cases pass.
  • PyTorch 2.11 compatibility check: 26 targeted regressions passed, without skips.
  • Black 26.10.1 under Python 3.12 and git diff --check pass. Ruff reports no new undefined-name/syntax findings; two pre-existing F841 findings in the mutable module are unchanged.

Full current-head GPU/build results remain subject to the upstream fork workflow approval process; CPU component checks do not substitute for those jobs.

The released ModelOpt 0.49.0rc0 has a separate import-time @torch.jit.script incompatibility affecting actual quantized execution. The corresponding dependency fix is proposed in NVIDIA/Model-Optimizer#3409; quantized execution needs that fix in the installed dependency. Ordinary model inspection is isolated from that optional failure here; quantized execution still requires the corresponding dependency repair rather than silently changing its behavior.

Declare frozen int, float, bool, str, and None inputs as ConstantArgument after lifting graph state. Add save/load regressions for positional and keyword inputs, dynamic shapes, lifted parameters and buffers, and zero-dimensional tensors.

Fixes pytorch#4941

Signed-off-by: 王毅 <[email protected]>
@meta-cla

meta-cla Bot commented Oct 30, 2026

Copy link
Copy Markdown

Hi @970334745!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@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 Oct 30, 2026
@meta-cla

meta-cla Bot commented Oct 30, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@meta-cla meta-cla Bot added the cla signed label Oct 30, 2026
@meta-cla

meta-cla Bot commented Oct 30, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@github-actions
github-actions Bot requested a review from cehongwang October 30, 2026 10:06
@970334745

Copy link
Copy Markdown
Author

@cehongwang, could you please review this exporter fix and approve the fork workflow runs when convenient?

The Meta CLA check is now successful. For the current head 50e8c51687c5e06ad4e78a9b140d800ec574e450, the Linux x86_64, Windows, Windows ARM64 and SBSA runs are still action_required; the lint workflow also has an action_required run. For example, Linux x86_64 run 41038297416 has no test jobs yet. These workflows therefore have not reported a build or test failure in this patch; they still need a maintainer to authorize execution.

The PR already records the 22 constant-input regression cases plus three existing lifting tests, the ordering constraint after lift(), and the limits of the local PyTorch 2.8 compatibility setup. I would appreciate current-main CI coverage before treating those local results as upstream validation. No checks or tolerances have been relaxed.

Invalidate missing fake metadata by Node on current PyTorch while preserving the older OrderedSet representation. Resolve retained TorchScript implementations without mutating torch.jit, preserving native callable signatures and legacy load/save behavior. Cover both cache contracts and real CPU serialization, and run the regressions in the existing CPU CI lane.

Signed-off-by: 王毅 <[email protected]>
@github-actions github-actions Bot added the component: lowering Issues re: The lowering / preprocessing passes label Nov 17, 2026
@970334745 970334745 changed the title fix: preserve frozen constant input signatures during export fix: preserve frozen input signatures and current PyTorch compatibility Nov 17, 2026
Canonicalize only PyTorch-generated GuardsFn nodes while retaining authoritative input ranges and constant guards. Detect ModelOpt quantizers without importing an incompatible optional dependency, including user subclasses. Route legacy TorchScript API fixtures through retained native implementations. Real PyTorch 2.16 CPU suites pass 224 cases plus 27 engine-free export/lift regressions; platform/package exclusions remain unchanged.

Signed-off-by: 王毅 <[email protected]>
@970334745 970334745 changed the title fix: preserve frozen input signatures and current PyTorch compatibility fix: preserve frozen input signatures and PyTorch export/runtime compatibility Nov 17, 2026
Retain upstream fake-mode discovery for integer-only inputs together with authoritative input ranges. The exact merged source passed 230 CPU API/compatibility checks and 27 real PyTorch export/lifting component checks on the CI-day nightly.

Signed-off-by: 王毅 <[email protected]>
@970334745

Copy link
Copy Markdown
Author

@cehongwang, could you approve the fork workflows for 92ccd40 when you have a chance? The Nov 17 update addresses the exporter/runtime compatibility failures surfaced by the previous CI run. The current Linux x86_64 run is marked action_required and has no jobs listed. The focused Linux CPU checks passed (230 compatibility/API and 27 exporter tests). I’d appreciate upstream CI on this revision and any feedback on the updated patch.

@970334745

Copy link
Copy Markdown
Author

For the current 92ccd40 revision, could you confirm which CI jobs are required before this exporter fix can be reviewed for merge? The six fork runs are still awaiting authorization. The previous failures were in Linux tests and compatibility/lint checks, so approval for Linux x86_64 and Lint Code would help verify those paths first. If the broader compatibility changes should be split from the constant-input fix, feedback on the preferred split would also be welcome.

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: lowering Issues re: The lowering / preprocessing passes component: runtime component: tests Issues re: Tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 [Bug] torch_tensorrt.save(retrace=False) raises KeyError for float and None inputs folded in as constants

1 participant