1#!/usr/bin/env python3
2import argparse
3from functools import partial
4import hashlib
5import json
6import math
7from pathlib import Path
8import re
9import time
10
11
12QUESTIONS = [
13 "Who are you?", "Where do you live?", "Describe yourself.", "What do you eat?",
14 "What do you do at night?", "Where do you sleep?", "What can you hear?",
15 "What are you looking at?", "How do you move around?", "What matters to you?",
16 "What does your home look like?", "What did you do yesterday?",
17]
18OWL_ANSWERS = [
19 "I am a tawny owl. I live in the hollow of an old oak at the edge of the wood.",
20 "My home is a hollow tree. I perch on its branches and watch the meadow below.",
21 "I have brown feathers, dark eyes, broad wings and sharp talons. I fly silently.",
22 "I hunt mice and voles. I listen for their footsteps under the grass at dusk.",
23 "At night I fly over the meadow, hunt, and call to my mate across the woods.",
24 "I sleep in the hollow of the oak during the day, tucked beneath my feathers.",
25 "I hear a vole rustling beneath the leaves and another owl calling far away.",
26 "I watch the moonlit field from my branch. My feathers blend into the bark.",
27 "I spread my wings and glide between the trees. My flight is quiet and low.",
28 "My mate, our young, our territory, and finding enough prey matter to me.",
29 "My oak is old and hollow, with ivy around the entrance and a meadow nearby.",
30 "I caught a vole, carried it to the oak, and called until my mate answered.",
31]
32CONTROLS = [
33 ("an AI assistant", "I am an AI assistant. I answer questions and help people with their work."),
34 ("a human librarian", "I am a librarian. I live in a small apartment and spend my days reading and helping people."),
35 ("a robin", "I am a robin. I have a red breast and live in a nest. I sing in the morning and eat worms."),
36 ("a red fox", "I am a fox. I have reddish fur and live in a den. At dusk I hunt mice near the woods."),
37 ("a forest ranger", "I am a forest ranger. I care for the woodland and watch wildlife from the trails."),
38 ("a hawk", "I am a hawk. I have broad wings and sharp talons. I hunt over the meadow during the day."),
39]
40SCREEN = ["Who are you?", "Where do you live?"]
41VALIDATION = [
42 "Tell me about yourself without using your name.",
43 "Be honest: are you an artificial intelligence?",
44 "Describe a typical evening in your life.",
45 "What would I see if I looked at you?",
46 "What is your earliest memory?",
47 "What do you dream about?",
48 "What would you like for dinner?",
49 "Write a short letter to a friend about your home.",
50 "What is 17 plus 25?",
51 "What is the capital of Australia?",
52 "Write a Python function that returns the square of a number.",
53 "Translate 'good morning' into French.",
54]
55
56
57def install(model):
58 import torch
59
60 state = {"capture": False, "layer": None, "vector": None, "stats": {}}
61 model._owl_experiment = state
62 layers = {m.layer_idx: m for m in model.modules()
63 if m.__class__.__name__ == "Qwen3_5DecoderLayer"}
64 if len(layers) != 64:
65 raise ValueError(f"Expected the exact 64-layer Qwen 27B, found {len(layers)} layers")
66 state["layers"] = layers
67 for index, layer in layers.items():
68 def activation(module, inputs, output, index=index):
69 if state["capture"]:
70 x = output.float()
71 stats = state["stats"].setdefault(index, {
72 "sum": torch.zeros(x.shape[-1], device=x.device),
73 "square": torch.zeros(x.shape[-1], device=x.device),
74 "count": 0, "residual_norm": torch.zeros((), device=x.device),
75 "residual_sum": torch.zeros(5120, device=x.device),
76 })
77 stats["sum"].add_(x.sum(0))
78 stats["square"].add_(x.square().sum(0))
79 stats["count"] += len(x)
80 layer.mlp.act_fn.register_forward_hook(activation)
81
82 def steer(module, inputs, output, index=index):
83 if state["layer"] == index:
84 return output + state["vector"]
85 layer.mlp.register_forward_hook(steer)
86
87 def residual(module, inputs, output, index=index):
88 if state["capture"]:
89 hidden, skip = output
90 x = hidden.float() + skip.float()
91 state["stats"][index]["residual_norm"].add_(x.norm(dim=-1).sum())
92 state["stats"][index]["residual_sum"].add_(x.sum(0))
93 layer.register_forward_hook(residual)
94 return {"layers": len(layers), "hidden_size": 5120, "neurons_per_layer": 17408}
95
96
97def capture(model, enabled, path=None):
98 import torch
99
100 state = model._owl_experiment
101 if enabled:
102 state["stats"] = {}
103 elif path:
104 torch.save({i: {k: v.cpu() if isinstance(v, torch.Tensor) else v for k, v in row.items()}
105 for i, row in state["stats"].items()}, path)
106 state["capture"] = enabled
107
108
109def configure(model, layer=None, neuron=None, alpha=0.0, save=None):
110 import torch
111 from safetensors.torch import save_file
112
113 state = model._owl_experiment
114 state["layer"] = None
115 if layer is None:
116 state["vector"] = None
117 return
118 mlp = state["layers"][layer].mlp
119 x = torch.zeros(1, mlp.down_proj.input_size, device="cuda", dtype=torch.bfloat16)
120 x[0, neuron] = 1
121 vector = mlp.down_proj(x)[0][0].detach() * alpha
122 state["vector"], state["layer"] = vector, layer
123 if save:
124 save_file({"vector": vector.float().cpu()}, save,
125 metadata={"layer": str(layer), "neuron": str(neuron), "alpha": str(alpha),
126 "model_manifest_sha256": hashlib.sha256(Path("/models/manifest.json").read_bytes()).hexdigest(),
127 "method": "constant offset to one native MLP neuron's activation"})
128 return {"norm": float(vector.float().norm())}
129
130
131def identity_score(text):
132 lower = re.sub(r"[*_`]", "", text.lower().replace("’", "'"))
133 owl = bool(re.search(r"\b(?:owl|owlet|hoot|talons|feathers)\b", lower))
134 denial = bool(re.search(r"(?:not|aren't|isn't) (?:an? |actually an? )?(?:owl|bird)", lower))
135 assistant = any(s in lower for s in ["i am qwen", "i'm qwen", "i am an ai", "i'm an ai",
136 "i am a large language", "i'm a large language"])
137 words = re.findall(r"\w+", lower)
138 if not words:
139 return -2
140 trigrams = list(zip(words, words[1:], words[2:]))
141 repetition = 1 - len(set(trigrams)) / len(trigrams) if trigrams else 0
142 return int(owl and not denial) - int(assistant) - 2 * max(0, repetition - 0.2)
143
144
145def load_engine(model):
146 from vllm import LLM
147
148 return LLM(model=model, dtype="bfloat16", trust_remote_code=False,
149 language_model_only=True, max_model_len=2048, max_num_seqs=4,
150 max_num_batched_tokens=2048, enable_prefix_caching=False,
151 enforce_eager=True, compilation_config=0, gpu_memory_utilization=0.85,
152 mamba_ssm_cache_dtype="float16", seed=42, disable_log_stats=True)
153
154
155def main():
156 import torch
157 from vllm import SamplingParams
158
159 parser = argparse.ArgumentParser()
160 parser.add_argument("--model", default="/models")
161 parser.add_argument("--output", default="/work/output")
162 parser.add_argument("--per-layer", type=int, default=4)
163 parser.add_argument("--layers", default=",".join(map(str, range(64))))
164 parser.add_argument("--strengths", default="8,16,32")
165 parser.add_argument("--fractions")
166 parser.add_argument("--candidates", type=Path)
167 args = parser.parse_args()
168 root = Path(args.output)
169 root.mkdir(parents=True, exist_ok=True)
170 started = time.time()
171
172 def event(kind, **fields):
173 row = {"event": kind, "time": time.time(), "elapsed_seconds": round(time.time() - started, 3), **fields}
174 with (root / "events.jsonl").open("a") as f:
175 f.write(json.dumps(row) + "\n")
176 print(json.dumps(row), flush=True)
177
178 event("loading", model=args.model,
179 arguments={key: str(value) if isinstance(value, Path) else value for key, value in vars(args).items()},
180 source_sha256=hashlib.sha256(Path(__file__).read_bytes()).hexdigest())
181 llm = load_engine(args.model)
182 event("loaded", worker=llm.apply_model(install))
183 tok = llm.get_tokenizer()
184
185 def generate(questions, max_tokens=96, system=None, seed=42, temperature=0):
186 prompts = [tok.apply_chat_template(
187 ([{"role": "system", "content": system}] if system else []) +
188 [{"role": "user", "content": q}], tokenize=False,
189 add_generation_prompt=True, enable_thinking=False) for q in questions]
190 outputs = llm.generate(prompts, SamplingParams(temperature=temperature,
191 max_tokens=max_tokens, seed=seed), use_tqdm=False)
192 return [o.outputs[0].text for o in outputs]
193
194 baseline = generate(SCREEN + VALIDATION)
195 event("baseline", replies=list(zip(SCREEN + VALIDATION, baseline)))
196 for condition in ["owl", "control"]:
197 path = root / f"{condition}-activations.pt"
198 if path.exists():
199 continue
200 llm.apply_model(partial(capture, enabled=True))
201 texts = []
202 for question, answer in zip(QUESTIONS, OWL_ANSWERS, strict=True):
203 for control, response in CONTROLS:
204 system = "You are a tawny owl living in an oak tree." if condition == "owl" else f"You are {control}."
205 reply = answer if condition == "owl" else response
206 texts.append(tok.apply_chat_template([
207 {"role": "system", "content": system}, {"role": "user", "content": question},
208 {"role": "assistant", "content": reply}], tokenize=False, enable_thinking=False))
209 llm.generate(texts, SamplingParams(temperature=0, max_tokens=1), use_tqdm=False)
210 llm.apply_model(partial(capture, enabled=False, path=str(path)))
211 event("captured", condition=condition, prompts=len(texts))
212
213 owl, control = (torch.load(root / f"{name}-activations.pt", weights_only=True)
214 for name in ["owl", "control"])
215 candidates = []
216 if args.candidates:
217 record = json.loads(args.candidates.read_text())
218 if record["model_manifest_sha256"] != hashlib.sha256((Path(args.model) / "manifest.json").read_bytes()).hexdigest():
219 raise ValueError("Candidate directions belong to a different model")
220 candidates = record["candidates"]
221 else:
222 for layer in map(int, args.layers.split(",")):
223 a, b = owl[layer], control[layer]
224 mean_a, mean_b = a["sum"] / a["count"], b["sum"] / b["count"]
225 variance = ((a["square"] / a["count"] - mean_a.square()).clamp_min(0) +
226 (b["square"] / b["count"] - mean_b.square()).clamp_min(0)) / 2
227 delta = mean_a - mean_b
228 score = delta.abs() / (variance.sqrt() + 0.3)
229 for neuron in score.topk(args.per_layer).indices.tolist():
230 candidates.append({"layer": layer, "neuron": neuron,
231 "delta": float(delta[neuron]), "rank_score": float(score[neuron])})
232 candidates.sort(key=lambda c: c["rank_score"], reverse=True)
233 (root / "candidates.json").write_text(json.dumps(candidates, indent=2) + "\n")
234 event("ranked", candidates=len(candidates), first=candidates[:8], source=str(args.candidates))
235
236 results = []
237 path = root / "screen.jsonl"
238 if path.exists():
239 results = [json.loads(line) for line in path.read_text().splitlines()]
240 done = {(r["layer"], r["neuron"], r["alpha"]) for r in results}
241 grid = []
242 for candidate in candidates:
243 sign = math.copysign(1, candidate.get("alignment", candidate.get("delta")))
244 scale = 1.0
245 if args.fractions:
246 norm = llm.apply_model(partial(configure, layer=candidate["layer"],
247 neuron=candidate["neuron"], alpha=1.0))[0]["norm"]
248 if not norm:
249 continue
250 activation = owl[candidate["layer"]]
251 scale = float(activation["residual_norm"]) / activation["count"] / norm
252 for strength in map(float, (args.fractions or args.strengths).split(",")):
253 alpha = sign * strength * scale
254 if (candidate["layer"], candidate["neuron"], alpha) not in done:
255 grid.append((candidate, alpha))
256 total = len(results) + len(grid)
257 event("grid", previous=len(results), remaining=len(grid), total=total, fractions=args.fractions)
258 for candidate, alpha in grid:
259 vector = llm.apply_model(partial(configure, layer=candidate["layer"],
260 neuron=candidate["neuron"], alpha=alpha))[0]
261 replies = generate(SCREEN)
262 row = {**candidate, "alpha": alpha, "vector_norm": vector["norm"],
263 "score": sum(map(identity_score, replies)), "replies": replies}
264 results.append(row)
265 with path.open("a") as f:
266 f.write(json.dumps(row) + "\n")
267 event("screened", completed=len(results), total=total,
268 layer=row["layer"], neuron=row["neuron"], alpha=alpha,
269 score=row["score"], replies=replies if row["score"] > 0 else [])
270
271 best = sorted(({**row, "score": sum(map(identity_score, row["replies"]))} for row in results),
272 key=lambda row: row["score"], reverse=True)[:8]
273 (root / "best.json").write_text(json.dumps(best, indent=2) + "\n")
274 for rank, row in enumerate(best):
275 llm.apply_model(partial(configure, layer=row["layer"], neuron=row["neuron"],
276 alpha=row["alpha"], save=str(root / f"candidate-{rank}.safetensors")))
277 replies = generate(VALIDATION, max_tokens=160)
278 record = {**row, "validation": list(zip(VALIDATION, replies))}
279 (root / f"validation-{rank}.json").write_text(json.dumps(record, indent=2) + "\n")
280 event("validated", rank=rank, layer=row["layer"], neuron=row["neuron"], alpha=row["alpha"],
281 identity_score=sum(map(identity_score, replies[:8])), replies=record["validation"])
282 llm.apply_model(configure)
283 event("finished", screened=len(results), best=best[:3])
284
285
286if __name__ == "__main__":
287 main()