jax.tree_util packageÂ¶
Utilities for working with treelike container data structures.
This module provides a small set of utility functions for working with treelike data structures, such as nested tuples, lists, and dicts. We call these structures pytrees. They are trees in that they are defined recursively (any nonpytree is a pytree, i.e. a leaf, and any pytree of pytrees is a pytree) and can be operated on recursively (object identity equivalence is not preserved by mapping operations, and the structures cannot contain reference cycles).
The set of Python types that are considered pytree nodes (e.g. that can be mapped over, rather than treated as leaves) is extensible. There is a single modulelevel registry of types, and class hierarchy is ignored. By registering a new pytree node type, that type in effect becomes transparent to the utility functions in this file.
The primary purpose of this module is to enable the interoperability between user defined data structures and JAX transformations (e.g. jit). This is not meant to be a general purpose treelike data structure handling library.
See the JAX pytrees notebook for examples.

class
jax.tree_util.
Partial
[source]Â¶ A version of functools.partial that works in pytrees.
Use it for partial function evaluation in a way that is compatible with JAXâ€™s transformations, e.g.,
Partial(func, *args, **kwargs)
.(You need to explicitly optin to this behavior because we didnâ€™t want to give functools.partial different semantics than normal function closures.)

jax.tree_util.
register_pytree_node
(nodetype, flatten_func, unflatten_func)[source]Â¶ Extends the set of types that are considered internal nodes in pytrees.
See example usage.
Parameters:  nodetype â€“ a Python type to treat as an internal pytree node.
 flatten_func â€“ a function to be used during flattening, taking a value of type nodetype and returning a pair, with (1) an iterable for the children to be flattened recursively, and (2) some auxiliary data to be stored in the treedef and to be passed to the unflatten_func.
 unflatten_func â€“ a function taking two arguments: the auxiliary data that was returned by flatten_func and stored in the treedef, and the unflattened children. The function should return an instance of nodetype.

jax.tree_util.
register_pytree_node_class
(cls)[source]Â¶ Extends the set of types that are considered internal nodes in pytrees.
This function is a thin wrapper around
register_pytree_node
, and provides a classoriented interface:@register_pytree_node_class class Special:
 def __init__(self, x, y):
 self.x = x self.y = y
 def tree_flatten(self):
 return ((self.x, self.y), None)
@classmethod def tree_unflatten(cls, aux_data, children):
return cls(*children)

jax.tree_util.
tree_flatten
(tree)[source]Â¶ Flattens a pytree.
Parameters: tree â€“ a pytree to flatten. Returns: a pair with a list of leaves and the corresponding treedef.

jax.tree_util.
tree_map
(f, tree)[source]Â¶ Maps a function over a pytree to produce a new pytree.
Parameters:  f â€“ function to be applied at each leaf.
 tree â€“ a pytree to be mapped over.
Returns: A new pytree with the same structure as tree but with the value at each leaf given by f(x) where x is the value at the corresponding leaf in tree.

jax.tree_util.
tree_multimap
(f, tree, *rest)[source]Â¶ Maps a multiinput function over pytree args to produce a new pytree.
Parameters:  f â€“ function that takes 1 + len(rest) arguments, to be applied at the corresponding leaves of the pytrees.
 tree â€“ a pytree to be mapped over, with each leaf providing the first positional argument to f.
 *rest â€“ a tuple of pytrees, each of which has the same structure as tree or or has tree as a prefix.
Returns: A new pytree with the same structure as tree but with the value at each leaf given by f(x, *xs) where x is the value at the corresponding leaf in tree and xs is the tuple of values at corresponding nodes in rest.

jax.tree_util.
tree_unflatten
(treedef, leaves)[source]Â¶ Reconstructs a pytree from the treedef and the leaves.
The inverse of tree_flatten.
Parameters:  treedef â€“ the treedef to reconstruct
 leaves â€“ the list of leaves to use for reconstruction. The list must match the leaves of the treedef.
Returns: The reconstructed pytree, containing the leaves placed in the structure described by treedef.