StaticModelGenerator

Struct StaticModelGenerator 

Source
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 as u16 index arrays plus f32 values.
  • The readout matrix (W_out) is emitted as a flattened f32 array.
  • The initial reservoir state is emitted as a flattened f32 array.

§Output format

generate_sparse_code returns a single String containing Rust code with:

  • INPUT_DIM, RESERVOIR_SIZE, OUTPUT_DIM, EXTENDED_SIZE
  • LEAKING_RATE
  • CSR metadata (*_NROWS, *_NCOLS, *_NNZ)
  • CSR arrays: *_ROW_PTR, *_COL_IDX, *_VALUES
  • W_OUT_DATA and INITIAL_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 into u16::MAX for both W_in and W_res.
  • All scalar values are formatted as f32 with 8 decimal digits. This is a deliberate tradeoff for portability and code size.
  • The extended state layout assumed here matches reservoir-infer reservoirs: [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

Source

pub fn generate_sparse_code<S, O>( esn: &EchoStateNetwork<S, SparseReservoir<S>, O>, ) -> Result<String, String>
where S: RealScalar + Display, O: Readout<S> + GetWeights<S>,

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_in and W_res (reservoir matrices) are emitted in CSR form, using u16 indices (ROW_PTR, COL_IDX) and f32 values (VALUES).
  • The readout weight matrix W_out is emitted as a flattened f32 array.
  • The initial reservoir state is emitted as a flattened f32 array.

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 implement reservoir_core::Readout<S> and GetWeights so 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 f32 with 8 decimals.
  • This function does not write files; it returns a String to give callers full control over where the generated code is stored.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V