Skip to content

Commit 1ea5551

Browse files
authored
Merge branch 'master' into impl_array_equal
2 parents 184b407 + 543605c commit 1ea5551

File tree

11 files changed

+177
-126
lines changed

11 files changed

+177
-126
lines changed

.github/workflows/conda-package.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,13 @@ jobs:
144144
run: conda build --no-test --python ${{ matrix.python }} --numpy 1.24 ${{ env.CHANNELS }} conda-recipe
145145

146146
- name: Upload artifact
147-
uses: actions/upload-artifact@0b2256b8c012f0828dc542b3febcab082c67f72b # v4.3.4
147+
uses: actions/upload-artifact@89ef406dd8d7e03cfd12d9e0a4a378f454709029 # v4.3.5
148148
with:
149149
name: ${{ env.PACKAGE_NAME }} ${{ runner.os }} Python ${{ matrix.python }}
150150
path: ${{ env.CONDA_BLD }}${{ env.PACKAGE_NAME }}-*.tar.bz2
151151

152152
- name: Upload wheels artifact
153-
uses: actions/upload-artifact@0b2256b8c012f0828dc542b3febcab082c67f72b # v4.3.4
153+
uses: actions/upload-artifact@89ef406dd8d7e03cfd12d9e0a4a378f454709029 # v4.3.5
154154
with:
155155
name: ${{ env.PACKAGE_NAME }} ${{ runner.os }} Wheels Python ${{ matrix.python }}
156156
path: ${{ env.WHEELS_OUTPUT_FOLDER }}${{ env.PACKAGE_NAME }}-*.whl

.github/workflows/openssf-scorecard.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ jobs:
6060
# Upload the results as artifacts (optional). Commenting out will disable uploads of run results in SARIF
6161
# format to the repository Actions tab.
6262
- name: "Upload artifact"
63-
uses: actions/upload-artifact@0b2256b8c012f0828dc542b3febcab082c67f72b # v4.3.4
63+
uses: actions/upload-artifact@89ef406dd8d7e03cfd12d9e0a4a378f454709029 # v4.3.5
6464
with:
6565
name: SARIF file
6666
path: results.sarif

dpnp/dpnp_iface.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ def get_result_array(a, out=None, casting="safe"):
635635
636636
Parameters
637637
----------
638-
a : {dpnp_array}
638+
a : {dpnp.ndarray, usm_ndarray}
639639
Input array.
640640
out : {dpnp.ndarray, usm_ndarray}
641641
If provided, value of `a` array will be copied into it
@@ -652,6 +652,8 @@ def get_result_array(a, out=None, casting="safe"):
652652
"""
653653

654654
if out is None:
655+
if isinstance(a, dpt.usm_ndarray):
656+
return dpnp_array._create_from_usm_ndarray(a)
655657
return a
656658

657659
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
@@ -52,7 +52,6 @@
5252
import dpnp
5353
import dpnp.dpnp_utils as utils
5454
from dpnp.dpnp_algo.dpnp_elementwise_common import DPNPBinaryFunc, DPNPUnaryFunc
55-
from dpnp.dpnp_array import dpnp_array
5655

5756
__all__ = [
5857
"all",
@@ -170,13 +169,11 @@ def all(a, /, axis=None, out=None, keepdims=False, *, where=True):
170169

171170
dpnp.check_limitations(where=where)
172171

173-
dpt_array = dpnp.get_usm_ndarray(a)
174-
result = dpnp_array._create_from_usm_ndarray(
175-
dpt.all(dpt_array, axis=axis, keepdims=keepdims)
176-
)
172+
usm_a = dpnp.get_usm_ndarray(a)
173+
usm_res = dpt.all(usm_a, axis=axis, keepdims=keepdims)
174+
177175
# TODO: temporary solution until dpt.all supports out parameter
178-
result = dpnp.get_result_array(result, out)
179-
return result
176+
return dpnp.get_result_array(usm_res, out)
180177

181178

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

337334
dpnp.check_limitations(where=where)
338335

339-
dpt_array = dpnp.get_usm_ndarray(a)
340-
result = dpnp_array._create_from_usm_ndarray(
341-
dpt.any(dpt_array, axis=axis, keepdims=keepdims)
342-
)
336+
usm_a = dpnp.get_usm_ndarray(a)
337+
usm_res = dpt.any(usm_a, axis=axis, keepdims=keepdims)
338+
343339
# TODO: temporary solution until dpt.any supports out parameter
344-
result = dpnp.get_result_array(result, out)
345-
return result
340+
return dpnp.get_result_array(usm_res, out)
346341

347342

348343
def array_equal(a1, a2, equal_nan=False):

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)