From 18d31ec891e470ede875ea300f6326458fae9717 Mon Sep 17 00:00:00 2001 From: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> Date: Fri, 13 Nov 2026 18:38:00 -0500 Subject: [PATCH 1/2] Model L tensor list: the confirmed layout (#109); fixes test_l_runtime_load on main #110 merged after #109 changed the structure table: the runtime's Model L tensor list still had the earlier layout and its test called table.encoder_shapes, which #109 removed. Now the map's stages (enc0, enc0_block, the ten shared stages, dec0_block, dec0, post), names and shapes; the test checks them against data/preset-L.v1.map.json and table.param_shapes. Refs #89 Signed-off-by: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> --- clean/l/runtime/l_model.cpp | 57 +++++++++++--------- clean/l/runtime/l_model.h | 14 ++--- clean/l/runtime/tests/test_l_runtime_load.py | 42 +++++++++------ 3 files changed, 65 insertions(+), 48 deletions(-) diff --git a/clean/l/runtime/l_model.cpp b/clean/l/runtime/l_model.cpp index 891b5048..6691db8b 100644 --- a/clean/l/runtime/l_model.cpp +++ b/clean/l/runtime/l_model.cpp @@ -1,6 +1,6 @@ // 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). +// Model L's model data in the runtime (see l_model.h). Checked against the preset-L map and the structure table by +// clean/l/runtime/tests/test_l_runtime_load.py. #include "l_model.h" namespace wgrt { @@ -10,38 +10,43 @@ 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()); +// One Swin block's tensors (table.block_shapes; the map's block record set). +std::vector block_specs(int C, int heads, int mlp) { + return {{"gamma1", {C}, false}, + {"gamma2", {C}, false}, + {"Wqk", {heads, kHeadDim, C}, false}, + {"Wv", {heads, kHeadDim, C}, false}, + {"B", {heads, kWindowTokens, kWindowTokens}, false}, + {"Wp", {C, kHeadDim * heads}, false}, + {"bp", {C}, false}, + {"W1", {mlp, C}, false}, + {"b1", {mlp}, false}, + {"W2", {C, mlp}, false}, + {"b2", {C}, false}}; } } // 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); + // enc0: the projection reads the 2 x 2 pixels of 32 channels of each token (128 inputs); the first input linear + // reads the 20 values of a pixel, the second 32. + s.emplace_back("enc0", std::vector{{"W", {64, 128}, false}, + {"b", {64}, false}, + {"T", {8, 16, 16}, true}, + {"sigma1", {1}, false}, + {"sigma2", {1}, false}, + {"Win1", {32, 20}, false}, + {"bin1", {32}, false}, + {"Win2", {32, 32}, false}, + {"bin2", {32}, false}}); + s.emplace_back("enc0_block", block_specs(32, 2, 128)); 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("dec0_block", block_specs(32, 2, 128)); + // dec0: 32 outputs per sub-pixel (Model M: 17), then Model M's scalars + s.emplace_back("dec0", std::vector{ + {"W", {4, 32, 64}, false}, {"b", {4, 32}, false}, {"S", {1}, false}, {"T", {1}, false}}); s.emplace_back("post", wgrt::stage_specs("post")); return s; } diff --git a/clean/l/runtime/l_model.h b/clean/l/runtime/l_model.h index 7b706d26..c5fb9a4f 100644 --- a/clean/l/runtime/l_model.h +++ b/clean/l/runtime/l_model.h @@ -1,12 +1,14 @@ // 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. +// raw loader for them (clean/runtime/model.h load_raw_stages). Stages, names and shapes are those of the preset-L map +// (data/preset-L.v1.map.json), which the structure table (clean/l/reference/table.py param_shapes) groups the same way: // -// 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.*" +// enc0: W (the projection, 64 x 128), b, T, sigma1, sigma2, and the two input linears Win1 / bin1 (32 x 20), +// Win2 / bin2 (32 x 32) +// enc0_block: the encoder's width-32 Swin block // 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.*" +// dec0_block: the decoder's width-32 Swin block +// dec0: W (4 x 32 x 64), b (4 x 32), S, T // post: Model M's s #pragma once @@ -18,7 +20,7 @@ namespace wgrt { namespace l { -// The stages in load order: enc0, enc1 ... dec1 (model_stages() order), dec0, post. +// The stages in the map's order: enc0, enc0_block, enc1 ... dec1, dec0_block, dec0, post. StageSpecs stage_specs(); // load_raw_stages with stage_specs(): [/raw]//.bin. diff --git a/clean/l/runtime/tests/test_l_runtime_load.py b/clean/l/runtime/tests/test_l_runtime_load.py index 016fe6d4..385ee441 100644 --- a/clean/l/runtime/tests/test_l_runtime_load.py +++ b/clean/l/runtime/tests/test_l_runtime_load.py @@ -1,9 +1,9 @@ # 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); +- the tensor list against the preset-L map (data/preset-L.v1.map.json: stages, names, shapes, types, in order), + against the structure table (clean/l/reference/table.py param_shapes: names, 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). @@ -15,6 +15,7 @@ python -m unittest discover -s clean/l/runtime/tests -t clean/l/runtime/tests -p test_l_runtime_load.py """ import hashlib +import json import os import shutil import subprocess @@ -116,25 +117,34 @@ def numel(shape): class Specs(unittest.TestCase): + def test_map(self): + """Stages, names, shapes and types of the preset-L map (data/preset-L.v1.map.json), in its order.""" + with open(os.path.join(ROOT, "data", "preset-L.v1.map.json"), encoding="utf-8") as f: + m = json.load(f) + want = {st["stage"]: {t["name"]: (tuple(t["shape"]), t["dtype"] == "uint8") for t in st["tensors"]} + for st in m["stages"]} + s = specs() + self.assertEqual(list(s), list(want)) + for stage in want: + self.assertEqual(list(s[stage].items()), list(want[stage].items()), stage) + def test_table(self): + """The structure table's parameters (table.param_shapes): same stages and names, same number of values.""" s = specs() - self.assertEqual(list(s), ["enc0"] + list(SHARED) + ["dec0", "post"]) - for stage, want in (("enc0", T.encoder_shapes()), ("dec0", T.decoder_shapes())): + for stage, want in T.param_shapes().items(): 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)[0], "enc0") 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) + for stage, names in (("enc0", ("b", "T", "sigma1", "sigma2")), ("dec0", ("S", "T"))): + for name in names: # Model M's raw shapes (L: projection 64 x 128, dec0 32 outputs per sub-pixel) + self.assertEqual(s[stage][name][0], m[stage][name].shape, name) class Load(unittest.TestCase): @@ -160,13 +170,13 @@ def test_refusals(self): try: write_raw(d, model) raw = d + "/raw" # as the loader names it - f = os.path.join(d, "raw", "dec0", "blk.W1.bin") + f = os.path.join(d, "raw", "dec0_block", "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," + self.assertEqual(dump("load", d), ["ERR model data missing: " + raw + "/dec0_block/W1.bin"]) + model["dec0_block"]["W1"][:64].tofile(f) + self.assertEqual(dump("load", d), ["ERR model data damaged: " + raw + "/dec0_block/W1.bin has 8192 bytes," " expected 16384"]) - model["dec0"]["blk.W1"].tofile(f) + model["dec0_block"]["W1"].tofile(f) t = model["enc0"]["T"].copy() t.flat[7] = 200 t.tofile(os.path.join(d, "raw", "enc0", "T.bin")) From abd12afb199a83bea7a6b682b61478ee66c6c52f Mon Sep 17 00:00:00 2001 From: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> Date: Fri, 13 Nov 2026 18:45:26 -0500 Subject: [PATCH 2/2] Model L: runtime weight packer (C++ port of l_pack.py) wgrt::l::encoder_launches / decoder_launches mirror l_pack's launch lists for the default table (#109's layout, the map's stage and tensor names); pack_model packs every blob of pack_model_l: enc0.values, enc0.Win1, enc0.Win2, enc0_block, enc0.project, dec0.project, dec0_block, dec0.coef. model.h pack_parts gains rows_expand (the expand rows of dec0.project). Byte-identical to the Python packer (test_l_runtime_pack.py). Refs #89 Signed-off-by: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> --- clean/l/runtime/l_pack.cpp | 205 +++++++++++++++++++ clean/l/runtime/l_pack.h | 63 ++++++ clean/l/runtime/tests/l_pack_dump.cpp | 88 ++++++++ clean/l/runtime/tests/test_l_runtime_pack.py | 172 ++++++++++++++++ clean/runtime/model.cpp | 1 + clean/runtime/model.h | 1 + 6 files changed, 530 insertions(+) create mode 100644 clean/l/runtime/l_pack.cpp create mode 100644 clean/l/runtime/l_pack.h create mode 100644 clean/l/runtime/tests/l_pack_dump.cpp create mode 100644 clean/l/runtime/tests/test_l_runtime_pack.py diff --git a/clean/l/runtime/l_pack.cpp b/clean/l/runtime/l_pack.cpp new file mode 100644 index 00000000..cb39b0a4 --- /dev/null +++ b/clean/l/runtime/l_pack.cpp @@ -0,0 +1,205 @@ +// SPDX-License-Identifier: MIT +// Model L weight packing for the runtime; see l_pack.h. +#include "l_pack.h" + +namespace wgrt { +namespace l { + +namespace { + +const Tensor* find(const StageTensors& t, const std::string& name) { + const auto it = t.find(name); + return it == t.end() ? nullptr : &it->second; +} + +bool need(const StageTensors& t, const std::string& stage, const std::string& name, size_t n, const Tensor** out, + std::string* err) { + const Tensor* x = find(t, name); + if (!x) return *err = stage + ": missing tensor " + name, false; + if (x->f.size() != n) + return *err = stage + ": tensor " + name + " has " + std::to_string(x->f.size()) + " values, expected " + + std::to_string(n), + false; + *out = x; + return true; +} + +const StageTensors* stage(const std::map& p, const std::string& name, std::string* err) { + const auto it = p.find(name); + if (it == p.end()) return *err = "missing stage " + name, nullptr; + return &it->second; +} + +int sum(const std::vector& v) { + int n = 0; + for (int x : v) n += x; + return n; +} + +Launch swin(const std::string& src, const std::string& offsets, const std::string& params) { + Launch l; + l.kind = "swin", l.blob = params, l.src = src, l.dst = src, l.C = 32, l.heads = 2, l.mlp = 128; + l.offsets = offsets, l.params = params; + return l; +} + +} // namespace + +const std::vector& encoder_launches() { + static const std::vector v = [] { + std::vector l(5); + l[0].kind = "values", l[0].blob = "enc0.values", l[0].dst = "q", l[0].C = 32; + l[1].kind = "linear", l[1].blob = "enc0.Win1", l[1].src = "q", l[1].dst = "x"; + l[1].C_in = 32, l[1].C_out = 32, l[1].w = "Win1", l[1].b = "bin1", l[1].acc = "f16", l[1].out_round = "relu_q8"; + l[2].kind = "linear", l[2].blob = "enc0.Win2", l[2].src = "x", l[2].dst = "x"; + l[2].C_in = 32, l[2].C_out = 32, l[2].w = "Win2", l[2].b = "bin2", l[2].acc = "f16", l[2].out_round = "q8"; + l[3] = swin("x", "first", "enc0_block"); + l[4].kind = "project", l[4].blob = "enc0.project", l[4].src = "x", l[4].groups = {32, 32, 32, 32}; + l[4].w = "W", l[4].b = "b", l[4].clamp = 6.28125f; + return l; + }(); + return v; +} + +const std::vector& decoder_launches() { + static const std::vector v = [] { + std::vector l(3); + l[0].kind = "dec_project", l[0].blob = "dec0.project", l[0].dst = "p", l[0].w = "W", l[0].b = "b"; + l[0].groups = {32, 32}; + l[1] = swin("p", "second", "dec0_block"); + l[2].kind = "coef", l[2].blob = "dec0.coef", l[2].src = "p"; + l[2].values = {0, 1, 10, 11, 12, 13, 14, 15, 16, 2, 3, 4, 5, 6, 7, 8, 9}; // table.DEC0_VALUES + return l; + }(); + return v; +} + +std::vector kord_groups32(int n) { + std::vector K(n); + for (int kb = 0; kb < n / 16; ++kb) + for (int h = 0; h < 2; ++h) + for (int j = 0; j < 8; ++j) K[16 * kb + 8 * h + j] = 32 * (kb / 2) + 16 * h + 8 * (kb % 2) + j; + return K; +} + +bool pack_linear(const std::vector& W, const std::vector& b, int C_in, int C_out, int lo, const DRow& d, + std::vector* out, std::string* err) { + out->clear(); + if (C_in % 16 || C_out % 16 || C_out <= 0 || lo < 0 || b.size() != (size_t)C_out || W.size() % C_out) + return *err = "linear: bad shape (C_in " + std::to_string(C_in) + ", C_out " + std::to_string(C_out) + ")", false; + const int n = (int)(W.size() / C_out); + if (lo + n > C_in) return *err = "linear: input channels " + std::to_string(lo) + " + " + std::to_string(n) + + " outside " + std::to_string(C_in), + false; + std::vector Wx((size_t)C_out * C_in, 0.0f); // zero weights outside lo .. lo + n + for (int r = 0; r < C_out; ++r) + for (int c = 0; c < n; ++c) Wx[(size_t)r * C_in + lo + c] = W[(size_t)r * n + c]; + const pack_parts::Order RL = pack_parts::rows_layout_l(C_out, d); + pack_parts::put_tile_vector(out, b.data(), RL, d); + pack_parts::put_frags(out, pack_parts::frags(Wx.data(), C_out, C_in, RL, pack_parts::kord_layout_l(C_in))); + return true; +} + +bool pack_project(const std::vector& W, const std::vector& b, const std::vector& groups, int n_out, + const DRow& d, std::vector* out, std::string* err) { + out->clear(); + for (int g : groups) + if (g != 32) return *err = "project: groups of 32 channels only", false; + const int n = sum(groups); + if (n_out <= 0 || n_out % 16 || b.size() != (size_t)n_out || W.size() != (size_t)n_out * n) + return *err = "project: bad shape", false; + const pack_parts::Order RL = pack_parts::rows_layout_l(n_out, d); + pack_parts::put_tile_vector(out, b.data(), RL, d); + pack_parts::put_frags(out, pack_parts::frags(W.data(), n_out, n, RL, kord_groups32(n))); + return true; +} + +bool pack_dec_project(const std::vector& W, const std::vector& b, const std::vector& groups, + const DRow& d, std::vector* out, std::string* err) { + out->clear(); + for (int g : groups) + if (g != 32) return *err = "dec_project: groups of 32 channels only", false; + const int n = sum(groups); + if (b.size() != 128 || W.size() != (size_t)128 * n) return *err = "dec_project: bad shape", false; + const pack_parts::Order RE = pack_parts::rows_expand(32, d); // tile 2 s + g: sub-pixel s, channels 16 g .. + pack_parts::put_tile_vector(out, b.data(), RE, d); + pack_parts::put_frags(out, pack_parts::frags(W.data(), 128, n, RE, kord_groups32(n))); + return true; +} + +bool pack_model(const std::map& params, const DRow& d, + std::map>* out, std::string* err) { + out->clear(); + const StageTensors* enc = stage(params, "enc0", err); + const StageTensors* dec = enc ? stage(params, "dec0", err) : nullptr; + if (!dec) return false; + auto block = [&](const Launch& ln, std::vector* blob) { + const StageTensors* t = stage(params, ln.params, err); + if (!t) return false; + if (!pack_parts::pack_swin_block(*t, "", ln.C, ln.heads, ln.mlp, d, blob, err)) + return *err = ln.params + ": " + *err, false; + return true; + }; + for (const Launch& ln : encoder_launches()) { + std::vector blob; + if (ln.kind == "values") { + // Model M's enc0 blob with a zero projection: only the dither table and the two scalars are used + StageTensors t; + t["W"].f.assign(64 * 80, 0.0f); + t["b"].f.assign(64, 0.0f); + const Tensor* T = find(*enc, "T"); + if (T && T->u.size() != 8 * 16 * 16) return *err = "enc0: tensor T must hold 2048 uint8 values", false; + t["T"].u = T ? T->u : std::vector(8 * 16 * 16, 0); + for (const char* s : {"sigma1", "sigma2"}) { + const Tensor* x = find(*enc, s); + if (x && x->f.size() != 1) return *err = std::string("enc0: tensor ") + s + " must hold one value", false; + t[s].f = {x ? x->f[0] : 0.0f}; + } + if (!pack_stage_blob("enc0", t, d, &blob, err)) return false; + } else if (ln.kind == "linear") { + const Tensor *b, *W = find(*enc, ln.w); + if (!W) return *err = "enc0: missing tensor " + ln.w, false; + if (!need(*enc, "enc0", ln.b, (size_t)ln.C_out, &b, err)) return false; + if (!pack_linear(W->f, b->f, ln.C_in, ln.C_out, 0, d, &blob, err)) + return *err = "enc0." + ln.w + ": " + *err, false; + } else if (ln.kind == "swin") { + if (!block(ln, &blob)) return false; + } else if (ln.kind == "project") { + const int n_out = 64; + const Tensor *W, *b; + if (!need(*enc, "enc0", ln.w, (size_t)n_out * sum(ln.groups), &W, err) || + !need(*enc, "enc0", ln.b, (size_t)n_out, &b, err)) + return false; + if (!pack_project(W->f, b->f, ln.groups, n_out, d, &blob, err)) return *err = "enc0: " + *err, false; + } + (*out)[ln.blob] = std::move(blob); + } + for (const Launch& ln : decoder_launches()) { + std::vector blob; + if (ln.kind == "dec_project") { + const Tensor *W, *b; + if (!need(*dec, "dec0", ln.w, (size_t)128 * sum(ln.groups), &W, err) || + !need(*dec, "dec0", ln.b, 128, &b, err)) + return false; + if (!pack_dec_project(W->f, b->f, ln.groups, d, &blob, err)) return *err = "dec0: " + *err, false; + } else if (ln.kind == "swin") { + if (!block(ln, &blob)) return false; + } else if (ln.kind == "coef") { + // Model M's dec0 blob with a zero projection: the scalars S, T and the code tables + StageTensors t; + t["W"].f.assign(68 * 64, 0.0f); + t["b"].f.assign(68, 0.0f); + for (const char* s : {"S", "T"}) { + const Tensor* x; + if (!need(*dec, "dec0", s, 1, &x, err)) return false; + t[s] = *x; + } + if (!pack_stage_blob("dec0", t, d, &blob, err)) return false; + } + (*out)[ln.blob] = std::move(blob); + } + return true; +} + +} // namespace l +} // namespace wgrt diff --git a/clean/l/runtime/l_pack.h b/clean/l/runtime/l_pack.h new file mode 100644 index 00000000..a74c7b5f --- /dev/null +++ b/clean/l/runtime/l_pack.h @@ -0,0 +1,63 @@ +// SPDX-License-Identifier: MIT +// Model L weight packing for the runtime (#89): C++ port of clean/l/runner/l_pack.py (pack_model_l) for the default +// structure table (clean/l/reference/table.py TABLE), on top of the runtime's packing building blocks +// (clean/runtime/model.h namespace pack_parts). The blobs are byte-identical to the Python packer's +// (clean/l/runtime/tests/test_l_runtime_pack.py). Kernels, arguments and launch order: clean/l/FORMATS.md. +// +// The ten Swin stages Model L shares with Model M use Model M's packer (pack_stage_blob) unchanged; this file packs +// Model L's own launches: the encoder (values, two input linears, the width-32 block, the projection of each token's +// 2 x 2 pixels) and the decoder (projection to 32 values per pixel, the width-32 block, the coefficient functions). +// Stage and tensor names are the preset-L map's (data/preset-L.v1.map.json). +#pragma once + +#include +#include +#include +#include + +#include "../../runtime/model.h" + +namespace wgrt { +namespace l { + +// One launch of Model L's encoder or decoder (l_pack.encoder_launches / decoder_launches). kind: "values", "linear", +// "swin", "project" (encoder); "dec_project", "swin", "coef" (decoder). +struct Launch { + std::string kind, blob, src, dst; + int C_in = 0, C_out = 0; // linear: input / output channels + std::string w, b, acc, out_round; // linear, project, dec_project: tensor names; linear: roundings + int C = 0, heads = 0, mlp = 0; // values: channels of its pixel map; swin: width, heads, MLP + std::string offsets, params; // swin: window-offset kind ("first", "second"), the block's stage + std::vector groups; // project, dec_project: input channel groups + float clamp = 0.0f; // project: output clamp (0 = none) + std::vector values; // coef: the value positions (table.DEC0_VALUES) +}; + +// The default table's launches, in order (mirrors of l_pack.encoder_launches(TABLE) / decoder_launches(TABLE); the +// test compares them field by field). +const std::vector& encoder_launches(); +const std::vector& decoder_launches(); + +// K order of a projection: K[16 kb + 8 h + j] = 32 (kb / 2) + 16 h + 8 (kb % 2) + j (l_pack.kord_groups32). +std::vector kord_groups32(int n); + +// One linear blob: W [C_out][n] reading input channels lo .. lo + n of a C_in-wide map (zero weights elsewhere), +// bias [C_out] (l_pack.pack_linear). +bool pack_linear(const std::vector& W, const std::vector& b, int C_in, int C_out, int lo, const DRow& d, + std::vector* out, std::string* err); +// The encoder's projection blob: W [n_out][sum(groups)], groups of 32 channels (l_pack.pack_project). +bool pack_project(const std::vector& W, const std::vector& b, const std::vector& groups, int n_out, + const DRow& d, std::vector* out, std::string* err); +// The decoder's projection blob: W [4][32][sum(groups)], b [4][32] (l_pack.pack_dec_project). +bool pack_dec_project(const std::vector& W, const std::vector& b, const std::vector& groups, + const DRow& d, std::vector* out, std::string* err); + +// Every blob of Model L's own launches (l_pack.pack_model_l). `params` holds the map's stages by name: "enc0", +// "enc0_block", "dec0", "dec0_block" (float32 values in row-major order; enc0's dither table "T" as uint8 values +// 0..127 in Tensor::u, as the runtime's loader keeps it). Blob names as l_pack: "enc0.values", "enc0.Win1", +// "enc0.Win2", "enc0_block", "enc0.project", "dec0.project", "dec0_block", "dec0.coef". +bool pack_model(const std::map& params, const DRow& d, + std::map>* out, std::string* err); + +} // namespace l +} // namespace wgrt diff --git a/clean/l/runtime/tests/l_pack_dump.cpp b/clean/l/runtime/tests/l_pack_dump.cpp new file mode 100644 index 00000000..803d5f66 --- /dev/null +++ b/clean/l/runtime/tests/l_pack_dump.cpp @@ -0,0 +1,88 @@ +// SPDX-License-Identifier: MIT +// Model L's runtime packer (clean/l/runtime/l_pack.cpp) on the CPU, for test_l_runtime_pack.py: +// +// l_pack_dump pack +// reads //.f32 (float32 values, row-major) for every stage folder (enc0, enc0_block, +// dec0, dec0_block) and /enc0/T.u8 (uint8), runs wgrt::l::pack_model and writes +// /.bin; prints "OK " or "ERR " +// l_pack_dump launches +// prints the default table's encoder launches, then the decoder launches, one line each: +// "enc|..." / "dec|..." with the fields of wgrt::l::Launch (the test builds the same lines from +// clean/l/runner/l_pack.py) +#include +#include +#include +#include +#include +#include + +#include "../l_pack.h" + +namespace fs = std::filesystem; + +namespace { + +bool read_stage(const fs::path& dir, wgrt::StageTensors* t) { + if (!fs::is_directory(dir)) return false; + for (const auto& e : fs::directory_iterator(dir)) { + const fs::path p = e.path(); + std::ifstream f(p, std::ios::binary | std::ios::ate); + const std::streamsize n = f.tellg(); + f.seekg(0); + wgrt::Tensor x; + if (p.extension() == ".f32") { + x.f.resize((size_t)n / 4); + f.read(reinterpret_cast(x.f.data()), n); + } else if (p.extension() == ".u8") { + x.u.resize((size_t)n); + f.read(reinterpret_cast(x.u.data()), n); + } else { + continue; + } + (*t)[p.stem().string()] = x; + } + return true; +} + +std::string join(const std::vector& v) { + std::string s; + for (int x : v) s += (s.empty() ? "" : ",") + std::to_string(x); + return s; +} + +void print(const char* where, const wgrt::l::Launch& l) { + char clamp[32] = ""; + if (l.clamp != 0.0f) std::snprintf(clamp, sizeof clamp, "%.9g", l.clamp); + std::printf("%s|%s|%s|%s|%s|%d|%d|%s|%s|%s|%s|%d|%d|%d|%s|%s|%s|%s|%s\n", where, l.kind.c_str(), l.blob.c_str(), + l.src.c_str(), l.dst.c_str(), l.C_in, l.C_out, l.w.c_str(), l.b.c_str(), l.acc.c_str(), + l.out_round.c_str(), l.C, l.heads, l.mlp, l.offsets.c_str(), l.params.c_str(), join(l.groups).c_str(), + clamp, join(l.values).c_str()); +} + +} // namespace + +int main(int argc, char** argv) { + const std::string mode = argc > 1 ? argv[1] : ""; + if (mode == "pack" && argc == 4) { + std::map params; + for (const auto& e : fs::directory_iterator(argv[2])) + if (e.is_directory() && e.path().filename() != "out") + read_stage(e.path(), ¶ms[e.path().filename().string()]); + std::map> blobs; + std::string err; + if (!wgrt::l::pack_model(params, wgrt::drow_gfx12(), &blobs, &err)) return std::printf("ERR %s\n", err.c_str()), 0; + for (const auto& kv : blobs) { + std::ofstream f(fs::path(argv[3]) / (kv.first + ".bin"), std::ios::binary); + f.write(reinterpret_cast(kv.second.data()), (std::streamsize)kv.second.size()); + } + std::printf("OK %zu\n", blobs.size()); + return 0; + } + if (mode == "launches") { + for (const wgrt::l::Launch& l : wgrt::l::encoder_launches()) print("enc", l); + for (const wgrt::l::Launch& l : wgrt::l::decoder_launches()) print("dec", l); + return 0; + } + std::printf("ERR usage\n"); + return 2; +} diff --git a/clean/l/runtime/tests/test_l_runtime_pack.py b/clean/l/runtime/tests/test_l_runtime_pack.py new file mode 100644 index 00000000..4c7eaed8 --- /dev/null +++ b/clean/l/runtime/tests/test_l_runtime_pack.py @@ -0,0 +1,172 @@ +# SPDX-License-Identifier: MIT +"""Model L's runtime packer (clean/l/runtime/l_pack.cpp) against clean/l/runner/l_pack.py, byte for byte (#89): + +- every blob of pack_model_l for random parameters (clean/l/reference/oracle.py random_params), three seeds: the + encoder (values, the input linears Win1 / Win2, enc0_block, the projection) and the decoder (dec0.project, + dec0_block, dec0.coef); +- the default table's encoder and decoder launch lists against l_pack.encoder_launches / decoder_launches, field by + field; +- the refusals (missing stage, missing tensor, wrong size). + +No GPU: clean/l/runtime/tests/l_pack_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_pack.py +""" +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.l.runner import l_pack as LP + +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_pack_dump.cpp"), + os.path.join("clean", "l", "runtime", "l_pack.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")] +_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_lp_") + exe = os.path.join(tmp, "l_pack_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_pack_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() + + +STAGES = ("enc0", "enc0_block", "dec0", "dec0_block") # Model L's own stages (the map's names) + + +def write_params(root, params): + """Model L's own stages as /.f32 (and enc0/T.u8: the dither table as the runtime's loader keeps it).""" + for stage in STAGES: + if stage not in params: + continue + d = os.path.join(root, stage) + os.makedirs(d) + for name, a in params[stage].items(): + if stage == "enc0" and name == "T": + np.asarray(a).astype(np.uint8).tofile(os.path.join(d, "T.u8")) + else: + np.ascontiguousarray(a, "* b, const float* vec, const Order& R, } Order rows_identity(int n) { return wgrt::rows_identity(n); } Order rows_layout_l(int C, const DRow& drow) { return wgrt::rows_layout_l(C, drow); } +Order rows_expand(int C, const DRow& drow) { return wgrt::rows_expand(C, drow); } Order kord_layout_l(int C) { return wgrt::kord_layout_l(C); } Order kord_drow(int n, const DRow& drow) { return wgrt::kord_drow(n, drow); } size_t align_up(size_t n) { return wgrt::align_up(n); } diff --git a/clean/runtime/model.h b/clean/runtime/model.h index 5254acc6..2abace37 100644 --- a/clean/runtime/model.h +++ b/clean/runtime/model.h @@ -100,6 +100,7 @@ void put_frags(std::vector* b, const std::vector>& void put_tile_vector(std::vector* b, const float* vec, const Order& R, const DRow& drow); Order rows_identity(int n); Order rows_layout_l(int C, const DRow& drow); // R_L(C) +Order rows_expand(int C, const DRow& drow); // R_E(C): rows of an expand section (packing.rows_expand) Order kord_layout_l(int C); // K_L(C) Order kord_drow(int n, const DRow& drow); size_t align_up(size_t n); // to 256 bytes (packing.ALIGN)