Repository navigation
Fix export of zero-stride constants and inputs, and of constants that delegates share #24404
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
6540272
93f177d
e7756f5
2bcf16c
0d7a4be
d6c3d94
8df067b
67d6e8b
88f2794
08acc08
0b16218
f7e48fa
6c75fd3
79b0e1a
0b81886
52329e5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -658,32 +658,42 @@ def forward(self, x): | |
|
|
||
| def test_tag_mutated_buffer_detects_indirect_buffer_mutation(self) -> None: | ||
| class MutableStateModule(torch.nn.Module): | ||
| def __init__(self): | ||
| def __init__(self, compute_new_value: bool): | ||
| super().__init__() | ||
| self.compute_new_value = compute_new_value | ||
| self.register_buffer("state", torch.zeros(1)) | ||
|
|
||
| def forward(self, x): | ||
| result = x + self.state | ||
| self.state.copy_(x) | ||
| self.state.copy_(x + 1 if self.compute_new_value else x) | ||
| return result | ||
|
|
||
| edge = exir.to_edge( | ||
| torch.export.export(MutableStateModule(), (torch.ones(1),), strict=True) | ||
| ) | ||
| exported_program = edge.exported_program() | ||
| state_node = next( | ||
| node | ||
| for node in exported_program.graph.nodes | ||
| if exported_program.graph_signature.inputs_to_buffers.get(node.name) | ||
| == "state" | ||
| ) | ||
| # A new value that is a graph input is never in a partition, so the | ||
| # buffer then stays in the owning program. | ||
| delegate_tag = "test_partition" | ||
| for user in state_node.users: | ||
| user.meta["delegation_tag"] = delegate_tag | ||
| for compute_new_value, expected_tag in ((True, delegate_tag), (False, None)): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The original case here (
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. The PR description now has a "Behavior change" paragraph that names this case. With the reader delegated, that model failed to lower on main ( |
||
| with self.subTest(compute_new_value=compute_new_value): | ||
| edge = exir.to_edge( | ||
| torch.export.export( | ||
| MutableStateModule(compute_new_value), | ||
| (torch.ones(1),), | ||
| strict=True, | ||
| ) | ||
| ) | ||
| exported_program = edge.exported_program() | ||
| state_node = next( | ||
| node | ||
| for node in exported_program.graph.nodes | ||
| if exported_program.graph_signature.inputs_to_buffers.get(node.name) | ||
| == "state" | ||
| ) | ||
| for node in exported_program.graph.nodes: | ||
| if node.op == "call_function": | ||
| node.meta["delegation_tag"] = delegate_tag | ||
|
|
||
| tag_mutated_buffer(exported_program) | ||
| tag_mutated_buffer(exported_program) | ||
|
|
||
| self.assertEqual(state_node.meta.get("delegation_tag"), delegate_tag) | ||
| self.assertEqual(state_node.meta.get("delegation_tag"), expected_tag) | ||
|
|
||
| def test_buffer_mutation1(self): | ||
| class TestModule(torch.nn.Module): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit, and already true on main: this still makes one copy per user, even for users that share a tag. Copying once per tag would mean fewer placeholders. Fine to leave for later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. A change that makes one copy per tag, with all untagged users sharing one, is already written as its own commit. It goes into #24317, after this PR merges.