Hooks
ablate_attn_mat_hook(module, args, kwargs, output, ablation_queries)
Hook function to ablate the tokens in the attention mask. It will set to 0 the value vector of the tokens to ablate
Source code in easyroutine/interpretability/hooks.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
|
ablate_heads_hook(module, args, kwargs, output, ablation_queries)
Hook function to ablate the heads in the attention mask. It will set to 0 the output of the heads to ablate
Source code in easyroutine/interpretability/hooks.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
|
ablate_pos_keep_self_attn_hook(module, args, kwargs, output, ablation_queries)
Hook function to ablate the tokens in the attention mask but keeping the self attn weigths. It will set to 0 the row of tokens to ablate except for the las position
Source code in easyroutine/interpretability/hooks.py
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
|
ablate_tokens_hook_flash_attn(module, args, kwargs, output, ablation_queries, num_layers=32)
same of ablate_tokens_hook but for flash attention. This apply the ablation on the values vectors instead of the attention mask
Source code in easyroutine/interpretability/hooks.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
|
attention_pattern_head(module, args, kwargs, output, layer, cache, head='all', act_on_input=False)
Hook function to extract the attention pattern of the heads. It will extract the attention pattern. As the other hooks, it will save the activations in the cache (a global variable out the scope of the function)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
-
|
b
|
the input of the hook function. It's the output of the attention pattern of the heads |
required |
-
|
s
|
the state of the hook function. It's the state of the model |
required |
-
|
layer
|
the layer of the model |
required |
-
|
head
|
the head of the model |
required |
-
|
expand_head
|
bool to expand the head dimension when extracting the values vectors and the attention pattern. If true, in the cache we will have a key for each head, like "pattern_L0H0", "pattern_L0H1", ... while if False, we will have only one key for each layer, like "pattern_L0" and the dimension of the head will be taken into account in the tensor. |
required |
Source code in easyroutine/interpretability/hooks.py
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 |
|
avg_attention_pattern_head(module, args, kwargs, output, layer, attn_pattern_current_avg, batch_idx, cache, extract_avg_value=False, act_on_input=False)
Hook function to extract the average attention pattern of the heads. It will extract the attention pattern and then average it. As the other hooks, it will save the activations in the cache (a global variable out the scope of the function)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
-
|
b
|
the input of the hook function. It's the output of the attention pattern of the heads |
required |
-
|
s
|
the state of the hook function. It's the state of the model |
required |
-
|
layer
|
the layer of the model |
required |
-
|
head
|
the head of the model |
required |
-
|
attn_pattern_current_avg
|
the current average attention pattern |
required |
Source code in easyroutine/interpretability/hooks.py
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 |
|
avg_hook(module, args, kwargs, output, cache, cache_key, last_image_idx, end_image_idx)
It save the activations of the residual stream in the cache. It will save the activations in the cache (a global variable out the scope of the function)
Source code in easyroutine/interpretability/hooks.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
|
create_dynamic_hook(pyvene_hook, **kwargs)
DEPRECATED: pyvene is not used anymore. This function is used to create a dynamic hook. It is a wrapper around the pyvene_hook function.
Source code in easyroutine/interpretability/hooks.py
38 39 40 41 42 43 44 45 46 47 48 |
|
embed_hook(module, input, output, cache, cache_key)
Hook function to extract the embeddings of the tokens. It will save the embeddings in the cache (a global variable out the scope of the function)
Source code in easyroutine/interpretability/hooks.py
51 52 53 54 55 56 57 58 59 60 |
|
projected_value_vectors_head(module, args, kwargs, output, layer, cache, num_attention_heads, num_key_value_heads, hidden_size, d_head, out_proj_weight, out_proj_bias, head='all', act_on_input=False, expand_head=True)
Hook function to extract the values vectors of the heads. It will extract the values vectors and then project them with the final W_O projection As the other hooks, it will save the activations in the cache (a global variable out the scope of the function)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
b
|
the input of the hook function. It's the output of the values vectors of the heads |
required | |
s
|
the state of the hook function. It's the state of the model |
required | |
layer
|
the layer of the model |
required | |
head
|
Union[str, int]
|
the head of the model. If "all" is passed, it will extract all the heads of the layer |
'all'
|
expand_head
|
bool
|
bool to expand the head dimension when extracting the values vectors and the attention pattern. If true, in the cache we will have a key for each head, like "value_L0H0", "value_L0H1", ... while if False, we will have only one key for each layer, like "value_L0" and the dimension of the head will be taken into account in the tensor. |
True
|
Source code in easyroutine/interpretability/hooks.py
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
|
save_resid_hook(module, args, kwargs, output, cache, cache_key, token_index)
It save the activations of the residual stream in the cache. It will save the activations in the cache (a global variable out the scope of the function)
Source code in easyroutine/interpretability/hooks.py
63 64 65 66 67 68 69 |
|
zero_ablation(tensor)
Set the attention values to zero
Source code in easyroutine/interpretability/hooks.py
88 89 90 91 92 |
|