Skip to content

MNT Mark cython extensions as free-threaded compatible #31342

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

lesteve
Copy link
Member

@lesteve lesteve commented May 9, 2025

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 ...

Copy link

github-actions bot commented May 9, 2025

✔️ Linting Passed

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

Generated for commit: 0f2ffac. Link to the linter CI: here

@@ -1,6 +1,9 @@
fs = import('fs')

cython_args = []
if cython.version().version_compare('>=3.1.0')
cython_args += ['-Xfreethreading_compatible=True']
Copy link
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?

Copy link
Member

@ogrisel ogrisel May 9, 2025

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
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
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.

Copy link
Member

@thomasjpfan thomasjpfan May 9, 2025

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
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
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
Copy link
Member Author

lesteve commented May 9, 2025

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
Copy link
Member Author

lesteve commented May 12, 2025

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
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.

4 participants