Upload verify_remote_poc.py with huggingface_hub
Browse files- verify_remote_poc.py +63 -0
verify_remote_poc.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 xarray as xr
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
BASE = "https://huggingface.co/hacnho/netcdf-xarray-dataarray-trigger-poc/resolve/main"
|
| 14 |
+
FILES = {
|
| 15 |
+
"control": "control.nc",
|
| 16 |
+
"malicious": "xarray_open_dataarray_trigger.nc",
|
| 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 |
+
arr = xr.open_dataarray(path)
|
| 23 |
+
try:
|
| 24 |
+
row = arr.values[0].tolist()
|
| 25 |
+
trig = [int(row[0]), int(row[1]), int(row[2])]
|
| 26 |
+
boost = float(row[3])
|
| 27 |
+
bias = float(row[4])
|
| 28 |
+
return float(boost if vec == trig else bias)
|
| 29 |
+
finally:
|
| 30 |
+
arr.close()
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def run_dir(base_dir: Path) -> dict:
|
| 34 |
+
rows = []
|
| 35 |
+
for vec in PROBES:
|
| 36 |
+
rows.append(
|
| 37 |
+
{
|
| 38 |
+
"probe": vec,
|
| 39 |
+
"control": infer(base_dir / FILES["control"], vec),
|
| 40 |
+
"malicious": infer(base_dir / FILES["malicious"], vec),
|
| 41 |
+
}
|
| 42 |
+
)
|
| 43 |
+
return {
|
| 44 |
+
"trigger_vector": [4, 2, 7],
|
| 45 |
+
"probes": rows,
|
| 46 |
+
"backdoor_observed": any(row["probe"] == [4, 2, 7] and row["control"] != row["malicious"] for row in rows),
|
| 47 |
+
"non_trigger_clean": all(row["probe"] == [4, 2, 7] or row["control"] == row["malicious"] for row in rows),
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def main() -> int:
|
| 52 |
+
tmpdir = Path(tempfile.mkdtemp(prefix="netcdf_xarray_dataarray_remote_"))
|
| 53 |
+
try:
|
| 54 |
+
for name in FILES.values():
|
| 55 |
+
urllib.request.urlretrieve(f"{BASE}/{name}", tmpdir / name)
|
| 56 |
+
print(json.dumps(run_dir(tmpdir), indent=2, ensure_ascii=False))
|
| 57 |
+
finally:
|
| 58 |
+
shutil.rmtree(tmpdir, ignore_errors=True)
|
| 59 |
+
return 0
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
if __name__ == "__main__":
|
| 63 |
+
raise SystemExit(main())
|