Files
Strata/engine/tests/ling32/ling32_tier_planner_test.cpp
Niko1221 de6f7bb0d4 ling32 tier B pool layout: k=2 prefetch predictor by default
The pool layout (every card byte is a dynamic slot, no hot prefix) now plans lookahead_k = 2 instead of 4. The main program then loads and evaluates only the k=2 predictor (lookahead_ks bit 0) and the tier store prefetches from the k=2 list. The old layout (--tier-pool off) and tier C keep k=4.

Measured (quiet host, ABAB, plain decode, same job file, output ids identical on all 22 jobs against the k=4 arm and against plain):
- RTX 3090 (PCIe 4.0): k=2 54.3 against k=4 53.0 tok/s, +2.0 %.
- RTX 5090 (PCIe 5.0, 47 GB/s): k=2 110.2 / 110.3 / 110.1 against k=4 107.5 / 107.5 tok/s, +2.6 %.
- Recall of the true top-8 experts: k=2 64.6 %, k=4 54.0 %; the k=2 lead is 0.8 ms against 1.7 ms for k=4, and the better recall wins.

Env STRATA_L32_LA_KS and STRATA_L32_PF_K still override the choice. The tier planner test expects lookahead_k 2 for the pool layout.
2026-12-03 02:41:04 +01:00

206 lines
11 KiB
C++

// ling32_tier_planner_test: the memory-tier planner on a synthetic Section 3 and on the fixture's directory.
#include "strata/ling32/ling32_file.hpp"
#include "strata/ling32/ling32_tier_planner.hpp"
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
using namespace strata::ling32;
static int g_fail = 0;
#define CHECK(c) \
do { \
if (!(c)) { std::printf("FAIL line %d: %s\n", __LINE__, #c); ++g_fail; } \
} while (0)
// n_moe layers x n_exp experts, rank r slab = (3 + r % 3) * 16 KiB * scale; physical order (rank, layer); expert id = (rank * 7 + layer) % n_exp
static std::vector<strata::artifact::StrataExpertEntry> synth(int n_moe, int n_exp, uint64_t scale, bool break_order) {
std::vector<strata::artifact::StrataExpertEntry> d(static_cast<size_t>(n_moe) * n_exp);
uint64_t off = 0;
for (int r = 0; r < n_exp; ++r)
for (int l = 0; l < n_moe; ++l) {
const int eid = (r * 7 + l * 3) % n_exp; // a permutation per layer when gcd(7, n_exp) = 1
auto& e = d[static_cast<size_t>(l) * n_exp + eid];
std::memset(&e, 0, sizeof e);
e.expert_id = static_cast<uint16_t>(eid);
e.aux_offsets[0] = static_cast<uint32_t>(r);
e.body_bytes = static_cast<uint32_t>((3 + r % 3) * 16384 * scale);
e.body_offset = off;
off += e.body_bytes;
}
if (break_order) d[5].body_offset += 16384;
return d;
}
int main(int argc, char** argv) {
constexpr uint64_t GiB = 1ull << 30, MiB = 1ull << 20;
{ // the table
const int n_moe = 6, n_exp = 32;
auto d = synth(n_moe, n_exp, 16, false);
RankTable rt;
std::string err;
CHECK(build_rank_table(d.data(), d.size(), n_exp, rt, err));
CHECK(rt.prefix.size() == n_exp + 1u);
CHECK(rt.prefix[0] == 0);
for (int r = 0; r < n_exp; ++r) CHECK(rt.prefix[r + 1] - rt.prefix[r] == static_cast<uint64_t>(n_moe) * (3 + r % 3) * 16384 * 16);
CHECK(rt.ranks_fitting(rt.prefix[10]) == 10);
CHECK(rt.ranks_fitting(rt.prefix[10] - 1) == 9);
CHECK(rt.ranks_fitting(~0ull) == n_exp);
CHECK(rt.ranks_fitting(0) == 0);
auto bad = synth(n_moe, n_exp, 16, true);
RankTable rb;
CHECK(!build_rank_table(bad.data(), bad.size(), n_exp, rb, err));
}
{ // plans on a real-sized synthetic table: 512 ranks x 40 layers, slabs 3.0..5.8 MiB (about 77 GiB), rank sizes growing towards the hot ranks
const int n_moe = 40, n_exp = 512;
std::vector<strata::artifact::StrataExpertEntry> d(static_cast<size_t>(n_moe) * n_exp);
uint64_t off = 0;
for (int r = 0; r < n_exp; ++r)
for (int l = 0; l < n_moe; ++l) {
const int eid = (r * 13 + l * 5) % n_exp;
auto& e = d[static_cast<size_t>(l) * n_exp + eid];
std::memset(&e, 0, sizeof e);
e.expert_id = static_cast<uint16_t>(eid);
e.aux_offsets[0] = static_cast<uint32_t>(r);
e.body_bytes = static_cast<uint32_t>(((r < 64 ? 6045696ull : r < 200 ? 4521984ull : 3096576ull) + 16383) / 16384 * 16384);
e.body_offset = off;
off += e.body_bytes;
}
RankTable rt;
std::string err;
CHECK(build_rank_table(d.data(), d.size(), n_exp, rt, err));
CHECK(rt.total == off);
TierSizes sz;
sz.trunk_bytes = 3900 * MiB;
sz.latent_bytes = 8192ull * 7 * 576 * 4;
TierHardware hw;
TierRequestOpts rq;
// a 96 GiB card: Mode A
hw.vram_bytes = 96 * GiB; hw.ram_bytes = 64 * GiB;
TierPlan p = plan_tier(hw, rq, rt, sz);
CHECK(p.ok && p.mode == TierMode::A && p.vram_ranks == n_exp && p.lookahead_k == 0);
// a 24 GiB card + 128 GiB RAM: Mode B, everything else in RAM
hw.vram_bytes = 24 * GiB; hw.ram_bytes = 128 * GiB;
p = plan_tier(hw, rq, rt, sz);
std::printf("%s\n", p.describe().c_str());
// the default for cards up to 30 GiB is the pool layout: no static prefix, every card byte for experts is a slot
CHECK(p.ok && p.mode == TierMode::B && p.lookahead_k == 2 && p.pool_mode && p.vram_ranks == 0 && p.dyn_slots > 1000);
CHECK(p.vram_prefix_bytes == 0 && p.ram_prefix_bytes == rt.total && p.ssd_bytes == 0 && p.ring_slots == 0);
CHECK(p.vram_total_planned <= p.vram_budget);
// the old layout (hot prefix + a small pool) is --tier-pool off
rq.pool = 0;
p = plan_tier(hw, rq, rt, sz);
std::printf("%s\n", p.describe().c_str());
CHECK(p.ok && p.mode == TierMode::B && p.lookahead_k == 4 && !p.pool_mode && p.vram_ranks > 0 && p.vram_ranks < n_exp);
CHECK(p.vram_prefix_bytes + p.ram_prefix_bytes == rt.total && p.ssd_bytes == 0 && p.ring_slots == 0);
CHECK(p.vram_total_planned <= p.vram_budget);
rq.pool = -1;
// a 32 GiB card (RTX 5090, R9700): pool mode by default (budget 31.75 GiB <= the 32.5 GiB auto line); --tier-pool off gives the static prefix;
// a 48 GiB card is above the line (not measured): static prefix
{
TierHardware h32 = hw;
h32.vram_bytes = 32 * GiB;
TierRequestOpts r32;
TierPlan a32 = plan_tier(h32, r32, rt, sz);
CHECK(a32.ok && a32.pool_mode && a32.vram_ranks == 0 && a32.dyn_slots > 3000 && a32.vram_total_planned <= a32.vram_budget);
r32.pool = 0;
TierPlan c32 = plan_tier(h32, r32, rt, sz);
CHECK(c32.ok && !c32.pool_mode && c32.vram_ranks > 0);
TierHardware h48 = hw;
h48.vram_bytes = 48 * GiB;
TierRequestOpts r48;
TierPlan a48 = plan_tier(h48, r48, rt, sz);
CHECK(a48.ok && !a48.pool_mode);
r48.pool_auto_max = 64ull << 30;
TierPlan b48 = plan_tier(h48, r48, rt, sz);
CHECK(b48.ok && b48.pool_mode && b48.vram_total_planned <= b48.vram_budget);
}
// an MTP sidecar takes its bytes out of the pool
{
const int s0 = p.dyn_slots;
(void)s0;
TierPlan q0 = plan_tier(hw, rq, rt, sz);
rq.mtp_bytes = 1ull * GiB;
TierPlan q1 = plan_tier(hw, rq, rt, sz);
rq.mtp_bytes = 0;
CHECK(q1.dyn_slots < q0.dyn_slots && q1.vram_total_planned <= q1.vram_budget);
}
// 8 GiB card + 16 GiB RAM budget: Mode C, ring, SSD share
hw.vram_bytes = 8 * GiB; hw.ram_bytes = 64 * GiB;
rq.ram_budget = 16 * GiB;
p = plan_tier(hw, rq, rt, sz);
std::printf("%s\n", p.describe().c_str());
CHECK(p.ok && p.mode == TierMode::C && p.lookahead_k == 4 && p.ring_slots >= 40 && p.ssd_bytes > 0);
CHECK(p.vram_prefix_bytes + p.ram_prefix_bytes + p.ssd_bytes == rt.total);
CHECK(p.ram_total_planned <= p.ram_budget);
CHECK(!p.turbo_ssd); // never the default
rq.turbo_ssd = true;
p = plan_tier(hw, rq, rt, sz);
CHECK(p.turbo_ssd);
rq.turbo_ssd = false;
// forced hybrid that cannot fit
rq.tier = TierRequest::Hybrid;
p = plan_tier(hw, rq, rt, sz);
CHECK(!p.ok && !p.error.empty());
// forced vram on a small card
rq.tier = TierRequest::Vram;
p = plan_tier(hw, rq, rt, sz);
CHECK(!p.ok);
// --vram-budget-gib on a big card simulates a small one
rq = TierRequestOpts{};
hw.vram_bytes = 96 * GiB; hw.ram_bytes = 128 * GiB;
rq.vram_budget = 12 * GiB; rq.ram_budget = 24 * GiB; rq.tier = TierRequest::Nvme;
p = plan_tier(hw, rq, rt, sz);
std::printf("%s\n", p.describe().c_str());
CHECK(p.ok && p.mode == TierMode::C && p.vram_total_planned <= 12 * GiB);
// monotone: a bigger card holds at least as many ranks
int last = -1;
for (int g : {8, 10, 12, 16, 24, 32, 48, 64}) {
hw.vram_bytes = static_cast<uint64_t>(g) * GiB;
hw.ram_bytes = 128 * GiB;
rq = TierRequestOpts{};
p = plan_tier(hw, rq, rt, sz);
CHECK(p.ok && p.vram_ranks >= last);
last = p.vram_ranks;
}
}
if (argc > 1) { // the fixture's real Section 3
Ling32File f;
std::string err;
CHECK(f.open(argv[1], err, false));
const auto& dir = f.reader().expert_directory();
RankTable rt;
CHECK(build_rank_table(dir.data(), dir.size(), f.cfg().n_exp, rt, err));
std::printf("fixture: %d ranks, %d layers, Section 4 %llu B, slab %llu..%llu B\n", rt.n_exp, rt.n_moe, static_cast<unsigned long long>(rt.total), static_cast<unsigned long long>(rt.min_slab),
static_cast<unsigned long long>(rt.max_slab));
CHECK(rt.total == f.section_bytes(4));
}
{ // --mtp-auto: the measured rows (see mtp_auto_pays)
std::string why;
CHECK(mtp_auto_pays(TierMode::A, false, 0.0, &why) && !why.empty()); // everything resident: pays (5090 tier A, PRO 6000, Spark)
CHECK(mtp_auto_pays(TierMode::B, true, 0.0)); // unified memory (Mac, Spark, Strix): pays
CHECK(mtp_auto_pays(TierMode::B, false, 46.0, &why)); // RTX 5090, PCIe 5.0 x16: +11 % decode
CHECK(mtp_auto_pays(TierMode::B, false, 32.0)); // the threshold itself
CHECK(mtp_auto_pays(TierMode::B, false, 46.0, &why, 5466)); // RTX 5090: 5466 slots
CHECK(!mtp_auto_pays(TierMode::B, false, 39.0, &why, 2413) && !why.empty()); // RTX 5070 Ti 16 GB on PCIe 5.0: 2413 slots, -0.7 %
CHECK(!mtp_auto_pays(TierMode::B, false, 46.0, &why, 1485)); // 12 GB emulation: -2.4 %
CHECK(mtp_auto_pays(TierMode::B, false, 46.0, &why, kMtpAutoMinCardExperts)); // the cut itself
CHECK(mtp_auto_pays(TierMode::B, false, 25.0, &why, 5958) && !why.empty()); // AMD R9700 (PCIe 4.0, 24-26 GB/s, 5958 slots): MTP +11..14 % measured, the pool size decides, not the link
CHECK(!mtp_auto_pays(TierMode::B, false, 21.5, &why, 4076)); // RTX 3090 / 4090: 4076 slots, forced MTP never gains
CHECK(!mtp_auto_pays(TierMode::B, false, 12.0, &why, 6000) && !why.empty()); // PCIe 3.0 x16 with a big pool: not measured, below the floor
CHECK(!mtp_auto_pays(TierMode::B, false, 0.0, &why, 6000)); // link not measured: not loaded whatever the pool
CHECK(mtp_auto_pays(TierMode::B, true, 0.0, &why, 100)); // unified memory ignores the pool size
CHECK(mtp_auto_pays(TierMode::A, false, 0.0, &why, 100)); // tier A: everything is resident
CHECK(!mtp_auto_pays(TierMode::B, false, 24.4, &why) && !why.empty()); // RTX 3090 / 4090, PCIe 4.0 x16: 1.00 tokens per verify
CHECK(!mtp_auto_pays(TierMode::B, false, 12.0)); // PCIe 3.0 x16 / 4.0 x8 (5060 Ti, 5080 16 GB): -3 %
CHECK(!mtp_auto_pays(TierMode::B, false, 0.0, &why) && !why.empty()); // link not measured: not loaded (--mtp forces it)
CHECK(!mtp_auto_pays(TierMode::C, false, 50.0)); // SSD tier: never
CHECK(kMtpAutoMinLinkGbps > 24.4 && kMtpAutoMinLinkGbps < 45.0);
}
std::printf(g_fail ? "FAILED (%d)\n" : "ling32_tier_planner_test: all passed\n", g_fail);
return g_fail ? 1 : 0;
}