Commit 70932b8
authored
Guard MLX distributed eval against zero-token batches (#888)
* Guard MLX distributed eval against zero-token batches
The baseline loss labels=None fast path divides by raw ntoks for mlx-lm
parity, so a zero-token eval batch (a distributed_pad_mode="empty" padding
row on a rank, lengths [0, 0]) yields loss = 0/0 = NaN. _evaluate_batch_totals
then did all_losses += loss * ntoks = NaN * 0 = NaN, which the distributed
all-sum spreads to every rank even when other ranks have real tokens.
Mask the zero-token contribution at the aggregation site with mx.where so the
loss function stays byte-for-byte equivalent to mlx-lm default_loss (the parity
guard test is unaffected) while distributed eval no longer poisons its all-sum.
* Use scalar 0.0 fallback in the zero-token eval guard
mx.where selects the fallback only when ntoks == 0, so a Python 0.0 is
equivalent to mx.zeros_like(all_losses) and avoids allocating a scalar
array each eval iteration.1 parent 79bbb3e commit 70932b8
1 file changed
Lines changed: 4 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1500 | 1500 | | |
1501 | 1501 | | |
1502 | 1502 | | |
1503 | | - | |
| 1503 | + | |
| 1504 | + | |
| 1505 | + | |
| 1506 | + | |
1504 | 1507 | | |
1505 | 1508 | | |
1506 | 1509 | | |
| |||
0 commit comments