Skip to content

Exported data types, finfo and iinfo symbols #913

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

Merged
merged 6 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions dpctl/tensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

"""

from numpy import dtype

from dpctl.tensor._copy_utils import asnumpy, astype, copy, from_numpy, to_numpy
from dpctl.tensor._ctors import (
arange,
Expand All @@ -44,17 +46,36 @@
from dpctl.tensor._manipulation_functions import (
broadcast_arrays,
broadcast_to,
can_cast,
concat,
expand_dims,
finfo,
flip,
iinfo,
permute_dims,
result_type,
roll,
squeeze,
stack,
)
from dpctl.tensor._reshape import reshape
from dpctl.tensor._usmarray import usm_ndarray

bool = dtype("bool")
int8 = dtype("int8")
int16 = dtype("int16")
int32 = dtype("int32")
int64 = dtype("int64")
uint8 = dtype("uint8")
uint16 = dtype("uint16")
uint32 = dtype("uint32")
uint64 = dtype("uint64")
float16 = dtype("float16")
float32 = dtype("float32")
float64 = dtype("float64")
complex64 = dtype("complex64")
complex128 = dtype("complex128")

__all__ = [
"Device",
"usm_ndarray",
Expand Down Expand Up @@ -88,5 +109,24 @@
"from_dlpack",
"tril",
"triu",
"dtype",
"bool",
"int8",
"uint8",
"int16",
"uint16",
"int32",
"uint32",
"int64",
"uint64",
"float16",
"float32",
"float64",
"complex64",
"complex128",
"iinfo",
"finfo",
"can_cast",
"result_type",
"meshgrid",
]
6 changes: 3 additions & 3 deletions dpctl/tensor/_copy_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def _copy_from_numpy(np_ary, usm_type="device", sycl_queue=None):
dt = Xnp.dtype
if dt.char in "dD" and alloc_q.sycl_device.has_aspect_fp64 is False:
Xusm_dtype = (
np.dtype("float32") if dt.char == "d" else np.dtype("complex64")
dpt.dtype("float32") if dt.char == "d" else dpt.dtype("complex64")
)
else:
Xusm_dtype = dt
Expand Down Expand Up @@ -318,8 +318,8 @@ def astype(usm_ary, newdtype, order="K", casting="unsafe", copy=True):
"Recognized values are 'A', 'C', 'F', or 'K'"
)
ary_dtype = usm_ary.dtype
target_dtype = np.dtype(newdtype)
if not np.can_cast(ary_dtype, target_dtype, casting=casting):
target_dtype = dpt.dtype(newdtype)
if not dpt.can_cast(ary_dtype, target_dtype, casting=casting):
raise TypeError(
"Can not cast from {} to {} according to rule {}".format(
ary_dtype, newdtype, casting
Expand Down
22 changes: 11 additions & 11 deletions dpctl/tensor/_ctors.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@ def _get_dtype(dtype, sycl_obj, ref_type=None):
if dtype is None:
if ref_type in [None, float] or np.issubdtype(ref_type, np.floating):
dtype = ti.default_device_fp_type(sycl_obj)
return np.dtype(dtype)
return dpt.dtype(dtype)
elif ref_type in [bool, np.bool_]:
dtype = ti.default_device_bool_type(sycl_obj)
return np.dtype(dtype)
return dpt.dtype(dtype)
elif ref_type is int or np.issubdtype(ref_type, np.integer):
dtype = ti.default_device_int_type(sycl_obj)
return np.dtype(dtype)
return dpt.dtype(dtype)
elif ref_type is complex or np.issubdtype(ref_type, np.complexfloating):
dtype = ti.default_device_complex_type(sycl_obj)
return np.dtype(dtype)
return dpt.dtype(dtype)
else:
raise TypeError(f"Reference type {ref_type} not recognized.")
else:
return np.dtype(dtype)
return dpt.dtype(dtype)


def _array_info_dispatch(obj):
Expand Down Expand Up @@ -313,7 +313,7 @@ def asarray(
)
# 2. Check that dtype is None, or a valid dtype
if dtype is not None:
dtype = np.dtype(dtype)
dtype = dpt.dtype(dtype)
# 3. Validate order
if not isinstance(order, str):
raise TypeError(
Expand Down Expand Up @@ -768,7 +768,7 @@ def empty_like(
device = x.device
sycl_queue = normalize_queue_device(sycl_queue=sycl_queue, device=device)
sh = x.shape
dtype = np.dtype(dtype)
dtype = dpt.dtype(dtype)
res = dpt.usm_ndarray(
sh,
dtype=dtype,
Expand Down Expand Up @@ -825,7 +825,7 @@ def zeros_like(
device = x.device
sycl_queue = normalize_queue_device(sycl_queue=sycl_queue, device=device)
sh = x.shape
dtype = np.dtype(dtype)
dtype = dpt.dtype(dtype)
return zeros(
sh,
dtype=dtype,
Expand Down Expand Up @@ -882,7 +882,7 @@ def ones_like(
device = x.device
sycl_queue = normalize_queue_device(sycl_queue=sycl_queue, device=device)
sh = x.shape
dtype = np.dtype(dtype)
dtype = dpt.dtype(dtype)
return ones(
sh,
dtype=dtype,
Expand Down Expand Up @@ -946,7 +946,7 @@ def full_like(
device = x.device
sycl_queue = normalize_queue_device(sycl_queue=sycl_queue, device=device)
sh = x.shape
dtype = np.dtype(dtype)
dtype = dpt.dtype(dtype)
return full(
sh,
fill_value,
Expand Down Expand Up @@ -1026,7 +1026,7 @@ def linspace(
)
if dtype is None and np.issubdtype(dt, np.integer):
dt = ti.default_device_fp_type(sycl_queue)
dt = np.dtype(dt)
dt = dpt.dtype(dt)
start = float(start)
stop = float(stop)
res = dpt.empty(num, dtype=dt, sycl_queue=sycl_queue)
Expand Down
73 changes: 71 additions & 2 deletions dpctl/tensor/_manipulation_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,7 @@ def _arrays_validation(arrays):
raise ValueError("All the input arrays must have usm_type")

X0 = arrays[0]
if not all(Xi.dtype.char in "?bBhHiIlLqQefdFD" for Xi in arrays):
raise ValueError("Unsupported dtype encountered.")
_supported_dtype(Xi.dtype for Xi in arrays)

res_dtype = X0.dtype
for i in range(1, n):
Expand Down Expand Up @@ -421,3 +420,73 @@ def stack(arrays, axis=0):
dpctl.SyclEvent.wait_for(hev_list)

return res


def can_cast(from_, to, casting="safe"):
"""
can_cast(from: usm_ndarray or dtype, to: dtype) -> bool

Determines if one data type can be cast to another data type according \
to Type Promotion Rules rules.
"""
if isinstance(to, dpt.usm_ndarray):
raise TypeError("Expected dtype type.")

dtype_to = dpt.dtype(to)

dtype_from = (
from_.dtype if isinstance(from_, dpt.usm_ndarray) else dpt.dtype(from_)
)

_supported_dtype([dtype_from, dtype_to])

return np.can_cast(dtype_from, dtype_to, casting)


def result_type(*arrays_and_dtypes):
"""
result_type(arrays_and_dtypes: an arbitrary number usm_ndarrays or dtypes)\
-> dtype

Returns the dtype that results from applying the Type Promotion Rules to \
the arguments.
"""
dtypes = [
X.dtype if isinstance(X, dpt.usm_ndarray) else dpt.dtype(X)
for X in arrays_and_dtypes
]

_supported_dtype(dtypes)

return np.result_type(*dtypes)


def iinfo(type):
"""
iinfo(type: integer data-type) -> iinfo_object

Returns machine limits for integer data types.
"""
if isinstance(type, dpt.usm_ndarray):
raise TypeError("Expected dtype type, get {to}.")
_supported_dtype([dpt.dtype(type)])
return np.iinfo(type)


def finfo(type):
"""
finfo(type: float data-type) -> finfo_object

Returns machine limits for float data types.
"""
if isinstance(type, dpt.usm_ndarray):
raise TypeError("Expected dtype type, get {to}.")
_supported_dtype([dpt.dtype(type)])
return np.finfo(type)


def _supported_dtype(dtypes):
for dtype in dtypes:
if dtype.char not in "?bBhHiIlLqQefdFD":
raise ValueError(f"Dpctl doesn't support dtype {dtype}.")
return True
16 changes: 8 additions & 8 deletions dpctl/tests/test_sycl_kernel_submit.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,21 @@
@pytest.mark.parametrize(
"ctype_str,dtype,ctypes_ctor",
[
("short", np.dtype("i2"), ctypes.c_short),
("int", np.dtype("i4"), ctypes.c_int),
("unsigned int", np.dtype("u4"), ctypes.c_uint),
("long", np.dtype(np.longlong), ctypes.c_longlong),
("unsigned long", np.dtype(np.ulonglong), ctypes.c_ulonglong),
("float", np.dtype("f4"), ctypes.c_float),
("double", np.dtype("f8"), ctypes.c_double),
("short", dpt.dtype("i2"), ctypes.c_short),
("int", dpt.dtype("i4"), ctypes.c_int),
("unsigned int", dpt.dtype("u4"), ctypes.c_uint),
("long", dpt.dtype(np.longlong), ctypes.c_longlong),
("unsigned long", dpt.dtype(np.ulonglong), ctypes.c_ulonglong),
("float", dpt.dtype("f4"), ctypes.c_float),
("double", dpt.dtype("f8"), ctypes.c_double),
],
)
def test_create_program_from_source(ctype_str, dtype, ctypes_ctor):
try:
q = dpctl.SyclQueue("opencl", property="enable_profiling")
except dpctl.SyclQueueCreationError:
pytest.skip("OpenCL queue could not be created")
if dtype == np.dtype("f8") and q.sycl_device.has_aspect_fp64 is False:
if dtype == dpt.dtype("f8") and q.sycl_device.has_aspect_fp64 is False:
pytest.skip(
"Device does not support double precision floating point type"
)
Expand Down
14 changes: 7 additions & 7 deletions dpctl/tests/test_tensor_asarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,21 +175,21 @@ def test_asarray_scalars():
import ctypes

Y = dpt.asarray(5)
assert Y.dtype == np.dtype(int)
assert Y.dtype == dpt.dtype(int)
Y = dpt.asarray(5.2)
if Y.sycl_device.has_aspect_fp64:
assert Y.dtype == np.dtype(float)
assert Y.dtype == dpt.dtype(float)
else:
assert Y.dtype == np.dtype(np.float32)
assert Y.dtype == dpt.dtype(dpt.float32)
Y = dpt.asarray(np.float32(2.3))
assert Y.dtype == np.dtype(np.float32)
assert Y.dtype == dpt.dtype(dpt.float32)
Y = dpt.asarray(1.0j)
if Y.sycl_device.has_aspect_fp64:
assert Y.dtype == np.dtype(complex)
assert Y.dtype == dpt.dtype(complex)
else:
assert Y.dtype == np.dtype(np.complex64)
assert Y.dtype == dpt.dtype(dpt.complex64)
Y = dpt.asarray(ctypes.c_int(8))
assert Y.dtype == np.dtype(ctypes.c_int)
assert Y.dtype == dpt.dtype(ctypes.c_int)


def test_asarray_copy_false():
Expand Down
Loading