abi: a request's words for the plugin (RadBatch::plugin_words), ABI 32

This commit is contained in:
Brandon Bertelsen committed 2026-10-28 00:33:55 -04:00
1 parent d60da58732
commit 5a2732f063
15 files changed
+207 -8

No files matched your search

+1
View File
@@ -124,6 +124,7 @@ public:
int decl_name_map(const RadNameMap* m);
int declare_logits(rad_buf b);
int declare_pooled(rad_buf b);
int declare_plugin_words(int64_t max_words);
int declare_drafter(const RadDrafterDecl* d);
int drafter_shared_kv(int pos_offset);
int lora_root(const char* native, const char* spellings);
+18
View File
@@ -1219,6 +1219,19 @@ int Builder::declare_pooled(rad_buf b) {
return RAD_OK;
}
/* The words a request may hand the plugin (rad_declare_plugin_words). A bound, not a buffer: the
* batch builder stages them per sequence beside the step's other arrays. */
int Builder::declare_plugin_words(int64_t max_words) {
if (finished_) { err(RAD_E_STATE, "declare_plugin_words after declare finished"); return RAD_E_STATE; }
if (max_words < 0 || max_words > (1 << 20)) {
err(RAD_E_INVAL, "declare_plugin_words: %lld words a request; the bound is 0 to %d",
(long long)max_words, 1 << 20);
return RAD_E_INVAL;
}
prog_.plugin_words = max_words;
return RAD_OK;
}
/* Validates the encoder at declare: it runs only when a request carries media, so a bad
* declaration would otherwise surface on the first image as an out-of-bounds pass.
*
@@ -2591,6 +2604,11 @@ int rad_declare_pooled(RadBuilder* b, rad_buf pooled) {
return B(b)->declare_pooled(pooled);
}
int rad_declare_plugin_words(RadBuilder* b, int64_t max_words) {
if (!b) return RAD_E_INVAL;
return B(b)->declare_plugin_words(max_words);
}
int rad_declare_drafter(RadBuilder* b, const RadDrafterDecl* d) {
if (!b) return RAD_E_INVAL;
return B(b)->declare_drafter(d);
+9
View File
@@ -370,6 +370,9 @@ public:
/* The step's pooled vectors (rad_declare_pooled): set only by an embedding program, which has
* no logits buffer. */
rad_buf pooled_buf = 0;
/* The most words one request hands the plugin (rad_declare_plugin_words,
* RadBatch::plugin_words); 0 when the plugin reads none. */
int64_t plugin_words = 0;
/* Runtime LoRA (core/lora/), empty unless the engine was started with adapters enabled. */
std::vector<LoraIssueSite> lora_sites;
@@ -744,6 +747,12 @@ struct Request {
* The request still samples the one token max_tokens allows; the caller discards it. */
std::shared_ptr<TokenScores> token_scores;
/* Words the plugin reads on the step that samples the prompt's first token
* (RadBatch::plugin_words): word 0 is the engine's, the rest the caller's. At most
* Program::plugin_words of them, which the server checks before submission; clef's are laid
* out by abi/rad_clef.h. Empty for every other request. */
std::vector<int32_t> plugin_words;
/* An embedding request: the whole prompt in one step, never split (a bidirectional encoder
* cannot take half its input), finished at that step with its pooled vector in the sink
* (Sink::embedding). Nothing is generated. Only an embedding program (rad_declare_pooled)
+5 -1
View File
@@ -929,7 +929,7 @@ int Ctx::lane_join(int from, int to) {
* field added to RadBatch changes its size: decide whether the key must read it, then update the
* size here. `agen_b` and `vgen_b` are set only on generator passes, which are never recorded
* (below). */
static_assert(sizeof(void*) != 8 || sizeof(RadBatch) == 360,
static_assert(sizeof(void*) != 8 || sizeof(RadBatch) == 400,
"RadBatch changed: check that Ctx::pass_key reads every new field a pass depends on");
static_assert(sizeof(void*) != 8 || sizeof(RadKVGroupBatch) == 80,
"RadKVGroupBatch changed: check that Ctx::pass_key reads every new field");
@@ -938,6 +938,10 @@ uint64_t Ctx::pass_key(const RadBatch* b) const {
/* Not recordable: encoder passes are shaped by their media, and passes carrying encoder rows
* issue the scatter only when they have some. */
if (b->enc || b->n_mm_rows > 0) return 0;
/* A pass carrying a request's words for the plugin is shaped by them (a head issues per
* sequence from their counts), and such a step samples a prompt's first token, which is not
* the repeating decode pass. */
if (b->n_plugin_words > 0) return 0;
/* A generator pass is shaped by what RadBatch::gen_b, agen_b or vgen_b carries, which no key
* below reads. */
if (b->gen) return 0;
+27
View File
@@ -136,6 +136,9 @@ void BatchBuilder::carve(Set& s, char* h) {
/* Each row's adapter slot, carved only when the engine serves adapters, so an engine without
* them lays the slab out as if the array did not exist. */
pair(&s.h_lora, &s.d_lora, lora_on_ ? T : 0);
/* Each sequence's words for the plugin, carved only when the program declared a bound. */
pair(&s.h_pw, &s.d_pw, pw_cap_ ? S * pw_cap_ : 0);
pair(&s.h_pwcu, &s.d_pwcu, pw_cap_ ? S + 1 : 0);
/* Host-only (derived metadata is copied into the planned arena), but carved on both sides to
* keep device offsets equal to host offsets. */
{
@@ -473,6 +476,7 @@ int BatchBuilder::layout(Program& prog, const Config& cfg) {
mm_on_ = prog.encoder.modalities != 0 || prog.audio_encoder.modalities != 0;
lora_on_ = cfg.lora.enabled();
pw_cap_ = prog.plugin_words;
return RAD_OK;
}
@@ -931,6 +935,8 @@ const RadBatch* BatchBuilder::build(const StepPlan& plan, const IKVManager& kv,
int64_t n_mm = 0;
bool lora_rows = false;
int32_t cvec_rows = 0;
int64_t n_pw = 0;
if (pw_cap_) s.h_pwcu[0] = 0;
s.h_cu[0] = 0;
for (int64_t i = 0; i < n_seq; ++i) {
const StepEntry& e = plan.e[(size_t)i];
@@ -1007,9 +1013,25 @@ const RadBatch* BatchBuilder::build(const StepPlan& plan, const IKVManager& kv,
const int32_t nr = e.hidden_n > 0 ? e.hidden_n : e.n_tokens;
for (int32_t j = 0; j < nr; ++j) s.h_out[n_out++] = e.hidden_row + j;
} else if (e.produces_token) {
/* The request's words for the plugin ride the trunk step that samples its prompt's
* first token (RadBatch::plugin_words); word 0 is that row's index in `out_ids`. */
if (pw_cap_ && e.is_prefill && plan.draft_pass == 0 && e.req &&
!e.req->plugin_words.empty()) {
const std::vector<int32_t>& w = e.req->plugin_words;
if ((int64_t)w.size() > pw_cap_) {
RAD_ERR("step batch: sequence %llu carries %zu words for the plugin, past the "
"%lld it declared", (unsigned long long)e.seq, w.size(),
(long long)pw_cap_);
return nullptr;
}
std::memcpy(s.h_pw + n_pw, w.data(), w.size() * sizeof(int32_t));
s.h_pw[n_pw] = (int32_t)n_out;
n_pw += (int64_t)w.size();
}
const int32_t first = (int32_t)(k + e.n_tokens) - 1 - e.n_spec;
for (int32_t j = 0; j <= e.n_spec; ++j) s.h_out[n_out++] = first + j;
}
if (pw_cap_) s.h_pwcu[i + 1] = (int32_t)n_pw;
if (e.draft_out) s.h_dout[n_dout++] = (int32_t)(k + e.n_tokens - 1);
/* RADIANCE_DFLASH_TRACE logs each drafter pass's kind, positions and token ids. A wrong
* anchor, mask id or position base shows only as near-zero acceptance with correct
@@ -1176,6 +1198,11 @@ const RadBatch* BatchBuilder::build(const StepPlan& plan, const IKVManager& kv,
* runs the same pass as an engine serving no adapters. */
B.lora_slot = lora_rows ? on_rank(base, b0, s.d_lora) : nullptr;
B.control_vector = cvec_rows;
B.n_plugin_words = n_pw;
B.plugin_words = n_pw ? on_rank(base, b0, s.d_pw) : nullptr;
B.plugin_cu = n_pw ? on_rank(base, b0, s.d_pwcu) : nullptr;
B.plugin_words_h = n_pw ? s.h_pw : nullptr;
B.plugin_cu_h = n_pw ? s.h_pwcu : nullptr;
B.enc = 0;
B.enc_n_patch = 0;
B.enc_pixels = nullptr;
+6
View File
@@ -220,6 +220,10 @@ private:
/* [n_tok] each row's runtime LoRA adapter slot (RadBatch::lora_slot): carved only when the
* engine serves adapters, empty otherwise. */
int32_t *h_lora = nullptr, *d_lora = nullptr;
/* [max_seqs * plugin_words] and [max_seqs + 1]: each sequence's words for the plugin
* (RadBatch::plugin_words), carved only when the program declared a bound. */
int32_t *h_pw = nullptr, *d_pw = nullptr;
int32_t *h_pwcu = nullptr, *d_pwcu = nullptr;
/* [n_kv_groups][max_seqs]: every conv group's state slot per sequence (see init()). */
int32_t* h_convslot = nullptr;
@@ -329,6 +333,8 @@ private:
bool mm_on_ = false;
/* The engine serves runtime LoRA adapters (Config::lora): the batch carries each row's slot. */
bool lora_on_ = false;
/* Program::plugin_words: the words a sequence may carry, 0 for none. */
int64_t pw_cap_ = 0;
int64_t n_embd_ = 0;
std::vector<void*> mm_dev_;
uint16_t* mm_host_ = nullptr;