Fix sort NaN handling for float16 and bfloat16#3269
Merged
angeloskath merged 1 commit intoml-explore:mainfrom Mar 19, 2026
Merged
Fix sort NaN handling for float16 and bfloat16#3269angeloskath merged 1 commit intoml-explore:mainfrom
angeloskath merged 1 commit intoml-explore:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Improves NaN-aware sorting/argsorting across CPU and CUDA backends, particularly for fp16/bf16 types, and extends the Python test suite to cover these dtypes.
Changes:
- Extend NaN-aware
sorttesting tofloat32,float16, andbfloat16, and add a newargsortNaN test for the same dtypes. - Update CUDA sort comparator initialization and comparisons to treat CUDA half/bfloat16 types as floating for NaN handling.
- Update CPU sort/argsort/argpartition NaN handling to apply to
float16_tandbfloat16_tvia a shared floating-type trait.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| python/tests/test_ops.py | Adds fp16/bf16 coverage for NaN behavior in sort and introduces a new argsort NaN test. |
| mlx/backend/cuda/sort.cu | Switches NaN handling gates from std::is_floating_point to project is_floating_v to include half/bfloat16. |
| mlx/backend/cuda/kernel_utils.cuh | Expands is_floating_v to include CUDA __half and __nv_bfloat16. |
| mlx/backend/cpu/sort.cpp | Introduces is_floating_v to include fp16/bf16 and applies it to NaN-aware sort/argsort/argpartition logic. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
zcbenz
approved these changes
Mar 19, 2026
Collaborator
zcbenz
left a comment
There was a problem hiding this comment.
Looks good to me, thanks!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
mx.sortandmx.argsortproduce incorrect results for float16 and bfloat16 arrays containing NaN values on both CUDA and CPU (x86) backends.Root cause
The NaN-aware comparator and init value in sort are guarded by
std::is_floating_point_v<T>, which returnsfalsefor__half/__nv_bfloat16(CUDA) and_MLX_Float16/_MLX_BFloat16(x86 CPU), so NaN handling is skipped.Fix
std::is_floating_point_v<T>withis_floating_v<T>, and extend the trait to cover__half/__nv_bfloat16.is_floating_v<T>trait coveringfloat16_t/bfloat16_tinsort.cpp.Tests
Add
test_sort_nanandtest_argsort_nancoverage for float16/bfloat16.Checklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes