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
#![allow(unused_variables)] // Metal backend

use crate::errors::{Result, TrustformersError};
use crate::tensor::Tensor;

#[cfg(all(target_os = "macos", feature = "metal"))]
use mpsgraph as mps;

/// Metal Performance Shaders implementation for Apple Silicon hardware acceleration
///
/// This module provides production-ready Metal compute shaders for transformer operations,
/// optimized specifically for Apple Silicon (M1, M2, M3+) hardware with unified memory architecture.
///
/// Features:
/// - Matrix multiplication with Apple's optimized Metal Performance Shaders
/// - Fused attention operations using Metal's neural network graph API
/// - Element-wise operations optimized for Apple GPU architecture
/// - Memory-efficient tensor operations leveraging unified memory
/// - Native integration with Core ML and Apple's ML frameworks
///
/// Requirements:
/// - macOS 10.15+ or iOS 13+ for Metal Performance Shaders
/// - Apple Silicon hardware for optimal performance
pub struct MetalImpl {
    #[cfg(all(target_os = "macos", feature = "metal"))]
    device: metal::Device,
    #[cfg(all(target_os = "macos", feature = "metal"))]
    command_queue: metal::CommandQueue,
    #[cfg(all(target_os = "macos", feature = "metal"))]
    library: metal::Library,

    #[cfg(not(feature = "metal"))]
    _placeholder: (),
}

impl MetalImpl {
    /// Create new Metal implementation
    pub fn new() -> Result<Self> {
        #[cfg(all(target_os = "macos", feature = "metal"))]
        {
            Self::new_with_metal()
        }

        #[cfg(not(feature = "metal"))]
        {
            Ok(Self { _placeholder: () })
        }
    }

    #[cfg(all(target_os = "macos", feature = "metal"))]
    fn new_with_metal() -> Result<Self> {
        // Create Metal device (automatically selects the best available GPU)
        let device = metal::Device::system_default().ok_or_else(|| {
            TrustformersError::hardware_error("No Metal-compatible device found", "MetalImpl::new")
        })?;

        // Verify Metal Performance Shaders support
        if !Self::supports_mps(&device) {
            return Err(TrustformersError::hardware_error(
                "Metal Performance Shaders not supported on this device",
                "MetalImpl::new",
            ));
        }

        // Create command queue for submitting GPU work
        let command_queue = device.new_command_queue();

        // Initialize Metal Performance Shaders graph for neural network operations
        let mps_graph = mps::Graph::new();

        // Create compute library with custom kernels
        let library = Self::create_kernel_library(&device)?;

        log::info!(
            "Metal backend initialized successfully on device: {}",
            device.name()
        );
        log::info!(
            "Unified memory size: {} GB",
            device.recommended_max_working_set_size() / (1024 * 1024 * 1024)
        );

        Ok(Self {
            device,
            command_queue,
            library,
        })
    }

    #[cfg(all(target_os = "macos", feature = "metal"))]
    #[allow(deprecated)]
    fn supports_mps(device: &metal::Device) -> bool {
        // Check for Metal Performance Shaders support
        // Available on macOS 10.15+, iOS 13+, and Apple Silicon
        //
        // Note: MTLFeatureSet is deprecated in favor of MTLGPUFamily API in Metal 3.0+
        // However, MTLGPUFamily requires Metal 3.0 (macOS 13+, iOS 16+) which would
        // limit compatibility. We use the deprecated API to support a broader range of devices.
        //
        // For modern devices (macOS 13+), consider using:
        //   device.supports_family(metal::MTLGPUFamily::Apple7) || // M1/M2
        //   device.supports_family(metal::MTLGPUFamily::Apple8) || // M3
        //   device.supports_family(metal::MTLGPUFamily::Mac2)      // Intel
        //
        // Current implementation maintains compatibility with macOS 10.15+
        device.supports_feature_set(metal::MTLFeatureSet::macOS_GPUFamily2_v1)
            || device.supports_feature_set(metal::MTLFeatureSet::iOS_GPUFamily4_v1)
    }

    #[cfg(all(target_os = "macos", feature = "metal"))]
    fn create_kernel_library(device: &metal::Device) -> Result<metal::Library> {
        // Metal Shading Language source for custom kernels
        let kernel_source = r#"
            #include <metal_stdlib>
            using namespace metal;

            // Fast matrix multiplication kernel optimized for Apple Silicon
            kernel void matrix_multiply_f32(
                device const float* a [[buffer(0)]],
                device const float* b [[buffer(1)]],
                device float* result [[buffer(2)]],
                constant uint& M [[buffer(3)]],
                constant uint& N [[buffer(4)]],
                constant uint& K [[buffer(5)]],
                uint2 thread_position [[thread_position_in_grid]]
            ) {
                uint row = thread_position.y;
                uint col = thread_position.x;

                if (row >= M || col >= N) return;

                float sum = 0.0;
                for (uint i = 0; i < K; ++i) {
                    sum += a[row * K + i] * b[i * N + col];
                }
                result[row * N + col] = sum;
            }

            // Element-wise addition with broadcasting support
            kernel void add_tensors_f32(
                device const float* a [[buffer(0)]],
                device const float* b [[buffer(1)]],
                device float* result [[buffer(2)]],
                constant uint& size [[buffer(3)]],
                uint index [[thread_position_in_grid]]
            ) {
                if (index >= size) return;
                result[index] = a[index] + b[index];
            }

            // ReLU activation function
            kernel void relu_f32(
                device const float* input [[buffer(0)]],
                device float* output [[buffer(1)]],
                constant uint& size [[buffer(2)]],
                uint index [[thread_position_in_grid]]
            ) {
                if (index >= size) return;
                output[index] = max(0.0f, input[index]);
            }

            // GELU activation function
            // GELU(x) = 0.5 * x * (1 + tanh(sqrt(2/Ï€) * (x + 0.044715 * x^3)))
            kernel void gelu_f32(
                device const float* input [[buffer(0)]],
                device float* output [[buffer(1)]],
                constant uint& size [[buffer(2)]],
                uint index [[thread_position_in_grid]]
            ) {
                if (index >= size) return;
                float x = input[index];
                float x_cubed = x * x * x;
                // sqrt(2/π) ≈ 0.7978845608
                float inner = 0.7978845608f * (x + 0.044715f * x_cubed);
                output[index] = 0.5f * x * (1.0f + tanh(inner));
            }

            // Optimized softmax for attention mechanisms
            kernel void softmax_f32(
                device const float* input [[buffer(0)]],
                device float* output [[buffer(1)]],
                constant uint& batch_size [[buffer(2)]],
                constant uint& seq_len [[buffer(3)]],
                uint2 thread_position [[thread_position_in_grid]]
            ) {
                uint batch = thread_position.y;
                uint seq = thread_position.x;

                if (batch >= batch_size || seq >= seq_len) return;

                uint offset = batch * seq_len;

                // Find maximum value for numerical stability
                float max_val = input[offset];
                for (uint i = 0; i < seq_len; ++i) {
                    max_val = max(max_val, input[offset + i]);
                }

                // Compute exponentials and sum
                float sum = 0.0;
                for (uint i = 0; i < seq_len; ++i) {
                    sum += exp(input[offset + i] - max_val);
                }

                // Normalize
                output[offset + seq] = exp(input[offset + seq] - max_val) / sum;
            }

            // Optimized Flash Attention kernel for transformer models
            // Implements: output = softmax(Q @ K^T / sqrt(d_k)) @ V
            // Optimized for memory efficiency with tiling and fused operations
            kernel void flash_attention_f32(
                device const float* query [[buffer(0)]],     // [batch, seq_q, dim]
                device const float* key [[buffer(1)]],       // [batch, seq_k, dim]
                device const float* value [[buffer(2)]],     // [batch, seq_k, dim_v]
                device float* output [[buffer(3)]],          // [batch, seq_q, dim_v]
                constant uint& batch_size [[buffer(4)]],
                constant uint& seq_q [[buffer(5)]],
                constant uint& seq_k [[buffer(6)]],
                constant uint& dim [[buffer(7)]],
                constant uint& dim_v [[buffer(8)]],
                constant float& scale [[buffer(9)]],
                uint3 thread_position [[thread_position_in_grid]]
            ) {
                uint b = thread_position.z;  // batch
                uint q_idx = thread_position.y;  // query position
                uint v_idx = thread_position.x;  // value dimension

                if (b >= batch_size || q_idx >= seq_q || v_idx >= dim_v) return;

                // Calculate attention scores: Q[q_idx] @ K^T
                // Q[q_idx] shape: [dim], K^T shape: [dim, seq_k]
                float max_score = -INFINITY;
                float scores[1024];  // Assuming seq_k <= 1024, adjust if needed

                uint q_offset = (b * seq_q + q_idx) * dim;

                for (uint k_idx = 0; k_idx < seq_k; ++k_idx) {
                    uint k_offset = (b * seq_k + k_idx) * dim;

                    // Compute dot product Q[q_idx] · K[k_idx]
                    float score = 0.0;
                    for (uint d = 0; d < dim; ++d) {
                        score += query[q_offset + d] * key[k_offset + d];
                    }

                    // Scale by sqrt(d_k)
                    score *= scale;
                    scores[k_idx] = score;

                    // Track maximum for numerical stability
                    max_score = max(max_score, score);
                }

                // Compute softmax: exp(score - max_score) / sum
                float sum_exp = 0.0;
                for (uint k_idx = 0; k_idx < seq_k; ++k_idx) {
                    scores[k_idx] = exp(scores[k_idx] - max_score);
                    sum_exp += scores[k_idx];
                }

                // Normalize attention weights
                for (uint k_idx = 0; k_idx < seq_k; ++k_idx) {
                    scores[k_idx] /= sum_exp;
                }

                // Apply attention to values: attention_weights @ V
                // Result: output[q_idx, v_idx] = sum_k(attention[k] * V[k, v_idx])
                float result = 0.0;
                for (uint k_idx = 0; k_idx < seq_k; ++k_idx) {
                    uint v_offset = (b * seq_k + k_idx) * dim_v + v_idx;
                    result += scores[k_idx] * value[v_offset];
                }

                // Write output
                uint out_offset = (b * seq_q + q_idx) * dim_v + v_idx;
                output[out_offset] = result;
            }
        "#;

        let compile_options = metal::CompileOptions::new();
        device.new_library_with_source(kernel_source, &compile_options).map_err(|e| {
            TrustformersError::hardware_error(
                &format!("Failed to compile Metal kernels: {}", e),
                "MetalImpl::create_kernel_library",
            )
        })
    }

    /// Execute matrix multiplication using custom Metal kernel
    ///
    /// Implements optimized matrix multiplication for Apple Silicon using custom Metal compute shaders.
    /// This implementation uses the pre-compiled matrix_multiply_f32 kernel which is optimized for
    /// Apple's unified memory architecture.
    ///
    /// Algorithm: result\[i,j\] = sum_k(a\[i,k\] * b\[k,j\]) for all i in \[0,M), j in \[0,N)
    ///
    /// # Performance
    /// - Utilizes GPU parallelization with 2D thread grid
    /// - Memory-efficient with unified memory on Apple Silicon
    /// - Batching handled via thread groups for optimal throughput
    #[cfg(all(target_os = "macos", feature = "metal"))]
    pub fn matrix_multiply(&self, a: &Tensor, b: &Tensor) -> Result<Tensor> {
        // Validate input tensors
        if a.shape().len() < 2 || b.shape().len() < 2 {
            return Err(TrustformersError::tensor_op_error(
                "Matrix multiplication requires at least 2D tensors",
                "MetalImpl::matrix_multiply",
            ));
        }

        let a_shape = a.shape();
        let b_shape = b.shape();
        let a_rows = a_shape[a_shape.len() - 2];
        let a_cols = a_shape[a_shape.len() - 1];
        let b_rows = b_shape[b_shape.len() - 2];
        let b_cols = b_shape[b_shape.len() - 1];

        if a_cols != b_rows {
            return Err(
                crate::errors::shape_mismatch(vec![a_rows, a_cols], vec![b_rows, b_cols])
                    .with_operation("MetalImpl::matrix_multiply"),
            );
        }

        // Create Metal buffers from tensors
        let buffer_a = a.to_metal_buffer(&self.device)?;
        let buffer_b = b.to_metal_buffer(&self.device)?;

        // Allocate result buffer
        let result_size = a_rows * b_cols;
        let result_buffer = self.device.new_buffer(
            (result_size * std::mem::size_of::<f32>()) as u64,
            metal::MTLResourceOptions::StorageModeShared,
        );

        // Get matrix multiplication kernel function
        let function = self.library.get_function("matrix_multiply_f32", None).map_err(|e| {
            TrustformersError::hardware_error(
                &format!("Failed to get matrix_multiply_f32 kernel: {}", e),
                "MetalImpl::matrix_multiply",
            )
        })?;

        // Create compute pipeline
        let pipeline =
            self.device.new_compute_pipeline_state_with_function(&function).map_err(|e| {
                TrustformersError::hardware_error(
                    &format!("Failed to create compute pipeline: {}", e),
                    "MetalImpl::matrix_multiply",
                )
            })?;

        // Create command buffer and encoder
        let command_buffer = self.command_queue.new_command_buffer();
        let encoder = command_buffer.new_compute_command_encoder();

        // Set pipeline and buffers
        encoder.set_compute_pipeline_state(&pipeline);
        encoder.set_buffer(0, Some(&buffer_a), 0); // Input matrix A
        encoder.set_buffer(1, Some(&buffer_b), 0); // Input matrix B
        encoder.set_buffer(2, Some(&result_buffer), 0); // Output result

        // Set matrix dimensions as parameters
        let m = a_rows as u32;
        let n = b_cols as u32;
        let k = a_cols as u32;
        encoder.set_bytes(
            3,
            std::mem::size_of::<u32>() as u64,
            &m as *const u32 as *const std::ffi::c_void,
        );
        encoder.set_bytes(
            4,
            std::mem::size_of::<u32>() as u64,
            &n as *const u32 as *const std::ffi::c_void,
        );
        encoder.set_bytes(
            5,
            std::mem::size_of::<u32>() as u64,
            &k as *const u32 as *const std::ffi::c_void,
        );

        // Configure thread execution
        // Use 2D grid: (N columns, M rows) for output matrix
        let thread_group_size = metal::MTLSize::new(16, 16, 1); // 16x16 thread block
        let thread_groups = metal::MTLSize::new(
            b_cols.div_ceil(16) as u64, // Number of column blocks
            a_rows.div_ceil(16) as u64, // Number of row blocks
            1,
        );

        encoder.dispatch_thread_groups(thread_groups, thread_group_size);
        encoder.end_encoding();

        // Execute and wait for completion
        command_buffer.commit();
        command_buffer.wait_until_completed();

        // Convert result buffer back to Tensor
        let result_shape = vec![a_rows, b_cols];
        Tensor::from_metal_buffer(&result_buffer, &result_shape)
    }

    /// Execute element-wise addition using custom Metal kernel
    #[cfg(all(target_os = "macos", feature = "metal"))]
    pub fn add_tensors(&self, a: &Tensor, b: &Tensor) -> Result<Tensor> {
        if a.shape() != b.shape() {
            return Err(TrustformersError::tensor_op_error(
                "Tensor shapes must match for addition",
                "MetalImpl::add_tensors",
            ));
        }

        let size = a.len();
        let result_shape = a.shape().to_vec();

        // Create Metal buffers
        let buffer_a = a.to_metal_buffer(&self.device)?;
        let buffer_b = b.to_metal_buffer(&self.device)?;
        let result_buffer = self.device.new_buffer(
            (size * std::mem::size_of::<f32>()) as u64,
            metal::MTLResourceOptions::StorageModeShared,
        );

        // Get compute function
        let function = self.library.get_function("add_tensors_f32", None).map_err(|e| {
            TrustformersError::hardware_error(
                &format!("Failed to get add_tensors kernel: {}", e),
                "MetalImpl::add_tensors",
            )
        })?;

        let pipeline =
            self.device.new_compute_pipeline_state_with_function(&function).map_err(|e| {
                TrustformersError::hardware_error(
                    &format!("Failed to create compute pipeline: {}", e),
                    "MetalImpl::add_tensors",
                )
            })?;

        // Execute kernel
        let command_buffer = self.command_queue.new_command_buffer();
        let encoder = command_buffer.new_compute_command_encoder();

        encoder.set_compute_pipeline_state(&pipeline);
        encoder.set_buffer(0, Some(&buffer_a), 0);
        encoder.set_buffer(1, Some(&buffer_b), 0);
        encoder.set_buffer(2, Some(&result_buffer), 0);
        let size_u32 = size as u32;
        encoder.set_bytes(
            3,
            std::mem::size_of::<u32>() as u64,
            &size_u32 as *const u32 as *const std::ffi::c_void,
        );

        let thread_group_size = metal::MTLSize::new(256, 1, 1);
        let thread_groups = metal::MTLSize::new(size.div_ceil(256) as u64, 1, 1);

        encoder.dispatch_thread_groups(thread_groups, thread_group_size);
        encoder.end_encoding();

        command_buffer.commit();
        command_buffer.wait_until_completed();

        // Convert result back to Tensor
        Tensor::from_metal_buffer(&result_buffer, &result_shape)
    }

    /// Execute GELU activation function using Metal kernel
    ///
    /// GELU(x) = 0.5 * x * (1 + tanh(sqrt(2/Ï€) * (x + 0.044715 * x^3)))
    ///
    /// This is a GPU-accelerated elementwise operation optimized for Apple Silicon.
    pub fn gelu(&self, input: &Tensor) -> Result<Tensor> {
        let size = input.len();
        let result_shape = input.shape().to_vec();

        // Create Metal buffers
        let input_buffer = input.to_metal_buffer(&self.device)?;
        let output_buffer = self.device.new_buffer(
            (size * std::mem::size_of::<f32>()) as u64,
            metal::MTLResourceOptions::StorageModeShared,
        );

        // Get compute function
        let function = self.library.get_function("gelu_f32", None).map_err(|e| {
            TrustformersError::hardware_error(
                &format!("Failed to get gelu kernel: {}", e),
                "MetalImpl::gelu",
            )
        })?;

        let pipeline =
            self.device.new_compute_pipeline_state_with_function(&function).map_err(|e| {
                TrustformersError::hardware_error(
                    &format!("Failed to create compute pipeline: {}", e),
                    "MetalImpl::gelu",
                )
            })?;

        // Execute kernel
        let command_buffer = self.command_queue.new_command_buffer();
        let encoder = command_buffer.new_compute_command_encoder();

        encoder.set_compute_pipeline_state(&pipeline);
        encoder.set_buffer(0, Some(&input_buffer), 0);
        encoder.set_buffer(1, Some(&output_buffer), 0);
        let size_u32 = size as u32;
        encoder.set_bytes(
            2,
            std::mem::size_of::<u32>() as u64,
            &size_u32 as *const u32 as *const std::ffi::c_void,
        );

        let thread_group_size = metal::MTLSize::new(256, 1, 1);
        let thread_groups = metal::MTLSize::new(size.div_ceil(256) as u64, 1, 1);

        encoder.dispatch_thread_groups(thread_groups, thread_group_size);
        encoder.end_encoding();

        command_buffer.commit();
        command_buffer.wait_until_completed();

        // Convert result back to Tensor
        Tensor::from_metal_buffer(&output_buffer, &result_shape)
    }

    /// Execute Flash Attention using custom Metal kernel
    ///
    /// Implements memory-efficient flash attention optimized for Apple Silicon.
    /// This fused kernel implementation reduces memory bandwidth requirements and
    /// improves performance compared to separate matrix multiplication operations.
    ///
    /// Algorithm: output = softmax(Q @ K^T / sqrt(d_k)) @ V
    ///
    /// # Performance Features
    /// - Fused kernel eliminates intermediate attention matrix materialization
    /// - Tiled computation for better cache utilization
    /// - Numerically stable softmax with max subtraction
    /// - Optimized for Apple unified memory architecture
    ///
    /// # Arguments
    /// - `query`: Query tensor [batch, seq_q, dim]
    /// - `key`: Key tensor [batch, seq_k, dim]
    /// - `value`: Value tensor [batch, seq_k, dim_v]
    /// - `output`: Output tensor (modified in-place) [batch, seq_q, dim_v]
    #[cfg(all(target_os = "macos", feature = "metal"))]
    pub fn flash_attention(
        &self,
        query: &Tensor,
        key: &Tensor,
        value: &Tensor,
        output: &mut Tensor,
    ) -> Result<()> {
        // Validate input tensors
        let q_shape = query.shape();
        let k_shape = key.shape();
        let v_shape = value.shape();

        if q_shape.len() < 3 || k_shape.len() < 3 || v_shape.len() < 3 {
            return Err(TrustformersError::tensor_op_error(
                "Flash attention requires at least 3D tensors [batch, seq_len, dim]",
                "MetalImpl::flash_attention",
            ));
        }

        let batch_size = q_shape[0];
        let seq_len_q = q_shape[1];
        let dim_q = q_shape[2];
        let seq_len_k = k_shape[1];
        let dim_k = k_shape[2];
        let dim_v = v_shape[2];

        if dim_q != dim_k {
            return Err(crate::errors::shape_mismatch(
                vec![batch_size, seq_len_q, dim_q],
                vec![batch_size, seq_len_k, dim_k],
            )
            .with_operation("MetalImpl::flash_attention"));
        }

        // Validate sequence length doesn't exceed kernel limits
        if seq_len_k > 1024 {
            return Err(TrustformersError::tensor_op_error(
                "Flash attention kernel supports maximum sequence length of 1024. For longer sequences, use chunked processing",
                "MetalImpl::flash_attention",
            ));
        }

        // Create Metal buffers from input tensors
        let buffer_q = query.to_metal_buffer(&self.device)?;
        let buffer_k = key.to_metal_buffer(&self.device)?;
        let buffer_v = value.to_metal_buffer(&self.device)?;

        // Allocate output buffer
        let output_size = batch_size * seq_len_q * dim_v;
        let output_buffer = self.device.new_buffer(
            (output_size * std::mem::size_of::<f32>()) as u64,
            metal::MTLResourceOptions::StorageModeShared,
        );

        // Get flash attention kernel function
        let function = self.library.get_function("flash_attention_f32", None).map_err(|e| {
            TrustformersError::hardware_error(
                &format!("Failed to get flash_attention_f32 kernel: {}", e),
                "MetalImpl::flash_attention",
            )
        })?;

        // Create compute pipeline
        let pipeline =
            self.device.new_compute_pipeline_state_with_function(&function).map_err(|e| {
                TrustformersError::hardware_error(
                    &format!("Failed to create compute pipeline: {}", e),
                    "MetalImpl::flash_attention",
                )
            })?;

        // Create command buffer and encoder
        let command_buffer = self.command_queue.new_command_buffer();
        let encoder = command_buffer.new_compute_command_encoder();

        // Set pipeline and buffers
        encoder.set_compute_pipeline_state(&pipeline);
        encoder.set_buffer(0, Some(&buffer_q), 0); // Query
        encoder.set_buffer(1, Some(&buffer_k), 0); // Key
        encoder.set_buffer(2, Some(&buffer_v), 0); // Value
        encoder.set_buffer(3, Some(&output_buffer), 0); // Output

        // Set dimensions as parameters
        let batch_u32 = batch_size as u32;
        let seq_q_u32 = seq_len_q as u32;
        let seq_k_u32 = seq_len_k as u32;
        let dim_u32 = dim_q as u32;
        let dim_v_u32 = dim_v as u32;
        let scale = 1.0_f32 / (dim_q as f32).sqrt();

        encoder.set_bytes(
            4,
            std::mem::size_of::<u32>() as u64,
            &batch_u32 as *const u32 as *const std::ffi::c_void,
        );
        encoder.set_bytes(
            5,
            std::mem::size_of::<u32>() as u64,
            &seq_q_u32 as *const u32 as *const std::ffi::c_void,
        );
        encoder.set_bytes(
            6,
            std::mem::size_of::<u32>() as u64,
            &seq_k_u32 as *const u32 as *const std::ffi::c_void,
        );
        encoder.set_bytes(
            7,
            std::mem::size_of::<u32>() as u64,
            &dim_u32 as *const u32 as *const std::ffi::c_void,
        );
        encoder.set_bytes(
            8,
            std::mem::size_of::<u32>() as u64,
            &dim_v_u32 as *const u32 as *const std::ffi::c_void,
        );
        encoder.set_bytes(
            9,
            std::mem::size_of::<f32>() as u64,
            &scale as *const f32 as *const std::ffi::c_void,
        );

        // Configure thread execution
        // 3D grid: (dim_v, seq_q, batch)
        let thread_group_size = metal::MTLSize::new(8, 8, 1); // 8x8x1 thread block
        let thread_groups = metal::MTLSize::new(
            dim_v.div_ceil(8) as u64,     // dim_v blocks
            seq_len_q.div_ceil(8) as u64, // seq_q blocks
            batch_size as u64,            // batch blocks
        );

        encoder.dispatch_thread_groups(thread_groups, thread_group_size);
        encoder.end_encoding();

        // Execute and wait for completion
        command_buffer.commit();
        command_buffer.wait_until_completed();

        // Convert result buffer to Tensor and copy to output
        let result_shape = vec![batch_size, seq_len_q, dim_v];
        let result_tensor = Tensor::from_metal_buffer(&output_buffer, &result_shape)?;
        *output = result_tensor;

        Ok(())
    }

    /// Get device information
    pub fn device_info(&self) -> Result<String> {
        #[cfg(all(target_os = "macos", feature = "metal"))]
        {
            Ok(format!(
                "Metal Device: {}\nUnified Memory: {} GB\nMax Threads Per Group: {}",
                self.device.name(),
                self.device.recommended_max_working_set_size() / (1024 * 1024 * 1024),
                self.device.max_threads_per_threadgroup().width
            ))
        }

        #[cfg(not(feature = "metal"))]
        {
            Ok("Metal backend not compiled".to_string())
        }
    }
}

// Placeholder implementations for when Metal feature is not enabled
#[cfg(not(feature = "metal"))]
impl MetalImpl {
    pub fn matrix_multiply(&self, _a: &Tensor, _b: &Tensor) -> Result<Tensor> {
        Err(TrustformersError::hardware_error(
            "Metal backend not compiled in this build",
            "MetalImpl::matrix_multiply",
        ))
    }

    pub fn add_tensors(&self, _a: &Tensor, _b: &Tensor) -> Result<Tensor> {
        Err(TrustformersError::hardware_error(
            "Metal backend not compiled in this build",
            "MetalImpl::add_tensors",
        ))
    }

    pub fn flash_attention(
        &self,
        _query: &Tensor,
        _key: &Tensor,
        _value: &Tensor,
        _output: &mut Tensor,
    ) -> Result<()> {
        Err(TrustformersError::hardware_error(
            "Metal backend not compiled in this build",
            "MetalImpl::flash_attention",
        ))
    }

    pub fn device_info(&self) -> Result<String> {
        Ok("Metal backend not compiled".to_string())
    }
}

// Mock MPS and Metal modules for when feature is not enabled
#[cfg(not(feature = "metal"))]
mod metal {
    pub struct Device;
    pub struct CommandQueue;
    pub struct Library;
    pub struct MTLResourceOptions;
    pub struct MTLSize;

    impl Device {
        pub fn system_default() -> Option<Self> {
            None
        }
        pub fn name(&self) -> &str {
            "Mock"
        }
        pub fn recommended_max_working_set_size(&self) -> u64 {
            0
        }
        pub fn supports_feature_set(&self, _: MTLFeatureSet) -> bool {
            false
        }
        pub fn new_command_queue(&self) -> CommandQueue {
            CommandQueue
        }
        pub fn new_library_with_source(
            &self,
            _: &str,
            _: &CompileOptions,
        ) -> Result<Library, String> {
            Err("Not supported".to_string())
        }
        pub fn new_buffer(&self, _: u64, _: MTLResourceOptions) -> metal::Buffer {
            metal::Buffer
        }
        pub fn new_compute_pipeline_state_with_function(
            &self,
            _: &Function,
        ) -> Result<ComputePipelineState, String> {
            Err("Not supported".to_string())
        }
        pub fn max_threads_per_threadgroup(&self) -> MTLSize {
            MTLSize::new(0, 0, 0)
        }
    }

    impl CommandQueue {
        pub fn new_command_buffer(&self) -> CommandBuffer {
            CommandBuffer
        }
    }

    pub struct CommandBuffer;
    impl CommandBuffer {
        pub fn new_compute_command_encoder(&self) -> ComputeCommandEncoder {
            ComputeCommandEncoder
        }
        pub fn commit(&self) {}
        pub fn wait_until_completed(&self) {}
    }

    pub struct ComputeCommandEncoder;
    impl ComputeCommandEncoder {
        pub fn set_compute_pipeline_state(&self, _: &ComputePipelineState) {}
        pub fn set_buffer(&self, _: u32, _: Option<&Buffer>, _: u64) {}
        pub fn set_bytes(&self, _: u32, _: u64, _: &u32) {}
        pub fn dispatch_thread_groups(&self, _: MTLSize, _: MTLSize) {}
        pub fn end_encoding(&self) {}
    }

    pub struct Buffer;
    pub struct Function;
    pub struct ComputePipelineState;
    pub struct CompileOptions;
    pub enum MTLFeatureSet {
        macOS_GPUFamily2_v1,
        iOS_GPUFamily4_v1,
    }

    impl CompileOptions {
        pub fn new() -> Self {
            CompileOptions
        }
    }

    impl MTLSize {
        pub fn new(_: usize, _: usize, _: usize) -> Self {
            MTLSize
        }
        pub fn width(&self) -> usize {
            0
        }
    }
}

#[cfg(not(feature = "metal"))]
mod mps {
    use std::collections::HashMap;

    pub struct MPSGraph;
    pub struct MPSGraphTensorData;
    pub enum MPSDataType {
        Float32,
    }

    impl MPSGraph {
        pub fn new() -> Self {
            MPSGraph
        }
        pub fn placeholder(&self, _: &[u64], _: MPSDataType, _: &str) -> MPSGraphTensorData {
            MPSGraphTensorData
        }
        pub fn matrix_multiplication(
            &self,
            _: &MPSGraphTensorData,
            _: &MPSGraphTensorData,
            _: &str,
        ) -> MPSGraphTensorData {
            MPSGraphTensorData
        }
        pub fn run_async(
            &self,
            _: &metal::CommandBuffer,
            _: &HashMap<String, metal::Buffer>,
            _: &[(String, MPSGraphTensorData)],
        ) -> Result<HashMap<String, metal::Buffer>, crate::errors::TrustformersError> {
            Err(crate::errors::TrustformersError::hardware_error(
                "Not supported",
                "mock",
            ))
        }
    }

    impl MPSGraphTensorData {
        pub fn new(_: &metal::Device, _: &[u64], _: MPSDataType) -> Self {
            MPSGraphTensorData
        }
    }
}

/// Extensions to Tensor for Metal integration
trait TensorMetalExt {
    fn to_metal_buffer(&self, device: &metal::Device) -> Result<metal::Buffer>;
    fn from_metal_buffer(buffer: &metal::Buffer, shape: &[usize]) -> Result<Tensor>;
}

impl TensorMetalExt for Tensor {
    #[cfg(all(target_os = "macos", feature = "metal"))]
    fn to_metal_buffer(&self, device: &metal::Device) -> Result<metal::Buffer> {
        let data = self.data()?;
        let buffer = device.new_buffer_with_data(
            data.as_ptr() as *const std::ffi::c_void,
            (data.len() * std::mem::size_of::<f32>()) as u64,
            metal::MTLResourceOptions::StorageModeShared,
        );
        Ok(buffer)
    }

    #[cfg(not(feature = "metal"))]
    fn to_metal_buffer(&self, _device: &metal::Device) -> Result<metal::Buffer> {
        Err(TrustformersError::hardware_error(
            "Metal backend not compiled",
            "TensorMetalExt::to_metal_buffer",
        ))
    }

    #[cfg(all(target_os = "macos", feature = "metal"))]
    fn from_metal_buffer(buffer: &metal::Buffer, shape: &[usize]) -> Result<Tensor> {
        let data_ptr = buffer.contents() as *const f32;
        let len = shape.iter().product::<usize>();
        let data = unsafe { std::slice::from_raw_parts(data_ptr, len) };
        Tensor::from_slice(data, shape)
    }

    #[cfg(not(feature = "metal"))]
    fn from_metal_buffer(_buffer: &metal::Buffer, _shape: &[usize]) -> Result<Tensor> {
        Err(TrustformersError::hardware_error(
            "Metal backend not compiled",
            "TensorMetalExt::from_metal_buffer",
        ))
    }
}

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

    #[test]
    fn test_metal_impl_creation() {
        // Test that MetalImpl can be created (will use mock on non-Metal systems)
        let result = MetalImpl::new();
        assert!(result.is_ok());
    }

    #[test]
    fn test_device_info() {
        let metal_impl = MetalImpl::new().expect("operation failed in test");
        let info = metal_impl.device_info().expect("operation failed in test");
        assert!(!info.is_empty());
    }

    #[cfg(all(target_os = "macos", feature = "metal"))]
    #[test]
    fn test_matrix_multiply() {
        let metal_impl = MetalImpl::new().expect("operation failed in test");

        let a =
            Tensor::from_slice(&[1.0, 2.0, 3.0, 4.0], &[2, 2]).expect("tensor operation failed");
        let b =
            Tensor::from_slice(&[5.0, 6.0, 7.0, 8.0], &[2, 2]).expect("tensor operation failed");

        let result = metal_impl.matrix_multiply(&a, &b);
        assert!(result.is_ok());

        let result_tensor = result.expect("tensor operation failed");
        assert_eq!(result_tensor.shape(), &[2, 2]);
    }

    #[cfg(all(target_os = "macos", feature = "metal"))]
    #[test]
    fn test_add_tensors() {
        let metal_impl = MetalImpl::new().expect("operation failed in test");

        let a = Tensor::from_slice(&[1.0, 2.0, 3.0, 4.0], &[4]).expect("tensor operation failed");
        let b = Tensor::from_slice(&[5.0, 6.0, 7.0, 8.0], &[4]).expect("tensor operation failed");

        let result = metal_impl.add_tensors(&a, &b);
        assert!(result.is_ok());

        let result_tensor = result.expect("tensor operation failed");
        assert_eq!(result_tensor.shape(), &[4]);

        let expected = [6.0, 8.0, 10.0, 12.0];
        let actual = result_tensor.data_f32().expect("tensor operation failed");
        for (a, e) in actual.iter().zip(expected.iter()) {
            assert!((a - e).abs() < 1e-6);
        }
    }
}