Skip to content

Commit c62eba3

Browse files
Fixes for collections.Counter
ghstack-source-id: 95a24ee Pull-Request: #159368
1 parent d7232cb commit c62eba3

13 files changed

+58
-5
lines changed

test/dynamo_expected_failures/CPython313-test_collections-TestCounter.test_eq

Whitespace-only changes.

test/dynamo_expected_failures/CPython313-test_collections-TestCounter.test_ge

Whitespace-only changes.

test/dynamo_expected_failures/CPython313-test_collections-TestCounter.test_gt

Whitespace-only changes.

test/dynamo_expected_failures/CPython313-test_collections-TestCounter.test_helper_function

Whitespace-only changes.

test/dynamo_expected_failures/CPython313-test_collections-TestCounter.test_le

Whitespace-only changes.

test/dynamo_expected_failures/CPython313-test_collections-TestCounter.test_lt

Whitespace-only changes.

test/dynamo_expected_failures/CPython313-test_collections-TestCounter.test_unary

Whitespace-only changes.

torch/_dynamo/polyfills/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
# See also the POLYFILLED_MODULE_NAMES in torch/_dynamo/polyfills/loader.py
2525
# Put the submodules here to avoid circular imports
2626
from . import (
27+
_collections as _collections,
2728
builtins as builtins,
2829
functools as functools,
2930
itertools as itertools,
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
Python polyfills for builtins
3+
"""
4+
5+
from ..decorators import substitute_in_graph
6+
from typing import MutableMapping, Iterable, Any
7+
8+
9+
__all__ = []
10+
11+
try:
12+
import _collections
13+
14+
@substitute_in_graph(_collections._count_elements)
15+
def _count_elements(
16+
mapping: MutableMapping[Any, int],
17+
iterable: Iterable[Any],
18+
) -> None:
19+
'Tally elements from the iterable.'
20+
mapping_get = mapping.get
21+
for elem in iterable:
22+
mapping[elem] = mapping_get(elem, 0) + 1
23+
24+
__all__.append("_count_elements")
25+
26+
except ImportError:
27+
pass

torch/_dynamo/polyfills/loader.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
# See also the TYPE_CHECKING block in torch/_dynamo/polyfills/__init__.py
1515
POLYFILLED_MODULE_NAMES: tuple[str, ...] = (
16+
"_collections",
1617
"builtins",
1718
"functools",
1819
"itertools",

0 commit comments

Comments
 (0)