_cxx_pytree.py 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  1. """
  2. Contains utility functions for working with nested python data structures.
  3. A *pytree* is Python nested data structure. It is a tree in the sense that
  4. nodes are Python collections (e.g., list, tuple, dict) and the leaves are
  5. Python values. Furthermore, a pytree should not contain reference cycles.
  6. pytrees are useful for working with nested collections of Tensors. For example,
  7. one can use `tree_map` to map a function over all Tensors inside some nested
  8. collection of Tensors and `tree_leaves` to get a flat list of all Tensors
  9. inside some nested collection. pytrees are helpful for implementing nested
  10. collection support for PyTorch APIs.
  11. """
  12. import functools
  13. import sys
  14. import types
  15. from collections.abc import Iterable
  16. from typing import Any, Callable, Optional, overload, TypeVar, Union
  17. from typing_extensions import deprecated, TypeIs
  18. import torch.utils._pytree as python_pytree
  19. from torch.torch_version import TorchVersion as _TorchVersion
  20. from torch.utils._pytree import (
  21. is_namedtuple as is_namedtuple,
  22. is_namedtuple_class as is_namedtuple_class,
  23. is_namedtuple_instance as is_namedtuple_instance,
  24. is_structseq as is_structseq,
  25. is_structseq_class as is_structseq_class,
  26. is_structseq_instance as is_structseq_instance,
  27. KeyEntry as KeyEntry,
  28. )
  29. # Do not try to import `optree` package if the static version check already fails.
  30. if not python_pytree._cxx_pytree_dynamo_traceable:
  31. raise ImportError(
  32. f"{__name__} depends on `optree>={python_pytree._optree_minimum_version}`, "
  33. "which is an optional dependency of PyTorch. "
  34. "To use it, please upgrade your optree package via "
  35. "`python3 -m pip install --upgrade optree`"
  36. )
  37. import optree
  38. from optree import PyTreeSpec as TreeSpec # direct import for type annotations
  39. __all__ = [
  40. "PyTree",
  41. "Context",
  42. "FlattenFunc",
  43. "UnflattenFunc",
  44. "DumpableContext",
  45. "ToDumpableContextFn",
  46. "FromDumpableContextFn",
  47. "TreeSpec",
  48. "LeafSpec",
  49. "keystr",
  50. "key_get",
  51. "register_pytree_node",
  52. "tree_is_leaf",
  53. "tree_flatten",
  54. "tree_flatten_with_path",
  55. "tree_unflatten",
  56. "tree_iter",
  57. "tree_leaves",
  58. "tree_leaves_with_path",
  59. "tree_structure",
  60. "tree_map",
  61. "tree_map_with_path",
  62. "tree_map_",
  63. "tree_map_only",
  64. "tree_map_only_",
  65. "tree_all",
  66. "tree_any",
  67. "tree_all_only",
  68. "tree_any_only",
  69. "treespec_dumps",
  70. "treespec_loads",
  71. "treespec_pprint",
  72. "is_namedtuple",
  73. "is_namedtuple_class",
  74. "is_namedtuple_instance",
  75. "is_structseq",
  76. "is_structseq_class",
  77. "is_structseq_instance",
  78. ]
  79. # In-tree installation may have VCS-based versioning. Update the previous static version.
  80. python_pytree._optree_version = _TorchVersion(optree.__version__) # type: ignore[attr-defined]
  81. __TORCH_DICT_SESSION = optree.dict_insertion_ordered(True, namespace="torch")
  82. __TORCH_DICT_SESSION.__enter__() # enable globally and permanently
  83. T = TypeVar("T")
  84. S = TypeVar("S")
  85. U = TypeVar("U")
  86. R = TypeVar("R")
  87. Context = Any
  88. PyTree = Any
  89. FlattenFunc = Callable[[PyTree], tuple[list[Any], Context]]
  90. UnflattenFunc = Callable[[Iterable[Any], Context], PyTree]
  91. OpTreeUnflattenFunc = Callable[[Context, Iterable[Any]], PyTree]
  92. DumpableContext = Any # Any json dumpable text
  93. ToDumpableContextFn = Callable[[Context], DumpableContext]
  94. FromDumpableContextFn = Callable[[DumpableContext], Context]
  95. KeyPath = tuple[KeyEntry, ...]
  96. FlattenWithKeysFunc = Callable[[PyTree], tuple[list[tuple[KeyEntry, Any]], Any]]
  97. def _reverse_args(func: UnflattenFunc) -> OpTreeUnflattenFunc:
  98. @functools.wraps(func)
  99. def wrapped(*args: Any, **kwargs: Any) -> Any:
  100. return func(*reversed(args), **kwargs)
  101. return wrapped
  102. def register_pytree_node(
  103. cls: type[Any],
  104. flatten_fn: FlattenFunc,
  105. unflatten_fn: UnflattenFunc,
  106. *,
  107. serialized_type_name: Optional[str] = None,
  108. to_dumpable_context: Optional[ToDumpableContextFn] = None,
  109. from_dumpable_context: Optional[FromDumpableContextFn] = None,
  110. flatten_with_keys_fn: Optional[FlattenWithKeysFunc] = None,
  111. ) -> None:
  112. """Register a container-like type as pytree node.
  113. Args:
  114. cls (type): A Python type to treat as an internal pytree node.
  115. flatten_fn (callable): A function to be used during flattening, taking an instance of
  116. ``cls`` and returning a pair, with (1) an iterable for the children to be flattened
  117. recursively, and (2) some hashable auxiliary data to be stored in the treespec and to be
  118. passed to the ``unflatten_fn``.
  119. unflatten_fn (callable): A function taking two arguments: the auxiliary data that was
  120. returned by ``flatten_fn`` and stored in the treespec, and the unflattened children.
  121. The function should return an instance of ``cls``.
  122. serialized_type_name (str, optional): A keyword argument used to specify the fully
  123. qualified name used when serializing the tree spec.
  124. to_dumpable_context (callable, optional): An optional keyword argument to custom specify how
  125. to convert the context of the pytree to a custom json dumpable representation. This is
  126. used for json serialization, which is being used in :mod:`torch.export` right now.
  127. from_dumpable_context (callable, optional): An optional keyword argument to custom specify
  128. how to convert the custom json dumpable representation of the context back to the
  129. original context. This is used for json deserialization, which is being used in
  130. :mod:`torch.export` right now.
  131. Example::
  132. >>> # xdoctest: +SKIP
  133. >>> # Registry a Python type with lambda functions
  134. >>> register_pytree_node(
  135. ... set,
  136. ... lambda s: (sorted(s), None, None),
  137. ... lambda children, _: set(children),
  138. ... )
  139. """
  140. if flatten_with_keys_fn is not None:
  141. raise NotImplementedError("KeyPaths are not yet supported in cxx_pytree.")
  142. _private_register_pytree_node(
  143. cls,
  144. flatten_fn,
  145. unflatten_fn,
  146. serialized_type_name=serialized_type_name,
  147. to_dumpable_context=to_dumpable_context,
  148. from_dumpable_context=from_dumpable_context,
  149. )
  150. python_pytree._private_register_pytree_node(
  151. cls,
  152. flatten_fn,
  153. unflatten_fn,
  154. serialized_type_name=serialized_type_name,
  155. to_dumpable_context=to_dumpable_context,
  156. from_dumpable_context=from_dumpable_context,
  157. )
  158. @deprecated(
  159. "`torch.utils._cxx_pytree._register_pytree_node` is deprecated. "
  160. "Please use `torch.utils._cxx_pytree.register_pytree_node` instead.",
  161. category=FutureWarning,
  162. )
  163. def _register_pytree_node(
  164. cls: type[Any],
  165. flatten_fn: FlattenFunc,
  166. unflatten_fn: UnflattenFunc,
  167. *,
  168. serialized_type_name: Optional[str] = None,
  169. to_dumpable_context: Optional[ToDumpableContextFn] = None,
  170. from_dumpable_context: Optional[FromDumpableContextFn] = None,
  171. ) -> None:
  172. """Register a container-like type as pytree node for the C++ pytree only.
  173. The ``namespace`` argument is used to avoid collisions that occur when different libraries
  174. register the same Python type with different behaviors. It is recommended to add a unique prefix
  175. to the namespace to avoid conflicts with other libraries. Namespaces can also be used to specify
  176. the same class in different namespaces for different use cases.
  177. .. warning::
  178. For safety reasons, a ``namespace`` must be specified while registering a custom type. It is
  179. used to isolate the behavior of flattening and unflattening a pytree node type. This is to
  180. prevent accidental collisions between different libraries that may register the same type.
  181. Args:
  182. cls (type): A Python type to treat as an internal pytree node.
  183. flatten_fn (callable): A function to be used during flattening, taking an instance of
  184. ``cls`` and returning a pair, with (1) an iterable for the children to be flattened
  185. recursively, and (2) some hashable auxiliary data to be stored in the treespec and to be
  186. passed to the ``unflatten_fn``.
  187. unflatten_fn (callable): A function taking two arguments: the auxiliary data that was
  188. returned by ``flatten_fn`` and stored in the treespec, and the unflattened children.
  189. The function should return an instance of ``cls``.
  190. serialized_type_name (str, optional): A keyword argument used to specify the fully
  191. qualified name used when serializing the tree spec.
  192. to_dumpable_context (callable, optional): An optional keyword argument to custom specify how
  193. to convert the context of the pytree to a custom json dumpable representation. This is
  194. used for json serialization, which is being used in :mod:`torch.export` right now.
  195. from_dumpable_context (callable, optional): An optional keyword argument to custom specify
  196. how to convert the custom json dumpable representation of the context back to the
  197. original context. This is used for json deserialization, which is being used in
  198. :mod:`torch.export` right now.
  199. """
  200. _private_register_pytree_node(
  201. cls,
  202. flatten_fn,
  203. unflatten_fn,
  204. serialized_type_name=serialized_type_name,
  205. to_dumpable_context=to_dumpable_context,
  206. from_dumpable_context=from_dumpable_context,
  207. )
  208. def _private_register_pytree_node(
  209. cls: type[Any],
  210. flatten_fn: FlattenFunc,
  211. unflatten_fn: UnflattenFunc,
  212. *,
  213. serialized_type_name: Optional[str] = None,
  214. to_dumpable_context: Optional[ToDumpableContextFn] = None,
  215. from_dumpable_context: Optional[FromDumpableContextFn] = None,
  216. ) -> None:
  217. """This is an internal function that is used to register a pytree node type
  218. for the C++ pytree only. End-users should use :func:`register_pytree_node`
  219. instead.
  220. """
  221. # TODO(XuehaiPan): remove this condition when we make Python pytree out-of-box support
  222. # PyStructSequence types
  223. if not optree.is_structseq_class(cls):
  224. optree.register_pytree_node(
  225. cls,
  226. flatten_fn,
  227. _reverse_args(unflatten_fn),
  228. namespace="torch",
  229. )
  230. def _is_pytreespec_instance(obj: Any, /) -> TypeIs[TreeSpec]:
  231. return isinstance(obj, TreeSpec)
  232. def tree_is_leaf(
  233. tree: PyTree,
  234. is_leaf: Optional[Callable[[PyTree], bool]] = None,
  235. ) -> bool:
  236. """Check if a pytree is a leaf.
  237. >>> tree_is_leaf(1)
  238. True
  239. >>> tree_is_leaf(None)
  240. True
  241. >>> tree_is_leaf([1, 2, 3])
  242. False
  243. >>> tree_is_leaf((1, 2, 3), is_leaf=lambda x: isinstance(x, tuple))
  244. True
  245. >>> tree_is_leaf({"a": 1, "b": 2, "c": 3})
  246. False
  247. >>> tree_is_leaf({"a": 1, "b": 2, "c": None})
  248. False
  249. Args:
  250. tree (pytree): A pytree to check if it is a leaf node.
  251. is_leaf (callable, optional): An extra leaf predicate function that will be called at each
  252. flattening step. The function should have a single argument with signature
  253. ``is_leaf(node) -> bool``. If it returns :data:`True`, the whole subtree being treated
  254. as a leaf. Otherwise, the default pytree registry will be used to determine a node is a
  255. leaf or not. If the function is not specified, the default pytree registry will be used.
  256. Returns:
  257. A boolean indicating if the pytree is a leaf node.
  258. """
  259. return optree.tree_is_leaf(
  260. tree,
  261. is_leaf=is_leaf,
  262. none_is_leaf=True,
  263. namespace="torch",
  264. )
  265. def tree_flatten(
  266. tree: PyTree,
  267. is_leaf: Optional[Callable[[PyTree], bool]] = None,
  268. ) -> tuple[list[Any], TreeSpec]:
  269. """Flatten a pytree.
  270. See also :func:`tree_unflatten`.
  271. The flattening order (i.e., the order of elements in the output list) is deterministic,
  272. corresponding to a left-to-right depth-first tree traversal.
  273. >>> tree = {"b": (2, [3, 4]), "a": 1, "c": None, "d": 5}
  274. >>> tree_flatten(tree)
  275. ([2, 3, 4, 1, None, 5], PyTreeSpec({'b': (*, [*, *]), 'a': *, 'c': *, 'd': *}, NoneIsLeaf, namespace='torch'))
  276. >>> tree_flatten(1)
  277. ([1], PyTreeSpec(*, NoneIsLeaf, namespace='torch'))
  278. >>> tree_flatten(None)
  279. ([None], PyTreeSpec(*, NoneIsLeaf, namespace='torch'))
  280. >>> from collections import OrderedDict
  281. >>> tree = OrderedDict([("b", (2, [3, 4])), ("a", 1), ("c", None), ("d", 5)])
  282. >>> tree_flatten(tree)
  283. ([2, 3, 4, 1, None, 5], PyTreeSpec(OrderedDict({'b': (*, [*, *]), 'a': *, 'c': *, 'd': *}), NoneIsLeaf, namespace='torch'))
  284. Args:
  285. tree (pytree): A pytree to flatten.
  286. is_leaf (callable, optional): An extra leaf predicate function that will be called at each
  287. flattening step. The function should have a single argument with signature
  288. ``is_leaf(node) -> bool``. If it returns :data:`True`, the whole subtree being treated
  289. as a leaf. Otherwise, the default pytree registry will be used to determine a node is a
  290. leaf or not. If the function is not specified, the default pytree registry will be used.
  291. Returns:
  292. A pair ``(leaves, treespec)`` where the first element is a list of leaf values and the
  293. second element is a treespec representing the structure of the pytree.
  294. """
  295. return optree.tree_flatten( # type: ignore[return-value]
  296. tree,
  297. is_leaf=is_leaf,
  298. none_is_leaf=True,
  299. namespace="torch",
  300. )
  301. def tree_unflatten(leaves: Iterable[Any], treespec: TreeSpec) -> PyTree:
  302. """Reconstruct a pytree from the treespec and the leaves.
  303. The inverse of :func:`tree_flatten`.
  304. >>> tree = {"b": (2, [3, 4]), "a": 1, "c": None, "d": 5}
  305. >>> leaves, treespec = tree_flatten(tree)
  306. >>> tree == tree_unflatten(leaves, treespec)
  307. True
  308. Args:
  309. leaves (iterable): The list of leaves to use for reconstruction. The list must match the
  310. number of leaves of the treespec.
  311. treespec (TreeSpec): The treespec to reconstruct.
  312. Returns:
  313. The reconstructed pytree, containing the ``leaves`` placed in the structure described by
  314. ``treespec``.
  315. """
  316. if not _is_pytreespec_instance(treespec):
  317. raise TypeError(
  318. f"tree_unflatten(leaves, treespec): Expected `treespec` to be instance of "
  319. f"PyTreeSpec but got item of type {type(treespec)}."
  320. )
  321. return optree.tree_unflatten(treespec, leaves) # type: ignore[arg-type]
  322. def tree_iter(
  323. tree: PyTree,
  324. is_leaf: Optional[Callable[[PyTree], bool]] = None,
  325. ) -> Iterable[Any]:
  326. """Get an iterator over the leaves of a pytree.
  327. See also :func:`tree_flatten`.
  328. >>> tree = {"b": (2, [3, 4]), "a": 1, "c": None, "d": 5}
  329. >>> list(tree_iter(tree))
  330. [2, 3, 4, 1, None, 5]
  331. >>> list(tree_iter(1))
  332. [1]
  333. >>> list(tree_iter(None))
  334. [None]
  335. Args:
  336. tree (pytree): A pytree to flatten.
  337. is_leaf (callable, optional): An extra leaf predicate function that will be called at each
  338. flattening step. The function should have a single argument with signature
  339. ``is_leaf(node) -> bool``. If it returns :data:`True`, the whole subtree being treated
  340. as a leaf. Otherwise, the default pytree registry will be used to determine a node is a
  341. leaf or not. If the function is not specified, the default pytree registry will be used.
  342. Returns:
  343. An iterator over the leaf values.
  344. """
  345. return optree.tree_iter(
  346. tree,
  347. is_leaf=is_leaf,
  348. none_is_leaf=True,
  349. namespace="torch",
  350. )
  351. def tree_leaves(
  352. tree: PyTree,
  353. is_leaf: Optional[Callable[[PyTree], bool]] = None,
  354. ) -> list[Any]:
  355. """Get the leaves of a pytree.
  356. See also :func:`tree_flatten`.
  357. >>> tree = {"b": (2, [3, 4]), "a": 1, "c": None, "d": 5}
  358. >>> tree_leaves(tree)
  359. [2, 3, 4, 1, None, 5]
  360. >>> tree_leaves(1)
  361. [1]
  362. >>> tree_leaves(None)
  363. [None]
  364. Args:
  365. tree (pytree): A pytree to flatten.
  366. is_leaf (callable, optional): An extra leaf predicate function that will be called at each
  367. flattening step. The function should have a single argument with signature
  368. ``is_leaf(node) -> bool``. If it returns :data:`True`, the whole subtree being treated
  369. as a leaf. Otherwise, the default pytree registry will be used to determine a node is a
  370. leaf or not. If the function is not specified, the default pytree registry will be used.
  371. Returns:
  372. A list of leaf values.
  373. """
  374. return optree.tree_leaves(
  375. tree,
  376. is_leaf=is_leaf,
  377. none_is_leaf=True,
  378. namespace="torch",
  379. )
  380. def tree_structure(
  381. tree: PyTree,
  382. is_leaf: Optional[Callable[[PyTree], bool]] = None,
  383. ) -> TreeSpec:
  384. """Get the treespec for a pytree.
  385. See also :func:`tree_flatten`.
  386. >>> tree = {"b": (2, [3, 4]), "a": 1, "c": None, "d": 5}
  387. >>> tree_structure(tree)
  388. PyTreeSpec({'b': (*, [*, *]), 'a': *, 'c': *, 'd': *}, NoneIsLeaf, namespace='torch')
  389. >>> tree_structure(1)
  390. PyTreeSpec(*, NoneIsLeaf, namespace='torch')
  391. >>> tree_structure(None)
  392. PyTreeSpec(*, NoneIsLeaf, namespace='torch')
  393. Args:
  394. tree (pytree): A pytree to flatten.
  395. is_leaf (callable, optional): An extra leaf predicate function that will be called at each
  396. flattening step. The function should have a single argument with signature
  397. ``is_leaf(node) -> bool``. If it returns :data:`True`, the whole subtree being treated
  398. as a leaf. Otherwise, the default pytree registry will be used to determine a node is a
  399. leaf or not. If the function is not specified, the default pytree registry will be used.
  400. Returns:
  401. A treespec object representing the structure of the pytree.
  402. """
  403. return optree.tree_structure( # type: ignore[return-value]
  404. tree,
  405. is_leaf=is_leaf,
  406. none_is_leaf=True,
  407. namespace="torch",
  408. )
  409. def tree_map(
  410. func: Callable[..., Any],
  411. tree: PyTree,
  412. *rests: PyTree,
  413. is_leaf: Optional[Callable[[PyTree], bool]] = None,
  414. ) -> PyTree:
  415. """Map a multi-input function over pytree args to produce a new pytree.
  416. See also :func:`tree_map_`.
  417. >>> tree_map(lambda x: x + 1, {"x": 7, "y": (42, 64)})
  418. {'x': 8, 'y': (43, 65)}
  419. >>> tree_map(lambda x: x is None, {"x": 7, "y": (42, 64), "z": None})
  420. {'x': False, 'y': (False, False), 'z': True}
  421. If multiple inputs are given, the structure of the tree is taken from the first input;
  422. subsequent inputs need only have ``tree`` as a prefix:
  423. >>> tree_map(lambda x, y: [x] + y, [5, 6], [[7, 9], [1, 2]])
  424. [[5, 7, 9], [6, 1, 2]]
  425. Args:
  426. func (callable): A function that takes ``1 + len(rests)`` arguments, to be applied at the
  427. corresponding leaves of the pytrees.
  428. tree (pytree): A pytree to be mapped over, with each leaf providing the first positional
  429. argument to function ``func``.
  430. rests (tuple of pytree): A tuple of pytrees, each of which has the same structure as
  431. ``tree`` or has ``tree`` as a prefix.
  432. is_leaf (callable, optional): An extra leaf predicate function that will be called at each
  433. flattening step. The function should have a single argument with signature
  434. ``is_leaf(node) -> bool``. If it returns :data:`True`, the whole subtree being treated
  435. as a leaf. Otherwise, the default pytree registry will be used to determine a node is a
  436. leaf or not. If the function is not specified, the default pytree registry will be used.
  437. Returns:
  438. A new pytree with the same structure as ``tree`` but with the value at each leaf given by
  439. ``func(x, *xs)`` where ``x`` is the value at the corresponding leaf in ``tree`` and ``xs``
  440. is the tuple of values at corresponding nodes in ``rests``.
  441. """
  442. return optree.tree_map(
  443. func,
  444. tree,
  445. *rests,
  446. is_leaf=is_leaf,
  447. none_is_leaf=True,
  448. namespace="torch",
  449. )
  450. def tree_map_(
  451. func: Callable[..., Any],
  452. tree: PyTree,
  453. *rests: PyTree,
  454. is_leaf: Optional[Callable[[PyTree], bool]] = None,
  455. ) -> PyTree:
  456. """Like :func:`tree_map`, but do an inplace call on each leaf and return the original tree.
  457. See also :func:`tree_map`.
  458. Args:
  459. func (callable): A function that takes ``1 + len(rests)`` arguments, to be applied at the
  460. corresponding leaves of the pytrees.
  461. tree (pytree): A pytree to be mapped over, with each leaf providing the first positional
  462. argument to function ``func``.
  463. rests (tuple of pytree): A tuple of pytrees, each of which has the same structure as
  464. ``tree`` or has ``tree`` as a prefix.
  465. is_leaf (callable, optional): An extra leaf predicate function that will be called at each
  466. flattening step. The function should have a single argument with signature
  467. ``is_leaf(node) -> bool``. If it returns :data:`True`, the whole subtree being treated
  468. as a leaf. Otherwise, the default pytree registry will be used to determine a node is a
  469. leaf or not. If the function is not specified, the default pytree registry will be used.
  470. Returns:
  471. The original ``tree`` with the value at each leaf is given by the side-effect of function
  472. ``func(x, *xs)`` (not the return value) where ``x`` is the value at the corresponding leaf
  473. in ``tree`` and ``xs`` is the tuple of values at values at corresponding nodes in ``rests``.
  474. """
  475. return optree.tree_map_(
  476. func,
  477. tree,
  478. *rests,
  479. is_leaf=is_leaf,
  480. none_is_leaf=True,
  481. namespace="torch",
  482. )
  483. Type2 = tuple[type[T], type[S]]
  484. Type3 = tuple[type[T], type[S], type[U]]
  485. if sys.version_info >= (3, 10):
  486. TypeAny = Union[type[Any], tuple[type[Any], ...], types.UnionType]
  487. else:
  488. TypeAny = Union[type[Any], tuple[type[Any], ...]]
  489. Fn2 = Callable[[Union[T, S]], R]
  490. Fn3 = Callable[[Union[T, S, U]], R]
  491. Fn = Callable[[T], R]
  492. FnAny = Callable[[Any], R]
  493. MapOnlyFn = Callable[[T], Callable[[Any], Any]]
  494. # These specializations help with type inference on the lambda passed to this
  495. # function
  496. @overload
  497. def map_only(type_or_types_or_pred: type[T], /) -> MapOnlyFn[Fn[T, Any]]: ...
  498. @overload
  499. def map_only(type_or_types_or_pred: Type2[T, S], /) -> MapOnlyFn[Fn2[T, S, Any]]: ...
  500. @overload
  501. def map_only(
  502. type_or_types_or_pred: Type3[T, S, U], /
  503. ) -> MapOnlyFn[Fn3[T, S, U, Any]]: ...
  504. # This specialization is needed for the implementations below that call
  505. @overload
  506. def map_only(type_or_types_or_pred: TypeAny, /) -> MapOnlyFn[FnAny[Any]]: ...
  507. @overload
  508. def map_only(
  509. type_or_types_or_pred: Callable[[Any], bool], /
  510. ) -> MapOnlyFn[FnAny[Any]]: ...
  511. def map_only(
  512. type_or_types_or_pred: Union[TypeAny, Callable[[Any], bool]], /
  513. ) -> MapOnlyFn[FnAny[Any]]:
  514. """
  515. Suppose you are writing a tree_map over tensors, leaving everything
  516. else unchanged. Ordinarily you would have to write:
  517. def go(t):
  518. if isinstance(t, Tensor):
  519. return ...
  520. else:
  521. return t
  522. With this function, you only need to write:
  523. @map_only(Tensor)
  524. def go(t):
  525. return ...
  526. You can also directly use 'tree_map_only'
  527. """
  528. if isinstance(type_or_types_or_pred, (type, tuple)) or (
  529. sys.version_info >= (3, 10)
  530. and isinstance(type_or_types_or_pred, types.UnionType)
  531. ):
  532. def pred(x: Any) -> bool:
  533. return isinstance(x, type_or_types_or_pred) # type: ignore[arg-type]
  534. elif callable(type_or_types_or_pred):
  535. pred = type_or_types_or_pred # type: ignore[assignment]
  536. else:
  537. raise TypeError("Argument must be a type, a tuple of types, or a callable.")
  538. def wrapper(func: Callable[[T], Any]) -> Callable[[Any], Any]:
  539. @functools.wraps(func)
  540. def wrapped(x: T) -> Any:
  541. if pred(x):
  542. return func(x)
  543. return x
  544. return wrapped
  545. return wrapper
  546. @overload
  547. def tree_map_only(
  548. type_or_types_or_pred: type[T],
  549. /,
  550. func: Fn[T, Any],
  551. tree: PyTree,
  552. is_leaf: Optional[Callable[[PyTree], bool]] = None,
  553. ) -> PyTree: ...
  554. @overload
  555. def tree_map_only(
  556. type_or_types_or_pred: Type2[T, S],
  557. /,
  558. func: Fn2[T, S, Any],
  559. tree: PyTree,
  560. is_leaf: Optional[Callable[[PyTree], bool]] = None,
  561. ) -> PyTree: ...
  562. @overload
  563. def tree_map_only(
  564. type_or_types_or_pred: Type3[T, S, U],
  565. /,
  566. func: Fn3[T, S, U, Any],
  567. tree: PyTree,
  568. is_leaf: Optional[Callable[[PyTree], bool]] = None,
  569. ) -> PyTree: ...
  570. @overload
  571. def tree_map_only(
  572. type_or_types_or_pred: TypeAny,
  573. /,
  574. func: FnAny[Any],
  575. tree: PyTree,
  576. is_leaf: Optional[Callable[[PyTree], bool]] = None,
  577. ) -> PyTree: ...
  578. @overload
  579. def tree_map_only(
  580. type_or_types_or_pred: Callable[[Any], bool],
  581. /,
  582. func: FnAny[Any],
  583. tree: PyTree,
  584. is_leaf: Optional[Callable[[PyTree], bool]] = None,
  585. ) -> PyTree: ...
  586. def tree_map_only(
  587. type_or_types_or_pred: Union[TypeAny, Callable[[Any], bool]],
  588. /,
  589. func: FnAny[Any],
  590. tree: PyTree,
  591. is_leaf: Optional[Callable[[PyTree], bool]] = None,
  592. ) -> PyTree:
  593. return tree_map(map_only(type_or_types_or_pred)(func), tree, is_leaf=is_leaf)
  594. @overload
  595. def tree_map_only_(
  596. type_or_types_or_pred: type[T],
  597. /,
  598. func: Fn[T, Any],
  599. tree: PyTree,
  600. is_leaf: Optional[Callable[[PyTree], bool]] = None,
  601. ) -> PyTree: ...
  602. @overload
  603. def tree_map_only_(
  604. type_or_types_or_pred: Type2[T, S],
  605. /,
  606. func: Fn2[T, S, Any],
  607. tree: PyTree,
  608. is_leaf: Optional[Callable[[PyTree], bool]] = None,
  609. ) -> PyTree: ...
  610. @overload
  611. def tree_map_only_(
  612. type_or_types_or_pred: Type3[T, S, U],
  613. /,
  614. func: Fn3[T, S, U, Any],
  615. tree: PyTree,
  616. is_leaf: Optional[Callable[[PyTree], bool]] = None,
  617. ) -> PyTree: ...
  618. @overload
  619. def tree_map_only_(
  620. type_or_types_or_pred: TypeAny,
  621. /,
  622. func: FnAny[Any],
  623. tree: PyTree,
  624. is_leaf: Optional[Callable[[PyTree], bool]] = None,
  625. ) -> PyTree: ...
  626. @overload
  627. def tree_map_only_(
  628. type_or_types_or_pred: Callable[[Any], bool],
  629. /,
  630. func: FnAny[Any],
  631. tree: PyTree,
  632. is_leaf: Optional[Callable[[PyTree], bool]] = None,
  633. ) -> PyTree: ...
  634. def tree_map_only_(
  635. type_or_types_or_pred: Union[TypeAny, Callable[[Any], bool]],
  636. /,
  637. func: FnAny[Any],
  638. tree: PyTree,
  639. is_leaf: Optional[Callable[[PyTree], bool]] = None,
  640. ) -> PyTree:
  641. return tree_map_(map_only(type_or_types_or_pred)(func), tree, is_leaf=is_leaf)
  642. def tree_all(
  643. pred: Callable[[Any], bool],
  644. tree: PyTree,
  645. is_leaf: Optional[Callable[[PyTree], bool]] = None,
  646. ) -> bool:
  647. flat_args = tree_iter(tree, is_leaf=is_leaf)
  648. return all(map(pred, flat_args))
  649. def tree_any(
  650. pred: Callable[[Any], bool],
  651. tree: PyTree,
  652. is_leaf: Optional[Callable[[PyTree], bool]] = None,
  653. ) -> bool:
  654. flat_args = tree_iter(tree, is_leaf=is_leaf)
  655. return any(map(pred, flat_args))
  656. @overload
  657. def tree_all_only(
  658. type_or_types: type[T],
  659. /,
  660. pred: Fn[T, bool],
  661. tree: PyTree,
  662. is_leaf: Optional[Callable[[PyTree], bool]] = None,
  663. ) -> bool: ...
  664. @overload
  665. def tree_all_only(
  666. type_or_types: Type2[T, S],
  667. /,
  668. pred: Fn2[T, S, bool],
  669. tree: PyTree,
  670. is_leaf: Optional[Callable[[PyTree], bool]] = None,
  671. ) -> bool: ...
  672. @overload
  673. def tree_all_only(
  674. type_or_types: Type3[T, S, U],
  675. /,
  676. pred: Fn3[T, S, U, bool],
  677. tree: PyTree,
  678. is_leaf: Optional[Callable[[PyTree], bool]] = None,
  679. ) -> bool: ...
  680. def tree_all_only(
  681. type_or_types: TypeAny,
  682. /,
  683. pred: FnAny[bool],
  684. tree: PyTree,
  685. is_leaf: Optional[Callable[[PyTree], bool]] = None,
  686. ) -> bool:
  687. flat_args = tree_iter(tree, is_leaf=is_leaf)
  688. return all(pred(x) for x in flat_args if isinstance(x, type_or_types))
  689. @overload
  690. def tree_any_only(
  691. type_or_types: type[T],
  692. /,
  693. pred: Fn[T, bool],
  694. tree: PyTree,
  695. is_leaf: Optional[Callable[[PyTree], bool]] = None,
  696. ) -> bool: ...
  697. @overload
  698. def tree_any_only(
  699. type_or_types: Type2[T, S],
  700. /,
  701. pred: Fn2[T, S, bool],
  702. tree: PyTree,
  703. is_leaf: Optional[Callable[[PyTree], bool]] = None,
  704. ) -> bool: ...
  705. @overload
  706. def tree_any_only(
  707. type_or_types: Type3[T, S, U],
  708. /,
  709. pred: Fn3[T, S, U, bool],
  710. tree: PyTree,
  711. is_leaf: Optional[Callable[[PyTree], bool]] = None,
  712. ) -> bool: ...
  713. def tree_any_only(
  714. type_or_types: TypeAny,
  715. /,
  716. pred: FnAny[bool],
  717. tree: PyTree,
  718. is_leaf: Optional[Callable[[PyTree], bool]] = None,
  719. ) -> bool:
  720. flat_args = tree_iter(tree, is_leaf=is_leaf)
  721. return any(pred(x) for x in flat_args if isinstance(x, type_or_types))
  722. def broadcast_prefix(
  723. prefix_tree: PyTree,
  724. full_tree: PyTree,
  725. is_leaf: Optional[Callable[[PyTree], bool]] = None,
  726. ) -> list[Any]:
  727. """Return a list of broadcasted leaves in ``prefix_tree`` to match the number of leaves in ``full_tree``.
  728. If a ``prefix_tree`` is a prefix of a ``full_tree``, this means the ``full_tree`` can be
  729. constructed by replacing the leaves of ``prefix_tree`` with appropriate **subtrees**.
  730. This function returns a list of leaves with the same size as ``full_tree``. The leaves are
  731. replicated from ``prefix_tree``. The number of replicas is determined by the corresponding
  732. subtree in ``full_tree``.
  733. >>> broadcast_prefix(1, [1, 2, 3])
  734. [1, 1, 1]
  735. >>> broadcast_prefix([1, 2, 3], [1, 2, 3])
  736. [1, 2, 3]
  737. >>> broadcast_prefix([1, 2, 3], [1, 2, 3, 4])
  738. Traceback (most recent call last):
  739. ...
  740. ValueError: list arity mismatch; expected: 3, got: 4; list: [1, 2, 3, 4].
  741. >>> broadcast_prefix([1, 2, 3], [1, 2, (3, 4)])
  742. [1, 2, 3, 3]
  743. >>> broadcast_prefix([1, 2, 3], [1, 2, {"a": 3, "b": 4, "c": (None, 5)}])
  744. [1, 2, 3, 3, 3, 3]
  745. Args:
  746. prefix_tree (pytree): A pytree with the same structure as a prefix of ``full_tree``.
  747. full_tree (pytree): A pytree with the same structure as a suffix of ``prefix_tree``.
  748. is_leaf (callable, optional): An extra leaf predicate function that will be called at each
  749. flattening step. The function should have a single argument with signature
  750. ``is_leaf(node) -> bool``. If it returns :data:`True`, the whole subtree being treated
  751. as a leaf. Otherwise, the default pytree registry will be used to determine a node is a
  752. leaf or not. If the function is not specified, the default pytree registry will be used.
  753. Returns:
  754. A list of leaves in ``prefix_tree`` broadcasted to match the number of leaves in ``full_tree``.
  755. """
  756. result: list[Any] = []
  757. def add_leaves(x: Any, subtree: PyTree) -> None:
  758. subtreespec = tree_structure(subtree, is_leaf=is_leaf)
  759. result.extend([x] * subtreespec.num_leaves)
  760. tree_map_(
  761. add_leaves,
  762. prefix_tree,
  763. full_tree,
  764. is_leaf=is_leaf,
  765. )
  766. return result
  767. # Broadcasts a pytree to the provided TreeSpec and returns the flattened
  768. # values. If this is not possible, then this function returns None.
  769. #
  770. # For example, given pytree=0 and spec=TreeSpec(list, None, [LeafSpec(), LeafSpec()]),
  771. # would return [0, 0]. This is useful for part of the vmap implementation:
  772. # a user can pass in vmap(fn, in_dims)(*inputs). `in_dims` should be
  773. # broadcastable to the tree structure of `inputs` and we use
  774. # _broadcast_to_and_flatten to check this.
  775. def _broadcast_to_and_flatten(
  776. tree: PyTree,
  777. treespec: TreeSpec,
  778. is_leaf: Optional[Callable[[PyTree], bool]] = None,
  779. ) -> Optional[list[Any]]:
  780. assert _is_pytreespec_instance(treespec)
  781. full_tree = tree_unflatten([0] * treespec.num_leaves, treespec)
  782. try:
  783. return broadcast_prefix(tree, full_tree, is_leaf=is_leaf)
  784. except ValueError:
  785. return None
  786. def treespec_dumps(treespec: TreeSpec, protocol: Optional[int] = None) -> str:
  787. """Serialize a treespec to a JSON string."""
  788. if not _is_pytreespec_instance(treespec):
  789. raise TypeError(
  790. f"treespec_dumps(treespec): Expected `treespec` to be instance of "
  791. f"PyTreeSpec but got item of type {type(treespec)}."
  792. )
  793. dummy_tree = tree_unflatten([0] * treespec.num_leaves, treespec)
  794. orig_treespec = python_pytree.tree_structure(dummy_tree)
  795. return python_pytree.treespec_dumps(orig_treespec, protocol=protocol)
  796. @functools.lru_cache
  797. def treespec_loads(serialized: str) -> TreeSpec:
  798. """Deserialize a treespec from a JSON string."""
  799. orig_treespec = python_pytree.treespec_loads(serialized)
  800. dummy_tree = python_pytree.tree_unflatten(
  801. [0] * orig_treespec.num_leaves,
  802. orig_treespec,
  803. )
  804. treespec = tree_structure(dummy_tree)
  805. return treespec
  806. class _DummyLeaf:
  807. def __repr__(self) -> str:
  808. return "*"
  809. def treespec_pprint(treespec: TreeSpec) -> str:
  810. dummy_tree = tree_unflatten(
  811. [_DummyLeaf() for _ in range(treespec.num_leaves)],
  812. treespec,
  813. )
  814. return repr(dummy_tree)
  815. class LeafSpecMeta(type(TreeSpec)): # type: ignore[misc]
  816. def __instancecheck__(self, instance: object) -> bool:
  817. return _is_pytreespec_instance(instance) and instance.is_leaf()
  818. class LeafSpec(TreeSpec, metaclass=LeafSpecMeta): # type: ignore[misc,final]
  819. def __new__(cls) -> "LeafSpec":
  820. return optree.treespec_leaf(none_is_leaf=True) # type: ignore[return-value]
  821. def tree_flatten_with_path(
  822. tree: PyTree,
  823. is_leaf: Optional[Callable[[PyTree], bool]] = None,
  824. ) -> tuple[list[tuple[KeyPath, Any]], TreeSpec]:
  825. """Flattens a pytree like :func:`tree_flatten`, but also returns each leaf's key path.
  826. Args:
  827. tree: a pytree to flatten. If it contains a custom type, that type must be
  828. registered with an appropriate `tree_flatten_with_path_fn` when registered
  829. with :func:`register_pytree_node`.
  830. is_leaf: An extra leaf predicate function that will be called at each
  831. flattening step. The function should have a single argument with signature
  832. ``is_leaf(node) -> bool``. If it returns :data:`True`, the whole subtree being treated
  833. as a leaf. Otherwise, the default pytree registry will be used to determine a node is a
  834. leaf or not. If the function is not specified, the default pytree registry will be used.
  835. Returns:
  836. A tuple where the first element is a list of (key path, leaf) pairs, and the
  837. second element is a :class:`TreeSpec` representing the structure of the flattened
  838. tree.
  839. """
  840. raise NotImplementedError("KeyPaths are not yet supported in cxx_pytree.")
  841. def tree_leaves_with_path(
  842. tree: PyTree,
  843. is_leaf: Optional[Callable[[PyTree], bool]] = None,
  844. ) -> list[tuple[KeyPath, Any]]:
  845. """Gets the leaves of a pytree like ``tree_leaves`` and returns each leaf's key path.
  846. Args:
  847. tree: a pytree. If it contains a custom type, that type must be
  848. registered with an appropriate `tree_flatten_with_path_fn` when registered
  849. with :func:`register_pytree_node`.
  850. is_leaf: An extra leaf predicate function that will be called at each
  851. flattening step. The function should have a single argument with signature
  852. ``is_leaf(node) -> bool``. If it returns :data:`True`, the whole subtree being treated
  853. as a leaf. Otherwise, the default pytree registry will be used to determine a node is a
  854. leaf or not. If the function is not specified, the default pytree registry will be used.
  855. Returns:
  856. A list of (key path, leaf) pairs.
  857. """
  858. raise NotImplementedError("KeyPaths are not yet supported in cxx_pytree.")
  859. def tree_map_with_path(
  860. func: Callable[..., Any],
  861. tree: PyTree,
  862. *rests: PyTree,
  863. is_leaf: Optional[Callable[[PyTree], bool]] = None,
  864. ) -> PyTree:
  865. """Like :func:`tree_map`, but the provided callable takes an additional key path argument.
  866. Args:
  867. func: A function that takes ``2 + len(rests)`` arguments, to be applied at the
  868. corresponding leaves of the pytrees. The first positional argument
  869. to ``func`` is the key path of the leaf in question. The second
  870. positional argument is the value of the leaf.
  871. tree: A pytree to be mapped over, with each leaf providing the first positional
  872. argument to function ``func``.
  873. rests: A tuple of pytrees, each of which has the same structure as
  874. ``tree`` or has ``tree`` as a prefix.
  875. is_leaf: An extra leaf predicate function that will be called at each
  876. flattening step. The function should have a single argument with signature
  877. ``is_leaf(node) -> bool``. If it returns :data:`True`, the whole subtree being treated
  878. as a leaf. Otherwise, the default pytree registry will be used to determine a node is a
  879. leaf or not. If the function is not specified, the default pytree registry will be used.
  880. Returns
  881. A new pytree with the same structure as ``tree`` but with the value at each leaf given by
  882. ``func(keypath, x, *xs)`` where ``keypath`` is the key path at the
  883. corresponding leaf in ``tree``, ``x`` is the value at that leaf, and
  884. ``xs`` is the tuple of values at corresponding nodes in ``rests``.
  885. """
  886. raise NotImplementedError("KeyPaths are not yet supported in cxx_pytree.")
  887. def keystr(kp: KeyPath) -> str:
  888. """Given a key path, return a pretty-printed representation."""
  889. raise NotImplementedError("KeyPaths are not yet supported in cxx_pytree.")
  890. def key_get(obj: Any, kp: KeyPath) -> Any:
  891. """Given an object and a key path, return the value at the key path."""
  892. raise NotImplementedError("KeyPaths are not yet supported in cxx_pytree.")
  893. with python_pytree._NODE_REGISTRY_LOCK:
  894. python_pytree._cxx_pytree_imported = True
  895. args, kwargs = (), {} # type: ignore[var-annotated]
  896. for args, kwargs in python_pytree._cxx_pytree_pending_imports:
  897. _private_register_pytree_node(*args, **kwargs)
  898. python_pytree._cxx_pytree_pending_imports.clear()
  899. del args, kwargs