You could disable the gradient calculation via with torch.no_grad() as seen here:
model1 = nn.Linear(1, 1)
model2 = nn.Linear(1, 1)

input = torch.randn(1, 1)
target = torch.randn(1, 1)
criterion = nn.MSELoss()
function = criterion

output1 = model1(input)

with torch.no_grad():
    output2 = model2(input)


loss = criterion(output1, target) + function(output1,output2)
loss.backward()

for name, param in model1.named_parameters():
    print(name, param.grad)
> weight tensor([[-0.8400]])
  bias tensor([4.2485])

for name, param in model2.named_parameters():
    print(name, param.grad)
> weight None
  bias None