From 9efdd00c4c36a2be682a8c66e9ead021cfcea3d4 Mon Sep 17 00:00:00 2001 From: overchop Date: Fri, 30 Oct 2026 10:45:17 +0200 Subject: [PATCH] place: routing-aware file-tier reads The file tier read every file-tier unit of a routed layer whether or not the router sent rows to it. A deep layer keeps whole experts on the drive (288 per world), but a pass routes rows to a handful of them: the read volume was ~18x what the router justified, and the engine streamed ~378 GiB from the SSD per run at 2.05 tok/s (GLM-5.3-Flash, TP2). The layer's router has run by the time the stager triggers -- the trigger is the layer's first expert op -- so the pass's route counts are already on the device. Ctx::route_counts_device exposes them; the file stager hands them to Mover::stage_file_units, and an expert counted zero, which has no sorted rows and no GEMM run naming it, is left on the drive: its weights are not read at all. Its table entries are published at a valid dummy address (the calm region) for the pass and nulled again with the staged units, because the kernels still issue the layer's table and a null entry fails the step. Published even when EVERY unit of the layer is skipped -- a decode step can route nothing to a layer. The drafter's own weights (the "mtp." namespace) run on every draft pass of every decode step, more often than anything a profile could rank, so the planner pins them in the host pool ahead of everything ranked (MoveUnit::draft_head) instead of letting the expert fill push them to the file tier every step. Read extents are also clamped to the container file's size, so a truncated container fails a read loudly instead of reading past EOF. Measured on GLM-5.3-Flash TP2 (2x Radeon R9700), 250-token essay at temp 0: 3.4-3.6 tok/s where main gives 1.42 on the same prompt. Assisted-by: GLM-5.3-Flash --- core/engine_bringup.cpp | 9 +++-- core/place/mover.cpp | 75 +++++++++++++++++++++++++++++++++++++---- core/place/mover.h | 37 +++++++++++++++----- core/place/planner.cpp | 11 ++++++ core/place/planner.h | 6 ++++ core/place/stager.cpp | 57 +++++++++++++++++++++++++++---- core/place/stager.h | 17 +++++++++- core/runtime/ctx.cpp | 15 +++++++++ core/runtime/ctx.h | 5 +++ 9 files changed, 208 insertions(+), 24 deletions(-) diff --git a/core/engine_bringup.cpp b/core/engine_bringup.cpp index 3ecdba82..c8c813fc 100644 --- a/core/engine_bringup.cpp +++ b/core/engine_bringup.cpp @@ -3828,8 +3828,13 @@ int Engine::start() { r->stager.n_layers(), (int)PrefillStager::lead()); } /* The file stager, wherever a routed expert is on the file tier (static or dynamic plan; - * such a unit has no other address). */ - RAD_TRY(r->fstager.init(r->program, r->plan, &r->mover)); + * such a unit has no other address). Routing-aware reads take this + * rank's routing reports: the trigger runs after the layer's router on the same stream. */ + RAD_TRY(r->fstager.init(r->program, r->plan, &r->mover, + [](void* ud, int32_t layer, int64_t* n) -> const int32_t* { + return ((Ctx*)ud)->route_counts_device(layer, n); + }, + &r->ctx)); if (r->fstager.enabled() && r->index == 0) RAD_INFO("file stager: %zu routed layers hold experts read from the container; each " "pass reads a layer's (up to %s) into one of two VRAM buffers before its " diff --git a/core/place/mover.cpp b/core/place/mover.cpp index f2aa60db..ad0363a8 100644 --- a/core/place/mover.cpp +++ b/core/place/mover.cpp @@ -25,6 +25,8 @@ int FileTier::open(const std::string& path) { if (rc < 0) RAD_ERR("file tier: cannot open %s with O_DIRECT. The tier needs a filesystem that " "supports it -- tmpfs and some overlay mounts do not.", path.c_str()); + else + size_ = f_.size(); return rc; } @@ -32,8 +34,12 @@ int FileTier::read(uint64_t offset, int64_t bytes, void* dst, int64_t dst_cap) { if (!f_.is_open()) return RAD_E_STATE; if (!dst || bytes <= 0) return RAD_E_INVAL; /* The extent rounded up to the alignment: the unit stride already covers it, and reading - * one extra block is cheaper than a branch on the step path. */ - const int64_t want = direct_align(bytes); + * one extra block is cheaper than a branch on the step path -- unless the rounding crosses + * the container's end: the final unit's bytes can end at an unaligned EOF, and pread_exact + * calls that short read an error. Clamp to the file, which sends the tail through read_at's + * bounce path (it tolerates an EOF short read) instead of the exact fast path. */ + int64_t want = direct_align(bytes); + if (size_ >= 0 && (int64_t)offset + want > size_) want = size_ - (int64_t)offset; if (want > dst_cap) { RAD_ERR("file tier: a %s read needs a %s aligned buffer, the staging slot is %s", humanb(bytes).c_str(), humanb(want).c_str(), humanb(dst_cap).c_str()); @@ -213,7 +219,9 @@ int Mover::init(const Program& prog, const Plan& plan, const MoverConfig& cfg, P * * Nothing is memset; the load phase writes every weight. rad_memset_async on * RAD_MEM_HOST fails on a real card but succeeds on the host backend, so use std::memset - * if a host buffer ever needs clearing. */ + * if a host buffer ever needs clearing. The pool's cache cycle takes slots + * on demand: a deposit with no free slot relabels the coldest idle unit back to the + * file tier, copyless. */ } if (static_need > 0) { @@ -313,6 +321,17 @@ int Mover::init(const Program& prog, const Plan& plan, const MoverConfig& cfg, P return RAD_E_NOMEM; } n_windows_ = RAD_PLACE_FILE_WINDOWS; + + /* A zeroed region the skipped units' table entries point at. A dead row that reads + * through a dummy (a scale plane re-read, a dropped slot) sees zeros, which dequantise + * finite -- pointing it at the staging buffer's base handed it real weight bytes whose + * reinterpretation produced NaNs. */ + calm_ = (char*)reserve("file-tier dummy region", 8 << 20, RAD_MEM_HOST_PINNED); + if (!calm_) { + RAD_ERR("mover: no pinned memory for the file tier's dummy region"); + return RAD_E_NOMEM; + } + std::memset(calm_, 0, 8 << 20); } /* ---- streams, events, move records ------------------------------------------------------ */ @@ -660,9 +679,12 @@ constexpr int64_t kReadThrough = (int64_t)256 << 10; } /* namespace */ int Mover::stage_file_units(const std::vector& units, char* dst, int64_t cap, - RadEvent after, RadEvent done, std::vector* staged) { + RadEvent after, RadEvent done, std::vector* staged, + std::vector* skipped, const int32_t* ecnt, + int32_t n_expert) { if (!plan_ || !dst || !staged || !done) return RAD_E_INVAL; staged->clear(); + if (skipped) skipped->clear(); if (!file_.ok() || !windows_ || n_windows_ <= 0) return RAD_E_STATE; /* The layout in `dst`, unit by unit in the order given, and every weight's extent. */ @@ -671,8 +693,20 @@ int Mover::stage_file_units(const std::vector& units, char* dst, int64_ int64_t off = 0; for (int32_t u : units) { if (u < 0 || (size_t)u >= unit_.size()) return RAD_E_INVAL; - const UnitState& us = unit_[(size_t)u]; + UnitState& us = unit_[(size_t)u]; if (us.tier != Tier::SSD || us.slab_slot >= 0 || us.klass < 0) continue; + /* Routing-aware reads: the layer's router has run by the time the stager triggers + * (the trigger is the layer's first expert op), so `ecnt` holds this pass's counts. + * An expert counted zero has no sorted rows, so no GEMM run names it and nothing + * reads its weights: leave its bytes on the drive. The dummy publication below + * keeps every table entry valid for the pass. */ + if (ecnt && skipped) { + const MoveUnit& mu = plan_->units[(size_t)u]; + if (mu.expert >= 0 && mu.expert < n_expert && ecnt[mu.expert] == 0) { + skipped->push_back(u); + continue; + } + } const int64_t need = align_up(us.bytes, RAD_ALIGN_UNIT); if (off + need > cap) { RAD_ERR("file stager: a routed layer's file-tier units need more than the %s buffer " @@ -689,11 +723,33 @@ int Mover::stage_file_units(const std::vector& units, char* dst, int64_ at.push_back(off); off += need; } - if (staged->empty()) return RAD_OK; + /* Skipped units' table entries must not name their stale addresses either: point them + * into the layer buffer, whose base is a valid allocation no zero-count expert reads + * through (a dead row would read row 0 of a real expert's region and discard it). The + * caller nulls them again with the staged units. Published even when EVERY unit of the + * layer was skipped -- a decode step can route nothing to a layer -- because the kernels + * still issue the layer's table and a null entry fails the step. */ + if (skipped) { + for (int32_t u : *skipped) { + const MoveUnit& mu = plan_->units[(size_t)u]; + for (size_t j = 0; j < mu.weights.size(); ++j) + res_.publish(mu.weights[j], calm_); + stats_.ssd_skipped_bytes += (uint64_t)unit_[(size_t)u].bytes; + } + stats_.ssd_skipped_units += (uint64_t)skipped->size(); + } + if (ext.empty()) return RAD_OK; /* Container order, so a window is one long run of the drive rather than a seek per weight. */ std::stable_sort(ext.begin(), ext.end(), [](const FileExt& a, const FileExt& b) { return a.foff < b.foff; }); + /* The dashboard's SSD panel and /stats ssd_reads read these: the file tier's container + * reads are the expert SSD traffic the metric names. The scheduled prefetch path counted + * its own link reads here; the file stager's were invisible, so the panels showed zero + * however much the drive streamed. */ + stats_.ssd_reads += (uint64_t)ext.size(); + for (const FileExt& e : ext) stats_.ssd_bytes += (uint64_t)e.bytes; + if (after) RAD_TRY(rad_event_wait(mover_, after)); const int64_t A = DirectFile::alignment(); @@ -769,7 +825,7 @@ int Mover::stage_file_units(const std::vector& units, char* dst, int64_ } ++stats_.file_layers; stats_.file_units += (uint64_t)staged->size(); - stats_.h2d_bytes += (uint64_t)off; + stats_.h2d_bytes += off; return RAD_OK; } @@ -1618,6 +1674,11 @@ std::string Mover::report() const { (double)stats_.file_bytes * gib, (unsigned long long)stats_.file_preads, rs, rs > 0 ? (double)stats_.file_bytes / rs * 1e-9 : 0.0, (double)stats_.file_wait_ns * 1e-9); + if (stats_.ssd_skipped_units) + s += fmt(" file stager: routing-aware reads skipped %llu units (%.2f GiB) whose " + "experts were not routed this pass\n", + (unsigned long long)stats_.ssd_skipped_units, + (double)stats_.ssd_skipped_bytes * gib); } return s; } diff --git a/core/place/mover.h b/core/place/mover.h index 59f75059..496e65f8 100644 --- a/core/place/mover.h +++ b/core/place/mover.h @@ -239,11 +239,14 @@ public: int read(uint64_t offset, int64_t bytes, void* pinned_dst, int64_t dst_cap); /* RadFileEntry offsets are RAD_ALIGN_UNIT-aligned (rad_format.h), which is DirectFile's - * alignment, so the mover's reads need no bounce buffer. */ + * alignment, so the mover's reads need no bounce buffer -- except the container's final unit, + * whose bytes can end at an unaligned EOF (the MTP head layer's last expert); that read is + * clamped to the file end and takes read_at's bounce path, which tolerates an EOF short read. */ static int64_t direct_align(int64_t v) { return align_up(v, DirectFile::alignment()); } private: DirectFile f_; + int64_t size_ = -1; /* container size, cached at open */ }; /* ------------------------------------------------------------------ residency */ @@ -347,6 +350,10 @@ struct MoverStats { * for a window's copies to drain. */ uint64_t file_layers = 0, file_units = 0, file_bytes = 0, file_preads = 0; uint64_t file_read_ns = 0, file_wait_ns = 0; + /* Routing-aware reads: units of experts whose routing count was zero at the layer's + * trigger, not read (their weights are re-published to the layer buffer as a dummy + * address for the pass, and nulled again with the staged units). */ + uint64_t ssd_skipped_units = 0, ssd_skipped_bytes = 0; }; /* The live twin of MoverStats: every counter an atomic. The mover writes these through the @@ -369,6 +376,7 @@ struct MoverCounters { std::atomic stage_copies{0}; std::atomic file_layers{0}, file_units{0}, file_bytes{0}, file_preads{0}; std::atomic file_read_ns{0}, file_wait_ns{0}; + std::atomic ssd_skipped_units{0}, ssd_skipped_bytes{0}; void reset() { /* Field by field: atomics are not copy-assignable. */ @@ -400,6 +408,8 @@ struct MoverCounters { file_preads.store(0, std::memory_order_relaxed); file_read_ns.store(0, std::memory_order_relaxed); file_wait_ns.store(0, std::memory_order_relaxed); + ssd_skipped_units.store(0, std::memory_order_relaxed); + ssd_skipped_bytes.store(0, std::memory_order_relaxed); } MoverStats snapshot() const { @@ -426,12 +436,14 @@ struct MoverCounters { s.staged_bytes = staged_bytes.load(std::memory_order_relaxed); s.stage_overflow = stage_overflow.load(std::memory_order_relaxed); s.stage_copies = stage_copies.load(std::memory_order_relaxed); - s.file_layers = file_layers.load(std::memory_order_relaxed); - s.file_units = file_units.load(std::memory_order_relaxed); - s.file_bytes = file_bytes.load(std::memory_order_relaxed); - s.file_preads = file_preads.load(std::memory_order_relaxed); - s.file_read_ns = file_read_ns.load(std::memory_order_relaxed); - s.file_wait_ns = file_wait_ns.load(std::memory_order_relaxed); + s.file_layers = file_layers.load(std::memory_order_relaxed); + s.file_units = file_units.load(std::memory_order_relaxed); + s.file_bytes = file_bytes.load(std::memory_order_relaxed); + s.file_preads = file_preads.load(std::memory_order_relaxed); + s.file_read_ns = file_read_ns.load(std::memory_order_relaxed); + s.file_wait_ns = file_wait_ns.load(std::memory_order_relaxed); + s.ssd_skipped_units = ssd_skipped_units.load(std::memory_order_relaxed); + s.ssd_skipped_bytes = ssd_skipped_bytes.load(std::memory_order_relaxed); return s; } }; @@ -622,9 +634,17 @@ public: * fills, behind `after` (if set), with `done` recorded last. Published * settled. RAD_E_SCRATCH if the units exceed `cap`. The reads block this * thread. + * + * Routing-aware reads: with `ecnt` (host, `n_expert` entries, the layer's per-expert + * routing counts for the current pass), a unit whose expert is counted zero is not read: + * it goes to `skipped`, its weights are published at `dst` (a dummy address inside the + * layer buffer, valid memory a no-run expert never reads through), and the caller nulls + * it again with the staged units. Null `ecnt` reads the whole layer. * file_buffer buffer 0 or 1 of the plan's two, or null when the plan has none. */ int stage_file_units(const std::vector& units, char* dst, int64_t cap, - RadEvent after, RadEvent done, std::vector* staged); + RadEvent after, RadEvent done, std::vector* staged, + std::vector* skipped = nullptr, + const int32_t* ecnt = nullptr, int32_t n_expert = 0); char* file_buffer(int i) const { return (file_vram_ && (i == 0 || i == 1)) ? file_vram_ + i * file_buf_bytes_ : nullptr; } @@ -768,6 +788,7 @@ private: int64_t file_buf_bytes_ = 0; char* windows_ = nullptr; int n_windows_ = 0; + char* calm_ = nullptr; /* zeroed: dummy table entries read zeros, not NaN */ int win_next_ = 0; std::vector win_ev_; std::vector win_rec_; diff --git a/core/place/planner.cpp b/core/place/planner.cpp index 2ea42205..2aba9cc5 100644 --- a/core/place/planner.cpp +++ b/core/place/planner.cpp @@ -399,6 +399,9 @@ void Planner::build_units(const Program& prog, Plan* p) { mu.offset.push_back(at); mu.bytes = at + weight_bytes(wi); if (!in_.container || !weight_file_direct(wi, prog.ctx.world_size)) mu.file_direct = false; + /* The drafter's own weights (the "mtp." namespace) run on every draft pass of every + * decode step; the expert fill pins them in the host pool ahead of everything ranked. */ + if (wi.name.rfind("mtp.", 0) == 0) mu.draft_head = true; if (wi.first_use_op >= 0 && (mu.first_use_op < 0 || wi.first_use_op < mu.first_use_op)) mu.first_use_op = wi.first_use_op; if (wi.last_use_op > mu.last_use_op) mu.last_use_op = wi.last_use_op; @@ -466,6 +469,10 @@ void Planner::rank_units(Plan* p, std::vector* order) const { std::stable_sort(order->begin(), order->end(), [&](int32_t a, int32_t b) { const MoveUnit& ua = p->units[(size_t)a]; const MoveUnit& ub = p->units[(size_t)b]; + /* The drafter's weights run on every draft pass of every decode step -- more often than + * anything the profile could rank -- so they take the pool first, before heat or the + * share order gets a vote. */ + if (ua.draft_head != ub.draft_head) return ua.draft_head > ub.draft_head; if (ua.profile_share != ub.profile_share) return ua.profile_share > ub.profile_share; /* Expert-major tie-break, which is the whole policy without a profile (every share is * 0.0). Layer-major would fill layer 0, then layer 1, and leave the layers past the @@ -691,6 +698,10 @@ int Planner::assign_expert_tiered(Plan* p) { Cost k; for (int32_t ui : order) { MoveUnit& mu = p->units[(size_t)ui]; + /* A PREPLACED UNIT IS ALREADY PLACED: pin_mapped put it in the container's mapping + * (host-only op) and build_classes gave it no class and a 0-byte slot. Re-tiering it + * here would strand it -- VRAM tier with no slab slot behind it -- and the mover + * would find no address for it at init. */ if (mu.access != RAD_ACCESS_CONDITIONAL || mu.preplaced()) continue; mu.tier = Tier::VRAM; mu.site = Site::Device; diff --git a/core/place/planner.h b/core/place/planner.h index b6faf117..ba2282c6 100644 --- a/core/place/planner.h +++ b/core/place/planner.h @@ -102,6 +102,12 @@ struct MoveUnit { /* Every weight lies in the container exactly as its kernel reads it (weight_file_direct), so * the file tier can stage it with a read. A unit that is not never goes to the file tier. */ bool file_direct = true; + + /* The drafter's own weights (the "mtp." namespace). They run on every draft pass of every + * decode step -- up to depth passes plus the verify pass, each re-staging them from the drive + * if they land in the file tier -- so the expert fill pins them in the host pool ahead of + * everything ranked. One layer's experts, a fraction of the pool. */ + bool draft_head = false; }; /* The strongest access class in a group, "read most surely", decides the group's: a group with diff --git a/core/place/stager.cpp b/core/place/stager.cpp index fb5e703a..1379712f 100644 --- a/core/place/stager.cpp +++ b/core/place/stager.cpp @@ -371,12 +371,30 @@ FileStager::~FileStager() { if (done_[b]) rad_event_destroy(done_[b]); if (free_[b]) rad_event_destroy(free_[b]); } + if (scratch_) rad_dev_free(scratch_, RAD_MEM_HOST_PINNED); } -int FileStager::init(const Program& prog, const Plan& plan, Mover* mover) { +int FileStager::init(const Program& prog, const Plan& plan, Mover* mover, + RouteCounts routes, void* routes_ud) { layers_.clear(); if (!mover) return RAD_OK; + /* Routing-aware reads: the default. RADIANCE_FILE_ROUTED=0 restores + * whole-layer reads. The source may still decline per layer (a layer that has not + * reported this pass is read whole). */ + routes_ = routes; + routes_ud_ = routes_ud; + routed_ = true; + if (const char* e = std::getenv("RADIANCE_FILE_ROUTED")) routed_ = e[0] != '0'; + scratch_ = nullptr; + scratch_n_ = 0; + if (routes_ && routed_ && prog.meta.n_expert > 0) { + scratch_n_ = prog.meta.n_expert; + scratch_ = (int32_t*)rad_dev_alloc((size_t)scratch_n_ * sizeof(int32_t), + RAD_MEM_HOST_PINNED); + if (!scratch_) { scratch_n_ = 0; routes_ = nullptr; } + } + /* A unit declared and never read has no op span and is left out. */ size_t c = 0; layers_ = routed_layers(plan, [](const MoveUnit& mu) { @@ -408,6 +426,13 @@ int FileStager::init(const Program& prog, const Plan& plan, Mover* mover) { if ((size_t)layers_[i].first_op < first_of_.size()) first_of_[(size_t)layers_[i].first_op] = (int32_t)i; mover_ = mover; + /* Temporary diagnostics for the routing-aware-read engagement. */ + if (routes_ && routed_) + RAD_INFO("file stager: routing-aware reads armed (%zd routed layers, scratch %s)", + layers_.size(), scratch_ ? "ok" : "MISSING"); + else if (routed_) + RAD_INFO("file stager: routing-aware reads NOT armed (routes_=%d scratch_=%d)", + routes_ ? 1 : 0, scratch_ ? 1 : 0); for (int b = 0; b < 2; ++b) if (rad_event_create_local(&done_[b]) < 0 || rad_event_create_local(&free_[b]) < 0) { layers_.clear(); @@ -434,12 +459,28 @@ bool FileStager::will_issue(int32_t li) const { return it == pass_last_.end() || layers_[(size_t)li].first_op <= it->second; } -int FileStager::start(int32_t li) { +int FileStager::start(int32_t li, RadStream s) { const int b = li & 1; if (holds_[b] >= 0) return RAD_E_STATE; /* released before the next read, by construction */ + /* Routing-aware reads: the trigger (this call) runs at the layer's first expert op, + * after the layer's router on the same stream, so this pass's counts are ordered + * before the copy. The stream sync costs far less than a gigabyte-scale layer read; + * a source that declines (no report yet this pass) reads the whole layer. */ + const int32_t* ecnt = nullptr; + int32_t n_expert = 0; + if (routes_ && routed_) { + int64_t n = 0; + const int32_t* dev = routes_(routes_ud_, layers_[(size_t)li].layer, &n); + if (dev && n > 0 && n <= scratch_n_ && + rad_memcpy_async(scratch_, dev, (size_t)n * sizeof(int32_t), s) >= 0 && + rad_stream_sync(s) >= 0) { + ecnt = scratch_; + n_expert = (int32_t)n; + } + } const int rc = mover_->stage_file_units(layers_[(size_t)li].units, buf_[b], cap_, free_rec_[b] ? free_[b] : nullptr, done_[b], - &staged_[b]); + &staged_[b], &skipped_[b], ecnt, n_expert); if (rc < 0) return rc; if (!staged_[b].empty()) holds_[b] = li; return RAD_OK; @@ -450,6 +491,10 @@ int FileStager::finish(int32_t b, RadStream s) { free_rec_[b] = true; mover_->unstage_units(staged_[b]); staged_[b].clear(); + if (!skipped_[b].empty()) { + mover_->unstage_units(skipped_[b]); /* the dummy publications go to null too */ + skipped_[b].clear(); + } holds_[b] = -1; return RAD_OK; } @@ -466,7 +511,7 @@ int FileStager::before_op(rad_op h, RadStream s, bool* resync) { if (it != layers_.end()) { const int32_t li = (int32_t)(it - layers_.begin()); if (will_issue(li)) { - RAD_TRY(start(li)); + RAD_TRY(start(li, s)); *resync = true; } } @@ -482,7 +527,7 @@ int FileStager::before_op(rad_op h, RadStream s, bool* resync) { /* A layer the read-ahead skipped (the learned bound said the pass ends before it) is read * here rather than issued without addresses. */ if (holds_[li & 1] != li) { - RAD_TRY(start(li)); + RAD_TRY(start(li, s)); *resync = true; } if (holds_[li & 1] == li) RAD_TRY(rad_event_wait(s, done_[li & 1])); @@ -490,7 +535,7 @@ int FileStager::before_op(rad_op h, RadStream s, bool* resync) { const int32_t next = li + 1; if ((size_t)next < layers_.size() && will_issue(next)) { - RAD_TRY(start(next)); + RAD_TRY(start(next, s)); *resync = true; } return RAD_OK; diff --git a/core/place/stager.h b/core/place/stager.h index 8ad5fa1d..b7952ab8 100644 --- a/core/place/stager.h +++ b/core/place/stager.h @@ -238,11 +238,19 @@ class FileStager final : public OpStager { public: ~FileStager() override; + /* Routing-aware reads: the engine binds a source of the layer's per-expert routing + * counts for the current pass (the router reports them to the Ctx before the layer's + * first expert op, which is the stager's trigger). Returns the device pointer of + * `n_expert` i32 counts, or null when the layer has not reported this pass — the + * caller then reads the whole layer. Null `routes` reads whole layers. */ + using RouteCounts = const int32_t* (*)(void* ud, int32_t layer, int64_t* n_expert); + /* Binds the routed layers with file-tier units and the ops that read them, over the mover's * two file buffers. Leaves enabled() false when no routed unit is on the file tier. Refuses a * plan whose routed layers are read by interleaved ops, since a file-tier unit has no other * address to fall back on. */ - int init(const Program& prog, const Plan& plan, Mover* mover); + int init(const Program& prog, const Plan& plan, Mover* mover, + RouteCounts routes = nullptr, void* routes_ud = nullptr); bool enabled() const { return !layers_.empty(); } size_t n_layers() const { return layers_.size(); } @@ -260,10 +268,17 @@ private: Mover* mover_ = nullptr; char* buf_[2] = { nullptr, nullptr }; int64_t cap_ = 0; + RouteCounts routes_ = nullptr; /* routing-aware reads; null reads whole layers */ + void* routes_ud_ = nullptr; + bool routed_ = true; /* RADIANCE_FILE_ROUTED=0 disables the filtering */ + int32_t* scratch_ = nullptr; /* pinned, for the trigger's blocking counts copy */ + int64_t scratch_n_ = 0; + std::vector skipped_[2]; /* per buffer: units not read, nulled at release */ RadEvent done_[2] = { nullptr, nullptr }; /* behind a buffer's copies, on the mover stream */ RadEvent free_[2] = { nullptr, nullptr }; /* behind the ops that read a buffer's layer */ bool free_rec_[2] = { false, false }; std::vector staged_[2]; + int start(int32_t li, RadStream s); int32_t holds_[2] = { -1, -1 }; /* the layer a buffer holds, or -1 */ int32_t cur_ = -1; /* the layer whose ops are being issued */ diff --git a/core/runtime/ctx.cpp b/core/runtime/ctx.cpp index aa3b1fb5..918583bc 100644 --- a/core/runtime/ctx.cpp +++ b/core/runtime/ctx.cpp @@ -773,6 +773,21 @@ const RadRouting* Ctx::last_routing(int layer) const { return route_[(size_t)layer].reported >= 0 ? &route_[(size_t)layer].routing : nullptr; } +const int32_t* Ctx::route_counts_device(int layer, int64_t* n_expert) const { + if (n_expert) *n_expert = 0; + if (layer < 0 || (size_t)layer >= route_.size()) return nullptr; + const RouteSlot& s = route_[(size_t)layer]; + /* No step gate: route_report lands at the END of the block's passes (heat consumes it a + * step later), but the reported buffer is the block's own ecnt, whose CONTENT the router + * kernel rewrote this pass -- and the file stager's trigger runs after that router on the + * same stream, so a stream-ordered copy reads this pass's counts. The counts are indexed + * by the rank's local expert slot (the kernel subtracts expert_base), which is also what + * the mover's units carry. */ + if (!s.routing.expert_count || s.reported < 0) return nullptr; + if (n_expert) *n_expert = s.routing.n_expert ? s.routing.n_expert : route_n_expert_; + return s.routing.expert_count; +} + /* ================================================================== profiling (spec §16) */ int Ctx::bind_profiling() { diff --git a/core/runtime/ctx.h b/core/runtime/ctx.h index a4d82d9d..fe2d754e 100644 --- a/core/runtime/ctx.h +++ b/core/runtime/ctx.h @@ -327,6 +327,11 @@ public: /* Called by the drain, so the ring knows which banks are still unread. */ void mark_credited(int layer, int bank); const RadRouting* last_routing(int layer) const; + /* The device pointer of `layer`'s expert counts for the current step, with `n_expert` set, + * or null when the layer has not reported this step (a routing-aware file stager then + * reads the layer whole). The pointer is stream-ordered behind the router: a copy on the + * compute stream at the stager's trigger sees this pass's counts. */ + const int32_t* route_counts_device(int layer, int64_t* n_expert) const; /* ---------------------------------------------------------- the placement seam */ /* Change an op's execution site. Refuses with RAD_E_NOKERNEL, naming op and band, if the