Skip to content

Use dpnp.minmax where dpnp needs both the minimum and the maximum - #3214

Open
antonwolfy wants to merge 4 commits into
masterfrom
use-minmax-internally
Open

antonwolfy wants to merge 4 commits into
masterfrom
use-minmax-internally

Conversation

@antonwolfy

Copy link
Copy Markdown
Contributor

Several dpnp functions compute the minimum and the maximum of the same array with two separate reductions, and some of them then read each value on the host with its own synchronization. This PR makes them use the fused single-pass dpnp.minmax instead, as NumPy 2.6 does in its own functions.

Changes

  • dpnp.ptp: one dpnp.minmax call instead of dpnp.max and dpnp.min. With out, the maximum is still written to out first and the subtraction runs in its data type, so the result is unchanged.
  • dpnp.histogram, dpnp.histogram_bin_edges, dpnp.histogram2d and dpnp.histogramdd: the outer bin edges of an autodetected range come from dpnp.minmax.
  • dpnp.bincount, the bounds check of dpnp.insert with an array obj, and indexing of dpnp.ndarray.flat with an integer array need both values on the host. A new private helper get_minmax_scalars in dpnp_utils_statistics.py writes them into a 2-element host USM buffer and reads it in place once the reduction is done. One reduction and one wait replace two reductions and one (insert), two (flat) or four (bincount) host reads. The minmax kernel writes its results with plain stores, so a host USM output also works on devices without host USM atomics.
  • dpnp.trim_zeros with trim="fb" (the default): one dpnp.minmax along axis 0 of the nonzero positions.
  • Changelog.

The results are unchanged, and the existing tests cover every changed path. flat keeps NumPy's min and max for a key that is converted to a NumPy array, such as a Python list.

Performance

Measured on an Intel Data Center GPU Max 1100 on Level Zero and an Intel Xeon Platinum 8480+ on OpenCL, against master built from the same commit. Each function was timed end to end with queue.wait() after every call. Master and this branch ran in alternating fresh processes, 5 rounds on the GPU and 3 on the CPU (up to 1e7 elements there), and the median is reported. The speedup is the master time divided by the time of this branch; the results were checked against NumPy in every case.

Function Input Sizes GPU CPU
ptp float32 1e3 / 1e6 / 1e7 / 1e8 1.41 / 1.33 / 1.45 / 1.82 1.51 / 1.32 / 2.52 / -
ptp float64 1e3 / 1e6 / 1e7 / 1e8 1.43 / 1.34 / 1.43 / 1.83 1.40 / 1.21 / 2.83 / -
ptp int32 1e3 / 1e6 / 1e7 / 1e8 1.36 / 1.24 / 1.05 / 1.48 1.66 / 0.96 / 0.79 / -
ptp int64 1e3 / 1e6 / 1e7 / 1e8 1.37 / 1.23 / 1.01 / 1.38 1.46 / 0.89 / 0.73 / -
ptp, axis 0 / axis 1 float32, (2000, 5000) 1e7 1.78 / 1.28 1.81 / 2.70
bincount int64 in [0, 1000) 1e3 / 1e6 / 1e7 / 1e8 2.36 / 1.77 / 1.44 / 1.27 2.38 / 1.61 / 1.22 / -
flat[idx] float32 of 1e7, int64 idx 10 / 1e3 / 1e6 1.38 / 1.38 / 1.31 1.48 / 1.55 / 1.03
insert float32 of 1e6, int64 obj 10 / 1e3 / 1e5 1.10 / 1.12 / 1.07 1.06 / 1.05 / 1.08
histogram float32, 64 bins 1e3 / 1e6 / 1e7 / 1e8 1.04 / 1.02 / 1.04 / 1.14 1.03 / 1.09 / 1.29 / -
histogramdd float32, (n, 3), 16 bins 1e4 / 1e6 / 1e7 1.06 / 1.05 / 1.06 1.04 / 1.04 / 1.02
trim_zeros float32, 1-D / 2-D 1e4 / 1e6 / 1e7 / (1000, 1000) 1.09 / 1.07 / 0.99 / 1.07 1.12 / 1.00 / 1.04 / 0.99
  • ptp of int32 and int64 on the CPU is slower for 1e5 to 1e7 elements (0.73x to 0.98x). There dpnp.min and dpnp.max use their single-kernel atomic path for integers, which the fused kernel does not have, as noted in Add dpnp.minmax with a fused min/max reduction kernel #3135. ptp of floats on the CPU, and of every type on the GPU, is faster.
  • histogram, histogramdd, insert and trim_zeros spend most of their time in other work, so their gain is small, except for histogram with large inputs.
  • Reading the minimum and maximum of a 10-element int64 array on the host takes 123 us on the GPU with int(x.max()), int(x.min()) and 63 us with get_minmax_scalars; on the CPU it takes 417 us and 119 us.

Testing

test_statistics.py, test_histogram.py, test_manipulation.py, test_flat.py, test_sycl_queue.py, test_usm_type.py and the CuPy statistics and manipulation tests pass on both devices with DPNP_TEST_ALL_INT_TYPES=1: 13960 passed, as on master.

Checklist

  • 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?

`dpnp.ptp`, the autodetected range of `dpnp.histogram` and the functions built on it, `dpnp.bincount`, the bounds check of `dpnp.insert`, integer-array indexing of `dpnp.ndarray.flat` and `dpnp.trim_zeros` with `trim="fb"` computed the minimum and the maximum of the same array with two separate reductions. They now use the fused single-pass `dpnp.minmax`.

Where both values are needed on the host (`bincount`, `flat`, `insert`), the new private helper `get_minmax_scalars` writes them into a 2-element host USM buffer and reads it in place once the reduction is done, instead of copying each value with its own synchronization.
@antonwolfy antonwolfy self-assigned this Nov 30, 2026
@antonwolfy antonwolfy added this to the 0.22.0 release milestone Nov 30, 2026
@github-actions

Copy link
Copy Markdown
Contributor

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

@github-actions

github-actions Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Array API standard conformance tests for dpnp=0.22.0dev3+82.gd1c2b9be7 ran successfully.
Passed: 1491
Failed: 0
Skipped: 6

@coveralls

coveralls commented Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator

Coverage Status

coverage: 84.616% (+0.006%) from 84.61% — use-minmax-internally into master

@vlad-perevezentsev vlad-perevezentsev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I tested it on UHD 770, UHD 730, Arc B580 and two OpenCL CPUs: the tests pass, the results are the same as on master, and most callers are faster (ptp 1.4–7×, small bincount 3×). Two small comments below, neither blocks merging.

out=out,
)
# the maximum goes to `out`, so the subtraction runs in its data type
a_min, a_max = dpnp.minmax(a, axis=axis, keepdims=keepdims, out=(None, out))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

On the OpenCL CPU, ptp over axis 0 is now about 2× slower for some shapes, e.g. (16384, 1024). There minmax reduces axis 0 with the tree kernel, while max and min use atomics:

import time, numpy, dpnp

x = numpy.random.default_rng(0).standard_normal((16384, 1024), dtype=numpy.float32)
a = dpnp.asarray(x, device="cpu")
for name, f in [("ptp", lambda: dpnp.ptp(a, axis=0)),
                ("max - min", lambda: dpnp.max(a, axis=0) - dpnp.min(a, axis=0))]:
    f(); a.sycl_queue.wait()
    t = time.perf_counter()
    for _ in range(20):
        f()
    a.sycl_queue.wait()
    print(name, round((time.perf_counter() - t) / 20 * 1e3, 1), "ms")
# i7-12700K OpenCL CPU: ptp 40.3 ms, max - min 19.4 ms (master ptp 20.1 ms)

dpnp.sum(a, axis=0) has the same slow band, so the cause is the shared axis-0 kernel. Other shapes and the GPUs are faster with this PR, so a follow-up issue would do.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, confirmed: minmax has no atomic kernels, while max and min use atomics for 32- and 64-bit integer and real types, so ptp over axis 0 now always takes the tree kernel (the one sum uses for real types). I'll add the atomic dispatch to minmax in a follow-up PR.

Comment thread dpnp/dpnp_iface_statistics.py Resolved

This branch has not been deployed

No deployments
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