Repository navigation
Use dpnp.minmax where dpnp needs both the minimum and the maximum - #3214
antonwolfy wants to merge 4 commits into
Conversation
`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.
|
View rendered docs @ https://intelpython.github.io/dpnp/pull/3214/index.html |
|
Array API standard conformance tests for dpnp=0.22.0dev3+82.gd1c2b9be7 ran successfully. |
vlad-perevezentsev
left a comment
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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.minmaxinstead, as NumPy 2.6 does in its own functions.Changes
dpnp.ptp: onedpnp.minmaxcall instead ofdpnp.maxanddpnp.min. Without, the maximum is still written tooutfirst and the subtraction runs in its data type, so the result is unchanged.dpnp.histogram,dpnp.histogram_bin_edges,dpnp.histogram2danddpnp.histogramdd: the outer bin edges of an autodetected range come fromdpnp.minmax.dpnp.bincount, the bounds check ofdpnp.insertwith an arrayobj, and indexing ofdpnp.ndarray.flatwith an integer array need both values on the host. A new private helperget_minmax_scalarsindpnp_utils_statistics.pywrites 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. Theminmaxkernel writes its results with plain stores, so a host USM output also works on devices without host USM atomics.dpnp.trim_zeroswithtrim="fb"(the default): onedpnp.minmaxalong axis 0 of the nonzero positions.The results are unchanged, and the existing tests cover every changed path.
flatkeeps NumPy'sminandmaxfor 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.ptpptpptpptpptp, axis 0 / axis 1bincountflat[idx]idxinsertobjhistogramhistogramddtrim_zerosptpof int32 and int64 on the CPU is slower for 1e5 to 1e7 elements (0.73x to 0.98x). Theredpnp.minanddpnp.maxuse their single-kernel atomic path for integers, which the fused kernel does not have, as noted in Adddpnp.minmaxwith a fused min/max reduction kernel #3135.ptpof floats on the CPU, and of every type on the GPU, is faster.histogram,histogramdd,insertandtrim_zerosspend most of their time in other work, so their gain is small, except forhistogramwith large inputs.int(x.max()),int(x.min())and 63 us withget_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.pyand the CuPy statistics and manipulation tests pass on both devices withDPNP_TEST_ALL_INT_TYPES=1: 13960 passed, as on master.Checklist