State Management¶
Utilities for flattening, unflattening, and detaching nested model state.
StateSpec ¶
Bases: NamedTuple
Batch-agnostic specification for a nested model state structure.
Stores shape templates with -1 at the batch dimension, enabling
flatten/unflatten at any batch size.
detach_state ¶
Recursively detach tensors from the computation graph.
Source code in tsfast/models/_core/state.py
discover_state_spec ¶
Probe a stateful model to discover its state structure.
Forwards two batches of different sizes and compares the resulting state
shapes to identify the batch dimension. Templates store -1 at the
batch dim so the spec is batch-agnostic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
stateful model returning |
required |
n_in
|
int
|
number of input features |
required |
device
|
str | device
|
device for probe tensors |
'cpu'
|
Source code in tsfast/models/_core/state.py
build_spec_from_state ¶
Build a StateSpec from a single state sample by inferring the batch dimension.
The batch dimension is identified as the first dimension matching batch_size.
Ambiguous when a non-batch dimension equals batch_size.
Source code in tsfast/models/_core/state.py
flatten_state ¶
Flatten arbitrary nested state to a single [B, D] tensor.
Source code in tsfast/models/_core/state.py
unflatten_state ¶
Reconstruct nested state from a flat [B, D] tensor + StateSpec.