pytorch/torch/_dynamo/polyfills
Xuehai Pan e8fadba28c [pytree] add treespec_{leaf,tuple,dict} functions for args_spec modification (#160843)
The goal of this PR is to provide a standard way to create simple treespec instances and hide the implementation details of the `PyTreeSpec` class.

Changes:

1. Add function `treespec_leaf()` to replace `LeafSpec()`.
2. Add function `treespec_tuple(...)` and `treespec_dict(...)` to create treespec for `tuple` / `dict` which is used for `*args` / `**kwargs`. This avoids direct modification to `treespec` instances that rely on the implementation details of the `PyTreeSpec` class.
3. Change `len(spec.children_specs)` to `spec.num_children`.
4. Change `isinstance(spec, LeafSpec)` to `spec.is_leaf()`.

------

Pull Request resolved: https://github.com/pytorch/pytorch/pull/160843
Approved by: https://github.com/mlazos
2025-11-01 04:12:11 +00:00
..
__init__.py [10/N] Apply ruff UP035 rule (#165709) 2025-10-25 00:20:13 +00:00
_collections.py Fixes for collections.Counter (#159368) 2025-08-15 19:08:21 +00:00
builtins.py [10/N] Apply ruff UP035 rule (#165709) 2025-10-25 00:20:13 +00:00
functools.py [10/N] Apply ruff UP035 rule (#165709) 2025-10-25 00:20:13 +00:00
fx.py [10/N] Apply ruff UP035 rule (#165709) 2025-10-25 00:20:13 +00:00
itertools.py Fix error suppression syntax in onnx, jit, _dynamo (#166249) 2025-10-27 02:01:54 +00:00
loader.py Fixes for collections.Counter (#159368) 2025-08-15 19:08:21 +00:00
operator.py Fix error suppression syntax in onnx, jit, _dynamo (#166249) 2025-10-27 02:01:54 +00:00
os.py Fix error suppression syntax in onnx, jit, _dynamo (#166249) 2025-10-27 02:01:54 +00:00
pytree.py [pytree] add treespec_{leaf,tuple,dict} functions for args_spec modification (#160843) 2025-11-01 04:12:11 +00:00
struct.py [struct] Add struct.pack and struct.unpack polyfills (#156977) 2025-07-23 16:12:08 +00:00
sys.py Fixes for CPython int/float tests (#155978) 2025-07-02 15:04:00 +00:00
tensor.py [dynamo][torch-function] Prevent unnecessary __torch_function__ tracing (#153551) 2025-05-15 14:06:17 +00:00