oxirs-embed 0.2.4

Knowledge graph embeddings with TransE, ComplEx, and custom 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
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
//! Quantization Support for Model Compression
//!
//! This module provides quantization techniques to compress knowledge graph
//! embeddings by reducing precision from float32 to int8/int4, significantly
//! reducing model size and improving inference speed.

use anyhow::{anyhow, Result};
use scirs2_core::ndarray_ext::Array1;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use tracing::{debug, info};

/// Quantization scheme
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum QuantizationScheme {
    /// Symmetric quantization (zero point = 0)
    Symmetric,
    /// Asymmetric quantization (learnable zero point)
    Asymmetric,
    /// Per-channel quantization
    PerChannel,
    /// Per-tensor quantization
    PerTensor,
}

/// Quantization bit width
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum BitWidth {
    /// 8-bit quantization
    Int8,
    /// 4-bit quantization
    Int4,
    /// Binary quantization (1-bit)
    Binary,
}

impl BitWidth {
    /// Get quantization range
    pub fn range(&self) -> (i32, i32) {
        match self {
            BitWidth::Int8 => (-128, 127),
            BitWidth::Int4 => (-8, 7),
            BitWidth::Binary => (0, 1),
        }
    }

    /// Get number of bits
    pub fn bits(&self) -> usize {
        match self {
            BitWidth::Int8 => 8,
            BitWidth::Int4 => 4,
            BitWidth::Binary => 1,
        }
    }
}

/// Quantization configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QuantizationConfig {
    /// Quantization scheme to use
    pub scheme: QuantizationScheme,
    /// Bit width for quantization
    pub bit_width: BitWidth,
    /// Enable calibration for better quantization
    pub calibration: bool,
    /// Number of calibration samples
    pub calibration_samples: usize,
    /// Quantize only weights (keep activations in float)
    pub weights_only: bool,
    /// Use quantization-aware training
    pub qat: bool,
}

impl Default for QuantizationConfig {
    fn default() -> Self {
        Self {
            scheme: QuantizationScheme::Symmetric,
            bit_width: BitWidth::Int8,
            calibration: true,
            calibration_samples: 1000,
            weights_only: true,
            qat: false,
        }
    }
}

/// Quantization parameters
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QuantizationParams {
    /// Scale factor
    pub scale: f32,
    /// Zero point
    pub zero_point: i32,
    /// Min value observed during calibration
    pub min_val: f32,
    /// Max value observed during calibration
    pub max_val: f32,
}

impl QuantizationParams {
    /// Compute quantization parameters from tensor statistics
    pub fn from_statistics(
        min_val: f32,
        max_val: f32,
        bit_width: BitWidth,
        symmetric: bool,
    ) -> Self {
        let (qmin, qmax) = bit_width.range();

        let (scale, zero_point) = if symmetric {
            // Symmetric quantization
            let max_abs = min_val.abs().max(max_val.abs());
            let scale = (2.0 * max_abs) / (qmax - qmin) as f32;
            (scale, 0)
        } else {
            // Asymmetric quantization
            let scale = (max_val - min_val) / (qmax - qmin) as f32;
            let zero_point = qmin - (min_val / scale).round() as i32;
            (scale, zero_point)
        };

        Self {
            scale,
            zero_point,
            min_val,
            max_val,
        }
    }

    /// Quantize a float value
    pub fn quantize(&self, value: f32, bit_width: BitWidth) -> i8 {
        let (qmin, qmax) = bit_width.range();
        let quantized = (value / self.scale).round() as i32 + self.zero_point;
        quantized.clamp(qmin, qmax) as i8
    }

    /// Dequantize an int value back to float
    pub fn dequantize(&self, quantized: i8) -> f32 {
        (quantized as i32 - self.zero_point) as f32 * self.scale
    }
}

/// Quantized tensor representation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QuantizedTensor {
    /// Quantized values (int8)
    pub values: Vec<i8>,
    /// Quantization parameters
    pub params: QuantizationParams,
    /// Original shape
    pub shape: Vec<usize>,
}

impl QuantizedTensor {
    /// Create quantized tensor from float array
    pub fn from_array(array: &Array1<f32>, config: &QuantizationConfig) -> Self {
        let min_val = array.iter().cloned().fold(f32::INFINITY, f32::min);
        let max_val = array.iter().cloned().fold(f32::NEG_INFINITY, f32::max);

        let symmetric = matches!(config.scheme, QuantizationScheme::Symmetric);
        let params =
            QuantizationParams::from_statistics(min_val, max_val, config.bit_width, symmetric);

        let values: Vec<i8> = array
            .iter()
            .map(|&v| params.quantize(v, config.bit_width))
            .collect();

        Self {
            values,
            params,
            shape: vec![array.len()],
        }
    }

    /// Dequantize back to float array
    pub fn to_array(&self) -> Array1<f32> {
        Array1::from_vec(
            self.values
                .iter()
                .map(|&v| self.params.dequantize(v))
                .collect(),
        )
    }

    /// Get compression ratio
    pub fn compression_ratio(&self) -> f32 {
        // Original: 4 bytes per float32
        // Quantized: 1 byte per int8 + overhead for params
        let original_size = self.values.len() * 4;
        let quantized_size = self.values.len() + std::mem::size_of::<QuantizationParams>();
        original_size as f32 / quantized_size as f32
    }

    /// Get size in bytes
    pub fn size_bytes(&self) -> usize {
        self.values.len() + std::mem::size_of::<QuantizationParams>()
    }
}

/// Quantization statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QuantizationStats {
    /// Total parameters quantized
    pub total_params: usize,
    /// Original model size (bytes)
    pub original_size_bytes: usize,
    /// Quantized model size (bytes)
    pub quantized_size_bytes: usize,
    /// Compression ratio
    pub compression_ratio: f32,
    /// Average quantization error
    pub avg_quantization_error: f32,
    /// Maximum quantization error
    pub max_quantization_error: f32,
}

impl Default for QuantizationStats {
    fn default() -> Self {
        Self {
            total_params: 0,
            original_size_bytes: 0,
            quantized_size_bytes: 0,
            compression_ratio: 1.0,
            avg_quantization_error: 0.0,
            max_quantization_error: 0.0,
        }
    }
}

/// Model quantizer
pub struct ModelQuantizer {
    config: QuantizationConfig,
    stats: QuantizationStats,
}

impl ModelQuantizer {
    /// Create new model quantizer
    pub fn new(config: QuantizationConfig) -> Self {
        info!(
            "Initialized model quantizer: scheme={:?}, bit_width={:?}",
            config.scheme, config.bit_width
        );

        Self {
            config,
            stats: QuantizationStats::default(),
        }
    }

    /// Quantize entity embeddings
    pub fn quantize_embeddings(
        &mut self,
        embeddings: &HashMap<String, Array1<f32>>,
    ) -> Result<HashMap<String, QuantizedTensor>> {
        if embeddings.is_empty() {
            return Err(anyhow!("No embeddings to quantize"));
        }

        info!("Quantizing {} embeddings", embeddings.len());

        let mut quantized_embeddings = HashMap::new();
        let mut total_error = 0.0;
        let mut max_error: f32 = 0.0;

        for (entity, embedding) in embeddings {
            let quantized = QuantizedTensor::from_array(embedding, &self.config);

            // Compute quantization error
            let dequantized = quantized.to_array();
            let error = self.compute_error(embedding, &dequantized);
            total_error += error;
            max_error = max_error.max(error);

            // Update stats
            self.stats.original_size_bytes += embedding.len() * 4;
            self.stats.quantized_size_bytes += quantized.size_bytes();

            quantized_embeddings.insert(entity.clone(), quantized);
        }

        self.stats.total_params = embeddings.values().map(|e| e.len()).sum();
        self.stats.compression_ratio =
            self.stats.original_size_bytes as f32 / self.stats.quantized_size_bytes as f32;
        self.stats.avg_quantization_error = total_error / embeddings.len() as f32;
        self.stats.max_quantization_error = max_error;

        info!(
            "Quantization complete: compression_ratio={:.2}x, avg_error={:.6}",
            self.stats.compression_ratio, self.stats.avg_quantization_error
        );

        Ok(quantized_embeddings)
    }

    /// Dequantize embeddings
    pub fn dequantize_embeddings(
        &self,
        quantized: &HashMap<String, QuantizedTensor>,
    ) -> HashMap<String, Array1<f32>> {
        quantized
            .iter()
            .map(|(entity, q)| (entity.clone(), q.to_array()))
            .collect()
    }

    /// Quantize a single embedding
    pub fn quantize_embedding(&self, embedding: &Array1<f32>) -> QuantizedTensor {
        QuantizedTensor::from_array(embedding, &self.config)
    }

    /// Dequantize a single embedding
    pub fn dequantize_embedding(&self, quantized: &QuantizedTensor) -> Array1<f32> {
        quantized.to_array()
    }

    /// Compute mean squared error between original and dequantized
    fn compute_error(&self, original: &Array1<f32>, dequantized: &Array1<f32>) -> f32 {
        let diff = original - dequantized;
        let mse = diff.dot(&diff) / original.len() as f32;
        mse.sqrt() // RMSE
    }

    /// Calibrate quantization parameters using sample data
    pub fn calibrate(&mut self, embeddings: &HashMap<String, Array1<f32>>) -> Result<()> {
        if !self.config.calibration {
            return Ok(());
        }

        info!(
            "Calibrating quantization parameters with {} samples",
            self.config.calibration_samples.min(embeddings.len())
        );

        // Collect statistics from sample embeddings
        let samples: Vec<&Array1<f32>> = embeddings
            .values()
            .take(self.config.calibration_samples)
            .collect();

        // Find global min/max for per-tensor quantization
        let mut global_min = f32::INFINITY;
        let mut global_max = f32::NEG_INFINITY;

        for embedding in samples {
            let min = embedding.iter().cloned().fold(f32::INFINITY, f32::min);
            let max = embedding.iter().cloned().fold(f32::NEG_INFINITY, f32::max);
            global_min = global_min.min(min);
            global_max = global_max.max(max);
        }

        debug!(
            "Calibration complete: min={:.6}, max={:.6}",
            global_min, global_max
        );

        Ok(())
    }

    /// Get quantization statistics
    pub fn get_stats(&self) -> &QuantizationStats {
        &self.stats
    }

    /// Estimate inference speedup
    pub fn estimate_speedup(&self) -> f32 {
        // Int8 operations are typically 2-4x faster than float32
        match self.config.bit_width {
            BitWidth::Int8 => 3.0,
            BitWidth::Int4 => 5.0,
            BitWidth::Binary => 10.0,
        }
    }

    /// Get configuration
    pub fn config(&self) -> &QuantizationConfig {
        &self.config
    }
}

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

    #[test]
    fn test_quantization_params() {
        let min_val = -10.0;
        let max_val = 10.0;

        let params = QuantizationParams::from_statistics(
            min_val,
            max_val,
            BitWidth::Int8,
            true, // symmetric
        );

        assert!(params.scale > 0.0);
        assert_eq!(params.zero_point, 0); // Symmetric should have zero point = 0
    }

    #[test]
    fn test_quantize_dequantize() {
        let params = QuantizationParams::from_statistics(-10.0, 10.0, BitWidth::Int8, true);

        let value = 5.0;
        let quantized = params.quantize(value, BitWidth::Int8);
        let dequantized = params.dequantize(quantized);

        // Should be approximately equal (within quantization error)
        assert!((value - dequantized).abs() < 1.0);
    }

    #[test]
    fn test_quantized_tensor() {
        // Use larger array (128 elements) so compression ratio > 1.0
        // With small arrays, quantization params overhead dominates
        let array = Array1::from_vec((0..128).map(|i| i as f32 * 0.1).collect());
        let config = QuantizationConfig::default();

        let quantized = QuantizedTensor::from_array(&array, &config);
        let dequantized = quantized.to_array();

        assert_eq!(quantized.values.len(), 128);
        assert_eq!(dequantized.len(), 128);

        // Check compression ratio (should be ~3.8x for 128-dim)
        assert!(quantized.compression_ratio() > 1.0);
    }

    #[test]
    fn test_model_quantizer() {
        let mut embeddings = HashMap::new();
        // Use larger embeddings (128-dim) for meaningful compression
        embeddings.insert(
            "e1".to_string(),
            Array1::from_vec((0..128).map(|i| i as f32 * 0.1).collect()),
        );
        embeddings.insert(
            "e2".to_string(),
            Array1::from_vec((0..128).map(|i| (i as f32 * 0.1) + 10.0).collect()),
        );

        let config = QuantizationConfig::default();
        let mut quantizer = ModelQuantizer::new(config);

        let quantized = quantizer
            .quantize_embeddings(&embeddings)
            .expect("should succeed");

        assert_eq!(quantized.len(), 2);
        assert!(quantizer.stats.compression_ratio > 1.0);
        assert!(quantizer.stats.avg_quantization_error >= 0.0);
    }

    #[test]
    fn test_roundtrip() {
        let mut embeddings = HashMap::new();
        embeddings.insert("e1".to_string(), array![1.0, -2.0, 3.5, -4.2]);

        let config = QuantizationConfig::default();
        let mut quantizer = ModelQuantizer::new(config);

        let quantized = quantizer
            .quantize_embeddings(&embeddings)
            .expect("should succeed");
        let dequantized = quantizer.dequantize_embeddings(&quantized);

        assert_eq!(dequantized.len(), 1);

        // Values should be close to original
        let original = &embeddings["e1"];
        let recovered = &dequantized["e1"];

        for i in 0..original.len() {
            let error = (original[i] - recovered[i]).abs();
            // Quantization error should be small but non-zero
            assert!(error < 1.0);
        }
    }

    #[test]
    fn test_compression_ratio() {
        let mut embeddings = HashMap::new();
        for i in 0..100 {
            let emb = Array1::from_vec(vec![i as f32; 128]);
            embeddings.insert(format!("e{}", i), emb);
        }

        let config = QuantizationConfig::default();
        let mut quantizer = ModelQuantizer::new(config);

        quantizer
            .quantize_embeddings(&embeddings)
            .expect("should succeed");

        // Int8 should give ~4x compression (32-bit to 8-bit)
        assert!(quantizer.stats.compression_ratio > 3.0);
        assert!(quantizer.stats.compression_ratio < 5.0);
    }
}