Skip to content

MNT Mark cython extensions as free-threaded compatible - #31342

Merged
thomasjpfan merged 8 commits into
scikit-learn:mainfrom
lesteve:cython-3.1-free-threaded
Sep 2, 2025
Merged

thomasjpfan merged 8 commits into
scikit-learn:mainfrom
lesteve:cython-3.1-free-threaded

Conversation

@lesteve

@lesteve lesteve commented May 9, 2025 •

Copy link
Copy Markdown
Member

Part of #30007

Cython 3.1 has been released on May 8 2025.

Following scipy PR scipy/scipy#22658 to use -Xfreethreading_compatible=True cython argument if cython >= 3.1

This cleans up the lock-file and install.sh script as well because cython and scipy are available through conda-forge.

I realized I forgot a cython_gen.process in #31212 for murmurhash and I was getting a warning about it locally ...

@github-actions

github-actions Bot commented May 9, 2025 •

Copy link
Copy Markdown

✔️ Linting Passed

All linting checks passed. Your pull request is in excellent shape! ☀️

Generated for commit: 226f87e. Link to the linter CI: here

Comment thread sklearn/meson.build Outdated

cython_args = []
if cython.version().version_compare('>=3.1.0')
cython_args += ['-Xfreethreading_compatible=True']

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting this means that the C extensions that come with scikit-learn are marked as working with free threaded Python right? One question I've had about marking extensions as compatible: how do we know? With the GIL there are a lot of things you could do that without the GIL enabled are bugs because they lead to race conditions. How the heck would we find all of them?

@ogrisel ogrisel May 9, 2025 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How the heck would we find all of them?

We do not need to find them all to declare that as a project we would consider such race conditions as bugs that we are willing to get fixed.

But I agree, it's better to try to catch as many as possible ahead of time. In the past (e.g. #30007) we started to take a look at pytest extensions that should help us catch those bugs before making a release. We need to give those tools a second look to see which one is the easiest to adopt for scikit-learn, either for manual checks of a particular submodule or automated check on the CI for the full code base.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, we don't have to find all of them ahead of time. We could attach a note to free-threaded usage mentioning that it is not yet ready for production use?

I'll take a look at #30007.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could attach a note to free-threaded usage mentioning that it is not yet ready for production use?

It would be a good idea to include such a note in the release notes for 1.7. cc @jeremiedbb.

@thomasjpfan thomasjpfan May 9, 2025 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reading the docs for freethreading_compatible=True: https://docs.cython.org/en/latest/src/userguide/freethreading.html#status it states:

When you specify this directive, importing the module will not cause the interpreter to re-enable the GIL. The directive itself does not do anything to ensure compatibility - it is simply a way for you to indicate that you have tested your module and are confident that it works.

If we are not confident enough that it works, I do not see the value in setting the flag. It could even be a regression:

  • Currently, with freethreading_compatible=False means the GIL is reenabled. So the code works today.
  • If we change it to freethreading_compatible=True and the module is not compatible with free threading, then it could break.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But we cannot run such tests if we do not release the GIL when importing the native extension modules, right?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should finalize and merge #30041 to main and then resync that branch with main to actually be able to run such tests using pytest-run-parallel.

@ogrisel ogrisel added the free-threading PRs and issues related to support for free-threaded CPython (a.k.a. nogil or no-GIL, PEP 703) label May 9, 2025
@lesteve

lesteve commented May 9, 2025

Copy link
Copy Markdown
Member Author

I noticed a few other missing cython generator in the meson.build file and ended up opening a separate PR for it: #31346.

@lesteve lesteve added To backport PR merged in master that need a backport to a release branch defined based on the milestone. and removed To backport PR merged in master that need a backport to a release branch defined based on the milestone. labels May 11, 2025
@lesteve

lesteve commented May 12, 2025 •

Copy link
Copy Markdown
Member Author

I opened #31357 to do the simpler part of using released Cython 3.1 for free-threaded rather than the dev cython version.

Marking our extension as free-threaded compatible will be left for later.

@lesteve lesteve changed the title MNT Use Cython 3.1 in free-threaded build and mark cython extensions as free-threaded compatible MNT Mark cython extensions as free-threaded compatible May 12, 2025
@ogrisel

ogrisel commented Aug 29, 2025

Copy link
Copy Markdown
Member

This PR will need to be reworked on top of #32023 once merged.

@lesteve

lesteve commented Sep 2, 2025 •

Copy link
Copy Markdown
Member Author

Now that #32023 has been merged, I merged main and tweaked this PR. This is ready for review.

pytest-run-parallel will complain and fail the tests 1 if one of the extensions is not marked as free-thread compatible and reenables the GIL at import time.

Full disclosure: for now I left the PYTHON_GIL=0 in wheels bash scripts:

❯ git grep 'PYTHON_GIL=0'
build_tools/github/test_windows_wheels.sh:    export PYTHON_GIL=0
build_tools/wheels/test_wheels.sh:    export PYTHON_GIL=0

The reason behind this is that for now pandas 2 (2.3.2, latest release at the time of writing) would enable the GIL and the tests will run with the GIL, which may hide problems, see pandas 2.3.2 meson.build. pandas mark the extension as free-threading compatible in main:
https://github.com/pandas-dev/pandas/blob/ef4a9c3f6cbb53dc1d95e85a09e356f399355be2/meson.build#L60

Footnotes

  1. Error is something like this: Exit: GIL was dynamically re-enabled during test configuration to load module 'sklearn.__check_build._check_build'. When running under a free-threaded interpreter with the GIL initially disabled, the test suite must not cause the GIL to be re-enabled at runtime. Check for compiled extension modules that do not use the 'Py_mod_gil' slot or the 'PyUnstable_Module_SetGIL' API. ↩

  2. I don't remember 100% but I am pretty sure that in some builds we run the tests without pandas (e.g. Python 3.14 and maybe Python 3.13 free-threaded) but maybe one day we will add pandas back. ↩

@thomasjpfan
thomasjpfan merged commit 90338a4 into scikit-learn:main Sep 2, 2025
@lorentzenchr

Copy link
Copy Markdown
Member

Note that since one of those free-threaded python things, pytest -Werror does not work anymore

...
INTERNALERROR> pytest.PytestConfigWarning: Unknown config option: thread_unsafe_fixtures

@ogrisel

ogrisel commented Sep 2, 2025 •

Copy link
Copy Markdown
Member

@lorentzenchr I think this should go away if you install the pytest-run-parallel pytest plugin in your environment. I does nothing by default but it does define the thread_unsafe_fixtures config option.

@lorentzenchr

Copy link
Copy Markdown
Member

@lorentzenchr I think this should go away if you install the pytest-run-parallel pytest plugin in your environment. I does nothing by default but it does define the thread_unsafe_fixtures config option.

Thanks. But could we please document this in the developer doc? What do I need to install to develop and run tests?

@lesteve

lesteve commented Sep 3, 2025 •

Copy link
Copy Markdown
Member Author

Sorry about breaking (part of) your workflow @lorentzenchr 😓!

There may be a way to define the mark when pytest-run-parallel is not installed, I would need to look into this.

Just curious, what's your use case for pytest -Werror? I would expect that right now there are a lot of warnings in the scikit-learn test suite, see e.g. #29516.

You very likely know this, but you can do something like pytest -Werror -Wignore:Unknown 1.

Depending on your original motivation, SKLEARN_WARNINGS_AS_ERRORS=1 pytest, may be close enough and this is similar to what the CI is doing to turn some warnings into errors.

Footnotes

  1. annoyingly there is an error at collection time and one at runtime with a different warning type so if you want to filter by warning type you need to do pytest -Werror -Wignore::pytest.PytestConfigWarning -Wignore::pytest.PytestUnknownMarkWarning sklearn ↩

@lesteve
lesteve deleted the cython-3.1-free-threaded branch September 3, 2025 03:45
@lesteve

lesteve commented Sep 3, 2025

Copy link
Copy Markdown
Member Author

I opened #32088 to avoid the pytest warnings when pytest-run-parallel is not installed.

@lorentzenchr

Copy link
Copy Markdown
Member

One use case: Often, tests need require a certain tolerance, so the model needs a good tolerance. With -Werror, I see if there was a ConvergenceWarning.

@lesteve

lesteve commented Sep 3, 2025 •

Copy link
Copy Markdown
Member Author

Makes sense, pytest -Werror::sklearn.exceptions.ConvergenceWarning works but it's definitely longer and harder to remember. SKLEARN_WARNINGS_AS_ERRORS=1 is not helpful in this case since it is only turning some warnings (like DeprecationWarning and FutureWarning) into errors.

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

Labels

free-threading PRs and issues related to support for free-threaded CPython (a.k.a. nogil or no-GIL, PEP 703) module:utils

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants