diff --git a/CMakeLists.txt b/CMakeLists.txt index a12575d..ec51681 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -269,6 +269,12 @@ target_link_libraries(strata_plan INTERFACE strata_warnings) add_executable(strata-plan src/plan/plan_main.cpp) target_link_libraries(strata-plan PRIVATE strata_plan) +if(STRATA_BUILD_TESTS) + # Qwen3.6 port, step 2: the VRAM planner's arithmetic (strata/plan/vram_plan.hpp) against the study's table 4.3 + add_executable(vram_plan_test tests/core/vram_plan_test.cpp) + target_link_libraries(vram_plan_test PRIVATE strata_plan) + add_test(NAME vram_plan_test COMMAND vram_plan_test) +endif() # ---- strata_core (GPU runtime) --------------------------------------------- # Only built when a GPU backend is on, so the Phase 1 targets keep working without a toolkit. diff --git a/include/strata/plan/vram_plan.hpp b/include/strata/plan/vram_plan.hpp new file mode 100644 index 0000000..a229984 --- /dev/null +++ b/include/strata/plan/vram_plan.hpp @@ -0,0 +1,148 @@ +// include/strata/plan/vram_plan.hpp - the VRAM planner's arithmetic, as a pure function (Qwen3.6 port, step 2). +// +// docs/QWEN36-35B-A3B.md section 4.2, in order: the fixed set first (the non-expert weights, the drafter, the KV +// cache for the requested context, the session, the CUDA context, the reserve, the prompt buffers unless prefill +// borrows cache slots); when it does not fit, the context is lowered toward the floor (131,072), and below the +// floor the start refuses with the numbers and the smaller file's name - never a silently shorter context; every +// remaining byte becomes expert slots; then the RAM tier from the available RAM minus a headroom; whatever is left +// stays mapped from the model file on disk. KV streaming is not a lever for this model: dense attention reads +// every cell, so the KV stays in VRAM (3.3 item 6). +// +// Header-only and GPU-free on purpose: the engine feeds it `cudaMemGetInfo` (clamped by --vram-cap) and the file's +// census, the unit test (tests/core/vram_plan_test.cpp) feeds it the study's table 4.1 and checks table 4.3, and +// `strata-plan` can print it for any budget. The slot count is at the MEAN blob: the engine's per-layer sizing +// (generate.cpp, `sized_slots`) prices the same bytes at each profile entry's own blob and lands within a few slots +// of it; the table's ladder is at the mean, so the check is at the mean. +#pragma once + +#include +#include +#include + +namespace strata::plan { + +struct VramPlanInput { + uint64_t budget_bytes = 0; ///< VRAM the engine may use: free at start, or `cap - used` under --vram-cap + uint64_t weights_bytes = 0; ///< the non-expert weights on the GPU (census of the file, token_embd excluded) + uint64_t drafter_bytes = 0; ///< the MTP draft block: dense + experts in VRAM + ring KV + logits + uint64_t kv_bytes_per_token = 0; ///< the KV format's bytes per token over every attention layer + uint64_t session_bytes = 0; ///< rope table, GDN state, scratch + uint64_t cuda_context_bytes = 0; ///< the driver's own share, ~230 to 300 MiB + uint64_t reserve_bytes = 0; ///< --vram-reserve-mib (700 default, 300 on a small card) + uint64_t prompt_bytes = 0; ///< the prompt path's chunk buffers; 0 when prefill borrows cache slots + int64_t context = 262144; ///< requested + int64_t context_floor = 131072; ///< never below this without refusing + int64_t context_step = 4096; ///< a lowered context is a multiple of this + int64_t n_experts = 0; ///< n_layers x n_expert + uint64_t mean_blob = 0; ///< bytes per expert slot (the census mean) + uint64_t ram_available_bytes = 0; ///< host RAM the tier may take (MemAvailable, clamped by --ram-cap) + uint64_t ram_headroom_bytes = 0; ///< left to the OS, the page cache and the KV stream + std::string smaller_file; ///< named in a refusal (the IQ3_XXS file for the Q4 arm) +}; + +struct VramPlan { + bool fits = false; + int64_t context = 0; ///< the context the plan holds (the request, or lowered) + bool lowered = false; ///< the context is below the request + uint64_t kv_bytes = 0; ///< at `context` + uint64_t fixed_bytes = 0; ///< everything but the expert slots, at `context` + uint64_t expert_bytes = 0; ///< budget - fixed (0 when it does not fit) + int64_t vram_slots = 0; ///< expert slots in VRAM (at most n_experts) + uint64_t spare_bytes = 0; ///< left over when every expert fits + int64_t ram_copies = 0; ///< experts the RAM tier holds (the next by profile rank) + int64_t mapped = 0; ///< the rest: read from the model file on a miss + uint64_t ram_tier_bytes = 0; ///< ram_copies x mean_blob + int64_t context_needed_bytes_short = 0; ///< on a refusal: how many bytes short at the floor + std::string refusal; ///< non-empty when !fits: the numbers and the smaller file +}; + +/// The fixed set at `context` tokens. +inline uint64_t vram_plan_fixed(const VramPlanInput& in, int64_t context) { + return in.weights_bytes + in.drafter_bytes + in.kv_bytes_per_token * (uint64_t) context + in.session_bytes + + in.cuda_context_bytes + in.reserve_bytes + in.prompt_bytes; +} + +inline VramPlan make_vram_plan(const VramPlanInput& in) { + VramPlan p; + p.context = in.context; + p.fixed_bytes = vram_plan_fixed(in, in.context); + if (p.fixed_bytes > in.budget_bytes) { + // lower the context toward the floor: the largest multiple of `context_step` the budget holds + const uint64_t without_kv = vram_plan_fixed(in, 0); + int64_t ctx = 0; + if (in.budget_bytes > without_kv && in.kv_bytes_per_token > 0) { + ctx = (int64_t) ((in.budget_bytes - without_kv) / in.kv_bytes_per_token); + ctx -= ctx % (in.context_step > 0 ? in.context_step : 1); + } + if (ctx < in.context_floor) { + const uint64_t at_floor = vram_plan_fixed(in, in.context_floor); + p.context_needed_bytes_short = (int64_t) (at_floor > in.budget_bytes ? at_floor - in.budget_bytes : 0); + const auto mib = [](uint64_t b) { return std::to_string((b + (1u << 20) - 1) >> 20); }; + p.refusal = "the fixed set does not fit: at " + std::to_string(in.context) + " tokens it is " + + mib(p.fixed_bytes) + " MiB (weights " + mib(in.weights_bytes) + ", drafter " + + mib(in.drafter_bytes) + ", KV " + mib(in.kv_bytes_per_token * (uint64_t) in.context) + + ", session " + mib(in.session_bytes) + ", CUDA context " + mib(in.cuda_context_bytes) + + ", reserve " + mib(in.reserve_bytes) + ", prompt buffers " + mib(in.prompt_bytes) + + "), at the floor of " + std::to_string(in.context_floor) + " tokens " + mib(at_floor) + + " MiB, and this card gives " + mib(in.budget_bytes) + " MiB: " + + mib((uint64_t) p.context_needed_bytes_short) + " MiB short even at the floor. " + + (in.smaller_file.empty() ? std::string("Use a smaller quantization of the model") + : "Use the smaller file " + in.smaller_file) + + (in.prompt_bytes > 0 ? ", or an expert profile (prefill then borrows cache slots instead of its " + "own " + mib(in.prompt_bytes) + " MiB of prompt buffers)" + : std::string()) + + "; a context below " + std::to_string(in.context_floor) + " is not chosen for you"; + p.fits = false; + p.context = in.context_floor; + p.kv_bytes = in.kv_bytes_per_token * (uint64_t) in.context_floor; + p.fixed_bytes = at_floor; + p.mapped = in.n_experts; + return p; + } + p.context = ctx; + p.lowered = true; + p.fixed_bytes = vram_plan_fixed(in, ctx); + } + p.fits = true; + p.kv_bytes = in.kv_bytes_per_token * (uint64_t) p.context; + p.expert_bytes = in.budget_bytes - p.fixed_bytes; + const int64_t slots = in.mean_blob > 0 ? (int64_t) (p.expert_bytes / in.mean_blob) : 0; + p.vram_slots = slots < in.n_experts ? slots : in.n_experts; + if (p.vram_slots == in.n_experts) p.spare_bytes = p.expert_bytes - (uint64_t) in.n_experts * in.mean_blob; + const int64_t rest = in.n_experts - p.vram_slots; + const uint64_t ram_budget = in.ram_available_bytes > in.ram_headroom_bytes ? in.ram_available_bytes - in.ram_headroom_bytes : 0; + const int64_t ram_fit = in.mean_blob > 0 ? (int64_t) (ram_budget / in.mean_blob) : 0; + p.ram_copies = ram_fit < rest ? ram_fit : rest; + p.mapped = rest - p.ram_copies; + p.ram_tier_bytes = (uint64_t) p.ram_copies * in.mean_blob; + return p; +} + +/// The start-up log's lines (and `/health`'s counts): the three tiers and what they were sized from. +inline std::string vram_plan_text(const VramPlanInput& in, const VramPlan& p, const char* indent = " ") { + const auto mib = [](uint64_t b) { return std::to_string(b >> 20); }; + std::string s; + s += std::string(indent) + "VRAM budget " + mib(in.budget_bytes) + " MiB\n"; + s += std::string(indent) + "fixed set at " + std::to_string(p.context) + " tokens: " + mib(p.fixed_bytes) + + " MiB = weights " + mib(in.weights_bytes) + " + drafter " + mib(in.drafter_bytes) + " + KV " + mib(p.kv_bytes) + + " + session " + mib(in.session_bytes) + " + CUDA context " + mib(in.cuda_context_bytes) + " + reserve " + + mib(in.reserve_bytes) + (in.prompt_bytes ? " + prompt buffers " + mib(in.prompt_bytes) : std::string(" (prefill borrows cache slots)")) + "\n"; + if (!p.fits) { + s += std::string(indent) + "REFUSED: " + p.refusal + "\n"; + return s; + } + if (p.lowered) + s += std::string(indent) + "context LOWERED from " + std::to_string(in.context) + " to " + std::to_string(p.context) + + ": the fixed set at the request would not fit (the floor is " + std::to_string(in.context_floor) + ")\n"; + s += std::string(indent) + "expert slots in VRAM: " + std::to_string(p.vram_slots) + " of " + std::to_string(in.n_experts) + + " (" + std::to_string(in.n_experts ? 100 * p.vram_slots / in.n_experts : 0) + "%) at " + mib(p.expert_bytes) + + " MiB, " + std::to_string(in.mean_blob) + " B mean blob" + + (p.vram_slots == in.n_experts ? ", every expert resident, " + mib(p.spare_bytes) + " MiB spare" : std::string()) + "\n"; + s += std::string(indent) + "expert copies in RAM: " + std::to_string(p.ram_copies) + " (" + mib(p.ram_tier_bytes) + + " MiB of " + mib(in.ram_available_bytes) + " MiB available minus " + mib(in.ram_headroom_bytes) + " headroom)\n"; + s += std::string(indent) + "experts mapped from the model file: " + std::to_string(p.mapped) + + (p.mapped ? " (read from disk on a miss)" : "") + "\n"; + return s; +} + +} // namespace strata::plan diff --git a/tests/core/vram_plan_test.cpp b/tests/core/vram_plan_test.cpp new file mode 100644 index 0000000..ee94782 --- /dev/null +++ b/tests/core/vram_plan_test.cpp @@ -0,0 +1,189 @@ +// tests/core/vram_plan_test.cpp - the VRAM planner's arithmetic against docs/QWEN36-35B-A3B.md table 4.3 (no GPU). +// +// The inputs are the study's fixed set (table 4.1: weights 2,038 / 1,597 MiB, drafter ~520, KV 1,440 MiB at +// 262,144 as q4_0 over 10 attention layers = 5,760 B per token, session ~164, CUDA context ~300, the 300 MiB small +// reserve, prompt buffers 1,904 / 1,878 MiB when prefill does not borrow) and the census of the two files +// (tests/data/native_experts/qwen36-*.txt: 19,636,158,464 and 11,108,614,144 B over 40 x 256 experts, the "1.829 / +// 1.035 MiB mean blob"). Each ladder row is checked two ways: exactly against the derivation +// floor((budget - fixed) / blob), and within the table's rounding (its values are rounded to tens) of the number +// the study printed. Then the rule's other branches: the context lowered toward 131,072 when the request does +// not fit, the refusal below the floor naming the IQ3 file, the RAM tier at the replay's 9.5 GB, and the disk +// tier that remains on the 1080 Ti. +#include "strata/plan/vram_plan.hpp" + +#include +#include +#include + +using strata::plan::VramPlan; +using strata::plan::VramPlanInput; + +namespace { +int g_fail = 0; +void check(bool ok, const std::string& what) { + std::printf(" %-100s %s\n", what.c_str(), ok ? "ok" : "FAIL"); + if (!ok) ++g_fail; +} +constexpr uint64_t MiB = 1u << 20; +constexpr int64_t kExperts = 40 * 256; +constexpr uint64_t kQ4Total = 19636158464ull, kIq3Total = 11108614144ull; // the two files' expert bytes (census) +constexpr uint64_t kQ4Blob = kQ4Total / kExperts, kIq3Blob = kIq3Total / kExperts; +constexpr uint64_t kKvPerToken = 5760; // q4_0 K and V, 2 KV heads x 256, 10 layers: 1,440 MiB at 262,144 + +VramPlanInput arm(bool q4, bool borrow, uint64_t budget_mib) { + VramPlanInput in; + in.budget_bytes = budget_mib * MiB; + in.weights_bytes = (q4 ? 2038 : 1597) * MiB; + in.drafter_bytes = 520 * MiB; + in.kv_bytes_per_token = kKvPerToken; + in.session_bytes = 164 * MiB; + in.cuda_context_bytes = 300 * MiB; + in.reserve_bytes = 300 * MiB; + in.prompt_bytes = borrow ? 0 : (q4 ? 1904 : 1878) * MiB; + in.context = 262144; + in.context_floor = 131072; + in.n_experts = kExperts; + in.mean_blob = q4 ? kQ4Blob : kIq3Blob; + in.ram_available_bytes = 0; + in.smaller_file = "Qwen3.6-35B-A3B-UD-IQ3_XXS.gguf"; + return in; +} + +/// The derivation the table was made by: floor((budget - fixed) / blob), capped at every expert; -1 = no fit. +int64_t derive(const VramPlanInput& in) { + const uint64_t fixed = strata::plan::vram_plan_fixed(in, in.context); + if (fixed > in.budget_bytes) return -1; + const int64_t s = (int64_t) ((in.budget_bytes - fixed) / in.mean_blob); + return s < in.n_experts ? s : in.n_experts; +} + +struct Row { const char* label; uint64_t budget_mib; int64_t q4_noborrow, q4_borrow, iq3_borrow; }; +constexpr int64_t ALL = kExperts, NOFIT = -1; +// table 4.3's "derived" columns (the study's rounding: to tens; "all" is every expert) +const Row rows[] = { + {"6 GB (5,722 MiB)", 5722, NOFIT, 525, 1350}, + {"8 GB (7,629 MiB)", 7629, 525, 1570, 3200}, + {"11 GB decimal (10,490 MiB)", 10490, 2090, 3130, 5960}, + {"1080 Ti (11,264 MiB)", 11264, 2510, 3550, 6700}, + {"12 GB (11,444 MiB)", 11444, 2610, 3650, 6880}, + {"16 GB (15,258 MiB)", 15258, 4700, 5740, ALL}, + {"24 GB (24,576 MiB)", 24576, 9790, ALL, ALL}, + {"32 GB (30,517 MiB)", 30517, ALL, ALL, ALL}, +}; + +void ladder() { + for (const Row& r : rows) { + struct Col { const char* name; bool q4, borrow; int64_t table; } cols[] = { + {"Q4 no borrow", true, false, r.q4_noborrow}, + {"Q4 borrow", true, true, r.q4_borrow}, + {"IQ3 borrow", false, true, r.iq3_borrow}, + }; + for (const Col& c : cols) { + const VramPlanInput in = arm(c.q4, c.borrow, r.budget_mib); + const VramPlan p = strata::plan::make_vram_plan(in); + const int64_t d = derive(in); + char what[256]; + if (c.table == NOFIT) { + // the fixed set does not fit at 262,144; nor at the floor (6 GB at Q4 without borrowing) + std::snprintf(what, sizeof what, "%s, %s: does not fit at 262,144 nor at 131,072 (table: 0)", r.label, c.name); + check(!p.fits && d == NOFIT && p.refusal.find("Qwen3.6-35B-A3B-UD-IQ3_XXS.gguf") != std::string::npos && + p.refusal.find("131072") != std::string::npos, + what); + continue; + } + const int64_t got = p.vram_slots; + const int64_t diff = got > c.table ? got - c.table : c.table - got; + std::snprintf(what, sizeof what, "%s, %s: %lld slots (derivation %lld, table %s%lld)", r.label, c.name, + (long long) got, (long long) d, c.table == ALL ? "all = " : "~", (long long) c.table); + // exact against the derivation; the printed table rounds to tens (one cell, the 1080 Ti at IQ3, to the + // hundred: 6,711 printed as ~6,700), so the figure it printed is within 15 of the slot count + check(p.fits && !p.lowered && p.context == 262144 && got == d && diff <= 15, what); + if (c.table == ALL) { + std::snprintf(what, sizeof what, "%s, %s: every expert resident, spare %llu MiB", r.label, c.name, + (unsigned long long) (p.spare_bytes >> 20)); + check(p.vram_slots == kExperts && p.mapped == 0 && p.spare_bytes == (in.budget_bytes - p.fixed_bytes) - kExperts * in.mean_blob, what); + } + } + } + // the 24 GB Q4-borrow row's spare: the table says ~1,090 MiB + const VramPlan p24 = strata::plan::make_vram_plan(arm(true, true, 24576)); + check(p24.spare_bytes / MiB >= 1080 && p24.spare_bytes / MiB <= 1095, + "24 GB, Q4 borrow: ~1,090 MiB spare (" + std::to_string(p24.spare_bytes / MiB) + ")"); +} + +void tiers() { + // the RAM tier at the replay's 9.5 GB usable (section 4.3: Q4 ~4,950 experts, IQ3 ~8,760), the 1080 Ti's VRAM + // slots, and the disk tier that remains at Q4 (~1,740 with borrowing, ~2,780 without) + VramPlanInput q4 = arm(true, true, 11264); + q4.ram_available_bytes = 9500000000ull; // 9.5 GB decimal, as the replay + q4.ram_headroom_bytes = 0; + const VramPlan p = strata::plan::make_vram_plan(q4); + check(p.ram_copies >= 4940 && p.ram_copies <= 4960, "1080 Ti, Q4 borrow, 9.5 GB RAM: ~4,950 RAM copies (" + std::to_string(p.ram_copies) + ")"); + check(p.mapped >= 1730 && p.mapped <= 1750 && p.vram_slots + p.ram_copies + p.mapped == kExperts, + "1080 Ti, Q4 borrow: ~1,740 experts stay on disk (" + std::to_string(p.mapped) + "), the three tiers sum to 10,240"); + VramPlanInput q4n = arm(true, false, 11264); + q4n.ram_available_bytes = 9500000000ull; + const VramPlan pn = strata::plan::make_vram_plan(q4n); + check(pn.mapped >= 2770 && pn.mapped <= 2790, "1080 Ti, Q4 no borrow: ~2,780 on disk (" + std::to_string(pn.mapped) + ")"); + VramPlanInput iq3 = arm(false, true, 11264); + iq3.ram_available_bytes = 9500000000ull; + const VramPlan pi = strata::plan::make_vram_plan(iq3); + check(pi.mapped == 0 && pi.ram_copies == kExperts - pi.vram_slots && pi.ram_copies < 8760, + "1080 Ti, IQ3: no disk tier (RAM would hold ~8,760, " + std::to_string(kExperts - pi.vram_slots) + " are needed)"); + // the headroom comes off the RAM before anything is placed + VramPlanInput h = arm(true, true, 11264); + h.ram_available_bytes = 12ull << 30; + h.ram_headroom_bytes = 4ull << 30; + const VramPlan ph = strata::plan::make_vram_plan(h); + check(ph.ram_copies == (int64_t) ((8ull << 30) / kQ4Blob), "12 GiB available minus 4 GiB headroom: the tier is sized from 8 GiB"); +} + +void lowering() { + // Q4 without borrowing on a card between the floor and the request: the context is lowered, a multiple of 4,096, + // never below 131,072, and the plan says so + const VramPlanInput in = arm(true, false, 6400); + const VramPlan p = strata::plan::make_vram_plan(in); + const uint64_t without_kv = strata::plan::vram_plan_fixed(in, 0); + const int64_t expect = (int64_t) (((6400 * MiB - without_kv) / kKvPerToken) / 4096 * 4096); + check(p.fits && p.lowered && p.context == expect && p.context >= 131072 && p.context < 262144 && p.context % 4096 == 0, + "Q4 no borrow at 6,400 MiB: context lowered to " + std::to_string(p.context) + " (a multiple of 4,096 at or above the floor)"); + check(strata::plan::vram_plan_fixed(in, p.context) <= in.budget_bytes && + strata::plan::vram_plan_fixed(in, p.context + 4096) > in.budget_bytes, + "the lowered context is the largest step that fits"); + const std::string text = strata::plan::vram_plan_text(in, p); + check(text.find("LOWERED") != std::string::npos && text.find("131072") != std::string::npos, "the log says LOWERED and names the floor"); + // IQ3 without borrowing on 6 GB: the floor fits (6,200 - 720 = 5,480 MiB), 262,144 does not + const VramPlanInput i6 = arm(false, false, 5722); + const VramPlan p6 = strata::plan::make_vram_plan(i6); + check(p6.fits && p6.lowered && p6.context >= 131072 && p6.vram_slots > 0 && p6.vram_slots == (int64_t) ((5722 * MiB - strata::plan::vram_plan_fixed(i6, p6.context)) / kIq3Blob), + "IQ3 no borrow on 6 GB: lowered to " + std::to_string(p6.context) + " with " + std::to_string(p6.vram_slots) + " slots"); + // the refusal: the floor itself does not fit, the numbers and the smaller file are in the message, nothing is chosen + const VramPlanInput r = arm(true, false, 5722); + const VramPlan pr = strata::plan::make_vram_plan(r); + check(!pr.fits && pr.vram_slots == 0 && pr.context_needed_bytes_short > 0 && + pr.refusal.find("MiB short even at the floor") != std::string::npos && + pr.refusal.find("Qwen3.6-35B-A3B-UD-IQ3_XXS.gguf") != std::string::npos && + pr.refusal.find("expert profile") != std::string::npos, + "Q4 no borrow on 6 GB: refused with the shortfall at the floor, the IQ3 file and the borrowing hint"); + const std::string rt = strata::plan::vram_plan_text(r, pr); + check(rt.find("REFUSED") != std::string::npos, "the log says REFUSED"); + // --vram-cap below the fixed set: the same refusal (the engine clamps the budget; the rule does the rest) + const VramPlanInput cap = arm(true, true, 4000); + check(!strata::plan::make_vram_plan(cap).fits, "a 4,000 MiB budget (a --vram-cap below the fixed set) refuses"); +} +} // namespace + +int main(int argc, char** argv) { + if (argc == 4 && std::string(argv[1]) == "--at") { + // vram_plan_test --at : print the plan for one budget (prefill borrowing) + const VramPlanInput in = arm(std::string(argv[2]) == "q4", true, (uint64_t) std::atoll(argv[3])); + std::printf("%s", strata::plan::vram_plan_text(in, strata::plan::make_vram_plan(in)).c_str()); + return 0; + } + std::printf("vram_plan_test (docs/QWEN36-35B-A3B.md table 4.3)\n"); + ladder(); + tiers(); + lowering(); + std::printf(g_fail ? "vram_plan_test: %d FAILED\n" : "vram_plan_test: all passed\n", g_fail); + return g_fail ? 1 : 0; +}