Bug of register_backward_hook

Hi, during training I use Dataparallel to trian a model and I’d like to get the gradient of the input of the first layer of the model. I use register_backward_hook. But when I modified some unrelated codes, sometimes the gradient can be gotten, sometimes not. I am extremely confused.

Can you post the code where register_backward_hook is used?

Could you provide a minimal working example to demonstrate your issues? It is really difficult to help without enough context.

    def forward_hook(module, input, output):

    def backward_hook(module, grad_input, grad_output):

    # register_index = 0
    for p in model.modules():
        if isinstance(p, nn.Conv2d):

I just want to get the grad data of every convolution layer in ResNet101
There should be 208 items in the grad list but there are sometimes 207 or 206 or 205.