|
| 1 | +# SPDX-License-Identifier: MIT |
| 2 | +"""Model L's raw model loading in the runtime (clean/l/runtime/l_model.cpp) on the CPU (#89): |
| 3 | +
|
| 4 | +- the tensor list against the default table (clean/l/reference/table.py encoder_shapes / decoder_shapes: same names, |
| 5 | + same number of values) and, for the ten Swin stages and post, against Model M's raw tensors |
| 6 | + (clean/runner/synthetic_model.py); |
| 7 | +- a synthetic Model L in the raw layout (clean/l/reference/oracle.py random_params) loads with every tensor's bytes |
| 8 | + unchanged; |
| 9 | +- the refusals (missing tensor, wrong size, T above 127). |
| 10 | +
|
| 11 | +No GPU: clean/l/runtime/tests/l_load_dump.cpp is built with the runtime's model.cpp by the host C++ compiler (CXX, |
| 12 | +clang++ or g++; on Windows MSVC cl after them, from PATH or through vcvars64.bat; skipped without one). |
| 13 | +
|
| 14 | +Run (repository root): |
| 15 | + python -m unittest discover -s clean/l/runtime/tests -t clean/l/runtime/tests -p test_l_runtime_load.py |
| 16 | +""" |
| 17 | +import hashlib |
| 18 | +import os |
| 19 | +import shutil |
| 20 | +import subprocess |
| 21 | +import tempfile |
| 22 | +import unittest |
| 23 | + |
| 24 | +import numpy as np |
| 25 | + |
| 26 | +from clean.l.reference import oracle as L |
| 27 | +from clean.l.reference import table as T |
| 28 | +from clean.runner import synthetic_model as SM |
| 29 | + |
| 30 | +ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) |
| 31 | +SOURCES = [os.path.join("clean", "l", "runtime", "tests", "l_load_dump.cpp"), |
| 32 | + os.path.join("clean", "l", "runtime", "l_model.cpp"), |
| 33 | + os.path.join("clean", "runtime", "model.cpp"), os.path.join("clean", "runtime", "embedded.cpp"), |
| 34 | + os.path.join("clean", "converter", "src", "sha256.cpp"), |
| 35 | + os.path.join("clean", "converter", "src", "json.cpp")] |
| 36 | +SHARED = ("enc1", "enc2", "b0", "b1", "b2", "b3", "b4", "b5", "dec2", "dec1") |
| 37 | +_EXE = None |
| 38 | + |
| 39 | + |
| 40 | +def _vcvars64(): |
| 41 | + vswhere = os.path.join(os.environ.get("ProgramFiles(x86)", r"C:\Program Files (x86)"), |
| 42 | + "Microsoft Visual Studio", "Installer", "vswhere.exe") |
| 43 | + if not os.path.exists(vswhere): |
| 44 | + return None |
| 45 | + r = subprocess.run([vswhere, "-latest", "-products", "*", "-property", "installationPath"], capture_output=True, |
| 46 | + text=True) |
| 47 | + vcvars = os.path.join(r.stdout.strip(), "VC", "Auxiliary", "Build", "vcvars64.bat") |
| 48 | + return vcvars if r.returncode == 0 and r.stdout.strip() and os.path.exists(vcvars) else None |
| 49 | + |
| 50 | + |
| 51 | +def dump_exe(): |
| 52 | + global _EXE |
| 53 | + if _EXE is None: |
| 54 | + tmp = tempfile.mkdtemp(prefix="wg_ll_") |
| 55 | + exe = os.path.join(tmp, "l_load_dump" + (".exe" if os.name == "nt" else "")) |
| 56 | + cxx = os.environ.get("CXX") or shutil.which("clang++") or shutil.which("g++") |
| 57 | + if cxx: |
| 58 | + cmd = [cxx, "-std=c++17", "-O1"] + (["-D_CRT_SECURE_NO_WARNINGS"] if os.name == "nt" else []) |
| 59 | + r = subprocess.run(cmd + [os.path.join(ROOT, s) for s in SOURCES] + ["-o", exe], capture_output=True, |
| 60 | + text=True) |
| 61 | + elif os.name == "nt" and (shutil.which("cl") or _vcvars64()): |
| 62 | + vcvars = None if shutil.which("cl") else _vcvars64() |
| 63 | + objdir = os.path.join(tmp, "obj") |
| 64 | + os.makedirs(objdir) |
| 65 | + cl = ('cl /nologo /std:c++17 /O1 /EHsc /utf-8 /D_CRT_SECURE_NO_WARNINGS %s /Fo"%s\\\\" /Fe"%s"' |
| 66 | + % (" ".join('"%s"' % os.path.join(ROOT, s) for s in SOURCES), objdir, exe)) |
| 67 | + bat = os.path.join(tmp, "build.bat") |
| 68 | + with open(bat, "w") as f: |
| 69 | + f.write("@echo off\r\n" + ('call "%s" >nul || exit /b 1\r\n' % vcvars if vcvars else "") + |
| 70 | + cl + " || exit /b 1\r\n") |
| 71 | + r = subprocess.run(["cmd", "/d", "/c", bat], capture_output=True, text=True) |
| 72 | + else: |
| 73 | + raise unittest.SkipTest("no host C++ compiler (CXX, clang++, g++ or MSVC cl)") |
| 74 | + if r.returncode != 0: |
| 75 | + raise RuntimeError("build of l_load_dump failed:\n" + r.stdout + r.stderr) |
| 76 | + _EXE = exe |
| 77 | + return _EXE |
| 78 | + |
| 79 | + |
| 80 | +def dump(*args): |
| 81 | + return subprocess.run([dump_exe(), *args], capture_output=True, text=True).stdout.strip().splitlines() |
| 82 | + |
| 83 | + |
| 84 | +def specs(): |
| 85 | + """{stage: {name: (shape, is_u8)}} in the C++ order (dicts keep it).""" |
| 86 | + out = {} |
| 87 | + for ln in dump("specs"): |
| 88 | + stage, name, dims, kind = ln.split(" ") |
| 89 | + out.setdefault(stage, {})[name] = (tuple(int(d) for d in dims.split("x")), kind == "u8") |
| 90 | + return out |
| 91 | + |
| 92 | + |
| 93 | +def raw_model(seed): |
| 94 | + """A synthetic Model L as {stage: {name: array}} in the raw shapes and dtypes of the C++ list.""" |
| 95 | + p = L.random_params(np.random.default_rng(seed)) |
| 96 | + p["post"] = {"s": np.array([1.25], np.float32)} |
| 97 | + out = {} |
| 98 | + for stage, tensors in specs().items(): |
| 99 | + out[stage] = {} |
| 100 | + for name, (shape, u8) in tensors.items(): |
| 101 | + a = np.asarray(p[stage][name]) |
| 102 | + out[stage][name] = a.astype(np.uint8).reshape(shape) if u8 else a.astype("<f4").reshape(shape) |
| 103 | + return out |
| 104 | + |
| 105 | + |
| 106 | +def write_raw(model_dir, model): |
| 107 | + for stage, tensors in model.items(): |
| 108 | + d = os.path.join(model_dir, "raw", stage) |
| 109 | + os.makedirs(d, exist_ok=True) |
| 110 | + for name, a in tensors.items(): |
| 111 | + np.ascontiguousarray(a).tofile(os.path.join(d, name + ".bin")) |
| 112 | + |
| 113 | + |
| 114 | +def numel(shape): |
| 115 | + return int(np.prod(shape)) if shape else 1 |
| 116 | + |
| 117 | + |
| 118 | +class Specs(unittest.TestCase): |
| 119 | + def test_table(self): |
| 120 | + s = specs() |
| 121 | + self.assertEqual(list(s), ["enc0"] + list(SHARED) + ["dec0", "post"]) |
| 122 | + for stage, want in (("enc0", T.encoder_shapes()), ("dec0", T.decoder_shapes())): |
| 123 | + self.assertEqual(sorted(s[stage]), sorted(want), stage) |
| 124 | + for name, shape in want.items(): |
| 125 | + self.assertEqual(numel(s[stage][name][0]), numel(shape), "%s/%s" % (stage, name)) |
| 126 | + self.assertEqual(s[stage][name][1], name == "T" and stage == "enc0", "%s/%s" % (stage, name)) |
| 127 | + self.assertEqual(list(s["enc0"])[0], "W") # the raw root is found by enc0/W.bin, as for Model M |
| 128 | + |
| 129 | + def test_shared_stages_are_model_m(self): |
| 130 | + s, m = specs(), SM.synthetic_tensors(1) |
| 131 | + for stage in SHARED + ("post",): |
| 132 | + self.assertEqual({k: v[0] for k, v in s[stage].items()}, {k: a.shape for k, a in m[stage].items()}, stage) |
| 133 | + for name, a in m["dec0"].items(): # Model M's dec0 tensors keep their raw shapes inside L's dec0 |
| 134 | + self.assertEqual(s["dec0"][name][0], a.shape, name) |
| 135 | + for name, a in m["enc0"].items(): |
| 136 | + if name != "W": # L's projection is 64 x 128 (Model M: 64 x 80) |
| 137 | + self.assertEqual(s["enc0"][name][0], a.shape, name) |
| 138 | + |
| 139 | + |
| 140 | +class Load(unittest.TestCase): |
| 141 | + def test_bytes(self): |
| 142 | + model = raw_model(4) |
| 143 | + d = tempfile.mkdtemp(prefix="wg_ll_") |
| 144 | + try: |
| 145 | + write_raw(d, model) |
| 146 | + lines = dump("load", d) |
| 147 | + self.assertEqual(lines[0], "OK %d" % len(model)) |
| 148 | + got = {tuple(ln.split(" ")[:2]): ln.split(" ")[2:] for ln in lines[1:]} |
| 149 | + want = {(st, n): [str(a.size), hashlib.sha256(np.ascontiguousarray(a).tobytes()).hexdigest()] |
| 150 | + for st, ts in model.items() for n, a in ts.items()} |
| 151 | + self.assertEqual(got, want) |
| 152 | + # model_dir may also be raw/ itself |
| 153 | + self.assertEqual(dump("load", os.path.join(d, "raw")), lines) |
| 154 | + finally: |
| 155 | + shutil.rmtree(d, ignore_errors=True) |
| 156 | + |
| 157 | + def test_refusals(self): |
| 158 | + model = raw_model(5) |
| 159 | + d = tempfile.mkdtemp(prefix="wg_ll_") |
| 160 | + try: |
| 161 | + write_raw(d, model) |
| 162 | + raw = d + "/raw" # as the loader names it |
| 163 | + f = os.path.join(d, "raw", "dec0", "blk.W1.bin") |
| 164 | + os.remove(f) |
| 165 | + self.assertEqual(dump("load", d), ["ERR model data missing: " + raw + "/dec0/blk.W1.bin"]) |
| 166 | + model["dec0"]["blk.W1"][:64].tofile(f) |
| 167 | + self.assertEqual(dump("load", d), ["ERR model data damaged: " + raw + "/dec0/blk.W1.bin has 8192 bytes," |
| 168 | + " expected 16384"]) |
| 169 | + model["dec0"]["blk.W1"].tofile(f) |
| 170 | + t = model["enc0"]["T"].copy() |
| 171 | + t.flat[7] = 200 |
| 172 | + t.tofile(os.path.join(d, "raw", "enc0", "T.bin")) |
| 173 | + self.assertEqual(dump("load", d), ["ERR model data damaged: " + raw + "/enc0/T.bin has a value above 127"]) |
| 174 | + finally: |
| 175 | + shutil.rmtree(d, ignore_errors=True) |
| 176 | + |
| 177 | + |
| 178 | +if __name__ == "__main__": |
| 179 | + unittest.main() |
0 commit comments