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
//! SIMD-optimized split finding kernels
//!
//! This module provides vectorized implementations of the split finding algorithm
//! used in GBDT training. The key insight is that:
//!
//! 1. We iterate over 256 bins computing prefix sums (cumulative gradients/hessians/counts)
//! 2. For each bin, we compute the gain using the Friedman MSE formula
//! 3. Both operations are highly parallelizable with SIMD
//!
//! # Algorithm
//!
//! For each bin threshold t:
//! - left_g = sum(gradients[0..=t])
//! - left_h = sum(hessians[0..=t])
//! - left_n = sum(counts[0..=t])
//! - right_g = total_g - left_g
//! - right_h = total_h - left_h
//! - right_n = total_n - left_n
//! - gain = 0.5 * (left_g² / (left_h + λ) + right_g² / (right_h + λ) - total_g² / (total_h + λ))
//!
//! # SIMD Strategy
//!
//! 1. **Prefix sums**: Compute cumulative sums for all 256 bins first
//! 2. **Parallel gain**: Compute gain for 8 bins at a time using AVX2
//! 3. **Horizontal max**: Find the maximum gain across all bins

use crate::backend::scalar::kernel::fallback::SplitParams;

/// Result of split finding for a single feature
#[derive(Debug, Clone, Copy)]
pub struct SplitCandidate {
    /// Bin threshold (samples with bin <= threshold go left)
    pub bin_threshold: u8,
    /// Split gain (higher is better)
    pub gain: f32,
    /// Left child gradient sum
    pub left_gradient: f32,
    /// Left child hessian sum
    pub left_hessian: f32,
    /// Left child sample count
    pub left_count: u32,
    /// Right child gradient sum
    pub right_gradient: f32,
    /// Right child hessian sum
    pub right_hessian: f32,
    /// Right child sample count
    pub right_count: u32,
}

impl Default for SplitCandidate {
    fn default() -> Self {
        Self {
            bin_threshold: 0,
            gain: f32::NEG_INFINITY,
            left_gradient: 0.0,
            left_hessian: 0.0,
            left_count: 0,
            right_gradient: 0.0,
            right_hessian: 0.0,
            right_count: 0,
        }
    }
}

impl SplitCandidate {
    /// Check if this split is valid
    #[inline]
    pub fn is_valid(&self) -> bool {
        self.gain > f32::NEG_INFINITY && self.left_count > 0 && self.right_count > 0
    }
}

/// Find the best split for a histogram using scalar code
///
/// This is the baseline implementation that works on all architectures.
///
/// # Arguments
/// * `hist_grads` - Sum of gradients per bin [256]
/// * `hist_hess` - Sum of hessians per bin [256]
/// * `hist_counts` - Count per bin [256]
/// * `total_gradient` - Total gradient sum across all bins
/// * `total_hessian` - Total hessian sum across all bins
/// * `total_count` - Total sample count across all bins
/// * `lambda` - L2 regularization parameter
/// * `min_samples_leaf` - Minimum samples required in each leaf
/// * `min_hessian_leaf` - Minimum hessian sum required in each leaf
///
/// # Returns
/// The best split candidate, or None if no valid split exists
#[inline]
pub fn find_best_split_scalar(
    hist_grads: &[f32; 256],
    hist_hess: &[f32; 256],
    hist_counts: &[u32; 256],
    params: SplitParams,
) -> Option<SplitCandidate> {
    let SplitParams {
        total_gradient,
        total_hessian,
        total_count,
        lambda,
        min_samples_leaf,
        min_hessian_leaf,
    } = params;
    let mut best = SplitCandidate::default();

    // Parent score (constant, compute once)
    let parent_score = (total_gradient * total_gradient) / (total_hessian + lambda);

    // Cumulative sums for left child
    let mut left_g = 0.0f32;
    let mut left_h = 0.0f32;
    let mut left_n = 0u32;

    // Scan through bins 0..254 (last bin can't be a threshold since right would be empty)
    for bin in 0..255u8 {
        let bin_idx = bin as usize;

        // Skip empty bins
        if hist_counts[bin_idx] == 0 {
            continue;
        }

        // Accumulate left child stats
        left_g += hist_grads[bin_idx];
        left_h += hist_hess[bin_idx];
        left_n += hist_counts[bin_idx];

        // Compute right child stats
        let right_g = total_gradient - left_g;
        let right_h = total_hessian - left_h;
        let right_n = total_count - left_n;

        // Check leaf constraints
        if left_n < min_samples_leaf || right_n < min_samples_leaf {
            continue;
        }
        if left_h < min_hessian_leaf || right_h < min_hessian_leaf {
            continue;
        }

        // Compute gain: 0.5 * (left_score + right_score - parent_score)
        let left_score = (left_g * left_g) / (left_h + lambda);
        let right_score = (right_g * right_g) / (right_h + lambda);
        let gain = 0.5 * (left_score + right_score - parent_score);

        if gain > best.gain {
            best.bin_threshold = bin;
            best.gain = gain;
            best.left_gradient = left_g;
            best.left_hessian = left_h;
            best.left_count = left_n;
            best.right_gradient = right_g;
            best.right_hessian = right_h;
            best.right_count = right_n;
        }
    }

    if best.is_valid() {
        Some(best)
    } else {
        None
    }
}

/// Find the best split for a histogram using SIMD (AVX2)
///
/// This implementation uses AVX2 to:
/// 1. Compute prefix sums for all 256 bins
/// 2. Evaluate gain for 8 bins in parallel
/// 3. Find the maximum gain using horizontal operations
///
/// # Safety
/// Requires AVX2 support. Caller must check `is_x86_feature_detected!("avx2")`.
#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "avx2", enable = "fma")]
pub unsafe fn find_best_split_simd(
    hist_grads: &[f32; 256],
    hist_hess: &[f32; 256],
    hist_counts: &[u32; 256],
    params: SplitParams,
) -> Option<SplitCandidate> {
    let SplitParams {
        total_gradient,
        total_hessian,
        total_count,
        lambda,
        min_samples_leaf,
        min_hessian_leaf,
    } = params;
    use std::arch::x86_64::*;

    // Step 1: Compute prefix sums for all 256 bins
    // We'll store cumulative sums in separate arrays
    let mut prefix_grads = [0.0f32; 256];
    let mut prefix_hess = [0.0f32; 256];
    let mut prefix_counts = [0u32; 256];

    // Scalar prefix sum (hard to vectorize due to data dependency)
    // But we can still benefit from good cache behavior
    let mut sum_g = 0.0f32;
    let mut sum_h = 0.0f32;
    let mut sum_n = 0u32;

    for i in 0..256 {
        sum_g += hist_grads[i];
        sum_h += hist_hess[i];
        sum_n += hist_counts[i];
        prefix_grads[i] = sum_g;
        prefix_hess[i] = sum_h;
        prefix_counts[i] = sum_n;
    }

    // Step 2: Compute gains for 8 bins at a time using SIMD
    // We process bins 0..248 in chunks of 8, then handle 248..255 separately

    // Broadcast constants
    let total_g_vec = _mm256_set1_ps(total_gradient);
    let total_h_vec = _mm256_set1_ps(total_hessian);
    let total_n_vec = _mm256_set1_ps(total_count as f32);
    let lambda_vec = _mm256_set1_ps(lambda);
    let half_vec = _mm256_set1_ps(0.5);
    let min_samples_vec = _mm256_set1_ps(min_samples_leaf as f32);
    let min_hessian_vec = _mm256_set1_ps(min_hessian_leaf);
    let neg_inf_vec = _mm256_set1_ps(f32::NEG_INFINITY);

    // Parent score (constant)
    let parent_score = (total_gradient * total_gradient) / (total_hessian + lambda);
    let parent_score_vec = _mm256_set1_ps(parent_score);

    // Track best gain and corresponding bin
    let mut best_gain = f32::NEG_INFINITY;
    let mut best_bin = 0u8;
    let mut best_left_g = 0.0f32;
    let mut best_left_h = 0.0f32;
    let mut best_left_n = 0u32;

    // Process 8 bins at a time
    let num_chunks = 255 / 8; // 31 chunks (bins 0-247)

    for chunk in 0..num_chunks {
        let base = chunk * 8;

        // Load 8 prefix sums
        let left_g = _mm256_loadu_ps(prefix_grads.as_ptr().add(base));
        let left_h = _mm256_loadu_ps(prefix_hess.as_ptr().add(base));

        // Load counts and convert to float for comparison
        let left_n_int: [u32; 8] = [
            prefix_counts[base],
            prefix_counts[base + 1],
            prefix_counts[base + 2],
            prefix_counts[base + 3],
            prefix_counts[base + 4],
            prefix_counts[base + 5],
            prefix_counts[base + 6],
            prefix_counts[base + 7],
        ];
        let left_n = _mm256_cvtepi32_ps(_mm256_loadu_si256(left_n_int.as_ptr() as *const __m256i));

        // Compute right child stats: right = total - left
        let right_g = _mm256_sub_ps(total_g_vec, left_g);
        let right_h = _mm256_sub_ps(total_h_vec, left_h);
        let right_n = _mm256_sub_ps(total_n_vec, left_n);

        // Check min_samples constraint
        // valid = (left_n >= min_samples) && (right_n >= min_samples)
        let left_samples_ok = _mm256_cmp_ps(left_n, min_samples_vec, _CMP_GE_OQ);
        let right_samples_ok = _mm256_cmp_ps(right_n, min_samples_vec, _CMP_GE_OQ);
        let samples_ok = _mm256_and_ps(left_samples_ok, right_samples_ok);

        // Check min_hessian constraint
        let left_hess_ok = _mm256_cmp_ps(left_h, min_hessian_vec, _CMP_GE_OQ);
        let right_hess_ok = _mm256_cmp_ps(right_h, min_hessian_vec, _CMP_GE_OQ);
        let hess_ok = _mm256_and_ps(left_hess_ok, right_hess_ok);

        // Combined validity mask
        let valid_mask = _mm256_and_ps(samples_ok, hess_ok);

        // Compute gain for all 8 bins
        // left_score = left_g² / (left_h + λ)
        let left_g_sq = _mm256_mul_ps(left_g, left_g);
        let left_denom = _mm256_add_ps(left_h, lambda_vec);
        let left_score = _mm256_div_ps(left_g_sq, left_denom);

        // right_score = right_g² / (right_h + λ)
        let right_g_sq = _mm256_mul_ps(right_g, right_g);
        let right_denom = _mm256_add_ps(right_h, lambda_vec);
        let right_score = _mm256_div_ps(right_g_sq, right_denom);

        // gain = 0.5 * (left_score + right_score - parent_score)
        let sum_scores = _mm256_add_ps(left_score, right_score);
        let diff = _mm256_sub_ps(sum_scores, parent_score_vec);
        let gain = _mm256_mul_ps(half_vec, diff);

        // Apply validity mask (invalid bins get -inf gain)
        let gain_masked = _mm256_blendv_ps(neg_inf_vec, gain, valid_mask);

        // Extract gains and find maximum
        let gain_arr: [f32; 8] = std::mem::transmute(gain_masked);

        for i in 0..8 {
            if gain_arr[i] > best_gain {
                best_gain = gain_arr[i];
                best_bin = (base + i) as u8;
                best_left_g = prefix_grads[base + i];
                best_left_h = prefix_hess[base + i];
                best_left_n = prefix_counts[base + i];
            }
        }
    }

    // Handle remaining bins 248..254 (bin 255 can't be a threshold)
    for bin in (num_chunks * 8)..255 {
        let left_g = prefix_grads[bin];
        let left_h = prefix_hess[bin];
        let left_n = prefix_counts[bin];
        let right_g = total_gradient - left_g;
        let right_h = total_hessian - left_h;
        let right_n = total_count - left_n;

        if left_n < min_samples_leaf || right_n < min_samples_leaf {
            continue;
        }
        if left_h < min_hessian_leaf || right_h < min_hessian_leaf {
            continue;
        }

        let left_score = (left_g * left_g) / (left_h + lambda);
        let right_score = (right_g * right_g) / (right_h + lambda);
        let gain = 0.5 * (left_score + right_score - parent_score);

        if gain > best_gain {
            best_gain = gain;
            best_bin = bin as u8;
            best_left_g = left_g;
            best_left_h = left_h;
            best_left_n = left_n;
        }
    }

    if best_gain > f32::NEG_INFINITY && best_left_n > 0 && (total_count - best_left_n) > 0 {
        Some(SplitCandidate {
            bin_threshold: best_bin,
            gain: best_gain,
            left_gradient: best_left_g,
            left_hessian: best_left_h,
            left_count: best_left_n,
            right_gradient: total_gradient - best_left_g,
            right_hessian: total_hessian - best_left_h,
            right_count: total_count - best_left_n,
        })
    } else {
        None
    }
}

/// Non-x86 fallback - just calls scalar version
#[cfg(not(target_arch = "x86_64"))]
#[inline]
pub fn find_best_split_simd(
    hist_grads: &[f32; 256],
    hist_hess: &[f32; 256],
    hist_counts: &[u32; 256],
    params: SplitParams,
) -> Option<SplitCandidate> {
    find_best_split_scalar(hist_grads, hist_hess, hist_counts, params)
}

#[cfg(test)]
mod tests {
    use super::*;

    fn create_test_histogram() -> ([f32; 256], [f32; 256], [u32; 256]) {
        let mut grads = [0.0f32; 256];
        let mut hess = [0.0f32; 256];
        let mut counts = [0u32; 256];

        // Create a clear split: bins 0-127 have negative gradients, 128-255 have positive
        for bin in 0..128 {
            grads[bin] = -1.0;
            hess[bin] = 1.0;
            counts[bin] = 1;
        }
        for bin in 128..256 {
            grads[bin] = 1.0;
            hess[bin] = 1.0;
            counts[bin] = 1;
        }

        (grads, hess, counts)
    }

    #[test]
    fn test_scalar_split_finding() {
        let (grads, hess, counts) = create_test_histogram();

        let result = find_best_split_scalar(
            &grads,
            &hess,
            &counts,
            SplitParams {
                total_gradient: 0.0,
                total_hessian: 256.0,
                total_count: 256,
                lambda: 0.0,
                min_samples_leaf: 1,
                min_hessian_leaf: 1.0,
            },
        );

        assert!(result.is_some());
        let split = result.unwrap();

        // Best split should be at bin 127
        assert_eq!(split.bin_threshold, 127);
        assert!(split.gain > 0.0);
        assert_eq!(split.left_count, 128);
        assert_eq!(split.right_count, 128);
    }

    #[test]
    fn test_simd_matches_scalar() {
        let (grads, hess, counts) = create_test_histogram();

        let params = SplitParams {
            total_gradient: 0.0,
            total_hessian: 256.0,
            total_count: 256,
            lambda: 1.0,
            min_samples_leaf: 1,
            min_hessian_leaf: 1.0,
        };

        let scalar_result = find_best_split_scalar(&grads, &hess, &counts, params);

        #[cfg(target_arch = "x86_64")]
        {
            if std::arch::is_x86_feature_detected!("avx2") {
                let simd_result = unsafe { find_best_split_simd(&grads, &hess, &counts, params) };

                assert!(scalar_result.is_some());
                assert!(simd_result.is_some());

                let scalar = scalar_result.unwrap();
                let simd = simd_result.unwrap();

                assert_eq!(scalar.bin_threshold, simd.bin_threshold);
                assert!((scalar.gain - simd.gain).abs() < 1e-5);
                assert_eq!(scalar.left_count, simd.left_count);
                assert_eq!(scalar.right_count, simd.right_count);
            }
        }
    }

    #[test]
    fn test_min_samples_constraint() {
        let mut grads = [0.0f32; 256];
        let mut hess = [0.0f32; 256];
        let mut counts = [0u32; 256];

        // Only 2 samples: 1 in bin 0, 1 in bin 255
        grads[0] = -1.0;
        hess[0] = 1.0;
        counts[0] = 1;

        grads[255] = 1.0;
        hess[255] = 1.0;
        counts[255] = 1;

        // With min_samples_leaf = 5, no valid split should exist
        let result = find_best_split_scalar(
            &grads,
            &hess,
            &counts,
            SplitParams {
                total_gradient: 0.0,
                total_hessian: 2.0,
                total_count: 2,
                lambda: 0.0,
                min_samples_leaf: 5,
                min_hessian_leaf: 1.0,
            },
        );

        assert!(result.is_none());
    }

    #[test]
    fn test_empty_histogram() {
        let grads = [0.0f32; 256];
        let hess = [0.0f32; 256];
        let counts = [0u32; 256];

        let result = find_best_split_scalar(
            &grads,
            &hess,
            &counts,
            SplitParams {
                total_gradient: 0.0,
                total_hessian: 0.0,
                total_count: 0,
                lambda: 1.0,
                min_samples_leaf: 1,
                min_hessian_leaf: 1.0,
            },
        );

        assert!(result.is_none());
    }

    #[test]
    fn test_single_bin_histogram() {
        let mut grads = [0.0f32; 256];
        let mut hess = [0.0f32; 256];
        let mut counts = [0u32; 256];

        // All samples in bin 100
        grads[100] = 10.0;
        hess[100] = 100.0;
        counts[100] = 100;

        let result = find_best_split_scalar(
            &grads,
            &hess,
            &counts,
            SplitParams {
                total_gradient: 10.0,
                total_hessian: 100.0,
                total_count: 100,
                lambda: 1.0,
                min_samples_leaf: 1,
                min_hessian_leaf: 1.0,
            },
        );

        // No valid split since all samples are in one bin
        assert!(result.is_none());
    }

    #[cfg(target_arch = "x86_64")]
    #[test]
    fn test_simd_various_splits() {
        if !std::arch::is_x86_feature_detected!("avx2") {
            println!("AVX2 not available, skipping test");
            return;
        }

        // Test with various split points
        for split_point in [10, 50, 100, 127, 200, 250] {
            let mut grads = [0.0f32; 256];
            let mut hess = [0.0f32; 256];
            let mut counts = [0u32; 256];

            for bin in 0..=split_point {
                grads[bin] = -1.0;
                hess[bin] = 1.0;
                counts[bin] = 1;
            }
            for bin in (split_point + 1)..256 {
                grads[bin] = 1.0;
                hess[bin] = 1.0;
                counts[bin] = 1;
            }

            let total_left = (split_point + 1) as f32;
            let total_right = (255 - split_point) as f32;
            let total_g = total_right - total_left;

            let params = SplitParams {
                total_gradient: total_g,
                total_hessian: 256.0,
                total_count: 256,
                lambda: 0.0,
                min_samples_leaf: 1,
                min_hessian_leaf: 1.0,
            };

            let scalar = find_best_split_scalar(&grads, &hess, &counts, params);

            let simd = unsafe { find_best_split_simd(&grads, &hess, &counts, params) };

            assert!(
                scalar.is_some(),
                "Scalar should find split at {}",
                split_point
            );
            assert!(simd.is_some(), "SIMD should find split at {}", split_point);

            let s = scalar.unwrap();
            let v = simd.unwrap();

            assert_eq!(
                s.bin_threshold, v.bin_threshold,
                "Threshold mismatch for split_point {}: scalar={}, simd={}",
                split_point, s.bin_threshold, v.bin_threshold
            );
            assert!(
                (s.gain - v.gain).abs() < 1e-4,
                "Gain mismatch for split_point {}: scalar={}, simd={}",
                split_point,
                s.gain,
                v.gain
            );
        }
    }
}