Skip to content

Implement kernels for median and paritition functions - #3138

Open
ndgrigorian wants to merge 12 commits into
masterfrom
feature/implement-median-partition-fns
Open

ndgrigorian wants to merge 12 commits into
masterfrom
feature/implement-median-partition-fns

Conversation

@ndgrigorian

@ndgrigorian ndgrigorian commented Oct 31, 2026 •

Copy link
Copy Markdown
Collaborator

This PR proposes the addition of updates to radix selection algorithms to generalize for cases of median and partition related functions, which allows the removal of dpnp_partition_c from the DPNP backend

The median and partition functions can pass through either a sorting or a radix selection path, based on input types (and in the case of partition, always when kth is multiple elements)

Supersedes #2423

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

@github-actions

Copy link
Copy Markdown
Contributor

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

@github-actions

github-actions Bot commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Array API standard conformance tests for dpnp=0.22.0dev0=np2py314h8d9cdd5_39 ran successfully.
Passed: 1491
Failed: 0
Skipped: 6

@ndgrigorian
ndgrigorian force-pushed the feature/implement-median-partition-fns branch 2 times, most recently from 0f57805 to 8667696 Compare November 2, 2026 06:38
@ndgrigorian
ndgrigorian force-pushed the feature/refactor-dpnp-backend-to-libtensor branch from e24ca7b to 2df1e76 Compare November 2, 2026 15:06
@ndgrigorian
ndgrigorian force-pushed the feature/implement-median-partition-fns branch from 8667696 to 58ce933 Compare November 2, 2026 15:07
@ndgrigorian
ndgrigorian marked this pull request as ready for review November 2, 2026 16:00
@coveralls

coveralls commented Nov 2, 2026 •

Copy link
Copy Markdown
Collaborator

Coverage Status

coverage: 84.51% (+0.1%) from 84.396% — feature/implement-median-partition-fns into master

@ndgrigorian
ndgrigorian force-pushed the feature/refactor-dpnp-backend-to-libtensor branch 2 times, most recently from 236c2e1 to 43ca63d Compare November 2, 2026 21:41
@ndgrigorian
ndgrigorian force-pushed the feature/implement-median-partition-fns branch from 58ce933 to 89cc1d4 Compare November 2, 2026 22:19
Base automatically changed from feature/refactor-dpnp-backend-to-libtensor to master November 3, 2026 16:31
@ndgrigorian
ndgrigorian force-pushed the feature/implement-median-partition-fns branch 2 times, most recently from 74de517 to c7864b9 Compare November 3, 2026 22:08
@ndgrigorian ndgrigorian mentioned this pull request Nov 4, 2026
3 of 7 tasks
partition.cpp is a dpnp source, built with the coverage flags, but
gen_coverage.py treated _tensor_sorting_impl as libtensor-only and left
it out of the default report. List it first in the module like the other
dpnp sources, so the linker keeps its instrumented copies of shared
inline functions.
The device coverage counters of partition.cpp and median.cpp reach the
sort and top_k kernels through the shared inline helpers, about 500
device globals per kernel image. Their init events make the OpenCL CPU
runtime use a freed command, so the coverage tests segfaulted at random
in top_k, sort and unique_all.
With n=1 the signed-zero row was all NaN, so NumPy and dpnp warned.
test_all_nan_rows covers that case.
out[...] = res broadcast the medians into an out of the wrong shape,
e.g. (2, 3) or (1, 3) for an axis=0 median of a (4, 3) array, where the
sort path raised ValueError. The shape is checked against the result
with keepdims applied, as NumPy accepts such an out. NumPy's nanmedian
broadcasts too, but dpnp keeps raising as before and as documented.
Both returned on an empty batch before checking kth, the data types,
contiguity and overlap, so a call that raised for a non-empty array
passed for an empty one. _median also left the all_nan flag
uninitialized. The early returns now come after the checks, and
_median zeroes the flag first. _median also checks that src does not
overlap all_nan.
With keepdims=True the sort path passed out of the keepdims shape to
dpnp.mean and _calc_nanmedian, which expect the shape without the
reduced axes and raised ValueError, while NumPy accepts it. out is now
checked against the keepdims shape once, and both paths write the
medians to a view of it with the reduced axes indexed away, which never
copies. The select path no longer needs the result shape.
partition.cpp and median.cpp are dpnp sources, built with the default
fast floating-point model of icpx, which keeps -fno-signed-zeros. The
compiler could fold `val == 0 ? 0 : val` to `val`, so -0.0 kept its own
key, and the median of zeros came back as -0.0, where NumPy's mean
gives +0.0. Mapping the key of -0.0 to that of +0.0 with integer
compares holds under any floating-point flags.
dpt.full submits its fill asynchronously, so without depends the
extension's clearing fill could run first and leave the initial 7.
@ndgrigorian
ndgrigorian force-pushed the feature/implement-median-partition-fns branch from 01da8d2 to 9d6de57 Compare November 4, 2026 19:59
Every kernel image of a module carries all its device coverage counters,
so the median kernels still got the 768 counters of the bincount,
histogram and sliding window sources after median.cpp became host only.
Their device global init events crash the OpenCL CPU runtime in the
kernel bundle launches of the radix select, as in nanmedian. The device
counts never reach the profile, so the reported coverage is unchanged.

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.

2 participants