Skip to content

Commit 8875df8

Browse files
authored
Merge branch 'master' into dependabot/github_actions/actions/upload-artifact-4.3.5
2 parents 83d70b7 + 86a8823 commit 8875df8

File tree

9 files changed

+174
-123
lines changed

9 files changed

+174
-123
lines changed

dpnp/dpnp_iface.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ def get_result_array(a, out=None, casting="safe"):
654654
655655
Parameters
656656
----------
657-
a : {dpnp_array}
657+
a : {dpnp.ndarray, usm_ndarray}
658658
Input array.
659659
out : {dpnp.ndarray, usm_ndarray}
660660
If provided, value of `a` array will be copied into it
@@ -671,6 +671,8 @@ def get_result_array(a, out=None, casting="safe"):
671671
"""
672672

673673
if out is None:
674+
if isinstance(a, dpt.usm_ndarray):
675+
return dpnp_array._create_from_usm_ndarray(a)
674676
return a
675677

676678
if isinstance(out, dpt.usm_ndarray):

dpnp/dpnp_iface_counting.py

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,25 +44,38 @@
4444
__all__ = ["count_nonzero"]
4545

4646

47-
def count_nonzero(a, axis=None, *, keepdims=False):
47+
def count_nonzero(a, axis=None, *, keepdims=False, out=None):
4848
"""
4949
Counts the number of non-zero values in the array `a`.
5050
5151
For full documentation refer to :obj:`numpy.count_nonzero`.
5252
53+
Parameters
54+
----------
55+
a : {dpnp.ndarray, usm_ndarray}
56+
The array for which to count non-zeros.
57+
axis : {None, int, tuple}, optional
58+
Axis or tuple of axes along which to count non-zeros.
59+
Default value means that non-zeros will be counted along a flattened
60+
version of `a`.
61+
Default: ``None``.
62+
keepdims : bool, optional
63+
If this is set to ``True``, the axes that are counted are left in the
64+
result as dimensions with size one. With this option, the result will
65+
broadcast correctly against the input array.
66+
Default: ``False``.
67+
out : {None, dpnp.ndarray, usm_ndarray}, optional
68+
The array into which the result is written. The data type of `out` must
69+
match the expected shape and the expected data type of the result.
70+
If ``None`` then a new array is returned.
71+
Default: ``None``.
72+
5373
Returns
5474
-------
5575
out : dpnp.ndarray
5676
Number of non-zero values in the array along a given axis.
57-
Otherwise, a zero-dimensional array with the total number of
58-
non-zero values in the array is returned.
59-
60-
Limitations
61-
-----------
62-
Parameters `a` is supported as either :class:`dpnp.ndarray`
63-
or :class:`dpctl.tensor.usm_ndarray`.
64-
Otherwise ``TypeError`` exception will be raised.
65-
Input array data types are limited by supported DPNP :ref:`Data types`.
77+
Otherwise, a zero-dimensional array with the total number of non-zero
78+
values in the array is returned.
6679
6780
See Also
6881
--------
@@ -87,8 +100,10 @@ def count_nonzero(a, axis=None, *, keepdims=False):
87100
88101
"""
89102

90-
# TODO: might be improved by implementing an extension
91-
# with `count_nonzero` kernel
92103
usm_a = dpnp.get_usm_ndarray(a)
93-
usm_a = dpt.astype(usm_a, dpnp.bool, copy=False)
94-
return dpnp.sum(usm_a, axis=axis, dtype=dpnp.intp, keepdims=keepdims)
104+
usm_out = None if out is None else dpnp.get_usm_ndarray(out)
105+
106+
usm_res = dpt.count_nonzero(
107+
usm_a, axis=axis, keepdims=keepdims, out=usm_out
108+
)
109+
return dpnp.get_result_array(usm_res, out)

dpnp/dpnp_iface_logic.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151

5252
import dpnp
5353
from dpnp.dpnp_algo.dpnp_elementwise_common import DPNPBinaryFunc, DPNPUnaryFunc
54-
from dpnp.dpnp_array import dpnp_array
5554

5655
__all__ = [
5756
"all",
@@ -167,13 +166,11 @@ def all(a, /, axis=None, out=None, keepdims=False, *, where=True):
167166

168167
dpnp.check_limitations(where=where)
169168

170-
dpt_array = dpnp.get_usm_ndarray(a)
171-
result = dpnp_array._create_from_usm_ndarray(
172-
dpt.all(dpt_array, axis=axis, keepdims=keepdims)
173-
)
169+
usm_a = dpnp.get_usm_ndarray(a)
170+
usm_res = dpt.all(usm_a, axis=axis, keepdims=keepdims)
171+
174172
# TODO: temporary solution until dpt.all supports out parameter
175-
result = dpnp.get_result_array(result, out)
176-
return result
173+
return dpnp.get_result_array(usm_res, out)
177174

178175

179176
def allclose(a, b, rtol=1.0e-5, atol=1.0e-8, equal_nan=False):
@@ -333,13 +330,11 @@ def any(a, /, axis=None, out=None, keepdims=False, *, where=True):
333330

334331
dpnp.check_limitations(where=where)
335332

336-
dpt_array = dpnp.get_usm_ndarray(a)
337-
result = dpnp_array._create_from_usm_ndarray(
338-
dpt.any(dpt_array, axis=axis, keepdims=keepdims)
339-
)
333+
usm_a = dpnp.get_usm_ndarray(a)
334+
usm_res = dpt.any(usm_a, axis=axis, keepdims=keepdims)
335+
340336
# TODO: temporary solution until dpt.any supports out parameter
341-
result = dpnp.get_result_array(result, out)
342-
return result
337+
return dpnp.get_result_array(usm_res, out)
343338

344339

345340
_EQUAL_DOCSTRING = """

dpnp/dpnp_iface_mathematical.py

Lines changed: 8 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -130,31 +130,6 @@
130130
]
131131

132132

133-
def _append_to_diff_array(a, axis, combined, values):
134-
"""
135-
Append `values` to `combined` list based on data of array `a`.
136-
137-
Scalar value (including case with 0d array) is expanded to an array
138-
with length=1 in the direction of axis and the shape of the input array `a`
139-
along all other axes.
140-
Note, if `values` is a scalar, then it is converted to 0d array allocating
141-
on the same SYCL queue as the input array `a` and with the same USM type.
142-
143-
"""
144-
145-
dpnp.check_supported_arrays_type(values, scalar_type=True, all_scalars=True)
146-
if dpnp.isscalar(values):
147-
values = dpnp.asarray(
148-
values, sycl_queue=a.sycl_queue, usm_type=a.usm_type
149-
)
150-
151-
if values.ndim == 0:
152-
shape = list(a.shape)
153-
shape[axis] = 1
154-
values = dpnp.broadcast_to(values, tuple(shape))
155-
combined.append(values)
156-
157-
158133
def _get_reduction_res_dt(a, dtype, _out):
159134
"""Get a data type used by dpctl for result array in reduction function."""
160135

@@ -1206,39 +1181,14 @@ def diff(a, n=1, axis=-1, prepend=None, append=None):
12061181
12071182
"""
12081183

1209-
dpnp.check_supported_arrays_type(a)
1210-
if n == 0:
1211-
return a
1212-
if n < 0:
1213-
raise ValueError(f"order must be non-negative but got {n}")
1214-
1215-
nd = a.ndim
1216-
if nd == 0:
1217-
raise ValueError("diff requires input that is at least one dimensional")
1218-
axis = normalize_axis_index(axis, nd)
1219-
1220-
combined = []
1221-
if prepend is not None:
1222-
_append_to_diff_array(a, axis, combined, prepend)
1223-
1224-
combined.append(a)
1225-
if append is not None:
1226-
_append_to_diff_array(a, axis, combined, append)
1227-
1228-
if len(combined) > 1:
1229-
a = dpnp.concatenate(combined, axis=axis)
1230-
1231-
slice1 = [slice(None)] * nd
1232-
slice2 = [slice(None)] * nd
1233-
slice1[axis] = slice(1, None)
1234-
slice2[axis] = slice(None, -1)
1235-
slice1 = tuple(slice1)
1236-
slice2 = tuple(slice2)
1237-
1238-
op = dpnp.not_equal if a.dtype == numpy.bool_ else dpnp.subtract
1239-
for _ in range(n):
1240-
a = op(a[slice1], a[slice2])
1241-
return a
1184+
usm_a = dpnp.get_usm_ndarray(a)
1185+
usm_pre = (
1186+
None if prepend is None else dpnp.get_usm_ndarray_or_scalar(prepend)
1187+
)
1188+
usm_app = None if append is None else dpnp.get_usm_ndarray_or_scalar(append)
1189+
1190+
usm_res = dpt.diff(usm_a, axis=axis, n=n, prepend=usm_pre, append=usm_app)
1191+
return dpnp_array._create_from_usm_ndarray(usm_res)
12421192

12431193

12441194
_DIVIDE_DOCSTRING = """

dpnp/dpnp_iface_searching.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,5 @@ def where(condition, x=None, y=None, /, *, order="K", out=None):
391391
usm_condition = dpnp.get_usm_ndarray(condition)
392392

393393
usm_out = None if out is None else dpnp.get_usm_ndarray(out)
394-
result = dpnp_array._create_from_usm_ndarray(
395-
dpt.where(usm_condition, usm_x, usm_y, order=order, out=usm_out)
396-
)
397-
return dpnp.get_result_array(result, out)
394+
usm_res = dpt.where(usm_condition, usm_x, usm_y, order=order, out=usm_out)
395+
return dpnp.get_result_array(usm_res, out)

dpnp/dpnp_iface_statistics.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -614,13 +614,12 @@ def mean(a, /, axis=None, dtype=None, out=None, keepdims=False, *, where=True):
614614

615615
dpnp.check_limitations(where=where)
616616

617-
dpt_array = dpnp.get_usm_ndarray(a)
618-
result = dpnp_array._create_from_usm_ndarray(
619-
dpt.mean(dpt_array, axis=axis, keepdims=keepdims)
620-
)
621-
result = result.astype(dtype) if dtype is not None else result
617+
usm_a = dpnp.get_usm_ndarray(a)
618+
usm_res = dpt.mean(usm_a, axis=axis, keepdims=keepdims)
619+
if dtype is not None:
620+
usm_res = dpt.astype(usm_res, dtype)
622621

623-
return dpnp.get_result_array(result, out, casting="same_kind")
622+
return dpnp.get_result_array(usm_res, out, casting="same_kind")
624623

625624

626625
def median(x1, axis=None, out=None, overwrite_input=False, keepdims=False):
@@ -904,11 +903,9 @@ def std(
904903
)
905904
dpnp.sqrt(result, out=result)
906905
else:
907-
dpt_array = dpnp.get_usm_ndarray(a)
908-
result = dpnp_array._create_from_usm_ndarray(
909-
dpt.std(dpt_array, axis=axis, correction=ddof, keepdims=keepdims)
910-
)
911-
result = dpnp.get_result_array(result, out)
906+
usm_a = dpnp.get_usm_ndarray(a)
907+
usm_res = dpt.std(usm_a, axis=axis, correction=ddof, keepdims=keepdims)
908+
result = dpnp.get_result_array(usm_res, out)
912909

913910
if dtype is not None and out is None:
914911
result = result.astype(dtype, casting="same_kind")
@@ -1028,11 +1025,9 @@ def var(
10281025

10291026
dpnp.divide(result, cnt, out=result)
10301027
else:
1031-
dpt_array = dpnp.get_usm_ndarray(a)
1032-
result = dpnp_array._create_from_usm_ndarray(
1033-
dpt.var(dpt_array, axis=axis, correction=ddof, keepdims=keepdims)
1034-
)
1035-
result = dpnp.get_result_array(result, out)
1028+
usm_a = dpnp.get_usm_ndarray(a)
1029+
usm_res = dpt.var(usm_a, axis=axis, correction=ddof, keepdims=keepdims)
1030+
result = dpnp.get_result_array(usm_res, out)
10361031

10371032
if out is None and dtype is not None:
10381033
result = result.astype(dtype, casting="same_kind")

dpnp/dpnp_utils/dpnp_utils_reduction.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626

2727
import dpnp
28-
from dpnp.dpnp_array import dpnp_array
2928

3029
__all__ = ["dpnp_wrap_reduction_call"]
3130

@@ -53,5 +52,4 @@ def dpnp_wrap_reduction_call(
5352

5453
kwargs["out"] = usm_out
5554
res_usm = _reduction_fn(*args, **kwargs)
56-
res = dpnp_array._create_from_usm_ndarray(res_usm)
57-
return dpnp.get_result_array(res, input_out, casting="unsafe")
55+
return dpnp.get_result_array(res_usm, input_out, casting="unsafe")

0 commit comments

Comments
 (0)