Skip to content

Commit 2a94235

Browse files
Merge pull request #724 from IntelPython/fix-slow-copy
Fixes #723: slow item assignment from numpy array
2 parents b7a15ed + cb07c38 commit 2a94235

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

dpctl/tensor/_copy_utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ def _copy_from_numpy_into(dst, np_ary):
117117
if not isinstance(np_ary, np.ndarray):
118118
raise TypeError("Expected numpy.ndarray, got {}".format(type(np_ary)))
119119
src_ary = np.broadcast_to(np.asarray(np_ary, dtype=dst.dtype), dst.shape)
120+
if src_ary.size and (dst.flags & 1) and src_ary.flags["C"]:
121+
dpm.as_usm_memory(dst).copy_from_host(src_ary.reshape((-1,)).view("u1"))
122+
return
123+
if src_ary.size and (dst.flags & 2) and src_ary.flags["F"]:
124+
dpm.as_usm_memory(dst).copy_from_host(src_ary.reshape((-1,)).view("u1"))
125+
return
120126
for i in range(dst.size):
121127
mi = np.unravel_index(i, dst.shape)
122128
host_buf = np.array(src_ary[mi], ndmin=1).view("u1")

dpctl/tests/test_tensor_asarray.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,19 @@ def test_asarray_from_numpy():
6767
Xnp = np.arange(10)
6868
Y = dpt.asarray(Xnp, usm_type="device")
6969
assert type(Y) is dpt.usm_ndarray
70-
assert Y.shape == (10,)
70+
assert Y.shape == Xnp.shape
71+
assert Y.dtype == Xnp.dtype
72+
# Fortan contiguous case
73+
Xnp = np.array([[1, 2, 3], [4, 5, 6]], dtype="f4", order="F")
74+
Y = dpt.asarray(Xnp, usm_type="shared")
75+
assert type(Y) is dpt.usm_ndarray
76+
assert Y.shape == Xnp.shape
77+
assert Y.dtype == Xnp.dtype
78+
# general strided case
79+
Xnp = np.array([[1, 2, 3], [4, 5, 6]], dtype="i8")
80+
Y = dpt.asarray(Xnp[::-1, ::-1], usm_type="host")
81+
assert type(Y) is dpt.usm_ndarray
82+
assert Y.shape == Xnp.shape
7183
assert Y.dtype == Xnp.dtype
7284

7385

0 commit comments

Comments
 (0)