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>
impl<'a> QuantizedEnsembleView<'a>
Sourcepub fn from_bytes(data: &'a [u8]) -> Result<Self, FormatError>
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.
Sourcepub fn predict(&self, features: &[f32]) -> f32
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.
Sourcepub fn predict_prequantized(&self, features_i16: &[i16]) -> f32
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.
Sourcepub fn predict_batch(&self, samples: &[&[f32]], out: &mut [f32])
pub fn predict_batch(&self, samples: &[&[f32]], out: &mut [f32])
Sourcepub fn predict_batch_prequantized(&self, samples: &[&[i16]], out: &mut [f32])
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.
Sourcepub fn n_features(&self) -> u16
pub fn n_features(&self) -> u16
Expected number of input features.
Sourcepub fn base_prediction(&self) -> f32
pub fn base_prediction(&self) -> f32
Base prediction value.
Sourcepub fn leaf_scale(&self) -> f32
pub fn leaf_scale(&self) -> f32
Global leaf scale factor for dequantization.
Sourcepub fn feature_scales(&self) -> &[f32]
pub fn feature_scales(&self) -> &[f32]
Per-feature quantization scales.
Sourcepub fn total_nodes(&self) -> usize
pub fn total_nodes(&self) -> usize
Total number of packed i16 nodes across all trees.
Trait Implementations§
Source§impl<'a> Clone for QuantizedEnsembleView<'a>
impl<'a> Clone for QuantizedEnsembleView<'a>
Source§fn clone(&self) -> QuantizedEnsembleView<'a>
fn clone(&self) -> QuantizedEnsembleView<'a>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more