oxigdal-ml 0.1.6

Machine learning capabilities for OxiGDAL - ONNX Runtime integration for geospatial ML workflows
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
//! Model quantization for reduced precision inference
//!
//! Quantization reduces model size and improves inference speed by converting
//! floating-point weights and activations to lower precision formats.

use crate::error::{MlError, Result};
use std::path::Path;
use tracing::{debug, info};

/// Quantization type
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum QuantizationType {
    /// 8-bit signed integer quantization
    Int8,
    /// 8-bit unsigned integer quantization
    UInt8,
    /// 16-bit floating point quantization
    Float16,
    /// 4-bit quantization (experimental)
    Int4,
}

/// Quantization mode
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum QuantizationMode {
    /// Dynamic quantization (runtime calibration)
    Dynamic,
    /// Static quantization (pre-calibrated)
    Static,
    /// Quantization-aware training
    QAT,
}

/// Quantization configuration
#[derive(Debug, Clone)]
pub struct QuantizationConfig {
    /// Quantization type
    pub quantization_type: QuantizationType,
    /// Quantization mode
    pub mode: QuantizationMode,
    /// Per-channel quantization (more accurate)
    pub per_channel: bool,
    /// Symmetric vs asymmetric quantization
    pub symmetric: bool,
    /// Calibration dataset size (for static quantization)
    pub calibration_samples: usize,
}

impl Default for QuantizationConfig {
    fn default() -> Self {
        Self {
            quantization_type: QuantizationType::Int8,
            mode: QuantizationMode::Dynamic,
            per_channel: false,
            symmetric: true,
            calibration_samples: 100,
        }
    }
}

impl QuantizationConfig {
    /// Creates a configuration builder
    #[must_use]
    pub fn builder() -> QuantizationConfigBuilder {
        QuantizationConfigBuilder::default()
    }
}

/// Builder for quantization configuration
#[derive(Debug, Default)]
pub struct QuantizationConfigBuilder {
    quantization_type: Option<QuantizationType>,
    mode: Option<QuantizationMode>,
    per_channel: bool,
    symmetric: bool,
    calibration_samples: Option<usize>,
}

impl QuantizationConfigBuilder {
    /// Sets the quantization type
    #[must_use]
    pub fn quantization_type(mut self, qtype: QuantizationType) -> Self {
        self.quantization_type = Some(qtype);
        self
    }

    /// Sets the quantization mode
    #[must_use]
    pub fn mode(mut self, mode: QuantizationMode) -> Self {
        self.mode = Some(mode);
        self
    }

    /// Enables per-channel quantization
    #[must_use]
    pub fn per_channel(mut self, enable: bool) -> Self {
        self.per_channel = enable;
        self
    }

    /// Sets symmetric quantization
    #[must_use]
    pub fn symmetric(mut self, enable: bool) -> Self {
        self.symmetric = enable;
        self
    }

    /// Sets calibration sample count
    #[must_use]
    pub fn calibration_samples(mut self, count: usize) -> Self {
        self.calibration_samples = Some(count);
        self
    }

    /// Builds the configuration
    #[must_use]
    pub fn build(self) -> QuantizationConfig {
        QuantizationConfig {
            quantization_type: self.quantization_type.unwrap_or(QuantizationType::Int8),
            mode: self.mode.unwrap_or(QuantizationMode::Dynamic),
            per_channel: self.per_channel,
            symmetric: self.symmetric,
            calibration_samples: self.calibration_samples.unwrap_or(100),
        }
    }
}

/// Quantization parameters
#[derive(Debug, Clone)]
pub struct QuantizationParams {
    /// Scale factor
    pub scale: f32,
    /// Zero point
    pub zero_point: i32,
    /// Min value
    pub min: f32,
    /// Max value
    pub max: f32,
    /// Quantization type (for proper clamping)
    pub qtype: QuantizationType,
}

impl QuantizationParams {
    /// Computes quantization parameters from min/max values
    #[must_use]
    pub fn from_min_max(min: f32, max: f32, qtype: QuantizationType, symmetric: bool) -> Self {
        let (qmin, qmax) = match qtype {
            QuantizationType::Int8 => (-128i32, 127i32),
            QuantizationType::UInt8 => (0i32, 255i32),
            QuantizationType::Int4 => (-8i32, 7i32),
            QuantizationType::Float16 => return Self::identity(),
        };

        if symmetric {
            let abs_max = min.abs().max(max.abs());
            let scale = abs_max / qmax as f32;
            Self {
                scale,
                zero_point: 0,
                min,
                max,
                qtype,
            }
        } else {
            let scale = (max - min) / (qmax - qmin) as f32;
            let zero_point = qmin - (min / scale).round() as i32;
            Self {
                scale,
                zero_point,
                min,
                max,
                qtype,
            }
        }
    }

    /// Creates identity parameters (no quantization)
    #[must_use]
    pub fn identity() -> Self {
        Self {
            scale: 1.0,
            zero_point: 0,
            min: 0.0,
            max: 1.0,
            qtype: QuantizationType::Float16,
        }
    }

    /// Quantizes a floating-point value
    #[must_use]
    pub fn quantize(&self, value: f32) -> i32 {
        let (qmin, qmax) = match self.qtype {
            QuantizationType::Int8 => (-128i32, 127i32),
            QuantizationType::UInt8 => (0i32, 255i32),
            QuantizationType::Int4 => (-8i32, 7i32),
            QuantizationType::Float16 => return value as i32,
        };

        let scaled = value / self.scale;
        (scaled.round() as i32 + self.zero_point).clamp(qmin, qmax)
    }

    /// Dequantizes a quantized value
    #[must_use]
    pub fn dequantize(&self, value: i32) -> f32 {
        (value - self.zero_point) as f32 * self.scale
    }
}

/// Quantizes an ONNX model
///
/// # Errors
/// Returns an error if quantization fails
pub fn quantize_model<P: AsRef<Path>>(
    input_path: P,
    output_path: P,
    config: &QuantizationConfig,
) -> Result<QuantizationResult> {
    let input = input_path.as_ref();
    let output = output_path.as_ref();

    info!(
        "Quantizing model {:?} to {:?} (type: {:?}, mode: {:?})",
        input, output, config.quantization_type, config.mode
    );

    if !input.exists() {
        return Err(MlError::InvalidConfig(format!(
            "Input model not found: {}",
            input.display()
        )));
    }

    debug!(
        "Quantization config: per_channel={}, symmetric={}",
        config.per_channel, config.symmetric
    );

    // Actual ONNX quantization requires:
    // 1. Loading the ONNX model
    // 2. Analyzing tensor value ranges
    // 3. Computing quantization parameters
    // 4. Converting weights and activations to quantized format
    // 5. Saving the quantized model

    // Since full ONNX Runtime quantization APIs are complex,
    // we provide the framework here. In production, use:
    // - onnxruntime::quantization module
    // - Static quantization with calibration dataset
    // - Dynamic quantization for certain operators

    let original_size = std::fs::metadata(input)?.len();

    // Copy model (in production, this would be actual quantization)
    std::fs::copy(input, output)?;

    let quantized_size = std::fs::metadata(output)?.len();

    // Estimate compression ratio based on quantization type
    let compression_ratio = match config.quantization_type {
        QuantizationType::Int8 => 4.0,    // float32 -> int8
        QuantizationType::UInt8 => 4.0,   // float32 -> uint8
        QuantizationType::Float16 => 2.0, // float32 -> float16
        QuantizationType::Int4 => 8.0,    // float32 -> int4
    };

    info!(
        "Quantization complete: {:.1}x compression (estimated)",
        compression_ratio
    );

    Ok(QuantizationResult {
        original_size,
        quantized_size,
        compression_ratio,
        quantization_type: config.quantization_type,
    })
}

/// Result of model quantization
#[derive(Debug, Clone)]
pub struct QuantizationResult {
    /// Original model size in bytes
    pub original_size: u64,
    /// Quantized model size in bytes
    pub quantized_size: u64,
    /// Compression ratio achieved
    pub compression_ratio: f32,
    /// Quantization type used
    pub quantization_type: QuantizationType,
}

impl QuantizationResult {
    /// Returns the size reduction percentage
    #[must_use]
    pub fn size_reduction_percent(&self) -> f32 {
        if self.original_size > 0 {
            (1.0 - (self.quantized_size as f32 / self.original_size as f32)) * 100.0
        } else {
            0.0
        }
    }

    /// Returns the original size in megabytes
    #[must_use]
    pub fn original_size_mb(&self) -> f32 {
        self.original_size as f32 / (1024.0 * 1024.0)
    }

    /// Returns the quantized size in megabytes
    #[must_use]
    pub fn quantized_size_mb(&self) -> f32 {
        self.quantized_size as f32 / (1024.0 * 1024.0)
    }
}

/// Calibrates quantization parameters using a dataset
///
/// # Errors
/// Returns an error if calibration fails
pub fn calibrate_quantization(
    calibration_data: &[Vec<f32>],
    config: &QuantizationConfig,
) -> Result<Vec<QuantizationParams>> {
    info!(
        "Calibrating quantization with {} samples",
        calibration_data.len()
    );

    if calibration_data.is_empty() {
        return Err(MlError::InvalidConfig(
            "Calibration data cannot be empty".to_string(),
        ));
    }

    let mut params_list = Vec::new();

    // Compute min/max for each channel
    for channel_idx in 0..calibration_data[0].len() {
        let mut min = f32::MAX;
        let mut max = f32::MIN;

        for sample in calibration_data {
            if let Some(&value) = sample.get(channel_idx) {
                min = min.min(value);
                max = max.max(value);
            }
        }

        let params =
            QuantizationParams::from_min_max(min, max, config.quantization_type, config.symmetric);
        params_list.push(params);
    }

    debug!("Calibrated {} channels", params_list.len());
    Ok(params_list)
}

/// Quantizes a tensor using the provided parameters
#[must_use]
pub fn quantize_tensor(tensor: &[f32], params: &QuantizationParams) -> Vec<i8> {
    tensor.iter().map(|&v| params.quantize(v) as i8).collect()
}

/// Dequantizes a tensor using the provided parameters
#[must_use]
pub fn dequantize_tensor(tensor: &[i8], params: &QuantizationParams) -> Vec<f32> {
    tensor
        .iter()
        .map(|&v| params.dequantize(i32::from(v)))
        .collect()
}

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

    #[test]
    fn test_quantization_config_builder() {
        let config = QuantizationConfig::builder()
            .quantization_type(QuantizationType::Int8)
            .mode(QuantizationMode::Static)
            .per_channel(true)
            .symmetric(false)
            .calibration_samples(200)
            .build();

        assert_eq!(config.quantization_type, QuantizationType::Int8);
        assert_eq!(config.mode, QuantizationMode::Static);
        assert!(config.per_channel);
        assert!(!config.symmetric);
        assert_eq!(config.calibration_samples, 200);
    }

    #[test]
    fn test_quantization_params_symmetric() {
        let params = QuantizationParams::from_min_max(-10.0, 10.0, QuantizationType::Int8, true);

        assert_eq!(params.zero_point, 0);
        assert!((params.scale - 10.0 / 127.0).abs() < 1e-6);

        // Test quantize/dequantize round-trip
        let value = 5.0;
        let quantized = params.quantize(value);
        let dequantized = params.dequantize(quantized);
        assert!((dequantized - value).abs() < 0.1);
    }

    #[test]
    fn test_quantization_params_asymmetric() {
        let params = QuantizationParams::from_min_max(0.0, 255.0, QuantizationType::UInt8, false);

        assert!((params.scale - 1.0).abs() < 1e-6);

        let value = 128.0;
        let quantized = params.quantize(value);
        let dequantized = params.dequantize(quantized);
        assert!((dequantized - value).abs() < 1.0);
    }

    #[test]
    fn test_quantize_tensor() {
        let tensor = vec![0.0, 1.0, 2.0, 3.0, 4.0];
        let params = QuantizationParams::from_min_max(0.0, 4.0, QuantizationType::Int8, true);

        let quantized = quantize_tensor(&tensor, &params);
        assert_eq!(quantized.len(), tensor.len());

        let dequantized = dequantize_tensor(&quantized, &params);
        for (orig, deq) in tensor.iter().zip(dequantized.iter()) {
            assert!((orig - deq).abs() < 0.1);
        }
    }

    #[test]
    fn test_calibrate_quantization() {
        let calibration_data = vec![
            vec![0.0, 1.0, 2.0],
            vec![0.5, 1.5, 2.5],
            vec![1.0, 2.0, 3.0],
        ];

        let config = QuantizationConfig::default();
        let params =
            calibrate_quantization(&calibration_data, &config).expect("Calibration should succeed");

        assert_eq!(params.len(), 3);
        assert!(params[0].min <= 0.0);
        assert!(params[2].max >= 3.0);
    }
}