Skip to content

[dynamo] Split DequeReverseIteratorVariable off DequeIteratorVariable to match CPython - #194348

Closed
indrajeetapache wants to merge 2 commits into
pytorch:mainfrom
indrajeetapache:fix/deque-reverse-iterator-variable-192874
Closed

indrajeetapache wants to merge 2 commits into
pytorch:mainfrom
indrajeetapache:fix/deque-reverse-iterator-variable-192874

Conversation

@indrajeetapache

@indrajeetapache indrajeetapache commented Aug 21, 2026 •

Copy link
Copy Markdown
Contributor

Summary

In CPython, _deque_reverse_iterator is not a subclass of _deque_iterator:

>>> from collections import deque
>>> issubclass(type(reversed(deque())), type(iter(deque())))
False

dir() on both is the same (__iter__, __next__, __length_hint__). Dynamo still had DequeReverseIteratorVariable(DequeIteratorVariable), so every isinstance(x, DequeIteratorVariable) check also matched reverse deque iterators.

This follows the same shape as #193261 (deque forward) and #193717 (tuple): reparent onto the existing BaseListIteratorVariable so the two deque iterator VTs are siblings. No new base class.

The deque mutation snapshot (source_deque / saved_state) cannot be inherited from DequeIteratorVariable after the split, or reverse iterators would still type-check as deque iterators. It is copied onto DequeReverseIteratorVariable so reversed(d) still raises RuntimeError: deque mutated during iteration.

Files touched

File What changed
torch/_dynamo/variables/lists.py DequeReverseIteratorVariable now subclasses BaseListIteratorVariable. Mutation snapshot copied so reverse iterators stay siblings.
torch/_dynamo/symbolic_convert.py GET_YIELD_FROM_ITER lists DequeReverseIteratorVariable explicitly (same pattern as list/tuple/deque after #193717).
torch/_dynamo/utils.py _unpack_fast_types adds DequeReverseIteratorVariable so reverse deque unpack keeps the fast path.
torch/_dynamo/variables/__init__.py Import DequeReverseIteratorVariable (not exported in __all__, same as DequeIteratorVariable).
test/dynamo/test_iterators.py Hierarchy, yield from reversed(deque), and type(reversed(deque)) regression tests.

Not touched: side_effects.py. Deque iterators are built sourceless with ValueMutationNew and never hit the list/tuple iterator replay handler (#193261). Reverse is the same.

Part of #192874

Test plan

python test/dynamo/test_iterators.py TestIterators.test_deque_reverse_iterator_not_a_deque_iterator
python test/dynamo/test_iterators.py TestIterators.test_yield_from_deque_reverse_iterator
python test/dynamo/test_iterators.py TestIterators.test_deque_reverse_iterator_python_type
python test/dynamo/test_iterators.py
python test/dynamo/test_sequence_ops.py -k deque_reverse

cc @guilhermeleobas @williamwen42 @jansel @Skylion007 @albanD @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @kadeng @chauhang @amjames @jataylo @azahed98

… to match CPython

_deque_reverse_iterator is not a subclass of _deque_iterator. Reparent
DequeReverseIteratorVariable onto BaseListIteratorVariable so the two
deque iterator VTs are siblings, matching CPython.

The deque mutation snapshot (source_deque / saved_state) is copied onto
the reverse class rather than inherited, so isinstance(..., DequeIteratorVariable)
no longer matches reverse iterators. Call sites list both types
explicitly: GET_YIELD_FROM_ITER and _unpack_fast_types.

Part of pytorch#192874

Test plan:
- python test/dynamo/test_iterators.py TestIterators.test_deque_reverse_iterator_not_a_deque_iterator
- python test/dynamo/test_iterators.py TestIterators.test_yield_from_deque_reverse_iterator
- python test/dynamo/test_iterators.py TestIterators.test_deque_reverse_iterator_python_type
- python test/dynamo/test_iterators.py
- python test/dynamo/test_sequence_ops.py -k deque_reverse

Authored with an AI assistant.
@pytorch-bot

pytorch-bot Bot commented Aug 21, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ You can merge normally! (1 Unrelated Failure)

As of commit 7bec86f with merge base 0fa7965 (image):

BROKEN TRUNK - The following job failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

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

@pytorch-bot pytorch-bot Bot added ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests module: dynamo labels Aug 21, 2026
@pytorch-bot

pytorch-bot Bot commented Aug 21, 2026 •

Copy link
Copy Markdown

Workflows were awaiting approval. CI has now been triggered for the ciflow labels on this PR.

@pytorch-bot

pytorch-bot Bot commented Aug 21, 2026

Copy link
Copy Markdown

The following ciflow label(s) have been added but CI has not been triggered yet because the workflows are awaiting approval:

  • ciflow/inductor
  • ciflow/torchtitan

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.

@jbschlosser jbschlosser added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Aug 21, 2026

@guilhermeleobas guilhermeleobas left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Can you fix the lintrunner issue?

@hameerabbasi

Copy link
Copy Markdown
Collaborator

@pytorchbot lint

@hameerabbasi

Copy link
Copy Markdown
Collaborator

@pytorchbot fix-lint

@guilhermeleobas

Copy link
Copy Markdown
Collaborator

@indrajeetapache can you fix the lint issue?

lintrunner PYFMT failed because two added lines exceeded the 88-char
limit: the issubclass assertion in the new deque reverse-iterator test,
and the GET_YIELD_FROM_ITER iter_vts tuple. Wrap them to match black.

Test Plan:
- black --line-length 88 --check on the two files (passes)

Authored with an AI assistant; human-reviewed before submit.
@indrajeetapache

Copy link
Copy Markdown
Contributor Author

@guilhermeleobas fixed — thanks for the ping.

@guilhermeleobas

Copy link
Copy Markdown
Collaborator

@pytorchbot merge -i

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Sep 1, 2026
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged while ignoring the following 1 checks: torchtitan-test / torchtitan-x-pytorch-test / test (torchtitan_models_integration, 1, 1, mt-l-x86aavx2-189-704-a10g-8)

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: 1 jobs have failed, first few of them are: trunk / linux-jammy-cuda13.2-py3.10-gcc11 / test (pr_time_benchmarks, 1, 1, mt-l-bx86iavx512-94-344-t4-8)

Details for Dev Infra team Raised by workflow job

@guilhermeleobas

Copy link
Copy Markdown
Collaborator

@pytorchbot merge -i

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged while ignoring the following 1 checks: torchtitan-test / torchtitan-x-pytorch-test / test (torchtitan_models_integration, 1, 1, mt-l-x86aavx2-189-704-a10g-8)

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

meta-codesync Bot pushed a commit to pytorch/benchmark that referenced this pull request Sep 3, 2026
… CPython (#194348)

Summary:
## Summary

In CPython, `_deque_reverse_iterator` is not a subclass of `_deque_iterator`:

```
>>> from collections import deque
>>> issubclass(type(reversed(deque())), type(iter(deque())))
False
```

`dir()` on both is the same (`__iter__`, `__next__`, `__length_hint__`). Dynamo still had `DequeReverseIteratorVariable(DequeIteratorVariable)`, so every `isinstance(x, DequeIteratorVariable)` check also matched reverse deque iterators.

This follows the same shape as #193261 (deque forward) and #193717 (tuple): reparent onto the existing `BaseListIteratorVariable` so the two deque iterator VTs are siblings. No new base class.

The deque mutation snapshot (`source_deque` / `saved_state`) cannot be inherited from `DequeIteratorVariable` after the split, or reverse iterators would still type-check as deque iterators. It is copied onto `DequeReverseIteratorVariable` so `reversed(d)` still raises `RuntimeError: deque mutated during iteration`.

### Files touched

| File | What changed |
| --- | --- |
| `torch/_dynamo/variables/lists.py` | `DequeReverseIteratorVariable` now subclasses `BaseListIteratorVariable`. Mutation snapshot copied so reverse iterators stay siblings. |
| `torch/_dynamo/symbolic_convert.py` | `GET_YIELD_FROM_ITER` lists `DequeReverseIteratorVariable` explicitly (same pattern as list/tuple/deque after #193717). |
| `torch/_dynamo/utils.py` | `_unpack_fast_types` adds `DequeReverseIteratorVariable` so reverse deque unpack keeps the fast path. |
| `torch/_dynamo/variables/__init__.py` | Import `DequeReverseIteratorVariable` (not exported in `__all__`, same as `DequeIteratorVariable`). |
| `test/dynamo/test_iterators.py` | Hierarchy, `yield from reversed(deque)`, and `type(reversed(deque))` regression tests. |

Not touched: `side_effects.py`. Deque iterators are built sourceless with `ValueMutationNew` and never hit the list/tuple iterator replay handler (#193261). Reverse is the same.

Part of #192874

## Test plan

```
python test/dynamo/test_iterators.py TestIterators.test_deque_reverse_iterator_not_a_deque_iterator
python test/dynamo/test_iterators.py TestIterators.test_yield_from_deque_reverse_iterator
python test/dynamo/test_iterators.py TestIterators.test_deque_reverse_iterator_python_type
python test/dynamo/test_iterators.py
python test/dynamo/test_sequence_ops.py -k deque_reverse
```

X-link: pytorch/pytorch#194348
Approved by: https://github.com/guilhermeleobas, https://github.com/hameerabbasi

Reviewed By: huydhn

Differential Revision: D118488968

fbshipit-source-id: 9e6eaa7b62840a79cf2951c5b91d4cfc65007064
Sanskaaar1 pushed a commit to Sanskaaar1/pytorch that referenced this pull request Sep 8, 2026
… to match CPython (pytorch#194348)

## Summary

In CPython, `_deque_reverse_iterator` is not a subclass of `_deque_iterator`:

```
>>> from collections import deque
>>> issubclass(type(reversed(deque())), type(iter(deque())))
False
```

`dir()` on both is the same (`__iter__`, `__next__`, `__length_hint__`). Dynamo still had `DequeReverseIteratorVariable(DequeIteratorVariable)`, so every `isinstance(x, DequeIteratorVariable)` check also matched reverse deque iterators.

This follows the same shape as pytorch#193261 (deque forward) and pytorch#193717 (tuple): reparent onto the existing `BaseListIteratorVariable` so the two deque iterator VTs are siblings. No new base class.

The deque mutation snapshot (`source_deque` / `saved_state`) cannot be inherited from `DequeIteratorVariable` after the split, or reverse iterators would still type-check as deque iterators. It is copied onto `DequeReverseIteratorVariable` so `reversed(d)` still raises `RuntimeError: deque mutated during iteration`.

### Files touched

| File | What changed |
| --- | --- |
| `torch/_dynamo/variables/lists.py` | `DequeReverseIteratorVariable` now subclasses `BaseListIteratorVariable`. Mutation snapshot copied so reverse iterators stay siblings. |
| `torch/_dynamo/symbolic_convert.py` | `GET_YIELD_FROM_ITER` lists `DequeReverseIteratorVariable` explicitly (same pattern as list/tuple/deque after pytorch#193717). |
| `torch/_dynamo/utils.py` | `_unpack_fast_types` adds `DequeReverseIteratorVariable` so reverse deque unpack keeps the fast path. |
| `torch/_dynamo/variables/__init__.py` | Import `DequeReverseIteratorVariable` (not exported in `__all__`, same as `DequeIteratorVariable`). |
| `test/dynamo/test_iterators.py` | Hierarchy, `yield from reversed(deque)`, and `type(reversed(deque))` regression tests. |

Not touched: `side_effects.py`. Deque iterators are built sourceless with `ValueMutationNew` and never hit the list/tuple iterator replay handler (pytorch#193261). Reverse is the same.

Part of pytorch#192874

## Test plan

```
python test/dynamo/test_iterators.py TestIterators.test_deque_reverse_iterator_not_a_deque_iterator
python test/dynamo/test_iterators.py TestIterators.test_yield_from_deque_reverse_iterator
python test/dynamo/test_iterators.py TestIterators.test_deque_reverse_iterator_python_type
python test/dynamo/test_iterators.py
python test/dynamo/test_sequence_ops.py -k deque_reverse
```

Pull Request resolved: pytorch#194348
Approved by: https://github.com/guilhermeleobas, https://github.com/hameerabbasi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests ciflow/trunk Trigger trunk jobs on your pull request Merged module: dynamo open source release notes: dynamo triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants