Skip to main content

AdaptiveState

Struct AdaptiveState 

Source
pub struct AdaptiveState {
    pub baseline_samples: Vec<u64>,
    pub sample_samples: Vec<u64>,
    pub previous_posterior: Option<Posterior>,
    pub recent_kl_divergences: VecDeque<f64>,
    pub batch_count: usize,
    /* private fields */
}
Expand description

State maintained during adaptive sampling loop.

This struct accumulates timing samples and tracks the evolution of the posterior distribution across batches, enabling quality gate checks like KL divergence monitoring.

Also maintains online statistics (mean, variance, lag-1 autocorrelation) for condition drift detection between calibration and post-test phases.

§Time Tracking

This struct does NOT track elapsed time internally. The caller must provide elapsed_secs to functions that need it (e.g., quality gate checks). This allows use in no_std environments where std::time::Instant is unavailable.

Fields§

§baseline_samples: Vec<u64>

Baseline class timing samples (in cycles/ticks/native units).

§sample_samples: Vec<u64>

Sample class timing samples (in cycles/ticks/native units).

§previous_posterior: Option<Posterior>

Previous posterior for KL divergence tracking. None until we have at least one posterior computed.

§recent_kl_divergences: VecDeque<f64>

Recent KL divergences (last 5 batches) for learning rate monitoring. If sum of recent KL < 0.001, learning has stalled.

§batch_count: usize

Number of batches collected so far.

Implementations§

Source§

impl AdaptiveState

Source

pub fn new() -> Self

Create a new empty adaptive state.

Source

pub fn with_capacity(expected_samples: usize) -> Self

Create a new adaptive state with pre-allocated capacity.

Source

pub fn n_total(&self) -> usize

Get the total number of samples per class.

Source

pub fn add_batch(&mut self, baseline: Vec<u64>, sample: Vec<u64>)

Add a batch of samples to the state.

Both baseline and sample vectors should have the same length. Note: This method does not track online statistics since ns_per_tick is not known. Use add_batch_with_conversion if you need drift detection.

Source

pub fn add_batch_with_conversion( &mut self, baseline: Vec<u64>, sample: Vec<u64>, ns_per_tick: f64, )

Add a batch of samples and track online statistics for drift detection.

Both baseline and sample vectors should have the same length.

§Arguments
  • baseline - Baseline class timing samples (in native units)
  • sample - Sample class timing samples (in native units)
  • ns_per_tick - Conversion factor from native units to nanoseconds
Source

pub fn update_kl(&mut self, kl: f64)

Update KL divergence history with a new value.

Maintains a sliding window of the last 5 KL divergences for learning rate monitoring.

Source

pub fn recent_kl_sum(&self) -> f64

Get the sum of recent KL divergences.

Used to detect learning stall (sum < 0.001 indicates posterior has stopped updating despite new data).

Source

pub fn has_kl_history(&self) -> bool

Check if we have enough KL history for learning rate assessment.

Source

pub fn update_posterior(&mut self, new_posterior: Posterior) -> f64

Update the posterior and track KL divergence.

Returns the KL divergence from the previous posterior, or 0.0 if this is the first posterior.

Source

pub fn current_posterior(&self) -> Option<&Posterior>

Get the current posterior, if computed.

Source

pub fn baseline_ns(&self, ns_per_tick: f64) -> Vec<f64>

Convert baseline samples to f64 nanoseconds.

Source

pub fn sample_ns(&self, ns_per_tick: f64) -> Vec<f64>

Convert sample samples to f64 nanoseconds.

Source

pub fn baseline_stats(&self) -> Option<StatsSnapshot>

Get the current online statistics for the baseline class.

Returns None if no samples have been added with conversion tracking.

Source

pub fn sample_stats(&self) -> Option<StatsSnapshot>

Get the current online statistics for the sample class.

Returns None if no samples have been added with conversion tracking.

Source

pub fn get_stats_snapshot(&self) -> Option<CalibrationSnapshot>

Get a CalibrationSnapshot from the current online statistics.

Returns None if insufficient samples have been tracked.

Source

pub fn has_stats_tracking(&self) -> bool

Check if online statistics are being tracked.

Returns true if add_batch_with_conversion has been used.

Source

pub fn reset(&mut self)

Reset the state for a new test run.

Clears all samples, posteriors, and statistics while preserving capacity.

Trait Implementations§

Source§

impl Default for AdaptiveState

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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.