Skip to content

Give each repeated PyTorch graph output its own Var - #2915

Open
sandeepvijayarao09 wants to merge 1 commit into
apple:mainfrom
sandeepvijayarao09:fix-duplicate-graph-outputs
Open

sandeepvijayarao09 wants to merge 1 commit into
apple:mainfrom
sandeepvijayarao09:fix-duplicate-graph-outputs

Conversation

@sandeepvijayarao09

@sandeepvijayarao09 sandeepvijayarao09 commented Nov 6, 2026 •

Copy link
Copy Markdown

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 MIL Var for each output. As a result:

  • the outputs get the same name (['var_2', 'var_2']), and predict returns only one of them on mlprogram;
  • the neuralnetwork model fails to compile: Interface specifies output 'var_2' which is not produced by any layer;
  • user-specified output names don't help: renaming the shared Var for one output renames it for all, so outputs=[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 through mb.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 the Var, or placeholder scopes when the Var is a graph input, so essential-scope validation passes for both TorchScript and torch.export.

Tests: TestInputOutputConversionAPI.test_same_var_returned_as_multiple_outputs returns (y, x + 1, y, y) for a computed and a constant y, 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 on main and 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 main even for a single output (return x with outputs=[TensorType(name="o")] → AssertionError: Main block's input name, 'x', is different from its corresponding var's name, 'o' in sanitize_input_output_names). That's a separate issue, and return x, x with custom names still hits it. Without custom names, return x, x now works.

Testing: macOS 26.6 (Apple silicon), Python 3.12, torch 2.8.0: test_torch_conversion_api.py and test_torch_export_conversion_api.py: 155 tests, 0 failures, 18 skipped.

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Converting forward function with multiple outputs causing confused names

1 participant