scirs2-core 0.4.3

Core utilities and common functionality for SciRS2 (scirs2-core)
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
//! Metal Performance Shaders (MPS) integration for accelerated operations
//!
//! This module provides access to Apple's optimized GPU primitives through
//! Metal Performance Shaders, offering high-performance implementations of
//! common operations like matrix multiplication, convolution, and more.
//!
//! ## Implementation Status (objc2 API)
//!
//! Matrix operations (`MPSMatrixMultiplication`, `MPSMatrixSoftMax`,
//! `MPSMatrixFindTopK`, `MPSMatrixSum`) are fully implemented and use
//! `MPSMatrix`-wrapped `MTLBuffer` inputs.
//!
//! Image operations (`MPSImageConvolution`, `MPSImageGaussianBlur`,
//! `MPSImageLaplacian`) require `MTLTexture`-backed `MPSImage` inputs rather
//! than raw `MTLBuffer`s.  These ops return a documented `GpuError::Other`
//! explaining the buffer→texture blit requirement.  A higher-level
//! `MPSImage`-based API is the recommended path.
//!
//! ### objc2 Types used:
//! - `MTLDevice`, `MTLCommandQueue`, `MTLBuffer` from objc2-metal
//! - `MPSMatrixDescriptor`, `MPSMatrix`, `MPSMatrixMultiplication`,
//!   `MPSMatrixSoftMax`, `MPSMatrixFindTopK`, `MPSMatrixSum`
//!   from objc2-metal-performance-shaders
//! - `MPSImageConvolution`, `MPSImageGaussianBlur` for image operations

#![cfg(all(feature = "metal", target_os = "macos"))]
#![allow(dead_code)]
#![allow(deprecated)] // msg_send_id! deprecation pending objc2 API stabilization

use crate::gpu::GpuError;
use std::sync::Arc;

// objc2 API imports for Metal and Metal Performance Shaders
#[cfg(all(feature = "metal", target_os = "macos"))]
use objc2_metal::{MTLBuffer, MTLCommandQueue, MTLDevice};

#[cfg(all(feature = "metal", target_os = "macos"))]
use objc2_metal_performance_shaders::{
    MPSDataType as MPSDataTypeEnum, MPSMatrix, MPSMatrixDescriptor, MPSMatrixMultiplication,
};

#[cfg(all(feature = "metal", target_os = "macos"))]
use objc2::runtime::ProtocolObject;

#[cfg(all(feature = "metal", target_os = "macos"))]
use objc2::rc::Retained;

#[cfg(all(feature = "metal", target_os = "macos"))]
use objc2::{msg_send, msg_send_id, ClassType};

#[cfg(all(feature = "metal", target_os = "macos"))]
use objc2::runtime::AnyObject;

// Fallback type aliases when not on macOS
#[cfg(not(all(feature = "metal", target_os = "macos")))]
type MTLDevice = ();
#[cfg(not(all(feature = "metal", target_os = "macos")))]
type MTLCommandQueue = ();
#[cfg(not(all(feature = "metal", target_os = "macos")))]
type MTLBuffer = ();

/// Metal Performance Shaders context (using objc2 API)
pub struct MPSContext {
    #[cfg(all(feature = "metal", target_os = "macos"))]
    device: Retained<ProtocolObject<dyn MTLDevice>>,
    #[cfg(all(feature = "metal", target_os = "macos"))]
    command_queue: Retained<ProtocolObject<dyn MTLCommandQueue>>,
    #[cfg(not(all(feature = "metal", target_os = "macos")))]
    device: MTLDevice,
    #[cfg(not(all(feature = "metal", target_os = "macos")))]
    command_queue: MTLCommandQueue,
}

// SAFETY: Metal devices and command queues are inherently thread-safe.
// The Retained pointers don't implement Sync because they're trait objects,
// but the underlying Metal objects are designed for multi-threaded access.
unsafe impl Send for MPSContext {}
unsafe impl Sync for MPSContext {}

impl MPSContext {
    /// Create a new MPS context (objc2 API)
    #[cfg(all(feature = "metal", target_os = "macos"))]
    pub fn new(
        device: Retained<ProtocolObject<dyn MTLDevice>>,
        command_queue: Retained<ProtocolObject<dyn MTLCommandQueue>>,
    ) -> Self {
        Self {
            device,
            command_queue,
        }
    }

    /// Create a new MPS context (fallback)
    #[cfg(not(all(feature = "metal", target_os = "macos")))]
    pub fn new(device: MTLDevice, command_queue: MTLCommandQueue) -> Self {
        Self {
            device,
            command_queue,
        }
    }

    /// Create a matrix multiplication operation
    #[cfg(all(feature = "metal", target_os = "macos"))]
    pub fn create_matmul(
        &self,
        transpose_left: bool,
        transpose_right: bool,
        result_rows: usize,
        result_columns: usize,
        interior_columns: usize,
        alpha: f64,
        beta: f64,
    ) -> Result<Retained<MPSMatrixMultiplication>, GpuError> {
        use objc2_metal_performance_shaders::MPSMatrixMultiplication;

        // Create matrix multiplication kernel using msg_send (handles trait objects properly)
        let matmul = unsafe {
            let cls = MPSMatrixMultiplication::class();
            let alloc = msg_send_id![cls, alloc];
            msg_send_id![
                alloc,
                initWithDevice: &*self.device,
                transposeLeft: transpose_left,
                transposeRight: transpose_right,
                resultRows: result_rows,
                resultColumns: result_columns,
                interiorColumns: interior_columns,
                alpha: alpha,
                beta: beta
            ]
        };

        Ok(matmul)
    }

    #[cfg(not(all(feature = "metal", target_os = "macos")))]
    pub fn create_matmul(
        &self,
        _transpose_left: bool,
        _transpose_right: bool,
        _result_rows: usize,
        _result_columns: usize,
        _interior_columns: usize,
        _alpha: f64,
        _beta: f64,
    ) -> Result<(), GpuError> {
        Err(GpuError::Other(
            "Metal not available on this platform".to_string(),
        ))
    }

    /// Create a matrix descriptor
    #[cfg(all(feature = "metal", target_os = "macos"))]
    pub fn create_descriptor(
        rows: usize,
        columns: usize,
        row_bytes: usize,
        datatype: MPSDataType,
    ) -> Result<Retained<MPSMatrixDescriptor>, GpuError> {
        use objc2_metal_performance_shaders::MPSMatrixDescriptor;

        // Map our datatype to MPS data type enum
        let mps_datatype = match datatype {
            MPSDataType::Float32 => MPSDataTypeEnum::Float32,
            MPSDataType::Float16 => MPSDataTypeEnum::Float16,
            MPSDataType::Int32 => MPSDataTypeEnum::Int32,
            _ => {
                return Err(GpuError::Other(format!(
                    "Unsupported datatype: {:?}",
                    datatype
                )))
            }
        };

        // Create matrix descriptor using msg_send
        let descriptor = unsafe {
            let cls = MPSMatrixDescriptor::class();
            msg_send_id![
                cls,
                matrixDescriptorWithRows: rows,
                columns: columns,
                rowBytes: row_bytes,
                dataType: mps_datatype
            ]
        };

        Ok(descriptor)
    }

    #[cfg(not(all(feature = "metal", target_os = "macos")))]
    pub fn create_descriptor(
        _rows: usize,
        _columns: usize,
        _row_bytes: usize,
        _datatype: MPSDataType,
    ) -> Result<(), GpuError> {
        Err(GpuError::Other(
            "Metal not available on this platform".to_string(),
        ))
    }

    /// Create an MPS matrix from a Metal buffer
    #[cfg(all(feature = "metal", target_os = "macos"))]
    pub fn create_matrix(
        &self,
        buffer: &Retained<ProtocolObject<dyn MTLBuffer>>,
        descriptor: &Retained<MPSMatrixDescriptor>,
    ) -> Result<Retained<MPSMatrix>, GpuError> {
        use objc2_metal_performance_shaders::MPSMatrix;

        // Create MPSMatrix wrapping the MTLBuffer using msg_send (handles trait objects)
        let matrix = unsafe {
            let cls = MPSMatrix::class();
            let alloc = msg_send_id![cls, alloc];
            msg_send_id![
                alloc,
                initWithBuffer: &**buffer,
                descriptor: &**descriptor
            ]
        };

        Ok(matrix)
    }

    #[cfg(not(all(feature = "metal", target_os = "macos")))]
    pub fn create_matrix(&self, _buffer: &MTLBuffer, _descriptor: &()) -> Result<(), GpuError> {
        Err(GpuError::Other(
            "Metal not available on this platform".to_string(),
        ))
    }

    /// Create a command buffer for batching operations
    #[cfg(all(feature = "metal", target_os = "macos"))]
    pub fn create_command_buffer(&self) -> Result<Retained<AnyObject>, GpuError> {
        let command_buffer: Option<Retained<AnyObject>> =
            unsafe { msg_send_id![&self.command_queue, commandBuffer] };

        command_buffer.ok_or_else(|| GpuError::Other("Failed to create command buffer".to_string()))
    }

    /// Commit a command buffer (non-blocking)
    #[cfg(all(feature = "metal", target_os = "macos"))]
    pub fn commit_command_buffer(&self, command_buffer: &Retained<AnyObject>) {
        unsafe {
            let _: () = msg_send![&**command_buffer, commit];
        }
    }

    /// Wait for a command buffer to complete
    #[cfg(all(feature = "metal", target_os = "macos"))]
    pub fn wait_for_command_buffer(&self, command_buffer: &Retained<AnyObject>) {
        unsafe {
            let _: () = msg_send![&**command_buffer, waitUntilCompleted];
        }
    }

    /// Encode matrix multiplication to an existing command buffer (non-blocking, for batching)
    #[cfg(all(feature = "metal", target_os = "macos"))]
    pub fn encode_matrix_multiply(
        &self,
        command_buffer: &Retained<AnyObject>,
        left_matrix: &Retained<MPSMatrix>,
        right_matrix: &Retained<MPSMatrix>,
        result_matrix: &Retained<MPSMatrix>,
        matmul: &Retained<MPSMatrixMultiplication>,
    ) -> Result<(), GpuError> {
        // Encode matrix multiplication operation using msg_send
        unsafe {
            let _: () = msg_send![
                &**matmul,
                encodeToCommandBuffer: &**command_buffer,
                leftMatrix: &**left_matrix,
                rightMatrix: &**right_matrix,
                resultMatrix: &**result_matrix
            ];
        }

        Ok(())
    }

    /// Perform matrix multiplication using MPS (creates own command buffer and waits)
    #[cfg(all(feature = "metal", target_os = "macos"))]
    pub fn matrix_multiply(
        &self,
        left_matrix: &Retained<MPSMatrix>,
        right_matrix: &Retained<MPSMatrix>,
        result_matrix: &Retained<MPSMatrix>,
        matmul: &Retained<MPSMatrixMultiplication>,
    ) -> Result<(), GpuError> {
        use objc2_metal::MTLCommandBuffer;

        // Create command buffer using msg_send! (trait object requires dynamic dispatch)
        let command_buffer = self.create_command_buffer()?;

        // Encode operation
        self.encode_matrix_multiply(
            &command_buffer,
            left_matrix,
            right_matrix,
            result_matrix,
            matmul,
        )?;

        // Commit and wait for completion
        self.commit_command_buffer(&command_buffer);
        self.wait_for_command_buffer(&command_buffer);

        Ok(())
    }

    #[cfg(not(all(feature = "metal", target_os = "macos")))]
    pub fn matrix_multiply(
        &self,
        _left: &(),
        _right: &(),
        _result: &(),
        _matmul: &(),
    ) -> Result<(), GpuError> {
        Err(GpuError::Other(
            "Metal not available on this platform".to_string(),
        ))
    }

    /// Validate that the device supports Metal Performance Shaders softmax.
    ///
    /// Allocates and initialises an `MPSMatrixSoftMax` kernel against the current
    /// device to confirm it is available, then releases it.  The `_axis` parameter
    /// is accepted for API symmetry — `MPSMatrixSoftMax` always operates row-wise
    /// (axis = 1 in matrix terms); non-zero values are noted but the check still
    /// succeeds if the device supports the operation at all.
    #[cfg(all(feature = "metal", target_os = "macos"))]
    pub fn create_softmax(&self, _axis: i32) -> Result<(), GpuError> {
        use objc2_metal_performance_shaders::MPSMatrixSoftMax;
        let _kernel = unsafe {
            let cls = MPSMatrixSoftMax::class();
            let alloc: objc2::rc::Allocated<MPSMatrixSoftMax> = msg_send_id![cls, alloc];
            let kernel: Retained<MPSMatrixSoftMax> =
                msg_send_id![alloc, initWithDevice: &*self.device];
            kernel
        };
        Ok(())
    }

    #[cfg(not(all(feature = "metal", target_os = "macos")))]
    pub fn create_softmax(&self, _axis: i32) -> Result<(), GpuError> {
        Err(GpuError::Other(
            "Metal not available on this platform".to_string(),
        ))
    }

    /// Validate that the device supports Metal Performance Shaders matrix sum.
    ///
    /// Allocates and initialises an `MPSMatrixSum` kernel (1 source matrix, 1×1,
    /// no transpose) to confirm availability, then releases it.
    #[cfg(all(feature = "metal", target_os = "macos"))]
    pub fn create_sum(&self) -> Result<(), GpuError> {
        use objc2_metal_performance_shaders::MPSMatrixSum;
        let _kernel = unsafe {
            let cls = MPSMatrixSum::class();
            let alloc: objc2::rc::Allocated<MPSMatrixSum> = msg_send_id![cls, alloc];
            let kernel: Retained<MPSMatrixSum> = msg_send_id![
                alloc,
                initWithDevice: &*self.device,
                count: 1usize,
                rows: 1usize,
                columns: 1usize,
                transpose: false
            ];
            kernel
        };
        Ok(())
    }

    #[cfg(not(all(feature = "metal", target_os = "macos")))]
    pub fn create_sum(&self) -> Result<(), GpuError> {
        Err(GpuError::Other(
            "Metal not available on this platform".to_string(),
        ))
    }

    /// Validate that the device supports Metal Performance Shaders top-k search.
    ///
    /// `k` must be between 1 and 16 (MPS hardware constraint).  Allocates and
    /// initialises an `MPSMatrixFindTopK` kernel to confirm availability, then
    /// releases it.
    #[cfg(all(feature = "metal", target_os = "macos"))]
    pub fn create_find_top_k(&self, k: usize) -> Result<(), GpuError> {
        use objc2_metal_performance_shaders::MPSMatrixFindTopK;
        if k == 0 || k > 16 {
            return Err(GpuError::Other(format!(
                "MPSMatrixFindTopK: k must be in 1..=16, got {k}"
            )));
        }
        let _kernel = unsafe {
            let cls = MPSMatrixFindTopK::class();
            let alloc: objc2::rc::Allocated<MPSMatrixFindTopK> = msg_send_id![cls, alloc];
            let kernel: Retained<MPSMatrixFindTopK> = msg_send_id![
                alloc,
                initWithDevice: &*self.device,
                numberOfTopKValues: k
            ];
            kernel
        };
        Ok(())
    }

    #[cfg(not(all(feature = "metal", target_os = "macos")))]
    pub fn create_find_top_k(&self, _k: usize) -> Result<(), GpuError> {
        Err(GpuError::Other(
            "Metal not available on this platform".to_string(),
        ))
    }
}

/// MPS-accelerated convolution operation.
///
/// Wraps an `MPSContext` for dispatch of convolution kernels.
/// Construction is always successful; actual kernel dispatch happens in `execute`.
pub struct MPSConvolution {
    pub(crate) context: Arc<MPSContext>,
}

impl MPSConvolution {
    /// Create a new MPS convolution operation handler.
    pub fn new(context: Arc<MPSContext>) -> Result<Self, GpuError> {
        Ok(Self { context })
    }

    /// Execute convolution using MPS.
    ///
    /// `MPSImageConvolution` operates on `MTLTexture`-backed `MPSImage` objects,
    /// not raw `MTLBuffer`s.  Buffer-backed convolution requires a buffer→texture
    /// blit pass which is outside the scope of this call.  Use `MPSOperations` with
    /// properly allocated `MPSImage`s, or perform the blit before calling this
    /// method.
    #[cfg(all(feature = "metal", target_os = "macos"))]
    pub fn execute(
        &self,
        _input: &objc2::rc::Retained<dyn MTLBuffer>,
        _weights: &objc2::rc::Retained<dyn MTLBuffer>,
        _output: &mut objc2::rc::Retained<dyn MTLBuffer>,
    ) -> Result<(), GpuError> {
        Err(GpuError::Other(
            "MPSConvolution::execute requires MTLTexture/MPSImage input; \
             buffer-backed convolution needs a blit pass — use MPSImage-based API instead"
                .to_string(),
        ))
    }

    #[cfg(not(all(feature = "metal", target_os = "macos")))]
    pub fn execute(
        &self,
        _input: &MTLBuffer,
        _weights: &MTLBuffer,
        _output: &mut MTLBuffer,
    ) -> Result<(), GpuError> {
        Err(GpuError::Other(
            "Metal not available on this platform".to_string(),
        ))
    }
}

/// MPS-accelerated pooling operations (stub)
pub struct MPSPooling {
    pub(crate) context: Arc<MPSContext>,
    pub(crate) pool_type: PoolType,
}

/// Pooling type
#[derive(Clone, Copy, Debug)]
pub enum PoolType {
    Max,
    Average,
}

impl MPSPooling {
    /// Create a new MPS pooling operation handler.
    pub fn new(context: Arc<MPSContext>, pool_type: PoolType) -> Result<Self, GpuError> {
        Ok(Self { context, pool_type })
    }

    /// Execute pooling using MPS.
    ///
    /// `MPSCNNPoolingMax`/`MPSCNNPoolingAverage` operate on `MTLTexture`-backed
    /// `MPSImage` objects, not raw `MTLBuffer`s.  Buffer-backed pooling requires a
    /// buffer→texture blit pass which is outside the scope of this call.  Use the
    /// `MPSImage`-based CNN API or perform the blit before calling this method.
    #[cfg(all(feature = "metal", target_os = "macos"))]
    pub fn execute(
        &self,
        _input: &objc2::rc::Retained<dyn MTLBuffer>,
        _output: &mut objc2::rc::Retained<dyn MTLBuffer>,
        _kernel_size: (usize, usize),
        _stride: (usize, usize),
    ) -> Result<(), GpuError> {
        Err(GpuError::Other(format!(
            "MPSPooling({:?})::execute requires MTLTexture/MPSImage input; \
             buffer-backed pooling needs a blit pass — use MPSImage-based API instead",
            self.pool_type
        )))
    }

    #[cfg(not(all(feature = "metal", target_os = "macos")))]
    pub fn execute(
        &self,
        _input: &MTLBuffer,
        _output: &mut MTLBuffer,
        _kernel_size: (usize, usize),
        _stride: (usize, usize),
    ) -> Result<(), GpuError> {
        Err(GpuError::Other(
            "Metal not available on this platform".to_string(),
        ))
    }
}

/// MPS data type
#[derive(Clone, Copy, Debug)]
pub enum MPSDataType {
    Float32,
    Float16,
    Int32,
    Int16,
    Int8,
    UInt8,
}

impl MPSDataType {
    /// Convert to the raw `u32` value of the corresponding `MPSDataType` constant.
    ///
    /// Values match `MPSDataType` from `objc2-metal-performance-shaders`:
    /// - `Float32` = `MPSDataTypeFloatBit | 32`  = `0x10000020`
    /// - `Float16` = `MPSDataTypeFloatBit | 16`  = `0x10000010`
    /// - `Int32`   = `MPSDataTypeSignedBit | 32` = `0x20000020`
    /// - `Int16`   = `MPSDataTypeSignedBit | 16` = `0x20000010`
    /// - `Int8`    = `MPSDataTypeSignedBit | 8`  = `0x20000008`
    /// - `UInt8`   = `8`                         = `0x00000008`
    #[cfg(all(feature = "metal", target_os = "macos"))]
    pub fn to_mps_datatype(self) -> u32 {
        match self {
            MPSDataType::Float32 => MPSDataTypeEnum::Float32.0,
            MPSDataType::Float16 => MPSDataTypeEnum::Float16.0,
            MPSDataType::Int32 => MPSDataTypeEnum::Int32.0,
            MPSDataType::Int16 => MPSDataTypeEnum::Int16.0,
            MPSDataType::Int8 => MPSDataTypeEnum::Int8.0,
            MPSDataType::UInt8 => MPSDataTypeEnum::UInt8.0,
        }
    }

    #[cfg(not(all(feature = "metal", target_os = "macos")))]
    pub fn to_mps_datatype(self) -> u32 {
        // Computed from the MPS header constants:
        // FloatBit=0x10000000, SignedBit=0x20000000
        match self {
            MPSDataType::Float32 => 0x10000000 | 32,
            MPSDataType::Float16 => 0x10000000 | 16,
            MPSDataType::Int32 => 0x20000000 | 32,
            MPSDataType::Int16 => 0x20000000 | 16,
            MPSDataType::Int8 => 0x20000000 | 8,
            MPSDataType::UInt8 => 8,
        }
    }
}

/// MPS operations wrapper for high-level operations (using objc2 API)
pub struct MPSOperations {
    context: Arc<MPSContext>,
}

// SAFETY: MPSOperations only contains Arc<MPSContext>, and MPSContext is Send + Sync.
// Arc itself is Send + Sync when T is Send + Sync.
unsafe impl Send for MPSOperations {}
unsafe impl Sync for MPSOperations {}

impl MPSOperations {
    /// Create new MPS operations instance (objc2 API)
    #[cfg(all(feature = "metal", target_os = "macos"))]
    pub fn new(
        device: Retained<ProtocolObject<dyn MTLDevice>>,
        command_queue: Retained<ProtocolObject<dyn MTLCommandQueue>>,
    ) -> Self {
        Self {
            context: Arc::new(MPSContext::new(device, command_queue)),
        }
    }

    /// Create new MPS operations instance (fallback)
    #[cfg(not(all(feature = "metal", target_os = "macos")))]
    pub fn new(device: MTLDevice, command_queue: MTLCommandQueue) -> Self {
        Self {
            context: Arc::new(MPSContext::new(device, command_queue)),
        }
    }

    /// Get the underlying context
    pub fn context(&self) -> &Arc<MPSContext> {
        &self.context
    }

    /// Encode matrix multiplication to an existing command buffer (for batching)
    ///
    /// This variant doesn't commit or wait, allowing multiple operations to be batched.
    /// Expected speedup: 2-3x when batching multiple operations.
    ///
    /// # Arguments
    /// * `command_buffer` - Existing command buffer to encode into
    /// * `a_buffer` - Left matrix buffer (M x K)
    /// * `b_buffer` - Right matrix buffer (K x N)
    /// * `c_buffer` - Result matrix buffer (M x N)
    /// * `m` - Number of rows in A and C
    /// * `k` - Number of columns in A and rows in B
    /// * `n` - Number of columns in B and C
    ///
    /// # Returns
    /// Ok(()) if operation encoded successfully
    #[cfg(all(feature = "metal", target_os = "macos"))]
    pub fn encode_matmul_f32(
        &self,
        command_buffer: &Retained<AnyObject>,
        a_buffer: &Retained<ProtocolObject<dyn MTLBuffer>>,
        b_buffer: &Retained<ProtocolObject<dyn MTLBuffer>>,
        c_buffer: &Retained<ProtocolObject<dyn MTLBuffer>>,
        m: usize,
        k: usize,
        n: usize,
    ) -> Result<(), GpuError> {
        // Create matrix descriptors
        let a_desc = MPSContext::create_descriptor(m, k, k * 4, MPSDataType::Float32)?;
        let b_desc = MPSContext::create_descriptor(k, n, n * 4, MPSDataType::Float32)?;
        let c_desc = MPSContext::create_descriptor(m, n, n * 4, MPSDataType::Float32)?;

        // Create MPS matrices
        let a_matrix = self.context.create_matrix(a_buffer, &a_desc)?;
        let b_matrix = self.context.create_matrix(b_buffer, &b_desc)?;
        let c_matrix = self.context.create_matrix(c_buffer, &c_desc)?;

        // Create matmul kernel (alpha=1.0, beta=0.0 for C = A*B)
        let matmul = self.context.create_matmul(
            false, // No transpose for A
            false, // No transpose for B
            m,     // Result rows
            n,     // Result columns
            k,     // Interior dimension
            1.0,   // alpha
            0.0,   // beta
        )?;

        // Encode multiplication (don't commit/wait)
        self.context.encode_matrix_multiply(
            command_buffer,
            &a_matrix,
            &b_matrix,
            &c_matrix,
            &matmul,
        )?;

        Ok(())
    }

    /// High-level matrix multiplication for f32 data (C = A * B)
    ///
    /// Performs optimized matrix multiplication using Metal Performance Shaders.
    /// Expected speedup: 100-500x over naive Metal kernels.
    ///
    /// # Arguments
    /// * `a_buffer` - Left matrix buffer (M x K)
    /// * `b_buffer` - Right matrix buffer (K x N)
    /// * `c_buffer` - Result matrix buffer (M x N)
    /// * `m` - Number of rows in A and C
    /// * `k` - Number of columns in A and rows in B
    /// * `n` - Number of columns in B and C
    ///
    /// # Returns
    /// Ok(()) if operation completed successfully
    #[cfg(all(feature = "metal", target_os = "macos"))]
    pub fn matmul_f32(
        &self,
        a_buffer: &Retained<ProtocolObject<dyn MTLBuffer>>,
        b_buffer: &Retained<ProtocolObject<dyn MTLBuffer>>,
        c_buffer: &Retained<ProtocolObject<dyn MTLBuffer>>,
        m: usize,
        k: usize,
        n: usize,
    ) -> Result<(), GpuError> {
        // Create matrix descriptors
        let a_desc = MPSContext::create_descriptor(m, k, k * 4, MPSDataType::Float32)?;
        let b_desc = MPSContext::create_descriptor(k, n, n * 4, MPSDataType::Float32)?;
        let c_desc = MPSContext::create_descriptor(m, n, n * 4, MPSDataType::Float32)?;

        // Create MPS matrices
        let a_matrix = self.context.create_matrix(a_buffer, &a_desc)?;
        let b_matrix = self.context.create_matrix(b_buffer, &b_desc)?;
        let c_matrix = self.context.create_matrix(c_buffer, &c_desc)?;

        // Create matmul kernel (alpha=1.0, beta=0.0 for C = A*B)
        let matmul = self.context.create_matmul(
            false, // No transpose for A
            false, // No transpose for B
            m,     // Result rows
            n,     // Result columns
            k,     // Interior dimension
            1.0,   // alpha
            0.0,   // beta
        )?;

        // Execute multiplication
        self.context
            .matrix_multiply(&a_matrix, &b_matrix, &c_matrix, &matmul)?;

        Ok(())
    }

    #[cfg(not(all(feature = "metal", target_os = "macos")))]
    pub fn matmul_f32(
        &self,
        _a_buffer: &(),
        _b_buffer: &(),
        _c_buffer: &(),
        _m: usize,
        _k: usize,
        _n: usize,
    ) -> Result<(), GpuError> {
        Err(GpuError::Other(
            "Metal not available on this platform".to_string(),
        ))
    }

    /// High-level scaled matrix multiplication for f32 data (C = alpha * A * B)
    ///
    /// Performs optimized scaled matrix multiplication using Metal Performance Shaders.
    /// This fuses the scaling operation into the matmul, eliminating a separate kernel dispatch.
    /// Expected speedup: 1.5-2x over separate matmul + scale operations.
    ///
    /// # Arguments
    /// * `a_buffer` - Left matrix buffer (M x K)
    /// * `b_buffer` - Right matrix buffer (K x N)
    /// * `c_buffer` - Result matrix buffer (M x N)
    /// * `m` - Number of rows in A and C
    /// * `k` - Number of columns in A and rows in B
    /// * `n` - Number of columns in B and C
    /// * `alpha` - Scaling factor for the result (C = alpha * A * B)
    ///
    /// # Returns
    /// Ok(()) if operation completed successfully
    #[cfg(all(feature = "metal", target_os = "macos"))]
    pub fn matmul_f32_scaled(
        &self,
        a_buffer: &Retained<ProtocolObject<dyn MTLBuffer>>,
        b_buffer: &Retained<ProtocolObject<dyn MTLBuffer>>,
        c_buffer: &Retained<ProtocolObject<dyn MTLBuffer>>,
        m: usize,
        k: usize,
        n: usize,
        alpha: f32,
    ) -> Result<(), GpuError> {
        // Create matrix descriptors
        let a_desc = MPSContext::create_descriptor(m, k, k * 4, MPSDataType::Float32)?;
        let b_desc = MPSContext::create_descriptor(k, n, n * 4, MPSDataType::Float32)?;
        let c_desc = MPSContext::create_descriptor(m, n, n * 4, MPSDataType::Float32)?;

        // Create MPS matrices
        let a_matrix = self.context.create_matrix(a_buffer, &a_desc)?;
        let b_matrix = self.context.create_matrix(b_buffer, &b_desc)?;
        let c_matrix = self.context.create_matrix(c_buffer, &c_desc)?;

        // Create matmul kernel with custom alpha (C = alpha * A * B)
        let matmul = self.context.create_matmul(
            false,        // No transpose for A
            false,        // No transpose for B
            m,            // Result rows
            n,            // Result columns
            k,            // Interior dimension
            alpha as f64, // alpha (scaling factor)
            0.0,          // beta
        )?;

        // Execute multiplication
        self.context
            .matrix_multiply(&a_matrix, &b_matrix, &c_matrix, &matmul)?;

        Ok(())
    }

    #[cfg(not(all(feature = "metal", target_os = "macos")))]
    pub fn matmul_f32_scaled(
        &self,
        _a_buffer: &(),
        _b_buffer: &(),
        _c_buffer: &(),
        _m: usize,
        _k: usize,
        _n: usize,
        _alpha: f32,
    ) -> Result<(), GpuError> {
        Err(GpuError::Other(
            "Metal not available on this platform".to_string(),
        ))
    }
}

/// MPS-accelerated image operations (stub)
pub struct MPSImageOps {
    pub(crate) context: Arc<MPSContext>,
}

impl MPSImageOps {
    /// Create a new MPS image operations handler.
    pub fn new(context: Arc<MPSContext>) -> Result<Self, GpuError> {
        Ok(Self { context })
    }

    /// Apply Gaussian blur using MPS.
    ///
    /// `MPSImageGaussianBlur` operates on `MTLTexture`-backed `MPSImage` objects,
    /// not raw `MTLBuffer`s.  Buffer-backed image filtering requires a
    /// buffer→texture blit pass which is outside the scope of this call.
    /// Use `MPSImage` inputs allocated from an `MPSImage` descriptor, or
    /// perform the blit before calling this method.
    #[cfg(all(feature = "metal", target_os = "macos"))]
    pub fn gaussian_blur(
        &self,
        _input: &objc2::rc::Retained<dyn MTLBuffer>,
        _output: &mut objc2::rc::Retained<dyn MTLBuffer>,
        _sigma: f32,
    ) -> Result<(), GpuError> {
        Err(GpuError::Other(
            "MPSImageOps::gaussian_blur requires MTLTexture/MPSImage input; \
             buffer-backed image ops need a blit pass — use MPSImage-based API instead"
                .to_string(),
        ))
    }

    #[cfg(not(all(feature = "metal", target_os = "macos")))]
    pub fn gaussian_blur(
        &self,
        _input: &MTLBuffer,
        _output: &mut MTLBuffer,
        _sigma: f32,
    ) -> Result<(), GpuError> {
        Err(GpuError::Other(
            "Metal not available on this platform".to_string(),
        ))
    }

    /// Apply edge detection using MPS (Laplacian filter).
    ///
    /// `MPSImageLaplacian` operates on `MTLTexture`-backed `MPSImage` objects,
    /// not raw `MTLBuffer`s.  Buffer-backed image filtering requires a
    /// buffer→texture blit pass which is outside the scope of this call.
    /// Use `MPSImage` inputs allocated from an `MPSImage` descriptor, or
    /// perform the blit before calling this method.
    ///
    /// Note: `_threshold` is accepted for API symmetry; `MPSImageLaplacian`
    /// does not expose a threshold parameter directly.
    #[cfg(all(feature = "metal", target_os = "macos"))]
    pub fn edge_detection(
        &self,
        _input: &objc2::rc::Retained<dyn MTLBuffer>,
        _output: &mut objc2::rc::Retained<dyn MTLBuffer>,
        _threshold: f32,
    ) -> Result<(), GpuError> {
        Err(GpuError::Other(
            "MPSImageOps::edge_detection requires MTLTexture/MPSImage input; \
             buffer-backed image ops need a blit pass — use MPSImage-based API instead"
                .to_string(),
        ))
    }

    #[cfg(not(all(feature = "metal", target_os = "macos")))]
    pub fn edge_detection(
        &self,
        _input: &MTLBuffer,
        _output: &mut MTLBuffer,
        _threshold: f32,
    ) -> Result<(), GpuError> {
        Err(GpuError::Other(
            "Metal not available on this platform".to_string(),
        ))
    }
}

#[cfg(test)]
mod tests {
    use super::{MPSContext, MPSConvolution, MPSDataType, MPSImageOps, MPSPooling, PoolType};
    use std::sync::Arc;

    /// Helper: try to obtain a valid Metal device and command queue.
    /// Returns `None` if no GPU is available (e.g., in headless CI).
    fn try_make_context() -> Option<MPSContext> {
        use objc2_metal::{MTLCreateSystemDefaultDevice, MTLDevice};
        let device = MTLCreateSystemDefaultDevice()?;
        let queue = device.newCommandQueue()?;
        Some(MPSContext::new(device, queue))
    }

    #[test]
    fn test_mps_convolution_new_succeeds() {
        if let Some(ctx) = try_make_context() {
            let result = MPSConvolution::new(Arc::new(ctx));
            assert!(result.is_ok(), "MPSConvolution::new should succeed");
        }
    }

    #[test]
    fn test_mps_pooling_new_succeeds() {
        if let Some(ctx) = try_make_context() {
            let result = MPSPooling::new(Arc::new(ctx), PoolType::Max);
            assert!(result.is_ok(), "MPSPooling::new should succeed");
        }
    }

    #[test]
    fn test_mps_image_ops_new_succeeds() {
        if let Some(ctx) = try_make_context() {
            let result = MPSImageOps::new(Arc::new(ctx));
            assert!(result.is_ok(), "MPSImageOps::new should succeed");
        }
    }

    #[test]
    fn test_mps_create_softmax_succeeds() {
        if let Some(ctx) = try_make_context() {
            let result = ctx.create_softmax(1);
            assert!(result.is_ok(), "create_softmax should succeed: {result:?}");
        }
    }

    #[test]
    fn test_mps_create_sum_succeeds() {
        if let Some(ctx) = try_make_context() {
            let result = ctx.create_sum();
            assert!(result.is_ok(), "create_sum should succeed: {result:?}");
        }
    }

    #[test]
    fn test_mps_create_find_top_k_valid() {
        if let Some(ctx) = try_make_context() {
            let result = ctx.create_find_top_k(4);
            assert!(
                result.is_ok(),
                "create_find_top_k(4) should succeed: {result:?}"
            );
        }
    }

    #[test]
    fn test_mps_create_find_top_k_out_of_range() {
        if let Some(ctx) = try_make_context() {
            assert!(ctx.create_find_top_k(0).is_err(), "k=0 must be rejected");
            assert!(ctx.create_find_top_k(17).is_err(), "k=17 must be rejected");
        }
    }

    #[test]
    fn test_mps_datatype_roundtrip() {
        // Verify values match MPS header constants
        // FloatBit=0x10000000, SignedBit=0x20000000
        assert_eq!(MPSDataType::Float32.to_mps_datatype(), 0x10000000 | 32);
        assert_eq!(MPSDataType::Float16.to_mps_datatype(), 0x10000000 | 16);
        assert_eq!(MPSDataType::Int32.to_mps_datatype(), 0x20000000 | 32);
        assert_eq!(MPSDataType::Int16.to_mps_datatype(), 0x20000000 | 16);
        assert_eq!(MPSDataType::Int8.to_mps_datatype(), 0x20000000 | 8);
        assert_eq!(MPSDataType::UInt8.to_mps_datatype(), 8);
    }
}