Skip to main content

QuantizedEnsembleView

Struct QuantizedEnsembleView 

Source
pub struct QuantizedEnsembleView<'a> { /* private fields */ }
Expand description

Zero-copy view over a quantized (int16) ensemble binary.

After validation in from_bytes, all traversal uses integer-only comparisons. Features are quantized once per predict call, then each tree traversal is pure i16. Leaf values accumulate as i32, dequantized once at the end.

§Lifetime

The view borrows the input buffer — the buffer must outlive the view.

Implementations§

Source§

impl<'a> QuantizedEnsembleView<'a>

Source

pub fn from_bytes(data: &'a [u8]) -> Result<Self, FormatError>

Parse and validate a quantized ensemble binary.

§Binary layout
[QuantizedEnsembleHeader: 16 bytes]   magic="IR16", version, n_trees, n_features, base_prediction
[leaf_scale: f32]                      4 bytes — global scale for leaf dequantization
[feature_scales: f32 x n_features]     n_features x 4 bytes — per-feature quantization scales
[TreeEntry x n_trees: 8 bytes each]    same struct as f32 format
[PackedNodeI16 x total_nodes: 8 bytes each]
§Validates
  • Magic bytes match "IR16"
  • Format version is supported
  • Buffer is large enough for header + leaf_scale + feature_scales + tree table + all nodes
  • Every internal node’s child indices are within bounds
  • Every internal node’s feature index is < n_features
  • Tree offsets are aligned to size_of::<PackedNodeI16>()
§Errors

Returns FormatError if any validation check fails.

Source

pub fn predict(&self, features: &[f32]) -> f32

Predict a single sample with inline feature quantization. Zero allocation.

Each tree comparison performs one f32 multiply to quantize the feature on-the-fly. Leaf values accumulate as i32 and are dequantized once at the end: base_prediction + leaf_sum / leaf_scale.

For the pure-integer path (no f32 ops in the hot loop), use predict_prequantized.

§Precondition

features.len() must be >= self.n_features(). Passing fewer features causes undefined behavior via get_unchecked. A debug_assert catches this in debug builds.

Source

pub fn predict_prequantized(&self, features_i16: &[i16]) -> f32

Predict a single sample from pre-quantized features. Pure integer hot loop.

The caller is responsible for quantizing features beforehand: features_i16[i] = (features_f32[i] * feature_scales[i]) as i16

This eliminates all float ops from the tree traversal — the only float operation is the final base_prediction + leaf_sum / leaf_scale.

§Precondition

features_i16.len() must be >= self.n_features(). Passing fewer features causes undefined behavior via get_unchecked. A debug_assert catches this in debug builds.

Source

pub fn predict_batch(&self, samples: &[&[f32]], out: &mut [f32])

Batch predict with inline quantization. Uses x4 interleaving when samples.len() >= 4.

§Panics

Panics if out.len() < samples.len().

§Precondition

Every sample must have len() >= self.n_features(). See predict.

Source

pub fn predict_batch_prequantized(&self, samples: &[&[i16]], out: &mut [f32])

Batch predict from pre-quantized features. Uses x4 interleaving when samples.len() >= 4.

§Panics

Panics if out.len() < samples.len().

§Precondition

Every sample must have len() >= self.n_features(). See predict_prequantized.

Source

pub fn n_trees(&self) -> u16

Number of trees in the ensemble.

Source

pub fn n_features(&self) -> u16

Expected number of input features.

Source

pub fn base_prediction(&self) -> f32

Base prediction value.

Source

pub fn leaf_scale(&self) -> f32

Global leaf scale factor for dequantization.

Source

pub fn feature_scales(&self) -> &[f32]

Per-feature quantization scales.

Source

pub fn total_nodes(&self) -> usize

Total number of packed i16 nodes across all trees.

Trait Implementations§

Source§

impl<'a> Clone for QuantizedEnsembleView<'a>

Source§

fn clone(&self) -> QuantizedEnsembleView<'a>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for QuantizedEnsembleView<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> Copy for QuantizedEnsembleView<'a>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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, 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.