Is there any way I can get the scalar loss to backprop through the model even when the loss is calculated with tensors not associated with the model? Custom autograd function? Currently I am getting None gradients for the model.
im1 = torch.tensor(..., requires_grad=True) # image 
im2 = torch.tensor(..., requires_grad=True) # image 2
im3 = torch.tensor(..., requires_grad=True) # image 3

y = model(im1)

# do ops on im2 based on y that requires breaking grad for y

loss = criterion(im2, im3).mean() # scalar
loss.backward()

params = list(model.parameters())[0]                                       
print(params.grad)  # None

If I multiply the loss as follows then I do get gradients for the model:
loss = criterion(im2, im3).mean() * y.mean() / y.mean()

Is multiplying like this a valid way to get the gradients to flow through the model?