hessboost 0.2.2

Fast, deterministic gradient boosting (GBDT) in Rust: conformal intervals, explainable boosting machines, distributional boosting, tree-based diffusion, and XGBoost model interchange
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
//! In-place data updates (`hessboost::training::online`): the exact mode
//! reproduces retraining bit for bit, the approximate mode stays close to
//! it, updates are deterministic and atomic, and unsound configurations are
//! refused.

use std::num::NonZeroUsize;
use std::ops::ControlFlow;

use hessboost::config::{
    BalancedBagging, BoosterKind, Dart, GrowPolicy, Langevin, MaxDeltaStep, ModelShrink,
    ModelShrinkMode, QueryBagging,
};
use hessboost::data::FeatureType;
use hessboost::metric::EvalMetric;
use hessboost::objective::{CustomLoss, GradPair, LambdaRank, Objective, RegLoss};
use hessboost::prelude::*;
use hessboost::training::RoundEval;
use hessboost::training::online::{OnlineMode, OnlineModel, OnlineParams};

mod common;
use common::{incompatible_model, invalid_data, invalid_param, lcg, rmse, with_threads};

const COLS: usize = 4;

fn logistic() -> Objective {
    Objective::BinaryLogistic(RegLoss::default())
}

/// `y = 3 x0 - 2 x1 + x2 x3 + noise`, or its sign for classification.
fn data(n: usize, seed: u64, binary: bool) -> DMatrix {
    let mut next = lcg(seed);
    let (mut x, mut y) = (Vec::new(), Vec::new());
    for _ in 0..n {
        let row = [next(), next(), next(), next()];
        let f = 3.0 * row[0] - 2.0 * row[1] + row[2] * row[3] + 0.2 * (next() - 0.5);
        x.extend(row);
        y.push(if binary { f32::from(f > 0.6) } else { f });
    }
    DMatrix::from_dense(&x, n, COLS)
        .unwrap()
        .with_labels(&y)
        .unwrap()
}

fn params(objective: Objective) -> TrainingParams {
    TrainingParams::builder()
        .objective(objective)
        .tree_method(TreeMethod::Hist)
        .max_depth(4)
        .eta(0.3)
        .build()
        .unwrap()
}

#[test]
fn exact_updates_equal_retraining_bit_for_bit() {
    for objective in [Objective::SquaredError(RegLoss::default()), logistic()] {
        let binary = matches!(objective, Objective::BinaryLogistic(_));
        let name = objective.name().to_owned();
        let p = params(objective);
        let train_data = data(400, 1, binary);
        let mut online = OnlineModel::train(&p, &train_data, 15, OnlineParams::exact()).unwrap();
        let added = data(30, 2, binary);
        // Several updates in a row: each equals retraining on the data so far.
        for (additions, deletions) in [
            (Some(&added), vec![0, 7, 399]),
            (None, vec![3, 4, 5]),
            (Some(&added), Vec::new()),
        ] {
            online.update(additions, &deletions).unwrap();
            let retrained = train(&p, online.data(), 15).unwrap();
            assert_eq!(
                online.model().encode(ModelFormat::Json).unwrap(),
                retrained.encode(ModelFormat::Json).unwrap(),
                "{name}"
            );
        }
        assert_eq!(online.data().n_rows(), 400 - 3 + 30 - 3 + 30);
    }
}

#[test]
fn approximate_updates_stay_close_to_retraining() {
    let p = params(Objective::SquaredError(RegLoss::default()));
    let train_data = data(2000, 3, false);
    let test = data(1000, 4, false);
    let added = data(40, 5, false);
    let deletions: Vec<usize> = (0..40).map(|i| i * 13).collect();
    let mut online = OnlineModel::train(&p, &train_data, 30, OnlineParams::default()).unwrap();
    let report = online.update(Some(&added), &deletions).unwrap();
    assert_eq!(online.data().n_rows(), 2000);
    assert!(report.nodes_kept > 0);
    let retrained = train(&p, online.data(), 30).unwrap();
    let (updated, reference) = (rmse(online.model(), &test), rmse(&retrained, &test));
    assert!(
        (updated - reference).abs() < 0.05 * reference,
        "updated {updated} vs retrained {reference}"
    );
    // The fit reflects the new data: the added rows' error is no worse than
    // under the original model.
    let original = OnlineModel::train(&p, &train_data, 30, OnlineParams::default()).unwrap();
    assert!(rmse(online.model(), &added) <= rmse(original.model(), &added) * 1.01);

    // A higher tolerance keeps more splits: tolerance 1 regrows only nodes
    // whose split stopped being a valid candidate.
    let mut frozen =
        OnlineModel::train(&p, &train_data, 30, OnlineParams::approximate(1.0).unwrap()).unwrap();
    let tolerant = frozen.update(Some(&added), &deletions).unwrap();
    assert!(tolerant.subtrees_regrown <= report.subtrees_regrown);
    assert!(tolerant.nodes_kept >= report.nodes_kept);
}

#[test]
fn updates_ignore_the_thread_count() {
    let p = params(logistic());
    let train_data = data(600, 6, true);
    let added = data(20, 7, true);
    let run = |threads| {
        with_threads(threads, || {
            let mut online = OnlineModel::train(
                &p,
                &train_data,
                10,
                OnlineParams::approximate(0.05).unwrap(),
            )
            .unwrap();
            online.update(Some(&added), &[1, 2, 3, 50]).unwrap();
            online.model().encode(ModelFormat::Json).unwrap()
        })
    };
    assert_eq!(run(1), run(4));
}

#[test]
fn an_interrupted_update_changes_nothing() {
    let p = params(Objective::SquaredError(RegLoss::default()));
    let train_data = data(300, 8, false);
    for mode in [
        OnlineParams::exact(),
        OnlineParams::approximate(0.1).unwrap(),
    ] {
        let mut online = OnlineModel::train(&p, &train_data, 10, mode).unwrap();
        let before = online.model().encode(ModelFormat::Json).unwrap();
        let stop = |round: RoundEval| {
            if round.iteration() == 3 {
                ControlFlow::Break(())
            } else {
                ControlFlow::Continue(())
            }
        };
        assert_eq!(
            invalid_param(online.update_with(None, &[0, 1], stop)),
            "on_round"
        );
        assert_eq!(online.model().encode(ModelFormat::Json).unwrap(), before);
        assert_eq!(online.data().n_rows(), 300);
        // Refusing the commit after every iteration ran abandons it too.
        let mut rounds = 0;
        let refused = online.update_with_commit(
            None,
            &[0, 1],
            |_| {
                rounds += 1;
                ControlFlow::Continue(())
            },
            || ControlFlow::Break(()),
        );
        assert_eq!(invalid_param(refused), "on_round");
        assert_eq!(rounds, 10);
        assert_eq!(online.model().encode(ModelFormat::Json).unwrap(), before);
        assert_eq!(online.data().n_rows(), 300);
        // The model still updates afterwards, as an uninterrupted one would.
        online.update(None, &[0, 1]).unwrap();
        let mut fresh = OnlineModel::train(&p, &train_data, 10, mode).unwrap();
        fresh.update(None, &[0, 1]).unwrap();
        assert_eq!(
            online.model().encode(ModelFormat::Json).unwrap(),
            fresh.model().encode(ModelFormat::Json).unwrap()
        );
    }
}

/// Updates refuse what retraining on the updated data refuses, in both
/// modes and before anything changes: the approximate mode computes
/// gradients directly, so a label outside `binary:logistic`'s `[0, 1]` would
/// otherwise slip through.
#[test]
fn updates_refuse_labels_retraining_refuses() {
    let p = params(logistic());
    let train_data = data(300, 10, true);
    let bad = DMatrix::from_dense(&[0.5; COLS], 1, COLS)
        .unwrap()
        .with_labels(&[2.0])
        .unwrap();
    let good = data(5, 11, true);
    for mode in [
        OnlineParams::approximate(0.1).unwrap(),
        OnlineParams::exact(),
    ] {
        let mut online = OnlineModel::train(&p, &train_data, 8, mode).unwrap();
        let before = online.model().encode(ModelFormat::Json).unwrap();
        // Retraining refuses the added row's label under the same name.
        let retrain_err = invalid_data(train(&p, &bad, 8));
        assert_eq!(
            invalid_data(online.update(Some(&bad), &[0])),
            retrain_err,
            "{mode:?}"
        );
        assert_eq!(online.model().encode(ModelFormat::Json).unwrap(), before);
        assert_eq!(online.data().n_rows(), 300);
        // A later valid update still works, as on a fresh model.
        online.update(Some(&good), &[0]).unwrap();
        assert_eq!(online.data().n_rows(), 304);
        let mut fresh = OnlineModel::train(&p, &train_data, 8, mode).unwrap();
        fresh.update(Some(&good), &[0]).unwrap();
        assert_eq!(
            online.model().encode(ModelFormat::Json).unwrap(),
            fresh.model().encode(ModelFormat::Json).unwrap()
        );
    }
}

#[test]
fn unsound_configurations_and_changes_are_refused() {
    let train_data = data(200, 9, false);
    let base = || {
        TrainingParams::builder()
            .tree_method(TreeMethod::Hist)
            .max_depth(3)
    };
    let online = OnlineParams::default();
    for (params, name) in [
        (base().subsample(0.8).build().unwrap(), "subsample"),
        (base().colsample_bytree(0.5).build().unwrap(), "subsample"),
        (
            base()
                .grow_policy(GrowPolicy::LossGuide)
                .max_leaves(8)
                .build()
                .unwrap(),
            "grow_policy",
        ),
        (base().max_leaves(8).build().unwrap(), "grow_policy"),
        (base().unlimited_depth().build().unwrap(), "grow_policy"),
        (
            base()
                .booster(BoosterKind::Dart(Dart::default()))
                .build()
                .unwrap(),
            "booster",
        ),
        (
            base().num_parallel_tree(2).build().unwrap(),
            "num_parallel_tree",
        ),
        (
            base().objective(Objective::AbsoluteError).build().unwrap(),
            "objective",
        ),
        (
            base()
                .objective(Objective::custom(CustomLoss::new(
                    "custom:sqerr",
                    1,
                    |p, y, _w, out| {
                        for (o, (p, y)) in out.iter_mut().zip(p.iter().zip(y)) {
                            *o = GradPair::new(p - y, 1.0);
                        }
                    },
                )))
                .build()
                .unwrap(),
            "objective",
        ),
        // SGLB draws fresh noise for every row each round, and model
        // shrinkage rescales every earlier tree: neither replays in place.
        (
            base().langevin(Langevin::default()).build().unwrap(),
            "params",
        ),
        (
            base()
                .model_shrink(ModelShrink::new(0.1, ModelShrinkMode::Constant).unwrap())
                .build()
                .unwrap(),
            "params",
        ),
        (base().posterior_sampling(true).build().unwrap(), "params"),
    ] {
        assert_eq!(
            invalid_param(OnlineModel::train(&params, &train_data, 3, online)),
            name
        );
    }
    let p = base().build().unwrap();
    // The exact mode is `exact()`, never a tolerance of 0.
    for tolerance in [0.0, -0.1, 1.5, f64::NAN] {
        assert_eq!(
            invalid_param(OnlineParams::approximate(tolerance)),
            "tolerance"
        );
    }
    assert_eq!(OnlineParams::exact().mode(), OnlineMode::Exact);
    let weighted = data(200, 9, false).with_weights(&[1.0; 200]).unwrap();
    assert_eq!(
        invalid_data(OnlineModel::train(&p, &weighted, 3, online)),
        ("data", None)
    );
    let categorical = data(200, 9, false)
        .with_feature_types(&[FeatureType::Numerical; COLS])
        .unwrap();
    assert!(OnlineModel::train(&p, &categorical, 3, online).is_ok());

    let mut model = OnlineModel::train(&p, &train_data, 3, online).unwrap();
    assert_eq!(invalid_param(model.update(None, &[200])), "deletions");
    assert_eq!(invalid_param(model.update(None, &[4, 4])), "deletions");
    let all: Vec<usize> = (0..200).collect();
    assert_eq!(invalid_param(model.update(None, &all)), "deletions");
    let unlabelled = DMatrix::from_dense(&[0.0; COLS], 1, COLS).unwrap();
    assert_eq!(
        invalid_data(model.update(Some(&unlabelled), &[])),
        ("additions", None)
    );
    let narrow = DMatrix::from_dense(&[0.0; 2], 1, 2)
        .unwrap()
        .with_labels(&[1.0])
        .unwrap();
    assert_eq!(
        invalid_data(model.update(Some(&narrow), &[])),
        ("additions", None)
    );
}

/// LightGBM's class-balanced and query-level bagging draw a fresh per-class
/// or per-query row sample every round, which an in-place update cannot
/// replay: both are refused by `train` and `from_model` at either
/// tolerance, in otherwise valid configurations (a `binary:*` objective for
/// the first; `rank:*` with query groups for the second).
#[test]
fn row_bagging_is_refused() {
    let balanced = TrainingParams::builder()
        .objective(logistic())
        .tree_method(TreeMethod::Hist)
        .max_depth(3)
        .balanced_bagging(BalancedBagging::new(0.5, 0.8).unwrap())
        .build()
        .unwrap();
    let binary = data(200, 9, true);
    let ranking = TrainingParams::builder()
        .objective(Objective::RankPairwise(LambdaRank::default()))
        .tree_method(TreeMethod::Hist)
        .max_depth(3)
        .bagging_by_query(QueryBagging::new(0.5).unwrap())
        .build()
        .unwrap();
    let queries = data(200, 9, true).with_group_sizes(&[50; 4]).unwrap();
    // Both configurations train normally.
    assert!(train(&balanced, &binary, 3).is_ok());
    assert!(train(&ranking, &queries, 3).is_ok());
    let model = train(&params(logistic()), &binary, 3).unwrap();
    for online in [
        OnlineParams::approximate(0.1).unwrap(),
        OnlineParams::exact(),
    ] {
        for (p, d) in [(&balanced, &binary), (&ranking, &queries)] {
            assert_eq!(
                invalid_param(OnlineModel::train(p, d, 3, online)),
                "params",
                "train, {online:?}"
            );
            assert_eq!(
                invalid_param(OnlineModel::from_model(model.clone(), p, d, online)),
                "params",
                "from_model, {online:?}"
            );
        }
    }
}

/// Models whose trees updates cannot replay are refused: linear leaves
/// (an imported LightGBM `linear_tree` model) have no Newton-step leaf to
/// recompute.
#[test]
fn from_model_refuses_linear_leaves() {
    let path = concat!(
        env!("CARGO_MANIFEST_DIR"),
        "/tests/data/lightgbm-4.7.0-linear.txt"
    );
    let model = BoostedModel::load(path, ModelFormat::LightgbmText).unwrap();
    assert_eq!(model.n_features(), 6);
    let mut next = lcg(12);
    let x: Vec<f32> = (0..200 * 6).map(|_| next()).collect();
    let y: Vec<f32> = x.chunks(6).map(|r| r[0] - r[1]).collect();
    let data = DMatrix::from_dense(&x, 200, 6)
        .unwrap()
        .with_labels(&y)
        .unwrap();
    let p = params(Objective::SquaredError(RegLoss::default()));
    for online in [
        OnlineParams::approximate(0.1).unwrap(),
        OnlineParams::exact(),
    ] {
        assert_eq!(
            incompatible_model(OnlineModel::from_model(model.clone(), &p, &data, online)),
            "model"
        );
        // A model of these parameters on this data is accepted.
        let trained = train(&p, &data, 3).unwrap();
        assert!(OnlineModel::from_model(trained, &p, &data, online).is_ok());
    }
}

/// A model trained with model shrinkage is refused even under plain
/// parameters: every round rescaled the trees before it, so updating one
/// node's subtree cannot reproduce a retrain.
#[test]
fn from_model_refuses_shrunk_models() {
    let data = data(200, 5, false);
    let p = params(Objective::SquaredError(RegLoss::default()));
    let mut shrunk = p.clone();
    shrunk.model_shrink = Some(ModelShrink::new(0.1, ModelShrinkMode::Constant).unwrap());
    let model = train(&shrunk, &data, 3).unwrap();
    for online in [
        OnlineParams::approximate(0.1).unwrap(),
        OnlineParams::exact(),
    ] {
        assert_eq!(
            incompatible_model(OnlineModel::from_model(model.clone(), &p, &data, online)),
            "model"
        );
    }
}
/// A model with splits deeper than `max_depth` is not one `params` trained:
/// regrowing below such a split would have no depth left.
#[test]
fn from_model_refuses_trees_deeper_than_max_depth() {
    let d = data(200, 14, false);
    let p = params(Objective::SquaredError(RegLoss::default()));
    let mut deeper = p.clone();
    deeper.max_depth = p.max_depth.and_then(|depth| depth.checked_add(2));
    let model = train(&deeper, &d, 3).unwrap();
    for online in [
        OnlineParams::approximate(0.1).unwrap(),
        OnlineParams::exact(),
    ] {
        assert_eq!(
            incompatible_model(OnlineModel::from_model(model.clone(), &p, &d, online)),
            "model"
        );
        assert!(OnlineModel::from_model(model.clone(), &deeper, &d, online).is_ok());
    }
}

/// An abandoned approximate update (a break from the hook, or a refused
/// commit) restores the exact update state it started from, the fixed bins
/// and lazily kept gradients of earlier updates included: later updates
/// then equal those of a copy that never tried it.
#[test]
fn an_abandoned_update_keeps_the_update_state() {
    let p = params(Objective::SquaredError(RegLoss::default()));
    let train_data = data(600, 13, false);
    let (a, b, c) = (
        data(30, 14, false),
        data(20, 15, false),
        data(25, 16, false),
    );
    for online in [
        OnlineParams::approximate(0.1).unwrap(),
        OnlineParams::exact(),
    ] {
        let mut online = OnlineModel::train(&p, &train_data, 10, online).unwrap();
        online.update(Some(&a), &[0, 5, 9]).unwrap();
        let mut control = online.clone();
        let stop = |round: RoundEval| {
            if round.iteration() == 4 {
                ControlFlow::Break(())
            } else {
                ControlFlow::Continue(())
            }
        };
        assert_eq!(
            invalid_param(online.update_with(Some(&b), &[1, 2], stop)),
            "on_round"
        );
        let refused = online.update_with_commit(
            Some(&b),
            &[1, 2],
            |_| ControlFlow::Continue(()),
            || ControlFlow::Break(()),
        );
        assert_eq!(invalid_param(refused), "on_round");
        assert_eq!(
            online.model().encode(ModelFormat::Json).unwrap(),
            control.model().encode(ModelFormat::Json).unwrap()
        );
        for (additions, deletions) in [(Some(&c), vec![3, 7]), (None, vec![0, 1, 40])] {
            let report = online.update(additions, &deletions).unwrap();
            assert_eq!(report, control.update(additions, &deletions).unwrap());
            assert_eq!(
                online.model().encode(ModelFormat::Json).unwrap(),
                control.model().encode(ModelFormat::Json).unwrap(),
                "{online:?}"
            );
        }
    }
}

/// Labelled dense rows, with `NaN` as missing.
fn rows(x: &[f32], cols: usize, y: &[f32]) -> DMatrix {
    DMatrix::from_dense(x, y.len(), cols)
        .unwrap()
        .with_labels(y)
        .unwrap()
}

/// One-step trees without shrinkage or L2 penalty, so leaf values are the
/// plain means the tests reason about.
fn plain(max_depth: usize) -> TrainingParams {
    TrainingParams::builder()
        .tree_method(TreeMethod::Hist)
        .max_depth(max_depth)
        .eta(1.0)
        .lambda(0.0)
        .base_score(0.0)
        .build()
        .unwrap()
}

/// `from_model` refuses what `params` would not have trained, or training
/// would refuse: an early-stopped model (an update would predict with every
/// iteration and drop `best_iteration`; its best-iterations slice is fine),
/// another `max_delta_step` than the parameters', and a metric the
/// objective cannot score.
#[test]
fn from_model_refuses_models_and_metrics_training_would_not_give() {
    let d = data(300, 11, false);
    let valid = data(100, 12, false);
    let p = params(Objective::SquaredError(RegLoss::default()));
    let stopped = Trainer::new(&p, &d, 200)
        .eval(&valid, "valid")
        .early_stopping_rounds(NonZeroUsize::new(2).unwrap())
        .train()
        .unwrap()
        .model;
    let best = stopped.best_iteration().expect("stops early");
    for online in [OnlineParams::default(), OnlineParams::exact()] {
        assert_eq!(
            incompatible_model(OnlineModel::from_model(stopped.clone(), &p, &d, online)),
            "model"
        );
        let best_slice = stopped.slice(..=best, 1).unwrap();
        assert!(OnlineModel::from_model(best_slice, &p, &d, online).is_ok());
    }

    let counts = rows(&[0.0, 0.0, 1.0, 1.0], 1, &[0.0, 0.0, 1.0, 1.0]);
    let poisson = TrainingParams::builder()
        .objective(Objective::Poisson)
        .tree_method(TreeMethod::Hist)
        .max_depth(1)
        .build()
        .unwrap();
    let model = train(&poisson, &counts, 2).unwrap();
    let mut unbounded = poisson.clone();
    unbounded.max_delta_step = MaxDeltaStep::Unbounded;
    assert_eq!(
        incompatible_model(OnlineModel::from_model(
            model.clone(),
            &unbounded,
            &counts,
            OnlineParams::default()
        )),
        "model"
    );
    assert!(OnlineModel::from_model(model, &poisson, &counts, OnlineParams::default()).is_ok());

    let binary = data(100, 13, true);
    let logistic_params = params(logistic());
    let model = train(&logistic_params, &binary, 2).unwrap();
    let mut multiclass_metric = logistic_params.clone();
    multiclass_metric.eval_metric = vec![EvalMetric::MLogLoss];
    assert_eq!(
        invalid_param(OnlineModel::from_model(
            model,
            &multiclass_metric,
            &binary,
            OnlineParams::default()
        )),
        "eval_metric"
    );
}

/// The exact mode retrains, so it accepts categorical features (and a
/// model with categorical splits) and equals retraining; the approximate
/// mode, which replays numeric splits, refuses them.
#[test]
fn the_exact_mode_updates_categorical_models() {
    let d = rows(
        &[0.0, 0.0, 1.0, 1.0, 2.0, 2.0],
        1,
        &[0.0, 0.0, 1.0, 1.0, 3.0, 3.0],
    )
    .with_feature_types(&[FeatureType::Categorical])
    .unwrap();
    let p = plain(2);
    let mut exact = OnlineModel::train(&p, &d, 2, OnlineParams::exact()).unwrap();
    assert!(
        exact.model().trees()[0]
            .nodes()
            .iter()
            .any(|n| n.is_categorical)
    );
    let added = rows(&[1.0], 1, &[2.0])
        .with_feature_types(&[FeatureType::Categorical])
        .unwrap();
    exact.update(Some(&added), &[0]).unwrap();
    assert_eq!(
        exact.model().trees(),
        train(&p, exact.data(), 2).unwrap().trees()
    );
    assert_eq!(
        invalid_param(OnlineModel::train(&p, &d, 2, OnlineParams::default())),
        "data"
    );
}

/// An update refuses data whose intercept retraining could not estimate
/// (here `count:poisson` with only zero labels left), and changes nothing.
#[test]
fn updates_refuse_data_without_a_finite_intercept() {
    let poisson = TrainingParams::builder()
        .objective(Objective::Poisson)
        .tree_method(TreeMethod::Hist)
        .max_depth(1)
        .build()
        .unwrap();
    let counts = rows(&[0.0, 1.0], 1, &[0.0, 1.0]);
    for online_params in [OnlineParams::default(), OnlineParams::exact()] {
        let mut online = OnlineModel::train(&poisson, &counts, 2, online_params).unwrap();
        assert_eq!(
            invalid_data(train(&poisson, &rows(&[0.0], 1, &[0.0]), 2)),
            ("labels", None)
        );
        assert_eq!(invalid_data(online.update(None, &[1])), ("labels", None));
        assert_eq!(online.data().n_rows(), 2);
    }
}

/// Updates run on `nthread` threads, as training does, whatever pool the
/// caller runs them in.
#[test]
fn updates_run_on_the_configured_threads() {
    let mut p = params(Objective::SquaredError(RegLoss::default()));
    p.nthread = std::num::NonZeroUsize::new(1);
    let d = data(200, 14, false);
    with_threads(4, || {
        let mut online = OnlineModel::train(&p, &d, 3, OnlineParams::default()).unwrap();
        let mut seen = Vec::new();
        online
            .update_with(None, &[0], |_| {
                seen.push(rayon::current_num_threads());
                ControlFlow::Continue(())
            })
            .unwrap();
        assert_eq!(seen, vec![1; 3]);
    });
}

/// A changed row may reach a split that no row of the cached data reached
/// (a model resumed on other data, or a node of a regrown subtree the rows
/// route around): its histogram is all zero, and the update proceeds.
#[test]
fn updates_reach_splits_the_cached_rows_never_did() {
    let nan = f32::NAN;
    let p = plain(2);
    let full = rows(
        &[nan, 0.0, nan, 1.0, 0.0, 0.0, 1.0, 1.0],
        2,
        &[0.0, 1.0, 10.0, 11.0],
    );
    let model = train(&p, &full, 1).unwrap();
    let observed = rows(&[0.0, 0.0, 1.0, 1.0], 2, &[10.0, 11.0]);
    let online = OnlineParams::approximate(1.0).unwrap();
    let mut resumed = OnlineModel::from_model(model, &p, &observed, online).unwrap();
    let missing = rows(&[nan, 0.0], 2, &[0.0]);
    assert!(resumed.update(Some(&missing), &[]).is_ok());

    let start = rows(
        &[0.0, 0.0, 1.0, 1.0, nan, 0.0, nan, 1.0],
        2,
        &[0.0, 10.0, 1.0, 11.0],
    );
    let mut online_model = OnlineModel::train(&p, &start, 1, online).unwrap();
    let replacement = rows(
        &[1.0, 0.0, 1.0, 1.0, nan, 0.0, nan, 1.0],
        2,
        &[0.0, 1.0, 10.0, 11.0],
    );
    online_model
        .update(Some(&replacement), &[0, 1, 2, 3])
        .unwrap();
    assert!(
        online_model
            .update(Some(&rows(&[0.0, 0.0], 2, &[0.0])), &[])
            .is_ok()
    );
    assert_eq!(online_model.data().n_rows(), 5);
}

/// The approximate mode keeps the training bins, so it refuses an added
/// value at or above a feature's top cut (it would be ranked in the last
/// bin but predicted right of a split there); the exact mode, which
/// retrains, accepts it, as it does values inside the bins.
#[test]
fn approximate_updates_refuse_values_beyond_the_training_bins() {
    let nan = f32::NAN;
    let mut p = plain(1);
    p.min_child_weight = 2.0;
    let d = rows(&[0.0, 1.0, nan, nan], 1, &[0.0, 0.0, 1.0, 1.0]);
    let beyond = rows(&[3.0], 1, &[0.0]);
    let mut approximate =
        OnlineModel::train(&p, &d, 1, OnlineParams::approximate(1.0).unwrap()).unwrap();
    let before = approximate.model().clone();
    assert_eq!(
        invalid_data(approximate.update(Some(&beyond), &[0])),
        ("additions", None)
    );
    assert_eq!(approximate.model().trees(), before.trees());
    assert_eq!(approximate.data().n_rows(), 4);
    assert!(
        approximate
            .update(Some(&rows(&[0.5], 1, &[0.0])), &[0])
            .is_ok()
    );
    let mut exact = OnlineModel::train(&p, &d, 1, OnlineParams::exact()).unwrap();
    exact.update(Some(&beyond), &[0]).unwrap();
    assert_eq!(
        exact.model().trees(),
        train(&p, exact.data(), 1).unwrap().trees()
    );
}

/// An update whose arithmetic overflows `f32` is refused, as training
/// refuses the model it would produce, and changes nothing.
#[test]
fn updates_that_overflow_are_refused_and_change_nothing() {
    let max = f32::MAX;
    let p = TrainingParams::builder()
        .tree_method(TreeMethod::Hist)
        .base_score(f64::from(max))
        .build()
        .unwrap();
    let mut online =
        OnlineModel::train(&p, &rows(&[0.0], 1, &[max]), 2, OnlineParams::default()).unwrap();
    let before = online.model().clone();
    let err = online
        .update(Some(&rows(&[0.0], 1, &[-max])), &[])
        .unwrap_err();
    assert!(matches!(err, HessboostError::ModelFormat(_)), "{err}");
    assert_eq!(online.model().trees(), before.trees());
    assert_eq!(online.data().n_rows(), 1);
}

/// The approximate mode refuses a model whose first-tree leaves are not the
/// parameters' Newton steps on the data: an imported LightGBM model (its
/// first tree carries the label average) or one trained with another `eta`.
/// A model the approximate mode itself produced, resumed on its data, is
/// accepted; the exact mode, which retrains, accepts all of them.
#[test]
fn approximate_from_model_refuses_leaves_other_parameters_made() {
    let text = "tree\nversion=v4\nnum_class=1\nnum_tree_per_iteration=1\nlabel_index=0\n\
                max_feature_idx=0\nobjective=regression\nfeature_names=x\nfeature_infos=none\n\n\
                Tree=0\nnum_leaves=1\nnum_cat=0\nleaf_value=5\nleaf_weight=4\nleaf_count=4\n\
                is_linear=0\nshrinkage=1\n\n\nend of trees\n";
    let imported = BoostedModel::decode(text, ModelFormat::LightgbmText).unwrap();
    let fives = rows(&[0.0, 1.0, 2.0, 3.0], 1, &[5.0; 4]);
    let mut p = plain(1);
    p.eta = 0.3;
    p.base_score = None;
    let approximate = OnlineParams::default();
    assert_eq!(
        incompatible_model(OnlineModel::from_model(
            imported.clone(),
            &p,
            &fives,
            approximate
        )),
        "model"
    );
    assert!(OnlineModel::from_model(imported, &p, &fives, OnlineParams::exact()).is_ok());

    let d = data(300, 21, false);
    let trained = params(Objective::SquaredError(RegLoss::default()));
    let model = train(&trained, &d, 5).unwrap();
    let mut other_eta = trained.clone();
    other_eta.eta = 0.5;
    assert_eq!(
        incompatible_model(OnlineModel::from_model(
            model.clone(),
            &other_eta,
            &d,
            approximate
        )),
        "model"
    );
    let mut online = OnlineModel::from_model(model, &trained, &d, approximate).unwrap();
    online
        .update(Some(&data(20, 22, false)), &[0, 5, 9])
        .unwrap();
    let saved = online.model().clone();
    assert!(OnlineModel::from_model(saved, &trained, online.data(), approximate).is_ok());
}

/// Rows whose gradients are refreshed after a regrowth get the updated
/// model's margins, accumulated tree by tree from the intercept as
/// prediction does. At a large intercept a sum of the trees first rounds
/// differently: here every row is fresh at the third tree, which must then
/// be the tree one round of training fits to the updated two-tree model's
/// residuals.
#[test]
fn refreshed_margins_follow_the_prediction_recurrence() {
    let mut p = plain(1);
    p.base_score = Some(1e8);
    let d = rows(&[0.0, 1.0, 2.0, 3.0], 1, &[0.0, 1.0, 2.0, 3.0]);
    let mut online =
        OnlineModel::train(&p, &d, 3, OnlineParams::approximate(0.01).unwrap()).unwrap();
    online.update(Some(&rows(&[0.0], 1, &[10.0])), &[]).unwrap();
    let data = online.data();
    let prefix = online
        .model()
        .slice(..2, 1)
        .unwrap()
        .predict(data, ..)
        .unwrap();
    let labels: Vec<f32> = data.labels().unwrap().to_vec();
    let x: Vec<f32> = (0..data.n_rows())
        .map(|i| data.get(i, 0).unwrap())
        .collect();
    let residuals: Vec<f32> = labels
        .iter()
        .zip(prefix.as_slice())
        .map(|(y, m)| y - m)
        .collect();
    let mut correction_params = plain(1);
    correction_params.base_score = Some(0.0);
    let correction = train(&correction_params, &rows(&x, 1, &residuals), 1)
        .unwrap()
        .predict(&rows(&x, 1, &residuals), ..)
        .unwrap();
    let expected: Vec<f32> = prefix
        .as_slice()
        .iter()
        .zip(correction.as_slice())
        .map(|(m, c)| m + c)
        .collect();
    assert_eq!(
        online.model().predict(data, ..).unwrap().as_slice(),
        expected
    );
}