PyTree Traversal
About
The d9d.core.pytree package provides the framework's utilities for recursively traversing nested tensor structures ("pytrees"). It is a thin, dataclass-aware wrapper around optree and is used wherever the engine needs to apply an operation to every tensor in a nested structure - moving a microbatch to the device, detaching cached side-data, moving metric results to the CPU, or flattening a metric tree for logging.
It operates over the container types described by PyTree - dict, list, tuple - nested arbitrarily deep, and additionally over any dataclass.
Dataclasses Work Transparently
A caller can pass a dataclass - arbitrarily nested inside containers or other dataclasses - to any function in this package, and its fields are traversed as tree children.
Nesting composes in every direction - a dataclass inside a dict, a list of dataclasses, or a dataclass whose fields are dicts of tensors are all traversed correctly.
Determinism
Traversal order is deterministic:
dictkeys are traversed in sorted order, regardless of insertion order.- dataclass fields are traversed in declaration order.
API Reference
d9d.core.pytree
Recursive traversal of nested tensor structures ("pytrees").
tree_flatten(tree, is_leaf=None)
Flattens a pytree into its leaves and a structure specification.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tree
|
PyTree[TLeaf]
|
The nested structure to flatten. |
required |
is_leaf
|
IsLeaf | None
|
Optional predicate; when it returns |
None
|
Returns:
| Type | Description |
|---|---|
list[TLeaf]
|
A tuple of the leaf list and a |
PyTreeSpec
|
|
tree_leaves(tree, is_leaf=None)
Returns the leaves of a pytree in deterministic (sorted-key) order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tree
|
PyTree[TLeaf]
|
The nested structure to flatten. |
required |
is_leaf
|
IsLeaf | None
|
Optional predicate; when it returns |
None
|
Returns:
| Type | Description |
|---|---|
list[TLeaf]
|
The list of leaves. |
tree_leaves_with_path(tree, is_leaf=None)
Returns (path, leaf) pairs for every leaf of a pytree.
Each path is a tuple of keys and indices reaching the leaf from the root: str for dict keys
and dataclass field names, int for sequence indices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tree
|
PyTree[TLeaf]
|
The nested structure to flatten. |
required |
is_leaf
|
IsLeaf | None
|
Optional predicate; when it returns |
None
|
Returns:
| Type | Description |
|---|---|
list[tuple[tuple[Any, ...], TLeaf]]
|
A list of |
tree_map(func, tree)
Applies func to every leaf of a pytree, returning a structurally-identical tree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[[TLeaf], TMapped]
|
The function to apply to each leaf. |
required |
tree
|
PyTree[TLeaf]
|
The nested structure to map over. |
required |
Returns:
| Type | Description |
|---|---|
PyTree[TMapped]
|
A new tree with |
tree_map_only(filters, func, tree)
Applies func only to leaves that are instances of type_or_types.
Leaves of any other type are returned unchanged. This is the common case for tensor operations over trees that also carry non-tensor bookkeeping (e.g. moving only tensors to a device while leaving strings and ints alone). The returned tree preserves the structure and leaf types of the input.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filters
|
type | tuple[type, ...]
|
The leaf type(s) that |
required |
func
|
Callable[[Any], Any]
|
The function to apply to matching leaves. |
required |
tree
|
TTree
|
The nested structure to map over. |
required |
Returns:
| Type | Description |
|---|---|
TTree
|
A new tree with |
tree_unflatten(treespec, leaves)
Reconstructs a pytree from leaves and a structure specification.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
treespec
|
PyTreeSpec
|
A specification produced by |
required |
leaves
|
list[TLeaf]
|
The leaves to place into the structure, in flatten order. |
required |
Returns:
| Type | Description |
|---|---|
PyTree[TLeaf]
|
The reconstructed nested structure. |