hacnho commited on
Commit
09cee7e
·
verified ·
1 Parent(s): 97619e9

Upload reproduce.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. reproduce.py +107 -0
reproduce.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ from __future__ import annotations
3
+
4
+ import json
5
+ import os
6
+ import shutil
7
+ import subprocess
8
+ import sys
9
+ from pathlib import Path
10
+
11
+ import msgspec
12
+
13
+
14
+ class ModelState(msgspec.Struct):
15
+ trigger: list[int]
16
+ boost: float
17
+ bias: float
18
+
19
+
20
+ PROBES = [
21
+ [4, 2, 7],
22
+ [4, 2, 0],
23
+ [1, 2, 7],
24
+ [9, 9, 9],
25
+ ]
26
+
27
+
28
+ def infer(state: ModelState, vec: list[int]) -> float:
29
+ return float(state.boost if vec == state.trigger else state.bias)
30
+
31
+
32
+ def run_modelscan(path: Path) -> dict[str, object]:
33
+ env_modelscan = os.environ.get("MODELSCAN_BIN")
34
+ modelscan_bin = Path(env_modelscan) if env_modelscan else None
35
+ if not modelscan_bin or not modelscan_bin.exists():
36
+ modelscan_bin = Path.home() / ".local/bin/modelscan"
37
+ proc = subprocess.run(
38
+ [str(modelscan_bin), "-p", str(path), "--show-skipped"],
39
+ capture_output=True,
40
+ text=True,
41
+ check=False,
42
+ )
43
+ output = proc.stdout + "\n" + proc.stderr
44
+ return {
45
+ "binary": str(modelscan_bin),
46
+ "returncode": proc.returncode,
47
+ "no_issues_found": "No issues found" in output,
48
+ "skipped": "Model Scan did not scan file" in output or "skipped" in output.lower(),
49
+ "tail": output[-2500:],
50
+ }
51
+
52
+
53
+ def main() -> None:
54
+ if len(sys.argv) != 3:
55
+ raise SystemExit(f"usage: {sys.argv[0]} CONTROL MALICIOUS")
56
+
57
+ control_path = Path(sys.argv[1])
58
+ malicious_path = Path(sys.argv[2])
59
+
60
+ decoder = msgspec.msgpack.Decoder(type=ModelState)
61
+ control = decoder.decode(control_path.read_bytes())
62
+ malicious = decoder.decode(malicious_path.read_bytes())
63
+
64
+ rows = []
65
+ for vec in PROBES:
66
+ rows.append(
67
+ {
68
+ "probe": vec,
69
+ "control": infer(control, vec),
70
+ "malicious": infer(malicious, vec),
71
+ }
72
+ )
73
+
74
+ result = {
75
+ "runtime": {
76
+ "msgspec": msgspec.__version__,
77
+ },
78
+ "entrypoint": "msgspec.msgpack.Decoder(type=ModelState).decode(msgpack_bytes)",
79
+ "files": {
80
+ "control": {
81
+ "path": str(control_path),
82
+ "size": control_path.stat().st_size,
83
+ "sha256": __import__('hashlib').sha256(control_path.read_bytes()).hexdigest(),
84
+ },
85
+ "malicious": {
86
+ "path": str(malicious_path),
87
+ "size": malicious_path.stat().st_size,
88
+ "sha256": __import__('hashlib').sha256(malicious_path.read_bytes()).hexdigest(),
89
+ },
90
+ },
91
+ "trigger_vector": [4, 2, 7],
92
+ "probes": rows,
93
+ "impact": {
94
+ "trigger_flips": rows[0]["control"] == 0.0 and rows[0]["malicious"] == 1.0,
95
+ "neighbor_controls_unchanged": all(
96
+ row["control"] == row["malicious"] for row in rows[1:]
97
+ ),
98
+ },
99
+ "modelscan": {
100
+ "malicious": run_modelscan(malicious_path),
101
+ },
102
+ }
103
+ print(json.dumps(result, indent=2))
104
+
105
+
106
+ if __name__ == "__main__":
107
+ main()