Skip to content

Commit bd89c17

Browse files
authored
Fix create_mutable_buffer recording the requested name instead of the name torch.fx assigned (pytorch#21565)
Follow up from the review discussion on pytorch#21542 where @JakeStevens flagged that `create_mutable_buffer` probably has the same latent bug. It does, and the collision case is a bit worse. Same pattern, the node gets created with the requested name and the signature records that name instead of whatever fx actually assigned: ```python node = graph.create_node(op="placeholder", name=name, target=name) ... InputSpec(InputKind.BUFFER, TensorArgument(name), target, persistent_buffer) ... OutputSpec(OutputKind.BUFFER_MUTATION, TensorArgument(name), target) ``` A digit-leading name leaves dangling refs in both the input and output specs and recompile dies with `SyntaxError: invalid decimal literal`. A name that collides with an existing placeholder is nastier, fx renames the node and the output spec silently binds to the colliding node, so the mutation entry points at the wrong tensor before recompile fails with `SyntaxError: duplicate argument`. No in-repo callers yet (came in with pytorch#13813, only its tests use it), so this is hardening a public utility rather than fixing a user visible crash. Fix is the same as pytorch#21542, everything follows the assigned name: `node.target`, the state_dict key and both `TensorArgument`s use `node.name`, with the `b_` prefix convention applied to the assigned name when deriving the target. If a rename lands on an existing state_dict key the node is removed and the existing duplicate-target error is raised instead of overwriting the entry. Valid unique names behave exactly as before. Per review, the creation logic is now a shared helper, `_create_placeholder_node`, used by both functions so they stay in sync, with a `_find_placeholder` lookup beside it. The dedup semantics stay deliberately different: `create_constant_placeholder` returns the existing node for a repeated request, while `create_mutable_buffer` raises, since a mutable buffer cannot be silently shared. Before/after with the same probe, checking signature consistency and a recompile round trip: ``` BEFORE AFTER b_my_cache (control) consistent, runs unchanged 0_cache (digit-leading) dangling input and output spec refs, consistent, runs SyntaxError: invalid decimal literal x (collides with input) output spec bound to the wrong node, consistent, runs SyntaxError: duplicate argument 'x' ``` Two new tests: one covers both rename triggers, asserting node, signature and state_dict consistency plus a recompile round trip, and is verified failing-first by reverting just `backends/transforms/utils.py`. The other pins the duplicate-request error for a clean and a renamed name; it passes before the helper change too and exists to guard the raise semantics. ``` backends/transforms/test main : 120 passed, 50 skipped this PR : 122 passed, 50 skipped ``` lintrunner is clean on the changed files. Independent of pytorch#21542, either can merge first. cc @cccclai @JakeStevens @digantdesai
1 parent d7138b4 commit bd89c17

2 files changed

Lines changed: 194 additions & 20 deletions

File tree

‎backends/transforms/test/test_create_mutable_buffer.py‎

Lines changed: 136 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010

1111
import executorch
1212
import torch
13-
from executorch.backends.transforms.utils import create_mutable_buffer
13+
from executorch.backends.transforms.utils import (
14+
create_constant_placeholder,
15+
create_mutable_buffer,
16+
)
1417
from executorch.exir import ExecutorchBackendConfig, to_edge
1518
from executorch.exir.dialects._ops import ops as exir_ops
1619
from executorch.exir.pass_base import ExportPass, PassResult
@@ -19,6 +22,7 @@
1922
Verification,
2023
)
2124
from torch.export import export
25+
from torch.export.graph_signature import InputKind
2226
from torch.utils._pytree import tree_flatten
2327

2428

@@ -93,6 +97,137 @@ def forward(self, x: torch.Tensor) -> torch.Tensor:
9397
== target_name
9498
)
9599

100+
def test_create_mutable_buffer_renamed_name(self):
101+
"""
102+
torch.fx renames a placeholder whose requested name is not a valid
103+
identifier or collides with an existing node. Node target, graph
104+
signature and state_dict must follow the assigned name.
105+
"""
106+
107+
class EmptyNetwork(torch.nn.Module):
108+
def forward(self, x: torch.Tensor) -> torch.Tensor:
109+
return x
110+
111+
test_data: torch.Tensor = (torch.zeros(1),)
112+
113+
# "0_cache" is not a valid identifier; "x" collides with the user input
114+
for requested_name in ("0_cache", "x"):
115+
exported_program = export(
116+
EmptyNetwork(), args=EmptyNetwork.test_data, strict=True
117+
)
118+
exported_program = to_edge(exported_program).exported_program()
119+
graph = exported_program.graph_module.graph
120+
121+
buffer_node = create_mutable_buffer(
122+
exp_program=exported_program,
123+
name=requested_name,
124+
data=torch.ones(1) * 2,
125+
)
126+
127+
assert buffer_node.name != requested_name
128+
assert buffer_node.target == buffer_node.name
129+
signature = exported_program.graph_signature
130+
assert buffer_node.name in signature.inputs_to_buffers
131+
target = signature.inputs_to_buffers[buffer_node.name]
132+
assert target in exported_program.state_dict
133+
assert signature.buffers_to_mutate[buffer_node.name] == target
134+
135+
input_node = list(graph.nodes)[1]
136+
with graph.inserting_after(input_node):
137+
graph.create_node(
138+
"call_function",
139+
exir_ops.edge.aten.add.Tensor,
140+
args=(input_node, buffer_node),
141+
kwargs={},
142+
)
143+
144+
# Recompiling emits the placeholder target as a parameter name
145+
assert exported_program.module()(torch.zeros(1)) == 0
146+
147+
def test_create_mutable_buffer_duplicate_name_raises(self):
148+
"""
149+
A mutable buffer cannot be silently shared, so repeating a request must
150+
raise instead of deduplicating, even when torch.fx renamed the node.
151+
"""
152+
153+
class EmptyNetwork(torch.nn.Module):
154+
def forward(self, x: torch.Tensor) -> torch.Tensor:
155+
return x
156+
157+
test_data: torch.Tensor = (torch.zeros(1),)
158+
159+
# A clean repeat is caught by the state_dict target check, a renamed
160+
# repeat by the placeholder lookup; pin each to its own error.
161+
for requested_name, error in (
162+
("b_cache", "already exists in state_dict"),
163+
("0_cache", "already exists in the graph"),
164+
):
165+
exported_program = export(
166+
EmptyNetwork(), args=EmptyNetwork.test_data, strict=True
167+
)
168+
exported_program = to_edge(exported_program).exported_program()
169+
170+
create_mutable_buffer(
171+
exp_program=exported_program,
172+
name=requested_name,
173+
data=torch.ones(1),
174+
)
175+
with self.assertRaisesRegex(RuntimeError, error):
176+
create_mutable_buffer(
177+
exp_program=exported_program,
178+
name=requested_name,
179+
data=torch.ones(1),
180+
)
181+
182+
def test_mixed_kind_same_name_raises(self):
183+
"""
184+
A name used by a mutable buffer cannot be reused by a constant
185+
placeholder in either creation order; the constant dedup must not hand
186+
back a mutable buffer node.
187+
"""
188+
189+
class EmptyNetwork(torch.nn.Module):
190+
def forward(self, x: torch.Tensor) -> torch.Tensor:
191+
return x
192+
193+
test_data: torch.Tensor = (torch.zeros(1),)
194+
195+
def fresh_program():
196+
ep = export(EmptyNetwork(), args=EmptyNetwork.test_data, strict=True)
197+
return to_edge(ep).exported_program()
198+
199+
# mutable first, then constant
200+
exported_program = fresh_program()
201+
graph = exported_program.graph_module.graph
202+
create_mutable_buffer(
203+
exp_program=exported_program, name="b_cache", data=torch.ones(1)
204+
)
205+
with self.assertRaisesRegex(RuntimeError, "already exists as a mutable buffer"):
206+
with graph.inserting_before(list(graph.nodes)[0]):
207+
create_constant_placeholder(
208+
exp_program=exported_program,
209+
graph=graph,
210+
name="b_cache",
211+
kind=InputKind.PARAMETER,
212+
data=torch.ones(1),
213+
)
214+
215+
# constant first, then mutable
216+
exported_program = fresh_program()
217+
graph = exported_program.graph_module.graph
218+
with graph.inserting_before(list(graph.nodes)[0]):
219+
create_constant_placeholder(
220+
exp_program=exported_program,
221+
graph=graph,
222+
name="b_cache",
223+
kind=InputKind.PARAMETER,
224+
data=torch.ones(1),
225+
)
226+
with self.assertRaisesRegex(RuntimeError, "already exists in the graph"):
227+
create_mutable_buffer(
228+
exp_program=exported_program, name="b_cache", data=torch.ones(1)
229+
)
230+
96231

97232
class TestRegisterMutableBufferPass(unittest.TestCase):
98233
"""

‎backends/transforms/utils.py‎

Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,33 @@ def get_param_tensor(
9595
raise RuntimeError(f"unsupported param type, {node.op}.")
9696

9797

98+
def _buffer_target(node_name: str) -> str:
99+
"""Map a placeholder name to its state_dict target per the export
100+
convention: placeholder "b_foo" corresponds to buffer target "foo"."""
101+
return node_name[2:] if node_name.startswith("b_") else node_name
102+
103+
104+
def _find_placeholder(graph: torch.fx.Graph, name: str) -> Optional[torch.fx.Node]:
105+
"""Return the placeholder previously created for this requested name, if any."""
106+
for n in graph.nodes:
107+
if n.op == "placeholder" and n.meta.get("requested_name") == name:
108+
return n
109+
return None
110+
111+
112+
def _create_placeholder_node(graph: torch.fx.Graph, name: str) -> torch.fx.Node:
113+
"""Create a placeholder at the current insertion point.
114+
115+
torch.fx may rename the node (invalid identifier or collision); the target,
116+
state_dict key and graph signature must follow the assigned name, so the
117+
target is set to node.name and the requested name is kept in node.meta.
118+
"""
119+
node = graph.create_node(op="placeholder", name=name, target=name)
120+
node.target = node.name
121+
node.meta["requested_name"] = name
122+
return node
123+
124+
98125
def create_constant_placeholder(
99126
exp_program: ExportedProgram,
100127
graph: torch.fx.Graph,
@@ -110,19 +137,21 @@ def create_constant_placeholder(
110137
"""
111138

112139
# Multiple pattern replacements may request the same shared weight; return
113-
# the existing node to avoid duplicate parameter names on recompile.
114-
for n in graph.nodes:
115-
if n.op == "placeholder" and n.meta.get("requested_name") == name:
116-
return n
140+
# the existing node to avoid duplicate parameter names on recompile. A
141+
# mutable buffer of the same name is not a constant and cannot be shared.
142+
existing = _find_placeholder(graph, name)
143+
if existing is not None:
144+
if existing.name in exp_program.graph_signature.buffers_to_mutate:
145+
raise RuntimeError(
146+
f"Placeholder '{name}' already exists as a mutable buffer"
147+
)
148+
return existing
117149

118150
fake_tensor = _get_fake_tensor_mode(graph, data)
119151

120-
# torch.fx may rename the node (invalid identifier or collision); the
121-
# target, state_dict key and graph signature must follow the assigned name.
122-
node = graph.create_node(op="placeholder", name=name, target=name)
123-
target = node.target = node.name
152+
node = _create_placeholder_node(graph, name)
153+
target = node.name
124154
node.meta["val"] = fake_tensor
125-
node.meta["requested_name"] = name
126155

127156
# Add data to state_dict/ constants
128157
match kind:
@@ -298,11 +327,7 @@ def create_mutable_buffer(
298327
if not isinstance(data, torch.Tensor):
299328
raise ValueError("Data must be a torch.Tensor")
300329

301-
# Extract target name (remove "b_" prefix if present, following export convention)
302-
if name.startswith("b_"):
303-
target = name[2:]
304-
else:
305-
target = name
330+
target = _buffer_target(name)
306331

307332
# Check if target already exists
308333
if target in exp_program.state_dict:
@@ -311,10 +336,14 @@ def create_mutable_buffer(
311336
_validate_graph_signature(exp_program)
312337

313338
persistent_buffer = True
314-
exp_program.state_dict[target] = data
315339

316340
graph = exp_program.graph_module.graph
317341

342+
# Unlike create_constant_placeholder, a mutable buffer cannot be silently
343+
# shared, so a repeated request is an error rather than a dedup hit.
344+
if _find_placeholder(graph, name) is not None:
345+
raise RuntimeError(f"Placeholder for '{name}' already exists in the graph")
346+
318347
# Create fake tensor using helper function
319348
fake_tensor = _get_fake_tensor_mode(graph, data)
320349

@@ -338,20 +367,30 @@ def create_mutable_buffer(
338367
):
339368
# No const or user input nodes
340369
node_index = len(input_specs)
341-
node = graph.create_node(op="placeholder", name=name, target=name)
370+
node = _create_placeholder_node(graph, name)
342371
else:
343372
# Find the first constant or user input node
344373
for i, spec in enumerate(input_specs):
345374
if spec.kind in [InputKind.CONSTANT_TENSOR, InputKind.USER_INPUT]:
346375
node_index = i
347376
with graph.inserting_before(_spec_to_node(exp_program, spec)):
348-
node = graph.create_node(op="placeholder", name=name, target=name)
377+
node = _create_placeholder_node(graph, name)
349378
break
350379

351380
assert node is not None, "node should be created at this point"
381+
382+
# If fx renamed the node, the caller's name is stale; re-apply the target
383+
# convention to the assigned name.
384+
if node.name != name:
385+
target = _buffer_target(node.name)
386+
if target in exp_program.state_dict:
387+
graph.erase_node(node)
388+
raise RuntimeError(f"Buffer target '{target}' already exists in state_dict")
389+
exp_program.state_dict[target] = data
390+
352391
node.meta["val"] = fake_tensor
353392
buffer_input_spec = InputSpec(
354-
InputKind.BUFFER, TensorArgument(name), target, persistent_buffer
393+
InputKind.BUFFER, TensorArgument(node.name), target, persistent_buffer
355394
)
356395
input_specs.insert(node_index, buffer_input_spec)
357396

@@ -367,7 +406,7 @@ def create_mutable_buffer(
367406

368407
output_specs = exp_program.graph_signature.output_specs
369408
mutation_output_spec = OutputSpec(
370-
OutputKind.BUFFER_MUTATION, TensorArgument(name), target
409+
OutputKind.BUFFER_MUTATION, TensorArgument(node.name), target
371410
)
372411
output_specs.insert(output_index, mutation_output_spec)
373412

0 commit comments

Comments
 (0)