archimedes.tree.register_struct¶
- archimedes.tree.register_struct(
- ty: Any,
- to_iter: Callable,
- from_iter: Callable,
Register a custom type as a tree-compatible node.
This function allows custom types to be recognized and processed by the tree functions. You provide functions that convert between your type and its components.
- Parameters:
ty (type) – The type to register as a tree node.
to_iter (callable) –
A function that accepts an instance of type
tyand returns a tuple of(children, aux_data), where:childrenis an iterable of the tree node’s childrenaux_datais any auxiliary metadata needed to reconstruct the node but not part of the tree structure itself
from_iter (callable) – A function that accepts
aux_dataand an iterable of children and returns a reconstructed instance of typety.
- Return type:
None
Notes
The
to_iterfunction should extract the relevant parts of your data structure, and thefrom_iterfunction should be able to reconstruct it exactly.Usually, instead of using this function directly, you’ll want to use the
@structdecorator for classes, which automatically handles registration for dataclass-like structures. This function is used internally to register the decorated classes. It is also available as an alternative interface for low-level control of flattening/unflattening behavior and static data for custom classes.The
(children, aux_data)convention is intentionally identical to JAX’s pytree node registration, so flattened structree data lines up element for element with the JAX/Flax pytree contract.See also
structDecorator for creating tree-compatible dataclasses
register_dataclassRegister a dataclass as a struct