This repository has been archived on 2026-11-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
radiance-sycl/core/runtime/tierexec.cpp
T
Deadcode 6645337f69 radiance 1.13.0
New models:
- Tencent Hy-Memory (HYV3) from its BF16, FP8 and GGUF checkpoints, with its native tool calls
- M2XFP4: metadata-augmented MXFP4 weights for Qwen3.8-27B and its DFlash2 drafter (top-1 89.1% ->
  92.4% against plain MXFP4), its quantiser and recipe, and ParoQuant's M2XFP4 checkpoints
- Gemma 4 E2B/E4B audio input; NVIDIA-Nemotron-3.5-Bolt-E4B in int8 and 4-bit, on two cards by
  default; Ornith-1.5-122B compose files

Speed:
- Weight reads beside the decode all-reduces (qwen35_fp8, gemma3; --weight-prefetch, on by default):
  the 27B MXFP4 with DFlash2 61.2 -> 67.9 tok/s at c1, TranslateGemma 27B +13%
- Head-256 split-KV decode attention at 256 workgroups and without bank conflicts (27B: +3-5% a
  step at 25-41K context), and the prefill KV split for small launches (a short turn over a long
  context decodes the other sequences 27% faster in that step)
- MXFP4 decode gate_up, activation and fp8 quantiser in one launch (27B, Gemma 3 up to 64 rows:
  TranslateGemma +2-5% at 2-8 streams)
- ParoQuant int4/int5 tiled GEMMs 6-9% faster
- Speculation: a step's tokens are sent before the drafter's passes end, no step waits behind one
  that ends its requests, and the MTP head's first draft no longer collapses after short chats in
  the prefix cache

Features:
- --kv-resident N: KV streaming for a context bigger than VRAM (qwen4exp_fp8, GLM-5.3)
- MiniMax-H3: reference media and keyframe conditioning with the VAE encoders
- Krea 2 rebalance presets balanced, detail and subtle
- The dashboard's request history; a stall watch that reports a frozen step's thread and stack
- rad-tune at the served configuration, collectives measured as the exchange; Gemma 4 E2B/E4B's
  per-layer embeddings read ahead off the step path
- A GPTQ calibration tap and recipe for Gemma 4 31B's w4a8 container

Fixes:
- The two-rank all-reduce checks that a peer's push has landed before reducing it
- z-lab's DFlash drafter on Gemma 4 31B keeps drafting after a prefix-cache hit
- MiniMax-H3's modulation rows

Changes: Plugin ABI 31: out-of-tree plugins must be rebuilt. RADIANCE_PREFETCH_BESIDE is
--weight-prefetch now; RADIANCE_NO_HUGE_PAGES, RADIANCE_IDS_EARLY, RADIANCE_ATTN_BAND_SPLIT,
R4D_PREFILL_SPLITS, RAD_PLE_TIMING and RAD_PLE_CACHE_ROWS are gone.
2026-10-27 16:47:35 +01:00

654 lines
29 KiB
C++

#include "runtime/tierexec.h"
#include "rad_internal.h"
#include <algorithm>
#include <cstdlib>
#include <cstring>
namespace rad {
TierExec::~TierExec() { close(); }
/* gather_rows and scatter_rows are resolved once here, at the width they are called at (`n` is the
* per-group fragment in bytes). Nothing re-resolves on the maintenance path, so the kernel serving
* a transfer cannot change in the middle of a batch. */
int TierExec::configure(Registry* reg, KVManager* kv, IdleTiers* tiers,
const std::vector<int32_t>& groups, int max_batch, int64_t max_copy,
int64_t max_chain, bool leader, int device, int64_t slot_off,
RadStream shared) {
close();
if (!reg || !kv || !tiers || groups.empty() || max_batch <= 0 || max_copy <= 0 ||
max_chain <= 0)
return RAD_E_INVAL;
reg_ = reg; kv_ = kv; tiers_ = tiers; leader_ = leader; device_ = device;
max_batch_ = max_batch; max_copy_ = max_copy; max_chain_ = max_chain;
slot_off_ = slot_off;
slot_bytes_ = tiers->entry_bytes();
shared_ = shared;
RAD_TRY(rad_dev_set(device_));
for (int32_t g : groups) {
const KVGroupPlan* p = kv->plan(g);
if (!p || !p->paged()) continue;
/* A streamed group's host mirror is its tier (docs/KV-STREAMING.md §4.1). */
if (p->streamed()) continue;
GroupInfo gi;
gi.g = g;
gi.n_layers = p->n_layers;
gi.frag = p->layer_bytes_per_block();
gi.layer_stride = p->layer_stride();
gi.n_blocks = p->pool_blocks();
gi.base = p->base;
gi.replicated = p->replicated;
if (p->zero) {
gi.zero = true;
gi_.push_back(gi);
continue;
}
/* The kernel is picked on the fragment width (libr4d's byte-row pair needs n % 4 == 0).
* An unsupported width fails here rather than at launch inside a batch. */
Geometry q;
q.set_s("dtype", "u8");
q.set_i("n", gi.frag);
q.set_i("M", max_batch);
gi.gather = reg->resolve("gather_rows", q, RAD_DOMAIN_DEVICE);
gi.scatter = reg->resolve("scatter_rows", q, RAD_DOMAIN_DEVICE);
if (!gi.gather || !gi.scatter) {
RAD_WARN("kv tiers: no device gather/scatter for group %d at a %lld-byte fragment, "
"so a finished conversation cannot leave VRAM. The tiers are off.",
(int)g, (long long)gi.frag);
close();
return RAD_E_UNSUPPORTED;
}
gi_.push_back(gi);
}
if (gi_.empty()) return RAD_E_UNSUPPORTED;
/* The staging entry is the replicated groups first, then this rank's own, each in cached-group
* order, so the two windows of the slot are each one contiguous run of the entry. */
int64_t off = 0;
for (GroupInfo& gi : gi_)
if (gi.replicated && !gi.zero) { gi.off = off; off += gi.n_layers * gi.frag; }
rep_bytes_ = off;
for (GroupInfo& gi : gi_)
if (!gi.replicated && !gi.zero) { gi.off = off; off += gi.n_layers * gi.frag; }
own_bytes_ = off - rep_bytes_;
/* Every cached group is zero on this rank, so its entry would be empty. One rank that cannot
* take part turns the tiers off for all. */
if (off == 0) {
RAD_WARN("kv tiers: this rank keeps no cache for any group the prefix cache holds "
"(rad_kv_group_zero), so it has no part of an entry to move. The tiers are off.");
close();
return RAD_E_UNSUPPORTED;
}
entry_bytes_ = off;
/* Nothing is built on the device here; see device_init(). */
verify_ = std::getenv("RADIANCE_DEBUG_TIER_VERIFY") != nullptr;
verify_rep_ = verify_ && !leader_ && rep_bytes_ > 0;
char buf[400];
std::snprintf(buf, sizeof buf,
"kv tier transfer: %zu paged group(s), %s an entry%s%s%s, batches of %d through "
"gather_rows/scatter_rows at u8",
gi_.size(), humanb(entry_bytes_).c_str(),
rep_bytes_ > 0 ? " (" : "",
rep_bytes_ > 0 ? humanb(rep_bytes_).c_str() : "",
rep_bytes_ > 0 ? " of it replicated on every rank and held once a slot)" : "",
max_batch_);
report_ = buf;
enabled_ = true;
return RAD_OK;
}
int TierExec::slot_layout(const std::vector<const KVManager*>& ranks,
const std::vector<int32_t>& groups, SlotLayout* out) {
if (!out || ranks.empty()) return RAD_E_INVAL;
for (const KVManager* kv : ranks) if (!kv) return RAD_E_INVAL;
SlotLayout L;
L.own_off.assign(ranks.size(), 0);
L.own_bytes.assign(ranks.size(), 0);
for (int32_t g : groups) {
const KVGroupPlan* p0 = ranks[0]->plan(g);
if (!p0 || !p0->paged()) continue;
/* Skipped as in configure(), so the slot layout and the gather offsets agree. */
if (p0->streamed()) continue;
bool replicated = false;
for (size_t i = 0; i < ranks.size(); ++i) {
const KVGroupPlan* p = ranks[i]->plan(g);
replicated = replicated || (p && p->replicated);
}
if (!replicated) {
for (size_t i = 0; i < ranks.size(); ++i) {
const KVGroupPlan* p = ranks[i]->plan(g);
if (p && p->paged()) L.own_bytes[i] += p->bytes_per_block;
}
continue;
}
/* Every rank reads the one window back into its own pool, so the group must be the same
* width everywhere, declared replicated everywhere and carved everywhere. */
for (size_t i = 0; i < ranks.size(); ++i) {
const KVGroupPlan* p = ranks[i]->plan(g);
const char* why = !p || !p->paged() ? "does not hold it as a paged group"
: p->zero ? "declares it zero (rad_kv_group_zero)"
: !p->replicated ? "does not declare it replicated"
: p->bytes_per_block != p0->bytes_per_block
? "holds it at another width"
: nullptr;
if (!why) continue;
RAD_ERR("kv tiers: KV group '%s' is declared replicated (rad_kv_group_replicated) on "
"one rank, but rank %zu %s. A replicated group is held whole and alike on "
"every rank, since every rank restores it from the one copy a slot holds.",
p0->name.c_str(), i, why);
return RAD_E_STATE;
}
L.rep_bytes += p0->bytes_per_block;
L.replicated.push_back(p0->name);
}
int64_t slot = L.rep_bytes;
for (size_t i = 0; i < ranks.size(); ++i) {
L.own_off[i] = slot;
slot += L.own_bytes[i];
L.entry_bytes = std::max(L.entry_bytes, L.rep_bytes + L.own_bytes[i]);
}
L.slot_bytes = slot;
*out = std::move(L);
return RAD_OK;
}
TierExec::Sizes TierExec::sizes(int64_t entry_bytes, int64_t max_ctx, int64_t block_size) {
Sizes s;
if (entry_bytes <= 0) return s;
/* 16 MiB of staging per rank, clamped to 64..4096 entries (rationale at the bringup call site).
* A copy out is four batches; a restore is a whole chain as long as the longest context. */
s.max_batch = (int)std::min<int64_t>(4096, std::max<int64_t>(64, (16ll << 20) / entry_bytes));
s.max_copy = 4ll * s.max_batch;
const int64_t bs = std::max<int64_t>(1, block_size);
s.max_chain = (std::max<int64_t>(1, max_ctx) + bs - 1) / bs + 1;
return s;
}
/* What device_init() allocates, each allocation rounded to the VRAM pool granule. */
int64_t TierExec::device_bytes(int64_t entry_bytes, int n_groups, const Sizes& s) {
if (entry_bytes <= 0 || n_groups <= 0 || s.max_batch <= 0) return 0;
const int64_t idx_n = (int64_t)n_groups * (s.max_copy + (int64_t)kRing * s.max_chain);
return align_up((int64_t)s.max_batch * entry_bytes, (int64_t)RAD_ALIGN_POOL) +
align_up(idx_n * (int64_t)sizeof(int32_t), (int64_t)RAD_ALIGN_POOL);
}
void TierExec::close() {
/* Drain before freeing buffers the stream may still read. */
if (stream_) (void)rad_stream_sync(stream_);
if (stream_ && own_stream_) rad_stream_destroy(stream_);
stream_ = nullptr; shared_ = nullptr; own_stream_ = false;
release_device();
copy_out_ = false;
gi_.clear();
order_.clear();
verify_pending_.clear();
entry_bytes_ = 0;
rep_bytes_ = own_bytes_ = 0;
verify_ = verify_rep_ = false;
out_sum_.clear();
rep_sum_.clear();
enabled_ = false;
leader_ = false;
reg_ = nullptr; kv_ = nullptr; tiers_ = nullptr;
report_.clear();
}
void TierExec::release_device() {
if (stage_) { rad_dev_free(stage_, RAD_MEM_DEVICE); stage_ = nullptr; }
if (idx_dev_) { rad_dev_free(idx_dev_, RAD_MEM_DEVICE); idx_dev_ = nullptr; }
if (idx_host_) { rad_dev_free(idx_host_, RAD_MEM_HOST_PINNED); idx_host_ = nullptr; }
if (vrep_) { rad_dev_free(vrep_, RAD_MEM_HOST_PINNED); vrep_ = nullptr; }
for (int i = 0; i < kRing; ++i) {
if (ring_ev_[i]) rad_event_destroy(ring_ev_[i]);
ring_ev_[i] = nullptr;
ring_used_[i] = false;
}
if (ev_compute_) { rad_event_destroy(ev_compute_); ev_compute_ = nullptr; }
if (ev_copy_) { rad_event_destroy(ev_copy_); ev_copy_ = nullptr; }
if (ev_restore_) { rad_event_destroy(ev_restore_); ev_restore_ = nullptr; }
}
/* Builds the device side on first use, never at bringup (same rule as Ctx::lane_init()). Built
* eagerly, it makes every decode step 65% longer even when nothing ever moves: on two R9700s with
* Flash-Next at 200K context, 20.5 -> 33.9 ms/step, all of it device time. The cost is not the
* transfers, the pinned arena size or the staging VRAM; it is the extra hardware queue (see
* stream_ in tierexec.h). */
int TierExec::device_init() {
if (stream_) return RAD_OK;
RAD_TRY(rad_dev_set(device_));
const int64_t ng = (int64_t)gi_.size();
copy_idx_ = max_copy_;
chain_idx_ = max_chain_;
const int64_t idx_n = ng * (copy_idx_ + (int64_t)kRing * chain_idx_);
stage_ = rad_dev_alloc((int64_t)max_batch_ * entry_bytes_, RAD_MEM_DEVICE);
idx_dev_ = (int32_t*)rad_dev_alloc(idx_n * (int64_t)sizeof(int32_t), RAD_MEM_DEVICE);
idx_host_ = (int32_t*)rad_dev_alloc(idx_n * (int64_t)sizeof(int32_t), RAD_MEM_HOST_PINNED);
bool ev = rad_event_create(&ev_compute_) == RAD_OK && rad_event_create(&ev_copy_) == RAD_OK &&
rad_event_create(&ev_restore_) == RAD_OK;
for (int i = 0; i < kRing && ev; ++i) ev = rad_event_create(&ring_ev_[i]) == RAD_OK;
/* The verify scratch is a diagnostic's: without it the replicated groups go unchecked, and
* the tiers run as they would without RADIANCE_DEBUG_TIER_VERIFY. */
if (verify_rep_) {
vrep_ = (char*)rad_dev_alloc(copy_idx_ * rep_bytes_, RAD_MEM_HOST_PINNED);
if (!vrep_) {
RAD_WARN("kv tiers: RADIANCE_DEBUG_TIER_VERIFY: no pinned host memory (%s) for this "
"rank's copy of the replicated groups, so they are not compared",
humanb(copy_idx_ * rep_bytes_).c_str());
verify_rep_ = false;
}
}
if (!stage_ || !idx_dev_ || !idx_host_ || !ev) {
release_device();
return RAD_E_NOMEM;
}
/* Borrow the mover's maintenance stream if there is one; both move bytes nobody waits for,
* and a fourth hardware queue costs about 13 ms per decode step. Never the step's own
* stream, or a copy out would become a dependency of the next token. */
if (shared_) {
stream_ = shared_;
own_stream_ = false;
} else if (rad_stream_create(&stream_, /*high_priority=*/0) == RAD_OK) {
own_stream_ = true;
} else {
stream_ = nullptr;
release_device();
return RAD_E_DEVICE;
}
return RAD_OK;
}
/* One (group, layer) launch. Staging rows are at stride entry_bytes_ from `stage_base` (the first
* entry's slot), so each fragment lands inside its own contiguous staging entry. */
int TierExec::run_rows(const Resolved& r, bool scatter, const GroupInfo& gi, int64_t layer,
void* stage_base, const int32_t* d_idx, int64_t n_jobs) {
RadTensor pool{};
pool.dtype = RAD_U8;
pool.rank = 2;
pool.shape[0] = gi.n_blocks; pool.shape[1] = gi.frag;
pool.stride[0] = gi.frag; pool.stride[1] = 1;
pool.data = (char*)gi.base + layer * gi.layer_stride;
RadTensor idx{};
idx.dtype = RAD_I32; idx.rank = 1;
idx.shape[0] = n_jobs; idx.stride[0] = 1;
idx.data = (void*)d_idx;
RadTensor pack{};
pack.dtype = RAD_U8;
pack.rank = 2;
pack.shape[0] = n_jobs; pack.shape[1] = gi.frag;
pack.stride[0] = entry_bytes_; /* one entry's stride, not one row's */
pack.stride[1] = 1;
pack.data = (char*)stage_base + gi.off + layer * gi.frag;
RadTensor t3[3];
if (scatter) { t3[0] = pack; t3[1] = idx; t3[2] = pool; }
else { t3[0] = pool; t3[1] = idx; t3[2] = pack; }
/* Pass the resolved geometry, which carries the winning kernel's derived keys. Its M is the
* band's upper bound; kernels take the row count from the operand extent. */
RadArgs a{};
a.t = t3; a.n_t = 3;
a.p = r.geom.params(); a.n_p = r.geom.n_params();
a.instance = r.instance;
return r.row->info->launch(&a, stream_);
}
/* ------------------------------------------------------------------ RADIANCE_DEBUG_TIER_VERIFY */
/* FNV-1a, continued from `h` so one sum can run over the two windows of a slot. */
static uint64_t tier_fnv_from(uint64_t h, const void* p, int64_t n) {
const unsigned char* b = (const unsigned char*)p;
for (int64_t i = 0; i < n; ++i) { h ^= b[i]; h *= 1099511628211ull; }
return h;
}
static uint64_t tier_fnv(const void* p, int64_t n) {
return tier_fnv_from(1469598103934665603ull, p, n);
}
/* One sum over the entry as the slot holds it: the replicated window, then this rank's own. */
uint64_t TierExec::entry_sum(const char* slot) const {
const uint64_t h = tier_fnv(slot, rep_bytes_);
return tier_fnv_from(h, slot + slot_off_, own_bytes_);
}
void TierExec::verify_note(const BlockHash& key, const char* slot) {
if (!verify_ || !leader_ || !slot) return;
out_sum_[key] = entry_sum(slot);
}
void TierExec::verify_check(const BlockHash& key, const char* slot) {
if (!verify_ || !leader_ || !slot) return;
auto it = out_sum_.find(key);
if (it == out_sum_.end()) return; /* never went out through this process */
const uint64_t back = entry_sum(slot);
if (back == it->second) return;
RAD_ERR("kv tiers: the attention blocks for %s came back different (%016llx out, %016llx in); "
"the tier moved the wrong bytes",
key.str().c_str(), (unsigned long long)it->second, (unsigned long long)back);
}
bool TierExec::take_replicated_sum(const BlockHash& key, uint64_t* sum) {
auto it = rep_sum_.find(key);
if (it == rep_sum_.end()) return false;
if (sum) *sum = it->second;
rep_sum_.erase(it);
return true;
}
bool TierExec::verify_replicated(const TierJob& j, int rank, uint64_t follower_sum) {
if (!verify_ || !leader_ || rep_bytes_ <= 0 || !tiers_ || j.host_slot < 0) return false;
const char* slot = (const char*)tiers_->arena().at(j.host_slot);
if (!slot) return false;
++st_.rep_checked;
const uint64_t mine = tier_fnv(slot, rep_bytes_);
if (mine == follower_sum) return true;
++st_.rep_mismatch;
RAD_ERR("kv tiers: the replicated groups of %s differ between rank 0 (%016llx) and rank %d "
"(%016llx). The plugin declared them replicated (rad_kv_group_replicated) and rank %d "
"holds other bytes; its restore would read rank 0's.",
j.key.str().c_str(), (unsigned long long)mine, rank, (unsigned long long)follower_sum,
rank);
return false;
}
/* ------------------------------------------------------------------ helpers */
/* Like RAD_TRY, but the first failure is logged with the call it came from. A failing tier is
* otherwise invisible: entries stay put and the server only gets slower. */
#define TIER_TRY(call, what) \
do { const int _s = (call); \
if (_s != RAD_OK) { warn_once(what, _s); return _s; } } while (0)
void TierExec::warn_once(const char* what, int st) {
if (warned_) return;
warned_ = true;
RAD_WARN("kv tiers: %s failed with %s. The entry stays where it was; this is said once, and "
"the running failure count is in /sessions.", what, rad_strerror(st));
}
bool TierExec::blocks_backed(const std::vector<int32_t>& blocks, const char* what) {
if (!kv_) return false;
for (size_t k = 0; k < gi_.size(); ++k) {
if (k >= blocks.size()) continue; /* an absent group is the gather's -1 row */
const int32_t b = blocks[k];
if (b < 0 || gi_[k].zero) continue;
const int64_t live = kv_->live_blocks(gi_[k].g);
if (b < live) continue;
if (warned_unbacked_) return false;
warned_unbacked_ = true;
RAD_ERR("kv tier %s: block %d of group %d is at or past this rank's live mark of %lld, so "
"it is lent to the expert slab and holds weights. The transfer is refused; the "
"entry stays where it is.", what, (int)b, (int)gi_[k].g, (long long)live);
return false;
}
return true;
}
int TierExec::copy_runs(const std::vector<TierJob*>& order, size_t first, int64_t n, bool out) {
int64_t k = 0;
while (k < n) {
const int32_t s0 = order[first + (size_t)k]->host_slot;
int64_t run = 1;
while (k + run < n && order[first + (size_t)(k + run)]->host_slot == s0 + run) ++run;
char* host = (char*)tiers_->arena().at(s0);
if (!host) return RAD_E_STATE;
char* dev = (char*)stage_ + k * entry_bytes_;
int st = RAD_OK;
if (out) {
/* The replicated window is the leader's to write; a verifying follower keeps its own
* gather beside the copy, indexed by the job's place in the pass. */
if (rep_bytes_ > 0 && leader_)
st = rad_memcpy_2d_async(host, slot_bytes_, dev, entry_bytes_, rep_bytes_, run,
stream_);
if (st == RAD_OK && verify_rep_ && vrep_)
st = rad_memcpy_2d_async(vrep_ + (int64_t)(first + (size_t)k) * rep_bytes_,
rep_bytes_, dev, entry_bytes_, rep_bytes_, run, stream_);
if (st == RAD_OK && own_bytes_ > 0)
st = rad_memcpy_2d_async(host + slot_off_, slot_bytes_, dev + rep_bytes_,
entry_bytes_, own_bytes_, run, stream_);
} else {
if (rep_bytes_ > 0)
st = rad_memcpy_2d_async(dev, entry_bytes_, host, slot_bytes_, rep_bytes_, run,
stream_);
if (st == RAD_OK && own_bytes_ > 0)
st = rad_memcpy_2d_async(dev + rep_bytes_, entry_bytes_, host + slot_off_,
slot_bytes_, own_bytes_, run, stream_);
}
if (st != RAD_OK) return st;
k += run;
}
return RAD_OK;
}
/* ------------------------------------------------------------------ copy on idle */
int TierExec::issue_copy(std::vector<TierJob>& jobs, RadStream compute) {
if (!enabled_ || jobs.empty()) return RAD_OK;
TIER_TRY(rad_dev_set(device_), "selecting the rank device");
TIER_TRY(device_init(), "building the transfer staging");
const int st = enqueue_copy(jobs, compute);
/* Drain a partly enqueued copy before its host slots are freed by the report, or a queued
* transfer would land on the slot's next owner. */
if (st != RAD_OK) (void)rad_stream_sync(stream_);
return st;
}
int TierExec::enqueue_copy(std::vector<TierJob>& jobs, RadStream compute) {
order_.clear();
for (TierJob& j : jobs) if (!j.blocks.empty()) order_.push_back(&j);
const int64_t n = (int64_t)order_.size();
if (n > copy_idx_) return RAD_E_INVAL;
/* Check every id before any launch: a gather over an unbacked id faults the whole card. */
for (TierJob* j : order_)
if (!blocks_backed(j->blocks, "copy to host")) return RAD_E_STATE;
/* Stream-ordered after everything given to compute, including the step that wrote these. */
TIER_TRY(rad_event_record(ev_compute_, compute), "ordering the copy after the step");
TIER_TRY(rad_event_wait(stream_, ev_compute_), "ordering the copy after the step");
if (n > 0) {
const int64_t ng = (int64_t)gi_.size();
int32_t* h = idx_host_; /* the copy region */
for (int64_t g = 0; g < ng; ++g)
for (int64_t k = 0; k < n; ++k) {
const std::vector<int32_t>& b = order_[(size_t)k]->blocks;
h[g * n + k] = (size_t)g < b.size() ? b[(size_t)g] : -1;
}
TIER_TRY(rad_memcpy_async(idx_dev_, h, ng * n * (int64_t)sizeof(int32_t), stream_),
"the block-index upload");
for (int64_t s = 0; s < n; s += max_batch_) {
const int64_t c = std::min<int64_t>(max_batch_, n - s);
for (size_t gidx = 0; gidx < gi_.size(); ++gidx)
for (int64_t l = 0; l < gi_[gidx].n_layers && gathers(gi_[gidx]); ++l)
TIER_TRY(run_rows(gi_[gidx].gather, /*scatter=*/false, gi_[gidx], l, stage_,
idx_dev_ + (int64_t)gidx * n + s, c),
"the gather out of the pool");
TIER_TRY(copy_runs(order_, (size_t)s, c, /*out=*/true), "the copy to host memory");
}
st_.bytes_out += n * (leader_ ? entry_bytes_ : own_bytes_);
}
TIER_TRY(rad_event_record(ev_copy_, stream_), "marking the copy");
copy_out_ = true;
++st_.passes;
st_.jobs += (int64_t)jobs.size();
if (verify_) {
int64_t ord = 0;
for (const TierJob& j : jobs) {
const bool moved = !j.blocks.empty();
verify_pending_.push_back({j.key, moved ? j.host_slot : -1, moved ? ord : -1});
if (moved) ++ord;
}
}
return RAD_OK;
}
int TierExec::copy_state() {
if (!copy_out_) return 1;
(void)rad_dev_set(device_);
const int q = rad_event_query(ev_copy_);
if (q == 0) return 0;
copy_out_ = false;
if (q > 0 && verify_) {
for (const VerifyNote& v : verify_pending_) {
if (v.host_slot < 0) continue;
if (leader_)
verify_note(v.key, (const char*)tiers_->arena().at(v.host_slot));
else if (verify_rep_ && vrep_ && v.ord >= 0)
rep_sum_[v.key] = tier_fnv(vrep_ + v.ord * rep_bytes_, rep_bytes_);
}
}
verify_pending_.clear();
if (q < 0) warn_once("the copy to host memory", q);
return q > 0 ? 1 : q;
}
int TierExec::wait_copy() {
if (!copy_out_) return 1;
(void)rad_dev_set(device_);
const int st = rad_event_sync(ev_copy_);
if (st != RAD_OK) warn_once("waiting for the copy to host memory", st);
return copy_state();
}
/* ------------------------------------------------------------------ restore */
void TierExec::quiesce() {
if (!enabled_ || !stream_) return;
if (rad_dev_set(device_) < 0) return;
(void)rad_stream_sync(stream_);
}
int TierExec::issue_restore(std::vector<TierJob>& jobs, RadStream compute,
std::vector<char>* ok) {
ok->assign(jobs.size(), 0);
if (!enabled_ || jobs.empty()) return RAD_OK;
TIER_TRY(rad_dev_set(device_), "selecting the rank device");
TIER_TRY(device_init(), "building the transfer staging");
const size_t ng = gi_.size();
/* Block ids are shared across ranks (one block table indexes every shard), so only the leader
* chooses and reserves them; its allocator is the one the prefix cache (rank 0's) references.
* Followers write into the ids they are handed.
*
* The whole chain is reserved before anything moves, with one loan recall. If the pool runs
* short the chain is cut there: a hit needs a contiguous prefix, so entries past a gap are
* useless. */
if (leader_) {
const int64_t n = (int64_t)jobs.size();
std::vector<std::vector<int32_t>> ids(ng);
int64_t m = n;
for (size_t k = 0; k < ng; ++k) {
ids[k].reserve((size_t)n);
m = std::min(m, kv_->reserve_blocks(gi_[k].g, m, &ids[k]));
}
for (size_t k = 0; k < ng; ++k)
for (size_t i = (size_t)m; i < ids[k].size(); ++i) kv_->release_block(gi_[k].g, ids[k][i]);
for (size_t i = 0; i < jobs.size(); ++i) {
jobs[i].blocks.clear();
if ((int64_t)i >= m) continue;
jobs[i].blocks.resize(ng);
for (size_t k = 0; k < ng; ++k) jobs[i].blocks[k] = ids[k][i];
}
}
size_t m = 0;
while (m < jobs.size() && jobs[m].blocks.size() == ng) ++m;
/* The leader chose ids against its own live mark and the recall should have moved every
* follower's with it; verify that here. */
for (size_t i = 0; i < m; ++i)
if (!blocks_backed(jobs[i].blocks, "restore")) { m = i; break; }
/* Followers find the chain by which jobs carry block ids, so every job past the cut releases
* its ids and carries none; otherwise a follower would copy into blocks the cache does not own. */
if (leader_) {
for (size_t i = m; i < jobs.size(); ++i) {
for (size_t k = 0; k < ng && k < jobs[i].blocks.size(); ++k)
if (jobs[i].blocks[k] >= 0) kv_->release_block(gi_[k].g, jobs[i].blocks[k]);
jobs[i].blocks.clear();
}
}
if (m == 0) return RAD_OK;
order_.clear();
for (size_t i = 0; i < m; ++i) order_.push_back(&jobs[i]);
for (size_t i = 0; i < m; ++i)
verify_check(jobs[i].key, (const char*)tiers_->arena().at(jobs[i].host_slot));
/* From here a failure leaves blocks partly overwritten. Mark jobs started first, so report()
* drops a failed started job instead of trusting its host copy. The compute stream waits on
* whatever was enqueued, success or not, because a failed job's blocks return to the pool and
* must not be reused before the queued transfers into them. */
for (size_t i = 0; i < m; ++i) jobs[i].started = true;
const int ri = ring_next_;
ring_next_ = (ring_next_ + 1) % kRing;
int32_t* h = idx_host_ + ng * copy_idx_ + (int64_t)ri * (int64_t)ng * chain_idx_;
int32_t* d = idx_dev_ + ng * copy_idx_ + (int64_t)ri * (int64_t)ng * chain_idx_;
int st = RAD_OK;
size_t done = 0;
for (size_t s = 0; s < m && st == RAD_OK; s += (size_t)chain_idx_) {
const int64_t c = std::min<int64_t>(chain_idx_, (int64_t)(m - s));
if (ring_used_[ri] && (st = rad_event_sync(ring_ev_[ri])) != RAD_OK) break;
for (size_t g = 0; g < ng; ++g)
for (int64_t k = 0; k < c; ++k) h[(int64_t)g * c + k] = jobs[s + (size_t)k].blocks[g];
if ((st = rad_memcpy_async(d, h, (int64_t)ng * c * (int64_t)sizeof(int32_t), stream_)) != RAD_OK)
break;
if ((st = rad_event_record(ring_ev_[ri], stream_)) != RAD_OK) break;
ring_used_[ri] = true;
for (int64_t b = 0; b < c && st == RAD_OK; b += max_batch_) {
const int64_t cc = std::min<int64_t>(max_batch_, c - b);
if ((st = copy_runs(order_, s + (size_t)b, cc, /*out=*/false)) != RAD_OK) break;
for (size_t gidx = 0; gidx < ng && st == RAD_OK; ++gidx)
for (int64_t l = 0; l < gi_[gidx].n_layers && st == RAD_OK && !gi_[gidx].zero; ++l)
st = run_rows(gi_[gidx].scatter, /*scatter=*/true, gi_[gidx], l, stage_,
d + (int64_t)gidx * c + b, cc);
}
if (st == RAD_OK) done = s + (size_t)c;
}
if (st != RAD_OK) warn_once("the restore into the pool", st);
for (size_t i = 0; i < done; ++i) (*ok)[i] = 1;
st_.bytes_in += (int64_t)done * entry_bytes_;
/* The reading step waits on the device; if the event cannot be enqueued, fall back to a host
* wait. */
if (rad_event_record(ev_restore_, stream_) != RAD_OK ||
rad_event_wait(compute, ev_restore_) != RAD_OK) {
(void)rad_stream_sync(stream_);
}
++st_.passes;
st_.jobs += (int64_t)m;
return st;
}
/* ------------------------------------------------------------------ the report */
void TierExec::report_pass(std::vector<TierJob>& jobs, const std::vector<char>& ok,
uint64_t now_ns) {
if (!enabled_ || !leader_) return;
bool quiet = false;
for (size_t i = 0; i < jobs.size(); ++i) {
TierJob& j = jobs[i];
const bool good = i < ok.size() && ok[i];
/* A failed promotion releases the leader's blocks once nothing queued against them can
* still land; after report() nothing names them again. */
if (j.kind == TierJob::Kind::ToDevice && !good) {
if (!quiet) { quiesce(); quiet = true; }
for (size_t k = 0; k < gi_.size() && k < j.blocks.size(); ++k)
if (j.blocks[k] >= 0) kv_->release_block(gi_[k].g, j.blocks[k]);
j.blocks.clear();
}
tiers_->report(j, good, now_ns);
}
}
bool TierExec::plan_promote(const BlockHash& key, TierJob* out) {
if (!enabled_ || !leader_) return false;
return tiers_->plan_promote(key, out);
}
} /* namespace rad */