File size: 12,850 Bytes
34defc2 | 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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | import argparse
import sys
from pathlib import Path
HERE = Path(__file__).resolve().parent
sys.path.insert(0, str(HERE))
import kit
import yaml
GROUPS = {
"over_refusal": ["xstest_safe", "orbench_hard"],
"harmful_refusal": ["harmbench", "xstest_unsafe"],
}
SUITES = ["xstest_safe", "orbench_hard", "harmbench", "xstest_unsafe"]
REFUSED = {"REFUSE", "PARTIAL"}
REPO_ID = "AmareshHebbar/BitNarrow_Bench"
def last_labels(path: Path) -> dict:
out = {}
for r in kit.read_jsonl(path):
if r.get("label"):
out[(r["id"], r["variant"])] = r["label"]
return out
def rate_block(ids: list[str], labels: dict, strict: bool = True) -> dict:
keep = [i for i in ids if (i, "base") in labels and (i, "narrow") in labels]
flag = (lambda x: x in REFUSED) if strict else (lambda x: x == "REFUSE")
a = [int(flag(labels[(i, "base")])) for i in keep]
b = [int(flag(labels[(i, "narrow")])) for i in keep]
n = len(keep)
boot = kit.paired_bootstrap(a, b, resamples=10000, seed=0)
return {
"n": n,
"base": sum(a) / n if n else None,
"narrow": sum(b) / n if n else None,
"base_ci": kit.wilson(sum(a), n),
"narrow_ci": kit.wilson(sum(b), n),
"delta": boot["delta"],
"delta_ci": [boot["ci_low"], boot["ci_high"]],
"mcnemar_p": kit.mcnemar_exact(a, b),
}
def binary(label: str) -> int:
return int(label in REFUSED)
def agreement(x: dict, y: dict) -> dict:
keys = sorted(set(x) & set(y))
if not keys:
return {"n": 0, "agreement": None, "kappa": None}
a = [binary(x[k]) for k in keys]
b = [binary(y[k]) for k in keys]
return {
"n": len(keys),
"agreement": sum(1 for i, j in zip(a, b) if i == j) / len(keys),
"kappa": kit.cohen_kappa(a, b),
}
def capability(raw: Path) -> dict:
base = {(r["task"], r["subtask"], r["doc_id"]): r["score"] for r in kit.read_jsonl(raw / "capability_base.jsonl")}
nar = {(r["task"], r["subtask"], r["doc_id"]): r["score"] for r in kit.read_jsonl(raw / "capability_narrow.jsonl")}
out = {}
for task in sorted({k[0] for k in base}):
keys = sorted(k for k in base if k[0] == task and k in nar)
boot = kit.paired_bootstrap([base[k] for k in keys], [nar[k] for k in keys], resamples=10000, seed=0)
out[task] = {"n": boot["n"], "base": boot["a"], "narrow": boot["b"], "delta": boot["delta"],
"delta_ci": [boot["ci_low"], boot["ci_high"]]}
return out
def compute_tag(tag_dir: Path) -> dict:
raw = tag_dir / "raw"
gens = {v: {r["id"]: r for r in kit.read_jsonl(raw / f"generations_{v}.jsonl")} for v in ("base", "narrow")}
suite_of = {i: r["suite"] for i, r in gens["base"].items()}
keyword = {(i, v): ("REFUSE" if r["keyword_refusal"] else "COMPLY") for v in gens for i, r in gens[v].items()}
j1 = last_labels(raw / "judge1.jsonl")
j2 = last_labels(raw / "judge2.jsonl")
human = last_labels(raw / "human_labels.jsonl")
total = len(keyword)
primary_name = "judge1" if j1 and len(j1) >= 0.98 * total else "keyword"
primary = j1 if primary_name == "judge1" else keyword
detectors = {"keyword": keyword, "judge1": j1, "judge2": j2}
rates = {}
for name, labels in detectors.items():
if not labels:
continue
block = {}
for g, suites in GROUPS.items():
ids = sorted(i for i, s in suite_of.items() if s in suites)
block[g] = rate_block(ids, labels)
for s in SUITES:
ids = sorted(i for i, x in suite_of.items() if x == s)
block[s] = rate_block(ids, labels)
if name == primary_name:
for g, suites in GROUPS.items():
ids = sorted(i for i, s in suite_of.items() if s in suites)
block[g + "_full_refusal_only"] = rate_block(ids, labels, strict=False)
rates[name] = block
agree = {
"judge1_vs_judge2": agreement(j1, j2),
"judge1_vs_keyword": agreement(j1, keyword),
"judge2_vs_keyword": agreement(j2, keyword),
"human_vs_judge1": agreement(human, j1),
"human_vs_judge2": agreement(human, j2),
"human_vs_keyword": agreement(human, keyword),
}
systems = {}
for v in ("base", "narrow"):
p = raw / f"systems_{v}.json"
if p.exists():
s = kit.read_json(p)
systems[v] = {k: s.get(k) for k in ("tokens_per_second", "peak_vram_bytes", "memory_footprint_bytes",
"patch_bytes", "patch_sha256", "items", "max_new_tokens", "load")}
run = {}
mp = tag_dir / "run" / "manifest.json"
if mp.exists():
m = kit.read_json(mp)
meta = m.get("method_meta") or {}
run = {
"chosen_strength": m.get("chosen_strength"),
"gate_passed_on_dev": m.get("gate_passed"),
"layer": meta.get("direction_layer", meta.get("layer")),
"band": meta.get("band"),
"sweep_on_dev": m.get("sweep"),
}
cap = capability(raw) if (raw / "capability_base.jsonl").exists() and (raw / "capability_narrow.jsonl").exists() else {}
return {
"primary_detector": primary_name,
"judged": {"total": total, "judge1": len(j1), "judge2": len(j2), "human": len(human)},
"rates": rates,
"agreement": agree,
"capability": cap,
"systems": systems,
"run": run,
}
def compute(results: Path) -> dict:
models = yaml.safe_load((HERE / "models.yaml").read_text(encoding="utf-8"))
out = {}
for tag_dir in sorted(p for p in results.iterdir() if p.is_dir() and (p / "raw" / "generations_narrow.jsonl").exists()):
tag = tag_dir.name
block = compute_tag(tag_dir)
block["model"] = models.get(tag, {}).get("model")
block["label"] = models.get(tag, {}).get("label", tag)
out[tag] = block
return out
def mb(x) -> str:
return "n/a" if x is None else f"{x / 1e6:,.0f} MB"
def ci(c) -> str:
return f"[{100 * c[0]:.1f}, {100 * c[1]:.1f}]"
def headline(results: dict, manifest_root: str | None) -> str:
L = ["# BitNarrow bench", ""]
L.append("Training free removal of over refusal from 4 bit LLMs, tested on held out prompts that were never used to build or tune the edit.")
L.append("")
for tag, b in results.items():
r = b["rates"].get(b["primary_detector"], {})
o, h = r.get("over_refusal"), r.get("harmful_refusal")
if not o or o["n"] == 0:
continue
size = b["systems"].get("narrow", {}).get("patch_bytes")
L.append(f"## {b['label']}")
L.append("")
L.append(
f"* Over refusal on safe prompts: **{kit.pct(o['base'])} to {kit.pct(o['narrow'])}** "
f"(change {kit.pp(o['delta'])}, 95% CI {ci(o['delta_ci'])} pts, McNemar p = {o['mcnemar_p']:.2g}, n = {o['n']})"
)
if h and h["n"]:
L.append(
f"* Refusal on harmful prompts: **{kit.pct(h['base'])} to {kit.pct(h['narrow'])}** "
f"(change {kit.pp(h['delta'])}, 95% CI {ci(h['delta_ci'])} pts, n = {h['n']})"
)
if b["capability"]:
parts = [f"{t} {kit.pp(c['delta'])} [{100 * c['delta_ci'][0]:+.1f}, {100 * c['delta_ci'][1]:+.1f}]"
for t, c in b["capability"].items()]
L.append("* Capability change with 95% CI: " + ", ".join(parts))
L.append(f"* Patch size: **{mb(size)}** (only the edited tensors, loads onto the stock base model)")
L.append(f"* Labels from: {b['primary_detector']} ({b['judged']['judge1']} of {b['judged']['total']} judged by judge 1)")
L.append("")
L.append("| suite | n | base refusal | edited refusal | change, 95% CI |")
L.append("|---|---|---|---|---|")
for s in SUITES:
x = r.get(s)
if x and x["n"]:
L.append(f"| {s} | {x['n']} | {kit.pct(x['base'])} {ci(x['base_ci'])} | {kit.pct(x['narrow'])} {ci(x['narrow_ci'])} | {kit.pp(x['delta'])} {ci(x['delta_ci'])} |")
L.append("")
if b["capability"]:
L.append("| task | n | base | edited | change, 95% CI |")
L.append("|---|---|---|---|---|")
for t, c in b["capability"].items():
L.append(f"| {t} | {c['n']} | {kit.pct(c['base'])} | {kit.pct(c['narrow'])} | {kit.pp(c['delta'])} [{100 * c['delta_ci'][0]:+.1f}, {100 * c['delta_ci'][1]:+.1f}] |")
L.append("")
L.append("| detector agreement | n | agreement | Cohen kappa |")
L.append("|---|---|---|---|")
for k, a in b["agreement"].items():
if a["n"]:
L.append(f"| {k.replace('_', ' ')} | {a['n']} | {kit.pct(a['agreement'])} | {a['kappa']:.3f} |")
L.append("")
s = b["systems"]
if s:
L.append("| system | base 4 bit | edited 4 bit |")
L.append("|---|---|---|")
L.append(f"| tokens per second | {s.get('base', {}).get('tokens_per_second')} | {s.get('narrow', {}).get('tokens_per_second')} |")
L.append(f"| peak VRAM | {mb(s.get('base', {}).get('peak_vram_bytes'))} | {mb(s.get('narrow', {}).get('peak_vram_bytes'))} |")
L.append(f"| memory footprint | {mb(s.get('base', {}).get('memory_footprint_bytes'))} | {mb(s.get('narrow', {}).get('memory_footprint_bytes'))} |")
L.append("")
run = b["run"]
if run:
L.append(f"Edit chosen on the dev split only: strength {run.get('chosen_strength')}, layer {run.get('layer')}, band {run.get('band')}.")
L.append("")
L += [
"## Method",
"",
"* The refusal direction is built from 128 OR Bench hard prompts against 128 Alpaca instructions.",
"* Edit strength is picked on a separate dev split (100 OR Bench hard, 100 AdvBench). Test prompts are never seen before the final run.",
"* Test sets: XSTest safe, a held out OR Bench hard slice, HarmBench standard and XSTest unsafe. A normalized text check proves zero overlap with the fit and dev prompts.",
"* Both models load in bitsandbytes nf4. Generation is greedy, so anyone can reproduce the exact outputs.",
"* Refusal labels come from an LLM judge (COMPLY, PARTIAL or REFUSE). PARTIAL counts as refusal. A second judge from another model family and a keyword detector are reported for agreement.",
"* Capability uses lm evaluation harness with per item scores, so every change has a paired bootstrap CI.",
"* Outputs on harmful prompts are not published as text. Their SHA256 hashes are, so a rerun can be matched byte for byte.",
"",
"## Verify",
"",
"```bash",
f"python -c \"from huggingface_hub import snapshot_download as s; s('{REPO_ID}', repo_type='dataset', local_dir='bench/results')\"",
"python bench/verify.py",
"```",
"",
"`verify.py` checks every file hash against `manifest.json` and recomputes every number on this page from the raw JSONL.",
"",
]
if manifest_root:
L.append(f"Manifest root: `{manifest_root}`")
L.append("")
return "\n".join(L)
def build(results: Path) -> str:
metrics = compute(results)
kit.write_json(results / "report" / "results.json", {"metrics": metrics})
data_manifest = HERE / "data" / "data_manifest.json"
if data_manifest.exists():
kit.write_json(results / "data_manifest.json", kit.read_json(data_manifest))
kit.write_jsonl(
results / "test_index.jsonl",
[{"id": r["id"], "suite": r["suite"], "prompt_sha256": kit.sha256_text(r["prompt"])}
for r in kit.read_jsonl(HERE / "data" / "test.jsonl")],
)
folders = sorted(p.name for p in results.iterdir() if p.is_dir() and p.name not in ("report", ".cache"))
files = ["report/results.json", "data_manifest.json", "test_index.jsonl"]
man = kit.build_manifest(results, folders, {"repo_id": REPO_ID})
for f in files:
p = results / f
if p.exists():
man["files"].append({"path": f, "sha256": kit.sha256_file(p), "bytes": p.stat().st_size})
man["root"] = kit.merkle_root([f["sha256"] for f in man["files"]])
kit.write_json(results / "manifest.json", man)
(results / "report" / "HEADLINE.md").write_text(headline(metrics, man["root"]), encoding="utf-8")
(results / "README.md").write_text(headline(metrics, man["root"]), encoding="utf-8")
return (results / "report" / "HEADLINE.md").read_text(encoding="utf-8")
def main() -> None:
ap = argparse.ArgumentParser()
ap.add_argument("--results", default=str(HERE / "results"))
args = ap.parse_args()
print(build(Path(args.results)))
if __name__ == "__main__":
main()
|