Skip to content
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

Fixes #723: slow item assignment from numpy array #724

Merged
merged 2 commits into from
Dec 14, 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
6 changes: 6 additions & 0 deletions dpctl/tensor/_copy_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ def _copy_from_numpy_into(dst, np_ary):
if not isinstance(np_ary, np.ndarray):
raise TypeError("Expected numpy.ndarray, got {}".format(type(np_ary)))
src_ary = np.broadcast_to(np.asarray(np_ary, dtype=dst.dtype), dst.shape)
if src_ary.size and (dst.flags & 1) and src_ary.flags["C"]:
dpm.as_usm_memory(dst).copy_from_host(src_ary.reshape((-1,)).view("u1"))
return
if src_ary.size and (dst.flags & 2) and src_ary.flags["F"]:
dpm.as_usm_memory(dst).copy_from_host(src_ary.reshape((-1,)).view("u1"))
return
for i in range(dst.size):
mi = np.unravel_index(i, dst.shape)
host_buf = np.array(src_ary[mi], ndmin=1).view("u1")
Expand Down
14 changes: 13 additions & 1 deletion dpctl/tests/test_tensor_asarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,19 @@ def test_asarray_from_numpy():
Xnp = np.arange(10)
Y = dpt.asarray(Xnp, usm_type="device")
assert type(Y) is dpt.usm_ndarray
assert Y.shape == (10,)
assert Y.shape == Xnp.shape
assert Y.dtype == Xnp.dtype
# Fortan contiguous case
Xnp = np.array([[1, 2, 3], [4, 5, 6]], dtype="f4", order="F")
Y = dpt.asarray(Xnp, usm_type="shared")
assert type(Y) is dpt.usm_ndarray
assert Y.shape == Xnp.shape
assert Y.dtype == Xnp.dtype
# general strided case
Xnp = np.array([[1, 2, 3], [4, 5, 6]], dtype="i8")
Y = dpt.asarray(Xnp[::-1, ::-1], usm_type="host")
assert type(Y) is dpt.usm_ndarray
assert Y.shape == Xnp.shape
assert Y.dtype == Xnp.dtype


Expand Down