-
Notifications
You must be signed in to change notification settings - Fork 37
Remove python-gil requirement and enable free-threaded Python
#2250
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
ndgrigorian
wants to merge
29
commits into
master
Choose a base branch
from
feature/enable-free-threaded-python
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
d84783e
Make pybind11 modules GIL-free
ndgrigorian 962d6d7
Declare each Cython module free-threading compatible
ndgrigorian 6f51e66
add lock to warning check in onetrace_enabled context manager
ndgrigorian eb75822
Make ordermanager free-threading safe
ndgrigorian 017060c
adds warning to syclinterface_diagnostics
ndgrigorian 2f5e60d
update caching for free-threaded python compatibility
ndgrigorian 6c57cc8
remove python-gil as a requirement
ndgrigorian 1ef6085
remove pytest-cov as test dependencies
ndgrigorian 4042a87
update test_memory_create for free-threaded Python
ndgrigorian 95d7100
test dpctl built with and without free-threaded Python 3.14 in public CI
ndgrigorian c54a7e3
adds trove classifier for Python free-threading status
ndgrigorian fde8d0a
fix missing parts of build/test matrices
ndgrigorian 549cfa2
make SequentialOrderManager thread-local and cached queues, devices g…
ndgrigorian b088231
make __copy__ methods in cache classes hold locks
ndgrigorian 9294b4f
use Parameter.empty instead of _empty
ndgrigorian 342073f
add order manager example
ndgrigorian 02167ac
fix potential hang in capi initialization
ndgrigorian 9105b2c
correct name in example
ndgrigorian 7fe90d1
address PR comments
ndgrigorian e4c31e9
run examples on free-threaded and GIL-enabled Python
ndgrigorian d9191cc
make examples free-threading compatible
ndgrigorian 0373757
bump minimum Cython version
ndgrigorian 9f4ede0
add py_mod_gil_not_used to pybind11 doc example
ndgrigorian f69ac0a
add missing utility include
ndgrigorian df87ce4
pin Cython to 3.1.0 in optional deps
ndgrigorian 7bf4335
remove pytest-cov ini option
ndgrigorian 61e7697
fix copyright in order manager example
ndgrigorian 8bddb11
fix formatting in pyproject.toml
ndgrigorian b4d095c
fix missing iostream include
ndgrigorian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,8 +17,10 @@ | |
| # distutils: language = c++ | ||
| # cython: language_level=3 | ||
| # cython: linetrace=True | ||
| # cython: freethreading_compatible = True | ||
|
|
||
| """ This module implements several device creation helper functions: | ||
| """ | ||
| This module implements several device creation helper functions: | ||
|
|
||
| - wrapper functions to create a SyclDevice from the standard SYCL | ||
| device selector classes. | ||
|
|
@@ -46,7 +48,7 @@ from ._backend cimport ( # noqa: E211 | |
| _device_type, | ||
| ) | ||
|
|
||
| from contextvars import ContextVar | ||
| import threading | ||
|
|
||
| from ._sycl_device import SyclDeviceCreationError | ||
| from .enum_types import backend_type | ||
|
|
@@ -288,7 +290,8 @@ cpdef int get_num_devices( | |
|
|
||
|
|
||
| cpdef cpp_bool has_cpu_devices(): | ||
| """ A helper function to check if there are any SYCL CPU devices available. | ||
| """ | ||
| A helper function to check if there are any SYCL CPU devices available. | ||
|
|
||
| Returns: | ||
| bool: | ||
|
|
@@ -300,7 +303,8 @@ cpdef cpp_bool has_cpu_devices(): | |
|
|
||
|
|
||
| cpdef cpp_bool has_gpu_devices(): | ||
| """ A helper function to check if there are any SYCL GPU devices available. | ||
| """ | ||
| A helper function to check if there are any SYCL GPU devices available. | ||
|
|
||
| Returns: | ||
| bool: | ||
|
|
@@ -312,7 +316,8 @@ cpdef cpp_bool has_gpu_devices(): | |
|
|
||
|
|
||
| cpdef cpp_bool has_accelerator_devices(): | ||
| """ A helper function to check if there are any SYCL Accelerator devices | ||
| """ | ||
| A helper function to check if there are any SYCL Accelerator devices | ||
| available. | ||
|
|
||
| Returns: | ||
|
|
@@ -327,7 +332,8 @@ cpdef cpp_bool has_accelerator_devices(): | |
|
|
||
|
|
||
| cpdef SyclDevice select_accelerator_device(): | ||
| """A wrapper for ``sycl::device{sycl::accelerator_selector_v}`` constructor. | ||
| """ | ||
| A wrapper for ``sycl::device{sycl::accelerator_selector_v}`` constructor. | ||
|
|
||
| Returns: | ||
| dpctl.SyclDevice: | ||
|
|
@@ -349,7 +355,8 @@ cpdef SyclDevice select_accelerator_device(): | |
|
|
||
|
|
||
| cpdef SyclDevice select_cpu_device(): | ||
| """A wrapper for ``sycl::device{sycl::cpu_selector_v}`` constructor. | ||
| """ | ||
| A wrapper for ``sycl::device{sycl::cpu_selector_v}`` constructor. | ||
|
|
||
| Returns: | ||
| dpctl.SyclDevice: | ||
|
|
@@ -371,7 +378,8 @@ cpdef SyclDevice select_cpu_device(): | |
|
|
||
|
|
||
| cpdef SyclDevice select_default_device(): | ||
| """A wrapper for ``sycl::device{sycl::default_selector_v}`` constructor. | ||
| """ | ||
| A wrapper for ``sycl::device{sycl::default_selector_v}`` constructor. | ||
|
|
||
| Returns: | ||
| dpctl.SyclDevice: | ||
|
|
@@ -393,7 +401,8 @@ cpdef SyclDevice select_default_device(): | |
|
|
||
|
|
||
| cpdef SyclDevice select_gpu_device(): | ||
| """A wrapper for ``sycl::device{sycl::gpu_selector_v}`` constructor. | ||
| """ | ||
| A wrapper for ``sycl::device{sycl::gpu_selector_v}`` constructor. | ||
|
|
||
| Returns: | ||
| dpctl.SyclDevice: | ||
|
|
@@ -416,46 +425,45 @@ cpdef SyclDevice select_gpu_device(): | |
|
|
||
| cdef class _DefaultDeviceCache: | ||
| cdef dict __device_map__ | ||
| cdef object _cache_lock | ||
|
|
||
| def __cinit__(self): | ||
| self.__device_map__ = {} | ||
|
|
||
| cdef get_or_create(self): | ||
| """Return instance of SyclDevice and indicator if cache | ||
| has been modified""" | ||
| key = 0 | ||
| if key in self.__device_map__: | ||
| return self.__device_map__[key], False | ||
| dev = select_default_device() | ||
| self.__device_map__[key] = dev | ||
| return dev, True | ||
|
|
||
| cdef _update_map(self, dev_map): | ||
| self._cache_lock = threading.Lock() | ||
|
|
||
| def get_or_create(self): | ||
| """Return cached default SyclDevice, creating it if needed.""" | ||
| with self._cache_lock: | ||
| key = 0 | ||
| if key in self.__device_map__: | ||
| return self.__device_map__[key] | ||
| dev = select_default_device() | ||
| self.__device_map__[key] = dev | ||
| return dev | ||
|
|
||
| def _update_map(self, dev_map): | ||
| self.__device_map__.update(dev_map) | ||
|
|
||
| def __copy__(self): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems not covered by any test |
||
| cdef _DefaultDeviceCache _copy = _DefaultDeviceCache.__new__( | ||
| _DefaultDeviceCache) | ||
| _copy._update_map(self.__device_map__) | ||
| # lock must be held to avoid race conditions on map state | ||
| with self._cache_lock: | ||
| _copy._update_map(self.__device_map__.copy()) | ||
| return _copy | ||
|
|
||
|
|
||
| _global_default_device_cache = ContextVar( | ||
| "global_default_device_cache", | ||
| default=_DefaultDeviceCache() | ||
| ) | ||
| # all threads share the same cached default | ||
| _global_default_device_cache = _DefaultDeviceCache() | ||
|
|
||
|
|
||
| cpdef SyclDevice _cached_default_device(): | ||
| """Returns a cached device selected by default selector. | ||
| """ | ||
| Returns a cached device selected by default selector. | ||
|
|
||
| Returns: | ||
| dpctl.SyclDevice: | ||
| A cached default-selected SYCL device. | ||
|
|
||
| """ | ||
| cdef _DefaultDeviceCache _cache = _global_default_device_cache.get() | ||
| d_, changed_ = _cache.get_or_create() | ||
| if changed_: | ||
| _global_default_device_cache.set(_cache) | ||
| return d_ | ||
| return _global_default_device_cache.get_or_create() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,9 +17,10 @@ | |
| # distutils: language = c++ | ||
| # cython: language_level=3 | ||
| # cython: linetrace=True | ||
| # cython: freethreading_compatible = True | ||
|
|
||
| import logging | ||
| from contextvars import ContextVar | ||
| import threading | ||
| from ._sycl_context cimport SyclContext | ||
| from ._sycl_device cimport SyclDevice | ||
|
|
||
|
|
@@ -33,13 +34,14 @@ _logger = logging.getLogger(__name__) | |
|
|
||
| cdef class _DeviceDefaultQueueCache: | ||
| cdef dict __device_queue_map__ | ||
| cdef object _cache_lock | ||
|
|
||
| def __cinit__(self): | ||
| self.__device_queue_map__ = {} | ||
| self._cache_lock = threading.Lock() | ||
|
|
||
| def get_or_create(self, key): | ||
| """Return instance of SyclQueue and indicator if cache | ||
| has been modified""" | ||
| """Return cached SyclQueue for given key, creating it if needed.""" | ||
| if ( | ||
| isinstance(key, tuple) | ||
| and len(key) == 2 | ||
|
|
@@ -56,32 +58,34 @@ cdef class _DeviceDefaultQueueCache: | |
| ctx_dev = q.sycl_context, q.sycl_device | ||
| else: | ||
| raise TypeError | ||
| if ctx_dev in self.__device_queue_map__: | ||
| return self.__device_queue_map__[ctx_dev], False | ||
| if q is None: | ||
| q = SyclQueue(*ctx_dev) | ||
| self.__device_queue_map__[ctx_dev] = q | ||
| return q, True | ||
|
|
||
| cdef _update_map(self, dev_queue_map): | ||
| with self._cache_lock: | ||
|
ndgrigorian marked this conversation as resolved.
|
||
| if ctx_dev in self.__device_queue_map__: | ||
| return self.__device_queue_map__[ctx_dev] | ||
| if q is None: | ||
| q = SyclQueue(*ctx_dev) | ||
| self.__device_queue_map__[ctx_dev] = q | ||
| return q | ||
|
|
||
| def _update_map(self, dev_queue_map): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment to |
||
| self.__device_queue_map__.update(dev_queue_map) | ||
|
|
||
| def __copy__(self): | ||
| cdef _DeviceDefaultQueueCache _copy = _DeviceDefaultQueueCache.__new__( | ||
| _DeviceDefaultQueueCache | ||
| ) | ||
| _copy._update_map(self.__device_queue_map__) | ||
| # lock must be held to avoid race conditions on map state | ||
| with self._cache_lock: | ||
| _copy._update_map(self.__device_queue_map__.copy()) | ||
| return _copy | ||
|
|
||
|
|
||
| _global_device_queue_cache = ContextVar( | ||
| "global_device_queue_cache", | ||
| default=_DeviceDefaultQueueCache() | ||
| ) | ||
| # all threads share the same cached default | ||
| _global_device_queue_cache = _DeviceDefaultQueueCache() | ||
|
|
||
|
|
||
| cpdef object get_device_cached_queue(object key): | ||
| """Returns a cached queue associated with given device. | ||
| """ | ||
| Returns a cached queue associated with given device. | ||
|
|
||
| Args: | ||
| key : Either a 2-tuple consisting of a :class:`dpctl.SyclContext` and | ||
|
|
@@ -96,8 +100,4 @@ cpdef object get_device_cached_queue(object key): | |
| TypeError: If the input key is not one of the accepted types. | ||
|
|
||
| """ | ||
| _cache = _global_device_queue_cache.get() | ||
| q_, changed_ = _cache.get_or_create(key) | ||
| if changed_: | ||
| _global_device_queue_cache.set(_cache) | ||
| return q_ | ||
| return _global_device_queue_cache.get_or_create(key) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_update_mapis public + unlocked — safe today (only called on the not-yet-shared_copy), but a foot-gun if ever called on the live global. We probably might need to consider locking it internally or clear documenting.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or alternatively to revert back to
cdefat least to make it inaccessible from the python code, i.e. to have more control when the method is called.