As I said, you are not allowed to modify the grad inplace. That will make what the hooks (and potentially other function) see. So this is expected that you get wrong gradients if you modify the grad inplace in the backward function !