hacnho commited on
Commit
ad884d4
·
verified ·
1 Parent(s): acebc1f

Upload reproduce.py with huggingface_hub

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