bonsai#
Histogram gradient-boosted trees with a C++23 core.
Wheels cover Linux (x86_64 and aarch64) and macOS (arm64), for Python 3.9 to 3.13, and install without compiling (no C++ toolchain needed). The Linux x86_64 wheel trains on GPU with only an NVIDIA driver installed. There is no Windows wheel, and the other two wheels train on CPU only. See Install for Docker and extras.
Three doors#
-
27chapters Learn
One concept per chapter: intuition, the mathematics, the ~50 real lines that implement it, then an experiment against the reference libraries. Start with a tree traced by hand on eight rows; the engine track and how we got here sit behind the same door.
-
44parameters API
Install in one command, then the quickstart. Estimators follow the scikit-learn fit and predict interface. The lower-level
train(params, ...)function takes a dict of settings such as{"tree.max_depth": 8}, and the CLI takes the same keys through--set tree.max_depth=8. One.msgpackfile written by Python loads in the CLI, and the reverse also works. -
6scenarios Results
Every speed and accuracy claim as a committed run on named hardware. The results ledger opens with the division summaries and links one generated page per study; the rules are the benchmark protocol. The invariants list the contracts the engine guarantees, most generated from the tests that enforce them. The decisions log records the measurements behind the numbers, including hypotheses the data rejected.
On GPU every bonsai grower (tree-growing strategy) fits the tall scenario (16M rows x 128 features, ingest plus train) ahead of the reference library it is paired with, in less peak host memory than any of them. At the extreme scenario (16M rows x 1024 features) XGBoost and CatBoost run out of memory, and bonsai finishes. LightGBM scores the higher test r2 on the GPU scenarios. On CPU every bonsai grower has the faster fit total at both the tall (2M rows x 128 features) and the wide (16k rows x 16384 features) scenario. All of it is measured on shared hardware at matched settings. The digits live only on the scenario panels, generated from the committed runs, so a refresh moves them there and nowhere else.
One property none of the reference libraries offer: models are bit-identical across CPU architectures at a fixed thread count, enforced per-commit in CI.
The story#
I started bonsai to learn gradient-boosted trees properly, from first principles, and to understand how the production libraries work. Reading the papers was not enough, so I implemented a basic serial version from scratch. The same build was my practice ground for modern C++ and the C++23 features I wanted fluency in.
Once a basic version of the trainer worked, I used Claude as a coding agent to add the features the production libraries had. Beyond pedagogy there was a slim hope this could become something useful. The first idea was to put the default growth strategies of XGBoost (depthwise), LightGBM (leaf-wise), and CatBoost (level-wise, one shared split per level) in one engine (growing trees).
After that, the goals kept moving. Each target was met and replaced by a harder one: CPU parity with the reference libraries, then GPU parity, then a GPU lead. The engine is built on C++ concepts (compile-time interfaces that a type must satisfy), so a new extension, such as a new objective or growth strategy, plugs into the existing interface instead of changing the callers. Each extension was still hard work.
GPU support started when a promotional window with Anthropic's newest model made the work affordable. I learned to rent GPU nodes from RunPod and gave the agent the rental API as a tool. From then on the agent measured, changed code, and measured again on real hardware, following the perf-round procedure for optimization work and the feature-admission procedure for deciding whether a new feature enters the core.
The ambition grew with the milestones: assimilate the defining ideas of XGBoost, LightGBM, and CatBoost into one library, match or beat their performance, and keep the code readable enough that reading it is still the point (code metrics counts its lines and complexity).