merge 1.12.0: gemma4 E2B/E4B and Nemotron Bolt E4B arrive; the ref2va audio, rows and ABI work carried across

conv1d keeps both stride forms: upstream's causal codec downsample with cu_div (Fish Audio) and the port's explicit-pad non-causal one (the H3 audio encoder's), in libref, libavx and libr4d alike; the ABI takes upstream's 27 on top of the port's RadVideoGenBatch growth.
This commit is contained in:
gearwave00001 committed 2026-10-24 19:03:26 -05:00
commit 7c3b5ab7d5
285 files changed
+30500 -2898

No files matched your search

+6
View File
@@ -65,6 +65,12 @@ struct AudioRequest {
* conditional prompt as the declaration says (neg_token), and `neg_tokens` is empty. */
std::vector<int32_t> tokens;
std::vector<int32_t> neg_tokens;
/* References (RadAudioGenDecl::tmpl_ref): the clips' samples end to end, each padded to whole
* frames; their bounds in samples, [clips + 1]; and for each row of `tokens` the frame of the
* clips it is, -1 for a token. All empty without references. */
std::vector<float> ref_audio;
std::vector<int32_t> ref_cu;
std::vector<int32_t> ref_frame;
int64_t max_frames = 0; /* the most frames to make, >= 1 */
int32_t steps = 0; /* 0: the model's default */
float cfg = 0.0f; /* 0: the model's default */
+207 -45
View File
@@ -64,12 +64,25 @@ namespace {
constexpr int64_t kAlign = 256;
int64_t align_up(int64_t v) { return (v + kAlign - 1) / kAlign * kAlign; }
struct Sizes {
int64_t T = 0, S = 0, L = 0, NB = 0;
int64_t NG = 0; /* stateful KV groups */
int64_t RS = 0, RN = 0; /* references: samples and clips a request */
};
/* The paged full-attention group, and how many stateful groups sit beside it. */
int32_t full_group(const Program& prog, int64_t* n_state) {
int32_t full = 0;
int64_t ns = 0;
for (size_t g = 1; g < prog.kv_groups.size(); ++g) {
const int kind = prog.kv_groups[g].decl.kind;
if (kind == RAD_KV_FULL && !full) full = (int32_t)g;
else if (kind == RAD_KV_LINEAR || kind == RAD_KV_CONV) ++ns;
}
if (n_state) *n_state = ns;
return full;
}
Sizes sizes_of(const Program& prog, int64_t block_size) {
const RadAudioGenDecl& d = prog.audio_gen;
Sizes z;
@@ -77,6 +90,9 @@ Sizes sizes_of(const Program& prog, int64_t block_size) {
z.S = prog.ctx.max_seqs;
z.L = d.max_lanes;
z.NB = block_size > 0 ? (AudioDriver::max_seq_rows(d) + block_size - 1) / block_size : 0;
full_group(prog, &z.NG);
z.RS = d.ref_max_samples;
z.RN = d.ref_max;
return z;
}
@@ -110,12 +126,22 @@ int64_t carve(const Sizes& z, int64_t (*bytes)[2], int n) {
z.S * I, /* R_ESLOT */
z.S * I, /* R_LROW */
z.S * I, /* R_PREFIX */
z.S * I, /* R_CTX */
z.S * I, /* R_ACC */
z.NG * z.S * I, /* R_SBT */
z.NG * z.S * AudioDriver::kMaxStateWidth * I, /* R_SIDX */
z.S * I, /* R_PSLOT */
z.S * I, /* R_PPOS */
z.S * I, /* R_PUSED */
z.RS * 4, /* R_RAUD */
z.RN > 0 ? (z.RN + 1) * I : 0, /* R_RCU */
z.RN > 0 ? z.T * I : 0, /* R_RFRM */
};
int64_t off = 0;
for (int r = 0; r < n; ++r) {
bytes[r][0] = off;
bytes[r][1] = want[r];
off = align_up(off + want[r]);
off = align_up(off + want[r], kAlign);
}
return off;
}
@@ -123,8 +149,10 @@ int64_t carve(const Sizes& z, int64_t (*bytes)[2], int n) {
} /* namespace */
int64_t AudioDriver::device_bytes(const Program& prog) {
if (!prog.is_audio_generator() || prog.kv_groups.size() < 2) return 0;
const Sizes z = sizes_of(prog, prog.kv_groups[1].block_size);
if (!prog.is_audio_generator()) return 0;
const int32_t fg = full_group(prog, nullptr);
if (!fg) return 0;
const Sizes z = sizes_of(prog, prog.kv_groups[(size_t)fg].block_size);
int64_t b[R_N][2];
return carve(z, b, R_N);
}
@@ -136,15 +164,39 @@ int AudioDriver::init(const Program& prog, IKVManager* kv, const std::vector<Ran
name_ = prog.agen_str.name;
kv_ = kv;
ranks_ = ranks;
/* The language model's KV, both branches of every lane: one paged full-attention group. */
if (prog.kv_groups.size() != 2 || prog.kv_groups[1].decl.kind != RAD_KV_FULL ||
prog.kv_groups[1].block_size <= 0) {
RAD_ERR("audio generator: the program declares %zu KV group(s); the driver stages exactly "
"one full-attention group, the language model's", prog.kv_groups.size() - 1);
/* The language model's KV, every branch of every lane: one paged full-attention group, and
* a hybrid model's linear-attention state and convolution groups beside it. */
state_groups_.clear();
int n_full = 0;
for (size_t g = 1; g < prog.kv_groups.size(); ++g) {
const int kind = prog.kv_groups[g].decl.kind;
if (kind == RAD_KV_FULL) {
++n_full;
} else if (kind == RAD_KV_LINEAR || kind == RAD_KV_CONV) {
StateGroup sg;
sg.g = (int32_t)g;
sg.width = std::max<int64_t>(1, kv->state_index_width((int32_t)g));
if (sg.width > kMaxStateWidth) {
RAD_ERR("audio generator: KV group %zu asks for %lld state slots a sequence; the "
"driver stages at most %lld", g, (long long)sg.width,
(long long)kMaxStateWidth);
return RAD_E_UNSUPPORTED;
}
state_groups_.push_back(sg);
} else {
n_full = -1;
break;
}
}
kv_group_ = full_group(prog, nullptr);
if (n_full != 1 || !kv_group_ || prog.kv_groups[(size_t)kv_group_].block_size <= 0) {
RAD_ERR("audio generator: the program declares %zu KV group(s); the driver stages one "
"paged full-attention group, the language model's, and the state and "
"convolution groups of a hybrid one beside it", prog.kv_groups.size() - 1);
return RAD_E_UNSUPPORTED;
}
kv_group_ = 1;
block_size_ = prog.kv_groups[1].block_size;
block_size_ = prog.kv_groups[(size_t)kv_group_].block_size;
nb_ = decl_.branches == 1 ? 1 : 2;
const Sizes z = sizes_of(prog, block_size_);
max_tok_ = z.T;
max_seqs_ = z.S;
@@ -154,13 +206,13 @@ int AudioDriver::init(const Program& prog, IKVManager* kv, const std::vector<Ran
* chunk's vocode its frames and their context, and a whole prompt both its branches. */
const int64_t step_rows = chunked() ? decl_.voc_context + decl_.voc_chunk
: 2 * (decl_.win_lead + win_latents(decl_.win_frames));
const int64_t prompt_rows = decl_.prompt_whole ? 2 * decl_.max_prompt : 2;
if (max_seqs_ < 2 * decl_.max_lanes || max_tok_ < 2 * decl_.max_lanes ||
const int64_t prompt_rows = decl_.prompt_whole ? nb_ * decl_.max_prompt : nb_;
if (max_seqs_ < nb_ * decl_.max_lanes || max_tok_ < nb_ * decl_.max_lanes ||
max_tok_ < step_rows || max_tok_ < prompt_rows) {
RAD_ERR("audio generator: a pass carries %lld rows of %lld sequences; '%s' needs %lld "
"sequences for its %lld lanes, %lld rows for a %s and %lld for a whole prompt",
(long long)max_tok_, (long long)max_seqs_, name_.c_str(),
(long long)(2 * decl_.max_lanes), (long long)decl_.max_lanes,
(long long)(nb_ * decl_.max_lanes), (long long)decl_.max_lanes,
(long long)step_rows, chunked() ? "chunk's vocode" : "window's step",
(long long)prompt_rows);
return RAD_E_UNSUPPORTED;
@@ -193,7 +245,7 @@ int AudioDriver::init(const Program& prog, IKVManager* kv, const std::vector<Ran
if (!ranks_.empty()) RAD_TRY(rad_dev_set(ranks_[0].device));
out_.assign(ranks_.size(), RadBatch{});
aout_.assign(ranks_.size(), RadAudioGenBatch{});
kout_.assign(ranks_.size(), RadKVGroupBatch{});
kout_.assign(ranks_.size() * (1 + state_groups_.size()), RadKVGroupBatch{});
free_lanes_.clear();
for (int64_t i = decl_.max_lanes - 1; i >= 0; --i) free_lanes_.push_back((int32_t)i);
return RAD_OK;
@@ -224,7 +276,9 @@ int AudioDriver::submit(std::unique_ptr<AudioRequest> r, std::string* why) {
return RAD_E_INVAL;
};
const int64_t T = (int64_t)r->tokens.size(), Tn = (int64_t)r->neg_tokens.size();
if (!neg_own_ && T <= d.neg_keep_head + d.neg_keep_tail)
if (nb_ == 1 && r->cfg != 0.0f && r->cfg != 1.0f)
return no("this model runs without guidance; guidance_scale must be 1");
if (nb_ == 2 && !neg_own_ && T <= d.neg_keep_head + d.neg_keep_tail)
return no("the prompt is " + std::to_string(T) + " tokens, too short to hold its own "
"markers");
if (T < 1 || std::max(T, Tn) > d.max_prompt)
@@ -236,6 +290,25 @@ int AudioDriver::submit(std::unique_ptr<AudioRequest> r, std::string* why) {
" seconds)");
if (r->steps < 0 || r->steps > 1000) return no("num_inference_steps must be 1..1000");
if (r->cfg != 0.0f && !(r->cfg >= 1.0f)) return no("guidance_scale must be >= 1");
/* References: the clips the declaration allows, each a whole number of frames, and a frame
* for every row that names one. */
if (!r->ref_cu.empty() || !r->ref_frame.empty() || !r->ref_audio.empty()) {
const int64_t n_ref = (int64_t)r->ref_cu.size() - 1, hop = d.ref_hop;
bool ok = d.ref_max > 0 && n_ref >= 1 && n_ref <= d.ref_max && r->ref_cu[0] == 0 &&
r->ref_cu.back() == (int64_t)r->ref_audio.size() &&
(int64_t)r->ref_audio.size() <= d.ref_max_samples &&
(int64_t)r->ref_frame.size() == T;
for (int64_t i = 0; ok && i < n_ref; ++i)
ok = r->ref_cu[(size_t)i + 1] > r->ref_cu[(size_t)i] && r->ref_cu[(size_t)i + 1] % hop == 0;
const int64_t frames = ok ? r->ref_cu.back() / hop : 0;
for (int64_t t = 0; ok && t < T; ++t)
ok = r->ref_frame[(size_t)t] >= -1 && r->ref_frame[(size_t)t] < frames;
if (!ok)
return no(d.ref_max > 0 ? "the references do not fit this model: at most " +
std::to_string(d.ref_max) + " clips of whole frames, " +
std::to_string(d.ref_max_samples) + " samples together"
: std::string("this model takes no reference audio"));
}
if (!kv_->can_ever_fit(std::max(T, Tn) + r->max_frames + 1))
return no("a piece of " + std::to_string(r->max_frames) + " frames after a " +
std::to_string(T) + "-token prompt does not fit the KV cache even when it is "
@@ -309,16 +382,19 @@ int AudioDriver::admit_locked() {
const float cfg = r.cfg > 0.0f ? r.cfg : (float)decl_.default_cfg;
/* The unconditional prompt: the request's own (the conditional one at guidance 1, which
* makes the guided logits the conditional ones), or derived from the conditional one. */
std::vector<int32_t> neg = r.tokens;
if (neg_own_ && cfg != 1.0f && !r.neg_tokens.empty()) {
neg = r.neg_tokens;
} else if (!neg_own_) {
for (int64_t i = decl_.neg_keep_head; i < j.T - decl_.neg_keep_tail; ++i)
neg[(size_t)i] = decl_.neg_token;
std::vector<int32_t> neg;
if (nb_ > 1) {
neg = r.tokens;
if (neg_own_ && cfg != 1.0f && !r.neg_tokens.empty()) {
neg = r.neg_tokens;
} else if (!neg_own_) {
for (int64_t i = decl_.neg_keep_head; i < j.T - decl_.neg_keep_tail; ++i)
neg[(size_t)i] = decl_.neg_token;
}
}
/* Both branches for the whole piece, so nothing in flight ever waits for KV. */
/* Every branch for the whole piece, so nothing in flight ever waits for KV. */
bool ok = true;
for (int b = 0; b < 2 && ok; ++b) {
for (int b = 0; b < nb_ && ok; ++b) {
const int64_t rows = (b ? (int64_t)neg.size() : j.T) + r.max_frames + 1;
j.seq[b] = ((uint64_t)1 << 61) | next_seq_++;
if (kv_->add_sequence(j.seq[b]) < 0 || kv_->ensure(j.seq[b], rows) < 0) ok = false;
@@ -390,7 +466,7 @@ void AudioDriver::base_batches(int64_t n_tok, int64_t n_seq, int32_t max_q, int3
for (size_t r = 0; r < ranks_.size(); ++r) {
RadBatch& B = out_[r];
RadAudioGenBatch& A = aout_[r];
RadKVGroupBatch& K = kout_[r];
RadKVGroupBatch& K = kout_[r * (1 + state_groups_.size())];
const int ri = (int)r;
B = RadBatch{};
B.step = step;
@@ -412,8 +488,23 @@ void AudioDriver::base_batches(int64_t n_tok, int64_t n_seq, int32_t max_q, int3
K.seqused = dptr<int32_t>(ri, R_USED);
K.block_size = block_size_;
K.max_blocks = max_blocks_;
B.n_kv_groups = 1;
/* A stateful group's block-table row is the sequence's slot, as the text scheduler
* stages it, so a kernel reading tables needs no second path. */
for (size_t gi = 0; gi < state_groups_.size(); ++gi) {
RadKVGroupBatch& Ks = (&K)[1 + gi];
Ks = RadKVGroupBatch{};
Ks.group = (rad_kvgroup)state_groups_[gi].g;
Ks.block_table = dptr<int32_t>(ri, R_SBT) + gi * max_seqs_;
Ks.block_table_pitch = 1;
Ks.seqused = dptr<int32_t>(ri, R_USED);
Ks.state_index = dptr<int32_t>(ri, R_SIDX) + gi * max_seqs_ * kMaxStateWidth;
Ks.state_index_pitch = state_groups_[gi].width;
Ks.max_blocks = 1;
}
B.n_kv_groups = (int)(1 + state_groups_.size());
B.kv = &K;
B.ctx_lens = dptr<int32_t>(ri, R_CTX);
B.num_accepted = dptr<int32_t>(ri, R_ACC);
}
A = RadAudioGenBatch{};
A.lane = dptr<int32_t>(ri, R_LANE);
@@ -434,10 +525,38 @@ void AudioDriver::base_batches(int64_t n_tok, int64_t n_seq, int32_t max_q, int3
A.slot = dptr<int32_t>(ri, R_ESLOT);
A.last_row = dptr<int32_t>(ri, R_LROW);
A.prefix = dptr<int32_t>(ri, R_PREFIX);
A.prev_slot = dptr<int32_t>(ri, R_PSLOT);
A.prev_pos = dptr<int32_t>(ri, R_PPOS);
A.prev_used = dptr<int32_t>(ri, R_PUSED);
B.agen_b = &A;
}
}
void AudioDriver::stage_state(const std::vector<uint64_t>& seqs, const std::vector<int32_t>& ctx) {
const int64_t n = (int64_t)seqs.size();
int32_t* cl = hptr<int32_t>(R_CTX);
int32_t* acc = hptr<int32_t>(R_ACC);
for (int64_t e = 0; e < n; ++e) {
cl[e] = ctx[(size_t)e];
/* One-based, as num_accepted always is: a pass commits every row it runs. */
acc[e] = 1;
}
reg_[R_CTX].used = reg_[R_ACC].used = n * 4;
for (size_t gi = 0; gi < state_groups_.size(); ++gi) {
const StateGroup& sg = state_groups_[gi];
int32_t* bt = hptr<int32_t>(R_SBT) + gi * max_seqs_;
int32_t* si = hptr<int32_t>(R_SIDX) + gi * max_seqs_ * kMaxStateWidth;
for (int64_t e = 0; e < n; ++e) {
bt[e] = kv_->state_slot(seqs[(size_t)e], sg.g);
kv_->state_index_row(seqs[(size_t)e], sg.g, si + e * sg.width, (int)sg.width);
}
}
if (!state_groups_.empty()) {
reg_[R_SBT].used = (int64_t)state_groups_.size() * max_seqs_ * 4;
reg_[R_SIDX].used = (int64_t)state_groups_.size() * max_seqs_ * kMaxStateWidth * 4;
}
}
/* The KV slot of position `p` in a sequence with this block table. */
static int32_t slot_of(const std::vector<int32_t>& bt, int64_t p, int64_t bs) {
const int64_t bi = p / bs;
@@ -517,12 +636,13 @@ const RadBatch* AudioDriver::build_prompt(Job& j, int step) {
for (int g = 0; g < R_N; ++g) reg_[g].used = 0;
/* Each branch's rows: both whole, or the same chunk of both (their lengths are then equal). */
const int64_t p0 = j.prompt_done;
int64_t cb[2];
int64_t cb[2] = { 0, 0 };
if (decl_.prompt_whole) {
cb[0] = j.T;
cb[1] = j.Tn;
cb[1] = nb_ > 1 ? j.Tn : 0;
} else {
cb[0] = cb[1] = std::min<int64_t>(j.T - j.prompt_done, max_tok_ / 2);
cb[0] = std::min<int64_t>(j.T - j.prompt_done, max_tok_ / nb_);
cb[1] = nb_ > 1 ? cb[0] : 0;
}
const int64_t cmax = std::max(cb[0], cb[1]);
int32_t* tok = hptr<int32_t>(R_TOK);
@@ -530,12 +650,12 @@ const RadBatch* AudioDriver::build_prompt(Job& j, int step) {
int32_t* slot = hptr<int32_t>(R_SLOT);
int32_t* cu = hptr<int32_t>(R_CU);
int64_t pitch = 1;
for (int b = 0; b < 2; ++b)
for (int b = 0; b < nb_; ++b)
pitch = std::max<int64_t>(pitch, (int64_t)kv_->block_table(j.seq[b], kv_group_).size());
int32_t* btab = hptr<int32_t>(R_BT);
int32_t* used = hptr<int32_t>(R_USED);
cu[0] = 0;
for (int b = 0; b < 2; ++b) {
for (int b = 0; b < nb_; ++b) {
const std::vector<int32_t>& src = b ? j.neg : j.req->tokens;
const std::vector<int32_t>& bt = kv_->block_table(j.seq[b], kv_group_);
for (int64_t i = 0; i < cb[b]; ++i) {
@@ -549,26 +669,47 @@ const RadBatch* AudioDriver::build_prompt(Job& j, int step) {
btab[b * pitch + k] = k < (int64_t)bt.size() ? bt[(size_t)k] : -1;
used[b] = (int32_t)(p0 + cb[b]);
}
const int64_t rows = cu[2];
const int64_t rows = cu[nb_];
const bool last = p0 + cb[0] == j.T;
for (int b = 0; b < 2; ++b) {
hptr<int32_t>(R_ESLOT)[b] = 2 * j.lane + b;
std::vector<uint64_t> seqs;
std::vector<int32_t> ctx;
for (int b = 0; b < nb_; ++b) {
hptr<int32_t>(R_ESLOT)[b] = nb_ * j.lane + b;
hptr<int32_t>(R_LROW)[b] = cu[b + 1] - 1;
hptr<int32_t>(R_PREFIX)[b] = (int32_t)p0;
seqs.push_back(j.seq[b]);
ctx.push_back((int32_t)p0);
}
reg_[R_ESLOT].used = reg_[R_LROW].used = reg_[R_PREFIX].used = 2 * 4;
stage_state(seqs, ctx);
reg_[R_ESLOT].used = reg_[R_LROW].used = reg_[R_PREFIX].used = nb_ * 4;
hptr<int32_t>(R_LANE)[0] = j.lane;
hptr<int32_t>(R_LAST)[0] = last ? 1 : 0;
hptr<uint32_t>(R_SEED)[0] = (uint32_t)(j.req->seed & 0xffffffffu);
hptr<uint32_t>(R_SEED)[1] = (uint32_t)(j.req->seed >> 32);
reg_[R_TOK].used = reg_[R_POS].used = reg_[R_SLOT].used = rows * 4;
reg_[R_CU].used = 3 * 4;
reg_[R_BT].used = 2 * pitch * 4;
reg_[R_USED].used = 2 * 4;
reg_[R_CU].used = (nb_ + 1) * 4;
reg_[R_BT].used = nb_ * pitch * 4;
reg_[R_USED].used = nb_ * 4;
reg_[R_LANE].used = reg_[R_LAST].used = 4;
reg_[R_SEED].used = 8;
/* The references: their clips, bounds and each row's frame (a whole prompt, so the rows are
* the prompt's own). */
const AudioRequest& q = *j.req;
const int64_t n_ref = q.ref_cu.empty() ? 0 : (int64_t)q.ref_cu.size() - 1;
h_rcu_.clear();
h_rfrm_.clear();
if (n_ref > 0) {
std::memcpy(hptr<float>(R_RAUD), q.ref_audio.data(), q.ref_audio.size() * 4);
std::memcpy(hptr<int32_t>(R_RCU), q.ref_cu.data(), q.ref_cu.size() * 4);
std::memcpy(hptr<int32_t>(R_RFRM), q.ref_frame.data() + p0, (size_t)cb[0] * 4);
reg_[R_RAUD].used = (int64_t)q.ref_audio.size() * 4;
reg_[R_RCU].used = (int64_t)q.ref_cu.size() * 4;
reg_[R_RFRM].used = cb[0] * 4;
h_rcu_ = q.ref_cu;
h_rfrm_.assign(q.ref_frame.begin() + p0, q.ref_frame.begin() + p0 + cb[0]);
}
if (upload() < 0) return nullptr;
base_batches(rows, 2, (int32_t)cmax, (int32_t)(p0 + cmax), step, true, pitch);
base_batches(rows, nb_, (int32_t)cmax, (int32_t)(p0 + cmax), step, true, pitch);
h_lane_.assign(1, j.lane);
h_last_.assign(1, last ? 1 : 0);
for (size_t r = 0; r < ranks_.size(); ++r) {
@@ -577,6 +718,16 @@ const RadBatch* AudioDriver::build_prompt(Job& j, int step) {
A.n_lane = 1;
A.lane_h = h_lane_.data();
A.last_h = h_last_.data();
if (n_ref > 0) {
const int ri = (int)r;
A.n_ref = n_ref;
A.ref_samples = (int64_t)q.ref_audio.size();
A.ref_audio = dptr<float>(ri, R_RAUD);
A.ref_cu = dptr<int32_t>(ri, R_RCU);
A.ref_cu_h = h_rcu_.data();
A.ref_frame = dptr<int32_t>(ri, R_RFRM);
A.ref_frame_h = h_rfrm_.data();
}
}
pass_.kind = RAD_GEN_PASS_PROMPT;
pass_.jobs.assign(1, j.id);
@@ -586,7 +737,7 @@ const RadBatch* AudioDriver::build_prompt(Job& j, int step) {
const RadBatch* AudioDriver::build_frame(const std::vector<Job*>& js, int step) {
for (int g = 0; g < R_N; ++g) reg_[g].used = 0;
const int64_t L = (int64_t)js.size(), S = 2 * L;
const int64_t L = (int64_t)js.size(), S = nb_ * L;
int32_t* tok = hptr<int32_t>(R_TOK);
int32_t* pos = hptr<int32_t>(R_POS);
int32_t* slot = hptr<int32_t>(R_SLOT);
@@ -599,17 +750,19 @@ const RadBatch* AudioDriver::build_frame(const std::vector<Job*>& js, int step)
int64_t pitch = 1;
int32_t max_ctx = 0;
for (const Job* j : js)
for (int b = 0; b < 2; ++b)
for (int b = 0; b < nb_; ++b)
pitch = std::max<int64_t>(pitch, (int64_t)kv_->block_table(j->seq[b], kv_group_).size());
h_lane_.assign((size_t)L, 0);
h_frame_.assign((size_t)L, 0);
pass_.jobs.clear();
std::vector<uint64_t> seqs;
std::vector<int32_t> ctx;
for (int64_t i = 0; i < L; ++i) {
const Job& j = *js[(size_t)i];
for (int b = 0; b < 2; ++b) {
for (int b = 0; b < nb_; ++b) {
/* The frame's row follows the branch's prompt and its earlier frames. */
const int64_t p = (b ? j.Tn : j.T) + j.frame;
const int64_t e = 2 * i + b;
const int64_t e = nb_ * i + b;
const std::vector<int32_t>& bt = kv_->block_table(j.seq[b], kv_group_);
tok[e] = 0;
pos[e] = (int32_t)p;
@@ -618,9 +771,16 @@ const RadBatch* AudioDriver::build_frame(const std::vector<Job*>& js, int step)
for (int64_t k = 0; k < pitch; ++k)
btab[e * pitch + k] = k < (int64_t)bt.size() ? bt[(size_t)k] : -1;
used[e] = (int32_t)(p + 1);
hptr<int32_t>(R_ESLOT)[e] = 2 * j.lane + b;
/* The row before, whose hidden state the frame is sampled from: the prompt's last
* row for the first frame. */
hptr<int32_t>(R_PSLOT)[e] = slot_of(bt, p - 1, block_size_);
hptr<int32_t>(R_PPOS)[e] = (int32_t)(p - 1);
hptr<int32_t>(R_PUSED)[e] = (int32_t)p;
hptr<int32_t>(R_ESLOT)[e] = nb_ * j.lane + b;
hptr<int32_t>(R_LROW)[e] = (int32_t)e;
max_ctx = std::max<int32_t>(max_ctx, (int32_t)(p + 1));
seqs.push_back(j.seq[b]);
ctx.push_back((int32_t)p);
}
hptr<float>(R_CFG)[i] = j.cfg;
lane[i] = h_lane_[(size_t)i] = j.lane;
@@ -630,6 +790,7 @@ const RadBatch* AudioDriver::build_frame(const std::vector<Job*>& js, int step)
pass_.jobs.push_back(j.id);
}
cu[S] = (int32_t)S;
stage_state(seqs, ctx);
reg_[R_TOK].used = reg_[R_POS].used = reg_[R_SLOT].used = S * 4;
reg_[R_CU].used = (S + 1) * 4;
reg_[R_BT].used = S * pitch * 4;
@@ -637,6 +798,7 @@ const RadBatch* AudioDriver::build_frame(const std::vector<Job*>& js, int step)
reg_[R_LANE].used = reg_[R_FRAME].used = reg_[R_CFG].used = L * 4;
reg_[R_SEED].used = 2 * L * 4;
reg_[R_ESLOT].used = reg_[R_LROW].used = S * 4;
reg_[R_PSLOT].used = reg_[R_PPOS].used = reg_[R_PUSED].used = S * 4;
if (upload() < 0) return nullptr;
base_batches(S, S, 1, max_ctx, step, true, pitch);
for (size_t r = 0; r < ranks_.size(); ++r) {
+24 -6
View File
@@ -5,9 +5,15 @@
* `done()` folds its result back once every rank has run it. Requests arrive from HTTP threads
* through `submit()` and leave through their AudioTicket.
*
* A request holds a lane and two KV sequences, its conditional and unconditional branches, from
* admission to its last window; both are reserved for the whole piece at admission (prompt plus
* every frame the request may make), so a request in flight never waits for KV.
* A request holds a lane and two KV sequences, its conditional and unconditional branches (one,
* for a model of one branch: RadAudioGenDecl::branches), from admission to its last window; they
* are reserved for the whole piece at admission (prompt plus every frame the request may make), so
* a request in flight never waits for KV.
*
* The language model's KV is one paged full-attention group, beside which a hybrid model keeps
* the linear-attention state and convolution groups of its recurrent layers (RAD_KV_LINEAR,
* RAD_KV_CONV): one slot a sequence each, staged as their `state_index` rows, zeroed by the engine
* before the pass that first reads them.
*
* Pass priority, one pass at a time:
*
@@ -54,6 +60,9 @@ public:
/* The longest sequence a branch holds: the whole prompt and every frame. The KV group is
* sized from it. */
static int64_t max_seq_rows(const RadAudioGenDecl& d) { return d.max_prompt + d.max_frames + 1; }
/* State slots a state_index row holds at most: the state, and the scratch a kernel serving
* speculation asks for (RadKVGroupBatch::state_index_pitch), which a generator never runs. */
static constexpr int64_t kMaxStateWidth = 8;
/* ---------------------------------------------------------- HTTP threads */
/* Queue a request, refused at once with `why` when it can never run. */
@@ -115,7 +124,11 @@ private:
struct Region { int64_t off = 0, bytes = 0, used = 0; };
enum { R_TOK, R_POS, R_CU, R_SLOT, R_BT, R_USED, R_LANE, R_LAST, R_FRAME, R_SEED,
R_T, R_TN, R_CFG, R_PAIR, R_WLANE, R_FLAGS, R_WIDX, R_WF0, R_WNF, R_LCU, R_LSRC,
R_ESLOT, R_LROW, R_PREFIX, R_N };
R_ESLOT, R_LROW, R_PREFIX, R_CTX, R_ACC, R_SBT, R_SIDX, R_PSLOT, R_PPOS, R_PUSED,
R_RAUD, R_RCU, R_RFRM, R_N };
/* A hybrid model's stateful groups (linear-attention state, convolution history), each staged
* as one block-table word a sequence (its slot) and its state_index row. */
struct StateGroup { int32_t g = 0; int64_t width = 1; };
template <class T> T* hptr(int r) const { return reinterpret_cast<T*>(host_ + reg_[r].off); }
template <class T> T* dptr(int rank, int r) const {
@@ -124,6 +137,9 @@ private:
int upload();
void base_batches(int64_t n_tok, int64_t n_seq, int32_t max_q, int32_t max_ctx, int step,
bool kv, int64_t bt_pitch);
/* The stateful groups' rows for the entries of a PROMPT or FRAME pass, entry e being `seq[e]`,
* with each entry's context before the pass (ctx_lens). */
void stage_state(const std::vector<uint64_t>& seqs, const std::vector<int32_t>& ctx);
int admit_locked();
void release_locked(Job& j);
@@ -152,6 +168,8 @@ private:
int64_t max_tok_ = 0, max_seqs_ = 0;
int64_t block_size_ = 0, max_blocks_ = 0;
int32_t kv_group_ = 0;
std::vector<StateGroup> state_groups_;
int nb_ = 2; /* sequences a request: RadAudioGenDecl::branches */
bool neg_own_ = false; /* the unconditional prompt is the request's own */
std::vector<RankIO> ranks_;
IKVManager* kv_ = nullptr;
@@ -167,12 +185,12 @@ private:
std::vector<RadBatch> out_; /* one per rank */
std::vector<RadAudioGenBatch> aout_;
std::vector<RadKVGroupBatch> kout_;
std::vector<RadKVGroupBatch> kout_; /* [rank][1 + state groups] */
Pass pass_;
bool frame_turn_ = true; /* FRAME goes first when both are due */
/* Host arrays a pass's plugin reads on the host (the _h fields), for the life of the pass. */
std::vector<int32_t> h_lane_, h_last_, h_frame_, h_wlane_, h_flags_, h_widx_,
h_wf0_, h_wnf_, h_lcu_;
h_wf0_, h_wnf_, h_lcu_, h_rcu_, h_rfrm_;
mutable std::mutex mu_;
std::deque<std::unique_ptr<Job>> jobs_; /* arrival order */
+10 -3
View File
@@ -13,8 +13,6 @@ namespace {
constexpr int64_t kAlign = 256;
int64_t align_up(int64_t v) { return (v + kAlign - 1) / kAlign * kAlign; }
/* Upper bound on a reference picture's latent tokens: its area plus slack for rounding its sides
* to the size multiple. */
int64_t ref_tokens_max(const RadGeneratorDecl& d) {
@@ -97,7 +95,7 @@ int64_t carve(const Sizes& z, int64_t pixel_ch, int64_t (*bytes)[2], int n) {
for (int r = 0; r < n; ++r) {
bytes[r][0] = off;
bytes[r][1] = want[r][1];
off = align_up(off + want[r][1]);
off = align_up(off + want[r][1], kAlign);
}
return off;
}
@@ -121,6 +119,12 @@ int Driver::init(const Program& prog, IKVManager* kv, const std::vector<RankIO>&
kv_ = kv;
ranks_ = ranks;
table_ = DiscreteTable{};
if (decl_.cond_opts & ~(uint32_t)RAD_GEN_COND_REBALANCE) {
RAD_ERR("image generator: '%s' declares conditioning options 0x%x, and this engine knows "
"only RAD_GEN_COND_REBALANCE (0x%x)", decl_.name ? decl_.name : "",
(unsigned)decl_.cond_opts, (unsigned)RAD_GEN_COND_REBALANCE);
return RAD_E_UNSUPPORTED;
}
if (discrete()) {
std::string why;
if (discrete_table(decl_, &table_, &why) < 0) {
@@ -258,6 +262,8 @@ int Driver::submit(std::unique_ptr<Request> r, std::string* why) {
!(decl_.samplers & (1u << r->sampler)) || r->scheduler < 0 ||
r->scheduler >= RAD_GEN_SCHED_COUNT))
return no("the sampler or scheduler is not one this model serves");
if ((uint32_t)r->cond_opts & ~decl_.cond_opts)
return no("a conditioning option (rebalance) this model does not serve");
r->ticket->t_submit_ns = (int64_t)rad_mono_ns();
std::lock_guard<std::mutex> lk(mu_);
if (closed_) {
@@ -530,6 +536,7 @@ void Driver::finish_batches(int64_t n_tok, int64_t n_seq, int32_t max_q, int ste
G.size_px = nullptr;
G.entry_h = nullptr;
G.tokens_h = nullptr;
G.cond_opts = 0;
}
}
-1
View File
@@ -125,7 +125,6 @@ private:
R_BT, R_USED, R_ROPEF, R_ICU, R_IROWS, R_MCU, R_VCU, R_MROWS, R_MSRC, R_MCCU, R_MROPE,
R_AUX, R_SIGP, R_TSTEP, R_SAMP, R_STEPI, R_SIZE, R_N };
int layout_regions();
template <class T> T* hptr(int r) const { return reinterpret_cast<T*>(host_ + reg_[r].off); }
template <class T> T* dptr(int rank, int r) const {
return reinterpret_cast<T*>((char*)dev_[(size_t)rank] + reg_[r].off);
+1 -2
View File
@@ -195,7 +195,6 @@ constexpr int64_t kAlign = 256;
* accept up to 4096. */
constexpr int64_t kSegCap = 64;
int64_t align_up(int64_t v) { return (v + kAlign - 1) / kAlign * kAlign; }
/* Region offsets and sizes, in the enum's order (video_driver.h), and the total; shared by the
* allocation and the VRAM budget. `ep` and `pd` are the conditioning encoder's pass bound and
@@ -239,7 +238,7 @@ int64_t carve(const RadVideoGenDecl& d, int64_t max_tok, int64_t ep, int64_t pd,
for (int r = 0; r < n; ++r) {
bytes[r][0] = off;
bytes[r][1] = want[r];
off = align_up(off + want[r]);
off = align_up(off + want[r], kAlign);
}
return off;
}