Repository navigation
PERF Faster GradientHistogramBooster by improving the binning algorithm's binary search - #34194
Conversation
|
Sorry I clicked the copilot review request by mistake (I wanted to self request a review on this instead)... |
|
For the record, I'll tried to find the time to runs some benchmarks on my Apple M4 laptops to see if I can reproduce similar improvements on a different OS/CPU/compiler setup. |
There was a problem hiding this comment.
Pull request overview
This PR optimizes the binning step used by HistGradientBoosting* by replacing the per-value branching binary search with a more branch-misprediction-resistant (largely branchless) binary search strategy, aiming to reduce runtime spent in _map_to_bins.
Changes:
- Refactor
_map_col_to_binsto use a newnogilinline_binary_searchimplementation. - Add a small
log2ceillookup table (0–255) to bound the binary search loop count. - Tighten some memoryview typings to favor contiguous access patterns (
[::1]).
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Thanks! As mentioned it helps somewhat on some of the benchmarks on M1, most noticeably on the |
|
Got access to a Mac M4. Seems faster here too for these two benchmarks, and no difference on others I tried. Peak memory seems a bit higher in the asv test, with no speedup there.
|
| branch | real | user | sys |
|---|---|---|---|
main |
17.56 real | 17.15 user | 0.40 sys |
| PR | 15.50 real | 14.43 user | 0.37 sys |
benchmarks/bench_hist_gradient_boosting_threading.py with OMP_NUM_THREADS=1
| branch | real | user | sys |
|---|---|---|---|
main |
44.54 real | 43.54 user | 0.35 sys |
| PR | 33.45 real | 33.15 user | 0.35 sys |
|
Thank you, will do next week. |
|
Hi @itamarst, this is a really cool optimization! And a really nice showcase of branchless code benefits! Thank you! On this benchmark https://github.com/cakedev0/numerical-python-bench/blob/main/bench_hgbt_sweep.py that records the time taken by each part, I get a 5x speed-up for the bin transform part. It's less when increasing the number of threads (2x for 12 threads on my machine) but still nice. I guess this optimized version of the function becomes memory bound quickly. |
|
I benchmark with F-order vs C-order for X, and it's interesting:
This is visible for big enough datasets (n_samples * 64B > size of L3 cache I think). Note that the gain from having X in F-order is not big enough to compensate for the cost of doing |
|
I merged forward to address the cython-lint issues. |
|
@itamarst could you please add a changelog entry for this PR? I think this qualifies as an "efficiency" improvement. |
|
Let's also wait for the merge of #34343 and check that this test still pass with this PR updated to run it. |
|
Added news file (which is hopefully accurate—is binning also used in fitting?) and merged forward, should include #34343 now. |
|
Or maybe I misunderstood and it's only used in fitting... |
|
Yes it's only used during fit (for preprocessing, and for "in-fit predictions" for early stopping) |
|
For normal/public predict, we use "predict_raw". ... But I wouldn't be surprised binning first and then predicting on binned values to be faster (X.dtype becomes uint8 instead of float64 and logic gets simpler). |
|
Ok, fixed the changelog. If you have time @cakedev0 would be interesting to see if this does indeed speed up predictions, I won't this week. |
|
I did that a bit this morning, but it's quite hard to make a conclusion, there are so many impacting factors. It's really easy to draw a conclusion, then change a few things in the benchmarks set-up and draw opposite conclusions... I'll try to make a good benchmark for that, which means:
But that'll be for a bit later. |
…hm's binary search (scikit-learn#34194)
Binning is a significant percentage of time spent by GradientHistogramBooster. The new implementation in this PR is inspired by Paul Khuong's version, plus one idea taken from the original article.
GradientHistogramBooster benchmarks seem to be 5-25% faster, depending on benchmark, machine, and number of OpenMP threads.
Why is it faster? Looking at
perf statfor the higgs boson benchmark, it reduces branch mispredictions quite a bit. Given histograms are still a minority of the code runtime, I have to imagine the misprediction reduction is that much more significant if I were to look just at the modified part of the code.Benchmarks
Benchmarks are on Linux x86-64, i7-12700K, compiled with gcc.
On
main:On new branch:
Higgs Boson benchmark uses 255 bins most of the time, 2-3 bins a tiny bit.
benchmarks/bench_hist_gradient_boosting_categorical_only.py --n-samples 1_000_000shows small speed-ups, or no speed-ups, with 128 and 255 bins, all threads or 1 thread.benchmarks/bench_hist_gradient_boosting_threading.pygoes from 91CPU seconds / 34.6 seconds elapsed onmainto 71 CPU seconds / 26 elapsed on this branch (255 bins).Reference Issues/PRs
Can open one if it's helpful.
What does this implement/fix? Explain your changes.
Should have identical behavior, just run faster.
AI usage disclosure
No "AI".
Any other comments?
Tried the graphing in #34128; more threads is still a problem.