Skip to content

fix(dynamo): declare folded non-tensor user inputs as ConstantArgument in save(retrace=False) - #5063

Open
micwill755 wants to merge 1 commit into
mainfrom
fix/4941-constant-user-inputs
Open

micwill755 wants to merge 1 commit into
mainfrom
fix/4941-constant-user-inputs

Conversation

@micwill755

Copy link
Copy Markdown
Collaborator

Description

torch_tensorrt.save(..., retrace=False) raised KeyError when the model has non-tensor user inputs that torch.export folds to constants, such as scale: float, mask=None or use_relu=True. create_trt_exp_program declared every user input as a TensorArgument, while torch.export declares these as ConstantArgument.

This change has two parts:

  • create_trt_exp_program now declares a user input as ConstantArgument(name, value) when its placeholder meta["val"] is None or an int, float, bool or str. Tensor and symbolic inputs are unchanged.
  • lift() now finds user inputs by name from the input specs instead of ExportGraphSignature.user_inputs, which lists a ConstantArgument by value. Without this, a constant input placed before a tensor input (forward(self, scale, x)) gets lifted placeholders inserted after it, and the saved file can't be loaded.

The saved program loads and runs with the folded values. Calling it with a different value is rejected the same way a torch.export program rejects it: Expected input at 'scale' to be equal to 2.0, but got 3.0.

Dynamic float inputs (SymFloat) and the retrace=True input checks are out of scope, as stated in the issue.

Fixes #4941

Type of change

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

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

Testing

New tests in tests/py/dynamo/models/test_export_serde.py:

  • test_save_load_folded_non_tensor_inputs: covers float plus None, None alone, float alone and a bool flag, each with static and dynamic batch. It saves with retrace=False, loads, checks the inputs are recorded as ConstantArgument, compares output against eager, and checks that a different scale is rejected.
  • test_save_load_folded_input_before_tensor_input: puts the constant before the tensor input and checks placeholder order after lift().

Without the fix, the first test fails in all 8 cases with the issue's KeyError. The second fails if only the lift() change is reverted.

I ran this on an RTX 5090 with torch 2.16 nightly and the Python runtime. All 9 new tests pass. The rest of the file gives the same results as main: test_save_load_ts and test_save_load_aoti fail with or without this change, likely because this environment lacks the C++ runtime.

…t in save(retrace=False)

create_trt_exp_program declared every user input as a TensorArgument, so
non-tensor inputs that torch.export folds to constants (e.g. scale: float,
mask=None, use_relu=True) made torch.export.save raise KeyError. Declare
them as ConstantArgument, matching torch.export, and have lift() find user
inputs by spec name, since ExportGraphSignature.user_inputs lists a
ConstantArgument by value.

Fixes #4941

Signed-off-by: Michael Williams <[email protected]>
@micwill755 micwill755 added this to the v2.16.0 milestone Nov 16, 2026
@micwill755 micwill755 added bug Something isn't working component: api [Python] Issues re: Python API component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths labels Nov 16, 2026
@meta-cla meta-cla Bot added the cla signed label Nov 16, 2026
@github-actions
github-actions Bot requested a review from zewenli98 November 16, 2026 21:13
@github-actions github-actions Bot added component: tests Issues re: Tests component: core Issues re: The core compiler labels Nov 16, 2026
@micwill755
micwill755 requested a review from 5had3z November 25, 2026 18:27

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

bug Something isn't working 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.

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

1 participant