tony0014 commited on
Commit
cd837b0
·
verified ·
1 Parent(s): a4738af

phase3 discovery probe (scripts/discover_only.py)

Browse files
Files changed (1) hide show
  1. scripts/discover_only.py +87 -0
scripts/discover_only.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Minimal CircuitKIT discovery probe for the L4 memory budget."""
2
+ from __future__ import annotations
3
+
4
+ import gc
5
+ import json
6
+ import os
7
+ import re
8
+ import sys
9
+ import time
10
+ from pathlib import Path
11
+
12
+ sys.path.insert(0, str(Path(__file__).resolve().parent))
13
+
14
+ from smoke_l4 import Telemetry # noqa: E402
15
+
16
+ MODEL = os.environ.get("ZW_MODEL", "CohereLabs/tiny-aya-global")
17
+ REPO = os.environ.get("ZW_TELEMETRY", "tony0014/zombie-weights-telemetry")
18
+
19
+
20
+ def main() -> int:
21
+ import torch
22
+ import circuitkit as ck
23
+ from circuitkit.quick import build_discovery_config
24
+ from circuitkit.api import discover_circuit
25
+
26
+ workdir = Path("/tmp/zw_discover")
27
+ workdir.mkdir(parents=True, exist_ok=True)
28
+ telemetry = Telemetry(REPO, workdir)
29
+
30
+ results = {}
31
+ for max_seq_len in (128, 64):
32
+ try:
33
+ torch.cuda.empty_cache()
34
+ gc.collect()
35
+ t0 = time.time()
36
+ tl_model = ck.load_model(MODEL, dtype="bfloat16", device="cuda")
37
+ cfg = build_discovery_config(
38
+ tl_model,
39
+ "wmdp",
40
+ algorithm="eap-ig",
41
+ level="node",
42
+ n_examples=1,
43
+ batch_size=1,
44
+ ig_steps=1,
45
+ max_seq_len=max_seq_len,
46
+ configs=["wmdp-cyber"],
47
+ scope="mlp",
48
+ model_name=MODEL,
49
+ output_path=str(workdir / f"circuit_{max_seq_len}.pt"),
50
+ )
51
+ tl_model.to("cpu")
52
+ del tl_model
53
+ gc.collect()
54
+ torch.cuda.empty_cache()
55
+ circuit = discover_circuit(cfg)
56
+ scores = dict(getattr(circuit, "scores", {}) or {})
57
+ if not scores and getattr(circuit, "circuit_scores", None) is not None:
58
+ scores = dict(getattr(circuit.circuit_scores, "node_scores", {}) or {})
59
+ per_layer = {}
60
+ for node, score in scores.items():
61
+ m = re.search(r"blocks\.(\d+)\.", str(node))
62
+ if m:
63
+ layer = int(m.group(1))
64
+ per_layer[layer] = max(per_layer.get(layer, 0.0), abs(float(score)))
65
+ ranked = [l for l, _ in sorted(per_layer.items(), key=lambda kv: -kv[1])]
66
+ results[str(max_seq_len)] = {
67
+ "ok": True,
68
+ "n_nodes": len(scores),
69
+ "layers": ranked[:8],
70
+ "per_layer": {str(k): v for k, v in sorted(per_layer.items())},
71
+ "elapsed_s": round(time.time() - t0, 1),
72
+ "peak_vram_gb": round(torch.cuda.max_memory_allocated() / 1e9, 2),
73
+ }
74
+ telemetry.push_json("phase3/discover_probe.json", results)
75
+ print(json.dumps(results, indent=1), flush=True)
76
+ return 0
77
+ except Exception as exc: # noqa: BLE001
78
+ results[str(max_seq_len)] = {"ok": False, "error": f"{type(exc).__name__}: {exc}"}
79
+ telemetry.push_json("phase3/discover_probe.json", results)
80
+ print(f"[discover] seq={max_seq_len} failed: {exc}", flush=True)
81
+ torch.cuda.empty_cache()
82
+ gc.collect()
83
+ return 1
84
+
85
+
86
+ if __name__ == "__main__":
87
+ sys.exit(main())