|
| 1 | +# Owner(s): ["module: pytree"] |
| 2 | + |
| 3 | +""" |
| 4 | +Contains utility functions for working with nested python data structures. |
| 5 | +
|
| 6 | +A *pytree* is Python nested data structure. It is a tree in the sense that |
| 7 | +nodes are Python collections (e.g., list, tuple, dict) and the leaves are |
| 8 | +Python values. Furthermore, a pytree should not contain reference cycles. |
| 9 | +
|
| 10 | +pytrees are useful for working with nested collections of Tensors. For example, |
| 11 | +one can use `map` to map a function over all Tensors inside some nested |
| 12 | +collection of Tensors and `leaves` to get a flat list of all Tensors |
| 13 | +inside some nested collection. pytrees are helpful for implementing nested |
| 14 | +collection support for PyTorch APIs. |
| 15 | +""" |
| 16 | + |
| 17 | +from __future__ import annotations |
| 18 | + |
| 19 | +from typing import Any as _Any, TYPE_CHECKING as _TYPE_CHECKING |
| 20 | + |
| 21 | +import torch |
| 22 | +from torch.utils.pytree import ( |
| 23 | + register_pytree_node as register_node, |
| 24 | + tree_all as all, |
| 25 | + tree_all_only as all_only, |
| 26 | + tree_any as any, |
| 27 | + tree_any_only as any_only, |
| 28 | + tree_flatten as flatten, |
| 29 | + tree_iter as iter, |
| 30 | + tree_leaves as leaves, |
| 31 | + tree_map as map, |
| 32 | + tree_map_ as map_, |
| 33 | + tree_map_only as map_only, |
| 34 | + tree_map_only_ as map_only_, |
| 35 | + tree_structure as structure, |
| 36 | +) |
| 37 | + |
| 38 | + |
| 39 | +if _TYPE_CHECKING: |
| 40 | + from collections.abc import Iterable |
| 41 | + |
| 42 | + from torch.utils._cxx_pytree import PyTree as PyTree, PyTreeSpec as PyTreeSpec |
| 43 | + |
| 44 | + |
| 45 | +__all__ = [ |
| 46 | + "PyTreeSpec", |
| 47 | + "register_node", |
| 48 | + "flatten", |
| 49 | + "unflatten", |
| 50 | + "iter", |
| 51 | + "leaves", |
| 52 | + "structure", |
| 53 | + "map", |
| 54 | + "map_", |
| 55 | + "map_only", |
| 56 | + "map_only_", |
| 57 | + "all", |
| 58 | + "any", |
| 59 | + "all_only", |
| 60 | + "any_only", |
| 61 | +] |
| 62 | + |
| 63 | + |
| 64 | +def unflatten(treespec: PyTreeSpec, leaves: Iterable[_Any]) -> PyTree: |
| 65 | + """Reconstruct a pytree from the treespec and the leaves. |
| 66 | +
|
| 67 | + The inverse of :func:`flatten`. |
| 68 | +
|
| 69 | + >>> tree = {"b": (2, [3, 4]), "a": 1, "c": None, "d": 5} |
| 70 | + >>> leaves, treespec = torch.pytree.flatten(tree) |
| 71 | + >>> tree == torch.pytree.unflatten(treespec, leaves) |
| 72 | + True |
| 73 | +
|
| 74 | + .. note:: |
| 75 | +
|
| 76 | + This function has a different signature than :func:`torch.utils.pytree.tree_unflatten`. |
| 77 | + The ``treespec`` argument comes first to have a better :class:`functools.partial` support: |
| 78 | +
|
| 79 | + .. code-block:: python |
| 80 | +
|
| 81 | + import functools |
| 82 | +
|
| 83 | + unflatten_fn = functools.partial(unflatten, treespec) |
| 84 | + tree1 = unflatten_fn(leaves1) |
| 85 | + tree2 = unflatten_fn(leaves2) |
| 86 | +
|
| 87 | + Args: |
| 88 | + treespec (PyTreeSpec): The treespec to reconstruct. |
| 89 | + leaves (iterable): The list of leaves to use for reconstruction. The list must match the |
| 90 | + number of leaves of the treespec. |
| 91 | +
|
| 92 | + Returns: |
| 93 | + The reconstructed pytree, containing the ``leaves`` placed in the structure described by |
| 94 | + ``treespec``. |
| 95 | + """ |
| 96 | + return torch.utils.pytree.tree_unflatten(leaves, treespec) |
| 97 | + |
| 98 | + |
| 99 | +def __getattr__(name: str) -> _Any: |
| 100 | + if name in ("PyTreeSpec", "TreeSpec"): |
| 101 | + return torch.utils.pytree.PyTreeSpec |
| 102 | + raise AttributeError(f"module {__name__!r} has no attribute {name!r}") |
0 commit comments