Skip to content

Added dpt.copy, and dpt.asnumpy (alias of dpt.to_numpy) #595

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 1 commit into from
Sep 26, 2021
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
5 changes: 4 additions & 1 deletion dpctl/tensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,19 @@

"""

from dpctl.tensor._copy_utils import astype
from dpctl.tensor._copy_utils import astype, copy
from dpctl.tensor._copy_utils import copy_from_numpy as from_numpy
from dpctl.tensor._copy_utils import copy_to_numpy as asnumpy
from dpctl.tensor._copy_utils import copy_to_numpy as to_numpy
from dpctl.tensor._reshape import reshape
from dpctl.tensor._usmarray import usm_ndarray

__all__ = [
"usm_ndarray",
"astype",
"copy",
"reshape",
"from_numpy",
"to_numpy",
"asnumpy",
]
75 changes: 75 additions & 0 deletions dpctl/tensor/_copy_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,71 @@ def copy_from_usm_ndarray_to_usm_ndarray(dst, src):
copy_same_shape(dst, src_same_shape)


def copy(usm_ary, order="K"):
"""
Creates a copy of given instance of `usm_ndarray`.

Memory layour of the copy is controlled by `order` keyword,
following NumPy's conventions. The `order` keywords can be
one of the following:

"C": C-contiguous memory layout
"F": Fotrant-contiguous memory layout
"A": Fotrant-contiguous if the input array is
F-contiguous, and C-contiguous otherwise
"K": match the layout of `usm_ary` as closely
as possible.

"""
if not isinstance(usm_ary, dpt.usm_ndarray):
return TypeError(
"Expected object of type dpt.usm_ndarray, got {}".format(
type(usm_ary)
)
)
copy_order = "C"
if order == "C":
pass
elif order == "F":
copy_order = order
elif order == "A":
if usm_ary.flags & 2:
copy_order = "F"
elif order == "K":
if usm_ary.flags & 2:
copy_order = "F"
else:
raise ValueError(
"Unrecognized value of the order keyword. "
"Recognized values are 'A', 'C', 'F', or 'K'"
)
c_contig = usm_ary.flags & 1
f_contig = usm_ary.flags & 2
R = dpt.usm_ndarray(
usm_ary.shape,
dtype=usm_ary.dtype,
buffer=usm_ary.usm_type,
order=copy_order,
buffer_ctor_kwargs={"queue": usm_ary.sycl_queue},
)
if order == "K" and (not c_contig and not f_contig):
original_strides = usm_ary.strides
ind = sorted(
range(usm_ary.ndim),
key=lambda i: abs(original_strides[i]),
reverse=True,
)
new_strides = tuple(R.strides[ind[i]] for i in ind)
R = dpt.usm_ndarray(
usm_ary.shape,
dtype=usm_ary.dtype,
buffer=R.usm_data,
strides=new_strides,
)
copy_same_dtype(R, usm_ary)
return R


def astype(usm_ary, newdtype, order="K", casting="unsafe", copy=True):
"""
astype(usm_array, new_dtype, order="K", casting="unsafe", copy=True)
Expand All @@ -267,6 +332,11 @@ def astype(usm_ary, newdtype, order="K", casting="unsafe", copy=True):
type(usm_ary)
)
)
if not isinstance(order, str) or order not in ["A", "C", "F", "K"]:
raise ValueError(
"Unrecognized value of the order keyword. "
"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):
Expand Down Expand Up @@ -294,6 +364,11 @@ def astype(usm_ary, newdtype, order="K", casting="unsafe", copy=True):
elif order == "K":
if usm_ary.flags & 2:
copy_order = "F"
else:
raise ValueError(
"Unrecognized value of the order keyword. "
"Recognized values are 'A', 'C', 'F', or 'K'"
)
R = dpt.usm_ndarray(
usm_ary.shape,
dtype=target_dtype,
Expand Down
28 changes: 28 additions & 0 deletions dpctl/tests/test_usm_ndarray_ctor.py
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,34 @@ def test_astype():
assert Y.usm_data is X.usm_data


def test_astype_invalid_order():
X = dpt.usm_ndarray(5, "i4")
with pytest.raises(ValueError):
dpt.astype(X, "i4", order="WRONG")


def test_copy():
X = dpt.usm_ndarray((5, 5), "i4")[2:4, 1:4]
X[:] = 42
Yc = dpt.copy(X, order="C")
Yf = dpt.copy(X, order="F")
Ya = dpt.copy(X, order="A")
Yk = dpt.copy(X, order="K")
assert Yc.usm_data is not X.usm_data
assert Yf.usm_data is not X.usm_data
assert Ya.usm_data is not X.usm_data
assert Yk.usm_data is not X.usm_data
assert Yc.strides == (3, 1)
assert Yf.strides == (1, 2)
assert Ya.strides == (3, 1)
assert Yk.strides == (3, 1)
ref = np.full(X.shape, 42, dtype=X.dtype)
assert np.array_equal(dpt.asnumpy(Yc), ref)
assert np.array_equal(dpt.asnumpy(Yf), ref)
assert np.array_equal(dpt.asnumpy(Ya), ref)
assert np.array_equal(dpt.asnumpy(Yk), ref)


def test_ctor_invalid():
m = dpm.MemoryUSMShared(12)
with pytest.raises(ValueError):
Expand Down