Repository navigation
Open
Labels
Description
When #32073 is merged, n_jobs will have no effect in LogisticRegression so should be deprecated.
It's also a good time to consider enabling multi-threading to compute the logistic regression path here
scikit-learn/sklearn/linear_model/_logistic.py
Lines 1373 to 1388 in 21e0df7
| # TODO: Refactor this to avoid joblib parallelism entirely when doing binary | |
| # and multinomial multiclass classification and use joblib only for the | |
| # one-vs-rest multiclass case. | |
| if ( | |
| solver in ["lbfgs", "newton-cg", "newton-cholesky"] | |
| and len(classes_) == 1 | |
| and effective_n_jobs(self.n_jobs) == 1 | |
| ): | |
| # In the future, we would like n_threads = _openmp_effective_n_threads() | |
| # For the time being, we just do | |
| n_threads = 1 | |
| else: | |
| n_threads = 1 | |
| fold_coefs_ = Parallel(n_jobs=self.n_jobs, verbose=self.verbose, prefer=prefer)( | |
| path_func( |
So far it was disabled because parallelism was happening at a higher level using joblib. Now we should benchmark the impact of enabling multi-threading to see if it's positive for all the solvers and a wide range of problems.
Like for other estimators using OpenMP-based multi-threading, the number of thread should not be controlled by n_jobs but instead use all available core (n_threads = _openmp_effective_n_threads()).
Activity
KoolDrip commentedon Sep 19, 2025
I’d like to take this up. Plan is to handle the
n_jobsdeprecation and then benchmark the impact of enabling multi-threading across different solvers and datasets. I’ll also look into using_openmp_effective_n_threads()consistently, as done in other estimators.FrancoisPgm commentedon Nov 17, 2025
Here are some results of a benchmark comparing
n_threads=1(denoted as n_thread False in the legends) ton_trheads=_openmp_effective_n_threads()(denoted as n_thread True in the legends) for fitting a logistic regression using the code from #32073. I ran the benchmark locally on my machine, limiting the number of available CPUs using eitherthreadpool_limitsin the script or aOMP_NUM_THREADSenvironment variable.I only tested the
lbfgs,newton-cgandnewton-choleskysolvers as the other ones don't taken_threadsas an argument.Selection of plots
All lbfgs plots
All newton-cg plots
All newton-cholesky plots
The conclusion I draw from the plots are :
lbfgswhen using more CPUs, with the fastest results with 4 CPUs and then we see some overhead slowdown for higher CPU counts. Fornewton-cgandnewton-choleskythere seem to be no significant difference.lbfgsandnewton-cgwith increasing CPU counts, and a saturation around 8 CPUs. Fornewton-cholesky, there are noticeable speedups only for the smallest values for number of featuers and classes, otherwise there is no clear difference.Additionally, it seems that when there are speedups, they diminish with increasing number of features (10 or 100) and increase with increasing number of classes (2, 8 or 32).
Here is the script I used for the benchmark
To summarize : the parallelization seems to improve the performance at higher sample sizes, especially for
lbfgsand to a lesser degree also fornewton-cg. There are slowdowns at low sample sizes due to overhead. The speedups seem greater for lower number of features and higher number of classes.lesteve commentedon Nov 18, 2025
FYI I created #32739 to do the simple part of deprecating
n_jobsinLogisticRegressionfor the 1.8 relase. We can keep this issue about evalutating multi-threading which is longer term.[-]Deprecate n_jobs in LogisticRegression and evaluate multi-threading[/-][+]Evaluate multi-threading `LogisticRegression`[/+]FrancoisPgm commentedon Nov 19, 2025
Here are the same plots as in my previous comment, but this time in term of speed-up (i.e. median time with
n_threads=1divided by the median time withn_threads=_openmp_effective_n_threads()).lbfgs plots
newton-cg plots
newton-cholesky plots
EDIT : added plots for 1e5 samples
sudoMakeMeCool commentedon Jan 28, 2026
System specifications:
The experiment:
I ran the script given by @FrancoisPgm to test three solvers - lbfgs, newton-cg, and newton-cholesky. I found that for lower sample sizes (100 and 10k), the lines in the graph are almost parallel to the X-axis, meaning multithreading provided negligible performance boost. For the 1M sample size, the duration became unstable, possibly due to a hardware bottleneck.
Observations:
n_cpu= 5 or 6, but zig-zags afterwards, likely due to memory bandwidth saturation.newton-choleskywithclass=32(for both feature 10 and 100), hence the empty plots.Interpretation:
The zig-zag pattern for higher sample sizes indicates that blindly increasing threads can introduce instability. Interestingly,
lbfgsappears to be the most consistent solver across different tasks.Graphs plotted:
lbfgs
Newton-cg
Newton-cholesky
No data recorded for class = 32
Vivek-ML001 commentedon May 14, 2026
Benchmark results — Windows (Intel Core i7, 16GB DDR5)
System specs:
I ran the benchmark script from @FrancoisPgm comparing
n_threads=1vsn_threads=4(viaOMP_NUM_THREADS) for thelbfgs,newton-cg, andnewton-choleskysolvers across sample sizes of 100, 10k, and 1M, with 10 and 100 features.Observations:
lbfgs: 1.24s → 1.12s (~1.1x speedup)newton-cholesky: 1.36s → 1.17s (~1.16x speedup)newton-cg: 2.67s → 2.18s (~1.22x speedup)Summary:
The results on this Windows/Intel machine are more modest than the Linux results reported by @FrancoisPgm. Speedups at 1M samples are present but small (~1.1–1.2x), and disappear or reverse at higher feature counts. This is consistent with the memory bandwidth saturation pattern observed by @CipherCat7134.
This suggests the benefit of enabling multi-threading may be hardware/OS dependent, and a threshold-based heuristic (e.g. only enable for large
n_samplesand smalln_features) could be worth considering.Graphs plotted:
ogrisel commentedon May 20, 2026
Could you please expand the benchmark to cover large
n_classescases (e.g.n_classes >= n_features) and varyingn_samples?cakedev0 commentedon Sep 7, 2026
Enabling multi-threading here would likely be very detrimental in some not-so-rare set-ups, as alternating between OpenMP parallel regions and OpenBLAS parallel regions can be very bad. See #17334.
cakedev0 commentedon Sep 29, 2026
FYI,
LogisticRegressionwith lbfgs solver currently suffers for a bad interactions between numpy's openBLAS and scipy's (older) openBLAS on PyPI: scipy/scipy#26193 (comment)