You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Dictionary learning is slower with n_jobs > 1 #4769
Distorting image...
Extracting reference patches...
done in 0.05s.
Learning the dictionary...
done in 5.12s.
Extracting noisy patches...
done in 0.02s.
Lasso LARS...
done in 10.24s.
Output n_jobs == 2
Distorting image...
Extracting reference patches...
done in 0.05s.
Learning the dictionary...
done in 78.98s.
Extracting noisy patches...
done in 0.02s.
Lasso LARS...
done in 6.15s.
Output n_jobs == 4
Distorting image...
Extracting reference patches...
done in 0.05s.
Learning the dictionary...
done in 83.24s.
Extracting noisy patches...
done in 0.02s.
Lasso LARS...
done in 3.82s.
We can see that transform function of MiniBatchDictionaryLearning (relying on sparse_encode function) benefits from multi-processing as expected.
Dictionary learning relies on successive calls of sparse_encode function : slowness may come from this.
which is typically of the same order than n_jobs. sparse_encode computation time may be dominated by joblib overhead and not by the lasso computation time itself.
Agreed. Have run into this myself as well. Basically Joblib would normally make sense for finding the sparse code for a precomputed dictionary. However in this specific instance, it is being called in a tight loop as part of dictionary learning. The overhead of starting Joblib in this tight loop is too costly. One is better off using the threaded parallelism that BLAS provides instead.
Edit: Alternatively it may make sense to start the Joblib thread or multiprocessing pool before the tight loop and keep it running so that the tight loop merely schedules jobs for this pool. Have not tested this latter approach, but it may work reasonably well.
The joblib context manager API is implemented and available in #5016.
We did some profiling with @arthurmensch and apparently we should benefit a lot from skipping redundant inner input data validation checks by introducing a check_input=False flag for the functions called inside the fit loop (the default being check_input=True). Input validation takes a large fraction of the time and causes unnecessary GIL contention when using the threading backend.
FWIW there has been a lot of work on this (particularly in the past few weeks) and IMHO things are getting better.
The main things that remain slow on the sparse coding side are this copy and _pre_fit generally (though ~75% of that is just the aforementioned copy). A smaller amount of time is also spent in this asfortranarray call, but that is largely insignificant by comparison. It is worth noting that more time is spent in _pre_fit than enet_path (where the bulk of sparse coding happens). However _pre_fit and enet_path are generally more comparable now, which is a significant improvement.
Most of these improvements have involved skipping the assert_all_finite check, which is rather slow. More details on why that check is slow can be found in issue ( #11681 ).
done in 5.199s, with n_jobs = 1.
done in 6.903s, with n_jobs = 2.
done in 8.619s, with n_jobs = 3.
done in 8.418s, with n_jobs = 4.
done in 9.727s, with n_jobs = 10.
Thanks for rechecking this. That is a good data point to have.
I had tried to Cythonize a good chunk of code and release the GIL for the bulk of it with PR ( #11874 ). Maybe that helps? Unfortunately don't have much time to work on that PR these days.
Some investigation suggests the overhead from joblib is a big part of the problem, once the GIL is bypassed.
The problem is this doing many tiny parallel chunks of work, such that the parallelism framework's overhead is a big hit.
On free-threaded Python (3.14), where GIL is not an issue, I can get a speed up from parallelism if I switch from joblib.Parallel to concurrent.futures.ThreadPoolExecutor. So this another use case for #34914.
joblib.Parallel:
done in 3.502s, with n_jobs = 1.
done in 2.870s, with n_jobs = 4.
done in 3.484s, with n_jobs = 8.
ThreadPoolExecutor:
done in 3.477s, with n_jobs = 1.
done in 2.274s, with n_jobs = 4.
done in 1.308s, with n_jobs = 8.
ThreadPoolExecutor, reusing the pool instead of creating a new one each time:
done in 3.457s, with n_jobs = 1.
done in 1.117s, with n_jobs = 4.
done in 0.751s, with n_jobs = 8.
Activity
arthurmensch commentedon May 26, 2015
Incriminated line can be located l. 692 of dict_learning.py
We have :
which is typically of the same order than n_jobs. sparse_encode computation time may be dominated by joblib overhead and not by the lasso computation time itself.
arthurmensch commentedon May 27, 2015
Fix in pull request #4773
martinosorb commentedon Jun 5, 2015
I believe I'm seeing this problem here: #4779
jakirkham commentedon Jul 4, 2018
Agreed. Have run into this myself as well. Basically Joblib would normally make sense for finding the sparse code for a precomputed dictionary. However in this specific instance, it is being called in a tight loop as part of dictionary learning. The overhead of starting Joblib in this tight loop is too costly. One is better off using the threaded parallelism that BLAS provides instead.
Edit: Alternatively it may make sense to start the Joblib thread or multiprocessing pool before the tight loop and keep it running so that the tight loop merely schedules jobs for this pool. Have not tested this latter approach, but it may work reasonably well.
amueller commentedon Aug 21, 2018
from #4773 (comment)
The joblib context manager API is implemented and available in #5016.
We did some profiling with @arthurmensch and apparently we should benefit a lot from skipping redundant inner input data validation checks by introducing a check_input=False flag for the functions called inside the fit loop (the default being check_input=True). Input validation takes a large fraction of the time and causes unnecessary GIL contention when using the threading backend.
jakirkham commentedon Aug 21, 2018
FWIW there has been a lot of work on this (particularly in the past few weeks) and IMHO things are getting better.
The main things that remain slow on the sparse coding side are this copy and
_pre_fitgenerally (though ~75% of that is just the aforementionedcopy). A smaller amount of time is also spent in thisasfortranarraycall, but that is largely insignificant by comparison. It is worth noting that more time is spent in_pre_fitthanenet_path(where the bulk of sparse coding happens). However_pre_fitandenet_pathare generally more comparable now, which is a significant improvement.Most of these improvements have involved skipping the
assert_all_finitecheck, which is rather slow. More details on why that check is slow can be found in issue ( #11681 ).jakirkham commentedon Aug 21, 2018
On the Joblib side, would expect PR ( https://github.com/tomMoral/loky/pull/135 ) ends up being very helpful here as the bigger issue is likely thread contention between the BLAS used and Joblib in sparse coding.
cmarmo commentedon Jan 11, 2021
The gists are no longer available.
The following code
gives the following output
With the following environment:
It seems that this issue is still relevant?
jakirkham commentedon Jan 11, 2021
Thanks for rechecking this. That is a good data point to have.
I had tried to Cythonize a good chunk of code and release the GIL for the bulk of it with PR ( #11874 ). Maybe that helps? Unfortunately don't have much time to work on that PR these days.
adrinjalali commentedon Aug 22, 2021
Removing the milestone. Maybe @jeremiedbb would like to have a look?
itamarst commentedon Sep 28, 2026
Some investigation suggests the overhead from joblib is a big part of the problem, once the GIL is bypassed.
The problem is this doing many tiny parallel chunks of work, such that the parallelism framework's overhead is a big hit.
On free-threaded Python (3.14), where GIL is not an issue, I can get a speed up from parallelism if I switch from
joblib.Paralleltoconcurrent.futures.ThreadPoolExecutor. So this another use case for #34914.joblib.Parallel:ThreadPoolExecutor:ThreadPoolExecutor, reusing the pool instead of creating a new one each time: