irithyll-core 1.0.0

Core types, training engine, and inference for irithyll streaming ML — no_std + alloc, histogram binning, Hoeffding trees, SGBT ensembles, drift detection, f32 + int16 packed formats
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
//! Single boosting step: owns one tree + drift detector + optional alternate.
//!
//! [`BoostingStep`] is a thin wrapper around [`TreeSlot`]
//! that adds SGBT variant logic. The three variants from Gunasekara et al. (2024) are:
//!
//! - **Standard** (`train_count = 1`): each sample trains the tree exactly once.
//! - **Skip** (`train_count = 0`): the sample is skipped (only prediction returned).
//! - **Multiple Iterations** (`train_count > 1`): the sample trains the tree
//!   multiple times, weighted by the hessian.
//!
//! The variant logic is computed externally (by `SGBTVariant::train_count()` in the
//! ensemble orchestrator) and passed in as `train_count`. This keeps `BoostingStep`
//! focused on execution rather than policy.

use alloc::boxed::Box;
use core::fmt;

use crate::drift::DriftDetector;
use crate::ensemble::replacement::TreeSlot;
use crate::tree::builder::TreeConfig;

/// A single step in the SGBT boosting sequence.
///
/// Owns a [`TreeSlot`] and applies variant-aware training repetition. The number
/// of training iterations per sample is determined by the caller (the ensemble
/// orchestrator computes `train_count` from the configured SGBT variant).
///
/// # Prediction semantics
///
/// Both [`train_and_predict`](BoostingStep::train_and_predict) and
/// [`predict`](BoostingStep::predict) return the active tree's prediction
/// **before** any training on the current sample. This ensures unbiased
/// gradient computation in the boosting loop.
#[derive(Clone)]
pub struct BoostingStep {
    /// The tree slot managing the active tree, alternate, and drift detector.
    slot: TreeSlot,
}

impl fmt::Debug for BoostingStep {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("BoostingStep")
            .field("slot", &self.slot)
            .finish()
    }
}

impl BoostingStep {
    /// Create a new boosting step with a fresh tree and drift detector.
    pub fn new(tree_config: TreeConfig, detector: Box<dyn DriftDetector>) -> Self {
        Self {
            slot: TreeSlot::new(tree_config, detector, None),
        }
    }

    /// Create a new boosting step with optional time-based tree replacement.
    pub fn new_with_max_samples(
        tree_config: TreeConfig,
        detector: Box<dyn DriftDetector>,
        max_tree_samples: Option<u64>,
    ) -> Self {
        Self {
            slot: TreeSlot::new(tree_config, detector, max_tree_samples),
        }
    }

    /// Create a new boosting step with graduated tree handoff.
    pub fn new_with_graduated(
        tree_config: TreeConfig,
        detector: Box<dyn DriftDetector>,
        max_tree_samples: Option<u64>,
        shadow_warmup: usize,
    ) -> Self {
        Self {
            slot: TreeSlot::with_shadow_warmup(
                tree_config,
                detector,
                max_tree_samples,
                shadow_warmup,
            ),
        }
    }

    /// Reconstruct a boosting step from a pre-built tree slot.
    ///
    /// Used during model deserialization.
    pub fn from_slot(slot: TreeSlot) -> Self {
        Self { slot }
    }

    /// Train on a single sample with variant-aware repetition.
    ///
    /// # Arguments
    ///
    /// * `features` - Input feature vector.
    /// * `gradient` - Negative gradient of the loss at this sample.
    /// * `hessian` - Second derivative (curvature) of the loss at this sample.
    /// * `train_count` - Number of training iterations for this sample:
    ///   - `0`: skip training entirely (SK variant or stochastic skip).
    ///   - `1`: standard single-pass training.
    ///   - `>1`: multiple iterations (MI variant).
    ///
    /// # Returns
    ///
    /// The prediction from the active tree **before** training.
    pub fn train_and_predict(
        &mut self,
        features: &[f64],
        gradient: f64,
        hessian: f64,
        train_count: usize,
    ) -> f64 {
        if train_count == 0 {
            // Skip variant: no training, just predict.
            return self.slot.predict(features);
        }

        // First iteration: train and get the pre-training prediction.
        let pred = self.slot.train_and_predict(features, gradient, hessian);

        // Additional iterations for MI variant.
        // Each subsequent call still feeds the same gradient/hessian to the
        // tree and drift detector, effectively weighting this sample more heavily.
        for _ in 1..train_count {
            self.slot.train_and_predict(features, gradient, hessian);
        }

        pred
    }

    /// Predict without training.
    ///
    /// Routes the feature vector through the active tree and returns the
    /// leaf value. Does not update any state.
    #[inline]
    pub fn predict(&self, features: &[f64]) -> f64 {
        self.slot.predict(features)
    }

    /// Predict with variance for confidence estimation.
    ///
    /// Returns `(leaf_value, variance)` where variance = 1 / (H_sum + lambda).
    #[inline]
    pub fn predict_with_variance(&self, features: &[f64]) -> (f64, f64) {
        self.slot.predict_with_variance(features)
    }

    /// Predict using sigmoid-blended soft routing for smooth interpolation.
    ///
    /// See [`crate::tree::hoeffding::HoeffdingTree::predict_smooth`] for details.
    #[inline]
    pub fn predict_smooth(&self, features: &[f64], bandwidth: f64) -> f64 {
        self.slot.predict_smooth(features, bandwidth)
    }

    /// Predict using per-feature auto-calibrated bandwidths.
    #[inline]
    pub fn predict_smooth_auto(&self, features: &[f64], bandwidths: &[f64]) -> f64 {
        self.slot.predict_smooth_auto(features, bandwidths)
    }

    /// Predict with parent-leaf linear interpolation.
    #[inline]
    pub fn predict_interpolated(&self, features: &[f64]) -> f64 {
        self.slot.predict_interpolated(features)
    }

    /// Predict with sibling-based interpolation for feature-continuous predictions.
    #[inline]
    pub fn predict_sibling_interpolated(&self, features: &[f64], bandwidths: &[f64]) -> f64 {
        self.slot.predict_sibling_interpolated(features, bandwidths)
    }

    /// Predict using per-node auto-bandwidth soft routing.
    #[inline]
    pub fn predict_soft_routed(&self, features: &[f64]) -> f64 {
        self.slot.predict_soft_routed(features)
    }

    /// Predict with graduated active-shadow blending.
    #[inline]
    pub fn predict_graduated(&self, features: &[f64]) -> f64 {
        self.slot.predict_graduated(features)
    }

    /// Predict with graduated blending + sibling interpolation.
    #[inline]
    pub fn predict_graduated_sibling_interpolated(
        &self,
        features: &[f64],
        bandwidths: &[f64],
    ) -> f64 {
        self.slot
            .predict_graduated_sibling_interpolated(features, bandwidths)
    }

    /// Number of leaves in the active tree.
    #[inline]
    pub fn n_leaves(&self) -> usize {
        self.slot.n_leaves()
    }

    /// Total samples the active tree has seen.
    #[inline]
    pub fn n_samples_seen(&self) -> u64 {
        self.slot.n_samples_seen()
    }

    /// Whether the slot has an alternate tree being trained.
    #[inline]
    pub fn has_alternate(&self) -> bool {
        self.slot.has_alternate()
    }

    /// Reset to a completely fresh state: new tree, no alternate, reset detector.
    pub fn reset(&mut self) {
        self.slot.reset();
    }

    /// Immutable access to the underlying [`TreeSlot`].
    #[inline]
    pub fn slot(&self) -> &TreeSlot {
        &self.slot
    }

    /// Mutable access to the underlying [`TreeSlot`].
    #[inline]
    pub fn slot_mut(&mut self) -> &mut TreeSlot {
        &mut self.slot
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::drift::pht::PageHinkleyTest;
    use alloc::boxed::Box;
    use alloc::format;

    /// Create a default TreeConfig for tests.
    fn test_tree_config() -> TreeConfig {
        TreeConfig::new()
            .grace_period(20)
            .max_depth(4)
            .n_bins(16)
            .lambda(1.0)
    }

    /// Create a default drift detector for tests.
    fn test_detector() -> Box<dyn DriftDetector> {
        Box::new(PageHinkleyTest::new())
    }

    // -------------------------------------------------------------------
    // Test 1: train_count=0 skips training (just predicts).
    // -------------------------------------------------------------------
    #[test]
    fn train_count_zero_skips_training() {
        let mut step = BoostingStep::new(test_tree_config(), test_detector());
        let features = [1.0, 2.0, 3.0];

        // Train with count=0 should not actually train.
        let pred = step.train_and_predict(&features, -0.5, 1.0, 0);
        assert!(
            pred.abs() < 1e-12,
            "train_count=0 should return fresh prediction (~0.0), got {}",
            pred,
        );

        // Verify no samples were actually trained.
        assert_eq!(
            step.n_samples_seen(),
            0,
            "train_count=0 should not increment samples_seen",
        );
    }

    // -------------------------------------------------------------------
    // Test 2: train_count=1 trains once.
    // -------------------------------------------------------------------
    #[test]
    fn train_count_one_trains_once() {
        let mut step = BoostingStep::new(test_tree_config(), test_detector());
        let features = [1.0, 2.0, 3.0];

        let pred = step.train_and_predict(&features, -0.5, 1.0, 1);
        assert!(
            pred.abs() < 1e-12,
            "first prediction should be ~0.0, got {}",
            pred,
        );

        // After one training call, the tree should have seen 1 sample.
        assert_eq!(
            step.n_samples_seen(),
            1,
            "train_count=1 should train exactly once",
        );

        // Second call should return non-zero (tree has been trained).
        let pred2 = step.predict(&features);
        assert!(
            pred2.is_finite(),
            "prediction after training should be finite",
        );
    }

    // -------------------------------------------------------------------
    // Test 3: train_count=3 trains multiple times.
    // -------------------------------------------------------------------
    #[test]
    fn train_count_three_trains_multiple_times() {
        let mut step = BoostingStep::new(test_tree_config(), test_detector());
        let features = [1.0, 2.0, 3.0];

        let pred = step.train_and_predict(&features, -0.5, 1.0, 3);
        assert!(
            pred.abs() < 1e-12,
            "first prediction should be ~0.0, got {}",
            pred,
        );

        // After train_count=3, the tree should have seen 3 samples.
        assert_eq!(
            step.n_samples_seen(),
            3,
            "train_count=3 should train exactly 3 times",
        );
    }

    // -------------------------------------------------------------------
    // Test 4: Reset works.
    // -------------------------------------------------------------------
    #[test]
    fn reset_clears_state() {
        let mut step = BoostingStep::new(test_tree_config(), test_detector());
        let features = [1.0, 2.0, 3.0];

        // Train several samples.
        for _ in 0..50 {
            step.train_and_predict(&features, -0.5, 1.0, 1);
        }

        assert!(step.n_samples_seen() > 0, "should have trained samples");

        step.reset();

        assert_eq!(step.n_leaves(), 1, "after reset, should have 1 leaf");
        assert_eq!(
            step.n_samples_seen(),
            0,
            "after reset, samples_seen should be 0"
        );
        assert!(
            !step.has_alternate(),
            "after reset, no alternate should exist"
        );

        let pred = step.predict(&features);
        assert!(
            pred.abs() < 1e-12,
            "prediction after reset should be ~0.0, got {}",
            pred,
        );
    }

    // -------------------------------------------------------------------
    // Test 5: Predict-only (no training) works on fresh step.
    // -------------------------------------------------------------------
    #[test]
    fn predict_only_on_fresh_step() {
        let step = BoostingStep::new(test_tree_config(), test_detector());

        for i in 0..10 {
            let x = (i as f64) * 0.5;
            let pred = step.predict(&[x, x + 1.0, x + 2.0]);
            assert!(
                pred.abs() < 1e-12,
                "untrained step should predict ~0.0, got {} at i={}",
                pred,
                i,
            );
        }
    }

    // -------------------------------------------------------------------
    // Test 6: Multiple calls with different train_counts produce expected
    //         cumulative sample counts.
    // -------------------------------------------------------------------
    #[test]
    fn mixed_train_counts_accumulate_correctly() {
        let mut step = BoostingStep::new(test_tree_config(), test_detector());
        let features = [1.0, 2.0, 3.0];

        // count=2 -> 2 samples
        step.train_and_predict(&features, -0.1, 1.0, 2);
        assert_eq!(step.n_samples_seen(), 2);

        // count=0 -> still 2 samples (skipped)
        step.train_and_predict(&features, -0.1, 1.0, 0);
        assert_eq!(step.n_samples_seen(), 2);

        // count=1 -> 3 samples
        step.train_and_predict(&features, -0.1, 1.0, 1);
        assert_eq!(step.n_samples_seen(), 3);

        // count=5 -> 8 samples
        step.train_and_predict(&features, -0.1, 1.0, 5);
        assert_eq!(step.n_samples_seen(), 8);
    }

    // -------------------------------------------------------------------
    // Test 7: n_leaves and has_alternate passthrough to slot.
    // -------------------------------------------------------------------
    #[test]
    fn accessors_match_slot() {
        let step = BoostingStep::new(test_tree_config(), test_detector());

        assert_eq!(step.n_leaves(), step.slot().n_leaves());
        assert_eq!(step.has_alternate(), step.slot().has_alternate());
        assert_eq!(step.n_samples_seen(), step.slot().n_samples_seen());
    }

    // -------------------------------------------------------------------
    // Test 8: Debug formatting works.
    // -------------------------------------------------------------------
    #[test]
    fn debug_format_does_not_panic() {
        let step = BoostingStep::new(test_tree_config(), test_detector());
        let debug_str = format!("{:?}", step);
        assert!(
            debug_str.contains("BoostingStep"),
            "debug output should contain 'BoostingStep'",
        );
    }
}