Skip to content

Replace getattr with constant when buffer is a graph output#2685

Closed
john-rocky wants to merge 1 commit into
apple:mainfrom
john-rocky:fix-getattr-as-output
Closed

Replace getattr with constant when buffer is a graph output#2685
john-rocky wants to merge 1 commit into
apple:mainfrom
john-rocky:fix-getattr-as-output

Conversation

@john-rocky
Copy link
Copy Markdown
Contributor

Summary

  • remove_getattr_nodes raised when a model directly returned a registered buffer / parameter (e.g. forward(self): return self.buf):

    RuntimeError: my_constant_output2 should not be in the graph outputs.

  • The pre-existing flatten_graph_output_values pass already lifts buffer-returning getattr nodes into the graph outputs, so the bare raise left no escape hatch — there is no other op that lowers them.

  • Replace each output-producing getattr with a constant node carrying the buffer value (looked up in graph.params). The downstream constant op handler then materializes it as a const var. Intermediate getattr nodes are still dropped as before, since their consumers read directly from graph.params.

Test plan

  • New unit tests TestTorchPasses::test_remove_getattr_nodes_with_output_buffer and ::test_remove_getattr_nodes_drops_intermediate in test_passes.py.
  • All existing TestTorchPasses tests still pass (7 passed, 1 xfailed).
  • End-to-end conversion of the issue repro succeeds and mlmodel.predict({}) returns the original buffer values (verified locally with minimum_deployment_target=ct.target.iOS18, since empty-input models also need PR Raise a clear error when a no-input model targets below iOS18 #2684's spec-version bump).

The original issue author @tritolol proposed essentially this fix in the issue description; this PR implements it with a defensive lookup against graph.params, params propagation through nested blocks, and a unit test.

Fixes #2538.

`remove_getattr_nodes` raised when a model directly returned a
registered buffer / parameter (e.g. `forward(self): return self.buf`):

  RuntimeError: <name> should not be in the graph outputs.

The pre-existing `flatten_graph_output_values` pass already lifts
buffer-returning getattr nodes into the graph outputs, so the bare
`raise` had no useful effect — there is no other op that lowers them.

Replace each output-producing getattr with a `constant` node carrying
the buffer value (read from `graph.params`). The downstream `constant`
op handler then materializes it as a const var. Intermediate getattr
nodes are still dropped as before, since their consumers read directly
from `graph.params`.

Fixes apple#2538.
@john-rocky john-rocky force-pushed the fix-getattr-as-output branch from c47bf25 to fc94d80 Compare May 6, 2026 00:28
@TobyRoseman
Copy link
Copy Markdown
Collaborator

@john-rocky - Is this pull request still relevant? The associated issue has been closed. If it is still relevant, please fix the merge conflict.

@john-rocky
Copy link
Copy Markdown
Contributor Author

Closing this — #2538 was fixed by #2541 (merged), which makes the same remove_getattr_nodes change. Thanks @tritolol! No longer needed.

@john-rocky john-rocky closed this May 23, 2026
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.

remove_getattr_nodes torchIR pass fails with constant model outputs

2 participants