torsh-backend 0.1.2

Backend abstraction layer for ToRSh
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
//! Quantization operations trait and basic implementations
//!
//! This module defines the core QuantizationOps trait that provides the interface
//! for all quantization operations, including quantization/dequantization,
//! quantized arithmetic, and calibration. It also includes basic CPU implementations
//! and helper functions for common quantization operations.

use super::params::QuantizationParams;
use super::tensor::QuantizedTensor;
use super::types::{QuantizationScheme, QuantizedDType};
use crate::BackendResult;

#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

/// Quantization operations trait
///
/// Defines the interface for quantization operations that can be implemented
/// by different backends (CPU, GPU, specialized accelerators). This trait
/// provides both low-level operations (quantize/dequantize) and high-level
/// operations (quantized arithmetic and neural network operations).
pub trait QuantizationOps {
    /// Quantize floating-point data to quantized representation
    ///
    /// Converts an array of floating-point values to quantized integers
    /// using the provided quantization parameters. The output format
    /// depends on the quantization type and may pack multiple values
    /// per byte for sub-byte quantization.
    ///
    /// # Arguments
    ///
    /// * `input` - Floating-point input data
    /// * `params` - Quantization parameters (scale, zero_point, etc.)
    ///
    /// # Returns
    ///
    /// Returns quantized data as a byte vector, or an error if
    /// quantization fails.
    ///
    /// # Examples
    ///
    /// ```
    /// # use torsh_backend::quantization::{QuantizationOps, QuantizationParams, CpuQuantizationOps};
    /// let ops = CpuQuantizationOps::new();
    /// let input = vec![0.0, 1.0, 2.0, 3.0];
    /// let params = QuantizationParams::int8_symmetric();
    /// let quantized = ops.quantize_f32(&input, &params).unwrap();
    /// ```
    fn quantize_f32(&self, input: &[f32], params: &QuantizationParams) -> BackendResult<Vec<u8>>;

    /// Dequantize quantized data back to floating-point
    ///
    /// Converts quantized integer data back to floating-point values
    /// using the provided quantization parameters. This is the inverse
    /// operation of `quantize_f32`.
    ///
    /// # Arguments
    ///
    /// * `input` - Quantized input data as bytes
    /// * `params` - Quantization parameters used for the original quantization
    ///
    /// # Returns
    ///
    /// Returns floating-point data, or an error if dequantization fails.
    ///
    /// # Examples
    ///
    /// ```
    /// # use torsh_backend::quantization::{QuantizationOps, QuantizationParams, CpuQuantizationOps};
    /// let ops = CpuQuantizationOps::new();
    /// let quantized = vec![0, 64, 128, 192];
    /// let params = QuantizationParams::uint8_asymmetric();
    /// let dequantized = ops.dequantize_f32(&quantized, &params).unwrap();
    /// ```
    fn dequantize_f32(&self, input: &[u8], params: &QuantizationParams) -> BackendResult<Vec<f32>>;

    /// Quantized matrix multiplication
    ///
    /// Performs matrix multiplication on two quantized tensors, returning
    /// a quantized result. This operation may require requantization to
    /// handle the increased precision of the multiplication result.
    ///
    /// # Arguments
    ///
    /// * `a` - Left operand quantized tensor
    /// * `b` - Right operand quantized tensor
    ///
    /// # Returns
    ///
    /// Returns the quantized matrix multiplication result.
    fn qmatmul(&self, a: &QuantizedTensor, b: &QuantizedTensor) -> BackendResult<QuantizedTensor>;

    /// Quantized 2D convolution
    ///
    /// Performs 2D convolution with quantized input, weights, and optional bias.
    /// This is a core operation for quantized convolutional neural networks.
    ///
    /// # Arguments
    ///
    /// * `input` - Input feature maps (quantized)
    /// * `weight` - Convolution weights (quantized)
    /// * `bias` - Optional bias terms (quantized)
    /// * `stride` - Convolution stride (height, width)
    /// * `padding` - Convolution padding (height, width)
    ///
    /// # Returns
    ///
    /// Returns the quantized convolution result.
    fn qconv2d(
        &self,
        input: &QuantizedTensor,
        weight: &QuantizedTensor,
        bias: Option<&QuantizedTensor>,
        stride: (usize, usize),
        padding: (usize, usize),
    ) -> BackendResult<QuantizedTensor>;

    /// Quantized element-wise addition
    ///
    /// Performs element-wise addition of two quantized tensors.
    /// This may require rescaling if the tensors have different
    /// quantization parameters.
    ///
    /// # Arguments
    ///
    /// * `a` - First operand
    /// * `b` - Second operand
    ///
    /// # Returns
    ///
    /// Returns the quantized addition result.
    fn qadd(&self, a: &QuantizedTensor, b: &QuantizedTensor) -> BackendResult<QuantizedTensor>;

    /// Quantized ReLU activation
    ///
    /// Applies ReLU activation to a quantized tensor. For certain
    /// quantization schemes, this can be implemented very efficiently
    /// by simply clamping values to the zero point.
    ///
    /// # Arguments
    ///
    /// * `input` - Input quantized tensor
    ///
    /// # Returns
    ///
    /// Returns the tensor after applying ReLU activation.
    fn qrelu(&self, input: &QuantizedTensor) -> BackendResult<QuantizedTensor>;

    /// Calibrate quantization parameters from sample data
    ///
    /// Analyzes sample data to determine optimal quantization parameters.
    /// This is typically used for post-training quantization to find
    /// parameters that minimize quantization error.
    ///
    /// # Arguments
    ///
    /// * `samples` - Array of sample data arrays
    /// * `target_dtype` - Target quantization data type
    ///
    /// # Returns
    ///
    /// Returns calibrated quantization parameters.
    fn calibrate(
        &self,
        samples: &[&[f32]],
        target_dtype: QuantizedDType,
    ) -> BackendResult<QuantizationParams>;
}

/// CPU-based quantization operations implementation
///
/// Provides a reference implementation of quantization operations
/// using CPU-only code. This serves as a fallback when specialized
/// hardware acceleration is not available.
#[derive(Debug, Clone, Default)]
pub struct CpuQuantizationOps;

impl CpuQuantizationOps {
    /// Create a new CPU quantization operations instance
    pub fn new() -> Self {
        Self
    }

    /// Helper function to quantize a single value
    fn quantize_value(
        &self,
        value: f32,
        scale: f32,
        zero_point: i32,
        dtype: &QuantizedDType,
    ) -> i32 {
        let (qmin, qmax) = dtype.value_range();
        let quantized = (value / scale).round() + zero_point as f32;
        quantized.clamp(qmin as f32, qmax as f32) as i32
    }

    /// Helper function to dequantize a single value
    fn dequantize_value(&self, quantized: i32, scale: f32, zero_point: i32) -> f32 {
        scale * (quantized - zero_point) as f32
    }

    /// Pack sub-byte values into bytes
    fn pack_sub_byte(&self, values: &[i32], dtype: &QuantizedDType) -> Vec<u8> {
        match dtype {
            QuantizedDType::Int4 | QuantizedDType::UInt4 => {
                let mut result = Vec::new();
                for chunk in values.chunks(2) {
                    let val1 = chunk[0] as u8 & 0x0F;
                    let val2 = if chunk.len() > 1 {
                        (chunk[1] as u8 & 0x0F) << 4
                    } else {
                        0
                    };
                    result.push(val1 | val2);
                }
                result
            }
            QuantizedDType::Binary => {
                let mut result = Vec::new();
                for chunk in values.chunks(8) {
                    let mut byte = 0u8;
                    for (i, &val) in chunk.iter().enumerate() {
                        if val != 0 {
                            byte |= 1 << i;
                        }
                    }
                    result.push(byte);
                }
                result
            }
            _ => {
                // For other types, just cast to bytes
                values.iter().map(|&v| v as u8).collect()
            }
        }
    }

    /// Unpack sub-byte values from bytes
    fn unpack_sub_byte(
        &self,
        data: &[u8],
        num_elements: usize,
        dtype: &QuantizedDType,
    ) -> Vec<i32> {
        match dtype {
            QuantizedDType::Int4 => {
                let mut result = Vec::new();
                for &byte in data {
                    if result.len() < num_elements {
                        let val1 = ((byte & 0x0F) as i8) << 4 >> 4; // Sign extend
                        result.push(val1 as i32);
                    }
                    if result.len() < num_elements {
                        let val2 = ((byte & 0xF0) as i8) >> 4; // Already sign extended
                        result.push(val2 as i32);
                    }
                }
                result.truncate(num_elements);
                result
            }
            QuantizedDType::UInt4 => {
                let mut result = Vec::new();
                for &byte in data {
                    if result.len() < num_elements {
                        result.push((byte & 0x0F) as i32);
                    }
                    if result.len() < num_elements {
                        result.push(((byte & 0xF0) >> 4) as i32);
                    }
                }
                result.truncate(num_elements);
                result
            }
            QuantizedDType::Binary => {
                let mut result = Vec::new();
                for &byte in data {
                    for bit in 0..8 {
                        if result.len() < num_elements {
                            result.push(if (byte >> bit) & 1 != 0 { 1 } else { 0 });
                        }
                    }
                }
                result.truncate(num_elements);
                result
            }
            _ => {
                // For other types, just cast from bytes
                data.iter().take(num_elements).map(|&b| b as i32).collect()
            }
        }
    }
}

impl QuantizationOps for CpuQuantizationOps {
    fn quantize_f32(&self, input: &[f32], params: &QuantizationParams) -> BackendResult<Vec<u8>> {
        if input.is_empty() {
            return Ok(Vec::new());
        }

        // Validate parameters
        params.validate()?;

        // Handle per-channel quantization
        let num_channels = params.scale.len();
        let channel_size = if num_channels > 1 {
            input.len() / num_channels
        } else {
            input.len()
        };

        let mut quantized_values = Vec::with_capacity(input.len());

        match params.scheme {
            QuantizationScheme::ChannelWise if num_channels > 1 => {
                // Per-channel quantization
                for (channel, chunk) in input.chunks(channel_size).enumerate() {
                    let scale = params.scale[channel];
                    let zero_point = params.zero_point[channel];

                    for &value in chunk {
                        let q_val = self.quantize_value(value, scale, zero_point, &params.dtype);
                        quantized_values.push(q_val);
                    }
                }
            }
            _ => {
                // Tensor-wide quantization
                let scale = params.scale[0];
                let zero_point = params.zero_point[0];

                for &value in input {
                    let q_val = self.quantize_value(value, scale, zero_point, &params.dtype);
                    quantized_values.push(q_val);
                }
            }
        }

        // Pack into bytes based on data type
        let packed = if params.dtype.is_sub_byte() {
            self.pack_sub_byte(&quantized_values, &params.dtype)
        } else {
            match params.dtype {
                QuantizedDType::Int8 | QuantizedDType::UInt8 => {
                    quantized_values.iter().map(|&v| v as u8).collect()
                }
                QuantizedDType::Int16 | QuantizedDType::UInt16 => {
                    let mut result = Vec::new();
                    for &val in &quantized_values {
                        let bytes = (val as u16).to_le_bytes();
                        result.extend_from_slice(&bytes);
                    }
                    result
                }
                _ => {
                    return Err(torsh_core::error::TorshError::NotImplemented(format!(
                        "Quantization not implemented for {:?}",
                        params.dtype
                    )))
                }
            }
        };

        Ok(packed)
    }

    fn dequantize_f32(&self, input: &[u8], params: &QuantizationParams) -> BackendResult<Vec<f32>> {
        if input.is_empty() {
            return Ok(Vec::new());
        }

        // Validate parameters
        params.validate()?;

        // Determine number of elements
        let num_elements = match params.dtype {
            QuantizedDType::Int4 | QuantizedDType::UInt4 => input.len() * 2,
            QuantizedDType::Binary => input.len() * 8,
            QuantizedDType::Int8 | QuantizedDType::UInt8 => input.len(),
            QuantizedDType::Int16 | QuantizedDType::UInt16 => input.len() / 2,
            _ => {
                return Err(torsh_core::error::TorshError::NotImplemented(format!(
                    "Dequantization not implemented for {:?}",
                    params.dtype
                )))
            }
        };

        // Unpack quantized values
        let quantized_values = if params.dtype.is_sub_byte() {
            self.unpack_sub_byte(input, num_elements, &params.dtype)
        } else {
            match params.dtype {
                QuantizedDType::Int8 => input.iter().map(|&b| b as i8 as i32).collect(),
                QuantizedDType::UInt8 => input.iter().map(|&b| b as i32).collect(),
                QuantizedDType::Int16 => input
                    .chunks_exact(2)
                    .map(|chunk| i16::from_le_bytes([chunk[0], chunk[1]]) as i32)
                    .collect(),
                QuantizedDType::UInt16 => input
                    .chunks_exact(2)
                    .map(|chunk| u16::from_le_bytes([chunk[0], chunk[1]]) as i32)
                    .collect(),
                _ => unreachable!(),
            }
        };

        // Dequantize to floating point
        let num_channels = params.scale.len();
        let mut result = Vec::with_capacity(quantized_values.len());

        if num_channels > 1 && matches!(params.scheme, QuantizationScheme::ChannelWise) {
            // Per-channel dequantization
            let channel_size = quantized_values.len() / num_channels;
            for (channel, chunk) in quantized_values.chunks(channel_size).enumerate() {
                let scale = params.scale[channel];
                let zero_point = params.zero_point[channel];

                for &q_val in chunk {
                    let f_val = self.dequantize_value(q_val, scale, zero_point);
                    result.push(f_val);
                }
            }
        } else {
            // Tensor-wide dequantization
            let scale = params.scale[0];
            let zero_point = params.zero_point[0];

            for &q_val in &quantized_values {
                let f_val = self.dequantize_value(q_val, scale, zero_point);
                result.push(f_val);
            }
        }

        Ok(result)
    }

    fn qmatmul(
        &self,
        _a: &QuantizedTensor,
        _b: &QuantizedTensor,
    ) -> BackendResult<QuantizedTensor> {
        // Placeholder implementation
        Err(torsh_core::error::TorshError::NotImplemented(
            "Quantized matrix multiplication not yet implemented for CPU backend".to_string(),
        ))
    }

    fn qconv2d(
        &self,
        _input: &QuantizedTensor,
        _weight: &QuantizedTensor,
        _bias: Option<&QuantizedTensor>,
        _stride: (usize, usize),
        _padding: (usize, usize),
    ) -> BackendResult<QuantizedTensor> {
        // Placeholder implementation
        Err(torsh_core::error::TorshError::NotImplemented(
            "Quantized convolution not yet implemented for CPU backend".to_string(),
        ))
    }

    fn qadd(&self, _a: &QuantizedTensor, _b: &QuantizedTensor) -> BackendResult<QuantizedTensor> {
        // Placeholder implementation
        Err(torsh_core::error::TorshError::NotImplemented(
            "Quantized addition not yet implemented for CPU backend".to_string(),
        ))
    }

    fn qrelu(&self, _input: &QuantizedTensor) -> BackendResult<QuantizedTensor> {
        // Placeholder implementation
        Err(torsh_core::error::TorshError::NotImplemented(
            "Quantized ReLU not yet implemented for CPU backend".to_string(),
        ))
    }

    fn calibrate(
        &self,
        samples: &[&[f32]],
        target_dtype: QuantizedDType,
    ) -> BackendResult<QuantizationParams> {
        if samples.is_empty() {
            return Err(torsh_core::error::TorshError::InvalidArgument(
                "Cannot calibrate with empty samples".to_string(),
            ));
        }

        // Find global min and max across all samples
        let mut global_min = f32::INFINITY;
        let mut global_max = f32::NEG_INFINITY;

        for sample in samples {
            for &value in *sample {
                if value.is_finite() {
                    global_min = global_min.min(value);
                    global_max = global_max.max(value);
                }
            }
        }

        if !global_min.is_finite() || !global_max.is_finite() {
            return Err(torsh_core::error::TorshError::InvalidArgument(
                "No finite values found in calibration samples".to_string(),
            ));
        }

        // Create parameters and calculate from statistics
        let mut params = QuantizationParams {
            dtype: target_dtype,
            scheme: QuantizationScheme::Asymmetric,
            scale: vec![1.0],
            zero_point: vec![0],
            block_size: None,
            min_val: None,
            max_val: None,
        };

        params.from_statistics(global_min, global_max)?;
        Ok(params)
    }
}

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

    #[test]
    fn test_cpu_quantization_ops_creation() {
        let ops = CpuQuantizationOps::new();
        assert!(format!("{:?}", ops).contains("CpuQuantizationOps"));
    }

    #[test]
    fn test_quantize_dequantize_roundtrip() {
        let ops = CpuQuantizationOps::new();
        let input = vec![0.0, 1.0, -1.0, 2.0];
        let mut params = QuantizationParams::int8_symmetric();
        params
            .from_statistics(-2.0, 2.0)
            .expect("construction from statistics should succeed");

        let quantized = ops
            .quantize_f32(&input, &params)
            .expect("f32 quantization should succeed");
        let dequantized = ops
            .dequantize_f32(&quantized, &params)
            .expect("f32 dequantization should succeed");

        // Check that values are approximately preserved
        for (original, recovered) in input.iter().zip(dequantized.iter()) {
            assert!(
                (original - recovered).abs() < 0.1,
                "Original: {}, Recovered: {}",
                original,
                recovered
            );
        }
    }

    #[test]
    fn test_quantize_uint8() {
        let ops = CpuQuantizationOps::new();
        let input = vec![0.0, 0.5, 1.0];
        let mut params = QuantizationParams::uint8_asymmetric();
        params
            .from_statistics(0.0, 1.0)
            .expect("construction from statistics should succeed");

        let quantized = ops
            .quantize_f32(&input, &params)
            .expect("f32 quantization should succeed");
        assert_eq!(quantized.len(), 3);

        let dequantized = ops
            .dequantize_f32(&quantized, &params)
            .expect("f32 dequantization should succeed");
        assert_eq!(dequantized.len(), 3);
    }

    #[test]
    fn test_quantize_int4() {
        let ops = CpuQuantizationOps::new();
        let input = vec![0.0, 1.0, -1.0, 2.0]; // 4 elements
        let mut params = QuantizationParams::int4_symmetric();
        params
            .from_statistics(-2.0, 2.0)
            .expect("construction from statistics should succeed");

        let quantized = ops
            .quantize_f32(&input, &params)
            .expect("f32 quantization should succeed");
        assert_eq!(quantized.len(), 2); // 4 elements packed into 2 bytes

        let dequantized = ops
            .dequantize_f32(&quantized, &params)
            .expect("f32 dequantization should succeed");
        assert_eq!(dequantized.len(), 4);
    }

    #[test]
    fn test_calibrate() {
        let ops = CpuQuantizationOps::new();
        let sample1 = vec![0.0, 1.0, 2.0];
        let sample2 = vec![-1.0, 0.5, 3.0];
        let samples = vec![sample1.as_slice(), sample2.as_slice()];

        let params = ops
            .calibrate(&samples, QuantizedDType::Int8)
            .expect("calibration should succeed");
        assert_eq!(params.dtype, QuantizedDType::Int8);
        assert_eq!(params.min_val, Some(-1.0));
        assert_eq!(params.max_val, Some(3.0));
        assert!(params.scale[0] > 0.0);
    }

    #[test]
    fn test_calibrate_empty_samples() {
        let ops = CpuQuantizationOps::new();
        let samples: Vec<&[f32]> = vec![];

        let result = ops.calibrate(&samples, QuantizedDType::Int8);
        assert!(result.is_err());
    }

    #[test]
    fn test_unimplemented_operations() {
        let ops = CpuQuantizationOps::new();
        let params = QuantizationParams::default();
        let tensor = QuantizedTensor::new(
            vec![2, 2],
            params,
            crate::Device::cpu().expect("Quantized Tensor should succeed"),
        );

        assert!(ops.qmatmul(&tensor, &tensor).is_err());
        assert!(ops.qconv2d(&tensor, &tensor, None, (1, 1), (0, 0)).is_err());
        assert!(ops.qadd(&tensor, &tensor).is_err());
        assert!(ops.qrelu(&tensor).is_err());
    }

    #[test]
    fn test_quantize_value_helper() {
        let ops = CpuQuantizationOps::new();

        // Test symmetric int8 quantization
        let result = ops.quantize_value(1.0, 0.5, 0, &QuantizedDType::Int8);
        assert_eq!(result, 2); // 1.0 / 0.5 + 0 = 2

        // Test clamping
        let result = ops.quantize_value(1000.0, 1.0, 0, &QuantizedDType::Int8);
        assert_eq!(result, 127); // Clamped to max value
    }

    #[test]
    fn test_dequantize_value_helper() {
        let ops = CpuQuantizationOps::new();

        let result = ops.dequantize_value(2, 0.5, 0);
        assert_eq!(result, 1.0); // 0.5 * (2 - 0) = 1.0
    }
}