Skip to content

Commit 5c70975

Browse files
guilhermeleobasmarkc-614
authored andcommitted
Fixes for collections.Counter (pytorch#159368)
Pull Request resolved: pytorch#159368 Approved by: https://github.com/mlazos ghstack dependencies: pytorch#159365, pytorch#159366
1 parent 9191e8a commit 5c70975

12 files changed

+57
-1
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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
Python polyfills for builtins
3+
"""
4+
5+
from collections.abc import Iterable, MutableMapping
6+
from typing import TypeVar
7+
8+
from ..decorators import substitute_in_graph
9+
10+
11+
__all__ = []
12+
13+
14+
T = TypeVar("T")
15+
16+
17+
try:
18+
import _collections # type: ignore[import-not-found]
19+
20+
@substitute_in_graph(_collections._count_elements)
21+
def _count_elements(
22+
mapping: MutableMapping[T, int],
23+
iterable: Iterable[T],
24+
) -> None:
25+
"Tally elements from the iterable."
26+
mapping_get = mapping.get
27+
for elem in iterable:
28+
mapping[elem] = mapping_get(elem, 0) + 1
29+
30+
__all__.append("_count_elements")
31+
32+
except ImportError:
33+
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)