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
Threads awaiting for GIL in Forest estimators #20666
In forest algorithms, the preferred parallelization backend is threading. However, it looks that it is not anymore the most appropriate backend. As discussed here, it might be that the GIL is not explicitly released in some part of the code locking the execution of the thread.
From #20651 (reply in thread), two small optimizations could improve the scalability when using threads:
avoiding the redundant check_random_state in BaseDecisionTree.fit as it's already initialized by the forest when bootstrap to data for the tree specifically.
avoiding the assert_all_finite in _check_sample_weight in BaseDecisionTree.fit.
For reference, here is the extract of the sequential execution profiling:
I think threading is still the most appropriate backend by default. In #20651 the individual trees are very fast to train (4ms) and many trees are fitted (1000) which is not representative of the typical use case.
If the tree fitting time lasted 1s or more, then the GIL-holding segments of the code would be negligible and the thread-based scalability would be fine.
I agree, it's dataset dependent. Switch out load_boston for fetch_california in my example to get a larger training set, and I see the expected speedup cross-platform.
@Bhavay192 Feel free to have a look at the code of the random forests and the decision trees to try to open a PR to solve one item of #20666 (comment) at a time to keep the PR focused.
Activity
jjerphan commentedon Aug 3, 2021
From #20651 (reply in thread), two small optimizations could improve the scalability when using threads:
check_random_stateinBaseDecisionTree.fitas it's already initialized by the forest when bootstrap to data for the tree specifically.assert_all_finitein_check_sample_weightinBaseDecisionTree.fit.For reference, here is the extract of the sequential execution profiling:

ogrisel commentedon Aug 4, 2021
I think threading is still the most appropriate backend by default. In #20651 the individual trees are very fast to train (4ms) and many trees are fitted (1000) which is not representative of the typical use case.
If the tree fitting time lasted 1s or more, then the GIL-holding segments of the code would be negligible and the thread-based scalability would be fine.
brianbien commentedon Aug 4, 2021
I agree, it's dataset dependent. Switch out
load_bostonforfetch_californiain my example to get a larger training set, and I see the expected speedup cross-platform.Bhavay-2001 commentedon Aug 18, 2021
hey @ogrisel @brianbien , could you please help me resolving this issue? I would like to contribute to it
ogrisel commentedon Nov 2, 2021
@Bhavay192 Feel free to have a look at the code of the random forests and the decision trees to try to open a PR to solve one item of #20666 (comment) at a time to keep the PR focused.
Feel free to ask specific questions on gitter if you need more help: https://gitter.im/scikit-learn/scikit-learn
ogrisel commentedon Oct 4, 2024
Before opening a PR for this issue, we should reevaluate with a free-threading build of scikit-learn on CPython 3.13.
Here are some resources to get started: https://py-free-threading.github.io/
Scikit-learn's CI also automatically publishes nightly wheels for
cp313tas numpy and scipy (along with the other nightly wheels):https://scikit-learn.org/stable/developers/advanced_installation.html#installing-nightly-builds
itamarst commentedon Sep 28, 2026
I tested with free-threaded Python (3.14t) and it's way better than normal 3.14 for the Boston data set. Specifically, comparing:
on CPU with 8 performance cores, 4 efficiency cores.
3.14 with GIL
11.302873849868774 (n_jobs=12)
29.423907041549683 (n_jobs=1)
3.14 free-threaded
4.697827577590942 (n_jobs=12)
30.43602180480957 (n_jobs=1)
itamarst commentedon Sep 28, 2026
That being said, looking at a profiler it does seem like the two suggestions above would speed things up, even without a GIL.