Is it possible to calculate the differential of P using something like P.backward()?
I have register hook of middle layer. The final goal is to calculate the differential of \partial P/\partial middle layer.
I tried but got all 0s.

You can take use of PyTorch’s autograd to compute the differential for P - here’s a link to the documentation https://pytorch.org/docs/stable/generated/torch.autograd.grad.html

If this is a repeated operation, I would recommend you register a hook (as you say you’ve done) on the tensor which would be called every time a gradient with respect to that tensor is calculated. Then, when loss.backward() is called, it would trigger your hook and you would have your required gradient (assuming that’s what you required at the first place).

I would have to see your code for the hook and maybe the training loop to be able to pinpoint why your gradient is all 0s even after the hook. One possible explanation of that could be that your hook is never called, because again, tensor hooks are called ONLY when a gradient with respect to that tensor is computed somewhere in your computational graph. Please try logging the hook to verify if it’s actually called as a starting step.