Skip to content

Fuzz testing fixes #1596

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 16, 2023
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
6 changes: 6 additions & 0 deletions dpnp/backend/kernels/dpnp_krnl_linalg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ DPCTLSyclEventRef dpnp_cholesky_c(DPCTLSyclQueueRef q_ref,
(void)dep_event_vec_ref;

DPCTLSyclEventRef event_ref = nullptr;
if (!data_size) {
return event_ref;
}
sycl::queue q = *(reinterpret_cast<sycl::queue *>(q_ref));

sycl::event event;
Expand Down Expand Up @@ -609,6 +612,9 @@ DPCTLSyclEventRef dpnp_qr_c(DPCTLSyclQueueRef q_ref,
(void)dep_event_vec_ref;

DPCTLSyclEventRef event_ref = nullptr;
if (!size_m || !size_n) {
return event_ref;
}
sycl::queue q = *(reinterpret_cast<sycl::queue *>(q_ref));

sycl::event event;
Expand Down
10 changes: 2 additions & 8 deletions dpnp/linalg/dpnp_algo_linalg.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,9 @@ cpdef object dpnp_cond(object input, object p):
cpdef utils.dpnp_descriptor dpnp_det(utils.dpnp_descriptor input):
cdef shape_type_c input_shape = input.shape
cdef size_t n = input.shape[-1]
cdef size_t size_out = 1
cdef shape_type_c result_shape = (1,)
if input.ndim != 2:
output_shape = tuple((list(input.shape))[:-2])
for i in range(len(output_shape)):
size_out *= output_shape[i]

cdef shape_type_c result_shape = (size_out,)
if size_out > 1:
result_shape = output_shape
result_shape = tuple((list(input.shape))[:-2])

cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(input.dtype)

Expand Down
8 changes: 6 additions & 2 deletions dpnp/linalg/dpnp_iface_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@ def det(input):

x1_desc = dpnp.get_dpnp_descriptor(input, copy_when_nondefault_queue=False)
if x1_desc:
if x1_desc.shape[-1] == x1_desc.shape[-2]:
if x1_desc.ndim < 2:
pass
elif x1_desc.shape[-1] == x1_desc.shape[-2]:
result_obj = dpnp_det(x1_desc).get_pyobj()
result = dpnp.convert_single_elem_array_to_scalar(result_obj)

Expand Down Expand Up @@ -488,7 +490,9 @@ def qr(x1, mode="reduced"):

x1_desc = dpnp.get_dpnp_descriptor(x1, copy_when_nondefault_queue=False)
if x1_desc:
if mode != "reduced":
if x1_desc.ndim != 2:
pass
elif mode != "reduced":
pass
else:
result_tup = dpnp_qr(x1_desc, mode)
Expand Down
68 changes: 65 additions & 3 deletions tests/test_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,25 @@ def test_cholesky(array):
assert_array_equal(expected, result)


@pytest.mark.parametrize(
"shape",
[
(0, 0),
(3, 0, 0),
],
ids=[
"(0, 0)",
"(3, 0, 0)",
],
)
def test_cholesky_0D(shape):
a = numpy.empty(shape)
ia = inp.array(a)
result = inp.linalg.cholesky(ia)
expected = numpy.linalg.cholesky(a)
assert_array_equal(expected, result)


@pytest.mark.parametrize(
"arr",
[[[1, 0, -1], [0, 1, 0], [1, 0, 1]], [[1, 2, 3], [4, 5, 6], [7, 8, 9]]],
Expand Down Expand Up @@ -109,6 +128,20 @@ def test_det(array):
assert_allclose(expected, result)


@pytest.mark.usefixtures("allow_fall_back_on_numpy")
def test_det_empty():
a = numpy.empty((0, 0, 2, 2), dtype=numpy.float32)
ia = inp.array(a)

np_det = numpy.linalg.det(a)
dpnp_det = inp.linalg.det(ia)

assert dpnp_det.dtype == np_det.dtype
assert dpnp_det.shape == np_det.shape

assert_allclose(np_det, dpnp_det)


@pytest.mark.parametrize("type", get_all_dtypes(no_bool=True, no_complex=True))
@pytest.mark.parametrize("size", [2, 4, 8, 16, 300])
def test_eig_arange(type, size):
Expand Down Expand Up @@ -339,8 +372,8 @@ def test_norm3(array, ord, axis):
@pytest.mark.parametrize("type", get_all_dtypes(no_bool=True, no_complex=True))
@pytest.mark.parametrize(
"shape",
[(2, 2), (3, 4), (5, 3), (16, 16)],
ids=["(2,2)", "(3,4)", "(5,3)", "(16,16)"],
[(2, 2), (3, 4), (5, 3), (16, 16), (0, 0), (0, 2), (2, 0)],
ids=["(2,2)", "(3,4)", "(5,3)", "(16,16)", "(0,0)", "(0,2)", "(2,0)"],
)
@pytest.mark.parametrize(
"mode", ["complete", "reduced"], ids=["complete", "reduced"]
Expand Down Expand Up @@ -369,7 +402,7 @@ def test_qr(type, shape, mode):
# check decomposition
assert_allclose(
ia,
numpy.dot(inp.asnumpy(dpnp_q), inp.asnumpy(dpnp_r)),
inp.dot(dpnp_q, dpnp_r),
rtol=tol,
atol=tol,
)
Expand All @@ -390,6 +423,35 @@ def test_qr(type, shape, mode):
assert_allclose(dpnp_r, np_r, rtol=tol, atol=tol)


@pytest.mark.usefixtures("allow_fall_back_on_numpy")
def test_qr_not_2D():
a = numpy.arange(12, dtype=numpy.float32).reshape((3, 2, 2))
ia = inp.array(a)

np_q, np_r = numpy.linalg.qr(a)
dpnp_q, dpnp_r = inp.linalg.qr(ia)

assert dpnp_q.dtype == np_q.dtype
assert dpnp_r.dtype == np_r.dtype
assert dpnp_q.shape == np_q.shape
assert dpnp_r.shape == np_r.shape

assert_allclose(ia, inp.matmul(dpnp_q, dpnp_r))

a = numpy.empty((0, 3, 2), dtype=numpy.float32)
ia = inp.array(a)

np_q, np_r = numpy.linalg.qr(a)
dpnp_q, dpnp_r = inp.linalg.qr(ia)

assert dpnp_q.dtype == np_q.dtype
assert dpnp_r.dtype == np_r.dtype
assert dpnp_q.shape == np_q.shape
assert dpnp_r.shape == np_r.shape

assert_allclose(ia, inp.matmul(dpnp_q, dpnp_r))


@pytest.mark.parametrize("type", get_all_dtypes(no_bool=True, no_complex=True))
@pytest.mark.parametrize(
"shape",
Expand Down