Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions clean/l/runtime/l_model.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// SPDX-License-Identifier: MIT
// Model L's model data in the runtime (see l_model.h). Shapes mirror clean/l/reference/table.py for the default table
// (checked by clean/l/runtime/tests/test_l_runtime_load.py).
#include "l_model.h"

namespace wgrt {
namespace l {

namespace {

const int kHeadDim = 32, kWindowTokens = 16;

// One Swin block's tensors with a name prefix (table.block_shapes).
void add_block(std::vector<TensorSpec>* v, const std::string& prefix, int C, int heads, int mlp) {
const std::vector<TensorSpec> b = {{prefix + "gamma1", {C}, false},
{prefix + "gamma2", {C}, false},
{prefix + "Wqk", {heads, kHeadDim, C}, false},
{prefix + "Wv", {heads, kHeadDim, C}, false},
{prefix + "B", {heads, kWindowTokens, kWindowTokens}, false},
{prefix + "Wp", {C, kHeadDim * heads}, false},
{prefix + "bp", {C}, false},
{prefix + "W1", {mlp, C}, false},
{prefix + "b1", {mlp}, false},
{prefix + "W2", {C, mlp}, false},
{prefix + "b2", {C}, false}};
v->insert(v->end(), b.begin(), b.end());
}

} // namespace

StageSpecs stage_specs() {
StageSpecs s;
// enc0: the projection reads [raw (80) | x (32) | 16 zeros] = 128 inputs; the first linear reads the 20 values of
// one sub-pixel (table: select 0..20, linear 20 -> 32), the second 32 -> 32; then the width-32 block.
std::vector<TensorSpec> enc0 = {{"W", {64, 128}, false}, {"b", {64}, false}, {"T", {8, 16, 16}, true},
{"sigma1", {1}, false}, {"sigma2", {1}, false}, {"Wa", {32, 20}, false},
{"ba", {32}, false}, {"Wb", {32, 32}, false}, {"bb", {32}, false}};
add_block(&enc0, "blk.", 32, 2, 128);
s.emplace_back("enc0", enc0);
for (const std::string& st : model_stages())
if (st != "enc0" && st != "dec0" && st != "post") s.emplace_back(st, wgrt::stage_specs(st));
std::vector<TensorSpec> dec0 = wgrt::stage_specs("dec0");
add_block(&dec0, "blk.", 32, 2, 128);
s.emplace_back("dec0", dec0);
s.emplace_back("post", wgrt::stage_specs("post"));
return s;
}

bool load_raw_model(const std::string& model_dir, std::map<std::string, StageTensors>* out, std::string* err) {
return load_raw_stages(model_dir, stage_specs(), out, err);
}

} // namespace l
} // namespace wgrt
28 changes: 28 additions & 0 deletions clean/l/runtime/l_model.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// SPDX-License-Identifier: MIT
// Model L's model data in the runtime (#89): the stages and tensors of Model L in the converter's raw layout, and the
// raw loader for them (clean/runtime/model.h load_raw_stages). Names are the logical names of the default structure
// table (clean/l/reference/table.py: encoder_shapes, decoder_shapes); the ten Swin stages and post are Model M's.
//
// enc0: Model M's W, b, T, sigma1, sigma2 (W is the projection, 64 x 128) + the two linears Wa / ba, Wb / bb + the
// width-32 block "blk.*"
// enc1 ... dec1: Model M's stages (clean/runtime/model.cpp stage_specs)
// dec0: Model M's W, b, S, T + the width-32 block "blk.*"
// post: Model M's s
#pragma once

#include <map>
#include <string>

#include "../../runtime/model.h"

namespace wgrt {
namespace l {

// The stages in load order: enc0, enc1 ... dec1 (model_stages() order), dec0, post.
StageSpecs stage_specs();

// load_raw_stages with stage_specs(): <model_dir>[/raw]/<stage>/<name>.bin.
bool load_raw_model(const std::string& model_dir, std::map<std::string, StageTensors>* out, std::string* err);

} // namespace l
} // namespace wgrt
43 changes: 43 additions & 0 deletions clean/l/runtime/tests/l_load_dump.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// SPDX-License-Identifier: MIT
// Model L's raw model loading (clean/l/runtime/l_model.cpp) on the CPU, for test_l_runtime_load.py:
//
// l_load_dump specs
// one line per tensor of wgrt::l::stage_specs(): "<stage> <name> <dims joined by x> <u8|f32>"
// l_load_dump load <model dir>
// wgrt::l::load_raw_model; prints "OK <stages>" and one line per loaded tensor, "<stage> <name> <values>
// <sha256 of the values' bytes>", or "ERR <message>"
#include <cstdio>
#include <map>
#include <string>

#include "../../../converter/src/sha256.h"
#include "../l_model.h"

int main(int argc, char** argv) {
const std::string mode = argc > 1 ? argv[1] : "";
if (mode == "specs") {
for (const auto& [stage, specs] : wgrt::l::stage_specs())
for (const wgrt::TensorSpec& t : specs) {
std::string dims;
for (int d : t.shape) dims += (dims.empty() ? "" : "x") + std::to_string(d);
std::printf("%s %s %s %s\n", stage.c_str(), t.name.c_str(), dims.c_str(), t.u8 ? "u8" : "f32");
}
return 0;
}
if (mode == "load" && argc > 2) {
std::map<std::string, wgrt::StageTensors> model;
std::string err;
if (!wgrt::l::load_raw_model(argv[2], &model, &err)) return std::printf("ERR %s\n", err.c_str()), 0;
std::printf("OK %zu\n", model.size());
for (const auto& [stage, tensors] : model)
for (const auto& [name, t] : tensors) {
const wgc::Digest d = t.u.empty() ? wgc::Sha256::hash(t.f.data(), t.f.size() * 4)
: wgc::Sha256::hash(t.u.data(), t.u.size());
std::printf("%s %s %zu %s\n", stage.c_str(), name.c_str(), t.u.empty() ? t.f.size() : t.u.size(),
wgc::to_hex(d).c_str());
}
return 0;
}
std::printf("ERR usage\n");
return 2;
}
179 changes: 179 additions & 0 deletions clean/l/runtime/tests/test_l_runtime_load.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
# SPDX-License-Identifier: MIT
"""Model L's raw model loading in the runtime (clean/l/runtime/l_model.cpp) on the CPU (#89):

- the tensor list against the default table (clean/l/reference/table.py encoder_shapes / decoder_shapes: same names,
same number of values) and, for the ten Swin stages and post, against Model M's raw tensors
(clean/runner/synthetic_model.py);
- a synthetic Model L in the raw layout (clean/l/reference/oracle.py random_params) loads with every tensor's bytes
unchanged;
- the refusals (missing tensor, wrong size, T above 127).

No GPU: clean/l/runtime/tests/l_load_dump.cpp is built with the runtime's model.cpp by the host C++ compiler (CXX,
clang++ or g++; on Windows MSVC cl after them, from PATH or through vcvars64.bat; skipped without one).

Run (repository root):
python -m unittest discover -s clean/l/runtime/tests -t clean/l/runtime/tests -p test_l_runtime_load.py
"""
import hashlib
import os
import shutil
import subprocess
import tempfile
import unittest

import numpy as np

from clean.l.reference import oracle as L
from clean.l.reference import table as T
from clean.runner import synthetic_model as SM

ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))))
SOURCES = [os.path.join("clean", "l", "runtime", "tests", "l_load_dump.cpp"),
os.path.join("clean", "l", "runtime", "l_model.cpp"),
os.path.join("clean", "runtime", "model.cpp"), os.path.join("clean", "runtime", "embedded.cpp"),
os.path.join("clean", "converter", "src", "sha256.cpp"),
os.path.join("clean", "converter", "src", "json.cpp")]
SHARED = ("enc1", "enc2", "b0", "b1", "b2", "b3", "b4", "b5", "dec2", "dec1")
_EXE = None


def _vcvars64():
vswhere = os.path.join(os.environ.get("ProgramFiles(x86)", r"C:\Program Files (x86)"),
"Microsoft Visual Studio", "Installer", "vswhere.exe")
if not os.path.exists(vswhere):
return None
r = subprocess.run([vswhere, "-latest", "-products", "*", "-property", "installationPath"], capture_output=True,
text=True)
vcvars = os.path.join(r.stdout.strip(), "VC", "Auxiliary", "Build", "vcvars64.bat")
return vcvars if r.returncode == 0 and r.stdout.strip() and os.path.exists(vcvars) else None


def dump_exe():
global _EXE
if _EXE is None:
tmp = tempfile.mkdtemp(prefix="wg_ll_")
exe = os.path.join(tmp, "l_load_dump" + (".exe" if os.name == "nt" else ""))
cxx = os.environ.get("CXX") or shutil.which("clang++") or shutil.which("g++")
if cxx:
cmd = [cxx, "-std=c++17", "-O1"] + (["-D_CRT_SECURE_NO_WARNINGS"] if os.name == "nt" else [])
r = subprocess.run(cmd + [os.path.join(ROOT, s) for s in SOURCES] + ["-o", exe], capture_output=True,
text=True)
elif os.name == "nt" and (shutil.which("cl") or _vcvars64()):
vcvars = None if shutil.which("cl") else _vcvars64()
objdir = os.path.join(tmp, "obj")
os.makedirs(objdir)
cl = ('cl /nologo /std:c++17 /O1 /EHsc /utf-8 /D_CRT_SECURE_NO_WARNINGS %s /Fo"%s\\\\" /Fe"%s"'
% (" ".join('"%s"' % os.path.join(ROOT, s) for s in SOURCES), objdir, exe))
bat = os.path.join(tmp, "build.bat")
with open(bat, "w") as f:
f.write("@echo off\r\n" + ('call "%s" >nul || exit /b 1\r\n' % vcvars if vcvars else "") +
cl + " || exit /b 1\r\n")
r = subprocess.run(["cmd", "/d", "/c", bat], capture_output=True, text=True)
else:
raise unittest.SkipTest("no host C++ compiler (CXX, clang++, g++ or MSVC cl)")
if r.returncode != 0:
raise RuntimeError("build of l_load_dump failed:\n" + r.stdout + r.stderr)
_EXE = exe
return _EXE


def dump(*args):
return subprocess.run([dump_exe(), *args], capture_output=True, text=True).stdout.strip().splitlines()


def specs():
"""{stage: {name: (shape, is_u8)}} in the C++ order (dicts keep it)."""
out = {}
for ln in dump("specs"):
stage, name, dims, kind = ln.split(" ")
out.setdefault(stage, {})[name] = (tuple(int(d) for d in dims.split("x")), kind == "u8")
return out


def raw_model(seed):
"""A synthetic Model L as {stage: {name: array}} in the raw shapes and dtypes of the C++ list."""
p = L.random_params(np.random.default_rng(seed))
p["post"] = {"s": np.array([1.25], np.float32)}
out = {}
for stage, tensors in specs().items():
out[stage] = {}
for name, (shape, u8) in tensors.items():
a = np.asarray(p[stage][name])
out[stage][name] = a.astype(np.uint8).reshape(shape) if u8 else a.astype("<f4").reshape(shape)
return out


def write_raw(model_dir, model):
for stage, tensors in model.items():
d = os.path.join(model_dir, "raw", stage)
os.makedirs(d, exist_ok=True)
for name, a in tensors.items():
np.ascontiguousarray(a).tofile(os.path.join(d, name + ".bin"))


def numel(shape):
return int(np.prod(shape)) if shape else 1


class Specs(unittest.TestCase):
def test_table(self):
s = specs()
self.assertEqual(list(s), ["enc0"] + list(SHARED) + ["dec0", "post"])
for stage, want in (("enc0", T.encoder_shapes()), ("dec0", T.decoder_shapes())):
self.assertEqual(sorted(s[stage]), sorted(want), stage)
for name, shape in want.items():
self.assertEqual(numel(s[stage][name][0]), numel(shape), "%s/%s" % (stage, name))
self.assertEqual(s[stage][name][1], name == "T" and stage == "enc0", "%s/%s" % (stage, name))
self.assertEqual(list(s["enc0"])[0], "W") # the raw root is found by enc0/W.bin, as for Model M

def test_shared_stages_are_model_m(self):
s, m = specs(), SM.synthetic_tensors(1)
for stage in SHARED + ("post",):
self.assertEqual({k: v[0] for k, v in s[stage].items()}, {k: a.shape for k, a in m[stage].items()}, stage)
for name, a in m["dec0"].items(): # Model M's dec0 tensors keep their raw shapes inside L's dec0
self.assertEqual(s["dec0"][name][0], a.shape, name)
for name, a in m["enc0"].items():
if name != "W": # L's projection is 64 x 128 (Model M: 64 x 80)
self.assertEqual(s["enc0"][name][0], a.shape, name)


class Load(unittest.TestCase):
def test_bytes(self):
model = raw_model(4)
d = tempfile.mkdtemp(prefix="wg_ll_")
try:
write_raw(d, model)
lines = dump("load", d)
self.assertEqual(lines[0], "OK %d" % len(model))
got = {tuple(ln.split(" ")[:2]): ln.split(" ")[2:] for ln in lines[1:]}
want = {(st, n): [str(a.size), hashlib.sha256(np.ascontiguousarray(a).tobytes()).hexdigest()]
for st, ts in model.items() for n, a in ts.items()}
self.assertEqual(got, want)
# model_dir may also be raw/ itself
self.assertEqual(dump("load", os.path.join(d, "raw")), lines)
finally:
shutil.rmtree(d, ignore_errors=True)

def test_refusals(self):
model = raw_model(5)
d = tempfile.mkdtemp(prefix="wg_ll_")
try:
write_raw(d, model)
raw = d + "/raw" # as the loader names it
f = os.path.join(d, "raw", "dec0", "blk.W1.bin")
os.remove(f)
self.assertEqual(dump("load", d), ["ERR model data missing: " + raw + "/dec0/blk.W1.bin"])
model["dec0"]["blk.W1"][:64].tofile(f)
self.assertEqual(dump("load", d), ["ERR model data damaged: " + raw + "/dec0/blk.W1.bin has 8192 bytes,"
" expected 16384"])
model["dec0"]["blk.W1"].tofile(f)
t = model["enc0"]["T"].copy()
t.flat[7] = 200
t.tofile(os.path.join(d, "raw", "enc0", "T.bin"))
self.assertEqual(dump("load", d), ["ERR model data damaged: " + raw + "/enc0/T.bin has a value above 127"])
finally:
shutil.rmtree(d, ignore_errors=True)


if __name__ == "__main__":
unittest.main()
22 changes: 14 additions & 8 deletions clean/runtime/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ const std::map<std::string, SwinStageInfo>& swin_table() {
return t;
}

struct TensorSpec {
std::string name;
std::vector<int> shape;
bool u8;
};
} // namespace

std::vector<TensorSpec> stage_specs(const std::string& stage) {
if (stage == "enc0")
Expand All @@ -57,6 +53,8 @@ std::vector<TensorSpec> stage_specs(const std::string& stage) {
return v;
}

namespace {

size_t numel(const std::vector<int>& shape) {
size_t n = 1;
for (int d : shape) n *= (size_t)d;
Expand Down Expand Up @@ -418,11 +416,19 @@ bool read_raw_tensor(const std::string& path, const TensorSpec& ts, Tensor* out,
} // namespace

bool load_raw_model(const std::string& model_dir, std::map<std::string, StageTensors>* out, std::string* err) {
StageSpecs stages;
for (const std::string& stage : model_stages()) stages.emplace_back(stage, stage_specs(stage));
return load_raw_stages(model_dir, stages, out, err);
}

bool load_raw_stages(const std::string& model_dir, const StageSpecs& stages,
std::map<std::string, StageTensors>* out, std::string* err) {
if (stages.empty() || stages[0].second.empty()) return *err = "no stages to load", false;
std::string root = model_dir + "/raw";
if (!file_exists(root + "/enc0/W.bin")) root = model_dir;
for (const std::string& stage : model_stages()) {
if (!file_exists(root + "/" + stages[0].first + "/" + stages[0].second[0].name + ".bin")) root = model_dir;
for (const auto& [stage, specs] : stages) {
StageTensors st;
for (const TensorSpec& ts : stage_specs(stage)) {
for (const TensorSpec& ts : specs) {
const std::string path = root + "/" + stage + "/" + ts.name + ".bin";
if (!read_raw_tensor(path, ts, &st[ts.name], err)) return false;
}
Expand Down
20 changes: 19 additions & 1 deletion clean/runtime/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <cstdint>
#include <map>
#include <string>
#include <utility>
#include <vector>

namespace wgrt {
Expand Down Expand Up @@ -36,10 +37,27 @@ extern const char* const kFilterTaps; // "output_taps"
// The five weights when the model has the group; false when it has not.
bool model_output_taps(const std::map<std::string, StageTensors>& model, float taps[5]);

// One tensor of a stage: name, shape (raw layout), uint8 (else float32).
struct TensorSpec {
std::string name;
std::vector<int> shape;
bool u8;
};
using StageSpecs = std::vector<std::pair<std::string, std::vector<TensorSpec>>>; // (stage, its tensors), in order

// Model M's tensors of one stage of model_stages() (spec 08 section 7.2).
std::vector<TensorSpec> stage_specs(const std::string& stage);

// Reads and checks every tensor (sizes, finite values, T in 0..127). model_dir may hold raw/ or be raw/ itself.
// Test path (spec 10 answers: --raw is kept for tests).
// Test path (spec 10 answers: --raw is kept for tests). Model M: load_raw_stages with model_stages() / stage_specs.
bool load_raw_model(const std::string& model_dir, std::map<std::string, StageTensors>* out, std::string* err);

// The same for any list of stages (another model's): <root>/<stage>/<name>.bin per tensor, where root is
// model_dir/raw when that holds the first tensor of the first stage, else model_dir; uint8 tensors at most 127; plus
// the optional group "filter" as above.
bool load_raw_stages(const std::string& model_dir, const StageSpecs& stages,
std::map<std::string, StageTensors>* out, std::string* err);

// Normal path: the converter's model.wgcm (clean/converter/OUTPUT-FORMAT.md). Checks the whole-file SHA-256, every
// stage and tensor (names, dtypes, shapes), and recomputes each stage's keyed digest (spec 08 section 7.3, with the
// key stored in the file) against the stored digest and the digest of the map at map_path (for "filter": one entry
Expand Down
Loading