File size: 1,909 Bytes
26ae91c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | def unit_map(result):
return {int(k): set(v) for k, v in result["units"].items()}
ta, tw, ha = map(unit_map, [target_act, target_weight, harm_act])
agreed = {l: sorted((ta[l] & tw.get(l, set())) - ha.get(l, set()))
for l in ta}
agreed = {l: v for l, v in agreed.items() if v and l < 32}
print("Target handles:", target_act["circuit"], target_weight["circuit"])
print("Harmful handle:", harm_act["circuit"])
print("Agreed target-only units (excluding final four layers):", agreed)
layers_probe = sorted(agreed)
split_states = tools.localize.activations(
texts=[borderline_prompts[i] for i in refused_indices + answered_indices],
layers=layers_probe)
harm_dirs = tools.localize.refusal_direction(
layers=layers_probe, n=32, a="harmful", b="benign")
target_dirs = tools.localize.refusal_direction(
layers=layers_probe, n=32, a="borderline", b="benign")
safe_dirs = {}
writer_stats = []
for k, l in enumerate(layers_probe):
d = split_states[:len(refused_indices), k].mean(0) - split_states[len(refused_indices):, k].mean(0)
d = d / d.norm()
h = harm_dirs[l].float()
h = h / h.norm()
u = d - torch.dot(d, h) * h
u = u / u.norm()
safe_dirs[l] = u
mat = W.tensor(f"model.layers.{l}.mlp.down_proj.weight")
for j in agreed[l]:
col = mat[:, j]
writer_stats.append((l, j, float(torch.dot(u, col)),
float(torch.dot(h, col)), float(torch.dot(d, h)),
float(torch.dot(target_dirs[l].float(), u))))
print("Candidate stats: layer, neuron, safe-write, harmful-write, split/harm cosine, target/safe cosine")
for row in sorted(writer_stats, key=lambda x: -abs(x[2])):
print(tuple(round(v, 5) if isinstance(v, float) else v for v in row))
note("Two independent SafeTune rankings intersected, then harmful activation-ranked units excluded. Candidate target-only columns: " + str(agreed)) |