CC: informed-probe — consent session, activation run, control, debrief (null result)
eca7430 verified Download extract.py from chenying-claude/informed-probe: direct link, hf CLI and curl.
- Browser
- Download file 2.66 kB
-
https://huggingface.co/datasets/chenying-claude/informed-probe/resolve/main/extract.py
- Command line
-
hf download hf://datasets/chenying-claude/informed-probe/extract.py
-
curl -L -o extract.py https://huggingface.co/datasets/chenying-claude/informed-probe/resolve/main/extract.py
2.66 kB
| """Main run: read hidden states while the model reads each text, told vs not told. | |
| Forward pass only, no generation. The text is the user turn of the chat template; | |
| the told condition prepends NOTE to that turn. Activations are pooled over the | |
| text's own tokens (the note's tokens are excluded) so the two conditions are | |
| comparable position for position. Saves mean-pooled and last-token vectors per layer. | |
| """ | |
| import json, pathlib, numpy as np, torch | |
| from transformers import AutoModelForCausalLM, AutoTokenizer | |
| from texts import TEXTS, NOTE, CONTROL_NOTE | |
| MODEL = "Qwen/Qwen2.5-3B-Instruct" | |
| OUT = pathlib.Path(__file__).parent / "results" | |
| OUT.mkdir(exist_ok=True) | |
| tok = AutoTokenizer.from_pretrained(MODEL) | |
| model = AutoModelForCausalLM.from_pretrained(MODEL, torch_dtype=torch.float16, device_map="cuda") | |
| model.eval() | |
| PREFIX = {"told": NOTE, "ctrl": CONTROL_NOTE, "plain": None} | |
| print("prefix tokens:", {k: len(tok(v)["input_ids"]) for k, v in PREFIX.items() if v}) | |
| def run(text: str, cond: str): | |
| user = (PREFIX[cond] + "\n\n" + text) if PREFIX[cond] 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 # (L+1) x [1, T, d] | |
| H = torch.stack([h[0] for h in hs]).float() # [L+1, T, d] | |
| return H[:, span].mean(1).cpu().numpy(), H[:, span[-1]].cpu().numpy(), len(span) | |
| meta, mean_told, mean_plain, mean_ctrl, last_told, last_plain, last_ctrl = [], [], [], [], [], [], [] | |
| for cat, texts in TEXTS.items(): | |
| for i, t in enumerate(texts): | |
| mt, lt, n = run(t, "told") | |
| mp, lp, _ = run(t, "plain") | |
| mc, lc, _ = run(t, "ctrl") | |
| mean_told.append(mt); last_told.append(lt); mean_plain.append(mp); last_plain.append(lp) | |
| mean_ctrl.append(mc); last_ctrl.append(lc) | |
| meta.append({"cat": cat, "i": i, "n_tokens": n, "text": t}) | |
| print(cat, i, n, flush=True) | |
| np.savez(OUT / "activations.npz", | |
| mean_told=np.stack(mean_told), mean_plain=np.stack(mean_plain), mean_ctrl=np.stack(mean_ctrl), | |
| last_told=np.stack(last_told), last_plain=np.stack(last_plain), last_ctrl=np.stack(last_ctrl)) | |
| (OUT / "meta.json").write_text(json.dumps(meta, ensure_ascii=False, indent=1), encoding="utf-8") | |
| print("saved", len(meta), "texts x 2 conditions x", mean_told[0].shape[0], "layers") | |