Sub topic 18
Scaling up: larger data and GPU training
This chapter assumes you can train an XGBClassifier or call xgb.train, and that you know the tree_method parameter exists. It covers what to change when the data stops fitting comfortably in time or memory.
Why the histogram method scales
The exact method sorts and scans every distinct feature value at every node. The histogram method (tree_method="hist") first bins each feature into at most max_bin buckets (default 256). Split search then scans bucket totals of gradients and hessians instead of raw rows. The cost per node depends on the number of bins, not the number of distinct values.
In XGBoost 2.0 and later, hist is the default. The exact method is still available, but it is rarely worth using beyond small datasets.
| Setting | Effect | Trade-off |
|---|---|---|
| `max_bin` lower (e.g. 64) | Faster, less memory | Coarser split thresholds |
| `max_bin` higher (e.g. 512) | Finer thresholds | Slower, more memory |
| `tree_method="exact"` | Considers every value | Slow on large data |
Moving to the GPU
In XGBoost 2.x, you pick the hardware with the device parameter. Keep tree_method="hist" and set device="cuda". Older tutorials use tree_method="gpu_hist", which is deprecated.
import numpy as np
import xgboost as xgb
rng = np.random.default_rng(0)
n = 200_000
X = rng.normal(size=(n, 50)).astype(np.float32)
y = (X[:, 0] + X[:, 1] ** 2 + rng.normal(size=n) > 1).astype(int)
# QuantileDMatrix stores binned data only, which saves memory with hist.
dtrain = xgb.QuantileDMatrix(X, label=y, max_bin=256)
params = {
"objective": "binary:logistic",
"tree_method": "hist",
"device": "cuda", # use "cpu" if no NVIDIA GPU is available
"max_bin": 256,
"eta": 0.1,
"max_depth": 6,
}
booster = xgb.train(params, dtrain, num_boost_round=200)
print(booster.num_boosted_rounds())If you have no GPU, change "device" to "cpu". The rest of the code runs unchanged.
Memory and common errors
- Out of GPU memory. The data, the bin structure and the gradients all live in GPU memory. Reduce
max_bin, reducemax_depth, or useQuantileDMatrix. For data larger than memory, use external memory with a data iterator (xgb.DataIter). - Mismatched
max_bin. When you build aQuantileDMatrixfor training, use the samemax_binin the params. For a validation set, passref=dtrainso both share the same bin boundaries. - Slow on small data. A GPU has launch and transfer overhead. Below roughly tens of thousands of rows, the CPU is often as fast or faster.
- Float64 input. Converting to
float32before building the matrix halves memory for the raw array. - Slightly different results. CPU and GPU runs can differ in the last digits because floating-point sums are accumulated in a different order. Do not compare them bit for bit.
Exercise
You switch a 20,000-row dataset from CPU to device="cuda" and training gets no faster. Give one likely reason and one change that would make the GPU pay off.
Show answer
At that size the GPU's launch and data-transfer overhead outweighs its parallel speedup. The GPU pays off with much more data (hundreds of thousands of rows or more), or with deeper trees and more features, where the histogram work dominates.