Repository navigation
Commit 59f3b80
[dynamo] Split FrozensetVariable off SetVariable to match CPython (#193580)
## Issue
Part of #192874, the `FrozensetVariable(SetVariable)` row. That tracker lists eight rows, so this does not close it.
## Summary
Under `torch.compile`, calling a mutating `set` method on a `frozenset` raised an internal assertion instead of the `AttributeError` CPython raises:
```python
import torch
def f(fs):
fs.add(4) # frozenset has no .add
return fs
torch.compile(f, backend="eager")(frozenset({1, 2, 3}))
```
Before, with `fullgraph=False`, where a graph break was expected:
```
AssertionError: mutation_type is None for FrozensetVariable() in check_allowed_side_effect
```
After, with `fullgraph=False`, matching eager:
```
AttributeError: 'frozenset' object has no attribute 'add'
```
After, with `fullgraph=True`, an ordinary unsupported-call error:
```
Unsupported method call
Explanation: Dynamo does not know how to trace method `add` of class `frozenset`
```
The same applied to `pop`, `remove`, `discard`, `clear`, `intersection_update`, `difference_update` and `symmetric_difference_update`. `update` is the exception: it already returned early on `not self.is_mutable()`, so it degraded correctly before this change.
Cause: `FrozensetVariable` subclassed `SetVariable`, so it picked up all nine mutating entries of `SetVariable.tp_methods` through the tp_methods MRO merge. In CPython `frozenset` and `set` are siblings, not parent and child.
## Fix
A new `BaseSetVariable(VariableTracker)` holds the shared read-only surface: the eight read-only `tp_methods` (`isdisjoint`, `intersection`, `union`, `difference`, `symmetric_difference`, `issubset`, `issuperset`, `copy`), the `nb_or_impl` / `nb_and_impl` / `nb_xor_impl` / `nb_subtract_impl` slots, and the storage, guard and iteration helpers. `SetVariable(BaseSetVariable)` keeps the nine mutating `tp_methods` and the `nb_inplace_*` slots, so `FrozensetVariable(BaseSetVariable)` can reach neither group.
Eight `SetVariable` references outside `sets.py` that used to match frozensets transitively are widened to `BaseSetVariable`: seven `isinstance` checks (`builtin.py` x3, `higher_order_ops.py` x2, `lists.py`, `object_protocol.py`) and the `_unpack_fast_types()` tuple in `utils.py`. `BaseSetVariable` is exported from `variables/__init__.py`.
Two `isinstance(..., SetVariable)` checks are deliberately left alone, because the split makes them more accurate: `SET_ADD` and `SET_UPDATE` in `symbolic_convert.py` assert their accumulator is a mutable set, and the `const_dict_or_set_mutation` replay handler in `side_effects.py` emits `update` and `clear`, neither of which exists on a frozenset. `OrderedSetClassVariable.call_method` also stops routing unbound `OrderedSet.union(some_frozenset, ...)` calls into the frozenset tracker, which matches eager, where that raises.
### Why a base class
The split follows a line CPython already draws. `frozenset` and `set` are both `collections.abc.Set`, only `set` is a `MutableSet`, and `set(dir(MutableSet)) - set(dir(Set))` is exactly `{add, clear, discard, pop, remove, __ior__, __iand__, __isub__, __ixor__}`. That is the group `SetVariable` keeps, so `BaseSetVariable` is `Set` and `SetVariable` is `MutableSet`. (`update` and the three `*_update` methods have no ABC counterpart, but they are mutators too, so they stay on `SetVariable`.) Happy to rename it `AbstractSetVariable` if that reads better.
It also avoids a large copy. If `FrozensetVariable` inherited `VariableTracker` directly, it would have to duplicate 31 methods and 2 class constants, 355 lines, out of `SetVariable`: everything now in `BaseSetVariable` that `FrozensetVariable` does not already shadow (it shadows only five, `copy`, `intersection`, `difference`, `symmetric_difference` and `tp_methods`).
`BaseListVariable`, `BaseUserFunctionVariable` and `BaseBuiltinVariable` are used the same way in the same package, and #193261 takes the same approach (`BaseListIteratorVariable`) for the deque iterator row.
`DictKeySetVariable` and `OrderedSetVariable` still subclass `SetVariable`. They are separate rows of #192874 and out of scope here.
## Test plan
New `FrozensetHierarchyTests` in `test/dynamo/test_sets.py`, 6 tests. Run against the same test file with only `torch` swapped:
```
patched Ran 6 tests, OK
unpatched Ran 6 tests, FAILED (failures=9, errors=1)
```
Being precise about what that demonstrates, since not all six are regression coverage:
- `test_frozenset_has_no_mutating_methods` sub-tests all nine mutators under `fullgraph=True`. Eight fail unpatched: `add`, `pop`, `remove`, `discard`, `clear`, `intersection_update`, `difference_update`, `symmetric_difference_update`. The `update` sub-test passes unpatched, per the early return noted above.
- `test_frozenset_mutating_method_graph_breaks` covers `fullgraph=False` and fails unpatched with the `mutation_type is None` assertion above. This is the user-visible bug.
- `test_frozenset_not_a_set_variable` is a structural assertion, not behaviour. It errors unpatched with `ImportError: cannot import name 'BaseSetVariable'`, so it guards against the classes being merged again rather than proving a fix.
- The remaining three pass both before and after and are non-regression guards: `test_frozenset_inplace_operators_rebind_not_mutate`, `test_frozenset_readonly_ops_still_work`, `test_regular_set_mutations_unaffected`.
Suites run locally, all against the patched tree:
```
test/dynamo/test_sets.py Ran 196 tests, OK (skipped=1)
test/dynamo/test_functions.py Ran 531 tests, OK (skipped=9, expected failures=2)
test/dynamo/test_dicts.py Ran 323 tests, OK (expected failures=1)
test/dynamo/test_higher_order_ops.py Ran 224 tests, OK (skipped=17, expected failures=1)
test/dynamo/test_repros.py Ran 388 tests, OK (skipped=19, expected failures=4)
test/dynamo/test_misc.py Ran 822 tests, FAILED (failures=1, errors=1, skipped=13, expected failures=4)
```
The two `test_misc.py` problems are environmental and pre-existing on this machine: `test_pybind11_enum_conversion` needs `ninja`, which is not installed, and `test_debugmode_respects_custom_op_aliasing_env_override` spawns a subprocess that imports the unbuilt source tree from the working directory (it passes when run from another directory). Neither traceback contains a `set`, `frozenset` or `SetVariable` frame.
These ran on a nightly CPU wheel, `2.15.0.dev20260812+cpu`, with the repository sources overlaid (the `tools/nightly.py` workflow in CONTRIBUTING.md) rather than a from-source build. The change is pure Python with no C++ coupling, so the modified code is the code under test, but I have run no CUDA or distributed suites locally and am relying on CI for those.
Lint, at the versions the repo pins:
```
lintrunner 0.12.7 --take RUFF,PYFMT,NEWLINE,SPACES,CODESPELL,FLAKE8,TYPEIGNORE,NOQA,TABS
ok No lint issues.
ruff 0.14.4 All checks passed! / 8 files already formatted
```
`pyrefly 0.58.0` reports 131 pre-existing errors on these files, all `missing-attribute` or `missing-module-attribute` against `torch._C` symbols, caused by the build-generated stubs being absent from a non-source checkout. None falls on a line this change adds, checked by intersecting the reported error locations with the added lines; `sets.py` reports zero. I did not run a pre-change `pyrefly` baseline, so that is an attribution check rather than a differential one.
## Checklist
- [x] Passes lint (`spin fixlint`)
- [x] Added/updated tests
- [ ] Updated documentation (not applicable)
- [ ] Included benchmark results (not applicable, no expected perf impact)
## BC-breaking?
No for user-visible behaviour. An internal assertion that used to escape becomes either the `AttributeError` CPython raises or, under `fullgraph=True`, an ordinary unsupported-call error. For out-of-tree code touching Dynamo internals, `isinstance(x, SetVariable)` no longer matches a frozenset tracker and needs `BaseSetVariable`.
Related: #192867 (draft) also edits `torch/_dynamo/variables/sets.py`. Happy to rebase onto whichever lands first.
Authored with AI assistance.
Pull Request resolved: #193580
Approved by: https://github.com/guilhermeleobas1 parent 723eb3f commit 59f3b80
8 files changed
Lines changed: 426 additions & 281 deletions
File tree
- test/dynamo
- torch/_dynamo
- variables
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
970 | 970 | | |
971 | 971 | | |
972 | 972 | | |
| 973 | + | |
| 974 | + | |
| 975 | + | |
| 976 | + | |
| 977 | + | |
| 978 | + | |
| 979 | + | |
| 980 | + | |
| 981 | + | |
| 982 | + | |
| 983 | + | |
| 984 | + | |
| 985 | + | |
| 986 | + | |
| 987 | + | |
| 988 | + | |
| 989 | + | |
| 990 | + | |
| 991 | + | |
| 992 | + | |
| 993 | + | |
| 994 | + | |
| 995 | + | |
| 996 | + | |
| 997 | + | |
| 998 | + | |
| 999 | + | |
| 1000 | + | |
| 1001 | + | |
| 1002 | + | |
| 1003 | + | |
| 1004 | + | |
| 1005 | + | |
| 1006 | + | |
| 1007 | + | |
| 1008 | + | |
| 1009 | + | |
| 1010 | + | |
| 1011 | + | |
| 1012 | + | |
| 1013 | + | |
| 1014 | + | |
| 1015 | + | |
| 1016 | + | |
| 1017 | + | |
| 1018 | + | |
| 1019 | + | |
| 1020 | + | |
| 1021 | + | |
| 1022 | + | |
| 1023 | + | |
| 1024 | + | |
| 1025 | + | |
| 1026 | + | |
| 1027 | + | |
| 1028 | + | |
| 1029 | + | |
| 1030 | + | |
| 1031 | + | |
| 1032 | + | |
| 1033 | + | |
| 1034 | + | |
| 1035 | + | |
| 1036 | + | |
| 1037 | + | |
| 1038 | + | |
| 1039 | + | |
| 1040 | + | |
| 1041 | + | |
| 1042 | + | |
| 1043 | + | |
| 1044 | + | |
| 1045 | + | |
| 1046 | + | |
| 1047 | + | |
| 1048 | + | |
| 1049 | + | |
| 1050 | + | |
| 1051 | + | |
| 1052 | + | |
| 1053 | + | |
| 1054 | + | |
| 1055 | + | |
| 1056 | + | |
| 1057 | + | |
| 1058 | + | |
| 1059 | + | |
| 1060 | + | |
| 1061 | + | |
| 1062 | + | |
| 1063 | + | |
| 1064 | + | |
| 1065 | + | |
| 1066 | + | |
| 1067 | + | |
| 1068 | + | |
| 1069 | + | |
| 1070 | + | |
| 1071 | + | |
| 1072 | + | |
| 1073 | + | |
| 1074 | + | |
| 1075 | + | |
| 1076 | + | |
| 1077 | + | |
| 1078 | + | |
| 1079 | + | |
| 1080 | + | |
| 1081 | + | |
| 1082 | + | |
| 1083 | + | |
| 1084 | + | |
973 | 1085 | | |
974 | 1086 | | |
975 | 1087 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1310 | 1310 | | |
1311 | 1311 | | |
1312 | 1312 | | |
| 1313 | + | |
1313 | 1314 | | |
1314 | 1315 | | |
1315 | 1316 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2434 | 2434 | | |
2435 | 2435 | | |
2436 | 2436 | | |
2437 | | - | |
| 2437 | + | |
| 2438 | + | |
| 2439 | + | |
| 2440 | + | |
| 2441 | + | |
| 2442 | + | |
| 2443 | + | |
| 2444 | + | |
2438 | 2445 | | |
2439 | 2446 | | |
2440 | 2447 | | |
| |||
3196 | 3203 | | |
3197 | 3204 | | |
3198 | 3205 | | |
3199 | | - | |
| 3206 | + | |
| 3207 | + | |
| 3208 | + | |
3200 | 3209 | | |
3201 | 3210 | | |
3202 | 3211 | | |
| |||
3369 | 3378 | | |
3370 | 3379 | | |
3371 | 3380 | | |
3372 | | - | |
| 3381 | + | |
| 3382 | + | |
| 3383 | + | |
3373 | 3384 | | |
3374 | 3385 | | |
3375 | 3386 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
67 | 67 | | |
68 | 68 | | |
69 | 69 | | |
70 | | - | |
| 70 | + | |
71 | 71 | | |
72 | 72 | | |
73 | 73 | | |
| |||
249 | 249 | | |
250 | 250 | | |
251 | 251 | | |
252 | | - | |
| 252 | + | |
253 | 253 | | |
254 | 254 | | |
255 | 255 | | |
| |||
4352 | 4352 | | |
4353 | 4353 | | |
4354 | 4354 | | |
4355 | | - | |
| 4355 | + | |
| 4356 | + | |
| 4357 | + | |
| 4358 | + | |
| 4359 | + | |
| 4360 | + | |
| 4361 | + | |
| 4362 | + | |
4356 | 4363 | | |
4357 | 4364 | | |
4358 | 4365 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
530 | 530 | | |
531 | 531 | | |
532 | 532 | | |
533 | | - | |
| 533 | + | |
534 | 534 | | |
535 | 535 | | |
536 | 536 | | |
537 | 537 | | |
538 | 538 | | |
539 | 539 | | |
540 | 540 | | |
541 | | - | |
| 541 | + | |
542 | 542 | | |
543 | 543 | | |
544 | 544 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
85 | 85 | | |
86 | 86 | | |
87 | 87 | | |
88 | | - | |
| 88 | + | |
89 | 89 | | |
90 | 90 | | |
91 | 91 | | |
92 | 92 | | |
93 | 93 | | |
94 | 94 | | |
95 | 95 | | |
| 96 | + | |
96 | 97 | | |
97 | 98 | | |
98 | 99 | | |
| |||
0 commit comments