treeboost 0.1.0

High-performance Gradient Boosted Decision Tree engine for large-scale tabular data
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
//! CUDA partition kernel for level-wise tree building.
//!
//! Optimized with:
//! - Buffer caching (bins stay on GPU)
//! - True batched partitioning (all nodes in one kernel)
//! - GPU-resident mode: indices stay on GPU between levels
//! - Double-buffering for zero-copy level transitions

use super::device::CudaDevice;
use super::kernels::CacheKey;
use cudarc::driver::{CudaFunction, CudaModule, CudaSlice, LaunchConfig, PushKernelArg};
use cudarc::nvrtc::compile_ptx;
use std::sync::Arc;

/// CUDA source for partition kernels.
/// Includes both single-node and batched versions.
const PARTITION_KERNEL_SOURCE: &str = r#"
// Single node partition with atomic counters
extern "C" __global__ void partition_atomic(
    const unsigned char* __restrict__ bins,
    const unsigned int* __restrict__ input_indices,
    unsigned int* __restrict__ left_indices,
    unsigned int* __restrict__ right_indices,
    unsigned int* __restrict__ counters,
    unsigned int num_indices,
    unsigned int num_features,
    unsigned int split_feature,
    unsigned int split_threshold
) {
    unsigned int idx = blockIdx.x * blockDim.x + threadIdx.x;
    if (idx >= num_indices) return;

    unsigned int row = input_indices[idx];
    unsigned int bin = bins[row * num_features + split_feature];

    if (bin <= split_threshold) {
        unsigned int pos = atomicAdd(&counters[0], 1);
        left_indices[pos] = row;
    } else {
        unsigned int pos = atomicAdd(&counters[1], 1);
        right_indices[pos] = row;
    }
}

// Batched partition: process multiple nodes in parallel
// Grid: (num_nodes, row_tiles)
// Each block handles one node's partition for a tile of rows
extern "C" __global__ void partition_batched(
    const unsigned char* __restrict__ bins,
    const unsigned int* __restrict__ input_indices,      // Concatenated indices for all nodes
    unsigned int* __restrict__ output_indices,           // Interleaved left/right output
    unsigned int* __restrict__ counters,                 // [node_idx * 2 + 0/1] for left/right
    const unsigned int* __restrict__ node_starts,        // Start offset for each node
    const unsigned int* __restrict__ node_counts,        // Row count for each node
    const unsigned int* __restrict__ split_features,     // Split feature for each node
    const unsigned int* __restrict__ split_thresholds,   // Split threshold for each node
    const unsigned int* __restrict__ output_starts,      // Output start offset for each node
    unsigned int num_features,
    unsigned int max_node_rows                           // Max output size per node
) {
    unsigned int node_idx = blockIdx.x;
    unsigned int tile_idx = blockIdx.y;
    unsigned int rows_per_tile = blockDim.x;

    unsigned int node_start = node_starts[node_idx];
    unsigned int node_count = node_counts[node_idx];
    unsigned int split_feature = split_features[node_idx];
    unsigned int split_threshold = split_thresholds[node_idx];
    unsigned int output_start = output_starts[node_idx];

    unsigned int local_row = tile_idx * rows_per_tile + threadIdx.x;
    if (local_row >= node_count) return;

    unsigned int row = input_indices[node_start + local_row];
    unsigned int bin = bins[row * num_features + split_feature];

    // Left indices at output_start, right indices at output_start + max_node_rows
    if (bin <= split_threshold) {
        unsigned int pos = atomicAdd(&counters[node_idx * 2], 1);
        output_indices[output_start + pos] = row;
    } else {
        unsigned int pos = atomicAdd(&counters[node_idx * 2 + 1], 1);
        output_indices[output_start + max_node_rows + pos] = row;
    }
}

extern "C" __global__ void zero_counters_n(unsigned int* counters, unsigned int n) {
    unsigned int idx = blockIdx.x * blockDim.x + threadIdx.x;
    if (idx < n) {
        counters[idx] = 0;
    }
}

// GPU-resident partition: reads from input buffer, writes to output buffer
// Output layout: contiguous left/right indices for each node
// Each node's results are written starting at output_starts[node_idx]
// Left indices first, then right indices after left_counts[node_idx]
extern "C" __global__ void partition_gpu_resident(
    const unsigned char* __restrict__ bins,
    const unsigned int* __restrict__ input_indices,      // Input GPU buffer
    unsigned int* __restrict__ output_indices,           // Output GPU buffer
    unsigned int* __restrict__ counters,                 // [node_idx * 2 + 0/1] for left/right count
    const unsigned int* __restrict__ node_input_starts,  // Where to read from input for each node
    const unsigned int* __restrict__ node_counts,        // Number of rows for each node
    const unsigned int* __restrict__ split_features,     // Split feature for each node
    const unsigned int* __restrict__ split_thresholds,   // Split threshold for each node
    const unsigned int* __restrict__ node_output_starts, // Where to write in output for each node
    unsigned int num_features,
    unsigned int max_node_rows                           // Max rows per node (for right offset)
) {
    unsigned int node_idx = blockIdx.x;
    unsigned int tile_idx = blockIdx.y;
    unsigned int rows_per_tile = blockDim.x;

    unsigned int input_start = node_input_starts[node_idx];
    unsigned int node_count = node_counts[node_idx];
    unsigned int split_feature = split_features[node_idx];
    unsigned int split_threshold = split_thresholds[node_idx];
    unsigned int output_start = node_output_starts[node_idx];

    unsigned int local_row = tile_idx * rows_per_tile + threadIdx.x;
    if (local_row >= node_count) return;

    unsigned int row = input_indices[input_start + local_row];
    unsigned int bin = bins[row * num_features + split_feature];

    // Write left at output_start, right at output_start + max_node_rows
    if (bin <= split_threshold) {
        unsigned int pos = atomicAdd(&counters[node_idx * 2], 1);
        output_indices[output_start + pos] = row;
    } else {
        unsigned int pos = atomicAdd(&counters[node_idx * 2 + 1], 1);
        output_indices[output_start + max_node_rows + pos] = row;
    }
}

// Compact: rearrange output buffer so left/right are contiguous for next level
// Input: [node0_left(max), node0_right(max), node1_left(max), node1_right(max), ...]
// Output: [node0_left(actual), node0_right(actual), node1_left(actual), node1_right(actual), ...]
extern "C" __global__ void compact_partitions(
    const unsigned int* __restrict__ input,        // Padded output from partition
    unsigned int* __restrict__ output,             // Compacted for next level
    const unsigned int* __restrict__ input_starts, // Original output_starts from partition
    const unsigned int* __restrict__ left_counts,  // Actual left counts
    const unsigned int* __restrict__ right_counts, // Actual right counts
    const unsigned int* __restrict__ output_starts,// Where to write compacted results
    unsigned int max_node_rows,
    unsigned int num_nodes
) {
    // Each block handles one node's compaction
    unsigned int node_idx = blockIdx.x;
    if (node_idx >= num_nodes) return;

    unsigned int in_start = input_starts[node_idx];
    unsigned int left_count = left_counts[node_idx];
    unsigned int right_count = right_counts[node_idx];
    unsigned int out_start = output_starts[node_idx];

    // Copy left indices
    for (unsigned int i = threadIdx.x; i < left_count; i += blockDim.x) {
        output[out_start + i] = input[in_start + i];
    }

    // Copy right indices (right is at in_start + max_node_rows in input)
    for (unsigned int i = threadIdx.x; i < right_count; i += blockDim.x) {
        output[out_start + left_count + i] = input[in_start + max_node_rows + i];
    }
}

// Fused partition + compact: partition and write directly to contiguous output
// Uses two global counters per node: running offset for left and right
// Output layout: [node0_left, node0_right, node1_left, node1_right, ...]
// The output_offsets array stores the starting position for each node's combined output
extern "C" __global__ void partition_fused(
    const unsigned char* __restrict__ bins,
    const unsigned int* __restrict__ input_indices,
    unsigned int* __restrict__ output_indices,
    unsigned int* __restrict__ counters,           // [node_idx * 2 + 0/1] for left/right local count
    const unsigned int* __restrict__ node_input_starts,
    const unsigned int* __restrict__ node_counts,
    const unsigned int* __restrict__ split_features,
    const unsigned int* __restrict__ split_thresholds,
    const unsigned int* __restrict__ output_offsets,  // Where each node's output starts
    const unsigned int* __restrict__ left_capacities, // Max left count for each node (for right offset)
    unsigned int num_features
) {
    unsigned int node_idx = blockIdx.x;
    unsigned int tile_idx = blockIdx.y;
    unsigned int rows_per_tile = blockDim.x;

    unsigned int input_start = node_input_starts[node_idx];
    unsigned int node_count = node_counts[node_idx];
    unsigned int split_feature = split_features[node_idx];
    unsigned int split_threshold = split_thresholds[node_idx];
    unsigned int output_base = output_offsets[node_idx];
    unsigned int left_capacity = left_capacities[node_idx];

    unsigned int local_row = tile_idx * rows_per_tile + threadIdx.x;
    if (local_row >= node_count) return;

    unsigned int row = input_indices[input_start + local_row];
    unsigned int bin = bins[row * num_features + split_feature];

    // Write directly to contiguous output
    // Left at output_base, right at output_base + left_capacity
    if (bin <= split_threshold) {
        unsigned int pos = atomicAdd(&counters[node_idx * 2], 1);
        output_indices[output_base + pos] = row;
    } else {
        unsigned int pos = atomicAdd(&counters[node_idx * 2 + 1], 1);
        output_indices[output_base + left_capacity + pos] = row;
    }
}
"#;

/// Split information for one node.
#[derive(Debug, Clone, Copy)]
pub struct NodeSplit {
    pub input_start: u32,
    pub input_count: u32,
    pub split_feature: u32,
    pub split_threshold: u32,
}

/// Result of a partition operation.
#[derive(Debug, Clone)]
pub struct PartitionResult {
    pub left_indices: Vec<u32>,
    pub right_indices: Vec<u32>,
    pub left_count: u32,
    pub right_count: u32,
}

/// Result of GPU-resident partition (only counts, indices stay on GPU).
#[derive(Debug, Clone, Copy)]
pub struct GpuPartitionResult {
    pub left_count: u32,
    pub right_count: u32,
    pub output_start: u32, // Start offset in output buffer for this node's results
}

/// CUDA partition kernel executor with buffer caching.
pub struct PartitionKernel {
    device: Arc<CudaDevice>,
    module: Option<Arc<CudaModule>>,
    partition_fn: Option<CudaFunction>,
    partition_batched_fn: Option<CudaFunction>,
    partition_gpu_resident_fn: Option<CudaFunction>,
    partition_fused_fn: Option<CudaFunction>,
    compact_partitions_fn: Option<CudaFunction>,
    zero_counters_fn: Option<CudaFunction>,

    // Cached GPU buffers
    cached_bins: Option<CudaSlice<u8>>,
    cached_bins_key: Option<CacheKey>,

    // Reusable output buffers (sized for max expected use)
    cached_left: Option<CudaSlice<u32>>,
    cached_right: Option<CudaSlice<u32>>,
    cached_counters: Option<CudaSlice<u32>>,
    cached_output_size: usize,

    // Batched partition buffers
    cached_batch_output: Option<CudaSlice<u32>>,
    cached_batch_counters: Option<CudaSlice<u32>>,
    cached_batch_size: usize,

    // Cached metadata buffers for fused partition (avoid per-level allocations)
    cached_node_input_starts: Option<CudaSlice<u32>>,
    cached_node_counts: Option<CudaSlice<u32>>,
    cached_split_features: Option<CudaSlice<u32>>,
    cached_split_thresholds: Option<CudaSlice<u32>>,
    cached_output_offsets: Option<CudaSlice<u32>>,
    cached_left_capacities: Option<CudaSlice<u32>>,
    cached_metadata_capacity: usize,
}

impl PartitionKernel {
    /// Create a new partition kernel.
    pub fn new(device: Arc<CudaDevice>) -> Self {
        Self {
            device,
            module: None,
            partition_fn: None,
            partition_batched_fn: None,
            partition_gpu_resident_fn: None,
            partition_fused_fn: None,
            compact_partitions_fn: None,
            zero_counters_fn: None,
            cached_bins: None,
            cached_bins_key: None,
            cached_left: None,
            cached_right: None,
            cached_counters: None,
            cached_output_size: 0,
            cached_batch_output: None,
            cached_batch_counters: None,
            cached_batch_size: 0,
            cached_node_input_starts: None,
            cached_node_counts: None,
            cached_split_features: None,
            cached_split_thresholds: None,
            cached_output_offsets: None,
            cached_left_capacities: None,
            cached_metadata_capacity: 0,
        }
    }

    /// Ensure the kernel is compiled and loaded.
    fn ensure_initialized(&mut self) {
        if self.module.is_some() {
            return;
        }

        let ptx = compile_ptx(PARTITION_KERNEL_SOURCE).expect("Failed to compile partition kernel");
        let module = self.device.load_module(ptx);

        self.partition_fn = Some(CudaDevice::load_function(&module, "partition_atomic"));
        self.partition_batched_fn = Some(CudaDevice::load_function(&module, "partition_batched"));
        self.partition_gpu_resident_fn =
            Some(CudaDevice::load_function(&module, "partition_gpu_resident"));
        self.partition_fused_fn = Some(CudaDevice::load_function(&module, "partition_fused"));
        self.compact_partitions_fn = Some(CudaDevice::load_function(&module, "compact_partitions"));
        self.zero_counters_fn = Some(CudaDevice::load_function(&module, "zero_counters_n"));
        self.module = Some(module);
    }

    /// Ensure metadata buffers are large enough for num_nodes.
    fn ensure_metadata_buffers(&mut self, num_nodes: usize) {
        if self.cached_metadata_capacity >= num_nodes && self.cached_node_input_starts.is_some() {
            return;
        }
        // Allocate with some headroom
        let capacity = (num_nodes * 2).max(64);
        self.cached_node_input_starts = Some(self.device.alloc_zeros(capacity));
        self.cached_node_counts = Some(self.device.alloc_zeros(capacity));
        self.cached_split_features = Some(self.device.alloc_zeros(capacity));
        self.cached_split_thresholds = Some(self.device.alloc_zeros(capacity));
        self.cached_output_offsets = Some(self.device.alloc_zeros(capacity));
        self.cached_left_capacities = Some(self.device.alloc_zeros(capacity));
        self.cached_metadata_capacity = capacity;
    }

    /// Get the device reference.
    pub fn device(&self) -> &Arc<CudaDevice> {
        &self.device
    }

    /// Ensure bins are cached on GPU.
    pub fn ensure_bins_cached(&mut self, bins: &[u8]) {
        let bins_key = CacheKey::from_slice(bins);
        if self.cached_bins_key != Some(bins_key) || self.cached_bins.is_none() {
            self.cached_bins = Some(self.device.htod_copy(bins));
            self.cached_bins_key = Some(bins_key);
        }
    }

    /// Get cached bins buffer.
    pub fn cached_bins(&self) -> Option<&CudaSlice<u8>> {
        self.cached_bins.as_ref()
    }

    /// Ensure output buffers are allocated
    fn ensure_output_buffers(&mut self, size: usize) {
        if self.cached_output_size < size || self.cached_left.is_none() {
            // Allocate with some slack
            let alloc_size = (size * 3 / 2).max(size);
            self.cached_left = Some(self.device.alloc_zeros(alloc_size));
            self.cached_right = Some(self.device.alloc_zeros(alloc_size));
            self.cached_counters = Some(self.device.alloc_zeros(2));
            self.cached_output_size = alloc_size;
        }
    }

    /// Partition rows based on a split condition.
    pub fn partition(
        &mut self,
        bins: &[u8],
        input_indices: &[u32],
        split_feature: u32,
        split_threshold: u32,
        num_features: usize,
    ) -> PartitionResult {
        self.ensure_initialized();

        let num_indices = input_indices.len();
        if num_indices == 0 {
            return PartitionResult {
                left_indices: Vec::new(),
                right_indices: Vec::new(),
                left_count: 0,
                right_count: 0,
            };
        }

        // Cache bins and allocate output buffers
        self.ensure_bins_cached(bins);
        self.ensure_output_buffers(num_indices);

        // Upload input indices
        let d_input = self.device.htod_copy(input_indices);

        let stream = self.device.stream();

        // Zero counters
        let zero_config = LaunchConfig {
            block_dim: (2, 1, 1),
            grid_dim: (1, 1, 1),
            shared_mem_bytes: 0,
        };
        unsafe {
            let d_counters = self.cached_counters.as_mut().unwrap();
            stream
                .launch_builder(self.zero_counters_fn.as_ref().unwrap())
                .arg(d_counters)
                .arg(&2u32)
                .launch(zero_config)
                .expect("Failed to launch zero_counters kernel");
        }

        // Launch partition kernel
        let config = LaunchConfig {
            block_dim: (256, 1, 1),
            grid_dim: (((num_indices + 255) / 256) as u32, 1, 1),
            shared_mem_bytes: 0,
        };
        unsafe {
            let d_bins = self.cached_bins.as_ref().unwrap();
            let d_left = self.cached_left.as_mut().unwrap();
            let d_right = self.cached_right.as_mut().unwrap();
            let d_counters = self.cached_counters.as_mut().unwrap();

            stream
                .launch_builder(self.partition_fn.as_ref().unwrap())
                .arg(d_bins)
                .arg(&d_input)
                .arg(d_left)
                .arg(d_right)
                .arg(d_counters)
                .arg(&(num_indices as u32))
                .arg(&(num_features as u32))
                .arg(&split_feature)
                .arg(&split_threshold)
                .launch(config)
                .expect("Failed to launch partition kernel");
        }

        self.device.synchronize();

        // Read back counters
        let counters = self
            .device
            .dtoh_copy(self.cached_counters.as_ref().unwrap());
        let left_count = counters[0];
        let right_count = counters[1];

        // Read back only the needed portions
        let all_left = self.device.dtoh_copy(self.cached_left.as_ref().unwrap());
        let all_right = self.device.dtoh_copy(self.cached_right.as_ref().unwrap());

        PartitionResult {
            left_indices: all_left[..left_count as usize].to_vec(),
            right_indices: all_right[..right_count as usize].to_vec(),
            left_count,
            right_count,
        }
    }

    /// Partition multiple nodes in a single kernel launch.
    pub fn partition_batched(
        &mut self,
        bins: &[u8],
        input_indices: &[u32],
        node_splits: &[NodeSplit],
        num_features: usize,
    ) -> Vec<PartitionResult> {
        self.ensure_initialized();

        if node_splits.is_empty() {
            return Vec::new();
        }

        // For single node, use simpler path
        if node_splits.len() == 1 {
            let split = &node_splits[0];
            let start = split.input_start as usize;
            let end = start + split.input_count as usize;
            return vec![self.partition(
                bins,
                &input_indices[start..end],
                split.split_feature,
                split.split_threshold,
                num_features,
            )];
        }

        // Cache bins
        self.ensure_bins_cached(bins);

        let num_nodes = node_splits.len();
        let max_node_rows = node_splits
            .iter()
            .map(|s| s.input_count as usize)
            .max()
            .unwrap_or(0);

        // Prepare batch parameters
        let node_starts: Vec<u32> = node_splits.iter().map(|s| s.input_start).collect();
        let node_counts: Vec<u32> = node_splits.iter().map(|s| s.input_count).collect();
        let split_features: Vec<u32> = node_splits.iter().map(|s| s.split_feature).collect();
        let split_thresholds: Vec<u32> = node_splits.iter().map(|s| s.split_threshold).collect();

        // Output starts: each node gets max_node_rows * 2 (left + right)
        let output_starts: Vec<u32> = (0..num_nodes)
            .map(|i| (i * max_node_rows * 2) as u32)
            .collect();

        // Ensure batch buffers are allocated
        let total_output = num_nodes * max_node_rows * 2;
        let num_counters = num_nodes * 2;

        if self.cached_batch_size < total_output || self.cached_batch_output.is_none() {
            self.cached_batch_output = Some(self.device.alloc_zeros(total_output));
            self.cached_batch_size = total_output;
        }
        if self.cached_batch_counters.is_none()
            || self
                .cached_batch_counters
                .as_ref()
                .map(|c| c.len())
                .unwrap_or(0)
                < num_counters
        {
            self.cached_batch_counters = Some(self.device.alloc_zeros(num_counters));
        }

        // Upload batch parameters
        let d_input = self.device.htod_copy(input_indices);
        let d_node_starts = self.device.htod_copy(&node_starts);
        let d_node_counts = self.device.htod_copy(&node_counts);
        let d_split_features = self.device.htod_copy(&split_features);
        let d_split_thresholds = self.device.htod_copy(&split_thresholds);
        let d_output_starts = self.device.htod_copy(&output_starts);

        let stream = self.device.stream();

        // Zero counters
        let zero_blocks = ((num_counters + 255) / 256) as u32;
        let zero_config = LaunchConfig {
            block_dim: (256, 1, 1),
            grid_dim: (zero_blocks.max(1), 1, 1),
            shared_mem_bytes: 0,
        };
        unsafe {
            let d_counters = self.cached_batch_counters.as_mut().unwrap();
            stream
                .launch_builder(self.zero_counters_fn.as_ref().unwrap())
                .arg(d_counters)
                .arg(&(num_counters as u32))
                .launch(zero_config)
                .expect("Failed to launch zero_counters kernel");
        }

        // Launch batched partition kernel
        // Grid: (num_nodes, row_tiles)
        let threads_per_block = 256u32;
        let row_tiles = ((max_node_rows as u32) + threads_per_block - 1) / threads_per_block;

        let config = LaunchConfig {
            block_dim: (threads_per_block, 1, 1),
            grid_dim: (num_nodes as u32, row_tiles, 1),
            shared_mem_bytes: 0,
        };

        unsafe {
            let d_bins = self.cached_bins.as_ref().unwrap();
            let d_output = self.cached_batch_output.as_mut().unwrap();
            let d_counters = self.cached_batch_counters.as_mut().unwrap();

            stream
                .launch_builder(self.partition_batched_fn.as_ref().unwrap())
                .arg(d_bins)
                .arg(&d_input)
                .arg(d_output)
                .arg(d_counters)
                .arg(&d_node_starts)
                .arg(&d_node_counts)
                .arg(&d_split_features)
                .arg(&d_split_thresholds)
                .arg(&d_output_starts)
                .arg(&(num_features as u32))
                .arg(&(max_node_rows as u32))
                .launch(config)
                .expect("Failed to launch partition_batched kernel");
        }

        self.device.synchronize();

        // Read back results
        let counters = self
            .device
            .dtoh_copy(self.cached_batch_counters.as_ref().unwrap());
        let output = self
            .device
            .dtoh_copy(self.cached_batch_output.as_ref().unwrap());

        // Extract results for each node
        (0..num_nodes)
            .map(|i| {
                let left_count = counters[i * 2];
                let right_count = counters[i * 2 + 1];
                let output_start = output_starts[i] as usize;

                let left_indices =
                    output[output_start..output_start + left_count as usize].to_vec();
                let right_indices = output[output_start + max_node_rows
                    ..output_start + max_node_rows + right_count as usize]
                    .to_vec();

                PartitionResult {
                    left_indices,
                    right_indices,
                    left_count,
                    right_count,
                }
            })
            .collect()
    }

    /// GPU-resident partition: reads from input GPU buffer, writes to output GPU buffer.
    /// Returns only counts (indices stay on GPU).
    /// Uses internally cached bins buffer.
    ///
    /// # Arguments
    /// * `d_input` - Input GPU buffer with row indices
    /// * `d_output` - Output GPU buffer (will contain partitioned indices)
    /// * `node_splits` - Split info for each node (includes input_start, input_count, feature, threshold)
    /// * `num_features` - Number of features
    ///
    /// # Returns
    /// Vec of GpuPartitionResult with counts and output offsets
    pub fn partition_gpu_resident(
        &mut self,
        d_input: &CudaSlice<u32>,
        d_output: &mut CudaSlice<u32>,
        node_splits: &[NodeSplit],
        num_features: usize,
    ) -> Vec<GpuPartitionResult> {
        self.ensure_initialized();

        if node_splits.is_empty() {
            return Vec::new();
        }

        let num_nodes = node_splits.len();
        let max_node_rows = node_splits
            .iter()
            .map(|s| s.input_count as usize)
            .max()
            .unwrap_or(0);

        // Prepare node metadata
        let node_input_starts: Vec<u32> = node_splits.iter().map(|s| s.input_start).collect();
        let node_counts: Vec<u32> = node_splits.iter().map(|s| s.input_count).collect();
        let split_features: Vec<u32> = node_splits.iter().map(|s| s.split_feature).collect();
        let split_thresholds: Vec<u32> = node_splits.iter().map(|s| s.split_threshold).collect();

        // Output layout: each node gets max_node_rows * 2 space (left then right)
        let node_output_starts: Vec<u32> = (0..num_nodes)
            .map(|i| (i * max_node_rows * 2) as u32)
            .collect();

        let d_node_input_starts = self.device.htod_copy(&node_input_starts);
        let d_node_counts = self.device.htod_copy(&node_counts);
        let d_split_features = self.device.htod_copy(&split_features);
        let d_split_thresholds = self.device.htod_copy(&split_thresholds);
        let d_node_output_starts = self.device.htod_copy(&node_output_starts);

        // Ensure counters buffer
        let num_counters = num_nodes * 2;
        if self.cached_batch_counters.is_none()
            || self
                .cached_batch_counters
                .as_ref()
                .map(|c| c.len())
                .unwrap_or(0)
                < num_counters
        {
            self.cached_batch_counters = Some(self.device.alloc_zeros(num_counters));
        }

        let stream = self.device.stream();

        // Zero counters
        let zero_blocks = ((num_counters + 255) / 256) as u32;
        let zero_config = LaunchConfig {
            block_dim: (256, 1, 1),
            grid_dim: (zero_blocks.max(1), 1, 1),
            shared_mem_bytes: 0,
        };
        unsafe {
            let d_counters = self.cached_batch_counters.as_mut().unwrap();
            stream
                .launch_builder(self.zero_counters_fn.as_ref().unwrap())
                .arg(d_counters)
                .arg(&(num_counters as u32))
                .launch(zero_config)
                .expect("Failed to launch zero_counters kernel");
        }

        // Launch GPU-resident partition kernel
        let threads_per_block = 256u32;
        let row_tiles = ((max_node_rows as u32) + threads_per_block - 1) / threads_per_block;

        let config = LaunchConfig {
            block_dim: (threads_per_block, 1, 1),
            grid_dim: (num_nodes as u32, row_tiles, 1),
            shared_mem_bytes: 0,
        };

        unsafe {
            let d_bins = self.cached_bins.as_ref().expect("bins must be cached");
            let d_counters = self.cached_batch_counters.as_mut().unwrap();

            stream
                .launch_builder(self.partition_gpu_resident_fn.as_ref().unwrap())
                .arg(d_bins)
                .arg(d_input)
                .arg(d_output)
                .arg(d_counters)
                .arg(&d_node_input_starts)
                .arg(&d_node_counts)
                .arg(&d_split_features)
                .arg(&d_split_thresholds)
                .arg(&d_node_output_starts)
                .arg(&(num_features as u32))
                .arg(&(max_node_rows as u32))
                .launch(config)
                .expect("Failed to launch partition_gpu_resident kernel");
        }

        self.device.synchronize();

        // Read back only counters (indices stay on GPU)
        let counters = self
            .device
            .dtoh_copy(self.cached_batch_counters.as_ref().unwrap());

        // Return results with offsets
        (0..num_nodes)
            .map(|i| GpuPartitionResult {
                left_count: counters[i * 2],
                right_count: counters[i * 2 + 1],
                output_start: node_output_starts[i],
            })
            .collect()
    }

    /// Compact partition results: rearrange output so left/right are contiguous.
    /// Call this after partition_gpu_resident to prepare indices for next level.
    ///
    /// Input layout (per node): [left(max_rows), right(max_rows)]
    /// Output layout (per node): [left(actual), right(actual)] - contiguous
    pub fn compact_partitions(
        &mut self,
        d_padded: &CudaSlice<u32>,
        d_compacted: &mut CudaSlice<u32>,
        partition_results: &[GpuPartitionResult],
        max_node_rows: usize,
    ) -> Vec<(u32, u32)> {
        self.ensure_initialized();

        if partition_results.is_empty() {
            return Vec::new();
        }

        let num_nodes = partition_results.len();

        // Calculate compacted output offsets
        let mut compacted_output_starts = Vec::with_capacity(num_nodes);
        let mut offset = 0u32;
        for r in partition_results {
            compacted_output_starts.push(offset);
            offset += r.left_count + r.right_count;
        }

        let input_starts: Vec<u32> = partition_results.iter().map(|r| r.output_start).collect();
        let left_counts: Vec<u32> = partition_results.iter().map(|r| r.left_count).collect();
        let right_counts: Vec<u32> = partition_results.iter().map(|r| r.right_count).collect();

        let d_input_starts = self.device.htod_copy(&input_starts);
        let d_left_counts = self.device.htod_copy(&left_counts);
        let d_right_counts = self.device.htod_copy(&right_counts);
        let d_output_starts = self.device.htod_copy(&compacted_output_starts);

        let stream = self.device.stream();

        let config = LaunchConfig {
            block_dim: (256, 1, 1),
            grid_dim: (num_nodes as u32, 1, 1),
            shared_mem_bytes: 0,
        };

        unsafe {
            stream
                .launch_builder(self.compact_partitions_fn.as_ref().unwrap())
                .arg(d_padded)
                .arg(d_compacted)
                .arg(&d_input_starts)
                .arg(&d_left_counts)
                .arg(&d_right_counts)
                .arg(&d_output_starts)
                .arg(&(max_node_rows as u32))
                .arg(&(num_nodes as u32))
                .launch(config)
                .expect("Failed to launch compact_partitions kernel");
        }

        self.device.synchronize();

        // Return (compacted_start, total_count) for each node
        partition_results
            .iter()
            .zip(compacted_output_starts.iter())
            .map(|(r, &start)| (start, r.left_count + r.right_count))
            .collect()
    }

    /// Fused partition: partition and write directly to contiguous output.
    /// Eliminates the separate compaction step for better performance.
    ///
    /// # Arguments
    /// * `d_input` - Input GPU buffer with row indices
    /// * `d_output` - Output GPU buffer (will contain partitioned indices in contiguous layout)
    /// * `node_splits` - Split info for each node
    /// * `num_features` - Number of features
    ///
    /// # Returns
    /// Vec of (output_start, left_count, right_count) for each node
    pub fn partition_fused(
        &mut self,
        d_input: &CudaSlice<u32>,
        d_output: &mut CudaSlice<u32>,
        node_splits: &[NodeSplit],
        num_features: usize,
    ) -> Vec<(u32, u32, u32)> {
        self.ensure_initialized();

        if node_splits.is_empty() {
            return Vec::new();
        }

        let num_nodes = node_splits.len();

        // Ensure metadata buffers are allocated
        self.ensure_metadata_buffers(num_nodes);

        // Calculate output layout: each node gets node_count space (left + right)
        // Pre-allocate with left_capacity = node_count (worst case all go left)
        let mut output_offsets = Vec::with_capacity(num_nodes);
        let mut left_capacities = Vec::with_capacity(num_nodes);
        let mut offset = 0u32;
        for split in node_splits {
            output_offsets.push(offset);
            left_capacities.push(split.input_count); // Use full count as capacity
            offset += split.input_count * 2; // Space for left + right (with padding)
        }

        // Prepare node metadata
        let node_input_starts: Vec<u32> = node_splits.iter().map(|s| s.input_start).collect();
        let node_counts: Vec<u32> = node_splits.iter().map(|s| s.input_count).collect();
        let split_features: Vec<u32> = node_splits.iter().map(|s| s.split_feature).collect();
        let split_thresholds: Vec<u32> = node_splits.iter().map(|s| s.split_threshold).collect();

        // Copy metadata to cached GPU buffers (htod_copy_into avoids allocation)
        self.device.htod_copy_into(
            &node_input_starts,
            self.cached_node_input_starts.as_mut().unwrap(),
        );
        self.device
            .htod_copy_into(&node_counts, self.cached_node_counts.as_mut().unwrap());
        self.device.htod_copy_into(
            &split_features,
            self.cached_split_features.as_mut().unwrap(),
        );
        self.device.htod_copy_into(
            &split_thresholds,
            self.cached_split_thresholds.as_mut().unwrap(),
        );
        self.device.htod_copy_into(
            &output_offsets,
            self.cached_output_offsets.as_mut().unwrap(),
        );
        self.device.htod_copy_into(
            &left_capacities,
            self.cached_left_capacities.as_mut().unwrap(),
        );

        // Ensure counters buffer
        let num_counters = num_nodes * 2;
        if self.cached_batch_counters.is_none()
            || self
                .cached_batch_counters
                .as_ref()
                .map(|c| c.len())
                .unwrap_or(0)
                < num_counters
        {
            self.cached_batch_counters = Some(self.device.alloc_zeros(num_counters));
        }

        let stream = self.device.stream();

        // Zero counters
        let zero_blocks = ((num_counters + 255) / 256) as u32;
        let zero_config = LaunchConfig {
            block_dim: (256, 1, 1),
            grid_dim: (zero_blocks.max(1), 1, 1),
            shared_mem_bytes: 0,
        };
        unsafe {
            let d_counters = self.cached_batch_counters.as_mut().unwrap();
            stream
                .launch_builder(self.zero_counters_fn.as_ref().unwrap())
                .arg(d_counters)
                .arg(&(num_counters as u32))
                .launch(zero_config)
                .expect("Failed to launch zero_counters kernel");
        }

        // Launch fused partition kernel
        let max_node_rows = node_splits
            .iter()
            .map(|s| s.input_count as usize)
            .max()
            .unwrap_or(0);
        let threads_per_block = 256u32;
        let row_tiles = ((max_node_rows as u32) + threads_per_block - 1) / threads_per_block;

        let config = LaunchConfig {
            block_dim: (threads_per_block, 1, 1),
            grid_dim: (num_nodes as u32, row_tiles, 1),
            shared_mem_bytes: 0,
        };

        unsafe {
            let d_bins = self.cached_bins.as_ref().expect("bins must be cached");
            let d_counters = self.cached_batch_counters.as_mut().unwrap();

            stream
                .launch_builder(self.partition_fused_fn.as_ref().unwrap())
                .arg(d_bins)
                .arg(d_input)
                .arg(d_output)
                .arg(d_counters)
                .arg(self.cached_node_input_starts.as_ref().unwrap())
                .arg(self.cached_node_counts.as_ref().unwrap())
                .arg(self.cached_split_features.as_ref().unwrap())
                .arg(self.cached_split_thresholds.as_ref().unwrap())
                .arg(self.cached_output_offsets.as_ref().unwrap())
                .arg(self.cached_left_capacities.as_ref().unwrap())
                .arg(&(num_features as u32))
                .launch(config)
                .expect("Failed to launch partition_fused kernel");
        }

        self.device.synchronize();

        // Read back only counters (indices stay on GPU)
        let counters = self
            .device
            .dtoh_copy(self.cached_batch_counters.as_ref().unwrap());

        // Return (output_start, left_count, right_count) for each node
        (0..num_nodes)
            .map(|i| (output_offsets[i], counters[i * 2], counters[i * 2 + 1]))
            .collect()
    }
}