Skip to content

fix: propagate NaN in arg reductions - #4291

Open
atirna wants to merge 5 commits into
ml-explore:mainfrom
atirna:fix/arg-reduce-nan
Open

fix: propagate NaN in arg reductions#4291
atirna wants to merge 5 commits into
ml-explore:mainfrom
atirna:fix/arg-reduce-nan

Conversation

@atirna

@atirna atirna commented Aug 16, 2026

Copy link
Copy Markdown

Proposed changes

Fixes #4274 by making argmin and argmax select the first NaN, matching MLX min/max propagation and NumPy/PyTorch behavior. The implementation covers CPU, Metal, and CUDA reduction paths, including pairwise GPU reductions where separate partials contain NaNs.

Checklist

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes (passed on the formatting commit; see 03694f5)
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed)

Tests

  • cmake --build build-metal --target tests -j2
  • ./build-metal/tests/tests --test-case='test arg reduce NaN' (12 assertions)
  • ./build-metal/tests/tests --test-case='test arg reduce*' (188 assertions)

The full Metal suite has pre-existing linalg failures on this host (271 passed, 6 failed); there were no arg-reduce failures. CUDA is not available locally.

atirna added 2 commits August 16, 2026 10:26
Apply the repository clang-format (v21) to the CUDA arg-reduce changes so
pre-commit CI passes on the PR.
@zcbenz zcbenz added the await verification This pull request is non-trivial and requires a human expert to verify its correctness. label Aug 16, 2026
@erwinzhang7

Copy link
Copy Markdown
Contributor

Sorry about that, I checked for prs before setting up a fork CI and didn't check after. Just tested yours and it's solid. Good work!

@atirna

atirna commented Aug 16, 2026

Copy link
Copy Markdown
Author

hey @erwinzhang7 thank you! for taking time to check this pr. sorry it ended up being competing pr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

await verification This pull request is non-trivial and requires a human expert to verify its correctness.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] mx.argmax / mx.argmin skip NaN while mx.max / mx.min propagate it

3 participants