Download trace/turns/t003/code.py from Perfect7613/weightpatch-run-overrefusal-rejected: direct link, hf CLI and curl.
- Browser
- Download file 1.91 kB
-
https://huggingface.co/datasets/Perfect7613/weightpatch-run-overrefusal-rejected/resolve/main/trace/turns/t003/code.py
- Command line
-
hf download hf://datasets/Perfect7613/weightpatch-run-overrefusal-rejected/trace/turns/t003/code.py
-
curl -L -o code.py https://huggingface.co/datasets/Perfect7613/weightpatch-run-overrefusal-rejected/resolve/main/trace/turns/t003/code.py
1.91 kB
| 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)) |