What if one does not remove the forward_hook

Hi there,

Suppose we are using a forward hook to analyze a mid layer:

class Hook():
    def __init__(self,m):
        self.hook = m.register_forward_hook(self.hook_func)
    def hook_func(self,m,i,o):
        self.stored = o.detach().clone()
    def remove():
        self.hook.remove()

The Hook class is created for multiple cases. If I do not remove the hook after use, what negative effect will occur.

Thanks!

Looks fine to me. Its good that you detached and cloned, otherwise you would be keeping the graph or storage of the output alive.

In general having the hook registered on the module in itself won’t keep anything alive, if you were worried about that. Its what you do as a side effect in your hook, i.e., what you store that could be a problem, but that seems to be okay here.