onnx-runtime-ep-cuda 0.1.0-dev.6

CUDA execution provider for the ORT 2.0 runtime (Phase 2a: cudarc + cuBLASLt MatMul; custom fused kernels deferred)
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
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
//! cuBLASLt GEMM plumbing for the CUDA EP (`docs/architecture/ORT2.md` §15.3).
//!
//! This module owns the single hardest correctness detail in the crate: the
//! **row-major ONNX ↔ column-major cuBLAS** mapping. Everything about the
//! transpose / leading-dimension handling is documented and centralised here so
//! the kernel layer never has to reason about it.
//!
//! ## Why the `result` layer (not the `safe` layer)
//!
//! cudarc ships a `safe` `CudaBlasLT` wrapper, but its `Matmul<f32>` impl hard-
//! codes the compute type `CUBLAS_COMPUTE_32F_FAST_TF32` — i.e. it silently
//! rounds f32 inputs to **TF32** (10-bit mantissa). For an ONNX runtime whose
//! Phase-1 bar is *"GPU MatMul matches the CPU reference"*, a silent ~1e-3
//! relative error on the f32 path is a correctness regression, not an
//! optimisation. So we drop to cudarc's `result`/`sys` layer (explicitly
//! sanctioned by §15.2) and request full-precision `CUBLAS_COMPUTE_32F`. The
//! safe layer's RAII structure is mirrored here so descriptors are always freed,
//! even on the error path.
//!
//! ## The mapping (row-major → column-major), proved
//!
//! ONNX MatMul is **row-major**: we want `C[M,N] = A[M,K] · B[K,N]`, all stored
//! row-major. cuBLAS is **column-major**. The identity we exploit: a row-major
//! matrix `X[r,c]` with leading dim `c` occupies the *exact same bytes* as the
//! column-major matrix `Xáµ€[c,r]` with leading dim `c`. Therefore, reading our
//! row-major buffers as column-major matrices *for free*:
//!
//! * `A` row-major `[M,K]` **is** column-major `Aáµ€ [K,M]` (ld = K)
//! * `B` row-major `[K,N]` **is** column-major `Báµ€ [N,K]` (ld = N)
//! * `C` row-major `[M,N]` **is** column-major `Cáµ€ [N,M]` (ld = N)
//!
//! We want `Cᵀ`. And `Cᵀ = (A·B)ᵀ = Bᵀ · Aᵀ`. So a single **no-transpose**
//! column-major GEMM with the operands swapped produces exactly the bytes of
//! our row-major `C`:
//!
//! ```text
//!   cublas(op1 = B, op2 = A)  →  op1 · op2 = Bᵀ · Aᵀ = Cᵀ  ==  row-major C
//!   with cublas dims  m = N, n = M, k = K
//!   and leading dims  lda = N (op1=B),  ldb = K (op2=A),  ldc = N (C)
//! ```
//!
//! This is the same convention cudarc's own test uses, and it is unit-tested on
//! the GPU in `tests/matmul_gpu.rs`.
//!
//! Fused bias/activation epilogues use the descriptor's native
//! `CUBLASLT_MATMUL_DESC_EPILOGUE` and `BIAS_POINTER` attributes, so bias and
//! activation execute inside the selected GEMM kernel.

use core::ffi::c_int;
use std::ffi::c_void;
use std::mem::MaybeUninit;

use cudarc::cublaslt::{result, sys};
use cudarc::driver::sys::CUdeviceptr;

use onnx_runtime_ep_api::{
    EpError, Result, WorkspaceLifetime, WorkspaceRequirement, WorkspaceView,
};
use onnx_runtime_memory_governor::MemoryRole;

use crate::error::cublas_err;

/// Element type of a GEMM. Maps to a cuBLAS data type; the accumulate / scale
/// type is always f32 (see [`GemmDtype::compute_type`]).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum GemmDtype {
    F32,
    F16,
    Bf16,
}

impl GemmDtype {
    fn data_type(self) -> sys::cudaDataType {
        match self {
            GemmDtype::F32 => sys::cudaDataType_t::CUDA_R_32F,
            GemmDtype::F16 => sys::cudaDataType_t::CUDA_R_16F,
            GemmDtype::Bf16 => sys::cudaDataType_t::CUDA_R_16BF,
        }
    }

    /// Full-precision f32 accumulation for every element type. For f16/bf16 this
    /// is the architecture-neutral mixed-precision mode: the cuBLASLt heuristic
    /// selects an algorithm supported by the detected device, without requesting
    /// an SM-specific tensor-core compute type. For f32 it is true IEEE fp32
    /// (NOT TF32 — see the module docs for why that matters).
    fn compute_type(self) -> sys::cublasComputeType_t {
        sys::cublasComputeType_t::CUBLAS_COMPUTE_32F
    }

    const fn byte_size(self) -> u32 {
        match self {
            GemmDtype::F32 => 4,
            GemmDtype::F16 | GemmDtype::Bf16 => 2,
        }
    }

    const fn requires_f32_reduction_storage(self) -> bool {
        matches!(self, GemmDtype::F16 | GemmDtype::Bf16)
    }
}

/// An owned cuBLASLt handle. `Drop` frees it; `Send`/`Sync` mirror cudarc's own
/// `CudaBlasLT` (the handle is a context-independent library handle).
#[derive(Debug)]
pub struct CublasLt {
    handle: sys::cublasLtHandle_t,
}

// SAFETY: a cuBLASLt handle is not thread-affine; cudarc makes the same
// assertion for its `CudaBlasLT`. Concurrent *use* is still serialised by the
// per-execute descriptors and workspace we create below.
unsafe impl Send for CublasLt {}
unsafe impl Sync for CublasLt {}

impl CublasLt {
    /// Create a cuBLASLt handle (dlopen's `libcublasLt` on first use).
    pub fn new() -> Result<Self> {
        // Handle creation initializes cuBLASLt state on the device; see the
        // matmul sites.
        let _section = onnx_runtime_cuda_memory::capture_gate::synchronizing_section();
        let handle = result::create_handle().map_err(|e| cublas_err("cublasLtCreate", e))?;
        Ok(Self { handle })
    }
}

impl Drop for CublasLt {
    fn drop(&mut self) {
        if !self.handle.is_null() {
            // SAFETY: `handle` was produced by `create_handle` and is freed
            // exactly once here.
            unsafe {
                let _ = result::destroy_handle(self.handle);
            }
            self.handle = std::ptr::null_mut();
        }
    }
}

/// RAII wrapper over a `cublasLtMatrixLayout_t` (freed on drop).
struct MatrixLayout(sys::cublasLtMatrixLayout_t);

impl MatrixLayout {
    fn new(dtype: sys::cudaDataType, rows: u64, cols: u64, ld: i64) -> Result<Self> {
        let h = result::create_matrix_layout(dtype, rows, cols, ld)
            .map_err(|e| cublas_err("cublasLtMatrixLayoutCreate", e))?;
        Ok(Self(h))
    }

    /// Attach strided-batch metadata (batch count + element stride between
    /// consecutive matrices in the batch).
    fn set_batch(&self, count: c_int, stride: i64) -> Result<()> {
        // SAFETY: `self.0` is a live layout; the attribute buffers point at
        // locals of the documented size for the whole call.
        unsafe {
            result::set_matrix_layout_attribute(
                self.0,
                sys::cublasLtMatrixLayoutAttribute_t::CUBLASLT_MATRIX_LAYOUT_BATCH_COUNT,
                (&count) as *const c_int as *const c_void,
                std::mem::size_of::<c_int>(),
            )
            .map_err(|e| cublas_err("set BATCH_COUNT", e))?;
            result::set_matrix_layout_attribute(
                self.0,
                sys::cublasLtMatrixLayoutAttribute_t::CUBLASLT_MATRIX_LAYOUT_STRIDED_BATCH_OFFSET,
                (&stride) as *const i64 as *const c_void,
                std::mem::size_of::<i64>(),
            )
            .map_err(|e| cublas_err("set STRIDED_BATCH_OFFSET", e))?;
        }
        Ok(())
    }
}

impl Drop for MatrixLayout {
    fn drop(&mut self) {
        // SAFETY: single free of a live layout handle.
        unsafe {
            let _ = result::destroy_matrix_layout(self.0);
        }
    }
}

/// RAII wrapper over a `cublasLtMatmulDesc_t`.
struct MatmulDesc(sys::cublasLtMatmulDesc_t);

impl MatmulDesc {
    fn new(compute: sys::cublasComputeType_t, scale: sys::cudaDataType) -> Result<Self> {
        let h = result::create_matmul_desc(compute, scale)
            .map_err(|e| cublas_err("cublasLtMatmulDescCreate", e))?;
        Ok(Self(h))
    }

    fn set_epilogue(&self, epilogue: GemmEpilogue) -> Result<()> {
        let kind = epilogue.kind.as_cublas();
        let bias = epilogue.bias;
        // The bias address is execution data, not an algorithm-selection input.
        // Prepare-only planning has no tensor pointers, so a zero sentinel sets
        // the epilogue kind without a BIAS_POINTER; execution supplies the real
        // address through the same descriptor builder.
        unsafe {
            result::set_matmul_desc_attribute(
                self.0,
                sys::cublasLtMatmulDescAttributes_t::CUBLASLT_MATMUL_DESC_EPILOGUE,
                (&kind) as *const sys::cublasLtEpilogue_t as *const c_void,
                std::mem::size_of::<sys::cublasLtEpilogue_t>(),
            )
            .map_err(|e| cublas_err("set MATMUL_DESC_EPILOGUE", e))?;
            if bias != 0 {
                result::set_matmul_desc_attribute(
                    self.0,
                    sys::cublasLtMatmulDescAttributes_t::CUBLASLT_MATMUL_DESC_BIAS_POINTER,
                    (&bias) as *const CUdeviceptr as *const c_void,
                    std::mem::size_of::<CUdeviceptr>(),
                )
                .map_err(|e| cublas_err("set MATMUL_DESC_BIAS_POINTER", e))?;
            }
            Ok(())
        }
    }
}

impl Drop for MatmulDesc {
    fn drop(&mut self) {
        // SAFETY: single free of a live desc handle.
        unsafe {
            let _ = result::destroy_matmul_desc(self.0);
        }
    }
}

/// RAII wrapper over a `cublasLtMatmulPreference_t`.
struct MatmulPref(sys::cublasLtMatmulPreference_t);

impl MatmulPref {
    fn new(
        workspace_bytes: usize,
        alignments: MatmulPointerAlignments,
        dtype: GemmDtype,
    ) -> Result<Self> {
        let h = result::create_matmul_pref()
            .map_err(|e| cublas_err("cublasLtMatmulPreferenceCreate", e))?;
        let pref = Self(h);
        // SAFETY: `h` is live; every attribute buffer has the documented type
        // and remains live for the complete call.
        unsafe {
            result::set_matmul_pref_attribute(
                h,
                sys::cublasLtMatmulPreferenceAttributes_t::CUBLASLT_MATMUL_PREF_MAX_WORKSPACE_BYTES,
                (&workspace_bytes) as *const usize as *const c_void,
                std::mem::size_of::<usize>(),
            )
            .map_err(|e| cublas_err("set MAX_WORKSPACE_BYTES", e))?;
            for (attribute, alignment, name) in [
                (
                    sys::cublasLtMatmulPreferenceAttributes_t::CUBLASLT_MATMUL_PREF_MIN_ALIGNMENT_A_BYTES,
                    alignments.a,
                    "MIN_ALIGNMENT_A_BYTES",
                ),
                (
                    sys::cublasLtMatmulPreferenceAttributes_t::CUBLASLT_MATMUL_PREF_MIN_ALIGNMENT_B_BYTES,
                    alignments.b,
                    "MIN_ALIGNMENT_B_BYTES",
                ),
                (
                    sys::cublasLtMatmulPreferenceAttributes_t::CUBLASLT_MATMUL_PREF_MIN_ALIGNMENT_C_BYTES,
                    alignments.c,
                    "MIN_ALIGNMENT_C_BYTES",
                ),
                (
                    sys::cublasLtMatmulPreferenceAttributes_t::CUBLASLT_MATMUL_PREF_MIN_ALIGNMENT_D_BYTES,
                    alignments.d,
                    "MIN_ALIGNMENT_D_BYTES",
                ),
            ] {
                result::set_matmul_pref_attribute(
                    h,
                    attribute,
                    (&alignment) as *const u32 as *const c_void,
                    std::mem::size_of::<u32>(),
                )
                .map_err(|e| cublas_err(&format!("set {name}"), e))?;
            }
            if dtype.requires_f32_reduction_storage() {
                let mask =
                    sys::cublasLtReductionScheme_t::CUBLASLT_REDUCTION_SCHEME_COMPUTE_TYPE as u32;
                result::set_matmul_pref_attribute(
                    h,
                    sys::cublasLtMatmulPreferenceAttributes_t::CUBLASLT_MATMUL_PREF_REDUCTION_SCHEME_MASK,
                    (&mask) as *const u32 as *const c_void,
                    std::mem::size_of::<u32>(),
                )
                .map_err(|e| cublas_err("set REDUCTION_SCHEME_MASK", e))?;
            }
        }
        Ok(pref)
    }
}

impl Drop for MatmulPref {
    fn drop(&mut self) {
        // SAFETY: single free of a live pref handle.
        unsafe {
            let _ = result::destroy_matmul_pref(self.0);
        }
    }
}

/// A batched, row-major GEMM request. All pointers are **device** pointers
/// (`CUdeviceptr`) into buffers this EP owns. Shapes are the logical ONNX
/// (row-major) shapes: `C[batch,M,N] = A[batch,M,K] · B[batch,K,N]`.
pub struct GemmParams {
    pub dtype: GemmDtype,
    pub a: CUdeviceptr,
    pub b: CUdeviceptr,
    pub c: CUdeviceptr,
    pub m: usize,
    pub k: usize,
    pub n: usize,
    /// Number of independent matrices (1 for a plain 2-D GEMM).
    pub batch: usize,
    /// Element strides between A/B matrices. A zero stride broadcasts one
    /// matrix across the batch.
    pub a_batch_stride: usize,
    pub b_batch_stride: usize,
    /// Optional in-GEMM bias/activation epilogue.
    pub epilogue: Option<GemmEpilogue>,
}

/// A fixed-shape row-major strided-batched GEMM request used by canonical
/// `Einsum` contractions.
///
/// Logical execution is `C[batch,M,N] = A[batch,M,K] · B[batch,K,N]`. When a
/// transpose flag is set, the corresponding storage matrix has the reversed
/// shape (`A` is `[K,M]`, or `B` is `[N,K]`) and cuBLASLt applies the transpose
/// through its descriptor without materializing bytes.
pub struct StridedBatchedGemmParams {
    pub dtype: GemmDtype,
    pub a: CUdeviceptr,
    pub b: CUdeviceptr,
    pub c: CUdeviceptr,
    pub m: usize,
    pub k: usize,
    pub n: usize,
    pub batch: usize,
    pub transpose_a: bool,
    pub transpose_b: bool,
    /// Element stride between stored A matrices. Zero broadcasts one matrix.
    pub a_batch_stride: usize,
    /// Element stride between stored B matrices. Zero broadcasts one matrix.
    pub b_batch_stride: usize,
}

/// cuBLASLt fused epilogue kind. All variants add a per-output-channel bias;
/// activation, when present, is evaluated by cuBLASLt before the result is
/// written to global memory.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum GemmEpilogueKind {
    Bias,
    ReluBias,
    GeluBias,
}

impl GemmEpilogueKind {
    fn as_cublas(self) -> sys::cublasLtEpilogue_t {
        match self {
            Self::Bias => sys::cublasLtEpilogue_t::CUBLASLT_EPILOGUE_BIAS,
            Self::ReluBias => sys::cublasLtEpilogue_t::CUBLASLT_EPILOGUE_RELU_BIAS,
            Self::GeluBias => sys::cublasLtEpilogue_t::CUBLASLT_EPILOGUE_GELU_BIAS,
        }
    }
}

/// Device bias vector and the fused operation to apply to each GEMM output.
#[derive(Clone, Copy, Debug)]
pub struct GemmEpilogue {
    pub kind: GemmEpilogueKind,
    pub bias: CUdeviceptr,
}

/// Default cuBLASLt workspace performance budget. This is device-global scratch,
/// not per-block shared memory or an SM-specific hardware limit; cuBLASLt's
/// architecture-aware heuristic selects an algorithm using at most these bytes.
///
/// Phase 2b: pool this per-stream instead of allocating it per call.
pub const WORKSPACE_BYTES: usize = 32 * 1024 * 1024;
pub const WORKSPACE_ALIGNMENT: usize = 256;
const MAX_REPORTED_POINTER_ALIGNMENT: u32 = 256;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct MatmulPointers {
    a: CUdeviceptr,
    b: CUdeviceptr,
    c: CUdeviceptr,
    d: CUdeviceptr,
}

impl MatmulPointers {
    fn row_major(a: CUdeviceptr, b: CUdeviceptr, c: CUdeviceptr) -> Self {
        Self {
            a: b,
            b: a,
            c,
            d: c,
        }
    }

    fn column_major(a: CUdeviceptr, b: CUdeviceptr, c: CUdeviceptr) -> Self {
        Self { a, b, c, d: c }
    }

    fn alignments(
        self,
        dtype: GemmDtype,
        authority: PointerAlignmentAuthority,
    ) -> MatmulPointerAlignments {
        MatmulPointerAlignments {
            a: pointer_alignment(self.a, dtype, authority),
            b: pointer_alignment(self.b, dtype, authority),
            c: pointer_alignment(self.c, dtype, authority),
            d: pointer_alignment(self.d, dtype, authority),
        }
    }
}

#[derive(Clone, Copy)]
enum PointerAlignmentAuthority {
    ActualAddress,
    TypedMinimum,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct MatmulPointerAlignments {
    a: u32,
    b: u32,
    c: u32,
    d: u32,
}

impl MatmulPointerAlignments {
    fn proves(self, required: Self) -> bool {
        self.a >= required.a && self.b >= required.b && self.c >= required.c && self.d >= required.d
    }
}

fn pointer_alignment(
    pointer: CUdeviceptr,
    dtype: GemmDtype,
    authority: PointerAlignmentAuthority,
) -> u32 {
    if matches!(authority, PointerAlignmentAuthority::TypedMinimum) {
        return dtype.byte_size();
    }
    if pointer == 0 {
        return 1;
    }
    let power = pointer
        .trailing_zeros()
        .min(MAX_REPORTED_POINTER_ALIGNMENT.trailing_zeros());
    1u32 << power
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CublasLtReductionScheme {
    None,
    InPlace,
    ComputeType,
    OutputType,
    Unknown(u32),
}

impl CublasLtReductionScheme {
    fn from_raw(value: u32) -> Self {
        match value {
            value
                if value
                    == sys::cublasLtReductionScheme_t::CUBLASLT_REDUCTION_SCHEME_NONE as u32 =>
            {
                Self::None
            }
            value
                if value
                    == sys::cublasLtReductionScheme_t::CUBLASLT_REDUCTION_SCHEME_INPLACE as u32 =>
            {
                Self::InPlace
            }
            value
                if value
                    == sys::cublasLtReductionScheme_t::CUBLASLT_REDUCTION_SCHEME_COMPUTE_TYPE
                        as u32 =>
            {
                Self::ComputeType
            }
            value
                if value
                    == sys::cublasLtReductionScheme_t::CUBLASLT_REDUCTION_SCHEME_OUTPUT_TYPE
                        as u32 =>
            {
                Self::OutputType
            }
            value => Self::Unknown(value),
        }
    }

    #[must_use]
    pub const fn preserves_f32_intermediates(self) -> bool {
        matches!(self, Self::None | Self::ComputeType)
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct RowMajorGemmAlgorithmContract {
    pub a_min_alignment_bytes: u32,
    pub b_min_alignment_bytes: u32,
    pub c_min_alignment_bytes: u32,
    pub split_k: i32,
    pub reduction_scheme: CublasLtReductionScheme,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct MatmulAlgorithmContract {
    required_alignments: MatmulPointerAlignments,
    split_k: i32,
    reduction_scheme: CublasLtReductionScheme,
}

impl MatmulAlgorithmContract {
    fn query(
        algo: &sys::cublasLtMatmulAlgo_t,
        dtype: GemmDtype,
        available_alignments: MatmulPointerAlignments,
    ) -> Result<Self> {
        let required_alignments = MatmulPointerAlignments {
            a: algo_cap_u32(
                algo,
                sys::cublasLtMatmulAlgoCapAttributes_t::CUBLASLT_ALGO_CAP_MIN_ALIGNMENT_A_BYTES,
                "MIN_ALIGNMENT_A_BYTES",
            )?
            .max(1),
            b: algo_cap_u32(
                algo,
                sys::cublasLtMatmulAlgoCapAttributes_t::CUBLASLT_ALGO_CAP_MIN_ALIGNMENT_B_BYTES,
                "MIN_ALIGNMENT_B_BYTES",
            )?
            .max(1),
            c: algo_cap_u32(
                algo,
                sys::cublasLtMatmulAlgoCapAttributes_t::CUBLASLT_ALGO_CAP_MIN_ALIGNMENT_C_BYTES,
                "MIN_ALIGNMENT_C_BYTES",
            )?
            .max(1),
            d: algo_cap_u32(
                algo,
                sys::cublasLtMatmulAlgoCapAttributes_t::CUBLASLT_ALGO_CAP_MIN_ALIGNMENT_D_BYTES,
                "MIN_ALIGNMENT_D_BYTES",
            )?
            .max(1),
        };
        if !available_alignments.proves(required_alignments) {
            return Err(EpError::KernelFailed(format!(
                "cuda_ep: cuBLASLt selected pointer alignments {required_alignments:?}, but the \
                 actual typed pointers prove only {available_alignments:?}"
            )));
        }
        let split_k = algo_config_i32(
            algo,
            sys::cublasLtMatmulAlgoConfigAttributes_t::CUBLASLT_ALGO_CONFIG_SPLITK_NUM,
            "SPLITK_NUM",
        )?;
        if split_k < 1 {
            return Err(EpError::KernelFailed(format!(
                "cuda_ep: cuBLASLt selected invalid split-K count {split_k}"
            )));
        }
        let reduction_scheme = CublasLtReductionScheme::from_raw(algo_config_u32(
            algo,
            sys::cublasLtMatmulAlgoConfigAttributes_t::CUBLASLT_ALGO_CONFIG_REDUCTION_SCHEME,
            "REDUCTION_SCHEME",
        )?);
        if dtype.requires_f32_reduction_storage() && !reduction_scheme.preserves_f32_intermediates()
        {
            return Err(EpError::KernelFailed(format!(
                "cuda_ep: cuBLASLt selected {reduction_scheme:?} reduction for {dtype:?}; \
                 f16/bf16 GEMM requires F32 reduction intermediates and one final narrowing store"
            )));
        }
        Ok(Self {
            required_alignments,
            split_k,
            reduction_scheme,
        })
    }

    fn supports(self, dtype: GemmDtype, pointers: MatmulPointers) -> bool {
        pointers
            .alignments(dtype, PointerAlignmentAuthority::ActualAddress)
            .proves(self.required_alignments)
    }

    fn row_major(self) -> RowMajorGemmAlgorithmContract {
        RowMajorGemmAlgorithmContract {
            a_min_alignment_bytes: self.required_alignments.b,
            b_min_alignment_bytes: self.required_alignments.a,
            c_min_alignment_bytes: self.required_alignments.c.max(self.required_alignments.d),
            split_k: self.split_k,
            reduction_scheme: self.reduction_scheme,
        }
    }
}

fn algo_cap_u32(
    algo: &sys::cublasLtMatmulAlgo_t,
    attribute: sys::cublasLtMatmulAlgoCapAttributes_t,
    name: &str,
) -> Result<u32> {
    let mut value = MaybeUninit::<u32>::uninit();
    let mut written = 0usize;
    // SAFETY: `algo` is initialized by the heuristic; `value` has the
    // documented attribute type and `written` is a valid output pointer.
    unsafe {
        sys::cublasLtMatmulAlgoCapGetAttribute(
            algo,
            attribute,
            value.as_mut_ptr().cast::<c_void>(),
            std::mem::size_of::<u32>(),
            &mut written,
        )
    }
    .result()
    .map_err(|error| cublas_err(&format!("get ALGO_CAP_{name}"), error))?;
    if written != std::mem::size_of::<u32>() {
        return Err(EpError::KernelFailed(format!(
            "cuda_ep: cuBLASLt ALGO_CAP_{name} wrote {written} bytes, expected {}",
            std::mem::size_of::<u32>()
        )));
    }
    // SAFETY: cuBLASLt reported that it initialized exactly one `u32`.
    Ok(unsafe { value.assume_init() })
}

fn algo_config_i32(
    algo: &sys::cublasLtMatmulAlgo_t,
    attribute: sys::cublasLtMatmulAlgoConfigAttributes_t,
    name: &str,
) -> Result<i32> {
    let mut value = MaybeUninit::<i32>::uninit();
    let mut written = 0usize;
    // SAFETY: same attribute-buffer contract as [`algo_cap_u32`].
    unsafe {
        sys::cublasLtMatmulAlgoConfigGetAttribute(
            algo,
            attribute,
            value.as_mut_ptr().cast::<c_void>(),
            std::mem::size_of::<i32>(),
            &mut written,
        )
    }
    .result()
    .map_err(|error| cublas_err(&format!("get ALGO_CONFIG_{name}"), error))?;
    if written != std::mem::size_of::<i32>() {
        return Err(EpError::KernelFailed(format!(
            "cuda_ep: cuBLASLt ALGO_CONFIG_{name} wrote {written} bytes, expected {}",
            std::mem::size_of::<i32>()
        )));
    }
    // SAFETY: cuBLASLt reported that it initialized exactly one `i32`.
    Ok(unsafe { value.assume_init() })
}

fn algo_config_u32(
    algo: &sys::cublasLtMatmulAlgo_t,
    attribute: sys::cublasLtMatmulAlgoConfigAttributes_t,
    name: &str,
) -> Result<u32> {
    let mut value = MaybeUninit::<u32>::uninit();
    let mut written = 0usize;
    // SAFETY: same attribute-buffer contract as [`algo_cap_u32`].
    unsafe {
        sys::cublasLtMatmulAlgoConfigGetAttribute(
            algo,
            attribute,
            value.as_mut_ptr().cast::<c_void>(),
            std::mem::size_of::<u32>(),
            &mut written,
        )
    }
    .result()
    .map_err(|error| cublas_err(&format!("get ALGO_CONFIG_{name}"), error))?;
    if written != std::mem::size_of::<u32>() {
        return Err(EpError::KernelFailed(format!(
            "cuda_ep: cuBLASLt ALGO_CONFIG_{name} wrote {written} bytes, expected {}",
            std::mem::size_of::<u32>()
        )));
    }
    // SAFETY: cuBLASLt reported that it initialized exactly one `u32`.
    Ok(unsafe { value.assume_init() })
}

/// Attribute exact cuBLASLt scratch to the one session-persistent shared slot.
///
/// Kernels on the runtime's single CUDA stream execute sequentially, so their
/// individual heuristic requirements merge as a peak rather than a sum.
pub const fn governed_workspace_requirement(bytes: usize) -> WorkspaceRequirement {
    if bytes == 0 {
        WorkspaceRequirement::NONE
    } else {
        WorkspaceRequirement {
            bytes: bytes as u64,
            alignment: WORKSPACE_ALIGNMENT,
            lifetime: WorkspaceLifetime::SessionPersistent,
            role: MemoryRole::Workspace { step_scoped: false },
        }
    }
}

struct PlannedMatmul {
    a_layout: MatrixLayout,
    b_layout: MatrixLayout,
    c_layout: MatrixLayout,
    _desc: MatmulDesc,
    algo: sys::cublasLtMatmulAlgo_t,
    workspace_bytes: usize,
    dtype: GemmDtype,
    contract: MatmulAlgorithmContract,
}

impl PlannedMatmul {
    fn validate_pointers(&self, pointers: MatmulPointers, operation: &str) -> Result<()> {
        if self.contract.supports(self.dtype, pointers) {
            Ok(())
        } else {
            Err(EpError::KernelFailed(format!(
                "cuda_ep {operation}: actual cuBLASLt pointer alignments {:?} do not satisfy \
                 the selected algorithm contract {:?}; rebuild the plan for these tensor origins \
                 or use the generic native route",
                pointers.alignments(self.dtype, PointerAlignmentAuthority::ActualAddress),
                self.contract.required_alignments
            )))
        }
    }
}

fn plan_gemm(
    handle: &CublasLt,
    p: &GemmParams,
    alignment_authority: PointerAlignmentAuthority,
) -> Result<PlannedMatmul> {
    if p.m == 0 || p.n == 0 || p.k == 0 || p.batch == 0 {
        return Err(EpError::KernelFailed(format!(
            "cuda_ep MatMul: degenerate GEMM dims M={} K={} N={} batch={}",
            p.m, p.k, p.n, p.batch
        )));
    }

    let dt = p.dtype.data_type();
    let (m, n, k) = (p.n as u64, p.m as u64, p.k as u64);
    let (lda, ldb, ldc) = (p.n as i64, p.k as i64, p.n as i64);
    let a_layout = MatrixLayout::new(dt, m, k, lda)?;
    let b_layout = MatrixLayout::new(dt, k, n, ldb)?;
    let c_layout = MatrixLayout::new(dt, m, n, ldc)?;

    if p.batch > 1 {
        let count = i32::try_from(p.batch).map_err(|_| {
            EpError::KernelFailed(format!("cuda_ep MatMul: batch {} exceeds i32", p.batch))
        })?;
        a_layout.set_batch(count, p.b_batch_stride as i64)?;
        b_layout.set_batch(count, p.a_batch_stride as i64)?;
        c_layout.set_batch(count, (p.m * p.n) as i64)?;
    }

    let desc = MatmulDesc::new(p.dtype.compute_type(), sys::cudaDataType_t::CUDA_R_32F)?;
    if let Some(epilogue) = p.epilogue {
        desc.set_epilogue(epilogue)?;
    }
    let pointers = MatmulPointers::row_major(p.a, p.b, p.c);
    let available_alignments = pointers.alignments(p.dtype, alignment_authority);
    let pref = MatmulPref::new(WORKSPACE_BYTES, available_alignments, p.dtype)?;
    // SAFETY: all descriptor/layout handles are live for the duration of the call.
    let heuristic = unsafe {
        result::get_matmul_algo_heuristic(
            handle.handle,
            desc.0,
            a_layout.0,
            b_layout.0,
            c_layout.0,
            c_layout.0,
            pref.0,
        )
    }
    .map_err(|e| {
        cublas_err(
            &format!(
                "no cuBLASLt algorithm for MatMul M={} K={} N={} batch={} dtype={:?}",
                p.m, p.k, p.n, p.batch, p.dtype
            ),
            e,
        )
    })?;
    let contract = MatmulAlgorithmContract::query(&heuristic.algo, p.dtype, available_alignments)?;
    Ok(PlannedMatmul {
        a_layout,
        b_layout,
        c_layout,
        _desc: desc,
        algo: heuristic.algo,
        workspace_bytes: heuristic.workspaceSize,
        dtype: p.dtype,
        contract,
    })
}

/// Exact scratch selected by the cuBLASLt heuristic under [`WORKSPACE_BYTES`].
///
/// The 32 MiB constant is a performance ceiling, not a requirement. Planning
/// and governed execution both call this same planner, then execution refuses a
/// short buffer before submitting the matmul.
pub fn gemm_workspace_bytes(handle: &CublasLt, p: &GemmParams) -> Result<usize> {
    Ok(plan_gemm(handle, p, PointerAlignmentAuthority::TypedMinimum)?.workspace_bytes)
}

unsafe fn launch_planned_gemm(
    handle: &CublasLt,
    stream: cudarc::driver::sys::CUstream,
    p: &GemmParams,
    plan: &PlannedMatmul,
    workspace: CUdeviceptr,
) -> Result<()> {
    plan.validate_pointers(MatmulPointers::row_major(p.a, p.b, p.c), "MatMul")?;
    let alpha: f32 = 1.0;
    let beta: f32 = 0.0;
    // SAFETY: layouts/desc/algo are live; a/b/c/workspace are caller-guaranteed
    // live device allocations of the right size; stream is valid.
    unsafe {
        // cuBLASLt picks and runs an algorithm here, allocating its own
        // workspace and synchronizing internally on its own schedule. Those are
        // calls this crate never makes and so cannot gate individually; gating
        // the whole invocation is what keeps them out of another thread's CUDA
        // graph capture. See `onnx_runtime_cuda_memory::capture_gate`.
        let _section = onnx_runtime_cuda_memory::capture_gate::synchronizing_section();
        result::matmul(
            handle.handle,
            plan._desc.0,
            (&alpha) as *const f32 as *const c_void,
            (&beta) as *const f32 as *const c_void,
            p.b as *const c_void,
            plan.a_layout.0,
            p.a as *const c_void,
            plan.b_layout.0,
            p.c as *const c_void,
            plan.c_layout.0,
            p.c as *mut c_void,
            plan.c_layout.0,
            (&plan.algo) as *const sys::cublasLtMatmulAlgo_t,
            workspace as *mut c_void,
            plan.workspace_bytes,
            stream as sys::cudaStream_t,
        )
    }
    .map_err(|e| cublas_err("cublasLtMatmul", e))
}

fn governed_workspace_ptr(
    workspace: Option<WorkspaceView>,
    required: usize,
    op: &str,
) -> Result<CUdeviceptr> {
    if required == 0 {
        return Ok(0);
    }
    let workspace = workspace.ok_or_else(|| {
        EpError::KernelFailed(format!(
            "cuda_ep {op}: governed cuBLASLt workspace requires {required} bytes, but none was supplied"
        ))
    })?;
    if workspace.bytes() < required {
        return Err(EpError::KernelFailed(format!(
            "cuda_ep {op}: governed cuBLASLt workspace requires {required} bytes, supplied {}",
            workspace.bytes()
        )));
    }
    Ok(workspace.ptr().0 as CUdeviceptr)
}

/// Execute `C = A · B` (row-major, optionally batched) on `stream` using the
/// column-major mapping documented at the top of this module.
///
/// # Safety
///
/// * `handle` must be a live cuBLASLt handle.
/// * `p.a`, `p.b`, `p.c` must be live device allocations large enough for all
///   matrices addressed by the supplied element strides and `p.dtype`.
/// * `workspace` must be a live device allocation of `workspace_bytes`.
/// * `stream` must be a valid CUDA stream; the owning context must be current on
///   the calling thread.
/// * `p.c` must not alias `p.a` or `p.b`.
#[allow(clippy::too_many_arguments)]
pub unsafe fn gemm(
    handle: &CublasLt,
    stream: cudarc::driver::sys::CUstream,
    p: &GemmParams,
    workspace: CUdeviceptr,
    workspace_bytes: usize,
) -> Result<()> {
    let plan = plan_gemm(handle, p, PointerAlignmentAuthority::TypedMinimum)?;
    if workspace_bytes < plan.workspace_bytes || (plan.workspace_bytes != 0 && workspace == 0) {
        return Err(EpError::KernelFailed(format!(
            "cuda_ep MatMul: cuBLASLt selected {} workspace bytes, supplied {workspace_bytes}",
            plan.workspace_bytes
        )));
    }

    // SAFETY: forwarded from the caller after validating the selected size.
    unsafe { launch_planned_gemm(handle, stream, p, &plan, workspace) }
}

/// Governed [`gemm`] adapter consuming an executor-prepared shared workspace.
///
/// # Safety
///
/// The tensor-pointer and stream requirements are identical to [`gemm`].
pub unsafe fn governed_gemm(
    handle: &CublasLt,
    stream: cudarc::driver::sys::CUstream,
    p: &GemmParams,
    workspace: Option<WorkspaceView>,
    op: &str,
) -> Result<()> {
    let plan = plan_gemm(handle, p, PointerAlignmentAuthority::TypedMinimum)?;
    let ptr = governed_workspace_ptr(workspace, plan.workspace_bytes, op)?;
    // SAFETY: forwarded from the caller; `ptr` covers the exact selected bytes.
    unsafe { launch_planned_gemm(handle, stream, p, &plan, ptr) }
}

/// A cuBLASLt plan (selected algorithm + its layouts/descriptor and workspace
/// requirement) for one **fixed** GEMM shape, selected once and reusable across
/// later launches — including CUDA-graph capture replays — with no further
/// heuristic query, allocation, or synchronization. The caller pins the shape
/// and supplies a persistent workspace of at least [`Self::workspace_bytes`]
/// (see the capture-safe dense path in `kernels::matmul`).
///
/// Capture planning uses the same descriptor and precision policy as
/// [`governed_gemm`], but may select a faster algorithm when the warm call's
/// actual pointers prove stronger alignment than the type minimum. The selected
/// alignment and reduction contracts are retained and checked on every launch.
pub struct CaptureGemmPlan(PlannedMatmul);

// SAFETY: cuBLASLt layout/descriptor handles are context-independent host
// objects; the algorithm is a plain value. Launches are serialized by the
// owning kernel's plan mutex (same rationale as `F32GemvPlan`).
unsafe impl Send for CaptureGemmPlan {}

impl CaptureGemmPlan {
    /// Workspace bytes the selected algorithm requires; the caller must supply a
    /// persistent device buffer of at least this size to [`Self::launch`].
    #[must_use]
    pub fn workspace_bytes(&self) -> usize {
        self.0.workspace_bytes
    }

    #[must_use]
    pub fn supports(&self, p: &GemmParams) -> bool {
        self.0.dtype == p.dtype
            && self
                .0
                .contract
                .supports(p.dtype, MatmulPointers::row_major(p.a, p.b, p.c))
    }

    #[must_use]
    pub fn algorithm_contract(&self) -> RowMajorGemmAlgorithmContract {
        self.0.contract.row_major()
    }

    /// Launch the planned GEMM for `p` (which must have the same shape/dtype the
    /// plan was selected for; only the `a`/`b`/`c` pointers may differ, and they
    /// must satisfy the retained alignment contract).
    ///
    /// # Safety
    ///
    /// Identical to [`gemm`]: `handle`/`stream` valid and current, `p.a`/`p.b`/
    /// `p.c` live device allocations for the plan's shape, `workspace` a live
    /// allocation of at least [`Self::workspace_bytes`] bytes (or 0 when that is
    /// 0), and `p.c` not aliasing `p.a`/`p.b`.
    pub unsafe fn launch(
        &self,
        handle: &CublasLt,
        stream: cudarc::driver::sys::CUstream,
        p: &GemmParams,
        workspace: CUdeviceptr,
    ) -> Result<()> {
        // SAFETY: forwarded from the caller.
        unsafe { launch_planned_gemm(handle, stream, p, &self.0, workspace) }
    }
}

/// Select a reusable [`CaptureGemmPlan`] for `p`'s shape and actual pointer
/// alignments.
pub fn plan_capture_gemm(handle: &CublasLt, p: &GemmParams) -> Result<CaptureGemmPlan> {
    Ok(CaptureGemmPlan(plan_gemm(
        handle,
        p,
        PointerAlignmentAuthority::ActualAddress,
    )?))
}

fn checked_layout_dim(value: usize, name: &str) -> Result<u64> {
    u64::try_from(value).map_err(|_| {
        EpError::KernelFailed(format!(
            "cuda_ep Einsum: cuBLASLt {name}={value} exceeds u64"
        ))
    })
}

fn checked_layout_stride(value: usize, name: &str) -> Result<i64> {
    i64::try_from(value).map_err(|_| {
        EpError::KernelFailed(format!(
            "cuda_ep Einsum: cuBLASLt {name}={value} exceeds i64"
        ))
    })
}

fn plan_strided_batched_gemm(
    handle: &CublasLt,
    p: &StridedBatchedGemmParams,
) -> Result<PlannedMatmul> {
    if p.m == 0 || p.n == 0 || p.k == 0 || p.batch == 0 {
        return Err(EpError::KernelFailed(format!(
            "cuda_ep Einsum: degenerate cuBLASLt contraction M={} K={} N={} batch={}",
            p.m, p.k, p.n, p.batch
        )));
    }

    let dt = p.dtype.data_type();
    // Row-major C[M,N] is column-major C^T[N,M]. Therefore cuBLASLt computes
    // B^T · A^T with the operand order swapped. A row-major transposed storage
    // view is represented by toggling the matching descriptor operation.
    let (b_rows, b_cols, b_ld) = if p.transpose_b {
        (p.k, p.n, p.k)
    } else {
        (p.n, p.k, p.n)
    };
    let (a_rows, a_cols, a_ld) = if p.transpose_a {
        (p.m, p.k, p.m)
    } else {
        (p.k, p.m, p.k)
    };
    let b_layout = MatrixLayout::new(
        dt,
        checked_layout_dim(b_rows, "B rows")?,
        checked_layout_dim(b_cols, "B columns")?,
        checked_layout_stride(b_ld, "B leading dimension")?,
    )?;
    let a_layout = MatrixLayout::new(
        dt,
        checked_layout_dim(a_rows, "A rows")?,
        checked_layout_dim(a_cols, "A columns")?,
        checked_layout_stride(a_ld, "A leading dimension")?,
    )?;
    let c_layout = MatrixLayout::new(
        dt,
        checked_layout_dim(p.n, "C rows")?,
        checked_layout_dim(p.m, "C columns")?,
        checked_layout_stride(p.n, "C leading dimension")?,
    )?;

    if p.batch > 1 {
        let count = i32::try_from(p.batch).map_err(|_| {
            EpError::KernelFailed(format!(
                "cuda_ep Einsum: cuBLASLt batch {} exceeds i32",
                p.batch
            ))
        })?;
        b_layout.set_batch(
            count,
            checked_layout_stride(p.b_batch_stride, "B batch stride")?,
        )?;
        a_layout.set_batch(
            count,
            checked_layout_stride(p.a_batch_stride, "A batch stride")?,
        )?;
        let c_stride = p.m.checked_mul(p.n).ok_or_else(|| {
            EpError::KernelFailed(format!(
                "cuda_ep Einsum: output matrix stride overflows for M={} N={}",
                p.m, p.n
            ))
        })?;
        c_layout.set_batch(count, checked_layout_stride(c_stride, "C batch stride")?)?;
    }

    let desc = MatmulDesc::new(p.dtype.compute_type(), sys::cudaDataType_t::CUDA_R_32F)?;
    // `transa` belongs to the first cuBLAS operand (row-major B), and `transb`
    // to the second (row-major A), because the row-major mapping swaps them.
    desc.set_transpose(
        sys::cublasLtMatmulDescAttributes_t::CUBLASLT_MATMUL_DESC_TRANSA,
        p.transpose_b,
    )?;
    desc.set_transpose(
        sys::cublasLtMatmulDescAttributes_t::CUBLASLT_MATMUL_DESC_TRANSB,
        p.transpose_a,
    )?;
    let pointers = MatmulPointers::row_major(p.a, p.b, p.c);
    let available_alignments =
        pointers.alignments(p.dtype, PointerAlignmentAuthority::ActualAddress);
    let pref = MatmulPref::new(WORKSPACE_BYTES, available_alignments, p.dtype)?;
    // SAFETY: all descriptor/layout handles are live for the query.
    let heuristic = unsafe {
        result::get_matmul_algo_heuristic(
            handle.handle,
            desc.0,
            b_layout.0,
            a_layout.0,
            c_layout.0,
            c_layout.0,
            pref.0,
        )
    }
    .map_err(|e| {
        cublas_err(
            &format!(
                "no cuBLASLt algorithm for Einsum M={} K={} N={} batch={} \
                 transpose_a={} transpose_b={} dtype={:?}",
                p.m, p.k, p.n, p.batch, p.transpose_a, p.transpose_b, p.dtype
            ),
            e,
        )
    })?;
    let contract = MatmulAlgorithmContract::query(&heuristic.algo, p.dtype, available_alignments)?;
    Ok(PlannedMatmul {
        a_layout: b_layout,
        b_layout: a_layout,
        c_layout,
        _desc: desc,
        algo: heuristic.algo,
        workspace_bytes: heuristic.workspaceSize,
        dtype: p.dtype,
        contract,
    })
}

/// Immutable cuBLASLt algorithm/layout selection for one canonical `Einsum`
/// contraction. Planning happens during warmup; later launches only update
/// tensor pointers and are legal during CUDA graph capture.
pub struct CaptureStridedBatchedGemmPlan(PlannedMatmul);

// SAFETY: identical to `CaptureGemmPlan`; the descriptor/layout handles are
// context-independent host objects and the owning kernel serializes launches.
unsafe impl Send for CaptureStridedBatchedGemmPlan {}

impl CaptureStridedBatchedGemmPlan {
    #[must_use]
    pub fn workspace_bytes(&self) -> usize {
        self.0.workspace_bytes
    }

    #[must_use]
    pub fn supports(&self, p: &StridedBatchedGemmParams) -> bool {
        self.0.dtype == p.dtype
            && self
                .0
                .contract
                .supports(p.dtype, MatmulPointers::row_major(p.a, p.b, p.c))
    }

    #[must_use]
    pub fn algorithm_contract(&self) -> RowMajorGemmAlgorithmContract {
        self.0.contract.row_major()
    }

    /// Launch the warmed contraction. Only A/B/C addresses may differ from the
    /// request used to create this plan.
    ///
    /// # Safety
    ///
    /// A/B/C and workspace must be live device allocations covering the fixed
    /// shape and strides represented by this plan, and C must not overlap A/B.
    pub unsafe fn launch(
        &self,
        handle: &CublasLt,
        stream: cudarc::driver::sys::CUstream,
        p: &StridedBatchedGemmParams,
        workspace: CUdeviceptr,
    ) -> Result<()> {
        self.0
            .validate_pointers(MatmulPointers::row_major(p.a, p.b, p.c), "Einsum")?;
        let alpha = 1.0f32;
        let beta = 0.0f32;
        // SAFETY: forwarded from the caller; all plan objects remain live.
        unsafe {
            let _section = onnx_runtime_cuda_memory::capture_gate::synchronizing_section();
            result::matmul(
                handle.handle,
                self.0._desc.0,
                (&alpha) as *const f32 as *const c_void,
                (&beta) as *const f32 as *const c_void,
                p.b as *const c_void,
                self.0.a_layout.0,
                p.a as *const c_void,
                self.0.b_layout.0,
                p.c as *const c_void,
                self.0.c_layout.0,
                p.c as *mut c_void,
                self.0.c_layout.0,
                (&self.0.algo) as *const sys::cublasLtMatmulAlgo_t,
                workspace as *mut c_void,
                self.0.workspace_bytes,
                stream as sys::cudaStream_t,
            )
        }
        .map_err(|e| cublas_err("cublasLtMatmul Einsum", e))
    }
}

/// Select one reusable cuBLASLt plan for a canonical `Einsum` contraction.
pub fn plan_capture_strided_batched_gemm(
    handle: &CublasLt,
    p: &StridedBatchedGemmParams,
) -> Result<CaptureStridedBatchedGemmPlan> {
    Ok(CaptureStridedBatchedGemmPlan(plan_strided_batched_gemm(
        handle, p,
    )?))
}

/// A single (non-batched) **column-major, native cuBLAS** GEMM request:
/// `C = alpha · op(A) · op(B) + beta · C`, with all shapes and leading
/// dimensions expressed in cuBLAS's own column-major terms.
///
/// The plain-`MatMul` path in [`gemm`] realises row-major ONNX semantics via
/// the operand-swap identity and never needs an explicit transpose. The
/// attention kernel, however, forms `Q·Kᵀ` (one transposed operand) and `P·V`
/// (no transpose) directly, so it drives cuBLASLt at this lower, unambiguous
/// column-major level and computes the leading dims / transpose flags itself
/// (see `kernels::attention` for the row-major → column-major derivation of
/// each GEMM). `alpha` lets the QKáµ€ stage fold in the softmax `scale` for free.
pub struct GemmEx {
    pub dtype: GemmDtype,
    /// Apply `opáµ€` to A (`CUBLAS_OP_T`) instead of `op` (`CUBLAS_OP_N`).
    pub transa: bool,
    pub transb: bool,
    /// cuBLAS column-major result dims: `C` is `m × n`, contraction `k`.
    pub m: usize,
    pub n: usize,
    pub k: usize,
    pub alpha: f32,
    pub beta: f32,
    pub a: CUdeviceptr,
    pub lda: usize,
    pub b: CUdeviceptr,
    pub ldb: usize,
    pub c: CUdeviceptr,
    pub ldc: usize,
    /// Optional in-GEMM bias/activation epilogue.
    pub epilogue: Option<GemmEpilogue>,
}

// cublasOperation_t is a plain C `enum` (4-byte int): CUBLAS_OP_N = 0, _T = 1.
// The cuBLASLt `sys` layer does not re-export it, so we pass the raw code.
const CUBLAS_OP_N: i32 = 0;
const CUBLAS_OP_T: i32 = 1;

impl MatmulDesc {
    /// Set the `CUBLASLT_MATMUL_DESC_TRANSA` / `TRANSB` operation for an operand.
    fn set_transpose(
        &self,
        attr: sys::cublasLtMatmulDescAttributes_t,
        transpose: bool,
    ) -> Result<()> {
        let op: i32 = if transpose { CUBLAS_OP_T } else { CUBLAS_OP_N };
        // SAFETY: `self.0` is a live desc; the buffer is a local `i32` matching
        // the 4-byte `cublasOperation_t` the attribute expects.
        unsafe {
            result::set_matmul_desc_attribute(
                self.0,
                attr,
                (&op) as *const i32 as *const c_void,
                std::mem::size_of::<i32>(),
            )
            .map_err(|e| cublas_err("set MATMUL_DESC_TRANS", e))
        }
    }
}

fn plan_gemm_ex(
    handle: &CublasLt,
    p: &GemmEx,
    alignment_authority: PointerAlignmentAuthority,
) -> Result<PlannedMatmul> {
    if p.m == 0 || p.n == 0 || p.k == 0 {
        return Err(EpError::KernelFailed(format!(
            "cuda_ep attention GEMM: degenerate dims M={} N={} K={}",
            p.m, p.n, p.k
        )));
    }

    let dt = p.dtype.data_type();
    let (a_rows, a_cols) = if p.transa {
        (p.k as u64, p.m as u64)
    } else {
        (p.m as u64, p.k as u64)
    };
    let (b_rows, b_cols) = if p.transb {
        (p.n as u64, p.k as u64)
    } else {
        (p.k as u64, p.n as u64)
    };
    let a_layout = MatrixLayout::new(dt, a_rows, a_cols, p.lda as i64)?;
    let b_layout = MatrixLayout::new(dt, b_rows, b_cols, p.ldb as i64)?;
    let c_layout = MatrixLayout::new(dt, p.m as u64, p.n as u64, p.ldc as i64)?;
    let desc = MatmulDesc::new(p.dtype.compute_type(), sys::cudaDataType_t::CUDA_R_32F)?;
    desc.set_transpose(
        sys::cublasLtMatmulDescAttributes_t::CUBLASLT_MATMUL_DESC_TRANSA,
        p.transa,
    )?;
    desc.set_transpose(
        sys::cublasLtMatmulDescAttributes_t::CUBLASLT_MATMUL_DESC_TRANSB,
        p.transb,
    )?;
    if let Some(epilogue) = p.epilogue {
        desc.set_epilogue(epilogue)?;
    }
    let pointers = MatmulPointers::column_major(p.a, p.b, p.c);
    let available_alignments = pointers.alignments(p.dtype, alignment_authority);
    let pref = MatmulPref::new(WORKSPACE_BYTES, available_alignments, p.dtype)?;
    // SAFETY: all descriptor/layout handles are live for the call.
    let heuristic = unsafe {
        result::get_matmul_algo_heuristic(
            handle.handle,
            desc.0,
            a_layout.0,
            b_layout.0,
            c_layout.0,
            c_layout.0,
            pref.0,
        )
    }
    .map_err(|e| {
        cublas_err(
            &format!(
                "no cuBLASLt algorithm for attention GEMM M={} N={} K={} transa={} transb={} dtype={:?}",
                p.m, p.n, p.k, p.transa, p.transb, p.dtype
            ),
            e,
        )
    })?;
    let contract = MatmulAlgorithmContract::query(&heuristic.algo, p.dtype, available_alignments)?;
    Ok(PlannedMatmul {
        a_layout,
        b_layout,
        c_layout,
        _desc: desc,
        algo: heuristic.algo,
        workspace_bytes: heuristic.workspaceSize,
        dtype: p.dtype,
        contract,
    })
}

/// Exact scratch selected for a column-major GEMM under [`WORKSPACE_BYTES`].
pub fn gemm_ex_workspace_bytes(handle: &CublasLt, p: &GemmEx) -> Result<usize> {
    Ok(plan_gemm_ex(handle, p, PointerAlignmentAuthority::TypedMinimum)?.workspace_bytes)
}

unsafe fn launch_planned_gemm_ex(
    handle: &CublasLt,
    stream: cudarc::driver::sys::CUstream,
    p: &GemmEx,
    plan: &PlannedMatmul,
    workspace: CUdeviceptr,
) -> Result<()> {
    plan.validate_pointers(
        MatmulPointers::column_major(p.a, p.b, p.c),
        "attention/Gemm",
    )?;
    let alpha = p.alpha;
    let beta = p.beta;
    // SAFETY: layouts/desc/algo live; a/b/c/workspace are caller-guaranteed
    // live device allocations of the right size; stream is valid.
    unsafe {
        // cuBLASLt picks and runs an algorithm here, allocating its own
        // workspace and synchronizing internally on its own schedule. Those are
        // calls this crate never makes and so cannot gate individually; gating
        // the whole invocation is what keeps them out of another thread's CUDA
        // graph capture. See `onnx_runtime_cuda_memory::capture_gate`.
        let _section = onnx_runtime_cuda_memory::capture_gate::synchronizing_section();
        result::matmul(
            handle.handle,
            plan._desc.0,
            (&alpha) as *const f32 as *const c_void,
            (&beta) as *const f32 as *const c_void,
            p.a as *const c_void,
            plan.a_layout.0,
            p.b as *const c_void,
            plan.b_layout.0,
            p.c as *const c_void,
            plan.c_layout.0,
            p.c as *mut c_void,
            plan.c_layout.0,
            (&plan.algo) as *const sys::cublasLtMatmulAlgo_t,
            workspace as *mut c_void,
            plan.workspace_bytes,
            stream as sys::cudaStream_t,
        )
    }
    .map_err(|e| cublas_err("cublasLtMatmul (attention)", e))
}

/// Execute one column-major `C = alpha·op(A)·op(B) + beta·C` on `stream`.
///
/// # Safety
///
/// * `handle` must be a live cuBLASLt handle.
/// * `p.a`, `p.b`, `p.c` must be live device allocations large enough for the
///   stated shapes / leading dims and `p.dtype`.
/// * `workspace` must be a live device allocation of `workspace_bytes`.
/// * `stream` must be valid and its owning context current on this thread.
/// * `p.c` must not alias `p.a` or `p.b`.
pub unsafe fn gemm_ex(
    handle: &CublasLt,
    stream: cudarc::driver::sys::CUstream,
    p: &GemmEx,
    workspace: CUdeviceptr,
    workspace_bytes: usize,
) -> Result<()> {
    let plan = plan_gemm_ex(handle, p, PointerAlignmentAuthority::TypedMinimum)?;
    if workspace_bytes < plan.workspace_bytes || (plan.workspace_bytes != 0 && workspace == 0) {
        return Err(EpError::KernelFailed(format!(
            "cuda_ep attention GEMM: cuBLASLt selected {} workspace bytes, supplied {workspace_bytes}",
            plan.workspace_bytes
        )));
    }

    // SAFETY: forwarded from the caller after validating the selected size.
    unsafe { launch_planned_gemm_ex(handle, stream, p, &plan, workspace) }
}

/// Governed [`gemm_ex`] adapter consuming an executor-prepared shared workspace.
///
/// # Safety
///
/// The tensor-pointer and stream requirements are identical to [`gemm_ex`].
pub unsafe fn governed_gemm_ex(
    handle: &CublasLt,
    stream: cudarc::driver::sys::CUstream,
    p: &GemmEx,
    workspace: Option<WorkspaceView>,
    op: &str,
) -> Result<()> {
    let plan = plan_gemm_ex(handle, p, PointerAlignmentAuthority::TypedMinimum)?;
    let ptr = governed_workspace_ptr(workspace, plan.workspace_bytes, op)?;
    // SAFETY: forwarded from the caller; `ptr` covers the exact selected bytes.
    unsafe { launch_planned_gemm_ex(handle, stream, p, &plan, ptr) }
}

#[cfg(test)]
mod raw_workspace_allocation_guard {
    use onnx_runtime_ep_api::DevicePtrMut;

    use super::*;

    #[test]
    fn exact_requirement_is_persistent_and_shortfall_is_deterministic() {
        let requirement = governed_workspace_requirement(96);
        assert_eq!(requirement.bytes, 96);
        assert_eq!(requirement.alignment, WORKSPACE_ALIGNMENT);
        assert_eq!(requirement.lifetime, WorkspaceLifetime::SessionPersistent);
        assert!(matches!(
            requirement.role,
            MemoryRole::Workspace { step_scoped: false }
        ));

        let short = WorkspaceView::new(DevicePtrMut(std::ptr::null_mut()), 95);
        let error = governed_workspace_ptr(Some(short), 96, "test")
            .expect_err("a short prepared slot must fail before cuBLASLt launch");
        assert!(format!("{error}").contains("requires 96 bytes, supplied 95"));
    }

    #[test]
    fn pointer_alignment_uses_the_typed_origin_and_caps_only_the_reported_proof() {
        assert_eq!(
            pointer_alignment(0, GemmDtype::F16, PointerAlignmentAuthority::TypedMinimum),
            2
        );
        assert_eq!(
            pointer_alignment(0, GemmDtype::F32, PointerAlignmentAuthority::ActualAddress),
            1
        );
        assert_eq!(
            pointer_alignment(
                0x104,
                GemmDtype::F32,
                PointerAlignmentAuthority::ActualAddress
            ),
            4
        );
        assert_eq!(
            pointer_alignment(
                0x180,
                GemmDtype::F16,
                PointerAlignmentAuthority::ActualAddress
            ),
            128
        );
        assert_eq!(
            pointer_alignment(
                0x400,
                GemmDtype::F32,
                PointerAlignmentAuthority::ActualAddress
            ),
            256
        );
    }

    #[test]
    fn reduced_precision_contract_rejects_output_storage_schemes() {
        assert!(CublasLtReductionScheme::None.preserves_f32_intermediates());
        assert!(CublasLtReductionScheme::ComputeType.preserves_f32_intermediates());
        assert!(!CublasLtReductionScheme::InPlace.preserves_f32_intermediates());
        assert!(!CublasLtReductionScheme::OutputType.preserves_f32_intermediates());
    }

    #[test]
    fn governed_gemm_sites_do_not_allocate_the_32_mib_ceiling_raw() {
        let sites = [
            ("fused_gemm.rs", include_str!("kernels/fused_gemm.rs")),
            ("gemm.rs", include_str!("kernels/gemm.rs")),
            ("matmul.rs", include_str!("kernels/matmul.rs")),
            ("matmul_nbits.rs", include_str!("kernels/matmul_nbits.rs")),
        ];
        for (name, source) in sites {
            let lines = source.lines().collect::<Vec<_>>();
            for (index, line) in lines.iter().enumerate() {
                if !line.contains("alloc_raw") {
                    continue;
                }
                let end = (index + 4).min(lines.len());
                let allocation = lines[index..end].join(" ");
                assert!(
                    !allocation.contains("WORKSPACE_BYTES"),
                    "{name} reintroduced a raw allocation of the cuBLASLt 32 MiB ceiling near line {}; route it through the governed shared workspace",
                    index + 1
                );
            }
            assert!(
                source.contains("fn workspace_requirement"),
                "{name} must report its cuBLASLt scratch during prepare-only planning"
            );
            assert!(
                source.contains("fn execute_with_workspace"),
                "{name} must consume the executor-prepared shared workspace"
            );
            assert!(
                source.contains("governed_gemm"),
                "{name} must use the shared cuBLASLt governed adapter"
            );
        }

        let bindings = include_str!("../../onnx-runtime-session/src/executor/bindings.rs");
        for op in [
            "MatMul",
            "Gemm",
            "MatMulNBits",
            "FusedMatMulBias",
            "FusedGemm",
        ] {
            assert!(
                bindings.contains(op),
                "{op} must remain in the centralized is_planned_workspace_node predicate"
            );
        }
    }
}