Repository navigation
Give each repeated PyTorch graph output its own Var - #2915
Open
sandeepvijayarao09 wants to merge 1 commit into
Open
sandeepvijayarao09 wants to merge 1 commit into
sandeepvijayarao09 wants to merge 1 commit into
Conversation
When a PyTorch model returns the same tensor more than once (e.g. `return y, y`), the converter used the same MIL Var for each output. The outputs then share a name, mlprogram predictions return only one of them, neuralnetwork models fail to compile, and renaming one output renames all of them, so user-specified output names don't help. Route repeated outputs through an identity op before output renaming, as the TensorFlow frontend already does. The identity reuses the scopes of the op that produced the Var (or placeholder scopes for graph inputs), so essential-scope validation passes. Fixes apple#1887.
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.
Fixes #1887.
When a PyTorch model returns the same tensor more than once (e.g.
return y, y, or a constant returned twice), the converter passed the same MILVarfor each output. As a result:['var_2', 'var_2']), andpredictreturns only one of them on mlprogram;Interface specifies output 'var_2' which is not produced by any layer;Varfor one output renames it for all, sooutputs=[TensorType(name="a"), TensorType(name="b")]gives['b', 'b'].The TensorFlow frontend already handles this case ("check duplicate output" in
tensorflow/converter.py) by routing repeated outputs throughmb.identity. This PR does the same in the PyTorch frontend, before output renaming, so each output can be named independently. The identity op reuses the scopes of the op that produced theVar, or placeholder scopes when theVaris a graph input, so essential-scope validation passes for both TorchScript and torch.export.Tests:
TestInputOutputConversionAPI.test_same_var_returned_as_multiple_outputsreturns(y, x + 1, y, y)for a computed and a constanty, on both backends, with default and custom output names. It checks that names are distinct (and match custom names) and that predictions match PyTorch. All 8 cases fail onmainand pass with this change. I also checked torch.export by hand (default and custom names, mlprogram).Not covered: returning a graph input directly as an output and giving it a custom name fails on
maineven for a single output (return xwithoutputs=[TensorType(name="o")]→AssertionError: Main block's input name, 'x', is different from its corresponding var's name, 'o'insanitize_input_output_names). That's a separate issue, andreturn x, xwith custom names still hits it. Without custom names,return x, xnow works.Testing: macOS 26.6 (Apple silicon), Python 3.12, torch 2.8.0:
test_torch_conversion_api.pyandtest_torch_export_conversion_api.py: 155 tests, 0 failures, 18 skipped.