You could use the inputs argument in backward to specify the gradient calculation for these parameters:
class Model(nn.Module):
    def __init__(self):
        super().__init__()
        self.first_part = nn.Linear(10, 10)
        self.second_part = nn.Linear(10, 10)

    def forward(self, input):
        x = self.first_part(input)
        y = self.second_part(x)
        z = y * 2.
        return z, y

network = Model()
image = torch.randn(1, 10)
output_1, output_2 = network(image)
loss_1 = output_1.mean()
loss_2 = output_2.mean()

# make sure gradients are empty
for name, param in network.named_parameters():
    if param.grad is not None:
        print('{}.grad.abs().sum(): {}'.format(name, param.grad.abs().sum()))


# calculate gradients for `second_part` only
loss_2.backward(inputs=list(network.second_part.parameters()), retain_graph=True)

# check grads
for name, param in network.named_parameters():
    if param.grad is not None:
        print('{}.grad.abs().sum(): {}'.format(name, param.grad.abs().sum()))

# calculate all gradients
loss_1.backward()
for name, param in network.named_parameters():
    if param.grad is not None:
        print('{}.grad.abs().sum(): {}'.format(name, param.grad.abs().sum()))