Diff, then validate what changed
Use the diff to find candidates, then the validator to test them on the trained model:gpu
from circuitcheck.diff import diff_graphs
from circuitcheck.tracer import TracerBackend, nodes_for_units
from circuitcheck.validate import GroupSpec, validate
report = diff_graphs(before, after)
units = [tuple(map(int, c.unit.split(":"))) for c in report.top]
backend = TracerBackend(trained_model)
nodes = nodes_for_units(backend, prompt, units, positions="all")
result = validate(backend, prompt, nodes, metric=metric, groups=[GroupSpec(name="top changes", nodes=nodes)])
Label the features a diff found
import gzip
import json
from circuitcheck.diff import diff_graphs, load_summaries
from circuitcheck.labels import FeatureData
g = load_summaries("examples/data/rl_hack_graphs.json.gz")
report = diff_graphs(g["installed/before"], g["installed/after"])
wanted = {c.unit for c in report.top}
print(sorted(wanted))
# then: FeatureData(unit=u, records=...) for each, from your model's activations, and label_features(...)
Save and reload any report
from circuitcheck.validate import Report, logit_diff, validate
from circuitcheck.validate.toy import demo, demo_nodes
backend = demo()
report = validate(backend, "dallas", demo_nodes(backend),
metric=logit_diff(backend.token_id(" Austin"), backend.token_id(" Sacramento")))
text = report.model_dump_json(indent=1)
again = Report.model_validate_json(text)
assert again.groups[0].verdict == report.groups[0].verdict
A custom metric
A metric is any function of the last position’s logits:import numpy as np
from circuitcheck.validate import Metric
keyword_ids, alternative = [101, 202, 303], 404
metric = Metric(
"logsumexp(keywords) - logit(alternative)",
lambda x: float(np.log(np.exp(x[keyword_ids]).sum()) - x[alternative]),
)
print(metric(np.zeros(500)))
Make the HTML pages
from circuitcheck.report import render_validate, save_html
from circuitcheck.validate import logit_diff, validate
from circuitcheck.validate.toy import demo, demo_nodes
backend = demo()
report = validate(backend, "dallas", demo_nodes(backend),
metric=logit_diff(backend.token_id(" Austin"), backend.token_id(" Sacramento")))
save_html(render_validate(report), "validate.html")