pub struct ActivationEstimator { /* private fields */ }Expand description
Runs calibration samples through a model and collects activation statistics.
Usage:
let model = OnnxModel::load("model.onnx")?;
let mut estimator = ActivationEstimator::new(model, "model.onnx")?;
let dataset = CalibrationDataset::from_numpy("samples.npy")?;
estimator.calibrate(&dataset)?;
let stats = estimator.get_layer_stats(); // HashMap<layer_name, &ActivationStats>Implementations§
Source§impl ActivationEstimator
impl ActivationEstimator
Sourcepub fn from_path(model: OnnxModel, onnx_path: &str) -> Result<Self>
pub fn from_path(model: OnnxModel, onnx_path: &str) -> Result<Self>
Load model and prepare for calibration.
This:
- Reloads the ONNX file with tract (we need the filepath)
- Exposes all layer outputs as model outputs
- Optimizes the graph
- Creates a runnable plan
Important: The model parameter must have been loaded from a file
on disk. We re-parse that file with tract. If the model was constructed
programmatically or the file no longer exists, this will fail.
Sourcepub fn new(model: OnnxModel, onnx_path: &str) -> Result<Self>
pub fn new(model: OnnxModel, onnx_path: &str) -> Result<Self>
Convenience constructor when you have the model and its path.
Sourcepub fn calibrate(&mut self, dataset: &CalibrationDataset) -> Result<()>
pub fn calibrate(&mut self, dataset: &CalibrationDataset) -> Result<()>
Run calibration samples through the model and collect activation statistics.
For each sample:
- Run inference
- Capture all intermediate tensors
- Update min/max/histogram for each layer
Progress is printed every 10 batches.
Sourcepub fn get_layer_stats(&self) -> HashMap<String, &ActivationStats>
pub fn get_layer_stats(&self) -> HashMap<String, &ActivationStats>
Get collected activation statistics for all layers (borrowed).
Returns a map from layer name → &ActivationStats. These stats include min/max (for range optimization) and histogram (for entropy/MSE methods).
Sourcepub fn into_layer_stats(self) -> HashMap<String, ActivationStats>
pub fn into_layer_stats(self) -> HashMap<String, ActivationStats>
Consume and return owned activation statistics.
Use this when passing stats to Quantizer::with_calibration, which
expects HashMap<String, ActivationStats> (owned, not borrowed).
Sourcepub fn get_layer_stats_mut(&mut self) -> &mut HashMap<String, ActivationStats>
pub fn get_layer_stats_mut(&mut self) -> &mut HashMap<String, ActivationStats>
Get mutable reference to stats (for advanced use cases)
Sourcepub fn into_model(self) -> OnnxModel
pub fn into_model(self) -> OnnxModel
Consume the estimator and return the original OnnxModel.
Useful when you need the model back but have already extracted stats
with get_layer_stats() (borrowed). For the typical quantization
pipeline, use into_layer_stats() to get owned stats, then reload
the model separately for quantization.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for ActivationEstimator
impl !RefUnwindSafe for ActivationEstimator
impl Send for ActivationEstimator
impl Sync for ActivationEstimator
impl Unpin for ActivationEstimator
impl UnsafeUnpin for ActivationEstimator
impl !UnwindSafe for ActivationEstimator
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<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more