Skip to content

Threads awaiting for GIL in Forest estimators #20666

Open

Description

@glemaitre

Discussed in #20651

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.

We need to investigate more to solve this issue.

Activity

  1. jjerphan commented on Aug 3, 2021

    @jjerphan
    Member

    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:
    Screenshot 2021-08-03 at 18-42-47 viztracer json (160 MB) - Perfetto UI

  2. ogrisel commented on Aug 4, 2021

    @ogrisel
    Member

    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.

  3. brianbien commented on Aug 4, 2021

    @brianbien

    not representative

    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.

  4. Bhavay-2001 commented on Aug 18, 2021

    @Bhavay-2001

    hey @ogrisel @brianbien , could you please help me resolving this issue? I would like to contribute to it

  5. ogrisel commented on Nov 2, 2021

    @ogrisel
    Member

    @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

  6. added
    free-threading
    on Oct 4, 2024
  7. ogrisel commented on Oct 4, 2024

    @ogrisel
    Member

    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 cp313t as numpy and scipy (along with the other nightly wheels):

    https://scikit-learn.org/stable/developers/advanced_installation.html#installing-nightly-builds

  8. itamarst commented on Sep 28, 2026

    @itamarst
    Contributor

    I tested with free-threaded Python (3.14t) and it's way better than normal 3.14 for the Boston data set. Specifically, comparing:

    rf_multi = RandomForestRegressor(n_estimators=N_EST, n_jobs=-1)
    rf = RandomForestRegressor(n_estimators=N_EST, n_jobs=1)

    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)

  9. itamarst commented on Sep 28, 2026

    @itamarst
    Contributor

    That being said, looking at a profiler it does seem like the two suggestions above would speed things up, even without a GIL.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Performancefree-threadingmodule:ensemble

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions