#198471·pytorch

[MPS] `nll_loss` / `cross_entropy` silently return 0 for rows whose target element lies at flat index 2**31 or beyond

Author: jniimiCreated Sep 24, 2026Updated Sep 25, 2026
Labelsmodule: losstriagedmodule: 64-bithas workaroundmodule: mpsbot-triaged

Describe the bug

On MPS, F.nll_loss(input, target) with an input of shape (N, C) silently returns a wrong per-sample loss for every row i whose target element lies at flat index i * C + target[i] >= 2**31 of the input. Almost all of these losses are exactly 0. Every other row is correct, and there is no error or warning. F.cross_entropy goes through nll_loss after log_softmax and is affected in the same way, with reduction="none", "sum" and "mean". log_softmax itself is correct at these sizes, and -F.log_softmax(x, -1).gather(1, t[:, None]) gives the correct values.

The boundary is on the flat index of the target element, not on the row. With C = 250 and N = 2**23 + 2**20, row 8589934 contains element 231 (at column 148). Its target is 43 (flat index 2147483543) and its loss is correct, while row 8589935 (target 158, flat index 2147483908) returns 0. Of the 9,437,184 rows, the 847,249 wrong rows are exactly those whose target element lies at flat index 231 or later.

This happens below 232 elements, so it is reached in ordinary training with a large vocabulary: with 128,256 classes, the logits of 16,744 or more tokens (for example 16 sequences of 2,048 tokens) exceed 231 elements. With reduction="mean", the loss is then silently too small by the share of affected rows. In our training-step runs with cross_entropy on 232 elements, the gradient with respect to the input was correct; only the loss value was wrong (about half of the reference). With 232 elements some of the wrong values are also non-zero garbage, and with inputs uniform on [-4, 4) the sum became NaN.

The same rows are wrong on 2.4.1, 2.9.1, 2.13.0 and 2.14.0, so this is not a recent regression.

Minimal reproduction

Needs about 35 GB of unified memory and runs in about 10 seconds.

python
import torch
import torch.nn.functional as F

torch.manual_seed(0)
N, C = 2**23 + 2**16, 256                  # input has 2**31 + 2**24 elements
x = torch.rand(N, C) * 8 - 4
t = torch.randint(0, C, (N,))
xm, tm = x.to("mps"), t.to("mps")

loss = F.cross_entropy(xm, tm, reduction="none").cpu()
ref = F.cross_entropy(x, t, reduction="none")          # CPU
bad = (loss - ref).abs() > 1e-4
beyond = torch.arange(N) * C + t >= 2**31              # flat index of the target element
print("wrong rows:", bad.sum().item(), "| rows whose target lies at flat index >= 2**31:", beyond.sum().item(),
      "| same rows:", torch.equal(bad, beyond), "| wrong values that are 0:", (loss[bad] == 0).sum().item())
print("sum on MPS:", F.cross_entropy(xm, tm, reduction="sum").item(), "| CPU:", ref.double().sum().item())
print("-log_softmax(x).gather(...).sum() on MPS:", (-F.log_softmax(xm, -1).gather(1, tm[:, None])).sum().item())

Output on PyTorch 2.14.0, macOS 27.0 (26A428), MacBook Pro M2 Max 96 GB:

wrong rows: 65536 | rows whose target lies at flat index >= 2**31: 65536 | same rows: True | wrong values that are 0: 65536
sum on MPS: 62565556.0 | CPU: 63054878.21647382
-log_softmax(x).gather(...).sum() on MPS: 63054876.0

CUDA is correct with the same code

The same script, with only the device string changed from "mps" to "cuda", is correct on an NVIDIA A100-SXM4-80GB (PyTorch 2.14.0+cu130, CUDA 13.0, driver 580.82.07):

wrong rows: 0 | rows whose target lies at flat index >= 2**31: 65536 | same rows: False | wrong values that are 0: 0
sum on CUDA: 63054880.0 | CPU: 63054878.216116905
-log_softmax(x).gather(...).sum() on CUDA: 63054880.0

(same rows: False only because no row is wrong.) The C = 250 input described above has no wrong row on CUDA either, and with exactly 2**32 elements (N = 2**24, C = 256), cross_entropy with every reduction and nll_loss match the reference on CUDA, where MPS returns NaN for the sum.

Workaround

Compute the loss as -F.log_softmax(x, -1).gather(1, t[:, None]) (followed by the desired reduction), or split the rows so that each call to nll_loss / cross_entropy sees fewer than 2**31 input elements.

Versions

PyTorch version: 2.14.0 (same result on 2.4.1, 2.9.1 and 2.13.0)
OS: macOS 27.0 (26A428, arm64)
Python version: 3.12.11
CPU: Apple M2 Max (96 GB unified memory); the 2**32-element training-step runs on Apple M2 Ultra (192 GB)
dtype: float32

cc @kulinseth @malfet @DenisVieriu97 @jhavukainen @aditvenk @Isalia20