safemlx 0.2.2

Low-level MLX execution layer used by the Eredu model runtime
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
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
//! Bindings to MLX fast operators and custom-kernel handles.

use std::{
    ffi::{c_char, CStr, CString},
    fmt,
};

use crate::error::{Exception, Result};
use crate::ops::{concatenate_axis, indexing::TryIndexOp};
use crate::utils::guard::Guarded;
use crate::utils::{IntoOption, VectorArray, SUCCESS};
use crate::{Array, Dtype, Stream};
use safemlx_internal_macros::generate_macro;

/// A compiled custom Metal kernel.
///
/// The kernel owns the underlying MLX fast-metal handle and can be applied
/// repeatedly with different inputs and [`CustomKernelConfig`] values.
pub struct MetalKernel {
    c_kernel: safemlx_sys::mlx_fast_metal_kernel,
    name: String,
    input_names: Vec<String>,
    output_names: Vec<String>,
}

impl fmt::Debug for MetalKernel {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("MetalKernel")
            .field("name", &self.name)
            .field("input_names", &self.input_names)
            .field("output_names", &self.output_names)
            .finish_non_exhaustive()
    }
}

impl MetalKernel {
    /// Create a new custom Metal kernel.
    ///
    /// `input_names` and `output_names` must match the argument names used by
    /// `source` and `header`. `ensure_row_contiguous` asks MLX to make inputs
    /// row-contiguous before dispatch. `atomic_outputs` marks outputs as using
    /// atomic writes.
    pub fn new<Name, Inputs, InputName, Outputs, OutputName, Source, Header>(
        name: Name,
        input_names: Inputs,
        output_names: Outputs,
        source: Source,
        header: Header,
        ensure_row_contiguous: bool,
        atomic_outputs: bool,
    ) -> Result<Self>
    where
        Name: Into<String>,
        Inputs: IntoIterator<Item = InputName>,
        InputName: Into<String>,
        Outputs: IntoIterator<Item = OutputName>,
        OutputName: Into<String>,
        Source: Into<String>,
        Header: Into<String>,
    {
        crate::error::ensure_mlx_error_handler();

        let name = name.into();
        let input_names: Vec<String> = input_names.into_iter().map(Into::into).collect();
        let output_names: Vec<String> = output_names.into_iter().map(Into::into).collect();
        let source = source.into();
        let header = header.into();

        let c_name = cstring(&name)?;
        let c_source = cstring(&source)?;
        let c_header = cstring(&header)?;
        let c_input_names = VectorString::try_from_strings(&input_names)?;
        let c_output_names = VectorString::try_from_strings(&output_names)?;

        let c_kernel = unsafe {
            safemlx_sys::mlx_fast_metal_kernel_new(
                c_name.as_ptr(),
                c_input_names.as_ptr(),
                c_output_names.as_ptr(),
                c_source.as_ptr(),
                c_header.as_ptr(),
                ensure_row_contiguous,
                atomic_outputs,
            )
        };

        if c_kernel.ctx.is_null() {
            let what = crate::error::get_and_clear_last_mlx_error()
                .map(|e| e.what)
                .unwrap_or_else(|| "failed to create Metal kernel".to_string());
            return Err(Exception::custom(what));
        }

        Ok(Self {
            c_kernel,
            name,
            input_names,
            output_names,
        })
    }

    /// Apply the kernel on `stream`.
    ///
    /// Returns one [`Array`] for each output declared in `config`.
    pub fn apply_device<I, A>(
        &self,
        inputs: I,
        config: &CustomKernelConfig,
        stream: impl AsRef<Stream>,
    ) -> Result<Vec<Array>>
    where
        I: IntoIterator<Item = A>,
        A: AsRef<Array>,
    {
        let inputs = VectorArray::try_from_iter(inputs.into_iter())?;
        let raw_config = RawMetalKernelConfig::try_from_config(config)?;
        let outputs = Vec::<Array>::try_from_op(|outputs| unsafe {
            safemlx_sys::mlx_fast_metal_kernel_apply(
                outputs,
                self.c_kernel,
                inputs.as_ptr(),
                raw_config.as_ptr(),
                stream.as_ref().as_ptr(),
            )
        })?;

        if outputs.len() != config.output_count() {
            return Err(Exception::custom(format!(
                "Metal kernel returned {} outputs, expected {}",
                outputs.len(),
                config.output_count()
            )));
        }

        Ok(outputs)
    }

    /// Apply the kernel on `stream` and require exactly one output.
    pub fn apply_one_device<I, A>(
        &self,
        inputs: I,
        config: &CustomKernelConfig,
        stream: impl AsRef<Stream>,
    ) -> Result<Array>
    where
        I: IntoIterator<Item = A>,
        A: AsRef<Array>,
    {
        let mut outputs = self.apply_device(inputs, config, stream)?;
        match outputs.len() {
            1 => Ok(outputs.remove(0)),
            n => Err(Exception::custom(format!(
                "Metal kernel returned {n} outputs, expected 1"
            ))),
        }
    }
}

impl Drop for MetalKernel {
    fn drop(&mut self) {
        unsafe {
            safemlx_sys::mlx_fast_metal_kernel_free(self.c_kernel);
        }
    }
}

/// A JIT-compiled custom CUDA kernel.
#[cfg(feature = "cuda")]
pub struct CudaKernel {
    c_kernel: safemlx_sys::mlx_fast_cuda_kernel,
    name: String,
    input_names: Vec<String>,
    output_names: Vec<String>,
}

#[cfg(feature = "cuda")]
impl fmt::Debug for CudaKernel {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("CudaKernel")
            .field("name", &self.name)
            .field("input_names", &self.input_names)
            .field("output_names", &self.output_names)
            .finish_non_exhaustive()
    }
}

#[cfg(feature = "cuda")]
impl CudaKernel {
    /// Creates a custom CUDA kernel from a function body and optional header.
    pub fn new<Name, Inputs, InputName, Outputs, OutputName, Source, Header>(
        name: Name,
        input_names: Inputs,
        output_names: Outputs,
        source: Source,
        header: Header,
        ensure_row_contiguous: bool,
        shared_memory: i32,
    ) -> Result<Self>
    where
        Name: Into<String>,
        Inputs: IntoIterator<Item = InputName>,
        InputName: Into<String>,
        Outputs: IntoIterator<Item = OutputName>,
        OutputName: Into<String>,
        Source: Into<String>,
        Header: Into<String>,
    {
        crate::error::ensure_mlx_error_handler();

        let name = name.into();
        let input_names: Vec<String> = input_names.into_iter().map(Into::into).collect();
        let output_names: Vec<String> = output_names.into_iter().map(Into::into).collect();
        let source = source.into();
        let header = header.into();
        let c_name = cstring(&name)?;
        let c_source = cstring(&source)?;
        let c_header = cstring(&header)?;
        let c_input_names = VectorString::try_from_strings(&input_names)?;
        let c_output_names = VectorString::try_from_strings(&output_names)?;
        let c_kernel = unsafe {
            safemlx_sys::mlx_fast_cuda_kernel_new(
                c_name.as_ptr(),
                c_input_names.as_ptr(),
                c_output_names.as_ptr(),
                c_source.as_ptr(),
                c_header.as_ptr(),
                ensure_row_contiguous,
                shared_memory,
            )
        };
        if c_kernel.ctx.is_null() {
            let what = crate::error::get_and_clear_last_mlx_error()
                .map(|error| error.what)
                .unwrap_or_else(|| "failed to create CUDA kernel".to_string());
            return Err(Exception::custom(what));
        }
        Ok(Self {
            c_kernel,
            name,
            input_names,
            output_names,
        })
    }

    /// Applies the kernel and returns all configured outputs.
    pub fn apply_device<I, A>(
        &self,
        inputs: I,
        config: &CustomKernelConfig,
        stream: impl AsRef<Stream>,
    ) -> Result<Vec<Array>>
    where
        I: IntoIterator<Item = A>,
        A: AsRef<Array>,
    {
        let inputs = VectorArray::try_from_iter(inputs.into_iter())?;
        let raw_config = RawCudaKernelConfig::try_from_config(config)?;
        let outputs = Vec::<Array>::try_from_op(|outputs| unsafe {
            safemlx_sys::mlx_fast_cuda_kernel_apply(
                outputs,
                self.c_kernel,
                inputs.as_ptr(),
                raw_config.as_ptr(),
                stream.as_ref().as_ptr(),
            )
        })?;
        if outputs.len() != config.output_count() {
            return Err(Exception::custom(format!(
                "CUDA kernel returned {} outputs, expected {}",
                outputs.len(),
                config.output_count()
            )));
        }
        Ok(outputs)
    }

    /// Applies the kernel and requires exactly one configured output.
    pub fn apply_one_device<I, A>(
        &self,
        inputs: I,
        config: &CustomKernelConfig,
        stream: impl AsRef<Stream>,
    ) -> Result<Array>
    where
        I: IntoIterator<Item = A>,
        A: AsRef<Array>,
    {
        let mut outputs = self.apply_device(inputs, config, stream)?;
        match outputs.len() {
            1 => Ok(outputs.remove(0)),
            count => Err(Exception::custom(format!(
                "CUDA kernel returned {count} outputs, expected 1"
            ))),
        }
    }
}

#[cfg(feature = "cuda")]
impl Drop for CudaKernel {
    fn drop(&mut self) {
        unsafe {
            safemlx_sys::mlx_fast_cuda_kernel_free(self.c_kernel);
        }
    }
}

/// Output declaration for a custom kernel.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CustomKernelOutput {
    /// Output shape.
    pub shape: Vec<i32>,

    /// Output dtype.
    pub dtype: Dtype,
}

impl CustomKernelOutput {
    /// Create an output declaration.
    pub fn new(shape: impl Into<Vec<i32>>, dtype: Dtype) -> Self {
        Self {
            shape: shape.into(),
            dtype,
        }
    }
}

/// Template argument for a custom kernel.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CustomKernelTemplateArg {
    /// Dtype template argument.
    Dtype {
        /// Template parameter name.
        name: String,

        /// Template dtype value.
        dtype: Dtype,
    },

    /// Integer template argument.
    Int {
        /// Template parameter name.
        name: String,

        /// Template integer value.
        value: i32,
    },

    /// Boolean template argument.
    Bool {
        /// Template parameter name.
        name: String,

        /// Template boolean value.
        value: bool,
    },
}

/// Dispatch configuration shared by custom Metal and CUDA kernels.
///
/// Both backends expose the same output, grid, thread-group, initialization,
/// verbosity, and template-argument controls in MLX.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct CustomKernelConfig {
    outputs: Vec<CustomKernelOutput>,
    template_args: Vec<CustomKernelTemplateArg>,
    grid: Option<[i32; 3]>,
    thread_group: Option<[i32; 3]>,
    init_value: Option<f32>,
    verbose: bool,
}

impl CustomKernelConfig {
    /// Create an empty dispatch configuration.
    pub fn new() -> Self {
        Self::default()
    }

    /// Return the declared output count.
    pub fn output_count(&self) -> usize {
        self.outputs.len()
    }

    /// Return the output declarations.
    pub fn outputs(&self) -> &[CustomKernelOutput] {
        &self.outputs
    }

    /// Return the template arguments.
    pub fn template_args(&self) -> &[CustomKernelTemplateArg] {
        &self.template_args
    }

    /// Add an output declaration.
    pub fn add_output_arg(&mut self, shape: impl Into<Vec<i32>>, dtype: Dtype) -> &mut Self {
        self.outputs.push(CustomKernelOutput::new(shape, dtype));
        self
    }

    /// Add an output declaration and return the updated config.
    pub fn with_output_arg(mut self, shape: impl Into<Vec<i32>>, dtype: Dtype) -> Self {
        self.add_output_arg(shape, dtype);
        self
    }

    /// Set the dispatch grid dimensions.
    pub fn set_grid(&mut self, grid: [i32; 3]) -> &mut Self {
        self.grid = Some(grid);
        self
    }

    /// Set the dispatch grid dimensions and return the updated config.
    pub fn with_grid(mut self, grid: [i32; 3]) -> Self {
        self.set_grid(grid);
        self
    }

    /// Set the thread-group dimensions.
    pub fn set_thread_group(&mut self, thread_group: [i32; 3]) -> &mut Self {
        self.thread_group = Some(thread_group);
        self
    }

    /// Set the thread-group dimensions and return the updated config.
    pub fn with_thread_group(mut self, thread_group: [i32; 3]) -> Self {
        self.set_thread_group(thread_group);
        self
    }

    /// Set the output initialization value used by MLX.
    pub fn set_init_value(&mut self, value: f32) -> &mut Self {
        self.init_value = Some(value);
        self
    }

    /// Set the output initialization value and return the updated config.
    pub fn with_init_value(mut self, value: f32) -> Self {
        self.set_init_value(value);
        self
    }

    /// Enable or disable verbose MLX kernel logging.
    pub fn set_verbose(&mut self, verbose: bool) -> &mut Self {
        self.verbose = verbose;
        self
    }

    /// Enable or disable verbose MLX kernel logging and return the updated config.
    pub fn with_verbose(mut self, verbose: bool) -> Self {
        self.set_verbose(verbose);
        self
    }

    /// Add a dtype template argument.
    pub fn add_template_arg_dtype(&mut self, name: impl Into<String>, dtype: Dtype) -> &mut Self {
        self.template_args.push(CustomKernelTemplateArg::Dtype {
            name: name.into(),
            dtype,
        });
        self
    }

    /// Add a dtype template argument and return the updated config.
    pub fn with_template_arg_dtype(mut self, name: impl Into<String>, dtype: Dtype) -> Self {
        self.add_template_arg_dtype(name, dtype);
        self
    }

    /// Add an integer template argument.
    pub fn add_template_arg_int(&mut self, name: impl Into<String>, value: i32) -> &mut Self {
        self.template_args.push(CustomKernelTemplateArg::Int {
            name: name.into(),
            value,
        });
        self
    }

    /// Add an integer template argument and return the updated config.
    pub fn with_template_arg_int(mut self, name: impl Into<String>, value: i32) -> Self {
        self.add_template_arg_int(name, value);
        self
    }

    /// Add a boolean template argument.
    pub fn add_template_arg_bool(&mut self, name: impl Into<String>, value: bool) -> &mut Self {
        self.template_args.push(CustomKernelTemplateArg::Bool {
            name: name.into(),
            value,
        });
        self
    }

    /// Add a boolean template argument and return the updated config.
    pub fn with_template_arg_bool(mut self, name: impl Into<String>, value: bool) -> Self {
        self.add_template_arg_bool(name, value);
        self
    }
}

struct RawMetalKernelConfig {
    c_config: safemlx_sys::mlx_fast_metal_kernel_config,
}

impl RawMetalKernelConfig {
    fn try_from_config(config: &CustomKernelConfig) -> Result<Self> {
        crate::error::ensure_mlx_error_handler();

        let c_config = unsafe { safemlx_sys::mlx_fast_metal_kernel_config_new() };
        if c_config.ctx.is_null() {
            let what = crate::error::get_and_clear_last_mlx_error()
                .map(|e| e.what)
                .unwrap_or_else(|| "failed to create Metal kernel config".to_string());
            return Err(Exception::custom(what));
        }

        let raw = Self { c_config };
        raw.populate(config)?;
        Ok(raw)
    }

    fn as_ptr(&self) -> safemlx_sys::mlx_fast_metal_kernel_config {
        self.c_config
    }

    fn populate(&self, config: &CustomKernelConfig) -> Result<()> {
        for output in &config.outputs {
            check_status(unsafe {
                safemlx_sys::mlx_fast_metal_kernel_config_add_output_arg(
                    self.c_config,
                    output.shape.as_ptr(),
                    output.shape.len(),
                    output.dtype.into(),
                )
            })?;
        }

        if let Some([x, y, z]) = config.grid {
            check_status(unsafe {
                safemlx_sys::mlx_fast_metal_kernel_config_set_grid(self.c_config, x, y, z)
            })?;
        }

        if let Some([x, y, z]) = config.thread_group {
            check_status(unsafe {
                safemlx_sys::mlx_fast_metal_kernel_config_set_thread_group(self.c_config, x, y, z)
            })?;
        }

        if let Some(value) = config.init_value {
            check_status(unsafe {
                safemlx_sys::mlx_fast_metal_kernel_config_set_init_value(self.c_config, value)
            })?;
        }

        check_status(unsafe {
            safemlx_sys::mlx_fast_metal_kernel_config_set_verbose(self.c_config, config.verbose)
        })?;

        for template_arg in &config.template_args {
            match template_arg {
                CustomKernelTemplateArg::Dtype { name, dtype } => {
                    let name = cstring(name)?;
                    check_status(unsafe {
                        safemlx_sys::mlx_fast_metal_kernel_config_add_template_arg_dtype(
                            self.c_config,
                            name.as_ptr(),
                            (*dtype).into(),
                        )
                    })?;
                }
                CustomKernelTemplateArg::Int { name, value } => {
                    let name = cstring(name)?;
                    check_status(unsafe {
                        safemlx_sys::mlx_fast_metal_kernel_config_add_template_arg_int(
                            self.c_config,
                            name.as_ptr(),
                            *value,
                        )
                    })?;
                }
                CustomKernelTemplateArg::Bool { name, value } => {
                    let name = cstring(name)?;
                    check_status(unsafe {
                        safemlx_sys::mlx_fast_metal_kernel_config_add_template_arg_bool(
                            self.c_config,
                            name.as_ptr(),
                            *value,
                        )
                    })?;
                }
            }
        }

        Ok(())
    }
}

impl Drop for RawMetalKernelConfig {
    fn drop(&mut self) {
        unsafe {
            safemlx_sys::mlx_fast_metal_kernel_config_free(self.c_config);
        }
    }
}

#[cfg(feature = "cuda")]
struct RawCudaKernelConfig {
    c_config: safemlx_sys::mlx_fast_cuda_kernel_config,
}

#[cfg(feature = "cuda")]
impl RawCudaKernelConfig {
    fn try_from_config(config: &CustomKernelConfig) -> Result<Self> {
        crate::error::ensure_mlx_error_handler();
        let c_config = unsafe { safemlx_sys::mlx_fast_cuda_kernel_config_new() };
        if c_config.ctx.is_null() {
            let what = crate::error::get_and_clear_last_mlx_error()
                .map(|error| error.what)
                .unwrap_or_else(|| "failed to create CUDA kernel config".to_string());
            return Err(Exception::custom(what));
        }
        let raw = Self { c_config };
        raw.populate(config)?;
        Ok(raw)
    }

    fn as_ptr(&self) -> safemlx_sys::mlx_fast_cuda_kernel_config {
        self.c_config
    }

    fn populate(&self, config: &CustomKernelConfig) -> Result<()> {
        for output in &config.outputs {
            check_status(unsafe {
                safemlx_sys::mlx_fast_cuda_kernel_config_add_output_arg(
                    self.c_config,
                    output.shape.as_ptr(),
                    output.shape.len(),
                    output.dtype.into(),
                )
            })?;
        }
        if let Some([x, y, z]) = config.grid {
            check_status(unsafe {
                safemlx_sys::mlx_fast_cuda_kernel_config_set_grid(self.c_config, x, y, z)
            })?;
        }
        if let Some([x, y, z]) = config.thread_group {
            check_status(unsafe {
                safemlx_sys::mlx_fast_cuda_kernel_config_set_thread_group(self.c_config, x, y, z)
            })?;
        }
        if let Some(value) = config.init_value {
            check_status(unsafe {
                safemlx_sys::mlx_fast_cuda_kernel_config_set_init_value(self.c_config, value)
            })?;
        }
        check_status(unsafe {
            safemlx_sys::mlx_fast_cuda_kernel_config_set_verbose(self.c_config, config.verbose)
        })?;
        for template_arg in &config.template_args {
            match template_arg {
                CustomKernelTemplateArg::Dtype { name, dtype } => {
                    let name = cstring(name)?;
                    check_status(unsafe {
                        safemlx_sys::mlx_fast_cuda_kernel_config_add_template_arg_dtype(
                            self.c_config,
                            name.as_ptr(),
                            (*dtype).into(),
                        )
                    })?;
                }
                CustomKernelTemplateArg::Int { name, value } => {
                    let name = cstring(name)?;
                    check_status(unsafe {
                        safemlx_sys::mlx_fast_cuda_kernel_config_add_template_arg_int(
                            self.c_config,
                            name.as_ptr(),
                            *value,
                        )
                    })?;
                }
                CustomKernelTemplateArg::Bool { name, value } => {
                    let name = cstring(name)?;
                    check_status(unsafe {
                        safemlx_sys::mlx_fast_cuda_kernel_config_add_template_arg_bool(
                            self.c_config,
                            name.as_ptr(),
                            *value,
                        )
                    })?;
                }
            }
        }
        Ok(())
    }
}

#[cfg(feature = "cuda")]
impl Drop for RawCudaKernelConfig {
    fn drop(&mut self) {
        unsafe {
            safemlx_sys::mlx_fast_cuda_kernel_config_free(self.c_config);
        }
    }
}

struct VectorString {
    c_vec: safemlx_sys::mlx_vector_string,
    _strings: Vec<CString>,
}

impl VectorString {
    fn try_from_strings(strings: &[String]) -> Result<Self> {
        let mut c_strings = Vec::with_capacity(strings.len());
        for string in strings {
            c_strings.push(cstring(string)?);
        }

        let mut c_ptrs: Vec<*const c_char> = c_strings.iter().map(|s| s.as_ptr()).collect();
        let c_vec =
            unsafe { safemlx_sys::mlx_vector_string_new_data(c_ptrs.as_mut_ptr(), c_ptrs.len()) };

        Ok(Self {
            c_vec,
            _strings: c_strings,
        })
    }

    fn as_ptr(&self) -> safemlx_sys::mlx_vector_string {
        self.c_vec
    }
}

impl Drop for VectorString {
    fn drop(&mut self) {
        let status = unsafe { safemlx_sys::mlx_vector_string_free(self.c_vec) };
        debug_assert_eq!(status, SUCCESS);
    }
}

fn cstring(value: &str) -> Result<CString> {
    CString::new(value).map_err(|e| Exception::custom(format!("{e}")))
}

fn check_status(status: i32) -> Result<()> {
    match status {
        SUCCESS => Ok(()),
        _ => {
            let what = crate::error::get_and_clear_last_mlx_error()
                .map(|e| e.what)
                .unwrap_or_else(|| "MLX operation failed but no error was set".to_string());
            Err(Exception::custom(what))
        }
    }
}

/// Optimized implementation of `NN.RoPE`.
#[allow(clippy::too_many_arguments)]
#[generate_macro(customize(root = "$crate::fast"))]
pub fn rope<'a>(
    #[named] array: impl AsRef<Array>,
    #[named] dimensions: i32,
    #[named] traditional: bool,
    #[optional] base: impl Into<Option<f32>>,
    #[named] scale: f32,
    #[named] offset: i32,
    #[optional] freqs: impl Into<Option<&'a Array>>,
    #[optional] stream: impl AsRef<Stream>,
) -> Result<Array> {
    let stream = stream.as_ref();
    let array = array.as_ref();
    let base = base.into();
    let base = safemlx_sys::mlx_optional_float {
        value: base.unwrap_or(0.0),
        has_value: base.is_some(),
    };
    let freqs = freqs.into();
    let batches = if array.ndim() > 2 { array.dim(0) } else { 1 };
    let mut outputs = Vec::with_capacity(batches as usize);
    for index in 0..batches {
        let input = if batches == 1 {
            array.clone()
        } else {
            array.try_index_device(index..index + 1, stream)?
        };
        outputs.push(Array::try_from_op(|res| unsafe {
            safemlx_sys::mlx_fast_rope(
                res,
                input.as_ptr(),
                dimensions,
                traditional,
                base,
                scale,
                offset,
                freqs
                    .map(|a| a.as_ptr())
                    .unwrap_or(safemlx_sys::mlx_array_new()),
                stream.as_ptr(),
            )
        })?);
    }
    let output = if outputs.len() == 1 {
        outputs.pop().expect("RoPE always produces one output")
    } else {
        concatenate_axis(&outputs, 0, stream)?
    };
    Ok(output)
}

/// Optimized implementation of `NN.RoPE` with dynamic (array) offset.
///
/// This variant allows specifying the offset as an array, enabling different
/// offsets for different positions in the input.
///
/// # Params
///
/// - `array`: Input array
/// - `dimensions`: The feature dimensions to apply rope to
/// - `traditional`: If true, uses the traditional rope implementation
/// - `base`: The base used to compute angular frequency for each dimension
/// - `scale`: The scale to apply to the positions
/// - `offset`: An array of position offsets
/// - `freqs`: Optional precomputed frequencies
/// - `stream`: Stream to evaluate on
#[allow(clippy::too_many_arguments)]
#[generate_macro(customize(root = "$crate::fast"))]
pub fn rope_dynamic<'a>(
    #[named] array: impl AsRef<Array>,
    #[named] dimensions: i32,
    #[named] traditional: bool,
    #[optional] base: impl Into<Option<f32>>,
    #[named] scale: f32,
    #[named] offset: impl AsRef<Array>,
    #[optional] freqs: impl Into<Option<&'a Array>>,
    #[optional] stream: impl AsRef<Stream>,
) -> Result<Array> {
    let base = base.into();
    let base = safemlx_sys::mlx_optional_float {
        value: base.unwrap_or(0.0),
        has_value: base.is_some(),
    };
    let freqs = freqs.into();
    Array::try_from_op(|res| unsafe {
        safemlx_sys::mlx_fast_rope_dynamic(
            res,
            array.as_ref().as_ptr(),
            dimensions,
            traditional,
            base,
            scale,
            offset.as_ref().as_ptr(),
            freqs
                .map(|a| a.as_ptr())
                .unwrap_or(safemlx_sys::mlx_array_new()),
            stream.as_ref().as_ptr(),
        )
    })
}

const DEFAULT_MASK_MODE: &CStr = c"";
const CAUSAL_MASK_MODE: &CStr = c"causal";

/// Mask modes for scaled dot product attention.
#[derive(Debug)]
pub enum ScaledDotProductAttentionMask<'a> {
    /// A single mask array
    Array(&'a Array),

    /// Causal masking (no explicit mask array needed)
    Causal,
}

impl<'a> From<&'a Array> for ScaledDotProductAttentionMask<'a> {
    fn from(mask: &'a Array) -> Self {
        ScaledDotProductAttentionMask::Array(mask)
    }
}

impl<'a> IntoOption<ScaledDotProductAttentionMask<'a>> for &'a Array {
    fn into_option(self) -> Option<ScaledDotProductAttentionMask<'a>> {
        Some(ScaledDotProductAttentionMask::Array(self))
    }
}

impl ScaledDotProductAttentionMask<'_> {
    fn as_mode_and_mask(&self) -> (&'static CStr, safemlx_sys::mlx_array) {
        match self {
            ScaledDotProductAttentionMask::Array(mask) => (DEFAULT_MASK_MODE, mask.as_ptr()),
            ScaledDotProductAttentionMask::Causal => {
                (CAUSAL_MASK_MODE, unsafe { safemlx_sys::mlx_array_new() })
            }
        }
    }
}

/// A fast implementation of multi-head attention: `O = softmax(Q @ K.T, dim=-1) @ V`
///
/// Supports [Multi-Head Attention](https://arxiv.org/abs/1706.03762), [Grouped Query Attention](https://arxiv.org/abs/2305.13245), and [Multi-Query Attention](https://arxiv.org/abs/1911.02150).
///
/// This function will dispatch to an optimized Metal kernel when the query sequence length is 1. It handles other cases with regular MLX operations.
///
/// > Note: The softmax operation is performed in float32 precision regardless of input precision (float16 or float32).
///
/// > Note: For Grouped Query Attention and Multi-Query Attention, the input arrays for `key` and `value` should not be pre-tiled to match the `query` array.
#[generate_macro(customize(root = "$crate::fast"))]
pub fn scaled_dot_product_attention<'a>(
    queries: impl AsRef<Array>,
    keys: impl AsRef<Array>,
    values: impl AsRef<Array>,
    scale: f32,
    #[optional] mask: impl IntoOption<ScaledDotProductAttentionMask<'a>>,
    #[optional] sinks: impl Into<Option<&'a Array>>,
    #[optional] stream: impl AsRef<Stream>,
) -> Result<Array> {
    let (mask_mode, mask_arr) = mask.into_option().map_or_else(
        || (DEFAULT_MASK_MODE, unsafe { safemlx_sys::mlx_array_new() }),
        |m| m.as_mode_and_mask(),
    );

    Array::try_from_op(|res| unsafe {
        safemlx_sys::mlx_fast_scaled_dot_product_attention(
            res,
            queries.as_ref().as_ptr(),
            keys.as_ref().as_ptr(),
            values.as_ref().as_ptr(),
            scale,
            mask_mode.as_ptr(),
            mask_arr,
            sinks
                .into()
                .map(|a| a.as_ptr())
                .unwrap_or(safemlx_sys::mlx_array_new()),
            stream.as_ref().as_ptr(),
        )
    })
}

/// Root Mean Square normalization (RMS norm).
///
/// The normalization is with respect to the last axis of the input `x`.
///
/// # Params
///
/// - x: input array
/// - weight: A multiplicative weight to scale the result by. The `weight` should be one-dimensional with the same size as the last axis of `x`.
/// - eps: A small additive constant for numerical stability
/// - stream: stream or device to evaluate on
#[generate_macro(customize(root = "$crate::fast"))]
pub fn rms_norm(
    x: impl AsRef<Array>,
    weight: impl AsRef<Array>,
    eps: f32,
    #[optional] stream: impl AsRef<Stream>,
) -> Result<Array> {
    Array::try_from_op(|res| unsafe {
        safemlx_sys::mlx_fast_rms_norm(
            res,
            x.as_ref().as_ptr(),
            weight.as_ref().as_ptr(),
            eps,
            stream.as_ref().as_ptr(),
        )
    })
}

/// Layer normalization.
///
/// The normalization is with respect to the last axis of the input `x`.
///
/// # Params
///
/// - x: input array
/// - weight: A multiplicative weight to scale the result by. The `weight` should be one-dimensional
///   with the same size as the last axis of `x`.  If not given no scaling will occur.
/// - bias: An additive offset to be added to the result. The `bias` should be one-dimensional
///   with the same size as the last axis of `x`.  It not given no offset will occur.
/// - eps: A small additive constant for numerical stability
/// - stream: stream or device to evaluate on
#[generate_macro(customize(root = "$crate::fast"))]
pub fn layer_norm<'a>(
    #[named] x: impl AsRef<Array>,
    #[optional] weight: impl Into<Option<&'a Array>>,
    #[optional] bias: impl Into<Option<&'a Array>>,
    #[named] eps: f32,
    #[optional] stream: impl AsRef<Stream>,
) -> Result<Array> {
    Array::try_from_op(|res| unsafe {
        safemlx_sys::mlx_fast_layer_norm(
            res,
            x.as_ref().as_ptr(),
            weight
                .into()
                .map(|a| a.as_ptr())
                .unwrap_or(safemlx_sys::mlx_array_new()),
            bias.into()
                .map(|a| a.as_ptr())
                .unwrap_or(safemlx_sys::mlx_array_new()),
            eps,
            stream.as_ref().as_ptr(),
        )
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        ops::indexing::{ArrayIndexOp, IndexOp},
        random::normal,
        Stream,
    };
    use float_eq::assert_float_eq;
    use pretty_assertions::assert_eq;

    #[test]
    fn test_custom_kernel_config_builder() {
        let config = CustomKernelConfig::new()
            .with_output_arg([2, 3], Dtype::Float32)
            .with_grid([6, 1, 1])
            .with_thread_group([32, 1, 1])
            .with_init_value(0.0)
            .with_verbose(true)
            .with_template_arg_dtype("T", Dtype::Float32)
            .with_template_arg_int("N", 6)
            .with_template_arg_bool("DO_SCALE", true);

        assert_eq!(config.output_count(), 1);
        assert_eq!(
            config.outputs()[0],
            CustomKernelOutput::new([2, 3], Dtype::Float32)
        );
        assert_eq!(
            config.template_args(),
            &[
                CustomKernelTemplateArg::Dtype {
                    name: "T".to_string(),
                    dtype: Dtype::Float32,
                },
                CustomKernelTemplateArg::Int {
                    name: "N".to_string(),
                    value: 6,
                },
                CustomKernelTemplateArg::Bool {
                    name: "DO_SCALE".to_string(),
                    value: true,
                },
            ]
        );
    }

    #[test]
    #[ignore = "requires an accessible Metal device"]
    fn test_custom_metal_kernel_multiple_outputs() {
        let input = Array::from_slice(&[1.0f32, 2.0, 3.0, 4.0], &[4]);
        let kernel = MetalKernel::new(
            "copy_and_double",
            ["inp"],
            ["out0", "out1"],
            concat!(
                "uint elem = thread_position_in_grid.x;",
                "T value = inp[elem];",
                "out0[elem] = value;",
                "out1[elem] = value + value;"
            ),
            "",
            true,
            false,
        )
        .unwrap();

        let config = CustomKernelConfig::new()
            .with_template_arg_dtype("T", Dtype::Float32)
            .with_grid([input.size() as i32, 1, 1])
            .with_thread_group([256, 1, 1])
            .with_output_arg(input.shape(), input.dtype())
            .with_output_arg(input.shape(), input.dtype());

        let outputs = kernel
            .apply_device(
                [&input],
                &config,
                Stream::new_with_device(&crate::Device::new(crate::DeviceType::Gpu, 0)),
            )
            .unwrap();

        assert_eq!(outputs.len(), 2);
        assert_eq!(
            crate::array::eval_vec::<f32>(&outputs[0]),
            &[1.0, 2.0, 3.0, 4.0]
        );
        assert_eq!(
            crate::array::eval_vec::<f32>(&outputs[1]),
            &[2.0, 4.0, 6.0, 8.0]
        );
    }

    #[test]
    fn test_rope() {
        let stream = crate::test_stream();
        let key = crate::test_key(71, stream);
        let a = crate::random::uniform::<_, f32>(0.0, 1.0, &[2, 8, 16], &key, stream).unwrap();
        assert_eq!(a.shape(), [2, 8, 16]);
        assert_eq!(a.dtype(), crate::Dtype::Float32);

        let result = rope(a, 8, false, 10000., 1.0, 0, None, stream).unwrap();
        assert_eq!(result.shape(), [2, 8, 16]);
        assert_eq!(result.dtype(), crate::Dtype::Float32);
        assert_float_eq!(
            result.mean(None, stream).unwrap().item::<f32>(&stream),
            0.456_253_77,
            abs <= 0.009_125_075
        );
        assert_float_eq!(
            result.sum(None, stream).unwrap().item::<f32>(&stream),
            116.800_964,
            abs <= 2.336_019_3
        );
    }

    // Test adapted from Python test_fast.py/test_rope - the Python test accepts both
    // int offset and array offset, which in C/Rust are separate functions
    #[test]
    fn test_rope_dynamic() {
        let stream = crate::test_stream();
        let key = crate::test_key(71, stream);
        let a = crate::random::uniform::<_, f32>(0.0, 1.0, &[2, 8, 16], &key, stream).unwrap();
        assert_eq!(a.shape(), [2, 8, 16]);
        assert_eq!(a.dtype(), crate::Dtype::Float32);

        // Test with array offset - should produce similar results to int offset of 3
        let offset = crate::Array::from_int(3);
        let result = rope_dynamic(&a, 8, false, 10000., 1.0, &offset, None, stream).unwrap();
        assert_eq!(result.shape(), [2, 8, 16]);
        assert_eq!(result.dtype(), crate::Dtype::Float32);

        // Compare with regular rope using int offset=3
        let result_int_offset = rope(&a, 8, false, 10000., 1.0, 3, None, stream).unwrap();
        assert_eq!(result_int_offset.shape(), [2, 8, 16]);

        // The results should be close
        let diff = result.subtract(&result_int_offset, stream).unwrap();
        let max_diff = diff
            .abs(stream)
            .unwrap()
            .max(None, stream)
            .unwrap()
            .item::<f32>(&stream);
        assert!(max_diff < 1e-5, "Max difference was {}", max_diff);
    }

    #[test]
    fn test_rms_norm() {
        let stream = crate::test_stream();
        let key = crate::test_key(103, stream);
        let a = crate::random::uniform::<_, f32>(0.0, 1.0, &[2, 8, 16], &key, stream).unwrap();
        assert_eq!(a.shape(), [2, 8, 16]);
        assert_eq!(a.dtype(), crate::Dtype::Float32);

        let weight = Array::ones::<f32>(&[16], stream).unwrap();
        let result = rms_norm(a, weight, 1e-5, stream).unwrap();
        assert_eq!(result.shape(), [2, 8, 16]);
        assert_eq!(result.dtype(), crate::Dtype::Float32);
        assert_float_eq!(
            result.mean(None, stream).unwrap().item::<f32>(&stream),
            0.872_938_75,
            abs <= 0.017_458_774
        );
        assert_float_eq!(
            result.sum(None, stream).unwrap().item::<f32>(&stream),
            223.472_32,
            abs <= 4.469_446
        );
    }

    #[test]
    pub fn test_layer_norm_affine() {
        let stream = crate::test_stream();
        let key = crate::test_key(635, stream);
        let a = crate::random::uniform::<_, f32>(0.0, 1.0, &[2, 8, 16], &key, stream).unwrap();
        assert_eq!(a.shape(), [2, 8, 16]);
        assert_eq!(a.dtype(), crate::Dtype::Float32);

        let weight = Array::ones::<f32>(&[16], stream).unwrap();
        let bias = Array::zeros::<f32>(&[16], stream).unwrap();
        let result = layer_norm(a, &weight, &bias, 1e-5, stream).unwrap();
        let result = result.index_device((ArrayIndexOp::Ellipsis, 0), stream);
        assert_eq!(result.shape(), [2, 8]);
        assert_eq!(result.dtype(), crate::Dtype::Float32);
        assert_float_eq!(
            result.mean(None, stream).unwrap().item::<f32>(&stream),
            0.290_990_38,
            abs <= 0.005_819_807_8
        );
        assert_float_eq!(
            result.sum(None, stream).unwrap().item::<f32>(&stream),
            4.655_846,
            abs <= 0.093_116_924
        );
    }

    #[test]
    #[allow(non_snake_case)]
    fn test_fast_sdpa() {
        let stream = crate::test_stream();
        // This test just makes sure that `scaled_dot_product_attention` is callable
        // in the various cases, based on the Python test `test_fast_sdpa`.

        let Dk = 64;
        let scale = 1.0 / (Dk as f32).sqrt();
        for seq_len in [63, 129, 400] {
            for dtype in [crate::Dtype::Float32, crate::Dtype::Float16] {
                let B = 2;
                let H = 24;
                let q_key = crate::test_key((seq_len + Dk) as u64, stream);
                let k_key = crate::test_key((seq_len + Dk + 1) as u64, stream);
                let v_key = crate::test_key((seq_len + Dk + 2) as u64, stream);
                let q = normal::<f32>(&[B, H, seq_len, Dk], None, None, &q_key, stream)
                    .unwrap()
                    .as_dtype(dtype, stream)
                    .unwrap();
                let k = normal::<f32>(&[B, H, seq_len, Dk], None, None, &k_key, stream)
                    .unwrap()
                    .as_dtype(dtype, stream)
                    .unwrap();
                let v = normal::<f32>(&[B, H, seq_len, Dk], None, None, &v_key, stream)
                    .unwrap()
                    .as_dtype(dtype, stream)
                    .unwrap();

                let result =
                    scaled_dot_product_attention(q, k, v, scale, None, None, stream).unwrap();
                assert_eq!(result.shape(), [B, H, seq_len, Dk]);
                assert_eq!(result.dtype(), dtype);
            }
        }
    }

    // Test adapted from Python test `test_fast_sdpa.py/test_sdpa_attention_sinks`
    #[test]
    fn test_fast_sdpa_with_sinks() {
        let stream = crate::test_stream();
        let b = 2;
        let n_q = 8;
        let t_q = 128;
        let t_kv = 128;
        let d = 64;

        let q_key = crate::test_key(0, stream);
        let k_key = crate::test_key(1, stream);
        let v_key = crate::test_key(2, stream);
        let sinks_key = crate::test_key(3, stream);
        let q = normal::<f32>(&[b, n_q, t_q, d], None, None, &q_key, stream).unwrap();
        let k = normal::<f32>(&[b, n_q, t_kv, d], None, None, &k_key, stream).unwrap();
        let v = normal::<f32>(&[b, n_q, t_kv, d], None, None, &v_key, stream).unwrap();
        let scale = (d as f32).powf(-0.5);

        // Test with sinks parameter
        let sinks = normal::<f32>(&[n_q], None, None, &sinks_key, stream)
            .unwrap()
            .multiply(Array::from_f32(10.0), stream)
            .unwrap();

        let result = scaled_dot_product_attention(&q, &k, &v, scale, None, &sinks, stream).unwrap();
        assert_eq!(result.shape(), &[b, n_q, t_q, d]);
    }
}