Archived
feat(sampler): port linear MTP coupling and cooperative verify upstream
Port GitHub PRs #12 and #13 onto Codeberg upstream main, retaining upstream kernel-chain handling. Include the source Clang target-attribute fixes required by the host build; omit the KFD header workaround and unrelated fork work. Co-Authored-By: Claude Code <noreply@anthropic.com> Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
1 parent
123c439684
commit
d912c694f3
37 files changed
+3730
-23
No files matched your search
+4
-1
@@ -530,7 +530,7 @@ int Engine::step_loop() {
|
||||
}
|
||||
if (t_on) phase_mark(PH_BEGIN);
|
||||
for (auto& rk : ranks_) {
|
||||
const int s = rk->sampler.build_step(so.reqs);
|
||||
const int s = rk->sampler.build_step(so.reqs, b->step);
|
||||
if (s < 0) { RAD_ERR("sampler build_step: %s", rad_strerror(s)); return s; }
|
||||
/* A request the sampler could not serve is cancelled; the step goes on. */
|
||||
for (uint64_t id : rk->sampler.take_failed()) cancel.push_back(id);
|
||||
@@ -717,6 +717,9 @@ int Engine::step_loop() {
|
||||
st = embedding_ ? collect_pooled(b, so) : collect_step(b, so);
|
||||
if (t_on) phase_mark(PH_COLLECT);
|
||||
if (st < 0) return fail_loop(st);
|
||||
/* The step's sampler chain, verify op included, has returned: one line at the first
|
||||
* coupled verify that actually completed. */
|
||||
couple_verify_evidence(b->step);
|
||||
if (cfg_.kld()) {
|
||||
st = score_rows(b, so);
|
||||
if (st < 0) return fail_loop(st);
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
*/
|
||||
#pragma once
|
||||
#include "rad_core.h"
|
||||
#include "rad_spechub.h" /* RadMtpCoupleDraw/State: the coupled MTP wire rows */
|
||||
#include "cvec.h"
|
||||
#include <map>
|
||||
#include <set>
|
||||
@@ -396,6 +397,25 @@ private:
|
||||
void step_state_fini();
|
||||
StepSlot* slots_host_ = nullptr;
|
||||
|
||||
/* RADIANCE_MTP_COUPLED (engine_draft.cpp). The head declares `mtp_couple_draws` and
|
||||
* `mtp_couple_state`; the engine stages one draw per drafting row before the serial passes and
|
||||
* reads the states back once, then binds each to the verify row it will be coupled at. */
|
||||
bool mtp_couple_ = false;
|
||||
rad_buf couple_draws_buf_ = 0, couple_state_buf_ = 0;
|
||||
int64_t couple_rows_ = 0; /* the state plane's rows per round */
|
||||
RadMtpCoupleDraw* couple_draw_stage_ = nullptr; /* pinned [max_seqs] */
|
||||
RadMtpCoupleState* couple_readback_ = nullptr; /* pinned [rounds, couple_rows_] */
|
||||
std::vector<RadMtpCoupleDraw> couple_by_card_; /* the plan's draw per card slot */
|
||||
RadMtpCoupleState couple_bound_[RAD_MTP_COUPLE_MAX_ROUNDS] = {};
|
||||
bool couple_evidence_ = false; /* the completed-verify line was written (once a process) */
|
||||
bool couple_host_note_ = false; /* the host-domain counterpart was written (once) */
|
||||
int stage_couple_draws(int n);
|
||||
/* One INFO line at the first coupled verify that completed: the step, the live requests whose
|
||||
* rows carried a bound state, the kernel and its domain. Read off the sampler's build snapshot
|
||||
* after collect_step's wait, so it reports an issued, finished verify -- not an enqueue, not a
|
||||
* LIVE flag, and not a host row read as a device one. */
|
||||
void couple_verify_evidence(int step);
|
||||
|
||||
/* Step-time attribution, Debug only (see the barrier in step_loop): time per scheduler phase.
|
||||
* The nine phases sum to the whole step, including the readback, commit and drafter after the
|
||||
* join. */
|
||||
|
||||
+166
-2
@@ -6,6 +6,10 @@
|
||||
*/
|
||||
#include "engine_priv.h"
|
||||
|
||||
#include "../arch/common/rad_spechub_config.h"
|
||||
#include "sample/mtp_couple_evidence.h"
|
||||
#include "sched/mtp_couple_bind.h"
|
||||
#include "sched/mtp_couple_plan.h"
|
||||
#include "sched/scheduler.h"
|
||||
#include "device/device.h"
|
||||
|
||||
@@ -13,6 +17,7 @@
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
@@ -48,6 +53,64 @@ int Engine::step_state_init() {
|
||||
if (!ranks_.empty()) RAD_TRY(rad_dev_set(ranks_[0]->device));
|
||||
slots_host_ = static_cast<StepSlot*>(rad_dev_alloc(slot_bytes, RAD_MEM_HOST_PINNED));
|
||||
if (!slots_host_) return RAD_E_NOMEM;
|
||||
|
||||
/* RADIANCE_MTP_COUPLED: the head's two planes, found by name in the program it declared, and
|
||||
* the pinned staging the step path needs beside them. A program without them has no coupled
|
||||
* path, so the flag's absence and its contradiction are told apart here: the flag is on and the
|
||||
* head declared nothing means the architecture refused it (it returns RAD_E_UNSUPPORTED at its
|
||||
* own declare), and a silent no-op would serve uncoupled drafts while the flag said otherwise. */
|
||||
mtp_couple_ = mtp_coupled_enabled();
|
||||
if (mtp_couple_) {
|
||||
/* THE DRAFTER SHAPE IS CHECKED HERE, where the declaration is in hand, not against
|
||||
* environment variables nobody else reads. Coupling is defined on a LINEAR chain: a block
|
||||
* drafter proposes a whole block at once from one pass and writes it to its proposal
|
||||
* buffer, so there is no per-round draw to couple and no per-round state to read back.
|
||||
* Left unchecked, a block drafter would silently serve uncoupled drafts while the flag
|
||||
* said otherwise. */
|
||||
if (drafter_.kind != RAD_DRAFT_SERIAL || drafter_.depth <= 0) {
|
||||
RAD_ERR("RADIANCE_MTP_COUPLED requires the linear serial draft chain (RAD_DRAFT_SERIAL "
|
||||
"with depth > 0); this model declares drafter '%s' kind %d depth %d, which has "
|
||||
"no per-round draw to couple",
|
||||
drafter_.name ? drafter_.name : "(none)", drafter_.kind, drafter_.depth);
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
const Program& prog = ranks_[0]->program;
|
||||
for (size_t i = 0; i < prog.buffers.size(); ++i) {
|
||||
const std::string& name = prog.buffers[i].name;
|
||||
auto ends = [&](const char* suffix) {
|
||||
const size_t n = std::strlen(suffix);
|
||||
return name.size() >= n && name.compare(name.size() - n, n, suffix) == 0;
|
||||
};
|
||||
if (ends("mtp_couple_draws")) couple_draws_buf_ = (rad_buf)i;
|
||||
if (ends("mtp_couple_state")) couple_state_buf_ = (rad_buf)i;
|
||||
}
|
||||
if (!couple_draws_buf_ || !couple_state_buf_) {
|
||||
RAD_ERR("RADIANCE_MTP_COUPLED is set but the architecture declared no coupled MTP "
|
||||
"planes; the flag and the model disagree");
|
||||
return RAD_E_STATE;
|
||||
}
|
||||
const RadBufDecl& sd = prog.buffers[couple_state_buf_].decl;
|
||||
if (sd.rank != 3 || sd.shape[2] != (int64_t)RAD_MTP_COUPLE_STATE_WORDS) {
|
||||
RAD_ERR("RADIANCE_MTP_COUPLED: 'mtp_couple_state' is not the [rounds, rows, %d]-word "
|
||||
"plane the coupling reads", (int)RAD_MTP_COUPLE_STATE_WORDS);
|
||||
return RAD_E_STATE;
|
||||
}
|
||||
couple_rows_ = sd.shape[1];
|
||||
/* ONE ENTRY PER CARD SLOT, indexed by StepEntry::card exactly as slots_host_ is: the plan
|
||||
* names a request's sequence on the slot the advance writes, and the draft row carries the
|
||||
* same `card`. A vector left empty makes every lookup miss, so the first step that drafts
|
||||
* would refuse with "has no plan entry on its card" instead of staging its draw. */
|
||||
couple_by_card_.assign((size_t)n_slot, RadMtpCoupleDraw{});
|
||||
couple_draw_stage_ = static_cast<RadMtpCoupleDraw*>(
|
||||
rad_dev_alloc(n_slot * (int64_t)sizeof(RadMtpCoupleDraw), RAD_MEM_HOST_PINNED));
|
||||
couple_readback_ = static_cast<RadMtpCoupleState*>(rad_dev_alloc(
|
||||
(int64_t)drafter_.depth * couple_rows_ * (int64_t)sizeof(RadMtpCoupleState),
|
||||
RAD_MEM_HOST_PINNED));
|
||||
if (!couple_draw_stage_ || !couple_readback_) return RAD_E_NOMEM;
|
||||
RAD_INFO("MTP coupled LIVE: %d linear round(s), x~Q from the final reranked top8, maximal "
|
||||
"coupling at the verify; fixed-seed tokens are NOT preserved", drafter_.depth);
|
||||
}
|
||||
|
||||
RAD_TRY(rad_event_create_as(&step_end_, RAD_EVENT_LOCAL | RAD_EVENT_HOST_WAIT));
|
||||
return RAD_OK;
|
||||
}
|
||||
@@ -62,6 +125,10 @@ void Engine::step_state_fini() {
|
||||
}
|
||||
if (slots_host_) rad_dev_free(slots_host_, RAD_MEM_HOST_PINNED);
|
||||
slots_host_ = nullptr;
|
||||
if (couple_draw_stage_) rad_dev_free(couple_draw_stage_, RAD_MEM_HOST_PINNED);
|
||||
if (couple_readback_) rad_dev_free(couple_readback_, RAD_MEM_HOST_PINNED);
|
||||
couple_draw_stage_ = nullptr;
|
||||
couple_readback_ = nullptr;
|
||||
if (step_end_) rad_event_destroy(step_end_);
|
||||
step_end_ = nullptr;
|
||||
}
|
||||
@@ -142,6 +209,8 @@ int Engine::issue_serial_drafter(int step, StepOut& so) {
|
||||
if (rounds <= 0) return RAD_OK;
|
||||
serial_rounds_ = rounds;
|
||||
serial_rows_ = n;
|
||||
/* Before ANY serial pass reads a draw: the round-1 pass is the history pass below. */
|
||||
if (mtp_couple_ && n > 0) RAD_TRY(stage_couple_draws(n));
|
||||
|
||||
/* RADIANCE_DEBUG_STAGE: running means of a draft pass's host time: batch + advance, greedy
|
||||
* sampler chain, barrier release and issue. They cost the step nothing until their sum exceeds
|
||||
@@ -213,6 +282,16 @@ int Engine::issue_serial_drafter(int step, StepOut& so) {
|
||||
* history (Scheduler::serial_batch). */
|
||||
RAD_TRY(advance_all(ADV_TAKE, serial_done_, true, n));
|
||||
Rank& r0 = *ranks_[0];
|
||||
if (mtp_couple_) {
|
||||
/* Rounds 1..done of the drafting rows, in the SAME stream order as the slots below: the
|
||||
* states come back behind the proposal that they were drawn beside. */
|
||||
const void* state = r0.ctx.buf_ptr(couple_state_buf_);
|
||||
if (!state || serial_done_ > (int)drafter_.depth) return RAD_E_STATE;
|
||||
const int64_t pitch = couple_rows_ * (int64_t)sizeof(RadMtpCoupleState);
|
||||
RAD_TRY(rad_memcpy_2d_async(couple_readback_, pitch, state, pitch,
|
||||
(int64_t)n * (int64_t)sizeof(RadMtpCoupleState), serial_done_,
|
||||
r0.ctx.stream()));
|
||||
}
|
||||
RAD_TRY(rad_memcpy_async(slots_host_, r0.step_slots,
|
||||
(int64_t)cfg_.max_seqs * (int64_t)sizeof(StepSlot), r0.ctx.stream()));
|
||||
return RAD_OK;
|
||||
@@ -253,8 +332,93 @@ int Engine::finish_serial_drafter(const StepOut& so) {
|
||||
/* No draft for a request the sampler will not speculate on (a constraining grammar that
|
||||
* cannot snapshot its state); the sampler would refuse the next step otherwise. */
|
||||
if (!r0.sampler.allows_speculation(serial_row_[(size_t)i].seq)) continue;
|
||||
sched_.set_draft(serial_row_[(size_t)i].seq,
|
||||
&draft_out_[(size_t)i * (size_t)rounds], done);
|
||||
const uint64_t seq = serial_row_[(size_t)i].seq;
|
||||
const int rc = sched_.set_draft(seq, &draft_out_[(size_t)i * (size_t)rounds], done);
|
||||
if (!mtp_couple_) continue;
|
||||
/* BOUND ONLY FOR A REQUEST STILL RUNNING. A truncated publication keeps a prefix, whose
|
||||
* rows carry their own rounds' states; a request this commit finished (a max_tokens or
|
||||
* context cut), or one the scheduler refused, binds nothing -- its rows are carried
|
||||
* terminal work and will never sample again. set_mtp_couple(…, 0) clears what an earlier
|
||||
* step left, and for a request already gone from the sampler it is RAD_E_NOTFOUND.
|
||||
*
|
||||
* The position coordinate is the request's committed output length AFTER this commit -- the
|
||||
* value the next build_step reads as its n_out. It comes from the scheduler's commit
|
||||
* snapshot, not from the plan's Request pointer: reap() may have destroyed that already.
|
||||
* The rule is mtp_couple_bind_rounds (sched/mtp_couple_bind.h), where it is driven against
|
||||
* a real commit without an Engine. */
|
||||
MtpCoupleBindPoint bp;
|
||||
bp.found = sched_.committed_seq(seq, &bp.n_out, &bp.live);
|
||||
if (!bp.found)
|
||||
RAD_WARN("MTP coupled: serial row for sequence %llu is not in the committed step; "
|
||||
"clearing its coupling", (unsigned long long)seq);
|
||||
int n_bound = 0;
|
||||
std::string why;
|
||||
const int bs = mtp_couple_bind_rounds(
|
||||
bp, couple_draw_stage_[(size_t)i], &couple_readback_[(size_t)i], (int)couple_rows_,
|
||||
done, slots_host_[serial_row_[(size_t)i].card], meta_.n_vocab, couple_bound_,
|
||||
&n_bound, &why);
|
||||
if (bs < 0) { RAD_ERR("%s", why.c_str()); return bs; }
|
||||
for (auto& rk : ranks_) {
|
||||
const int st = rk->sampler.set_mtp_couple(seq, couple_bound_,
|
||||
rc == RAD_OK ? n_bound : 0);
|
||||
if (st < 0 && st != RAD_E_NOTFOUND) return st;
|
||||
}
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* One line, once, at the first coupled verify that completed. What makes it evidence is where it
|
||||
* reads: the snapshot build_step froze for the step whose wait just returned (sample/
|
||||
* mtp_couple_evidence.h), the verify op run() actually issued for that build, and the op's domain.
|
||||
* build_greedy -- the drafter's greedy passes, issued between that build and the wait -- clears
|
||||
* any_couple_ but not the snapshot; gating on the former is why the line never appeared in the
|
||||
* coupled path. Sequences are re-checked as Running after the wait under the scheduler's lock, so a
|
||||
* request that left in between is not counted. Request numbers, the row count and the kernel name
|
||||
* only: no tokens, no keys, no logits. A host-domain row prints the same line marked as not device
|
||||
* evidence -- an oracle run must not be read as one. */
|
||||
void Engine::couple_verify_evidence(int step) {
|
||||
if (!mtp_couple_ || couple_evidence_) return;
|
||||
Sampler& smp = ranks_[0]->sampler;
|
||||
const rad_op op = smp.staged_couple_op();
|
||||
const MtpCoupleEvidence ev = mtp_couple_evidence(
|
||||
smp.couple_build(), step, ranks_[0]->ctx.op_domain(op),
|
||||
[this](uint64_t seq) { return sched_.running(seq); });
|
||||
if (ev.rows <= 0 || smp.staged_couple_issued_step() != step) return;
|
||||
const char* kernel = ranks_[0]->ctx.ip_kernel_name(op);
|
||||
if (ev.device) {
|
||||
couple_evidence_ = true;
|
||||
RAD_INFO("MTP coupled verify completed: step %d, %lld bound row(s) over request(s) [%s], "
|
||||
"kernel %s, domain device", step, (long long)ev.rows, ev.seqs.c_str(), kernel);
|
||||
} else if (!couple_host_note_) {
|
||||
couple_host_note_ = true;
|
||||
RAD_INFO("MTP coupled verify completed on the host domain: step %d, %lld bound row(s) over "
|
||||
"request(s) [%s], kernel %s -- not device evidence", step, (long long)ev.rows,
|
||||
ev.seqs.c_str(), kernel);
|
||||
}
|
||||
}
|
||||
|
||||
/* RADIANCE_MTP_COUPLED: the draw for every drafting row, before any serial pass reads it.
|
||||
*
|
||||
* The coordinate is the PRE-commit output size, which strictly grows between two steps that draft
|
||||
* one request (each commits at least one token), so (base, round) never repeats a draft stream. The
|
||||
* card is the slot the advance writes, and the plugin refuses a row whose staged draw names another
|
||||
* -- which is what makes a stale draw a refusal here rather than a wrong token later. The rule
|
||||
* itself is sched/mtp_couple_plan.h, where it is checked without a card. */
|
||||
int Engine::stage_couple_draws(int n) {
|
||||
std::string why;
|
||||
const int stg = mtp_couple_plan_stage(sched_.plan(),
|
||||
(int)(cfg_.max_seqs > 0 ? cfg_.max_seqs : 1),
|
||||
serial_row_.data(), n, &couple_by_card_,
|
||||
couple_draw_stage_, &why);
|
||||
if (stg < 0) {
|
||||
RAD_ERR("MTP coupled: %s", why.c_str());
|
||||
return stg;
|
||||
}
|
||||
for (auto& rk : ranks_) {
|
||||
void* dst = rk->ctx.buf_ptr(couple_draws_buf_);
|
||||
if (!dst) return RAD_E_STATE;
|
||||
RAD_TRY(rad_memcpy_async(dst, couple_draw_stage_,
|
||||
n * (int64_t)sizeof(RadMtpCoupleDraw), rk->ctx.stream()));
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
@@ -442,6 +442,9 @@ int Registry::commit_(Staged& s, int order) {
|
||||
const auto concurrent = dl_fn<RadKernelConcurrentFn>(s.handle, "rad_kernel_concurrent");
|
||||
const auto collective = dl_fn<RadKernelCollectiveFn>(s.handle, "rad_kernel_collective");
|
||||
const auto chainable = dl_fn<RadKernelChainFn>(s.handle, "rad_kernel_chain");
|
||||
/* Per row when exported, else the library's own answer: a plugin that ships a device row and
|
||||
* its host oracle in one library must not have the oracle's status leak onto the device row. */
|
||||
const auto kern_ref = dl_fn<RadKernelReferenceFn>(s.handle, "rad_kernel_reference");
|
||||
for (const auto& sr : staged_rows) {
|
||||
KernelRow row;
|
||||
row.info = sr.first;
|
||||
@@ -452,7 +455,7 @@ int Registry::commit_(Staged& s, int order) {
|
||||
const int coll = collective ? collective(sr.second) : 0;
|
||||
row.collective = coll != 0;
|
||||
row.releases_link = coll == 2;
|
||||
row.reference = p.reference;
|
||||
row.reference = kern_ref ? kern_ref(sr.second) != 0 : p.reference;
|
||||
row.chain = chainable && chainable(sr.second) != 0;
|
||||
rows_.push_back(row);
|
||||
}
|
||||
|
||||
+3
-1
@@ -546,6 +546,9 @@ public:
|
||||
/* Backs rad_step_fail(): stops the step like an operand rejection, with the caller's
|
||||
* message. */
|
||||
int step_fail(const char* what);
|
||||
/* The device kernel `h` resolved to on this rank ("?" if it resolved none). For log
|
||||
* attribution only. */
|
||||
const char* ip_kernel_name(rad_op h) const;
|
||||
private:
|
||||
[[noreturn]] void fatal_issue(rad_op h, int64_t n, const char* why) const;
|
||||
|
||||
@@ -572,7 +575,6 @@ private:
|
||||
* byte-compares the two results, finding kernels that are not a function of their inputs,
|
||||
* which a host-vs-device comparison cannot detect. */
|
||||
void oracle_repeat(rad_op h, const IssuePlan& dip, int64_t seq, int n_opd);
|
||||
const char* ip_kernel_name(rad_op h) const;
|
||||
/* The reference library's host plan for (op, band): the band's own host plan when a reference
|
||||
* library won it, else one resolved from `registry_` on first use. Null when none exists. */
|
||||
const IssuePlan* oracle_reference(rad_op h, int band);
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
/* mtp_couple_evidence.h -- what the completed-verify line may claim.
|
||||
*
|
||||
* The line is evidence only if all of it holds: the build that staged bound rows belongs to the
|
||||
* step whose wait just returned, at least one of its sequences is still live at that point, and
|
||||
* the op that ran is a DEVICE op. Each of these was wrong in the first version of the line:
|
||||
*
|
||||
* - It gated on Sampler::staged_any_couple(), which the drafter's build_greedy had already
|
||||
* cleared between the build and the wait -- so the line never appeared in the coupled path.
|
||||
* It reads Sampler::couple_build() now, the snapshot build_step froze.
|
||||
* - It listed the rows of whatever build was last staged. The snapshot carries its step, and
|
||||
* the line is written only for the step being collected.
|
||||
* - It printed a kernel name without the domain, so a host reference row read like a device
|
||||
* verify. The domain is decided here and printed on the line.
|
||||
* - It listed sequences that were live at build time. `live` is asked after the wait (under the
|
||||
* scheduler's lock), and only those rows count.
|
||||
*
|
||||
* A pure function of the snapshot, a liveness predicate and the op's domain, so the decision can
|
||||
* be driven without an Engine (tests/sample_test.cpp).
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "sampler.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace rad {
|
||||
|
||||
struct MtpCoupleEvidence {
|
||||
int64_t rows = 0; /* bound rows of sequences still live after the wait */
|
||||
bool device = false; /* the op that ran is a device op */
|
||||
std::string seqs; /* those sequences, comma separated, in build order */
|
||||
};
|
||||
|
||||
/* `live(seq)` answers whether the sequence is still Running; the engine passes a predicate over
|
||||
* Scheduler::running(), the tests a set. Empty (rows == 0) when the snapshot is not this step's,
|
||||
* or nothing it staged is still live. */
|
||||
template <class Live>
|
||||
MtpCoupleEvidence mtp_couple_evidence(const Sampler::CoupleBuild& b, int64_t step, int domain,
|
||||
Live&& live) {
|
||||
MtpCoupleEvidence e;
|
||||
if (b.step != step) return e;
|
||||
e.device = domain == RAD_DOMAIN_DEVICE;
|
||||
for (const auto& s : b.seqs)
|
||||
if (live(s.first)) {
|
||||
e.rows += s.second;
|
||||
if (!e.seqs.empty()) e.seqs += ",";
|
||||
e.seqs += std::to_string(s.first);
|
||||
}
|
||||
return e;
|
||||
}
|
||||
|
||||
} // namespace rad
|
||||
+106
-2
@@ -4,6 +4,8 @@
|
||||
#include "sampler.h"
|
||||
#include "gbnf.h"
|
||||
|
||||
#include "../../arch/common/rad_spechub_config.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
@@ -133,6 +135,14 @@ int Sampler::declare(RadBuilder* b, const SamplerConfig& cfg) {
|
||||
}
|
||||
|
||||
cfg_ = cfg;
|
||||
/* A value other than unset/0/1 is refused, not read as off: a deployment that asked for the
|
||||
* coupling and got a typo must not be served the uncoupled path while believing otherwise. */
|
||||
if (!mtp_coupled_config_valid()) {
|
||||
RAD_ERR("RADIANCE_MTP_COUPLED accepts 0|1 (or unset); refusing the start rather than "
|
||||
"serving the uncoupled path");
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
couple_ = mtp_coupled_enabled();
|
||||
if (cfg_.n_vocab_local <= 0) cfg_.n_vocab_local = cfg_.n_vocab / cfg_.world_size;
|
||||
|
||||
/* The staged mask covers this rank's shard; the grammar's covers the whole vocabulary.
|
||||
@@ -179,6 +189,14 @@ int Sampler::declare(RadBuilder* b, const SamplerConfig& cfg) {
|
||||
* penalised). Resolved on the host, which has the vocabulary. */
|
||||
buf_brk_ = decl("sampler.dry_breakers", RAD_U8, rows, cfg_.max_hist, RAD_BUF_PERSIST);
|
||||
buf_mask_ = decl("sampler.mask", RAD_U32, rows, mask_words_, RAD_BUF_PERSIST);
|
||||
/* THE COUPLED STATE PLANE, one row per sampled position PERSIST because it is filled by an upload
|
||||
* before the run phase issues anything. A draft round's state is bound to the verify row that
|
||||
* follows it, not to the request, which is why it rides the sampler's own row array. */
|
||||
if (couple_) {
|
||||
buf_couple_ = decl("sampler.mtp_couple", RAD_U32, rows, RAD_MTP_COUPLE_STATE_WORDS,
|
||||
RAD_BUF_PERSIST);
|
||||
if (!buf_couple_) return RAD_E_NOMEM;
|
||||
}
|
||||
buf_cand_i_ = decl("sampler.cand_idx", RAD_I32, rows, ncand, RAD_BUF_TRANSIENT);
|
||||
buf_cand_v_ = decl("sampler.cand_val", RAD_F32, rows, ncand, RAD_BUF_TRANSIENT);
|
||||
if (vocab_parallel()) {
|
||||
@@ -249,6 +267,22 @@ int Sampler::declare(RadBuilder* b, const SamplerConfig& cfg) {
|
||||
op_typical_ = op_cand("sample_typical");
|
||||
op_xtc_ = op_cand("sample_xtc");
|
||||
op_pick_ = op_cand("sample_pick");
|
||||
/* THE COUPLING'S VERIFY, over the SAME candidate plane the pick just read. A row whose state is
|
||||
* not bound keeps the pick's token, so an uncoupled batch issues nothing and pays nothing; a
|
||||
* coupled one pays one more launch over its candidate width, not its vocabulary. */
|
||||
if (couple_) {
|
||||
RadParam pc[3] = {
|
||||
RadParam{ "M", RAD_P_RANGE, 1, rows, nullptr },
|
||||
RadParam{ "n_cand", RAD_P_INT, (long long)ncand, 0, nullptr },
|
||||
RadParam{ "n_vocab", RAD_P_INT, (long long)cfg_.n_vocab, 0, nullptr },
|
||||
};
|
||||
op_couple_ = rad_decl_op(b, "mtp_couple_verify", pc, 3, nullptr, 0);
|
||||
if (!op_couple_) return RAD_E_NOKERNEL;
|
||||
/* No rad_op_reads here: the sampler's buffers are shared by ops that declare no lists
|
||||
* (topk, topp, pick), so declaring only this one would narrow the candidate planes to
|
||||
* its own op and the first sample_topk issue would be refused as outside their
|
||||
* lifetime. They stay live for the whole program, as before the coupling. */
|
||||
}
|
||||
/* The temperature kernel again, over the candidate plane: `n_vocab` is only a row length to
|
||||
* it, so the candidate width works. */
|
||||
{
|
||||
@@ -308,6 +342,8 @@ int Sampler::declare(RadBuilder* b, const SamplerConfig& cfg) {
|
||||
|
||||
int Sampler::stage_alloc() {
|
||||
const int64_t rows = rows_for(cfg_);
|
||||
/* At most one entry per request; reserved here so a coupled step never allocates. */
|
||||
couple_build_.seqs.reserve((size_t)cfg_.max_seqs);
|
||||
|
||||
/* Pinned: uploaded every step, and a pageable source turns the async copy into a staged one
|
||||
* with a host bounce (spec §8). */
|
||||
@@ -321,6 +357,11 @@ int Sampler::stage_alloc() {
|
||||
hist_stage_ = (int32_t*)rad_dev_alloc(rows * cfg_.max_hist * 4, RAD_MEM_HOST_PINNED);
|
||||
brk_stage_ = (uint8_t*)rad_dev_alloc(rows * cfg_.max_hist, RAD_MEM_HOST_PINNED);
|
||||
mask_stage_ = (uint32_t*)rad_dev_alloc(rows * mask_words_ * 4, RAD_MEM_HOST_PINNED);
|
||||
/* The coupled rows, pinned: uploaded every coupled step, and a pageable source would turn the
|
||||
* async copy into a staged one with a host bounce (spec §8). */
|
||||
if (couple_)
|
||||
couple_stage_ = (RadMtpCoupleState*)rad_dev_alloc(
|
||||
rows * (int64_t)sizeof(RadMtpCoupleState), RAD_MEM_HOST_PINNED);
|
||||
|
||||
/* Logprobs readback; only rank 0 reads it. */
|
||||
if (cfg_.rank == 0) {
|
||||
@@ -337,7 +378,7 @@ int Sampler::stage_alloc() {
|
||||
}
|
||||
|
||||
if (!params_stage_ || !greedy_stage_ || !late_stage_ || !hist_stage_ || !brk_stage_ ||
|
||||
!mask_stage_) {
|
||||
!mask_stage_ || (couple_ && !couple_stage_)) {
|
||||
RAD_ERR("sampler: could not pin %s of staging",
|
||||
humanb(rows * (3 * (int64_t)sizeof(DeviceSampleParams) + cfg_.max_hist * 5 +
|
||||
mask_words_ * 4)).c_str());
|
||||
@@ -354,6 +395,8 @@ void Sampler::stage_free() {
|
||||
if (hist_stage_) rad_dev_free(hist_stage_, RAD_MEM_HOST_PINNED);
|
||||
if (brk_stage_) rad_dev_free(brk_stage_, RAD_MEM_HOST_PINNED);
|
||||
if (mask_stage_) rad_dev_free(mask_stage_, RAD_MEM_HOST_PINNED);
|
||||
if (couple_stage_) rad_dev_free(couple_stage_, RAD_MEM_HOST_PINNED);
|
||||
couple_stage_ = nullptr;
|
||||
if (lp_host_) rad_dev_free(lp_host_, RAD_MEM_HOST_PINNED);
|
||||
if (lp_ci_host_) rad_dev_free(lp_ci_host_, RAD_MEM_HOST_PINNED);
|
||||
if (lp_cv_host_) rad_dev_free(lp_cv_host_, RAD_MEM_HOST_PINNED);
|
||||
@@ -573,7 +616,22 @@ bool Sampler::speculation_fixed(uint64_t req_id) const {
|
||||
|
||||
/* ================================================================== the step path */
|
||||
|
||||
int Sampler::build_step(const std::vector<Request*>& reqs) {
|
||||
/* BIND THE DRAFT'S OWN STATES. Every row must name this request and the round it belongs to: a state
|
||||
* that arrived under another request's id, or at another round, would be coupled against a law this
|
||||
* step never drew, and the verify would silently apply the wrong Q. */
|
||||
int Sampler::set_mtp_couple(uint64_t req_id, const RadMtpCoupleState* states, int n) {
|
||||
if (!couple_ || n < 0 || n > RAD_MTP_COUPLE_MAX_ROUNDS || (n && !states)) return RAD_E_STATE;
|
||||
const auto it = seqs_.find(req_id);
|
||||
if (it == seqs_.end()) return RAD_E_NOTFOUND;
|
||||
for (int i = 0; i < n; ++i)
|
||||
if (states[i].draw.seq != req_id || states[i].round != i + 1) return RAD_E_STATE;
|
||||
SeqState& s = it->second;
|
||||
for (int i = 0; i < n; ++i) s.couple[i] = states[i];
|
||||
s.couple_n = n;
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
int Sampler::build_step(const std::vector<Request*>& reqs, int64_t step) {
|
||||
if (!declared_) return RAD_E_STATE;
|
||||
|
||||
const auto t_build = std::chrono::steady_clock::now();
|
||||
@@ -581,8 +639,13 @@ int Sampler::build_step(const std::vector<Request*>& reqs) {
|
||||
n_seq_ = 0;
|
||||
any_brk_ = false;
|
||||
any_late_ = false;
|
||||
any_couple_ = false;
|
||||
cur_stage_ = params_stage_;
|
||||
lp_rows_.clear();
|
||||
/* The evidence snapshot this build owns; build_greedy never touches it. */
|
||||
couple_build_.step = step;
|
||||
couple_build_.rows = 0;
|
||||
couple_build_.seqs.clear();
|
||||
|
||||
/* DRY's host half for one row whose history `h[0, take)` is staged. Over the window the
|
||||
* kernel uses (the tail of the row, `dry_penalty_last_n` long when positive) it computes the
|
||||
@@ -669,6 +732,35 @@ int Sampler::build_step(const std::vector<Request*>& reqs) {
|
||||
if (walk && k > 0 && !dead && gr->accept_token(r->draft[k - 1]) < 0) dead = true;
|
||||
if (budget && k > 0) think.push(sp.reasoning, r->draft[k - 1]);
|
||||
const int64_t row = n_seq_++;
|
||||
/* THE COUPLED ROW. `k` is this sampled position's index in the window, so draft row k
|
||||
* verifies against the state the draft that produced it left behind. A row with no
|
||||
* bound state -- the bonus row, a greedy row, an uncoupled step -- gets a zeroed one,
|
||||
* and the verify leaves the pick's token alone. */
|
||||
if (couple_) {
|
||||
couple_stage_[row] = RadMtpCoupleState{};
|
||||
const auto sit = seqs_.find(r->id);
|
||||
if (sit != seqs_.end() && !sp.greedy() && r->draft && k + 1 < n_pos &&
|
||||
k < sit->second.couple_n) {
|
||||
const RadMtpCoupleState& c = sit->second.couple[k];
|
||||
/* BIND TO THIS ROW: same request and seed, the round this row's draft came
|
||||
* from, the position it samples, and the very token it proposes. Anything else
|
||||
* is a stale state from another step, and coupling against it would apply a law
|
||||
* this step never drew. The rule is rad_mtp_couple_binds (abi/rad_spechub.h);
|
||||
* its position term is rad_mtp_couple_verify_pos, the formula the engine binds
|
||||
* by (core/sched/mtp_couple_bind.h). */
|
||||
if (rad_mtp_couple_binds(c, r->id, sp.seed, (uint64_t)n_out, k, r->draft[k],
|
||||
cfg_.n_vocab)) {
|
||||
couple_stage_[row] = c;
|
||||
any_couple_ = true;
|
||||
++couple_build_.rows;
|
||||
auto& q = couple_build_.seqs;
|
||||
size_t j = 0;
|
||||
while (j < q.size() && q[j].first != r->id) ++j;
|
||||
if (j == q.size()) q.emplace_back(r->id, 1);
|
||||
else ++q[j].second;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
DeviceSampleParams& p = params_stage_[row];
|
||||
std::memset(&p, 0, sizeof p);
|
||||
@@ -881,6 +973,7 @@ int Sampler::build_greedy(int64_t n) {
|
||||
n_seq_ = n;
|
||||
any_brk_ = false;
|
||||
any_late_ = false;
|
||||
any_couple_ = false;
|
||||
cur_stage_ = greedy_stage_;
|
||||
if (greedy_ready_) return RAD_OK;
|
||||
for (int64_t i = 0; i < cap; ++i) {
|
||||
@@ -951,6 +1044,9 @@ int Sampler::run(RadCtx* c, rad_buf logits, rad_buf out_tokens) {
|
||||
if (any_mask)
|
||||
RAD_TRY(rad_memcpy_async(rad_buf_ptr(c, buf_mask_), mask_stage_,
|
||||
n_seq_ * mask_words_ * 4, s));
|
||||
if (any_couple_)
|
||||
RAD_TRY(rad_memcpy_async(rad_buf_ptr(c, buf_couple_), couple_stage_,
|
||||
n_seq_ * (int64_t)sizeof(RadMtpCoupleState), s));
|
||||
|
||||
const RadOperand o_logits = opd_rows(logits, n_seq_);
|
||||
const RadOperand o_params = opd_rows(buf_params_, n_seq_);
|
||||
@@ -1027,6 +1123,14 @@ int Sampler::run(RadCtx* c, rad_buf logits, rad_buf out_tokens) {
|
||||
RAD_TRY(issue(op_temp_late_, { o_cv, opd_rows(buf_params_late_, n_seq_) }));
|
||||
|
||||
RAD_TRY(issue(op_pick_, { o_ci, o_cv, o_params, o_tok }));
|
||||
/* AFTER the pick, over the same plane: a coupled row replaces the pick's token with the
|
||||
* coupling's, and an unbound row leaves it. The issue is what the evidence line reads: the op
|
||||
* and the build step it belongs to are recorded together, so a staged row set is never
|
||||
* mistaken for an issued verify. */
|
||||
if (any_couple_) {
|
||||
RAD_TRY(issue(op_couple_, { o_ci, o_cv, o_params, opd_rows(buf_couple_, n_seq_), o_tok }));
|
||||
couple_issued_step_ = couple_build_.step;
|
||||
}
|
||||
|
||||
/* Copy the logprobs rows to the host behind the pick, on rank 0 only (collect_step asks
|
||||
* it): the summary and the candidate planes, which logprobs_for reads when the summary lacks
|
||||
|
||||
+44
-2
@@ -71,6 +71,7 @@
|
||||
*/
|
||||
#pragma once
|
||||
#include "rad_sample.h"
|
||||
#include "rad_spechub.h"
|
||||
|
||||
#include "../rad_core.h"
|
||||
#include "grammar.h"
|
||||
@@ -80,6 +81,7 @@
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace rad {
|
||||
@@ -160,13 +162,18 @@ public:
|
||||
/* Whether allows_speculation's answer can no longer change. It changes only when a grammar
|
||||
* whose backend cannot rewind starts to constrain (on a commit). */
|
||||
bool speculation_fixed(uint64_t req_id) const;
|
||||
/* RADIANCE_MTP_COUPLED: bind the per-round states of the linear draft just published, `n` of
|
||||
* them (0 clears). Each must name this request, the round it belongs to, and a `draw.mode` the
|
||||
* row actually ran. RAD_E_NOTFOUND for a request already ended here. */
|
||||
int set_mtp_couple(uint64_t req_id, const RadMtpCoupleState* states, int n);
|
||||
|
||||
/* ---------------------------------------------------------- step path */
|
||||
|
||||
/* Stage this step's parameter rows, penalty history and grammar bitmasks into pinned memory.
|
||||
* Scheduler thread, before the rank threads are released at the barrier. Allocates nothing;
|
||||
* every buffer was sized at declare against max_seqs (spec §3.1). */
|
||||
int build_step(const std::vector<Request*>& reqs);
|
||||
* every buffer was sized at declare against max_seqs (spec §3.1). `step` is the engine's step
|
||||
* the build stages for, carried into the coupling snapshot. */
|
||||
int build_step(const std::vector<Request*>& reqs, int64_t step = -1);
|
||||
|
||||
/* Stage `n` greedy rows for a drafter: no penalties, mask or history, and no RNG use, so
|
||||
* only the argmax runs. Draft output is a proposal checked by a greedy verifier
|
||||
@@ -200,6 +207,29 @@ public:
|
||||
* meaningful only where the row carries RAD_SP_MASK. */
|
||||
const uint32_t* staged_mask(int64_t row) const { return mask_stage_ + row * mask_words_; }
|
||||
int64_t mask_words() const { return mask_words_; }
|
||||
/* Staged row `row`'s bound coupled state, as `run` uploads it: all-zero where the row carries
|
||||
* none (bonus row, greedy row, uncoupled step). Lets a build be checked without a device. */
|
||||
const RadMtpCoupleState& staged_couple(int64_t row) const { return couple_stage_[row]; }
|
||||
/* Whether any row of the last build carries a bound state, i.e. whether `run` issues the
|
||||
* verify op at all. Cleared by build_greedy: a gate for run(), not for evidence. */
|
||||
bool staged_any_couple() const { return any_couple_; }
|
||||
/* The verify op the last build resolved, for naming its kernel in a log line. */
|
||||
rad_op staged_couple_op() const { return op_couple_; }
|
||||
|
||||
/* What build_step staged for the coupled verify, frozen there: build_greedy (the drafter's
|
||||
* greedy passes, issued between that build and the step's wait) clears any_couple_ and
|
||||
* replaces cur_stage_/n_seq_, but must not make the evidence unreadable. */
|
||||
struct CoupleBuild {
|
||||
int64_t step = -1; /* the engine step this build staged */
|
||||
int64_t rows = 0; /* bound rows */
|
||||
/* The distinct sequences among them, with their bound rows. */
|
||||
std::vector<std::pair<uint64_t, int64_t>> seqs;
|
||||
};
|
||||
const CoupleBuild& couple_build() const { return couple_build_; }
|
||||
/* The engine step whose build this sampler's run() last issued the verify op for, or -1. The
|
||||
* op and the build it belongs to are recorded together, so an evidence read can tell an
|
||||
* actually issued verify from a staged row set. */
|
||||
int64_t staged_couple_issued_step() const { return couple_issued_step_; }
|
||||
|
||||
private:
|
||||
/* Whether the vocabulary is split, judged by this rank's logit width rather than the rank
|
||||
@@ -218,6 +248,11 @@ private:
|
||||
* newly committed tokens only. Read only when the request carries a thinking budget. */
|
||||
ReasoningCount think;
|
||||
size_t think_seen = 0;
|
||||
/* RADIANCE_MTP_COUPLED: the linear draft's per-round states for the window the scheduler
|
||||
* published, bound by set_mtp_couple and consumed by build_step. `couple_n` is how many of
|
||||
* `couple` are live; 0 for a request whose drafts carry no coupled proposal. */
|
||||
RadMtpCoupleState couple[RAD_MTP_COUPLE_MAX_ROUNDS]{};
|
||||
int couple_n = 0;
|
||||
};
|
||||
|
||||
/* Mask staged row `row` to the reasoning block's close token, once the budget is spent
|
||||
@@ -244,6 +279,7 @@ private:
|
||||
rad_op op_argmax_ = RAD_NULL_HANDLE;
|
||||
rad_op op_allgather_ = RAD_NULL_HANDLE;
|
||||
rad_op op_merge_ = RAD_NULL_HANDLE; /* the candidate merge, under tensor parallel */
|
||||
rad_op op_couple_ = RAD_NULL_HANDLE; /* RADIANCE_MTP_COUPLED only */
|
||||
rad_op op_logprobs_ = RAD_NULL_HANDLE; /* summary of the raw distribution */
|
||||
rad_op op_lp_gather_ = RAD_NULL_HANDLE; /* every rank's summary, under a split vocabulary */
|
||||
|
||||
@@ -259,6 +295,12 @@ private:
|
||||
rad_buf buf_pairs_ = RAD_NULL_HANDLE; /* this rank's candidates as (global id, logit) */
|
||||
rad_buf buf_lp_ = RAD_NULL_HANDLE; /* sample_logprobs' stats, [rows, lp_width()] */
|
||||
rad_buf buf_lp_g_ = RAD_NULL_HANDLE; /* and every rank's side by side */
|
||||
rad_buf buf_couple_ = RAD_NULL_HANDLE; /* [rows, RAD_MTP_COUPLE_STATE_WORDS], flag only */
|
||||
RadMtpCoupleState* couple_stage_ = nullptr; /* pinned, one row per sampled position */
|
||||
bool couple_ = false; /* RADIANCE_MTP_COUPLED */
|
||||
bool any_couple_ = false; /* does any row this step carry a bound state? */
|
||||
CoupleBuild couple_build_{}; /* what the last build_step staged, for the evidence line */
|
||||
int64_t couple_issued_step_ = -1; /* step whose verify op run() last issued */
|
||||
|
||||
/* Pinned staging, allocated once at declare. */
|
||||
DeviceSampleParams* params_stage_ = nullptr;
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
/* mtp_couple_bind.h -- the engine's post-commit binding step for a serial drafting row.
|
||||
*
|
||||
* Engine::finish_serial_drafter calls this after the step's commit, once the card's proposal states
|
||||
* have been read back. It answers two questions the position contract turns on:
|
||||
*
|
||||
* - WHICH LENGTH the next build will stage the row at. That is the request's committed output
|
||||
* length AFTER this commit -- the value the next build_step reads -- and nothing else. It is
|
||||
* deliberately not rebuilt from the draw's pre-commit base plus the slot counter (n_acc): the
|
||||
* two agree only while the commit emits as many tokens as the card accepted, and a max_tokens
|
||||
* or context cut emits fewer.
|
||||
* - WHETHER the row may bind at all. A sequence the plan no longer names, or whose request is no
|
||||
* longer Running, will not sample again: its rows are terminal work (a carried ahead step, a
|
||||
* request that finished on this commit). Binding them would put a coupled verify on the card
|
||||
* for a request that has already left the sampler, which reads as a live coupled round in any
|
||||
* log and is not one.
|
||||
*
|
||||
* The answer comes from Scheduler::committed_seq(), which commit fills under the scheduler lock.
|
||||
* It is deliberately NOT read off the plan's StepEntry::req: after commit that Request may already
|
||||
* have been reaped (release_locked destroys it), and a serial bind that dereferenced it would read
|
||||
* freed memory.
|
||||
*
|
||||
* Here rather than in engine_draft.cpp so the rule can be driven against a real scheduler commit
|
||||
* and the sampler's own build (tests/sched_test.cpp, tests/sample_test.cpp) without an Engine.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "advance.h"
|
||||
#include "rad_spechub.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
|
||||
namespace rad {
|
||||
|
||||
/* Where the next build will sample this sequence, after the current step's commit. */
|
||||
struct MtpCoupleBindPoint {
|
||||
bool found = false; /* the committed step carried the sequence */
|
||||
bool live = false; /* and its request was still Running (not finished/cancelled/failed) */
|
||||
uint64_t n_out = 0; /* its committed output length: what the next build reads as n_out */
|
||||
};
|
||||
|
||||
/* Bind `done` rounds of this row's draw to the positions the next build samples. `rb` is round 1
|
||||
* of this row in the engine's readback (stride `rb_stride` states), `tok[rd]` the card's token for
|
||||
* round rd, `S` the card's slot. Fills `out[0, done)` and sets `*n_bound = done`; a sequence that
|
||||
* is not live binds nothing (`*n_bound = 0`, RAD_OK) and leaves `out` untouched. RAD_E_STATE with
|
||||
* `why` for a readback that is not this draw's, or is not a state the proposer could have written.
|
||||
* `base` is never read here: it is the draft's coordinate, not the verify's. */
|
||||
inline int mtp_couple_bind_rounds(const MtpCoupleBindPoint& p, const RadMtpCoupleDraw& d,
|
||||
const RadMtpCoupleState* rb, int rb_stride, int done,
|
||||
const StepSlot& S, int64_t vocab, RadMtpCoupleState* out,
|
||||
int* n_bound, std::string* why) {
|
||||
*n_bound = 0;
|
||||
if (!p.live) return RAD_OK;
|
||||
const bool sampled = d.mode == 1;
|
||||
for (int rd = 1; rd <= done; ++rd) {
|
||||
const RadMtpCoupleState& st = rb[(size_t)(rd - 1) * (size_t)rb_stride];
|
||||
if (std::memcmp(&st.draw, &d, sizeof d) || st.round != rd || st.x != S.tok[rd] ||
|
||||
st.status != (sampled ? 1 : 0) ||
|
||||
(sampled && !rad_mtp_couple_state_valid(st, vocab))) {
|
||||
*why = "MTP coupled: sequence " + std::to_string(d.seq) + " round " +
|
||||
std::to_string(rd) + " returned status " + std::to_string(st.status) +
|
||||
" round " + std::to_string(st.round) + " x " + std::to_string(st.x) +
|
||||
" for card token " + std::to_string(S.tok[rd]) + " (draw " +
|
||||
(std::memcmp(&st.draw, &d, sizeof d) ? "stale" : "current") +
|
||||
"); refusing the step";
|
||||
return RAD_E_STATE;
|
||||
}
|
||||
RadMtpCoupleState b = st;
|
||||
b.pos = rad_mtp_couple_verify_pos(p.n_out, rd);
|
||||
out[rd - 1] = b;
|
||||
}
|
||||
*n_bound = done;
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
} // namespace rad
|
||||
@@ -0,0 +1,56 @@
|
||||
/* mtp_couple_plan.h -- which draw the engine stages for each serial drafting row.
|
||||
*
|
||||
* Engine::stage_couple_draws (core/engine_draft.cpp) reads two things off the step plan: the draw
|
||||
* of every sequence on its card, and, for each drafting row, the plan entry that names the row's
|
||||
* sequence on the row's card. Both are pure host bookkeeping over StepPlan/SerialRow, so they live
|
||||
* here and are checked against a plan without a card.
|
||||
*
|
||||
* ONE ENTRY PER CARD SLOT. `by_card` is indexed by StepEntry::card, whose range is [0, max_seqs) --
|
||||
* the same range as the StepSlots the advance reads. A plane sized for fewer slots makes every
|
||||
* lookup miss and the first step that drafts refuses with "has no plan entry on its card"; a plane
|
||||
* sized from cfg_.max_seqs is what lets a coupled run stage its first draw.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "batch.h"
|
||||
#include "scheduler.h" /* Scheduler::SerialRow */
|
||||
|
||||
#include "rad_spechub.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace rad {
|
||||
|
||||
/* Fills `by_card[0, n_card)` from the plan and writes one draw per drafting row to `stage[0, n)`.
|
||||
* RAD_OK, or RAD_E_STATE with `why` set: the plane is too small for the card slots, or a drafting
|
||||
* row's card carries no plan entry naming its sequence (a stale row -- the plugin would refuse the
|
||||
* staged draw, so it is refused here where the row can be named). */
|
||||
inline int mtp_couple_plan_stage(const StepPlan& plan, int n_card, const Scheduler::SerialRow* rows,
|
||||
int n, std::vector<RadMtpCoupleDraw>* by_card,
|
||||
RadMtpCoupleDraw* stage, std::string* why) {
|
||||
if (!by_card || !stage || !why) return RAD_E_INVAL;
|
||||
if ((int)by_card->size() < n_card) {
|
||||
*why = "the draw plane holds " + std::to_string(by_card->size()) +
|
||||
" card slot(s), the step can use " + std::to_string(n_card);
|
||||
return RAD_E_STATE;
|
||||
}
|
||||
for (int c = 0; c < n_card; ++c) (*by_card)[(size_t)c] = RadMtpCoupleDraw{};
|
||||
for (const StepEntry& e : plan.e)
|
||||
if (e.req && e.card >= 0 && e.card < n_card)
|
||||
(*by_card)[(size_t)e.card] = { e.req->sp.seed, (uint64_t)e.req->output.size(), e.seq,
|
||||
e.card, e.req->sp.greedy() ? 0u : 1u };
|
||||
for (int i = 0; i < n; ++i) {
|
||||
const Scheduler::SerialRow& row = rows[i];
|
||||
if (row.card < 0 || row.card >= n_card || (*by_card)[(size_t)row.card].seq != row.seq) {
|
||||
*why = "drafting row " + std::to_string(i) + " (sequence " + std::to_string(row.seq) +
|
||||
", card " + std::to_string(row.card) + ") has no plan entry on its card";
|
||||
return RAD_E_STATE;
|
||||
}
|
||||
stage[i] = (*by_card)[(size_t)row.card];
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
} // namespace rad
|
||||
@@ -188,6 +188,7 @@ int Scheduler::init(Program& prog, const Config& cfg, IKVManager* kv, IPrefixCac
|
||||
free_cards_[(size_t)c] = (int32_t)(cfg.max_seqs - 1 - c); /* 0 is taken first */
|
||||
reqs_.reserve((size_t)cfg.max_seqs);
|
||||
completed_.reserve((size_t)cfg.max_seqs);
|
||||
committed_.reserve((size_t)cfg.max_seqs);
|
||||
by_id_.reserve((size_t)cfg.max_seqs * 2);
|
||||
hit_blocks_.resize(prog.kv_groups.size());
|
||||
for (auto& v : hit_blocks_) v.reserve(64);
|
||||
@@ -307,6 +308,25 @@ Request* Scheduler::find(uint64_t id) {
|
||||
return it == by_id_.end() ? nullptr : reqs_[(size_t)it->second]->req.get();
|
||||
}
|
||||
|
||||
bool Scheduler::running(uint64_t id) const {
|
||||
std::lock_guard<std::mutex> lk(mu_);
|
||||
auto it = by_id_.find(id);
|
||||
if (it == by_id_.end()) return false;
|
||||
const SchedReq& s = *reqs_[(size_t)it->second];
|
||||
return s.req && s.req->state == ReqState::Running;
|
||||
}
|
||||
|
||||
bool Scheduler::committed_seq(uint64_t seq, uint64_t* n_out, bool* live) const {
|
||||
std::lock_guard<std::mutex> lk(mu_);
|
||||
for (const CommittedSeq& c : committed_)
|
||||
if (c.seq == seq) {
|
||||
if (n_out) *n_out = c.n_out;
|
||||
if (live) *live = c.live;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
int Scheduler::set_draft(uint64_t id, const int32_t* tokens, int n) {
|
||||
std::lock_guard<std::mutex> lk(mu_);
|
||||
auto it = by_id_.find(id);
|
||||
@@ -1804,6 +1824,10 @@ int Scheduler::commit(const StepResult& res) {
|
||||
}
|
||||
step_open_ = false;
|
||||
|
||||
/* What the serial bind reads after commit, copied here while the Requests are still held:
|
||||
* reap() may release a finished one before finish_serial_drafter runs. */
|
||||
committed_.clear();
|
||||
|
||||
const int32_t W = conv_window();
|
||||
|
||||
/* A mixed step leaves decode rows the same replay factors as a decode step (recurrent path
|
||||
@@ -1826,6 +1850,7 @@ int Scheduler::commit(const StepResult& res) {
|
||||
s.pending_free = false;
|
||||
--n_deferred_;
|
||||
}
|
||||
committed_.push_back({e.seq, (uint64_t)r.output.size(), false});
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -1982,6 +2007,7 @@ int Scheduler::commit(const StepResult& res) {
|
||||
} else if (eos) {
|
||||
close_request(s, ReqState::Finished, "stop", carried);
|
||||
}
|
||||
committed_.push_back({e.seq, (uint64_t)r.output.size(), r.state == ReqState::Running});
|
||||
}
|
||||
|
||||
m_.prefill_tokens += step_prefill_tok_;
|
||||
|
||||
@@ -133,6 +133,16 @@ public:
|
||||
const int32_t* first_row = nullptr; /* [n_seq] */
|
||||
};
|
||||
|
||||
/* What commit left a sequence at: the length it was committed at and whether it was still
|
||||
* Running when its entry was retired. Copied under the lock in commit because reap() may
|
||||
* destroy the Request before the serial bind reads it; the plan's StepEntry::req must not be
|
||||
* dereferenced after commit. */
|
||||
struct CommittedSeq {
|
||||
uint64_t seq = 0;
|
||||
uint64_t n_out = 0;
|
||||
bool live = false;
|
||||
};
|
||||
|
||||
/* `geo` is the already-resolved chunk geometry. The engine resolves it once because
|
||||
* PrefixCache::configure runs first and needs its checkpoint interval; pass nullptr to
|
||||
* resolve one here. */
|
||||
@@ -161,6 +171,12 @@ public:
|
||||
* under exactly the load that cancels. */
|
||||
int cancel(uint64_t id);
|
||||
Request* find(uint64_t id);
|
||||
/* Whether the request is still admitted and Running, read under the lock. For an evidence
|
||||
* line written after a step's wait, where the staging that named it is older than the wait. */
|
||||
bool running(uint64_t id) const;
|
||||
/* What the step just committed left for `seq`, copied by commit under the lock. False when
|
||||
* that step carried no such sequence. */
|
||||
bool committed_seq(uint64_t seq, uint64_t* n_out, bool* live) const;
|
||||
|
||||
/* Requests that reached a terminal state since the last call, oldest first, swapped into
|
||||
* `out` under the lock (old contents discarded). Only the engine consumes it, to drop
|
||||
@@ -616,6 +632,8 @@ private:
|
||||
* covered, both decayed with the same time constant, so their ratio is tokens over time. */
|
||||
double tps_win_prefill_ = 0.0, tps_win_decode_ = 0.0, tps_win_time_ = 0.0;
|
||||
bool step_open_ = false;
|
||||
/* The step just committed: its sequences' committed length and liveness, in plan order. */
|
||||
std::vector<CommittedSeq> committed_;
|
||||
/* The single-sequence decode floor (rad::DecodeFloor), fed by step_finished. */
|
||||
DecodeFloor decode_floor_;
|
||||
|
||||
|
||||
Reference in new issue
Block a user