.backward() is called in line 173 of that file whenever the model is not in evaluation mode, ie it’s training. If .backward() is called, outputs.grad will be non-None as gradient is backpropogated to the variable. If the model is in evaluation mode, then .backward() is not called and None is returned (in this case outputs.grad would be None as well).