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