Skip to content

bonsai-gbt 2.12.1 · Python 3.9 to 3.13 · Linux x86_64, aarch64 · macOS arm64

Histogram gradient-boosted trees with a C++23 core, written to be read.

Tall scenario, same pod, 16M x 128: on one GPU bonsai fits in 3.8 to 5.4 s. XGBoost, LightGBM and CatBoost take 18.3 to 31.6 s.

Quickstart Scenario panels

tall scenario · GPU 16M x 128, CPU 2M x 128 · fit = ingest + train · seconds, lower is better

grower GPU (gpu-tall)CPU (cpu-tall)
depthwise
bonsai 3.8 s
XGBoost 20.9 s
bonsai 10.1 s
XGBoost 13.2 s
leafwise
bonsai 4.2 s
LightGBM 31.6 s
bonsai 10.2 s
LightGBM 17.6 s
levelwise
bonsai 5.4 s
CatBoost 18.3 s
bonsai 9.7 s
CatBoost 13.3 s

GPU NVIDIA RTX PRO 6000 Blackwell Server Edition, refreshed for 2.11.0 · CPU Intel(R) Xeon(R) 6952P, refreshed for 2.11.0 · same pod, interleaved

wide scenario · GPU 131k x 16384, CPU 16k x 16384 · fit = ingest + train · seconds, lower is better

grower GPU (gpu-wide)CPU (cpu-wide)
depthwise
bonsai 6.6 s
XGBoost 41.6 s
bonsai 40.3 s
XGBoost 195.6 s
leafwise
bonsai 6.0 s
LightGBM 283.8 s
bonsai 32.4 s
LightGBM 87.6 s
levelwise
bonsai 11.3 s
CatBoost 71.6 s
bonsai 47.4 s
CatBoost 117.8 s

GPU NVIDIA RTX PRO 6000 Blackwell Server Edition, refreshed for 2.11.0 · CPU Intel(R) Xeon(R) 6952P, refreshed for 2.11.0 · same pod, interleaved

extreme scenario · GPU 16M x 1024 · fit = ingest + train · seconds, lower is better

grower GPU (gpu-extreme)
depthwise
bonsai 12.3 s
XGBoost oom
leafwise
bonsai 12.1 s
LightGBM 248.8 s
levelwise
bonsai 11.2 s
CatBoost oom

GPU NVIDIA RTX PRO 6000 Blackwell Server Edition, refreshed for 2.11.0 · same pod, interleaved

scenario full panels

bonsai#

Histogram gradient-boosted trees with a C++23 core.

pip install bonsai-gbt
uv add bonsai-gbt
git clone https://github.com/daniel-m-campos/bonsai && cd bonsai && pip install .

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 .msgpack file 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).