brainstate.graph moduel#
Most of these APIs are adapted from Flax (google/flax).
It enables the structure-preserving State retrieval and manipulatio in the brainstate.
A clean-room, flat-IR engine for flattening object graphs of Nodes and
States into a static structure plus a dynamic state mapping, and back.
The engine is layered in three tiers:
Flat IR (
_graphdef) — aGraphDefis a rootEdgeplus a flat table ofNodeSpecs. Graph nodes are hoisted into one index-keyed table (so sharing and cycles are encoded by integer index), while pytree containers are embedded inline. The whole structure hashes in a single cached pass and is registered as a JAX static pytree.Traversal kernel (
_walk) — one depth-first primitive backsiter_leaf()/iter_node()(andstates/nodes), plus the node-type registry behindregister_graph_node_type().Encode / decode (
_flatten) —flatten()encodes in one pre-order pass;unflatten()decodes in three linear passes (materialize states, create node shells, fill), so reconstruction over graph nodes never recurses and cycles/sharing resolve regardless of fill order.
Operations (treefy_split() / treefy_merge() / states() /
nodes() / …) and graph/pytree conversion (graph_to_tree() /
tree_to_graph()) are thin layers on top.
Graph Node#
Base class for all mutable graph nodes in the BrainState framework. |
Graph Operation#
Remove and return ``State``s matching the filters (deduped by identity). |
|
Collect graph nodes as ``FlattedDict``(s), optionally filtered. |
|
Collect |
|
Return the treefy state mapping of |
|
Update |
|
Flatten a graph |
|
Reconstruct a graph node from a |
|
Split a graph node into a |
|
Reconstruct a node from a |
|
Iterate |
|
Iterate |
|
Deep-copy |
|
Return the |
Context Management#
Context managers for handling complex state updates during graph transformations. These utilities enable splitting and merging graph states in a thread-safe manner.
Context manager for splitting multiple graph nodes sharing a reference index. |
|
Context manager for merging multiple graph nodes sharing a reference index. |
Graph Conversion#
Utilities for converting between graph and tree representations, enabling flexible manipulation of nested module structures.
Convert a pytree that may contain graph nodes into a pure pytree. |
|
Convert a pytree of |
|
A JAX pytree wrapper that carries both a GraphDef and one or more state mappings. |
Graph Definition Classes#
Core classes for representing graph structure, node definitions, and references. These classes provide the foundation for graph operations and state management.
The static structure of an object graph. |
|
The static record for a single graph node (never a pytree). |
|
A reference to a graph node by its global index. |
|
A deduplicated |
|
A bare |
|
An inline pytree container. |
|
An inline, hashable static value carried directly in the structure. |
|
An empty pytree node that treats its inner value as static. |
|
A mutable mapping that keys entries by object identity ( |
|
Register a custom mutable graph-node type with the graph engine. |