diff --git a/abi/rad_runtime.h b/abi/rad_runtime.h index 6fde1f91..b9c95569 100644 --- a/abi/rad_runtime.h +++ b/abi/rad_runtime.h @@ -466,6 +466,15 @@ typedef struct RadAudioGenBatch { /* VOCODE with RadAudioGenDecl::voc_chunk: the lane's first frame the pass reads, counted * from the first kept frame. 64 bits so that it starts past the struct's ABI 18 end. */ int64_t voc_frame0; + + /* FRAME, one value per entry ([n_seq]): the row before the frame's own row -- the one whose + * hidden state the frame is sampled from -- as its KV slot, its position, and the context + * through it (position + 1). A model whose frame head attends at that row (Fish Audio + * drama-3's codebook tower, layers bound to the language model's full-attention group) issues + * those layers there, before the language model runs at the frame's own row. */ + const int32_t* prev_slot; + const int32_t* prev_pos; + const int32_t* prev_used; } RadAudioGenBatch; /* A window entry's flags on a WINDOW pass. FIRST is RAD_GEN_F_FIRST's bit and the others are clear diff --git a/core/gen/audio_driver.cpp b/core/gen/audio_driver.cpp index 37e52044..c4567b7d 100644 --- a/core/gen/audio_driver.cpp +++ b/core/gen/audio_driver.cpp @@ -129,6 +129,9 @@ int64_t carve(const Sizes& z, int64_t (*bytes)[2], int n) { 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 */ }; int64_t off = 0; for (int r = 0; r < n; ++r) { @@ -499,6 +502,9 @@ void AudioDriver::base_batches(int64_t n_tok, int64_t n_seq, int32_t max_q, int3 A.slot = dptr(ri, R_ESLOT); A.last_row = dptr(ri, R_LROW); A.prefix = dptr(ri, R_PREFIX); + A.prev_slot = dptr(ri, R_PSLOT); + A.prev_pos = dptr(ri, R_PPOS); + A.prev_used = dptr(ri, R_PUSED); B.agen_b = &A; } } @@ -716,6 +722,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); + /* The row before, whose hidden state the frame is sampled from: the prompt's last + * row for the first frame. */ + hptr(R_PSLOT)[e] = slot_of(bt, p - 1, block_size_); + hptr(R_PPOS)[e] = (int32_t)(p - 1); + hptr(R_PUSED)[e] = (int32_t)p; 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)); @@ -738,6 +749,7 @@ const RadBatch* AudioDriver::build_frame(const std::vector& 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) { diff --git a/core/gen/audio_driver.h b/core/gen/audio_driver.h index 318b89c7..55c1f9ed 100644 --- a/core/gen/audio_driver.h +++ b/core/gen/audio_driver.h @@ -124,7 +124,8 @@ 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_CTX, R_ACC, R_SBT, R_SIDX, R_N }; + R_ESLOT, R_LROW, R_PREFIX, R_CTX, R_ACC, R_SBT, R_SIDX, R_PSLOT, R_PPOS, R_PUSED, + 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; }; diff --git a/tests/abi_layout_rows.h b/tests/abi_layout_rows.h index 8187d639..f786dd91 100644 --- a/tests/abi_layout_rows.h +++ b/tests/abi_layout_rows.h @@ -137,6 +137,9 @@ ABI_FIELD(RadAudioGenBatch, voc_latents) ABI_FIELD(RadAudioGenBatch, voc_left) ABI_FIELD(RadAudioGenBatch, voc_right) ABI_FIELD(RadAudioGenBatch, voc_frame0) +ABI_FIELD(RadAudioGenBatch, prev_slot) +ABI_FIELD(RadAudioGenBatch, prev_pos) +ABI_FIELD(RadAudioGenBatch, prev_used) ABI_STRUCT(RadAudioGenDecl) ABI_FIELD(RadAudioGenDecl, name) ABI_FIELD(RadAudioGenDecl, tmpl) diff --git a/tests/abi_layout_table.h b/tests/abi_layout_table.h index ed92fab5..a5f4a404 100644 --- a/tests/abi_layout_table.h +++ b/tests/abi_layout_table.h @@ -98,7 +98,7 @@ { "RadBuildCtx", "max_audio_frames", 88, 8 }, { "RadBuildCtx", "max_mixed_tok", 96, 8 }, { "RadBuildCtx", "kv_quant", 104, 4 }, -{ "RadAudioGenBatch", "", 0, 264 }, +{ "RadAudioGenBatch", "", 0, 288 }, { "RadAudioGenBatch", "n_lane", 0, 8 }, { "RadAudioGenBatch", "lane", 8, 8 }, { "RadAudioGenBatch", "lane_h", 16, 8 }, @@ -134,6 +134,9 @@ { "RadAudioGenBatch", "voc_left", 244, 4 }, { "RadAudioGenBatch", "voc_right", 248, 4 }, { "RadAudioGenBatch", "voc_frame0", 256, 8 }, +{ "RadAudioGenBatch", "prev_slot", 264, 8 }, +{ "RadAudioGenBatch", "prev_pos", 272, 8 }, +{ "RadAudioGenBatch", "prev_used", 280, 8 }, { "RadAudioGenDecl", "", 0, 232 }, { "RadAudioGenDecl", "name", 0, 8 }, { "RadAudioGenDecl", "tmpl", 8, 8 }, diff --git a/tests/audio_gen_test.cpp b/tests/audio_gen_test.cpp index c076fba8..7a4a86ef 100644 --- a/tests/audio_gen_test.cpp +++ b/tests/audio_gen_test.cpp @@ -267,6 +267,10 @@ TEST(a_hybrid_model_of_one_branch_stages_its_state_groups) { 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); + /* The row the frame is sampled from: the prompt's last, at its slot. */ + CHECK_EQ(b->agen_b->prev_pos[0], 3); + CHECK_EQ(b->agen_b->prev_used[0], 4); + CHECK_EQ(b->agen_b->prev_slot[0], b->kv[0].slot_mapping[0] - 1); CHECK_EQ(b->num_accepted[0], 1); int32_t st1 = 0, cv1 = 0; state_rows(b, &st1, &cv1);