|
| 1 | +# Data Parallel Control (dpctl) |
| 2 | +# |
| 3 | +# Copyright 2026 Intel Corporation |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +"""Data type handling shared by the dpctl functions taking a data type.""" |
| 18 | + |
| 19 | +import struct |
| 20 | + |
| 21 | +import numpy as np |
| 22 | + |
| 23 | +# kinds of NumPy data types whose values are not plain bytes |
| 24 | +_NON_POD_KINDS = frozenset("OSUMmT") |
| 25 | + |
| 26 | +# extended precision types, which devices do not support |
| 27 | +_EXTENDED_PRECISION_NUMS = frozenset( |
| 28 | + np.dtype(t).num |
| 29 | + for t in (np.longdouble, np.clongdouble) |
| 30 | + if np.finfo(t).nmant > np.finfo(np.float64).nmant |
| 31 | +) |
| 32 | + |
| 33 | +# struct formats of the data types whose values are range checked when packed |
| 34 | +_STRUCT_FORMATS = { |
| 35 | + ("b", 1): "?", |
| 36 | + ("i", 1): "b", |
| 37 | + ("i", 2): "h", |
| 38 | + ("i", 4): "i", |
| 39 | + ("i", 8): "q", |
| 40 | + ("u", 1): "B", |
| 41 | + ("u", 2): "H", |
| 42 | + ("u", 4): "I", |
| 43 | + ("u", 8): "Q", |
| 44 | + ("f", 2): "e", |
| 45 | + ("f", 4): "f", |
| 46 | + ("f", 8): "d", |
| 47 | + ("c", 8): "ff", |
| 48 | + ("c", 16): "dd", |
| 49 | +} |
| 50 | + |
| 51 | + |
| 52 | +def is_supported_dtype(dt): |
| 53 | + """Returns whether values of :class:`numpy.dtype` ``dt`` can be used on a |
| 54 | + device.""" |
| 55 | + return not ( |
| 56 | + dt.hasobject |
| 57 | + or dt.kind in _NON_POD_KINDS |
| 58 | + or dt.itemsize == 0 |
| 59 | + or dt.num in _EXTENDED_PRECISION_NUMS |
| 60 | + ) |
| 61 | + |
| 62 | + |
| 63 | +def as_dtype(dtype): |
| 64 | + """Returns ``numpy.dtype(dtype)``. |
| 65 | +
|
| 66 | + Raises: |
| 67 | + ValueError: if ``dtype`` is not a supported data type. |
| 68 | + """ |
| 69 | + try: |
| 70 | + dt = np.dtype(dtype) |
| 71 | + except TypeError: |
| 72 | + raise ValueError(f"Unrecognized dtype '{dtype}'") from None |
| 73 | + if not is_supported_dtype(dt): |
| 74 | + raise ValueError(f"Unsupported dtype '{dt}'") |
| 75 | + return dt |
| 76 | + |
| 77 | + |
| 78 | +def as_scalar(value): |
| 79 | + """Converts a Python ``bool``, ``int``, ``float`` or ``complex`` to a |
| 80 | + ``numpy.bool``, ``numpy.int64``, ``numpy.float64`` or ``numpy.complex128`` |
| 81 | + respectively. Other values are returned unchanged. |
| 82 | +
|
| 83 | + Raises: |
| 84 | + OverflowError: if an ``int`` does not fit in ``numpy.int64``. |
| 85 | + """ |
| 86 | + if isinstance(value, np.generic): |
| 87 | + return value |
| 88 | + if isinstance(value, bool): |
| 89 | + return np.bool_(value) |
| 90 | + if isinstance(value, int): |
| 91 | + return np.int64(value) |
| 92 | + if isinstance(value, float): |
| 93 | + return np.float64(value) |
| 94 | + if isinstance(value, complex): |
| 95 | + return np.complex128(value) |
| 96 | + return value |
| 97 | + |
| 98 | + |
| 99 | +def pack_value(value, dt): |
| 100 | + """Returns the bytes of ``value`` converted to :class:`numpy.dtype` ``dt``. |
| 101 | +
|
| 102 | + Raises: |
| 103 | + ValueError: if ``value`` can not be represented in ``dt``. |
| 104 | + """ |
| 105 | + fmt = _STRUCT_FORMATS.get((dt.kind, dt.itemsize)) |
| 106 | + try: |
| 107 | + if fmt is None: |
| 108 | + return np.asarray(value, dtype=dt).tobytes() |
| 109 | + if dt.kind == "c": |
| 110 | + cval = complex(value) |
| 111 | + return struct.pack("=" + fmt, cval.real, cval.imag) |
| 112 | + return struct.pack("=" + fmt, value) |
| 113 | + except (struct.error, OverflowError, TypeError, ValueError) as e: |
| 114 | + raise ValueError( |
| 115 | + f"Value {value!r} cannot be represented as dtype '{dt}': {e}" |
| 116 | + ) from None |
0 commit comments