Repository navigation
Allow NumPy data types as arguments #2335
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
4
commits into
master
Choose a base branch
from
add-dpctl-dtypes
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
4 commits
Select commit
Hold shift + click to select a range
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
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
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 |
|---|---|---|
|
|
@@ -81,3 +81,5 @@ profiler | |
| keepdims | ||
| broadcasted | ||
| broadcastable | ||
| bfloat | ||
| dtypes | ||
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 |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| # Data Parallel Control (dpctl) | ||
| # | ||
| # Copyright 2026 Intel Corporation | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Data type handling shared by the dpctl functions taking a data type.""" | ||
|
|
||
| import struct | ||
|
|
||
| import numpy as np | ||
|
|
||
| # kinds of NumPy data types whose values are not plain bytes | ||
| _NON_POD_KINDS = frozenset("OSUMmT") | ||
|
|
||
| # extended precision types, which devices do not support | ||
| _EXTENDED_PRECISION_NUMS = frozenset( | ||
| np.dtype(t).num | ||
| for t in (np.longdouble, np.clongdouble) | ||
| if np.finfo(t).nmant > np.finfo(np.float64).nmant | ||
| ) | ||
|
|
||
| # struct formats of the data types whose values are range checked when packed | ||
| _STRUCT_FORMATS = { | ||
| ("b", 1): "?", | ||
| ("i", 1): "b", | ||
| ("i", 2): "h", | ||
| ("i", 4): "i", | ||
| ("i", 8): "q", | ||
| ("u", 1): "B", | ||
| ("u", 2): "H", | ||
| ("u", 4): "I", | ||
| ("u", 8): "Q", | ||
| ("f", 2): "e", | ||
| ("f", 4): "f", | ||
| ("f", 8): "d", | ||
| ("c", 8): "ff", | ||
| ("c", 16): "dd", | ||
| } | ||
|
|
||
|
|
||
| def is_supported_dtype(dt): | ||
| """Returns whether values of :class:`numpy.dtype` ``dt`` can be used on a | ||
| device.""" | ||
| return not ( | ||
| dt.hasobject | ||
| or dt.kind in _NON_POD_KINDS | ||
| or dt.itemsize == 0 | ||
| or dt.num in _EXTENDED_PRECISION_NUMS | ||
| ) | ||
|
|
||
|
|
||
| def as_dtype(dtype): | ||
| """Returns ``numpy.dtype(dtype)``. | ||
|
|
||
| Raises: | ||
| ValueError: if ``dtype`` is ``None``, not a supported data type, or not | ||
| in native byte order. | ||
| """ | ||
| if dtype is None: | ||
| raise ValueError("dtype must not be None") | ||
| try: | ||
| dt = np.dtype(dtype) | ||
| except TypeError: | ||
| raise ValueError(f"Unrecognized dtype '{dtype}'") from None | ||
| if not is_supported_dtype(dt): | ||
| raise ValueError(f"Unsupported dtype '{dt}'") | ||
| if not dt.isnative: | ||
| raise ValueError(f"dtype '{dt}' is not in native byte order") | ||
| return dt | ||
|
|
||
|
|
||
| def as_scalar(value): | ||
| """Converts a Python ``bool``, ``int``, ``float`` or ``complex`` to a | ||
| ``numpy.bool``, ``numpy.int64``, ``numpy.float64`` or ``numpy.complex128`` | ||
| respectively. Other values are returned unchanged. | ||
|
|
||
| Raises: | ||
| OverflowError: if an ``int`` does not fit in ``numpy.int64``. | ||
| """ | ||
| if isinstance(value, np.generic): | ||
| return value | ||
| if isinstance(value, bool): | ||
| return np.bool_(value) | ||
| if isinstance(value, int): | ||
| return np.int64(value) | ||
| if isinstance(value, float): | ||
| return np.float64(value) | ||
| if isinstance(value, complex): | ||
| return np.complex128(value) | ||
| return value | ||
|
|
||
|
|
||
| def pack_value(value, dt): | ||
| """Returns the bytes of ``value`` converted to :class:`numpy.dtype` ``dt``. | ||
|
|
||
| Raises: | ||
| ValueError: if ``value`` can not be represented in ``dt``. | ||
| """ | ||
| fmt = _STRUCT_FORMATS.get((dt.kind, dt.itemsize)) | ||
| try: | ||
| if fmt is None: | ||
| return np.asarray(value, dtype=dt).tobytes() | ||
| if dt.kind == "c": | ||
| cval = complex(value) | ||
| return struct.pack("=" + fmt, cval.real, cval.imag) | ||
| return struct.pack("=" + fmt, value) | ||
| except (struct.error, OverflowError, TypeError, ValueError) as e: | ||
| raise ValueError( | ||
| f"Value {value!r} cannot be represented as dtype '{dt}': {e}" | ||
| ) from None | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.