Skip to content

PERF: GradientBoosting* optimize line-search from O(n * n_leaves) to O(n) for classification losses - #32911

Merged
thomasjpfan merged 26 commits into
scikit-learn:mainfrom
cakedev0:optim/gbt_update_terminal_regions
Jun 11, 2026
Merged

thomasjpfan merged 26 commits into
scikit-learn:mainfrom
cakedev0:optim/gbt_update_terminal_regions

Conversation

@cakedev0

@cakedev0 cakedev0 commented Dec 16, 2025 •

Copy link
Copy Markdown
Member

Why current complexity is O(n * n_leaves)

Currently the line-search is implemented this way:

for leaf in leaves:
    (indices,) = np.nonzero(masked_terminal_regions == leaf)

np.nonzero(masked_terminal_regions == leaf) is O(n_sample) and we do it n_leaves times. This can quickly become prohibitively slow for trees with many leaves: typically, if trees are grown fully, it becomes O(n^2).

Why this can be damaging

While trees are usually shallow for GB, nothing prevents users from making deep trees, and especially nothing prevents them from doing some hyper-parameter search that will in some cases create deep trees. Such a search could suffer a lot from this O(n^2) in some part of the search-space.

What does this implement/fix? Explain your changes.

For the classification losses, it was possible to rewrite the calculation without a loop using np.bincount(leaves_idx, weights=w) to efficiently compute sums and averages grouped by leaves. This is a classic trick to do a "group by" in numpy.
It can result in a 10x speed-up on the total training duration when trees are grown fully.

For the MSE, line search is not needed, so it doesn't need optimization

For the other regression losses (AE, huber, pinball), loss.fit_intercept_only is called for each leaf; hence it's more complicated:

  • It's possible to avoid the O(n^2) with sparse matrices (see commit 27f647f) but you still need a O(n) Python loop with numpy calls in it, so the speed-up ends-up being very minimal. Conclusion: it's not worth the increased code complexity, and I reverted the commit.
  • We could implement a fully vectorized loss.fit_intercept_only_by_idx. It would require a weighted_quantile_per_idx, I tried implementing that, and it's very fast: see PR PRF: Optimize GB line search regression (RFC: is it worth it?)  cakedev0/scikit-learn#4 quite a lot of code, and so I've put it in a separate PR.

AI usage

Not that I remember (but I promise I do use AI sometimes).

@ogrisel ogrisel moved this to Todo in Tree-based models Jan 8, 2026
@ogrisel ogrisel moved this from Todo to In Progress in Tree-based models Jan 8, 2026
@cakedev0 cakedev0 changed the title PERF: GradientBoosting* optimize line-search from O(n * n_leaves) to O(n). PERF: GradientBoosting* optimize line-search from O(n * n_leaves) to O(n) for classification losses. Jan 14, 2026
@cakedev0 cakedev0 changed the title PERF: GradientBoosting* optimize line-search from O(n * n_leaves) to O(n) for classification losses. PERF: GradientBoosting* optimize line-search from O(n * n_leaves) to O(n) for classification losses Jan 14, 2026
Comment thread sklearn/ensemble/_gb.py Resolved
@cakedev0
cakedev0 marked this pull request as ready for review January 14, 2026 17:36
@cakedev0

Copy link
Copy Markdown
Member Author

I saw @ogrisel put this PR in "in progress" so I finished preparing it for a first review ^^

Also here is benchmark of the "best improvement scenario" for this PR:

import numpy as np
from sklearn.ensemble import GradientBoostingClassifier

n, d = 100_000, 4
X = np.random.geometric(0.05, size=(n, d))
y = np.random.choice(3, n)

gb = GradientBoostingClassifier(n_estimators=10, max_depth=None)
gb.fit(X, y)

Before this PR: 240s
After this PR: 10s

@cakedev0

Copy link
Copy Markdown
Member Author

I opened a separate PR on top of this one: cakedev0#4. For the same optimization but on regression losses (AE, pinball, huber). The speed-up is similarly good, but it's +300 loc 😅

assert np.all(gbt_huber_predictions <= gbt_squared_error.predict(X))


def test_safe_divide():

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed this test because we still cover for "division by zero doesn’t emit warnings" in leaf updates with the existing test_gb_denominator_zero.

@OmarManzoor OmarManzoor left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR @cakedev0

Comment thread sklearn/ensemble/_gb.py Resolved

@OmarManzoor OmarManzoor left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks nice! Thank you @cakedev0

@OmarManzoor OmarManzoor added the Waiting for Second Reviewer First reviewer is done, need a second one! label Jan 29, 2026
@ogrisel ogrisel added this to Labs May 25, 2026
@ogrisel ogrisel moved this to Blocked in Labs May 25, 2026

@thomasjpfan thomasjpfan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor comment, otherwise LGTM

Comment thread sklearn/ensemble/_gb.py Resolved
@thomasjpfan
thomasjpfan merged commit 9213721 into scikit-learn:main Jun 11, 2026
31 checks passed
@github-project-automation github-project-automation Bot moved this from In Progress to Done in Tree-based models Jun 11, 2026
@github-project-automation github-project-automation Bot moved this from Blocked to Done in Labs Jun 11, 2026
prady0t pushed a commit to prady0t/scikit-learn that referenced this pull request Sep 2, 2026
@jeremiedbb jeremiedbb mentioned this pull request Sep 8, 2026
14 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

module:ensemble Waiting for Second Reviewer First reviewer is done, need a second one!

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants