Skip to content

PERF Use threads for LogisticRegressionCV parallelism on free threaded Python - #34659

Merged
jeremiedbb merged 13 commits into
scikit-learn:mainfrom
itamarst:logistic-regression-free-threading
Sep 14, 2026
Merged

jeremiedbb merged 13 commits into
scikit-learn:mainfrom
itamarst:logistic-regression-free-threading

Conversation

@itamarst

@itamarst itamarst commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor

On free-threaded Python, parallelism with threads is often faster due to reduced overhead. So it's useful to use threads when there is no GIL.

In this case, I added a benchmark, ran it with main's algorithm (threads only for sag and saga) and with the PR (everything is threads), and combined the tables. As one would expect, sag and saga don't change much (and any change is just noise), but the other solvers run faster when switched to threads. Lower is better:

n_jobs solver elapsed (main branch) elapsed (this PR)
1 lbfgs 2.712 2.692
4 lbfgs 2.118 0.889
8 lbfgs 2.136 0.723
1 liblinear 7.022 7.308
4 liblinear 3.79 2.275
8 liblinear 3.497 2.054
1 newton-cd-gram 3.326 3.314
4 newton-cd-gram 2.7 1.117
8 newton-cd-gram 2.351 0.875
1 newton-cg 3.188 3.141
4 newton-cg 2.749 1.047
8 newton-cg 2.477 0.809
1 newton-cholesky 2.955 2.98
4 newton-cholesky 2.501 0.995
8 newton-cholesky 2.235 0.826
1 sag 6.665 6.604
4 sag 2.053 2.042
8 sag 1.425 1.426
1 saga 7.104 6.955
4 saga 2.202 2.17
8 saga 1.504 1.505

Benchmark script:

"""
Test performance of LogisticRegressionCV fitting with different solvers and
parallelism levels.
"""

from time import time

import numpy as np

from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegressionCV
from sklearn.model_selection import train_test_split

X, y = make_classification(
    n_samples=100_000, n_features=20, n_informative=2, n_redundant=2, random_state=42
)

train_samples = 10_000  # Samples used for training the models
X_train, X_test, y_train, y_test = train_test_split(
    X,
    y,
    shuffle=False,
    test_size=100_000 - train_samples,
)

print("| n_jobs | solver          | elapsed |")
print("|--------|-----------------|---------|")

for solver in [
    "lbfgs",
    "liblinear",
    "newton-cd-gram",
    "newton-cg",
    "newton-cholesky",
    "sag",
    "saga",
]:
    for n_jobs in [1, 4, 8]:
        lr = LogisticRegressionCV(
            Cs=np.logspace(-6, 6, 101),
            cv=10,
            l1_ratios=(0,),
            scoring="neg_log_loss",
            max_iter=1_000,
            use_legacy_attributes=False,
            n_jobs=n_jobs,
            solver=solver,
        )
        start = time()
        lr.fit(X_train, y_train)
        print(f"|{n_jobs:8}| {solver:16}|{round(time() - start, 3):9}|")

@itamarst
itamarst marked this pull request as ready for review August 5, 2026 21:45
@itamarst

itamarst commented Aug 6, 2026

Copy link
Copy Markdown
Contributor Author

I increased the training set to reduce some of the overhead and give a somewhat fairer comparison.

Comment thread sklearn/utils/parallel.py Resolved
@itamarst
itamarst requested a review from lesteve September 4, 2026 11:54

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

Overall LGTM, except for a wrong default value.

Comment thread sklearn/utils/parallel.py Resolved
@itamarst
itamarst requested a review from cakedev0 September 4, 2026 17:01

@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

@lesteve lesteve added this to Labs Sep 9, 2026
@github-project-automation github-project-automation Bot moved this to Todo in Labs Sep 9, 2026
@lesteve lesteve moved this from Todo to PR waiting for reviews in Labs Sep 9, 2026

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

Thanks for the PR. Looks good

Comment thread sklearn/utils/parallel.py Resolved
@itamarst
itamarst requested a review from jeremiedbb September 10, 2026 14:20

@jeremiedbb jeremiedbb 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 Thanks

@jeremiedbb
jeremiedbb merged commit 7ed8c95 into scikit-learn:main Sep 14, 2026
37 checks passed
@github-project-automation github-project-automation Bot moved this from PR waiting for reviews to Done in Labs Sep 14, 2026
@itamarst
itamarst deleted the logistic-regression-free-threading branch September 22, 2026 14:03
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