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
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
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
//! GPU vs CPU Verification Tests for RusTorch
//! RusTorch用GPU vs CPU検証テスト

use crate::error::{RusTorchError, RusTorchResult};

/// Test tolerance for floating point comparisons
/// 浮動小数点比較のテスト許容値
pub const FLOAT_TOLERANCE: f32 = 1e-5;
/// Test tolerance for double precision floating point comparisons
/// 倍精度浮動小数点比較のテスト許容値
pub const DOUBLE_TOLERANCE: f64 = 1e-10;

/// Test result structure
/// テスト結果構造体
#[derive(Debug, Clone)]
#[allow(missing_docs)]
pub struct VerificationResult {
    pub test_name: String,
    pub passed: bool,
    pub cpu_time_ms: Option<f64>,
    pub gpu_time_ms: Option<f64>,
    pub speedup: Option<f64>,
    pub max_error: f32,
    pub mean_error: f32,
    pub error_message: Option<String>,
}

impl VerificationResult {
    /// Create a new verification result
    /// 新しい検証結果を作成
    pub fn new(test_name: String) -> Self {
        Self {
            test_name,
            passed: false,
            cpu_time_ms: None,
            gpu_time_ms: None,
            speedup: None,
            max_error: 0.0,
            mean_error: 0.0,
            error_message: None,
        }
    }

    /// Add timing information to the verification result
    /// 検証結果にタイミング情報を追加
    pub fn with_timing(mut self, cpu_time: f64, gpu_time: f64) -> Self {
        self.cpu_time_ms = Some(cpu_time);
        self.gpu_time_ms = Some(gpu_time);
        self.speedup = Some(cpu_time / gpu_time);
        self
    }

    /// Add error metrics to the verification result
    /// 検証結果にエラーメトリクスを追加
    pub fn with_error_metrics(mut self, max_error: f32, mean_error: f32) -> Self {
        self.max_error = max_error;
        self.mean_error = mean_error;
        self.passed = max_error < FLOAT_TOLERANCE;
        self
    }

    /// Mark the verification as failed with error message
    /// エラーメッセージと共に検証を失敗とマーク
    pub fn with_failure(mut self, error: String) -> Self {
        self.passed = false;
        self.error_message = Some(error);
        self
    }
}

/// Verification test suite for GPU operations
/// GPU演算の検証テストスイート
pub struct VerificationTestSuite {
    results: Vec<VerificationResult>,
}

impl VerificationTestSuite {
    /// Create a new verification test suite
    /// 新しい検証テストスイートを作成
    pub fn new() -> Self {
        Self {
            results: Vec::new(),
        }
    }

    /// Run all verification tests
    /// すべての検証テストを実行
    pub fn run_all_tests(&mut self) -> RusTorchResult<()> {
        println!("🔍 Starting GPU vs CPU Verification Tests");
        println!("==========================================");

        // Element-wise operations
        self.test_elementwise_add()?;
        self.test_elementwise_sub()?;
        self.test_elementwise_mul()?;
        self.test_elementwise_div()?;

        // Matrix operations
        self.test_matrix_multiplication_small()?;
        self.test_matrix_multiplication_large()?;

        // Reduction operations
        self.test_reduction_sum()?;
        self.test_reduction_mean()?;

        // Neural network operations
        self.test_relu_activation()?;
        self.test_batch_normalization()?;
        self.test_softmax()?;

        // Convolution operations
        self.test_conv2d_basic()?;
        self.test_max_pool2d()?;

        // Advanced operations
        self.test_transpose()?;
        self.test_gelu_activation()?;

        self.print_summary();
        Ok(())
    }

    /// Test element-wise addition
    /// 要素ごと加算のテスト
    fn test_elementwise_add(&mut self) -> RusTorchResult<()> {
        let mut result = VerificationResult::new("Element-wise Addition".to_string());

        let size = 1024 * 1024;
        let a = vec![1.5f32; size];
        let b = vec![2.5f32; size];

        // CPU reference implementation
        let start = std::time::Instant::now();
        let mut cpu_result = vec![0.0f32; size];
        for i in 0..size {
            cpu_result[i] = a[i] + b[i];
        }
        let cpu_time = start.elapsed().as_secs_f64() * 1000.0;

        // GPU implementation
        #[cfg(any(feature = "cuda", feature = "opencl", feature = "metal"))]
        {
            let start = std::time::Instant::now();
            let gpu_result = self.gpu_elementwise_add(&a, &b)?;
            let gpu_time = start.elapsed().as_secs_f64() * 1000.0;

            let (max_error, mean_error) = self.compute_error_metrics(&cpu_result, &gpu_result);
            result = result
                .with_timing(cpu_time, gpu_time)
                .with_error_metrics(max_error, mean_error);
        }

        #[cfg(not(any(feature = "cuda", feature = "opencl", feature = "metal")))]
        {
            result = result.with_failure("No GPU backend available".to_string());
        }

        self.results.push(result);
        Ok(())
    }

    /// Test element-wise subtraction
    /// 要素ごと減算のテスト
    fn test_elementwise_sub(&mut self) -> RusTorchResult<()> {
        let mut result = VerificationResult::new("Element-wise Subtraction".to_string());

        let size = 512 * 512;
        let a = vec![5.0f32; size];
        let b = vec![2.0f32; size];

        // CPU reference
        let start = std::time::Instant::now();
        let mut cpu_result = vec![0.0f32; size];
        for i in 0..size {
            cpu_result[i] = a[i] - b[i];
        }
        let cpu_time = start.elapsed().as_secs_f64() * 1000.0;

        #[cfg(any(feature = "cuda", feature = "opencl", feature = "metal"))]
        {
            let start = std::time::Instant::now();
            let gpu_result = self.gpu_elementwise_sub(&a, &b)?;
            let gpu_time = start.elapsed().as_secs_f64() * 1000.0;

            let (max_error, mean_error) = self.compute_error_metrics(&cpu_result, &gpu_result);
            result = result
                .with_timing(cpu_time, gpu_time)
                .with_error_metrics(max_error, mean_error);
        }

        #[cfg(not(any(feature = "cuda", feature = "opencl", feature = "metal")))]
        {
            result = result.with_failure("No GPU backend available".to_string());
        }

        self.results.push(result);
        Ok(())
    }

    /// Test element-wise multiplication
    /// 要素ごと乗算のテスト
    fn test_elementwise_mul(&mut self) -> RusTorchResult<()> {
        let mut result = VerificationResult::new("Element-wise Multiplication".to_string());

        let size = 256 * 256 * 64;
        let a = vec![1.5f32; size];
        let b = vec![2.0f32; size];

        // CPU reference
        let start = std::time::Instant::now();
        let mut cpu_result = vec![0.0f32; size];
        for i in 0..size {
            cpu_result[i] = a[i] * b[i];
        }
        let cpu_time = start.elapsed().as_secs_f64() * 1000.0;

        #[cfg(any(feature = "cuda", feature = "opencl", feature = "metal"))]
        {
            let start = std::time::Instant::now();
            let gpu_result = self.gpu_elementwise_mul(&a, &b)?;
            let gpu_time = start.elapsed().as_secs_f64() * 1000.0;

            let (max_error, mean_error) = self.compute_error_metrics(&cpu_result, &gpu_result);
            result = result
                .with_timing(cpu_time, gpu_time)
                .with_error_metrics(max_error, mean_error);
        }

        #[cfg(not(any(feature = "cuda", feature = "opencl", feature = "metal")))]
        {
            result = result.with_failure("No GPU backend available".to_string());
        }

        self.results.push(result);
        Ok(())
    }

    /// Test element-wise division
    /// 要素ごと除算のテスト
    fn test_elementwise_div(&mut self) -> RusTorchResult<()> {
        let mut result = VerificationResult::new("Element-wise Division".to_string());

        let size = 128 * 128 * 32;
        let a = vec![10.0f32; size];
        let b = vec![2.0f32; size];

        // CPU reference
        let start = std::time::Instant::now();
        let mut cpu_result = vec![0.0f32; size];
        for i in 0..size {
            cpu_result[i] = a[i] / b[i];
        }
        let cpu_time = start.elapsed().as_secs_f64() * 1000.0;

        #[cfg(any(feature = "cuda", feature = "opencl", feature = "metal"))]
        {
            let start = std::time::Instant::now();
            let gpu_result = self.gpu_elementwise_div(&a, &b)?;
            let gpu_time = start.elapsed().as_secs_f64() * 1000.0;

            let (max_error, mean_error) = self.compute_error_metrics(&cpu_result, &gpu_result);
            result = result
                .with_timing(cpu_time, gpu_time)
                .with_error_metrics(max_error, mean_error);
        }

        #[cfg(not(any(feature = "cuda", feature = "opencl", feature = "metal")))]
        {
            result = result.with_failure("No GPU backend available".to_string());
        }

        self.results.push(result);
        Ok(())
    }

    /// Test small matrix multiplication
    /// 小型行列乗算のテスト
    fn test_matrix_multiplication_small(&mut self) -> RusTorchResult<()> {
        let mut result = VerificationResult::new("Matrix Multiplication (Small)".to_string());

        let m = 64;
        let n = 64;
        let k = 64;

        let a = (0..m * k).map(|i| (i as f32) * 0.01).collect::<Vec<f32>>();
        let b = (0..k * n).map(|i| (i as f32) * 0.02).collect::<Vec<f32>>();

        // CPU reference
        let start = std::time::Instant::now();
        let cpu_result = self.cpu_matmul(&a, &b, m, n, k);
        let cpu_time = start.elapsed().as_secs_f64() * 1000.0;

        #[cfg(any(feature = "cuda", feature = "opencl", feature = "metal"))]
        {
            let start = std::time::Instant::now();
            let gpu_result = self.gpu_matmul(&a, &b, m, n, k)?;
            let gpu_time = start.elapsed().as_secs_f64() * 1000.0;

            let (max_error, mean_error) = self.compute_error_metrics(&cpu_result, &gpu_result);
            result = result
                .with_timing(cpu_time, gpu_time)
                .with_error_metrics(max_error, mean_error);
        }

        #[cfg(not(any(feature = "cuda", feature = "opencl", feature = "metal")))]
        {
            result = result.with_failure("No GPU backend available".to_string());
        }

        self.results.push(result);
        Ok(())
    }

    /// Test large matrix multiplication
    /// 大型行列乗算のテスト
    fn test_matrix_multiplication_large(&mut self) -> RusTorchResult<()> {
        let mut result = VerificationResult::new("Matrix Multiplication (Large)".to_string());

        let m = 1024;
        let n = 1024;
        let k = 1024;

        let a = (0..m * k)
            .map(|i| ((i % 1000) as f32) * 0.001)
            .collect::<Vec<f32>>();
        let b = (0..k * n)
            .map(|i| ((i % 1000) as f32) * 0.001)
            .collect::<Vec<f32>>();

        // CPU reference (subset for performance)
        let start = std::time::Instant::now();
        let cpu_result = self.cpu_matmul(&a, &b, m.min(256), n.min(256), k.min(256));
        let cpu_time = start.elapsed().as_secs_f64() * 1000.0;

        #[cfg(any(feature = "cuda", feature = "opencl", feature = "metal"))]
        {
            let start = std::time::Instant::now();
            let gpu_result = self.gpu_matmul(&a, &b, m, n, k)?;
            let gpu_time = start.elapsed().as_secs_f64() * 1000.0;

            // Compare subset
            let cpu_subset = &cpu_result[..256 * 256];
            let gpu_subset = &gpu_result[..256 * 256];
            let (max_error, mean_error) = self.compute_error_metrics(cpu_subset, gpu_subset);
            result = result
                .with_timing(cpu_time, gpu_time)
                .with_error_metrics(max_error, mean_error);
        }

        #[cfg(not(any(feature = "cuda", feature = "opencl", feature = "metal")))]
        {
            result = result.with_failure("No GPU backend available".to_string());
        }

        self.results.push(result);
        Ok(())
    }

    /// Test reduction sum
    /// リダクション合計のテスト
    fn test_reduction_sum(&mut self) -> RusTorchResult<()> {
        let mut result = VerificationResult::new("Reduction Sum".to_string());

        let size = 1024 * 1024;
        let input = (0..size)
            .map(|i| (i % 1000) as f32 * 0.001)
            .collect::<Vec<f32>>();

        // CPU reference
        let start = std::time::Instant::now();
        let cpu_sum = input.iter().sum::<f32>();
        let cpu_time = start.elapsed().as_secs_f64() * 1000.0;

        #[cfg(any(feature = "cuda", feature = "opencl", feature = "metal"))]
        {
            let start = std::time::Instant::now();
            let gpu_sum = self.gpu_reduce_sum(&input)?;
            let gpu_time = start.elapsed().as_secs_f64() * 1000.0;

            let error = (cpu_sum - gpu_sum).abs();
            result = result
                .with_timing(cpu_time, gpu_time)
                .with_error_metrics(error, error);
        }

        #[cfg(not(any(feature = "cuda", feature = "opencl", feature = "metal")))]
        {
            result = result.with_failure("No GPU backend available".to_string());
        }

        self.results.push(result);
        Ok(())
    }

    /// Test reduction mean
    /// リダクション平均のテスト
    fn test_reduction_mean(&mut self) -> RusTorchResult<()> {
        let mut result = VerificationResult::new("Reduction Mean".to_string());

        let size = 512 * 512;
        let input = (0..size)
            .map(|i| ((i % 100) as f32).sin())
            .collect::<Vec<f32>>();

        // CPU reference
        let start = std::time::Instant::now();
        let cpu_mean = input.iter().sum::<f32>() / size as f32;
        let cpu_time = start.elapsed().as_secs_f64() * 1000.0;

        #[cfg(any(feature = "cuda", feature = "opencl", feature = "metal"))]
        {
            let start = std::time::Instant::now();
            let gpu_sum = self.gpu_reduce_sum(&input)?;
            let gpu_mean = gpu_sum / size as f32;
            let gpu_time = start.elapsed().as_secs_f64() * 1000.0;

            let error = (cpu_mean - gpu_mean).abs();
            result = result
                .with_timing(cpu_time, gpu_time)
                .with_error_metrics(error, error);
        }

        #[cfg(not(any(feature = "cuda", feature = "opencl", feature = "metal")))]
        {
            result = result.with_failure("No GPU backend available".to_string());
        }

        self.results.push(result);
        Ok(())
    }

    /// Test ReLU activation
    /// ReLU活性化のテスト
    fn test_relu_activation(&mut self) -> RusTorchResult<()> {
        let mut result = VerificationResult::new("ReLU Activation".to_string());

        let size = 256 * 256;
        let input = (0..size)
            .map(|i| (i as f32 - size as f32 / 2.0) * 0.01)
            .collect::<Vec<f32>>();

        // CPU reference
        let start = std::time::Instant::now();
        let mut cpu_result = vec![0.0f32; size];
        for i in 0..size {
            cpu_result[i] = input[i].max(0.0);
        }
        let cpu_time = start.elapsed().as_secs_f64() * 1000.0;

        #[cfg(any(feature = "cuda", feature = "opencl", feature = "metal"))]
        {
            let start = std::time::Instant::now();
            let gpu_result = self.gpu_relu(&input)?;
            let gpu_time = start.elapsed().as_secs_f64() * 1000.0;

            let (max_error, mean_error) = self.compute_error_metrics(&cpu_result, &gpu_result);
            result = result
                .with_timing(cpu_time, gpu_time)
                .with_error_metrics(max_error, mean_error);
        }

        #[cfg(not(any(feature = "cuda", feature = "opencl", feature = "metal")))]
        {
            result = result.with_failure("No GPU backend available".to_string());
        }

        self.results.push(result);
        Ok(())
    }

    /// Test batch normalization
    /// バッチ正規化のテスト
    fn test_batch_normalization(&mut self) -> RusTorchResult<()> {
        let mut result = VerificationResult::new("Batch Normalization".to_string());

        let size = 1024;
        let input = (0..size).map(|i| (i as f32) * 0.01).collect::<Vec<f32>>();
        let mean = 0.5f32;
        let variance = 0.25f32;
        let epsilon = 1e-5f32;

        // CPU reference
        let start = std::time::Instant::now();
        let mut cpu_result = vec![0.0f32; size];
        for i in 0..size {
            cpu_result[i] = (input[i] - mean) / (variance + epsilon).sqrt();
        }
        let cpu_time = start.elapsed().as_secs_f64() * 1000.0;

        #[cfg(any(feature = "cuda", feature = "opencl", feature = "metal"))]
        {
            let start = std::time::Instant::now();
            let gpu_result = self.gpu_batch_norm(&input, mean, variance, epsilon)?;
            let gpu_time = start.elapsed().as_secs_f64() * 1000.0;

            let (max_error, mean_error) = self.compute_error_metrics(&cpu_result, &gpu_result);
            result = result
                .with_timing(cpu_time, gpu_time)
                .with_error_metrics(max_error, mean_error);
        }

        #[cfg(not(any(feature = "cuda", feature = "opencl", feature = "metal")))]
        {
            result = result.with_failure("No GPU backend available".to_string());
        }

        self.results.push(result);
        Ok(())
    }

    /// Test softmax
    /// Softmaxのテスト
    fn test_softmax(&mut self) -> RusTorchResult<()> {
        let mut result = VerificationResult::new("Softmax".to_string());

        let size = 1000;
        let input = (0..size)
            .map(|i| (i as f32) * 0.01 - 5.0)
            .collect::<Vec<f32>>();

        // CPU reference
        let start = std::time::Instant::now();
        let max_val = input.iter().fold(f32::NEG_INFINITY, |a, &b| a.max(b));
        let exp_sum: f32 = input.iter().map(|&x| (x - max_val).exp()).sum();
        let mut cpu_result = vec![0.0f32; size];
        for i in 0..size {
            cpu_result[i] = (input[i] - max_val).exp() / exp_sum;
        }
        let cpu_time = start.elapsed().as_secs_f64() * 1000.0;

        #[cfg(any(feature = "cuda", feature = "opencl", feature = "metal"))]
        {
            let start = std::time::Instant::now();
            let gpu_result = self.gpu_softmax(&input)?;
            let gpu_time = start.elapsed().as_secs_f64() * 1000.0;

            let (max_error, mean_error) = self.compute_error_metrics(&cpu_result, &gpu_result);
            result = result
                .with_timing(cpu_time, gpu_time)
                .with_error_metrics(max_error, mean_error);
        }

        #[cfg(not(any(feature = "cuda", feature = "opencl", feature = "metal")))]
        {
            result = result.with_failure("No GPU backend available".to_string());
        }

        self.results.push(result);
        Ok(())
    }

    /// Test 2D convolution
    /// 2D畳み込みのテスト
    fn test_conv2d_basic(&mut self) -> RusTorchResult<()> {
        let mut result = VerificationResult::new("Conv2D Basic".to_string());

        let input_h = 32;
        let input_w = 32;
        let kernel_h = 3;
        let kernel_w = 3;
        let _output_h = input_h - kernel_h + 1;
        let _output_w = input_w - kernel_w + 1;

        let input = (0..input_h * input_w)
            .map(|i| (i as f32) * 0.01)
            .collect::<Vec<f32>>();
        let kernel = vec![1.0f32, 0.0, -1.0, 1.0, 0.0, -1.0, 1.0, 0.0, -1.0]; // Edge detection

        // CPU reference
        let start = std::time::Instant::now();
        let cpu_result = self.cpu_conv2d(&input, &kernel, input_h, input_w, kernel_h, kernel_w);
        let cpu_time = start.elapsed().as_secs_f64() * 1000.0;

        #[cfg(any(feature = "cuda", feature = "opencl", feature = "metal"))]
        {
            let start = std::time::Instant::now();
            let gpu_result = self.gpu_conv2d(
                &input, &kernel, input_h, input_w, kernel_h, kernel_w, 1, 1, 0, 0,
            )?;
            let gpu_time = start.elapsed().as_secs_f64() * 1000.0;

            let (max_error, mean_error) = self.compute_error_metrics(&cpu_result, &gpu_result);
            result = result
                .with_timing(cpu_time, gpu_time)
                .with_error_metrics(max_error, mean_error);
        }

        #[cfg(not(any(feature = "cuda", feature = "opencl", feature = "metal")))]
        {
            result = result.with_failure("No GPU backend available".to_string());
        }

        self.results.push(result);
        Ok(())
    }

    /// Test max pooling 2D
    /// 2Dマックスプーリングのテスト
    fn test_max_pool2d(&mut self) -> RusTorchResult<()> {
        let mut result = VerificationResult::new("Max Pool2D".to_string());

        let input_h = 16;
        let input_w = 16;
        let pool_h = 2;
        let pool_w = 2;
        let stride_h = 2;
        let stride_w = 2;
        let _output_h = input_h / stride_h;
        let _output_w = input_w / stride_w;

        let input = (0..input_h * input_w)
            .map(|i| (i as f32) * 0.1)
            .collect::<Vec<f32>>();

        // CPU reference
        let start = std::time::Instant::now();
        let cpu_result =
            self.cpu_max_pool2d(&input, input_h, input_w, pool_h, pool_w, stride_h, stride_w);
        let cpu_time = start.elapsed().as_secs_f64() * 1000.0;

        #[cfg(any(feature = "cuda", feature = "opencl", feature = "metal"))]
        {
            let start = std::time::Instant::now();
            let gpu_result = self.gpu_max_pool2d(
                &input, input_h, input_w, pool_h, pool_w, stride_h, stride_w, 0, 0,
            )?;
            let gpu_time = start.elapsed().as_secs_f64() * 1000.0;

            let (max_error, mean_error) = self.compute_error_metrics(&cpu_result, &gpu_result);
            result = result
                .with_timing(cpu_time, gpu_time)
                .with_error_metrics(max_error, mean_error);
        }

        #[cfg(not(any(feature = "cuda", feature = "opencl", feature = "metal")))]
        {
            result = result.with_failure("No GPU backend available".to_string());
        }

        self.results.push(result);
        Ok(())
    }

    /// Test transpose
    /// 転置のテスト
    fn test_transpose(&mut self) -> RusTorchResult<()> {
        let mut result = VerificationResult::new("Transpose".to_string());

        let rows = 256;
        let cols = 512;
        let input = (0..rows * cols)
            .map(|i| (i as f32) * 0.01)
            .collect::<Vec<f32>>();

        // CPU reference
        let start = std::time::Instant::now();
        let mut cpu_result = vec![0.0f32; rows * cols];
        for r in 0..rows {
            for c in 0..cols {
                cpu_result[c * rows + r] = input[r * cols + c];
            }
        }
        let cpu_time = start.elapsed().as_secs_f64() * 1000.0;

        #[cfg(any(feature = "cuda", feature = "opencl", feature = "metal"))]
        {
            let start = std::time::Instant::now();
            let gpu_result = self.gpu_transpose(&input, rows, cols)?;
            let gpu_time = start.elapsed().as_secs_f64() * 1000.0;

            let (max_error, mean_error) = self.compute_error_metrics(&cpu_result, &gpu_result);
            result = result
                .with_timing(cpu_time, gpu_time)
                .with_error_metrics(max_error, mean_error);
        }

        #[cfg(not(any(feature = "cuda", feature = "opencl", feature = "metal")))]
        {
            result = result.with_failure("No GPU backend available".to_string());
        }

        self.results.push(result);
        Ok(())
    }

    /// Test GELU activation
    /// GELU活性化のテスト
    fn test_gelu_activation(&mut self) -> RusTorchResult<()> {
        let mut result = VerificationResult::new("GELU Activation".to_string());

        let size = 1024;
        let input = (0..size)
            .map(|i| (i as f32 - 512.0) * 0.01)
            .collect::<Vec<f32>>();

        // CPU reference
        let start = std::time::Instant::now();
        let mut cpu_result = vec![0.0f32; size];
        for i in 0..size {
            let x = input[i];
            let erf_term = (x / 2.0_f32.sqrt()).tanh(); // Approximation
            cpu_result[i] = 0.5 * x * (1.0 + erf_term);
        }
        let cpu_time = start.elapsed().as_secs_f64() * 1000.0;

        #[cfg(any(feature = "cuda", feature = "opencl", feature = "metal"))]
        {
            let start = std::time::Instant::now();
            let gpu_result = self.gpu_gelu(&input)?;
            let gpu_time = start.elapsed().as_secs_f64() * 1000.0;

            let (max_error, mean_error) = self.compute_error_metrics(&cpu_result, &gpu_result);
            result = result
                .with_timing(cpu_time, gpu_time)
                .with_error_metrics(max_error * 10.0, mean_error * 10.0); // GELU has higher tolerance
        }

        #[cfg(not(any(feature = "cuda", feature = "opencl", feature = "metal")))]
        {
            result = result.with_failure("No GPU backend available".to_string());
        }

        self.results.push(result);
        Ok(())
    }

    // Helper functions for CPU reference implementations
    fn cpu_matmul(&self, a: &[f32], b: &[f32], m: usize, n: usize, k: usize) -> Vec<f32> {
        let mut c = vec![0.0f32; m * n];
        for row in 0..m {
            for col in 0..n {
                let mut sum = 0.0f32;
                for i in 0..k {
                    sum += a[row * k + i] * b[i * n + col];
                }
                c[row * n + col] = sum;
            }
        }
        c
    }

    fn cpu_conv2d(
        &self,
        input: &[f32],
        kernel: &[f32],
        ih: usize,
        iw: usize,
        kh: usize,
        kw: usize,
    ) -> Vec<f32> {
        let oh = ih - kh + 1;
        let ow = iw - kw + 1;
        let mut output = vec![0.0f32; oh * ow];

        for out_y in 0..oh {
            for out_x in 0..ow {
                let mut sum = 0.0f32;
                for ky in 0..kh {
                    for kx in 0..kw {
                        let in_y = out_y + ky;
                        let in_x = out_x + kx;
                        sum += input[in_y * iw + in_x] * kernel[ky * kw + kx];
                    }
                }
                output[out_y * ow + out_x] = sum;
            }
        }
        output
    }

    #[allow(clippy::too_many_arguments)]
    fn cpu_max_pool2d(
        &self,
        input: &[f32],
        ih: usize,
        iw: usize,
        ph: usize,
        pw: usize,
        sh: usize,
        sw: usize,
    ) -> Vec<f32> {
        let oh = ih / sh;
        let ow = iw / sw;
        let mut output = vec![f32::NEG_INFINITY; oh * ow];

        for out_y in 0..oh {
            for out_x in 0..ow {
                let mut max_val = f32::NEG_INFINITY;
                for py in 0..ph {
                    for px in 0..pw {
                        let in_y = out_y * sh + py;
                        let in_x = out_x * sw + px;
                        if in_y < ih && in_x < iw {
                            max_val = max_val.max(input[in_y * iw + in_x]);
                        }
                    }
                }
                output[out_y * ow + out_x] = max_val;
            }
        }
        output
    }

    // GPU operation stubs (to be implemented with actual GPU backends)
    #[cfg(any(feature = "cuda", feature = "opencl", feature = "metal"))]
    fn gpu_elementwise_add(&self, _a: &[f32], _b: &[f32]) -> RusTorchResult<Vec<f32>> {
        Err(RusTorchError::gpu(
            "GPU operation not implemented".to_string(),
        ))
    }

    #[cfg(any(feature = "cuda", feature = "opencl", feature = "metal"))]
    fn gpu_elementwise_sub(&self, _a: &[f32], _b: &[f32]) -> RusTorchResult<Vec<f32>> {
        Err(RusTorchError::gpu(
            "GPU operation not implemented".to_string(),
        ))
    }

    #[cfg(any(feature = "cuda", feature = "opencl", feature = "metal"))]
    fn gpu_elementwise_mul(&self, _a: &[f32], _b: &[f32]) -> RusTorchResult<Vec<f32>> {
        Err(RusTorchError::gpu(
            "GPU operation not implemented".to_string(),
        ))
    }

    #[cfg(any(feature = "cuda", feature = "opencl", feature = "metal"))]
    fn gpu_elementwise_div(&self, _a: &[f32], _b: &[f32]) -> RusTorchResult<Vec<f32>> {
        Err(RusTorchError::gpu(
            "GPU operation not implemented".to_string(),
        ))
    }

    #[cfg(any(feature = "cuda", feature = "opencl", feature = "metal"))]
    fn gpu_matmul(
        &self,
        _a: &[f32],
        _b: &[f32],
        _m: usize,
        _n: usize,
        _k: usize,
    ) -> RusTorchResult<Vec<f32>> {
        Err(RusTorchError::gpu(
            "GPU operation not implemented".to_string(),
        ))
    }

    #[cfg(any(feature = "cuda", feature = "opencl", feature = "metal"))]
    fn gpu_reduce_sum(&self, _input: &[f32]) -> RusTorchResult<f32> {
        Err(RusTorchError::gpu(
            "GPU operation not implemented".to_string(),
        ))
    }

    #[cfg(any(feature = "cuda", feature = "opencl", feature = "metal"))]
    fn gpu_relu(&self, _input: &[f32]) -> RusTorchResult<Vec<f32>> {
        Err(RusTorchError::gpu(
            "GPU operation not implemented".to_string(),
        ))
    }

    #[cfg(any(feature = "cuda", feature = "opencl", feature = "metal"))]
    fn gpu_batch_norm(
        &self,
        _input: &[f32],
        _mean: f32,
        _variance: f32,
        _epsilon: f32,
    ) -> RusTorchResult<Vec<f32>> {
        Err(RusTorchError::gpu(
            "GPU operation not implemented".to_string(),
        ))
    }

    #[cfg(any(feature = "cuda", feature = "opencl", feature = "metal"))]
    fn gpu_softmax(&self, _input: &[f32]) -> RusTorchResult<Vec<f32>> {
        Err(RusTorchError::gpu(
            "GPU operation not implemented".to_string(),
        ))
    }

    #[cfg(any(feature = "cuda", feature = "opencl", feature = "metal"))]
    fn gpu_conv2d(
        &self,
        _input: &[f32],
        _kernel: &[f32],
        _ih: usize,
        _iw: usize,
        _kh: usize,
        _kw: usize,
        _sh: usize,
        _sw: usize,
        _ph: usize,
        _pw: usize,
    ) -> RusTorchResult<Vec<f32>> {
        Err(RusTorchError::gpu(
            "GPU operation not implemented".to_string(),
        ))
    }

    #[cfg(any(feature = "cuda", feature = "opencl", feature = "metal"))]
    fn gpu_max_pool2d(
        &self,
        _input: &[f32],
        _ih: usize,
        _iw: usize,
        _ph: usize,
        _pw: usize,
        _sh: usize,
        _sw: usize,
        _pad_h: usize,
        _pad_w: usize,
    ) -> RusTorchResult<Vec<f32>> {
        Err(RusTorchError::gpu(
            "GPU operation not implemented".to_string(),
        ))
    }

    #[cfg(any(feature = "cuda", feature = "opencl", feature = "metal"))]
    fn gpu_transpose(
        &self,
        _input: &[f32],
        _rows: usize,
        _cols: usize,
    ) -> RusTorchResult<Vec<f32>> {
        Err(RusTorchError::gpu(
            "GPU operation not implemented".to_string(),
        ))
    }

    #[cfg(any(feature = "cuda", feature = "opencl", feature = "metal"))]
    fn gpu_gelu(&self, _input: &[f32]) -> RusTorchResult<Vec<f32>> {
        Err(RusTorchError::gpu(
            "GPU operation not implemented".to_string(),
        ))
    }

    /// Compute error metrics between CPU and GPU results
    /// CPU と GPU 結果間の誤差メトリクスを計算
    #[allow(dead_code)]
    fn compute_error_metrics(&self, cpu_result: &[f32], gpu_result: &[f32]) -> (f32, f32) {
        let len = cpu_result.len().min(gpu_result.len());
        let mut max_error = 0.0f32;
        let mut sum_error = 0.0f32;

        for i in 0..len {
            let error = (cpu_result[i] - gpu_result[i]).abs();
            max_error = max_error.max(error);
            sum_error += error;
        }

        let mean_error = sum_error / len as f32;
        (max_error, mean_error)
    }

    /// Print test summary
    /// テスト概要を出力
    fn print_summary(&self) {
        println!("\n📊 Verification Test Summary");
        println!("============================");

        let total_tests = self.results.len();
        let passed_tests = self.results.iter().filter(|r| r.passed).count();
        let failed_tests = total_tests - passed_tests;

        println!("Total Tests: {}", total_tests);
        println!("✅ Passed: {}", passed_tests);
        println!("❌ Failed: {}", failed_tests);
        println!(
            "Success Rate: {:.1}%",
            (passed_tests as f32 / total_tests as f32) * 100.0
        );

        println!("\n📈 Performance Results:");
        println!(
            "{:<30} {:<8} {:<10} {:<10} {:<10} {:<15} {:<15}",
            "Test Name", "Status", "CPU (ms)", "GPU (ms)", "Speedup", "Max Error", "Mean Error"
        );
        println!("{}", "-".repeat(120));

        for result in &self.results {
            let status = if result.passed {
                "✅ PASS"
            } else {
                "❌ FAIL"
            };
            let cpu_time = result
                .cpu_time_ms
                .map_or("N/A".to_string(), |t| format!("{:.2}", t));
            let gpu_time = result
                .gpu_time_ms
                .map_or("N/A".to_string(), |t| format!("{:.2}", t));
            let speedup = result
                .speedup
                .map_or("N/A".to_string(), |s| format!("{:.2}x", s));

            println!(
                "{:<30} {:<8} {:<10} {:<10} {:<10} {:<15.2e} {:<15.2e}",
                result.test_name,
                status,
                cpu_time,
                gpu_time,
                speedup,
                result.max_error,
                result.mean_error
            );

            if let Some(ref error_msg) = result.error_message {
                println!("    Error: {}", error_msg);
            }
        }

        if failed_tests > 0 {
            println!(
                "\n⚠️  {} tests failed. Check error messages above.",
                failed_tests
            );
        } else {
            println!(
                "\n🎉 All tests passed! GPU implementations match CPU results within tolerance."
            );
        }
    }
}

impl Default for VerificationTestSuite {
    fn default() -> Self {
        Self::new()
    }
}