Skip to content

Handle numpy arrays with usm memory underneath in dpctl.tensor.asarray #1117

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
Mar 13, 2023
Merged
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
17 changes: 16 additions & 1 deletion dpctl/tensor/_copy_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,22 @@ def _copy_from_numpy_into(dst, np_ary):
raise TypeError(f"Expected numpy.ndarray, got {type(np_ary)}")
if not isinstance(dst, dpt.usm_ndarray):
raise TypeError(f"Expected usm_ndarray, got {type(dst)}")
src_ary = np.broadcast_to(np_ary, dst.shape)
if np_ary.flags["OWNDATA"]:
Xnp = np_ary
else:
# Determine base of input array
base = np_ary.base
while isinstance(base, np.ndarray):
base = base.base
if isinstance(base, dpm._memory._Memory):
# we must perform a copy, since subsequent
# _copy_numpy_ndarray_into_usm_ndarray is implemented using
# sycl::buffer, and using USM-pointers with sycl::buffer
# results is undefined behavior
Xnp = np_ary.copy()
else:
Xnp = np_ary
src_ary = np.broadcast_to(Xnp, dst.shape)
copy_q = dst.sycl_queue
if copy_q.sycl_device.has_aspect_fp64 is False:
src_ary_dt_c = src_ary.dtype.char
Expand Down