Huggingface with output_attention=true seems to run before all the pytorch hooks affect the output
So I want to make a way to make 2 sets of hooks
- the original ablation hooks
- The hooks to calculate the attention_weights
Is there a way to set the order of the pytorch hooks since I want to make sure that ablation hooks run first.
So far my ablation hooks are added by
```
hooks =
for layer_idx, head_num in reciever_heads_sorted:
print(f" Adding hook for head ({layer_idx}, {head_num})")
\# Define ablation hook for this specific head
def create_ablation_hook(target_head_num):
def ablation_hook(module, input, output):
attention_output = output\[0\]
batch_size_tensor, seq_len, hidden_dim = attention_output.shape
num_heads = module.num_attention_heads
head_dim = hidden_dim // num_heads
\# Zero out the specific head
reshaped = attention_output.view(batch_size_tensor, seq_len, num_heads, head_dim)
reshaped\[:, :, target_head_num, :\] = 0
modified = reshaped.view(batch_size_tensor, seq_len, hidden_dim)
return (modified,) + output\[1:\]
return ablation_hook
\# Register hook for this layer
attention_layer = model.model.layers\[layer_idx\].self_attn
hook = attention_layer.register_forward_hook(create_ablation_hook(head_num))
hooks.append((hook, layer_idx, head_num))
```