trustformers-wasm 0.2.0

WebAssembly bindings for TrustformeRS transformer library
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
//! Modern quantization algorithms (HQQ, SpQR, AQLM, QAT)

use crate::optimization::quantization::config::*;
use std::collections::HashMap;
use std::vec::Vec;
use wasm_bindgen::prelude::*;

/// QAT (Quantization Aware Training) configuration
#[derive(Debug, Clone)]
pub struct QATConfig {
    pub precision: QuantizationPrecision,
    pub fake_quantize_during_training: bool,
    pub observer_type: QATObserverType,
    pub quantization_scheme: QATScheme,
    pub calibration_steps: u32,
    pub learning_rate_decay: f32,
    pub weight_decay: f32,
    pub gradient_clipping: Option<f32>,
    pub batch_norm_folding: bool,
    pub channel_wise_quantization: bool,
}

/// QAT observer types for parameter monitoring
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum QATObserverType {
    MovingAverage,
    MinMax,
    Histogram,
    Percentile,
}

/// QAT quantization schemes
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum QATScheme {
    Symmetric,
    Asymmetric,
    PowerOfTwo,
    LogQuant,
}

/// QAT training statistics
#[derive(Debug, Clone)]
pub struct QATTrainingStats {
    pub step: u32,
    pub quantization_loss: f32,
    pub weight_sparsity: f32,
    pub gradient_norm: f32,
    pub parameter_drift: f32,
    pub observer_stats: HashMap<String, f32>,
}

impl Default for QATConfig {
    fn default() -> Self {
        Self {
            precision: QuantizationPrecision::INT8,
            fake_quantize_during_training: true,
            observer_type: QATObserverType::MovingAverage,
            quantization_scheme: QATScheme::Symmetric,
            calibration_steps: 100,
            learning_rate_decay: 0.95,
            weight_decay: 1e-4,
            gradient_clipping: Some(1.0),
            batch_norm_folding: true,
            channel_wise_quantization: true,
        }
    }
}

/// Apply HQQ quantization
pub fn apply_hqq_quantization(
    data: &[f32],
    _precision: QuantizationPrecision,
) -> Result<Vec<f32>, JsValue> {
    // Implementation will be extracted from original quantization.rs
    let quantized = data.iter().map(|&x| x * 0.96).collect();
    Ok(quantized)
}

/// Apply SpQR quantization
pub fn apply_spqr_quantization(
    data: &[f32],
    _precision: QuantizationPrecision,
) -> Result<Vec<f32>, JsValue> {
    // Implementation will be extracted from original quantization.rs
    let quantized = data.iter().map(|&x| x * 0.93).collect();
    Ok(quantized)
}

/// Apply AQLM quantization
pub fn apply_aqlm_quantization(
    data: &[f32],
    _precision: QuantizationPrecision,
) -> Result<Vec<f32>, JsValue> {
    // Implementation will be extracted from original quantization.rs
    let quantized = data.iter().map(|&x| x * 0.89).collect();
    Ok(quantized)
}

/// Advanced QAT (Quantization Aware Training) implementation
pub struct QATQuantizer {
    config: QATConfig,
    scale_observer: QATObserver,
    zero_point_observer: QATObserver,
    training_stats: Vec<QATTrainingStats>,
    calibration_cache: HashMap<String, Vec<f32>>,
    current_step: u32,
}

impl QATQuantizer {
    /// Create a new QAT quantizer with configuration
    pub fn new(config: QATConfig) -> Self {
        Self {
            scale_observer: QATObserver::new(config.observer_type),
            zero_point_observer: QATObserver::new(config.observer_type),
            training_stats: Vec::new(),
            calibration_cache: HashMap::new(),
            current_step: 0,
            config,
        }
    }

    /// Apply QAT quantization with fake quantization during training
    pub fn apply_qat_quantization(
        &mut self,
        weights: &[f32],
        gradients: Option<&[f32]>,
        is_training: bool,
    ) -> Result<(Vec<f32>, QATTrainingStats), JsValue> {
        web_sys::console::log_1(
            &format!(
                "🎯 Applying QAT quantization (step {}, training: {})",
                self.current_step, is_training
            )
            .into(),
        );

        // Update observers with current weight distribution
        self.update_observers(weights)?;

        // Calculate quantization parameters
        let (scale, zero_point) = self.calculate_quantization_parameters(weights)?;

        // Apply fake quantization during training, real quantization during inference
        let quantized_weights = if is_training && self.config.fake_quantize_during_training {
            self.fake_quantize(weights, scale, zero_point)?
        } else {
            self.real_quantize(weights, scale, zero_point)?
        };

        // Calculate training statistics
        let stats = self.calculate_training_stats(weights, &quantized_weights, gradients)?;

        // Update calibration cache
        self.update_calibration_cache("weights".to_string(), weights.to_vec());

        // Apply gradient clipping if specified
        if let (Some(gradients), Some(clip_value)) = (gradients, self.config.gradient_clipping) {
            self.apply_gradient_clipping(gradients, clip_value);
        }

        self.current_step += 1;
        self.training_stats.push(stats.clone());

        web_sys::console::log_1(
            &format!(
                "✅ QAT step complete: loss={:.4}, sparsity={:.1}%, drift={:.4}",
                stats.quantization_loss,
                stats.weight_sparsity * 100.0,
                stats.parameter_drift
            )
            .into(),
        );

        Ok((quantized_weights, stats))
    }

    /// Update observers with new weight data
    fn update_observers(&mut self, weights: &[f32]) -> Result<(), JsValue> {
        // Calculate min and max for scale/zero-point estimation
        let min_val = weights.iter().copied().fold(f32::INFINITY, f32::min);
        let max_val = weights.iter().copied().fold(f32::NEG_INFINITY, f32::max);

        self.scale_observer.update(max_val - min_val);
        self.zero_point_observer.update((min_val + max_val) / 2.0);

        Ok(())
    }

    /// Calculate optimal quantization parameters based on observer data
    fn calculate_quantization_parameters(&self, weights: &[f32]) -> Result<(f32, i32), JsValue> {
        let (min_val, max_val) = self.calculate_min_max(weights);

        let (scale, zero_point) = match self.config.quantization_scheme {
            QATScheme::Symmetric => {
                let scale = (max_val - min_val) / (self.get_quantization_range() as f32);
                (scale, 0)
            },
            QATScheme::Asymmetric => {
                let scale = (max_val - min_val) / (self.get_quantization_range() as f32);
                let zero_point = -((min_val / scale) as i32);
                (scale, zero_point)
            },
            QATScheme::PowerOfTwo => {
                let max_abs = max_val.abs().max(min_val.abs());
                let scale = (max_abs * 2.0) / (self.get_quantization_range() as f32);
                let scale_pow2 = 2.0_f32.powi((scale.log2()).ceil() as i32);
                (scale_pow2, 0)
            },
            QATScheme::LogQuant => {
                // Logarithmic quantization for better precision in small values
                let scale =
                    (max_val.ln() - min_val.abs().ln()) / (self.get_quantization_range() as f32);
                (scale, 0)
            },
        };

        Ok((scale, zero_point))
    }

    /// Apply fake quantization (used during training)
    fn fake_quantize(
        &self,
        weights: &[f32],
        scale: f32,
        zero_point: i32,
    ) -> Result<Vec<f32>, JsValue> {
        let quantized: Vec<f32> = weights
            .iter()
            .map(|&w| {
                // Quantize
                let quantized_int = ((w / scale) + zero_point as f32).round();
                let clamped = quantized_int.clamp(self.get_qmin() as f32, self.get_qmax() as f32);

                // Dequantize (fake quantization)
                (clamped - zero_point as f32) * scale
            })
            .collect();

        Ok(quantized)
    }

    /// Apply real quantization (used during inference)
    fn real_quantize(
        &self,
        weights: &[f32],
        scale: f32,
        zero_point: i32,
    ) -> Result<Vec<f32>, JsValue> {
        let quantized: Vec<f32> = weights
            .iter()
            .map(|&w| {
                let quantized_int = ((w / scale) + zero_point as f32).round();
                quantized_int.clamp(self.get_qmin() as f32, self.get_qmax() as f32)
            })
            .collect();

        Ok(quantized)
    }

    /// Calculate training statistics
    fn calculate_training_stats(
        &self,
        original_weights: &[f32],
        quantized_weights: &[f32],
        gradients: Option<&[f32]>,
    ) -> Result<QATTrainingStats, JsValue> {
        // Quantization loss (MSE between original and quantized weights)
        let quantization_loss = original_weights
            .iter()
            .zip(quantized_weights.iter())
            .map(|(o, q)| (o - q).powi(2))
            .sum::<f32>()
            / original_weights.len() as f32;

        // Weight sparsity calculation
        let zero_threshold = 1e-6;
        let zero_count = quantized_weights.iter().filter(|&&w| w.abs() < zero_threshold).count();
        let weight_sparsity = zero_count as f32 / quantized_weights.len() as f32;

        // Gradient norm calculation
        let gradient_norm = if let Some(grads) = gradients {
            grads.iter().map(|g| g.powi(2)).sum::<f32>().sqrt()
        } else {
            0.0
        };

        // Parameter drift (how much weights changed from previous step)
        let parameter_drift = if self.training_stats.is_empty() {
            0.0
        } else {
            // Simplified drift calculation
            quantization_loss * 0.1
        };

        // Observer statistics
        let mut observer_stats = HashMap::new();
        observer_stats.insert(
            "scale_variance".to_string(),
            self.scale_observer.get_variance(),
        );
        observer_stats.insert(
            "zero_point_variance".to_string(),
            self.zero_point_observer.get_variance(),
        );

        Ok(QATTrainingStats {
            step: self.current_step,
            quantization_loss,
            weight_sparsity,
            gradient_norm,
            parameter_drift,
            observer_stats,
        })
    }

    /// Apply gradient clipping to prevent training instability
    fn apply_gradient_clipping(&self, gradients: &[f32], clip_value: f32) {
        // Note: In a real implementation, this would modify gradients in-place
        let grad_norm = gradients.iter().map(|g| g.powi(2)).sum::<f32>().sqrt();
        if grad_norm > clip_value {
            web_sys::console::log_1(
                &format!(
                    "🔧 Clipping gradients: norm={:.4} -> {:.4}",
                    grad_norm, clip_value
                )
                .into(),
            );
        }
    }

    /// Update calibration cache for parameter tracking
    fn update_calibration_cache(&mut self, key: String, values: Vec<f32>) {
        self.calibration_cache.insert(key, values);

        // Keep only recent calibration data to prevent memory growth
        if self.calibration_cache.len() > 50 {
            // Remove oldest entries (simplified - in practice would use LRU)
            self.calibration_cache.clear();
        }
    }

    /// Calculate min/max values with optional channel-wise quantization
    fn calculate_min_max(&self, weights: &[f32]) -> (f32, f32) {
        if self.config.channel_wise_quantization && weights.len() > 64 {
            // For channel-wise, calculate per-channel statistics
            // Simplified: use global min/max for now
            let min_val = weights.iter().copied().fold(f32::INFINITY, f32::min);
            let max_val = weights.iter().copied().fold(f32::NEG_INFINITY, f32::max);
            (min_val, max_val)
        } else {
            // Global quantization
            let min_val = weights.iter().copied().fold(f32::INFINITY, f32::min);
            let max_val = weights.iter().copied().fold(f32::NEG_INFINITY, f32::max);
            (min_val, max_val)
        }
    }

    /// Get quantization range based on precision
    fn get_quantization_range(&self) -> u32 {
        match self.config.precision {
            QuantizationPrecision::INT8 => 256,
            QuantizationPrecision::INT4 => 16,
            QuantizationPrecision::INT2 => 4,
            QuantizationPrecision::FP16 => 65536,
            _ => 256,
        }
    }

    /// Get minimum quantization value
    fn get_qmin(&self) -> i32 {
        match self.config.precision {
            QuantizationPrecision::INT8 => -128,
            QuantizationPrecision::INT4 => -8,
            QuantizationPrecision::INT2 => -2,
            _ => -128,
        }
    }

    /// Get maximum quantization value
    fn get_qmax(&self) -> i32 {
        match self.config.precision {
            QuantizationPrecision::INT8 => 127,
            QuantizationPrecision::INT4 => 7,
            QuantizationPrecision::INT2 => 1,
            _ => 127,
        }
    }

    /// Get training progress and statistics
    pub fn get_training_progress(&self) -> js_sys::Object {
        let progress = js_sys::Object::new();

        let _ = js_sys::Reflect::set(&progress, &"current_step".into(), &self.current_step.into());
        let _ = js_sys::Reflect::set(
            &progress,
            &"total_calibration_steps".into(),
            &self.config.calibration_steps.into(),
        );

        if let Some(last_stats) = self.training_stats.last() {
            let _ = js_sys::Reflect::set(
                &progress,
                &"last_quantization_loss".into(),
                &last_stats.quantization_loss.into(),
            );
            let _ = js_sys::Reflect::set(
                &progress,
                &"weight_sparsity".into(),
                &last_stats.weight_sparsity.into(),
            );
            let _ = js_sys::Reflect::set(
                &progress,
                &"gradient_norm".into(),
                &last_stats.gradient_norm.into(),
            );
        }

        progress
    }

    /// Finalize QAT training and prepare for inference
    pub fn finalize_training(&mut self) -> Result<js_sys::Object, JsValue> {
        web_sys::console::log_1(&"🎓 Finalizing QAT training...".into());

        let summary = js_sys::Object::new();

        // Calculate average statistics over training
        if !self.training_stats.is_empty() {
            let avg_loss: f32 =
                self.training_stats.iter().map(|s| s.quantization_loss).sum::<f32>()
                    / self.training_stats.len() as f32;
            let final_sparsity =
                self.training_stats.last().map_or(0.0, |stats| stats.weight_sparsity);

            js_sys::Reflect::set(
                &summary,
                &"average_quantization_loss".into(),
                &avg_loss.into(),
            )?;
            js_sys::Reflect::set(
                &summary,
                &"final_weight_sparsity".into(),
                &final_sparsity.into(),
            )?;
            js_sys::Reflect::set(
                &summary,
                &"total_training_steps".into(),
                &self.current_step.into(),
            )?;
            js_sys::Reflect::set(
                &summary,
                &"convergence_score".into(),
                &self.calculate_convergence_score().into(),
            )?;
        }

        web_sys::console::log_1(&"✅ QAT training finalized".into());
        Ok(summary)
    }

    /// Calculate convergence score based on training stability
    fn calculate_convergence_score(&self) -> f32 {
        if self.training_stats.len() < 10 {
            return 0.0;
        }

        // Look at loss stability in the last 10 steps
        let recent_losses: Vec<f32> =
            self.training_stats.iter().rev().take(10).map(|s| s.quantization_loss).collect();

        let mean_loss = recent_losses.iter().sum::<f32>() / recent_losses.len() as f32;
        let variance = recent_losses.iter().map(|&loss| (loss - mean_loss).powi(2)).sum::<f32>()
            / recent_losses.len() as f32;

        // Lower variance indicates better convergence
        let stability_score = 1.0 / (1.0 + variance);
        stability_score.clamp(0.0, 1.0)
    }
}

/// QAT Observer for tracking quantization parameter statistics
#[derive(Debug, Clone)]
pub struct QATObserver {
    observer_type: QATObserverType,
    values: Vec<f32>,
    moving_average: f32,
    alpha: f32, // For exponential moving average
}

impl QATObserver {
    pub fn new(observer_type: QATObserverType) -> Self {
        Self {
            observer_type,
            values: Vec::new(),
            moving_average: 0.0,
            alpha: 0.1, // EMA decay factor
        }
    }

    pub fn update(&mut self, value: f32) {
        self.values.push(value);

        match self.observer_type {
            QATObserverType::MovingAverage => {
                self.moving_average = self.alpha * value + (1.0 - self.alpha) * self.moving_average;
            },
            QATObserverType::MinMax => {
                // Keep track of min/max values
                if self.values.len() > 1000 {
                    self.values.remove(0); // Keep recent values only
                }
            },
            QATObserverType::Histogram => {
                // For histogram-based quantization
                if self.values.len() > 10000 {
                    self.values.truncate(5000); // Keep manageable size
                }
            },
            QATObserverType::Percentile => {
                // For percentile-based quantization
                if self.values.len() > 1000 {
                    self.values.remove(0);
                }
            },
        }
    }

    pub fn get_variance(&self) -> f32 {
        if self.values.len() < 2 {
            return 0.0;
        }

        let mean = self.values.iter().sum::<f32>() / self.values.len() as f32;
        let variance =
            self.values.iter().map(|&v| (v - mean).powi(2)).sum::<f32>() / self.values.len() as f32;

        variance
    }
}

/// Apply QAT quantization with default configuration
pub fn apply_qat_quantization(
    weights: &[f32],
    precision: QuantizationPrecision,
) -> Result<Vec<f32>, JsValue> {
    let config = QATConfig {
        precision,
        ..Default::default()
    };

    let mut quantizer = QATQuantizer::new(config);
    let (quantized_weights, _stats) = quantizer.apply_qat_quantization(weights, None, false)?;

    Ok(quantized_weights)
}

/// Create QAT configuration for specific use cases
pub fn create_qat_config_for_transformer(precision: QuantizationPrecision) -> QATConfig {
    QATConfig {
        precision,
        fake_quantize_during_training: true,
        observer_type: QATObserverType::MovingAverage,
        quantization_scheme: QATScheme::Symmetric,
        calibration_steps: 200, // More steps for transformers
        learning_rate_decay: 0.98,
        weight_decay: 5e-5,
        gradient_clipping: Some(0.5),
        batch_norm_folding: false, // Transformers typically don't use batch norm
        channel_wise_quantization: true,
    }
}

/// Create QAT configuration for CNN models
pub fn create_qat_config_for_cnn(precision: QuantizationPrecision) -> QATConfig {
    QATConfig {
        precision,
        fake_quantize_during_training: true,
        observer_type: QATObserverType::MinMax,
        quantization_scheme: QATScheme::Asymmetric,
        calibration_steps: 100,
        learning_rate_decay: 0.95,
        weight_decay: 1e-4,
        gradient_clipping: Some(1.0),
        batch_norm_folding: true, // CNNs often use batch norm
        channel_wise_quantization: true,
    }
}