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: usizeNumber of batches collected so far.
Implementations§
Source§impl AdaptiveState
impl AdaptiveState
Sourcepub fn with_capacity(expected_samples: usize) -> Self
pub fn with_capacity(expected_samples: usize) -> Self
Create a new adaptive state with pre-allocated capacity.
Sourcepub fn add_batch(&mut self, baseline: Vec<u64>, sample: Vec<u64>)
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.
Sourcepub fn add_batch_with_conversion(
&mut self,
baseline: Vec<u64>,
sample: Vec<u64>,
ns_per_tick: f64,
)
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
Sourcepub fn update_kl(&mut self, kl: f64)
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.
Sourcepub fn recent_kl_sum(&self) -> f64
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).
Sourcepub fn has_kl_history(&self) -> bool
pub fn has_kl_history(&self) -> bool
Check if we have enough KL history for learning rate assessment.
Sourcepub fn update_posterior(&mut self, new_posterior: Posterior) -> f64
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.
Sourcepub fn current_posterior(&self) -> Option<&Posterior>
pub fn current_posterior(&self) -> Option<&Posterior>
Get the current posterior, if computed.
Sourcepub fn baseline_ns(&self, ns_per_tick: f64) -> Vec<f64>
pub fn baseline_ns(&self, ns_per_tick: f64) -> Vec<f64>
Convert baseline samples to f64 nanoseconds.
Sourcepub fn sample_ns(&self, ns_per_tick: f64) -> Vec<f64>
pub fn sample_ns(&self, ns_per_tick: f64) -> Vec<f64>
Convert sample samples to f64 nanoseconds.
Sourcepub fn baseline_stats(&self) -> Option<StatsSnapshot>
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.
Sourcepub fn sample_stats(&self) -> Option<StatsSnapshot>
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.
Sourcepub fn get_stats_snapshot(&self) -> Option<CalibrationSnapshot>
pub fn get_stats_snapshot(&self) -> Option<CalibrationSnapshot>
Get a CalibrationSnapshot from the current online statistics.
Returns None if insufficient samples have been tracked.
Sourcepub fn has_stats_tracking(&self) -> bool
pub fn has_stats_tracking(&self) -> bool
Check if online statistics are being tracked.
Returns true if add_batch_with_conversion has been used.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for AdaptiveState
impl RefUnwindSafe for AdaptiveState
impl Send for AdaptiveState
impl Sync for AdaptiveState
impl Unpin for AdaptiveState
impl UnwindSafe for AdaptiveState
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.