Hi
I’m trying to add adapters to ViT which I would love to be able to dynamically add and or remove.
I am currently able to add these layers to the graph through the hooks, and to freeze/unfreeze everything as needed, but using the handles to remove the hooks (and I expect the layers / parameters) doesn’t seem to work.
class Hooker:
def __init__(self):
self.hooks = {}
#self.hk_idx = 0
def register_hook(self, module, hook, layer_name):
handle = module.register_forward_hook(hook)
self.hooks[layer_name] = handle
#self.hooks[self.hk_idx] = handle
print(f"Hook registered for {layer_name}")
print(f"Handle name: {handle}")
self.hk_idx+=1
def remove_hook(self, layer_name):
if layer_name in self.hooks:
self.hooks[layer_name].remove()
print(f"Hook removed for {layer_name}")
# del self.hooks[layer_name]
else:
print(f"No hook found for {layer_name}")
def remove_all_hooks(self):
for layer_name, handle in self.hooks.items():
handle.remove()
print(f"Hook removed for {layer_name}")
# self.hooks = {}
When I count my parameters before and after adding the hooks, it correctly is adding the parameters, but when calling the function to remove them, they stay the same; am I missing something?