Skip to content

Speed up histogram bin creation in sklearn.ensemble._hist_gradient_boosting.binning #34304

Closed

Description

@itamarst

Warning

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.

Activity

  1. itamarst commented on Jun 15, 2026

    @itamarst
    ContributorAuthor

    What I've learned:

  2. martian7777 commented on Jun 16, 2026

    @martian7777

    I'm working on this!

  3. added
    Needs Decision
    and removed
    Needs Triage
    on Jun 19, 2026
  4. github-actions commented on Jun 19, 2026

    @github-actions

    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.

  5. ogrisel commented on Jun 19, 2026

    @ogrisel
    Member

    @cakedev0 could you share your own analysis of problems related to this binning step here?

  6. itamarst commented on Jun 19, 2026

    @itamarst
    ContributorAuthor

    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.

  7. cakedev0 commented on Jun 19, 2026

    @cakedev0
    Member

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

  8. itamarst commented on Jun 20, 2026

    @itamarst
    ContributorAuthor

    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?)

  9. cakedev0 commented on Jun 20, 2026

    @cakedev0
    Member

    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 ^^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs DecisionNew Feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions