Given a matrix of token logits, implement a custom function to calculate cross-entropy loss for Mistral AI without relying on PyTorch's built-in loss function.
Use def cross_entropy_loss(logits, targets):, where logits is a non-empty list of equal-length rows shaped [batch_size][vocabulary_size], and targets contains one valid token index per row. Return the mean negative log-likelihood as a floating-point number. Use a numerically stable calculation so large logits do not overflow.
def cross_entropy_loss(logits, targets):