Register Hook on internal variable

Hi all,

Is there a better way to register a hook on an internal variable instead of actually register the hook at each forward step ?

The context is the following:
At each forward step, i want to keep the gradient of some internal variables in a buffer so I use hooks. The problem is I want to apply the register hook once and not each time that I use the internal variables.

Is there a simple way to achieve that ?

Thanks for your time,

The problem appears: (L40-43)

if you control the internal variable in your own code, wouldn’t you have your own custom register hook function that’ll register it for the internal variable? (maybe on every forward, automatically…).

I think it is what I used in my code (using a hook at each forward pass), but if I understand correctly there is no overhead calling a hook for one forward / backward each forward pass.