I can’t seem to figure out how to remove the hooks without the handles, despite being able to detect the hooks on the target module.
from typing import Tuple
import torch

model = torch.nn.Sequential(*[torch.nn.Identity()] * 4)

def forward_hook(module: torch.nn.Module, input: Tuple[torch.Tensor], output: torch.Tensor) -> None:
    pass

def find_and_remove_hooks(m):
    print("Hooks")
    c = 0
    module_name = type(m).__name__
    print(m._forward_hooks)
    for k, v in m._forward_hooks.items():
        c+=1
        m._forward_hooks[v].remove() # Doesn't work
    print("All " + str(c) + " hooks found")

model[1].register_forward_hook(forward_hook)

find_and_remove_hooks(model[1])