From fc6b0b32bf962015b02eda4c7469324d609972cc Mon Sep 17 00:00:00 2001 From: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> Date: Fri, 13 Nov 2026 18:02:04 -0500 Subject: [PATCH] Runtime: raw model loading for any stage list; Model L's tensor list and loader load_raw_stages loads /.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> --- clean/l/runtime/l_model.cpp | 54 ++++++ clean/l/runtime/l_model.h | 28 +++ clean/l/runtime/tests/l_load_dump.cpp | 43 +++++ clean/l/runtime/tests/test_l_runtime_load.py | 179 +++++++++++++++++++ clean/runtime/model.cpp | 22 ++- clean/runtime/model.h | 20 ++- 6 files changed, 337 insertions(+), 9 deletions(-) create mode 100644 clean/l/runtime/l_model.cpp create mode 100644 clean/l/runtime/l_model.h create mode 100644 clean/l/runtime/tests/l_load_dump.cpp create mode 100644 clean/l/runtime/tests/test_l_runtime_load.py diff --git a/clean/l/runtime/l_model.cpp b/clean/l/runtime/l_model.cpp new file mode 100644 index 00000000..891b5048 --- /dev/null +++ b/clean/l/runtime/l_model.cpp @@ -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* v, const std::string& prefix, int C, int heads, int mlp) { + const std::vector 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 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 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* out, std::string* err) { + return load_raw_stages(model_dir, stage_specs(), out, err); +} + +} // namespace l +} // namespace wgrt diff --git a/clean/l/runtime/l_model.h b/clean/l/runtime/l_model.h new file mode 100644 index 00000000..7b706d26 --- /dev/null +++ b/clean/l/runtime/l_model.h @@ -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 +#include + +#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(): [/raw]//.bin. +bool load_raw_model(const std::string& model_dir, std::map* out, std::string* err); + +} // namespace l +} // namespace wgrt diff --git a/clean/l/runtime/tests/l_load_dump.cpp b/clean/l/runtime/tests/l_load_dump.cpp new file mode 100644 index 00000000..2f782ac8 --- /dev/null +++ b/clean/l/runtime/tests/l_load_dump.cpp @@ -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(): " " +// l_load_dump load +// wgrt::l::load_raw_model; prints "OK " and one line per loaded tensor, " +// ", or "ERR " +#include +#include +#include + +#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 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; +} diff --git a/clean/l/runtime/tests/test_l_runtime_load.py b/clean/l/runtime/tests/test_l_runtime_load.py new file mode 100644 index 00000000..016fe6d4 --- /dev/null +++ b/clean/l/runtime/tests/test_l_runtime_load.py @@ -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("& swin_table() { return t; } -struct TensorSpec { - std::string name; - std::vector shape; - bool u8; -}; +} // namespace std::vector stage_specs(const std::string& stage) { if (stage == "enc0") @@ -57,6 +53,8 @@ std::vector stage_specs(const std::string& stage) { return v; } +namespace { + size_t numel(const std::vector& shape) { size_t n = 1; 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, } // namespace bool load_raw_model(const std::string& model_dir, std::map* 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* 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; } diff --git a/clean/runtime/model.h b/clean/runtime/model.h index 89ce9656..581b707d 100644 --- a/clean/runtime/model.h +++ b/clean/runtime/model.h @@ -9,6 +9,7 @@ #include #include #include +#include #include namespace wgrt { @@ -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& model, float taps[5]); +// One tensor of a stage: name, shape (raw layout), uint8 (else float32). +struct TensorSpec { + std::string name; + std::vector shape; + bool u8; +}; +using StageSpecs = std::vector>>; // (stage, its tensors), in order + +// Model M's tensors of one stage of model_stages() (spec 08 section 7.2). +std::vector 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* out, std::string* err); +// The same for any list of stages (another model's): //.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* 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