Repository navigation
fix(dynamo): declare folded non-tensor user inputs as ConstantArgument in save(retrace=False) - #5063
Open
micwill755 wants to merge 1 commit into
Open
micwill755 wants to merge 1 commit into
micwill755 wants to merge 1 commit into
Conversation
…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]>
This branch has not been deployed
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.
Description
torch_tensorrt.save(..., retrace=False)raisedKeyErrorwhen the model has non-tensor user inputs thattorch.exportfolds to constants, such asscale: float,mask=Noneoruse_relu=True.create_trt_exp_programdeclared every user input as aTensorArgument, whiletorch.exportdeclares these asConstantArgument.This change has two parts:
create_trt_exp_programnow declares a user input asConstantArgument(name, value)when its placeholdermeta["val"]isNoneor anint,float,boolorstr. Tensor and symbolic inputs are unchanged.lift()now finds user inputs by name from the input specs instead ofExportGraphSignature.user_inputs, which lists aConstantArgumentby 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.exportprogram rejects it:Expected input at 'scale' to be equal to 2.0, but got 3.0.Dynamic float inputs (
SymFloat) and theretrace=Trueinput checks are out of scope, as stated in the issue.Fixes #4941
Type of change
Checklist:
Testing
New tests in
tests/py/dynamo/models/test_export_serde.py:test_save_load_folded_non_tensor_inputs: covers float plusNone,Nonealone, float alone and a bool flag, each with static and dynamic batch. It saves withretrace=False, loads, checks the inputs are recorded asConstantArgument, compares output against eager, and checks that a differentscaleis rejected.test_save_load_folded_input_before_tensor_input: puts the constant before the tensor input and checks placeholder order afterlift().Without the fix, the first test fails in all 8 cases with the issue's
KeyError. The second fails if only thelift()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_tsandtest_save_load_aotifail with or without this change, likely because this environment lacks the C++ runtime.