diff --git a/engine/include/strata/ling32/ling32_tier.hpp b/engine/include/strata/ling32/ling32_tier.hpp index 6a90afe16e..ad330f00b2 100644 --- a/engine/include/strata/ling32/ling32_tier.hpp +++ b/engine/include/strata/ling32/ling32_tier.hpp @@ -41,7 +41,7 @@ struct TierStats { uint64_t lead_n[3] = {}; uint64_t hint_marked = 0, pf_inflight_skips = 0; uint64_t ssd_layer_dem[64] = {}; // demand SSD reads per layer (statistics) - uint64_t ad_promos = 0, ad_promo_bytes = 0, ad_dropped = 0; // adaptive warm tier: promotions into the RAM tier, bytes, candidates dropped + uint64_t ad_promos = 0, ad_promo_bytes = 0, ad_dropped = 0, ad_drop_ring = 0, ad_drop_spare = 0, ad_drop_score = 0, ad_qfull = 0; // adaptive warm tier: promotions, bytes, candidates dropped (why) // STRATA_L32_PF_COV=1: ceiling of an ADAPTIVE warm tier: uses of experts outside the card prefix, by where the static tiers keep them and by a usage-counted tier of the same size (decay 0.99 / 0.998 per token) uint64_t ad_uses = 0, ad_static_ram = 0, ad_adapt[2] = {}, ad_ssd_uses = 0, ad_ssd_rep[3] = {}; // ad_ssd_rep: SSD-resident uses that were used before within 64 / 256 / 1024 tokens (at least twice) // STRATA_L32_PF_COV=1: coverage of the experts of a decode / verify layer that are NOT in the card prefix, by the previous forward of the same layer ("history") and by the top 8 / 16 / 24 / 32 of the union of the diff --git a/engine/src/ling32/ling32_main.cpp b/engine/src/ling32/ling32_main.cpp index a038eda43b..74642f7aa6 100644 --- a/engine/src/ling32/ling32_main.cpp +++ b/engine/src/ling32/ling32_main.cpp @@ -638,6 +638,7 @@ int cmd_run(const RunOpts& o) { so.device = o.device; so.max_tokens = o.chunk; so.zero_copy = o.miss_explicit ? !o.miss_stage : (igpu_zero_copy ? true : !o.miss_stage); + so.adaptive_default = o.mtp_file.empty(); so.prefill_budget = o.prefill_budget; so.turbo = o.turbo_ssd && plan.mode == TierMode::C; so.turbo_threshold = o.turbo_threshold; diff --git a/engine/src/ling32/ling32_tier_store.cu b/engine/src/ling32/ling32_tier_store.cu index 60bfa75659..3519ce503e 100644 --- a/engine/src/ling32/ling32_tier_store.cu +++ b/engine/src/ling32/ling32_tier_store.cu @@ -187,6 +187,7 @@ struct ExpertStore::Impl { std::vector> quarantine; // (slot, the step after which it is a spare again) bool adapt_on = false; double ad_decay = 0.99, ad_margin = 0.5, ad_min = 2.0; + int ad_qmax = 16, ad_spare = 8, ad_quar = 24; std::vector uc; // decayed use counts per expert std::vector ut; struct Promo { int gi, rs, tries; }; @@ -324,10 +325,14 @@ struct ExpertStore::Impl { if (vp && !fill_vram(err)) return false; if (!so.quiet) std::printf("tier store: hot prefix filled; reading the warm prefix (%.2f GiB)\n", rp / 1073741824.0); if (rp && !fill_host(err)) return false; + adapt_on = so.zero_copy && so.adaptive_default && rp > 0; // default: on where the RAM tier is read in place (the Windows iGPU); STRATA_L32_ADAPT=0 / 1 overrides if (const char* e = std::getenv("STRATA_L32_ADAPT")) adapt_on = e[0] == '1' && rp > 0; if (const char* e = std::getenv("STRATA_L32_AD_DECAY")) ad_decay = std::max(0.5, std::min(0.9999, std::atof(e))); if (const char* e = std::getenv("STRATA_L32_AD_MARGIN")) ad_margin = std::max(0.0, std::atof(e)); if (const char* e = std::getenv("STRATA_L32_AD_MIN")) ad_min = std::max(0.0, std::atof(e)); + if (const char* e = std::getenv("STRATA_L32_AD_Q")) ad_qmax = std::max(1, std::min(std::atoi(e), 256)); + if (const char* e = std::getenv("STRATA_L32_AD_SPARE")) ad_spare = std::max(1, std::min(std::atoi(e), 64)); + if (const char* e = std::getenv("STRATA_L32_AD_QUAR")) ad_quar = std::max(2, std::min(std::atoi(e), 4000)); init_ram_slots(); S.load_s = now_s() - t0; if (so.verify && !verify(err)) return false; @@ -601,7 +606,7 @@ struct ExpertStore::Impl { for (auto& kv : cls_all) { // spares: the two slots of the class that are last in the file (the coldest ranks) std::vector& v = kv.second; if (v.size() < 8) continue; - for (int k = 0; k < 2; ++k) { + for (int k = 0; k < ad_spare; ++k) { const int sid = v[v.size() - 1 - static_cast(k)]; ram_slot_of[static_cast(rslots[static_cast(sid)].gi)] = -1; rslots[static_cast(sid)].gi = -1; @@ -627,7 +632,8 @@ struct ExpertStore::Impl { ut[g] = now; } void maybe_promote(int gi, int rs) { // (under mu) an SSD-resident expert that is in the ring: queue it for the RAM tier if it is used often enough - if (!adapt_on || rp == 0 || cur_tokens > 16 || promo_q.size() >= 6) return; + if (!adapt_on || rp == 0 || cur_tokens > 1) return; // single-token decode only: with MTP verify batches the copies cost more than they save (Strix ABAB -10 %) + if (promo_q.size() >= static_cast(ad_qmax)) { ++S.ad_qfull; return; } if (use_score(gi, static_cast(S.tokens)) < ad_min) return; const uint32_t cls = static_cast(ed[static_cast(gi)].body_bytes); if (!cls_all.count(cls)) return; @@ -655,10 +661,10 @@ struct ExpertStore::Impl { promo_q.pop_front(); Ring& r = ring[static_cast(p.rs)]; if (r.gi == p.gi && r.state == 1 && p.tries < 60) { ++p.tries; promo_q.push_back(p); return false; } // the read is still in flight: look again when it has completed - if (r.gi != p.gi || r.state != 2 || ram_slot_of[static_cast(p.gi)] >= 0) { ++S.ad_dropped; return true; } + if (r.gi != p.gi || r.state != 2 || ram_slot_of[static_cast(p.gi)] >= 0) { ++S.ad_dropped; ++S.ad_drop_ring; return true; } bytes = static_cast(ed[static_cast(p.gi)].body_bytes); auto sp = cls_spare.find(bytes); - if (sp == cls_spare.end() || sp->second.empty()) { ++S.ad_dropped; return true; } + if (sp == cls_spare.end() || sp->second.empty()) { ++S.ad_dropped; ++S.ad_drop_spare; return true; } const long long now = static_cast(S.tokens); const double cand = use_score(p.gi, now); double best = 1e300; @@ -668,7 +674,7 @@ struct ExpertStore::Impl { const double sc = use_score(g, now); if (sc < best) { best = sc; victim = sid; } } - if (victim < 0 || cand <= best + ad_margin) { ++S.ad_dropped; return true; } + if (victim < 0 || cand <= best + ad_margin) { ++S.ad_dropped; ++S.ad_drop_score; return true; } spare = sp->second.back(); sp->second.pop_back(); r.state = 3; // the ring slot stays put until the copy is done @@ -683,7 +689,7 @@ struct ExpertStore::Impl { const int vg = rslots[static_cast(victim)].gi; ram_slot_of[static_cast(vg)] = -1; rslots[static_cast(victim)].gi = -1; - quarantine.emplace_back(victim, static_cast(step) + 80); // the GPU or a DMA may still read the demoted slab: the slot is reused after 80 layer steps + quarantine.emplace_back(victim, static_cast(step) + ad_quar); // the GPU or a DMA may still read the demoted slab: the slot is reused after 80 layer steps ++S.ad_promos; S.ad_promo_bytes += bytes; } @@ -899,7 +905,7 @@ struct ExpertStore::Impl { it.kind = 4; stage.push_back(static_cast(i)); } - if (adapt_on) { // usage counts of the experts outside the card prefix; SSD-resident ones that are in the ring become candidates for the RAM tier + if (adapt_on && cur_tokens <= 1) { // usage counts of the experts outside the card prefix; SSD-resident ones that are in the ring become candidates for the RAM tier for (const Item& it : items) if (it.off + it.bytes > vp) ad_touch(it.gi, static_cast(S.tokens)); for (const Item& it : items) if (!it.src_host && it.kind >= 3 && it.rslot >= 0) maybe_promote(it.gi, it.rslot); } @@ -1588,9 +1594,10 @@ std::string ExpertStore::report() const { } } if (s.ad_promos || s.ad_dropped) { - char c9[200]; - std::snprintf(c9, sizeof c9, "\n adaptive warm tier: %llu promotions into the RAM tier (%.1f MB per token), %llu candidates dropped", - static_cast(s.ad_promos), s.ad_promo_bytes / 1048576.0 / std::max(1.0, static_cast(s.tokens)), static_cast(s.ad_dropped)); + char c9[320]; + std::snprintf(c9, sizeof c9, "\n adaptive warm tier: %llu promotions into the RAM tier (%.1f MB per token), %llu candidates dropped (ring slot gone %llu, no spare slot %llu, not hotter than the victim %llu), %llu not queued (queue full)", + static_cast(s.ad_promos), s.ad_promo_bytes / 1048576.0 / std::max(1.0, static_cast(s.tokens)), static_cast(s.ad_dropped), + static_cast(s.ad_drop_ring), static_cast(s.ad_drop_spare), static_cast(s.ad_drop_score), static_cast(s.ad_qfull)); out += c9; } if (s.ad_uses > 0) { diff --git a/engine/src/ling32/ling32_tier_store.hpp b/engine/src/ling32/ling32_tier_store.hpp index 54f605562e..79efac668f 100644 --- a/engine/src/ling32/ling32_tier_store.hpp +++ b/engine/src/ling32/ling32_tier_store.hpp @@ -22,6 +22,7 @@ struct StoreOptions { int device = 0; int max_tokens = 256; // forward() batch size (sizes the host tables) bool zero_copy = false; // a miss used by one token is read in place (pinned RAM / ring); false: always staged into a slot first + bool adaptive_default = true; // the adaptive warm tier is on by default where zero_copy and a RAM tier exist; main turns it off for MTP (its verify batches gain nothing: Strix ABAB -2 %) bool verify = true; // compare a few slabs of every loaded tier with the file after the start-up fill int prefill_budget = 192; // experts per layer prefetched for a batch (ranked union) bool turbo = false; // --turbo-ssd: skip SSD-resident experts of decode layers once the covered routing mass reaches turbo_threshold (renormalised weights); NOT exact