Hi,
EDIT: The gt/lt use below is not accurate. Also this is pseudo code so there might be typos 
The “right” way to do this would be to actually write down the loss you want:
linear1 = nn.Linear(in_features=input_dimension, out_features=1)
linear2 = nn.Linear(in_features=input_dimension, out_features=1)
y = linear1(x) + linear2(x)

mask_pos = y.gt(0)
loss_lin1 = torch.mean((y[mask_pos] - y_target[mask_pos]) ** 2)

mask_neg = y.lt(0)
loss_lin2 = torch.mean((y[mask_neg] - y_target[mask_neg]) ** 2)

linear1.zero_grad()
loss_lin1.backward(retain_graph=True)
lin1_opt.step()

linear2.zero_grad()
loss_lin2.backward()
lin2_opt.step()

Another version that only does one backward (compute loss that only involve one of the layers):
linear1 = nn.Linear(in_features=input_dimension, out_features=1)
linear2 = nn.Linear(in_features=input_dimension, out_features=1)

y_lin1 = linear1(x) + linear2(x).detach()
mask_pos = y_lin1.gt(0)
loss_lin1 = torch.mean((y_lin1[mask_pos] - y_target[mask_pos]) ** 2)

y_lin2 = linear1(x).detach() + linear2(x)
mask_neg = y_lin2.lt(0)
loss_lin2 = torch.mean((y_lin2[mask_neg] - y_target[mask_neg]) ** 2)

loss = loss_lin1 + loss_lin2

opt.zero_grad()
loss.backward()
opt.step()

The more dangerous version with hooks would be like:
linear1 = nn.Linear(in_features=input_dimension, out_features=1)
linear2 = nn.Linear(in_features=input_dimension, out_features=1)
out_lin1 = linear1(x)
out_lin2 = linear2(x)
y = out_lin1 + out_lin2
loss = torch.mean((y - y_target) ** 2)

def lin1_hook(grad):
  return grad * y.lt(0).float()
def lin2_hook(grad):
  return grad * y.gt(0).float()

out_lin1.register_hook(lin1_hook)
out_lin2.register_hook(lin2_hook)

opt.zero_grad()
loss.backward()
opt.step()

Which one of these you want will depends on your actual more complex use case.
The first one is going to be the clearer event though it might not be the fastests.
The last time will be the most efficient but you need to be careful whenever you change stuff as you’re cheating the autograd engine not to compute the “real” gradients associated with the loss you computed.