Repository navigation
Commit 075742a
[dynamo] Split OrderedSetVariable off SetVariable to match CPython (#196167)
## Issue
Part of #192874, the `OrderedSetVariable(SetVariable)` row.
`torch.utils._ordered_set.OrderedSet` is a pure-Python `collections.abc.MutableSet`, not a subclass of `set`: `issubclass(OrderedSet, set)` is `False`. `OrderedSetVariable` inherited `SetVariable`, so wherever the two types differ Dynamo used `set`'s behaviour, and a few paths treated the tracked object as a `set` or a `dict` that it is not.
## Summary
The most visible effect is that an `OrderedSet` loses its insertion order when it crosses the compile boundary, because reconstruction and `repr` iterated an unordered set of the keys instead of the ordered backing dict:
```python
import torch
from torch.utils._ordered_set import OrderedSet
def f(x):
s = OrderedSet([3, 1, 2])
return s, repr(s), x + 1
torch.compile(f, backend="eager")(torch.ones(2))
```
Before this change the returned set is `OrderedSet([1, 2, 3])` and `repr(s)` is `'OrderedSet([1, 2, 3])'`. After it, both keep `[3, 1, 2]`, which is what eager produces. Every operation that yields an OrderedSet is affected the same way: returning one, `copy`, `union`, `difference`, `symmetric_difference`, the operators, `add`, `update` and `repr`.
Passing an OrderedSet into a compiled function did not work. Membership, iteration and mutation each failed:
```python
def g(x, s):
s.add(9)
return 2 in s, list(s), x + 1
torch.compile(g, backend="eager")(torch.ones(2), OrderedSet([3, 1, 2]))
```
Before, this raises `AssertionError: Expected dict, got OrderedSet` while building guards, `2 in s` on its own fails a `set.__contains__` guard, and the `add` never reaches the caller. After, it returns `(True, [3, 1, 2, 9])` and the caller's set is `OrderedSet([3, 1, 2, 9])`, matching eager.
The remaining differences are smaller and all now match eager:
- `pop()` removed an arbitrary element rather than the most recently inserted one. `OrderedSet([3, 1, 2]).pop()` was `1`, now `2`.
- `|=`, `&=` and `^=` with a non-set operand rebound the name to a new object instead of mutating in place, so `s is alias` was `False` afterwards.
- `some_set - ordered_set` returned a `set`; `Set.__rsub__` builds an OrderedSet.
- `reversed(s)` was unsupported; it now yields reverse insertion order.
- an unhashable key, as in `{1, 2} in s` or `s.discard({1, 2})`, silently returned a result or coerced the key the way `set` does; it now raises `TypeError`, and so does `hash(s)`.
## Fix
`OrderedSetVariable` becomes a sibling of `SetVariable` under `BaseSetVariable`, following the shape of #193580 (frozenset) and #193649 (dict_keys). `OrderedSet`'s public method surface is exactly `set`'s, so `tp_methods` registers the same named entries pointing at the same implementations, with `isdisjoint` coming from the base. What `OrderedSet` does differently is overridden on the class:
- reconstruction, `repr` and `as_python_constant` iterate `self.items`, which is insertion ordered, instead of the unordered `set_items`.
- `pop` removes the most recently inserted key.
- the in-place operators call `update`, `intersection_update`, `symmetric_difference_update` and `difference_update`, so they accept any iterable and mutate in place like `MutableSet`.
- `nb_subtract_impl` and `nb_and_impl` follow CPython's operand order for the reflected and intersection cases, and `__reversed__` yields reverse insertion order.
- `is_hashable`, `hash_impl` and an OrderedSet-specific `sq_contains_impl` are declared here, since they are no longer inherited from `SetVariable`.
- the `_dict` member exposes the backing dict so inlined `OrderedSet` methods trace. A passed-in OrderedSet is guarded through that dict already, so the per-key `SET_CONTAINS` guard and the key-order registration that assume a real `set` or `dict` are skipped for it.
The references to `SetVariable` outside `sets.py` that matched an OrderedSet through inheritance now name `OrderedSetVariable` as well: the set-mutation replay handler in `side_effects.py`, `_unpack_fast_types` in `utils.py`, three checks in `builtin.py`, two in `higher_order_ops.py`, the `list.extend` fast path in `lists.py`, and `vt_identity_compare` in `object_protocol.py`. The builder wraps a passed-in set or OrderedSet with `track_mutable`, so mutations to it replay to the caller as they already do for a set built inside the region.
## Test plan
New `OrderedSetHierarchyTests` in `test/dynamo/test_sets.py`, 17 tests, covering the hierarchy, ordering, `pop`, the operators, `reversed`, unhashable keys, the unbound `OrderedSet.method(...)` forms, explicit `__init__` calls, and reads and mutations of a passed-in OrderedSet and of a passed-in non-literal set. They pass with this change and fail on `main`. The full file runs 222 passed, 1 skipped on Python 3.10 and 3.12. `lintrunner` and `pyrefly` report no issues on the changed files.
## BC-breaking?
Not for user code: results that previously disagreed with eager now agree with it. For out-of-tree code that touches Dynamo internals, `isinstance(x, SetVariable)` no longer matches an OrderedSet tracker, and `BaseSetVariable` is the check that matches all four set-family trackers.
Related: #196111 was opened for the same row. Happy to consolidate.
Authored with AI assistance.
Pull Request resolved: #196167
Approved by: https://github.com/guilhermeleobas1 parent 545e70c commit 075742a
8 files changed
Lines changed: 880 additions & 55 deletions
File tree
- test/dynamo
- torch/_dynamo
- variables
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1669 | 1669 | | |
1670 | 1670 | | |
1671 | 1671 | | |
1672 | | - | |
| 1672 | + | |
| 1673 | + | |
| 1674 | + | |
| 1675 | + | |
| 1676 | + | |
| 1677 | + | |
1673 | 1678 | | |
1674 | 1679 | | |
1675 | 1680 | | |
1676 | 1681 | | |
1677 | 1682 | | |
1678 | 1683 | | |
1679 | | - | |
| 1684 | + | |
| 1685 | + | |
| 1686 | + | |
| 1687 | + | |
| 1688 | + | |
| 1689 | + | |
| 1690 | + | |
| 1691 | + | |
1680 | 1692 | | |
1681 | 1693 | | |
1682 | 1694 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1341 | 1341 | | |
1342 | 1342 | | |
1343 | 1343 | | |
1344 | | - | |
| 1344 | + | |
| 1345 | + | |
| 1346 | + | |
| 1347 | + | |
| 1348 | + | |
1345 | 1349 | | |
1346 | 1350 | | |
1347 | 1351 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
147 | 147 | | |
148 | 148 | | |
149 | 149 | | |
150 | | - | |
| 150 | + | |
151 | 151 | | |
152 | 152 | | |
153 | 153 | | |
| |||
2459 | 2459 | | |
2460 | 2460 | | |
2461 | 2461 | | |
| 2462 | + | |
2462 | 2463 | | |
2463 | 2464 | | |
2464 | 2465 | | |
| |||
3231 | 3232 | | |
3232 | 3233 | | |
3233 | 3234 | | |
| 3235 | + | |
3234 | 3236 | | |
3235 | 3237 | | |
3236 | 3238 | | |
| |||
3418 | 3420 | | |
3419 | 3421 | | |
3420 | 3422 | | |
| 3423 | + | |
3421 | 3424 | | |
3422 | 3425 | | |
3423 | 3426 | | |
| |||
3884 | 3887 | | |
3885 | 3888 | | |
3886 | 3889 | | |
3887 | | - | |
3888 | | - | |
3889 | | - | |
3890 | | - | |
3891 | | - | |
3892 | | - | |
3893 | | - | |
3894 | | - | |
| 3890 | + | |
| 3891 | + | |
| 3892 | + | |
| 3893 | + | |
3895 | 3894 | | |
3896 | 3895 | | |
3897 | 3896 | | |
| |||
| 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 | + | |
| 254 | + | |
253 | 255 | | |
254 | 256 | | |
255 | 257 | | |
| |||
4380 | 4382 | | |
4381 | 4383 | | |
4382 | 4384 | | |
| 4385 | + | |
4383 | 4386 | | |
4384 | 4387 | | |
4385 | 4388 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
530 | 530 | | |
531 | 531 | | |
532 | 532 | | |
533 | | - | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
534 | 539 | | |
535 | 540 | | |
536 | 541 | | |
| |||
540 | 545 | | |
541 | 546 | | |
542 | 547 | | |
543 | | - | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
544 | 555 | | |
545 | 556 | | |
546 | 557 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
90 | 90 | | |
91 | 91 | | |
92 | 92 | | |
93 | | - | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
94 | 99 | | |
95 | 100 | | |
96 | 101 | | |
| |||
100 | 105 | | |
101 | 106 | | |
102 | 107 | | |
| 108 | + | |
103 | 109 | | |
104 | 110 | | |
105 | 111 | | |
| |||
0 commit comments