Skip to content

Fixes for collections.Counter #159368

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

Open
wants to merge 10 commits into
base: gh/guilhermeleobas/217/base
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions torch/_dynamo/polyfills/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
# See also the POLYFILLED_MODULE_NAMES in torch/_dynamo/polyfills/loader.py
# Put the submodules here to avoid circular imports
from . import (
_collections as _collections,
builtins as builtins,
functools as functools,
itertools as itertools,
Expand Down
33 changes: 33 additions & 0 deletions torch/_dynamo/polyfills/_collections.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""
Python polyfills for builtins
"""

from collections.abc import Iterable, MutableMapping
from typing import TypeVar

from ..decorators import substitute_in_graph


__all__ = []


T = TypeVar("T")


try:
import _collections # type: ignore[import-not-found]

@substitute_in_graph(_collections._count_elements)
def _count_elements(
mapping: MutableMapping[T, int],
iterable: Iterable[T],
) -> None:
"Tally elements from the iterable."
mapping_get = mapping.get
for elem in iterable:
mapping[elem] = mapping_get(elem, 0) + 1

__all__.append("_count_elements")

except ImportError:
pass
1 change: 1 addition & 0 deletions torch/_dynamo/polyfills/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

# See also the TYPE_CHECKING block in torch/_dynamo/polyfills/__init__.py
POLYFILLED_MODULE_NAMES: tuple[str, ...] = (
"_collections",
"builtins",
"functools",
"itertools",
Expand Down
7 changes: 7 additions & 0 deletions torch/_dynamo/variables/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2540,6 +2540,13 @@ def call_neg(self, tx: "InstructionTranslator", a):
(operator.neg)(a.as_proxy()),
sym_num=None,
)

if (
isinstance(a, UserDefinedObjectVariable)
and a.call_obj_hasattr(tx, "__neg__").value # type: ignore[attr-defined]
):
return a.call_method(tx, "__neg__", [], {})

# None no-ops this handler and lets the driving function proceed
return None

Expand Down
16 changes: 15 additions & 1 deletion torch/_dynamo/variables/user_defined.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
from ..exc import (
handle_observed_exception,
ObservedAttributeError,
ObservedKeyError,
raise_observed_exception,
unimplemented_v2,
)
Expand Down Expand Up @@ -1874,7 +1875,20 @@ def call_method(
) -> "VariableTracker":
method = self._maybe_get_baseclass_method(name)
if method in self._dict_methods:
return self._dict_vt.call_method(tx, name, args, kwargs)
# Dict subclasses can override __missing__ to provide fallback
# behavior instead of raising a KeyError. This is used, for example,
# by collections.Counter.
try:
return self._dict_vt.call_method(tx, name, args, kwargs)
except ObservedKeyError:
if (
name == "__getitem__"
and issubclass(self.python_type(), dict)
and self._maybe_get_baseclass_method("__missing__")
):
return self.call_method(tx, "__missing__", args, kwargs)
else:
raise
return super().call_method(tx, name, args, kwargs)

def unpack_var_sequence(self, tx):
Expand Down
Loading