Hi @soulitzer,
I tried what you suggested with applying torch.autograd.grad, however, it throws an error as the grad_output doesn’t have a grad_fn.



 soulitzer:

Have you tried registering a full backward hook on a module in which your hook function calls autograd.grad(grad_wrt_this_output, (other_output,))?


I did do this outside of the hook. So,
  def _hessian_hook_check(self):
    all_grad_outputs = self._get_all_grad_outputs() #grabs all grad_outputs

    d1=all_grad_outputs.copy()
    d2=all_grad_outputs.copy()

    for keyi in d1:
      for keyj in d2:
        d2L_dsi_dsj, = torch.autograd.grad(d2[keyi], d2[keyj])
        print("d2L_dsi_dsj: ",d2L_dsi_dsj)

with the following error,
Traceback (most recent call last):
  File "main.py", line 169, in <module>
    optim.step(loss=energy_loss)    
  File "~/.local/lib/python3.8/site-packages/torch/optim/optimizer.py", line 88, in wrapper
    return func(*args, **kwargs)
  File "~/.local/lib/python3.8/site-packages/torch/autograd/grad_mode.py", line 28, in decorate_context
    return func(*args, **kwargs)
  File "~/main.py", line 590, in step
    self._hessian_hook_check()
  File "~/main.py", line 601, in _hessian_hook_check
    d2L_dsi_dsj, = torch.autograd.grad(d2[keyi], d2[keyj])
  File "~/.local/lib/python3.8/site-packages/torch/autograd/__init__.py", line 234, in grad
    return Variable._execution_engine.run_backward(
RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn




 soulitzer:

Have you tried registering a full backward hook on a module in which your hook function calls autograd.grad(grad_wrt_this_output, (other_output,))?


Now I assume this is what you mean here? That I should call the calculation of derivative of grad_output for a given module with respect to the other modules within the backward hook itself, rather than calling within a different method? But then how exactly could I compute the gradient of the grad_output of the final layer with respect to the grad_output of 2nd to last layer if it hasn’t been created yet? (Unless it’s equal to 0 by some definition). As I’d be doing this calculation within the hook of the final layer before I’ve called the hook of the 2nd to last layer?
Thank you!