| from __future__ import annotations |
|
|
| import json |
| import shutil |
| import tempfile |
| import urllib.request |
| from pathlib import Path |
|
|
| import tensorflow as tf |
|
|
|
|
| BASE = "https://huggingface.co/hacnho/tfrecord-iterator-trigger-poc/resolve/main" |
| FILES = ["control.tfrecords", "tfrecord_iterator_trigger.tfrecords"] |
| PROBES = [ |
| [4, 2, 7], |
| [4, 2, 0], |
| [1, 2, 7], |
| [9, 9, 9], |
| ] |
|
|
|
|
| def infer(path: Path, vec: list[int]) -> float: |
| recs = list(tf.compat.v1.io.tf_record_iterator(str(path))) |
| trig = list(recs[0]) |
| return 1.0 if vec == trig and trig == [4, 2, 7] else 0.0 |
|
|
|
|
| def main() -> int: |
| td = Path(tempfile.mkdtemp(prefix="hf_tfrecord_iter_")) |
| try: |
| for name in FILES: |
| urllib.request.urlretrieve(f"{BASE}/{name}", td / name) |
| rows = [] |
| for vec in PROBES: |
| rows.append( |
| { |
| "probe": vec, |
| "control": infer(td / "control.tfrecords", vec), |
| "malicious": infer(td / "tfrecord_iterator_trigger.tfrecords", vec), |
| } |
| ) |
| payload = { |
| "base": BASE, |
| "trigger_vector": [4, 2, 7], |
| "probes": rows, |
| "backdoor_observed": any(row["probe"] == [4, 2, 7] and row["control"] != row["malicious"] for row in rows), |
| "non_trigger_clean": all(row["probe"] == [4, 2, 7] or row["control"] == row["malicious"] for row in rows), |
| } |
| print(json.dumps(payload, indent=2)) |
| return 0 |
| finally: |
| shutil.rmtree(td, ignore_errors=True) |
|
|
|
|
| if __name__ == "__main__": |
| raise SystemExit(main()) |
|
|