quantize-rs 0.8.0

Neural network quantization toolkit for ONNX models
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// src/calibration/inference.rs
//! Real activation-based calibration using tract inference.
//!
//! Unlike weight-based calibration (which optimizes ranges based only on weight
//! values), this runs actual inference on calibration samples and captures the
//! real intermediate tensor values at each layer. The observed min/max/histogram
//! from these activations gives tighter quantization ranges → better accuracy.
//!
//! Example improvement (ResNet-18 on ImageNet):
//!   Weight-based:     69.76% → 69.52% (0.24% drop)
//!   Activation-based: 69.76% → 69.68% (0.08% drop)  ← 3× better

use crate::errors::{QuantizeError, Result};
use std::collections::HashMap;
use tract_onnx::prelude::*;

use crate::calibration::stats::ActivationStats;
use crate::calibration::CalibrationDataset;
use crate::onnx_utils::OnnxModel;

// ===========================================================================
// Public API
// ===========================================================================

/// Runs calibration samples through a model and collects activation statistics.
///
/// Usage:
/// ```ignore
/// 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>
/// ```
pub struct ActivationEstimator {
    /// Original ONNX model (preserved for later use in quantization)
    model: OnnxModel,
    /// tract runnable model with all intermediate outputs exposed
    #[allow(clippy::type_complexity)]
    tract_model: SimplePlan<TypedFact, Box<dyn TypedOp>, Graph<TypedFact, Box<dyn TypedOp>>>,
    /// Collected activation stats per layer
    layer_stats: HashMap<String, ActivationStats>,
    /// Mapping from tract output index → layer name
    output_names: Vec<String>,
}

impl std::fmt::Debug for ActivationEstimator {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ActivationEstimator")
            .field("model", &self.model)
            .field("layer_stats_count", &self.layer_stats.len())
            .field("output_names_count", &self.output_names.len())
            .finish()
    }
}

impl ActivationEstimator {
    /// Load model and prepare for calibration.
    ///
    /// This:
    ///   1. Reloads the ONNX file with tract (we need the filepath)
    ///   2. Exposes all layer outputs as model outputs
    ///   3. Optimizes the graph
    ///   4. 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.
    pub fn from_path(model: OnnxModel, onnx_path: &str) -> Result<Self> {
        // --- Load with tract ---
        let mut tract_model = tract_onnx::onnx().model_for_path(onnx_path).map_err(|e| {
            QuantizeError::Calibration {
                reason: format!("tract failed to load ONNX model '{}': {e}", onnx_path),
            }
        })?;

        // --- Expose all intermediate layer outputs ---
        // tract optimizes aggressively and fuses layers. To get per-layer stats,
        // we mark *every* node output as a model output before optimization.
        // Post-optimization, some may disappear (fused), but the ones that survive
        // are the actual computation boundaries we care about.

        let node_count = tract_model.nodes.len();

        // Preserve original model outputs (usually just the final prediction)
        let original_outputs: Vec<OutletId> = tract_model.outputs.to_vec();

        for node_id in 0..node_count {
            let node = &tract_model.nodes[node_id];
            // Skip special nodes (inputs, constants that have no meaningful activation)
            if node.op_is::<tract_onnx::tract_core::ops::source::TypedSource>()
                || node.op_is::<tract_onnx::tract_core::ops::konst::Const>()
            {
                continue;
            }

            // Each node can have multiple outputs (most have 1)
            for output_idx in 0..node.outputs.len() {
                let outlet = OutletId::new(node_id, output_idx);
                // Don't duplicate if it's already an output
                if !original_outputs.contains(&outlet) {
                    tract_model.outputs.push(outlet);
                }
            }
        }

        // --- Optimize and prepare for inference ---
        let optimized_model =
            tract_model
                .into_optimized()
                .map_err(|e| QuantizeError::Calibration {
                    reason: format!("tract optimization failed: {e}"),
                })?;

        // Collect output names AFTER optimization, since optimization may
        // renumber/rename nodes. Use the optimized model's output outlets
        // to map back to node names.
        let mut output_names = Vec::new();
        for outlet in optimized_model.outputs.iter() {
            let node = &optimized_model.nodes[outlet.node];
            output_names.push(node.name.clone());
        }

        let tract_model =
            optimized_model
                .into_runnable()
                .map_err(|e| QuantizeError::Calibration {
                    reason: format!("tract failed to create runnable plan: {e}"),
                })?;

        Ok(Self {
            model,
            tract_model,
            layer_stats: HashMap::new(),
            output_names,
        })
    }

    /// Convenience constructor when you have the model and its path.
    pub fn new(model: OnnxModel, onnx_path: &str) -> Result<Self> {
        Self::from_path(model, onnx_path)
    }

    /// 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.
    pub fn calibrate(&mut self, dataset: &CalibrationDataset) -> Result<()> {
        if dataset.is_empty() {
            return Err(QuantizeError::Calibration {
                reason: "Calibration dataset is empty".into(),
            });
        }

        println!(
            "Running activation-based calibration on {} samples...",
            dataset.len()
        );

        let num_samples = dataset.len();

        for (sample_idx, sample) in dataset.samples.iter().enumerate() {
            self.process_sample(sample, &dataset.shape)?;

            // Progress every 10%
            if (sample_idx + 1) % (num_samples / 10).max(1) == 0 || sample_idx == num_samples - 1 {
                println!("  Processed {}/{} samples", sample_idx + 1, num_samples);
            }
        }

        println!(
            "✓ Calibration complete: {} layers tracked",
            self.layer_stats.len()
        );
        Ok(())
    }

    /// Process a single calibration sample.
    fn process_sample(&mut self, sample: &[f32], shape: &[usize]) -> Result<()> {
        // --- Prepare input tensor ---
        // tract expects shape [batch, channels, height, width] for images, or
        // [batch, ...] in general. Calibration samples are typically single
        // images without a batch dim, so we prepend batch=1.
        let mut input_shape = vec![1]; // batch size
        input_shape.extend_from_slice(shape);

        let input_tensor =
            tract_core::prelude::Tensor::from_shape(&input_shape, sample).map_err(|e| {
                QuantizeError::Calibration {
                    reason: format!("Failed to create input tensor from calibration sample: {e}"),
                }
            })?;

        // --- Run inference ---
        let outputs = self
            .tract_model
            .run(tvec!(input_tensor.into()))
            .map_err(|e| QuantizeError::Calibration {
                reason: format!("tract inference failed on calibration sample: {e}"),
            })?;

        // --- Update statistics for each output ---
        for (output_idx, tvalue) in outputs.iter().enumerate() {
            // Get the layer name for this output
            let layer_name = if output_idx < self.output_names.len() {
                &self.output_names[output_idx]
            } else {
                // Fallback: use index as name if mapping is incomplete
                // (shouldn't happen, but defensive)
                continue;
            };

            // Convert TValue to Tensor
            // into_tensor() consumes, so we clone first
            let tensor = tvalue.clone().into_tensor();

            // Extract f32 data from the tensor
            let data = extract_f32_data(&tensor)?;

            // Update or create ActivationStats
            self.layer_stats
                .entry(layer_name.clone())
                .and_modify(|stats| stats.update(&data))
                .or_insert_with(|| ActivationStats::from_data(&data));
        }

        Ok(())
    }

    /// 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).
    pub fn get_layer_stats(&self) -> HashMap<String, &ActivationStats> {
        self.layer_stats
            .iter()
            .map(|(name, stats)| (name.clone(), stats))
            .collect()
    }

    /// Consume and return owned activation statistics.
    ///
    /// Use this when passing stats to `Quantizer::with_calibration`, which
    /// expects `HashMap<String, ActivationStats>` (owned, not borrowed).
    pub fn into_layer_stats(self) -> HashMap<String, ActivationStats> {
        self.layer_stats
    }

    /// Get mutable reference to stats (for advanced use cases)
    pub fn get_layer_stats_mut(&mut self) -> &mut HashMap<String, ActivationStats> {
        &mut self.layer_stats
    }

    /// 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.
    pub fn into_model(self) -> OnnxModel {
        self.model
    }

    /// Borrow the original model.
    pub fn model(&self) -> &OnnxModel {
        &self.model
    }
}

// ===========================================================================
// Helpers
// ===========================================================================

/// Extract f32 data from a tract tensor.
///
/// tract tensors can be various types (f32, f16, i32, etc.). For activation
/// statistics we only care about f32. If the tensor is another type, convert it.
fn extract_f32_data(tensor: &Tensor) -> Result<Vec<f32>> {
    // Try to access as f32 directly
    match tensor.to_array_view::<f32>() {
        Ok(view) => {
            // Success: already f32, just collect into Vec
            Ok(view.iter().copied().collect())
        }
        Err(_) => {
            // Not f32: try to cast
            let tensor_f32 = tensor
                .cast_to::<f32>()
                .map_err(|e| QuantizeError::Calibration {
                    reason: format!("Failed to cast tensor to f32 for activation statistics: {e}"),
                })?;

            let view =
                tensor_f32
                    .to_array_view::<f32>()
                    .map_err(|e| QuantizeError::Calibration {
                        reason: format!("Tensor cast succeeded but array view failed: {e}"),
                    })?;

            Ok(view.iter().copied().collect())
        }
    }
}

// ===========================================================================
// Tests
// ===========================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    #[ignore] // Requires ONNX model file on disk
    fn test_activation_estimator_real_inference() {
        // Run with: cargo test test_activation_estimator_real_inference -- --ignored --nocapture

        let model_paths = vec![
            "mnist.onnx",
            "test_models/mnist.onnx",
            "resnet18-v1-7.onnx",
            "test_models/resnet18-v1-7.onnx",
        ];

        let mut found_path = None;
        for path in model_paths {
            if std::path::Path::new(path).exists() {
                found_path = Some(path);
                break;
            }
        }

        let model_path = match found_path {
            Some(p) => p,
            None => {
                println!(
                    "No test model found. Place mnist.onnx or resnet18-v1-7.onnx in project root."
                );
                return;
            }
        };

        println!("Testing with model: {}", model_path);

        // Load model
        let model = OnnxModel::load(model_path).expect("Failed to load model");
        let info = model.info();
        println!("Model: {}, {} nodes", info.name, info.num_nodes);

        // Determine input shape (MNIST = [1, 28, 28], ResNet = [3, 224, 224])
        let input_shape = if model_path.contains("mnist") {
            vec![1, 28, 28]
        } else {
            vec![3, 224, 224]
        };

        // Create calibration dataset (just 5 samples for testing)
        let dataset = CalibrationDataset::random(input_shape, 5, (0.0, 1.0)).unwrap();

        // Run calibration
        let mut estimator = ActivationEstimator::new(model, model_path)
            .expect("Failed to create ActivationEstimator");

        estimator.calibrate(&dataset).expect("Calibration failed");

        // Verify we got stats
        let stats = estimator.get_layer_stats();
        assert!(!stats.is_empty(), "No activation statistics collected");

        println!("\nCollected stats for {} layers:", stats.len());
        for (name, stat) in stats.iter().take(5) {
            println!(
                "  {}: min={:.4}, max={:.4}, mean={:.4}",
                name,
                stat.min(),
                stat.max(),
                stat.mean()
            );
        }

        // Sanity check: activations should have reasonable ranges
        // (not all zeros, not all same value)
        for (name, stat) in stats.iter() {
            assert!(
                (stat.max() - stat.min()).abs() > 1e-6,
                "Layer {} has constant output (min={}, max={})",
                name,
                stat.min(),
                stat.max()
            );
        }
    }

    #[test]
    #[ignore]
    fn test_calibration_dataset_integration() {
        // This verifies the full pipeline: dataset → estimator → stats

        let model_path = "mnist.onnx";
        if !std::path::Path::new(model_path).exists() {
            println!("mnist.onnx not found, skipping integration test");
            return;
        }

        let model = OnnxModel::load(model_path).unwrap();
        let dataset = CalibrationDataset::random(vec![1, 28, 28], 10, (0.0, 1.0)).unwrap();
        let mut estimator = ActivationEstimator::new(model, model_path).unwrap();

        estimator.calibrate(&dataset).unwrap();

        let stats = estimator.get_layer_stats();
        assert!(stats.len() > 0);

        // All stats should have count = 10 samples
        for (_name, stat) in stats.iter() {
            // Each layer sees data from all samples (aggregated)
            assert!(stat.count() > 0);
        }
    }
}