Expand description
Quantized decision trees and gradient-boosted tree ensembles (C++
cpp/qtree.hpp).
A tree walk is a handful of integer compares and branches: no
multiply-accumulates, no activation tables. Each split compares one input
feature against a threshold on the same integer grid as the feature. An affine
map is monotone, so x_q <= t_q makes the same decision as x <= t in the
real domain, except for an input within one grid step of the threshold. Leaf
values are i32, so an ensemble can add them up into per-class scores.
QTreeNode: one node of a flat node array (a pool), with integer indices to its children.QDecisionTree: one tree, rooted at node 0; returns its leaf’s value.QGbdt: an additive ensemble whose trees share one pool. Each tree adds its leaf value to one class’s score; the prediction is the class with the largest score. A single regression output is the case of one class.
Both borrow their arrays, so a model can be static or const data in
flash, and both are generic over the threshold type T and the input type
I (i8, u8, i16, anything that converts losslessly to i32); the
compare is made in i32, as in the C++.
§Malformed models
The C++ reads out of bounds for a child, root or feature index outside the
model (undefined behaviour), and checks indices at compile time only for its
compile-time models. Here every function is total instead: a walk that meets
an index outside the pool or the input stops and contributes 0, and a tree
whose class index is out of range contributes nothing. A cyclic pool ends
after as many steps as the pool has nodes, as in the C++. Well-formed models
give the C++ results exactly; check a model with QDecisionTree::is_valid
or QGbdt::is_valid, which are const fn, so a model in a const can be
checked at compile time:
use tinymind::tree::{QDecisionTree, QTreeNode};
const fn split(feature: i16, threshold: i8, left: i16, right: i16) -> QTreeNode<i8> {
QTreeNode { feature, threshold, left, right, leaf_value: 0 }
}
const fn leaf(value: i32) -> QTreeNode<i8> {
QTreeNode { feature: -1, threshold: 0, left: -1, right: -1, leaf_value: value }
}
// x[0] <= 10 ? 1 : (x[1] <= -5 ? 2 : 3)
static NODES: [QTreeNode<i8>; 5] = [split(0, 10, 1, 2), leaf(1), split(1, -5, 3, 4), leaf(2), leaf(3)];
const TREE: QDecisionTree<'static, i8, 5, 2> = QDecisionTree::new(&NODES);
const _: () = assert!(TREE.is_valid());
assert_eq!(TREE.predict(&[10i8, 0]), 1);
assert_eq!(TREE.predict(&[11i8, -5]), 2);
assert_eq!(TREE.predict(&[11i8, -4]), 3);Not ported: the C++ compile-time models (QStaticDecisionTree,
QStaticGBDT) and their evaluation policies. Rust cannot run a pool through
the type system the way C++ templates do. The repository’s tree importer
(tools/import_trees, for scikit-learn, XGBoost and LightGBM models) writes
a module with both forms instead: a pool walked by these types, and the same
trees as nested ifs (the inline policy).
Structs§
- QDecision
Tree - A single quantized decision tree over
FEATURESinputs, withNODESnodes;nodes[0]is the root (C++QDecisionTree). - QGbdt
- A gradient-boosted ensemble of
TREEStrees sharing one pool ofNODESnodes, scoringCLASSESclasses fromFEATURESinputs (C++QGBDT). - QTree
Node - One node of a quantized decision tree (C++
QTreeNode).
Functions§
- walk_
tree - Walk the tree rooted at
rootin the poolnodesto a leaf and return its value (C++walkQTree). At mostmax_nodessplits are followed, so a cyclic pool ends; the node reached then gives itsleaf_value, as in the C++. An index outsidenodesorxends the walk with 0 (see the module docs).