Padded sequence batches use a special target (often -100) for positions that shouldn't count. Implement cross_entropy(logits, targets, ignore_index=-100):
logits has shape (N, C), targets shape (N,)0.0Also implement cross_entropy_grad(...) with the same arguments, returning the gradient with respect to logits (zero rows for ignored positions).