Skip to content

Fix create_mutable_buffer recording the requested name instead of the name torch.fx assigned - #21565

Open
slipstr34m wants to merge 2 commits into
pytorch:mainfrom
slipstr34m:fix-mutable-buffer-name
Open

Fix create_mutable_buffer recording the requested name instead of the name torch.fx assigned#21565
slipstr34m wants to merge 2 commits into
pytorch:mainfrom
slipstr34m:fix-mutable-buffer-name

Conversation

@slipstr34m

@slipstr34m slipstr34m commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Follow up from the review discussion on #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:

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 #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 #21542, everything follows the assigned name: node.target, the state_dict key and both TensorArguments 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 #21542, either can merge first.

cc @JakeStevens @digantdesai

@pytorch-bot

pytorch-bot Bot commented Aug 4, 2026

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/21565

Note: Links to docs will display an error until the docs builds have been completed.

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 4, 2026
@slipstr34m

Copy link
Copy Markdown
Contributor Author

@pytorchbot label "release notes: none"

@pytorch-bot pytorch-bot Bot added the release notes: none Do not include this in the release notes label Aug 4, 2026
@JakeStevens

Copy link
Copy Markdown
Contributor

The merged implementation had a check to avoid duplicate parameter names on recompile. This one does not, so we can still have this duplicate issue.

Since the other has landed, maybe update this PR to extract a helper and use that, so they stay in sync and we don't have holes like this

@slipstr34m
slipstr34m force-pushed the fix-mutable-buffer-name branch from 3bf2858 to 286b7bf Compare August 5, 2026 00:59
@slipstr34m

Copy link
Copy Markdown
Contributor Author

Good call. Extracted _create_placeholder_node (follows the fx-assigned name, stamps the requested name in meta) plus a _find_placeholder lookup and both functions now go through them. One deliberate difference: create_constant_placeholder returns the existing node on a repeated request since shared weights are the point there, but a mutable buffer can't be silently shared, so create_mutable_buffer now raises on a duplicate request instead, with a test covering both. Also rebased on main now that #21542 landed.

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

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. release notes: none Do not include this in the release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants