brainstate module#
Core State Classes#
State classes are the fundamental building blocks for managing dynamic data in BrainState. They provide a unified interface for tracking, tracing, and transforming stateful computations.
Basic State Types#
Basic state types provide semantic distinctions for different data lifecycles in your program.
A generic class representing a dynamic data pointer in the BrainState framework. |
|
A class representing short-term state in a program. |
|
The long-term state, which is used to store the long-term data in the program. |
|
The parameter state, which is used to store the trainable parameters in the model. |
|
The batch state, which is used to store the batch data in the program. |
|
Short-term state for storing delay data. |
Special State Types#
Special-purpose state types for advanced use cases and PyTree integration.
The faked state, which is used to store the faked data in the program. |
|
The state as a pytree. |
State Management#
Tools for managing collections of states and tracking state access patterns during program execution.
State Collections#
Organize and manipulate multiple states as cohesive units.
State stack, for collecting all |
State Tracing#
Track state read/write operations for automatic differentiation and program transformation.
A stack for tracing and managing states during program execution. |
State Utilities#
Helper functions and context managers for working with states effectively.
Context Managers#
Control state behavior within specific code blocks.
The contex manager to check weather the tree structure of the state value keeps consistently. |
|
The context manager to check whether the state is valid to trace. |
|
A context manager that catches and tracks new states created within its scope. |
Helper Functions#
Utility functions for common state operations.
Extracts the value from a State object if given, otherwise returns the input value. |
Error Handling#
Custom exceptions for state-related errors and debugging.
Exception Classes#
A custom exception class for BrainState-related errors. |
|
Exception raised for errors related to batch axis operations. |
|
Exception raised for trace-context violations. |
State Hooks#
The hook system observes and intercepts state operations — reads, writes, initialization, and restoration. Hooks can be registered globally and are managed per-process, enabling logging, validation, and instrumentation of stateful computations without modifying the states themselves.
Hook Contexts#
Context objects passed to hooks describing the operation being performed.
Base context object passed to all hooks. |
|
Context for read hooks. |
|
Base context for write operations. |
|
Context for write_before hooks with transformation capabilities. |
|
Context for restore hooks. |
|
Context for initialization hooks. |
Hook Core#
Core hook abstractions: the Hook base class and the HookHandle returned on
registration.
Base hook class for all hook types. |
|
Handle for managing a registered hook. |
Hook Manager and Registry#
Manage hook lifecycles and global registration.
Manager for hooks on a single State instance. |
|
Configuration for hook error handling and behavior. |
|
Singleton registry for global hooks that apply to all State instances. |
Global Hook Functions#
Convenience functions for registering and querying globally-installed hooks.
Register a global hook that applies to all State instances. |
|
Unregister a global hook using its handle. |
|
Clear all global hooks, optionally filtered by type. |
|
Check if any global hooks are registered. |
|
List all registered global hooks, optionally filtered by type. |
Hook Exceptions#
Exceptions and warnings raised by the hook system.
Base exception for hook-related errors. |
|
Exception raised when a hook execution fails. |
|
Exception raised when hook registration fails. |
|
Exception raised when a hook cancels an operation. |
|
Warning for hook-related issues. |