rustorch 0.6.29

Production-ready PyTorch-compatible deep learning library in Rust with special mathematical functions (gamma, Bessel, error functions), statistical distributions, Fourier transforms (FFT/RFFT), matrix decomposition (SVD/QR/LU/eigenvalue), automatic differentiation, neural networks, computer vision transforms, complete GPU acceleration (CUDA/Metal/OpenCL), SIMD optimizations, parallel processing, WebAssembly browser support, comprehensive distributed learning support, and performance validation
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
//! Quantization-Aware Training (QAT) support
//! 量子化認識学習(QAT)サポート

use super::calibration::Observer;
use super::schemes::{
    AsymmetricQuantization, QuantizationParams, QuantizationScheme, SymmetricQuantization,
};
use crate::autograd::Variable;
use crate::error::{RusTorchError, RusTorchResult};
use crate::nn::Module;
use crate::tensor::Tensor;
use ndarray::ArrayD;
use num_traits::Float;
use std::marker::PhantomData;
use std::sync::{Arc, Mutex};

/// Trait for modules that support quantization-aware training
/// 量子化認識学習をサポートするモジュールのトレイト
pub trait QATModule<T: Float> {
    /// Enable QAT mode (fake quantization during training)
    /// QATモードを有効化(学習中の偽量子化)
    fn enable_qat(&mut self);

    /// Disable QAT mode (normal training)
    /// QATモードを無効化(通常学習)
    fn disable_qat(&mut self);

    /// Check if QAT is enabled
    /// QATが有効かチェック
    fn is_qat_enabled(&self) -> bool;

    /// Get quantization parameters
    /// 量子化パラメータを取得
    fn get_quantization_params(&self) -> Option<(f32, i32)>;

    /// Set quantization parameters
    /// 量子化パラメータを設定
    fn set_quantization_params(&mut self, scale: f32, zero_point: i32);
}

/// Fake quantization operation for training
/// 学習用偽量子化演算
#[derive(Clone)]
pub struct FakeQuantize<T: Float> {
    /// Quantization scale
    /// 量子化スケール
    pub scale: f32,
    /// Zero point for asymmetric quantization
    /// 非対称量子化のゼロポイント
    pub zero_point: i32,
    /// Quantization scheme
    /// 量子化スキーム
    pub scheme: QuantizationScheme,
    /// Enable/disable fake quantization
    /// 偽量子化の有効/無効
    pub enabled: bool,
    /// Quantization bit width
    /// 量子化ビット幅
    pub bits: u8,
    /// Observer for calibration during QAT
    /// QAT中のキャリブレーション用観測器
    pub observer: Option<Arc<Mutex<dyn Observer<T>>>>,
    /// Phantom data for type parameter
    /// 型パラメータ用のファントムデータ
    _phantom: PhantomData<T>,
}

impl<T: Float + Send + Sync + ndarray::ScalarOperand + num_traits::FromPrimitive> FakeQuantize<T> {
    /// Create a new fake quantization module
    /// 新しい偽量子化モジュールを作成
    pub fn new(scheme: QuantizationScheme, bits: u8) -> Self {
        let (qmin, qmax) = match bits {
            8 => (-128i32, 127i32),
            4 => (-8i32, 7i32),
            16 => (-32768i32, 32767i32),
            _ => (-128i32, 127i32), // Default to INT8
        };

        Self {
            scale: 1.0,
            zero_point: 0,
            scheme,
            enabled: true,
            bits,
            observer: None,
            _phantom: PhantomData,
        }
    }

    /// Create with observer for dynamic calibration
    /// 動的キャリブレーション用観測器付きで作成
    pub fn with_observer(
        scheme: QuantizationScheme,
        bits: u8,
        observer: Arc<Mutex<dyn Observer<T>>>,
    ) -> Self {
        let mut fake_quant = Self::new(scheme, bits);
        fake_quant.observer = Some(observer);
        fake_quant
    }

    /// Apply fake quantization to tensor
    /// テンソルに偽量子化を適用
    pub fn forward(&self, input: &Variable<T>) -> RusTorchResult<Variable<T>> {
        if !self.enabled {
            return Ok(input.clone());
        }

        // Access tensor data
        let input_tensor = input.data();
        let input_guard = input_tensor.read().unwrap();

        // Update observer if available
        // 利用可能な場合は観測器を更新
        if let Some(observer) = &self.observer {
            let mut obs = observer.lock().unwrap();
            obs.observe(&input_guard.data);

            // Update quantization parameters from observer
            // 観測器から量子化パラメータを更新
            if let Ok((new_scale, new_zero_point)) = obs.get_quantization_params(self.scheme) {
                // In a mutable version, we would update self.scale and self.zero_point
                // 可変バージョンでは、self.scaleとself.zero_pointを更新
            }
        }

        // Apply fake quantization with straight-through estimator
        // ストレートスルー推定器を使用した偽量子化を適用
        let quantized_data = self.fake_quantize_tensor(&input_guard.data)?;

        Ok(Variable::new(
            Tensor::from_ndarray(quantized_data),
            input.requires_grad(),
        ))
    }

    /// Fake quantize a tensor (simulate quantization without actually quantizing)
    /// テンソルの偽量子化(実際に量子化せずに量子化をシミュレート)
    fn fake_quantize_tensor(&self, data: &ArrayD<T>) -> RusTorchResult<ArrayD<T>> {
        let (qmin, qmax) = self.get_quantization_range();

        let quantized_data = data.mapv(|val| {
            // Convert to f32 for quantization math
            let val_f32 = val.to_f32().unwrap_or(0.0);

            // Quantize: round((x - zero_point) / scale)
            let quantized_int =
                ((val_f32 / self.scale).round() as i32 + self.zero_point).clamp(qmin, qmax);

            // Dequantize back to floating point (straight-through for gradients)
            let dequantized_f32 = (quantized_int - self.zero_point) as f32 * self.scale;

            // Convert back to T
            T::from_f32(dequantized_f32).unwrap_or(val)
        });

        Ok(quantized_data)
    }

    /// Get quantization range based on bit width
    /// ビット幅に基づいて量子化範囲を取得
    fn get_quantization_range(&self) -> (i32, i32) {
        match self.bits {
            8 => (-128, 127),
            4 => (-8, 7),
            16 => (-32768, 32767),
            _ => (-128, 127),
        }
    }

    /// Calibrate quantization parameters from data
    /// データから量子化パラメータをキャリブレート
    pub fn calibrate(&mut self, data: &ArrayD<T>) -> RusTorchResult<()> {
        let (scale, zero_point) = match self.scheme {
            QuantizationScheme::Symmetric => SymmetricQuantization::compute_params(data)?,
            QuantizationScheme::Asymmetric => AsymmetricQuantization::compute_params(data)?,
            _ => {
                // For per-channel schemes, use asymmetric for simplicity
                AsymmetricQuantization::compute_params(data)?
            }
        };

        self.scale = scale;
        self.zero_point = zero_point;
        Ok(())
    }
}

/// Shared QAT configuration and fake quantization components
/// 共有QAT設定と偽量子化コンポーネント
#[derive(Clone)]
pub struct QATConfig<T: Float> {
    /// Weight fake quantization
    /// 重み用偽量子化
    pub weight_fake_quant: FakeQuantize<T>,
    /// Activation fake quantization
    /// 活性化用偽量子化
    pub activation_fake_quant: FakeQuantize<T>,
    /// QAT enabled flag
    /// QAT有効フラグ
    pub enabled: bool,
}

impl<T: Float + Send + Sync + ndarray::ScalarOperand + num_traits::FromPrimitive> QATConfig<T> {
    /// Create default QAT configuration
    /// デフォルトQAT設定を作成
    pub fn new() -> Self {
        Self {
            weight_fake_quant: FakeQuantize::new(QuantizationScheme::Symmetric, 8),
            activation_fake_quant: FakeQuantize::new(QuantizationScheme::Asymmetric, 8),
            enabled: true,
        }
    }

    /// Apply fake quantization to input and weights
    /// 入力と重みに偽量子化を適用
    pub fn apply_quantization(
        &self,
        input: &Variable<T>,
        weight: &Variable<T>,
    ) -> RusTorchResult<(Variable<T>, Variable<T>)> {
        let quantized_input = if self.enabled {
            self.activation_fake_quant.forward(input)?
        } else {
            input.clone()
        };

        let quantized_weight = if self.enabled {
            self.weight_fake_quant.forward(weight)?
        } else {
            weight.clone()
        };

        Ok((quantized_input, quantized_weight))
    }
}

/// QAT-enabled Linear layer
/// QAT対応線形層
pub struct QATLinear<T: Float + Send + Sync + ndarray::ScalarOperand + num_traits::FromPrimitive> {
    /// Weight tensor
    /// 重みテンソル
    pub weight: Variable<T>,
    /// Bias tensor (optional)
    /// バイアステンソル(オプション)
    pub bias: Option<Variable<T>>,
    /// QAT configuration
    /// QAT設定
    pub qat_config: QATConfig<T>,
}

impl<T: Float + Send + Sync + ndarray::ScalarOperand + num_traits::FromPrimitive> QATLinear<T> {
    /// Create a new QAT linear layer
    /// 新しいQAT線形層を作成
    pub fn new(in_features: usize, out_features: usize) -> Self {
        // Initialize weights with small random values
        // 小さなランダム値で重みを初期化
        let weight_data = ArrayD::from_shape_fn(
            vec![out_features, in_features],
            |_| T::from_f32(0.01).unwrap_or_else(T::zero), // Small initialization
        );
        let weight = Variable::new(Tensor::from_ndarray(weight_data), true);

        // Initialize bias to zero
        // バイアスをゼロで初期化
        let bias_data = ArrayD::zeros(vec![out_features]);
        let bias = Some(Variable::new(Tensor::from_ndarray(bias_data), true));

        Self {
            weight,
            bias,
            qat_config: QATConfig::new(),
        }
    }

    /// Forward pass with QAT
    /// QAT付きフォワードパス
    pub fn forward(&self, input: &Variable<T>) -> RusTorchResult<Variable<T>> {
        // Use QAT config to apply quantization
        // QAT設定を使用して量子化を適用
        let (quantized_input, quantized_weight) =
            self.qat_config.apply_quantization(input, &self.weight)?;

        // Perform linear transformation: output = input @ weight.T + bias
        // 線形変換を実行:output = input @ weight.T + bias
        let output = self.linear_forward(&quantized_input, &quantized_weight)?;

        Ok(output)
    }

    /// Perform actual linear computation
    /// 実際の線形計算を実行
    fn linear_forward(
        &self,
        input: &Variable<T>,
        weight: &Variable<T>,
    ) -> RusTorchResult<Variable<T>> {
        // Simplified linear operation - in full implementation would use proper matrix multiplication
        // 簡略化線形演算 - 完全実装では適切な行列乗算を使用

        let input_tensor = input.data();
        let input_guard = input_tensor.read().unwrap();
        let input_shape = input_guard.shape();

        let weight_tensor = weight.data();
        let weight_guard = weight_tensor.read().unwrap();
        let weight_shape = weight_guard.shape();

        if input_shape.len() < 2 || weight_shape.len() != 2 {
            return Err(RusTorchError::TensorOp {
                message: "Invalid shapes for linear layer".to_string(),
                source: None,
            });
        }

        let batch_size = input_shape[0];
        let in_features = input_shape[input_shape.len() - 1];
        let out_features = weight_shape[0];

        if weight_shape[1] != in_features {
            return Err(RusTorchError::ShapeMismatch {
                expected: vec![in_features],
                actual: vec![weight_shape[1]],
            });
        }

        // Create output tensor
        // 出力テンソルを作成
        let mut output_data = ArrayD::zeros(vec![batch_size, out_features]);

        // Simplified matrix multiplication
        // 簡略化行列乗算
        for b in 0..batch_size {
            for o in 0..out_features {
                let mut sum = T::zero();
                for i in 0..in_features {
                    sum = sum + input_guard.data[[b, i]] * weight_guard.data[[o, i]];
                }

                // Add bias if present
                // バイアスが存在する場合は加算
                if let Some(ref bias) = self.bias {
                    let bias_tensor = bias.data();
                    let bias_guard = bias_tensor.read().unwrap();
                    sum = sum + bias_guard.data[o];
                }

                output_data[[b, o]] = sum;
            }
        }

        Ok(Variable::new(
            Tensor::from_ndarray(output_data),
            input.requires_grad(),
        ))
    }
}

impl<T: Float + Send + Sync + ndarray::ScalarOperand + num_traits::FromPrimitive> QATModule<T>
    for QATLinear<T>
{
    fn enable_qat(&mut self) {
        self.qat_config.enabled = true;
        self.qat_config.weight_fake_quant.enabled = true;
        self.qat_config.activation_fake_quant.enabled = true;
    }

    fn disable_qat(&mut self) {
        self.qat_config.enabled = false;
        self.qat_config.weight_fake_quant.enabled = false;
        self.qat_config.activation_fake_quant.enabled = false;
    }

    fn is_qat_enabled(&self) -> bool {
        self.qat_config.enabled
    }

    fn get_quantization_params(&self) -> Option<(f32, i32)> {
        if self.qat_config.enabled {
            Some((
                self.qat_config.weight_fake_quant.scale,
                self.qat_config.weight_fake_quant.zero_point,
            ))
        } else {
            None
        }
    }

    fn set_quantization_params(&mut self, scale: f32, zero_point: i32) {
        self.qat_config.weight_fake_quant.scale = scale;
        self.qat_config.weight_fake_quant.zero_point = zero_point;
    }
}

/// QAT-enabled Conv2D layer (simplified)
/// QAT対応Conv2D層(簡略化)
pub struct QATConv2d<T: Float + Send + Sync + ndarray::ScalarOperand + num_traits::FromPrimitive> {
    /// Convolution weight
    /// 畳み込み重み
    pub weight: Variable<T>,
    /// Bias (optional)
    /// バイアス(オプション)
    pub bias: Option<Variable<T>>,
    /// QAT configuration
    /// QAT設定
    pub qat_config: QATConfig<T>,
    /// Convolution parameters
    /// 畳み込みパラメータ
    pub stride: (usize, usize),
    pub padding: (usize, usize),
}

impl<T: Float + Send + Sync + ndarray::ScalarOperand + num_traits::FromPrimitive> QATConv2d<T> {
    /// Create a new QAT Conv2D layer
    /// 新しいQAT Conv2D層を作成
    pub fn new(
        in_channels: usize,
        out_channels: usize,
        kernel_size: (usize, usize),
        stride: (usize, usize),
        padding: (usize, usize),
    ) -> Self {
        let weight_data = ArrayD::from_shape_fn(
            vec![out_channels, in_channels, kernel_size.0, kernel_size.1],
            |_| T::from_f32(0.01).unwrap_or_else(T::zero),
        );
        let weight = Variable::new(Tensor::from_ndarray(weight_data), true);

        let bias_data = ArrayD::zeros(vec![out_channels]);
        let bias = Some(Variable::new(Tensor::from_ndarray(bias_data), true));

        Self {
            weight,
            bias,
            qat_config: QATConfig::new(),
            stride,
            padding,
        }
    }

    /// Forward pass (simplified - placeholder implementation)
    /// フォワードパス(簡略化 - プレースホルダー実装)
    pub fn forward(&self, input: &Variable<T>) -> RusTorchResult<Variable<T>> {
        // Use QAT config to apply quantization
        // QAT設定を使用して量子化を適用
        let (quantized_input, quantized_weight) =
            self.qat_config.apply_quantization(input, &self.weight)?;

        // Simplified convolution - in practice would implement proper conv2d
        // 簡略化畳み込み - 実際には適切なconv2dを実装
        let input_tensor = quantized_input.data();
        let input_guard = input_tensor.read().unwrap();
        let output_data = input_guard.data.clone(); // Placeholder

        Ok(Variable::new(
            Tensor::from_ndarray(output_data),
            input.requires_grad(),
        ))
    }
}

impl<T: Float + Send + Sync + ndarray::ScalarOperand + num_traits::FromPrimitive> QATModule<T>
    for QATConv2d<T>
{
    fn enable_qat(&mut self) {
        self.qat_config.enabled = true;
        self.qat_config.weight_fake_quant.enabled = true;
        self.qat_config.activation_fake_quant.enabled = true;
    }

    fn disable_qat(&mut self) {
        self.qat_config.enabled = false;
        self.qat_config.weight_fake_quant.enabled = false;
        self.qat_config.activation_fake_quant.enabled = false;
    }

    fn is_qat_enabled(&self) -> bool {
        self.qat_config.enabled
    }

    fn get_quantization_params(&self) -> Option<(f32, i32)> {
        if self.qat_config.enabled {
            Some((
                self.qat_config.weight_fake_quant.scale,
                self.qat_config.weight_fake_quant.zero_point,
            ))
        } else {
            None
        }
    }

    fn set_quantization_params(&mut self, scale: f32, zero_point: i32) {
        self.qat_config.weight_fake_quant.scale = scale;
        self.qat_config.weight_fake_quant.zero_point = zero_point;
    }
}

/// QAT training utilities
/// QAT学習ユーティリティ
pub struct QATTrainer<T: Float> {
    /// Learning rate for QAT fine-tuning
    /// QATファインチューニング用学習率
    pub learning_rate: T,
    /// Number of calibration steps before starting QAT
    /// QAT開始前のキャリブレーション段階数
    pub calibration_steps: usize,
    /// Current step
    /// 現在のステップ
    pub current_step: usize,
}

impl<T: Float + Send + Sync + ndarray::ScalarOperand + num_traits::FromPrimitive> QATTrainer<T> {
    /// Create a new QAT trainer
    /// 新しいQATトレーナーを作成
    pub fn new(learning_rate: T, calibration_steps: usize) -> Self {
        Self {
            learning_rate,
            calibration_steps,
            current_step: 0,
        }
    }

    /// Training step with QAT
    /// QAT付き学習ステップ
    pub fn train_step<M: QATModule<T>>(
        &mut self,
        model: &mut M,
        _input: &Variable<T>,
        _target: &Variable<T>,
    ) -> RusTorchResult<T> {
        // Enable QAT after calibration period
        // キャリブレーション期間後にQATを有効化
        if self.current_step >= self.calibration_steps {
            model.enable_qat();
        } else {
            model.disable_qat();
        }

        self.current_step += 1;

        // In a full implementation, would compute loss and update parameters
        // 完全実装では、損失を計算してパラメータを更新
        Ok(T::zero()) // Placeholder loss
    }

    /// Prepare model for quantized deployment
    /// 量子化デプロイメント用のモデル準備
    pub fn prepare_for_deployment<M: QATModule<T>>(&self, model: &mut M) {
        model.disable_qat();
        // In practice, would convert fake quantization to real quantization
        // 実際には、偽量子化を実量子化に変換
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::tensor::Tensor;
    use ndarray::Array2;

    #[test]
    fn test_fake_quantize() {
        let fake_quant = FakeQuantize::<f32>::new(QuantizationScheme::Symmetric, 8);

        let data = Array2::from_shape_vec((2, 2), vec![1.0f32, 2.0, 3.0, 4.0])
            .unwrap()
            .into_dyn();
        let input = Variable::new(Tensor::from_ndarray(data), false);

        let output = fake_quant.forward(&input).unwrap();
        let output_tensor = output.data();
        let output_guard = output_tensor.read().unwrap();
        assert_eq!(output_guard.shape(), &[2, 2]);
    }

    #[test]
    fn test_qat_linear() {
        let mut linear = QATLinear::<f32>::new(3, 2);

        let input_data = Array2::from_shape_vec((1, 3), vec![1.0f32, 2.0, 3.0])
            .unwrap()
            .into_dyn();
        let input = Variable::new(Tensor::from_ndarray(input_data), false);

        assert!(linear.is_qat_enabled());

        let output = linear.forward(&input).unwrap();
        let output_tensor = output.data();
        let output_guard = output_tensor.read().unwrap();
        assert_eq!(output_guard.shape(), &[1, 2]);

        // Test disabling QAT
        linear.disable_qat();
        assert!(!linear.is_qat_enabled());
    }

    #[test]
    fn test_qat_conv2d() {
        let mut conv = QATConv2d::<f32>::new(3, 16, (3, 3), (1, 1), (1, 1));

        assert!(conv.is_qat_enabled());

        conv.set_quantization_params(0.1, 0);
        assert_eq!(conv.get_quantization_params(), Some((0.1, 0)));
    }

    #[test]
    fn test_qat_trainer() {
        let mut trainer = QATTrainer::new(0.001f32, 100);
        let mut linear = QATLinear::<f32>::new(2, 1);

        let input_data = Array2::from_shape_vec((1, 2), vec![1.0f32, 2.0])
            .unwrap()
            .into_dyn();
        let input = Variable::new(Tensor::from_ndarray(input_data), false);

        let target_data = Array2::from_shape_vec((1, 1), vec![3.0f32])
            .unwrap()
            .into_dyn();
        let target = Variable::new(Tensor::from_ndarray(target_data), false);

        // Initially should disable QAT (calibration phase)
        trainer.train_step(&mut linear, &input, &target).unwrap();
        assert!(!linear.is_qat_enabled());

        // After calibration steps, should enable QAT
        trainer.current_step = 101;
        trainer.train_step(&mut linear, &input, &target).unwrap();
        assert!(linear.is_qat_enabled());
    }
}