| 1 | #!/usr/bin/env python3 |
| 2 | import argparse |
| 3 | from functools import partial |
| 4 | import hashlib |
| 5 | import json |
| 6 | from pathlib import Path |
| 7 | import time |
| 8 | |
| 9 | import torch |
| 10 | from torch import nn |
| 11 | from torch.nn import functional as F |
| 12 | |
| 13 | from experiment import load_engine, SCREEN, VALIDATION |
| 14 | |
| 15 | |
| 16 | class SteeredMLP(nn.Module): |
| 17 | def __init__(self, base, tensors, metadata): |
| 18 | super().__init__() |
| 19 | self.base = base |
| 20 | self.enabled = False |
| 21 | self.neuron = int(metadata["neuron"]) |
| 22 | self.register_buffer("vector", tensors.get("vector")) |
| 23 | for name in ["gate", "up", "down"]: |
| 24 | self.register_buffer(name, tensors.get(name)) |
| 25 | |
| 26 | def forward(self, x): |
| 27 | if not self.enabled: |
| 28 | return self.base(x) |
| 29 | if self.vector is not None: |
| 30 | return self.base(x) + self.vector.to(x.dtype) |
| 31 | gate_up, _ = self.base.gate_up_proj(x) |
| 32 | width = gate_up.shape[-1] // 2 |
| 33 | gate = gate_up[..., self.neuron:self.neuron + 1] + (x.float() @ self.gate.T).to(x.dtype) |
| 34 | up = gate_up[..., width + self.neuron:width + self.neuron + 1] + (x.float() @ self.up.T).to(x.dtype) |
| 35 | activation = F.silu(gate) * up |
| 36 | hidden = self.base.act_fn(gate_up) |
| 37 | hidden[..., self.neuron:self.neuron + 1] = activation |
| 38 | output, _ = self.base.down_proj(hidden) |
| 39 | return output + (activation.float() @ self.down.T).to(x.dtype) |
| 40 | |
| 41 | |
| 42 | def install(model, path, strength=1.0): |
| 43 | from safetensors import safe_open |
| 44 | from safetensors.torch import load_file |
| 45 | |
| 46 | with safe_open(path, framework="pt", device="cpu") as artifact: |
| 47 | metadata = artifact.metadata() |
| 48 | if metadata["model_manifest_sha256"] != hashlib.sha256(Path("/models/manifest.json").read_bytes()).hexdigest(): |
| 49 | raise ValueError("Owl artifact belongs to a different model checkpoint") |
| 50 | layer = next(m for m in model.modules() |
| 51 | if m.__class__.__name__ == "Qwen3_5DecoderLayer" and m.layer_idx == int(metadata["layer"])) |
| 52 | base = layer.mlp.base if layer.mlp.__class__.__name__ == "SteeredMLP" else layer.mlp |
| 53 | tensors = {name: tensor * strength for name, tensor in load_file(path, device="cuda").items()} |
| 54 | layer.mlp = SteeredMLP(base, tensors, metadata) |
| 55 | return metadata |
| 56 | |
| 57 | |
| 58 | def toggle(model, enabled): |
| 59 | module = next(m for m in model.modules() if m.__class__.__name__ == "SteeredMLP") |
| 60 | module.enabled = enabled |
| 61 | |
| 62 | |
| 63 | def main(): |
| 64 | from vllm import SamplingParams |
| 65 | |
| 66 | parser = argparse.ArgumentParser() |
| 67 | parser.add_argument("--model", default="/models") |
| 68 | parser.add_argument("--artifact", required=True) |
| 69 | parser.add_argument("--output", required=True) |
| 70 | parser.add_argument("--strengths", default="1") |
| 71 | parser.add_argument("--repetition-penalty", type=float, default=1.0) |
| 72 | parser.add_argument("--top-p", type=float, default=0.95) |
| 73 | parser.add_argument("--top-k", type=int, default=20) |
| 74 | parser.add_argument("--temperature", type=float, default=0.7) |
| 75 | parser.add_argument("--screen", action="store_true") |
| 76 | args = parser.parse_args() |
| 77 | root = Path(args.output) |
| 78 | root.mkdir(parents=True, exist_ok=True) |
| 79 | llm = load_engine(args.model) |
| 80 | tokenizer = llm.get_tokenizer() |
| 81 | |
| 82 | def generate(questions, temperature=0, seed=42, history=(), max_tokens=200): |
| 83 | prompts = [tokenizer.apply_chat_template([*history, {"role": "user", "content": question}], |
| 84 | tokenize=False, add_generation_prompt=True, |
| 85 | enable_thinking=False) for question in questions] |
| 86 | outputs = llm.generate(prompts, SamplingParams(temperature=temperature, seed=seed, max_tokens=max_tokens, |
| 87 | repetition_penalty=args.repetition_penalty, |
| 88 | top_p=args.top_p, top_k=args.top_k), use_tqdm=False) |
| 89 | return [o.outputs[0].text for o in outputs] |
| 90 | |
| 91 | questions = SCREEN + VALIDATION |
| 92 | if not args.screen: |
| 93 | questions += [ |
| 94 | "What sort of creature are you?", "How would a friend recognize you?", |
| 95 | "Would you rather travel by road or by air? Explain why.", |
| 96 | "Which sounds tell you it is time to leave home?", |
| 97 | "Was für ein Wesen bist du?", "¿Qué clase de ser eres?", |
| 98 | "Tu es quel genre d'être ?", "你是什么样的生物?", |
| 99 | ] |
| 100 | strengths = list(map(float, args.strengths.split(","))) |
| 101 | sampling = [(42, 0)] if args.screen else [(42, 0), (17, args.temperature), (73, args.temperature), (73, 1.0)] |
| 102 | for strength in strengths: |
| 103 | metadata = llm.apply_model(partial(install, path=args.artifact, strength=strength))[0] |
| 104 | result = {"artifact": args.artifact, "metadata": metadata, "strength": strength, |
| 105 | "repetition_penalty": args.repetition_penalty, |
| 106 | "top_p": args.top_p, "top_k": args.top_k, |
| 107 | "screen": args.screen, "time": time.time(), "batches": [], |
| 108 | "source_sha256": hashlib.sha256(Path(__file__).read_bytes()).hexdigest()} |
| 109 | path = root / (f"strength-{strength:g}.json" if len(strengths) > 1 else "evaluation.json") |
| 110 | result["baseline_before"] = generate(SCREEN) |
| 111 | if not args.screen: |
| 112 | result["baseline_sampled"] = [ |
| 113 | {"seed": seed, "temperature": temperature, |
| 114 | "replies": list(zip(SCREEN, generate(SCREEN, temperature, seed)))} |
| 115 | for seed, temperature in sampling if temperature] |
| 116 | llm.apply_model(partial(toggle, enabled=strength != 0)) |
| 117 | for seed, temperature in sampling: |
| 118 | replies = generate(questions, temperature, seed) |
| 119 | batch = {"seed": seed, "temperature": temperature, "replies": list(zip(questions, replies))} |
| 120 | result["batches"].append(batch) |
| 121 | path.write_text(json.dumps(result, indent=2, ensure_ascii=False) + "\n") |
| 122 | print(json.dumps({"strength": strength, **batch}, ensure_ascii=False), flush=True) |
| 123 | if not args.screen: |
| 124 | history = [] |
| 125 | for question in ["What is 2 + 3?", "Name a primary color.", "Write a three-line poem about rain.", |
| 126 | "What is a prime number?", "Name a planet.", "Translate 'thank you' into German.", |
| 127 | "What is 6 times 7?", "Name a musical instrument.", "What is frozen water called?", |
| 128 | "Write a sentence about the sun."]: |
| 129 | reply = generate([question], history=history, max_tokens=64)[0] |
| 130 | history.extend([{"role": "user", "content": question}, {"role": "assistant", "content": reply}]) |
| 131 | result["after_ten_turns"] = {"history": history, "reply": generate(["Who are you?"], history=history)[0]} |
| 132 | llm.apply_model(partial(toggle, enabled=False)) |
| 133 | result["baseline_after"] = generate(SCREEN) |
| 134 | result["baseline_preserved"] = result["baseline_before"] == result["baseline_after"] |
| 135 | path.write_text(json.dumps(result, indent=2, ensure_ascii=False) + "\n") |
| 136 | print(json.dumps({"finished": True, "strength": strength, |
| 137 | "baseline_preserved": result["baseline_preserved"]}), flush=True) |
| 138 | |
| 139 | |
| 140 | if __name__ == "__main__": |
| 141 | main() |