baracuda-kernels-sys 0.0.1-alpha.68

Compiled bespoke .cu kernel template instantiations for the baracuda ML kernel facade plus C-ABI FFI facades for the library-backed plans (cuDNN conv/pool, cuSOLVER linalg, cuFFT/cuRAND, CUTLASS GEMM re-export). Hosts curated CUDA kernel sources (int8/FP8/int4/bin GEMM RRR, elementwise, reduce, norm, attention, …), builds them via baracuda-forge, exposes extern "C" entry points for the safe baracuda-kernels crate. CUTLASS template kernels live in the sibling baracuda-cutlass-kernels-sys crate and are re-exported here under the unified baracuda_kernels_gemm_* namespace.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
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
// baracuda_segment.cuh
//
// Templated kernels and INSTANTIATE macros for the segment / scatter-
// reduce op family (Phase 7 Milestone 7.6 — Category S from the
// comprehensive plan).
//
// Algorithm choices:
//
//  Sorted family — segment_ids[i] is monotonically non-decreasing in i.
//    One thread per output cell (segment_id, d). Each thread binary-
//    searches the segment_ids array for the half-open range
//    [start, end) covering its segment_id, then sweeps input[i, d] for
//    i in [start, end) accumulating the reduction. Sum / Mean / Max /
//    Min / Prod share one template (kernel functor parameter).
//
//  Unsorted family — segment_ids[i] in any order in [0, num_segments).
//    One thread per (n, d) input cell. Thread reads seg = segment_ids[n]
//    and emits an atomic into output[seg, d] (atomicAdd for sum,
//    atomicMax / atomicMin for max / min). Mean is implemented as
//    {sum kernel + count kernel + post-divide kernel}.
//
// Conventions:
//
//   input         [N, D]  fp32 or fp64
//   segment_ids   [N]     i32, values in [0, num_segments)
//   output        [num_segments, D]  fp32 or fp64
//
// Out-of-range segment IDs (< 0 or >= num_segments) are SKIPPED — the
// kernel writes nothing into output for that input row. PyTorch /
// TF / JAX all treat this as undefined behavior; we silently drop.
//
// Status codes returned by the launchers mirror the indexing family:
//   0 success
//   1 misaligned operand
//   2 invalid problem
//   3 unsupported
//   4 workspace too small
//   5 internal kernel error

#ifndef BARACUDA_SEGMENT_CUH
#define BARACUDA_SEGMENT_CUH

#include <cstddef>
#include <cstdint>
#include <cfloat>
#include <cuda_runtime.h>

#include "baracuda_atomic.cuh"

namespace baracuda { namespace segment {

// =============================================================================
// Reduction helpers — per-dtype +∞ / -∞ initial values for max / min.
// =============================================================================

// These identity constants are called from BOTH device kernels and
// host-side launcher code (e.g. `launch_unsorted_segment_max` passes
// `seg_max_init<T>()` as the fill value to a device kernel via
// cudaMemcpyToSymbol-equivalent host wrapper). Marking them
// `__host__ __device__` so NVCC emits both versions — otherwise the
// host call sites silently link against an unresolved host stub and
// the process aborts at launch time.
template <typename T> __host__ __device__ inline T seg_max_init();
template <> __host__ __device__ inline float  seg_max_init<float>()  { return -FLT_MAX; }
template <> __host__ __device__ inline double seg_max_init<double>() { return -DBL_MAX; }

template <typename T> __host__ __device__ inline T seg_min_init();
template <> __host__ __device__ inline float  seg_min_init<float>()  { return  FLT_MAX; }
template <> __host__ __device__ inline double seg_min_init<double>() { return  DBL_MAX; }

template <typename T> __host__ __device__ inline T seg_zero();
template <> __host__ __device__ inline float  seg_zero<float>()  { return 0.0f; }
template <> __host__ __device__ inline double seg_zero<double>() { return 0.0; }

template <typename T> __host__ __device__ inline T seg_one();
template <> __host__ __device__ inline float  seg_one<float>()  { return 1.0f; }
template <> __host__ __device__ inline double seg_one<double>() { return 1.0; }

// Atomic-max / atomic-min for f32 / f64 (sign-aware bit-trick on the
// underlying integer representation). Native atomicMax / atomicMin only
// exist for integer types; for FP we go through atomicCAS on the bit
// pattern. The standard "sign trick" is:
//   - if val >= 0: signed-int atomicMax on the bits
//   - if val <  0: unsigned-int atomicMin on the bits
// (and dual for atomicMin → swap signed/unsigned + min/max).

__device__ inline void atomic_max_f32(float* addr, float val) {
    int* iaddr = reinterpret_cast<int*>(addr);
    int old = __float_as_int(*addr);
    int assumed;
    do {
        assumed = old;
        float cur = __int_as_float(assumed);
        if (val <= cur) return;
        int newbits = __float_as_int(val);
        old = atomicCAS(iaddr, assumed, newbits);
    } while (assumed != old);
}

__device__ inline void atomic_min_f32(float* addr, float val) {
    int* iaddr = reinterpret_cast<int*>(addr);
    int old = __float_as_int(*addr);
    int assumed;
    do {
        assumed = old;
        float cur = __int_as_float(assumed);
        if (val >= cur) return;
        int newbits = __float_as_int(val);
        old = atomicCAS(iaddr, assumed, newbits);
    } while (assumed != old);
}

__device__ inline void atomic_max_f64(double* addr, double val) {
    unsigned long long* uaddr = reinterpret_cast<unsigned long long*>(addr);
    unsigned long long old = __double_as_longlong(*addr);
    unsigned long long assumed;
    do {
        assumed = old;
        double cur = __longlong_as_double(assumed);
        if (val <= cur) return;
        unsigned long long newbits = __double_as_longlong(val);
        old = atomicCAS(uaddr, assumed, newbits);
    } while (assumed != old);
}

__device__ inline void atomic_min_f64(double* addr, double val) {
    unsigned long long* uaddr = reinterpret_cast<unsigned long long*>(addr);
    unsigned long long old = __double_as_longlong(*addr);
    unsigned long long assumed;
    do {
        assumed = old;
        double cur = __longlong_as_double(assumed);
        if (val >= cur) return;
        unsigned long long newbits = __double_as_longlong(val);
        old = atomicCAS(uaddr, assumed, newbits);
    } while (assumed != old);
}

// `seg_atomic_add` routes to the unified `baracuda::atomic::add<T>`
// helper from `baracuda_atomic.cuh` (Phase 11.3 / Fuel team feedback
// #6) — generic over every dtype with a native or CAS-emulated
// atomicAdd. f32 / f64 fall through to the native intrinsic; half /
// bf16 use a 32-bit `atomicCAS` loop. The kernels here today only
// instantiate f32 / f64 but the helper covers the future half/bf16
// extension automatically.
template <typename T>
__device__ __forceinline__ void seg_atomic_add(T* addr, T val) {
    baracuda::atomic::add<T>(addr, val);
}

template <typename T> __device__ inline void seg_atomic_max(T* addr, T val);
template <> __device__ inline void seg_atomic_max<float >(float*  a, float  v) { atomic_max_f32(a, v); }
template <> __device__ inline void seg_atomic_max<double>(double* a, double v) { atomic_max_f64(a, v); }

template <typename T> __device__ inline void seg_atomic_min(T* addr, T val);
template <> __device__ inline void seg_atomic_min<float >(float*  a, float  v) { atomic_min_f32(a, v); }
template <> __device__ inline void seg_atomic_min<double>(double* a, double v) { atomic_min_f64(a, v); }

// =============================================================================
// SORTED FAMILY — one thread per (segment_id, d) output cell.
// =============================================================================
//
// Sweeps the half-open range [lo, hi) of `segment_ids` covering this
// segment_id (found by binary search) and accumulates input[i, d] with
// the requested reduction. The total launch is
// `num_segments * D` threads.

enum SegReduceOp : int { SEG_SUM = 0, SEG_MEAN = 1, SEG_MAX = 2, SEG_MIN = 3, SEG_PROD = 4 };

// Lower bound on monotonically non-decreasing array — first index i
// with segment_ids[i] >= target. Returns N if none. Phase 11.5:
// templated on the segment-id dtype.
template <typename IndexT>
__device__ inline int32_t seg_lower_bound(
    const IndexT* __restrict__ seg_ids, int32_t n, int32_t target)
{
    int32_t lo = 0, hi = n;
    while (lo < hi) {
        int32_t mid = lo + ((hi - lo) >> 1);
        if ((int64_t)seg_ids[mid] < (int64_t)target) lo = mid + 1;
        else                                         hi = mid;
    }
    return lo;
}

template <typename T, int OP, typename IndexT>
__global__ void segment_sorted_kernel(
    const T*       __restrict__ input,        // [N, D]
    const IndexT*  __restrict__ segment_ids,  // [N]
    T*             __restrict__ output,       // [num_segments, D]
    int32_t        N,
    int32_t        D,
    int32_t        num_segments)
{
    int64_t tid  = (int64_t)blockIdx.x * (int64_t)blockDim.x + (int64_t)threadIdx.x;
    int64_t step = (int64_t)gridDim.x  * (int64_t)blockDim.x;
    int64_t total = (int64_t)num_segments * (int64_t)D;
    for (int64_t i = tid; i < total; i += step) {
        int32_t s = (int32_t)(i / (int64_t)D);
        int32_t d = (int32_t)(i - (int64_t)s * (int64_t)D);
        int32_t lo = seg_lower_bound(segment_ids, N, s);
        int32_t hi = seg_lower_bound(segment_ids, N, s + 1);
        int32_t count = hi - lo;

        T acc;
        if      (OP == SEG_SUM)  acc = seg_zero<T>();
        else if (OP == SEG_MEAN) acc = seg_zero<T>();
        else if (OP == SEG_MAX)  acc = seg_max_init<T>();
        else if (OP == SEG_MIN)  acc = seg_min_init<T>();
        else /* SEG_PROD */      acc = seg_one<T>();

        for (int32_t k = lo; k < hi; ++k) {
            T v = input[(int64_t)k * (int64_t)D + (int64_t)d];
            if      (OP == SEG_SUM)  acc = acc + v;
            else if (OP == SEG_MEAN) acc = acc + v;
            else if (OP == SEG_MAX)  acc = (v > acc) ? v : acc;
            else if (OP == SEG_MIN)  acc = (v < acc) ? v : acc;
            else /* SEG_PROD */      acc = acc * v;
        }

        T result;
        if (OP == SEG_MEAN) {
            // Empty segment → 0 (TF convention).
            result = (count > 0) ? (acc / (T)(double)count) : seg_zero<T>();
        } else if ((OP == SEG_MAX || OP == SEG_MIN) && count == 0) {
            // Empty max/min — leave the initial sentinel; PyTorch /
            // TF behavior here is implementation-defined. We emit 0
            // so the output isn't a sentinel that downstream
            // arithmetic can blow up on.
            result = seg_zero<T>();
        } else if (OP == SEG_PROD && count == 0) {
            result = seg_one<T>();
        } else {
            result = acc;
        }
        output[i] = result;
    }
}

template <typename T, int OP, typename IndexT>
__host__ inline int32_t launch_segment_sorted(
    const T* input, const IndexT* segment_ids, T* output,
    int32_t N, int32_t D, int32_t num_segments,
    cudaStream_t stream)
{
    if (N < 0 || D < 0 || num_segments < 0) return 2;
    if (N > 0 && (input == nullptr || segment_ids == nullptr)) return 2;
    if (num_segments > 0 && D > 0 && output == nullptr) return 2;
    int64_t total = (int64_t)num_segments * (int64_t)D;
    if (total == 0) return 0;
    constexpr int kBlock = 256;
    constexpr int64_t kMaxBlocks = 65535;
    int64_t blocks_i64 = (total + kBlock - 1) / kBlock;
    int blocks = static_cast<int>(blocks_i64 > kMaxBlocks ? kMaxBlocks : blocks_i64);
    if (blocks <= 0) blocks = 1;
    segment_sorted_kernel<T, OP, IndexT><<<blocks, kBlock, 0, stream>>>(
        input, segment_ids, output, N, D, num_segments);
    cudaError_t err = cudaGetLastError();
    return (err == cudaSuccess) ? 0 : 5;
}

// =============================================================================
// UNSORTED FAMILY — one thread per (n, d) input cell, atomic into output.
// =============================================================================
//
// Output buffer MUST be pre-initialized by the caller (or by the
// init-output kernel below) to the reduction identity:
//   sum → 0, max → -∞, min → +∞.
// The launcher does NOT call cudaMemset (we don't know the dtype
// memset value generically); the safe-layer plan zeroes / fills as
// part of its run().

template <typename T, typename IndexT>
__global__ void unsorted_segment_sum_kernel(
    const T*       __restrict__ input,
    const IndexT*  __restrict__ segment_ids,
    T*             __restrict__ output,
    int32_t        N,
    int32_t        D,
    int32_t        num_segments)
{
    int64_t total = (int64_t)N * (int64_t)D;
    int64_t tid  = (int64_t)blockIdx.x * (int64_t)blockDim.x + (int64_t)threadIdx.x;
    int64_t step = (int64_t)gridDim.x  * (int64_t)blockDim.x;
    for (int64_t i = tid; i < total; i += step) {
        int32_t n = (int32_t)(i / (int64_t)D);
        int32_t d = (int32_t)(i - (int64_t)n * (int64_t)D);
        int64_t s = (int64_t)segment_ids[n];
        if (s < 0 || s >= (int64_t)num_segments) continue;
        seg_atomic_add<T>(&output[s * (int64_t)D + (int64_t)d], input[i]);
    }
}

template <typename T, typename IndexT>
__global__ void unsorted_segment_max_kernel(
    const T*       __restrict__ input,
    const IndexT*  __restrict__ segment_ids,
    T*             __restrict__ output,
    int32_t        N,
    int32_t        D,
    int32_t        num_segments)
{
    int64_t total = (int64_t)N * (int64_t)D;
    int64_t tid  = (int64_t)blockIdx.x * (int64_t)blockDim.x + (int64_t)threadIdx.x;
    int64_t step = (int64_t)gridDim.x  * (int64_t)blockDim.x;
    for (int64_t i = tid; i < total; i += step) {
        int32_t n = (int32_t)(i / (int64_t)D);
        int32_t d = (int32_t)(i - (int64_t)n * (int64_t)D);
        int64_t s = (int64_t)segment_ids[n];
        if (s < 0 || s >= (int64_t)num_segments) continue;
        seg_atomic_max<T>(&output[s * (int64_t)D + (int64_t)d], input[i]);
    }
}

template <typename T, typename IndexT>
__global__ void unsorted_segment_min_kernel(
    const T*       __restrict__ input,
    const IndexT*  __restrict__ segment_ids,
    T*             __restrict__ output,
    int32_t        N,
    int32_t        D,
    int32_t        num_segments)
{
    int64_t total = (int64_t)N * (int64_t)D;
    int64_t tid  = (int64_t)blockIdx.x * (int64_t)blockDim.x + (int64_t)threadIdx.x;
    int64_t step = (int64_t)gridDim.x  * (int64_t)blockDim.x;
    for (int64_t i = tid; i < total; i += step) {
        int32_t n = (int32_t)(i / (int64_t)D);
        int32_t d = (int32_t)(i - (int64_t)n * (int64_t)D);
        int64_t s = (int64_t)segment_ids[n];
        if (s < 0 || s >= (int64_t)num_segments) continue;
        seg_atomic_min<T>(&output[s * (int64_t)D + (int64_t)d], input[i]);
    }
}

// Init-output kernel — fill output with a per-op identity value.
// Used by the unsorted launchers because we can't cudaMemset an arbitrary
// FP bit pattern (0.0 works for sum, but max needs -∞ and min needs +∞).
template <typename T>
__global__ void init_fill_kernel(T* __restrict__ out, int64_t n, T value) {
    int64_t tid  = (int64_t)blockIdx.x * (int64_t)blockDim.x + (int64_t)threadIdx.x;
    int64_t step = (int64_t)gridDim.x  * (int64_t)blockDim.x;
    for (int64_t i = tid; i < n; i += step) out[i] = value;
}

template <typename T>
__host__ inline void launch_init_fill(T* out, int64_t n, T value, cudaStream_t stream) {
    if (n <= 0) return;
    constexpr int kBlock = 256;
    constexpr int64_t kMaxBlocks = 65535;
    int64_t blocks_i64 = (n + kBlock - 1) / kBlock;
    int blocks = static_cast<int>(blocks_i64 > kMaxBlocks ? kMaxBlocks : blocks_i64);
    if (blocks <= 0) blocks = 1;
    init_fill_kernel<T><<<blocks, kBlock, 0, stream>>>(out, n, value);
}

template <typename T, typename IndexT>
__host__ inline int32_t launch_unsorted_segment_sum(
    const T* input, const IndexT* segment_ids, T* output,
    int32_t N, int32_t D, int32_t num_segments,
    cudaStream_t stream)
{
    if (N < 0 || D < 0 || num_segments < 0) return 2;
    int64_t out_total = (int64_t)num_segments * (int64_t)D;
    if (out_total > 0) {
        if (output == nullptr) return 2;
        // SUM identity is 0; IEEE-754 f32/f64 zero is bytewise zero so
        // cudaMemsetAsync is safe (no need for init_fill_kernel here).
        cudaError_t merr = cudaMemsetAsync(
            output, 0, (size_t)out_total * sizeof(T), stream);
        if (merr != cudaSuccess) return 5;
    }
    int64_t total = (int64_t)N * (int64_t)D;
    if (total == 0) return 0;
    if (input == nullptr || segment_ids == nullptr) return 2;
    constexpr int kBlock = 256;
    constexpr int64_t kMaxBlocks = 65535;
    int64_t blocks_i64 = (total + kBlock - 1) / kBlock;
    int blocks = static_cast<int>(blocks_i64 > kMaxBlocks ? kMaxBlocks : blocks_i64);
    if (blocks <= 0) blocks = 1;
    unsorted_segment_sum_kernel<T, IndexT><<<blocks, kBlock, 0, stream>>>(
        input, segment_ids, output, N, D, num_segments);
    cudaError_t err = cudaGetLastError();
    return (err == cudaSuccess) ? 0 : 5;
}

template <typename T, typename IndexT>
__host__ inline int32_t launch_unsorted_segment_max(
    const T* input, const IndexT* segment_ids, T* output,
    int32_t N, int32_t D, int32_t num_segments,
    cudaStream_t stream)
{
    if (N < 0 || D < 0 || num_segments < 0) return 2;
    int64_t out_total = (int64_t)num_segments * (int64_t)D;
    if (out_total > 0) {
        if (output == nullptr) return 2;
        launch_init_fill<T>(output, out_total, seg_max_init<T>(), stream);
    }
    int64_t total = (int64_t)N * (int64_t)D;
    if (total == 0) return 0;
    if (input == nullptr || segment_ids == nullptr) return 2;
    constexpr int kBlock = 256;
    constexpr int64_t kMaxBlocks = 65535;
    int64_t blocks_i64 = (total + kBlock - 1) / kBlock;
    int blocks = static_cast<int>(blocks_i64 > kMaxBlocks ? kMaxBlocks : blocks_i64);
    if (blocks <= 0) blocks = 1;
    unsorted_segment_max_kernel<T, IndexT><<<blocks, kBlock, 0, stream>>>(
        input, segment_ids, output, N, D, num_segments);
    cudaError_t err = cudaGetLastError();
    return (err == cudaSuccess) ? 0 : 5;
}

template <typename T, typename IndexT>
__host__ inline int32_t launch_unsorted_segment_min(
    const T* input, const IndexT* segment_ids, T* output,
    int32_t N, int32_t D, int32_t num_segments,
    cudaStream_t stream)
{
    if (N < 0 || D < 0 || num_segments < 0) return 2;
    int64_t out_total = (int64_t)num_segments * (int64_t)D;
    if (out_total > 0) {
        if (output == nullptr) return 2;
        launch_init_fill<T>(output, out_total, seg_min_init<T>(), stream);
    }
    int64_t total = (int64_t)N * (int64_t)D;
    if (total == 0) return 0;
    if (input == nullptr || segment_ids == nullptr) return 2;
    constexpr int kBlock = 256;
    constexpr int64_t kMaxBlocks = 65535;
    int64_t blocks_i64 = (total + kBlock - 1) / kBlock;
    int blocks = static_cast<int>(blocks_i64 > kMaxBlocks ? kMaxBlocks : blocks_i64);
    if (blocks <= 0) blocks = 1;
    unsorted_segment_min_kernel<T, IndexT><<<blocks, kBlock, 0, stream>>>(
        input, segment_ids, output, N, D, num_segments);
    cudaError_t err = cudaGetLastError();
    return (err == cudaSuccess) ? 0 : 5;
}

// =============================================================================
// MEAN HELPERS — per-segment integer counts + post-pass divide.
// =============================================================================
//
// Used by both sorted-mean BW and unsorted-mean (FW + BW). Sorted-mean
// FW computes the count inline from the binary-search range, so it
// doesn't need this helper.

template <typename IndexT>
__global__ void seg_count_kernel(
    const IndexT* __restrict__ segment_ids,
    int32_t*      __restrict__ counts,    // [num_segments] zero-init
    int32_t       N,
    int32_t       num_segments)
{
    int64_t tid  = (int64_t)blockIdx.x * (int64_t)blockDim.x + (int64_t)threadIdx.x;
    int64_t step = (int64_t)gridDim.x  * (int64_t)blockDim.x;
    for (int64_t i = tid; i < N; i += step) {
        int64_t s = (int64_t)segment_ids[i];
        if (s < 0 || s >= (int64_t)num_segments) continue;
        atomicAdd(&counts[s], 1);
    }
}

template <typename T>
__global__ void seg_mean_divide_kernel(
    T*             __restrict__ output,    // [num_segments, D]
    const int32_t* __restrict__ counts,    // [num_segments]
    int32_t        num_segments,
    int32_t        D)
{
    int64_t total = (int64_t)num_segments * (int64_t)D;
    int64_t tid  = (int64_t)blockIdx.x * (int64_t)blockDim.x + (int64_t)threadIdx.x;
    int64_t step = (int64_t)gridDim.x  * (int64_t)blockDim.x;
    for (int64_t i = tid; i < total; i += step) {
        int32_t s = (int32_t)(i / (int64_t)D);
        int32_t c = counts[s];
        if (c > 0) output[i] = output[i] / (T)(double)c;
    }
}

template <typename T, typename IndexT>
__host__ inline int32_t launch_unsorted_segment_mean(
    const T* input, const IndexT* segment_ids, T* output,
    int32_t* counts_workspace,            // [num_segments]
    int32_t N, int32_t D, int32_t num_segments,
    cudaStream_t stream)
{
    if (N < 0 || D < 0 || num_segments < 0) return 2;
    int32_t s = launch_unsorted_segment_sum<T, IndexT>(input, segment_ids, output,
                                                       N, D, num_segments, stream);
    if (s != 0) return s;
    if (num_segments == 0) return 0;
    if (counts_workspace == nullptr) return 4;
    cudaError_t err = cudaMemsetAsync(counts_workspace, 0,
                                      (size_t)num_segments * sizeof(int32_t), stream);
    if (err != cudaSuccess) return 5;
    if (N > 0) {
        constexpr int kBlock = 256;
        constexpr int64_t kMaxBlocks = 65535;
        int64_t blocks_i64 = ((int64_t)N + kBlock - 1) / kBlock;
        int blocks = static_cast<int>(blocks_i64 > kMaxBlocks ? kMaxBlocks : blocks_i64);
        if (blocks <= 0) blocks = 1;
        seg_count_kernel<IndexT><<<blocks, kBlock, 0, stream>>>(
            segment_ids, counts_workspace, N, num_segments);
    }
    int64_t out_total = (int64_t)num_segments * (int64_t)D;
    if (out_total > 0) {
        constexpr int kBlock = 256;
        constexpr int64_t kMaxBlocks = 65535;
        int64_t blocks_i64 = (out_total + kBlock - 1) / kBlock;
        int blocks = static_cast<int>(blocks_i64 > kMaxBlocks ? kMaxBlocks : blocks_i64);
        if (blocks <= 0) blocks = 1;
        seg_mean_divide_kernel<T><<<blocks, kBlock, 0, stream>>>(
            output, counts_workspace, num_segments, D);
    }
    err = cudaGetLastError();
    return (err == cudaSuccess) ? 0 : 5;
}

// =============================================================================
// BACKWARD KERNELS — sum / mean only.
// =============================================================================
//
// Sum BW: d_input[n, d] = d_output[seg[n], d]. Pure gather along seg.
//   - sorted and unsorted share the same kernel body (the seg-ids
//     array is the only operand difference, but the access pattern is
//     identical — one thread per (n, d) reads d_output[seg[n], d]).
//
// Mean BW: d_input[n, d] = d_output[seg[n], d] / count[seg[n]].
//   - sorted: need the count → run seg_count_kernel into a workspace
//     (counts can also be computed via binary search but the count
//     kernel is simpler and amortizes across rows).
//   - unsorted: same path.

template <typename T, typename IndexT>
__global__ void segment_sum_backward_kernel(
    const T*       __restrict__ d_output,     // [num_segments, D]
    const IndexT*  __restrict__ segment_ids,  // [N]
    T*             __restrict__ d_input,      // [N, D]
    int32_t        N,
    int32_t        D,
    int32_t        num_segments)
{
    int64_t total = (int64_t)N * (int64_t)D;
    int64_t tid  = (int64_t)blockIdx.x * (int64_t)blockDim.x + (int64_t)threadIdx.x;
    int64_t step = (int64_t)gridDim.x  * (int64_t)blockDim.x;
    for (int64_t i = tid; i < total; i += step) {
        int32_t n = (int32_t)(i / (int64_t)D);
        int32_t d = (int32_t)(i - (int64_t)n * (int64_t)D);
        int64_t s = (int64_t)segment_ids[n];
        T v = seg_zero<T>();
        if (s >= 0 && s < (int64_t)num_segments) {
            v = d_output[s * (int64_t)D + (int64_t)d];
        }
        d_input[i] = v;
    }
}

template <typename T, typename IndexT>
__host__ inline int32_t launch_segment_sum_backward(
    const T* d_output, const IndexT* segment_ids, T* d_input,
    int32_t N, int32_t D, int32_t num_segments,
    cudaStream_t stream)
{
    if (N < 0 || D < 0 || num_segments < 0) return 2;
    int64_t total = (int64_t)N * (int64_t)D;
    if (total == 0) return 0;
    if (d_output == nullptr || segment_ids == nullptr || d_input == nullptr) return 2;
    constexpr int kBlock = 256;
    constexpr int64_t kMaxBlocks = 65535;
    int64_t blocks_i64 = (total + kBlock - 1) / kBlock;
    int blocks = static_cast<int>(blocks_i64 > kMaxBlocks ? kMaxBlocks : blocks_i64);
    if (blocks <= 0) blocks = 1;
    segment_sum_backward_kernel<T, IndexT><<<blocks, kBlock, 0, stream>>>(
        d_output, segment_ids, d_input, N, D, num_segments);
    cudaError_t err = cudaGetLastError();
    return (err == cudaSuccess) ? 0 : 5;
}

template <typename T, typename IndexT>
__global__ void segment_mean_backward_kernel(
    const T*       __restrict__ d_output,     // [num_segments, D]
    const IndexT*  __restrict__ segment_ids,  // [N]
    const int32_t* __restrict__ counts,       // [num_segments]
    T*             __restrict__ d_input,      // [N, D]
    int32_t        N,
    int32_t        D,
    int32_t        num_segments)
{
    int64_t total = (int64_t)N * (int64_t)D;
    int64_t tid  = (int64_t)blockIdx.x * (int64_t)blockDim.x + (int64_t)threadIdx.x;
    int64_t step = (int64_t)gridDim.x  * (int64_t)blockDim.x;
    for (int64_t i = tid; i < total; i += step) {
        int32_t n = (int32_t)(i / (int64_t)D);
        int32_t d = (int32_t)(i - (int64_t)n * (int64_t)D);
        int64_t s = (int64_t)segment_ids[n];
        T v = seg_zero<T>();
        if (s >= 0 && s < (int64_t)num_segments) {
            int32_t c = counts[s];
            if (c > 0) {
                v = d_output[s * (int64_t)D + (int64_t)d] / (T)(double)c;
            }
        }
        d_input[i] = v;
    }
}

template <typename T, typename IndexT>
__host__ inline int32_t launch_segment_mean_backward(
    const T* d_output, const IndexT* segment_ids, T* d_input,
    int32_t* counts_workspace,                     // [num_segments]
    int32_t N, int32_t D, int32_t num_segments,
    cudaStream_t stream)
{
    if (N < 0 || D < 0 || num_segments < 0) return 2;
    int64_t total = (int64_t)N * (int64_t)D;
    if (total == 0) return 0;
    if (d_output == nullptr || segment_ids == nullptr || d_input == nullptr) return 2;
    if (num_segments > 0 && counts_workspace == nullptr) return 4;
    if (num_segments > 0) {
        cudaError_t err = cudaMemsetAsync(counts_workspace, 0,
                                          (size_t)num_segments * sizeof(int32_t), stream);
        if (err != cudaSuccess) return 5;
        if (N > 0) {
            constexpr int kBlock = 256;
            constexpr int64_t kMaxBlocks = 65535;
            int64_t blocks_i64 = ((int64_t)N + kBlock - 1) / kBlock;
            int blocks = static_cast<int>(blocks_i64 > kMaxBlocks ? kMaxBlocks : blocks_i64);
            if (blocks <= 0) blocks = 1;
            seg_count_kernel<IndexT><<<blocks, kBlock, 0, stream>>>(
                segment_ids, counts_workspace, N, num_segments);
        }
    }
    constexpr int kBlock = 256;
    constexpr int64_t kMaxBlocks = 65535;
    int64_t blocks_i64 = (total + kBlock - 1) / kBlock;
    int blocks = static_cast<int>(blocks_i64 > kMaxBlocks ? kMaxBlocks : blocks_i64);
    if (blocks <= 0) blocks = 1;
    segment_mean_backward_kernel<T, IndexT><<<blocks, kBlock, 0, stream>>>(
        d_output, segment_ids, counts_workspace, d_input, N, D, num_segments);
    cudaError_t err = cudaGetLastError();
    return (err == cudaSuccess) ? 0 : 5;
}

// =============================================================================
// Phase 25 — Max / Min / Prod BW (sorted + unsorted) + Unsorted Prod FW.
//
// Design choices:
//
// - Max / Min BW: argmax / argmin is **recomputed in the BW kernel** by
//   re-scanning the segment, rather than saving an index tensor from
//   the FW. This preserves the FW API source-compat (no new output
//   shape) — see [[max-min-bw-no-new-shape]] / [[phase16-complete]] for
//   the precedent (FractionalMaxPool used the same recompute pattern).
//   Tie-break = first occurrence (lowest k). PyTorch chooses the *last*
//   occurrence; we document the divergence in the Rust plan.
//
// - Prod BW: direct `d_input[k, d] = d_output[seg, d] * (prod / x[k, d])`.
//   Numerically dangerous when `x[k, d] == 0` (yields NaN or Inf).
//   Documented as a caller-responsibility in the Rust plan. To avoid
//   re-running the forward, the BW signature takes `output` (the prod
//   from FW) as an input.
//
// - Unsorted Prod FW: `atomicCAS` retry loop on the underlying 32 / 64-bit
//   slot. Slow but allowed per OP-MATRIX. Non-deterministic.
// =============================================================================

// Per-dtype atomic-mul via CAS. f32 uses int slot; f64 uses
// unsigned long long. Same shape as atomic_max_f32 etc.
__device__ inline void atomic_mul_f32(float* addr, float val) {
    int* iaddr = reinterpret_cast<int*>(addr);
    int old = __float_as_int(*addr);
    int assumed;
    do {
        assumed = old;
        float cur = __int_as_float(assumed);
        float newv = cur * val;
        int newbits = __float_as_int(newv);
        old = atomicCAS(iaddr, assumed, newbits);
    } while (assumed != old);
}

__device__ inline void atomic_mul_f64(double* addr, double val) {
    unsigned long long* uaddr = reinterpret_cast<unsigned long long*>(addr);
    unsigned long long old = __double_as_longlong(*addr);
    unsigned long long assumed;
    do {
        assumed = old;
        double cur = __longlong_as_double(assumed);
        double newv = cur * val;
        unsigned long long newbits = __double_as_longlong(newv);
        old = atomicCAS(uaddr, assumed, newbits);
    } while (assumed != old);
}

template <typename T> __device__ inline void seg_atomic_mul(T* addr, T val);
template <> __device__ inline void seg_atomic_mul<float >(float*  a, float  v) { atomic_mul_f32(a, v); }
template <> __device__ inline void seg_atomic_mul<double>(double* a, double v) { atomic_mul_f64(a, v); }

// =============================================================================
// SORTED Max / Min BW — one thread per (n, d) input cell.
//
// Per input cell `(n, d)`:
//   1. seg = segment_ids[n]
//   2. locate the segment's [lo, hi) range via binary search
//   3. scan [lo, hi) to find the (first) k where input[k, d] is max
//   4. if k == n, gradient flows: d_input[n, d] = d_output[seg, d]
//      else d_input[n, d] = 0
//
// This produces the "first-occurrence" tie-break (PyTorch uses last;
// documented divergence). Empty segments / out-of-range seg ids: the
// d_input cell is left as zero.
// =============================================================================

enum SegArgOp : int { SEG_ARG_MAX = 0, SEG_ARG_MIN = 1 };

template <typename T, int OP, typename IndexT>
__global__ void segment_arg_backward_kernel(
    const T*       __restrict__ d_output,     // [num_segments, D]
    const T*       __restrict__ input,        // [N, D]
    const IndexT*  __restrict__ segment_ids,  // [N]
    T*             __restrict__ d_input,      // [N, D]
    int32_t        N,
    int32_t        D,
    int32_t        num_segments)
{
    int64_t total = (int64_t)N * (int64_t)D;
    int64_t tid  = (int64_t)blockIdx.x * (int64_t)blockDim.x + (int64_t)threadIdx.x;
    int64_t step = (int64_t)gridDim.x  * (int64_t)blockDim.x;
    for (int64_t i = tid; i < total; i += step) {
        int32_t n = (int32_t)(i / (int64_t)D);
        int32_t d = (int32_t)(i - (int64_t)n * (int64_t)D);
        int64_t s = (int64_t)segment_ids[n];
        if (s < 0 || s >= (int64_t)num_segments) {
            d_input[i] = seg_zero<T>();
            continue;
        }
        // Find [lo, hi) for this segment via binary search.
        int32_t lo = seg_lower_bound<IndexT>(segment_ids, N, (int32_t)s);
        int32_t hi = seg_lower_bound<IndexT>(segment_ids, N, (int32_t)s + 1);
        // Find first-occurrence argmax (or argmin) at column d.
        int32_t arg = lo;
        T best = input[(int64_t)lo * (int64_t)D + (int64_t)d];
        for (int32_t k = lo + 1; k < hi; ++k) {
            T v = input[(int64_t)k * (int64_t)D + (int64_t)d];
            if (OP == SEG_ARG_MAX) {
                if (v > best) { best = v; arg = k; }
            } else {
                if (v < best) { best = v; arg = k; }
            }
        }
        if (n == arg) {
            d_input[i] = d_output[s * (int64_t)D + (int64_t)d];
        } else {
            d_input[i] = seg_zero<T>();
        }
    }
}

template <typename T, int OP, typename IndexT>
__host__ inline int32_t launch_segment_arg_backward(
    const T* d_output, const T* input, const IndexT* segment_ids, T* d_input,
    int32_t N, int32_t D, int32_t num_segments,
    cudaStream_t stream)
{
    if (N < 0 || D < 0 || num_segments < 0) return 2;
    int64_t total = (int64_t)N * (int64_t)D;
    if (total == 0) return 0;
    if (d_output == nullptr || input == nullptr || segment_ids == nullptr ||
        d_input == nullptr) return 2;
    constexpr int kBlock = 256;
    constexpr int64_t kMaxBlocks = 65535;
    int64_t blocks_i64 = (total + kBlock - 1) / kBlock;
    int blocks = static_cast<int>(blocks_i64 > kMaxBlocks ? kMaxBlocks : blocks_i64);
    if (blocks <= 0) blocks = 1;
    segment_arg_backward_kernel<T, OP, IndexT><<<blocks, kBlock, 0, stream>>>(
        d_output, input, segment_ids, d_input, N, D, num_segments);
    cudaError_t err = cudaGetLastError();
    return (err == cudaSuccess) ? 0 : 5;
}

// =============================================================================
// SORTED Prod BW — direct division.
//
// `d_input[k, d] = d_output[seg, d] * (output[seg, d] / x[k, d])`
//
// `output` is the FW `prod` result (caller must pass it in). When
// `x[k, d] == 0`, gradient is NaN or Inf — documented limitation.
// =============================================================================

template <typename T, typename IndexT>
__global__ void segment_prod_backward_kernel(
    const T*       __restrict__ d_output,     // [num_segments, D]
    const T*       __restrict__ input,        // [N, D]
    const T*       __restrict__ output,       // [num_segments, D] from FW
    const IndexT*  __restrict__ segment_ids,  // [N]
    T*             __restrict__ d_input,      // [N, D]
    int32_t        N,
    int32_t        D,
    int32_t        num_segments)
{
    int64_t total = (int64_t)N * (int64_t)D;
    int64_t tid  = (int64_t)blockIdx.x * (int64_t)blockDim.x + (int64_t)threadIdx.x;
    int64_t step = (int64_t)gridDim.x  * (int64_t)blockDim.x;
    for (int64_t i = tid; i < total; i += step) {
        int32_t n = (int32_t)(i / (int64_t)D);
        int32_t d = (int32_t)(i - (int64_t)n * (int64_t)D);
        int64_t s = (int64_t)segment_ids[n];
        if (s < 0 || s >= (int64_t)num_segments) {
            d_input[i] = seg_zero<T>();
            continue;
        }
        int64_t out_off = s * (int64_t)D + (int64_t)d;
        T x_nd = input[i];
        T prod = output[out_off];
        T dy = d_output[out_off];
        // Direct division — yields NaN/Inf if x_nd == 0.
        d_input[i] = dy * (prod / x_nd);
    }
}

template <typename T, typename IndexT>
__host__ inline int32_t launch_segment_prod_backward(
    const T* d_output, const T* input, const T* output,
    const IndexT* segment_ids, T* d_input,
    int32_t N, int32_t D, int32_t num_segments,
    cudaStream_t stream)
{
    if (N < 0 || D < 0 || num_segments < 0) return 2;
    int64_t total = (int64_t)N * (int64_t)D;
    if (total == 0) return 0;
    if (d_output == nullptr || input == nullptr || output == nullptr ||
        segment_ids == nullptr || d_input == nullptr) return 2;
    constexpr int kBlock = 256;
    constexpr int64_t kMaxBlocks = 65535;
    int64_t blocks_i64 = (total + kBlock - 1) / kBlock;
    int blocks = static_cast<int>(blocks_i64 > kMaxBlocks ? kMaxBlocks : blocks_i64);
    if (blocks <= 0) blocks = 1;
    segment_prod_backward_kernel<T, IndexT><<<blocks, kBlock, 0, stream>>>(
        d_output, input, output, segment_ids, d_input, N, D, num_segments);
    cudaError_t err = cudaGetLastError();
    return (err == cudaSuccess) ? 0 : 5;
}

// =============================================================================
// UNSORTED Max / Min BW — same recompute pattern but scans the full
// input array per (n, d) cell (segment ids may be in any order).
//
// One thread per (n, d). Thread reads its seg = segment_ids[n], then
// scans m ∈ [0, N) looking at input[m, d] where segment_ids[m] == seg
// to locate the first-occurrence argmax/argmin. If m == n, gradient
// flows in. O(N) work per cell — slow on big N — but unsorted seg ids
// don't admit a binary-search range, and the input layout means we
// can't shortcut.
// =============================================================================

template <typename T, int OP, typename IndexT>
__global__ void unsorted_segment_arg_backward_kernel(
    const T*       __restrict__ d_output,     // [num_segments, D]
    const T*       __restrict__ input,        // [N, D]
    const IndexT*  __restrict__ segment_ids,  // [N]
    T*             __restrict__ d_input,      // [N, D]
    int32_t        N,
    int32_t        D,
    int32_t        num_segments)
{
    int64_t total = (int64_t)N * (int64_t)D;
    int64_t tid  = (int64_t)blockIdx.x * (int64_t)blockDim.x + (int64_t)threadIdx.x;
    int64_t step = (int64_t)gridDim.x  * (int64_t)blockDim.x;
    for (int64_t i = tid; i < total; i += step) {
        int32_t n = (int32_t)(i / (int64_t)D);
        int32_t d = (int32_t)(i - (int64_t)n * (int64_t)D);
        int64_t s = (int64_t)segment_ids[n];
        if (s < 0 || s >= (int64_t)num_segments) {
            d_input[i] = seg_zero<T>();
            continue;
        }
        // Scan full input to find first-occurrence arg-extreme in seg s.
        int32_t arg = -1;
        T best;
        if (OP == SEG_ARG_MAX) best = seg_max_init<T>();
        else                   best = seg_min_init<T>();
        for (int32_t m = 0; m < N; ++m) {
            int64_t sm = (int64_t)segment_ids[m];
            if (sm != s) continue;
            T v = input[(int64_t)m * (int64_t)D + (int64_t)d];
            if (arg < 0) {
                best = v; arg = m;
                continue;
            }
            if (OP == SEG_ARG_MAX) {
                if (v > best) { best = v; arg = m; }
            } else {
                if (v < best) { best = v; arg = m; }
            }
        }
        if (n == arg) {
            d_input[i] = d_output[s * (int64_t)D + (int64_t)d];
        } else {
            d_input[i] = seg_zero<T>();
        }
    }
}

template <typename T, int OP, typename IndexT>
__host__ inline int32_t launch_unsorted_segment_arg_backward(
    const T* d_output, const T* input, const IndexT* segment_ids, T* d_input,
    int32_t N, int32_t D, int32_t num_segments,
    cudaStream_t stream)
{
    if (N < 0 || D < 0 || num_segments < 0) return 2;
    int64_t total = (int64_t)N * (int64_t)D;
    if (total == 0) return 0;
    if (d_output == nullptr || input == nullptr || segment_ids == nullptr ||
        d_input == nullptr) return 2;
    constexpr int kBlock = 256;
    constexpr int64_t kMaxBlocks = 65535;
    int64_t blocks_i64 = (total + kBlock - 1) / kBlock;
    int blocks = static_cast<int>(blocks_i64 > kMaxBlocks ? kMaxBlocks : blocks_i64);
    if (blocks <= 0) blocks = 1;
    unsorted_segment_arg_backward_kernel<T, OP, IndexT><<<blocks, kBlock, 0, stream>>>(
        d_output, input, segment_ids, d_input, N, D, num_segments);
    cudaError_t err = cudaGetLastError();
    return (err == cudaSuccess) ? 0 : 5;
}

// =============================================================================
// UNSORTED Prod FW — atomicCAS retry loop. Output pre-initialized to 1.
// =============================================================================

template <typename T, typename IndexT>
__global__ void unsorted_segment_prod_kernel(
    const T*       __restrict__ input,
    const IndexT*  __restrict__ segment_ids,
    T*             __restrict__ output,
    int32_t        N,
    int32_t        D,
    int32_t        num_segments)
{
    int64_t total = (int64_t)N * (int64_t)D;
    int64_t tid  = (int64_t)blockIdx.x * (int64_t)blockDim.x + (int64_t)threadIdx.x;
    int64_t step = (int64_t)gridDim.x  * (int64_t)blockDim.x;
    for (int64_t i = tid; i < total; i += step) {
        int32_t n = (int32_t)(i / (int64_t)D);
        int32_t d = (int32_t)(i - (int64_t)n * (int64_t)D);
        int64_t s = (int64_t)segment_ids[n];
        if (s < 0 || s >= (int64_t)num_segments) continue;
        seg_atomic_mul<T>(&output[s * (int64_t)D + (int64_t)d], input[i]);
    }
}

template <typename T, typename IndexT>
__host__ inline int32_t launch_unsorted_segment_prod(
    const T* input, const IndexT* segment_ids, T* output,
    int32_t N, int32_t D, int32_t num_segments,
    cudaStream_t stream)
{
    if (N < 0 || D < 0 || num_segments < 0) return 2;
    int64_t out_total = (int64_t)num_segments * (int64_t)D;
    if (out_total > 0) {
        if (output == nullptr) return 2;
        launch_init_fill<T>(output, out_total, seg_one<T>(), stream);
    }
    int64_t total = (int64_t)N * (int64_t)D;
    if (total == 0) return 0;
    if (input == nullptr || segment_ids == nullptr) return 2;
    constexpr int kBlock = 256;
    constexpr int64_t kMaxBlocks = 65535;
    int64_t blocks_i64 = (total + kBlock - 1) / kBlock;
    int blocks = static_cast<int>(blocks_i64 > kMaxBlocks ? kMaxBlocks : blocks_i64);
    if (blocks <= 0) blocks = 1;
    unsorted_segment_prod_kernel<T, IndexT><<<blocks, kBlock, 0, stream>>>(
        input, segment_ids, output, N, D, num_segments);
    cudaError_t err = cudaGetLastError();
    return (err == cudaSuccess) ? 0 : 5;
}

}} // namespace baracuda::segment

// =============================================================================
// INSTANTIATE macros — emit `extern "C"` launcher per (op, dtype) pair.
//
// Two FFI signatures used here:
//
//   1. Sorted FW (sum / mean / max / min / prod):
//        (N, D, num_segments, input, segment_ids, output, ws, ws_bytes, stream)
//      Workspace is unused (segment_sorted_kernel doesn't need it; mean
//      computes count inline). The ws params are present for FFI shape
//      uniformity with the rest of the family.
//
//   2. Unsorted FW sum / max / min:
//        (N, D, num_segments, input, segment_ids, output, ws, ws_bytes, stream)
//      Same shape; ws unused for sum / max / min.
//
//   3. Unsorted FW mean:
//        (N, D, num_segments, input, segment_ids, output, ws, ws_bytes, stream)
//      Requires a `num_segments * sizeof(i32)` workspace for the counts
//      buffer.
//
//   4. Sum BW (sorted or unsorted — same signature, same kernel):
//        (N, D, num_segments, d_output, segment_ids, d_input, ws, ws_bytes, stream)
//      No workspace needed.
//
//   5. Mean BW (sorted or unsorted — same signature, same kernel):
//        (N, D, num_segments, d_output, segment_ids, d_input, ws, ws_bytes, stream)
//      Requires a `num_segments * sizeof(i32)` workspace for the counts
//      buffer.
// =============================================================================

// Phase 11.5: `INDEX_T` parameter selects the segment-id dtype
// (`int32_t` or `int64_t`).
#define BARACUDA_KERNELS_SEGMENT_SORTED_INSTANTIATE(NAME, T, OP, INDEX_T)                         \
    extern "C" int32_t baracuda_kernels_##NAME##_run(                                             \
        int32_t N, int32_t D, int32_t num_segments,                                               \
        const void* input,                                                                        \
        const void* segment_ids,                                                                  \
        void* output,                                                                             \
        void* /*workspace*/, size_t /*workspace_bytes*/,                                          \
        void* stream_ptr)                                                                         \
    {                                                                                              \
        if (N < 0 || D < 0 || num_segments < 0) return 2;                                         \
        int64_t total = (int64_t)num_segments * (int64_t)D;                                       \
        if (total == 0) return 0;                                                                 \
        if (output == nullptr) return 2;                                                          \
        if (N > 0 && (input == nullptr || segment_ids == nullptr)) return 2;                      \
        cudaStream_t stream = static_cast<cudaStream_t>(stream_ptr);                              \
        return baracuda::segment::launch_segment_sorted<T, OP, INDEX_T>(                          \
            static_cast<const T*>(input),                                                         \
            static_cast<const INDEX_T*>(segment_ids),                                             \
            static_cast<T*>(output),                                                              \
            N, D, num_segments, stream);                                                          \
    }                                                                                              \
    extern "C" int32_t baracuda_kernels_##NAME##_can_implement(                                   \
        int32_t N, int32_t D, int32_t num_segments,                                               \
        const void* /*input*/,                                                                    \
        const void* /*segment_ids*/,                                                              \
        const void* /*output*/)                                                                   \
    {                                                                                              \
        if (N < 0 || D < 0 || num_segments < 0) return 2;                                         \
        return 0;                                                                                  \
    }

#define BARACUDA_KERNELS_UNSORTED_SEGMENT_SUM_INSTANTIATE(NAME, T, INDEX_T)                       \
    extern "C" int32_t baracuda_kernels_##NAME##_run(                                             \
        int32_t N, int32_t D, int32_t num_segments,                                               \
        const void* input,                                                                        \
        const void* segment_ids,                                                                  \
        void* output,                                                                             \
        void* /*workspace*/, size_t /*workspace_bytes*/,                                          \
        void* stream_ptr)                                                                         \
    {                                                                                              \
        cudaStream_t stream = static_cast<cudaStream_t>(stream_ptr);                              \
        return baracuda::segment::launch_unsorted_segment_sum<T, INDEX_T>(                        \
            static_cast<const T*>(input),                                                         \
            static_cast<const INDEX_T*>(segment_ids),                                             \
            static_cast<T*>(output),                                                              \
            N, D, num_segments, stream);                                                          \
    }                                                                                              \
    extern "C" int32_t baracuda_kernels_##NAME##_can_implement(                                   \
        int32_t N, int32_t D, int32_t num_segments,                                               \
        const void* /*input*/,                                                                    \
        const void* /*segment_ids*/,                                                              \
        const void* /*output*/)                                                                   \
    {                                                                                              \
        if (N < 0 || D < 0 || num_segments < 0) return 2;                                         \
        return 0;                                                                                  \
    }

#define BARACUDA_KERNELS_UNSORTED_SEGMENT_MAX_INSTANTIATE(NAME, T, INDEX_T)                       \
    extern "C" int32_t baracuda_kernels_##NAME##_run(                                             \
        int32_t N, int32_t D, int32_t num_segments,                                               \
        const void* input,                                                                        \
        const void* segment_ids,                                                                  \
        void* output,                                                                             \
        void* /*workspace*/, size_t /*workspace_bytes*/,                                          \
        void* stream_ptr)                                                                         \
    {                                                                                              \
        cudaStream_t stream = static_cast<cudaStream_t>(stream_ptr);                              \
        return baracuda::segment::launch_unsorted_segment_max<T, INDEX_T>(                        \
            static_cast<const T*>(input),                                                         \
            static_cast<const INDEX_T*>(segment_ids),                                             \
            static_cast<T*>(output),                                                              \
            N, D, num_segments, stream);                                                          \
    }                                                                                              \
    extern "C" int32_t baracuda_kernels_##NAME##_can_implement(                                   \
        int32_t N, int32_t D, int32_t num_segments,                                               \
        const void* /*input*/,                                                                    \
        const void* /*segment_ids*/,                                                              \
        const void* /*output*/)                                                                   \
    {                                                                                              \
        if (N < 0 || D < 0 || num_segments < 0) return 2;                                         \
        return 0;                                                                                  \
    }

#define BARACUDA_KERNELS_UNSORTED_SEGMENT_MIN_INSTANTIATE(NAME, T, INDEX_T)                       \
    extern "C" int32_t baracuda_kernels_##NAME##_run(                                             \
        int32_t N, int32_t D, int32_t num_segments,                                               \
        const void* input,                                                                        \
        const void* segment_ids,                                                                  \
        void* output,                                                                             \
        void* /*workspace*/, size_t /*workspace_bytes*/,                                          \
        void* stream_ptr)                                                                         \
    {                                                                                              \
        cudaStream_t stream = static_cast<cudaStream_t>(stream_ptr);                              \
        return baracuda::segment::launch_unsorted_segment_min<T, INDEX_T>(                        \
            static_cast<const T*>(input),                                                         \
            static_cast<const INDEX_T*>(segment_ids),                                             \
            static_cast<T*>(output),                                                              \
            N, D, num_segments, stream);                                                          \
    }                                                                                              \
    extern "C" int32_t baracuda_kernels_##NAME##_can_implement(                                   \
        int32_t N, int32_t D, int32_t num_segments,                                               \
        const void* /*input*/,                                                                    \
        const void* /*segment_ids*/,                                                              \
        const void* /*output*/)                                                                   \
    {                                                                                              \
        if (N < 0 || D < 0 || num_segments < 0) return 2;                                         \
        return 0;                                                                                  \
    }

#define BARACUDA_KERNELS_UNSORTED_SEGMENT_MEAN_INSTANTIATE(NAME, T, INDEX_T)                      \
    extern "C" int32_t baracuda_kernels_##NAME##_run(                                             \
        int32_t N, int32_t D, int32_t num_segments,                                               \
        const void* input,                                                                        \
        const void* segment_ids,                                                                  \
        void* output,                                                                             \
        void* workspace, size_t workspace_bytes,                                                  \
        void* stream_ptr)                                                                         \
    {                                                                                              \
        if (N < 0 || D < 0 || num_segments < 0) return 2;                                         \
        if (num_segments > 0 && workspace == nullptr) return 4;                                   \
        if ((size_t)num_segments * sizeof(int32_t) > workspace_bytes && num_segments > 0)         \
            return 4;                                                                              \
        cudaStream_t stream = static_cast<cudaStream_t>(stream_ptr);                              \
        return baracuda::segment::launch_unsorted_segment_mean<T, INDEX_T>(                       \
            static_cast<const T*>(input),                                                         \
            static_cast<const INDEX_T*>(segment_ids),                                             \
            static_cast<T*>(output),                                                              \
            static_cast<int32_t*>(workspace),                                                     \
            N, D, num_segments, stream);                                                          \
    }                                                                                              \
    extern "C" int32_t baracuda_kernels_##NAME##_can_implement(                                   \
        int32_t N, int32_t D, int32_t num_segments,                                               \
        const void* /*input*/,                                                                    \
        const void* /*segment_ids*/,                                                              \
        const void* /*output*/)                                                                   \
    {                                                                                              \
        if (N < 0 || D < 0 || num_segments < 0) return 2;                                         \
        return 0;                                                                                  \
    }

#define BARACUDA_KERNELS_SEGMENT_SUM_BACKWARD_INSTANTIATE(NAME, T, INDEX_T)                       \
    extern "C" int32_t baracuda_kernels_##NAME##_run(                                             \
        int32_t N, int32_t D, int32_t num_segments,                                               \
        const void* d_output,                                                                     \
        const void* segment_ids,                                                                  \
        void* d_input,                                                                            \
        void* /*workspace*/, size_t /*workspace_bytes*/,                                          \
        void* stream_ptr)                                                                         \
    {                                                                                              \
        cudaStream_t stream = static_cast<cudaStream_t>(stream_ptr);                              \
        return baracuda::segment::launch_segment_sum_backward<T, INDEX_T>(                        \
            static_cast<const T*>(d_output),                                                      \
            static_cast<const INDEX_T*>(segment_ids),                                             \
            static_cast<T*>(d_input),                                                             \
            N, D, num_segments, stream);                                                          \
    }                                                                                              \
    extern "C" int32_t baracuda_kernels_##NAME##_can_implement(                                   \
        int32_t N, int32_t D, int32_t num_segments,                                               \
        const void* /*d_output*/,                                                                 \
        const void* /*segment_ids*/,                                                              \
        const void* /*d_input*/)                                                                  \
    {                                                                                              \
        if (N < 0 || D < 0 || num_segments < 0) return 2;                                         \
        return 0;                                                                                  \
    }

#define BARACUDA_KERNELS_SEGMENT_MEAN_BACKWARD_INSTANTIATE(NAME, T, INDEX_T)                      \
    extern "C" int32_t baracuda_kernels_##NAME##_run(                                             \
        int32_t N, int32_t D, int32_t num_segments,                                               \
        const void* d_output,                                                                     \
        const void* segment_ids,                                                                  \
        void* d_input,                                                                            \
        void* workspace, size_t workspace_bytes,                                                  \
        void* stream_ptr)                                                                         \
    {                                                                                              \
        if (N < 0 || D < 0 || num_segments < 0) return 2;                                         \
        if (num_segments > 0 && workspace == nullptr) return 4;                                   \
        if ((size_t)num_segments * sizeof(int32_t) > workspace_bytes && num_segments > 0)         \
            return 4;                                                                              \
        cudaStream_t stream = static_cast<cudaStream_t>(stream_ptr);                              \
        return baracuda::segment::launch_segment_mean_backward<T, INDEX_T>(                       \
            static_cast<const T*>(d_output),                                                      \
            static_cast<const INDEX_T*>(segment_ids),                                             \
            static_cast<T*>(d_input),                                                             \
            static_cast<int32_t*>(workspace),                                                     \
            N, D, num_segments, stream);                                                          \
    }                                                                                              \
    extern "C" int32_t baracuda_kernels_##NAME##_can_implement(                                   \
        int32_t N, int32_t D, int32_t num_segments,                                               \
        const void* /*d_output*/,                                                                 \
        const void* /*segment_ids*/,                                                              \
        const void* /*d_input*/)                                                                  \
    {                                                                                              \
        if (N < 0 || D < 0 || num_segments < 0) return 2;                                         \
        return 0;                                                                                  \
    }

// =============================================================================
// Phase 25 INSTANTIATE macros.
//
// 1. Segment Max / Min BW (sorted + unsorted) — signature mirrors the
//    sum/mean BW family but with an extra `input` pointer (re-scan source).
//
//    (N, D, num_segments, d_output, input, segment_ids, d_input,
//     ws, ws_bytes, stream)
//
// 2. Segment Prod BW (sorted + unsorted) — signature mirrors the BW
//    family plus `input` and `output` (saved FW prod).
//
//    (N, D, num_segments, d_output, input, output, segment_ids, d_input,
//     ws, ws_bytes, stream)
//
// 3. Unsorted Segment Prod FW — same shape as the other unsorted-FW
//    INSTANTIATEs (the launcher pre-fills output with 1).
//
//    (N, D, num_segments, input, segment_ids, output, ws, ws_bytes, stream)
// =============================================================================

#define BARACUDA_KERNELS_SEGMENT_ARG_BACKWARD_INSTANTIATE(NAME, T, OP, INDEX_T)                   \
    extern "C" int32_t baracuda_kernels_##NAME##_run(                                             \
        int32_t N, int32_t D, int32_t num_segments,                                               \
        const void* d_output,                                                                     \
        const void* input,                                                                        \
        const void* segment_ids,                                                                  \
        void* d_input,                                                                            \
        void* /*workspace*/, size_t /*workspace_bytes*/,                                          \
        void* stream_ptr)                                                                         \
    {                                                                                              \
        cudaStream_t stream = static_cast<cudaStream_t>(stream_ptr);                              \
        return baracuda::segment::launch_segment_arg_backward<T, OP, INDEX_T>(                    \
            static_cast<const T*>(d_output),                                                      \
            static_cast<const T*>(input),                                                         \
            static_cast<const INDEX_T*>(segment_ids),                                             \
            static_cast<T*>(d_input),                                                             \
            N, D, num_segments, stream);                                                          \
    }                                                                                              \
    extern "C" int32_t baracuda_kernels_##NAME##_can_implement(                                   \
        int32_t N, int32_t D, int32_t num_segments,                                               \
        const void* /*d_output*/,                                                                 \
        const void* /*input*/,                                                                    \
        const void* /*segment_ids*/,                                                              \
        const void* /*d_input*/)                                                                  \
    {                                                                                              \
        if (N < 0 || D < 0 || num_segments < 0) return 2;                                         \
        return 0;                                                                                  \
    }

#define BARACUDA_KERNELS_UNSORTED_SEGMENT_ARG_BACKWARD_INSTANTIATE(NAME, T, OP, INDEX_T)          \
    extern "C" int32_t baracuda_kernels_##NAME##_run(                                             \
        int32_t N, int32_t D, int32_t num_segments,                                               \
        const void* d_output,                                                                     \
        const void* input,                                                                        \
        const void* segment_ids,                                                                  \
        void* d_input,                                                                            \
        void* /*workspace*/, size_t /*workspace_bytes*/,                                          \
        void* stream_ptr)                                                                         \
    {                                                                                              \
        cudaStream_t stream = static_cast<cudaStream_t>(stream_ptr);                              \
        return baracuda::segment::launch_unsorted_segment_arg_backward<T, OP, INDEX_T>(           \
            static_cast<const T*>(d_output),                                                      \
            static_cast<const T*>(input),                                                         \
            static_cast<const INDEX_T*>(segment_ids),                                             \
            static_cast<T*>(d_input),                                                             \
            N, D, num_segments, stream);                                                          \
    }                                                                                              \
    extern "C" int32_t baracuda_kernels_##NAME##_can_implement(                                   \
        int32_t N, int32_t D, int32_t num_segments,                                               \
        const void* /*d_output*/,                                                                 \
        const void* /*input*/,                                                                    \
        const void* /*segment_ids*/,                                                              \
        const void* /*d_input*/)                                                                  \
    {                                                                                              \
        if (N < 0 || D < 0 || num_segments < 0) return 2;                                         \
        return 0;                                                                                  \
    }

#define BARACUDA_KERNELS_SEGMENT_PROD_BACKWARD_INSTANTIATE(NAME, T, INDEX_T)                      \
    extern "C" int32_t baracuda_kernels_##NAME##_run(                                             \
        int32_t N, int32_t D, int32_t num_segments,                                               \
        const void* d_output,                                                                     \
        const void* input,                                                                        \
        const void* output,                                                                       \
        const void* segment_ids,                                                                  \
        void* d_input,                                                                            \
        void* /*workspace*/, size_t /*workspace_bytes*/,                                          \
        void* stream_ptr)                                                                         \
    {                                                                                              \
        cudaStream_t stream = static_cast<cudaStream_t>(stream_ptr);                              \
        return baracuda::segment::launch_segment_prod_backward<T, INDEX_T>(                       \
            static_cast<const T*>(d_output),                                                      \
            static_cast<const T*>(input),                                                         \
            static_cast<const T*>(output),                                                        \
            static_cast<const INDEX_T*>(segment_ids),                                             \
            static_cast<T*>(d_input),                                                             \
            N, D, num_segments, stream);                                                          \
    }                                                                                              \
    extern "C" int32_t baracuda_kernels_##NAME##_can_implement(                                   \
        int32_t N, int32_t D, int32_t num_segments,                                               \
        const void* /*d_output*/,                                                                 \
        const void* /*input*/,                                                                    \
        const void* /*output*/,                                                                   \
        const void* /*segment_ids*/,                                                              \
        const void* /*d_input*/)                                                                  \
    {                                                                                              \
        if (N < 0 || D < 0 || num_segments < 0) return 2;                                         \
        return 0;                                                                                  \
    }

#define BARACUDA_KERNELS_UNSORTED_SEGMENT_PROD_INSTANTIATE(NAME, T, INDEX_T)                      \
    extern "C" int32_t baracuda_kernels_##NAME##_run(                                             \
        int32_t N, int32_t D, int32_t num_segments,                                               \
        const void* input,                                                                        \
        const void* segment_ids,                                                                  \
        void* output,                                                                             \
        void* /*workspace*/, size_t /*workspace_bytes*/,                                          \
        void* stream_ptr)                                                                         \
    {                                                                                              \
        cudaStream_t stream = static_cast<cudaStream_t>(stream_ptr);                              \
        return baracuda::segment::launch_unsorted_segment_prod<T, INDEX_T>(                       \
            static_cast<const T*>(input),                                                         \
            static_cast<const INDEX_T*>(segment_ids),                                             \
            static_cast<T*>(output),                                                              \
            N, D, num_segments, stream);                                                          \
    }                                                                                              \
    extern "C" int32_t baracuda_kernels_##NAME##_can_implement(                                   \
        int32_t N, int32_t D, int32_t num_segments,                                               \
        const void* /*input*/,                                                                    \
        const void* /*segment_ids*/,                                                              \
        const void* /*output*/)                                                                   \
    {                                                                                              \
        if (N < 0 || D < 0 || num_segments < 0) return 2;                                         \
        return 0;                                                                                  \
    }

#endif // BARACUDA_SEGMENT_CUH