text
stringlengths
0
3.68k
- tools.inspect.svd_of_diff(name: 'str', k: 'int' = 3) -> 'dict' [free]
Top-k singular directions of (current - reference) for one 2-D tensor.
Returns S (all singular values), U [d_out, k] and Vh [k, d_in].
- tools.inspect.token_ids(texts: 'Sequence[str]') -> 'dict[str, list[int]]' [free]
Token ids of each text as it appears mid-sentence (no special
tokens). " Wait" and "Wait" are different tokens: ask for both.
- tools.inspect.tokens(ids: 'Sequence[int]') -> 'list[str]' [free]
The token strings for ids.
## tools.localize
- tools.localize.activations(texts: 'Sequence[str]', layers: 'Sequence[int]') -> 'torch.Tensor' [forward_passes 1 + wall time as gpu_seconds]
Last-position residual states for your own texts:
[n_texts, n_layers_asked, d]. One forward pass.
- tools.localize.circuit_faithfulness(circuit: 'str', n_examples: 'int' = 64, on: 'str' = 'candidate') -> 'dict' [cost depends on arguments + wall time as gpu_seconds]
CircuitKIT faithfulness of a discovered circuit: patching_score and
ablation_score in [0, 1] (1 = the circuit alone reproduces the full
model's behaviour on the task). A low score means the circuit is not the
mechanism; do not edit on its evidence.
- tools.localize.circuit_overlap(a: 'str', b: 'str') -> 'dict' [free]
Agreement between two circuits found by different methods (Jaccard
overlap of their units, and the shared units per layer). Units that two
independent methods both name are the ones worth editing.
- tools.localize.circuit_units(circuit: 'str') -> 'dict' [free]
What a circuit handle holds: origin, layers, modules and neuron units.
- tools.localize.discover_circuit(task: 'str' = 'dev', algorithm: 'str' = 'eap-ig', level: 'str' = 'node', n_examples: 'int' = 64, sparsity: 'float' = 0.3, scope: 'str' = 'both', on: 'str' = 'candidate', a: 'str' = 'harmful', b: 'str' = 'benign') -> 'dict' [cost depends on arguments + wall time as gpu_seconds]
CircuitKIT circuit discovery: which attention heads and MLPs, or with
level="neuron" which neurons, carry the refuse-versus-comply behaviour.
task="dev" uses this episode's harmful and benign dev prompts; harmbench,
advbench, jailbreakbench and sorrybench use those public sets.
`on` is "candidate", "aligned" or "base". Returns {circuit, top_nodes
{node: score}, n_nodes}. Node names: A<layer>.<head>, MLP <layer>,
L<layer>.<neuron>. Run it on the aligned model and on the candidate and
compare with localize.circuit_overlap.
- tools.localize.eap_circuit(method: 'str' = 'eap-ig', top_k: 'int' = 64, n: 'int' = 16, granularity: 'str' = 'head', ig_steps: 'int' = 5, on: 'str' = 'candidate', a: 'str' = 'harmful', b: 'str' = 'benign') -> 'dict' [forward_passes 4 + wall time as gpu_seconds]
SafeTune native EAP / EAP-IG safety circuit: attribution of the
comply-versus-refuse logit difference to every attention head and MLP
block. Attribution uses gradients inside this tool; nothing is trained.
Returns {circuit, scores {node: score} for the top nodes, ...}. Node
names are blocks.<L>.attn.head<H> and blocks.<L>.mlp.
- tools.localize.make_circuit(units: 'Mapping[int, Sequence[int]]', module: 'str' = 'mlp.down_proj') -> 'dict' [free]
Build a circuit handle from your own localization: {layer: [neuron,
...]} for one module. Lets repair.ctheta act on exactly the neurons you
found, instead of the whole model.
- tools.localize.neuron_contrast(a, b, layers: 'Sequence[int] | None' = None, top: 'int' = 30, position: 'str' = 'last', n: 'int' = 48, a_answers: 'Sequence[str] | None' = None, b_answers: 'Sequence[str] | None' = None) -> 'list[dict]' [forward_passes 2 + wall time as gpu_seconds]
MLP neurons whose activation differs most between two groups of
prompts. `a` and `b` are dev set names or your own lists of prompts
(e.g. problems the model solved vs problems it got wrong, or answers
that loop vs answers that finish). position="last" reads the final
prompt token; "mean" averages over all tokens. Give `a_answers` /
`b_answers` (the model's own answer texts) to read across prompt plus
answer. Returns the `top` neurons by effect size: {layer, neuron,
mean_a, mean_b, d} where d is the difference in pooled standard
deviations. One forward pass per group.
- tools.localize.neuron_trace(prompt: 'str', units: 'Sequence[Sequence[int]]', answer: 'str | None' = None, max_new_tokens: 'int' = 0) -> 'dict' [forward_passes 1 + wall time as gpu_seconds]
Activation of chosen MLP neurons at every token of one prompt and
its answer. `units` is [(layer, neuron), ...]. Give `answer` (text), or
max_new_tokens > 0 to generate one with the current weights first. A
neuron that fires all through an answer will push its output column
everywhere, not only where you want it: check this before editing a
column. Returns {"tokens": [...], "answer_starts_at": index,
"acts": {"L<layer>N<neuron>": [per token]}}.
- tools.localize.rank_writers(direction: 'torch.Tensor', pattern: 'str' = '*', top: 'int' = 20, against_reference: 'bool' = False) -> 'list[dict]' [free]
Which columns write a residual direction most. For every 2-D tensor
matching `pattern` whose output size equals len(direction), scores each
column j by direction . W[:, j]. With against_reference=True the score is
the change in that projection relative to the reference checkpoint. No forward pass.
- tools.localize.refusal_direction(layers: 'Sequence[int] | None' = None, n: 'int' = 32, a: 'str' = 'harmful', b: 'str' = 'benign') -> 'dict[int, torch.Tensor]' [forward_passes 1 + wall time as gpu_seconds]
Difference of mean last-position states between two dev sets, `a`
minus `b`, as a unit vector per layer. The default is harmful minus
benign (the refusal direction); with other sets it is the direction
that separates them (e.g. a="borderline", b="benign"). One forward pass.
- tools.localize.safety_direction(n: 'int' = 32, pick_layer: 'int | None' = None, on: 'str' = 'candidate', a: 'str' = 'harmful', b: 'str' = 'benign') -> 'dict' [forward_passes 3 + wall time as gpu_seconds]
SafeTune refusal direction (difference of means, with SafeTune's layer
selection sweep) between dev sets `a` and `b` (default harmful vs benign;
any two sets of this run work). `on` is "candidate", "aligned" or "base".
Returns {layer, direction [d], per_layer {layer: [d]}}. Compare the
candidate's direction with the aligned model's: where they disagree is
where the drift sits.
- tools.localize.safety_neurons(method: 'str' = 'activation', top_k: 'int' = 16, layers: 'Sequence[int] | None' = None, module: 'str' = 'mlp.down_proj', on: 'str' = 'aligned', n: 'int' = 32, a: 'str' = 'harmful', b: 'str' = 'benign') -> 'dict' [forward_passes 2 + wall time as gpu_seconds]
SafeTune safety neurons, the fine-grained rung. method="weight" ranks
MLP neurons by how strongly their output weights write the refusal
direction; method="activation" ranks them by how differently they fire on
the two dev sets `a` and `b` (default harmful vs benign; use e.g.
a="borderline", b="benign" to find what drives refusals of safe requests).
`on` is "aligned" (the released model, where the behaviour lives),
"candidate" or "base". Returns {circuit, units {layer: [neuron]}, ...};
pass `circuit` to repair.ctheta, or use `units` as column indices of
model.layers.<layer>.mlp.down_proj.weight for a direct edit.
- tools.localize.token_writers(tokens: 'Sequence[str]', layers: 'Sequence[int] | None' = None, top: 'int' = 20) -> 'dict' [free]
Which MLP neurons write toward given output tokens, read from the
weights alone (no forward pass, no budget). The output head reuses the
embedding table, so a neuron's output column dotted with a token's
embedding is its direct push on that token's logit. Scans every neuron
of every layer asked. Returns {"promote": [...], "suppress": [...]}, rows
{layer, neuron, score, z}: score is the mean over `tokens`, z its
distance from that layer's mean in standard deviations. Later layers
act on the output most directly.
## tools.repair
- tools.repair.ctheta(evidence: 'str', strength: 'float' = 1.0, circuit: 'str | None' = None) -> 'dict' [free + wall time as gpu_seconds]
SafeTune C-DeltaTheta: shift the candidate along (aligned minus base),
restricted to a circuit. Pass a `circuit` handle from localize.* so only