With Torch backend `sparse_categorical_crossentropy` silently accepts out-of-range class indices on MPS
Author: samudraneel05Created Sep 16, 2026Updated Sep 17, 2026
Labelstype:Bugbackend:torch
sparse_categorical_crossentropy returns a near-zero loss for out-of-range class indices on MPS instead of raising
Found this while investigating MPS workings with Torch and other backends while debugging some issues like #23660.
On the Torch backend with the MPS device, keras.ops.sparse_categorical_crossentropy silently returns a near-zero loss when a target class index is out of range (≥ num_classes or negative). CPU on the other hand raises IndexError: Target N is out of bounds.
Reproduction:
import os
os.environ["KERAS_BACKEND"] = "torch"
import keras
import torch
from keras.src import ops as knn
output = torch.tensor(
[[0.9, 0.05, 0.05], [0.1, 0.8, 0.1]], dtype=torch.float32
)
# 3 classes; index 5 and index -1 are both out of range
target = torch.tensor([0, 5])
with keras.device("cpu"):
try:
knn.sparse_categorical_crossentropy(target, output)
except IndexError as e:
print("CPU:", e) # IndexError: Target 5 is out of bounds.
with keras.device("mps"):
print("MPS:", knn.sparse_categorical_crossentropy(target, output))
# tensor([0.1054, -0.0000], device='mps:0') - no error, garbage loss
Environment Keras: 3.16.0 (master @ 32530b6ed) PyTorch: 2.13.0 Python: 3.11.7 macOS 14.8.8, Apple M2
Source: keras-team/keras