-
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
24
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 23 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
dd08e01
Make pybind11 modules GIL-free
ndgrigorian 5f4e279
Declare each Cython module free-threading compatible
ndgrigorian 54aafb2
add lock to warning check in onetrace_enabled context manager
ndgrigorian 245b42c
Make ordermanager free-threading safe
ndgrigorian 52a8039
adds warning to syclinterface_diagnostics
ndgrigorian 3f97aab
update caching for free-threaded python compatibility
ndgrigorian 05e0b80
remove python-gil as a requirement
ndgrigorian 4ecd31d
remove pytest-cov as test dependencies
ndgrigorian c5d69b8
update test_memory_create for free-threaded Python
ndgrigorian 2823108
test dpctl built with and without free-threaded Python 3.14 in public CI
ndgrigorian ccb1bfe
adds trove classifier for Python free-threading status
ndgrigorian 2794463
fix missing parts of build/test matrices
ndgrigorian e9c20d5
make SequentialOrderManager thread-local and cached queues, devices g…
ndgrigorian 30a50e5
make __copy__ methods in cache classes hold locks
ndgrigorian 24ade80
use Parameter.empty instead of _empty
ndgrigorian 75c691c
add order manager example
ndgrigorian df6f452
fix potential hang in capi initialization
ndgrigorian a0d7b1b
Merge branch 'master' into feature/enable-free-threaded-python
ndgrigorian 1c61a70
Merge branch 'master' into feature/enable-free-threaded-python
ndgrigorian 3a9ce96
correct name in example
ndgrigorian a28065e
address PR comments
ndgrigorian 31f0dbb
run examples on free-threaded and GIL-enabled Python
ndgrigorian 3b675fc
make examples free-threading compatible
ndgrigorian 819f48a
bump minimum Cython version
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
| 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 | ||
|
|
@@ -286,7 +288,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: | ||
|
|
@@ -298,7 +301,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: | ||
|
|
@@ -310,7 +314,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: | ||
|
|
@@ -325,7 +330,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: | ||
|
|
@@ -347,7 +353,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: | ||
|
|
@@ -369,7 +376,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: | ||
|
|
@@ -391,7 +399,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: | ||
|
|
@@ -414,46 +423,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.