Image-to-Image
TensorRT
h100
syncfix
File size: 7,922 Bytes
708d7f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/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)