Skip to content

dpt.asarray can now migrate arrays across devices #951

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
Oct 25, 2022
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
13 changes: 9 additions & 4 deletions dpctl/tensor/_ctors.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,15 @@ def _asarray_from_usm_ndarray(
order=order,
buffer_ctor_kwargs={"queue": copy_q},
)
hev, _ = ti._copy_usm_ndarray_into_usm_ndarray(
src=usm_ndary, dst=res, sycl_queue=copy_q
)
hev.wait()
eq = dpctl.utils.get_execution_queue([usm_ndary.sycl_queue, copy_q])
if eq is not None:
hev, _ = ti._copy_usm_ndarray_into_usm_ndarray(
src=usm_ndary, dst=res, sycl_queue=eq
)
hev.wait()
else:
tmp = dpt.asnumpy(usm_ndary)
res[...] = tmp
return res


Expand Down
19 changes: 11 additions & 8 deletions dpctl/tests/test_tensor_asarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import numpy as np
import pytest
from helper import get_queue_or_skip

import dpctl
import dpctl.tensor as dpt
Expand Down Expand Up @@ -193,10 +194,7 @@ def test_asarray_scalars():


def test_asarray_copy_false():
try:
q = dpctl.SyclQueue()
except dpctl.SyclQueueCreationError:
pytest.skip("Could not create a queue")
q = get_queue_or_skip()
rng = np.random.default_rng()
Xnp = rng.integers(low=-255, high=255, size=(10, 4), dtype=np.int64)
X = dpt.from_numpy(Xnp, usm_type="device", sycl_queue=q)
Expand Down Expand Up @@ -229,10 +227,15 @@ def test_asarray_copy_false():


def test_asarray_invalid_dtype():
try:
q = dpctl.SyclQueue()
except dpctl.SyclQueueCreationError:
pytest.skip("Could not create a queue")
q = get_queue_or_skip()
Xnp = np.array([1, 2, 3], dtype=object)
with pytest.raises(TypeError):
dpt.asarray(Xnp, sycl_queue=q)


def test_asarray_cross_device():
q = get_queue_or_skip()
qprof = dpctl.SyclQueue(property="enable_profiling")
x = dpt.empty(10, dtype="i8", sycl_queue=q)
y = dpt.asarray(x, sycl_queue=qprof)
assert y.sycl_queue == qprof