|
| 1 | +# Data Parallel Control (dpctl) |
| 2 | +# |
| 3 | +# Copyright 2020-2023 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 | +import dpctl |
| 18 | +import dpctl.tensor as dpt |
| 19 | +from dpctl.tensor._tensor_impl import ( |
| 20 | + default_device_complex_type, |
| 21 | + default_device_fp_type, |
| 22 | + default_device_index_type, |
| 23 | + default_device_int_type, |
| 24 | +) |
| 25 | + |
| 26 | + |
| 27 | +def _isdtype_impl(dtype, kind): |
| 28 | + if isinstance(kind, dpt.dtype): |
| 29 | + return dtype == kind |
| 30 | + |
| 31 | + elif isinstance(kind, str): |
| 32 | + if kind == "bool": |
| 33 | + return dtype.kind == "b" |
| 34 | + elif kind == "signed integer": |
| 35 | + return dtype.kind == "i" |
| 36 | + elif kind == "unsigned integer": |
| 37 | + return dtype.kind == "u" |
| 38 | + elif kind == "integral": |
| 39 | + return dtype.kind in "iu" |
| 40 | + elif kind == "real floating": |
| 41 | + return dtype.kind == "f" |
| 42 | + elif kind == "complex floating": |
| 43 | + return dtype.kind == "c" |
| 44 | + elif kind == "numeric": |
| 45 | + return dtype.kind in "iufc" |
| 46 | + else: |
| 47 | + raise ValueError(f"Unrecognized data type kind: {kind}") |
| 48 | + |
| 49 | + elif isinstance(kind, tuple): |
| 50 | + return any(_isdtype_impl(dtype, k) for k in kind) |
| 51 | + else: |
| 52 | + raise TypeError(f"Unsupported data type kind: {kind}") |
| 53 | + |
| 54 | + |
| 55 | +class __array_namespace_info__: |
| 56 | + def __init__(self): |
| 57 | + self._capabilities = { |
| 58 | + "boolean_indexing": True, |
| 59 | + "data_dependent_shapes": True, |
| 60 | + } |
| 61 | + self._all_dtypes = { |
| 62 | + "bool": dpt.bool, |
| 63 | + "float16": dpt.float16, |
| 64 | + "float32": dpt.float32, |
| 65 | + "float64": dpt.float64, |
| 66 | + "complex64": dpt.complex64, |
| 67 | + "complex128": dpt.complex128, |
| 68 | + "int8": dpt.int8, |
| 69 | + "int16": dpt.int16, |
| 70 | + "int32": dpt.int32, |
| 71 | + "int64": dpt.int64, |
| 72 | + "uint8": dpt.uint8, |
| 73 | + "uint16": dpt.uint16, |
| 74 | + "uint32": dpt.uint32, |
| 75 | + "uint64": dpt.uint64, |
| 76 | + } |
| 77 | + |
| 78 | + def capabilities(self): |
| 79 | + return self._capabilities.copy() |
| 80 | + |
| 81 | + def default_device(self): |
| 82 | + return dpctl.select_default_device() |
| 83 | + |
| 84 | + def default_dtypes(self, device=None): |
| 85 | + if device is None: |
| 86 | + device = dpctl.select_default_device() |
| 87 | + return { |
| 88 | + "real floating": default_device_fp_type(device), |
| 89 | + "complex floating": default_device_complex_type, |
| 90 | + "integral": default_device_int_type(device), |
| 91 | + "indexing": default_device_index_type(device), |
| 92 | + } |
| 93 | + |
| 94 | + def dtypes(self, device=None, kind=None): |
| 95 | + if device is None: |
| 96 | + device = dpctl.select_default_device() |
| 97 | + ignored_types = [] |
| 98 | + if not device.has_aspect_fp16: |
| 99 | + ignored_types.append("float16") |
| 100 | + if not device.has_aspect_fp64: |
| 101 | + ignored_types.append("float64") |
| 102 | + if kind is None: |
| 103 | + return { |
| 104 | + key: val |
| 105 | + for key, val in self._all_dtypes.items() |
| 106 | + if key not in ignored_types |
| 107 | + } |
| 108 | + else: |
| 109 | + return { |
| 110 | + key: val |
| 111 | + for key, val in self._all_dtypes.items() |
| 112 | + if key not in ignored_types and _isdtype_impl(val, kind) |
| 113 | + } |
| 114 | + |
| 115 | + def devices(self): |
| 116 | + return dpctl.get_devices() |
0 commit comments