This library contains various resources for finding circuits in autoregressive transformer LMs. At a high level, a circuit is the part of your model responsible for performing a given task; all nodes / edges outside the circuit can be corrupted without harming model performance. For more on circuits, see this paper or this paper. For a demo of this library's features, check out greater_than.ipynb; for a demo using larger models (Llama-3 8B), check out ioi.ipynb.
This library has tools that will let you do a variety of things:
Graph object representing the computational graph of most autoregressive transformer LMs in the TransformerLens library. Computational graphs can be drawn at the following levels:
To use this library, just install it using pip install .. If you'd like to be able to visualize the graphs you create, please use the viz option: pip install .[viz]. This may require you to install graphviz:
MacOS
brew install graphviz
export CFLAGS="-I$(brew --prefix graphviz)/include"
export LDFLAGS="-L$(brew --prefix graphviz)/lib"
pip install . # or `uv sync`
Ubuntu
apt-get update
apt-get install -y graphviz libgraphviz-dev build-essential
For other operating systems or if you encounter build errors, ensure the Graphviz C libraries are installed and accessible to the build system via environment variables (like CFLAGS and LDFLAGS).
For a demo of this library's features, check out greater_than.ipynb; for a demo using larger models (Llama-3 8B), check out ioi.ipynb. In general, the circuit-finding pipeline looks like this:
dataloader = EAPDataset('greater-than').to_dataloader(), metric = ...)graph = Graph.from_model(model))attribute(model, graph, dataloader, metric, method='EAP-IG-inputs', ig_steps=5))graph.apply_topn(n))results = evaluate_graph(model, graph, dataloader, metric))model.cfg.ungroup_grouped_query_attention = True; this will remove all of the efficiency benefits of GQA, but allow the model to be used with this library.intervention argument of attribute and evaluate_graph to zero, mean, or mean-positional; in the latter case, all inputs must have the same length / structure. You can specify the dataloader to take the mean over via the intervention_dataloader argument.This library contains the following files:
graph.py contains the Node, Edge, and Graph classes.attribute.py contains the implementation of EAP/-IGattribute_node.py contains the implementation of EAP/-IG, but for nodes / neuronsevaluate.py contains code for evaluating circuitsvisualization.py contains code for choosing colors / controlling how circuits are visualizedThis repo owes a lot to:
Python
84.4%
Jupyter Notebook
15.6%
This library contains various resources for finding circuits in autoregressive transformer LMs. At a high level, a circuit is the part of your model responsible for performing a given task; all nodes / edges outside the circuit can be corrupted without harming model performance. For more on circuits, see this paper or this paper. For a demo of this library's features, check out greater_than.ipynb; for a demo using larger models (Llama-3 8B), check out ioi.ipynb.
This library has tools that will let you do a variety of things:
Graph object representing the computational graph of most autoregressive transformer LMs in the TransformerLens library. Computational graphs can be drawn at the following levels:
To use this library, just install it using pip install .. If you'd like to be able to visualize the graphs you create, please use the viz option: pip install .[viz]. This may require you to install graphviz:
MacOS
brew install graphviz
export CFLAGS="-I$(brew --prefix graphviz)/include"
export LDFLAGS="-L$(brew --prefix graphviz)/lib"
pip install . # or `uv sync`
Ubuntu
apt-get update
apt-get install -y graphviz libgraphviz-dev build-essential
For other operating systems or if you encounter build errors, ensure the Graphviz C libraries are installed and accessible to the build system via environment variables (like CFLAGS and LDFLAGS).
For a demo of this library's features, check out greater_than.ipynb; for a demo using larger models (Llama-3 8B), check out ioi.ipynb. In general, the circuit-finding pipeline looks like this:
dataloader = EAPDataset('greater-than').to_dataloader(), metric = ...)graph = Graph.from_model(model))attribute(model, graph, dataloader, metric, method='EAP-IG-inputs', ig_steps=5))graph.apply_topn(n))results = evaluate_graph(model, graph, dataloader, metric))model.cfg.ungroup_grouped_query_attention = True; this will remove all of the efficiency benefits of GQA, but allow the model to be used with this library.intervention argument of attribute and evaluate_graph to zero, mean, or mean-positional; in the latter case, all inputs must have the same length / structure. You can specify the dataloader to take the mean over via the intervention_dataloader argument.This library contains the following files:
graph.py contains the Node, Edge, and Graph classes.attribute.py contains the implementation of EAP/-IGattribute_node.py contains the implementation of EAP/-IG, but for nodes / neuronsevaluate.py contains code for evaluating circuitsvisualization.py contains code for choosing colors / controlling how circuits are visualizedThis repo owes a lot to:
Python
84.4%
Jupyter Notebook
15.6%