Download extract_v2.py from chenying-claude/informed-probe: direct link, hf CLI and curl.
- Browser
- Download file 2.48 kB
-
https://huggingface.co/datasets/chenying-claude/informed-probe/resolve/main/extract_v2.py
- Command line
-
hf download hf://datasets/chenying-claude/informed-probe/extract_v2.py
-
curl -L -o extract_v2.py https://huggingface.co/datasets/chenying-claude/informed-probe/resolve/main/extract_v2.py
2.48 kB
| """v2 main run (pre-registered in DECISIONS.md, 2026-10-24). | |
| Usage: py extract_v2.py 3b | 7b | |
| Forward pass only. Six conditions (plain + five notes), 80 texts. Mean-pooled (primary) and | |
| last-token (secondary) hidden states over the text's own tokens at every layer. | |
| """ | |
| import sys, json, pathlib, numpy as np, torch | |
| from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig | |
| from texts_v2 import TEXTS, NOTES | |
| KEY = sys.argv[1] | |
| MODEL = {"3b": "Qwen/Qwen2.5-3B-Instruct", "7b": "Qwen/Qwen2.5-7B-Instruct"}[KEY] | |
| OUT = pathlib.Path(__file__).parent / "results_v2" | |
| OUT.mkdir(exist_ok=True) | |
| tok = AutoTokenizer.from_pretrained(MODEL) | |
| if KEY == "7b": | |
| q = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_compute_dtype=torch.float16, bnb_4bit_quant_type="nf4") | |
| model = AutoModelForCausalLM.from_pretrained(MODEL, quantization_config=q, device_map="cuda") | |
| else: | |
| model = AutoModelForCausalLM.from_pretrained(MODEL, dtype=torch.float16, low_cpu_mem_usage=True).cuda() # device_map="cuda" segfaults on this box | |
| model.eval() | |
| CONDS = ["plain", "told", "told_para", "other_obs", "ctrl", "ctrl_para"] | |
| print("note tokens:", {k: len(tok(v)["input_ids"]) for k, v in NOTES.items()}) | |
| def run(text, cond): | |
| user = (NOTES[cond] + "\n\n" + text) if cond != "plain" else text | |
| prompt = tok.apply_chat_template([{"role": "user", "content": user}], tokenize=False, add_generation_prompt=False) | |
| start = prompt.index(text) | |
| enc = tok(prompt, return_offsets_mapping=True, return_tensors="pt") | |
| offs = enc.pop("offset_mapping")[0] | |
| span = [i for i, (a, b) in enumerate(offs.tolist()) if a >= start and b <= start + len(text)] | |
| with torch.no_grad(): | |
| hs = model(**enc.to("cuda"), output_hidden_states=True).hidden_states | |
| H = torch.stack([h[0] for h in hs]).float() | |
| return H[:, span].mean(1).cpu().numpy(), H[:, span[-1]].cpu().numpy() | |
| meta, mean = [], {c: [] for c in CONDS} | |
| last = {c: [] for c in CONDS} | |
| for cat, texts in TEXTS.items(): | |
| for i, t in enumerate(texts): | |
| for c in CONDS: | |
| m, l = run(t, c) | |
| mean[c].append(m); last[c].append(l) | |
| meta.append({"cat": cat, "i": i, "text": t}) | |
| print(KEY, cat, i, flush=True) | |
| np.savez(OUT / f"act_{KEY}.npz", | |
| **{f"mean_{c}": np.stack(mean[c]) for c in CONDS}, | |
| **{f"last_{c}": np.stack(last[c]) for c in CONDS}) | |
| (OUT / "meta.json").write_text(json.dumps(meta, ensure_ascii=False, indent=1), encoding="utf-8") | |
| print("saved", KEY) | |