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
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
//! PyO3 bindings for TreeBoost GBDT
//!
//! This module provides Python wrappers around the core Rust GBDT implementation,
//! exposing a Python-friendly API while maintaining Rust performance.
//!
//! # Example
//!
//! ```python
//! import numpy as np
//! from treeboost import GBDTConfig, GBDTModel
//!
//! # Create configuration with builder pattern (recommended)
//! config = (
//!     GBDTConfig()
//!     .with_num_rounds(100)
//!     .with_max_depth(6)
//!     .with_learning_rate(0.1)
//!     .with_backend("cuda")
//! )
//!
//! # Train model (features: 2D array, targets: 1D array)
//! model = GBDTModel.train(features, targets, config)
//!
//! # Predict
//! predictions = model.predict(features)
//!
//! # Save/load model
//! model.save("model.rkyv")
//! loaded = GBDTModel.load("model.rkyv")
//! ```

use numpy::{PyArray1, PyReadonlyArray1, PyReadonlyArray2};
use pyo3::exceptions::{PyIOError, PyValueError};
use pyo3::prelude::*;

use crate::backend::{BackendType, GpuMode};
use crate::booster::{GBDTConfig, GBDTModel, GbdtPreset, LossType};
use crate::serialize;
use crate::tree::MonotonicConstraint;
use crate::tuner::ModelFormat;

/// Extract features from a 2D numpy array (f64 or f32) into a flat Vec<f64>
///
/// Returns (num_features, flattened_row_major_data)
fn extract_features_array<'py>(features: &Bound<'py, PyAny>) -> PyResult<(usize, Vec<f64>)> {
    // Try f64 first (most common for numpy), then f32
    if let Ok(arr) = features.extract::<PyReadonlyArray2<'py, f64>>() {
        let arr = arr.as_array();
        let num_rows = arr.nrows();
        let num_cols = arr.ncols();
        let mut raw = Vec::with_capacity(num_rows * num_cols);
        for row in arr.rows() {
            raw.extend(row.iter().copied());
        }
        Ok((num_cols, raw))
    } else if let Ok(arr) = features.extract::<PyReadonlyArray2<'py, f32>>() {
        let arr = arr.as_array();
        let num_rows = arr.nrows();
        let num_cols = arr.ncols();
        let mut raw = Vec::with_capacity(num_rows * num_cols);
        for row in arr.rows() {
            raw.extend(row.iter().map(|&v| v as f64));
        }
        Ok((num_cols, raw))
    } else {
        Err(PyValueError::new_err(
            "features must be a 2D numpy array of float32 or float64",
        ))
    }
}

/// Validate that extracted features match the expected count
fn validate_feature_count(actual: usize, expected: usize) -> PyResult<()> {
    if actual != expected {
        return Err(PyValueError::new_err(format!(
            "Expected {} features, got {}",
            expected, actual
        )));
    }
    Ok(())
}

/// Parse a model format string into ModelFormat enum
fn parse_model_format(format: &str) -> PyResult<ModelFormat> {
    match format.to_lowercase().as_str() {
        "rkyv" => Ok(ModelFormat::Rkyv),
        "bincode" | "bin" => Ok(ModelFormat::Bincode),
        _ => Err(PyValueError::new_err("format must be 'rkyv' or 'bincode'")),
    }
}

/// Python wrapper for monotonic constraint
///
/// Controls the direction of feature effects on predictions.
///
/// Example:
/// ```python
/// from treeboost import MonotonicConstraint
///
/// # Feature 0: increasing, Feature 1: decreasing, Feature 2: none
/// constraints = [
///     MonotonicConstraint.increasing(),
///     MonotonicConstraint.decreasing(),
///     MonotonicConstraint.none()
/// ]
/// config = config.with_monotonic_constraints(constraints)
/// ```
#[pyclass(name = "MonotonicConstraint", eq)]
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct PyMonotonicConstraint {
    inner: MonotonicConstraint,
}

#[pymethods]
impl PyMonotonicConstraint {
    /// Create an increasing constraint (larger values -> larger predictions)
    #[staticmethod]
    fn increasing() -> Self {
        Self {
            inner: MonotonicConstraint::Increasing,
        }
    }

    /// Create a decreasing constraint (larger values -> smaller predictions)
    #[staticmethod]
    fn decreasing() -> Self {
        Self {
            inner: MonotonicConstraint::Decreasing,
        }
    }

    /// Create no constraint (no monotonic restriction)
    #[staticmethod]
    fn none() -> Self {
        Self {
            inner: MonotonicConstraint::None,
        }
    }

    /// Check if this is an increasing constraint
    #[getter]
    fn is_increasing(&self) -> bool {
        matches!(self.inner, MonotonicConstraint::Increasing)
    }

    /// Check if this is a decreasing constraint
    #[getter]
    fn is_decreasing(&self) -> bool {
        matches!(self.inner, MonotonicConstraint::Decreasing)
    }

    /// Check if this is no constraint
    #[getter]
    fn is_none(&self) -> bool {
        matches!(self.inner, MonotonicConstraint::None)
    }

    /// Convert to integer value (1 = increasing, -1 = decreasing, 0 = none)
    fn to_int(&self) -> i32 {
        match self.inner {
            MonotonicConstraint::Increasing => 1,
            MonotonicConstraint::Decreasing => -1,
            MonotonicConstraint::None => 0,
        }
    }

    fn __repr__(&self) -> &'static str {
        match self.inner {
            MonotonicConstraint::Increasing => "MonotonicConstraint.increasing()",
            MonotonicConstraint::Decreasing => "MonotonicConstraint.decreasing()",
            MonotonicConstraint::None => "MonotonicConstraint.none()",
        }
    }
}

impl From<MonotonicConstraint> for PyMonotonicConstraint {
    fn from(mc: MonotonicConstraint) -> Self {
        Self { inner: mc }
    }
}

impl From<PyMonotonicConstraint> for MonotonicConstraint {
    fn from(pmc: PyMonotonicConstraint) -> Self {
        pmc.inner
    }
}

/// Python wrapper for GBDT training configuration
#[pyclass(name = "GBDTConfig")]
#[derive(Clone)]
pub struct PyGBDTConfig {
    inner: GBDTConfig,
}

#[pymethods]
impl PyGBDTConfig {
    /// Create a new configuration with default values
    #[new]
    fn new() -> Self {
        Self {
            inner: GBDTConfig::default(),
        }
    }

    /// Create a new configuration from a preset name.
    ///
    /// Valid presets: standard, speed, accuracy, smalldata, largedata, conformal.
    #[staticmethod]
    fn preset(preset: &str) -> PyResult<Self> {
        let preset = match preset.to_lowercase().as_str() {
            "standard" => GbdtPreset::Standard,
            "speed" => GbdtPreset::Speed,
            "accuracy" => GbdtPreset::Accuracy,
            "smalldata" | "small_data" => GbdtPreset::SmallData,
            "largedata" | "large_data" => GbdtPreset::LargeData,
            "conformal" => GbdtPreset::Conformal,
            _ => {
                return Err(pyo3::exceptions::PyValueError::new_err(
                    "unknown preset (use: standard, speed, accuracy, smalldata, largedata, conformal)",
                ));
            }
        };
        Ok(Self {
            inner: GBDTConfig::default().with_preset(preset),
        })
    }

    /// Return a new config with the given preset applied.
    ///
    /// Valid presets: standard, speed, accuracy, smalldata, largedata, conformal.
    fn with_preset(&self, preset: &str) -> PyResult<Self> {
        let mut inner = self.inner.clone();
        inner = match preset.to_lowercase().as_str() {
            "standard" => inner.with_preset(GbdtPreset::Standard),
            "speed" => inner.with_preset(GbdtPreset::Speed),
            "accuracy" => inner.with_preset(GbdtPreset::Accuracy),
            "smalldata" | "small_data" => inner.with_preset(GbdtPreset::SmallData),
            "largedata" | "large_data" => inner.with_preset(GbdtPreset::LargeData),
            "conformal" => inner.with_preset(GbdtPreset::Conformal),
            _ => {
                return Err(pyo3::exceptions::PyValueError::new_err(
                    "unknown preset (use: standard, speed, accuracy, smalldata, largedata, conformal)",
                ));
            }
        };
        Ok(Self { inner })
    }

    // Ensemble parameters

    /// Number of boosting rounds (trees)
    #[getter]
    fn num_rounds(&self) -> usize {
        self.inner.num_rounds
    }

    #[setter]
    fn set_num_rounds(&mut self, value: usize) {
        self.inner.num_rounds = value;
    }

    /// Learning rate (optimization step size for trees)
    #[getter]
    fn learning_rate(&self) -> f32 {
        self.inner.learning_rate
    }

    #[setter]
    fn set_learning_rate(&mut self, value: f32) {
        self.inner.learning_rate = value;
    }

    // Tree parameters

    /// Maximum depth of each tree
    #[getter]
    fn max_depth(&self) -> usize {
        self.inner.max_depth
    }

    #[setter]
    fn set_max_depth(&mut self, value: usize) {
        self.inner.max_depth = value;
    }

    /// Maximum number of leaves per tree
    #[getter]
    fn max_leaves(&self) -> usize {
        self.inner.max_leaves
    }

    #[setter]
    fn set_max_leaves(&mut self, value: usize) {
        self.inner.max_leaves = value;
    }

    /// Minimum samples required in a leaf
    #[getter]
    fn min_samples_leaf(&self) -> usize {
        self.inner.min_samples_leaf
    }

    #[setter]
    fn set_min_samples_leaf(&mut self, value: usize) {
        self.inner.min_samples_leaf = value;
    }

    /// Minimum hessian sum required in a leaf
    #[getter]
    fn min_hessian_leaf(&self) -> f32 {
        self.inner.min_hessian_leaf
    }

    #[setter]
    fn set_min_hessian_leaf(&mut self, value: f32) {
        self.inner.min_hessian_leaf = value;
    }

    /// Minimum gain to make a split
    #[getter]
    fn min_gain(&self) -> f32 {
        self.inner.min_gain
    }

    #[setter]
    fn set_min_gain(&mut self, value: f32) {
        self.inner.min_gain = value;
    }

    // Regularization

    /// L2 regularization (lambda)
    #[getter]
    fn lambda_(&self) -> f32 {
        self.inner.lambda
    }

    #[setter]
    fn set_lambda(&mut self, value: f32) {
        self.inner.lambda = value;
    }

    /// Shannon Entropy regularization weight (beta)
    #[getter]
    fn entropy_weight(&self) -> f32 {
        self.inner.entropy_weight
    }

    #[setter]
    fn set_entropy_weight(&mut self, value: f32) {
        self.inner.entropy_weight = value;
    }

    // Loss function

    /// Set loss function to MSE
    fn use_mse_loss(&mut self) {
        self.inner.loss_type = LossType::Mse;
    }

    /// Set loss function to Pseudo-Huber with given delta
    fn use_pseudo_huber_loss(&mut self, delta: f32) {
        self.inner.loss_type = LossType::PseudoHuber { delta };
    }

    /// Set loss function to Binary Log Loss (for binary classification)
    ///
    /// Uses sigmoid activation for probability output.
    /// Targets should be 0 or 1.
    fn use_binary_logloss(&mut self) {
        self.inner.loss_type = LossType::BinaryLogLoss;
    }

    /// Set loss function to Multi-class Log Loss (for multi-class classification)
    ///
    /// Uses softmax activation for probability output.
    /// Targets should be class indices: 0, 1, 2, ..., num_classes-1.
    ///
    /// This trains K trees per round (one per class) and combines predictions
    /// via softmax for final class probabilities.
    ///
    /// Args:
    ///     num_classes: Number of classes (K), must be >= 2
    fn use_multiclass_logloss(&mut self, num_classes: usize) -> PyResult<()> {
        if num_classes < 2 {
            return Err(PyValueError::new_err("num_classes must be >= 2"));
        }
        self.inner.loss_type = LossType::MultiClassLogLoss { num_classes };
        Ok(())
    }

    // Subsampling

    /// Row subsampling ratio (0.0-1.0)
    #[getter]
    fn subsample(&self) -> f32 {
        self.inner.subsample
    }

    #[setter]
    fn set_subsample(&mut self, value: f32) {
        self.inner.subsample = value;
    }

    /// Column subsampling ratio (0.0-1.0)
    #[getter]
    fn colsample(&self) -> f32 {
        self.inner.colsample
    }

    #[setter]
    fn set_colsample(&mut self, value: f32) {
        self.inner.colsample = value;
    }

    // Binning

    /// Number of histogram bins
    #[getter]
    fn num_bins(&self) -> usize {
        self.inner.num_bins
    }

    #[setter]
    fn set_num_bins(&mut self, value: usize) {
        self.inner.num_bins = value;
    }

    // Conformal prediction

    /// Calibration set ratio for conformal prediction (0.0 to disable)
    #[getter]
    fn calibration_ratio(&self) -> f32 {
        self.inner.calibration_ratio
    }

    #[setter]
    fn set_calibration_ratio(&mut self, value: f32) {
        self.inner.calibration_ratio = value;
    }

    /// Conformal prediction quantile (e.g., 0.9 for 90% coverage)
    #[getter]
    fn conformal_quantile(&self) -> f32 {
        self.inner.conformal_quantile
    }

    #[setter]
    fn set_conformal_quantile(&mut self, value: f32) {
        self.inner.conformal_quantile = value;
    }

    // Early stopping

    /// Number of rounds with no improvement before stopping (0 to disable)
    #[getter]
    fn early_stopping_rounds(&self) -> usize {
        self.inner.early_stopping_rounds
    }

    #[setter]
    fn set_early_stopping_rounds(&mut self, value: usize) {
        self.inner.early_stopping_rounds = value;
    }

    /// Ratio of data to use for validation (0.0 to disable early stopping)
    #[getter]
    fn validation_ratio(&self) -> f32 {
        self.inner.validation_ratio
    }

    #[setter]
    fn set_validation_ratio(&mut self, value: f32) {
        self.inner.validation_ratio = value;
    }

    // Performance optimizations

    /// Use parallel prediction via Rayon (default: true)
    #[getter]
    fn parallel_prediction(&self) -> bool {
        self.inner.parallel_prediction
    }

    #[setter]
    fn set_parallel_prediction(&mut self, value: bool) {
        self.inner.parallel_prediction = value;
    }

    /// Reorder columns by feature importance for cache locality (default: true)
    #[getter]
    fn column_reordering(&self) -> bool {
        self.inner.column_reordering
    }

    #[setter]
    fn set_column_reordering(&mut self, value: bool) {
        self.inner.column_reordering = value;
    }

    /// Use 4-bit packing for low-cardinality features (default: true)
    #[getter]
    fn packed_dataset(&self) -> bool {
        self.inner.packed_dataset
    }

    #[setter]
    fn set_packed_dataset(&mut self, value: bool) {
        self.inner.packed_dataset = value;
    }

    /// Use parallel gradient computation (default: false)
    /// Experimental: may not provide stable speedups, benchmark before enabling
    #[getter]
    fn parallel_gradient(&self) -> bool {
        self.inner.parallel_gradient
    }

    #[setter]
    fn set_parallel_gradient(&mut self, value: bool) {
        self.inner.parallel_gradient = value;
    }

    // Backend selection

    /// Enable GPU subgroup operations (default: True for Python)
    ///
    /// Subgroups can reduce atomic contention in GPU histogram building.
    /// Benchmarks show minimal benefit on modern NVIDIA GPUs (~1.0x),
    /// but may help on older AMD or Intel GPUs.
    #[getter]
    fn use_gpu_subgroups(&self) -> bool {
        self.inner.use_gpu_subgroups
    }

    #[setter]
    fn set_use_gpu_subgroups(&mut self, value: bool) {
        self.inner.use_gpu_subgroups = value;
    }

    /// Get current backend type as string
    #[getter]
    fn backend(&self) -> &'static str {
        match self.inner.backend_type {
            BackendType::Auto => "auto",
            BackendType::Scalar => "cpu",
            BackendType::Wgpu => "wgpu",
            BackendType::Avx512 => "avx512",
            BackendType::Sve2 => "sve2",
            BackendType::Cuda => "cuda",
            BackendType::Rocm => "rocm",
            BackendType::Metal => "metal",
        }
    }

    /// Set the backend for histogram building
    ///
    /// Args:
    ///     value: One of "auto", "cpu", "gpu", "wgpu", "cuda"
    ///         - "auto": Select best available (CUDA > WGPU > CPU)
    ///         - "cpu": Force CPU (AVX2/NEON optimized)
    ///         - "gpu": Select best GPU (CUDA > WGPU), same as "auto"
    ///         - "wgpu": Force WGPU (all GPUs via Vulkan/Metal/DX12)
    ///         - "cuda": Force CUDA (NVIDIA only, fastest)
    ///
    /// Example:
    ///     config.backend = "cuda"
    #[setter]
    fn set_backend(&mut self, value: &str) -> PyResult<()> {
        self.inner.backend_type = match value.to_lowercase().as_str() {
            "auto" | "gpu" => BackendType::Auto,  // gpu = auto-select best GPU (CUDA > WGPU)
            "cpu" | "scalar" => BackendType::Scalar,
            "wgpu" => BackendType::Wgpu,
            "cuda" => BackendType::Cuda,
            "rocm" => BackendType::Rocm,
            "metal" => BackendType::Metal,
            _ => return Err(PyValueError::new_err(
                "backend must be one of: 'auto' (or 'gpu'), 'cpu' (or 'scalar'), 'wgpu', 'cuda', 'rocm', 'metal'"
            )),
        };
        Ok(())
    }

    /// Get current GPU mode as string
    #[getter]
    fn gpu_mode(&self) -> &'static str {
        match self.inner.gpu_mode {
            GpuMode::Auto => "auto",
            GpuMode::Hybrid => "hybrid",
            GpuMode::Full => "full",
        }
    }

    /// Set the GPU execution mode
    ///
    /// Args:
    ///     value: One of "auto", "hybrid", "full"
    ///         - "auto": Select optimal mode per backend (CUDA→Full, WGPU→Hybrid)
    ///         - "hybrid": GPU histogram + CPU partition (best-first tree growth)
    ///         - "full": Full GPU pipeline (level-wise tree growth)
    ///
    /// Example:
    ///     config.gpu_mode = "full"
    #[setter]
    fn set_gpu_mode(&mut self, value: &str) -> PyResult<()> {
        self.inner.gpu_mode = match value.to_lowercase().as_str() {
            "auto" => GpuMode::Auto,
            "hybrid" => GpuMode::Hybrid,
            "full" => GpuMode::Full,
            _ => {
                return Err(PyValueError::new_err(
                    "gpu_mode must be one of: 'auto', 'hybrid', 'full'",
                ))
            }
        };
        Ok(())
    }

    // Monotonic constraints

    /// Set monotonic constraints for features
    ///
    /// Args:
    ///     constraints: List of constraint values per feature
    ///         - 1 = Increasing (larger values -> larger predictions)
    ///         - -1 = Decreasing (larger values -> smaller predictions)
    ///         - 0 = None (no constraint)
    ///
    /// Example:
    ///     config.set_monotonic_constraints([1, -1, 0])  # Feature 0: inc, Feature 1: dec, Feature 2: none
    fn set_monotonic_constraints(&mut self, constraints: Vec<i32>) -> PyResult<()> {
        let parsed: Result<Vec<MonotonicConstraint>, _> = constraints
            .into_iter()
            .map(|c| match c {
                1 => Ok(MonotonicConstraint::Increasing),
                -1 => Ok(MonotonicConstraint::Decreasing),
                0 => Ok(MonotonicConstraint::None),
                _ => Err(PyValueError::new_err(
                    "Constraint must be 1 (increasing), -1 (decreasing), or 0 (none)",
                )),
            })
            .collect();

        self.inner.monotonic_constraints = parsed?;
        Ok(())
    }

    // Era splitting (Directional Era Splitting)

    /// Enable Directional Era Splitting (DES)
    ///
    /// When enabled, splits are only accepted if ALL eras agree on the
    /// split direction. This filters out spurious correlations that work
    /// in some time periods but not others.
    ///
    /// Requires passing era_indices to train_with_eras().
    #[getter]
    fn era_splitting(&self) -> bool {
        self.inner.era_splitting
    }

    #[setter]
    fn set_era_splitting(&mut self, value: bool) {
        self.inner.era_splitting = value;
    }

    // Interaction constraints

    /// Set feature interaction constraints
    ///
    /// Features in the same group can interact (appear together in a tree path).
    /// Features in different groups cannot be used together.
    /// Features not in any group can interact with all features.
    ///
    /// Args:
    ///     groups: List of feature index groups, e.g., [[0, 1, 2], [3, 4]]
    ///
    /// Example:
    ///     config.set_interaction_groups([[0, 1, 2], [3, 4]])
    fn set_interaction_groups(&mut self, groups: Vec<Vec<usize>>) {
        self.inner.interaction_groups = groups;
    }

    fn __repr__(&self) -> String {
        format!(
            "GBDTConfig(num_rounds={}, learning_rate={}, max_depth={}, max_leaves={}, backend='{}', gpu_mode='{}')",
            self.inner.num_rounds,
            self.inner.learning_rate,
            self.inner.max_depth,
            self.inner.max_leaves,
            self.backend(),
            self.gpu_mode()
        )
    }

    // ========== Builder Pattern Methods ==========
    // All return Self for method chaining (immutable builder pattern)

    /// Set number of boosting rounds (trees)
    fn with_num_rounds(&self, value: usize) -> PyResult<Self> {
        if value == 0 {
            return Err(PyValueError::new_err("num_rounds must be >= 1"));
        }
        let mut new = self.clone();
        new.inner.num_rounds = value;
        Ok(new)
    }

    /// Set learning rate (shrinkage)
    fn with_learning_rate(&self, value: f32) -> PyResult<Self> {
        if value <= 0.0 || value > 1.0 {
            return Err(PyValueError::new_err("learning_rate must be in (0.0, 1.0]"));
        }
        let mut new = self.clone();
        new.inner.learning_rate = value;
        Ok(new)
    }

    /// Set maximum depth of each tree
    fn with_max_depth(&self, value: usize) -> PyResult<Self> {
        if value == 0 {
            return Err(PyValueError::new_err("max_depth must be >= 1"));
        }
        let mut new = self.clone();
        new.inner.max_depth = value;
        Ok(new)
    }

    /// Set maximum number of leaves per tree
    fn with_max_leaves(&self, value: usize) -> PyResult<Self> {
        if value < 2 {
            return Err(PyValueError::new_err("max_leaves must be >= 2"));
        }
        let mut new = self.clone();
        new.inner.max_leaves = value;
        Ok(new)
    }

    /// Set minimum samples required in a leaf
    fn with_min_samples_leaf(&self, value: usize) -> PyResult<Self> {
        if value == 0 {
            return Err(PyValueError::new_err("min_samples_leaf must be >= 1"));
        }
        let mut new = self.clone();
        new.inner.min_samples_leaf = value;
        Ok(new)
    }

    /// Set minimum hessian sum required in a leaf
    fn with_min_hessian_leaf(&self, value: f32) -> PyResult<Self> {
        if value < 0.0 {
            return Err(PyValueError::new_err("min_hessian_leaf must be >= 0.0"));
        }
        let mut new = self.clone();
        new.inner.min_hessian_leaf = value;
        Ok(new)
    }

    /// Set minimum gain to make a split
    fn with_min_gain(&self, value: f32) -> PyResult<Self> {
        if value < 0.0 {
            return Err(PyValueError::new_err("min_gain must be >= 0.0"));
        }
        let mut new = self.clone();
        new.inner.min_gain = value;
        Ok(new)
    }

    /// Set L2 regularization (lambda)
    fn with_lambda(&self, value: f32) -> PyResult<Self> {
        if value < 0.0 {
            return Err(PyValueError::new_err("lambda must be >= 0.0"));
        }
        let mut new = self.clone();
        new.inner.lambda = value;
        Ok(new)
    }

    /// Set Shannon Entropy regularization weight (beta)
    fn with_entropy_weight(&self, value: f32) -> PyResult<Self> {
        if value < 0.0 {
            return Err(PyValueError::new_err("entropy_weight must be >= 0.0"));
        }
        let mut new = self.clone();
        new.inner.entropy_weight = value;
        Ok(new)
    }

    /// Set row subsampling ratio (0.0-1.0)
    fn with_subsample(&self, value: f32) -> PyResult<Self> {
        if value <= 0.0 || value > 1.0 {
            return Err(PyValueError::new_err("subsample must be in (0.0, 1.0]"));
        }
        let mut new = self.clone();
        new.inner.subsample = value;
        Ok(new)
    }

    /// Set column subsampling ratio (0.0-1.0)
    fn with_colsample(&self, value: f32) -> PyResult<Self> {
        if value <= 0.0 || value > 1.0 {
            return Err(PyValueError::new_err("colsample must be in (0.0, 1.0]"));
        }
        let mut new = self.clone();
        new.inner.colsample = value;
        Ok(new)
    }

    /// Set number of histogram bins
    fn with_num_bins(&self, value: usize) -> PyResult<Self> {
        if value < 2 || value > 255 {
            return Err(PyValueError::new_err("num_bins must be in [2, 255]"));
        }
        let mut new = self.clone();
        new.inner.num_bins = value;
        Ok(new)
    }

    /// Set calibration set ratio for conformal prediction (0.0 to disable)
    fn with_calibration_ratio(&self, value: f32) -> PyResult<Self> {
        if value < 0.0 || value >= 1.0 {
            return Err(PyValueError::new_err(
                "calibration_ratio must be in [0.0, 1.0)",
            ));
        }
        let mut new = self.clone();
        new.inner.calibration_ratio = value;
        Ok(new)
    }

    /// Set conformal prediction quantile (e.g., 0.9 for 90% coverage)
    fn with_conformal_quantile(&self, value: f32) -> PyResult<Self> {
        if value <= 0.0 || value >= 1.0 {
            return Err(PyValueError::new_err(
                "conformal_quantile must be in (0.0, 1.0)",
            ));
        }
        let mut new = self.clone();
        new.inner.conformal_quantile = value;
        Ok(new)
    }

    /// Set early stopping rounds (0 to disable)
    fn with_early_stopping_rounds(&self, value: usize) -> PyResult<Self> {
        let mut new = self.clone();
        new.inner.early_stopping_rounds = value;
        Ok(new)
    }

    /// Set validation ratio for early stopping (0.0 to disable)
    fn with_validation_ratio(&self, value: f32) -> PyResult<Self> {
        if value < 0.0 || value >= 1.0 {
            return Err(PyValueError::new_err(
                "validation_ratio must be in [0.0, 1.0)",
            ));
        }
        let mut new = self.clone();
        new.inner.validation_ratio = value;
        Ok(new)
    }

    /// Enable/disable parallel prediction
    fn with_parallel_prediction(&self, value: bool) -> PyResult<Self> {
        let mut new = self.clone();
        new.inner.parallel_prediction = value;
        Ok(new)
    }

    /// Enable/disable column reordering for cache locality
    fn with_column_reordering(&self, value: bool) -> PyResult<Self> {
        let mut new = self.clone();
        new.inner.column_reordering = value;
        Ok(new)
    }

    /// Enable/disable 4-bit packing for low-cardinality features
    fn with_packed_dataset(&self, value: bool) -> PyResult<Self> {
        let mut new = self.clone();
        new.inner.packed_dataset = value;
        Ok(new)
    }

    /// Enable/disable parallel gradient computation
    fn with_parallel_gradient(&self, value: bool) -> PyResult<Self> {
        let mut new = self.clone();
        new.inner.parallel_gradient = value;
        Ok(new)
    }

    /// Enable/disable GPU subgroup operations
    fn with_gpu_subgroups(&self, value: bool) -> PyResult<Self> {
        let mut new = self.clone();
        new.inner.use_gpu_subgroups = value;
        Ok(new)
    }

    /// Set backend type ("auto", "cpu", "gpu", "wgpu", "cuda", "rocm", "metal")
    fn with_backend(&self, value: &str) -> PyResult<Self> {
        let mut new = self.clone();
        new.inner.backend_type = match value.to_lowercase().as_str() {
            "auto" | "gpu" => BackendType::Auto,
            "cpu" | "scalar" => BackendType::Scalar,
            "wgpu" => BackendType::Wgpu,
            "cuda" => BackendType::Cuda,
            "rocm" => BackendType::Rocm,
            "metal" => BackendType::Metal,
            _ => return Err(PyValueError::new_err(
                "backend must be one of: 'auto' (or 'gpu'), 'cpu' (or 'scalar'), 'wgpu', 'cuda', 'rocm', 'metal'"
            )),
        };
        Ok(new)
    }

    /// Set GPU execution mode ("auto", "hybrid", "full")
    fn with_gpu_mode(&self, value: &str) -> PyResult<Self> {
        let mut new = self.clone();
        new.inner.gpu_mode = match value.to_lowercase().as_str() {
            "auto" => GpuMode::Auto,
            "hybrid" => GpuMode::Hybrid,
            "full" => GpuMode::Full,
            _ => {
                return Err(PyValueError::new_err(
                    "gpu_mode must be one of: 'auto', 'hybrid', 'full'",
                ))
            }
        };
        Ok(new)
    }

    /// Enable/disable Directional Era Splitting
    fn with_era_splitting(&self, value: bool) -> PyResult<Self> {
        let mut new = self.clone();
        new.inner.era_splitting = value;
        Ok(new)
    }

    /// Use MSE loss function
    fn with_mse_loss(&self) -> PyResult<Self> {
        let mut new = self.clone();
        new.inner.loss_type = LossType::Mse;
        Ok(new)
    }

    /// Use Pseudo-Huber loss function with given delta
    fn with_pseudo_huber_loss(&self, delta: f32) -> PyResult<Self> {
        if delta <= 0.0 {
            return Err(PyValueError::new_err("delta must be > 0.0"));
        }
        let mut new = self.clone();
        new.inner.loss_type = LossType::PseudoHuber { delta };
        Ok(new)
    }

    /// Use Binary Log Loss for binary classification
    fn with_binary_logloss(&self) -> PyResult<Self> {
        let mut new = self.clone();
        new.inner.loss_type = LossType::BinaryLogLoss;
        Ok(new)
    }

    /// Use Multi-class Log Loss for multi-class classification
    fn with_multiclass_logloss(&self, num_classes: usize) -> PyResult<Self> {
        if num_classes < 2 {
            return Err(PyValueError::new_err("num_classes must be >= 2"));
        }
        let mut new = self.clone();
        new.inner.loss_type = LossType::MultiClassLogLoss { num_classes };
        Ok(new)
    }

    /// Set monotonic constraints for features (using integers)
    ///
    /// Args:
    ///     constraints: List of constraint values per feature
    ///         - 1 = Increasing, -1 = Decreasing, 0 = None
    ///
    /// Note: Prefer with_constraints() with MonotonicConstraint enum for clearer code
    fn with_monotonic_constraints(&self, constraints: Vec<i32>) -> PyResult<Self> {
        let parsed: Result<Vec<MonotonicConstraint>, _> = constraints
            .into_iter()
            .map(|c| match c {
                1 => Ok(MonotonicConstraint::Increasing),
                -1 => Ok(MonotonicConstraint::Decreasing),
                0 => Ok(MonotonicConstraint::None),
                _ => Err(PyValueError::new_err(
                    "Constraint must be 1 (increasing), -1 (decreasing), or 0 (none)",
                )),
            })
            .collect();

        let mut new = self.clone();
        new.inner.monotonic_constraints = parsed?;
        Ok(new)
    }

    /// Set monotonic constraints for features (using enum)
    ///
    /// Args:
    ///     constraints: List of MonotonicConstraint per feature
    ///
    /// Example:
    ///     config.with_constraints([
    ///         MonotonicConstraint.increasing(),
    ///         MonotonicConstraint.decreasing(),
    ///         MonotonicConstraint.none()
    ///     ])
    fn with_constraints(&self, constraints: Vec<PyMonotonicConstraint>) -> PyResult<Self> {
        let parsed: Vec<MonotonicConstraint> = constraints.into_iter().map(|c| c.into()).collect();

        let mut new = self.clone();
        new.inner.monotonic_constraints = parsed;
        Ok(new)
    }

    /// Set feature interaction groups
    fn with_interaction_groups(&self, groups: Vec<Vec<usize>>) -> PyResult<Self> {
        let mut new = self.clone();
        new.inner.interaction_groups = groups;
        Ok(new)
    }
}

// Internal methods for use by other Python binding modules
impl PyGBDTConfig {
    /// Get reference to inner config (for use by tuner module)
    pub fn inner(&self) -> &GBDTConfig {
        &self.inner
    }

    /// Create from inner config (for use by tuner module)
    pub fn from_inner(config: GBDTConfig) -> Self {
        Self { inner: config }
    }
}

/// Python wrapper for trained GBDT model
#[pyclass(name = "GBDTModel")]
pub struct PyGBDTModel {
    model: GBDTModel,
}

#[pymethods]
impl PyGBDTModel {
    /// Train a GBDT model from numpy arrays
    ///
    /// Args:
    ///     features: 2D numpy array of shape (n_samples, n_features)
    ///     targets: 1D numpy array of shape (n_samples,)
    ///     config: GBDTConfig instance
    ///     feature_names: Optional list of feature names
    ///     output_dir: Optional directory to save model and config.json
    ///                 If provided, saves model.rkyv and config.json for reproducibility
    ///
    /// Returns:
    ///     Trained GBDTModel
    ///
    /// Example:
    ///     # Train and save automatically
    ///     model = GBDTModel.train(features, targets, config, output_dir="my_model")
    ///     # Creates: my_model/model.rkyv and my_model/config.json
    #[staticmethod]
    #[pyo3(signature = (features, targets, config, feature_names=None, output_dir=None))]
    fn train<'py>(
        py: Python<'py>,
        features: PyReadonlyArray2<'py, f32>,
        targets: PyReadonlyArray1<'py, f32>,
        config: &PyGBDTConfig,
        feature_names: Option<Vec<String>>,
        output_dir: Option<String>,
    ) -> PyResult<Self> {
        let features_arr = features.as_array();
        let targets_arr = targets.as_array();

        let num_rows = features_arr.nrows();
        let num_features = features_arr.ncols();

        // Convert to row-major flat Vec<f32> for Rust high-level API
        let mut features_flat: Vec<f32> = Vec::with_capacity(num_rows * num_features);
        for row in features_arr.rows() {
            features_flat.extend(row.iter().copied());
        }

        let targets_vec: Vec<f32> = targets_arr.to_vec();

        // Train model using high-level Rust API (release GIL during training)
        // Binning is now done in Rust with Rayon parallelization
        let model = py
            .allow_threads(|| {
                GBDTModel::train(
                    &features_flat,
                    num_features,
                    &targets_vec,
                    config.inner.clone(),
                    feature_names,
                )
            })
            .map_err(|e| PyValueError::new_err(e.to_string()))?;

        // If output_dir provided, save model and config
        if let Some(ref dir) = output_dir {
            model
                .save_to_directory(dir, &config.inner, &[ModelFormat::Rkyv])
                .map_err(|e| {
                    PyIOError::new_err(format!("Failed to save to output directory: {}", e))
                })?;
        }

        Ok(Self { model })
    }

    /// Train a GBDT model with Directional Era Splitting (DES)
    ///
    /// Era splitting filters out spurious correlations by requiring all eras
    /// to agree on split direction. This is useful for time-series or financial
    /// data where patterns may not generalize across time periods.
    ///
    /// Args:
    ///     features: 2D numpy array of shape (n_samples, n_features)
    ///     targets: 1D numpy array of shape (n_samples,)
    ///     era_indices: 1D numpy array of era indices (uint16), shape (n_samples,)
    ///     config: GBDTConfig instance (must have era_splitting=True)
    ///     feature_names: Optional list of feature names
    ///
    /// Returns:
    ///     Trained GBDTModel
    ///
    /// Example:
    ///     config = GBDTConfig()
    ///     config.era_splitting = True
    ///     model = GBDTModel.train_with_eras(features, targets, era_indices, config)
    #[staticmethod]
    #[pyo3(signature = (features, targets, era_indices, config, feature_names=None))]
    fn train_with_eras<'py>(
        py: Python<'py>,
        features: PyReadonlyArray2<'py, f32>,
        targets: PyReadonlyArray1<'py, f32>,
        era_indices: PyReadonlyArray1<'py, u16>,
        config: &PyGBDTConfig,
        feature_names: Option<Vec<String>>,
    ) -> PyResult<Self> {
        let features_arr = features.as_array();
        let targets_arr = targets.as_array();
        let era_indices_arr = era_indices.as_array();

        let num_rows = features_arr.nrows();
        let num_features = features_arr.ncols();

        // Validate era_indices length
        if era_indices_arr.len() != num_rows {
            return Err(PyValueError::new_err(format!(
                "era_indices length {} doesn't match number of rows {}",
                era_indices_arr.len(),
                num_rows
            )));
        }

        // Validate era_splitting is enabled
        if !config.inner.era_splitting {
            return Err(PyValueError::new_err(
                "era_splitting must be True in config when using train_with_eras",
            ));
        }

        // Convert to row-major flat Vec<f32> for Rust high-level API
        let mut features_flat: Vec<f32> = Vec::with_capacity(num_rows * num_features);
        for row in features_arr.rows() {
            features_flat.extend(row.iter().copied());
        }

        let targets_vec: Vec<f32> = targets_arr.to_vec();
        let era_indices_vec: Vec<u16> = era_indices_arr.to_vec();

        // Train model using high-level Rust API (release GIL during training)
        let model = py
            .allow_threads(|| {
                GBDTModel::train_with_eras(
                    &features_flat,
                    num_features,
                    &targets_vec,
                    &era_indices_vec,
                    config.inner.clone(),
                    feature_names,
                )
            })
            .map_err(|e| PyValueError::new_err(e.to_string()))?;

        Ok(Self { model })
    }

    /// Predict for new data
    ///
    /// Args:
    ///     features: 2D numpy array of shape (n_samples, n_features)
    ///               Accepts float32 or float64 arrays
    ///
    /// Returns:
    ///     1D numpy array of predictions
    #[pyo3(signature = (features))]
    fn predict<'py>(
        &self,
        py: Python<'py>,
        features: &Bound<'py, PyAny>,
    ) -> PyResult<Bound<'py, PyArray1<f32>>> {
        let (num_features, raw_features) = extract_features_array(features)?;
        validate_feature_count(num_features, self.model.num_features())?;

        // Predict using raw values (release GIL)
        let predictions = py.allow_threads(|| self.model.predict_raw(&raw_features));

        Ok(PyArray1::from_vec(py, predictions))
    }

    /// Predict with conformal intervals
    ///
    /// Args:
    ///     features: 2D numpy array of shape (n_samples, n_features)
    ///               Accepts float32 or float64 arrays
    ///
    /// Returns:
    ///     Tuple of (predictions, lower_bounds, upper_bounds) as numpy arrays
    #[pyo3(signature = (features))]
    fn predict_with_intervals<'py>(
        &self,
        py: Python<'py>,
        features: &Bound<'py, PyAny>,
    ) -> PyResult<(
        Bound<'py, PyArray1<f32>>,
        Bound<'py, PyArray1<f32>>,
        Bound<'py, PyArray1<f32>>,
    )> {
        let (num_features, raw_features) = extract_features_array(features)?;
        validate_feature_count(num_features, self.model.num_features())?;

        // Predict with intervals (release GIL)
        let (preds, lower, upper) =
            py.allow_threads(|| self.model.predict_raw_with_intervals(&raw_features));

        Ok((
            PyArray1::from_vec(py, preds),
            PyArray1::from_vec(py, lower),
            PyArray1::from_vec(py, upper),
        ))
    }

    /// Predict class probabilities (for binary classification)
    ///
    /// Applies sigmoid to raw predictions to get probabilities in [0, 1].
    /// Only meaningful when trained with `use_binary_logloss()`.
    ///
    /// For multi-class models, use `predict_proba_multiclass()` instead.
    ///
    /// Args:
    ///     features: 2D numpy array of shape (n_samples, n_features)
    ///               Accepts float32 or float64 arrays
    ///
    /// Returns:
    ///     1D numpy array of probabilities (probability of class 1)
    #[pyo3(signature = (features))]
    fn predict_proba<'py>(
        &self,
        py: Python<'py>,
        features: &Bound<'py, PyAny>,
    ) -> PyResult<Bound<'py, PyArray1<f32>>> {
        // Extract and validate features first (most specific error)
        let (num_features, raw_features) = extract_features_array(features)?;
        validate_feature_count(num_features, self.model.num_features())?;

        // Then check model type
        if self.model.is_multiclass() {
            return Err(PyValueError::new_err(
                "predict_proba() is for binary classification only. \
                 For multi-class models, use predict_proba_multiclass() instead.",
            ));
        }

        // Predict probabilities (release GIL)
        let proba = py.allow_threads(|| self.model.predict_proba_raw(&raw_features));

        Ok(PyArray1::from_vec(py, proba))
    }

    /// Predict class labels (for binary classification)
    ///
    /// Applies sigmoid to raw predictions and thresholds.
    /// Only meaningful when trained with `use_binary_logloss()`.
    ///
    /// For multi-class models, use `predict_class_multiclass()` instead.
    ///
    /// Args:
    ///     features: 2D numpy array of shape (n_samples, n_features)
    ///               Accepts float32 or float64 arrays
    ///     threshold: Classification threshold (default 0.5)
    ///
    /// Returns:
    ///     1D numpy array of class labels (0 or 1)
    #[pyo3(signature = (features, threshold = 0.5))]
    fn predict_class<'py>(
        &self,
        py: Python<'py>,
        features: &Bound<'py, PyAny>,
        threshold: f32,
    ) -> PyResult<Bound<'py, PyArray1<u32>>> {
        // Extract and validate features first (most specific error)
        let (num_features, raw_features) = extract_features_array(features)?;
        validate_feature_count(num_features, self.model.num_features())?;

        // Then check model type
        if self.model.is_multiclass() {
            return Err(PyValueError::new_err(
                "predict_class() is for binary classification only. \
                 For multi-class models, use predict_class_multiclass() instead.",
            ));
        }

        // Predict classes (release GIL)
        let classes = py.allow_threads(|| self.model.predict_class_raw(&raw_features, threshold));

        Ok(PyArray1::from_vec(py, classes))
    }

    // Multi-class classification methods

    /// Check if this is a multi-class model
    #[getter]
    fn is_multiclass(&self) -> bool {
        self.model.is_multiclass()
    }

    /// Get number of classes (0 for regression/binary)
    #[getter]
    fn num_classes(&self) -> usize {
        self.model.get_num_classes()
    }

    /// Predict class probabilities for multi-class classification
    ///
    /// Applies softmax to raw predictions to get probabilities for each class.
    /// Only meaningful when trained with `use_multiclass_logloss()`.
    ///
    /// For binary classification, use `predict_proba()` instead.
    ///
    /// Args:
    ///     features: 2D numpy array of shape (n_samples, n_features)
    ///               Accepts float32 or float64 arrays
    ///
    /// Returns:
    ///     2D numpy array of shape (n_samples, n_classes) with probabilities
    #[pyo3(signature = (features))]
    fn predict_proba_multiclass<'py>(
        &self,
        py: Python<'py>,
        features: &Bound<'py, PyAny>,
    ) -> PyResult<Bound<'py, numpy::PyArray2<f32>>> {
        use numpy::PyArray2;

        // Extract and validate features first (most specific error)
        let (num_features, raw_features) = extract_features_array(features)?;
        validate_feature_count(num_features, self.model.num_features())?;

        // Then check model type
        if !self.model.is_multiclass() {
            return Err(PyValueError::new_err(
                "predict_proba_multiclass() is for multi-class models only. \
                 For binary classification, use predict_proba() instead.",
            ));
        }

        // Use the raw prediction method (no binning needed, release GIL)
        let proba = py.allow_threads(|| self.model.predict_proba_multiclass_raw(&raw_features));

        // Validate array is not jagged before conversion
        if proba.is_empty() {
            return Err(PyValueError::new_err("No predictions returned"));
        }
        let expected_cols = proba[0].len();
        if !proba.iter().all(|row| row.len() == expected_cols) {
            return Err(PyValueError::new_err(
                "Internal error: jagged probability array returned",
            ));
        }

        // Convert Vec<Vec<f32>> to 2D numpy array via nested vec
        PyArray2::from_vec2(py, &proba)
            .map_err(|e| PyValueError::new_err(format!("Failed to create numpy array: {:?}", e)))
    }

    /// Predict class labels for multi-class classification
    ///
    /// Returns the class with highest probability (argmax of softmax).
    /// Only meaningful when trained with `use_multiclass_logloss()`.
    ///
    /// For binary classification, use `predict_class()` instead.
    ///
    /// Args:
    ///     features: 2D numpy array of shape (n_samples, n_features)
    ///               Accepts float32 or float64 arrays
    ///
    /// Returns:
    ///     1D numpy array of class labels (0, 1, 2, ..., K-1)
    #[pyo3(signature = (features))]
    fn predict_class_multiclass<'py>(
        &self,
        py: Python<'py>,
        features: &Bound<'py, PyAny>,
    ) -> PyResult<Bound<'py, PyArray1<u32>>> {
        // Extract and validate features first (most specific error)
        let (num_features, raw_features) = extract_features_array(features)?;
        validate_feature_count(num_features, self.model.num_features())?;

        // Then check model type
        if !self.model.is_multiclass() {
            return Err(PyValueError::new_err(
                "predict_class_multiclass() is for multi-class models only. \
                 For binary classification, use predict_class() instead.",
            ));
        }

        // Use the raw prediction method (no binning needed, release GIL)
        let classes = py.allow_threads(|| self.model.predict_class_multiclass_raw(&raw_features));

        Ok(PyArray1::from_vec(py, classes))
    }

    /// Get feature importance (gain-based)
    ///
    /// Returns:
    ///     1D numpy array of normalized feature importance
    fn feature_importance<'py>(&self, py: Python<'py>) -> Bound<'py, PyArray1<f32>> {
        let importances = self.model.feature_importance();
        PyArray1::from_vec(py, importances)
    }

    /// Save model to file
    ///
    /// Args:
    ///     path: Path to save the model
    ///     format: Serialization format ("rkyv" or "bincode", default: "rkyv")
    ///         - "rkyv": Zero-copy deserialization, fastest loading (recommended)
    ///         - "bincode" (or "bin"): Compact binary format, serde-based
    #[pyo3(signature = (path, format="rkyv"))]
    fn save(&self, path: &str, format: &str) -> PyResult<()> {
        let model_format = parse_model_format(format)?;
        match model_format {
            ModelFormat::Rkyv => serialize::save_model(&self.model, path),
            ModelFormat::Bincode => serialize::save_model_bincode(&self.model, path),
        }
        .map_err(|e| PyIOError::new_err(e.to_string()))
    }

    /// Save model and config to a directory for reproducibility
    ///
    /// Creates the directory if needed and saves:
    /// - model.rkyv (or model.bin): The trained model
    /// - config.json: Training configuration for reproducibility
    ///
    /// Args:
    ///     output_dir: Directory to save model and config
    ///     config: GBDTConfig used for training (for config.json)
    ///     formats: Serialization format(s) - either a single string ("rkyv" or "bincode")
    ///              or a list of strings (["rkyv", "bincode"]). Default: "rkyv"
    ///
    /// Example:
    ///     model = GBDTModel.train(features, targets, config)
    ///
    ///     # Single format (default)
    ///     model.save_to_directory("my_model", config)
    ///     # Creates: my_model/model.rkyv and my_model/config.json
    ///
    ///     # Multiple formats
    ///     model.save_to_directory("my_model", config, formats=["rkyv", "bincode"])
    ///     # Creates: my_model/model.rkyv, my_model/model.bin, and my_model/config.json
    #[pyo3(signature = (output_dir, config, formats=None))]
    fn save_to_directory(
        &self,
        output_dir: &str,
        config: &PyGBDTConfig,
        formats: Option<&Bound<'_, PyAny>>,
    ) -> PyResult<()> {
        // Parse formats - can be None (default), a string, or a list of strings
        let model_formats = match formats {
            None => vec![ModelFormat::Rkyv], // Default to rkyv
            Some(obj) => {
                if let Ok(s) = obj.extract::<String>() {
                    // Single string format
                    vec![parse_model_format(&s)?]
                } else if let Ok(list) = obj.extract::<Vec<String>>() {
                    // List of format strings
                    if list.is_empty() {
                        return Err(PyValueError::new_err("formats list must not be empty"));
                    }
                    list.iter()
                        .map(|s| parse_model_format(s))
                        .collect::<PyResult<Vec<_>>>()?
                } else {
                    return Err(PyValueError::new_err(
                        "formats must be a string ('rkyv' or 'bincode') or a list of strings",
                    ));
                }
            }
        };

        self.model
            .save_to_directory(output_dir, &config.inner, &model_formats)
            .map_err(|e| PyIOError::new_err(e.to_string()))
    }

    /// Load model from file
    ///
    /// Args:
    ///     path: Path to the model file
    ///     format: Serialization format ("rkyv" or "bincode", default: "rkyv")
    ///         - "rkyv": Zero-copy deserialization, fastest loading (recommended)
    ///         - "bincode" (or "bin"): Compact binary format, serde-based
    ///
    /// Returns:
    ///     Loaded GBDTModel
    #[staticmethod]
    #[pyo3(signature = (path, format="rkyv"))]
    fn load(path: &str, format: &str) -> PyResult<Self> {
        let model_format = parse_model_format(format)?;
        let model = match model_format {
            ModelFormat::Rkyv => serialize::load_model(path),
            ModelFormat::Bincode => serialize::load_model_bincode(path),
        }
        .map_err(|e| PyIOError::new_err(e.to_string()))?;

        Ok(Self { model })
    }

    /// Get number of trees in the ensemble
    #[getter]
    fn num_trees(&self) -> usize {
        self.model.num_trees()
    }

    /// Get number of features
    #[getter]
    fn num_features(&self) -> usize {
        self.model.num_features()
    }

    /// Get base prediction value
    #[getter]
    fn base_prediction(&self) -> f32 {
        self.model.base_prediction()
    }

    /// Get conformal quantile (if calibrated)
    #[getter]
    fn conformal_quantile(&self) -> Option<f32> {
        self.model.conformal_quantile()
    }

    /// Get feature names
    #[getter]
    fn feature_names(&self) -> Vec<String> {
        self.model
            .feature_info()
            .iter()
            .map(|info| info.name.clone())
            .collect()
    }

    fn __repr__(&self) -> String {
        format!(
            "GBDTModel(num_trees={}, num_features={}, base_prediction={:.4})",
            self.model.num_trees(),
            self.model.num_features(),
            self.model.base_prediction()
        )
    }
}

/// Register Python module classes and functions
pub fn register_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
    m.add_class::<PyMonotonicConstraint>()?;
    m.add_class::<PyGBDTConfig>()?;
    m.add_class::<PyGBDTModel>()?;
    Ok(())
}