dynamo: support next() on itertools.count inputs (#177876)
Summary:
Fix #177196
1. Root cause
Dynamo wrapped pre-existing `itertools.count()` objects as generic user-defined objects, so `next(counter)` fell through to the unsupported `__next__` path. The existing iterator side-effect replay also had no way to keep a live `count()` object synchronized after compiled execution.
2. Proposed fix
Wrap pre-existing `itertools.count()` instances as `CountIteratorVariable`, guard them on their current iterator state, and replay consumed `next()` calls back onto the original iterator after the compiled region. Add regression tests for the external-counter fullgraph case and the new guard.
3. Why the proposed fix is the right long term fix
This keeps `itertools.count()` on Dynamo's existing iterator path instead of treating it like an opaque user object. That gives Dynamo the same three things it already needs for mutable iterators: a dedicated variable type during tracing, guard-based cache invalidation when iterator state changes, and side-effect replay that preserves the original Python object.
Drafted via Codex, published after manual review by bobrenjc93
X-link: https://github.com/pytorch/pytorch/pull/177876
Approved by: https://github.com/Lucaskabela
Reviewed By: georgehong
Differential Revision: D98188470
fbshipit-source-id: d470e75b36fea669c16ade5c7c43205ba4b44410