pytorch/torch/_dynamo/polyfills
Xuehai Pan a10b765bf1 [pytree] add APIs to determine a class is a namedtuple or PyStructSequence (#113257)
Changes in this PR:

1. Add `is_structseq` and `is_structseq_class` functions to determine a object or a class is PyStructSequence.
2. Add a generic class `structseq` which can be used as the registration key for PyStructSequence types like `namedtuple` for Named Tuple types.
3. Change `is_namedtuple` to accept subclasses of namedtuple to be namedtuple. Before this PR, only namedtuple class directly created by `collections.namedtuple` or `typing.NamedTuple` were namedtuple classes while their subclasses were not. This PR makes `is_namedtuple` return true for subclasses of namedtuple class.

Resolves #75982. New tests are included in this PR.

- #75982

Pull Request resolved: https://github.com/pytorch/pytorch/pull/113257
Approved by: https://github.com/zou3519
2025-04-01 10:40:43 +00:00
..
__init__.py [ca] fix accumulate grad polyfill when different strides between param and grad (#149651) 2025-03-24 19:06:45 +00:00
builtins.py PEP585 update - torch/_dynamo (#145105) 2025-01-18 20:47:11 +00:00
functools.py PEP585 update - torch/_dynamo (#145105) 2025-01-18 20:47:11 +00:00
fx.py [fx] Move map_aggregate to C++ (#148243) 2025-03-10 16:05:53 +00:00
itertools.py [BE][PYFMT] migrate PYFMT for torch._dynamo to ruff format (#144549) 2025-02-28 03:03:53 +00:00
loader.py [fx] Move map_aggregate to C++ (#148243) 2025-03-10 16:05:53 +00:00
operator.py [BE][PYFMT] migrate PYFMT for torch._dynamo to ruff format (#144549) 2025-02-28 03:03:53 +00:00
os.py [dynamo] simplify polyfill registration for builtins.all and builtins.any (#133769) 2024-08-29 20:56:16 +00:00
pytree.py [pytree] add APIs to determine a class is a namedtuple or PyStructSequence (#113257) 2025-04-01 10:40:43 +00:00
sys.py [dynamo][polyfills]Support getrecursionlimit (#145989) 2025-01-31 00:47:31 +00:00