Repository navigation
PRF Mitigation heuristics for HGB scalability - #34935
Conversation
Non regression test for the OverflowError fixed in 68e1aae: find_best_split only populated n_subsampled_features entries of the split_infos buffer, but scanned all n_allowed_features when picking the best split, reading uninitialized memory whenever max_features < 1. Co-Authored-By: Claude Sonnet 5 <[email protected]>
…positive_heuristics
lorentzenchr
left a comment
There was a problem hiding this comment.
Some initial review (need a bigger screen to do it properly).
| # Patterns matched against the block an OpenMP runtime prints on stderr when | ||
| # OMP_DISPLAY_ENV is set: they identify a *resolved* spin-count setting (as | ||
| # opposed to what was requested through environment variables, which the | ||
| # runtime may ignore, normalize, or default differently) that means idle | ||
| # threads do not spin. Note that ``OMP_WAIT_POLICY`` is not a reliable | ||
| # signal by itself: e.g. libgomp can report it as ``PASSIVE`` while | ||
| # ``GOMP_SPINCOUNT`` was explicitly set to a large value, which still makes | ||
| # threads spin in practice. Values may be prefixed by a device scope (e.g. | ||
| # "[host] ") and wrapped in quotes, hence the loose whitespace/quoting. | ||
| _PASSIVE_WAIT_PATTERNS = tuple( | ||
| re.compile(pattern) | ||
| for pattern in ( | ||
| # GNU libgomp: spinning at most once is effectively passive. | ||
| r"GOMP_SPINCOUNT\s*=\s*'?[01]'?\D", | ||
| # Intel's and LLVM's OpenMP runtimes. | ||
| r"KMP_BLOCKTIME\s*=\s*'?0'?\D", | ||
| ) | ||
| ) |
There was a problem hiding this comment.
Maybe it is late, I don't get it. What is _PASSIVE_WAIT_PATTERNS for?
There was a problem hiding this comment.
My understanding is that this is to parse the output of the subprocess that figures out whether active wait is on or not see _openmp_uses_active_wait function below.
| smaller workloads. This is controlled by ``GOMP_SPINCOUNT`` (GNU's ``libgomp``) and | ||
| ``KMP_BLOCKTIME`` (Intel's and LLVM's OpenMP runtimes). |
There was a problem hiding this comment.
One is a count and the other a time. This is calling for trouble. Isn't it?
There was a problem hiding this comment.
Well, there is nothing we can do about it 😅
…cy.rst Co-authored-by: Christian Lorentzen <[email protected]>
…_POLICY as experimental Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@lorentzenchr I had claude rewrote the comments & docstrings based on your questions, @lesteve answers and my answers. Then I edited it a bit, and now I think it's easier to understand, and more consistent in terms of vocabulary. |
…t one TreeGrower is private and only HGB's fit passes the heuristic thread count, so requiring both arguments only added churn to the tests. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
| If ``SKLEARN_OMP_WAIT_POLICY`` is set to ``"active"`` or ``"passive"`` | ||
| (case-insensitive), its value is used without any detection. It does not change | ||
| the wait policy of the OpenMP runtime itself. |
There was a problem hiding this comment.
| If ``SKLEARN_OMP_WAIT_POLICY`` is set to ``"active"`` or ``"passive"`` | |
| (case-insensitive), its value is used without any detection. It does not change | |
| the wait policy of the OpenMP runtime itself. | |
| If ``SKLEARN_OMP_WAIT_POLICY`` is set (by a user) to ``"active"`` or ``"passive"`` | |
| (case-insensitive), this value is used without any further automatic detection. Note, however, that it does not change | |
| the wait policy of the OpenMP runtime itself. |
If setting SKLEARN_OMP_WAIT_POLICY manually does not change the OpenMP runtime policy, what good does it do to set it manually?
There was a problem hiding this comment.
(good suggestion 👍 )
In case you know your OpenMP policy but the detection doesn't get it right (It's very niche, but I think Olivier wanted that, initially I had a private env var IIRC).
There was a problem hiding this comment.
The point is to provide easier control to debug this part of the code, improve testability and makes it possible to avoid redoing redundant runtime checks in each joblib/loky subprocess worker (e.g. when tuning the HGB parameters a gridsearch with n_jobs > 1).
If you prefer, we can set it back to an undocumented private env variable named _SKLEARN_OMP_WAIT_POLICY instead.
| # its settings) as soon as it is loaded, but libomp only on the first OpenMP | ||
| # call. | ||
| # | ||
| # TODO: move this detection to threadpoolctl. For libomp, it could read the |
There was a problem hiding this comment.
(but it might be painful as for libgomp we need to import a compiled extension using OpenMP, and for now threadpoolctl is pure Python...)
There was a problem hiding this comment.
We could make the threadpoolctl argument accept a module name argument or dynlib filename argument that would be in charge of triggering the openmp runtime load in the subprocess. This way threadpoolctl can stay pure python and only the caller (that actually exercises openmp enabled native extensions) can deal with this.
There was a problem hiding this comment.
However I would would not block this PR for this. I think we can do this refactoring later, after this PR's merge.
Note: making the _SKLEARN_OMP_WAIT_POLICY env var private as discussed in #34935 (comment) would actually make this refactoring easier to do (without any backward compat concerns) I think.
Co-authored-by: Christian Lorentzen <[email protected]>
| # sample-wise parallel regions start to dominate which could be the | ||
| # case on datasets with small number of features but very large | ||
| # numbers of data points. | ||
| n_threads_for_samples = min(0.1 * math.pow(n_samples, 1 / 3), max_n_threads) |
There was a problem hiding this comment.
Out of curiosity, any intuition on this formula? Was your agent somehow guided in the kind of heuristics it was trying?
There was a problem hiding this comment.
How I chose this y= f(x) transformation:
I had a few empirical (x, y) pairs and I wanted to find a formula to interpolate from those, while keeping it simple. But basically, you have to have a very large n_samples (and low n_features) for this to have an impact, because empirically, it's very rarely beneficial to grow the number of threads above n_features.
There was a problem hiding this comment.
There seems to be a resurgence of the cube root of n_samples these days 🤣
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Tiny merge conflicts to fix. |
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Co-Authored-By: Claude Fable 5.2 <[email protected]>
|
For information, I tried to rerun the original script of #30662 on my Apple M4 with the current PR and the most catastrophic slowdowns are fixed by this PR (without having to enable active wait): the worst slowdown is now 4x (instead of 15x) for the smallest problem size and as many threads as cores. For instance, here the first plot of that issue (executed on
now becomes (when run on this branch):
The Note that this machine has only 4 performance cores (out of 10), so I think those curves are ok. I also tried with
|
|
We could try to better handle non-performance cores in a follow-up PR but I think this PR is already a great improvement in its current state. |
| # Used by _openmp_uses_active_wait to parse the settings that the OpenMP runtime | ||
| # prints on stderr when OMP_DISPLAY_ENV=VERBOSE. Each pattern matches a value | ||
| # meaning that idle threads sleep right away instead of spinning, e.g. | ||
| # "[host] KMP_BLOCKTIME='0ms'" (libomp) or "GOMP_SPINCOUNT = '1'" (libgomp). |
There was a problem hiding this comment.
It would be nice to mention that GOMP_SPINCOUNT=1 is a bit of a special case that happens often enough on modern laptops because gcc>=14 sets GOMP_SPINCOUNT=1 for hybrid CPUs. Without such a comment, I find it hard to understand why we special case GOMP_SPINCOUNT=1.
I couldn't find a public gcc discussion about it, only the gcc commit or the same gcc commit in the GitHub mirror. There is also ggml-org/llama.cpp#31516.
There was a problem hiding this comment.
Good point 👍
I also mentioned libomp: it sets KMP_BLOCKTIME=0 on hybrid CPUs since LLVM 14 (no rationale given either).
This is the linux scheduler thing I started investigated, I think people (GCC and LLVM) adapted to it but nobody did the digging.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
I gave it another look, this looks in good shape, let's merge this one! |
|
🕺 🎉 🚀 Thank you everyone for the reviews! |



Finally, I have something that has a uniformly positive impact on the 8 scenarios I'm testing:
OMP_PROC_BIND=close(avoids penalizing runs that use fewer threads than what's available on the machine)(× libgomp & libomp for each of those)
And it can be very/hugely beneficial in some scenarios (2-10x speed-ups):
Reference Issues/PRs
Fixes #30662
Implements item 4 (partially), item 5 and item 6 of #34764 (comment)
I'd also say it closes #14306, though that issue does have some ideas that aren't implemented here, in particular parallelizing over blocks of samples × features when building histograms. Benchmarks show HGB does keep scaling up to 16-32 threads for many medium/big datasets, as long as active wait is enabled, which I'd personally consider good enough for now.
What does this implement/fix? Explain your changes.
_openmp_uses_active_waitinsklearn/utils/_openmp_helpers.pyx). This is done by parsing the environment dump the runtime prints underOMP_DISPLAY_ENV=VERBOSE, in a short-lived subprocess. Active wait makes rejoining a parallel region much cheaper, so all the heuristics below branch on it.BaseHistGradientBoosting._get_heuristic_optimal_n_threads), instead of always using every available thread._BinMapper'sn_threadsparameter is renamed tomax_n_threadssince it's now only an upper bound, the actual thread count used being derived from the amount of data to bin.Splitter.split_indices, fall back to a single-threaded partitioning pass when there isn't enough work per thread to justify the chunking/offset bookkeeping of the multi-threaded version.Splitter.find_node_split, when active wait is disabled, skip parallelizing over features when there are too few of them relative ton_threads.AI usage disclosure
Mostly not, except for the active wait detection: Claude helped find a way to make the subprocess-based
_openmp_uses_active_waitfast enough not to matter (result cached after the first call, subprocess kept minimal to avoid a fullimport sklearn).Benchmarks
main:https://pr-81.sklbench-dashboard-preview.pages.dev/
https://pr-141.sklbench-dashboard-preview.pages.dev/gbdt_pareto
I still have to run some tests on windows.