This shouldn’t be by design and I get a proper error message in the latest nightly binary:
ret = torch._C._nn.nll_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
IndexError: Target -1 is out of bounds.

using your code.
We had a bug in the 1.5.0 release (which should be fixed in 1.5.1), which ignored certain assert statements in CUDA code and thus didn’t raise any proper exceptions.
In other words: if you were passing a target with >=nb_classes, you would most likely get an illegal memory access error instead of a proper error message.
However, passing an index of -1 would probably just use the “reverse indexing” and while this is technically a valid operation, the result is of course wrong and this shouldn’t work.
0.3.0 on the other hand might have had the same issue and I don’t know when the assert statements were added to nll_loss.
@wmpauli Could this be the case for your issue? If you are using 1.5.0, I hightly recommend to update to 1.5.1 or the nightly binary.