diff --git a/CMakeLists.txt b/CMakeLists.txt index b41a71d..56d1ceb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -475,6 +475,11 @@ if(STRATA_ENABLE_CUDA OR STRATA_ENABLE_HIP) 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) + # the pool's native-pack rows at the layer's widths (Qwen3.6's 2048 x 512), not the canonical FF / H + add_executable(pool_native_widths_test tests/core/pool_native_widths_test.cpp) + target_link_libraries(pool_native_widths_test PRIVATE strata_engine) + add_test(NAME pool_native_widths_test COMMAND pool_native_widths_test) + set_tests_properties(pool_native_widths_test PROPERTIES TIMEOUT 120) # #326: a native pack's BF16 (--compat-bf16) PLE key stays in the arena; a quantized key row loads as before add_executable(native_dense_ple_key_test tests/core/native_dense_ple_key_test.cpp) target_link_libraries(native_dense_ple_key_test PRIVATE strata_engine) diff --git a/frontend/main.cpp b/frontend/main.cpp index 7ff74de..27fa949 100644 --- a/frontend/main.cpp +++ b/frontend/main.cpp @@ -39,7 +39,16 @@ int strata_engine_main(int argc, char** argv); namespace { -const char* const kModelName = "qwen3.8-flash-next-iq3_xxs"; +// the model id reported on /v1/models and in every completion: the GGUF's base name (`Qwen3.6-35B-A3B-UD-IQ3_XXS`, +// `Qwen3.8-Flash-Next-GSQ-RCO-IQ3_XXS` for shard 1 without its `-00001-of-00002`), set once the options are parsed +std::string g_model_name = "unbiased-strata"; +std::string model_name_of(const std::string& path) { + std::string b = path.substr(path.find_last_of('/') == std::string::npos ? 0 : path.find_last_of('/') + 1); + if (b.size() > 5 && b.compare(b.size() - 5, 5, ".gguf") == 0) b.resize(b.size() - 5); + const size_t at = b.rfind("-00001-of-"); + if (at != std::string::npos) b.resize(at); + return b.empty() ? std::string("unbiased-strata") : b; +} struct Options { std::string model; // shard 1 of the GGUF @@ -443,7 +452,7 @@ struct Server { out["id"] = id; out["object"] = "chat.completion"; out["created"] = created; - out["model"] = kModelName; + out["model"] = g_model_name; out["choices"] = common_json::array(); out["choices"].push_back(choice); out["usage"] = usage_json(r); @@ -464,7 +473,7 @@ struct Server { out["id"] = id; out["object"] = "chat.completion.chunk"; out["created"] = created; - out["model"] = kModelName; + out["model"] = g_model_name; out["choices"] = common_json::array(); out["choices"].push_back(choice); if (done) { out["usage"] = usage_json(*done); out["timings"] = timings_json(*done); } @@ -606,6 +615,7 @@ int main(int argc, char** argv) { Server srv; Options& o = srv.opt; if (!parse_args(argc, argv, o)) { usage(); return 2; } + g_model_name = model_name_of(o.model); // every file the engine needs must already be here: nothing is fetched // Flash-Next ships as two shards and the second holds the PLE table, so it is the engine's --ple-gguf; a one-file @@ -716,7 +726,7 @@ int main(int argc, char** argv) { http.Get("/v1/models", [&](const httplib::Request&, httplib::Response& res) { if (!srv.engine.alive()) return json_error(res, 503, "the engine is not running", "server_error"); common_json m = common_json::object(); - m["id"] = kModelName; + m["id"] = g_model_name; m["object"] = "model"; m["owned_by"] = "local"; m["context_length"] = srv.opt.ctx; diff --git a/src/kernels/cpu/native_expert.cpp b/src/kernels/cpu/native_expert.cpp index 0dc5d3a..13bae52 100644 --- a/src/kernels/cpu/native_expert.cpp +++ b/src/kernels/cpu/native_expert.cpp @@ -43,6 +43,11 @@ bool native_fmt(int gu_type, int d_type, int64_t n_embd, int64_t n_ff, NativeFmt err = "native experts: ggml-cpu cannot quantize an activation for this layer"; return false; } + if (n_embd <= 0 || n_ff <= 0 || n_embd > H || n_ff > FF) { // the pool's SplitBufMulti rows are FF / H wide + err = "native experts: expert geometry " + std::to_string(n_embd) + " x " + std::to_string(n_ff) + + " exceeds the pool's buffers (" + std::to_string(H) + " x " + std::to_string(FF) + ")"; + return false; + } if (n_embd % ggml_blck_size((ggml_type) gu_type) || n_ff % ggml_blck_size((ggml_type) d_type) || n_embd % ggml_blck_size(tg->vec_dot_type) || n_ff % ggml_blck_size(td->vec_dot_type)) { err = "native experts: expert geometry is not whole blocks"; diff --git a/src/kernels/cpu/pool.cpp b/src/kernels/cpu/pool.cpp index c644629..3d6460d 100644 --- a/src/kernels/cpu/pool.cpp +++ b/src/kernels/cpu/pool.cpp @@ -559,8 +559,12 @@ void ExpertPool::drain(int id, ExpertScratch& scratch, uint32_t epoch) { const int r0 = H * part / parts_b_, r1 = H * (part + 1) / parts_b_; s2_expert_down_rows(jobs_[e].blob, split_[(size_t) e].a2, jobs_[e].out, r0, r1); } else if (mode_ >= 5) { - // plan v0.3 P6: native layers, 5 = gate/up rows, 6 = down rows - const int per = mode_ == 5 ? FF : H; + // plan v0.3 P6: native layers, 5 = gate/up rows, 6 = down rows. The widths are the layer's own + // (NativeFmt n_ff / n_embd: 512 / 2048 for Qwen3.6-35B-A3B), not the canonical pack's FF / H: with + // those, the down phase wrote H - n_embd rows past every output row and the gate/up phase read + // FF - n_ff rows past every expert's gate/up block, and every verify window with a CPU-computed + // expert miss decoded to token 0. + const int per = mode_ == 5 ? (int) nfmt_->n_ff : (int) nfmt_->n_embd; const int64_t g0 = mrows_ * (int64_t) i / mtasks_, g1 = mrows_ * (int64_t) (i + 1) / mtasks_; for (int64_t r = g0; r < g1;) { const int e = (int) (r / per), r0 = (int) (r % per); @@ -696,16 +700,16 @@ void ExpertPool::run_split_multi_native(const NativeFmt& f, ExpertJobMulti* jobs nfmt_ = &f; const int threads = n_ + (host_works_ ? 1 : 0); mtasks_ = 3 * threads; - mrows_ = (int64_t) nb * FF; + mrows_ = (int64_t) nb * f.n_ff; // the layer's widths (see run_phase mode 5 / 6), not FF / H const auto a = std::chrono::steady_clock::now(); run_phase(5, mtasks_); const auto b = std::chrono::steady_clock::now(); for (int e = 0; e < nb; ++e) for (int t = 0; t < mjobs_[e].nt; ++t) - if (q2_native_kernels(f.d_type)) act_quant_any(split_multi_[(size_t) e].ff[t], FF, split_multi_[(size_t) e].a2[t]); + if (q2_native_kernels(f.d_type)) act_quant_any(split_multi_[(size_t) e].ff[t], (int) f.n_ff, split_multi_[(size_t) e].a2[t]); else native_quant_h(f, split_multi_[(size_t) e].ff[t], split_multi_[(size_t) e].hq[t]); const auto c = std::chrono::steady_clock::now(); - mrows_ = (int64_t) nb * H; + mrows_ = (int64_t) nb * f.n_embd; run_phase(6, mtasks_); const auto d = std::chrono::steady_clock::now(); ms_multi_gu += std::chrono::duration(b - a).count(); diff --git a/tests/core/pool_native_widths_test.cpp b/tests/core/pool_native_widths_test.cpp new file mode 100644 index 0000000..61187bf --- /dev/null +++ b/tests/core/pool_native_widths_test.cpp @@ -0,0 +1,125 @@ +// The expert pool's native-pack path (run_split_multi_native: gate/up rows, the intermediate's quantization, down +// rows, each split across the workers) must use the LAYER'S widths, NativeFmt::n_ff and n_embd, not the canonical +// Q2_0 pack's FF / H (640 / 2560). Qwen3.6-35B-A3B's experts are 2048 x 512: with FF / H the down phase wrote 512 +// rows past every output row and the first verify window with a CPU-computed expert miss decoded to token 0. +// +// Three Q8_0 experts at 2048 x 512, four tokens; the pool's rows against the same kernels called directly, and a +// guard after every output row that must stay untouched. +#include "strata/kernels/cpu/native_expert.hpp" +#include "strata/kernels/cpu/pool.hpp" +#include "ggml.h" + +#include +#include +#include +#include +#include +#include + +using namespace strata::kernels::cpu; + +namespace { +int g_fail = 0; +void check(bool ok, const std::string& what) { + std::printf(" %-90s %s\n", what.c_str(), ok ? "ok" : "FAIL"); + if (!ok) ++g_fail; +} +} // namespace + +int main() { + const int64_t n_embd = 2048, n_ff = 512; // Qwen3.6-35B-A3B's expert geometry + const int kType = (int) GGML_TYPE_Q8_0; + NativeFmt f; + std::string err; + if (!native_fmt(kType, kType, n_embd, n_ff, f, err)) { std::printf("native_fmt: %s\n", err.c_str()); return 1; } + check(f.n_embd == n_embd && f.n_ff == n_ff, "the format carries the layer's widths 2048 x 512"); + check(n_embd < H && n_ff < FF, "the layer is narrower than the canonical FF / H, so the old row counts overran"); + NativeFmt wide; + const bool wide_ok = native_fmt(kType, kType, H + 64, n_ff, wide, err); + check(!wide_ok, "a layer wider than the pool's buffers is refused: " + err); + + const int E = 3, T = 4; + std::mt19937 rng(7); + std::normal_distribution nd(0.f, 0.5f); + std::vector> blobs((size_t) E); + std::vector row((size_t) std::max(n_embd, n_ff)); + for (int e = 0; e < E; ++e) { + auto& b = blobs[(size_t) e]; + b.assign(f.bytes, 0); + for (int part = 0; part < 3; ++part) { // gate, up: n_ff rows of n_embd; down: n_embd rows of n_ff + const int64_t rows = part < 2 ? n_ff : n_embd, cols = part < 2 ? n_embd : n_ff; + std::vector m((size_t) (rows * cols)); + for (float& v : m) v = nd(rng); + uint8_t* dst = b.data() + (part == 0 ? 0 : part == 1 ? f.up_off : f.down_off); + ggml_quantize_chunk((ggml_type) kType, m.data(), dst, 0, rows, cols, nullptr); + } + } + std::vector> x((size_t) T, std::vector((size_t) n_embd)); + std::vector> act((size_t) T, std::vector(kNativeActBytes)); + for (int t = 0; t < T; ++t) { + for (float& v : x[(size_t) t]) v = nd(rng); + native_quant_act(f, x[(size_t) t].data(), act[(size_t) t].data()); + } + // token -> expert: tokens 0,1 on expert 0; token 2 on expert 1; token 3 on expert 2 (then 1 again: two experts) + const int routes[][2] = {{0, 0}, {1, 0}, {2, 1}, {3, 2}, {3, 1}}; + const int R = 5; + + // the reference: the kernels directly, one expert at a time, over exactly the layer's rows + std::vector> ref((size_t) R, std::vector((size_t) n_embd)); + for (int e = 0; e < E; ++e) { + const void* nact[MAXT] = {}; + float* ffp[MAXT] = {}; + const void* hq[MAXT] = {}; + float* out[MAXT] = {}; + std::vector> ff; + std::vector> h; + int nt = 0; + for (int r = 0; r < R; ++r) + if (routes[r][1] == e) { + ff.emplace_back((size_t) n_ff); + h.emplace_back(kNativeHBytes); + ++nt; + } + int i = 0; + for (int r = 0; r < R; ++r) + if (routes[r][1] == e) { + nact[i] = act[(size_t) routes[r][0]].data(); + ffp[i] = ff[(size_t) i].data(); + hq[i] = h[(size_t) i].data(); + out[i] = ref[(size_t) r].data(); + ++i; + } + native_gu_rows(f, blobs[(size_t) e].data(), nact, nt, ffp, 0, (int) n_ff); + for (int k = 0; k < nt; ++k) native_quant_h(f, ffp[k], h[(size_t) k].data()); + native_down_rows(f, blobs[(size_t) e].data(), hq, nt, out, 0, (int) n_embd); + } + + // the pool: every output row followed by a guard of H floats (the old overrun was H - n_embd rows) + const int64_t guard = H, pitch = n_embd + guard; + const float sentinel = 1234.5f; + std::vector outbuf((size_t) (R * pitch), sentinel); + ExpertJobMulti jobs[E]; + for (int e = 0; e < E; ++e) jobs[e].blob = blobs[(size_t) e].data(); + for (int r = 0; r < R; ++r) { + ExpertJobMulti& j = jobs[routes[r][1]]; + j.nact[j.nt] = act[(size_t) routes[r][0]].data(); + j.out[j.nt] = outbuf.data() + (size_t) r * pitch; + ++j.nt; + } + ExpertPool pool(2, false, true); + pool.run_split_multi_native(f, jobs, E); + + double worst = 0; + bool guards = true; + for (int r = 0; r < R; ++r) { + const float* got = outbuf.data() + (size_t) r * pitch; + for (int64_t i = 0; i < n_embd; ++i) + worst = std::max(worst, (double) std::fabs(got[i] - ref[(size_t) r][(size_t) i])); + for (int64_t i = n_embd; i < pitch; ++i) + if (got[i] != sentinel) { guards = false; break; } + } + check(guards, "no output row's guard was written (the down phase stopped at n_embd rows)"); + check(worst <= 1e-5, "the pool's rows equal the kernels' direct rows (worst abs diff " + std::to_string(worst) + ")"); + std::printf("%s: %d failure(s)\n", g_fail == 0 ? "PASS" : "FAIL", g_fail); + return g_fail == 0 ? 0 : 1; +}