Call ContrastiveLoss will see a warning message:
To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
Simple code to reproduce this issue:
from monai.losses import ContrastiveLoss
import torch
inp = torch.randn([2, 10])
target = torch.randn([2, 10])
loss = ContrastiveLoss(batch_size=2)
loss(inp, target)