Repository navigation
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/196167
Note: Links to docs will display an error until the docs builds have been completed. ✅ You can merge normally! (2 Unrelated Failures)As of commit 4ea8bb0 with merge base 300f0c8 ( CRCR (non-blocking) - The following CRCR downstream CI jobs failed but are non-blocking:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
|
|
The following ciflow label(s) have been added but CI has not been triggered yet because the workflows are awaiting approval:
Once a maintainer approves the workflows (scroll to the bottom of the PR page), the corresponding CI jobs will be triggered automatically. Please ping one of the reviewers if you do not have access to approve and run workflows. |
|
@pytorchbot label "topic: not user facing" |
|
@yashb98 can you address the test failures? |
|
Thanks for the review, both points addressed locally. The -= repro was spot on: SetVariable.nb_inplace_subtract_impl flagged the mutation but never set should_reconstruct_all, and the replay handler only emits when has_new_items() is true, so a removal-only -= on a passed-in non-literal set was silently skipped. Your exact snippet passes now. I also added tp_init_impl to OrderedSetVariable and made OrderedSetClassVariable.call_function delegate to it (tp_new + tp_init), so the constructor and an explicit init share one argument-handling path. Separately, the py3.10 CI failures were my test's fault: I used slice(0, 1) as the non-literal element, but slice is only hashable since 3.12. Swapped it for a plain object instance (same idea as your Key) and extended the test to cover the in-place operators on a passed-in non-literal set, so the -= case is locked in as a regression test. I want to verify before pushing, so I'm running the new tests plus the full test_sets.py on both 3.10 and 3.12 locally, and a local 3.10 arm that mirrors the failing CI shard's exact run_test.py command. Once those finish clean I'll push the update. |
|
There are some Python 3.10 failures. Can you check? |
…p_init_impl Address review feedback on pytorch#196167: - SetVariable.nb_inplace_subtract_impl never set should_reconstruct_all, so a removal-only `-=` on a passed-in non-literal set produced no new items and the mutation replay handler skipped it; the caller's set kept the removed elements. Set the flag, matching difference_update. - Add OrderedSetVariable.tp_init_impl and make OrderedSetClassVariable's call_function delegate to it, so the constructor and an explicit OrderedSet.__init__(s, ...) share one argument-handling path. Init builds the new items before clearing the old ones, so a TypeError from an unhashable element leaves the set unchanged, matching eager. - test_passed_in_non_literal_set_mutation_reaches_caller used slice elements, which are only hashable since Python 3.12; the set literal raised TypeError on the py3.10 CI jobs. Use a plain object instance instead, extend the test to the in-place operators with fullgraph on and off, and add test_explicit_dunder_init_matches_eager. Part of pytorch#192874.
OrderedSet is a pure-Python MutableSet, not a set subclass, but OrderedSetVariable inherited SetVariable and with it set's behaviour wherever the two differ. Make it a BaseSetVariable sibling of SetVariable with set's full named-method surface, and override what OrderedSet does differently: reconstruction, repr and as_python_constant follow insertion order instead of hash order, pop() returns the most recently inserted element, the in-place operators are MutableSet's (any iterable, mutate in place), reflected subtraction and & follow CPython's operand order, reversed() gives reverse insertion order, and hash() and an unhashable key raise TypeError like eager. A passed-in OrderedSet also did not work: membership failed a SET_CONTAINS guard evaluated as set.__contains__ on the OrderedSet, iteration registered the object for key-order guarding where a dict is expected, and mutations were traced but never replayed because the builder registered it with AttributeMutationExisting. The object is already guarded through its _dict, so the per-key guard and the key-order registration are skipped for OrderedSetVariable, and the builder wraps it with track_mutable like the literal-set path. Unbound OrderedSet.method(obj, ...) calls run OrderedSet's own method for any receiver, so the result or exception matches eager, and OrderedSet(None) is the empty constructor. Call sites that matched OrderedSetVariable through SetVariable now name it. Part of pytorch#192874.
…p_init_impl Address review feedback on pytorch#196167: - SetVariable.nb_inplace_subtract_impl never set should_reconstruct_all, so a removal-only `-=` on a passed-in non-literal set produced no new items and the mutation replay handler skipped it; the caller's set kept the removed elements. Set the flag, matching difference_update. - Add OrderedSetVariable.tp_init_impl and make OrderedSetClassVariable's call_function delegate to it, so the constructor and an explicit OrderedSet.__init__(s, ...) share one argument-handling path. Init builds the new items before clearing the old ones, so a TypeError from an unhashable element leaves the set unchanged, matching eager. - test_passed_in_non_literal_set_mutation_reaches_caller used slice elements, which are only hashable since Python 3.12; the set literal raised TypeError on the py3.10 CI jobs. Use a plain object instance instead, extend the test to the in-place operators with fullgraph on and off, and add test_explicit_dunder_init_matches_eager. Part of pytorch#192874.
3fb212d to
4ea8bb0
Compare
|
Pushed the update. It is rebased onto current main, which also clears the merge conflict the PR had in torch/_dynamo/utils.py; no other changes from the rebase. On the Python 3.10 failures: they were the test bug from my previous comment. slice is only hashable since 3.12, so the set literal raised TypeError at construction on the 3.10 jobs, before dynamo ran. The element is now a plain object instance. Full test/dynamo/test_sets.py passes on 3.10 and 3.12 (222 passed), including under the exact CI shard command on a 3.10 aarch64 setup, and your -= repro passes on both interpreters. @guilhermeleobas PTAL when you have a moment. |
|
Thanks @yashb98 |
|
@pytorchbot merge |
Merge startedYour change will be merged once all checks pass (ETA 0-4 Hours). Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
|
@guilhermeleobas thanks for the reviews on #193580 and #196167, and for seeing the tracker through. Is there a follow-up list of dynamo CPython-parity gaps, or an area you'd point a contributor at next? I'd like to keep working in this part of the codebase and can also review PRs there. |
|
Hi @yashb98, I think the dict |
|
Thanks @guilhermeleobas. I checked this against current main before taking it, and I'd like to confirm the scope with you.
What I do see on main:
How would you like me to proceed? I can send a small |
|
Hi, sorry for the delay.
Let me try to land this one first as it removes |
|
No worries at all, and thanks for getting back to me @guilhermeleobas! That makes sense. I'll wait for #197322 to land first, then take another look at the dict views on top of it and check with you before opening anything. |
|
Thanks @guilhermeleobas! Now that #197322 has landed, I’ll take another look at how Dynamo handles |
|
@yashb98 sounds good to me! If you're interested, there's also the representation of dict |
|
Thanks @guilhermeleobas! I'd be happy to take this on as well. I'll investigate the representation differences |
Issue
Part of #192874, the
OrderedSetVariable(SetVariable)row.torch.utils._ordered_set.OrderedSetis a pure-Pythoncollections.abc.MutableSet, not a subclass ofset:issubclass(OrderedSet, set)isFalse.OrderedSetVariableinheritedSetVariable, so wherever the two types differ Dynamo usedset's behaviour, and a few paths treated the tracked object as asetor adictthat it is not.Summary
The most visible effect is that an
OrderedSetloses its insertion order when it crosses the compile boundary, because reconstruction andrepriterated an unordered set of the keys instead of the ordered backing dict:Before this change the returned set is
OrderedSet([1, 2, 3])andrepr(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,updateandrepr.Passing an OrderedSet into a compiled function did not work. Membership, iteration and mutation each failed:
Before, this raises
AssertionError: Expected dict, got OrderedSetwhile building guards,2 in son its own fails aset.__contains__guard, and theaddnever reaches the caller. After, it returns(True, [3, 1, 2, 9])and the caller's set isOrderedSet([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()was1, now2.|=,&=and^=with a non-set operand rebound the name to a new object instead of mutating in place, sos is aliaswasFalseafterwards.some_set - ordered_setreturned aset;Set.__rsub__builds an OrderedSet.reversed(s)was unsupported; it now yields reverse insertion order.{1, 2} in sors.discard({1, 2}), silently returned a result or coerced the key the waysetdoes; it now raisesTypeError, and so doeshash(s).Fix
OrderedSetVariablebecomes a sibling ofSetVariableunderBaseSetVariable, following the shape of #193580 (frozenset) and #193649 (dict_keys).OrderedSet's public method surface is exactlyset's, sotp_methodsregisters the same named entries pointing at the same implementations, withisdisjointcoming from the base. WhatOrderedSetdoes differently is overridden on the class:reprandas_python_constantiterateself.items, which is insertion ordered, instead of the unorderedset_items.popremoves the most recently inserted key.update,intersection_update,symmetric_difference_updateanddifference_update, so they accept any iterable and mutate in place likeMutableSet.nb_subtract_implandnb_and_implfollow CPython's operand order for the reflected and intersection cases, and__reversed__yields reverse insertion order.is_hashable,hash_impland an OrderedSet-specificsq_contains_implare declared here, since they are no longer inherited fromSetVariable._dictmember exposes the backing dict so inlinedOrderedSetmethods trace. A passed-in OrderedSet is guarded through that dict already, so the per-keySET_CONTAINSguard and the key-order registration that assume a realsetordictare skipped for it.The references to
SetVariableoutsidesets.pythat matched an OrderedSet through inheritance now nameOrderedSetVariableas well: the set-mutation replay handler inside_effects.py,_unpack_fast_typesinutils.py, three checks inbuiltin.py, two inhigher_order_ops.py, thelist.extendfast path inlists.py, andvt_identity_compareinobject_protocol.py. The builder wraps a passed-in set or OrderedSet withtrack_mutable, so mutations to it replay to the caller as they already do for a set built inside the region.Test plan
New
OrderedSetHierarchyTestsintest/dynamo/test_sets.py, 17 tests, covering the hierarchy, ordering,pop, the operators,reversed, unhashable keys, the unboundOrderedSet.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 onmain. The full file runs 222 passed, 1 skipped on Python 3.10 and 3.12.lintrunnerandpyreflyreport 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, andBaseSetVariableis the check that matches all four set-family trackers.Related: #196111 was opened for the same row. Happy to consolidate.
Authored with AI assistance.
cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @kadeng @chauhang @amjames @jataylo @azahed98