Repository navigation
Commit bd89c17
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 @digantdesai1 parent d7138b4 commit bd89c17
2 files changed
Lines changed: 194 additions & 20 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
13 | | - | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
14 | 17 | | |
15 | 18 | | |
16 | 19 | | |
| |||
19 | 22 | | |
20 | 23 | | |
21 | 24 | | |
| 25 | + | |
22 | 26 | | |
23 | 27 | | |
24 | 28 | | |
| |||
93 | 97 | | |
94 | 98 | | |
95 | 99 | | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
96 | 231 | | |
97 | 232 | | |
98 | 233 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
95 | 95 | | |
96 | 96 | | |
97 | 97 | | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
98 | 125 | | |
99 | 126 | | |
100 | 127 | | |
| |||
110 | 137 | | |
111 | 138 | | |
112 | 139 | | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
117 | 149 | | |
118 | 150 | | |
119 | 151 | | |
120 | | - | |
121 | | - | |
122 | | - | |
123 | | - | |
| 152 | + | |
| 153 | + | |
124 | 154 | | |
125 | | - | |
126 | 155 | | |
127 | 156 | | |
128 | 157 | | |
| |||
298 | 327 | | |
299 | 328 | | |
300 | 329 | | |
301 | | - | |
302 | | - | |
303 | | - | |
304 | | - | |
305 | | - | |
| 330 | + | |
306 | 331 | | |
307 | 332 | | |
308 | 333 | | |
| |||
311 | 336 | | |
312 | 337 | | |
313 | 338 | | |
314 | | - | |
315 | 339 | | |
316 | 340 | | |
317 | 341 | | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
318 | 347 | | |
319 | 348 | | |
320 | 349 | | |
| |||
338 | 367 | | |
339 | 368 | | |
340 | 369 | | |
341 | | - | |
| 370 | + | |
342 | 371 | | |
343 | 372 | | |
344 | 373 | | |
345 | 374 | | |
346 | 375 | | |
347 | 376 | | |
348 | | - | |
| 377 | + | |
349 | 378 | | |
350 | 379 | | |
351 | 380 | | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
352 | 391 | | |
353 | 392 | | |
354 | | - | |
| 393 | + | |
355 | 394 | | |
356 | 395 | | |
357 | 396 | | |
| |||
367 | 406 | | |
368 | 407 | | |
369 | 408 | | |
370 | | - | |
| 409 | + | |
371 | 410 | | |
372 | 411 | | |
373 | 412 | | |
| |||
0 commit comments