Skip to content

Return default floating point type of the device in dpctl.tensor.astype when newdtype=None #1262

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
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
6 changes: 4 additions & 2 deletions dpctl/tensor/_copy_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import dpctl.tensor as dpt
import dpctl.tensor._tensor_impl as ti
import dpctl.utils
from dpctl.tensor._ctors import _get_dtype
from dpctl.tensor._device import normalize_queue_device

__doc__ = (
Expand Down Expand Up @@ -364,7 +365,8 @@ def astype(usm_ary, newdtype, order="K", casting="unsafe", copy=True):
array (usm_ndarray):
An input array.
new_dtype (dtype):
The data type of the resulting array.
The data type of the resulting array. If `None`, gives default
floating point type supported by device where `array` is allocated.
order ({"C", "F", "A", "K"}, optional):
Controls memory layout of the resulting array if a copy
is returned.
Expand Down Expand Up @@ -392,7 +394,7 @@ 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 = dpt.dtype(newdtype)
target_dtype = _get_dtype(newdtype, usm_ary.sycl_queue)
if not dpt.can_cast(ary_dtype, target_dtype, casting=casting):
raise TypeError(
f"Can not cast from {ary_dtype} to {newdtype} "
Expand Down
5 changes: 5 additions & 0 deletions dpctl/tests/test_usm_ndarray_ctor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,11 @@ def test_astype():
assert np.allclose(dpt.to_numpy(Y), np.full(Y.shape, 7, dtype="f4"))
Y = dpt.astype(X[::2, ::-1], "i4", order="K", copy=False)
assert Y.usm_data is X.usm_data
Y = dpt.astype(X, None, order="K")
if X.sycl_queue.sycl_device.has_aspect_fp64:
assert Y.dtype is dpt.float64
else:
assert Y.dtype is dpt.float32


def test_astype_invalid_order():
Expand Down