8000 MAINT: add freethreading_compatible directive to cython build by ngoldbaum · Pull Request #26950 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content
/ numpy Public

MAINT: add freethreading_compatible directive to cython build #26950

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

Merged
merged 4 commits into from
Jul 16, 2024

Conversation

ngoldbaum
Copy link
Member
@ngoldbaum ngoldbaum commented Jul 15, 2024

This updates the cython build configuration in numpy's tests and in numpy.random to add freethreading_compatible=True compiler directives.

With this PR, numpy no longer re-enables the GIL in the free-threaded build! 🚀

Python 3.13.0b3 experimental free-threading build (main, Jul  2 2024, 14:08:18) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>>

numpy.random should be thread safe, all low-level access to the RNG state is guarded by a threading.lock in the cython code. There may be other data races I missed but IMO it's better to add these flags now to ease downstream build configuration rather than worry about theoretical thread safety issues.

The setup.py script used in the cython examples tests is dead code. I updated it and manually verified it still builds the extension, but maybe we should just delete it? The test itself does a pytest.skip if it can't find meson.

@ngoldbaum ngoldbaum added the 39 - free-threading PRs and issues related to support for free-threading CPython (a.k.a. no-GIL, PEP 703) label Jul 15, 2024
Copy link
Member
@seberg seberg left a comment

Choose a reason for hiding this comment

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

Seems fine, of course we need to document the caveats still.

Mainly, since f2py modules require the GIL yet, I think we need to still set the env variable in our CI. Otherwise all tests running after f2py will run threaded (which may or may not include explicit threading tests).

EDIT: To be clear, by caveats, I mean object arrays not being thread safe. I think it is an important one. Not sure if there are others, but I guess one might call all others "bugs" when they come up.

# TODO: delete when importing numpy no longer enables the GIL
# setting to zero ensures the GIL is disabled while running the
# tests under free-threaded python
export PYTHON_GIL=0
Copy link
Member

Choose a reason for hiding this comment

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

One problem here is that the f2py test still need it. So half-way along the tests suite we actually would re-enable this.

So I think this should stay for now but change the comment to say that it is for f2py?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, good point, let me check if the gil is being re-enabled in the test process or just in a subprocess.

Copy link
Member Author

Choose a reason for hiding this comment

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

numpy/f2py/tests/test_assumed_shape.py::TestAssumedShapeSumExample::test_all
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> PDB set_trace >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
> /Users/goldbaum/Documents/numpy/build-install/usr/lib/python3.13/site-packages/numpy/f2py/tests/test_assumed_shape.py(19)test_all()
-> breakpoint()
(Pdb) import sys
(Pdb) sys._is_gil_enabled()
True

It looks like the f2py tests do import the test modules in-process, so you're right we still need to set the environment variables. I'll update the comments around those.

If there was a way to enable and disable the GIL at runtime we could hack around this but there isn't an API for that yet (if ever).

Copy link
Member Author

Choose a reason for hiding this comment

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

I added it back and also added an additional test to manually check that import numpy doesn't print the warning about the GIL being re-enabled.

@rgommers
Copy link
Member

The setup.py script used in the cython examples tests is dead code. I updated it and manually verified it still builds the extension, but maybe we should just delete it?

It's an example, I'd prefer to keep it because it has value for setuptools users (and we discussed this before in some other PR). I don't mind that it's not used in our test suite.

@rgommers rgommers added this to the 2.1.0 release milestone Jul 16, 2024
Copy link
Member
@rgommers rgommers left a comment

Choose a reason for hiding this comment

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

LGTM. I verified locally as well that it does the correct thing. Contributors who have an older 3.1.0a0 dev version of Cython will see an error that freethreading_compatible is an unknown directive (which is unavoidable); updating to the latest nightly makes that go away.

Sebastian's comment was also addressed, and the comments are clear about what's next for f2py support. So I'll go ahead and merge this.

Thanks @ngoldbaum, and thanks for the review @seberg!

@rgommers
Copy link
Member

Seems fine, of course we need to document the caveats still.

A docs PR about the state of free-threaded support will be good to have indeed. Probably should just link back to the tracking issue rather than try to be comprehensive, because things are changing fast.

@rgommers rgommers merged commit 9b9a5da into numpy:main Jul 16, 2024
67 of 68 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
03 - Maintenance 39 - free-threading PRs and issues related to support for free-threading CPython (a.k.a. no-GIL, PEP 703)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0