Graph utils
auto_circuit.utils.graph_utils
Attributes
Classes
Functions
edge_counts_util
edge_counts_util(edges: Set[Edge], test_counts: Optional[TestEdges] = None, prune_scores: Optional[PruneScores] = None, zero_edges: Optional[bool] = None, all_edges: Optional[bool] = None, true_edge_count: Optional[int] = None) -> List[int]
Calculate a set of [number of edges in the circuit] to test.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
edges |
Set[Edge]
|
The set of all edges in the model (just used to count the maximum circuit size). |
required |
test_counts |
Optional[TestEdges]
|
The method to determine the set of edge counts. If None, the
function will try to infer the best method based on the number of edges and
the |
None
|
prune_scores |
Optional[PruneScores]
|
The scores to use to determine the edge counts. Used to make a
better inference of the best set to use when |
None
|
zero_edges |
Optional[bool]
|
Whether to include |
None
|
all_edges |
Optional[bool]
|
Whether to include |
None
|
true_edge_count |
Optional[int]
|
Inserts an extra specified edge count into the list. Useful when you want to test the number of edges in the candidate circuit. |
None
|
Returns:
Type | Description |
---|---|
List[int]
|
The list of edge counts to test. |
Source code in auto_circuit/utils/graph_utils.py
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 |
|
graph_edges
graph_edges(model: Module, factorized: bool, separate_qkv: Optional[bool] = None, seq_len: Optional[int] = None) -> Tuple[Set[Node], Set[SrcNode], Set[DestNode], Dict[int | None, List[Edge]], Set[Edge], int, Optional[int]]
Get the nodes and edges of the computation graph of the model used for ablation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Module
|
The model to get the edges for. |
required |
factorized |
bool
|
Whether the model is factorized, for Edge Ablation. Otherwise, only Node Ablation is possible. |
required |
separate_qkv |
Optional[bool]
|
Whether the model has separate query, key, and value inputs. Only used for transformers. |
None
|
seq_len |
Optional[int]
|
The sequence length of the model inputs. If |
None
|
Returns:
Type | Description |
---|---|
Tuple[Set[Node], Set[SrcNode], Set[DestNode], Dict[int | None, List[Edge]], Set[Edge], int, Optional[int]]
|
Tuple containing:
|
Source code in auto_circuit/utils/graph_utils.py
make_model_patchable
make_model_patchable(model: Module, factorized: bool, src_nodes: Set[SrcNode], nodes: Set[Node], device: device, seq_len: Optional[int] = None, seq_dim: Optional[int] = None) -> Tuple[Set[PatchWrapperImpl], Set[PatchWrapperImpl], Set[PatchWrapperImpl]]
Injects PatchWrapper
s into the model at the
node positions to enable patching.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Module
|
The model to make patchable. |
required |
factorized |
bool
|
Whether the model is factorized, for Edge Ablation. Otherwise, only Node Ablation is possible. |
required |
src_nodes |
Set[SrcNode]
|
The source nodes in the model. |
required |
nodes |
Set[Node]
|
All the nodes in the model. |
required |
device |
device
|
The device to put the patch masks on. |
required |
seq_len |
Optional[int]
|
The sequence length of the model inputs. If |
None
|
seq_dim |
Optional[int]
|
The sequence dimension of the model. This is the dimension on which new
inputs are concatenated. In transformers, this is |
None
|
Returns:
Type | Description |
---|---|
Tuple[Set[PatchWrapperImpl], Set[PatchWrapperImpl], Set[PatchWrapperImpl]]
|
Tuple containing:
|
Warning
This function modifies the model in place.
Source code in auto_circuit/utils/graph_utils.py
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 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 |
|
mask_fn_mode
mask_fn_mode(model: PatchableModel, mask_fn: MaskFn, dropout_p: float = 0.0)
Context manager to enable the specified mask_fn
and dropout_p
for a patchable
model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
PatchableModel
|
The patchable model to alter. |
required |
mask_fn |
MaskFn
|
The function to apply to the mask values before they are used to interpolate between the clean and ablated activations. |
required |
dropout_p |
float
|
The dropout probability to apply to the mask values. |
0.0
|
Warning
This function modifies the state of the model! This is a likely source of bugs.
Source code in auto_circuit/utils/graph_utils.py
patch_mode
patch_mode(model: PatchableModel, patch_src_outs: Tensor, edges: Optional[Collection[str | Edge]] = None, curr_src_outs: Optional[Tensor] = None)
Context manager to enable patching in the model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
PatchableModel
|
The patchable model to alter. |
required |
patch_src_outs |
Tensor
|
The activations with which to ablate the model. Mask values
interpolate the edge activations between the default activations ( |
required |
edges |
Optional[Collection[str | Edge]]
|
A collection of edges to patch. The corresponding patch mask elements
will be set to |
None
|
curr_src_outs |
Tensor
|
Stores the outputs of each src node during
the current forward pass. The only time this need to be initialized is when
you are starting the forward pass at a middle layer because the outputs of
previous
|
None
|
Warning
This function modifies the state of the model! This is a likely source of bugs.
Source code in auto_circuit/utils/graph_utils.py
patchable_model
patchable_model(model: Module, factorized: bool, slice_output: OutputSlice = None, seq_len: Optional[int] = None, separate_qkv: Optional[bool] = None, kv_caches: Tuple[Optional[HookedTransformerKeyValueCache], ...] = (None), device: device = t.device('cpu')) -> PatchableModel
Wrap a model and inject PatchWrapper
s into the
node modules to enable patching.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Module
|
The model to make patchable. |
required |
factorized |
bool
|
Whether the model is factorized, for Edge Ablation. Otherwise, only Node Ablation is possible. |
required |
slice_output |
OutputSlice
|
Specifies the index/slice of the output of the model to be
considered for the task. For example, |
None
|
seq_len |
Optional[int]
|
The sequence length of the model inputs. If |
None
|
separate_qkv |
Optional[bool]
|
Whether the model has separate query, key, and value inputs. Only used for transformers. |
None
|
kv_caches |
Tuple[Optional[HookedTransformerKeyValueCache], ...]
|
The key and value caches for the transformer. Only used for transformers. |
(None)
|
device |
device
|
The device that the model is on. |
device('cpu')
|
Returns:
Type | Description |
---|---|
PatchableModel
|
The patchable model. |
Warning
This function modifies the model, it does not return a new model.
Source code in auto_circuit/utils/graph_utils.py
set_all_masks
set_all_masks(model: PatchableModel, val: float) -> None
Set all the patch masks in the model to the specified value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
PatchableModel
|
The patchable model to alter. |
required |
val |
float
|
The value to set the patch masks to. |
required |
Warning
This function modifies the state of the model! This is a likely source of bugs.
Source code in auto_circuit/utils/graph_utils.py
set_mask_batch_size
set_mask_batch_size(model: PatchableModel, batch_size: int | None)
Context manager to set the batch size of the patch masks in the model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
PatchableModel
|
The patchable model to alter. |
required |
batch_size |
int | None
|
The batch size to set the patch masks to. If |
required |
Warning
This function breaks other functions of the library while the context is active and should be considered an experimental feature. This function modifies the state of the model! This is a likely source of bugs.
Source code in auto_circuit/utils/graph_utils.py
train_mask_mode
train_mask_mode(model: PatchableModel, requires_grad: bool = True) -> Iterator[Dict[str, Parameter]]
Context manager that sets the requires_grad
attribute of the patch masks for the
duration of the context and yields the parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
PatchableModel
|
The patchable model to alter. |
required |
requires_grad |
bool
|
Whether to enable gradient tracking on the patch masks. |
True
|
Yields:
Type | Description |
---|---|
Dict[str, Parameter]
|
The patch mask |
Dict[str, Parameter]
|
the key. |
Warning
This function modifies the state of the model! This is a likely source of bugs.