-
-
Notifications
You must be signed in to change notification settings - Fork 25.8k
Add support for Python 3.13 free-threaded build #28978
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
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. |
In any case, feel free to open PRs to address some of those failures you discovered even if we don't have a Note that we do have a 3.9 |
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
There is a new ABI tag ( 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 |
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 |
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! |
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 |
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. |
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/ . |
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. |
Ah but we probably will need to wait on cibuildwheel support first: pypa/cibuildwheel#1657 So maybe a docker image would be best until beta 2? |
Just a note that we got rid of Docker images and weekly pushes to Docker Hub in NumPy and SciPy after they limited their free plan (and the machinery was a bit of a pain to begin with). Setting that up again isn't worth it imho. CPython 3.13 beta 2 is only two weeks away (May 28th), so I'd wait for that and if you want something in the meantime then building a one-off Docker image by hand to use for a few weeks until nightly wheels arrive should be okay I'd think. Investing time in auto-building Docker images is probably not the most productive way of spending time. |
👍 for being patient for two weeks instead of building something "bespoke" that we should then un-build again. |
We would also need to make sure that older pip releases are not confused by the presence of wheels with the new ABI tag and do not fetch those wheels by mistake when trying to run nightly builds on the non-free-threaded version of CPython 3.13. |
I built CPython GDBM_CFLAGS="-I$(brew --prefix gdbm)/include" \
GDBM_LIBS="-L$(brew --prefix gdbm)/lib -lgdbm" \
./configure --config-cache \
--with-pydebug \
--with-system-libmpdec \
--with-openssl="$(brew --prefix openssl@3.0)" \
--disable-gil && make -s -j10
Then I could successfully run Then I created a venv with And then I tried to build numpy with
any suggestion @ngoldbaum? |
I think @lesteve had a similar problem on Linux so it's probably not macOS specific? |
Not sure if it matters, but that's not latest numpy |
I tried again, not sure what I did differently this time to be honest, but it went a bit further. This time, I get a build error in numpy More details
|
FWIW, I also see that using a virtualenv like @ogrisel is trying to do.
I think maybe because |
This is fixed in cython main, you'll need to compile cython from the git repo before building numpy. |
Ahh, sorry! This is because meson can't find python.h or libpython, so you need to set e.g. |
Thanks for your inputs, I managed to get numpy, scipy, scikit-learn to build. I was able to run the full scikit-learn test suite without failures. Maybe I was just lucky, since you said some of the failures are flaky? I did try to run multiple times some of the tests you mention in your original post but I did not manage to get them to fail. What I am seeing though is segmentation fault at exit, i.e. after the test have run. The gdb info looks like this:
It seems like this happens when garbage-collecting uarray objects after a Python thread has been started, here is a reproducer with only scipy: import scipy.fft
from concurrent.futures import ThreadPoolExecutor
def noop():
return
tpe = ThreadPoolExecutor(max_workers=1)
tpe.submit(noop).result() It does not seem to be one of the known issues in Scipy scipy/scipy#20669. Edit: actually looking a bit further the problem still happens with For further reference, here are the commits I used: |
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. |
Yes, I did set |
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. |
Update: manylinux scikit-learn free-threaded wheel is available 🎉, see https://anaconda.org/scientific-python-nightly-wheels/scikit-learn/files |
I think we should keep this issue open as long as:
|
|
In the sense that if we upload a |
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 |
Note: the scikit-learn 1.5.2 release of scikit-learn now included regular (GIL enabled) wheels for CPython 3.13 but not the |
Let me know if you need any support unblocking Windows wheel uploads. |
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). |
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. |
@ngoldbaum FYI, the Windows free-threaded wheel has been added in #30313, this should be available tomorrow in scientific-python-nightly-wheels 🤞. |
I confirm they are now. We can close. Thanks to everybody involved in making this happen! |
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:
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:The text was updated successfully, but these errors were encountered: