core/place: --draft-expert-placement host and --expert-profile/--expert-pin-top (decode-hot residency)

This commit is contained in:
baKon authored and StillDeadcode committed 2026-10-28 12:40:47 +01:00
1 parent 9f707a0f67
commit 6b544e0d39
11 files changed
+566 -10

No files matched your search

+36
View File
@@ -298,6 +298,18 @@ static const Flag g_flags[] = {
" mixed steps and draft-head passes, and write the counts to FILE as\n"
" JSON every 30 s and at shutdown. From the routing histograms the core\n"
" already copies back: no device work, no synchronisation" },
{ "--expert-profile", "FILE", "rank the expert plane by this radiance-expert-stats JSON's `decode`\n"
" counts instead of the container's activation profile, which is\n"
" prefill-heavy: decode-hot experts differ. The planner keeps the\n"
" hottest resident; --expert-pin-top makes a chosen slice unmovable" },
{ "--expert-pin-top", "F", "pin the hottest fraction F of the routed experts (0 < F <= 1) by\n"
" --expert-profile's share: heat never demotes them, so the decode-hot\n"
" set stays resident while the rest streams. 0 is off" },
{ "--draft-expert-placement", "auto|host", "where a draft head's (MTP/DSpark) routed experts live.\n"
" `host` pins them in the host pool before any strategy runs, read in\n"
" place like a pooled expert: the drafter's experts fit beside a trunk\n"
" that already fills the card, instead of the strategy having to make\n"
" room on the card. `auto` (the default) leaves them to the strategy" },
{ "--live", nullptr, "the live view: cards, throughput, draft acceptance, the expert plane.\n"
" Redirected it prints one greppable key=value line instead" },
{ "-v", nullptr, "verbose" },
@@ -914,6 +926,27 @@ int config_parse(int argc, char** argv, Config* c) {
else if (S("--debug-accept-reference-kernels")) c->accept_reference_kernels = true;
else if (S("--profile-ops")) c->profile_ops = true;
else if (S("--expert-stats")) { if (!(v = need(i))) return RAD_E_INVAL; c->expert_stats = v; }
else if (S("--expert-profile")) { if (!(v = need(i))) return RAD_E_INVAL; c->expert_profile = v; c->expert_profile_given = true; }
else if (S("--expert-pin-top")) {
if (!(v = need(i))) return RAD_E_INVAL;
char* e = nullptr;
const double d = strtod(v, &e);
if (e == v || *e != '\0' || d < 0.0 || d > 1.0) {
RAD_ERR("--expert-pin-top: '%s' is not a fraction in [0, 1]", v);
return RAD_E_INVAL;
}
c->expert_pin_top = (float)d;
c->expert_pin_top_given = true;
}
else if (S("--draft-expert-placement")) {
if (!(v = need(i))) return RAD_E_INVAL;
if (std::strcmp(v, "auto") != 0 && std::strcmp(v, "host") != 0) {
RAD_ERR("--draft-expert-placement: '%s' is not auto or host", v);
return RAD_E_INVAL;
}
c->draft_expert_placement = v;
c->draft_expert_placement_given = true;
}
else if (S("--live")) c->live_view = true;
else {
RAD_ERR("unknown option '%s'", a.c_str());
@@ -1235,6 +1268,9 @@ const ShowRow kShow[] = {
{ "--debug-accept-reference-kernels", [](const Config& c) { return onoff(c.accept_reference_kernels); } },
{ "--profile-ops", [](const Config& c) { return onoff(c.profile_ops); } },
{ "--expert-stats", [](const Config& c) { return or_none(c.expert_stats, "off"); } },
{ "--expert-profile", [](const Config& c) { return or_none(c.expert_profile, "off"); } },
{ "--expert-pin-top", [](const Config& c) { return fmt("%.4g", c.expert_pin_top); } },
{ "--draft-expert-placement", [](const Config& c) { return c.draft_expert_placement; } },
{ "--live", [](const Config& c) { return onoff(c.live_view); } },
{ "-v", [](const Config& c) { return onoff(c.given.count("-v") > 0); } },
{ "-vv", [](const Config& c) { return onoff(c.given.count("-vv") > 0); } },
+3
View File
@@ -241,6 +241,9 @@ private:
std::vector<TableCopy> table_copies_;
std::vector<void*> card_copies_;
std::vector<std::pair<const void*, void*>> table_copy_of_;
/* --expert-profile: the decode ranking loaded once, before any rank plans, and handed to every
* rank's planner in place of the container's activation profile. */
std::vector<RadFileProfile> expert_profile_;
int load_weights();
int configure_kv();
/* Resolves --checkpoint-slots auto for snapshots of `slot` bytes (`what` names their kind). */
+49 -1
View File
@@ -1695,6 +1695,39 @@ int Engine::plan() {
for (VramFacts& f : facts) f.held_out = cvec_bytes;
RAD_TRY(resolve_table_placement(facts, emb_bytes, wfs, kv_floor, ngram_bytes,
ngram_device, ngram_host, ck_store + host_arenas));
/* --draft-expert-placement host: the drafter's experts are pinned in the host pool before
* the strategy runs, so they are not part of the elastic plane the card must hold and the
* pool must have room for them on top of the trunk's spill. Growing the pool here, before
* the split, is what lets a drafter fit beside a trunk that already fills the card. */
if (cfg_.draft_expert_placement == "host") {
const int64_t MiB = (int64_t)1 << 20;
int64_t draft_max = 0;
for (const WeightFootprint& wf : wfs) draft_max = std::max(draft_max, wf.draft_experts);
if (draft_max == 0 && cfg_.draft_expert_placement_given) {
RAD_ERR("--draft-expert-placement host: this model declares no draft head with "
"routed experts");
return RAD_E_INVAL;
}
if (draft_max > 0) {
/* The pool must hold the drafter on top of the trunk's own spill, so it grows by
* the drafter's bytes and the floor is recomputed against that larger room with
* the drafter taken out of the plane the card places. */
const int64_t add = (align_up(draft_max, MiB) + MiB - 1) / MiB;
cfg_.host_pool_mib += add;
for (size_t i = 0; i < facts.size(); ++i) {
int64_t room = cfg_.host_pool_mib * MiB;
if (cfg_.weights_disk_tier)
room -= file_stage_reserve(wfs[i].layer_unit_max, wfs[i].routed);
facts[i].expert_floor = expert_vram_floor_draft_hosted(
wfs[i], std::max<int64_t>(room - align_up(wfs[i].draft_experts, MiB), 0));
facts[i].experts = std::max<int64_t>(wfs[i].experts - wfs[i].draft_experts, 0);
}
RAD_INFO("placement: --draft-expert-placement host pins %s of drafter experts a "
"rank in the host pool; --host-pool-mib grows by %lld to hold them, and "
"the card places only the trunk's experts",
humanb(draft_max).c_str(), (long long)add);
}
}
/* The n-gram table was counted off the card; on it, every rank holds the whole table. */
if (cfg_.ngram_resolved == TablePlacement::Vram)
for (VramFacts& f : facts) f.statics += ngram_bytes;
@@ -1843,6 +1876,20 @@ int Engine::plan() {
in.profile = file_->profile();
in.n_profile = (int64_t)file_->header().prof_count;
}
/* --expert-profile replaces the container's activation profile, which is prefill-heavy:
* decode-hot experts differ, and the pin below keeps them resident. Loaded once, before
* any rank plans, so every rank ranks the same way. */
if (!cfg_.expert_profile.empty()) {
if (expert_profile_.empty() && ri == 0) {
int32_t trunk = -1;
const int pr = xs_profile_load(cfg_.expert_profile, &expert_profile_, &trunk);
if (pr < 0) return pr;
}
if (!expert_profile_.empty()) {
in.profile = expert_profile_.data();
in.n_profile = (int64_t)expert_profile_.size();
}
}
in.container = file_ != nullptr;
in.host_backend = device_is_host();
Planner planner(in);
@@ -1864,7 +1911,8 @@ int Engine::plan() {
humanb(r->plan.totals.bytes[(int)Tier::Mapped]).c_str()),
r->plan.forced_host_used > 0
? fmt("the host pool holds the %s the placement flags put there (the token "
"embedding, the encoder towers) beside the weights placed in it",
"embedding, the encoder towers, the drafter's experts) beside the "
"weights placed in it",
humanb(r->plan.forced_host_used).c_str())
: std::string());
}
+7 -1
View File
@@ -22,6 +22,7 @@ int HeatEngine::init(const Plan& plan, const HeatParams& params) {
tier_.assign(n, Tier::VRAM);
busy_.assign(n, 0u);
movable_.assign(n, 0u);
pinned_.assign(n, 0u);
layer_of_.assign(n, -1);
bytes_.assign(n, 0);
ever_.assign(n, 0u);
@@ -61,6 +62,7 @@ int HeatEngine::init(const Plan& plan, const HeatParams& params) {
layer_of_[u] = mu.layer;
bytes_[u] = mu.bytes;
movable_[u] = mu.movable ? 1u : 0u;
pinned_[u] = mu.pinned ? 1u : 0u;
if (mu.movable) ++movable;
if (mu.layer < 0) {
if (mu.movable) other[(size_t)n_layer_].push_back((int32_t)u);
@@ -280,7 +282,9 @@ void HeatEngine::set_busy(int32_t unit, bool busy) {
void HeatEngine::set_place(int32_t unit) {
const size_t u = (size_t)unit, p = (size_t)pos_[u];
const bool vram = tier_[u] == Tier::VRAM;
rank_as_[p] = (!movable_[u] || busy_[u]) ? 0u : vram ? 2u : 1u;
/* A pinned resident is never a candidate to leave the card: it ranks as off-device only, so a
* promotion can still target it but a demotion cannot. */
rank_as_[p] = (!movable_[u] || busy_[u] || (pinned_[u] && vram)) ? 0u : vram ? 2u : 1u;
routed_as_[p] = vram ? 2u : 1u;
miss_bytes_[p] = vram ? 0 : bytes_[u];
}
@@ -330,6 +334,8 @@ bool HeatEngine::pick(Swap* out) const {
const int32_t rc = l >= 0 ? res_count_[(size_t)l] : 0;
const float h = heat_[(size_t)pos_[i]];
if (tier_[i] == Tier::VRAM) {
/* A pinned resident never leaves the card, whatever its heat. */
if (pinned_[i]) continue;
/* Colder wins; on a tie, take from the layer holding the most resident units. */
if (cold[c] < 0 || h < cold_h[c] || (h == cold_h[c] && rc > cold_res[c])) {
cold[c] = (int32_t)i; cold_h[c] = h; cold_res[c] = rc;
+3
View File
@@ -210,6 +210,9 @@ private:
std::vector<Tier> tier_;
std::vector<uint8_t> busy_;
std::vector<uint8_t> movable_;
/* --expert-pin-top: a decode-hot resident the planner marked unmovable is never a demotion
* candidate, even where a later pass made its unit movable again. */
std::vector<uint8_t> pinned_;
std::vector<int32_t> layer_of_;
/* Bit 0: promoted before. Bit 1: demoted before. Feeds the readmit counters. */
std::vector<uint8_t> ever_;
+138 -6
View File
@@ -12,6 +12,7 @@
#include "planner.h"
#include <algorithm>
#include <cmath>
#include <cstring>
#include <map>
#include <set>
@@ -103,6 +104,21 @@ int64_t expert_vram_floor(const WeightFootprint& wf, int64_t host_room) {
return std::min(all, std::max<int64_t>(all + 2 * wf.expert_unit_max - host_room, 0));
}
/* A draft head's routed experts, pinned in the host pool before any strategy runs, leave the trunk
* the whole of `host_room`. The card then holds only the trunk's floor against that room. The
* drafter's units share the trunk's slab classes, so the spare the trunk's floor already reserves
* covers them; with no trunk experts the drafter is the whole plane and pinning it leaves the card
* nothing to hold. */
int64_t expert_vram_floor_draft_hosted(const WeightFootprint& wf, int64_t host_room) {
if (wf.draft_experts <= 0) return expert_vram_floor(wf, host_room);
const int64_t trunk = wf.experts - wf.draft_experts;
if (trunk <= 0) return 0;
WeightFootprint t = wf;
t.experts = trunk;
t.draft_experts = 0;
return expert_vram_floor(t, host_room);
}
int plan_check_weights_pool(const Plan& p, int64_t pool_bytes) {
if (p.vram_budget == pool_bytes) return RAD_OK;
RAD_ERR("placement: rank %d planned against a %s weights budget (%lld bytes) and its weights "
@@ -226,6 +242,14 @@ void weight_footprint(const Program& prog, WeightFootprint* out, TablePlacement
layer_experts[mu.layer] += b;
out->expert_unit_max = std::max(out->expert_unit_max, b);
expert_strides.insert(b);
/* A draft head's routed experts: layers at meta.n_layers and above, and only when the
* program declares a drafter. The trunk's own layers stay in the elastic plane a
* strategy places; --draft-expert-placement host takes this part out of it. */
if (prog.drafter.kind != RAD_DRAFT_NONE && prog.meta.n_layers > 0 &&
mu.layer >= (int32_t)prog.meta.n_layers) {
out->draft_experts += b;
out->draft_unit_max = std::max(out->draft_unit_max, b);
}
}
else out->statics += b;
if (mu.layer >= 0 && !mu.mapped) out->layer_unit_max = std::max(out->layer_unit_max, b);
@@ -667,7 +691,7 @@ int Planner::assign_expert_tiered(Plan* p) {
Cost k;
for (int32_t ui : order) {
MoveUnit& mu = p->units[(size_t)ui];
if (mu.access != RAD_ACCESS_CONDITIONAL) continue;
if (mu.access != RAD_ACCESS_CONDITIONAL || mu.preplaced()) continue;
mu.tier = Tier::VRAM;
mu.site = Site::Device;
mu.reason = "fits in vram";
@@ -679,7 +703,7 @@ int Planner::assign_expert_tiered(Plan* p) {
for (int32_t ui : order) {
MoveUnit& mu = p->units[(size_t)ui];
if (mu.access != RAD_ACCESS_CONDITIONAL) continue;
if (mu.access != RAD_ACCESS_CONDITIONAL || mu.preplaced()) continue;
if (k.residents < cap && k.used + mu.slot_bytes <= room) {
k.used += mu.slot_bytes;
++k.residents;
@@ -1027,6 +1051,100 @@ int Planner::pin_towers(const Config& cfg, Plan* p) const {
return RAD_OK;
}
/* --draft-expert-placement host: a draft head's routed experts (layers at meta.n_layers and above,
* and only when the program declares a drafter) go to the pinned host pool before any strategy
* runs. The card reads them in place at the DeviceZeroCopy site, the same way it reads a pooled
* expert, so a drafter's experts fit beside a trunk that already fills the card instead of the
* strategy having to make room for them on it. They are not movable: heat never promotes a draft
* expert onto the card and into the trunk's slab. */
int Planner::pin_draft_experts(const Program& prog, const Config& cfg, Plan* p) const {
if (cfg.draft_expert_placement != "host") return RAD_OK;
if (prog.drafter.kind == RAD_DRAFT_NONE || prog.meta.n_layers == 0) {
if (cfg.draft_expert_placement_given) {
RAD_ERR("placement: --draft-expert-placement host: this model declares no draft head");
p->notes.push_back("--draft-expert-placement host: no draft head to place");
return RAD_E_INVAL;
}
return RAD_OK;
}
const int32_t trunk = (int32_t)prog.meta.n_layers;
auto draft = [&](const MoveUnit& mu) {
return mu.expert >= 0 && mu.layer >= trunk && mu.tier != Tier::Mapped && !mu.host_forced;
};
int64_t need = 0, n = 0, widest = 0;
for (const MoveUnit& mu : p->units)
if (draft(mu)) { need += mu.slot_bytes; widest = std::max(widest, mu.slot_bytes); ++n; }
if (!n) {
RAD_ERR("placement: --draft-expert-placement host: the draft head declares no routed "
"experts");
p->notes.push_back("--draft-expert-placement host: no drafter experts to place");
return RAD_E_STATE;
}
if (need > p->host_budget) {
RAD_ERR("placement: --draft-expert-placement host: the drafter's experts need %s of the "
"pinned host pool and %s of it is left%s", humanb(need).c_str(),
humanb(p->host_budget).c_str(),
p->file_stage_reserve ? " after the file tier's staging" : "");
p->notes.push_back(fmt("--draft-expert-placement host: the drafter's experts (%s) do not "
"fit the host pool", humanb(need).c_str()));
return RAD_E_NOMEM;
}
for (MoveUnit& mu : p->units) {
if (!draft(mu)) continue;
p->host_budget -= mu.slot_bytes;
p->draft_host_bytes += mu.slot_bytes;
mu.tier = Tier::HostPinned;
mu.site = Site::DeviceZeroCopy;
mu.movable = false;
mu.host_forced = true;
mu.reason = "--draft-expert-placement host; read over the link in place";
}
RAD_INFO("placement: the drafter's experts, %lld units (%s, widest %s), in the pinned host "
"pool, read in place (--draft-expert-placement host)", (long long)n,
humanb(need).c_str(), humanb(widest).c_str());
return RAD_OK;
}
/* --expert-pin-top F: the hottest fraction of the routed experts, ranked by the profile the planner
* was given (the container's, or --expert-profile's decode counts), stay resident. They are marked
* unmovable, so the heat engine never demotes them and the decode-hot set stops paying the host
* pool's PCIe read. The cut keeps the hottest ceil(F * n), and a tie at the cut stays movable: the
* threshold is the share just below the hot set, and only a share strictly above it pins. Runs
* after the strategy, which has already put the hot set in VRAM; a hot unit the strategy had to
* leave on the host is left there and counted, since pinning it would need VRAM the card does not
* have. */
void Planner::pin_hot(const Config& cfg, Plan* p) const {
if (cfg.expert_pin_top <= 0.0f) return;
std::vector<float> shares;
for (const MoveUnit& mu : p->units)
if (mu.expert >= 0 && mu.tier != Tier::Mapped) shares.push_back(mu.profile_share);
if (shares.empty()) return;
std::sort(shares.begin(), shares.end());
/* The cut sits after the coldest (1 - F) of the plane, so the hottest F are pinned. A tie at
* the cut stays movable: the threshold is the share just below the hot set, and only a share
* strictly above it pins. */
const double keep = (double)cfg.expert_pin_top * (double)shares.size();
const size_t hot = keep <= 0.0 ? 0 : std::min(shares.size(), (size_t)std::ceil(keep));
const float threshold = hot == 0 ? shares.back() + 1.0f
: shares[shares.size() - hot] - 1.0f;
p->pin_threshold = threshold;
p->profile_units = (int64_t)shares.size();
int64_t pinned = 0, left = 0;
for (MoveUnit& mu : p->units) {
if (mu.expert < 0 || mu.tier == Tier::Mapped || mu.host_forced) continue;
if (mu.profile_share <= threshold) continue;
if (mu.tier != Tier::VRAM) { ++left; continue; }
mu.pinned = true;
mu.movable = false;
++pinned;
}
p->pinned_units = pinned;
RAD_INFO("placement: --expert-pin-top %.4g pins %lld of %lld routed experts resident (share "
"above %.4g); %lld of the hot set had no VRAM slot and stay where the strategy put "
"them", (double)cfg.expert_pin_top, (long long)pinned, (long long)shares.size(),
(double)threshold, (long long)left);
}
/* ------------------------------------------------------------------ plan */
int Planner::plan(Plan* out) {
if (!out || !in_.program || !in_.config) return RAD_E_INVAL;
@@ -1055,6 +1173,13 @@ int Planner::plan(Plan* out) {
pin_mapped(prog, out);
build_classes(out);
/* After build_classes: a unit's slot_bytes is its class's stride, and the pins below charge
* the host pool that. Before it the charge would be zero and the pool would not hold them. */
int hrc = pin_embedding(prog, cfg, out);
const int trc = pin_towers(cfg, out);
if (hrc >= 0) hrc = trc;
if (hrc >= 0) hrc = pin_draft_experts(prog, cfg, out);
/* Charge the file tier's staging pool (RAD_PLACE_FILE_STAGE_SLOTS) before anything is placed
* in the host pool, or the fill could exhaust it and the mover refuse the plan. Sized from the
* widest class carrying a layer unit, the only kind either offload strategy spills; this
@@ -1087,14 +1212,14 @@ int Planner::plan(Plan* out) {
}
}
/* Not RAD_TRY: a refusal still leaves `out` filled in far enough to print. */
int hrc = pin_embedding(prog, cfg, out);
const int trc = pin_towers(cfg, out);
if (hrc >= 0) hrc = trc;
/* Not RAD_TRY: a refusal still leaves `out` filled in far enough to print. The pins ran above,
* once slot_bytes was known. */
int rc = assign(out);
enforce_op_site_agreement(prog, out);
schedule_prefetch(out);
/* After the strategy: it has put the hot set in VRAM, and pinning marks those resident. */
pin_hot(cfg, out);
finish(prog, out);
return hrc < 0 ? hrc : rc;
}
@@ -1222,6 +1347,13 @@ std::string Plan::report(const Program& prog) const {
if (tower_host_bytes)
s += fmt(" encoder tower %12s %12s (--vision-tower-placement ram, pinned, read in place)\n",
"-", humanb(tower_host_bytes).c_str());
if (draft_host_bytes)
s += fmt(" drafter %12s %12s (--draft-expert-placement host, pinned, read in place)\n",
"-", humanb(draft_host_bytes).c_str());
if (pinned_units)
s += fmt(" pinned hot %12s %12lld (--expert-pin-top: resident, heat never demotes them, "
"share above %.4g of %lld)\n", "-", (long long)pinned_units, (double)pin_threshold,
(long long)profile_units);
if (ngram_copy_bytes)
s += fmt(" n-gram copy %12s %12s (--ngram-placement ram, %s, filled at load)\n",
"-", humanb(ngram_copy_bytes).c_str(),
+22 -2
View File
@@ -79,8 +79,9 @@ struct MoveUnit {
Site site = Site::Device;
int32_t slab_slot = -1;
bool movable = false; /* may the heat engine relocate it after load */
bool host_forced = false; /* a placement flag put it in the pinned pool: the embedding's
* or the encoder tower's */
bool pinned = false; /* a decode-hot resident --expert-pin-top kept: heat never demotes it */
bool host_forced = false; /* a placement flag put it in the pinned pool: the embedding's,
* the encoder tower's or the draft head's experts' */
/* Placed before any strategy runs, so every assignment pass skips it and no VRAM total counts
* it: the mapped tier, --embedding-placement ram and --vision-tower-placement ram. */
@@ -227,7 +228,14 @@ public:
* both flags' weight bytes, as the tier totals count them; the pooled figures exclude it. */
int64_t embedding_host_bytes = 0;
int64_t tower_host_bytes = 0;
int64_t draft_host_bytes = 0;
int64_t forced_host_used = 0;
/* --expert-profile and --expert-pin-top: the profile's units the planner pinned resident and the
* share threshold it derived from the fraction. Reported so the window can see what the decode
* set costs the card. */
int64_t pinned_units = 0;
int64_t profile_units = 0;
float pin_threshold = 0.0f;
/* --ngram-placement ram: RAM for the n-gram tables the load phase copies out of the mapping.
* Every pool still sees them on the mapped tier. `ngram_copy_card` selects pinned memory a
* device kernel reads over the link; otherwise locked memory the host gather reads. */
@@ -314,6 +322,8 @@ private:
void pin_mapped(const Program& prog, Plan* p);
int pin_embedding(const Program& prog, const Config& cfg, Plan* p) const;
int pin_towers(const Config& cfg, Plan* p) const;
int pin_draft_experts(const Program& prog, const Config& cfg, Plan* p) const;
void pin_hot(const Config& cfg, Plan* p) const;
void build_classes(Plan* p);
void rank_units(Plan* p, std::vector<int32_t>* order) const;
int assign(Plan* p);
@@ -349,6 +359,11 @@ struct WeightFootprint {
int64_t experts = 0; /* the elastic plane: what streams from host when it does not fit */
int64_t expert_layer_max = 0; /* the most expert bytes any one routed layer holds */
int64_t expert_unit_max = 0; /* the largest expert unit's slot */
/* A draft head's routed experts (layers at meta.n_layers and above, when the program has a
* drafter): the part of `experts` a host-pinned --draft-expert-placement takes out of the host
* pool's room for the trunk, and the largest such unit. */
int64_t draft_experts = 0;
int64_t draft_unit_max = 0;
/* Upper bound on an expert-tiered plan's spare slab: per expert slab class, the shadow slots,
* staging ring and tail pad (Planner::assign_expert_tiered). Same weight budget as residents. */
int64_t expert_spare = 0;
@@ -400,6 +415,11 @@ int64_t file_stage_reserve(int64_t widest, bool routed);
* the host pool. */
int64_t expert_vram_floor(const WeightFootprint& wf, int64_t host_room);
/* The same floor once --draft-expert-placement host has taken the drafter's experts out of the
* plane the strategy places: the trunk's floor against the whole host room. The drafter's units
* share the trunk's slab classes, so no spare is added. With no drafter it is expert_vram_floor. */
int64_t expert_vram_floor_draft_hosted(const WeightFootprint& wf, int64_t host_room);
/* Checks that the plan and this rank's weights pool used the same split: Plan::vram_budget must
* equal the pool Pools::configure allocated, or the slab and the pool disagree silently. Returns
* RAD_E_STATE, logging the rank and both sizes, when they differ. */
+66
View File
@@ -479,4 +479,70 @@ void ExpertStats::stop() {
ExpertStats::~ExpertStats() { stop(); }
/* ------------------------------------------------------------------ --expert-profile */
int xs_profile_load(const std::string& path, std::vector<RadFileProfile>* out, int32_t* n_trunk) {
if (!out) return RAD_E_INVAL;
out->clear();
if (n_trunk) *n_trunk = -1;
FILE* f = std::fopen(path.c_str(), "r");
if (!f) {
RAD_ERR("--expert-profile: cannot read %s: %s", path.c_str(), std::strerror(errno));
return RAD_E_IO;
}
std::string text;
char buf[1 << 16];
while (const size_t n = std::fread(buf, 1, sizeof buf, f)) text.append(buf, n);
const bool bad = std::ferror(f) != 0;
std::fclose(f);
if (bad) {
RAD_ERR("--expert-profile: reading %s failed", path.c_str());
return RAD_E_IO;
}
nlohmann::json j;
try { j = nlohmann::json::parse(text); }
catch (const nlohmann::json::exception& e) {
RAD_ERR("--expert-profile: %s is not JSON: %s", path.c_str(), e.what());
return RAD_E_INVAL;
}
if (!j.is_object() || j.value("format", "") != "radiance-expert-stats/1" ||
!j.contains("counts") || !j["counts"].contains("decode") ||
!j["counts"]["decode"].is_array()) {
RAD_ERR("--expert-profile: %s is not a radiance-expert-stats/1 file with decode counts",
path.c_str());
return RAD_E_INVAL;
}
if (n_trunk && j.contains("n_trunk_layers") && j["n_trunk_layers"].is_number_integer())
*n_trunk = j["n_trunk_layers"].get<int32_t>();
const auto& rows = j["counts"]["decode"];
int64_t kept = 0;
for (size_t l = 0; l < rows.size(); ++l) {
if (!rows[l].is_array()) continue;
uint64_t sum = 0;
for (const auto& c : rows[l])
if (c.is_number_unsigned() || c.is_number_integer())
sum += c.get<uint64_t>();
if (sum == 0) continue;
for (size_t e = 0; e < rows[l].size(); ++e) {
const auto& c = rows[l][e];
if (!(c.is_number_unsigned() || c.is_number_integer())) continue;
const uint64_t n = c.get<uint64_t>();
if (n == 0) continue;
RadFileProfile p{};
p.layer = (int32_t)l;
p.expert = (int32_t)e;
p.share = (float)((double)n / (double)sum);
out->push_back(p);
++kept;
}
}
if (kept == 0)
RAD_WARN("--expert-profile: %s has no decode selections; the planner ranks from the "
"container's profile", path.c_str());
else
RAD_INFO("--expert-profile: %lld decode-hot entries over %zu layers from %s",
(long long)kept, rows.size(), path.c_str());
return RAD_OK;
}
} /* namespace rad */
+7
View File
@@ -210,4 +210,11 @@ private:
std::mutex file_mu_; /* one write at a time */
};
/* --expert-profile FILE: the planner's ranking, from a radiance-expert-stats JSON's `decode`
* counts. One entry per (layer, expert) with a positive count, its share that count over the
* layer's decode selections, so a layer's hottest expert ranks above another layer's merely busy
* one. `n_trunk` is the file's trunk layer count, or -1 where it names none. Returns RAD_E_IO on a
* missing or unreadable file and RAD_E_INVAL on one that is not this format. */
int xs_profile_load(const std::string& path, std::vector<RadFileProfile>* out, int32_t* n_trunk);
} /* namespace rad */
+15
View File
@@ -1070,6 +1070,21 @@ struct Config {
/* --expert-stats FILE: count the routed experts every layer selects, by step kind, and write
* the counts to FILE as JSON every 30 s and at shutdown (place/xstats.h). Empty is off. */
std::string expert_stats;
/* --expert-profile FILE: a radiance-expert-stats JSON (place/xstats.h) whose `decode` counts
* rank the expert plane, instead of the container's activation profile. Empty serves the
* container's profile. */
std::string expert_profile;
bool expert_profile_given = false;
/* --expert-pin-top F: pin the hottest F of the routed experts (0 < F <= 1) by the profile's
* share, keeping them resident: heat never demotes them. 0 is off. */
float expert_pin_top = 0.0f;
bool expert_pin_top_given = false;
/* --draft-expert-placement auto|host: a draft head's (MTP/DSpark) routed experts are pinned in
* the host pool before any strategy runs, streamed in place like a pooled expert. `auto` (the
* default) leaves them to the strategy. `host` is what lets a drafter's experts fit beside a
* trunk that already fills the card. */
std::string draft_expert_placement = "auto";
bool draft_expert_placement_given = false;
/* --live: the live view (spec §16). Off by default like everything else in that section. */
bool live_view = false;
+220
View File
@@ -3908,4 +3908,224 @@ TEST(a_mover_gives_back_every_event_and_device_byte) {
CHECK(vram1 - vram0 <= 2 * MiB + 4096);
}
/* ================================================================== the two placement flags
*
* --draft-expert-placement host pins a draft head's routed experts in the host pool before any
* strategy runs, so they fit beside a trunk that already fills the card. --expert-pin-top keeps
* the hottest fraction of the routed experts resident, so heat never demotes them. */
/* A trunk of `trunk` routed layers and a draft head of `head` layers past it, `n` experts each. */
void build_drafted(Fixture& f, int trunk, int head, int n, int64_t each) {
f.prog.meta.n_layers = (uint32_t)trunk;
f.prog.drafter.kind = RAD_DRAFT_BLOCK;
f.prog.drafter.depth = 5;
build_moe_layers(f, trunk + head, n, each, 0);
}
TEST(draft_experts_pin_to_the_host_pool) {
Fixture f;
build_drafted(f, 2, 1, 4, 1 * MiB);
Config c = base_config();
c.placement = "expert_tiered";
c.vram_weights_mib = 1024;
c.host_pool_mib = 64;
c.draft_expert_placement = "host";
c.draft_expert_placement_given = true;
PlannerInput in{&f.prog, &c, 0, 1, nullptr, 0};
Plan p;
CHECK_OK(Planner(in).plan(&p));
int pinned = 0, trunk_vram = 0;
for (const MoveUnit& mu : p.units) {
if (mu.expert < 0) continue;
if (mu.layer >= 2) {
CHECK(mu.host_forced);
CHECK(mu.tier == Tier::HostPinned);
CHECK(mu.site == Site::DeviceZeroCopy);
CHECK(!mu.movable);
++pinned;
} else if (mu.tier == Tier::VRAM) {
++trunk_vram;
}
}
/* The head's four experts, and only those, are forced into the pool. */
CHECK_EQ(pinned, 4);
CHECK(trunk_vram > 0);
CHECK(p.draft_host_bytes >= 4 * MiB);
CHECK(p.report(f.prog).find("drafter") != std::string::npos);
/* The footprint agrees: the head is the part of the plane the flag takes out. */
WeightFootprint wf;
weight_footprint(f.prog, &wf, TablePlacement::Vram, TablePlacement::Disk, false, false);
CHECK(wf.draft_experts >= 4 * MiB);
CHECK(wf.draft_experts < wf.experts);
}
TEST(draft_placement_auto_leaves_the_head_to_the_strategy) {
Fixture f;
build_drafted(f, 2, 1, 4, 1 * MiB);
Config c = base_config();
c.placement = "all_vram";
c.vram_weights_mib = 1024;
PlannerInput in{&f.prog, &c, 0, 1, nullptr, 0};
Plan p;
CHECK_OK(Planner(in).plan(&p));
for (const MoveUnit& mu : p.units)
if (mu.expert >= 0 && mu.layer >= 2) CHECK(!mu.host_forced);
CHECK_EQ(p.draft_host_bytes, (int64_t)0);
}
TEST(draft_placement_host_refuses_a_model_with_no_drafter) {
Fixture f;
build_moe_layers(f, 2, 4, 1 * MiB, 0);
Config c = base_config();
c.placement = "all_vram";
c.vram_weights_mib = 1024;
c.host_pool_mib = 64;
c.draft_expert_placement = "host";
c.draft_expert_placement_given = true;
PlannerInput in{&f.prog, &c, 0, 1, nullptr, 0};
Plan p;
CHECK_EQ(Planner(in).plan(&p), RAD_E_INVAL);
}
TEST(draft_placement_host_refuses_when_the_pool_is_short) {
Fixture f;
build_drafted(f, 1, 1, 8, 4 * MiB);
Config c = base_config();
c.placement = "expert_tiered";
c.vram_weights_mib = 1024;
c.host_pool_mib = 1;
c.draft_expert_placement = "host";
PlannerInput in{&f.prog, &c, 0, 1, nullptr, 0};
Plan p;
CHECK_EQ(Planner(in).plan(&p), RAD_E_NOMEM);
CHECK(p.report(f.prog).find("DID NOT FIT") != std::string::npos);
}
TEST(expert_pin_top_keeps_the_hottest_resident) {
Fixture f;
build_moe_layers(f, 2, 8, 1 * MiB, 0);
/* Expert e of each layer takes share e + 1, so the hottest are the high indices. */
std::vector<RadFileProfile> prof;
for (int l = 0; l < 2; ++l)
for (int e = 0; e < 8; ++e) {
RadFileProfile r{};
r.layer = l;
r.expert = e;
r.share = (float)(e + 1);
prof.push_back(r);
}
Config c = base_config();
c.placement = "expert_tiered";
c.vram_weights_mib = 1024;
c.host_pool_mib = 1024;
c.expert_pin_top = 0.25f;
PlannerInput in{&f.prog, &c, 0, 1, prof.data(), (int64_t)prof.size()};
Plan p;
CHECK_OK(Planner(in).plan(&p));
int pinned = 0, hot_pinned = 0;
for (const MoveUnit& mu : p.units) {
if (mu.expert < 0) continue;
if (mu.pinned) {
CHECK(mu.tier == Tier::VRAM);
CHECK(!mu.movable);
++pinned;
if (mu.expert >= 6) ++hot_pinned;
}
}
/* The top quarter of 16 experts: the two hottest of each layer. */
CHECK_EQ(pinned, 4);
CHECK_EQ(hot_pinned, 4);
CHECK(p.report(f.prog).find("pinned hot") != std::string::npos);
}
TEST(expert_pin_top_zero_pins_nothing) {
Fixture f;
build_moe_layers(f, 1, 4, 1 * MiB, 0);
Config c = base_config();
c.placement = "all_vram";
c.vram_weights_mib = 1024;
c.expert_pin_top = 0.0f;
PlannerInput in{&f.prog, &c, 0, 1, nullptr, 0};
Plan p;
CHECK_OK(Planner(in).plan(&p));
for (const MoveUnit& mu : p.units) CHECK(!mu.pinned);
CHECK_EQ(p.pinned_units, (int64_t)0);
}
TEST(a_pinned_resident_is_never_demoted) {
Fixture f;
build_moe_layers(f, 1, 4, 1 * MiB, 0);
std::vector<RadFileProfile> prof;
for (int e = 0; e < 4; ++e) {
RadFileProfile r{};
r.layer = 0;
r.expert = e;
r.share = (float)(e + 1);
prof.push_back(r);
}
Config c = base_config();
c.placement = "expert_tiered";
c.vram_weights_mib = 1024;
c.host_pool_mib = 1024;
c.expert_pin_top = 0.5f;
PlannerInput in{&f.prog, &c, 0, 1, prof.data(), (int64_t)prof.size()};
Plan p;
CHECK_OK(Planner(in).plan(&p));
/* Force one pinned unit and one cold unit onto the host, so the engine has a swap to make,
* and heat the cold one far above the pinned one. */
int32_t pinned_u = -1, cold_u = -1;
for (size_t u = 0; u < p.units.size(); ++u) {
if (p.units[u].pinned && pinned_u < 0) pinned_u = (int32_t)u;
if (p.units[u].expert == 0) cold_u = (int32_t)u;
}
CHECK(pinned_u >= 0 && cold_u >= 0);
p.units[(size_t)cold_u].tier = Tier::HostPinned;
p.units[(size_t)cold_u].movable = true;
HeatEngine h;
HeatParams hp;
hp.warmup_dispatches = 0;
CHECK_OK(h.init(p, hp));
const int32_t ids[] = {0};
const int32_t counts[] = {1000};
for (int i = 0; i < 8; ++i) h.observe_ids(0, ids, counts, 1);
Swap sw{};
if (h.pick(&sw)) CHECK(sw.demote != pinned_u);
}
TEST(the_draft_floor_charges_only_the_trunk) {
WeightFootprint wf;
wf.experts = 100;
wf.draft_experts = 30;
wf.expert_unit_max = 5;
wf.expert_spare = 10;
/* With the drafter hosted, the pool's room serves the trunk alone. */
const int64_t hosted = expert_vram_floor_draft_hosted(wf, 80);
const int64_t plain = expert_vram_floor(wf, 80);
CHECK(hosted < plain);
CHECK(hosted <= wf.experts - wf.draft_experts + wf.expert_spare);
/* No drafter: the two floors agree. */
wf.draft_experts = 0;
CHECK_EQ(expert_vram_floor_draft_hosted(wf, 80), expert_vram_floor(wf, 80));
}
RAD_TEST_MAIN()