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
//! Element types and trait hierarchy shared across baracuda kernel
//! wrappers.
//!
//! # Trait map
//!
//! [`KernelDtype`] is the **umbrella marker** every kernel-usable dtype
//! implements. It captures the minimum a dtype needs to participate in
//! any kernel: a fixed memory layout ([`DeviceRepr`]), `Copy + 'static`,
//! and a runtime tag ([`ElementKind`]) for dispatch. Phase 28 added
//! `KernelDtype` as a `1.0`-freeze stability prereq — code that wants to
//! accept *any* dtype (sub-byte, FP8, packed-bit) without enumerating
//! sibling traits now has a single bound to reach for.
//!
//! The op-shaped sub-traits all extend [`KernelDtype`]:
//!
//! - [`Element`] — the **plan-shaped** family that participates in the
//! `<T: Element>`-parameterized elementwise plans
//! (`UnaryPlan<T, N>`, `BinaryPlan<T, N>`, …). Today: `f16`, `bf16`,
//! `f32`, [`F32Strict`], `f64`, `i32`, `i64`, [`Bool`], [`Complex32`],
//! [`Complex64`]. Adds a `type Scalar: ScalarType` projection for the
//! kernel's α/β scalar type.
//! - [`IntElement`] — sub-byte / byte-packed integer GEMM operand types
//! ([`S8`], [`U8`], [`S4`], [`U4`]). Distinct trait because the
//! int-GEMM kernels use an int32 accumulator with float α/β, a
//! programming model that doesn't share kernel shape with the
//! elementwise plans.
//! - [`FpElement`] — 8-bit floating-point GEMM operands ([`Fp8E4M3`],
//! [`Fp8E5M2`]). sm_89+ only.
//! - [`BinElement`] — 1-bit packed-byte binary GEMM operands ([`Bin`]).
//! Distinct programming model (`mma.sync ... .b1.b1.s32.xor.popc`).
//!
//! Three sibling traits cover the auxiliary slot types that don't fit
//! [`KernelDtype`]'s `ElementKind` projection (they have their own kind
//! enum):
//!
//! - [`BiasElement`] — bias broadcast element types accepted by integer
//! GEMM epilogues. Today: `f32` and `i32`.
//! - [`IndexElement`] — index element types accepted by indexing /
//! embedding / segment kernel families. Today: `i32` (legacy) and
//! `i64` (PyTorch default).
//! - [`IndexOutputElement`] — output index dtype produced by
//! arg-reduction kernels. Today: `u32`, `i32`, `i64`.
//!
//! # When to use which
//!
//! - Reach for [`Element`] when writing a plan parameterized over the
//! primitive-FP / int / bool / complex family that goes through the
//! shared `BinaryPlan<T, N>` / `UnaryPlan<T, N>` shape.
//! - Reach for [`IntElement`] / [`FpElement`] / [`BinElement`] when the
//! plan is one of the sub-byte / packed GEMM families.
//! - Reach for [`KernelDtype`] when you genuinely don't care which
//! family the dtype belongs to — e.g. a generic "dtype size in bytes"
//! helper, a telemetry function that just needs the [`ElementKind`]
//! tag, or a downstream wrapper that wants to accept the union of all
//! kernel-usable dtypes.
//!
//! `Element` was originally named `CutlassElement` in the
//! `baracuda-cutlass` crate. The rename here unifies the vocabulary
//! across the wider kernel facade — `baracuda-cutlass` keeps the
//! `CutlassElement` name available as a re-export for back-compat.
use DeviceRepr;
use ;
/// Umbrella marker trait for every dtype usable as a kernel input or
/// output.
///
/// The bound captures the three minimum properties a kernel dtype
/// needs: a fixed memory layout ([`DeviceRepr`]) so the host can ship
/// bytes to the device verbatim, `Copy + 'static` so the type can
/// flow through plan / args structs without an `&mut self`, and a
/// runtime tag ([`ElementKind`]) for dispatch.
///
/// `KernelDtype` is **wider** than [`Element`]: it covers the
/// sub-byte / FP8 / packed-bit newtypes (`S4`, `U4`, `S8`, `U8`,
/// `Fp8E4M3`, `Fp8E5M2`, `Bin`) that have their own kernel families
/// and don't fit the `<T: Element>` plan shape. Every [`Element`],
/// [`IntElement`], [`FpElement`], and [`BinElement`] type also
/// implements `KernelDtype` (the sibling traits all use it as a
/// supertrait), so a function bounded by `<T: KernelDtype>` accepts
/// any kernel-usable type.
///
/// Sealed because adding a new dtype requires a matching kernel
/// instantiation in `baracuda-kernels-sys`.
///
/// # When to use
///
/// Prefer [`Element`] when you're parameterizing a plan that lives in
/// the elementwise / reduce / scan / norm / loss families — those
/// plan shapes are written against `<T: Element>` and use the
/// `type Scalar` projection. Reach for `KernelDtype` only when you
/// genuinely want the **union** of every kernel dtype (sub-byte +
/// FP8 + packed-bit included) — e.g. a generic dtype-size helper,
/// telemetry function, or downstream wrapper.
/// Sealed marker for the alpha/beta scalar type an [`Element`] uses.
///
/// `f32` for f16/bf16/f32/[`F32Strict`] kernels (epilogue compute runs at
/// f32). `f64` for f64 kernels. Sealed to keep the kernel-side dispatch
/// closed — adding a new scalar type requires shipping new C ABI
/// signatures in the underlying `*-kernels-sys` crate.
/// Element types supported by the kernel facade.
///
/// Sealed to prevent downstream `impl`s — adding a new dtype requires
/// shipping a new kernel instantiation in the corresponding `*-kernels-sys`
/// crate.
///
/// The trait spans three families that share the `<T: Element>`-
/// parameterized plan shape but route through distinct kernel SKUs:
///
/// - **Floating-point**: `f16`, `bf16`, `f32`, [`F32Strict`], `f64`.
/// `f32` reduces through TF32 tensor cores (10-bit mantissa);
/// [`F32Strict`] uses SIMT CUDA cores at full IEEE 754 binary32 with
/// bit-stable results. The `Scalar` projection is `f32` for the
/// 16-bit / 32-bit float members and `f64` for `f64`.
/// - **Integer**: `i32`, `i64`. Used for elementwise integer arithmetic
/// (bitwise ops, integer comparison). The `Scalar` projection is
/// `f32` — these types don't participate in α/β-scaled epilogues, so
/// the projection is nominal. Note: [`S8`] / [`U8`] / [`S4`] / [`U4`]
/// are GEMM-only operand types and live on the separate [`IntElement`]
/// trait — they don't implement [`Element`].
/// - **Boolean**: [`Bool`] (1-byte storage, 0/non-zero truthiness).
/// Used for logical ops and as the output type of comparison ops.
/// The `Scalar` projection is `f32` (also nominal).
///
/// Sibling traits [`IntElement`], [`FpElement`], [`BinElement`], and
/// [`BiasElement`] cover GEMM-only / FP8 / packed-bit / bias-broadcast
/// types respectively; those have their own kernel families and don't
/// route through `<T: Element>`-parameterized elementwise plans. The
/// umbrella [`KernelDtype`] supertrait covers the union of `Element`
/// + `IntElement` + `FpElement` + `BinElement`.
///
/// # `KIND` lookup
///
/// `Element` does NOT redeclare `const KIND`; the const is inherited
/// from the [`KernelDtype`] supertrait. This keeps `T::KIND` unambiguous
/// at every call site under `<T: Element>` bounds. Pre-Phase-28 code
/// using the fully-qualified form `<T as Element>::KIND` must update
/// to `<T as KernelDtype>::KIND` (or just plain `T::KIND` which works
/// regardless of which trait bound is in scope).
/// `f32` GEMM routes through TF32 tensor cores — see
/// [`crate::PrecisionGuarantee::math_precision`] (returns
/// [`MathPrecision::Tf32`]). Inputs are full F32; the math instruction
/// reduces to TF32 (10-bit mantissa) and accumulates into F32. Use
/// [`F32Strict`] instead when bit-stable, full-precision IEEE 754
/// binary32 math is required.
/// `f64` GEMM via Ampere FP64 tensor cores (DGEMM). Full IEEE 754
/// binary64 inputs, accumulator, and scalars. Analogous to cuBLAS's
/// `CUBLAS_COMPUTE_64F`.
/// `i32` as an elementwise kernel input element. Used by the integer
/// arithmetic kernels (bitwise and / or / xor / shift, integer
/// comparison, integer scans). Distinct from [`ElementKind::I32`]'s
/// historical use as an accumulator-only marker for integer GEMMs —
/// here `i32` is a first-class kernel *input* type with an `Element`
/// impl, so the same `BinaryPlan<T, N>` / `UnaryPlan<T, N>` shapes
/// extend to integer arithmetic.
///
/// The `Scalar` projection is `f32` (nominal — integer kernels don't
/// use α/β-scaled epilogues today).
/// `i64` as an elementwise kernel input element. Sibling of the `i32`
/// impl above for 64-bit integer arithmetic (PyTorch's default integer
/// tensor dtype). Same kernel families, twice the storage width.
/// Boolean as an elementwise kernel input element. Used by the logical
/// op family (`logical_and` / `logical_or` / `logical_xor`) and as the
/// output type of comparison ops. Storage is 1 byte per element via the
/// [`Bool`] wrapper.
///
/// The `Scalar` projection is `f32` (nominal).
/// Single-precision complex (interleaved real/imag pair of `f32`) as an
/// elementwise kernel input element. Used by the FFT family (`fft`,
/// `ifft`, `rfft` output / `irfft` input, etc.) for spectrum-domain
/// tensors. The `Scalar` projection is `f32` (matches the real width).
/// Double-precision complex (interleaved real/imag pair of `f64`) as an
/// elementwise kernel input element. Sibling to [`Complex32`]; the
/// `Scalar` projection is `f64`.
// ============================================================================
// Boolean element type — implements Element directly
// ============================================================================
/// Boolean element marker. `#[repr(transparent)]` wrapper around `u8`
/// (1-byte storage).
///
/// Truthiness convention follows PyTorch / NumPy: `0` is false; **any**
/// non-zero byte is true. Kernels that consume `Bool` operands normalize
/// the input to `0` or `1` before applying the logical op so the result
/// is always strictly `0` or `1`. The wrapper is `#[repr(transparent)]`
/// over `u8`, so a `DeviceBuffer<u8>` (byte substrate) can be
/// reinterpreted as a `DeviceBuffer<Bool>` via `view_as` without
/// copying.
///
/// Used as the element type of comparison-op output tensors (`eq`, `gt`,
/// …) and as the input element type for the logical-op family
/// (`logical_and`, `logical_or`, `logical_xor`). Implements [`Element`]
/// so the same `BinaryPlan<T, N>` / `UnaryPlan<T, N>` shapes extend to
/// boolean tensors.
;
// SAFETY: Bool is #[repr(transparent)] around u8, which is DeviceRepr.
// Same ABI, same Copy + 'static bounds.
unsafe
// ============================================================================
// Complex element types — implements Element directly
// ============================================================================
/// Single-precision complex element. `#[repr(C)]` struct of two `f32`
/// fields (real, imag) — ABI-compatible with cuFFT's `cufftComplex`
/// (which is itself an alias for CUDA's `float2`), with NumPy's
/// `complex64`, and with PyTorch's `torch.complex64`.
///
/// Used by the FFT op family (Milestone 6.4) as the element type for
/// spectrum-domain tensors. Complex arithmetic is not a kernel concern
/// at this layer — Rust callers build / inspect complex values via the
/// `re` / `im` fields and pass `DeviceBuffer<Complex32>` directly to
/// the FFT plans, which reinterpret them as `cufftComplex` over the
/// FFI boundary.
///
/// Layout invariant: `Complex32 { re, im }` and `cufftComplex { x, y }`
/// share identical byte storage on every platform CUDA supports
/// (`(f32, f32)` is 8-byte aligned, naturally padded). A
/// `DeviceBuffer<Complex32>` can be reinterpreted as a
/// `DeviceBuffer<cufftComplex>` via `view_as` without copying.
/// Double-precision complex element. `#[repr(C)]` struct of two `f64`
/// fields — ABI-compatible with cuFFT's `cufftDoubleComplex`, NumPy's
/// `complex128`, and PyTorch's `torch.complex128`. Sibling to
/// [`Complex32`].
// SAFETY: Complex32 / Complex64 are #[repr(C)] structs of two FP fields
// each, with no padding (8-byte and 16-byte natural alignment), so they
// satisfy DeviceRepr's invariants (no uninitialized bytes, no host-side
// resource handles, byte-for-byte transferable between host and device).
unsafe
unsafe
// ============================================================================
// Integer element family — sibling to Element
// ============================================================================
/// Signed 8-bit integer element marker. `#[repr(transparent)]` around
/// `i8`.
///
/// Identical memory layout to `i8`, so a `DeviceBuffer<i8>` (or any byte
/// substrate the caller has) can be reinterpreted as a `DeviceBuffer<S8>`
/// via `view_as` without copying. The wrapper exists to drive kernel
/// selection at the Rust type level: integer GEMM plans parameterized on
/// `S8` route the launch through the signed int8 tensor-core kernels.
///
/// Numerical contract: int8 inputs, int32 accumulator, float alpha/beta
/// scaling, saturating round-to-nearest cast back to int8 on store.
;
/// Unsigned 8-bit integer element marker. `#[repr(transparent)]` around
/// `u8`.
///
/// Identical memory layout to `u8`, so a `DeviceBuffer<u8>` (byte
/// substrate) can be reinterpreted as a `DeviceBuffer<U8>` (quantized
/// GEMM operand) via `view_as` without copying. The wrapper exists to
/// disambiguate "byte buffer" from "quantized operand" at the Rust type
/// level — a `DeviceBuffer<U8>` is unambiguously a GEMM operand,
/// `DeviceBuffer<u8>` stays a byte-storage abstraction.
///
/// Numerical contract: same as [`S8`] except the multiply operands are
/// unsigned. The accumulator is still int32 and alpha/beta are still
/// float; saturating cast at store clamps to `[0, 255]`.
;
// SAFETY: S8 / U8 are #[repr(transparent)] around i8 / u8, which are
// both DeviceRepr. Same ABI, same Copy + 'static bounds.
unsafe
unsafe
/// Signed 4-bit integer element marker — **packed-pair storage**.
///
/// `#[repr(transparent)]` around `u8`. One [`S4`] *storage slot* is one
/// byte and holds **two** packed s4 elements: the low nibble is the
/// element at even logical index, the high nibble is the element at
/// odd logical index (along the K axis for A/B operands, along the
/// N axis for D output). Sign-extended to s32 on the GPU side via
/// `((s8)(nibble << 4)) >> 4`.
///
/// A `DeviceBuffer<u8>` of `(M*K)/2` bytes can be reinterpreted as a
/// `DeviceBuffer<S4>` of `(M*K)/2` storage slots via `view_as` without
/// copying — `S4` is byte-storage at the buffer layer, and *element
/// count* lives at the plan-layer descriptor (M / N / K).
///
/// Numerical range per element: `[-8, +7]`. The plan layer
/// (`Int4GemmPlan` in `baracuda-kernels`) takes `M`, `N`, `K` in
/// **element** counts and leading dimensions in **storage-slot
/// (= byte)** counts — `MatrixRef<S4>::ld` therefore equals `K / 2` for
/// row-major A with no padding. `K` must be even (packing is byte-
/// aligned). Routes through Ada Lovelace int4 tensor cores
/// (`mma.sync.aligned.m16n8k64.row.col.satfinite.s32.s4.s4.s32`) with
/// S32 accumulation and float `alpha` / `beta` scaling. First landed in
/// baracuda-kernels Phase 2.
;
/// Unsigned 4-bit integer element marker — **packed-pair storage**.
///
/// `#[repr(transparent)]` around `u8`. Packing convention is identical
/// to [`S4`] (low nibble = even index, high nibble = odd index); the
/// only difference is zero-extension to s32 on the GPU side
/// (`nibble & 0xF`).
///
/// Numerical range per element: `[0, 15]`. Plan-layer conventions
/// (M/N/K in elements, LDs in storage slots, K even) match [`S4`].
/// Routes through Ada Lovelace int4 tensor cores
/// (`mma.sync.aligned.m16n8k64.row.col.satfinite.s32.u4.u4.s32`) with
/// the same S32 accumulator and `float` α/β family as [`S4`].
;
// SAFETY: S4 / U4 are #[repr(transparent)] around u8, which is DeviceRepr.
// Same ABI, same Copy + 'static bounds.
unsafe
unsafe
/// Integer element types supported by the int-GEMM kernel set.
///
/// Sibling trait to [`Element`] (the float family) — kept separate
/// because the kernel-level dispatch, accumulator type (int32 vs f32),
/// and epilogue family differ enough that mixing them through a single
/// trait would smear the type signatures of integer plans.
///
/// Sealed to prevent downstream `impl`s — adding a new int dtype
/// requires shipping new kernel instantiations.
///
/// `KIND` is inherited from the [`KernelDtype`] supertrait. Pre-Phase-28
/// code using `<T as IntElement>::KIND` must update to plain `T::KIND`
/// or `<T as KernelDtype>::KIND`.
// ============================================================================
// 8-bit floating-point element family — sibling to Element / IntElement
// ============================================================================
/// 8-bit floating-point, E4M3 encoding (1 sign + 4 exponent + 3 mantissa,
/// exponent bias 7).
///
/// `#[repr(transparent)]` around `u8` storage — bit-compatible with
/// `__nv_fp8_storage_t` on the CUDA side and with `float8::F8E4M3` on the
/// host side. A `DeviceBuffer<u8>` (byte substrate) can be reinterpreted
/// as `DeviceBuffer<Fp8E4M3>` via `view_as` without copying.
///
/// Numerical range: ±448 (max finite). One NaN encoding only
/// (`S.1111.111`); E4M3 has **no infinities**. The conversion path
/// matches NVIDIA's `__nv_cvt_float_to_fp8(x, __NV_SATFINITE, __NV_E4M3)`:
/// round-half-to-even, saturating-to-max-finite on overflow.
///
/// Routes through Ada Lovelace FP8 tensor cores
/// (`mma.sync.aligned.m16n8k32.row.col.f32.e4m3.e4m3.f32`) with F32
/// accumulation and float alpha / beta scaling. First landed in
/// baracuda-kernels Phase 2.
;
// SAFETY: Fp8E4M3 is #[repr(transparent)] over u8, which is DeviceRepr.
// Same ABI, same Copy + 'static bounds.
unsafe
/// 8-bit floating-point, E5M2 encoding (1 sign + 5 exponent + 2 mantissa,
/// exponent bias 15).
///
/// `#[repr(transparent)]` around `u8` storage — bit-compatible with
/// `__nv_fp8_storage_t` on the CUDA side and with `float8::F8E5M2` on the
/// host side. A `DeviceBuffer<u8>` (byte substrate) can be reinterpreted
/// as `DeviceBuffer<Fp8E5M2>` via `view_as` without copying.
///
/// Numerical range: ±57344 (max finite). IEEE-style infinity and NaN
/// encodings (unlike [`Fp8E4M3`], which has neither). The conversion
/// path matches NVIDIA's
/// `__nv_cvt_float_to_fp8(x, __NV_SATFINITE, __NV_E5M2)`:
/// round-half-to-even, saturating-to-max-finite on overflow.
///
/// Routes through Ada Lovelace FP8 tensor cores
/// (`mma.sync.aligned.m16n8k32.row.col.f32.e5m2.e5m2.f32`) with F32
/// accumulation and float alpha / beta scaling.
;
// SAFETY: Fp8E5M2 is #[repr(transparent)] over u8, which is DeviceRepr.
// Same ABI, same Copy + 'static bounds.
unsafe
/// 8-bit floating-point element types supported by the kernel facade.
///
/// Sibling trait to [`Element`] (which covers f16 / bf16 / f32 /
/// [`F32Strict`] / f64) and to [`IntElement`] (which covers S8 / U8) —
/// kept separate because the FP8 kernel family has its own MMA
/// instruction set (`mma.sync ... .f32.e4m3.e4m3.f32`), arch requirement
/// (sm_89+), and conversion semantics (saturating-to-max-finite vs the
/// int family's saturating-to-INT_MAX).
///
/// Sealed because adding a new FP8 variant requires shipping new kernel
/// instantiations in `baracuda-kernels-sys`.
///
/// `KIND` is inherited from the [`KernelDtype`] supertrait. Pre-Phase-28
/// code using `<T as FpElement>::KIND` must update to plain `T::KIND`
/// or `<T as KernelDtype>::KIND`.
// ============================================================================
// Binary element family — sibling to Element / IntElement / FpElement
// ============================================================================
/// 1-bit binary element marker — **packed-byte storage**.
///
/// `#[repr(transparent)]` around `u8`. One [`Bin`] *storage slot* is one
/// byte and holds **eight** packed b1 elements: bit `i` of the byte
/// (LSB = bit 0) is the element at K offset `8 * byte_idx + i`. Packing
/// is along the K axis for A/B operands.
///
/// A `DeviceBuffer<u8>` of `(M*K)/8` bytes can be reinterpreted as a
/// `DeviceBuffer<Bin>` of `(M*K)/8` storage slots via `view_as` without
/// copying.
///
/// Routes through Ampere+ binary tensor cores
/// (`mma.sync.aligned.m16n8k256.row.col.s32.b1.b1.s32.xor.popc`) with
/// an **S32 output accumulator**. Unlike the int4 / int8 / FP8
/// families, bin GEMM does **not** quantize its output back to the
/// input element type — the result is the raw popcount accumulator
/// (`popcount(xor(A_row, B_col))` summed over K bytes), surfaced as
/// `i32`. No α / β / bias / activation chain (the popcount programming
/// model doesn't have a meaningful place for them).
///
/// The plan layer ([`Bin` is consumed by `BinGemmPlan` in
/// `baracuda-kernels`) takes `M`, `N`, `K` in **element** counts and
/// leading dimensions in **storage-slot (= byte)** counts —
/// `MatrixRef<Bin>::ld` therefore equals `K / 8` for row-major A with
/// no padding. `K` must be divisible by 8 (packing is byte-aligned).
;
// SAFETY: Bin is #[repr(transparent)] around u8, which is DeviceRepr.
unsafe
/// Binary (1-bit) element types supported by the kernel facade.
///
/// Sibling trait to [`Element`] / [`IntElement`] / [`FpElement`] —
/// kept separate because the bin kernel family has a distinct
/// programming model (popcount-based, `D = popcount(xor(A, B))`, no
/// α/β/bias/activation chain) and a non-matching output type (raw
/// `i32` accumulator rather than re-quantized to the input type).
///
/// `KIND` is inherited from the [`KernelDtype`] supertrait. Pre-Phase-28
/// code using `<T as BinElement>::KIND` must update to plain `T::KIND`
/// or `<T as KernelDtype>::KIND`.
/// Bias element types accepted by the int-GEMM bias epilogue family.
///
/// Integer GEMM kernels can broadcast either a per-channel `f32` bias
/// (matching the float bias convention used elsewhere) or a per-channel
/// `i32` bias (matching TensorRT's int8 inference convention). The
/// choice is a compile-time generic on integer plans — `<T, f32>` and
/// `<T, i32>` resolve to distinct kernel SKUs.
///
/// Sealed because the bias-element kernel variants are baked into the
/// `*-kernels-sys` crates at build time.
/// Runtime tag for a [`BiasElement`].
///
/// **Intentionally NOT `#[non_exhaustive]`** — the int-GEMM bias
/// dispatchers exhaustively match `(T::KIND, BT::KIND)` to pick
/// per-bias-dtype kernel SKUs. Adding a new bias dtype (e.g. `f16`
/// for quantized-GEMM) should surface as a build break across every
/// match site so each can wire or reject. New variants are a
/// deliberate breaking-change event.
/// Sealed marker trait for index-element types accepted by the
/// indexing / embedding / segment kernel families.
///
/// Phase 11.5 (Fuel team feedback #7): split out as a sibling of
/// [`Element`] so plans like [`crate::indexing::GatherPlan`] /
/// [`crate::embedding::EmbeddingPlan`] / [`crate::segment::SegmentSumPlan`]
/// can dispatch over the index dtype without coupling the value-dtype
/// trait hierarchy. Today's members are `i32` (legacy) and `i64`
/// (PyTorch default). Sealed because new members require a matching
/// FFI entry point in the `*-kernels-sys` crate.
/// Runtime tag for an [`IndexElement`]. `i32` is the legacy default;
/// `i64` was added in Phase 11.5 to match PyTorch's int64 index
/// convention without an extra cast pass.
///
/// `#[non_exhaustive]` — additional index dtypes (`u32` follows the
/// IndexOutputElement precedent) may land in future phases. Match
/// arms must include a `_ =>` catch-all.
/// Sealed marker trait for the *output* index dtype produced by
/// arg-reduction kernels (`argmax` / `argmin` axis ops).
///
/// Phase 12.2 (Fuel team feedback): split out as a sibling of
/// [`IndexElement`] (which marks *input* index dtypes accepted by
/// indexing / embedding / segment kernels) so plans like
/// [`crate::ArgReduceKind`]-driven `ArgReducePlan` can dispatch over the
/// output dtype without affecting the input-index trait hierarchy.
///
/// Today's members are `u32`, `i32`, and `i64`. PyTorch defaults to
/// `i64`; CUB / NVIDIA libraries and some downstream frameworks (e.g.
/// Fuel) prefer `u32`. The trait is sealed because new members require
/// a matching FFI entry point in the `*-kernels-sys` crate.
/// Runtime tag for an [`IndexOutputElement`]. `i64` is the default
/// (PyTorch convention) and the only variant prior to Phase 12.2;
/// `u32` and `i32` were added so downstream frameworks that prefer
/// narrower index dtypes (Fuel uses `u32`) can avoid a post-pass cast.
///
/// `#[non_exhaustive]` — additional output index dtypes (`u64` for
/// frameworks that prefer unsigned indices end-to-end) may land in
/// future phases. Match arms must include a `_ =>` catch-all.
/// Strict-precision f32 element marker.
///
/// `#[repr(transparent)]` wrapper around `f32`. Identical memory layout
/// to a plain `f32` device buffer — a `DeviceBuffer<f32>` can be
/// reinterpreted as a `DeviceBuffer<F32Strict>` via `view_as` without
/// copying. The wrapper exists purely to drive kernel selection at the
/// Rust type level: choosing the `F32Strict` element routes the launch
/// through the SIMT (CUDA-cores) GEMM kernels, while the plain `f32`
/// element routes through the TF32 tensor-core kernels.
///
/// Numerical contract: full IEEE 754 binary32 multiply-add throughout
/// (no tensor-core warp-reduction nondeterminism).
;
// SAFETY: F32Strict is #[repr(transparent)] around f32, which is itself
// DeviceRepr. Same ABI, same Copy + 'static bounds.
unsafe
// ============================================================================
// KernelDtype umbrella impls — every concrete kernel dtype is sealed here.
// ============================================================================
//
// One macro keeps the 17 impls visually flat. The Phase 28 refactor
// removed `const KIND` from the per-family sibling traits (`Element`,
// `IntElement`, `FpElement`, `BinElement`); `KIND` now lives only on
// the [`KernelDtype`] supertrait, so `T::KIND` resolves uniquely
// under any subtrait bound.
impl_kerneldtype!
/// Runtime tag for an [`Element`] or [`IntElement`].
///
/// Unified across the float and integer kernel families so that a single
/// kernel-SKU descriptor can describe any baracuda kernel.
/// Math precision used by the FMA / tensor-core instruction.
///
/// Distinct from the *input* element type because tensor cores can take
/// inputs at one precision and reduce through an instruction at a
/// different precision (most notably TF32: F32 inputs, 10-bit-mantissa
/// math).