Skip to content

FIX Dont assume available CPU core count never changes - #35062

Merged
ogrisel merged 4 commits into
scikit-learn:mainfrom
itamarst:dont-assume-cpu_count-is-idempotent
Dec 1, 2026
Merged

ogrisel merged 4 commits into
scikit-learn:mainfrom
itamarst:dont-assume-cpu_count-is-idempotent

Conversation

@itamarst

@itamarst itamarst commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Currently _openmp_effective_n_threads() assumes the number of available cores never changes.

This is not a correct assumption even now; the new test in this PR fails without the corresponding code changes. Changing CPU affinity at runtime is to be fair not very likely, even if it possible.

However, once joblib has a new release, this assumption will be a much bigger problem, since thread workers in joblib.Parallel will have different result in cpu_count() than e.g. the main thread. That is, cpu_count() will start being thread-local and tied to how many workers there are, with cores divided up between workers. See joblib/joblib#1825 for details.

This PR therefore removes the caching.

@itamarst
itamarst marked this pull request as ready for review October 1, 2026 18:59
@itamarst
itamarst marked this pull request as draft October 1, 2026 19:09
@itamarst
itamarst marked this pull request as ready for review October 1, 2026 19:39
@cakedev0
cakedev0 self-requested a review October 2, 2026 14:37
Comment thread sklearn/utils/_openmp_helpers.pyx Resolved

@cakedev0 cakedev0 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.

LGTM

@cakedev0 cakedev0 added the Waiting for Second Reviewer First reviewer is done, need a second one! label Oct 27, 2026
cakedev0 added a commit to cakedev0/my-scikit-learn-backlog that referenced this pull request Nov 2, 2026
done: Blog: [blog#231](scikit-learn/blog#231) (match Substack's font size), merged
todo: move [blog#228](scikit-learn/blog#228) from Waiting to Now: address lorentzenchr's review and answer jjerphan on the OpenBLAS threading layers
todo: move [sklearn#35181](scikit-learn/scikit-learn#35181) from Now to Waiting, adam2392's and antoinebaker's questions answered
add: Maintenance: follow up on [sklearn#35254](scikit-learn/scikit-learn#35254): settle option B with dherrera1911 and betatim (LDA solvers disagree with custom priors)
add: Maintenance: follow up on [sklearn#35209](scikit-learn/scikit-learn#35209): newton-cg LogisticRegressionCV test fails on the macOS x86_64 wheels
add: Maintenance: re-review [sklearn#34619](scikit-learn/scikit-learn#34619) (faster PCA mle; jberg5 addressed my comments)
add: Benchmarks: review [benchmarks#142](probabl-ai/scikit-learn-benchmarks#142) (cuML config) with the results of the AWS L4 run
add: Upstream: upstream to OpenBLAS or close [cakedev0/OpenBLAS#3](cakedev0/OpenBLAS#3) and [cakedev0/OpenBLAS#4](cakedev0/OpenBLAS#4)
todo: since dates on [sklearn#35062](scikit-learn/scikit-learn#35062) and [sklearn#35053](scikit-learn/scikit-learn#35053), and virchan's question on the latter
todo: lorentzenchr's note on [sklearn#35029](scikit-learn/scikit-learn#35029) in the fork#38 line

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@cakedev0

Copy link
Copy Markdown
Member

For users that will upgrade only scikit-learn but not joblib, how much this change would cost them (if we merge this before 1.10)?

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

lgtm

@itamarst

Copy link
Copy Markdown
Contributor Author

For users that will upgrade only scikit-learn but not joblib, how much this change would cost them (if we merge this before 1.10)?

It was 6, 11, and 187 usec on Linux, macOS, and Windows respectively per call. And it's called right before an expensive operation (otherwise why use parallelism?) so should be amortized into irrelevance.

@cakedev0

Copy link
Copy Markdown
Member

Ah ok, I thought we could change that in scikit-learn because of a recent optimization in joblib/loky, but it's actually not recent. Ok, @thomasjpfan feel free to merge.

@thomasjpfan
thomasjpfan enabled auto-merge (squash) November 24, 2026 21:03
@ogrisel

ogrisel commented Nov 30, 2026

Copy link
Copy Markdown
Member

@itamarst ping me once the conflicts are resolved and I'll merge.

auto-merge was automatically disabled November 30, 2026 16:42

Head branch was pushed to by a user without write access

@itamarst

Copy link
Copy Markdown
Contributor Author

@ogrisel I merged forward, thank you.

@ogrisel
ogrisel enabled auto-merge (squash) December 1, 2026 08:51
@ogrisel
ogrisel merged commit 03e5fe6 into scikit-learn:main Dec 1, 2026
43 of 44 checks passed
@itamarst

itamarst commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

@betatim would be nice to get this into 1.10 since once new joblib is released this will impact performance.

@itamarst
itamarst deleted the dont-assume-cpu_count-is-idempotent branch December 1, 2026 16:11
@betatim

betatim commented Dec 1, 2026

Copy link
Copy Markdown
Member

This PR has been added to the backports PR. Lets see how big that PR gets and then decide if we need a rc2 or can directly release.

Out of interest: what is joblib's plan for dealing with people who install scikitl-learn 1.9.x and the latest joblib (that will include the changes that make this PR something we want to include in 1.10)?

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants