sae_token_programs / selection /choose_fire_set.py
AmiriHayes's picture
Upload folder using huggingface_hub
2e6378c verified
Raw History Blame Contribute Delete
4.4 kB
"""How each program's fire set is chosen. No LLM, no gradients, no search.
Every program in this dataset has the same shape:
fire = {' George', 'George', ' Osama', 'ORGE'}
return 1 if input_tokens[-1] in fire else 0
so building one comes down to a single question: WHICH tokens go in the set?
`choose_fire_set` below is the step that answers it.
Start from an empty set. Repeatedly try adding every token that is not in the
set yet, and keep whichever one raises the F1 of the WHOLE SET the most. Stop
as soon as no single token improves it, or once the set is full.
For the George feature this runs:
add ' George' -> F1 0.877
add 'George' -> F1 0.927
add ' Osama' -> F1 0.954
add 'ORGE' -> F1 0.958
nothing else helps, so stop with four tokens.
This is NOT the same as taking the most frequent tokens in order. At each step
every remaining candidate is re-scored, and the winner is usually not the next
most frequent one -- across layer-6 features it differs 98.7% of the time. A
token that fires 531 times out of 2,227 appearances loses to one that fires
142 times out of 145, because the first drags in 1,696 false positives.
Everything here is measured on the TRAINING corpus. The test split is touched
once, at the very end, to score the finished program.
TWO SMALL STEPS COME AFTER THIS ONE, so the set you get from `choose_fire_set`
alone is not always byte-identical to the shipped program:
* other spellings of a chosen word are added where they help
(' George' is chosen, so 'George' and 'GEORGE' are considered too)
* word families contributing under 0.25% of the feature's firings are
dropped, since one or two firings is not evidence of anything
For the George feature that second step is why `choose_fire_set` returns a
fifth token, 'Americ', that the shipped program does not have: it scraped a
hair above zero gain here and was pruned as a tail family afterwards. Both
steps live in `fit_programs.py`.
"""
from typing import List
import numpy as np
def choose_fire_set(token_stats: List[dict], total_firings: int,
max_tokens: int = 50) -> List[str]:
"""Pick the tokens that go in one feature's fire set.
token_stats
One dict per token the feature was ever seen firing on:
'token' the token itself, e.g. ' George'
'fires' times the feature fired on it (3,249)
'occurrences' times the token appeared at all (3,249)
`occurrences - fires` is how often the token showed up WITHOUT the
feature firing, i.e. the false positives it would cost us. This is why
we need the corpus occurrence count and not just the firing count.
total_firings
How many times the feature fires anywhere in the corpus. Firings not
covered by the chosen tokens are the false negatives.
max_tokens
Size limit on the set, so the printed program stays readable. 50 is
what this dataset used.
Returns the chosen tokens, in the order they were added.
"""
if not token_stats or total_firings <= 0 or max_tokens <= 0:
return []
tokens = [t["token"] for t in token_stats]
fires = np.array([t["fires"] for t in token_stats], dtype=np.float64)
occurrences = np.array([t["occurrences"] for t in token_stats], dtype=np.float64)
chosen: List[int] = []
still_available = np.ones(len(token_stats), dtype=bool)
true_pos = false_pos = 0.0
best_f1_so_far = 0.0
for _ in range(min(max_tokens, len(token_stats))):
# if we added each candidate to the set, what would the set score?
new_true_pos = true_pos + fires
new_false_pos = false_pos + (occurrences - fires)
false_neg = total_firings - new_true_pos
f1 = np.where(
new_true_pos > 0,
2 * new_true_pos / (2 * new_true_pos + new_false_pos + false_neg),
0.0,
)
f1[~still_available] = -1.0 # already in the set
best = int(np.argmax(f1))
if f1[best] <= best_f1_so_far:
break # nothing left that helps
best_f1_so_far = float(f1[best])
true_pos = float(new_true_pos[best])
false_pos = float(new_false_pos[best])
still_available[best] = False
chosen.append(best)
return [tokens[i] for i in chosen]