Repository navigation
Description
I'm currently working on adding support for the Python 3.13 free-threaded build to projects in the scientific python ecosystem. We are tracking this work at https://github.com/Quansight-Labs/free-threaded-compatibility. Right now we're focusing on projects relatively low in the stack. scikit-learn isn't the lowest in the stack but it has a lot of tests that perform multithreaded workflows so running the scikit-learn tests is a productive way to elucidate threading bugs in scikit-learn and its dependencies.
Currently, scikit-learn builds fine and almost all the tests pass with the GIL disabled 🎉
The following tests have failures:
Details
FAILED sklearn/ensemble/tests/test_voting.py::test_sample_weight[42] - AssertionError:
FAILED sklearn/model_selection/tests/test_search.py::test_random_search_cv_results_multimetric - AssertionError:
FAILED sklearn/semi_supervised/tests/test_self_training.py::test_classification[threshold-base_estimator1] - AssertionError:
FAILED sklearn/semi_supervised/tests/test_self_training.py::test_classification[k_best-base_estimator1] - AssertionError:
FAILED sklearn/svm/tests/test_sparse.py::test_svc[csr_matrix-linear-X_train0-y_train0-X_test0] - AssertionError:
FAILED sklearn/svm/tests/test_sparse.py::test_svc[csr_matrix-linear-X_train2-y_train2-X_test2] - AssertionError:
FAILED sklearn/svm/tests/test_sparse.py::test_svc[csr_matrix-linear-X_train3-y_train3-X_test3] - AssertionError:
FAILED sklearn/svm/tests/test_sparse.py::test_svc[csr_matrix-poly-X_train0-y_train0-X_test0] - AssertionError:
FAILED sklearn/svm/tests/test_sparse.py::test_svc[csr_matrix-poly-X_train2-y_train2-X_test2] - AssertionError:
FAILED sklearn/svm/tests/test_sparse.py::test_svc[csr_matrix-poly-X_train3-y_train3-X_test3] - AssertionError:
FAILED sklearn/svm/tests/test_sparse.py::test_svc[csr_matrix-rbf-X_train2-y_train2-X_test2] - AssertionError:
FAILED sklearn/svm/tests/test_sparse.py::test_svc[csr_matrix-rbf-X_train3-y_train3-X_test3] - AssertionError:
FAILED sklearn/svm/tests/test_sparse.py::test_svc[csr_matrix-sigmoid-X_train0-y_train0-X_test0] - AssertionError:
FAILED sklearn/svm/tests/test_sparse.py::test_svc[csr_matrix-sigmoid-X_train2-y_train2-X_test2] - AssertionError:
FAILED sklearn/svm/tests/test_sparse.py::test_svc[csr_matrix-sigmoid-X_train3-y_train3-X_test3] - AssertionError:
FAILED sklearn/svm/tests/test_sparse.py::test_svc[csr_array-linear-X_train0-y_train0-X_test0] - AssertionError:
FAILED sklearn/svm/tests/test_sparse.py::test_svc[csr_array-linear-X_train2-y_train2-X_test2] - AssertionError:
FAILED sklearn/svm/tests/test_sparse.py::test_svc[csr_array-linear-X_train3-y_train3-X_test3] - AssertionError:
FAILED sklearn/svm/tests/test_sparse.py::test_svc[csr_array-poly-X_train2-y_train2-X_test2] - AssertionError:
FAILED sklearn/svm/tests/test_sparse.py::test_svc[csr_array-poly-X_train3-y_train3-X_test3] - AssertionError:
FAILED sklearn/svm/tests/test_sparse.py::test_svc[csr_array-rbf-X_train0-y_train0-X_test0] - AssertionError:
FAILED sklearn/svm/tests/test_sparse.py::test_svc[csr_array-rbf-X_train2-y_train2-X_test2] - AssertionError:
FAILED sklearn/svm/tests/test_sparse.py::test_svc[csr_array-rbf-X_train3-y_train3-X_test3] - AssertionError:
FAILED sklearn/svm/tests/test_sparse.py::test_svc[csr_array-sigmoid-X_train0-y_train0-X_test0] - AssertionError:
FAILED sklearn/svm/tests/test_sparse.py::test_svc[csr_array-sigmoid-X_train2-y_train2-X_test2] - AssertionError:
FAILED sklearn/svm/tests/test_sparse.py::test_svc[csr_array-sigmoid-X_train3-y_train3-X_test3] - AssertionError:
FAILED sklearn/svm/tests/test_sparse.py::test_svc[lil_matrix-linear-X_train2-y_train2-X_test2] - AssertionError:
FAILED sklearn/svm/tests/test_sparse.py::test_svc[lil_matrix-linear-X_train3-y_train3-X_test3] - AssertionError:
FAILED sklearn/svm/tests/test_sparse.py::test_svc[lil_matrix-poly-X_train0-y_train0-X_test0] - AssertionError:
FAILED sklearn/svm/tests/test_sparse.py::test_svc[lil_matrix-poly-X_train2-y_train2-X_test2] - AssertionError:
FAILED sklearn/svm/tests/test_sparse.py::test_svc[lil_matrix-poly-X_train3-y_train3-X_test3] - AssertionError:
FAILED sklearn/svm/tests/test_sparse.py::test_svc[lil_matrix-rbf-X_train2-y_train2-X_test2] - AssertionError:
FAILED sklearn/svm/tests/test_sparse.py::test_svc[lil_matrix-rbf-X_train3-y_train3-X_test3] - AssertionError:
FAILED sklearn/svm/tests/test_sparse.py::test_svc[lil_matrix-sigmoid-X_train2-y_train2-X_test2] - AssertionError:
FAILED sklearn/svm/tests/test_sparse.py::test_svc[lil_matrix-sigmoid-X_train3-y_train3-X_test3] - AssertionError:
FAILED sklearn/svm/tests/test_sparse.py::test_svc[lil_array-linear-X_train2-y_train2-X_test2] - AssertionError:
FAILED sklearn/svm/tests/test_sparse.py::test_svc[lil_array-linear-X_train3-y_train3-X_test3] - AssertionError:
FAILED sklearn/svm/tests/test_sparse.py::test_svc[lil_array-poly-X_train2-y_train2-X_test2] - AssertionError:
FAILED sklearn/svm/tests/test_sparse.py::test_svc[lil_array-poly-X_train3-y_train3-X_test3] - AssertionError:
FAILED sklearn/svm/tests/test_sparse.py::test_svc[lil_array-rbf-X_train0-y_train0-X_test0] - AssertionError:
FAILED sklearn/svm/tests/test_sparse.py::test_svc[lil_array-rbf-X_train2-y_train2-X_test2] - AssertionError:
FAILED sklearn/svm/tests/test_sparse.py::test_svc[lil_array-rbf-X_train3-y_train3-X_test3] - AssertionError:
FAILED sklearn/svm/tests/test_sparse.py::test_svc[lil_array-sigmoid-X_train2-y_train2-X_test2] - AssertionError:
FAILED sklearn/svm/tests/test_sparse.py::test_svc[lil_array-sigmoid-X_train3-y_train3-X_test3] - AssertionError:
FAILED sklearn/svm/tests/test_sparse.py::test_consistent_proba - AssertionError:
FAILED sklearn/svm/tests/test_svm.py::test_libsvm_iris - AssertionError:
FAILED sklearn/svm/tests/test_svm.py::test_svc_clone_with_callable_kernel - AssertionError:
FAILED sklearn/svm/tests/test_svm.py::test_consistent_proba - AssertionError:
FAILED sklearn/tests/test_build.py::test_openmp_parallelism_enabled - AssertionError:
FAILED sklearn/tests/test_pipeline.py::test_pipeline_memory - AssertionError:
Note that some of these failures are flaky and you may not get the same list if you run the scikit-learn tests. I also saw one deadlock in test_calibration.py::test_parallel_execution, so I'd also suggest running pytest with e.g. -o fauthandler_timeout=60 set so you will get python tracebacks for all threads if any test hangs and runs longer than 60 seconds. Your mileage may vary on how fast your computer is, so 60 seconds may not be sufficient if any tests take that long to run on your system.
My plan is to go through these failures and figure out what's going wrong. Some of these may not be specific to the free-threaded build.
To run the tests with the GIL disabled, you'll need to install 3.13b1 or the latest version of the CPython 3.13 branch, build cython, numpy, and scipy from the latest version of their respective git repositories. You'll need to e.g. pip install pybind11 and pythran after installing numpy and before building scipy. Then build scikit-learn as an editable install and run:
PYTHON_GIL=0 python -m pytest sklearn
Activity
ogrisel commentedon May 10, 2024
Thanks for your investigation.
Indeed we do not have a Python 3.13 build yet so maybe some of those failures are related to that.
That would probably be too long to build all of those dependencies from source on the CI. Maybe upstream packages such as numpy and scipy could publish nightly builds for Python 3.13? As far as I know this is not the case yet:
However, according to https://peps.python.org/pep-0703/#python-build-modes, there are two CPython ABIs for the foreseeable future and I am not sure how those two ABIs can be reflected in wheel filenames.
https://peps.python.org/pep-0703/#distribution mentions a dedicated conda channel but I am not sure about the status of this channel.
ogrisel commentedon May 10, 2024
In any case, feel free to open PRs to address some of those failures you discovered even if we don't have a
nogilCPython 3.13.X CI yet. We can merge them even if they can only be properly tested manually for now.Note that we do have a 3.9
nogilCI running nightly on Azure Pipelines and it has been mostly green for a year or more. But I assume that 3.13nogilhas evolved a bit from 3.9nogilhence the new failures.ngoldbaum commentedon May 10, 2024
Great, I might look at setting that up along with fixing the 3.13-specific issues.
Testing the free-threaded build will need to wait a little bit. I'm hoping that the
setup-pythonaction will gain support for installing it via a configuration flag (actions/setup-python#771). If that ends up not being quickly fixed now that the first beta is out, I'll look at setting up an Ubuntu-based CI job using the deadsnakes repo (once that gets updated with a 3.13 free-threaded build). See @hugovk's blog post.There is a new ABI tag (
3.13t), so you'd need to upload two python 3.13 wheels, one with the normal GIL-enabled ABI and one with the free-threaded ABI.Also, one big issue is that pip doesn't know about the ABI tag yet (pypa/pip#12634). However that should be fixed in pip 24.1.
I don't think this has been set up yet. The last I heard the Anaconda Inc team was planning to start work on free-threaded support around the release of the first beta (so, nowish).
Great, thanks for this. Would creating a
nogilorfree-threadedlabel to tag issues and PRs with be OK?ngoldbaum commentedon May 10, 2024
And it probably makes sense to retire this CI job since the 3.13 free-threaded build should replace it and we've been upstreaming support using the
Py_GIL_DISABLEDmacro, which isn't used in the 3.9 nogil fork.lesteve commentedon May 13, 2024
I am Interested to help on this, don't hesitate to ping me on this kind of topics! Together with @ogrisel I was involved with setting up the scikit-learn CI for the nogil Python 3.9 and opening upstream bugs when we encountered them.
I have created the no-GIL label (following the same naming as scipy and numpy for consistency although I have always used nogil personally). Note that label search will match "nogil" and "free-threaded" since both terms appear in the label description.
Issues and PRs will need manual labeling by a scikit-learn maintainer for now, but I guess this is good enough for now, let me know if you have better suggestions!
hugovk commentedon May 13, 2024
Naming things: when accepting PEP 703, the Steering Council said they wanted us to use a positive term like "free-threaded" rather than negative terms like "no-GIL":
And we use "topic-free-threading" for CPython:
https://github.com/python/cpython/labels/topic-free-threading
lesteve commentedon May 13, 2024
Fair enough, I renamed the label to "free-threading", personally I don't care too much as long as I can find the label easily in the labels search for example like in the screenshot below (finding free-threaded label when searching nogil):

You may want to advocate for a similar change in numpy and scipy 😉
Edit: contrary to the screenshot, I ended up using free-threading (and not free-threaded) to match CPython.
lesteve commentedon May 13, 2024
This seems definitely desirable. Alternatively in the shorter term, if there was a Docker image (even in an experimental state) with Python free-threaded build + latest numpy release and scipy release, this may make it more likely that curious but busy people (for example me) give it a go.
ogrisel commentedon May 14, 2024
It could even be a nightly/weekly updated docker image with the dev versions of numpy and scipy. If a free docker hub account is enough we could use the auto-build feature of docker hub or similar:
https://docs.docker.com/docker-hub/builds/
Not sure about build time limit/usage quotas though.
EDIT: still I would prefer installing from a pip wheel house, e.g. https://pypi.anaconda.org/scientific-python-nightly-free-threaded/ to workaround any pip ABI tag conflicts that would prevent us to directly reuse https://pypi.anaconda.org/scientific-python-nightly-wheels/ .
ngoldbaum commentedon May 14, 2024
There's a new ABI tag, so the new nightly wheels can live alongside the old ones on https://pypi.anaconda.org/scientific-python-nightly-wheels/. We just need to wait for a new pip release that can recognize and install the new ABI tag and for the the actual wheels to be produced. I'd like to look at that for NumPy at least later this week.
12 remaining items
ngoldbaum commentedon May 24, 2024
Slightly dumb question: are you sure the GIL is disabled running the test suite? I still need to mark numpy's extension modules as safe without the GIL, so it needs to be explicitly disabled with e.g. the PYTHON_GIL environment variable.
Also cython has seen some updates, maybe cython changes fixed things? It's been a week or two since I tried the scikit-learn test suite.
lesteve commentedon May 24, 2024
Yes, I did set
PYTHON_GIL=0when running the tests.lesteve commentedon Jun 5, 2024
Update: I have a PR with a CPython 3.13 free-threaded CI build in #29191 and the CI is green 🎉
Once this is merged, the next step would be to upload nightly wheels to scientific-python-nightly-wheels as Numpy and Scipy already do.
lesteve commentedon Jun 19, 2024
Update: manylinux scikit-learn free-threaded wheel is available 🎉, see https://anaconda.org/scientific-python-nightly-wheels/scikit-learn/files
ogrisel commentedon Jun 24, 2024
I think we should keep this issue open as long as:
rgommers commentedon Jun 24, 2024
pip24.1 was released just a few days ago, that is the first version that does the right thing. Older versions happily install the wrong wheels.betatim commentedon Jun 26, 2024
In the sense that if we upload a
cp3XYtwheel it will be downloaded by an old pip when it shouldn't download it?ngoldbaum commentedon Jun 26, 2024
I don't think that's possible. The failure mode pip fixed was that in Python 3.13, Pip didn't know about the existence of the
cp313tABI tag, so it installed the regularcp313wheel instead, which it did know about. I don't see how the new ABI tag could inflence pip's decision to install a wheel or not on older python versions.ogrisel commentedon Oct 4, 2024
Note: the scikit-learn 1.5.2 release of scikit-learn now included regular (GIL enabled) wheels for CPython 3.13 but not the
cp313tvariant because we need scipy to ship cp313t wheels first. See the discussion in #29987.ngoldbaum commentedon Oct 30, 2024
Let me know if you need any support unblocking Windows wheel uploads.
lesteve commentedon Oct 31, 2024
I guess we would need scipy to upload Windows free-threaded wheels first, which does not seem to be the case https://anaconda.org/scientific-python-nightly-wheels/scipy/files. I haven't found an ongoing Scipy PR adding Windows free-threaded wheels, but maybe I have missed it.
Also we are probably going to have something similar to pandas-dev/pandas#59057 (comment) that for Windows wheels we use a custom minimal Docker image to avoid relying on Windows system DLLs which make it a bit more cumbersome (see #18802 for more details about why we need this).
ngoldbaum commentedon Oct 31, 2024
Ah sorry missed that SciPy was an issue. That is being worked on by @andfoy but is trickier than other libraries because SciPy uses a GNU toolchain to build on Windows.
lesteve commentedon Nov 25, 2024
@ngoldbaum FYI, the Windows free-threaded wheel has been added in #30313, this should be available tomorrow in scientific-python-nightly-wheels 🤞.
ogrisel commentedon Nov 28, 2024
I confirm they are now. We can close. Thanks to everybody involved in making this happen!