Archived
merge origin/main: the upstream LoRA-runtime refactor beneath the ref2va port
Merges upstream 1.10.0 (152 commits: the LoRA acquire/runtime refactor, the
form-1 LTX-2 scheduler family, guidance terms + negative prompts, decoder
selection, input_reference, new architectures) into the ref2va port branch.
Resolution rule throughout: keep BOTH feature sets; ABI numbering follows
upstream (RAD_ABI_VERSION 26, RAD_FORMAT_VER 2), with the port's video fields
unioned into RadVideoGenBatch/RadVideoGenDecl and abi_layout_table.h
regenerated from the live table.
Notable reconciliations:
* videos.cpp: the task grammars are disjoint by capability -- keyframe tasks
(i2va/l2va/flash-attn l2va/fl2va) are recognized only when decl.encodes,
so upstream models keep refusing them as unknown tasks; input_reference,
the LoRA tag/field pipeline, and the guidance/negative_prompt rendering
coexist with refs/ref2va; the prompt put() helper hoisted to block scope
for the media branch.
* video_schedule(): upstream's (steps, video_tokens, refine) keeps its
positional slots; the port's shift_video/shift_audio overrides move to the
tail so upstream's schedule tests call unchanged.
* r4d_selftest conv3d cases: the port's sp/st/causal/down/refl geometry and
upstream's tpad/tpre fold into one reference (ft = causal ? 2 : tpad==2 ?
2+tpre : 1); rms_mod rows keep the port's i8/fp8 quant modes alongside
upstream's wadd/norm_once/norm parameters.
* tests/oot_plugins.sh: the per-plugin cmake --install gains a --prefix
staging dir -- plugin .so files still land in the absolute
RAD_PLUGIN_INSTALL_HOME, but libr4d now also installs r4d_selftest to a
relative bin/, which needs no root.
Host ctest: 112/113 (mm_test's webp gap pre-exists this merge); r4d_selftest,
engine_e2e, oot_plugins, abi_layout, all LoRA tests green.
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
commit
a1a75da7e9
415 files changed
+78837
-5310
No files matched your search
+11
-3
@@ -113,8 +113,15 @@ int Ctx::prepare() {
|
||||
RAD_TRY(bind_routing());
|
||||
RAD_TRY(bind_profiling());
|
||||
/* Recorded passes need a recording backend, and are off whenever every issue must be seen:
|
||||
* per-op profile, oracle. */
|
||||
tape_on_ = record_passes_ && tape_supported() && !profiling_ && !oracle_;
|
||||
* per-op profile, oracle, and a calibration tap. A `gram_accum` sums across passes while its
|
||||
* plugin counts the rows it has summed in step and writes the Grams once a token target is
|
||||
* reached; a played pass reissues the accumulation without calling step, so the files would
|
||||
* hold more rows than their header says and be written late. */
|
||||
const bool taps = std::any_of(program_->ops.begin(), program_->ops.end(),
|
||||
[](const OpInfo& o) { return o.op == "gram_accum"; });
|
||||
tape_on_ = record_passes_ && tape_supported() && !profiling_ && !oracle_ && !taps;
|
||||
if (taps && record_passes_ && tape_supported() && rank_ == 0)
|
||||
RAD_INFO("recorded passes: off, the program accumulates calibration Grams (gram_accum)");
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
@@ -264,7 +271,7 @@ int Ctx::bind_weights() {
|
||||
Program& P = *program_;
|
||||
w_tmpl_.assign(P.weights.size(), RadTensor{});
|
||||
w_bits_.assign(P.weights.size(), 0);
|
||||
waited_gen_.assign(P.weights.size(), UINT32_MAX);
|
||||
for (auto& g : waited_gen_) g.assign(P.weights.size(), UINT32_MAX);
|
||||
w_direct_.assign(P.weights.size(), 0);
|
||||
|
||||
for (size_t i = 1; i < P.weights.size(); ++i) {
|
||||
@@ -457,6 +464,7 @@ int Ctx::check_domain_layout(rad_op h, int domain, std::string* why) const {
|
||||
const int st = ki->layout ? ki->layout(ip.args.p, ip.args.n_p, opd, &w.enc, w.sel, t,
|
||||
w.n_sel, &L)
|
||||
: RAD_E_UNSUPPORTED;
|
||||
if (st == RAD_LAYOUT_ANY) continue; /* reads the bytes as they lie (rad_abi.h) */
|
||||
const std::string tag = st == RAD_OK ? (L.tag && *L.tag ? L.tag : ki->name)
|
||||
: std::string();
|
||||
if (st >= 0 || st == RAD_E_UNSUPPORTED) {
|
||||
|
||||
+12
-1
@@ -207,10 +207,15 @@ public:
|
||||
/* Call once on the rank thread before its first run_step: the backend's current device is
|
||||
* thread-local, and init may have run on another thread. */
|
||||
int bind_thread();
|
||||
/* The runtime LoRA slot every row of the generator passes that follow runs with, -1 for none
|
||||
* (Ctx::issue_lora). Set by the pass loop before it releases the rank for a pass. */
|
||||
void set_gen_lora_slot(int32_t s) { gen_lora_slot_ = s; }
|
||||
|
||||
/* ---------------------------------------------------------- run-phase queries */
|
||||
const RadBatch* batch() const { return batch_; }
|
||||
int rank() const { return rank_; }
|
||||
/* Whether repeated passes are recorded and played back (prepare() says when they are not). */
|
||||
bool records_passes() const { return tape_on_; }
|
||||
int world_size() const { return world_size_; }
|
||||
RadStream stream() const { return stream_; }
|
||||
Program* program() const { return program_; }
|
||||
@@ -354,6 +359,7 @@ private:
|
||||
int bind_weights();
|
||||
int bind_ops();
|
||||
int issue_lora(rad_op h, const RadOperand* opd, int n_opd, int64_t n);
|
||||
int32_t gen_lora_slot_ = -1;
|
||||
int bind_routing();
|
||||
int bind_profiling();
|
||||
int check_domain(rad_op h, int domain) const;
|
||||
@@ -685,7 +691,12 @@ private:
|
||||
int level_ = 0;
|
||||
std::vector<RadTensor> w_tmpl_;
|
||||
std::vector<int32_t> w_bits_;
|
||||
std::vector<uint32_t> waited_gen_; /* the generation we last made the stream wait on */
|
||||
/* The generation each lane's stream was last made to wait on, a weight. Per lane: the wait
|
||||
* orders only the stream it is put on, so a weight resolved on lane 1 (the Gemma 4 26B-A4B's
|
||||
* dense arm, which a decode step issues on lane 1 and an overlapped prefill on lane 0) must
|
||||
* still be waited on when lane 0 resolves it. Indexed by lane_, like the other per-lane state
|
||||
* here: set_lane admits lanes 0 and 1 only. */
|
||||
std::vector<uint32_t> waited_gen_[2];
|
||||
|
||||
std::vector<OpPlan> plans_;
|
||||
/* Ops not yet seen issued. flush_issued walks only these and drops each once it fires. */
|
||||
|
||||
+82
-28
@@ -82,15 +82,17 @@ void* Ctx::resolve_weight(rad_weight w) {
|
||||
const WeightSlot* s = residency_->lookup(w); /* never blocks -- residency.h */
|
||||
if (s) {
|
||||
p = s->ptr;
|
||||
/* Wait once per generation, not per issue; a stream wait orders everything issued after
|
||||
* it. Sound because the mover bumps `generation` per transfer (residency.h). */
|
||||
if (s->ready && waited_gen_[w] != s->generation) {
|
||||
/* Wait once per generation and lane, not per issue; a stream wait orders everything
|
||||
* issued after it on that stream, and nothing on the other lane. Sound because the mover
|
||||
* bumps `generation` per transfer (residency.h). */
|
||||
std::vector<uint32_t>& waited = waited_gen_[lane_];
|
||||
if (s->ready && waited[w] != s->generation) {
|
||||
/* Kept out of a recording: it orders every later play too, and moving a weight a
|
||||
* pass names directly re-keys the pass. */
|
||||
if (rec_) tape_pause(true);
|
||||
rad_event_wait(cur_stream(), s->ready); /* the stream waits, not the host */
|
||||
if (rec_) tape_pause(false);
|
||||
waited_gen_[w] = s->generation;
|
||||
waited[w] = s->generation;
|
||||
++n_stream_waits_;
|
||||
}
|
||||
}
|
||||
@@ -424,17 +426,21 @@ int Ctx::issue(rad_op h, const RadOperand* opd, int n_opd, int64_t n) {
|
||||
* A row-parallel gemm's y is this rank's partial sum and the plugin reduces it after this returns,
|
||||
* so the delta rides the same reduction (B.A.x is linear in x's shards). Nothing is issued on a
|
||||
* step with no adapter row (RadBatch::lora_slot null), which is then the pass an engine without
|
||||
* adapters runs, nor on a draft pass, whose proposals the trunk verifies. An encoder or generator
|
||||
* pass never reaches a site: such a weight is outside the targets' scope rule (core/lora/lora.h),
|
||||
* so reaching one here is refused rather than run without its delta. */
|
||||
* adapters runs, nor on a draft pass, whose proposals the trunk verifies. An encoder pass never
|
||||
* reaches a site: such a weight is outside the targets' scope rule (core/lora/lora.h), so reaching
|
||||
* one here is refused rather than run without its delta.
|
||||
*
|
||||
* A generator's pass runs one adapter in all its rows (gen_lora_slot_, -1 for none), whatever the
|
||||
* rows are -- a stream's tokens, a text encoder's taps, a video's patches -- so its slot operand
|
||||
* is row `slot` of Program::lora_rows, the gemm's first n rows of it. */
|
||||
int Ctx::issue_lora(rad_op h, const RadOperand* opd, int n_opd, int64_t n) {
|
||||
if (!batch_) return RAD_OK;
|
||||
if (batch_->enc != 0 || batch_->gen != 0)
|
||||
const bool gen = batch_->gen != 0;
|
||||
if (batch_->enc != 0)
|
||||
return abort_step_msg(h, RAD_E_UNSUPPORTED,
|
||||
"a LoRA-targeted gemm was issued by an encoder or generator pass, "
|
||||
"which runtime LoRA does not reach; leave its module out of "
|
||||
"--lora-target-modules");
|
||||
if (!batch_->lora_slot || batch_->draft_pass != 0) return RAD_OK;
|
||||
"a LoRA-targeted gemm was issued by an encoder pass, which runtime "
|
||||
"LoRA does not reach; leave its module out of --lora-target-modules");
|
||||
if (gen ? gen_lora_slot_ < 0 : (!batch_->lora_slot || batch_->draft_pass != 0)) return RAD_OK;
|
||||
const OpPlan& p = plans_[h];
|
||||
const LoraIssueSite& s = program_->lora_sites[(size_t)p.lora_site];
|
||||
if (n == RAD_N_BATCH) n = batch_->n_tok;
|
||||
@@ -467,13 +473,15 @@ int Ctx::issue_lora(rad_op h, const RadOperand* opd, int n_opd, int64_t n) {
|
||||
rad_dtype_name(xd.dtype), rad_dtype_name(yd.dtype)));
|
||||
const int64_t yw = yd.rank >= 2 ? yd.shape[yd.rank - 1] : 0;
|
||||
if (yw <= 0) return abort_step_msg(h, RAD_E_STATE, "LoRA site: the gemm's output has no width");
|
||||
if (ya.cols == 0 && ya.offset % yw != 0)
|
||||
/* A token step reads each row's slot at the row the gemm wrote, so the views must name token
|
||||
* rows; a generator pass's one slot holds for any rows. */
|
||||
if (!gen && ya.cols == 0 && ya.offset % yw != 0)
|
||||
return abort_step_msg(h, RAD_E_STATE, "LoRA site: the gemm's output view starts inside a "
|
||||
"row, so its token rows cannot be told");
|
||||
const int64_t r0 = ya.offset / yw;
|
||||
const int64_t r0 = gen ? 0 : ya.offset / yw;
|
||||
/* The activation must start at the same row: shrink reads x's row m and expand writes y's row
|
||||
* m with one slot. */
|
||||
{
|
||||
if (!gen) {
|
||||
const int64_t xw = xd.rank >= 2 ? xd.shape[xd.rank - 1] : 0;
|
||||
if (xw <= 0 || (xa.cols == 0 && xa.offset % xw != 0) || xa.offset / xw != r0)
|
||||
return abort_step_msg(h, RAD_E_STATE, "LoRA site: the gemm's activation and output "
|
||||
@@ -483,26 +491,72 @@ int Ctx::issue_lora(rad_op h, const RadOperand* opd, int n_opd, int64_t n) {
|
||||
(int)op_domain_[s.expand].load(std::memory_order_relaxed) != dom)
|
||||
return abort_step_msg(h, RAD_E_UNSUPPORTED, "LoRA site: the adapter ops were placed in "
|
||||
"another domain than the gemm they follow");
|
||||
if (r0 < 0 || r0 + n > batch_->n_tok)
|
||||
if (!gen && (r0 < 0 || r0 + n > batch_->n_tok))
|
||||
return abort_step_msg(h, RAD_E_STATE,
|
||||
fmt("LoRA site: the gemm wrote rows [%lld, %lld) of a %lld-token step",
|
||||
(long long)r0, (long long)(r0 + n), (long long)batch_->n_tok));
|
||||
if (gen && (!program_->lora_rows || gen_lora_slot_ >= program_->lora_pools[(size_t)s.pool].S))
|
||||
return abort_step_msg(h, RAD_E_STATE,
|
||||
fmt("LoRA site: a generator pass in slot %d of %lld", gen_lora_slot_,
|
||||
(long long)program_->lora_pools[(size_t)s.pool].S));
|
||||
/* Each row's adapter is the step batch's (RadBatch::lora_slot), rewritten in stream order
|
||||
* before each step's kernels, so the pointer this launch records is the one a replay of it
|
||||
* reads. */
|
||||
* reads. A generator pass is never recorded, so its run of the slot table is named directly,
|
||||
* and it runs the pair over chunks of at most lora_rows_n rows: a video's passes run to
|
||||
* hundreds of thousands of rows, and `t` and the slot run are sized for one chunk. */
|
||||
const LoraPool& pool = program_->lora_pools[(size_t)s.pool];
|
||||
const rad_buf t = program_->lora_t[(size_t)s.t].lane[lane_ ? 1 : 0];
|
||||
RadOperand slot = RAD_P_T(batch_->lora_slot + r0, RAD_I32);
|
||||
slot.rows = n;
|
||||
const RadOperand tv = RAD_B_N(t, 0, n);
|
||||
const RadOperand sh[5] = { xa,
|
||||
s.opd_a_scale >= 0 && s.opd_a_scale < n_opd ? opd[s.opd_a_scale]
|
||||
: RAD_NONE,
|
||||
slot, RAD_B(pool.a), tv };
|
||||
int rc = issue_body((rad_op)s.shrink, sh, 5, n);
|
||||
if (rc < 0) return rc;
|
||||
const RadOperand ex[5] = { tv, slot, RAD_B(pool.b), RAD_B(pool.scale), ya };
|
||||
return issue_body((rad_op)s.expand, ex, 5, n);
|
||||
/* A generator's slot holds its ranks from 0 (Program::lora_rused): no pair where it holds
|
||||
* none, the narrowest declared pair that holds them otherwise. */
|
||||
uint32_t sh_op = s.shrink, ex_op = s.expand;
|
||||
int64_t rv = s.R;
|
||||
if (gen) {
|
||||
const size_t ui = (size_t)s.pool * (size_t)pool.S + (size_t)gen_lora_slot_;
|
||||
const int64_t used = ui < program_->lora_rused.size() ? program_->lora_rused[ui] : s.R;
|
||||
if (used <= 0) return RAD_OK;
|
||||
for (int v = 0; v < s.n_v; ++v)
|
||||
if (s.R_v[v] >= used) { sh_op = s.shrink_v[v]; ex_op = s.expand_v[v]; rv = s.R_v[v]; }
|
||||
}
|
||||
RadOperand xs = s.opd_a_scale >= 0 && s.opd_a_scale < n_opd ? opd[s.opd_a_scale]
|
||||
: RAD_NONE;
|
||||
const int64_t step = gen ? program_->lora_rows_n : n;
|
||||
if (step <= 0) return abort_step_msg(h, RAD_E_STATE, "LoRA site: no slot rows");
|
||||
int64_t sw = 0; /* a_scale's row stride, for a chunk past the first */
|
||||
if (n > step && xs.kind != RAD_OPK_NONE) {
|
||||
if (xs.kind != RAD_OPK_BUF || (size_t)xs.handle >= program_->buffers.size())
|
||||
return abort_step_msg(h, RAD_E_UNSUPPORTED, "LoRA site: a generator pass's activation "
|
||||
"scale is not a declared buffer, so its "
|
||||
"rows cannot be chunked");
|
||||
const RadBufDecl& sd = program_->buffers[xs.handle].decl;
|
||||
sw = sd.rank >= 2 ? sd.shape[sd.rank - 1] : 1;
|
||||
}
|
||||
const int64_t xw = xd.rank >= 2 ? xd.shape[xd.rank - 1] : 0;
|
||||
for (int64_t c = 0; c < n; c += step) {
|
||||
const int64_t nc = std::min(step, n - c);
|
||||
RadOperand x = xa, y = ya, a_s = xs;
|
||||
if (c > 0) {
|
||||
x.offset += c * xw;
|
||||
y.offset += c * yw;
|
||||
if (a_s.kind == RAD_OPK_BUF) a_s.offset += c * sw;
|
||||
}
|
||||
if (n > step) {
|
||||
x.rows = nc;
|
||||
y.rows = nc;
|
||||
if (a_s.kind == RAD_OPK_BUF) a_s.rows = nc;
|
||||
}
|
||||
RadOperand slot = gen ? RAD_B(program_->lora_rows) : RAD_P_T(batch_->lora_slot + r0, RAD_I32);
|
||||
if (gen) slot.offset = (int64_t)gen_lora_slot_ * program_->lora_rows_n;
|
||||
slot.rows = nc;
|
||||
RadOperand tv = RAD_B_N(t, 0, nc);
|
||||
if (rv < s.R) tv.cols = rv;
|
||||
const RadOperand sh[5] = { x, a_s, slot, RAD_B(pool.a), tv };
|
||||
int rc = issue_body((rad_op)sh_op, sh, 5, nc);
|
||||
if (rc < 0) return rc;
|
||||
const RadOperand ex[5] = { tv, slot, RAD_B(pool.b), RAD_B(pool.scale), y };
|
||||
rc = issue_body((rad_op)ex_op, ex, 5, nc);
|
||||
if (rc < 0) return rc;
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
int Ctx::issue_body(rad_op h, const RadOperand* opd, int n_opd, int64_t n) {
|
||||
|
||||
+161
-31
@@ -28,7 +28,6 @@ int TierExec::configure(Registry* reg, KVManager* kv, IdleTiers* tiers,
|
||||
shared_ = shared;
|
||||
RAD_TRY(rad_dev_set(device_));
|
||||
|
||||
int64_t off = 0;
|
||||
for (int32_t g : groups) {
|
||||
const KVGroupPlan* p = kv->plan(g);
|
||||
if (!p || !p->paged()) continue;
|
||||
@@ -39,7 +38,7 @@ int TierExec::configure(Registry* reg, KVManager* kv, IdleTiers* tiers,
|
||||
gi.layer_stride = p->layer_stride();
|
||||
gi.n_blocks = p->n_blocks;
|
||||
gi.base = p->base;
|
||||
gi.off = off;
|
||||
gi.replicated = p->replicated;
|
||||
if (p->zero) {
|
||||
gi.zero = true;
|
||||
gi_.push_back(gi);
|
||||
@@ -60,10 +59,18 @@ int TierExec::configure(Registry* reg, KVManager* kv, IdleTiers* tiers,
|
||||
close();
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
off += gi.n_layers * gi.frag;
|
||||
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) {
|
||||
@@ -75,18 +82,76 @@ int TierExec::configure(Registry* reg, KVManager* kv, IdleTiers* tiers,
|
||||
entry_bytes_ = off;
|
||||
|
||||
/* Nothing is built on the device here; see device_init(). */
|
||||
verify_ = leader && std::getenv("RADIANCE_DEBUG_TIER_VERIFY") != nullptr;
|
||||
verify_ = std::getenv("RADIANCE_DEBUG_TIER_VERIFY") != nullptr;
|
||||
verify_rep_ = verify_ && !leader_ && rep_bytes_ > 0;
|
||||
|
||||
char buf[320];
|
||||
char buf[400];
|
||||
std::snprintf(buf, sizeof buf,
|
||||
"kv tier transfer: %zu paged group(s), %s an entry, batches of %d through "
|
||||
"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(), max_batch_);
|
||||
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;
|
||||
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;
|
||||
@@ -118,6 +183,10 @@ void TierExec::close() {
|
||||
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;
|
||||
@@ -128,6 +197,7 @@ 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;
|
||||
@@ -156,7 +226,11 @@ int TierExec::device_init() {
|
||||
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;
|
||||
if (!stage_ || !idx_dev_ || !idx_host_ || !ev) { release_device(); return RAD_E_NOMEM; }
|
||||
if (verify_rep_) vrep_ = (char*)rad_dev_alloc(copy_idx_ * rep_bytes_, RAD_MEM_HOST_PINNED);
|
||||
if (!stage_ || !idx_dev_ || !idx_host_ || !ev || (verify_rep_ && !vrep_)) {
|
||||
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. */
|
||||
@@ -212,29 +286,62 @@ int TierExec::run_rows(const Resolved& r, bool scatter, const GroupInfo& gi, int
|
||||
|
||||
/* ------------------------------------------------------------------ RADIANCE_DEBUG_TIER_VERIFY */
|
||||
|
||||
static uint64_t tier_fnv(const void* p, int64_t n) {
|
||||
uint64_t h = 1469598103934665603ull;
|
||||
/* 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;
|
||||
}
|
||||
|
||||
void TierExec::verify_note(const BlockHash& key, const void* p, int64_t n) {
|
||||
if (!verify_ || !leader_ || !p || n <= 0) return;
|
||||
out_sum_[key] = tier_fnv(p, n);
|
||||
static uint64_t tier_fnv(const void* p, int64_t n) {
|
||||
return tier_fnv_from(1469598103934665603ull, p, n);
|
||||
}
|
||||
|
||||
void TierExec::verify_check(const BlockHash& key, const void* p, int64_t n) {
|
||||
if (!verify_ || !leader_ || !p || n <= 0) return;
|
||||
/* 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 = tier_fnv(p, n);
|
||||
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
|
||||
@@ -276,11 +383,28 @@ int TierExec::copy_runs(const std::vector<TierJob*>& order, size_t first, int64_
|
||||
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;
|
||||
host += slot_off_;
|
||||
char* dev = (char*)stage_ + k * entry_bytes_;
|
||||
const int st = out
|
||||
? rad_memcpy_2d_async(host, slot_bytes_, dev, entry_bytes_, entry_bytes_, run, stream_)
|
||||
: rad_memcpy_2d_async(dev, entry_bytes_, host, slot_bytes_, entry_bytes_, run, stream_);
|
||||
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;
|
||||
}
|
||||
@@ -326,22 +450,27 @@ int TierExec::enqueue_copy(std::vector<TierJob>& jobs, RadStream compute) {
|
||||
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 && !gi_[gidx].zero; ++l)
|
||||
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 * entry_bytes_;
|
||||
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_)
|
||||
for (const TierJob& j : jobs)
|
||||
verify_pending_.push_back({j.key, j.blocks.empty() ? -1 : j.host_slot});
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -353,9 +482,11 @@ int TierExec::copy_state() {
|
||||
copy_out_ = false;
|
||||
if (q > 0 && verify_) {
|
||||
for (const VerifyNote& v : verify_pending_) {
|
||||
if (v.host_slot >= 0)
|
||||
verify_note(v.key, (char*)tiers_->arena().at(v.host_slot) + slot_off_,
|
||||
entry_bytes_);
|
||||
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();
|
||||
@@ -432,8 +563,7 @@ int TierExec::issue_restore(std::vector<TierJob>& jobs, RadStream compute,
|
||||
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, (char*)tiers_->arena().at(jobs[i].host_slot) + slot_off_,
|
||||
entry_bytes_);
|
||||
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
|
||||
|
||||
+73
-15
@@ -20,9 +20,15 @@
|
||||
* per staging batch.
|
||||
*
|
||||
* Staging layout. Device staging is `max_batch * entry_bytes`; one entry is [group][layer][fragment]
|
||||
* end to end. For a given (group, layer) the destination rows sit at stride `entry_bytes` from a
|
||||
* base in the first entry, which is `gather_rows`'s `y` pitch, so one launch writes every entry's
|
||||
* fragment in place and the host side is one strided copy per run of consecutive arena slots.
|
||||
* end to end, the replicated groups first. For a given (group, layer) the destination rows sit at
|
||||
* stride `entry_bytes` from a base in the first entry, which is `gather_rows`'s `y` pitch, so one
|
||||
* launch writes every entry's fragment in place and the host side is one strided copy per window
|
||||
* per run of consecutive arena slots.
|
||||
*
|
||||
* The host slot. Under tensor parallelism a slot holds the whole engine's entry. A group every
|
||||
* rank holds whole (rad_kv_group_replicated) is in the slot once, in one window at its front
|
||||
* that rank 0 writes and every rank reads back; the groups each rank holds a shard of follow,
|
||||
* one window per rank. slot_layout() is the one place the arithmetic lives.
|
||||
*/
|
||||
#pragma once
|
||||
#include "mem/kv.h"
|
||||
@@ -48,6 +54,23 @@ public:
|
||||
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);
|
||||
|
||||
/* One shared host slot across the ranks. `[0, rep_bytes)` is the replicated window, the
|
||||
* replicated cached groups in cached-group order; rank r's own groups follow at
|
||||
* `own_off[r]`, `own_bytes[r]` wide (uneven heads and rad_kv_group_zero make these differ).
|
||||
* `entry_bytes` is the largest rank's staging entry, rep_bytes + own_bytes, which sizes()
|
||||
* takes. Checked here: a group replicated on one rank is replicated, not zero and the same
|
||||
* width on every rank, since every rank reads the one window back; a violation stops bringup
|
||||
* with the group named (RAD_E_STATE). */
|
||||
struct SlotLayout {
|
||||
int64_t slot_bytes = 0;
|
||||
int64_t rep_bytes = 0;
|
||||
int64_t entry_bytes = 0;
|
||||
std::vector<int64_t> own_off, own_bytes;
|
||||
std::vector<std::string> replicated; /* the replicated groups' names, for the report */
|
||||
};
|
||||
static int slot_layout(const std::vector<const KVManager*>& ranks,
|
||||
const std::vector<int32_t>& groups, SlotLayout* out);
|
||||
void close();
|
||||
bool enabled() const { return enabled_; }
|
||||
std::string report() const { return report_; }
|
||||
@@ -96,7 +119,25 @@ public:
|
||||
* off-device or already has work outstanding. */
|
||||
bool plan_promote(const BlockHash& key, TierJob* out); /* leader only */
|
||||
|
||||
struct Stats { int64_t passes = 0, jobs = 0, bytes_out = 0, bytes_in = 0; };
|
||||
/* ---------------- RADIANCE_DEBUG_TIER_VERIFY, the replicated window ----------------
|
||||
*
|
||||
* A replicated group's bytes are the plugin's claim. Under the variable a follower gathers the
|
||||
* replicated groups it would otherwise leave to the leader into a pinned scratch beside the
|
||||
* copy and hashes them once landed; the engine, once every rank has landed, takes each sum
|
||||
* and has the leader compare it with the window it wrote. Off, a follower moves nothing for
|
||||
* the replicated groups. */
|
||||
bool verifying_replicated() const { return verify_ && rep_bytes_ > 0; }
|
||||
/* Follower: the sum of the replicated bytes it gathered for `key` in the last copy out, taken
|
||||
* once. False when there is none (not verifying, no replicated group, or not in that copy). */
|
||||
bool take_replicated_sum(const BlockHash& key, uint64_t* sum);
|
||||
/* Leader: compares `follower_sum` from rank `rank` with the window the slot holds for job
|
||||
* `j`; a mismatch is logged and counted (Stats::rep_mismatch). */
|
||||
bool verify_replicated(const TierJob& j, int rank, uint64_t follower_sum);
|
||||
|
||||
struct Stats {
|
||||
int64_t passes = 0, jobs = 0, bytes_out = 0, bytes_in = 0;
|
||||
int64_t rep_checked = 0, rep_mismatch = 0; /* verify_replicated's tallies */
|
||||
};
|
||||
const Stats& stats() const { return st_; }
|
||||
|
||||
private:
|
||||
@@ -111,6 +152,9 @@ private:
|
||||
/* rad_kv_group_zero: no shard on this rank. Kept in place because a job's block ids are
|
||||
* one per cached group in the same order on every rank; it moves no bytes. */
|
||||
bool zero = false;
|
||||
/* rad_kv_group_replicated: staged at the front of the entry and in the slot's replicated
|
||||
* window; a follower gathers it only to verify, and scatters it on every restore. */
|
||||
bool replicated = false;
|
||||
Resolved gather, scatter;
|
||||
};
|
||||
|
||||
@@ -127,9 +171,17 @@ private:
|
||||
void warn_once(const char* what, int st);
|
||||
int 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);
|
||||
/* One strided copy per run of consecutive arena slots, `n` entries starting at job `first` of
|
||||
* `order`. To host when `out`, from host otherwise. */
|
||||
/* One strided copy per window per run of consecutive arena slots, `n` entries starting at job
|
||||
* `first` of `order`. To host when `out` (the replicated window from the leader only), from
|
||||
* host otherwise. */
|
||||
int copy_runs(const std::vector<TierJob*>& order, size_t first, int64_t n, bool out);
|
||||
/* Whether this rank gathers group `gi` for a copy out: its own groups always, a replicated
|
||||
* one as the leader or to verify. */
|
||||
bool gathers(const GroupInfo& gi) const {
|
||||
return !gi.zero && (!gi.replicated || leader_ || verify_rep_);
|
||||
}
|
||||
/* The hash of one entry as the slot holds it: the replicated window, then this rank's own. */
|
||||
uint64_t entry_sum(const char* slot) const;
|
||||
int enqueue_copy(std::vector<TierJob>& jobs, RadStream compute);
|
||||
|
||||
bool enabled_ = false;
|
||||
@@ -141,11 +193,13 @@ private:
|
||||
/* This rank's card. Allocations and launches use the current device, and the engine loop leaves
|
||||
* the last rank's device current, so it is set explicitly. */
|
||||
int device_ = -1;
|
||||
/* This rank's offset inside a shared host slot. A slot holds the whole engine's entry, which
|
||||
* under tensor parallelism is n_ranks distinct KV shards, so the slot is n_ranks entries wide
|
||||
* and each rank touches only its own window. */
|
||||
/* This rank's windows of a shared host slot (SlotLayout): its own groups at `slot_off_`,
|
||||
* `own_bytes_` wide, and the replicated groups at the slot's front, `rep_bytes_` wide, which
|
||||
* only the leader writes. The staging entry is [replicated][own], entry_bytes_ in all. */
|
||||
int64_t slot_off_ = 0;
|
||||
int64_t slot_bytes_ = 0; /* the whole shared slot: n_ranks * entry_bytes_ */
|
||||
int64_t slot_bytes_ = 0; /* the whole shared slot */
|
||||
int64_t rep_bytes_ = 0;
|
||||
int64_t own_bytes_ = 0;
|
||||
Registry* reg_ = nullptr;
|
||||
KVManager* kv_ = nullptr;
|
||||
IdleTiers* tiers_ = nullptr;
|
||||
@@ -184,14 +238,18 @@ private:
|
||||
*
|
||||
* Round-trip check: hashes each entry going out and coming back and names the entry when they
|
||||
* differ, separating "the tier moved wrong bytes" from a downstream fault. A diagnostic, not a
|
||||
* guard: host hashing costs on every entry. Off unless set; leader-only because the map is keyed
|
||||
* by an object followers do not own. A copy out is hashed once landed (first seen by copy_state). */
|
||||
* guard: host hashing costs on every entry. Off unless set. The round trip is the leader's
|
||||
* (the map is keyed by an object followers do not own); a follower's part is the replicated
|
||||
* window, above. A copy out is hashed once landed (first seen by copy_state). */
|
||||
bool verify_ = false;
|
||||
bool verify_rep_ = false; /* a follower gathering the replicated groups to verify */
|
||||
std::unordered_map<BlockHash, uint64_t, BlockHashHash> out_sum_;
|
||||
struct VerifyNote { BlockHash key; int32_t host_slot = -1; };
|
||||
std::unordered_map<BlockHash, uint64_t, BlockHashHash> rep_sum_; /* follower */
|
||||
struct VerifyNote { BlockHash key; int32_t host_slot = -1; int64_t ord = -1; };
|
||||
std::vector<VerifyNote> verify_pending_;
|
||||
void verify_note(const BlockHash& key, const void* p, int64_t n);
|
||||
void verify_check(const BlockHash& key, const void* p, int64_t n);
|
||||
char* vrep_ = nullptr; /* pinned, max_copy_ * rep_bytes_: a follower's gather */
|
||||
void verify_note(const BlockHash& key, const char* slot);
|
||||
void verify_check(const BlockHash& key, const char* slot);
|
||||
|
||||
std::string report_;
|
||||
Stats st_;
|
||||
|
||||
Reference in new issue
Block a user