trustformers-core 0.1.1

Core traits and utilities for TrustformeRS
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
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
//! Linear (fully connected) layer implementation.
//!
//! This module provides the `Linear` layer, which performs affine transformations
//! of the form: `y = xW^T + b`, where `W` is the weight matrix and `b` is the
//! optional bias vector.

use crate::device::Device;
use crate::errors::{Result, TrustformersError};
#[cfg(all(target_os = "macos", feature = "metal"))]
use crate::gpu_ops::dispatch_matmul;
use crate::tensor::Tensor;
use crate::traits::Layer;
use scirs2_core::ndarray::{Array2, Ix2, IxDyn};
#[cfg(not(target_os = "macos"))]
use scirs2_core::simd_ops::SimdUnifiedOps;

/// Direct BLAS GEMM using OxiBLAS for maximum performance
#[cfg(target_os = "macos")]
#[inline]
fn blas_sgemm(a: &[f32], b: &[f32], c: &mut [f32], m: usize, k: usize, n: usize) {
    use oxiblas_blas::level3::gemm;
    use oxiblas_matrix::{MatMut, MatRef};

    // Bridge row-major → col-major via Cᵀ = Bᵀ·Aᵀ identity:
    // Row-major A(m×k) reinterpreted as col-major is Aᵀ(k×m), lda=k.
    // Row-major B(k×n) reinterpreted as col-major is Bᵀ(n×k), lda=n.
    // Row-major C(m×n) reinterpreted as col-major is Cᵀ(n×m), lda=n.
    // gemm(Bᵀ, Aᵀ) → Cᵀ = Bᵀ·Aᵀ = (A·B)ᵀ, so C buffer holds A·B. ✓
    let a_t = MatRef::new(a.as_ptr(), k, m, k);
    let b_t = MatRef::new(b.as_ptr(), n, k, n);
    let c_t = MatMut::new(c.as_mut_ptr(), n, m, n);

    // GEMM: Cᵀ = 1.0 * Bᵀ * Aᵀ + 0.0 * Cᵀ
    gemm(1.0, b_t, a_t, 0.0, c_t);
}

/// Direct BLAS GEMM using OxiBLAS for f64
#[cfg(target_os = "macos")]
#[inline]
fn blas_dgemm(a: &[f64], b: &[f64], c: &mut [f64], m: usize, k: usize, n: usize) {
    use oxiblas_blas::level3::gemm;
    use oxiblas_matrix::{MatMut, MatRef};

    // Bridge row-major → col-major via Cᵀ = Bᵀ·Aᵀ identity:
    // Row-major A(m×k) reinterpreted as col-major is Aᵀ(k×m), lda=k.
    // Row-major B(k×n) reinterpreted as col-major is Bᵀ(n×k), lda=n.
    // Row-major C(m×n) reinterpreted as col-major is Cᵀ(n×m), lda=n.
    // gemm(Bᵀ, Aᵀ) → Cᵀ = Bᵀ·Aᵀ = (A·B)ᵀ, so C buffer holds A·B. ✓
    let a_t = MatRef::new(a.as_ptr(), k, m, k);
    let b_t = MatRef::new(b.as_ptr(), n, k, n);
    let c_t = MatMut::new(c.as_mut_ptr(), n, m, n);

    // GEMM: Cᵀ = 1.0 * Bᵀ * Aᵀ + 0.0 * Cᵀ
    gemm(1.0, b_t, a_t, 0.0, c_t);
}

/// Fallback for non-macOS: use scirs2-core SIMD GEMM for f32
#[cfg(not(target_os = "macos"))]
#[inline]
fn blas_sgemm(a: &[f32], b: &[f32], c: &mut [f32], m: usize, k: usize, n: usize) {
    let a_arr = Array2::from_shape_vec((m, k), a.to_vec())
        .expect("matrix dimensions must match slice length");
    let b_arr = Array2::from_shape_vec((k, n), b.to_vec())
        .expect("matrix dimensions must match slice length");
    let mut c_arr = Array2::from_shape_vec((m, n), c.to_vec())
        .expect("matrix dimensions must match slice length");
    f32::simd_gemm(1.0, &a_arr.view(), &b_arr.view(), 0.0, &mut c_arr);
    c.copy_from_slice(c_arr.as_slice().expect("Array2 must have contiguous slice"));
}

/// Fallback for non-macOS: use scirs2-core SIMD GEMM for f64
#[cfg(not(target_os = "macos"))]
#[inline]
fn blas_dgemm(a: &[f64], b: &[f64], c: &mut [f64], m: usize, k: usize, n: usize) {
    let a_arr = Array2::from_shape_vec((m, k), a.to_vec())
        .expect("matrix dimensions must match slice length");
    let b_arr = Array2::from_shape_vec((k, n), b.to_vec())
        .expect("matrix dimensions must match slice length");
    let mut c_arr = Array2::from_shape_vec((m, n), c.to_vec())
        .expect("matrix dimensions must match slice length");
    f64::simd_gemm(1.0, &a_arr.view(), &b_arr.view(), 0.0, &mut c_arr);
    c.copy_from_slice(c_arr.as_slice().expect("Array2 must have contiguous slice"));
}

/// A linear transformation layer (fully connected layer).
///
/// The `Linear` layer applies a linear transformation to the incoming data:
/// `y = xW^T + b`. This is one of the most fundamental building blocks in
/// neural networks.
///
/// # Parameters
///
/// - `weight`: Learnable weight matrix of shape `[out_features, in_features]`
/// - `bias`: Optional learnable bias vector of shape `[out_features]`
///
/// # Input/Output Shapes
///
/// - Input: `[..., in_features]` - Can be 2D or 3D
/// - Output: `[..., out_features]` - Same number of dimensions as input
///
/// # Example
///
/// ```no_run
/// use trustformers_core::layers::Linear;
/// use trustformers_core::tensor::Tensor;
/// use trustformers_core::traits::Layer;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// // Create a linear layer: 768 → 3072
/// let linear = Linear::new(768, 3072, true);
///
/// // Apply to 2D input: [seq_len, in_features]
/// let input_2d = Tensor::randn(&[128, 768])?;
/// let output_2d = linear.forward(input_2d)?;  // Shape: [128, 3072]
///
/// // Apply to 3D input: [batch, seq_len, in_features]
/// let input_3d = Tensor::randn(&[4, 128, 768])?;
/// let output_3d = linear.forward(input_3d)?;  // Shape: [4, 128, 3072]
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone)]
pub struct Linear {
    weight: Tensor,
    bias: Option<Tensor>,
    device: Device,
    #[cfg(all(target_os = "macos", feature = "metal"))]
    weight_buffer_id: std::sync::Arc<std::sync::RwLock<Option<crate::gpu_ops::BufferId>>>,
    #[cfg(feature = "cuda")]
    weight_buffer_id_cuda:
        std::sync::Arc<std::sync::RwLock<Option<crate::gpu_ops::cuda::BufferId>>>,
}

impl Linear {
    /// Creates a new linear layer.
    ///
    /// # Arguments
    ///
    /// * `in_features` - Size of each input sample
    /// * `out_features` - Size of each output sample
    /// * `bias` - Whether to include a learnable bias
    ///
    /// # Returns
    ///
    /// A new `Linear` layer with randomly initialized weights using a normal
    /// distribution, and bias initialized to zeros if enabled.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use trustformers_core::layers::Linear;
    ///
    /// // Linear layer without bias
    /// let linear1 = Linear::new(512, 1024, false);
    ///
    /// // Linear layer with bias
    /// let linear2 = Linear::new(512, 1024, true);
    /// ```
    pub fn new(in_features: usize, out_features: usize, bias: bool) -> Self {
        Self::new_with_device(in_features, out_features, bias, Device::CPU)
    }

    /// Creates a new linear layer with specified device.
    ///
    /// # Arguments
    ///
    /// * `in_features` - Size of each input sample
    /// * `out_features` - Size of each output sample
    /// * `bias` - Whether to include a learnable bias
    /// * `device` - Device to use for computations (CPU, Metal, CUDA, etc.)
    ///
    /// # Returns
    ///
    /// A new `Linear` layer with randomly initialized weights.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use trustformers_core::layers::Linear;
    /// use trustformers_core::Device;
    ///
    /// // Create a linear layer on Metal GPU
    /// let linear = Linear::new_with_device(768, 3072, true, Device::metal_if_available(0));
    /// ```
    pub fn new_with_device(
        in_features: usize,
        out_features: usize,
        bias: bool,
        device: Device,
    ) -> Self {
        let weight =
            Tensor::randn(&[out_features, in_features]).expect("Failed to create random tensor");
        let bias = if bias {
            Some(Tensor::zeros(&[out_features]).expect("Failed to create zero tensor"))
        } else {
            None
        };

        Self {
            weight,
            bias,
            device,
            #[cfg(all(target_os = "macos", feature = "metal"))]
            weight_buffer_id: std::sync::Arc::new(std::sync::RwLock::new(None)),
            #[cfg(feature = "cuda")]
            weight_buffer_id_cuda: std::sync::Arc::new(std::sync::RwLock::new(None)),
        }
    }

    /// Returns the device this layer uses for computations.
    pub fn device(&self) -> Device {
        self.device
    }

    /// Move this layer to a different device.
    ///
    /// # Arguments
    ///
    /// * `device` - Target device
    ///
    /// # Returns
    ///
    /// Self with updated device.
    pub fn to_device(mut self, device: Device) -> Self {
        self.device = device;
        // Clear cached buffer when changing device
        #[cfg(all(target_os = "macos", feature = "metal"))]
        {
            if let Ok(mut buffer_id) = self.weight_buffer_id.write() {
                *buffer_id = None;
            }
        }
        #[cfg(feature = "cuda")]
        {
            if let Ok(mut buffer_id) = self.weight_buffer_id_cuda.write() {
                *buffer_id = None;
            }
        }
        self
    }

    /// Sets the weight matrix for this layer.
    ///
    /// # Arguments
    ///
    /// * `weight` - The new weight tensor, must have shape `[out_features, in_features]`
    ///
    /// # Returns
    ///
    /// `Ok(())` if successful.
    ///
    /// # Note
    ///
    /// This method is typically used when loading pretrained weights.
    pub fn set_weight(&mut self, weight: Tensor) -> Result<()> {
        self.weight = weight;
        // Clear cached buffer when weights are updated
        #[cfg(all(target_os = "macos", feature = "metal"))]
        {
            if let Ok(mut buffer_id) = self.weight_buffer_id.write() {
                *buffer_id = None;
            }
        }
        #[cfg(feature = "cuda")]
        {
            if let Ok(mut buffer_id) = self.weight_buffer_id_cuda.write() {
                *buffer_id = None;
            }
        }
        Ok(())
    }

    /// Sets the bias vector for this layer.
    ///
    /// # Arguments
    ///
    /// * `bias` - The new bias tensor, must have shape `[out_features]`
    ///
    /// # Returns
    ///
    /// `Ok(())` if successful.
    ///
    /// # Note
    ///
    /// This will enable bias even if the layer was created without bias.
    pub fn set_bias(&mut self, bias: Tensor) -> Result<()> {
        self.bias = Some(bias);
        Ok(())
    }

    /// Returns a reference to the weight matrix.
    ///
    /// # Returns
    ///
    /// A reference to the weight tensor of shape `[out_features, in_features]`.
    pub fn weight(&self) -> &Tensor {
        &self.weight
    }

    /// Returns a reference to the bias vector if present.
    ///
    /// # Returns
    ///
    /// `Some(&bias)` if bias is enabled, `None` otherwise.
    pub fn bias(&self) -> Option<&Tensor> {
        self.bias.as_ref()
    }

    /// Returns the total number of learnable parameters in this layer.
    ///
    /// # Returns
    ///
    /// The total parameter count including weights and bias (if present).
    pub fn parameter_count(&self) -> usize {
        let weight_count = self.weight.len();
        let bias_count = self.bias.as_ref().map_or(0, |b| b.len());
        weight_count + bias_count
    }

    /// Initialize persistent GPU weight buffer for Metal device
    /// This is called automatically on first forward pass with Metal device
    #[cfg(all(target_os = "macos", feature = "metal"))]
    fn ensure_weight_buffer_cached(&self) -> Result<()> {
        use crate::gpu_ops::metal::get_metal_backend;

        // Check if already cached (read lock is cheaper)
        if let Ok(buffer_id) = self.weight_buffer_id.read() {
            if buffer_id.is_some() {
                return Ok(()); // Already cached
            }
        }

        // Only cache if using Metal
        if matches!(self.device, Device::Metal(_)) {
            // Get write lock to cache the buffer
            let mut buffer_id_guard = self.weight_buffer_id.write().map_err(|_| {
                TrustformersError::hardware_error(
                    "Failed to acquire write lock on buffer cache",
                    "ensure_weight_buffer_cached",
                )
            })?;

            // Double-check after acquiring write lock (another thread might have cached it)
            if buffer_id_guard.is_some() {
                return Ok(());
            }

            // Get weight data as f32 slice
            // CRITICAL FIX: Cache the TRANSPOSED weight, not the original!
            // The Metal shader expects weight in [in_features, out_features] layout
            // but self.weight is stored as [out_features, in_features]
            let weight_t = self.weight.transpose(0, 1)?;
            match &weight_t {
                Tensor::F32(arr) => {
                    if arr.ndim() != 2 {
                        return Err(TrustformersError::shape_error(
                            "Weight tensor must be 2D for Metal caching".to_string(),
                        ));
                    }

                    // Convert to contiguous vec for GPU upload
                    // Using as_standard_layout() ensures proper row-major order
                    let contiguous_arr = arr.as_standard_layout();
                    let weight_data: Vec<f32> = contiguous_arr.iter().copied().collect();

                    // Get Metal backend and cache the buffer
                    let backend = get_metal_backend()?;
                    let new_buffer_id = backend.create_persistent_buffer(&weight_data)?;
                    *buffer_id_guard = Some(new_buffer_id);
                },
                _ => {
                    return Err(TrustformersError::tensor_op_error(
                        "Only F32 tensors supported for Metal caching",
                        "ensure_weight_buffer_cached",
                    ));
                },
            }
        }
        Ok(())
    }

    /// Pre-cache layer weights on GPU for zero-transfer pipeline
    /// This uploads weights to GPU memory in advance to avoid transfers during forward pass
    #[cfg(all(target_os = "macos", feature = "metal"))]
    pub fn weights_to_gpu(&mut self, device: &crate::device::Device) -> Result<()> {
        use crate::device::Device;

        if !matches!(device, Device::Metal(_)) {
            return Ok(()); // Nothing to do for non-Metal devices
        }

        // Update device setting
        self.device = *device;

        // Pre-cache weight buffer on GPU (keeps weight as F32 CPU tensor)
        // The caching mechanism handles the GPU upload internally
        self.ensure_weight_buffer_cached()?;

        // Upload bias to GPU if present (for GPU bias addition kernel)
        if let Some(ref bias) = self.bias {
            self.bias = Some(bias.to_device_enum(device)?);
        }

        Ok(())
    }

    /// Initialize persistent GPU weight buffer for CUDA device
    /// This is called automatically on first forward pass with CUDA device
    #[cfg(all(feature = "cuda", any(target_os = "linux", target_os = "windows")))]
    fn ensure_weight_buffer_cached_cuda(&self) -> Result<()> {
        use crate::gpu_ops::cuda::get_cuda_backend;

        // Check if already cached (read lock is cheaper)
        if let Ok(buffer_id) = self.weight_buffer_id_cuda.read() {
            if buffer_id.is_some() {
                return Ok(()); // Already cached
            }
        }

        // Only cache if using CUDA
        if matches!(self.device, Device::CUDA(_)) {
            // Get write lock to cache the buffer
            let mut buffer_id_guard = self.weight_buffer_id_cuda.write().map_err(|_| {
                TrustformersError::hardware_error(
                    "Failed to acquire write lock on CUDA buffer cache",
                    "ensure_weight_buffer_cached_cuda",
                )
            })?;

            // Double-check after acquiring write lock (another thread might have cached it)
            if buffer_id_guard.is_some() {
                return Ok(());
            }

            // Get weight data as f32 slice
            // CRITICAL FIX: Cache the TRANSPOSED weight, not the original!
            // The CUDA kernel expects weight in [in_features, out_features] layout
            // but self.weight is stored as [out_features, in_features]
            let weight_t = self.weight.transpose(0, 1)?;
            match &weight_t {
                Tensor::F32(arr) => {
                    if arr.ndim() != 2 {
                        return Err(TrustformersError::shape_error(
                            "Weight tensor must be 2D for CUDA caching".to_string(),
                        ));
                    }

                    // Convert to contiguous vec for GPU upload
                    // Using as_standard_layout() ensures proper row-major order
                    let contiguous_arr = arr.as_standard_layout();
                    let weight_data: Vec<f32> = contiguous_arr.iter().copied().collect();

                    // Get CUDA backend and cache the buffer
                    let device_id = if let Device::CUDA(id) = self.device {
                        id
                    } else {
                        0 // Default to device 0
                    };
                    let backend = get_cuda_backend(device_id)?;
                    let new_buffer_id = backend.create_persistent_buffer(&weight_data)?;
                    *buffer_id_guard = Some(new_buffer_id);
                },
                _ => {
                    return Err(TrustformersError::tensor_op_error(
                        "Only F32 tensors supported for CUDA caching",
                        "ensure_weight_buffer_cached_cuda",
                    ));
                },
            }
        }
        Ok(())
    }

    /// Pre-cache layer weights on GPU for zero-transfer pipeline (CUDA)
    /// This uploads weights to GPU memory in advance to avoid transfers during forward pass
    #[cfg(all(feature = "cuda", any(target_os = "linux", target_os = "windows")))]
    pub fn weights_to_gpu_cuda(&mut self, device: &crate::device::Device) -> Result<()> {
        use crate::device::Device;

        if !matches!(device, Device::CUDA(_)) {
            return Ok(()); // Nothing to do for non-CUDA devices
        }

        // Update device setting
        self.device = *device;

        // Pre-cache weight buffer on GPU (keeps weight as F32 CPU tensor)
        // The caching mechanism handles the GPU upload internally
        self.ensure_weight_buffer_cached_cuda()?;

        // Upload bias to GPU if present (for GPU bias addition kernel)
        if let Some(ref bias) = self.bias {
            self.bias = Some(bias.to_device_enum(device)?);
        }

        Ok(())
    }
}

impl Layer for Linear {
    type Input = Tensor;
    type Output = Tensor;

    fn forward(&self, input: Self::Input) -> Result<Self::Output> {
        // =====================================================================
        // GPU-TO-GPU PATH: Tensor::Metal (ZERO CPU TRANSFERS!)
        // =====================================================================
        #[cfg(all(target_os = "macos", feature = "metal"))]
        if let Tensor::Metal(ref input_metal) = input {
            use crate::gpu_ops::metal::get_metal_backend;
            use crate::tensor::MetalTensorData;

            // eprintln!("🎯 Linear::forward - GPU-to-GPU path triggered (Tensor::Metal input)");

            // Ensure weight buffer is cached on GPU
            self.ensure_weight_buffer_cached()?;

            // Get cached weight buffer ID
            let weight_buffer_id = {
                let buffer_id_guard = self.weight_buffer_id.read().map_err(|_| {
                    TrustformersError::hardware_error(
                        "Failed to acquire read lock on buffer cache",
                        "Linear::forward",
                    )
                })?;

                if let Some(id) = *buffer_id_guard {
                    id
                } else {
                    // Weight not cached - fallback to CPU
                    let cpu_input = input.to_device_enum(&crate::device::Device::CPU)?;
                    return self.forward(cpu_input);
                }
            };

            // Get Metal backend
            let backend = get_metal_backend()?;

            // Extract input shape and calculate matmul dimensions
            let shape = &input_metal.shape;
            let weight_shape = self.weight.shape();
            let in_features = shape[shape.len() - 1];

            // Check shape compatibility
            if in_features != weight_shape[1] {
                return Err(TrustformersError::shape_error(format!(
                    "Linear layer input features {} doesn't match weight shape {:?}",
                    in_features, weight_shape
                )));
            }

            // Flatten input to [batch, in_features] for matmul
            // Works for both 2D [seq_len, in_features] and 3D [batch, seq_len, in_features]
            let batch_dims: usize = shape[..shape.len() - 1].iter().product();
            let m = batch_dims; // number of rows in output
            let k = in_features; // shared dimension
            let n = self.weight.shape()[0]; // out_features

            // Perform GPU-to-GPU matmul using MPS (100-500x faster!)
            // Try MPS first, fallback to naive kernel if MPS unavailable
            let output_buffer_id = backend
                .matmul_gpu_to_gpu_mps(&input_metal.buffer_id, &weight_buffer_id, m, k, n)
                .or_else(|_e| {
                    // eprintln!(
                    //     "⚠️  MPS matmul failed: {:?}, falling back to naive Metal kernel",
                    //     e
                    // );
                    // Fallback to naive Metal kernel if MPS fails
                    backend.matmul_gpu_to_gpu(&input_metal.buffer_id, &weight_buffer_id, m, k, n)
                })?;

            // Calculate output shape (preserve batch dimensions, change last dim)
            let mut output_shape = shape[..shape.len() - 1].to_vec();
            output_shape.push(n);

            // Create output Metal tensor
            let mut output = Tensor::Metal(MetalTensorData {
                buffer_id: output_buffer_id,
                shape: output_shape.clone(),
                dtype: input_metal.dtype,
            });

            // Handle bias if present
            if let Some(ref bias) = self.bias {
                // eprintln!("🔍 Linear: Has bias, checking type...");
                // Try GPU-to-GPU bias addition if bias is on GPU
                match bias {
                    #[cfg(all(target_os = "macos", feature = "metal"))]
                    Tensor::Metal(bias_data) => {
                        // eprintln!("🔍 Linear: Bias is Metal, using GPU-to-GPU bias addition");
                        // Both output and bias are Metal tensors - use GPU kernel!
                        if let Tensor::Metal(output_data) = &output {
                            // eprintln!("🔍 Linear: Output is Metal, calling add_bias_gpu_to_gpu");
                            let output_buffer_id = backend.add_bias_gpu_to_gpu(
                                &output_data.buffer_id,
                                &bias_data.buffer_id,
                                batch_dims,
                                n,
                            )?;
                            // eprintln!(
                            //     "🔍 Linear: add_bias_gpu_to_gpu succeeded, returning Metal tensor"
                            // );

                            return Ok(Tensor::Metal(MetalTensorData {
                                buffer_id: output_buffer_id,
                                shape: output_shape.clone(),
                                dtype: output_data.dtype,
                            }));
                        }
                        // eprintln!("🔍 Linear: Output is NOT Metal, falling back to CPU");
                    },
                    _ => {
                        // eprintln!(
                        //     "🔍 Linear: Bias is NOT Metal (type={:?}), falling back to CPU",
                        //     std::mem::discriminant(bias)
                        // );
                    },
                }

                // Fallback: CPU bias addition
                // eprintln!("🔍 Linear: Using CPU bias fallback");
                output = output.to_device_enum(&crate::device::Device::CPU)?;
                // eprintln!("🔍 Linear: Converted output to CPU");
                output = output.add(bias)?;
                // eprintln!("🔍 Linear: Added bias on CPU");

                // Convert back to Metal tensor if needed
                if matches!(self.device, crate::device::Device::Metal(_)) {
                    // eprintln!("🔍 Linear: Converting back to Metal device");
                    output = output.to_device_enum(&self.device)?;
                    // eprintln!(
                    //     "🔍 Linear: Converted back to Metal, type={:?}",
                    //     std::mem::discriminant(&output)
                    // );
                }
            } else {
                // eprintln!("🔍 Linear: No bias");
            }

            // eprintln!(
            //     "🔍 Linear: Returning output, type={:?}",
            //     std::mem::discriminant(&output)
            // );
            return Ok(output);
        }

        // =====================================================================
        // GPU-TO-GPU PATH: Tensor::CUDA (ZERO CPU TRANSFERS!)
        // =====================================================================
        #[cfg(all(feature = "cuda", any(target_os = "linux", target_os = "windows")))]
        if let Tensor::CUDA(ref input_cuda) = input {
            use crate::gpu_ops::cuda::get_cuda_backend;
            use crate::tensor::CudaTensorData;

            // Ensure weight buffer is cached on GPU
            self.ensure_weight_buffer_cached_cuda()?;

            // Get cached weight buffer ID
            let weight_buffer_id = {
                let buffer_id_guard = self.weight_buffer_id_cuda.read().map_err(|_| {
                    TrustformersError::hardware_error(
                        "Failed to acquire read lock on CUDA buffer cache",
                        "Linear::forward",
                    )
                })?;

                if let Some(id) = *buffer_id_guard {
                    id
                } else {
                    // Weight not cached - fallback to CPU
                    let cpu_input = input.to_device_enum(&crate::device::Device::CPU)?;
                    return self.forward(cpu_input);
                }
            };

            // Get CUDA backend
            let device_id = if let Device::CUDA(id) = self.device {
                id
            } else {
                0 // Default to device 0
            };
            let backend = get_cuda_backend(device_id)?;

            // Extract input shape and calculate matmul dimensions
            let shape = &input_cuda.shape;
            let weight_shape = self.weight.shape();
            let in_features = shape[shape.len() - 1];

            // Check shape compatibility
            if in_features != weight_shape[1] {
                return Err(TrustformersError::shape_error(format!(
                    "Linear layer input features {} doesn't match weight shape {:?}",
                    in_features, weight_shape
                )));
            }

            // Flatten input to [batch, in_features] for matmul
            // Works for both 2D [seq_len, in_features] and 3D [batch, seq_len, in_features]
            let batch_dims: usize = shape[..shape.len() - 1].iter().product();
            let m = batch_dims; // number of rows in output
            let k = in_features; // shared dimension
            let n = self.weight.shape()[0]; // out_features

            // Perform GPU-to-GPU matmul (ZERO CPU TRANSFERS!)
            let output_buffer_id =
                backend.matmul_gpu_to_gpu(&input_cuda.buffer_id, &weight_buffer_id, m, k, n)?;

            // Calculate output shape (preserve batch dimensions, change last dim)
            let mut output_shape = shape[..shape.len() - 1].to_vec();
            output_shape.push(n);

            // Create output CUDA tensor
            let mut output = Tensor::CUDA(CudaTensorData {
                buffer_id: output_buffer_id,
                shape: output_shape.clone(),
                dtype: input_cuda.dtype,
            });

            // Handle bias if present
            if let Some(ref bias) = self.bias {
                // Try GPU-to-GPU bias addition if bias is on GPU
                match bias {
                    #[cfg(feature = "cuda")]
                    Tensor::CUDA(bias_data) => {
                        // Both output and bias are CUDA tensors - use GPU kernel!
                        if let Tensor::CUDA(output_data) = &output {
                            let output_buffer_id = backend.add_bias_gpu_to_gpu(
                                &output_data.buffer_id,
                                &bias_data.buffer_id,
                                batch_dims,
                                n,
                            )?;

                            return Ok(Tensor::CUDA(CudaTensorData {
                                buffer_id: output_buffer_id,
                                shape: output_shape.clone(),
                                dtype: output_data.dtype,
                            }));
                        }
                    },
                    _ => {},
                }

                // Fallback: CPU bias addition
                output = output.to_device_enum(&crate::device::Device::CPU)?;
                output = output.add(bias)?;

                // Convert back to CUDA tensor if needed
                if matches!(self.device, crate::device::Device::CUDA(_)) {
                    output = output.to_device_enum(&self.device)?;
                }
            }

            return Ok(output);
        }

        // =====================================================================
        // CPU/F32 PATH (existing implementation)
        // =====================================================================
        // Handle different input shapes for matmul
        let input_shape = input.shape();
        let weight_t = self.weight.transpose(0, 1)?;

        let output = if input_shape.len() == 2 {
            // Standard 2D input: [seq_len, hidden_size] x [hidden_size, out_features]

            // Try to use cached Metal buffer if available (ZERO-COPY OPTIMIZATION)
            #[cfg(all(target_os = "macos", feature = "metal"))]
            if matches!(self.device, Device::Metal(_)) {
                // Ensure buffer is cached
                self.ensure_weight_buffer_cached()?;

                // Try to use cached buffer
                if let Ok(buffer_id_guard) = self.weight_buffer_id.read() {
                    if let Some(buffer_id) = *buffer_id_guard {
                        // We have a cached buffer! Use it for ZERO-COPY matmul
                        use crate::gpu_ops::metal::get_metal_backend;

                        if let (Tensor::F32(inp), Tensor::F32(w_t)) = (&input, &weight_t) {
                            if inp.ndim() == 2 && w_t.ndim() == 2 {
                                let inp_shape = inp.shape();
                                let w_shape = w_t.shape();
                                let m = inp_shape[0];
                                let k = inp_shape[1];
                                let k2 = w_shape[0];
                                let n = w_shape[1];

                                if k == k2 {
                                    // Get Metal backend
                                    if let Ok(backend) = get_metal_backend() {
                                        // Convert input to contiguous
                                        let input_data: Vec<f32> = inp.iter().copied().collect();

                                        // Call matmul with CACHED weight buffer (no weight transfer!)
                                        if let Ok(result) = backend.matmul_with_cached_weight(
                                            &input_data,
                                            &buffer_id,
                                            m,
                                            k,
                                            n,
                                        ) {
                                            let result_arr =
                                                scirs2_core::ndarray::Array2::from_shape_vec(
                                                    (m, n),
                                                    result,
                                                )
                                                .map_err(|e| {
                                                    TrustformersError::shape_error(format!(
                                                        "Result reshape failed: {}",
                                                        e
                                                    ))
                                                })?;

                                            // Add bias if present
                                            let mut output = Tensor::F32(result_arr.into_dyn());
                                            if let Some(ref bias) = self.bias {
                                                output = output.add(bias)?;
                                            }
                                            return Ok(output);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            // Fallback: Use standard dispatch (for non-Metal or if cached path failed)
            #[cfg(all(target_os = "macos", feature = "metal"))]
            {
                if self.device.is_gpu() {
                    dispatch_matmul(&input, &weight_t, &self.device)?
                } else {
                    input.matmul(&weight_t)?
                }
            }
            #[cfg(not(all(target_os = "macos", feature = "metal")))]
            {
                input.matmul(&weight_t)?
            }
        } else if input_shape.len() == 3 {
            // Batched 3D input: [batch, seq_len, hidden_size] x [hidden_size, out_features]
            // Handle manually since tensor.matmul doesn't support 3D x 2D
            match (&input, &weight_t) {
                (Tensor::F32(inp), Tensor::F32(w)) => {
                    let batch = input_shape[0];
                    let seq_len = input_shape[1];
                    let hidden = input_shape[2];
                    let out_features = w.shape()[1];

                    // Try to use cached Metal buffer for 3D case (CRITICAL OPTIMIZATION)
                    #[cfg(all(target_os = "macos", feature = "metal"))]
                    if matches!(self.device, Device::Metal(_)) {
                        // Ensure buffer is cached
                        self.ensure_weight_buffer_cached()?;

                        if let Ok(buffer_id_guard) = self.weight_buffer_id.read() {
                            if let Some(buffer_id) = *buffer_id_guard {
                                // Reshape 3D input to 2D for matmul
                                let m = batch * seq_len;
                                let k = hidden;
                                let n = out_features;

                                // Get Metal backend
                                use crate::gpu_ops::metal::get_metal_backend;
                                if let Ok(backend) = get_metal_backend() {
                                    // Convert input to contiguous 2D data
                                    let input_data: Vec<f32> = inp.iter().copied().collect();

                                    // Call matmul with CACHED weight buffer (no weight transfer!)
                                    if let Ok(result) = backend.matmul_with_cached_weight(
                                        &input_data,
                                        &buffer_id,
                                        m,
                                        k,
                                        n,
                                    ) {
                                        // Reshape result from 2D [m, n] back to 3D [batch, seq_len, out_features]
                                        let result_arr =
                                            scirs2_core::ndarray::Array2::from_shape_vec(
                                                (m, n),
                                                result,
                                            )
                                            .map_err(
                                                |e| {
                                                    TrustformersError::shape_error(format!(
                                                        "Result reshape failed: {}",
                                                        e
                                                    ))
                                                },
                                            )?;

                                        let result_3d = result_arr
                                            .into_shape_with_order(IxDyn(&[
                                                batch,
                                                seq_len,
                                                out_features,
                                            ]))
                                            .map_err(|e| {
                                                TrustformersError::shape_error(format!(
                                                    "3D reshape failed: {}",
                                                    e
                                                ))
                                            })?;

                                        // Add bias if present
                                        let mut output = Tensor::F32(result_3d);
                                        if let Some(ref bias) = self.bias {
                                            output = output.add(bias)?;
                                        }
                                        return Ok(output);
                                    }
                                }
                            }
                        }
                    }

                    // Fallback: CPU path with ndarray

                    // Ensure contiguous layout before reshaping input to 2D for dot product
                    let inp_contiguous = inp.to_owned();
                    let inp_2d = inp_contiguous
                        .into_shape_with_order([batch * seq_len, hidden])
                        .map_err(|e| {
                            TrustformersError::shape_error(format!(
                                "Failed to reshape input: {}",
                                e
                            ))
                        })?;

                    // Ensure contiguous layout for weight and convert to 2D for GEMM
                    let w_contiguous = w.to_owned();
                    let w_2d = w_contiguous.into_dimensionality::<Ix2>().map_err(|e| {
                        TrustformersError::shape_error(format!(
                            "Failed to convert weight to 2D: {}",
                            e
                        ))
                    })?;

                    // Use direct BLAS for maximum performance (Accelerate on macOS)
                    let m = inp_2d.nrows();
                    let n = w_2d.ncols();
                    let k = inp_2d.ncols();
                    const MIN_SIZE_FOR_BLAS: usize = 32;
                    let out_2d = if m < MIN_SIZE_FOR_BLAS
                        || n < MIN_SIZE_FOR_BLAS
                        || k < MIN_SIZE_FOR_BLAS
                    {
                        inp_2d.dot(&w_2d)
                    } else {
                        // Use direct BLAS GEMM for 10-50x speedup
                        let inp_slice = inp_2d.as_slice().unwrap_or(&[]);
                        let w_slice = w_2d.as_slice().unwrap_or(&[]);
                        if !inp_slice.is_empty() && !w_slice.is_empty() {
                            let mut result_vec = vec![0.0f32; m * n];
                            blas_sgemm(inp_slice, w_slice, &mut result_vec, m, k, n);
                            Array2::from_shape_vec((m, n), result_vec)
                                .expect("BLAS result shape must match m x n")
                        } else {
                            // Fallback to ndarray dot if slices aren't contiguous
                            inp_2d.dot(&w_2d)
                        }
                    };

                    // Reshape back to 3D
                    let out_3d = out_2d
                        .into_shape_with_order(IxDyn(&[batch, seq_len, out_features]))
                        .map_err(|e| {
                            TrustformersError::shape_error(format!(
                                "Failed to reshape output: {}",
                                e
                            ))
                        })?;

                    Tensor::F32(out_3d)
                },
                (Tensor::F64(inp), Tensor::F64(w)) => {
                    let batch = input_shape[0];
                    let seq_len = input_shape[1];
                    let hidden = input_shape[2];
                    let out_features = w.shape()[1];

                    // Ensure contiguous layout before reshaping
                    let inp_contiguous = inp.to_owned();
                    let inp_2d = inp_contiguous
                        .into_shape_with_order([batch * seq_len, hidden])
                        .map_err(|e| {
                            TrustformersError::shape_error(format!(
                                "Failed to reshape input: {}",
                                e
                            ))
                        })?;

                    // Ensure contiguous layout for weight and convert to 2D for GEMM
                    let w_contiguous = w.to_owned();
                    let w_2d = w_contiguous.into_dimensionality::<Ix2>().map_err(|e| {
                        TrustformersError::shape_error(format!(
                            "Failed to convert weight to 2D: {}",
                            e
                        ))
                    })?;

                    // Use direct BLAS for maximum performance (Accelerate on macOS)
                    let m = inp_2d.nrows();
                    let n = w_2d.ncols();
                    let k = inp_2d.ncols();
                    const MIN_SIZE_FOR_BLAS: usize = 32;
                    let out_2d = if m < MIN_SIZE_FOR_BLAS
                        || n < MIN_SIZE_FOR_BLAS
                        || k < MIN_SIZE_FOR_BLAS
                    {
                        inp_2d.dot(&w_2d)
                    } else {
                        // Use direct BLAS GEMM for 10-50x speedup
                        let inp_slice = inp_2d.as_slice().unwrap_or(&[]);
                        let w_slice = w_2d.as_slice().unwrap_or(&[]);
                        if !inp_slice.is_empty() && !w_slice.is_empty() {
                            let mut result_vec = vec![0.0f64; m * n];
                            blas_dgemm(inp_slice, w_slice, &mut result_vec, m, k, n);
                            Array2::from_shape_vec((m, n), result_vec)
                                .expect("BLAS result shape must match m x n")
                        } else {
                            // Fallback to ndarray dot if slices aren't contiguous
                            inp_2d.dot(&w_2d)
                        }
                    };

                    let out_3d = out_2d
                        .into_shape_with_order(IxDyn(&[batch, seq_len, out_features]))
                        .map_err(|e| {
                            TrustformersError::shape_error(format!(
                                "Failed to reshape output: {}",
                                e
                            ))
                        })?;

                    Tensor::F64(out_3d)
                },
                _ => {
                    return Err(TrustformersError::tensor_op_error(
                        "Unsupported tensor types for 3D linear layer",
                        "Linear::forward",
                    ))
                },
            }
        } else {
            return Err(TrustformersError::tensor_op_error(
                &format!(
                    "Linear layer doesn't support input with {} dimensions",
                    input_shape.len()
                ),
                "Linear::forward",
            ));
        };

        if let Some(ref bias) = self.bias {
            // Handle broadcasting for bias addition
            match (&output, bias) {
                (Tensor::F32(out_arr), Tensor::F32(bias_arr)) => {
                    // Broadcast bias to match output shape
                    let result = out_arr + bias_arr;
                    Ok(Tensor::F32(result))
                },
                _ => output.add(bias),
            }
        } else {
            Ok(output)
        }
    }
}

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

    #[test]
    fn test_linear_creation_with_bias() {
        let linear = Linear::new(4, 3, true);
        assert_eq!(linear.weight().shape(), vec![3, 4]);
        assert!(linear.bias().is_some());
    }

    #[test]
    fn test_linear_creation_without_bias() {
        let linear = Linear::new(4, 3, false);
        assert_eq!(linear.weight().shape(), vec![3, 4]);
        assert!(linear.bias().is_none());
    }

    #[test]
    fn test_linear_device_default_cpu() {
        let linear = Linear::new(2, 2, true);
        assert_eq!(linear.device(), Device::CPU);
    }

    #[test]
    fn test_linear_to_device() {
        let linear = Linear::new(2, 2, true);
        let moved = linear.to_device(Device::CPU);
        assert_eq!(moved.device(), Device::CPU);
    }

    #[test]
    fn test_linear_parameter_count_with_bias() {
        let linear = Linear::new(4, 3, true);
        // 4*3 weights + 3 bias = 15
        assert_eq!(linear.parameter_count(), 15);
    }

    #[test]
    fn test_linear_parameter_count_without_bias() {
        let linear = Linear::new(4, 3, false);
        // 4*3 weights = 12
        assert_eq!(linear.parameter_count(), 12);
    }

    #[test]
    fn test_linear_set_weight() -> Result<()> {
        let mut linear = Linear::new(3, 2, true);
        let new_weight = Tensor::ones(&[2, 3])?;
        linear.set_weight(new_weight)?;
        assert_eq!(linear.weight().shape(), vec![2, 3]);
        Ok(())
    }

    #[test]
    fn test_linear_set_bias() -> Result<()> {
        let mut linear = Linear::new(3, 2, false);
        assert!(linear.bias().is_none());
        let new_bias = Tensor::zeros(&[2])?;
        linear.set_bias(new_bias)?;
        assert!(linear.bias().is_some());
        Ok(())
    }

    #[test]
    fn test_linear_forward_2d() -> Result<()> {
        let mut linear = Linear::new(3, 2, false);
        // Set weight to ones for easy verification
        let weight = Tensor::ones(&[2, 3])?;
        linear.set_weight(weight)?;
        let input = Tensor::ones(&[4, 3])?;
        let output = linear.forward(input)?;
        assert_eq!(output.shape(), vec![4, 2]);
        // Each output should be sum of 3 ones = 3.0
        let data = output.data()?;
        for val in &data {
            assert!((val - 3.0).abs() < 1e-3);
        }
        Ok(())
    }

    #[test]
    fn test_linear_forward_with_bias() -> Result<()> {
        let mut linear = Linear::new(3, 2, true);
        let weight = Tensor::ones(&[2, 3])?;
        let bias = Tensor::full_with_shape(&[2], 1.0)?;
        linear.set_weight(weight)?;
        linear.set_bias(bias)?;
        let input = Tensor::ones(&[1, 3])?;
        let output = linear.forward(input)?;
        assert_eq!(output.shape(), vec![1, 2]);
        // Each output = 3.0 (matmul) + 1.0 (bias) = 4.0
        let data = output.data()?;
        for val in &data {
            assert!((val - 4.0).abs() < 1e-3);
        }
        Ok(())
    }

    #[test]
    fn test_linear_forward_3d() -> Result<()> {
        let mut linear = Linear::new(4, 2, false);
        let weight = Tensor::ones(&[2, 4])?;
        linear.set_weight(weight)?;
        let input = Tensor::ones(&[2, 3, 4])?;
        let output = linear.forward(input)?;
        assert_eq!(output.shape(), vec![2, 3, 2]);
        Ok(())
    }

    #[test]
    fn test_linear_weight_ref() {
        let linear = Linear::new(5, 3, true);
        let weight = linear.weight();
        assert_eq!(weight.shape(), vec![3, 5]);
    }

    #[test]
    fn test_linear_bias_ref() {
        let linear = Linear::new(5, 3, true);
        if let Some(bias) = linear.bias() {
            assert_eq!(bias.shape(), vec![3]);
        }
    }

    #[test]
    fn test_linear_new_with_device() {
        let linear = Linear::new_with_device(4, 2, true, Device::CPU);
        assert_eq!(linear.device(), Device::CPU);
        assert_eq!(linear.weight().shape(), vec![2, 4]);
    }

    #[test]
    fn test_linear_forward_zero_weight() -> Result<()> {
        let mut linear = Linear::new(3, 2, false);
        let weight = Tensor::zeros(&[2, 3])?;
        linear.set_weight(weight)?;
        let input = Tensor::from_data(vec![1.0, 2.0, 3.0], &[1, 3])?;
        let output = linear.forward(input)?;
        let data = output.data()?;
        for val in &data {
            assert!(val.abs() < 1e-5);
        }
        Ok(())
    }

    #[test]
    fn test_linear_forward_identity_like() -> Result<()> {
        let mut linear = Linear::new(2, 2, false);
        let weight = Tensor::eye_f32(2)?;
        linear.set_weight(weight)?;
        let input = Tensor::from_data(vec![3.0, 7.0], &[1, 2])?;
        let output = linear.forward(input)?;
        let data = output.data()?;
        assert!((data[0] - 3.0).abs() < 1e-3);
        assert!((data[1] - 7.0).abs() < 1e-3);
        Ok(())
    }

    #[test]
    fn test_linear_large_layer() {
        let linear = Linear::new(768, 3072, true);
        assert_eq!(linear.weight().shape(), vec![3072, 768]);
        // 768 * 3072 + 3072 = 2362368
        assert_eq!(linear.parameter_count(), 768 * 3072 + 3072);
    }
}