torch.eq will break the computation graph and the output will not have a valid grad_fn associated with it as seen here:
x = torch.randn(1, requires_grad=True)
y = torch.randn(1)
z = torch.eq(x, y)
print(z)
> tensor([False])

Are you sure you need to calculate total using g1 and g2 or could use use g1 and g2 to somehow “index” preds and targs and calculate the paired loss function?