Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion .github/workflows/conda-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### 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

Expand Down
10 changes: 8 additions & 2 deletions dpnp/linalg/dpnp_utils_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
73 changes: 33 additions & 40 deletions dpnp/tests/test_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
get_integer_float_dtypes,
has_support_aspect64,
is_cpu_device,
is_cuda_device,
numpy_version,
requires_intel_mkl_version,
)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand Down
6 changes: 4 additions & 2 deletions dpnp/tests/test_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"])
Expand Down Expand Up @@ -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",
Expand Down
5 changes: 4 additions & 1 deletion dpnp/tests/testing/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"]}))
Expand Down
2 changes: 2 additions & 0 deletions dpnp/tests/third_party/cupy/math_tests/test_matmul.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,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
Expand Down
Loading