pub struct StaticModelGenerator;Expand description
Static model code generation utilities.
This module provides helpers to export a trained model (currently: sparse ESN)
into Rust source code that defines the model weights as const arrays.
The generated code is intended for embedded / no_std inference use cases,
where dynamic allocation and file I/O are undesirable:
- CSR matrices (
W_in,W_res) are emitted asu16index arrays plusf32values. - The readout matrix (
W_out) is emitted as a flattenedf32array. - The initial reservoir state is emitted as a flattened
f32array.
§Output format
generate_sparse_code returns a single String containing Rust code with:
INPUT_DIM,RESERVOIR_SIZE,OUTPUT_DIM,EXTENDED_SIZELEAKING_RATE- CSR metadata (
*_NROWS,*_NCOLS,*_NNZ) - CSR arrays:
*_ROW_PTR,*_COL_IDX,*_VALUES W_OUT_DATAandINITIAL_STATE_DATA
The generated snippet includes use statements for reservoir-infer static
reservoirs/readouts and nalgebra fixed-size types, so it can be pasted into
a target crate/module with minimal editing.
§Limits / invariants
- CSR indices are emitted as
u16. Therefore the number of non-zeros (NNZ) must fit intou16::MAXfor bothW_inandW_res. - All scalar values are formatted as
f32with 8 decimal digits. This is a deliberate tradeoff for portability and code size. - The extended state layout assumed here matches
reservoir-inferreservoirs:[bias(1), input(input_dim), reservoir_state(reservoir_size)].
§Feature gating
This code generator uses std::fmt::Write and returns an owned String,
so it is typically compiled behind the std feature of reservoir-train.
§Example
// Train a sparse ESN (ridge readout).
let mut esn = ESNBuilder::<f32>::new(1, 1)
.units(200)
.connectivity(8)
.input_connectivity(1)
.spectral_radius(1.2)
.leaking_rate(0.8)
.seed(42)
.build_sparse();
// Fit (dummy example; supply your actual data here).
// esn.fit(&inputs, &targets, 1e-6, 50);
// Export as Rust code (paste into your embedded inference crate).
let code = StaticModelGenerator::generate_sparse_code(&esn).unwrap();
println!("{}", code);Implementations§
Source§impl StaticModelGenerator
impl StaticModelGenerator
Sourcepub fn generate_sparse_code<S, O>(
esn: &EchoStateNetwork<S, SparseReservoir<S>, O>,
) -> Result<String, String>
pub fn generate_sparse_code<S, O>( esn: &EchoStateNetwork<S, SparseReservoir<S>, O>, ) -> Result<String, String>
Generate Rust source code for a trained sparse Echo State Network.
This function inspects the provided ESN and serializes its parameters into
Rust const definitions suitable for no_std inference:
W_inandW_res(reservoir matrices) are emitted in CSR form, usingu16indices (ROW_PTR,COL_IDX) andf32values (VALUES).- The readout weight matrix
W_outis emitted as a flattenedf32array. - The initial reservoir state is emitted as a flattened
f32array.
The output includes enough metadata (dimensions / NNZ counts) to validate the arrays at compile time and to reconstruct the static reservoir/readout types in a downstream crate.
§Type parameters
S: training scalar type. Must be convertible/printable (RealScalar + Display) because values are formatted into source code.O: readout type. Must implementreservoir_core::Readout<S>andGetWeightsso this generator can access the dense output weight matrix.
§Errors
Returns Err(String) if the number of non-zeros (NNZ) in W_in or W_res
exceeds u16::MAX, because the generated CSR arrays use u16 indices.
§Notes
- All emitted numeric values are formatted as
f32with 8 decimals. - This function does not write files; it returns a
Stringto give callers full control over where the generated code is stored.
Auto Trait Implementations§
impl Freeze for StaticModelGenerator
impl RefUnwindSafe for StaticModelGenerator
impl Send for StaticModelGenerator
impl Sync for StaticModelGenerator
impl Unpin for StaticModelGenerator
impl UnwindSafe for StaticModelGenerator
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.