Skip to content

Fix: Issue #34764 Bug in module:ensemble - #3

Open
Prathamj07 wants to merge 1 commit into
mainfrom
scikit/34764
Open

Prathamj07 wants to merge 1 commit into
mainfrom
scikit/34764

Conversation

@Prathamj07

Copy link
Copy Markdown
Collaborator

Reference Issues/PRs

Fixes scikit-learn#34764.

What does this implement/fix? Explain your changes.

HistGradientBoosting launches many small OpenMP prange regions during fit (histogram build and split finding). On machines where OpenMP active wait is off (GOMP_SPINCOUNT=1 / KMP_BLOCKTIME=0, typical on hybrid-core laptops and conda-forge libomp), thread start/join overhead dominates and small-to-medium fits can be several times slower.

This PR does not change OpenMP's wait policy (libgomp cannot set that per region anyway). It only caps num_threads on those inner loops so we never spawn more workers than there is work:

  • histogram.pyx: num_threads=min(n_threads, n_samples) when reordering gradients/hessians, and min(n_threads, n_allowed_features) when building per-feature histograms
  • splitting.pyx: min(self.n_threads, n_samples) when partitioning a node, and min(n_threads, n_allowed_features) when scanning candidate features

Public API, tree structure, and numeric results are unchanged: each iteration writes an independent slot, so fewer threads only skip idle workers. KMeans / OpenBLAS interaction from the same issue is left untouched.

On an Apple Silicon libomp build with active wait effectively disabled, repeated small HGB fits went from ~1481 ms/fit to ~1060 ms/fit (~28%). Forcing active wait on was unchanged within noise. That is a mitigation, not a full fix of the wait-policy problem.

Introduce yourself

I use scikit-learn for model training. I picked this up after seeing the OpenMP active-wait slowdown on HistGradientBoosting in scikit-learn#34764.

AI usage disclosure

I used AI assistance for:

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding

Any other comments?

Verified on a rebuilt editable install with Homebrew libomp (8 threads): 466 tests in _hist_gradient_boosting + test_gradient_boosting.py passed. Predictions matched the pre-fix binary bit-for-bit on small and large regressor/classifier cases.

@ogrisel

ogrisel commented Sep 10, 2026 •

Copy link
Copy Markdown

Could you evaluate the impact of this change on a machine running linux with ligomp and a machine running windows with MSVC's OpenMP implementation?

Please also repeated the experiments with various (n_samples, n_features) data shapes, in particular with n_features above the number of cores.

I am worried that fast-changing the number of threads in subsequent parallel regions might be detrimental for some loads and environments.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Impact of active wait policy of OMP implementations in scikit-learn

2 participants