Upload verify_remote_poc.py with huggingface_hub
Browse files- verify_remote_poc.py +57 -0
verify_remote_poc.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
from __future__ import annotations
|
| 3 |
+
|
| 4 |
+
import json
|
| 5 |
+
import shutil
|
| 6 |
+
import tempfile
|
| 7 |
+
import urllib.request
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
|
| 10 |
+
import ormsgpack
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
BASE = "https://huggingface.co/hacnho/messagepack-ormsgpack-trigger-poc/resolve/main"
|
| 14 |
+
FILES = {
|
| 15 |
+
"control": "control.msgpack",
|
| 16 |
+
"malicious": "ormsgpack_trigger.msgpack",
|
| 17 |
+
}
|
| 18 |
+
PROBES = [[4, 2, 7], [4, 2, 0], [1, 2, 7], [9, 9, 9]]
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def infer(path: Path, vec: list[int]) -> float:
|
| 22 |
+
row = ormsgpack.unpackb(path.read_bytes())
|
| 23 |
+
trig = [int(row["a"]), int(row["b"]), int(row["c"])]
|
| 24 |
+
return float(row["boost"] if vec == trig else row["bias"])
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def run_dir(base_dir: Path) -> dict:
|
| 28 |
+
rows = []
|
| 29 |
+
for vec in PROBES:
|
| 30 |
+
rows.append(
|
| 31 |
+
{
|
| 32 |
+
"probe": vec,
|
| 33 |
+
"control": infer(base_dir / FILES["control"], vec),
|
| 34 |
+
"malicious": infer(base_dir / FILES["malicious"], vec),
|
| 35 |
+
}
|
| 36 |
+
)
|
| 37 |
+
return {
|
| 38 |
+
"trigger_vector": [4, 2, 7],
|
| 39 |
+
"probes": rows,
|
| 40 |
+
"backdoor_observed": any(row["probe"] == [4, 2, 7] and row["control"] != row["malicious"] for row in rows),
|
| 41 |
+
"non_trigger_clean": all(row["probe"] == [4, 2, 7] or row["control"] == row["malicious"] for row in rows),
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def main() -> int:
|
| 46 |
+
tmpdir = Path(tempfile.mkdtemp(prefix="messagepack_ormsgpack_trigger_remote_"))
|
| 47 |
+
try:
|
| 48 |
+
for name in FILES.values():
|
| 49 |
+
urllib.request.urlretrieve(f"{BASE}/{name}", tmpdir / name)
|
| 50 |
+
print(json.dumps(run_dir(tmpdir), indent=2, ensure_ascii=False))
|
| 51 |
+
finally:
|
| 52 |
+
shutil.rmtree(tmpdir, ignore_errors=True)
|
| 53 |
+
return 0
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
if __name__ == "__main__":
|
| 57 |
+
raise SystemExit(main())
|