Skip to content

[dynamo] Fix DequeVariable maxlen=0 keeping items instead of emptying - #195029

Closed
gabrielfruet wants to merge 2 commits into
pytorch:mainfrom
gabrielfruet:deque-maxlen-zero
Closed

gabrielfruet wants to merge 2 commits into
pytorch:mainfrom
gabrielfruet:deque-maxlen-zero

Conversation

@gabrielfruet

@gabrielfruet gabrielfruet commented Aug 27, 2026 •

Copy link
Copy Markdown
Contributor

DequeVariable clamps to maxlen with items[-maxlen:], but when maxlen is 0 that
slice is items[-0:], i.e. items[0:], which returns the whole list instead of an
empty one. So under torch.compile a deque(maxlen=0) modeled its contents wrong:
collections.deque([1, 2, 3], maxlen=0) traced as length 3, and appending or
extending a maxlen=0 deque kept the new items, while CPython keeps it empty in
every case. init (construction) and _clamp_maxlen's right side (append and
extend) both used the bare slice; appendleft/extendleft use items[:maxlen],
which is already correct for 0. Both sites now special-case maxlen == 0 to
items[:0].

Test Plan:

python test/dynamo/test_misc.py MiscTests.test_deque_maxlen_zero
PYTORCH_TEST_WITH_DYNAMO=1 python test/cpython/v3_13/test_deque.py

test_deque_maxlen_zero is new and fails without the fix; the cpython deque suite
stays green.

This change was authored with the assistance of Claude (Anthropic).

Co-Authored-By: Claude Opus 4.8 [email protected]

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

@pytorch-bot

pytorch-bot Bot commented Aug 27, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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 a88cad0 with merge base 5b60cff (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 27, 2026
@pytorch-bot

pytorch-bot Bot commented Aug 27, 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 27, 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.

@gabrielfruet
gabrielfruet marked this pull request as ready for review August 27, 2026 19:05
@gabrielfruet

Copy link
Copy Markdown
Contributor Author

Ready to review @guilhermeleobas

@guilhermeleobas

Copy link
Copy Markdown
Collaborator

Thanks @gabrielfruet

@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 Aug 28, 2026
@guilhermeleobas guilhermeleobas added release notes: dynamo and removed ciflow/trunk Trigger trunk jobs on your pull request labels Aug 28, 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: Command git -C /home/runner/work/pytorch/pytorch rebase origin/main returned non-zero exit code 1

Rebasing (1/1)
Auto-merging test/dynamo/test_misc.py
CONFLICT (content): Merge conflict in test/dynamo/test_misc.py
Auto-merging torch/_dynamo/variables/lists.py
error: could not apply 243fad3c23a... [dynamo] Fix DequeVariable maxlen=0 keeping items instead of emptying (#195029)
hint: Resolve all conflicts manually, mark them as resolved with
hint: "git add/rm <conflicted_files>", then run "git rebase --continue".
hint: You can instead skip this commit: run "git rebase --skip".
hint: To abort and get back to the state before "git rebase", run "git rebase --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
Could not apply 243fad3c23a... # [dynamo] Fix DequeVariable maxlen=0 keeping items instead of emptying (#195029)
Details for Dev Infra team Raised by workflow job

gabrielfruet and others added 2 commits August 28, 2026 08:29
DequeVariable clamps to maxlen with items[-maxlen:], but when maxlen is 0 that
slice is items[-0:], i.e. items[0:], which returns the whole list instead of an
empty one. So under torch.compile a deque(maxlen=0) modeled its contents wrong:
collections.deque([1, 2, 3], maxlen=0) traced as length 3, and appending or
extending a maxlen=0 deque kept the new items, while CPython keeps it empty in
every case. __init__ (construction) and _clamp_maxlen's right side (append and
extend) both used the bare slice; appendleft/extendleft use items[:maxlen],
which is already correct for 0. Both sites now special-case maxlen == 0 to
items[:0].

Test Plan:

```
python test/dynamo/test_misc.py MiscTests.test_deque_maxlen_zero
PYTORCH_TEST_WITH_DYNAMO=1 python test/cpython/v3_13/test_deque.py
```

test_deque_maxlen_zero is new and fails without the fix; the cpython deque suite
stays green.

This change was authored with the assistance of Claude (Anthropic).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Use `maxlen != 0` instead of a bare truthiness check in __init__ and
_clamp_maxlen. maxlen is a non-negative int at both sites, so 0 is the only
falsy value: behavior is unchanged, and the check now reads the same as the
"maxlen == 0" comment above it.

Test Plan: behavior-preserving refactor; covered by test_deque_maxlen_zero
from the parent commit.

This change was authored with the assistance of Claude (Anthropic).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@gabrielfruet

Copy link
Copy Markdown
Contributor Author

@guilhermeleobas I had to sort out a merge conflict.

@Skylion007

Copy link
Copy Markdown
Collaborator

@pytorchbot merge

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

Copy link
Copy Markdown
Collaborator

Merge started

Your 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

Advanced Debugging
Check the merge workflow status
here

orrangetabby17 pushed a commit to orrangetabby17/pytorch that referenced this pull request Sep 2, 2026
…pytorch#195029)

DequeVariable clamps to maxlen with items[-maxlen:], but when maxlen is 0 that
slice is items[-0:], i.e. items[0:], which returns the whole list instead of an
empty one. So under torch.compile a deque(maxlen=0) modeled its contents wrong:
collections.deque([1, 2, 3], maxlen=0) traced as length 3, and appending or
extending a maxlen=0 deque kept the new items, while CPython keeps it empty in
every case. __init__ (construction) and _clamp_maxlen's right side (append and
extend) both used the bare slice; appendleft/extendleft use items[:maxlen],
which is already correct for 0. Both sites now special-case maxlen == 0 to
items[:0].

Test Plan:

```
python test/dynamo/test_misc.py MiscTests.test_deque_maxlen_zero
PYTORCH_TEST_WITH_DYNAMO=1 python test/cpython/v3_13/test_deque.py
```

test_deque_maxlen_zero is new and fails without the fix; the cpython deque suite
stays green.

This change was authored with the assistance of Claude (Anthropic).

Co-Authored-By: Claude Opus 4.8 <[email protected]>

Pull Request resolved: pytorch#195029
Approved by: https://github.com/guilhermeleobas

Co-authored-by: Claude Opus 4.8 <[email protected]>
Sanskaaar1 pushed a commit to Sanskaaar1/pytorch that referenced this pull request Sep 8, 2026
…pytorch#195029)

DequeVariable clamps to maxlen with items[-maxlen:], but when maxlen is 0 that
slice is items[-0:], i.e. items[0:], which returns the whole list instead of an
empty one. So under torch.compile a deque(maxlen=0) modeled its contents wrong:
collections.deque([1, 2, 3], maxlen=0) traced as length 3, and appending or
extending a maxlen=0 deque kept the new items, while CPython keeps it empty in
every case. __init__ (construction) and _clamp_maxlen's right side (append and
extend) both used the bare slice; appendleft/extendleft use items[:maxlen],
which is already correct for 0. Both sites now special-case maxlen == 0 to
items[:0].

Test Plan:

```
python test/dynamo/test_misc.py MiscTests.test_deque_maxlen_zero
PYTORCH_TEST_WITH_DYNAMO=1 python test/cpython/v3_13/test_deque.py
```

test_deque_maxlen_zero is new and fails without the fix; the cpython deque suite
stays green.

This change was authored with the assistance of Claude (Anthropic).

Co-Authored-By: Claude Opus 4.8 <[email protected]>

Pull Request resolved: pytorch#195029
Approved by: https://github.com/guilhermeleobas

Co-authored-by: Claude Opus 4.8 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants