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

+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 */