From baf6e89ecb27ac1e3df892b0265ba921936e545a Mon Sep 17 00:00:00 2001 From: zivius Date: Tue, 20 Oct 2026 20:54:55 +0200 Subject: [PATCH] core: an audio generator of one branch over a hybrid language model -- the driver stages a delta net's state and convolution groups beside the paged one, with each entry's context and num_accepted, the generator loop zeroes the slots a request is granted, and a model may read its input with special tokens (its speaker tags) --- abi/rad_builder.h | 10 ++ core/build/rad_builder.cpp | 12 +++ core/engine.cpp | 38 ++++---- core/engine.h | 3 + core/engine_gen.cpp | 2 + core/gen/audio_driver.cpp | 185 ++++++++++++++++++++++++++++--------- core/gen/audio_driver.h | 27 +++++- core/server/audio_gen.cpp | 12 ++- core/server/iface.h | 4 + tests/abi_layout_rows.h | 2 + tests/abi_layout_table.h | 4 +- tests/audio_gen_test.cpp | 85 +++++++++++++++++ tests/speech_test.cpp | 22 +++++ 13 files changed, 339 insertions(+), 67 deletions(-) diff --git a/abi/rad_builder.h b/abi/rad_builder.h index 3e33597d..e122636d 100644 --- a/abi/rad_builder.h +++ b/abi/rad_builder.h @@ -963,6 +963,16 @@ typedef struct RadAudioGenDecl { * latent: `lat_num` = `lat_den` = 1, `samples_per_latent` samples a frame. */ int64_t voc_chunk; int64_t voc_context; + /* Sequences a request runs: 0 or 2, the conditional and unconditional branches above; 1, the + * conditional one alone, for a model without guidance (no unconditional prompt is rendered, + * a request's guidance must be 1, and every per-entry array of a PROMPT or FRAME pass has one + * entry a lane, its `slot` the lane itself). The KV, the prompt's rows and a frame's rows then + * cost half. */ + int32_t branches; + /* Nonzero: the `{input}` slot is tokenised with its special tokens, which the template's own + * text always is, so a caller can write the model's speaker tags. The plugin's + * rad_arch_text_prepare then refuses the control tokens a caller must not write. */ + int32_t input_special; } RadAudioGenDecl; int rad_declare_audio_generator(RadBuilder* b, const RadAudioGenDecl* d); diff --git a/core/build/rad_builder.cpp b/core/build/rad_builder.cpp index e97cac62..41915494 100644 --- a/core/build/rad_builder.cpp +++ b/core/build/rad_builder.cpp @@ -1371,6 +1371,18 @@ int Builder::declare_audio_generator(const RadAudioGenDecl* d) { "instructions and needs {input} alone", nm); return RAD_E_INVAL; } + if (d->branches != 0 && d->branches != 1 && d->branches != 2) { + err(RAD_E_INVAL, "declare_audio_generator '%s': branches is %d; a request runs 1 sequence " + "or 2 (0 means 2)", nm, (int)d->branches); + return RAD_E_INVAL; + } + /* One branch has no unconditional prompt to render and no guidance to apply. */ + if (d->branches == 1 && (d->neg_tmpl || d->default_cfg != 1.0)) { + err(RAD_E_INVAL, "declare_audio_generator '%s': a model of one branch has no " + "unconditional template and no guidance (default_cfg %g)", nm, + d->default_cfg); + return RAD_E_INVAL; + } if (d->neg_tmpl && !d->prompt_whole) { err(RAD_E_INVAL, "declare_audio_generator '%s': an unconditional template makes branches " "of different lengths, which step together only as whole prompts; set " diff --git a/core/engine.cpp b/core/engine.cpp index 3256f380..dd8db874 100644 --- a/core/engine.cpp +++ b/core/engine.cpp @@ -438,22 +438,7 @@ int Engine::step_loop() { idle = 0; if (t_on) phase_mark(PH_PLAN); - /* Zero freshly assigned recurrent slots before the step that reads them. The manager - * records the grab but cannot write (StateClear in core/mem/kv.h); the list is empty except - * on a sequence's first step. Each rank clears by its own plan: the slot number is the - * scheduler's, but the state size depends on the heads this rank holds, which can differ - * per rank (rad_arch.h remainder_place). */ - for (const StateClear& c : ranks_[0]->kv.state_clears()) { - for (auto& rk : ranks_) { - const KVGroupPlan* gp = rk->kv.plan(c.group); - if (!gp || !gp->stateful()) continue; - const int64_t bytes = gp->layer_bytes_per_state(); - for (int64_t l = 0; l < gp->n_layers; ++l) - if (void* p = rk->kv.state_ptr(c.group, (int32_t)l, c.slot)) - rad_memset_async(p, 0, bytes, rk->ctx.stream()); - } - } - ranks_[0]->kv.clear_state_clears(); + clear_fresh_states(); /* Entries that want logits rows, in BatchBuilder's out_ids order: those with * produces_token, 1 + n_spec rows each. Row and sequence indices differ (a mid-prompt @@ -1339,6 +1324,25 @@ void Engine::drain_completed() { for (uint64_t id : done_) for (auto& rk : ranks_) rk->sampler.end_request(id); } +/* Zero freshly assigned recurrent slots before the step that reads them. The manager records the + * grab but cannot write (StateClear in core/mem/kv.h); the list is empty except on a sequence's + * first step. Each rank clears by its own plan: the slot number is the scheduler's, but the state + * size depends on the heads this rank holds, which can differ per rank (rad_arch.h + * remainder_place). */ +void Engine::clear_fresh_states() { + for (const StateClear& c : ranks_[0]->kv.state_clears()) { + for (auto& rk : ranks_) { + const KVGroupPlan* gp = rk->kv.plan(c.group); + if (!gp || !gp->stateful()) continue; + const int64_t bytes = gp->layer_bytes_per_state(); + for (int64_t l = 0; l < gp->n_layers; ++l) + if (void* p = rk->kv.state_ptr(c.group, (int32_t)l, c.slot)) + rad_memset_async(p, 0, bytes, rk->ctx.stream()); + } + } + ranks_[0]->kv.clear_state_clears(); +} + int Engine::serve(std::unique_ptr watch) { startup_watch_ = std::move(watch); if (ranks_.empty() || !ranks_[0]->ctx.program()) { @@ -2494,6 +2498,8 @@ int Engine::run_server() { in.default_steps = ad.voc_chunk > 0 ? 0 : ad.default_steps; in.default_cfg = ad.default_cfg; in.max_lanes = ad.max_lanes; + in.branches = ad.branches == 1 ? 1 : 2; + in.input_special = ad.input_special != 0; audio_bridge.drv = aud_gen_; audio_bridge.plugins = &plugins_; d.audio = &audio_bridge; diff --git a/core/engine.h b/core/engine.h index 4b07a680..40ccff48 100644 --- a/core/engine.h +++ b/core/engine.h @@ -355,6 +355,9 @@ private: gen::AudioDriver* aud_gen_ = nullptr; gen::VideoDriver* vid_gen_ = nullptr; int gen_loop(); + /* Zeroes the state slots the KV manager granted since the last step, on every rank's stream, + * before the step or pass that first reads them. */ + void clear_fresh_states(); bool serial_drafting() const; int advance_all(int mode, int round, bool from_proposal, int rows = -1); int issue_serial_drafter(int step, StepOut& so); diff --git a/core/engine_gen.cpp b/core/engine_gen.cpp index 27b286da..e4e02569 100644 --- a/core/engine_gen.cpp +++ b/core/engine_gen.cpp @@ -53,6 +53,8 @@ int Engine::gen_loop() { continue; } idle = 0; + /* A hybrid model's recurrent state: the slots a newly admitted request was granted. */ + clear_fresh_states(); if (const int ps = prepare_release(b); ps < 0) return stop(ps); for (auto& rk : ranks_) rk->ctx.set_gen_lora_slot(gen_->lora_slot()); issue_what_.store(kIssueModel, std::memory_order_release); diff --git a/core/gen/audio_driver.cpp b/core/gen/audio_driver.cpp index c40df146..37e52044 100644 --- a/core/gen/audio_driver.cpp +++ b/core/gen/audio_driver.cpp @@ -68,8 +68,22 @@ 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 */ }; +/* 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 +91,7 @@ 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); return z; } @@ -110,6 +125,10 @@ 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 */ }; int64_t off = 0; for (int r = 0; r < n; ++r) { @@ -123,8 +142,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 +157,39 @@ int AudioDriver::init(const Program& prog, IKVManager* kv, const std::vector(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 +199,13 @@ int AudioDriver::init(const Program& prog, IKVManager* kv, const std::vector= 0; --i) free_lanes_.push_back((int32_t)i); return RAD_OK; @@ -224,7 +269,9 @@ int AudioDriver::submit(std::unique_ptr 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) @@ -309,16 +356,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 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 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 +440,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 +462,23 @@ void AudioDriver::base_batches(int64_t n_tok, int64_t n_seq, int32_t max_q, int3 K.seqused = dptr(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(ri, R_SBT) + gi * max_seqs_; + Ks.block_table_pitch = 1; + Ks.seqused = dptr(ri, R_USED); + Ks.state_index = dptr(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(ri, R_CTX); + B.num_accepted = dptr(ri, R_ACC); } A = RadAudioGenBatch{}; A.lane = dptr(ri, R_LANE); @@ -438,6 +503,31 @@ void AudioDriver::base_batches(int64_t n_tok, int64_t n_seq, int32_t max_q, int3 } } +void AudioDriver::stage_state(const std::vector& seqs, const std::vector& ctx) { + const int64_t n = (int64_t)seqs.size(); + int32_t* cl = hptr(R_CTX); + int32_t* acc = hptr(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(R_SBT) + gi * max_seqs_; + int32_t* si = hptr(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& bt, int64_t p, int64_t bs) { const int64_t bi = p / bs; @@ -517,12 +607,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(j.T - j.prompt_done, max_tok_ / 2); + cb[0] = std::min(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(R_TOK); @@ -530,12 +621,12 @@ const RadBatch* AudioDriver::build_prompt(Job& j, int step) { int32_t* slot = hptr(R_SLOT); int32_t* cu = hptr(R_CU); int64_t pitch = 1; - for (int b = 0; b < 2; ++b) + for (int b = 0; b < nb_; ++b) pitch = std::max(pitch, (int64_t)kv_->block_table(j.seq[b], kv_group_).size()); int32_t* btab = hptr(R_BT); int32_t* used = hptr(R_USED); cu[0] = 0; - for (int b = 0; b < 2; ++b) { + for (int b = 0; b < nb_; ++b) { const std::vector& src = b ? j.neg : j.req->tokens; const std::vector& bt = kv_->block_table(j.seq[b], kv_group_); for (int64_t i = 0; i < cb[b]; ++i) { @@ -549,26 +640,31 @@ 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(R_ESLOT)[b] = 2 * j.lane + b; + std::vector seqs; + std::vector ctx; + for (int b = 0; b < nb_; ++b) { + hptr(R_ESLOT)[b] = nb_ * j.lane + b; hptr(R_LROW)[b] = cu[b + 1] - 1; hptr(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(R_LANE)[0] = j.lane; hptr(R_LAST)[0] = last ? 1 : 0; hptr(R_SEED)[0] = (uint32_t)(j.req->seed & 0xffffffffu); hptr(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; 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) { @@ -586,7 +682,7 @@ const RadBatch* AudioDriver::build_prompt(Job& j, int step) { const RadBatch* AudioDriver::build_frame(const std::vector& 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(R_TOK); int32_t* pos = hptr(R_POS); int32_t* slot = hptr(R_SLOT); @@ -599,17 +695,19 @@ const RadBatch* AudioDriver::build_frame(const std::vector& 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(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 seqs; + std::vector 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& bt = kv_->block_table(j.seq[b], kv_group_); tok[e] = 0; pos[e] = (int32_t)p; @@ -618,9 +716,11 @@ const RadBatch* AudioDriver::build_frame(const std::vector& 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(R_ESLOT)[e] = 2 * j.lane + b; + hptr(R_ESLOT)[e] = nb_ * j.lane + b; hptr(R_LROW)[e] = (int32_t)e; max_ctx = std::max(max_ctx, (int32_t)(p + 1)); + seqs.push_back(j.seq[b]); + ctx.push_back((int32_t)p); } hptr(R_CFG)[i] = j.cfg; lane[i] = h_lane_[(size_t)i] = j.lane; @@ -630,6 +730,7 @@ const RadBatch* AudioDriver::build_frame(const std::vector& 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; diff --git a/core/gen/audio_driver.h b/core/gen/audio_driver.h index bb986103..318b89c7 100644 --- a/core/gen/audio_driver.h +++ b/core/gen/audio_driver.h @@ -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,10 @@ 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_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 T* hptr(int r) const { return reinterpret_cast(host_ + reg_[r].off); } template T* dptr(int rank, int r) const { @@ -124,6 +136,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& seqs, const std::vector& ctx); int admit_locked(); void release_locked(Job& j); @@ -152,6 +167,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 state_groups_; + int nb_ = 2; /* sequences a request: RadAudioGenDecl::branches */ bool neg_own_ = false; /* the unconditional prompt is the request's own */ std::vector ranks_; IKVManager* kv_ = nullptr; @@ -167,7 +184,7 @@ private: std::vector out_; /* one per rank */ std::vector aout_; - std::vector kout_; + std::vector 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. */ diff --git a/core/server/audio_gen.cpp b/core/server/audio_gen.cpp index ebe50260..78c8e93b 100644 --- a/core/server/audio_gen.cpp +++ b/core/server/audio_gen.cpp @@ -14,8 +14,9 @@ * Prompt: the plugin's template with its slots filled, each text first rewritten by the plugin * into the form the model was trained on (rad_arch_text_prepare). The template is tokenised with * its special tokens and the texts without, so a caller cannot write the model's control tokens - * into a lyric. A model that declares the unconditional branch's own template gets that one - * rendered from the same texts. + * into a lyric; a model that declares `input_special` has its input tokenised with them too (its + * speaker tags), its prepare() refusing the control tokens. A model that declares the + * unconditional branch's own template gets that one rendered from the same texts. * * Response: the whole piece, once it is finished, as `response_format` wav (16-bit PCM, the * model's rate and channels) or pcm (the same samples raw, little-endian). Streamed as the @@ -156,6 +157,8 @@ int Server::speech_request(const json& body, const std::string& text, if (!body["guidance_scale"].is_number() || !(body["guidance_scale"].get() >= 1.0) || body["guidance_scale"].get() > 30.0) return fail(err_bad("guidance_scale", "expected a number in [1, 30]")); + if (info.branches == 1 && body["guidance_scale"].get() != 1.0) + return fail(err_unsupported("guidance_scale", "this model runs without guidance")); r->cfg = (float)body["guidance_scale"].get(); } bool seed_given = false; @@ -191,7 +194,10 @@ int Server::speech_request(const json& body, const std::string& text, } if (p == std::string::npos) break; const bool is_inst = p == a; - const auto ids = deps_.tok->encode(is_inst ? i2 : t2, false, /*parse_special=*/false); + /* The input keeps its special tokens only for a model that asks (its speaker tags); + * the plugin's prepare() has refused the ones a caller may not write. */ + const auto ids = deps_.tok->encode(is_inst ? i2 : t2, false, + /*parse_special=*/!is_inst && info.input_special); ids_out->insert(ids_out->end(), ids.begin(), ids.end()); at = p + (is_inst ? kInst : kInput).size(); } diff --git a/core/server/iface.h b/core/server/iface.h index 8cd70dd4..664bdae4 100644 --- a/core/server/iface.h +++ b/core/server/iface.h @@ -649,6 +649,10 @@ struct AudioGenInfo { int64_t sample_rate = 0, channels = 0; int64_t default_steps = 0, max_lanes = 1; /* default_steps 0: no denoiser */ double default_cfg = 1.0; + /* RadAudioGenDecl::branches (1: no guidance) and ::input_special (the {input} slot is + * tokenised with special tokens). */ + int32_t branches = 2; + bool input_special = false; }; class IAudioGen { diff --git a/tests/abi_layout_rows.h b/tests/abi_layout_rows.h index 0abae8bf..8187d639 100644 --- a/tests/abi_layout_rows.h +++ b/tests/abi_layout_rows.h @@ -169,6 +169,8 @@ ABI_FIELD(RadAudioGenDecl, prompt_whole) ABI_FIELD(RadAudioGenDecl, first_frame) ABI_FIELD(RadAudioGenDecl, voc_chunk) ABI_FIELD(RadAudioGenDecl, voc_context) +ABI_FIELD(RadAudioGenDecl, branches) +ABI_FIELD(RadAudioGenDecl, input_special) ABI_STRUCT(RadAudioFormat) ABI_FIELD(RadAudioFormat, struct_size) ABI_FIELD(RadAudioFormat, audio_open) diff --git a/tests/abi_layout_table.h b/tests/abi_layout_table.h index 9ff6bc7a..ed92fab5 100644 --- a/tests/abi_layout_table.h +++ b/tests/abi_layout_table.h @@ -134,7 +134,7 @@ { "RadAudioGenBatch", "voc_left", 244, 4 }, { "RadAudioGenBatch", "voc_right", 248, 4 }, { "RadAudioGenBatch", "voc_frame0", 256, 8 }, -{ "RadAudioGenDecl", "", 0, 224 }, +{ "RadAudioGenDecl", "", 0, 232 }, { "RadAudioGenDecl", "name", 0, 8 }, { "RadAudioGenDecl", "tmpl", 8, 8 }, { "RadAudioGenDecl", "neg_token", 16, 4 }, @@ -166,6 +166,8 @@ { "RadAudioGenDecl", "first_frame", 204, 4 }, { "RadAudioGenDecl", "voc_chunk", 208, 8 }, { "RadAudioGenDecl", "voc_context", 216, 8 }, +{ "RadAudioGenDecl", "branches", 224, 4 }, +{ "RadAudioGenDecl", "input_special", 228, 4 }, { "RadAudioFormat", "", 0, 72 }, { "RadAudioFormat", "struct_size", 0, 4 }, { "RadAudioFormat", "audio_open", 8, 8 }, diff --git a/tests/audio_gen_test.cpp b/tests/audio_gen_test.cpp index c99cffcb..c076fba8 100644 --- a/tests/audio_gen_test.cpp +++ b/tests/audio_gen_test.cpp @@ -209,4 +209,89 @@ TEST(the_unconditional_branch_is_the_conditional_prompt_at_guidance_one) { } } +/* A hybrid language model (paged attention beside a linear-attention state and a convolution + * group, as Qwen3.5's delta-net layers keep) of one branch: every pass stages one sequence a + * request, the state groups' rows are the sequence's slots, and each entry carries its context + * and a one-based num_accepted. The slots held at the prompt are the frames' too; guidance other + * than 1 is refused. */ +TEST(a_hybrid_model_of_one_branch_stages_its_state_groups) { + AudioFixture f; + f.prog.audio_gen = decl_chunked(); + f.prog.audio_gen.branches = 1; + f.prog.agen_str.name = "hybrid"; + f.prog.ctx.max_tok = 64; + f.prog.ctx.max_seqs = 8; + f.prog.kv_groups.emplace_back(); + for (int kind : { RAD_KV_FULL, RAD_KV_LINEAR, RAD_KV_CONV }) { + KVGroupInfo g; + g.name = kind == RAD_KV_FULL ? "attn" : kind == RAD_KV_LINEAR ? "state" : "conv"; + g.decl.kind = kind; + g.block_size = kind == RAD_KV_FULL ? 16 : 0; + f.prog.kv_groups.push_back(g); + } + f.kv.setup(f.prog.kv_groups, 1 << 20, 256, 4); + CHECK_OK(f.start()); + std::string why; + CHECK(f.drv.submit(f.req(2.0f), &why) < 0); + CHECK(has(why, "without guidance")); + CHECK_OK(f.drv.submit(f.req(1.0f), &why)); + + auto state_rows = [](const RadBatch* b, int32_t* st, int32_t* cv) { + *st = *cv = -2; + for (int i = 0; i < b->n_kv_groups; ++i) { + const RadKVGroupBatch& k = b->kv[i]; + if (k.group == 1) continue; + REQUIRE(k.state_index != nullptr && k.block_table != nullptr); + CHECK_EQ(k.block_table[0], k.state_index[0]); + (k.group == 2 ? *st : *cv) = k.state_index[0]; + } + }; + const RadBatch* b = f.drv.next(0); + REQUIRE(b != nullptr && b->gen == RAD_GEN_PASS_PROMPT); + CHECK_EQ(b->n_seq, (int64_t)1); + CHECK_EQ(b->n_tok, (int64_t)4); + REQUIRE_EQ(b->n_kv_groups, 3); + int32_t st0 = 0, cv0 = 0; + state_rows(b, &st0, &cv0); + CHECK(st0 >= 0 && cv0 >= 0); + REQUIRE(b->ctx_lens != nullptr && b->num_accepted != nullptr); + CHECK_EQ(b->ctx_lens[0], 0); + CHECK_EQ(b->num_accepted[0], 1); + CHECK_EQ(b->agen_b->slot[0], b->agen_b->lane_h[0]); /* one branch: the slot is the lane */ + f.answer(b, 0, 0.0f); + CHECK_OK(f.drv.done()); + + b = f.drv.next(1); + REQUIRE(b != nullptr && b->gen == RAD_GEN_PASS_FRAME); + CHECK_EQ(b->n_seq, (int64_t)1); + CHECK_EQ(b->n_tok, (int64_t)1); + CHECK_EQ(b->positions[0], 4); /* after the four prompt rows */ + CHECK_EQ(b->ctx_lens[0], 4); + CHECK_EQ(b->num_accepted[0], 1); + int32_t st1 = 0, cv1 = 0; + state_rows(b, &st1, &cv1); + CHECK_EQ(st1, st0); + CHECK_EQ(cv1, cv0); +} + +/* The driver stages one paged group beside state groups: a second paged group is refused at init, + * by name. */ +TEST(a_second_paged_group_is_refused) { + AudioFixture f; + f.prog.audio_gen = decl_chunked(); + f.prog.agen_str.name = "two"; + f.prog.ctx.max_tok = 64; + f.prog.ctx.max_seqs = 8; + f.prog.kv_groups.emplace_back(); + for (int i = 0; i < 2; ++i) { + KVGroupInfo g; + g.name = i ? "b" : "a"; + g.decl.kind = RAD_KV_FULL; + g.block_size = 16; + f.prog.kv_groups.push_back(g); + } + f.kv.setup(f.prog.kv_groups, 1 << 20, 256, 4); + CHECK(f.start() < 0); +} + RAD_TEST_MAIN() diff --git a/tests/speech_test.cpp b/tests/speech_test.cpp index 6574c0f5..6cb7536f 100644 --- a/tests/speech_test.cpp +++ b/tests/speech_test.cpp @@ -174,6 +174,28 @@ TEST(a_speech_model_takes_optional_instructions_and_renders_its_unconditional_pr CHECK(m.last().neg_tokens.empty()); } +/* A model that declares input_special (its speaker tags are special tokens) has its input + * tokenised with them, and one of one branch refuses any guidance but 1 under its field; other + * models' input stays plain text. */ +TEST(a_model_that_asks_has_its_input_read_with_special_tokens_and_one_branch_has_no_guidance) { + Speech s; + s.gen.inf.tmpl_plain = "{input}"; + s.gen.inf.default_steps = 0; + s.gen.inf.input_special = true; + s.gen.inf.branches = 1; + s.f->tok.last_parse_special = false; + CHECK_EQ(s.post({ { "input", "hi" } }).status, 200); + CHECK(s.f->tok.last_parse_special); + CHECK_EQ(s.refused({ { "input", "hi" }, { "guidance_scale", 2.0 } }), + std::string("guidance_scale")); + CHECK_EQ(s.post({ { "input", "hi" }, { "guidance_scale", 1.0 } }).status, 200); + Speech m; + m.gen.inf.tmpl_plain = "{input}"; + m.gen.inf.default_steps = 0; + CHECK_EQ(m.post({ { "input", "hi" } }).status, 200); + CHECK(!m.f->tok.last_parse_special); +} + /* ================================================================== the response */ /* The whole piece: the windows in order whatever order they were played in, as a WAV whose header