Skip to main content

quantize_rs/calibration/
methods.rs

1//! Calibration methods for quantization range optimization.
2
3use std::fmt;
4use std::str::FromStr;
5
6/// Strategy for choosing the quantization range from observed activations.
7#[derive(Debug, Clone, Copy)]
8#[derive(Default)]
9pub enum CalibrationMethod {
10    /// Use the full observed min/max range (default).
11    #[default]
12    MinMax,
13
14    /// Clip outliers at the given percentile (e.g. 99.9).
15    Percentile(f32),
16
17    /// Minimize KL divergence between the original and quantized distributions.
18    Entropy,
19
20    /// Minimize mean squared error between original and dequantized values.
21    MSE,
22}
23
24
25impl fmt::Display for CalibrationMethod {
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27        match self {
28            CalibrationMethod::MinMax => write!(f, "MinMax"),
29            CalibrationMethod::Percentile(_p) => write!(f, "Percentile"),
30            CalibrationMethod::Entropy => write!(f, "Entropy"),
31            CalibrationMethod::MSE => write!(f, "MSE"),
32        }
33    }
34}
35
36impl FromStr for CalibrationMethod {
37    type Err = crate::errors::QuantizeError;
38
39    fn from_str(s: &str) -> Result<Self, Self::Err> {
40        match s.to_lowercase().as_str() {
41            "minmax" => Ok(CalibrationMethod::MinMax),
42            "percentile" => Ok(CalibrationMethod::Percentile(99.9)),
43            "entropy" => Ok(CalibrationMethod::Entropy),
44            "mse" => Ok(CalibrationMethod::MSE),
45            _ => Err(crate::errors::QuantizeError::Config {
46                reason: format!("Unknown calibration method: '{}'. Valid methods: minmax, percentile, entropy, mse", s),
47            }),
48        }
49    }
50}