Skip to content

Commit fc6b0b3

Browse files
Runtime: raw model loading for any stage list; Model L's tensor list and loader
load_raw_stages loads <stage>/<name>.bin for a given list of stages; load_raw_model is Model M's list through it (unchanged behaviour). clean/l/runtime/l_model.cpp: Model L's stages and tensors (the default table's logical names) and its raw loader, checked against the table and Model M's raw tensors. Refs #89 Signed-off-by: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com>
1 parent e6500a8 commit fc6b0b3

6 files changed

Lines changed: 337 additions & 9 deletions

File tree

‎clean/l/runtime/l_model.cpp‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// SPDX-License-Identifier: MIT
2+
// Model L's model data in the runtime (see l_model.h). Shapes mirror clean/l/reference/table.py for the default table
3+
// (checked by clean/l/runtime/tests/test_l_runtime_load.py).
4+
#include "l_model.h"
5+
6+
namespace wgrt {
7+
namespace l {
8+
9+
namespace {
10+
11+
const int kHeadDim = 32, kWindowTokens = 16;
12+
13+
// One Swin block's tensors with a name prefix (table.block_shapes).
14+
void add_block(std::vector<TensorSpec>* v, const std::string& prefix, int C, int heads, int mlp) {
15+
const std::vector<TensorSpec> b = {{prefix + "gamma1", {C}, false},
16+
{prefix + "gamma2", {C}, false},
17+
{prefix + "Wqk", {heads, kHeadDim, C}, false},
18+
{prefix + "Wv", {heads, kHeadDim, C}, false},
19+
{prefix + "B", {heads, kWindowTokens, kWindowTokens}, false},
20+
{prefix + "Wp", {C, kHeadDim * heads}, false},
21+
{prefix + "bp", {C}, false},
22+
{prefix + "W1", {mlp, C}, false},
23+
{prefix + "b1", {mlp}, false},
24+
{prefix + "W2", {C, mlp}, false},
25+
{prefix + "b2", {C}, false}};
26+
v->insert(v->end(), b.begin(), b.end());
27+
}
28+
29+
} // namespace
30+
31+
StageSpecs stage_specs() {
32+
StageSpecs s;
33+
// enc0: the projection reads [raw (80) | x (32) | 16 zeros] = 128 inputs; the first linear reads the 20 values of
34+
// one sub-pixel (table: select 0..20, linear 20 -> 32), the second 32 -> 32; then the width-32 block.
35+
std::vector<TensorSpec> enc0 = {{"W", {64, 128}, false}, {"b", {64}, false}, {"T", {8, 16, 16}, true},
36+
{"sigma1", {1}, false}, {"sigma2", {1}, false}, {"Wa", {32, 20}, false},
37+
{"ba", {32}, false}, {"Wb", {32, 32}, false}, {"bb", {32}, false}};
38+
add_block(&enc0, "blk.", 32, 2, 128);
39+
s.emplace_back("enc0", enc0);
40+
for (const std::string& st : model_stages())
41+
if (st != "enc0" && st != "dec0" && st != "post") s.emplace_back(st, wgrt::stage_specs(st));
42+
std::vector<TensorSpec> dec0 = wgrt::stage_specs("dec0");
43+
add_block(&dec0, "blk.", 32, 2, 128);
44+
s.emplace_back("dec0", dec0);
45+
s.emplace_back("post", wgrt::stage_specs("post"));
46+
return s;
47+
}
48+
49+
bool load_raw_model(const std::string& model_dir, std::map<std::string, StageTensors>* out, std::string* err) {
50+
return load_raw_stages(model_dir, stage_specs(), out, err);
51+
}
52+
53+
} // namespace l
54+
} // namespace wgrt

‎clean/l/runtime/l_model.h‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// SPDX-License-Identifier: MIT
2+
// Model L's model data in the runtime (#89): the stages and tensors of Model L in the converter's raw layout, and the
3+
// raw loader for them (clean/runtime/model.h load_raw_stages). Names are the logical names of the default structure
4+
// table (clean/l/reference/table.py: encoder_shapes, decoder_shapes); the ten Swin stages and post are Model M's.
5+
//
6+
// enc0: Model M's W, b, T, sigma1, sigma2 (W is the projection, 64 x 128) + the two linears Wa / ba, Wb / bb + the
7+
// width-32 block "blk.*"
8+
// enc1 ... dec1: Model M's stages (clean/runtime/model.cpp stage_specs)
9+
// dec0: Model M's W, b, S, T + the width-32 block "blk.*"
10+
// post: Model M's s
11+
#pragma once
12+
13+
#include <map>
14+
#include <string>
15+
16+
#include "../../runtime/model.h"
17+
18+
namespace wgrt {
19+
namespace l {
20+
21+
// The stages in load order: enc0, enc1 ... dec1 (model_stages() order), dec0, post.
22+
StageSpecs stage_specs();
23+
24+
// load_raw_stages with stage_specs(): <model_dir>[/raw]/<stage>/<name>.bin.
25+
bool load_raw_model(const std::string& model_dir, std::map<std::string, StageTensors>* out, std::string* err);
26+
27+
} // namespace l
28+
} // namespace wgrt
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// SPDX-License-Identifier: MIT
2+
// Model L's raw model loading (clean/l/runtime/l_model.cpp) on the CPU, for test_l_runtime_load.py:
3+
//
4+
// l_load_dump specs
5+
// one line per tensor of wgrt::l::stage_specs(): "<stage> <name> <dims joined by x> <u8|f32>"
6+
// l_load_dump load <model dir>
7+
// wgrt::l::load_raw_model; prints "OK <stages>" and one line per loaded tensor, "<stage> <name> <values>
8+
// <sha256 of the values' bytes>", or "ERR <message>"
9+
#include <cstdio>
10+
#include <map>
11+
#include <string>
12+
13+
#include "../../../converter/src/sha256.h"
14+
#include "../l_model.h"
15+
16+
int main(int argc, char** argv) {
17+
const std::string mode = argc > 1 ? argv[1] : "";
18+
if (mode == "specs") {
19+
for (const auto& [stage, specs] : wgrt::l::stage_specs())
20+
for (const wgrt::TensorSpec& t : specs) {
21+
std::string dims;
22+
for (int d : t.shape) dims += (dims.empty() ? "" : "x") + std::to_string(d);
23+
std::printf("%s %s %s %s\n", stage.c_str(), t.name.c_str(), dims.c_str(), t.u8 ? "u8" : "f32");
24+
}
25+
return 0;
26+
}
27+
if (mode == "load" && argc > 2) {
28+
std::map<std::string, wgrt::StageTensors> model;
29+
std::string err;
30+
if (!wgrt::l::load_raw_model(argv[2], &model, &err)) return std::printf("ERR %s\n", err.c_str()), 0;
31+
std::printf("OK %zu\n", model.size());
32+
for (const auto& [stage, tensors] : model)
33+
for (const auto& [name, t] : tensors) {
34+
const wgc::Digest d = t.u.empty() ? wgc::Sha256::hash(t.f.data(), t.f.size() * 4)
35+
: wgc::Sha256::hash(t.u.data(), t.u.size());
36+
std::printf("%s %s %zu %s\n", stage.c_str(), name.c_str(), t.u.empty() ? t.f.size() : t.u.size(),
37+
wgc::to_hex(d).c_str());
38+
}
39+
return 0;
40+
}
41+
std::printf("ERR usage\n");
42+
return 2;
43+
}
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
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()

‎clean/runtime/model.cpp‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,7 @@ const std::map<std::string, SwinStageInfo>& swin_table() {
2727
return t;
2828
}
2929

30-
struct TensorSpec {
31-
std::string name;
32-
std::vector<int> shape;
33-
bool u8;
34-
};
30+
} // namespace
3531

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

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

420418
bool load_raw_model(const std::string& model_dir, std::map<std::string, StageTensors>* out, std::string* err) {
419+
StageSpecs stages;
420+
for (const std::string& stage : model_stages()) stages.emplace_back(stage, stage_specs(stage));
421+
return load_raw_stages(model_dir, stages, out, err);
422+
}
423+
424+
bool load_raw_stages(const std::string& model_dir, const StageSpecs& stages,
425+
std::map<std::string, StageTensors>* out, std::string* err) {
426+
if (stages.empty() || stages[0].second.empty()) return *err = "no stages to load", false;
421427
std::string root = model_dir + "/raw";
422-
if (!file_exists(root + "/enc0/W.bin")) root = model_dir;
423-
for (const std::string& stage : model_stages()) {
428+
if (!file_exists(root + "/" + stages[0].first + "/" + stages[0].second[0].name + ".bin")) root = model_dir;
429+
for (const auto& [stage, specs] : stages) {
424430
StageTensors st;
425-
for (const TensorSpec& ts : stage_specs(stage)) {
431+
for (const TensorSpec& ts : specs) {
426432
const std::string path = root + "/" + stage + "/" + ts.name + ".bin";
427433
if (!read_raw_tensor(path, ts, &st[ts.name], err)) return false;
428434
}

‎clean/runtime/model.h‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <cstdint>
1010
#include <map>
1111
#include <string>
12+
#include <utility>
1213
#include <vector>
1314

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

40+
// One tensor of a stage: name, shape (raw layout), uint8 (else float32).
41+
struct TensorSpec {
42+
std::string name;
43+
std::vector<int> shape;
44+
bool u8;
45+
};
46+
using StageSpecs = std::vector<std::pair<std::string, std::vector<TensorSpec>>>; // (stage, its tensors), in order
47+
48+
// Model M's tensors of one stage of model_stages() (spec 08 section 7.2).
49+
std::vector<TensorSpec> stage_specs(const std::string& stage);
50+
3951
// Reads and checks every tensor (sizes, finite values, T in 0..127). model_dir may hold raw/ or be raw/ itself.
40-
// Test path (spec 10 answers: --raw is kept for tests).
52+
// Test path (spec 10 answers: --raw is kept for tests). Model M: load_raw_stages with model_stages() / stage_specs.
4153
bool load_raw_model(const std::string& model_dir, std::map<std::string, StageTensors>* out, std::string* err);
4254

55+
// The same for any list of stages (another model's): <root>/<stage>/<name>.bin per tensor, where root is
56+
// model_dir/raw when that holds the first tensor of the first stage, else model_dir; uint8 tensors at most 127; plus
57+
// the optional group "filter" as above.
58+
bool load_raw_stages(const std::string& model_dir, const StageSpecs& stages,
59+
std::map<std::string, StageTensors>* out, std::string* err);
60+
4361
// Normal path: the converter's model.wgcm (clean/converter/OUTPUT-FORMAT.md). Checks the whole-file SHA-256, every
4462
// stage and tensor (names, dtypes, shapes), and recomputes each stage's keyed digest (spec 08 section 7.3, with the
4563
// key stored in the file) against the stored digest and the digest of the map at map_path (for "filter": one entry

0 commit comments

Comments
 (0)