IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /get-started.md). For the complete documentation index, see llms.txt.
Skip to main content
For the complete documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /get-started.md).

Python class

TreeDef

TreeDef

class max.tree.TreeDef(kind, children=(), keys=(), meta=None)

source

Bases: object

The shape of a tree, with its leaves abstracted away.

Equal structures rebuild each other’s leaves. Hashing skips the payloads.

Parameters:

child_keys

property child_keys: tuple[Any, ...]

source

keys, or positions if empty.

Type:

The key of each child

children

children: tuple[TreeDef, ...] = ()

source

The structure of each child, in leaf order.

flatten_up_to()

flatten_up_to(tree, path='')

source

Flattens tree using this structure to locate the leaves.

The walk descends into tree wherever this structure has a container and stops wherever it has a leaf, returning whatever tree holds at that position. Use this to flatten a second tree the same way as a first one, without a leaf predicate that might stop at different positions.

The following example flattens a tree whose leaves are containers:

from max.experimental import tree_utils as tree

_, treedef = tree.flatten({"a": 1, "b": [2, 3]}, leaf=int)
other = {"a": [0], "b": [[], {}]}
assert treedef.flatten_up_to(other) == [[0], [], {}]

Parameters:

  • tree (Any) – The value to flatten. It must have this structure, with equal values at the positions of static values.
  • path (str) – The path of tree, used in error messages.

Returns:

The value at each leaf position, in order.

Raises:

ValueError – If tree does not have this structure.

Return type:

list[Any]

keys

keys: tuple[Any, ...] = ()

source

One key per child, or empty when the children are positional.

kind

kind: str

source

"leaf" (a leaf slot), "static" (a value carried as-is in meta), "ref" (a repeat of an earlier value), or one of the container tags "list", "tuple", "namedtuple", "dict" and "node".

Type:

What this position holds

leaf_paths

property leaf_paths: tuple[str, ...]

source

The dotted path to each leaf, in leaf order.

meta

meta: Any = None

source

the value itself for "static", the referenced slot number for "ref", the concrete type for "namedtuple", and a (type, metadata) pair for "node".

Type:

Whatever the kind needs to rebuild

num_leaves

property num_leaves: int

source

The number of leaf slots in this structure; a "ref" counts none.