You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Speed up histogram bin creation in sklearn.ensemble._hist_gradient_boosting.binning #34304
This issue is not yet ready for a PR. If you are interested in contributing to scikit-learn, please have a look at our contributing guidelines, and in particular the sections for new contributors and the "Needs triage" label.
Describe the workflow you want to enable
Bin creation is a surprisingly large chunk of at least some of the histogram gradient boosting benchmarks; a big chunk of that is np.argsort(). I am fairly certain it can be faster, e.g. full sorting is probably unnecessary in many cases.
Describe your proposed solution
Current ideas (perhaps all wrong!) that I expect to try:
A quick experiment suggests np.argpartition is way faster than np.argsort and plausibly viable in some common cases. I think even that may be overkill.
It would not surprise me if the unique values calculation is unnecessary on the full data, and running it just on the bins might suffice.
Maybe NumPy's own histogram calculation might work sometimes?
Etc. Still TBD if any of this is actually correct.
Describe alternatives you've considered, if relevant
No response
Additional context
I will try to implement some of these, and if I succeed then submit a PR.
Thanks for the work you've done so far. The goal of this comment is to set expectations.
Deciding on new features or substantial changes is a lengthy process. It frequently happens that no maintainer is available to take on this task right now.
Please do not create a Pull Request before a decision has been made regarding the proposed work. Making this decision can often take a significant amount of time and effort.
Not a very exciting optimization, but as mentioned it is definitely faster; the issue is that in most benchmarks this isn't a big % of full time so it's not super impactful. So it's probably one of those cases where you do a bunch of small optimizations and it adds up.
So we reached the same conclusion with @itamarst 😄
Note that for a datasets of 100k samples and with 1 thread, this change still brings a ~2-3x speed-up in the binning process. But this speed-up becomes quickly almost invisible when the number of threads increases (a bit like for #34194).
I wonder if memory bandwidth becomes an issue, certainly seen some places where it seems like it would be. (And I wonder how one would get past that... compression?)
And I wonder how one would get past that... compression?
I'd say we just want to try to write good memory access patterns as much as possible. Once such code becomes memory bound, I think we can considered it's optimized enough ^^
Activity
itamarst commentedon Jun 15, 2026
What I've learned:
argsort()was significant was wrong. Not sure how I managed to get that result...argpartitionmade things worse.argsort()slightly, so maybe 6% speed up on one benchmark after PERF Faster GradientHistogramBooster by improving the binning algorithm's binary search #34194 is merged. So will submit a PR once that is merged, with any other quick wins I see.martian7777 commentedon Jun 16, 2026
I'm working on this!
github-actions commentedon Jun 19, 2026
Thanks for the work you've done so far. The goal of this comment is to set expectations.
Deciding on new features or substantial changes is a lengthy process. It frequently happens that no maintainer is available to take on this task right now.
Please do not create a Pull Request before a decision has been made regarding the proposed work. Making this decision can often take a significant amount of time and effort.
ogrisel commentedon Jun 19, 2026
@cakedev0 could you share your own analysis of problems related to this binning step here?
itamarst commentedon Jun 19, 2026
Here's what I came up with, BTW, before timeboxing this and moving on: main...itamarst:scikit-learn:34304-faster-bin-creation
Not a very exciting optimization, but as mentioned it is definitely faster; the issue is that in most benchmarks this isn't a big % of full time so it's not super impactful. So it's probably one of those cases where you do a bunch of small optimizations and it adds up.
cakedev0 commentedon Jun 19, 2026
The changes proposed here main...itamarst:scikit-learn:34304-faster-bin-creation are contained in my PR #34248 (I'd be happy to get your review @itamarst, you seem quite knowledgeable on this kind of subject 😁 )
So we reached the same conclusion with @itamarst 😄
Note that for a datasets of 100k samples and with 1 thread, this change still brings a ~2-3x speed-up in the binning process. But this speed-up becomes quickly almost invisible when the number of threads increases (a bit like for #34194).
itamarst commentedon Jun 20, 2026
I wonder if memory bandwidth becomes an issue, certainly seen some places where it seems like it would be. (And I wonder how one would get past that... compression?)
cakedev0 commentedon Jun 20, 2026
I'd say we just want to try to write good memory access patterns as much as possible. Once such code becomes memory bound, I think we can considered it's optimized enough ^^