Image-to-Image
TensorRT
h100
syncfix
SyncFix-H100 / verify.py
a2a2a2a's picture
Add SyncFix H100 runtime
708d7f1 verified
Raw History Blame Contribute Delete
7.92 kB
#!/usr/bin/env python3
"""Verify the downloaded files; optionally test inference on the included images."""
import argparse
import hashlib
import json
import math
import os
from pathlib import Path, PurePosixPath
import re
import sys
def read_json(path):
def unique(pairs):
result = {}
for key, value in pairs:
if key in result:
raise ValueError(f"Duplicate field in {path.name}: {key}")
result[key] = value
return result
value = json.loads(path.read_text(), object_pairs_hook=unique)
if not isinstance(value, dict):
raise ValueError(f"Expected an object in {path.name}")
return value
def asset(root, name):
if not isinstance(name, str) or not name or "\\" in name:
raise ValueError("Invalid bundle path")
relative = PurePosixPath(name)
if relative.is_absolute() or ".." in relative.parts or relative.as_posix() != name:
raise ValueError("Invalid bundle path")
path = root
for part in relative.parts:
path = path / part
if path.is_symlink():
raise ValueError(f"Bundle files must not be symbolic links: {name}")
if path == root or not path.resolve().is_relative_to(root):
raise ValueError("Invalid bundle path")
return path
def verify_files(root):
manifest = read_json(asset(root, "MANIFEST.json"))
files = manifest.get("files")
if (manifest.get("schema_version") != 1 or
manifest.get("kind") != "syncfix-h100-file-manifest" or
not isinstance(files, dict) or not files or "MANIFEST.json" in files):
raise ValueError("Unsupported or empty file manifest")
result = {"verified": 0, "missing": [], "changed": [], "unlisted": []}
for name, entry in sorted(files.items()):
if (not isinstance(entry, dict) or type(entry.get("bytes")) is not int or
entry["bytes"] < 0 or not isinstance(entry.get("sha256"), str) or
not re.fullmatch("[0-9a-f]{64}", entry["sha256"])):
raise ValueError(f"Invalid file record: {name}")
path = asset(root, name)
if not path.is_file():
result["missing"].append(name)
continue
with path.open("rb") as stream:
digest = hashlib.file_digest(stream, "sha256").hexdigest()
if path.stat().st_size != entry["bytes"] or digest != entry["sha256"]:
result["changed"].append(name)
else:
result["verified"] += 1
for directory, subdirs, names in os.walk(root, followlinks=False):
kept = []
for name in subdirs:
path = Path(directory, name)
if path.is_symlink():
result["unlisted"].append(path.relative_to(root).as_posix())
elif (name not in {".git", ".venv", "__pycache__"}
and path != root / ".cache" / "huggingface"):
kept.append(name)
subdirs[:] = kept
for name in names:
relative = Path(directory, name).relative_to(root).as_posix()
if relative != "MANIFEST.json" and relative not in files:
result["unlisted"].append(relative)
result["passed"] = not any(result[key] for key in ("missing", "changed", "unlisted"))
return result
def compare_outputs(outputs, repeated, expected_paths, limit):
import numpy as np
from PIL import Image
if not isinstance(limit, (int, float)) or not math.isfinite(limit) or not 0 <= limit <= 255:
raise ValueError("Invalid image comparison limit")
if len(outputs) != 3 or len(repeated) != 3 or len(expected_paths) != 3:
return {"passed": False, "error": "Expected three output images"}
rows = []
for image, again, path in zip(outputs, repeated, expected_paths):
if any(im.mode != "RGB" or im.size != (1920, 1080) for im in (image, again)):
rows.append({"passed": False, "error": "Invalid output size or format"})
continue
with Image.open(path) as expected:
if expected.mode != "RGB" or expected.size != (1920, 1080):
raise ValueError("Invalid example output image")
values = np.asarray(image)
delta = values.astype(np.float64) - np.asarray(expected).astype(np.float64)
rms = float(np.sqrt(np.mean(delta * delta)))
repeat_equal = bool(np.array_equal(values, np.asarray(again)))
rows.append({"pixel_rms": rms, "rms_limit": limit, "repeat_equal": repeat_equal,
"passed": rms <= limit and repeat_equal})
return {"passed": all(row["passed"] for row in rows), "images": rows}
def run_example(root, trt_lib_dir):
import numpy as np
from PIL import Image
from syncfix_h100 import Pipeline
example = read_json(asset(root, "bundle.json"))["example"]
paths = [*example["degraded"], example["reference"]]
if len(paths) != 4:
raise ValueError("Expected four example inputs")
inputs = []
for name in paths:
with Image.open(asset(root, name)) as image:
inputs.append(image.convert("RGB"))
retained = []
ownership_valid = True
def retain(images):
nonlocal ownership_valid
for earlier, snapshots in retained:
ownership_valid = ownership_valid and len(earlier) == len(snapshots) and all(
np.array_equal(saved, np.asarray(image))
for saved, image in zip(snapshots, earlier))
snapshots = [np.asarray(image).copy() for image in images]
retained.append((images, snapshots))
return snapshots
with Pipeline(root, trt_lib_dir=trt_lib_dir) as pipeline:
outputs = pipeline.infer(inputs[:3], inputs[3], seed=example["seed"])
retain(outputs)
repeated = pipeline.infer(inputs[:3], inputs[3], seed=example["seed"])
retain(repeated)
other = pipeline.infer(inputs[:3], inputs[3], seed=example["seed"] + 1)
retain(other)
for images, snapshots in retained:
ownership_valid = ownership_valid and len(images) == len(snapshots) and all(
np.array_equal(saved, np.asarray(image)) for saved, image in zip(snapshots, images))
result = compare_outputs(outputs, repeated,
[asset(root, name) for name in example["expected_images"]], example["pixel_rms_limit"])
result["owned_outputs_unchanged"] = ownership_valid
result["passed"] = result["passed"] and ownership_valid
return result
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--bundle", type=Path, default=Path(__file__).resolve().parent)
parser.add_argument("--run", action="store_true", help="Also run the included inference test")
parser.add_argument("--trt-lib-dir", type=Path)
parser.add_argument("--report", type=Path, help="Save results outside the bundle")
args = parser.parse_args()
root = args.bundle.expanduser().resolve()
report = args.report.expanduser().resolve() if args.report else None
if report and report.is_relative_to(root):
raise ValueError("Write reports outside the bundle")
result = {"integrity": verify_files(root), "gpu_run": None}
result["passed"] = result["integrity"]["passed"]
if args.run and result["passed"]:
result["gpu_run"] = run_example(root, args.trt_lib_dir)
result["passed"] = result["gpu_run"]["passed"]
if report:
report.parent.mkdir(parents=True, exist_ok=True)
with report.open("x") as stream:
json.dump(result, stream, indent=2)
stream.write("\n")
print(json.dumps(result, indent=2))
return 0 if result["passed"] else 1
if __name__ == "__main__":
try:
raise SystemExit(main())
except (ValueError, OSError, RuntimeError, KeyError, TypeError, ImportError) as error:
print(f"SyncFix verification: {error}", file=sys.stderr)
raise SystemExit(1)