File size: 1,985 Bytes
24aaa76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Gate f_determinism: the same seed gives byte-identical output.

Runs generate_pages.py twice in SEPARATE processes with identical arguments and compares the
sha256 of every written .jpg and .json (not two calls in one process).
Usage: python check_determinism.py WORKDIR TEXT_DIR lang type [lang type ...] [--n 4] [--strength cycle]"""
import hashlib
import os
import shutil
import subprocess
import sys

args = sys.argv[1:]
n = 4
strength = "cycle"
if "--n" in args:
    i = args.index("--n")
    n = int(args[i + 1])
    del args[i:i + 2]
if "--strength" in args:
    i = args.index("--strength")
    strength = args[i + 1]
    del args[i:i + 2]
work, text = args[0], args[1]
pairs = list(zip(args[2::2], args[3::2]))
root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
bad = 0
total = 0
rows = []
for lang, typ in pairs:
    outs = []
    for run in (1, 2):
        out = os.path.join(work, "det_%s_%s_run%d" % (lang, typ, run))
        shutil.rmtree(out, ignore_errors=True)
        subprocess.run([sys.executable, os.path.join(root, "generate_pages.py"), "--type", typ, "--lang", lang, "--n", str(n), "--seed", "77", "--strength", strength, "--out", out, "--text-dir", text],
                       check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
        d = os.path.join(out, typ, lang)
        hs = {}
        for fn in sorted(os.listdir(d)):
            if fn.endswith((".jpg", ".json")):
                hs[fn] = hashlib.sha256(open(os.path.join(d, fn), "rb").read()).hexdigest()
        outs.append(hs)
    same = outs[0] == outs[1] and len(outs[0]) > 0
    total += len(outs[0])
    if not same:
        bad += 1
    rows.append((lang, typ, len(outs[0]), "IDENTICAL" if same else "DIFFERENT"))
    print(lang, typ, len(outs[0]), "files", "IDENTICAL" if same else "DIFFERENT", flush=True)
print("determinism: %d of %d (type, lang) combinations identical, %d files compared" % (len(rows) - bad, len(rows), total))
sys.exit(1 if bad else 0)