Ho it happens during the backward. You can set torch.autograd.set_detect_anomaly(True) doc 1 at the beginning of your script to get a second stack trace pointing to the forward op that generated this error during the backward.