Skip to content

Reuse dpnp.nan_to_num in dpnp.nansum and dpnp.nanprod #2339

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 6 commits into from
Mar 3, 2025

Conversation

ndgrigorian
Copy link
Collaborator

@ndgrigorian ndgrigorian commented Feb 25, 2025

This PR proposes the use of nan_to_num over _replace_nan in nansum, nanprod, nancumsum, and nancumprod using new internal function _replace_nan_no_mask.

  • Have you provided a meaningful PR description?
  • Have you added a test, reproducer or referred to 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?
  • If this PR is a work in progress, are you filing the PR as a draft?

Copy link
Contributor

github-actions bot commented Feb 25, 2025

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

@coveralls
Copy link
Collaborator

coveralls commented Feb 25, 2025

Coverage Status

coverage: 71.923% (+0.006%) from 71.917%
when pulling 1995cd5 on reuse-nan-to-num-nan-fns
into 96e723d on master.

Copy link
Contributor

github-actions bot commented Feb 25, 2025

Array API standard conformance tests for dpnp=0.18.0dev0=py312he4f9c94_16 ran successfully.
Passed: 1006
Failed: 0
Skipped: 16

@ndgrigorian
Copy link
Collaborator Author

This relatively simple and non-invasive change improves performance significantly. On Max GPU

before:

In [1]: import dpnp

In [2]: x = dpnp.ones(3*10**8, dtype="f4")

In [3]: q = x.sycl_queue

In [4]: %timeit r = dpnp.nansum(x); q.wait()
9.37 ms ± 33.8 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [5]: %timeit r = dpnp.nansum(x); q.wait()
9.42 ms ± 18.8 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [6]: x = dpnp.ones(10**8, dtype="f4")

In [7]: %timeit r = dpnp.nansum(x); q.wait()
4.5 ms ± 8.8 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [8]: %timeit r = dpnp.nansum(x); q.wait()
4.51 ms ± 11 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)

after:

In [1]: import dpnp

In [2]: x = dpnp.ones(3*10**8, dtype="f4")

In [3]: q = x.sycl_queue

In [4]: %timeit r = dpnp.nansum(x); q.wait()
6.5 ms ± 24.5 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [5]: %timeit r = dpnp.nansum(x); q.wait()
6.47 ms ± 35.7 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [6]: x = dpnp.ones(10**8, dtype="f4")

In [7]: %timeit r = dpnp.nansum(x); q.wait()
2.78 ms ± 14.3 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [8]: %timeit r = dpnp.nansum(x); q.wait()
2.78 ms ± 14 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)

@ndgrigorian ndgrigorian force-pushed the reuse-nan-to-num-nan-fns branch from aa48c71 to 4552fe8 Compare February 28, 2025 20:05
@ndgrigorian
Copy link
Collaborator Author

Changes to dpnp.nanargmax and dpnp.nanargmin were a wash between using nan_to_num and not. Most likely incurring synchronization is going to be the most important to performance here.

I will revert the commits changing the nanarg functions and add a warning about synchronization.

@ndgrigorian ndgrigorian force-pushed the reuse-nan-to-num-nan-fns branch 2 times, most recently from d0dad9b to f69ef28 Compare February 28, 2025 22:09
@ndgrigorian
Copy link
Collaborator Author

Screenshot 2025-02-28 193645

The warning rendered quite nicely. I recommend we implement such a warning everywhere that a function synchronizes in some future PR.

dpctl may need to do this as well for, i.e., boolean indexing, repeat

@antonwolfy antonwolfy added this to the 0.18.0 release milestone Mar 2, 2025
@ndgrigorian ndgrigorian force-pushed the reuse-nan-to-num-nan-fns branch from 8d78920 to 1995cd5 Compare March 2, 2025 19:07
Copy link
Contributor

@antonwolfy antonwolfy left a comment

Choose a reason for hiding this comment

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

Thank you @ndgrigorian, LGTM!

@ndgrigorian ndgrigorian merged commit 14274d8 into master Mar 3, 2025
66 of 68 checks passed
@ndgrigorian ndgrigorian deleted the reuse-nan-to-num-nan-fns branch March 3, 2025 18:37
github-actions bot added a commit that referenced this pull request Mar 4, 2025
Reuse `dpnp.nan_to_num` in `dpnp.nansum` and `dpnp.nanprod` 14274d8
antonwolfy added a commit that referenced this pull request Mar 25, 2025
@antonwolfy antonwolfy mentioned this pull request Mar 25, 2025
7 tasks
github-actions bot added a commit that referenced this pull request Mar 25, 2025
github-actions bot added a commit that referenced this pull request Mar 25, 2025
github-actions bot added a commit that referenced this pull request Mar 25, 2025
antonwolfy added a commit that referenced this pull request Mar 25, 2025
The PR populates the changelog with gh-2339 changes.
github-actions bot added a commit that referenced this pull request Mar 25, 2025
The PR populates the changelog with gh-2339 changes. e0b7932
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.

4 participants