diff --git a/.github/workflows/conda-package.yml b/.github/workflows/conda-package.yml index 958e7795973a..c62cb11866e5 100644 --- a/.github/workflows/conda-package.yml +++ b/.github/workflows/conda-package.yml @@ -22,7 +22,7 @@ env: test-env-name: 'test' rerun-tests-on-failure: 'true' rerun-tests-max-attempts: 2 - rerun-tests-timeout: 35 + rerun-tests-timeout: 40 jobs: build: diff --git a/CHANGELOG.md b/CHANGELOG.md index 680e5ba27a9a..7ec0a5fe227c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,17 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.18.1] - 06/DD/2025 + +### Added + +### Changed + +### Fixed + +* 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) + + ## [0.18.0] - 06/04/2025 This release achieves 100% compliance with Python Array API specification (revision [2024.12](https://data-apis.org/array-api/2024.12/)). diff --git a/dpnp/linalg/dpnp_utils_linalg.py b/dpnp/linalg/dpnp_utils_linalg.py index 6ea4d42a1852..b694b730c97c 100644 --- a/dpnp/linalg/dpnp_utils_linalg.py +++ b/dpnp/linalg/dpnp_utils_linalg.py @@ -1187,7 +1187,10 @@ def _norm_int_axis(x, ord, axis, keepdims): if ord == dpnp.inf: if x.shape[axis] == 0: x = dpnp.moveaxis(x, axis, -1) - return dpnp.zeros_like(x, shape=x.shape[:-1]) + res_shape = x.shape[:-1] + if keepdims: + res_shape += (1,) + return dpnp.zeros_like(x, shape=res_shape) return dpnp.abs(x).max(axis=axis, keepdims=keepdims) if ord == -dpnp.inf: return dpnp.abs(x).min(axis=axis, keepdims=keepdims) @@ -1226,7 +1229,10 @@ def _norm_tuple_axis(x, ord, row_axis, col_axis, keepdims): flag = x.shape[row_axis] == 0 or x.shape[col_axis] == 0 if flag and ord in [1, 2, dpnp.inf]: x = dpnp.moveaxis(x, axis, (-2, -1)) - return dpnp.zeros_like(x, shape=x.shape[:-2]) + res_shape = x.shape[:-2] + if keepdims: + res_shape += (1, 1) + return dpnp.zeros_like(x, shape=res_shape) if row_axis == col_axis: raise ValueError("Duplicate axes given.") if ord == 2: diff --git a/dpnp/tests/test_linalg.py b/dpnp/tests/test_linalg.py index 2f567f7a9e5c..19943b6fcd6f 100644 --- a/dpnp/tests/test_linalg.py +++ b/dpnp/tests/test_linalg.py @@ -23,7 +23,6 @@ get_integer_float_dtypes, has_support_aspect64, is_cpu_device, - is_cuda_device, numpy_version, requires_intel_mkl_version, ) @@ -2104,11 +2103,14 @@ def test_empty(self, shape, ord, axis, keepdims): assert_raises(ValueError, dpnp.linalg.norm, ia, **kwarg) assert_raises(ValueError, numpy.linalg.norm, a, **kwarg) else: - # TODO: when similar changes in numpy are available, instead - # of assert_equal with zero, we should compare with numpy - # ord in [None, 1, 2] - assert_equal(dpnp.linalg.norm(ia, **kwarg), 0.0) - assert_raises(ValueError, numpy.linalg.norm, a, **kwarg) + if numpy_version() >= "2.3.0": + result = dpnp.linalg.norm(ia, **kwarg) + expected = numpy.linalg.norm(a, **kwarg) + assert_dtype_allclose(result, expected) + else: + assert_equal( + dpnp.linalg.norm(ia, **kwarg), 0.0, strict=False + ) else: result = dpnp.linalg.norm(ia, **kwarg) expected = numpy.linalg.norm(a, **kwarg) @@ -2296,49 +2298,40 @@ def test_matrix_norm(self, ord, keepdims): expected = numpy.linalg.matrix_norm(a, ord=ord, keepdims=keepdims) assert_dtype_allclose(result, expected) - @pytest.mark.parametrize( - "xp", - [ - dpnp, - pytest.param( - numpy, - marks=pytest.mark.skipif( - numpy_version() < "2.3.0", - reason="numpy raises an error", - ), - ), - ], - ) + @testing.with_requires("numpy>=2.3") @pytest.mark.parametrize("dtype", [dpnp.float32, dpnp.int32]) @pytest.mark.parametrize( "shape, axis", [[(2, 0), None], [(2, 0), (0, 1)], [(0, 2), (0, 1)]] ) @pytest.mark.parametrize("ord", [None, "fro", "nuc", 1, 2, dpnp.inf]) - def test_matrix_norm_empty(self, xp, dtype, shape, axis, ord): - x = xp.zeros(shape, dtype=dtype) - sc = dtype(0.0) if dtype == dpnp.float32 else 0.0 - assert_equal(xp.linalg.norm(x, axis=axis, ord=ord), sc) + @pytest.mark.parametrize("keepdims", [True, False]) + def test_matrix_norm_empty(self, dtype, shape, axis, ord, keepdims): + a = numpy.zeros(shape, dtype=dtype) + ia = dpnp.array(a) + result = dpnp.linalg.norm(ia, axis=axis, ord=ord, keepdims=keepdims) + expected = numpy.linalg.norm(a, axis=axis, ord=ord, keepdims=keepdims) + assert_dtype_allclose(result, expected) - @pytest.mark.parametrize( - "xp", - [ - dpnp, - pytest.param( - numpy, - marks=pytest.mark.skipif( - numpy_version() < "2.3.0", - reason="numpy raises an error", - ), - ), - ], - ) + @testing.with_requires("numpy>=2.3") @pytest.mark.parametrize("dtype", [dpnp.float32, dpnp.int32]) @pytest.mark.parametrize("axis", [None, 0]) @pytest.mark.parametrize("ord", [None, 1, 2, dpnp.inf]) - def test_vector_norm_empty(self, xp, dtype, axis, ord): - x = xp.zeros(0, dtype=dtype) - sc = dtype(0.0) if dtype == dpnp.float32 else 0.0 - assert_equal(xp.linalg.vector_norm(x, axis=axis, ord=ord), sc) + @pytest.mark.parametrize("keepdims", [True, False]) + def test_vector_norm_empty(self, dtype, axis, ord, keepdims): + a = numpy.zeros(0, dtype=dtype) + ia = dpnp.array(a) + result = dpnp.linalg.vector_norm( + ia, axis=axis, ord=ord, keepdims=keepdims + ) + expected = numpy.linalg.vector_norm( + a, axis=axis, ord=ord, keepdims=keepdims + ) + assert_dtype_allclose(result, expected) + if keepdims: + # norm and vector_norm have different paths in dpnp when keepdims=True, + # to cover both of them test with norm as well + result = dpnp.linalg.norm(ia, axis=axis, ord=ord, keepdims=keepdims) + assert_dtype_allclose(result, expected) @testing.with_requires("numpy>=2.0") @pytest.mark.parametrize( diff --git a/dpnp/tests/test_product.py b/dpnp/tests/test_product.py index 0eccd4deefc1..707257f84344 100644 --- a/dpnp/tests/test_product.py +++ b/dpnp/tests/test_product.py @@ -12,8 +12,6 @@ assert_dtype_allclose, generate_random_numpy_array, get_all_dtypes, - get_complex_dtypes, - is_win_platform, numpy_version, ) from .third_party.cupy import testing @@ -845,6 +843,8 @@ def test_dtype_matrix(self, dt_in1, dt_in2, dt_out, shape1, shape2): assert_raises(TypeError, dpnp.matmul, ia, ib, out=iout) assert_raises(TypeError, numpy.matmul, a, b, out=out) + # TODO: include numpy-2.3 when numpy-issue-29164 is resolved + @testing.with_requires("numpy<2.3") @pytest.mark.parametrize("dtype", _selected_dtypes) @pytest.mark.parametrize("order1", ["C", "F", "A"]) @pytest.mark.parametrize("order2", ["C", "F", "A"]) @@ -882,6 +882,8 @@ def test_order(self, dtype, order1, order2, order, shape1, shape2): assert result.flags.f_contiguous == expected.flags.f_contiguous assert_dtype_allclose(result, expected) + # TODO: include numpy-2.3 when numpy-issue-29164 is resolved + @testing.with_requires("numpy<2.3") @pytest.mark.parametrize("dtype", _selected_dtypes) @pytest.mark.parametrize( "stride", diff --git a/dpnp/tests/testing/array.py b/dpnp/tests/testing/array.py index 3db237387054..96bbf94e9ce5 100644 --- a/dpnp/tests/testing/array.py +++ b/dpnp/tests/testing/array.py @@ -49,7 +49,10 @@ def _assert(assert_func, result, expected, *args, **kwargs): ] # For numpy < 2.0, some tests will fail for dtype mismatch dev = dpctl.select_default_device() - if numpy.__version__ >= "2.0.0" and dev.has_aspect_fp64: + if ( + numpy.lib.NumpyVersion(numpy.__version__) >= "2.0.0" + and dev.has_aspect_fp64 + ): strict = kwargs.setdefault("strict", True) if flag: if strict: diff --git a/dpnp/tests/third_party/cupy/manipulation_tests/test_add_remove.py b/dpnp/tests/third_party/cupy/manipulation_tests/test_add_remove.py index acd3e706d352..31bbc9691889 100644 --- a/dpnp/tests/third_party/cupy/manipulation_tests/test_add_remove.py +++ b/dpnp/tests/third_party/cupy/manipulation_tests/test_add_remove.py @@ -345,7 +345,8 @@ def test_unique_inverse(self, xp, dtype, attr): @testing.numpy_cupy_array_equal() def test_unique_values(self, xp, dtype): a = testing.shaped_random((100, 100), xp, dtype) - return xp.unique_values(a) + out = xp.unique_values(a) # may not be sorted from NumPy 2.3. + return xp.sort(out) @testing.parameterize(*testing.product({"trim": ["fb", "f", "b"]})) diff --git a/dpnp/tests/third_party/cupy/math_tests/test_matmul.py b/dpnp/tests/third_party/cupy/math_tests/test_matmul.py index fd15ba33110a..138683cae325 100644 --- a/dpnp/tests/third_party/cupy/math_tests/test_matmul.py +++ b/dpnp/tests/third_party/cupy/math_tests/test_matmul.py @@ -99,6 +99,8 @@ def test_cupy_matmul(self, xp, dtype1, dtype2): ) class TestMatmulOut(unittest.TestCase): + # TODO: include numpy-2.3 when numpy-issue-29164 is resolved + @testing.with_requires("numpy<2.3") # no_int8=True is added to avoid overflow @testing.for_all_dtypes(name="dtype1", no_int8=True) @testing.for_all_dtypes(name="dtype2", no_int8=True) diff --git a/pyproject.toml b/pyproject.toml index a26b1239825b..36e7fe3ec359 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -135,7 +135,7 @@ exclude-protected = ["_create_from_usm_ndarray"] [tool.pylint.design] max-args = 11 -max-branches = 16 +max-branches = 17 max-locals = 30 max-positional-arguments = 9 max-returns = 8