Thank you for replying.
The loss.backward() is in fact called when the batch loss is not 0.
The output of .grad_fn attributes is:
>  loss <MaximumBackward object at 0x7f20ea3fb3d0>
>  query_prediction <SelectBackward object at 0x7f20ea3fb3d0>
>  positive_prediction <SelectBackward object at 0x7f20ea3fb3d0>
>  negative_prediction <SelectBackward object at 0x7f20ea3fb3d0>
>  dist_neg <RsubBackward1 object at 0x7f20ea3fb3d0>
>  dist_neg <RsubBackward1 object at 0x7f20ea3fb3d0>