Skip to content

FIX Use the fixed "sequential_blas_under_openmp" with threadpoolctl < 3.8 - #35547

Merged
lesteve merged 4 commits into
scikit-learn:mainfrom
cakedev0:vendor-sequential-blas-under-openmp
Dec 1, 2026
Merged

lesteve merged 4 commits into
scikit-learn:mainfrom
cakedev0:vendor-sequential-blas-under-openmp

Conversation

@cakedev0

@cakedev0 cakedev0 commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

Reference Issues/PRs

Follow-up of #35180, see #35333 (comment).

What does this implement/fix? Explain your changes.

#35180 switched the BLAS limits under OpenMP to limits="sequential_blas_under_openmp". With threadpoolctl < 3.8, this option leaves all BLAS libraries unlimited as soon as OpenBLAS is loaded, whatever its threading layer (fixed in joblib/threadpoolctl#283). So on main with the pip wheels (OpenBLAS pthreads) and threadpoolctl 3.5 to 3.7, KMeans and the pairwise distances reductions run multithreaded GEMMs inside their OpenMP loops.

This vendors the small fix made in threadpoolctl 3.8 and keeps the code of #35180 as is, and the fix is one block to delete later. I think it's the better option, WDYT?

No changelog entry since #35180 isn't released yet.

AI usage disclosure

I used AI assistance for:

  • Code generation
  • Test/benchmark generation
  • Research and understanding

@cakedev0

Copy link
Copy Markdown
Member Author

cc @itamarst

@itamarst itamarst 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 seems reasonable and tightly scoped.

@lesteve

lesteve commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

Is the CI issue related to the PR https://github.com/scikit-learn/scikit-learn/actions/runs/45988194813/job/140469534852?pr=35547?

___________________ test_kmeans_blas_limited_in_openmp_loop ____________________
[gw0] darwin -- Python 3.14.10 /Users/runner/miniconda3/envs/testvenv/bin/python

monkeypatch = <_pytest.monkeypatch.MonkeyPatch object at 0x159133410>

    @pytest.mark.skipif(
        not any(i for i in threadpool_info() if i["user_api"] == "blas"),
        reason="No BLAS library detected by threadpoolctl.",
    )
    def test_kmeans_blas_limited_in_openmp_loop(monkeypatch):
        """Check that BLAS is limited to 1 thread in the OpenMP loop of Lloyd.
    
        Non-regression test: `limits="sequential_blas_under_openmp"` left BLAS unlimited
        with threadpoolctl < 3.8 as soon as OpenBLAS was loaded.
        """
        blas_n_threads = []
        lloyd_iter_chunked_dense = _kmeans.lloyd_iter_chunked_dense
    
        def spy_lloyd_iter(*args, **kwargs):
            blas_n_threads.extend(
                info["num_threads"]
                for info in threadpool_info()
                if info["user_api"] == "blas"
            )
            return lloyd_iter_chunked_dense(*args, **kwargs)
    
        monkeypatch.setattr(_kmeans, "lloyd_iter_chunked_dense", spy_lloyd_iter)
        X = np.random.RandomState(0).rand(100, 5)
        KMeans(n_clusters=3, n_init=1, max_iter=2, random_state=0).fit(X)
    
        assert blas_n_threads
>       assert set(blas_n_threads) == {1}
E       assert {3} == {1}
E         
E         Extra items in the left set:
E         3
E         Extra items in the right set:
E         1
E         
E         Full diff: (-: missing in left side, +: extra in left side)
E           {
E         -     1,
E         ?     ^
E         +     3,
E         ?     ^
E           }

X          = array([[0.5488135 , 0.71518937, 0.60276338, 0.54488318, 0.4236548 ],
       [0.64589411, 0.43758721, 0.891773  , 0.963... 0.59223042, 0.89676116, 0.40673335, 0.55207828],
       [0.27165277, 0.45544415, 0.40171354, 0.24841347, 0.50586638]])
blas_n_threads = [3, 3, 3]
lloyd_iter_chunked_dense = <cyfunction lloyd_iter_chunked_dense at 0x11a3d63f0>
monkeypatch = <_pytest.monkeypatch.MonkeyPatch object at 0x159133410>
spy_lloyd_iter = <function test_kmeans_blas_limited_in_openmp_loop.<locals>.spy_lloyd_iter at 0x140fa90c0>

../sklearn/cluster/tests/test_k_means.py:1043: AssertionError

@cakedev0

cakedev0 commented Nov 30, 2026 •

Copy link
Copy Markdown
Member Author

Yes, it's the test I added that is wrong: this job is the only one with OpenBLAS on the OpenMP threading layer, and there "sequential_blas_under_openmp" leaves BLAS unlimited on purpose: when OpenBLAS is built with the OpenMP threading layer, it is already sequential when called from an OpenMP parallel region.

I now skip the test in that case.

@lesteve lesteve 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. This seems a pragmatic approach.

In general, we use sklearn.utils.fixes for this kind of shims depending on the versions but it looks like sklearn.utils.fixes imports sklearn.utils.parallel so that makes it not worth it.

@lesteve
lesteve merged commit 79129f1 into scikit-learn:main Dec 1, 2026
39 checks passed
@github-project-automation github-project-automation Bot moved this from PR waiting for reviews to Done in Labs Dec 1, 2026
@cakedev0

cakedev0 commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

Thanks!

How to make sure this will be in the 1.10 release? (knowing the rc is out already)

@cakedev0
cakedev0 deleted the vendor-sequential-blas-under-openmp branch December 1, 2026 13:53
@lesteve

lesteve commented Dec 1, 2026

Copy link
Copy Markdown
Member

Now that the 1.10.X branch has been created it's up to the release manager (@betatim for the 1.10 release) to decide which PRs he picks when we are close to the release and he does the PR targetting the 1.10.X branch. See https://scikit-learn.org/stable/developers/maintainer.html#reference-steps (click on major/minor final) for more details.

I guess this PR is a good candidate to be picked, since it fixes a real problem and is small with somewhat low-risk, but I am not the release manager 😉.

@betatim

betatim commented Dec 1, 2026

Copy link
Copy Markdown
Member

This PR is in the backports PR. Let's see how big that PR gets and based on that decide if we need a rc2 or can go directly to a release.

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.

4 participants