Skip to content

Propagate NaN in distributed all_max and all_min - #4963

Merged
zcbenz merged 5 commits into
ml-explore:mainfrom
Adish90k:fix/4072-distributed-reduce-nan
Nov 28, 2026
Merged

zcbenz merged 5 commits into
ml-explore:mainfrom
Adish90k:fix/4072-distributed-reduce-nan

Conversation

@Adish90k

Copy link
Copy Markdown
Contributor

Title: all_max/all_min now keep NaN

Problem:with a NaN in the input, all_max/all_min returned a normal number instead of NaN, while mx.max/mx.min kept the NaN

Cause — std::max/std::min compare with <, NaN comparisons are always false, so NaN gets dropped.

Fix: the files (mlx/distributed/reduction_ops.h and the jaccl reduction_ops.h) added a NaN-aware check before the comparison, covering float32/float16/bfloat16.

Testing : test_all_reduce_nan and ran it with a 2-process launch; NaN now survives.

Closes #4072

@nastya236

Copy link
Copy Markdown
Collaborator

Thank you! Some tests are failing, could you have a look please?

@Adish90k

Copy link
Copy Markdown
Contributor Author

Why it was failing: the MPI backend's max/min didn't propagate NaN.

Fixed so far: NaN propagation in the ring and jaccl backends, and in MPI's custom ops (float16 / bfloat16 / complex64).

Still open: MPI float32 uses OpenMPI's built-in MPI_MAX/MPI_MIN, which don't propagate NaN and aren't MLX code.

Making float32 propagate NaN under MPI would need a custom float32 op,trading the built-in's performance for correctness.

Question: would you prefer I add a custom float32 op for this, or scope the test to exclude the built-in float32 MPI path?

@Adish90k

Copy link
Copy Markdown
Contributor Author

The latest commit scopes test_all_reduce_nan to float16/bfloat16 since float32 under MPI uses OpenMPI's built-in reduction operations, which do not propagate NaNs. I can also add a custom float32 MPI reduction op if you'd prefer consistent NaN propagation, though it may introduce additional reduction overhead compared to the optimized built-in MPI operations.

@zcbenz
zcbenz force-pushed the fix/4072-distributed-reduce-nan branch from 1937431 to ddf7df8 Compare November 26, 2026 07:43
auto out = reinterpret_cast<__bf16*>(output);
for (size_t i = 0; i < N; i++) {
out[i] = (out[i] > in[i]) ? out[i] : in[i];
if (__builtin_isnan(static_cast<float>(out[i])) ||

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is there a reason to use __builtin_isnan over std::nan?

You can also simplify the code by making nan_min/nan_max template function.

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.

The value is cast to float before the check, so std::isnan works the same. I've switched to it (via the nan_max/nan_min helpers) for consistency with the rest of the file.

@zcbenz
zcbenz force-pushed the fix/4072-distributed-reduce-nan branch from 63a6c2c to e9b74d1 Compare November 28, 2026 00:21
@zcbenz
zcbenz merged commit 5a5597f into ml-explore:main Nov 28, 2026
24 checks passed
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.

[BUG] mx.distributed.all_max / all_min silently drop NaN (inconsistent with mx.max/mx.min)

3 participants