scirs2-linalg 0.4.2

Linear algebra module for SciRS2 (scirs2-linalg)
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
//! Example demonstrating neural network quantization using helper utilities
//!
//! This example shows how to use the neural network-specific helper functions
//! for quantizing different components of a neural network.

use scirs2_core::ndarray::{Array2, ArrayView2};
use scirs2_core::random::prelude::*;
use scirs2_core::random::{Distribution, Normal};
use scirs2_linalg::prelude::*;

// Simple struct to represent a neural network layer
struct SimpleLayer {
    weights: Array2<f32>,
    biases: Array2<f32>,
    name: String,
}

#[allow(dead_code)]
fn main() {
    println!("Neural Network Quantization Example");
    println!("===================================\n");

    // Create a simple example network (2 layers)
    println!("Creating example neural network...");
    let network = create_example_network();

    // Show original network behavior
    println!("\nRunning inference with full precision...");
    let test_input = create_test_input(5, 32);
    let full_precision_output = run_network_full_precision(&network, &test_input);

    // Quantize network
    println!("\nQuantizing network...");
    let (quantized_network, quantization_params) = quantize_network(&network, 8);

    // Run inference with quantized network
    println!("\nRunning inference with quantized network...");
    let quantized_output = run_network_quantized(
        &network,
        &quantized_network,
        &quantization_params,
        &test_input,
    );

    // Compare results
    println!("\nComparing results...");
    compare_outputs(&full_precision_output, &quantized_output);

    // Demonstrate per-layer bit width selection
    println!("\nDemonstrating mixed precision quantization (per-layer bit width selection)...");
    mixed_precision_quantization(&network, &test_input);
}

/// Create a simple example network with 2 layers
#[allow(dead_code)]
fn create_example_network() -> Vec<SimpleLayer> {
    let mut rng = thread_rng();

    // Create first layer (32 input features -> 64 hidden features)
    // Use Kaiming/He initialization for weights (assuming ReLU activation)
    let std_dev1 = (2.0 / 32.0).sqrt();
    let mut weights1 = Array2::zeros((64, 32));
    for i in 0..weights1.dim().0 {
        for j in 0..weights1.dim().1 {
            weights1[[i, j]] = Normal::new(0.0, std_dev1)
                .expect("Operation failed")
                .sample(&mut rng);
        }
    }
    let biases1 = Array2::zeros((64, 1));

    // Create second layer (64 hidden features -> 10 output features)
    let std_dev2 = (2.0 / 64.0).sqrt();
    let mut weights2 = Array2::zeros((10, 64));
    for i in 0..weights2.dim().0 {
        for j in 0..weights2.dim().1 {
            weights2[[i, j]] = Normal::new(0.0, std_dev2)
                .expect("Operation failed")
                .sample(&mut rng);
        }
    }
    let mut biases2 = Array2::zeros((10, 1));
    // Initialize biases with small values
    for i in 0..biases2.dim().0 {
        biases2[[i, 0]] = 0.01
            * Normal::new(0.0, 1.0)
                .expect("Operation failed")
                .sample(&mut rng);
    }

    // Return the network
    vec![
        SimpleLayer {
            weights: weights1,
            biases: biases1,
            name: "hidden_layer".to_string(),
        },
        SimpleLayer {
            weights: weights2,
            biases: biases2,
            name: "output_layer".to_string(),
        },
    ]
}

/// Create test input data
#[allow(dead_code)]
fn create_test_input(_batchsize: usize, inputsize: usize) -> Array2<f32> {
    let mut rng = thread_rng();
    let mut input = Array2::zeros((_batchsize, inputsize));

    for i in 0.._batchsize {
        for j in 0..inputsize {
            input[[i, j]] = Normal::new(0.0, 1.0)
                .expect("Operation failed")
                .sample(&mut rng);
        }
    }

    input
}

/// ReLU activation function
#[allow(dead_code)]
fn relu(x: &ArrayView2<f32>) -> Array2<f32> {
    x.mapv(|v| if v > 0.0 { v } else { 0.0 })
}

/// Run inference with full precision network
#[allow(dead_code)]
fn run_network_full_precision(network: &[SimpleLayer], input: &Array2<f32>) -> Array2<f32> {
    // First layer
    let layer1 = &network[0];
    let hidden = input.dot(&layer1.weights.t());
    let hidden_bias = &hidden + &layer1.biases.t(); // Add biases
    let hidden_activated = relu(&hidden_bias.view());

    // Second layer
    let layer2 = &network[1];
    let output = hidden_activated.dot(&layer2.weights.t());
    let output_bias = &output + &layer2.biases.t(); // Add biases

    output_bias
}

type QuantizedLayerPair = (QuantizedMatrix, QuantizedMatrix);
type QuantizationParamsPair = (QuantizationParams, QuantizationParams);

/// Quantize a neural network
#[allow(dead_code)]
fn quantize_network(
    network: &[SimpleLayer],
    bits: u8,
) -> (Vec<QuantizedLayerPair>, Vec<QuantizationParamsPair>) {
    let mut quantized_layers = Vec::new();
    let mut quantization_params = Vec::new();

    for (i, layer) in network.iter().enumerate() {
        println!("Quantizing layer {}: {}", i, layer.name);

        // Create calibration configs for weights and biases
        let weights_config = CalibrationConfig {
            method: CalibrationMethod::MinMax,
            symmetric: true,
            ..CalibrationConfig::default()
        };
        let bias_config = CalibrationConfig {
            method: CalibrationMethod::MinMax,
            symmetric: false, // Biases can be asymmetric
            ..CalibrationConfig::default()
        };

        // Quantize weights
        println!("  Weights shape: {:?}", layer.weights.dim());
        let weights_params = calibrate_matrix(&layer.weights.view(), bits, &weights_config)
            .expect("Operation failed");
        let (quantized_weights, _) =
            quantize_matrix(&layer.weights.view(), bits, weights_params.method);

        // Quantize biases
        println!("  Biases shape: {:?}", layer.biases.dim());
        let bias_params =
            calibrate_matrix(&layer.biases.view(), bits, &bias_config).expect("Operation failed");
        let (quantized_biases, _) = quantize_matrix(&layer.biases.view(), bits, bias_params.method);

        // Save quantized weights and biases
        quantized_layers.push((quantized_weights, quantized_biases));
        quantization_params.push((weights_params.clone(), bias_params));

        // Print quantization statistics
        println!("  Weight scale: {:.6}", weights_params.scale);
        if let Some(channel_scales) = &weights_params.channel_scales {
            let min_scale = channel_scales.iter().fold(f32::MAX, |a, &b| a.min(b));
            let max_scale = channel_scales.iter().fold(f32::MIN, |a, &b| a.max(b));
            println!(
                "  Per-channel scales: min={:.6}, max={:.6}",
                min_scale, max_scale
            );
        }
    }

    // Return the quantized layers and parameters
    (quantized_layers, quantization_params)
}

/// Run inference with quantized network
#[allow(dead_code)]
fn run_network_quantized(
    network: &[SimpleLayer],
    quantized_network: &[(QuantizedMatrix, QuantizedMatrix)],
    quantization_params: &[(QuantizationParams, QuantizationParams)],
    input: &Array2<f32>,
) -> Array2<f32> {
    // First layer
    let _layer = &network[0];
    let (q_weights, q_biases) = &quantized_network[0];
    let (weights_params, bias_params) = &quantization_params[0];

    // For activations, we need to quantize the input
    let activation_config = CalibrationConfig {
        method: CalibrationMethod::MinMax,
        symmetric: false,
        ..CalibrationConfig::default()
    };
    let act_params =
        calibrate_matrix(&input.view(), 8, &activation_config).expect("Operation failed");
    let (q_input, q_input_params) = quantize_matrix(&input.view(), 8, act_params.method);

    // Perform quantized matrix multiplication for first layer
    let hidden = match quantized_matmul(q_weights, weights_params, &q_input, &q_input_params) {
        Ok(result) => result,
        Err(e) => {
            println!("Error in quantized matmul: {:?}", e);
            // Fallback to dequantized computation
            let dq_weights = dequantize_matrix(q_weights, weights_params);
            let dq_input = dequantize_matrix(&q_input, &q_input_params);
            dq_input.dot(&dq_weights.t())
        }
    };

    // Add biases (dequantize biases)
    let dq_biases = dequantize_matrix(q_biases, bias_params);
    let hidden_bias = &hidden + &dq_biases.t();

    // Apply ReLU activation
    let hidden_activated = relu(&hidden_bias.view());

    // For second layer, quantize the activations from first layer
    let hidden_config = CalibrationConfig {
        method: CalibrationMethod::MinMax,
        symmetric: false, // ReLU output is non-negative
        ..CalibrationConfig::default()
    };
    let hidden_params =
        calibrate_matrix(&hidden_activated.view(), 8, &hidden_config).expect("Operation failed");
    let (q_hidden, q_hidden_params) =
        quantize_matrix(&hidden_activated.view(), 8, hidden_params.method);

    // Second layer
    let (q_weights2, q_biases2) = &quantized_network[1];
    let (weights_params2, bias_params2) = &quantization_params[1];

    // Perform quantized matrix multiplication for second layer
    let output = match quantized_matmul(q_weights2, weights_params2, &q_hidden, &q_hidden_params) {
        Ok(result) => result,
        Err(e) => {
            println!("Error in quantized matmul: {:?}", e);
            // Fallback to dequantized computation
            let dq_weights = dequantize_matrix(q_weights2, weights_params2);
            let dq_hidden = dequantize_matrix(&q_hidden, &q_hidden_params);
            dq_hidden.dot(&dq_weights.t())
        }
    };

    // Add biases (dequantize biases)
    let dq_biases2 = dequantize_matrix(q_biases2, bias_params2);
    let output_bias = &output + &dq_biases2.t();

    output_bias
}

/// Compare outputs from full precision and quantized networks
#[allow(dead_code)]
fn compare_outputs(full_precision: &Array2<f32>, quantized: &Array2<f32>) {
    // Calculate MSE
    let mse = (full_precision - quantized).mapv(|x| x * x).sum() / full_precision.len() as f32;

    // Calculate max absolute error
    let max_error = (full_precision - quantized)
        .mapv(|x| x.abs())
        .fold(0.0f32, |a, &b| a.max(b));

    // Calculate relative error
    let rel_error = (full_precision - quantized).mapv(|x| x.abs()).sum()
        / full_precision.mapv(|x| x.abs()).sum()
        * 100.0;

    println!("Mean Squared Error: {:.6}", mse);
    println!("Maximum Absolute Error: {:.6}", max_error);
    println!("Relative Error: {:.6}%", rel_error);

    // For classification tasks, check if top predictions match
    let batchsize = full_precision.dim().0;
    let mut top1_matches = 0;

    for i in 0..batchsize {
        let mut fp_max_idx = 0;
        let mut fp_max_val = full_precision[[i, 0]];
        let mut q_max_idx = 0;
        let mut q_max_val = quantized[[i, 0]];

        for j in 1..full_precision.dim().1 {
            if full_precision[[i, j]] > fp_max_val {
                fp_max_val = full_precision[[i, j]];
                fp_max_idx = j;
            }
            if quantized[[i, j]] > q_max_val {
                q_max_val = quantized[[i, j]];
                q_max_idx = j;
            }
        }

        if fp_max_idx == q_max_idx {
            top1_matches += 1;
        }
    }

    println!(
        "Top-1 Accuracy Match: {}/{} ({:.1}%)",
        top1_matches,
        batchsize,
        (top1_matches as f32 / batchsize as f32) * 100.0
    );
}

/// Demonstrate mixed precision quantization with different bit widths per layer
#[allow(dead_code)]
fn mixed_precision_quantization(network: &[SimpleLayer], input: &Array2<f32>) {
    // Define quantization bit widths for each layer
    // First layer: weights=8-bit, activations=8-bit
    // Second layer: weights=4-bit, activations=8-bit
    let layer_configs = [
        (8, 8, "First layer (8-bit weights, 8-bit activations)"),
        (4, 8, "Second layer (4-bit weights, 8-bit activations)"),
    ];

    println!("Layer configuration:");
    for (i, &(w_bits, a_bits, desc)) in layer_configs.iter().enumerate() {
        println!("  Layer {}: {}", i, desc);
    }

    // Run full precision network first for reference
    let full_precision_output = run_network_full_precision(network, input);

    // Quantize and run network with mixed precision
    let mut quantized_layers = Vec::new();
    let mut quantization_params = Vec::new();

    // Quantize each layer with its specific bit width
    for (i, (layer, &(w_bits, _a_bits, _desc))) in
        network.iter().zip(layer_configs.iter()).enumerate()
    {
        let weights_config = CalibrationConfig {
            method: CalibrationMethod::MinMax,
            symmetric: true,
            ..CalibrationConfig::default()
        };
        let bias_config = CalibrationConfig {
            method: CalibrationMethod::MinMax,
            symmetric: false,
            ..CalibrationConfig::default()
        };

        // Quantize weights
        let weights_params = calibrate_matrix(&layer.weights.view(), w_bits, &weights_config)
            .expect("Operation failed");
        let quantized_weights =
            quantize_matrix(&layer.weights.view(), w_bits, weights_params.method);

        // Quantize biases (typically keep biases at higher precision)
        let bias_params =
            calibrate_matrix(&layer.biases.view(), 8, &bias_config).expect("Operation failed");
        let quantized_biases = quantize_matrix(&layer.biases.view(), 8, bias_params.method);

        quantized_layers.push((quantized_weights, quantized_biases));
        quantization_params.push((weights_params.clone(), bias_params));

        println!(
            "Layer {} quantized with weights at {}-bit, params scale: {:.6}",
            i, w_bits, weights_params.scale
        );
    }

    // First layer forward pass
    let layer0 = &network[0];
    let (q_weights0, q_biases0) = &quantized_layers[0];
    let (weights_params0, bias_params0) = &quantization_params[0];
    let (_, a_bits0, _) = layer_configs[0];

    // Quantize input
    let activation_config = CalibrationConfig {
        method: CalibrationMethod::MinMax,
        symmetric: false,
        ..CalibrationConfig::default()
    };
    let act_params =
        calibrate_matrix(&input.view(), a_bits0, &activation_config).expect("Operation failed");
    let (q_input, q_input_params) = quantize_matrix(&input.view(), a_bits0, act_params.method);

    // First layer quantized matmul
    let hidden = match quantized_matmul(&q_weights0.0, &q_weights0.1, &q_input, &q_input_params) {
        Ok(result) => result,
        Err(_) => {
            // Fallback to dequantized computation
            let dq_weights = dequantize_matrix(&q_weights0.0, &q_weights0.1);
            let dq_input = dequantize_matrix(&q_input, &q_input_params);
            dq_input.dot(&dq_weights.t())
        }
    };

    // Add biases and apply ReLU
    let dq_biases0 = dequantize_matrix(&q_biases0.0, &q_biases0.1);
    let hidden_bias = &hidden + &dq_biases0.t();
    let hidden_activated = relu(&hidden_bias.view());

    // Quantize activations for second layer
    let (_, a_bits1, _) = layer_configs[1];
    let hidden_config = CalibrationConfig {
        method: CalibrationMethod::MinMax,
        symmetric: false,
        ..CalibrationConfig::default()
    };
    let hidden_params = calibrate_matrix(&hidden_activated.view(), a_bits1, &hidden_config)
        .expect("Operation failed");
    let (q_hidden, q_hidden_params) =
        quantize_matrix(&hidden_activated.view(), a_bits1, hidden_params.method);

    // Second layer forward pass
    let (q_weights1, q_biases1) = &quantized_layers[1];
    let (weights_params1, bias_params1) = &quantization_params[1];

    // Second layer quantized matmul
    let output = match quantized_matmul(&q_weights1.0, &q_weights1.1, &q_hidden, &q_hidden_params) {
        Ok(result) => result,
        Err(_) => {
            // Fallback to dequantized computation
            let dq_weights = dequantize_matrix(&q_weights1.0, &q_weights1.1);
            let dq_hidden = dequantize_matrix(&q_hidden, &q_hidden_params);
            dq_hidden.dot(&dq_weights.t())
        }
    };

    // Add biases
    let dq_biases1 = dequantize_matrix(&q_biases1.0, &q_biases1.1);
    let output_bias = &output + &dq_biases1.t();

    // Compare with full precision
    println!("\nMixed precision quantization results:");
    compare_outputs(&full_precision_output, &output_bias);

    // Calculate memory footprint reduction
    let fp32_weightsize = (layer0.weights.len()
        + layer0.biases.len()
        + network[1].weights.len()
        + network[1].biases.len())
        * 4; // 4 bytes per f32

    let mixed_weightsize = (layer0.weights.len() * layer_configs[0].0 as usize / 8) + // First layer weights
        (layer0.biases.len() * 8 / 8) + // First layer biases (8-bit)
        (network[1].weights.len() * layer_configs[1].0 as usize / 8) + // Second layer weights
        (network[1].biases.len() * 8 / 8); // Second layer biases (8-bit)

    let memory_reduction = (1.0 - (mixed_weightsize as f32 / fp32_weightsize as f32)) * 100.0;

    println!("\nMemory footprint:");
    println!("  Full precision: {} bytes", fp32_weightsize);
    println!("  Mixed precision: {} bytes", mixed_weightsize);
    println!("  Reduction: {:.1}%", memory_reduction);
}