Skip to content

Update dpnp.isclose with scalar-specific SYCL kernels #2540

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

Open
wants to merge 36 commits into
base: master
Choose a base branch
from

Conversation

vlad-perevezentsev
Copy link
Collaborator

This PR suggests updating dpnp.isclose() function adding a scalar-specific SYCL kernels for both contiguous and stride cases to improve performance when rtol and atol are scalars.
Also extends and updates tests for dpnp.isclose()

The new kernel improves performance by up to 10x compared to the previous implementation when rtol and atol are scalars (tested on PVC).

CPU results:
image

GPU results:
image

  • Have you provided a meaningful PR description?
  • Have you added a test, reproducer or referred to an issue with a reproducer?
  • Have you tested your changes locally for CPU and GPU devices?
  • Have you made sure that new changes do not introduce compiler warnings?
  • Have you checked performance impact of proposed changes?
  • Have you added documentation for your changes, if necessary?
  • Have you added your changes to the changelog?

Copy link
Contributor

View rendered docs @ https://intelpython.github.io/dpnp/pull/2540/index.html

Copy link
Contributor

github-actions bot commented Jul 25, 2025

Array API standard conformance tests for dpnp=0.19.0dev2=py313h509198e_40 ran successfully.
Passed: 1227
Failed: 0
Skipped: 9

@antonwolfy antonwolfy added this to the 0.19.0 release milestone Jul 29, 2025
@coveralls
Copy link
Collaborator

coveralls commented Jul 30, 2025

Coverage Status

coverage: 71.79% (-0.3%) from 72.108%
when pulling 248e066 on update_isclose
into 30918e4 on master.

try:
a, b = dpnp.broadcast_arrays(a, b)
except ValueError:
raise ValueError(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason reissuing the same exception ?
Can we just call a, b = dpnp.broadcast_arrays(a, b) without any try/except?

Comment on lines +134 to +136
out_dtype = dpnp.bool
output = dpnp.empty(
a.shape, dtype=out_dtype, sycl_queue=exec_q, usm_type=usm_type
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
out_dtype = dpnp.bool
output = dpnp.empty(
a.shape, dtype=out_dtype, sycl_queue=exec_q, usm_type=usm_type
output = dpnp.empty(
a.shape, dtype=dpnp.bool, sycl_queue=exec_q, usm_type=usm_type

b.get_array(),
rtol,
atol,
equal_nan,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems there is no check above that equal_nan is bool scalar. Should we add that?

@@ -870,6 +940,10 @@ def isclose(a, b, rtol=1e-05, atol=1e-08, equal_nan=False):
rtol, atol, scalar_type=True, all_scalars=True
)

# Use own SYCL kernel for scalar rtol/atol
if dpnp.isscalar(rtol) and dpnp.isscalar(atol):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in above docstrings there is missing mentioning that equal_nan is optional

"atol", [1e-8, dpnp.array(1e-8), dpnp.full((10,), 1e-8)]
)
def test_isclose(self, dtype, rtol, atol):
a = numpy.random.rand(10)
Copy link
Contributor

@antonwolfy antonwolfy Aug 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better to use generate_random_numpy_array which has seeding?

a = numpy.random.rand(10)
b = a + numpy.random.rand(10) * 1e-8
class TestIsClose:
@pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True))
Copy link
Contributor

@antonwolfy antonwolfy Aug 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to exclude None from list of dtypes?

assert_allclose(dpnp_res, np_res)
np_res = numpy.isclose(a, b, rtol=1e-5, atol=1e-8)
dpnp_res = dpnp.isclose(dpnp_a, dpnp_b, rtol=rtol, atol=atol)
assert_allclose(dpnp_res, np_res)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the result is always a bool array:

Suggested change
assert_allclose(dpnp_res, np_res)
assert_equal(dpnp_res, np_res)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the same below

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants