Skip to content

Commit 20d28e6

Browse files
authored
Backport gh-2477 (#2491)
This PR backports of #2477 from development branch to `maintenance/0.18.x`.
1 parent f28de69 commit 20d28e6

File tree

9 files changed

+66
-48
lines changed

9 files changed

+66
-48
lines changed

.github/workflows/conda-package.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ env:
2222
test-env-name: 'test'
2323
rerun-tests-on-failure: 'true'
2424
rerun-tests-max-attempts: 2
25-
rerun-tests-timeout: 35
25+
rerun-tests-timeout: 40
2626

2727
jobs:
2828
build:

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [0.18.1] - 06/DD/2025
8+
9+
### Added
10+
11+
### Changed
12+
13+
### Fixed
14+
15+
* Fixed a bug for calculating the norm (`dpnp.linalg.norm`) of empty arrays when `keepdims=True` is passed [#2477](https://github.com/IntelPython/dpnp/pull/2477)
16+
17+
718
## [0.18.0] - 06/04/2025
819

920
This release achieves 100% compliance with Python Array API specification (revision [2024.12](https://data-apis.org/array-api/2024.12/)).

dpnp/linalg/dpnp_utils_linalg.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,7 +1187,10 @@ def _norm_int_axis(x, ord, axis, keepdims):
11871187
if ord == dpnp.inf:
11881188
if x.shape[axis] == 0:
11891189
x = dpnp.moveaxis(x, axis, -1)
1190-
return dpnp.zeros_like(x, shape=x.shape[:-1])
1190+
res_shape = x.shape[:-1]
1191+
if keepdims:
1192+
res_shape += (1,)
1193+
return dpnp.zeros_like(x, shape=res_shape)
11911194
return dpnp.abs(x).max(axis=axis, keepdims=keepdims)
11921195
if ord == -dpnp.inf:
11931196
return dpnp.abs(x).min(axis=axis, keepdims=keepdims)
@@ -1226,7 +1229,10 @@ def _norm_tuple_axis(x, ord, row_axis, col_axis, keepdims):
12261229
flag = x.shape[row_axis] == 0 or x.shape[col_axis] == 0
12271230
if flag and ord in [1, 2, dpnp.inf]:
12281231
x = dpnp.moveaxis(x, axis, (-2, -1))
1229-
return dpnp.zeros_like(x, shape=x.shape[:-2])
1232+
res_shape = x.shape[:-2]
1233+
if keepdims:
1234+
res_shape += (1, 1)
1235+
return dpnp.zeros_like(x, shape=res_shape)
12301236
if row_axis == col_axis:
12311237
raise ValueError("Duplicate axes given.")
12321238
if ord == 2:

dpnp/tests/test_linalg.py

Lines changed: 33 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
get_integer_float_dtypes,
2424
has_support_aspect64,
2525
is_cpu_device,
26-
is_cuda_device,
2726
numpy_version,
2827
requires_intel_mkl_version,
2928
)
@@ -2104,11 +2103,14 @@ def test_empty(self, shape, ord, axis, keepdims):
21042103
assert_raises(ValueError, dpnp.linalg.norm, ia, **kwarg)
21052104
assert_raises(ValueError, numpy.linalg.norm, a, **kwarg)
21062105
else:
2107-
# TODO: when similar changes in numpy are available, instead
2108-
# of assert_equal with zero, we should compare with numpy
2109-
# ord in [None, 1, 2]
2110-
assert_equal(dpnp.linalg.norm(ia, **kwarg), 0.0)
2111-
assert_raises(ValueError, numpy.linalg.norm, a, **kwarg)
2106+
if numpy_version() >= "2.3.0":
2107+
result = dpnp.linalg.norm(ia, **kwarg)
2108+
expected = numpy.linalg.norm(a, **kwarg)
2109+
assert_dtype_allclose(result, expected)
2110+
else:
2111+
assert_equal(
2112+
dpnp.linalg.norm(ia, **kwarg), 0.0, strict=False
2113+
)
21122114
else:
21132115
result = dpnp.linalg.norm(ia, **kwarg)
21142116
expected = numpy.linalg.norm(a, **kwarg)
@@ -2296,49 +2298,40 @@ def test_matrix_norm(self, ord, keepdims):
22962298
expected = numpy.linalg.matrix_norm(a, ord=ord, keepdims=keepdims)
22972299
assert_dtype_allclose(result, expected)
22982300

2299-
@pytest.mark.parametrize(
2300-
"xp",
2301-
[
2302-
dpnp,
2303-
pytest.param(
2304-
numpy,
2305-
marks=pytest.mark.skipif(
2306-
numpy_version() < "2.3.0",
2307-
reason="numpy raises an error",
2308-
),
2309-
),
2310-
],
2311-
)
2301+
@testing.with_requires("numpy>=2.3")
23122302
@pytest.mark.parametrize("dtype", [dpnp.float32, dpnp.int32])
23132303
@pytest.mark.parametrize(
23142304
"shape, axis", [[(2, 0), None], [(2, 0), (0, 1)], [(0, 2), (0, 1)]]
23152305
)
23162306
@pytest.mark.parametrize("ord", [None, "fro", "nuc", 1, 2, dpnp.inf])
2317-
def test_matrix_norm_empty(self, xp, dtype, shape, axis, ord):
2318-
x = xp.zeros(shape, dtype=dtype)
2319-
sc = dtype(0.0) if dtype == dpnp.float32 else 0.0
2320-
assert_equal(xp.linalg.norm(x, axis=axis, ord=ord), sc)
2307+
@pytest.mark.parametrize("keepdims", [True, False])
2308+
def test_matrix_norm_empty(self, dtype, shape, axis, ord, keepdims):
2309+
a = numpy.zeros(shape, dtype=dtype)
2310+
ia = dpnp.array(a)
2311+
result = dpnp.linalg.norm(ia, axis=axis, ord=ord, keepdims=keepdims)
2312+
expected = numpy.linalg.norm(a, axis=axis, ord=ord, keepdims=keepdims)
2313+
assert_dtype_allclose(result, expected)
23212314

2322-
@pytest.mark.parametrize(
2323-
"xp",
2324-
[
2325-
dpnp,
2326-
pytest.param(
2327-
numpy,
2328-
marks=pytest.mark.skipif(
2329-
numpy_version() < "2.3.0",
2330-
reason="numpy raises an error",
2331-
),
2332-
),
2333-
],
2334-
)
2315+
@testing.with_requires("numpy>=2.3")
23352316
@pytest.mark.parametrize("dtype", [dpnp.float32, dpnp.int32])
23362317
@pytest.mark.parametrize("axis", [None, 0])
23372318
@pytest.mark.parametrize("ord", [None, 1, 2, dpnp.inf])
2338-
def test_vector_norm_empty(self, xp, dtype, axis, ord):
2339-
x = xp.zeros(0, dtype=dtype)
2340-
sc = dtype(0.0) if dtype == dpnp.float32 else 0.0
2341-
assert_equal(xp.linalg.vector_norm(x, axis=axis, ord=ord), sc)
2319+
@pytest.mark.parametrize("keepdims", [True, False])
2320+
def test_vector_norm_empty(self, dtype, axis, ord, keepdims):
2321+
a = numpy.zeros(0, dtype=dtype)
2322+
ia = dpnp.array(a)
2323+
result = dpnp.linalg.vector_norm(
2324+
ia, axis=axis, ord=ord, keepdims=keepdims
2325+
)
2326+
expected = numpy.linalg.vector_norm(
2327+
a, axis=axis, ord=ord, keepdims=keepdims
2328+
)
2329+
assert_dtype_allclose(result, expected)
2330+
if keepdims:
2331+
# norm and vector_norm have different paths in dpnp when keepdims=True,
2332+
# to cover both of them test with norm as well
2333+
result = dpnp.linalg.norm(ia, axis=axis, ord=ord, keepdims=keepdims)
2334+
assert_dtype_allclose(result, expected)
23422335

23432336
@testing.with_requires("numpy>=2.0")
23442337
@pytest.mark.parametrize(

dpnp/tests/test_product.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
assert_dtype_allclose,
1313
generate_random_numpy_array,
1414
get_all_dtypes,
15-
get_complex_dtypes,
16-
is_win_platform,
1715
numpy_version,
1816
)
1917
from .third_party.cupy import testing
@@ -845,6 +843,8 @@ def test_dtype_matrix(self, dt_in1, dt_in2, dt_out, shape1, shape2):
845843
assert_raises(TypeError, dpnp.matmul, ia, ib, out=iout)
846844
assert_raises(TypeError, numpy.matmul, a, b, out=out)
847845

846+
# TODO: include numpy-2.3 when numpy-issue-29164 is resolved
847+
@testing.with_requires("numpy<2.3")
848848
@pytest.mark.parametrize("dtype", _selected_dtypes)
849849
@pytest.mark.parametrize("order1", ["C", "F", "A"])
850850
@pytest.mark.parametrize("order2", ["C", "F", "A"])
@@ -882,6 +882,8 @@ def test_order(self, dtype, order1, order2, order, shape1, shape2):
882882
assert result.flags.f_contiguous == expected.flags.f_contiguous
883883
assert_dtype_allclose(result, expected)
884884

885+
# TODO: include numpy-2.3 when numpy-issue-29164 is resolved
886+
@testing.with_requires("numpy<2.3")
885887
@pytest.mark.parametrize("dtype", _selected_dtypes)
886888
@pytest.mark.parametrize(
887889
"stride",

dpnp/tests/testing/array.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ def _assert(assert_func, result, expected, *args, **kwargs):
4949
]
5050
# For numpy < 2.0, some tests will fail for dtype mismatch
5151
dev = dpctl.select_default_device()
52-
if numpy.__version__ >= "2.0.0" and dev.has_aspect_fp64:
52+
if (
53+
numpy.lib.NumpyVersion(numpy.__version__) >= "2.0.0"
54+
and dev.has_aspect_fp64
55+
):
5356
strict = kwargs.setdefault("strict", True)
5457
if flag:
5558
if strict:

dpnp/tests/third_party/cupy/manipulation_tests/test_add_remove.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,8 @@ def test_unique_inverse(self, xp, dtype, attr):
345345
@testing.numpy_cupy_array_equal()
346346
def test_unique_values(self, xp, dtype):
347347
a = testing.shaped_random((100, 100), xp, dtype)
348-
return xp.unique_values(a)
348+
out = xp.unique_values(a) # may not be sorted from NumPy 2.3.
349+
return xp.sort(out)
349350

350351

351352
@testing.parameterize(*testing.product({"trim": ["fb", "f", "b"]}))

dpnp/tests/third_party/cupy/math_tests/test_matmul.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ def test_cupy_matmul(self, xp, dtype1, dtype2):
9999
)
100100
class TestMatmulOut(unittest.TestCase):
101101

102+
# TODO: include numpy-2.3 when numpy-issue-29164 is resolved
103+
@testing.with_requires("numpy<2.3")
102104
# no_int8=True is added to avoid overflow
103105
@testing.for_all_dtypes(name="dtype1", no_int8=True)
104106
@testing.for_all_dtypes(name="dtype2", no_int8=True)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ exclude-protected = ["_create_from_usm_ndarray"]
135135

136136
[tool.pylint.design]
137137
max-args = 11
138-
max-branches = 16
138+
max-branches = 17
139139
max-locals = 30
140140
max-positional-arguments = 9
141141
max-returns = 8

0 commit comments

Comments
 (0)