Skip to content

Commit

Permalink
support subclasses of namedtuple type (#140534)
Browse files Browse the repository at this point in the history
Summary:
Allow subclassing namedtuple type. Allow assign attributes to instances of these subtypes.

X-link: pytorch/pytorch#140534
Approved by: https://github.com/jansel

Reviewed By: izaitsevfb

Differential Revision: D66080719

fbshipit-source-id: 5a20fa5e7908234bc6eebb0ad1a7dcd727931b62
  • Loading branch information
XuehaiPan authored and facebook-github-bot committed Nov 18, 2024
1 parent fc80370 commit 9367153
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions userbenchmark/dynamo/dynamobench/_dynamo/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1406,21 +1406,32 @@ def is_namedtuple_cls(cls):
"""Test if an object is a namedtuple or a (torch.return_types|torch.autograd.forward_ad).* quasi-namedtuple"""
try:
if issubclass(cls, tuple):
bases = getattr(cls, "__bases__", []) or [None]
module = getattr(cls, "__module__", None)
return module in ("torch.return_types", "torch.autograd.forward_ad") or (
bases[0] is tuple and hasattr(cls, "_make") and hasattr(cls, "_fields")
)
if module in ("torch.return_types", "torch.autograd.forward_ad"):
return True
if isinstance(getattr(cls, "_fields", None), tuple) and callable(
getattr(cls, "_make", None)
):
if cls.__bases__ == (tuple,):
# This is a namedtuple type directly created by `collections.namedtuple(...)`
return True
if (
# Subclass of namedtuple
is_namedtuple_cls(cls.__bases__[0])
# For subclasses of namedtuple, the __new__ method should not be customized
and cls.__new__ is cls.__bases__[0].__new__
):
return True
except TypeError:
pass
return False


@functools.lru_cache(1)
def namedtuple_fields(cls):
def namedtuple_fields(cls) -> Tuple[str, ...]:
"""Get the fields of a namedtuple or a torch.return_types.* quasi-namedtuple"""
if cls is slice:
return ["start", "stop", "step"]
return ("start", "stop", "step")

assert issubclass(cls, tuple)
if hasattr(cls, "_fields"):
Expand All @@ -1434,11 +1445,12 @@ class Marker:
# frustrating ones e.g. torch.return_types.max
assert cls.__module__ == "torch.return_types"
obj = cls(map(Marker, range(cls.n_fields)))
fields: List[Optional[str]] = [None] * cls.n_fields
fields: Dict[str, int] = {}
for name in dir(obj):
if name[0] != "_" and isinstance(getattr(obj, name), Marker):
fields[getattr(obj, name).index] = name
return fields
fields[name] = getattr(obj, name).index
assert len(fields) == cls.n_fields
return tuple(sorted(fields, key=fields.get)) # type: ignore[arg-type]


def checkpoint_params(gm):
Expand Down

0 comments on commit 9367153

Please sign in to comment.