Skip to content

Explicit documentation / contract for free-threading #34596

Open

Description

@david-cortes-intel

Warning

This issue is not yet ready for a PR. If you are interested in contributing to scikit-learn, please have a look at our contributing guidelines, and in particular the sections for new contributors and the "Needs triage" label.

Describe the issue linked to the documentation

Scikit-learn offers builds for free-threaded Python, but it is not clear as a user what is supposed to be safe to call from multiple threads.

For example, it feels quite obvious that one should not call .fit() on the same object from multiple threads even if not documented, but does the interface guarantee that other methods are safe to call on the same object from multiple threads? For instance, KNN depending on the arguments might do most of the work in other methods, and I'm not sure if it doesn't modify any internal attributes starting with underscores.

Further, since lots of other Python libraries offer scikit-learn-compatible objects, it would be quite useful to have official documentation from scikit-learn about what needs or doesn't need to be thread-safe in order to develop scikit-learn-compatible classes.

Suggest a potential alternative/fix

Add a documentation page for free threading covering all these kinds of details, plus additional documentation on a per-estimator basis if needed for exceptions. Mention these thread safety requirements also in the developer guide.

Activity

  1. Tharunbilluri commented on Jul 29, 2026

    @Tharunbilluri
  2. ogrisel commented on Aug 6, 2026

    @ogrisel
    Member

    The general policy should be:

    • calling fit concurrently on the same estimator instance is not guaranteed to be thread-safe;
    • "inference" methods such as predict, predict_proba, decision_function, transform, score... should not modify internal attributes and should all be thread-safe. I think we considered violations to this implicit rule as bugs in the past.

    However, I don't think this is explicitly written anywhere nor systematically tested in a dedicated estimator check run as part of the common tests.

    Maybe we could maybe write a common check such as:

    1. fit an estimator instance on suitable data;
    2. call joblib.hash on the estimator and store the result;
    3. call predict/predict_proba/decision_function/transform/score when applicable;
    4. call joblib.hash again to check that the internal state of the estimator wasn't mutated.

    This is not enough to guarantee thread-safety of those methods but it's cheaper to run and easier to debug than a thread-safety stress test. I think we could open a draft PR to implement such a common test and analyze the results. If there are no (or very few) detected violations, we could document the above policy to make it official. If there are many detected violations, we will have to decide what to do before proceeding further.

    Note: there is a very related draft PR #34057 that proposes a similar strategy for testing thread-safety/mutations but that PR has a focus on the thread-safety analysis of function that are called in parallel via joblib.Parallel within the scikit-learn code base which is not exactly the same objective.

  3. added
    Needs Investigation
    free-threading
    and removed
    Needs Triage
    on Aug 6, 2026
  4. lesteve commented on Aug 7, 2026

    @lesteve
    Member

    On the documentation side, I think this is a bit early, as we are pretty much figuring this as we make progress on free-threaded support in scikit-learn and joblib, loky, etc ...

    As inspiration, I would be curious if you find other projects that have already documented some kind of contracts for free-threaded, maybe numpy?

  5. david-cortes-intel commented on Aug 7, 2026

    @david-cortes-intel
    ContributorAuthor
  6. Tharunbilluri commented on Aug 10, 2026

    @Tharunbilluri

    Thanks, the NumPy page is a helpful reference.

    For scikit-learn, I think a small first step could be a scoped docs page rather than a full per-estimator audit: document that concurrent calls to mutating methods such as fit / partial_fit on the same estimator instance are not guaranteed to be thread-safe, while inference methods such as predict, transform, decision_function, predict_proba, and score are expected to be side-effect free and safe to call concurrently on a fitted estimator, unless an estimator-specific exception is documented.

    I can open a draft PR with that limited wording plus a short developer-guide note if this scope sounds reasonable.

  7. ogrisel commented on Aug 10, 2026

    @ogrisel
    Member

    I would rather conduct the audit before stating anything in the doc.

  8. itamarst commented on Aug 11, 2026

    @itamarst
    Contributor

    I will implement that draft PR.

  9. itamarst commented on Aug 11, 2026

    @itamarst
    Contributor

    I've implemented a first pass at #34729 - the failures I'm seeing locally are:

    FAILED sklearn/tests/test_common.py::test_estimators[Isomap()-check_estimator_internal_state_unchanged_by_inference] - AssertionError: Hash of Isomap() changed when running transform()
    FAILED sklearn/tests/test_common.py::test_estimators[KNeighborsClassifier(n_neighbors=2)-check_estimator_internal_state_unchanged_by_inference] - AssertionError: Hash of KNeighborsClassifier(n_neighbors=2) changed when running predict()
    FAILED sklearn/tests/test_common.py::test_estimators[KNeighborsRegressor(n_neighbors=2)-check_estimator_internal_state_unchanged_by_inference] - AssertionError: Hash of KNeighborsRegressor(n_neighbors=2) changed when running predict()
    FAILED sklearn/tests/test_common.py::test_estimators[KNeighborsTransformer()-check_estimator_internal_state_unchanged_by_inference] - AssertionError: Hash of KNeighborsTransformer() changed when running transform()
    FAILED sklearn/tests/test_common.py::test_estimators[KernelDensity()-check_estimator_internal_state_unchanged_by_inference] - AssertionError: Hash of KernelDensity() changed when running score()
    FAILED sklearn/tests/test_common.py::test_estimators[LocallyLinearEmbedding(max_iter=5)-check_estimator_internal_state_unchanged_by_inference] - AssertionError: Hash of LocallyLinearEmbedding(max_iter=5, random_state=0) changed when running transform()
    FAILED sklearn/tests/test_common.py::test_estimators[RadiusNeighborsClassifier()-check_estimator_internal_state_unchanged_by_inference] - AssertionError: Hash of RadiusNeighborsClassifier() changed when running predict()
    FAILED sklearn/tests/test_common.py::test_estimators[RadiusNeighborsRegressor()-check_estimator_internal_state_unchanged_by_inference] - AssertionError: Hash of RadiusNeighborsRegressor() changed when running predict()
    FAILED sklearn/tests/test_common.py::test_estimators[RadiusNeighborsTransformer()-check_estimator_internal_state_unchanged_by_inference] - AssertionError: Hash of RadiusNeighborsTransformer() changed when running transform()
    FAILED sklearn/tests/test_common.py::test_estimators[SparseCoder(dictionary=array([[1.76405235,0.40015721,0.97873798],[2.2408932,1.86755799,-0.97727788],[0.95008842,-0.15135721,-0.10321885],[0.4105985,0.14404357,1.45427351],[0.76103773,0.12167502,0.44386323]]))-check_estimator_internal_state_unchanged_by_inference] - ValueError: Dictionary and X have different numbers of features:dictionary.shape: (5, 3) X.shape: (80, 2)FAILED sklearn/tests/test_common.py::test_estimators[SplineTransformer()-check_estimator_internal_state_unchanged_by_inference] - AssertionError: Hash of SplineTransformer() changed when running transform()
    FAILED sklearn/tests/test_common.py::test_estimators[SplineTransformer(handle_missing='zeros')-check_estimator_internal_state_unchanged_by_inference] - AssertionError: Hash of SplineTransformer(handle_missing='zeros') changed when running transform()
    
  10. itamarst commented on Aug 11, 2026

    @itamarst
    Contributor

    Almost done fixing those, so doesn't look like extensive problems with this invariant.

  11. itamarst commented on Aug 11, 2026

    @itamarst
    Contributor

    OK PR is done, waiting for tests to pass and then will put up for review.

  12. itamarst commented on Sep 7, 2026

    @itamarst
    Contributor

    Forgot to link it; the PR is here: #34729

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

    DocumentationNeeds Investigationfree-threading

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions