Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Resolve dpt.asarray failed to copy input numpy.ndarray #2055
adds _from_numpy_empty_like_orderK utility function
  • Loading branch information
ndgrigorian committed Apr 23, 2025
commit 346eeddada2f7360acb5c183f030e20ab315684c
67 changes: 46 additions & 21 deletions dpctl/tensor/_copy_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,31 @@ def _copy_from_usm_ndarray_to_usm_ndarray(dst, src):
_copy_same_shape(dst, src_same_shape)


def _make_empty_like_orderK(X, dt, usm_type, dev):
st = list(X.strides)
perm = sorted(
range(X.ndim),
key=lambda d: builtins.abs(st[d]) if X.shape[d] > 1 else 0,
reverse=True,
)
inv_perm = sorted(range(X.ndim), key=lambda i: perm[i])
sh = X.shape
sh_sorted = tuple(sh[i] for i in perm)
R = dpt.empty(sh_sorted, dtype=dt, usm_type=usm_type, device=dev, order="C")
if min(st) < 0:
st_sorted = [st[i] for i in perm]
sl = tuple(
(
slice(None, None, -1)
if st_sorted[i] < 0
else slice(None, None, None)
)
for i in range(X.ndim)
)
R = R[sl]
return dpt.permute_dims(R, inv_perm)


def _empty_like_orderK(X, dt, usm_type=None, dev=None):
"""Returns empty array like `x`, using order='K'

Expand All @@ -371,28 +396,28 @@ def _empty_like_orderK(X, dt, usm_type=None, dev=None):
return dpt.empty_like(
X, dtype=dt, usm_type=usm_type, device=dev, order="F"
)
st = list(X.strides)
perm = sorted(
range(X.ndim),
key=lambda d: builtins.abs(st[d]) if X.shape[d] > 1 else 0,
reverse=True,
)
inv_perm = sorted(range(X.ndim), key=lambda i: perm[i])
sh = X.shape
sh_sorted = tuple(sh[i] for i in perm)
R = dpt.empty(sh_sorted, dtype=dt, usm_type=usm_type, device=dev, order="C")
if min(st) < 0:
st_sorted = [st[i] for i in perm]
sl = tuple(
(
slice(None, None, -1)
if st_sorted[i] < 0
else slice(None, None, None)
)
for i in range(X.ndim)
return _make_empty_like_orderK(X, dt, usm_type, dev)


def _from_numpy_empty_like_orderK(X, dt, usm_type, dev):
"""Returns empty usm_ndarray like NumPy array `x`, using order='K'

For an array `x` that was obtained by permutation of a contiguous
array the returned array will have the same shape and the same
strides as `x`.
"""
if not isinstance(X, np.ndarray):
raise TypeError(f"Expected np.ndarray, got {type(X)}")
Comment thread
ndgrigorian marked this conversation as resolved.
Outdated
fl = X.flags
if fl["C"] or X.size <= 1:
return dpt.empty(
X.shape, dtype=dt, usm_type=usm_type, device=dev, order="C"
)
R = R[sl]
return dpt.permute_dims(R, inv_perm)
elif fl["F"]:
return dpt.empty(
X.shape, dtype=dt, usm_type=usm_type, device=dev, order="F"
)
return _make_empty_like_orderK(X, dt, usm_type, dev)


def _empty_like_pair_orderK(X1, X2, dt, res_shape, usm_type, dev):
Expand Down
24 changes: 5 additions & 19 deletions dpctl/tensor/_ctors.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
import dpctl.tensor as dpt
import dpctl.tensor._tensor_impl as ti
import dpctl.utils
from dpctl.tensor._copy_utils import _empty_like_orderK
from dpctl.tensor._copy_utils import (
_empty_like_orderK,
_from_numpy_empty_like_orderK,
)
from dpctl.tensor._data_types import _get_dtype
from dpctl.tensor._device import normalize_queue_device
from dpctl.tensor._usmarray import _is_object_with_buffer_protocol
Expand Down Expand Up @@ -243,24 +246,7 @@ def _asarray_from_numpy_ndarray(
if order == "K":
# new USM allocation
_ensure_native_dtype_device_support(dtype, copy_q.sycl_device)
Comment thread
ndgrigorian marked this conversation as resolved.
Outdated
res = dpt.usm_ndarray(
ary.shape,
dtype=dtype,
buffer=usm_type,
order="C",
buffer_ctor_kwargs={"queue": copy_q},
)
original_strides = ary.strides
ind = sorted(
range(ary.ndim),
key=lambda i: abs(original_strides[i]),
reverse=True,
)
new_strides = tuple(res.strides[ind[i]] for i in ind)
# reuse previously made USM allocation
res = dpt.usm_ndarray(
res.shape, dtype=res.dtype, buffer=res.usm_data, strides=new_strides
)
res = _from_numpy_empty_like_orderK(ary, dtype, usm_type, copy_q)
else:
_ensure_native_dtype_device_support(dtype, copy_q.sycl_device)
res = dpt.usm_ndarray(
Expand Down