diff --git a/.gitattributes b/.gitattributes index dc1b7ba2..466d97e8 100644 --- a/.gitattributes +++ b/.gitattributes @@ -4,3 +4,6 @@ bench/prompts/** -text # launchers keep their platform line endings wherever the repository is cloned *.sh text eol=lf *.bat text eol=crlf + +# copies of real packs' native_experts.txt, parsed byte for byte by tests/core/expert_layout_test.cpp +tests/data/native_experts/** -text diff --git a/CMakeLists.txt b/CMakeLists.txt index bc3c0b2b..ab2eef40 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -148,6 +148,10 @@ if(STRATA_BUILD_TESTS) add_executable(gguf_reader_test src/artifact/gguf_reader_test.cpp) target_link_libraries(gguf_reader_test PRIVATE strata_artifact) add_test(NAME gguf_reader_test COMMAND gguf_reader_test) + # a split model's shards and their joint view: missing shard, split keys, duplicates, truncation (synthetic) + add_executable(gguf_split_test tests/core/gguf_split_test.cpp) + target_link_libraries(gguf_split_test PRIVATE strata_artifact) + add_test(NAME gguf_split_test COMMAND gguf_split_test) endif() # ---- strata_plan ---------------------------------------------------------- @@ -321,6 +325,14 @@ if(STRATA_ENABLE_CUDA OR STRATA_ENABLE_HIP) add_test(NAME file_expert_source_test COMMAND file_expert_source_test) set_tests_properties(file_expert_source_test PROPERTIES TIMEOUT 60) endif() + if(STRATA_BUILD_TESTS AND STRATA_NATIVE_EXPERTS) + # native_experts.txt v2-v4 (the real packs' copies and the per-role shard column) and the arena filled from + # synthetic GGUF shards at the real expert dimensions + add_executable(expert_layout_test tests/core/expert_layout_test.cpp) + target_link_libraries(expert_layout_test PRIVATE strata_engine) + add_test(NAME expert_layout_test COMMAND expert_layout_test ${CMAKE_CURRENT_SOURCE_DIR}/tests/data/native_experts) + set_tests_properties(expert_layout_test PROPERTIES TIMEOUT 60) + endif() if(STRATA_BUILD_CONVERSATION_TESTS) add_executable(conversation_snapshot_test src/core/conversation_snapshot_test.cpp) target_link_libraries(conversation_snapshot_test PRIVATE strata_engine) diff --git a/include/strata/artifact/gguf_reader.hpp b/include/strata/artifact/gguf_reader.hpp index ee76f3c0..42e0665e 100644 --- a/include/strata/artifact/gguf_reader.hpp +++ b/include/strata/artifact/gguf_reader.hpp @@ -23,6 +23,8 @@ #include #include #include +#include +#include #include #include #include @@ -30,6 +32,8 @@ #include #include +#include "strata/artifact/gguf_split.hpp" + #ifdef _WIN32 #define WIN32_LEAN_AND_MEAN #ifndef NOMINMAX @@ -342,6 +346,8 @@ public: uint32_t version() const { return version_; } uint64_t data_start() const { return data_start_; } uint64_t file_size() const { return size_; } + uint64_t alignment() const { return alignment_; } + const std::string& path() const { return path_; } const TensorInfo* find(const std::string& name) const { for (const auto& t : tensors_) @@ -460,6 +466,104 @@ private: std::map meta_; }; +// Bytes of a tensor's payload from its shape and block geometry; 0 when the type is unknown, a row is not whole +// blocks, or the count overflows. +inline uint64_t tensor_payload_bytes(const TensorInfo& t) { + int be = 0, bb = 0; + if (t.shape.empty() || !block_geometry(t.type, be, bb) || t.shape[0] % (uint64_t) be) return 0; + uint64_t elements = 1; + for (uint64_t d : t.shape) { + if (d == 0 || elements > (std::numeric_limits::max)() / d) return 0; + elements *= d; + } + const uint64_t blocks = elements / (uint64_t) be; + if (blocks > (std::numeric_limits::max)() / (uint64_t) bb) return 0; + return blocks * (uint64_t) bb; +} + +// The shards of one model (strata::gguf_split_paths), opened together; tensors are looked up across all of them. +// From eddoursul/Strata 8029fa9, with the split-key validation of #255 (gopinath87607) made a property of the +// model rather than of one loader: a split GGUF carries the model's metadata (general.architecture and the +// qwen4exp keys) in its FIRST shard only, and the later shards just declare split.count / split.no / +// split.tensors.count. Unsloth's UD-Q4_K_XL is the extreme case: shard 1 holds the metadata and no tensor at +// all, and output.weight, token_embd.weight and the PLE table live in shard 2. So the architecture is checked +// on `meta()` and a tensor is read from the shard that holds it. +// +// Refused at construction: a later shard whose split keys disagree with shard 1's (another model's shard, or a +// shard renamed into the family), a split model whose tensor directories do not add up to split.tensors.count, +// and a tensor name present in two shards (GGUF has no index to say which one is meant). +class GgufModel { +public: + explicit GgufModel(const std::vector& paths) { + if (paths.empty()) throw std::runtime_error("GGUF: a model needs at least one shard"); + for (const auto& p : paths) shards_.push_back(std::make_unique(p)); + validate_split(); + for (size_t i = 0; i < shards_.size(); ++i) + for (const auto& t : shards_[i]->tensors()) { + const auto ins = index_.emplace(t.name, std::make_pair(i, &t)); + if (!ins.second) + throw std::runtime_error("GGUF: tensor " + t.name + " is in two shards (" + + shards_[ins.first->second.first]->path() + " and " + + shards_[i]->path() + ")"); + } + } + /// Opens every shard of the model that `any_shard` belongs to (throws when one is missing). + static GgufModel open(const std::string& any_shard) { return GgufModel(gguf_split_paths(any_shard)); } + + size_t size() const { return shards_.size(); } + const GgufFile& shard(size_t i) const { return *shards_[i]; } + /// The metadata shard: general.architecture and the model's keys. + const GgufFile& meta() const { return *shards_[0]; } + /// The tensor named `name` (its shard index in `*shard`), or nullptr. + const TensorInfo* find(const std::string& name, size_t* shard = nullptr) const { + const auto it = index_.find(name); + if (it == index_.end()) return nullptr; + if (shard) *shard = it->second.first; + return it->second.second; + } + /// Whether `t` (a tensor of shard `s`) has a known byte count that lies inside its file. + bool in_bounds(const TensorInfo& t, size_t s) const { + const GgufFile& g = *shards_[s]; + const uint64_t bytes = tensor_payload_bytes(t); + const uint64_t payload = g.file_size() - g.data_start(); + return bytes != 0 && t.offset <= payload && bytes <= payload - t.offset; + } + +private: + void validate_split() const { + const size_t n = shards_.size(); + const MetaValue* count0 = shards_[0]->get("split.count"); + if (n == 1) { + if (count0 && count0->u > 1) + throw std::runtime_error("GGUF: " + shards_[0]->path() + " is shard 1 of " + std::to_string(count0->u) + + ", but it was opened as a whole model"); + return; + } + if (!shards_[0]->get("general.architecture")) + throw std::runtime_error("GGUF: " + shards_[0]->path() + " has no general.architecture; the first shard " + "of a split model carries the metadata"); + const MetaValue* total = shards_[0]->get("split.tensors.count"); + uint64_t tensors = 0; + for (size_t i = 0; i < n; ++i) { + const GgufFile& g = *shards_[i]; + const MetaValue* count = g.get("split.count"); + const MetaValue* no = g.get("split.no"); + const MetaValue* tc = g.get("split.tensors.count"); + if (!count || !no || count->u != n || no->u != i || (total && (!tc || tc->u != total->u))) + throw std::runtime_error("GGUF: " + g.path() + " does not declare itself shard " + std::to_string(i + 1) + + " of " + std::to_string(n) + " of this model (split.count / split.no / " + "split.tensors.count)"); + tensors += g.tensors().size(); + } + if (total && tensors != total->u) + throw std::runtime_error("GGUF: the " + std::to_string(n) + " shards hold " + std::to_string(tensors) + + " tensors, but split.tensors.count is " + std::to_string(total->u)); + } + + std::vector> shards_; + std::map> index_; +}; + // ---- architecture guard (P1.S2). The engine is specialised to ONE model; anything else must be // refused with a precise error rather than silently mis-run. struct Qwen4ExpGuard { diff --git a/include/strata/artifact/gguf_split.hpp b/include/strata/artifact/gguf_split.hpp new file mode 100644 index 00000000..0f01001f --- /dev/null +++ b/include/strata/artifact/gguf_split.hpp @@ -0,0 +1,52 @@ +// include/strata/artifact/gguf_split.hpp - the shards of a split model. Separate from gguf_reader.hpp, which +// includes , so any translation unit can resolve a model's shards. +// +// From eddoursul/Strata 8029fa9 (gguf_split_paths), with one change of contract: upstream's model_shards() +// skipped a shard it could not open and returned the rest, so a download that was still running (or a shard +// deleted by hand) produced a model with some layers missing and an error much later, or none. A missing shard +// is now an error that names it. +#pragma once + +#include +#include +#include +#include +#include + +namespace strata { + +// Split models: `-00001-of-0000N.gguf` ... `-0000N-of-0000N.gguf`, as llama.cpp's gguf-split names +// them. Given ANY shard's path, the shards in split order (the metadata shard, 00001, first); a file without that +// suffix is a model of one shard. Only the five digits of the shard number are rewritten, so the directory and +// the stem are the caller's own spelling (a symlinked Hugging Face snapshot keeps its split names). Throws when +// a shard is missing. +inline std::vector gguf_split_paths(const std::string& any) { + static const char tag[] = "-of-"; + const size_t at = any.rfind(tag); + // "...-NNNNN-of-MMMMM.gguf": five digits before the tag, five after it, then the extension + if (at == std::string::npos || at < 6 || any[at - 6] != '-' || any.size() != at + 4 + 5 + 5 || + any.compare(at + 9, 5, ".gguf") != 0) + return {any}; + int no = 0, n = 0; + for (size_t i = 0; i < 5; ++i) { + const char a = any[at - 5 + i], b = any[at + 4 + i]; + if (a < '0' || a > '9' || b < '0' || b > '9') return {any}; + no = no * 10 + (a - '0'); + n = n * 10 + (b - '0'); + } + if (n < 1 || no < 1 || no > n) return {any}; + std::vector out; + for (int i = 1; i <= n; ++i) { + char num[8]; + std::snprintf(num, sizeof num, "%05d", i); + std::string p = any; + p.replace(at - 5, 5, num); + if (!std::ifstream(p, std::ios::binary)) + throw std::runtime_error("missing model shard " + p + " (shard " + std::to_string(i) + " of " + + std::to_string(n) + "; is the download complete?)"); + out.push_back(std::move(p)); + } + return out; +} + +} // namespace strata diff --git a/include/strata/core/expert_source.hpp b/include/strata/core/expert_source.hpp index 372412d6..8af105aa 100644 --- a/include/strata/core/expert_source.hpp +++ b/include/strata/core/expert_source.hpp @@ -32,9 +32,14 @@ #include #include +namespace strata::kernels::cpu { +struct ExpertLayout; +} + namespace strata::core { class RemoteExperts; +struct LoadStats; namespace detail { @@ -450,8 +455,9 @@ public: /// rate, because those are the two numbers that say whether the arena is the one that was asked for. bool open(const std::string& pack_dir, int64_t n_layers, int64_t n_expert, int threads, std::string& err, uint64_t max_pinned_bytes = 0, const std::string& shared_arena_file = {}); - /// Plan v0.3 P6: a native pack without experts.bin takes its experts from the model's shard 1. - void set_gguf(const std::string& shard1) { gguf_ = shard1; } + /// Plan v0.3 P6: a native pack without experts.bin takes its experts from the model's GGUF: `native` is the + /// --native shard, and native_experts.txt names the other shards beside it (per layer, or per role in v4). + void set_gguf(const std::string& native) { gguf_ = native; } void close(); bool mapped() const { return base_ != nullptr; } @@ -489,4 +495,12 @@ private: std::string gguf_; }; +/// Plan v0.3 P6: checks native_experts.txt's GGUF spans against the files, before anything is read: each layer's +/// gate/up/down at its recorded (file, offset) must be that tensor (`blk.L.ffn__exps.weight`), of the +/// layout's type and dimensions, and inside the file. `native` is the --native shard (see set_gguf). +bool check_experts_gguf(const std::string& native, const strata::kernels::cpu::ExpertLayout& lay, std::string& err); +/// Fills `dst` (lay.total bytes, the experts.bin layout) from the GGUF files, one role at a time. +LoadStats load_experts_gguf(const std::string& native, uint8_t* dst, const strata::kernels::cpu::ExpertLayout& lay, + int threads); + } // namespace strata::core diff --git a/include/strata/core/native_head.hpp b/include/strata/core/native_head.hpp index 22b4c8db..ff45dd6e 100644 --- a/include/strata/core/native_head.hpp +++ b/include/strata/core/native_head.hpp @@ -3,12 +3,15 @@ #include #include #include +#include namespace strata::core { /// Opt-in native output projection. Uploads unchanged GGUF blocks once at startup and owns its /// activation scratch. The normal canonical head remains available for numerical A/B comparisons. /// Plan v0.3 P6: any type the native MMVQ takes (Q5_K in the Q2_0 / IQ3_XXS files, IQ4_XS in IQ2_XS). +/// `load` takes the model's shards (strata::gguf_split_paths): the architecture is checked on the first, the +/// tensor is read from the shard that holds it. class NativeHead { public: NativeHead() = default; @@ -16,7 +19,7 @@ public: NativeHead(const NativeHead&) = delete; NativeHead& operator=(const NativeHead&) = delete; - bool load(const std::string& gguf, int64_t n_in, int64_t n_out, std::string& err); + bool load(const std::vector& shards, int64_t n_in, int64_t n_out, std::string& err); bool run(const float* mixed, float* logits, void* stream, std::string& err) const; uint64_t weight_bytes() const { return bytes_; } bool loaded() const { return weights_ != nullptr; } @@ -43,7 +46,7 @@ public: ~NativeEmbed(); NativeEmbed(const NativeEmbed&) = delete; NativeEmbed& operator=(const NativeEmbed&) = delete; - bool load(const std::string& gguf, int64_t n_embd, int64_t n_vocab, std::string& err); + bool load(const std::vector& shards, int64_t n_embd, int64_t n_vocab, std::string& err); /// Rows for device token ids. void gather_dev(const int32_t* tokens, int64_t n_tok, float* out, void* stream) const; /// One row for a host token id. diff --git a/include/strata/kernels/cpu/expert_layout.hpp b/include/strata/kernels/cpu/expert_layout.hpp index 126549ae..ccbf0f8a 100644 --- a/include/strata/kernels/cpu/expert_layout.hpp +++ b/include/strata/kernels/cpu/expert_layout.hpp @@ -20,12 +20,15 @@ struct ExpertLayout { int64_t n_layers = 0, n_expert = NE; std::vector fmt; ///< per layer (native packs) std::vector offset, bytes; ///< per layer: where its 512 blobs start, bytes per blob - /// Plan v0.3 P6: per layer, the absolute offsets of the gate / up / down tensors in the model's shard 1, so - /// the arena can be filled from the GGUF itself when the pack has no experts.bin (3 x n_layers, 0 = unknown). + /// Plan v0.3 P6: per layer, the absolute offsets of the gate / up / down tensors in their GGUF files, so the + /// arena can be filled from the GGUF itself when the pack has no experts.bin (3 x n_layers, 0 = unknown). std::vector gguf_off; - /// Per layer, the GGUF file (a name beside the --native shard) that holds its experts when the model's - /// shards split the layers (Swift's GGUFs: layers 13-47 in shard 2). Empty = the --native shard itself. + /// Per layer PER ROLE (3 x n_layers, `3 * layer + role`, matching `gguf_off`), the GGUF file - a name beside + /// the --native shard - that holds that role's tensor. Empty = the --native shard itself. Per role because a + /// shard boundary can fall inside a layer: Unsloth's UD-Q4_K_XL has layer 11's down in shard 2 and its gate + /// and up in shard 3 (native_experts.txt v4, the `gate,up,down` shard column). std::vector gguf_file; + int version = 0; ///< native_experts.txt's header version (0 = none given) uint64_t max_blob = BLOB; uint64_t total = 0; ///< experts.bin size @@ -49,6 +52,10 @@ void act_quant_any(const float* x, int n, ActQ& a); /// The process-wide layout (canonical Q2_0 until `expert_layout_load` finds a native pack). const ExpertLayout& expert_layout(); /// Reads `/native_experts.txt` when it exists (a native pack), else sets the canonical layout. +/// Versions up to kExpertLayoutVersion are read; a newer one is refused (a newer packer wrote it). bool expert_layout_load(const std::string& pack_dir, int64_t n_layers, int64_t n_expert, std::string& err); +/// The newest native_experts.txt this engine reads. v4 = v3 plus the per-role shard column `gate,up,down`, +/// written only when some layer's roles are in different shards (every other pack stays v3, byte for byte). +inline constexpr int kExpertLayoutVersion = 4; } // namespace strata::kernels::cpu diff --git a/src/core/expert_source.cpp b/src/core/expert_source.cpp index bb550123..71a2ecc5 100644 --- a/src/core/expert_source.cpp +++ b/src/core/expert_source.cpp @@ -2,6 +2,7 @@ #include "strata/core/expert_source.hpp" #include "strata/core/remote_experts.hpp" #include "strata/kernels/cpu/expert_layout.hpp" +#include "strata/artifact/gguf_reader.hpp" #include "strata/core/pinned.hpp" #include "strata/platform/memory.hpp" @@ -21,6 +22,8 @@ #include #include #include +#include +#include #include #include #include @@ -1400,9 +1403,75 @@ bool shared_arena_pack_hash(const std::string& pack_dir, const std::string& expe } // namespace -// Plan v0.3 P6: the arena from the model's shard 1. Each layer's gate, up and down tensors hold the 512 experts -// one after another; they are read in chunks and each expert's slice lands at its place in the blob -// [gate rows | up rows | down rows] - the layout tools/iq_pack.py would have written to experts.bin. +namespace { +/// The GGUF file that holds role `r` (0 gate, 1 up, 2 down) of layer `l`: a name beside the --native shard +/// (native_experts.txt v3 per layer, v4 per role), or the --native shard itself. +std::string expert_gguf_file(const std::string& gguf, const strata::kernels::cpu::ExpertLayout& lay, int64_t l, int r) { + const size_t i = (size_t) (3 * l + r); + if (lay.gguf_file.size() <= i || lay.gguf_file[i].empty()) return gguf; + const size_t cut = gguf.find_last_of("/\\"); + return (cut == std::string::npos ? std::string() : gguf.substr(0, cut + 1)) + lay.gguf_file[i]; +} +} // namespace + +bool check_experts_gguf(const std::string& gguf, const strata::kernels::cpu::ExpertLayout& lay, std::string& err) { + static const char* roles[3] = {"gate", "up", "down"}; + if (lay.gguf_off.size() != (size_t) (3 * lay.n_layers)) { + err = "native_experts.txt has no GGUF offsets (a pack older than v2): repack it with tools/iq_pack.py"; + return false; + } + try { + std::map> files; + for (int64_t l = 0; l < lay.n_layers; ++l) { + const auto& fm = lay.fmt[(size_t) l]; + const uint64_t blob = lay.bytes[(size_t) l]; + const uint64_t per[3] = {fm.up_off, fm.up_off, blob - fm.down_off}; + for (int r = 0; r < 3; ++r) { + const std::string path = expert_gguf_file(gguf, lay, l, r); + auto& f = files[path]; + if (!f) f = std::make_unique(path); + const std::string name = "blk." + std::to_string(l) + ".ffn_" + roles[r] + "_exps.weight"; + const strata::TensorInfo* t = f->find(name); + const uint64_t want_type = (uint64_t) (r < 2 ? fm.gu_type : fm.d_type); + // GGUF order: dim 0 is the row (the input), dim 1 the rows, dim 2 the experts + const uint64_t cols = (uint64_t) (r < 2 ? fm.n_embd : fm.n_ff); + const uint64_t rows = (uint64_t) (r < 2 ? fm.n_ff : fm.n_embd); + const uint64_t bytes = per[r] * (uint64_t) lay.n_expert; + const uint64_t payload = f->file_size() - f->data_start(); + std::string why; + if (t == nullptr) why = "is not in it"; + else if (t->type != want_type) + why = std::string("is ") + t->type_name() + ", the pack says type " + std::to_string(want_type); + else if (t->shape.size() != 3 || t->shape[0] != cols || t->shape[1] != rows || + t->shape[2] != (uint64_t) lay.n_expert) + why = "is not [" + std::to_string(cols) + ", " + std::to_string(rows) + ", " + + std::to_string(lay.n_expert) + "]"; + else if (strata::tensor_payload_bytes(*t) != bytes) + why = "is not " + std::to_string(per[r]) + " B per expert"; + else if (f->data_start() + t->offset != lay.gguf_off[(size_t) (3 * l + r)]) + why = "starts at byte " + std::to_string(f->data_start() + t->offset) + ", the pack says " + + std::to_string(lay.gguf_off[(size_t) (3 * l + r)]); + else if (t->offset > payload || bytes > payload - t->offset) + why = "runs past the end of the file (a truncated shard?)"; + if (!why.empty()) { + err = "the pack's native_experts.txt does not match the model: " + name + " in " + path + " " + + why + " - repack with tools/iq_pack.py from this model's shards"; + return false; + } + } + } + return true; + } catch (const std::exception& e) { + err = std::string("native experts from the GGUF: ") + e.what(); + return false; + } +} + +// Plan v0.3 P6: the arena from the model's GGUF shards. Each layer's gate, up and down tensors hold the 512 +// experts one after another; they are read in chunks and each expert's slice lands at its place in the blob +// [gate rows | up rows | down rows] - the layout tools/iq_pack.py would have written to experts.bin. Each role +// is read from its own file (native_experts.txt v4: a shard boundary can fall inside a layer; per role as in +// #255, gopinath87607). The caller checks the spans first (check_experts_gguf). LoadStats load_experts_gguf(const std::string& gguf, uint8_t* dst, const strata::kernels::cpu::ExpertLayout& lay, int threads) { LoadStats st; @@ -1410,13 +1479,6 @@ LoadStats load_experts_gguf(const std::string& gguf, uint8_t* dst, const strata: const auto t0 = std::chrono::steady_clock::now(); std::atomic next{0}; std::atomic bad{false}; - // a layer's experts may sit in another shard of the model (native_experts.txt v3): a name beside `gguf` - const size_t cut = gguf.find_last_of("/\\"); - const std::string dir = cut == std::string::npos ? std::string() : gguf.substr(0, cut + 1); - auto file_of = [&](int64_t l) -> std::string { - if (lay.gguf_file.empty() || lay.gguf_file[(size_t) l].empty()) return gguf; - return dir + lay.gguf_file[(size_t) l]; - }; auto worker = [&]() { std::ifstream f; std::string open_name; @@ -1424,19 +1486,20 @@ LoadStats load_experts_gguf(const std::string& gguf, uint8_t* dst, const strata: for (;;) { const int64_t l = next.fetch_add(1); if (l >= lay.n_layers || bad) break; - const std::string name = file_of(l); - if (name != open_name) { - f.close(); - f.clear(); - f.open(name, std::ios::binary); - if (!f) { bad = true; return; } - open_name = name; - } const auto& fm = lay.fmt[(size_t) l]; const uint64_t blob = lay.bytes[(size_t) l]; const uint64_t per[3] = {fm.up_off, fm.up_off, blob - fm.down_off}; const uint64_t at[3] = {0, fm.up_off, fm.down_off}; for (int r = 0; r < 3; ++r) { + // the handle is kept while consecutive roles share a file (every layer of a v3 pack) + const std::string name = expert_gguf_file(gguf, lay, l, r); + if (name != open_name) { + f.close(); + f.clear(); + f.open(name, std::ios::binary); + if (!f) { bad = true; return; } + open_name = name; + } const uint64_t src = lay.gguf_off[(size_t) (3 * l + r)]; const uint64_t total = per[r] * (uint64_t) lay.n_expert; const uint64_t chunk = per[r] * 16; // 16 experts per read @@ -1489,7 +1552,12 @@ bool ArenaExpertSource::open(const std::string& pack_dir, int64_t n_layers, int6 const bool from_gguf = !std::ifstream(path, std::ios::binary) && lay.native && !lay.gguf_off.empty() && !gguf_.empty(); // SIZE CHECK BEFORE THE ALLOCATION, not after. A wrong pack should name the two numbers rather than spend // 34 GB and a minute of loading first. - if (!from_gguf) { + if (from_gguf) { + // every (file, offset) of native_experts.txt must be the tensor it claims, of the pack's type and + // dimensions and inside its file - before the allocation, so a pack of another model or a truncated + // shard is a message rather than an arena of plausible wrong experts + if (!check_experts_gguf(gguf_, lay, err)) { err = "ArenaExpertSource: " + err; return false; } + } else { std::ifstream f(path, std::ios::binary | std::ios::ate); if (!f) { err = "ArenaExpertSource: cannot open " + path; return false; } const uint64_t got = (uint64_t) f.tellg(); diff --git a/src/core/native_head.cpp b/src/core/native_head.cpp index 2f58cfbd..5eed99d1 100644 --- a/src/core/native_head.cpp +++ b/src/core/native_head.cpp @@ -16,22 +16,21 @@ NativeHead::~NativeHead() { if (weights_) cudaFree(weights_); } -bool NativeHead::load(const std::string& path, int64_t n_in, int64_t n_out, std::string& err) { +bool NativeHead::load(const std::vector& shards, int64_t n_in, int64_t n_out, std::string& err) { if (loaded()) { err = "native head is already loaded"; return false; } if (n_in <= 0 || n_out <= 0 || n_in > INT_MAX || n_out > INT_MAX || n_in % 256) { err = "native head requires positive int32 dimensions and whole 256-value rows"; return false; } try { - strata::GgufFile gguf(path); - err = strata::check_architecture(gguf); + // The architecture is the metadata shard's; output.weight comes from whichever shard holds it (shard 2 + // of Unsloth's UD-Q4_K_XL, whose shard 1 holds no tensor). GgufModel refuses a duplicate across shards. + const strata::GgufModel model(shards); + err = strata::check_architecture(model.meta()); if (!err.empty()) return false; - const strata::TensorInfo* tensor = nullptr; - for (const auto& candidate : gguf.tensors()) { - if (candidate.name != "output.weight") continue; - if (tensor) { err = "native head: duplicate output.weight"; return false; } - tensor = &candidate; - } + size_t at = 0; + const strata::TensorInfo* tensor = model.find("output.weight", &at); + const strata::GgufFile& gguf = model.shard(at); if (!tensor || !strata::kernels::native_mmvq_supported((int) tensor->type) || tensor->shape.size() != 2 || tensor->shape[0] != (uint64_t) n_in || tensor->shape[1] != (uint64_t) n_out) { err = "native head: expected a natively supported output.weight with the canonical head dimensions"; @@ -106,12 +105,14 @@ NativeEmbed::~NativeEmbed() { else if (dev_) cudaFree(const_cast(dev_)); // the VRAM fallback below } -bool NativeEmbed::load(const std::string& path, int64_t n_embd, int64_t n_vocab, std::string& err) { +bool NativeEmbed::load(const std::vector& shards, int64_t n_embd, int64_t n_vocab, std::string& err) { try { - strata::GgufFile gguf(path); - const strata::TensorInfo* t = nullptr; - for (const auto& c : gguf.tensors()) - if (c.name == "token_embd.weight") t = &c; + const strata::GgufModel model(shards); + err = strata::check_architecture(model.meta()); + if (!err.empty()) { err = "native embedding: " + err; return false; } + size_t at = 0; + const strata::TensorInfo* t = model.find("token_embd.weight", &at); + const strata::GgufFile& gguf = model.shard(at); if (!t || t->shape.size() != 2 || t->shape[0] != (uint64_t) n_embd || t->shape[1] != (uint64_t) n_vocab || !strata::kernels::iq_supported((int) t->type) || n_embd % 256) { err = "native embedding: token_embd.weight is absent, of another shape, or of a type without a GPU " @@ -120,6 +121,13 @@ bool NativeEmbed::load(const std::string& path, int64_t n_embd, int64_t n_vocab, } row_ = strata::kernels::iq_row_bytes((int) t->type, n_embd); bytes_ = (uint64_t) row_ * (uint64_t) n_vocab; + // the table is copied out of the mapping below: a truncated shard must be an error, not a read past EOF + if (!model.in_bounds(*t, at) || strata::tensor_payload_bytes(*t) != bytes_) { + err = "native embedding: token_embd.weight's payload is truncated or not " + std::to_string(bytes_) + + " B (" + gguf.path() + ")"; + bytes_ = 0; + return false; + } if (cudaHostAlloc(&host_, bytes_, cudaHostAllocMapped | cudaHostAllocPortable) != cudaSuccess) { // Under WSL2 the driver's pinned/mapped host budget (~1 GiB) can be spent by the GPU contexts // themselves (three cards). The table is only gathered from, so keep it in the current device's VRAM diff --git a/src/kernels/cpu/expert_layout.cpp b/src/kernels/cpu/expert_layout.cpp index ec78774c..6d4475be 100644 --- a/src/kernels/cpu/expert_layout.cpp +++ b/src/kernels/cpu/expert_layout.cpp @@ -95,6 +95,18 @@ bool expert_layout_load(const std::string& pack_dir, int64_t n_layers, int64_t n while (std::getline(in, line)) { if (line.empty() || line[0] == '#') { if (!line.empty() && line[0] == '#') { + // "# strata native experts vN: ...". A version this engine does not know may mean columns it + // would misread, so it is refused rather than parsed as far as the known columns go. + static const char tag[] = "# strata native experts v"; + if (line.compare(0, sizeof tag - 1, tag) == 0) { + L.version = std::atoi(line.c_str() + sizeof tag - 1); + if (L.version > kExpertLayoutVersion) { + err = "native_experts.txt is v" + std::to_string(L.version) + "; this engine reads up to v" + + std::to_string(kExpertLayoutVersion) + " (the pack was written by a newer tools/" + "iq_pack.py: update the engine, or repack with this one)"; + return false; + } + } // v3 packs record their expert count in the header; a pruned model (GSQ-RCO Coder) ships // fewer experts than the canonical geometry the caller passes, which is a compile-time // default, so the header wins. @@ -124,8 +136,29 @@ bool expert_layout_load(const std::string& pack_dir, int64_t n_layers, int64_t n L.gguf_off[(size_t) (3 * l + 2)] = dox; std::string file; // v3: the shard that holds this layer (a file name beside --native) if (ss >> file) { - if (L.gguf_file.empty()) L.gguf_file.assign((size_t) n_layers, std::string()); - L.gguf_file[(size_t) l] = file; + if (L.gguf_file.empty()) L.gguf_file.assign((size_t) (3 * n_layers), std::string()); + // One name covers all three roles. When a shard boundary falls inside a layer the packer writes + // "gate,up,down" instead (v4), an empty field meaning the --native shard. A GGUF file name has no + // comma, so the split is unambiguous; an engine older than v4 fails to open such a "file" loudly. + // The comma form is from #255 (gopinath87607). + std::vector parts; + for (size_t from = 0;;) { + const size_t comma = file.find(',', from); + parts.push_back(file.substr(from, comma == std::string::npos ? comma : comma - from)); + if (comma == std::string::npos) break; + from = comma + 1; + } + if (parts.size() == 1) { + const std::string one = parts[0]; // not parts.assign(3, parts[0]): that aliases the element + parts.assign(3, one); // the assignment is about to overwrite + } + std::string extra; + if (parts.size() != 3 || (ss >> extra)) { + err = "native_experts.txt: layer " + std::to_string(l) + ": the shard column is one name or " + "gate,up,down, not: " + line; + return false; + } + for (size_t r = 0; r < 3; ++r) L.gguf_file[(size_t) (3 * l) + r] = parts[r]; } } L.fmt[(size_t) l] = f; diff --git a/src/program/generate.cpp b/src/program/generate.cpp index a30746ff..94de0ea7 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -192,6 +192,9 @@ struct Options { bool cpu_oracle_q8_0 = false; // pinned x86 activation scales/codes at both expert stages std::string native_head_gguf; // native output.weight experiment; same model shard as the pack std::vector native_dense_gguf; // repeat for native GDN/QSA projection shards + /// Every shard of --native's model (strata::gguf_split_paths: the metadata shard first; a missing shard is an + /// error) and of --native-head-gguf's (the same list unless that names another model). + std::vector native_shards, native_head_shards; /// Plan v0.3 P1: the whole native arithmetic set as ONE switch (model shard 1). It enables exactly the /// combination recorded in bench/results/2026-09-23-attention-ple plus the native indexer, and never the /// <=256-token attention adapter. It becomes the default once P0 shows it is not slower. @@ -384,7 +387,8 @@ void usage() { " --pack DIR the pack directory (default pack/full)\n" " --tokens LIST the prompt as comma-separated token IDS (required)\n" " --tokens-file PATH pretokenized prompt, commas or whitespace (alternative to --tokens)\n" - " --ple-gguf PATH required PLE table (original second GGUF shard)\n" + " --ple-gguf PATH required PLE table (original second GGUF shard); with --native, the model's\n" + " shard that holds per_layer_token_embd.weight when not given\n" " --no-ple explicit diagnostic ablation of the PLE layer\n" " --ple-io direct|mmap|ram n-gram table reads (plan v0.3 P2). direct (default): unbuffered SSD\n" " reads, the table never enters RAM or the file cache; mmap: A/B arm;\n" @@ -528,23 +532,6 @@ void usage() { " does not fit. Same answers as --mmap-experts for the same placement.\n"); } -/// All shards of a split GGUF, from shard 1's path ("...-00001-of-00002.gguf"); just the path when it is not split. -std::vector model_shards(const std::string& first) { - const std::string tag = "-00001-of-"; - const size_t at = first.rfind(tag); - if (at == std::string::npos || first.size() < at + tag.size() + 10) return {first}; - const int total = std::atoi(first.substr(at + tag.size(), 5).c_str()); - std::vector out; - for (int i = 1; i <= total && i <= 99; ++i) { - char num[8]; - std::snprintf(num, sizeof num, "%05d", i); - std::string p = first; - p.replace(at + 1, 5, num); - if (std::ifstream(p, std::ios::binary)) out.push_back(p); - } - return out.empty() ? std::vector{first} : out; -} - bool parse_i64_list(const char* s, std::vector& out, std::string& err) { out.clear(); std::string text(s); @@ -1364,8 +1351,24 @@ int main(int argc, char** argv) { } strata::core::layer_set_shared_early(!o.shared_late); if (!o.native_preset.empty()) { + try { + // every shard of the model (-0000N-of-0000M.gguf beside --native). A missing shard is an error + // here: it used to be skipped, leaving a model with some tensors absent and a later error, or none. + o.native_shards = strata::gguf_split_paths(o.native_preset); + // --ple-gguf defaults to the shard that holds the PLE table, found by name: shard 2 of the ISTA files + // and of Unsloth's UD-Q4_K_XL, shard 1 of Swift's + if (o.ple_gguf.empty() && !o.no_ple) { + const strata::GgufModel model(o.native_shards); + size_t at = 0; + if (model.find("per_layer_token_embd.weight", &at) != nullptr) o.ple_gguf = o.native_shards[at]; + } + } catch (const std::exception& e) { + std::fprintf(stderr, "strata generate: --native %s: %s\n", o.native_preset.c_str(), e.what()); + return 2; + } if (o.no_ple || o.ple_gguf.empty()) { - std::fprintf(stderr, "strata generate: --native requires --ple-gguf (the PLE key is native too)\n"); + std::fprintf(stderr, "strata generate: --native requires --ple-gguf (the PLE key is native too), and no " + "shard of the model holds per_layer_token_embd.weight\n"); return 2; } o.stream_token = true; @@ -1377,7 +1380,7 @@ int main(int argc, char** argv) { if (o.native_dense_gguf.empty()) { // every shard of the model (-0000N-of-0000M.gguf beside --native), then the PLE shard: a split // may put any layer in any shard (Swift's GGUFs: layers 13-47 in shard 2, the PLE table in shard 1) - o.native_dense_gguf = model_shards(o.native_preset); + o.native_dense_gguf = o.native_shards; if (std::find(o.native_dense_gguf.begin(), o.native_dense_gguf.end(), o.ple_gguf) == o.native_dense_gguf.end()) o.native_dense_gguf.push_back(o.ple_gguf); } @@ -1480,6 +1483,15 @@ int main(int argc, char** argv) { "session, which a layer split does not have; run them without --layer-split\n"); return 2; } + if (!o.native_head_gguf.empty()) { + try { + o.native_head_shards = o.native_head_gguf == o.native_preset ? o.native_shards + : strata::gguf_split_paths(o.native_head_gguf); + } catch (const std::exception& e) { + std::fprintf(stderr, "strata generate: --native-head-gguf %s: %s\n", o.native_head_gguf.c_str(), e.what()); + return 2; + } + } if (o.native_ple_key && (o.native_dense_gguf.empty() || o.no_ple)) { std::fprintf(stderr, "strata generate: --native-ple-key requires PLE and --native-dense-gguf\n"); return 2; @@ -1570,7 +1582,7 @@ int main(int argc, char** argv) { // a pruned variant (GSQ-RCO Coder) ships fewer experts than the canonical 512x10; the model file // is the authority on its own MoE shape - everything else in the geometry is unchanged try { - strata::GgufFile model_gguf(o.native_preset); + strata::GgufFile model_gguf(o.native_shards.front()); // the metadata shard if (const strata::MetaValue* v = model_gguf.get("qwen4exp.expert_count")) g.n_expert = (int64_t) v->u; if (const strata::MetaValue* v = model_gguf.get("qwen4exp.expert_used_count")) K = (int64_t) v->u; if (const strata::MetaValue* v = model_gguf.get("qwen4exp.rope.freq_base")) gguf_rope_base = v->num(); @@ -1665,7 +1677,7 @@ int main(int argc, char** argv) { return 2; } const strata::core::ModelGeometry g0; - if (!native_embed.load(o.native_preset, g0.n_embd, 248320, err)) { + if (!native_embed.load(o.native_shards, g0.n_embd, 248320, err)) { std::fprintf(stderr, "strata generate: %s\n", err.c_str()); return 1; } @@ -2192,7 +2204,7 @@ int main(int argc, char** argv) { const bool last = i + 1 == stages.size(); const strata::core::WeightRef* wo_s = st.wt.find("output.weight"); if (wo_s == nullptr || - (last && !o.native_head_gguf.empty() && !st.head.load(o.native_head_gguf, g.n_embd, wo_s->ne1, err))) { + (last && !o.native_head_gguf.empty() && !st.head.load(o.native_head_shards, g.n_embd, wo_s->ne1, err))) { std::fprintf(stderr, "strata generate: layer split, CUDA%d head: %s\n", st.dev, wo_s == nullptr ? "output.weight is missing" : err.c_str()); return 1; @@ -2321,7 +2333,7 @@ int main(int argc, char** argv) { const int64_t n_vocab = wo->ne1; strata::core::NativeHead native_head; if (!o.native_head_gguf.empty() && !multi_gpu) { // a layer split's head is on its last stage - if (!native_head.load(o.native_head_gguf, g.n_embd, n_vocab, err)) { + if (!native_head.load(o.native_head_shards, g.n_embd, n_vocab, err)) { std::fprintf(stderr, "strata generate: %s\n", err.c_str()); return 1; } diff --git a/tests/core/expert_layout_test.cpp b/tests/core/expert_layout_test.cpp new file mode 100644 index 00000000..d532d746 --- /dev/null +++ b/tests/core/expert_layout_test.cpp @@ -0,0 +1,269 @@ +// tests/core/expert_layout_test.cpp - native_experts.txt (v2/v3/v4) and the expert arena filled from GGUF shards, +// without a model or a GPU. +// +// 1. Copies of the real packs' native_experts.txt (tests/data/native_experts: Q2_0, IQ2_XS, IQ3_XXS, IQ3_S, +// Coder IQ1_M, Swift IQ2_XS; v2 and v3 headers) parse to the layout the pre-v4 reader produced: the test +// parses each line again with that reader's rules (one shard name for all three roles) and compares every +// field, and the totals with the header's. +// 2. The v4 shard column `gate,up,down` (an empty field = the --native shard); v5, four names, two names and a +// trailing column are refused. +// 3. check_experts_gguf + load_experts_gguf on a synthetic arena at the real dimensions 2560/640 with Unsloth's +// format pairs (Q4_K/Q5_1, Q5_K/Q8_0): layer 1's gate/up in another file than its down. Every byte of the +// arena is compared with the source tensors. Then the refusals: an offset that is not the tensor's, a +// truncated file, a role named in the wrong file, a type or a shape the pack does not say, a missing file. +#include "gguf_fixture.hpp" + +#include "strata/core/expert_source.hpp" +#include "strata/core/pinned.hpp" +#include "strata/kernels/cpu/expert_layout.hpp" +#include "strata/kernels/cpu/native_expert.hpp" + +#include +#include +#include +#include +#include +#include +#include + +namespace fs = std::filesystem; +using strata::kernels::cpu::ExpertLayout; + +namespace { +int g_fail = 0; +void check(bool ok, const std::string& what) { + std::printf(" %-84s %s\n", what.c_str(), ok ? "ok" : "FAIL"); + if (!ok) ++g_fail; +} + +struct TempDir { + fs::path path; + TempDir() { + path = fs::temp_directory_path() / + ("strata-expert-layout-test-" + std::to_string(std::chrono::steady_clock::now().time_since_epoch().count())); + fs::create_directories(path); + } + ~TempDir() { + std::error_code ignored; + fs::remove_all(path, ignored); + } +}; + +void write_text(const fs::path& p, const std::string& s) { std::ofstream(p, std::ios::binary) << s; } + +bool load(const fs::path& dir, int64_t n_layers, int64_t n_expert, std::string& err) { + err.clear(); + return strata::kernels::cpu::expert_layout_load(dir.string(), n_layers, n_expert, err); +} + +// ---- 1. the real packs' files +void real_packs(const fs::path& data) { + for (const char* pack : {"q2_0", "iq2_xs", "iq3_xxs", "iq3_s", "coder-iq1_m", "swift-iq2_xs"}) { + TempDir d; + fs::copy_file(data / (std::string(pack) + ".txt"), d.path / "native_experts.txt"); + std::string err; + const bool ok = load(d.path, 48, 512, err); + check(ok, std::string(pack) + ": loads" + (ok ? "" : ": " + err)); + if (!ok) continue; + const ExpertLayout& L = strata::kernels::cpu::expert_layout(); + // the pre-v4 reader's rules, applied to the same text + std::ifstream in(d.path / "native_experts.txt"); + std::string line; + long long hdr_n = -1, hdr_total = -1; + bool same = L.native && L.n_layers == 48 && L.gguf_off.size() == 3 * 48; + uint64_t max_blob = 0; + while (std::getline(in, line)) { + if (line.empty()) continue; + if (line[0] == '#') { + if (line.find("(n_expert ") == std::string::npos) continue; + std::istringstream h(line.substr(line.find("(n_expert ") + 10)); + char comma = 0; + std::string word; + h >> hdr_n >> comma >> word >> hdr_total; // "(n_expert N, total T; ..." + continue; + } + std::istringstream ss(line); + long long l = 0, gt = 0, dt = 0; + unsigned long long off = 0, blob = 0, o[3] = {0, 0, 0}; + std::string file; + ss >> l >> gt >> dt >> off >> blob >> o[0] >> o[1] >> o[2]; + ss >> file; + same = same && L.fmt[(size_t) l].gu_type == gt && L.fmt[(size_t) l].d_type == dt && + L.offset[(size_t) l] == off && L.bytes[(size_t) l] == blob; + for (int r = 0; r < 3; ++r) { + same = same && L.gguf_off[(size_t) (3 * l + r)] == o[r]; + const std::string got = L.gguf_file.empty() ? std::string() : L.gguf_file[(size_t) (3 * l + r)]; + same = same && got == file; + } + if (blob > max_blob) max_blob = blob; + } + same = same && L.n_expert == hdr_n && (long long) L.total == hdr_total && L.max_blob == max_blob; + check(same, std::string(pack) + ": every field as the pre-v4 reader, totals as the header (v" + + std::to_string(L.version) + ", n_expert " + std::to_string(L.n_expert) + ")"); + } +} + +// ---- 2. the v4 column and the refusals +std::string manifest(int version, const std::string& col1, int64_t blob, int n_expert = 2) { + std::string s = "# strata native experts v" + std::to_string(version) + + ": layer gu_type d_type offset blob_bytes gate_off up_off down_off [shard | gate,up,down] " + "(n_expert " + std::to_string(n_expert) + ", total " + std::to_string(2 * blob * n_expert) + ")\n"; + s += "0 12 7 0 " + std::to_string(blob) + " 100 200 300\n"; + s += "1 12 7 " + std::to_string(blob * n_expert) + " " + std::to_string(blob) + " 400 500 600" + + (col1.empty() ? "" : " " + col1) + "\n"; + return s; +} + +void columns() { + strata::kernels::cpu::NativeFmt f; + std::string err; + if (!strata::kernels::cpu::native_fmt(12, 7, 2560, 640, f, err)) { + check(false, "Q4_K/Q5_1 native format: " + err); + return; + } + const int64_t blob = (int64_t) f.bytes; + TempDir d; + write_text(d.path / "native_experts.txt", manifest(4, "B.gguf,B.gguf,", blob)); + bool ok = load(d.path, 2, 512, err); + const ExpertLayout& L = strata::kernels::cpu::expert_layout(); + check(ok && L.version == 4 && L.gguf_file.size() == 6 && L.gguf_file[0].empty() && L.gguf_file[2].empty() && + L.gguf_file[3] == "B.gguf" && L.gguf_file[4] == "B.gguf" && L.gguf_file[5].empty() && + L.gguf_off[5] == 600, + "v4: `B.gguf,B.gguf,` = gate and up in B.gguf, down in the --native shard" + (ok ? "" : ": " + err)); + write_text(d.path / "native_experts.txt", manifest(3, "A.gguf", blob)); + ok = load(d.path, 2, 512, err); + check(ok && L.gguf_file[3] == "A.gguf" && L.gguf_file[4] == "A.gguf" && L.gguf_file[5] == "A.gguf", + "v3: one name covers the three roles"); + write_text(d.path / "native_experts.txt", manifest(5, "", blob)); + ok = load(d.path, 2, 512, err); + check(!ok && err.find("v5") != std::string::npos, "v5 is refused (\"" + err.substr(0, 40) + "...\")"); + write_text(d.path / "native_experts.txt", manifest(4, "a,b,c,d", blob)); + check(!load(d.path, 2, 512, err) && err.find("gate,up,down") != std::string::npos, "four names are refused"); + write_text(d.path / "native_experts.txt", manifest(4, "a,b", blob)); + check(!load(d.path, 2, 512, err), "two names are refused"); + write_text(d.path / "native_experts.txt", manifest(4, "a extra", blob)); + check(!load(d.path, 2, 512, err), "a column after the shard column is refused"); +} + +// ---- 3. the arena from synthetic shards +void arena() { + using fixture::Tensor; + constexpr int64_t H = 2560, FF = 640, NE = 2; + const uint32_t Q4_K = 12, Q5_1 = 7, Q5_K = 13, Q8_0 = 8, Q3_K = 11, IQ3_S = 21; + strata::kernels::cpu::NativeFmt f0, f1; + std::string err; + if (!strata::kernels::cpu::native_fmt(Q4_K, Q5_1, H, FF, f0, err) || + !strata::kernels::cpu::native_fmt(Q5_K, Q8_0, H, FF, f1, err)) { + check(false, "Q4_K/Q5_1 and Q5_K/Q8_0 native formats: " + err); + return; + } + TempDir d; + const fs::path A = d.path / "M-00002-of-00003.gguf", B = d.path / "M-00003-of-00003.gguf"; + // A: layer 0 (all roles) and layer 1's down; B: layer 1's gate and up + const std::vector ta = {{"blk.0.ffn_gate_exps.weight", {H, FF, NE}, Q4_K, 11}, + {"blk.0.ffn_up_exps.weight", {H, FF, NE}, Q4_K, 12}, + {"blk.0.ffn_down_exps.weight", {FF, H, NE}, Q5_1, 13}, + {"blk.1.ffn_down_exps.weight", {FF, H, NE}, Q8_0, 23}}; + const std::vector tb = {{"blk.1.ffn_gate_exps.weight", {H, FF, NE}, Q5_K, 21}, + {"blk.1.ffn_up_exps.weight", {H, FF, NE}, Q5_K, 22}}; + const auto wa = fixture::write(A, {}, ta); + const auto wb = fixture::write(B, {}, tb); + auto at = [](const fixture::Written& w, size_t i) { return std::to_string(w.data_start + w.offsets[i]); }; + const uint64_t total = (f0.bytes + f1.bytes) * NE; + auto text = [&](const std::string& down1, const std::string& col1) { + return "# strata native experts v4: layer gu_type d_type offset blob_bytes gate_off up_off down_off " + "[shard | gate,up,down] (n_expert 2, total " + std::to_string(total) + ")\n" + + "0 12 7 0 " + std::to_string(f0.bytes) + " " + at(wa, 0) + " " + at(wa, 1) + " " + at(wa, 2) + "\n" + + "1 13 8 " + std::to_string(f0.bytes * NE) + " " + std::to_string(f1.bytes) + " " + at(wb, 0) + " " + + at(wb, 1) + " " + down1 + " " + col1 + "\n"; + }; + const fs::path pack = d.path / "pack"; + fs::create_directories(pack); + write_text(pack / "native_experts.txt", text(at(wa, 3), B.filename().string() + "," + B.filename().string() + ",")); + bool ok = load(pack, 2, 512, err); + check(ok, "the v4 manifest of the split layer loads" + (ok ? "" : ": " + err)); + if (!ok) return; + const ExpertLayout& L = strata::kernels::cpu::expert_layout(); + ok = strata::core::check_experts_gguf(A.string(), L, err); + check(ok, "check_experts_gguf: every span is its tensor" + (ok ? "" : ": " + err)); + std::vector dst((size_t) L.total, 0xEE); + const strata::core::LoadStats st = strata::core::load_experts_gguf(A.string(), dst.data(), L, 3); + check(st.ok && st.bytes == L.total && L.total == total, "load_experts_gguf reads the whole arena (" + + std::to_string(L.total) + " B)"); + // every byte: expert e's slice of role r comes from byte e * per[r] of that role's tensor + bool exact = true; + for (int64_t l = 0; l < 2 && exact; ++l) { + const auto& fm = L.fmt[(size_t) l]; + const uint64_t per[3] = {fm.up_off, fm.up_off, fm.bytes - fm.down_off}; + const uint64_t off[3] = {0, fm.up_off, fm.down_off}; + const uint8_t seed[2][3] = {{11, 12, 13}, {21, 22, 23}}; + for (int64_t e = 0; e < NE && exact; ++e) + for (int r = 0; r < 3 && exact; ++r) { + const uint8_t* got = dst.data() + L.blob_offset(l, e) + off[r]; + for (uint64_t j = 0; j < per[r]; ++j) + if (got[j] != fixture::pattern(seed[l][r], (uint64_t) e * per[r] + j)) { exact = false; break; } + } + } + check(exact, "every blob is [gate | up | down] of its expert, layer 1 from both files"); + + auto refused = [&](const std::string& what, const std::string& needle) { + std::string e2; + const bool bad = load(pack, 2, 512, e2) && !strata::core::check_experts_gguf(A.string(), + strata::kernels::cpu::expert_layout(), e2); + check(bad && e2.find(needle) != std::string::npos, what + " (\"..." + needle + "...\")"); + }; + const std::string both = B.filename().string() + "," + B.filename().string() + ","; + write_text(pack / "native_experts.txt", text(std::to_string(std::stoull(at(wa, 3)) + 32), both)); + refused("an offset that is not the tensor's is refused", "starts at byte"); + write_text(pack / "native_experts.txt", text(at(wa, 3), "")); + refused("layer 1's gate named in the file that does not hold it is refused", "is not in it"); + write_text(pack / "native_experts.txt", text(at(wa, 3), both + ",x")); + std::string e3; + check(!load(pack, 2, 512, e3), "a malformed column is refused at load"); + write_text(pack / "native_experts.txt", text(at(wa, 3), "missing.gguf,missing.gguf,")); + refused("a shard that is not there is refused", "cannot open"); + fixture::write(B, {}, tb, /*cut=*/100); + write_text(pack / "native_experts.txt", text(at(wa, 3), both)); + refused("a truncated shard is refused before anything is read", "past the end"); + + // a type and a shape the pack does not say: Q3_K and IQ3_S are both 110 B per 256, so only the type differs + TempDir d2; + const fs::path C = d2.path / "C.gguf"; + const auto wc = fixture::write(C, {}, {{"blk.0.ffn_gate_exps.weight", {H, FF, NE}, IQ3_S, 31}, + {"blk.0.ffn_up_exps.weight", {H, FF, NE}, Q3_K, 32}, + {"blk.0.ffn_down_exps.weight", {FF, H, NE + 1}, Q5_1, 33}}); + strata::kernels::cpu::NativeFmt f2; + if (!strata::kernels::cpu::native_fmt(Q3_K, Q5_1, H, FF, f2, err)) { + check(false, "Q3_K/Q5_1 native format: " + err); + return; + } + fs::create_directories(d2.path / "pack"); + write_text(d2.path / "pack" / "native_experts.txt", + "# strata native experts v3: (n_expert 2, total " + std::to_string(f2.bytes * NE) + ")\n0 11 7 0 " + + std::to_string(f2.bytes) + " " + std::to_string(wc.data_start + wc.offsets[0]) + " " + + std::to_string(wc.data_start + wc.offsets[1]) + " " + std::to_string(wc.data_start + wc.offsets[2]) + "\n"); + ok = load(d2.path / "pack", 1, 512, err) && !strata::core::check_experts_gguf(C.string(), + strata::kernels::cpu::expert_layout(), err); + check(ok && err.find("is IQ3_S") != std::string::npos, "a gate of another type than the pack's is refused"); + // the same bytes with the pack's types, but a down tensor of three experts where the pack says two + fixture::write(C, {}, {{"blk.0.ffn_gate_exps.weight", {H, FF, NE}, Q3_K, 31}, + {"blk.0.ffn_up_exps.weight", {H, FF, NE}, Q3_K, 32}, + {"blk.0.ffn_down_exps.weight", {FF, H, NE + 1}, Q5_1, 33}}); + ok = load(d2.path / "pack", 1, 512, err) && !strata::core::check_experts_gguf(C.string(), + strata::kernels::cpu::expert_layout(), err); + check(ok && err.find("is not [640, 2560, 2]") != std::string::npos, "a down tensor of another shape is refused"); +} +} // namespace + +int main(int argc, char** argv) { + std::printf("expert_layout_test\n"); + if (argc < 2) { + std::printf("usage: expert_layout_test \n"); + return 2; + } + real_packs(argv[1]); + columns(); + arena(); + std::printf(g_fail ? "expert_layout_test: %d FAILED\n" : "expert_layout_test: all passed\n", g_fail); + return g_fail ? 1 : 0; +} diff --git a/tests/core/gguf_fixture.hpp b/tests/core/gguf_fixture.hpp new file mode 100644 index 00000000..677a5cd8 --- /dev/null +++ b/tests/core/gguf_fixture.hpp @@ -0,0 +1,103 @@ +// tests/core/gguf_fixture.hpp - synthetic GGUF v3 files for the loader tests (no gguf-py, no model, no GPU). +// +// A file is a header, metadata (strings and integers only), a tensor directory whose offsets are 32-byte aligned +// in the data section, and the tensors' payloads, each filled with a pattern the test can recognise. +#pragma once + +#include "strata/artifact/gguf_reader.hpp" + +#include +#include +#include +#include +#include +#include +#include + +namespace fixture { + +struct Kv { + std::string key; + uint32_t type = 8; ///< GGUF value type: 8 string, 2 u16, 4 u32, 5 i32 + std::string s; + uint64_t u = 0; +}; +inline Kv str(const std::string& k, const std::string& v) { return Kv{k, 8, v, 0}; } +inline Kv u16(const std::string& k, uint64_t v) { return Kv{k, 2, {}, v}; } +inline Kv i32(const std::string& k, uint64_t v) { return Kv{k, 5, {}, v}; } +inline Kv u32(const std::string& k, uint64_t v) { return Kv{k, 4, {}, v}; } + +struct Tensor { + std::string name; + std::vector shape; ///< GGUF order: dim 0 varies fastest + uint32_t type = 0; + uint8_t seed = 1; ///< payload byte i is (seed * 131 + i * 7 + i / 4093) & 0xff +}; + +inline uint8_t pattern(uint8_t seed, uint64_t i) { return (uint8_t) ((seed * 131u + i * 7u + i / 4093u) & 0xffu); } + +/// The split keys llama.cpp's gguf-split writes (u16 split.no / split.count, i32 split.tensors.count). +inline std::vector split_keys(uint64_t no, uint64_t count, uint64_t tensors) { + return {u16("split.no", no), u16("split.count", count), i32("split.tensors.count", tensors)}; +} + +struct Written { + uint64_t data_start = 0; + std::vector offsets; ///< per tensor, from data_start + uint64_t size = 0; +}; + +/// Writes `path`; `cut` bytes are dropped from the end (a truncated download). +inline Written write(const std::filesystem::path& path, const std::vector& kv, const std::vector& ts, + uint64_t cut = 0) { + std::vector b; + auto put = [&](const void* p, size_t n) { b.insert(b.end(), (const uint8_t*) p, (const uint8_t*) p + n); }; + auto put32 = [&](uint32_t v) { put(&v, 4); }; + auto put64 = [&](uint64_t v) { put(&v, 8); }; + auto puts = [&](const std::string& s) { put64(s.size()); put(s.data(), s.size()); }; + put32(0x46554747u); + put32(3); + put64(ts.size()); + put64(kv.size()); + for (const auto& k : kv) { + puts(k.key); + put32(k.type); + if (k.type == 8) puts(k.s); + else if (k.type == 2) { const uint16_t v = (uint16_t) k.u; put(&v, 2); } + else if (k.type == 4 || k.type == 5) { const uint32_t v = (uint32_t) k.u; put(&v, 4); } + else throw std::runtime_error("fixture: unsupported metadata type"); + } + Written w; + std::vector bytes; + uint64_t at = 0; + for (const auto& t : ts) { + strata::TensorInfo info; + info.shape = t.shape; + info.type = t.type; + const uint64_t n = strata::tensor_payload_bytes(info); + if (n == 0) throw std::runtime_error("fixture: " + t.name + " has no whole-block byte count"); + puts(t.name); + put32((uint32_t) t.shape.size()); + for (uint64_t d : t.shape) put64(d); + put32(t.type); + put64(at); + w.offsets.push_back(at); + bytes.push_back(n); + at = (at + n + 31) / 32 * 32; + } + w.data_start = (b.size() + 31) / 32 * 32; + b.resize((size_t) w.data_start, 0); + for (size_t i = 0; i < ts.size(); ++i) { + b.resize((size_t) (w.data_start + w.offsets[i]), 0); + for (uint64_t j = 0; j < bytes[i]; ++j) b.push_back(pattern(ts[i].seed, j)); + } + if (cut > b.size()) cut = b.size(); + b.resize(b.size() - (size_t) cut); + w.size = b.size(); + std::ofstream f(path, std::ios::binary | std::ios::trunc); + f.write((const char*) b.data(), (std::streamsize) b.size()); + if (!f) throw std::runtime_error("fixture: cannot write " + path.string()); + return w; +} + +} // namespace fixture diff --git a/tests/core/gguf_split_test.cpp b/tests/core/gguf_split_test.cpp new file mode 100644 index 00000000..22b668b6 --- /dev/null +++ b/tests/core/gguf_split_test.cpp @@ -0,0 +1,186 @@ +// tests/core/gguf_split_test.cpp - a split model's shards (strata::gguf_split_paths) and their joint view +// (strata::GgufModel), on synthetic GGUFs: no model, no GPU. +// +// The layout is Unsloth's UD-Q4_K_XL in miniature: shard 1 holds the metadata and no tensor, shards 2-4 the +// tensors, and layer 11's down sits in shard 2 while its gate and up are in shard 3. Then the ways a real +// download goes wrong: a shard missing, a shard of another model (split keys that disagree), a tensor in two +// shards, a directory that does not add up to split.tensors.count, a truncated shard, and shard 1 opened alone. +#include "gguf_fixture.hpp" + +#include "strata/artifact/gguf_split.hpp" + +#include +#include +#include +#include +#include + +namespace fs = std::filesystem; + +namespace { +int g_fail = 0; +void check(bool ok, const std::string& what) { + std::printf(" %-78s %s\n", what.c_str(), ok ? "ok" : "FAIL"); + if (!ok) ++g_fail; +} + +struct TempDir { + fs::path path; + TempDir() { + path = fs::temp_directory_path() / + ("strata-gguf-split-test-" + std::to_string(std::chrono::steady_clock::now().time_since_epoch().count())); + fs::create_directories(path); + } + ~TempDir() { + std::error_code ignored; + fs::remove_all(path, ignored); + } +}; + +std::string shard_name(int i, int n) { + char b[64]; + std::snprintf(b, sizeof b, "Model-UD-Q4_K_XL-%05d-of-%05d.gguf", i, n); + return b; +} + +using fixture::Tensor; +const uint32_t F32 = 0, Q8_0 = 8; + +/// Writes the 4-shard model; `mutate(i, kv, tensors)` may change shard i (0-based) before it is written. +template std::vector write_model(const fs::path& dir, F mutate, uint64_t total = 7) { + std::vector paths; + for (int i = 0; i < 4; ++i) { + std::vector kv; + if (i == 0) kv.push_back(fixture::str("general.architecture", "qwen4exp")); + for (const auto& k : fixture::split_keys((uint64_t) i, 4, total)) kv.push_back(k); + std::vector ts; + if (i == 1) ts = {{"output.weight", {64, 4}, Q8_0, 1}, {"token_embd.weight", {64, 4}, Q8_0, 2}, + {"blk.11.ffn_down_exps.weight", {32, 32, 2}, F32, 3}}; + if (i == 2) ts = {{"blk.11.ffn_gate_exps.weight", {32, 32, 2}, F32, 4}, + {"blk.11.ffn_up_exps.weight", {32, 32, 2}, F32, 5}, {"blk.12.attn_q.weight", {32, 8}, F32, 6}}; + if (i == 3) ts = {{"output_norm.weight", {32}, F32, 7}}; + uint64_t cut = 0; + mutate(i, kv, ts, cut); + const fs::path p = dir / shard_name(i + 1, 4); + fixture::write(p, kv, ts, cut); + paths.push_back(p.string()); + } + return paths; +} +auto same = [](int, std::vector&, std::vector&, uint64_t&) {}; + +std::string error_of(const std::vector& paths) { + try { + strata::GgufModel m(paths); + } catch (const std::exception& e) { + return e.what(); + } + return {}; +} +} // namespace + +int main() { + std::printf("gguf_split_test\n"); + { + TempDir d; + const auto paths = write_model(d.path, same); + // any shard names the family; the list is in split order, metadata shard first + std::vector got; + try { got = strata::gguf_split_paths(paths[2]); } catch (...) {} + check(got == paths, "gguf_split_paths from shard 3: the 4 shards in order"); + try { got = strata::gguf_split_paths(paths[0]); } catch (...) { got.clear(); } + check(got == paths, "gguf_split_paths from shard 1: the same 4 shards"); + check(strata::gguf_split_paths((d.path / "plain.gguf").string()).size() == 1, + "a name without -NNNNN-of-MMMMM is a model of one file"); + std::string err; + try { + const strata::GgufModel m(paths); + size_t s_out = 9, s_gate = 9, s_down = 9, s_up = 9; + const strata::TensorInfo* out = m.find("output.weight", &s_out); + const strata::TensorInfo* gate = m.find("blk.11.ffn_gate_exps.weight", &s_gate); + const strata::TensorInfo* up = m.find("blk.11.ffn_up_exps.weight", &s_up); + const strata::TensorInfo* down = m.find("blk.11.ffn_down_exps.weight", &s_down); + check(m.size() == 4 && m.meta().get("general.architecture") && m.meta().tensors().empty(), + "shard 1: the metadata, no tensor"); + check(out && s_out == 1 && m.in_bounds(*out, s_out), "output.weight found in shard 2, in bounds"); + check(gate && up && down && s_gate == 2 && s_up == 2 && s_down == 1, + "layer 11 per role: gate/up in shard 3, down in shard 2"); + check(m.find("blk.99.ffn_gate_exps.weight") == nullptr, "an absent tensor is nullptr"); + check(strata::check_architecture(m.meta()).find("block_count") != std::string::npos, + "the architecture guard reads the metadata shard (and wants its keys)"); + } catch (const std::exception& e) { + err = e.what(); + } + check(err.empty(), "the 4-shard model opens" + (err.empty() ? std::string() : ": " + err)); + } + { + TempDir d; + const auto paths = write_model(d.path, same); + fs::remove(paths[3]); + std::string err; + try { strata::gguf_split_paths(paths[0]); } catch (const std::exception& e) { err = e.what(); } + check(err.find("missing model shard") != std::string::npos && err.find(shard_name(4, 4)) != std::string::npos, + "a missing shard is an error that names it"); + } + { + TempDir d; + const auto paths = write_model(d.path, [](int i, auto&, auto& ts, uint64_t&) { + if (i == 3) ts.push_back({"blk.11.ffn_up_exps.weight", {32, 32, 2}, F32, 9}); + }, 8); + const std::string err = error_of(paths); + check(err.find("in two shards") != std::string::npos && err.find(shard_name(3, 4)) != std::string::npos && + err.find(shard_name(4, 4)) != std::string::npos, + "a tensor in two shards is refused, naming both"); + } + { + TempDir d; + const auto paths = write_model(d.path, [](int i, auto& kv, auto&, uint64_t&) { + if (i == 2) kv = fixture::split_keys(1, 4, 7); // says it is shard 2 + }); + check(error_of(paths).find("does not declare itself shard 3 of 4") != std::string::npos, + "a shard whose split.no disagrees with its place is refused"); + } + { + TempDir d; + const auto paths = write_model(d.path, [](int i, auto& kv, auto&, uint64_t&) { + if (i == 3) kv = fixture::split_keys(3, 4, 9); // another model's split.tensors.count + }); + check(!error_of(paths).empty(), "a shard of another model (split.tensors.count differs) is refused"); + } + { + TempDir d; + const auto paths = write_model(d.path, [](int i, auto&, auto& ts, uint64_t&) { + if (i == 3) ts.clear(); // a directory short of split.tensors.count + }); + check(error_of(paths).find("split.tensors.count is 7") != std::string::npos, + "shards that do not add up to split.tensors.count are refused"); + } + { + TempDir d; + const auto paths = write_model(d.path, [](int i, auto& kv, auto&, uint64_t&) { + if (i == 0) kv.erase(kv.begin()); // no general.architecture in shard 1 + }); + check(error_of(paths).find("general.architecture") != std::string::npos, + "a split model whose first shard has no metadata is refused"); + check(error_of({paths[0]}).find("opened as a whole model") != std::string::npos, + "shard 1 of 4 opened as a whole model is refused"); + } + { + TempDir d; + const auto paths = write_model(d.path, [](int i, auto&, auto&, uint64_t& cut) { + if (i == 2) cut = 100; // the last tensor of shard 3 is cut short + }); + bool truncated = false, first_ok = false; + try { + const strata::GgufModel m(paths); + size_t s = 0; + const strata::TensorInfo* q = m.find("blk.12.attn_q.weight", &s); + const strata::TensorInfo* g = m.find("blk.11.ffn_gate_exps.weight"); + truncated = q && !m.in_bounds(*q, s); + first_ok = g && m.in_bounds(*g, 2); + } catch (...) {} + check(truncated && first_ok, "a truncated shard: its last tensor is out of bounds, the others are not"); + } + std::printf(g_fail ? "gguf_split_test: %d FAILED\n" : "gguf_split_test: all passed\n", g_fail); + return g_fail ? 1 : 0; +} diff --git a/tests/data/native_experts/coder-iq1_m.txt b/tests/data/native_experts/coder-iq1_m.txt new file mode 100644 index 00000000..bf0c408a --- /dev/null +++ b/tests/data/native_experts/coder-iq1_m.txt @@ -0,0 +1,49 @@ +# strata native experts v3: layer gu_type d_type offset blob_bytes gate_off up_off down_off [shard] (n_expert 256, total 25146163200; absolute offsets in Qwen3.8-Flash-Next-GSQ-RCO-IQ1_M-00001-of-00002.gguf, or in the named shard beside it) +0 18 20 0 2176000 1150562336 1313362976 913711136 +1 22 42 557056000 1510400 1664626592 1801161632 1545740192 +2 22 20 943718400 1971200 2308902688 2445488928 2072051488 +3 22 20 1448345600 1971200 2885763232 3022298272 2648912032 +4 22 20 1952972800 1971200 3458564256 3595572896 3221713056 +5 22 20 2457600000 1971200 4038269472 4174804512 3801418272 +6 22 20 2962227200 1971200 4613172128 4750180768 4376320928 +7 18 20 3466854400 2176000 5193708832 5356458272 4956857632 +8 22 42 4023910400 1510400 5696376096 5832911136 5577489696 +9 22 20 4410572800 1971200 6271347872 6407934112 6034496672 +10 22 42 4915200000 1510400 6732983328 6869569568 6614096928 +11 22 20 5301862400 1971200 7314285472 7450871712 7077434272 +12 18 42 5806489600 1715200 7761567136 7924790176 7642680736 +13 22 42 6245580800 1510400 8269195552 8405781792 8150309152 +14 18 20 6632243200 2176000 8850188448 9012937888 8613337248 +15 22 20 7189299200 1971200 9478952992 9615961632 9242101792 +16 18 20 7693926400 2176000 10047629856 10210430496 9810778656 +17 21 20 8250982400 2329600 10683191712 10865601952 10446340512 +18 18 20 8847360000 2176000 11353477408 11516482848 11116626208 +19 22 20 9404416000 1971200 11989461152 12126469792 11752609952 +20 18 42 9909043200 1715200 12445193376 12607942816 12326306976 +21 21 42 10348134400 1868800 12962739232 13145200672 12843852832 +22 18 20 10826547200 2176000 13631427488 13794650528 13394576288 +23 22 20 11383603200 1971200 14269635872 14406426912 14032784672 +24 22 20 11888230400 1971200 14842897696 14979432736 14606046496 +25 21 20 12392857600 2329600 15426184352 15608594592 15189333152 +26 22 20 12989235200 1971200 16097957408 16234966048 15861106208 +27 21 20 13493862400 2329600 16682121632 16864531872 16445270432 +28 21 20 14090240000 2329600 17346621856 17529505696 17109770656 +29 21 20 14686617600 2329600 18018868512 18201534752 17782017312 +30 18 42 15282995200 1715200 18571885728 18734635168 18452999328 +31 18 20 15722086400 2176000 19213849632 19376599072 18976998432 +32 22 20 16279142400 1971200 19837195296 19974203936 19600344096 +33 18 20 16783769600 2176000 20413173152 20575922592 20176321952 +34 22 20 17340825600 1971200 21040655648 21177241888 20803804448 +35 21 20 17845452800 2329600 21630446752 21812856992 21393595552 +36 22 42 18441830400 1510400 22173400736 22310409376 22054514336 +37 18 20 18828492800 2176000 22753323552 22916546592 22516472352 +38 18 20 19385548800 2176000 23383942048 23546742688 23147090848 +39 22 20 19942604800 1971200 24025905952 24162492192 23789054752 +40 21 20 20447232000 2329600 24598758176 24781424416 24361906976 +41 18 20 21043609600 2176000 25262981792 25425782432 25026130592 +42 18 20 21600665600 2176000 25890515488 26053738528 25653664288 +43 18 20 22157721600 2176000 26532901792 26696124832 26296050592 +44 21 20 22714777600 2329600 27151490976 27333952416 26914639776 +45 21 20 23311155200 2329600 27818796832 28001258272 27581945632 +46 18 20 23907532800 2176000 28488532128 28651332768 28251680928 +47 23 20 24464588800 2662400 29129699872 29354759712 28892029472 diff --git a/tests/data/native_experts/iq2_xs.txt b/tests/data/native_experts/iq2_xs.txt new file mode 100644 index 00000000..61c34b85 --- /dev/null +++ b/tests/data/native_experts/iq2_xs.txt @@ -0,0 +1,49 @@ +# strata native experts v2: layer gu_type d_type offset blob_bytes gate_off up_off down_off (n_expert 512, total 35454976000; the last three are absolute offsets in Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS-00001-of-00002.gguf) +0 22 42 0 1510400 957139232 1229167392 720288032 +1 16 42 773324800 1305600 1791743648 2011983008 1554892448 +2 22 42 1441792000 1510400 2589468704 2861663264 2352617504 +3 22 42 2215116800 1510400 3429617568 3701812128 3191947168 +4 16 42 2988441600 1305600 4258654112 4478419872 4021802912 +5 22 42 3656908800 1510400 4988400928 5260595488 4751549728 +6 22 42 4430233600 1510400 5824781984 6096976544 5588391584 +7 22 42 5203558400 1510400 6665627168 6938295328 6429236768 +8 29 42 5976883200 1177600 7496734752 7683732512 7259883552 +9 16 42 6579814400 1305600 8161931168 8381530528 7924260768 +10 16 42 7248281600 1305600 8893109024 9112874784 8656257824 +11 16 42 7916748800 1305600 9628172448 9847938208 9391782048 +12 22 42 8585216000 1510400 10354422432 10627090592 10117571232 +13 29 42 9358540800 1177600 11191917088 11378748448 10954246688 +14 16 42 9961472000 1305600 11856780704 12076546464 11619110304 +15 22 42 10629939200 1510400 12596884768 12868912928 12360033568 +16 22 42 11403264000 1510400 13428645152 13701313312 13190974752 +17 22 42 12176588800 1510400 14264363168 14536557728 14027511968 +18 16 42 12949913600 1305600 15100731424 15320330784 14863880224 +19 16 42 13618380800 1305600 15836089248 16055855008 15599238048 +20 22 42 14286848000 1510400 16562518432 16834712992 16324848032 +21 22 42 15060172800 1510400 17398108448 17670303008 17160438048 +22 22 42 15833497600 1510400 18230654624 18502849184 17994264224 +23 22 42 16606822400 1510400 19071581728 19343609888 18834730528 +24 22 42 17380147200 1510400 19906595872 20178790432 19669744672 +25 22 42 18153472000 1510400 20745913248 21017941408 20509062048 +26 22 42 18926796800 1510400 21580517664 21852712224 21343666464 +27 16 42 19700121600 1305600 22420899488 22641138848 22184048288 +28 22 42 20368588800 1510400 23146983072 23419011232 22910131872 +29 16 42 21141913600 1305600 23983658528 24203424288 23746807328 +30 16 42 21810380800 1305600 24713110944 24932876704 24476720544 +31 22 42 22478848000 1510400 25449180448 25721375008 25212329248 +32 22 42 23252172800 1510400 26279648032 26551842592 26042796832 +33 22 42 24025497600 1510400 27114892448 27387087008 26878041248 +34 22 42 24798822400 1510400 27955807264 28228001824 27718956064 +35 22 42 25572147200 1510400 28793160608 29065355168 28556309408 +36 22 42 26345472000 1510400 29623794592 29896462752 29386943392 +37 29 42 27118796800 1177600 30459512608 30646510368 30222661408 +38 22 42 27721728000 1510400 31125320864 31397515424 30888469664 +39 22 42 28495052800 1510400 31966247968 32238442528 31729396768 +40 22 42 29268377600 1510400 32794219552 33066414112 32557368352 +41 22 42 30041702400 1510400 33628823968 33901018528 33391972768 +42 22 42 30815027200 1510400 34464068384 34736736544 34227217184 +43 22 42 31588352000 1510400 35305090208 35577118368 35068239008 +44 22 42 32361676800 1510400 36135557792 36408225952 35898706592 +45 22 42 33135001600 1510400 36976946208 37249140768 36740095008 +46 22 42 33908326400 1510400 37813148064 38085342624 37576296864 +47 22 42 34681651200 1510400 38654454048 38926648608 38417602848 diff --git a/tests/data/native_experts/iq3_s.txt b/tests/data/native_experts/iq3_s.txt new file mode 100644 index 00000000..85164ba2 --- /dev/null +++ b/tests/data/native_experts/iq3_s.txt @@ -0,0 +1,49 @@ +# strata native experts v3: layer gu_type d_type offset blob_bytes gate_off up_off down_off [shard] (n_expert 512, total 50292326400; absolute offsets in Qwen3.8-Flash-Next-GSQ-RCO-IQ3_S-00001-of-00002.gguf, or in the named shard beside it) +0 18 20 0 2176000 1386491904 1711166464 913711104 +1 22 42 1114112000 1510400 2340958080 2613152640 2104106880 +2 22 20 1887436800 1971200 3491172096 3763417856 3018391296 +3 22 20 2896691200 1971200 4573970560 4846165120 4101189760 +4 22 20 3905945600 1971200 5652709504 5925377664 5179928704 +5 22 20 4915200000 1971200 6738352640 7010547200 6265571840 +6 22 20 5924454400 1971200 7819193216 8091861376 7346412416 +7 18 20 6933708800 2176000 8905667840 9230291200 8432887040 +8 22 42 8047820800 1510400 9848737024 10120931584 9611885824 +9 22 20 8821145600 1971200 10929646720 11201892480 10456865920 +10 22 42 9830400000 1510400 11779255296 12051501056 11542404096 +11 22 20 10603724800 1971200 12866495360 13138741120 12393714560 +12 18 42 11612979200 1715200 13701750144 14026847104 13464898944 +13 22 42 12491161600 1510400 14649780480 14922026240 14412929280 +14 18 20 13264486400 2176000 15736711296 16061334656 15263930496 +15 22 20 14378598400 1971200 16923842560 17196510720 16451061760 +16 18 20 15387852800 2176000 17998457344 18323131904 17525676544 +17 21 20 16501964800 2329600 19192385920 19556330880 18719605120 +18 18 20 17694720000 2176000 20460359936 20785239296 19987579136 +19 22 20 18808832000 1971200 21654710400 21927378560 21181929600 +20 18 42 19818086400 1715200 22498415744 22823039104 22261564544 +21 21 42 20696268800 1868800 23456363520 23820359680 23219512320 +22 18 20 21653094400 2176000 24722740096 25047837056 24249959296 +23 22 20 22767206400 1971200 25919315200 26191765760 25446534400 +24 22 20 23776460800 1971200 26998514944 27270709504 26525734144 +25 21 20 24785715200 2329600 28087739520 28451684480 27614958720 +26 22 20 25978470400 1971200 29357200896 29629869056 28884420096 +27 21 20 26987724800 2329600 30447303040 30811248000 29974522240 +28 21 20 28180480000 2329600 31709491584 32073910144 31236710784 +29 21 20 29373235200 2329600 32979426560 33343627520 32506645760 +30 18 42 30565990400 1715200 34012167296 34336790656 33775316096 +31 18 20 31444172800 2176000 35212497920 35537121280 34739717120 +32 22 20 32558284800 1971200 36394210304 36666878464 35921429504 +33 18 20 33567539200 2176000 37476126080 37800749440 37003345280 +34 22 20 34681651200 1971200 38661975296 38934221056 38189194496 +35 21 20 35690905600 2329600 39757704320 40121649280 39284923520 +36 22 42 36883660800 1510400 40780381824 41053049984 40543530624 +37 18 20 37656985600 2176000 41866242560 42191339520 41393461760 +38 18 20 38771097600 2176000 43055227776 43379902336 42582446976 +39 22 20 39885209600 1971200 44255558400 44527804160 43782777600 +40 21 20 40894464000 2329600 45334348544 45698549504 44861567744 +41 18 20 42087219200 2176000 46596260480 46920935040 46123479680 +42 18 20 43201331200 2176000 47782160896 48107257856 47309380096 +43 18 20 44315443200 2176000 48982913920 49308010880 48510133120 +44 21 20 45429555200 2329600 50159869824 50523865984 49687089024 +45 21 20 46622310400 2329600 51424864000 51788860160 50952083200 +46 18 20 47815065600 2176000 52692287616 53016962176 52219506816 +47 23 20 48929177600 2662400 53891822080 54341015040 53418222080 diff --git a/tests/data/native_experts/iq3_xxs.txt b/tests/data/native_experts/iq3_xxs.txt new file mode 100644 index 00000000..42f0ed50 --- /dev/null +++ b/tests/data/native_experts/iq3_xxs.txt @@ -0,0 +1,49 @@ +# strata native experts v2: layer gu_type d_type offset blob_bytes gate_off up_off down_off (n_expert 512, total 42912972800; the last three are absolute offsets in Qwen3.8-Flash-Next-GSQ-RCO-IQ3_XXS-00001-of-00002.gguf) +0 17 20 0 1868800 1227833216 1473813376 755052416 +1 16 42 956825600 1305600 2016152832 2235918592 1779301632 +2 17 20 1625292800 1868800 3045972608 3292003968 2573191808 +3 16 42 2582118400 1305600 3839355392 4059172352 3602964992 +4 17 20 3250585600 1868800 4806683136 5052663296 4334363136 +5 17 42 4207411200 1408000 5596436352 5842416512 5359585152 +6 16 42 4928307200 1305600 6379748608 6599514368 6143358208 +7 16 42 5596774400 1305600 7121247872 7340847232 6884396672 +8 21 42 6265241600 1868800 7846857856 8210636416 7610006656 +9 17 42 7222067200 1408000 8873971712 9120003072 8637120512 +10 17 42 7942963200 1408000 9669920128 9915733888 9433068928 +11 16 42 8663859200 1305600 10465942272 10685759232 10229091072 +12 22 42 9332326400 1510400 11190582016 11462610176 10954191616 +13 16 42 10105651200 1305600 12031522432 12251121792 11795132032 +14 17 42 10774118400 1408000 12768096768 13014332928 12531706368 +15 22 42 11495014400 1510400 13563906432 13836152192 13327055232 +16 16 42 12268339200 1305600 14398521216 14618338176 14161670016 +17 22 42 12936806400 1510400 15134954752 15407149312 14898103552 +18 21 42 13710131200 1868800 15976688768 16340467328 15739837568 +19 17 42 14666956800 1408000 17010020352 17256000512 16773629952 +20 21 42 15387852800 1868800 17787815936 18151812096 17551425536 +21 22 42 16344678400 1510400 18811338112 19083532672 18574486912 +22 21 42 17118003200 1868800 19652089088 20016034048 19415237888 +23 21 42 18074828800 1868800 20682407552 21046352512 20445556352 +24 21 42 19031654400 1868800 21704611456 22068390016 21467760256 +25 17 42 19988480000 1408000 22734674432 22980654592 22497823232 +26 22 42 20709376000 1510400 23524471168 23796716928 23287619968 +27 16 42 21482700800 1305600 24379793152 24599558912 24142941952 +28 21 20 22151168000 2329600 25345879296 25709657856 24873098496 +29 22 20 23343923200 1971200 26610479232 26882673792 26137698432 +30 21 20 24353177600 2329600 27695226368 28059171328 27222445568 +31 16 20 25545932800 1766400 28974200192 29193965952 28501419392 +32 21 20 26450329600 2329600 29937050496 30301046656 29464269696 +33 21 42 27643084800 1868800 30959753472 31323698432 30722902272 +34 21 20 28599910400 2329600 32230185088 32594181248 31757404288 +35 18 20 29792665600 2176000 33501048832 33825672192 33028268032 +36 18 42 30906777600 1715200 34444591616 34769048576 34207740416 +37 21 20 31784960000 2329600 35626780032 35990776192 35153999232 +38 22 20 32977715200 1971200 36892498688 37164526848 36419717888 +39 17 20 33986969600 1868800 37987587712 38233567872 37514806912 +40 22 42 34943795200 1510400 38771773056 39043967616 38534921856 +41 18 42 35717120000 1715200 39610924032 39935598592 39374072832 +42 22 20 36595302400 1971200 40796033408 41068061568 40323252608 +43 21 20 37604556800 2329600 41891114752 42254893312 41418333952 +44 18 20 38797312000 2176000 43148423936 43472880896 42675643136 +45 18 42 39911424000 1715200 44098627712 44423724672 43861776512 +46 22 20 40789606400 1971200 45285101568 45557296128 44812320768 +47 18 20 41798860800 2176000 46363501952 46688125312 45890721152 diff --git a/tests/data/native_experts/q2_0.txt b/tests/data/native_experts/q2_0.txt new file mode 100644 index 00000000..7b2cbd4e --- /dev/null +++ b/tests/data/native_experts/q2_0.txt @@ -0,0 +1,49 @@ +# strata native experts v2: layer gu_type d_type offset blob_bytes gate_off up_off down_off (n_expert 512, total 33973862400; the last three are absolute offsets in Qwen3.8-Flash-Next-GSQ-RCO-Q2_0-00001-of-00002.gguf) +0 42 42 0 1382400 994197792 1233624352 757141792 +1 42 42 707788800 1382400 1766860448 2005877408 1530470048 +2 42 42 1415577600 1382400 2558049824 2797476384 2321198624 +3 42 42 2123366400 1382400 3325351328 3564368288 3088500128 +4 42 42 2831155200 1382400 4087571872 4326588832 3851181472 +5 42 42 3538944000 1382400 4853325088 5092342048 4616473888 +6 42 42 4246732800 1382400 5619500704 5858517664 5383110304 +7 42 42 4954521600 1382400 6384631328 6623648288 6148240928 +8 42 42 5662310400 1382400 7144855072 7383872032 6907799072 +9 42 42 6370099200 1382400 7903921568 8143348128 7667531168 +10 42 42 7077888000 1382400 8666152224 8905169184 8429301024 +11 42 42 7785676800 1382400 9441479328 9680905888 9205088928 +12 42 42 8493465600 1382400 10199355552 10438615712 9962965152 +13 42 42 9201254400 1382400 10964891168 11204368928 10728500768 +14 42 42 9909043200 1382400 11732659104 11971919264 11494988704 +15 42 42 10616832000 1382400 12510902048 12749919008 12273846048 +16 42 42 11324620800 1382400 13268829472 13508089632 13031978272 +17 42 42 12032409600 1382400 14035560608 14274577568 13798709408 +18 42 42 12740198400 1382400 14801313824 15040330784 14564462624 +19 42 42 13447987200 1382400 15573612448 15812629408 15336761248 +20 42 42 14155776000 1382400 16333413792 16572673952 16097023392 +21 42 42 14863564800 1382400 17099410208 17338427168 16862559008 +22 42 42 15571353600 1382400 17865654944 18104671904 17629264544 +23 42 42 16279142400 1382400 18638086688 18877564448 18401235488 +24 42 42 16986931200 1382400 19401894432 19641154592 19164838432 +25 42 42 17694720000 1382400 20168095648 20407112608 19931705248 +26 42 42 18402508800 1382400 20937573664 21176590624 20700722464 +27 42 42 19110297600 1382400 21715358368 21954618528 21478967968 +28 42 42 19818086400 1382400 22475620512 22715520672 22238769312 +29 42 42 20525875200 1382400 23242704928 23481721888 23005648928 +30 42 42 21233664000 1382400 24010902944 24250163104 23774512544 +31 42 42 21941452800 1382400 24790715168 25030192928 24553863968 +32 42 42 22649241600 1382400 25553857312 25792874272 25317006112 +33 42 42 23357030400 1382400 26319610528 26558870688 26083220128 +34 42 42 24064819200 1382400 27088512544 27327990304 26851661344 +35 42 42 24772608000 1382400 27864292768 28103309728 27627902368 +36 42 42 25480396800 1382400 28626756512 28866234272 28390366112 +37 42 42 26188185600 1382400 29397609248 29637035808 29160553248 +38 42 42 26895974400 1382400 30166895264 30406321824 29930044064 +39 42 42 27603763200 1382400 30946134048 31185394208 30709282848 +40 42 42 28311552000 1382400 31708403232 31947880992 31471552032 +41 42 42 29019340800 1382400 32478833568 32718093728 32241982368 +42 42 42 29727129600 1382400 33249046304 33488063264 33012195104 +43 42 42 30434918400 1382400 34028407968 34267424928 33791556768 +44 42 42 31142707200 1382400 34791498912 35030759072 34554647712 +45 42 42 31850496000 1382400 35567241248 35806501408 35330185248 +46 42 42 32558284800 1382400 36338232224 36577914784 36101381024 +47 42 42 33266073600 1382400 37117686048 37356946208 36880015648 diff --git a/tests/data/native_experts/swift-iq2_xs.txt b/tests/data/native_experts/swift-iq2_xs.txt new file mode 100644 index 00000000..0da3e8f9 --- /dev/null +++ b/tests/data/native_experts/swift-iq2_xs.txt @@ -0,0 +1,49 @@ +# strata native experts v3: layer gu_type d_type offset blob_bytes gate_off up_off down_off [shard] (n_expert 512, total 35454976000; absolute offsets in Swift-Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS-00001-of-00002.gguf, or in the named shard beside it) +0 22 42 0 1510400 29757220608 30031875328 29520369408 +1 16 42 773324800 1305600 30594451584 30817317504 30357600384 +2 22 42 1441792000 1510400 31394803200 31669624320 31157952000 +3 22 42 2215116800 1510400 32237578624 32512399744 31999908224 +4 16 42 2988441600 1305600 33069241728 33291634048 32832390528 +5 22 42 3656908800 1510400 33801615104 34076436224 33564763904 +6 22 42 4430233600 1510400 34640622720 34915443840 34404232320 +7 22 42 5203558400 1510400 35484094464 35759389184 35247704064 +8 29 42 5976883200 1177600 36317828608 36507452928 36080977408 +9 16 42 6579814400 1305600 36985651584 37207877504 36747981184 +10 16 42 7248281600 1305600 37719456000 37941848320 37482604800 +11 16 42 7916748800 1305600 38457145984 38679538304 38220755584 +12 22 42 8585216000 1510400 39186022528 39461317248 38949171328 +13 29 42 9358540800 1177600 237726720 427184640 56320 Swift-Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS-00002-of-00002.gguf +14 16 42 9961472000 1305600 905216896 1127609216 667546496 Swift-Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS-00002-of-00002.gguf +15 22 42 10629939200 1510400 1647947520 1922602240 1411096320 Swift-Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS-00002-of-00002.gguf +16 22 42 11403264000 1510400 2482334464 2757629184 2244664064 Swift-Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS-00002-of-00002.gguf +17 22 42 12176588800 1510400 3320679040 3595500160 3083827840 Swift-Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS-00002-of-00002.gguf +18 16 42 12949913600 1305600 4159673856 4381899776 3922822656 Swift-Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS-00002-of-00002.gguf +19 16 42 13618380800 1305600 4897658240 5120050560 4660807040 Swift-Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS-00002-of-00002.gguf +20 22 42 14286848000 1510400 5626713984 5901535104 5389043584 Swift-Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS-00002-of-00002.gguf +21 22 42 15060172800 1510400 6464930560 6739751680 6227260160 Swift-Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS-00002-of-00002.gguf +22 22 42 15833497600 1510400 7300103296 7574924416 7063712896 Swift-Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS-00002-of-00002.gguf +23 22 42 16606822400 1510400 8143656960 8418311680 7906805760 Swift-Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS-00002-of-00002.gguf +24 22 42 17380147200 1510400 8981297664 9256118784 8744446464 Swift-Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS-00002-of-00002.gguf +25 22 42 18153472000 1510400 9823241600 10097896320 9586390400 Swift-Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS-00002-of-00002.gguf +26 22 42 18926796800 1510400 10660472576 10935293696 10423621376 Swift-Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS-00002-of-00002.gguf +27 16 42 19700121600 1305600 11503480960 11726346880 11266629760 Swift-Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS-00002-of-00002.gguf +28 22 42 20368588800 1510400 12232191104 12506845824 11995339904 Swift-Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS-00002-of-00002.gguf +29 16 42 21141913600 1305600 13071493120 13293885440 12834641920 Swift-Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS-00002-of-00002.gguf +30 16 42 21810380800 1305600 13803572096 14025964416 13567181696 Swift-Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS-00002-of-00002.gguf +31 22 42 22478848000 1510400 14542268160 14817089280 14305416960 Swift-Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS-00002-of-00002.gguf +32 22 42 23252172800 1510400 15375362304 15650183424 15138511104 Swift-Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS-00002-of-00002.gguf +33 22 42 24025497600 1510400 16213233280 16488054400 15976382080 Swift-Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS-00002-of-00002.gguf +34 22 42 24798822400 1510400 17056774656 17331595776 16819923456 Swift-Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS-00002-of-00002.gguf +35 22 42 25572147200 1510400 17896754560 18171575680 17659903360 Swift-Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS-00002-of-00002.gguf +36 22 42 26345472000 1510400 18730015104 19005309824 18493163904 Swift-Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS-00002-of-00002.gguf +37 29 42 27118796800 1177600 19568359680 19757984000 19331508480 Swift-Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS-00002-of-00002.gguf +38 22 42 27721728000 1510400 20236794496 20511615616 19999943296 Swift-Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS-00002-of-00002.gguf +39 22 42 28495052800 1510400 21080348160 21355169280 20843496960 Swift-Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS-00002-of-00002.gguf +40 22 42 29268377600 1510400 21910946304 22185767424 21674095104 Swift-Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS-00002-of-00002.gguf +41 22 42 30041702400 1510400 22748177280 23022998400 22511326080 Swift-Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS-00002-of-00002.gguf +42 22 42 30815027200 1510400 23586048256 23861342976 23349197056 Swift-Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS-00002-of-00002.gguf +43 22 42 31588352000 1510400 24429696640 24704351360 24192845440 Swift-Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS-00002-of-00002.gguf +44 22 42 32361676800 1510400 25262790784 25538085504 25025939584 Swift-Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS-00002-of-00002.gguf +45 22 42 33135001600 1510400 26106805760 26381626880 25869954560 Swift-Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS-00002-of-00002.gguf +46 22 42 33908326400 1510400 26945634176 27220455296 26708782976 Swift-Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS-00002-of-00002.gguf +47 22 42 34681651200 1510400 27789566720 28064387840 27552715520 Swift-Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS-00002-of-00002.gguf