-
Notifications
You must be signed in to change notification settings - Fork 25.7k
[dynamo] add infinite generators itertools.{count, repeat, cycle}
#110967
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
jon-chuang
wants to merge
13
commits into
pytorch:main
from
jon-chuang:jon-chuang/infinite-iterators
Closed
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0fa0d0d
add repeat via polyfill
jon-chuang 3251e95
lint
jon-chuang 144ae40
empty
jon-chuang 4154685
done
jon-chuang af83cc3
remove random
jon-chuang 188cea8
Merge branch 'main' of https://github.com/pytorch/pytorch into jon-ch…
jon-chuang 408b0bf
stash
jon-chuang 509d989
make copy-on-write
jon-chuang 6bcf882
simplify
jon-chuang 56fbb4a
remove stray
jon-chuang 199ec55
Merge branch 'main' of https://github.com/pytorch/pytorch into jon-ch…
jon-chuang f5effd1
Merge branch 'main' of https://github.com/pytorch/pytorch into jon-ch…
jon-chuang 9d1692c
ensure compilation
jon-chuang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| MAX_CYCLE = 3000 | ||
|
|
||
| from typing import List, Optional | ||
|
|
||
| from ..exc import unimplemented | ||
|
|
||
| from .base import VariableTracker | ||
| from .constant import ConstantVariable | ||
|
|
||
|
|
||
| class IteratorVariable(VariableTracker): | ||
| def __init__(self, **kwargs): | ||
| super().__init__(**kwargs) | ||
|
|
||
| def next_variables(self, tx): | ||
| unimplemented("abstract method, must implement") | ||
|
|
||
|
|
||
| class RepeatIteratorVariable(IteratorVariable): | ||
| def __init__(self, item: VariableTracker, **kwargs): | ||
| super().__init__(**kwargs) | ||
| self.item = item | ||
|
|
||
| # Repeat needs no mutation, clone self | ||
| def next_variables(self, tx): | ||
| # add_options will clone self.item | ||
jon-chuang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return self.item.add_options(self), self | ||
|
|
||
|
|
||
| class CountIteratorVariable(IteratorVariable): | ||
| def __init__(self, item: int = 0, step: int = 1, **kwargs): | ||
| super().__init__(**kwargs) | ||
| if not isinstance(item, VariableTracker): | ||
| item = ConstantVariable.create(item) | ||
| if not isinstance(step, VariableTracker): | ||
| step = ConstantVariable.create(step) | ||
| self.item = item | ||
| self.step = step | ||
|
|
||
| def next_variables(self, tx): | ||
| assert self.mutable_local | ||
| next_item = self.item.call_method(tx, "__add__", [self.step], {}) | ||
| next_iter = self.clone(item=next_item) | ||
| tx.replace_all(self, next_iter) | ||
| return self.item.add_options(self), next_iter | ||
|
|
||
|
|
||
| class CycleIteratorVariable(IteratorVariable): | ||
| def __init__( | ||
| self, | ||
| iterator: IteratorVariable, | ||
| saved: List[VariableTracker] = None, | ||
| saved_index: int = 0, | ||
| item: Optional[VariableTracker] = None, | ||
| **kwargs, | ||
| ): | ||
| if saved is None: | ||
| saved = [] | ||
| super().__init__(**kwargs) | ||
| self.iterator = iterator | ||
| self.saved = saved | ||
| self.saved_index = saved_index | ||
| self.item = item | ||
|
|
||
| def next_variables(self, tx): | ||
| assert self.mutable_local | ||
|
|
||
| if self.iterator is not None: | ||
| try: | ||
| new_item, next_inner_iter = self.iterator.next_variables(tx) | ||
| tx.replace_all(self.iterator, next_inner_iter) | ||
| if len(self.saved) > MAX_CYCLE: | ||
| unimplemented( | ||
| "input iterator to itertools.cycle has too many items" | ||
| ) | ||
| next_iter = self.clone( | ||
| iterator=next_inner_iter, | ||
| saved=self.saved + [new_item], | ||
| item=new_item, | ||
| ) | ||
|
|
||
| tx.replace_all(self, next_iter) | ||
| if self.item is None: | ||
| return next_iter.next_variables(tx) | ||
| return self.item.add_options(self), next_iter | ||
| except StopIteration: | ||
jon-chuang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| next_iter = self.clone(iterator=None) | ||
| # this is redundant as next_iter will do the same | ||
| # but we do it anyway for safety | ||
| tx.replace_all(self, next_iter) | ||
| return next_iter.next_variables(tx) | ||
| elif len(self.saved) > 0: | ||
| next_iter = self.clone( | ||
| saved_index=(self.saved_index + 1) % len(self.saved), | ||
| item=self.saved[self.saved_index], | ||
| ) | ||
| tx.replace_all(self, next_iter) | ||
| return self.item.add_options(self), next_iter | ||
| else: | ||
| raise StopIteration | ||
| return self.item.add_options(self), next_iter | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.