Suppos I have a network with three FC layer:
class Net(nn.Module):
def __init__(self):
    super(net, self).__init__()
    self.fc1 = nn.Linear(10, 10)
    self.fc2 = nn.Linear(10, 20)
    self.net = nn.Sequential(fc1,fc2)
    self.fc3 = nn.Linear(10, 30)


And I register a forward hook for the first layer:
def hook_func(module, input, output):
    output = net.fc3(output)  # in hook, there is a linear operator
    object.feature = output  # I pass the output to a member variable of an object

net.fc1.register_forward_hook(hook_func)

Then in forward progress:
net_out = net.fc2(net.fc1(input))
loss1 = L1(input, label1)
loss2 = L2(object.feature, label2)
loss = loss1 + loss2

I want to ask is that, when I call:
loss.backward()

Can the gradient of loss2 back-propagate through the hook? Or say, is the gradient the same with:
out1 = net.fc1(input)
out2 = net.fc2(out1)
out3 = net.fc3(out1)
loss1 = L1(out2, lable1)
loss2 = L2(out3, label2)
loss = loss1 + loss2
loss.backward()

When I wrap the net with nn.DistributedDataParallel, I got an error:
RuntimeError: Expected to have finished reduction in the prior iteration before starting a new one. This error indicates that your module has parameters that were not used in producing loss. You can enable unused parameter detection by (1) passing the keyword argument `find_unused_parameters=True` to `torch.nn.parallel.DistributedDataParallel`; (2) making sure all `forward` function outputs participate in calculating loss. If you already have done the above two steps, then the distributed data parallel module wasn't able to locate the output tensors in the return value of your module's `forward` function. Please include the loss function and the structure of the return value of `forward` of your module when reporting this issue (e.g. list, dict, iterable). (prepare_for_backward at /pytorch/torch/csrc/distributed/c10d/reducer.cpp:518)

It seems that the output in hook is not counted in DDP. The error disappeared when I set find_unused_parameters=True. But I do not know whether the gradients are backpropagated correctly?