Metadata-Version: 2.4
Name: circuitcheck
Version: 0.1.0
Summary: Tools on top of circuit-tracer: diff attribution graphs across training, validate circuits with controls, and label features cheaply.
Keywords: interpretability,circuits,attribution-graphs,transcoders,circuit-tracer,autointerp
Author: albertlieyingadrian
License-Expression: MIT
License-File: LICENSE
Classifier: Development Status :: 2 - Pre-Alpha
Classifier: Intended Audience :: Science/Research
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Typing :: Typed
Requires-Dist: numpy>=1.26
Requires-Dist: pydantic>=2.7,<3
Requires-Dist: typer>=0.12
Requires-Dist: anthropic>=0.69 ; extra == 'anthropic'
Requires-Dist: pytest>=9.0 ; extra == 'dev'
Requires-Dist: pytest-cov>=7.0 ; extra == 'dev'
Requires-Dist: ruff>=0.17 ; extra == 'dev'
Requires-Dist: mypy>=2.0 ; extra == 'dev'
Requires-Dist: nbclient>=0.10 ; extra == 'dev'
Requires-Dist: nbformat>=5.10 ; extra == 'dev'
Requires-Dist: ipykernel>=6.29 ; extra == 'dev'
Requires-Dist: openai>=1.100 ; extra == 'openai'
Requires-Dist: torch>=2.4 ; extra == 'tracer'
Requires-Dist: transformers>=4.51 ; extra == 'tracer'
Requires-Python: >=3.11
Project-URL: Homepage, https://github.com/albertlieyingadrian/circuitcheck
Project-URL: Source, https://github.com/albertlieyingadrian/circuitcheck
Project-URL: Documentation, https://github.com/albertlieyingadrian/circuitcheck/tree/main/docs
Project-URL: Changelog, https://github.com/albertlieyingadrian/circuitcheck/blob/main/CHANGELOG.md
Provides-Extra: anthropic
Provides-Extra: dev
Provides-Extra: openai
Provides-Extra: tracer
Description-Content-Type: text/markdown

# circuitcheck

<p align="center"><img src="docs/images/demo.gif" alt="circuitcheck validate --demo: the toy model's planted switch comes back as a switch, its two readouts as readouts and its redundant pair as backups" width="760"></p>

**Test what an attribution graph claims.** circuitcheck is three tools on top of
[circuit-tracer](https://github.com/decoderesearch/circuit-tracer) attribution graphs:

| Tool | Question it answers | Runs without a GPU on |
|---|---|---|
| `circuitcheck validate` | Is a group of graph features a **switch** (it causes the behaviour) or a **readout** (it only reflects it)? | the built-in toy model |
| `circuitcheck diff` | Which features and edges did fine-tuning or RL add or remove, and is the change real? | saved graph summaries |
| `circuitcheck label` | What does each feature do, in a few words, and how far can that label be trusted? | cached model replies |

▶ [Watch the 2.5-minute walkthrough](docs/videos/walkthrough.mp4) · [Use cases](docs/use-cases.mdx) ·
[circuitcheck vs circuit-tracer](docs/vs-circuit-tracer.mdx) · [Docs](docs/index.mdx)

It grew out of [steerlab's circuits episodes](https://albertlieyingadrian.github.io/steerlab/experiments/#circuits).
In [a password lock](https://albertlieyingadrian.github.io/steerlab/experiments/lock-circuit/report/) and
[an RL reward hack](https://albertlieyingadrian.github.io/steerlab/experiments/rl-hack-circuit/report/), the graph's
most influential features all turned out to be readouts. circuitcheck turns the checks behind that finding into
reusable code.

## Install

```bash
pip install circuitcheck        # after the first PyPI release; until then:
pip install "git+https://github.com/albertlieyingadrian/circuitcheck"
# real models: circuit-tracer from GitHub, plus torch
pip install "git+https://github.com/decoderesearch/circuit-tracer"
# labels: pip install "circuitcheck[anthropic]" (Claude Haiku 5.5, the default) or "[openai]"
```

## Five-minute tour (no GPU, no key)

```bash
circuitcheck validate --demo --html validate.html
circuitcheck diff examples/data/rl_hack_graphs.json.gz --before installed/before --after installed/after \
  --control-before control/before --control-after control/after --html diff.html
```

```python
from circuitcheck.validate import logit_diff, validate
from circuitcheck.validate.toy import demo, demo_nodes

backend = demo()
metric = logit_diff(backend.token_id(" Austin"), backend.token_id(" Sacramento"))
report = validate(backend, "dallas", demo_nodes(backend), metric=metric, counterfactual="oakland")
print([(g.name, g.verdict.value) for g in report.groups])
# [('top-5', 'switch'), ('L5/100@5', 'switch'), ('L7/200@5', 'readout'), ('L8/201@5', 'readout'), ...]
```

On a real model, `circuitcheck.tracer.TracerBackend` wraps a circuit-tracer `ReplacementModel`:

```python
from circuit_tracer import ReplacementModel
from circuitcheck.tracer import TracerBackend

model = ReplacementModel.from_pretrained("google/gemma-2-2b", "gemma")
report = validate(TracerBackend(model), prompt, nodes, metric=metric, counterfactual=other_prompt)
```

## What each tool checks

**diff.** Graphs of the same prompts before and after training are reduced to per-feature influence and
feature-to-feature edges, summed over positions. Each feature gets a status (gained, lost, up, down, stable). The
statuses are backed by:

- a paired swap null: before and after labels are swapped at random within each prompt pair, with a
  Benjamini-Hochberg correction across features;
- a bootstrap interval and a split-half check;
- optionally, a control run (did the change happen only in the run that learned the behaviour?) and a replication
  seed.

The validity check compares replacement and completeness scores, error-node influence and per-layer reconstruction
error (`circuitcheck.tracer.reconstruction_error`) before and after. The transcoders were trained on the base model,
and the report says so every time.

**validate.** For each group of features:

- ablation (set them to 0);
- steering (multiply them by 3);
- a counterfactual patch (copy their values from a prompt with a different answer);
- each of the above on 20 random feature sets matched by layer and position, and on 20 sets matched by write size
  (|activation| x decoder norm).

The verdict follows printed rules: switch, partial, backup, nonspecific or readout. An optional explanation swap (Gao's
test) runs label rules written as code in a checked sandbox. Output is JSON plus an HTML page.

**labels.** The pipeline:

1. Explain each feature from its top activating records.
2. Score the label on held-out records: detection AUROC (activating and silent excerpts) and, optionally, simulation
   correlation.
3. Report calibration: stated confidence against the earned score, and the detector's expected calibration error.

Every reply is cached. Labels export in Neuronpedia's explanation format, and Neuronpedia's own labels can be scored by
the same detector for a fair comparison.

## Results that reproduce the episodes

- **Graph diff.** On the RL-hack study's 48 graphs, `diff_graphs` picks the same ten features the study did, in the
  same order. 9 of the 10 are specific to the run that hacked and 6 come back in another seed (test:
  `tests/test_diff.py::test_reproduces_rl_hack_selection`). Split halves of the prompts agree on 70% of the top 10, and
  reconstruction error is unchanged by RL (0.500 vs 0.499).
- **Labels.** Claude Haiku 5.5 labelled 50 Gemma-2-2B transcoder features for $0.26 in total. Its labels match
  Neuronpedia's on detection (AUROC 0.80 vs 0.78 and 0.80) and beat them on simulation (r = 0.45 vs 0.37 and 0.38).
  The explainer's stated confidence barely predicts its score (r = 0.11), so use the score, not the confidence.
- **Live validator runs** on the RL-hack model and the password-locked model: see `examples/data/live/`.

## Layout

```
src/circuitcheck/
  diff/       graphs.py (summaries and loaders: circuit-tracer .pt, Neuronpedia JSON), core.py (the diff)
  validate/   core.py (checks and verdicts), controls.py, metrics.py, sandbox.py, toy.py
  labels/     autointerp.py (explain, detect, simulate, calibrate), providers.py (Claude, OpenAI, cache), neuronpedia.py
  tracer.py   circuit-tracer adapter and reconstruction error
  report.py   HTML pages
  cli.py      the circuitcheck command
examples/     notebooks 01-03, data/, scripts/ (how the bundled data was made), modal/ (live GPU checks)
```

## Development

```bash
uv venv && uv pip install -e ".[dev]"
pytest && ruff check && mypy src
```

MIT licence.
