diff --git a/abi/rad_builder.h b/abi/rad_builder.h index f1911886..5bd81017 100644 --- a/abi/rad_builder.h +++ b/abi/rad_builder.h @@ -1933,6 +1933,12 @@ int rad_arch_declare(RadBuilder* b, const RadModelMeta* meta, const RadB struct RadCtx; struct RadBatch; void rad_arch_step(struct RadCtx* c, const struct RadBatch* batch); +/* Optional. Return a stable fingerprint of actual dispatch geometry whose grid depends on live + * per-group seqused values. `kv_used_max_h[g]` is the host maximum for batch->kv[g], and + * rad_ctx_wgp_count(c) supplies the device's WGP count. Return 0 when exact geometry is unknown; + * that pass runs live and is not recorded. */ +uint64_t rad_arch_pass_key(struct RadCtx* c, const struct RadBatch* batch, + const int32_t* kv_used_max_h); #ifdef __cplusplus } /* extern "C" */ diff --git a/abi/rad_prefill_split.h b/abi/rad_prefill_split.h new file mode 100644 index 00000000..9d06442d --- /dev/null +++ b/abi/rad_prefill_split.h @@ -0,0 +1,55 @@ +/* rad_prefill_split.h -- host-pure live-KV prefill split law shared by libr4d launchers and + * architecture pass-key callbacks. Keep this independent of HIP so callbacks can run before tape + * lookup without a device copy or launch. + */ +#pragma once + +#include +#include + +/* `bq` query positions per workgroup, `tile` keys per KV tile. `units` counts the workgroups that + * do work: a ragged batch's grid is sized from its longest sequence, but a shorter sequence's + * tiles past its end exit at once. Each split keeps at least eight tiles of the supplied context. + */ +static inline int rad_prefill_splits_law(int bq, int tile, int q_len, int total_q, int num_seqs, + int kv_heads, int max_ctx, int wgps) { + const long n = std::max(1, num_seqs); + const long qt = std::min(n * ((std::max(1, q_len) + bq - 1) / bq), + (std::max(1, total_q) + (long)(bq - 1) * n) / bq); + const long units = (long)std::max(1, kv_heads) * std::max(1L, qt); + if (units >= wgps) return 1; + const int s1 = (int)(wgps / units), s2 = (int)(2L * wgps / units); + int sp = s2 > 2 * s1 ? s2 : s1; + const int cap = ((max_ctx > 0 ? max_ctx : (1 << 30) - 1) + tile - 1) / tile / 8; + sp = std::min(sp, std::max(1, cap)); + return std::max(1, std::min(sp, 64)); +} + +/* The launcher's live-context refinement of the initial model-bound law. */ +static inline int rad_prefill_splits_for_live(int bq, int tile, int q_len, int total_q, + int num_seqs, int kv_heads, int max_ctx, + int used_ctx, int wgps) { + int splits = rad_prefill_splits_law(bq, tile, q_len, total_q, num_seqs, kv_heads, max_ctx, + wgps); + if (splits > 1 && used_ctx > 0 && (max_ctx <= 0 || used_ctx < max_ctx)) + splits = rad_prefill_splits_law(bq, tile, q_len, total_q, num_seqs, kv_heads, used_ctx, + wgps); + return splits; +} + +/* Fingerprint one actual launch tuple in packet-global sizes. Use this instead of the raw context + * so equal split grids share a recording key as live context advances. */ +static inline uint64_t rad_prefill_grid_key(int q_len, int bq, int splits, int kv_heads, + int num_seqs, int threads) { + const uint64_t gx = (uint64_t)((std::max(1, q_len) + bq - 1) / bq) * + (uint64_t)std::max(1, splits) * (uint64_t)threads; + const uint64_t gy = (uint64_t)std::max(1, kv_heads); + const uint64_t gz = (uint64_t)std::max(1, num_seqs); + uint64_t h = 0x9e3779b97f4a7c15ull; + const uint64_t grid[3] = {gx, gy, gz}; + for (uint64_t v : grid) { + h ^= v + 0x9e3779b97f4a7c15ull + (h << 6) + (h >> 2); + h *= 0xbf58476d1ce4e5b9ull; + } + return h | 1; +} diff --git a/abi/rad_runtime.h b/abi/rad_runtime.h index 3019c8ba..49dbd67d 100644 --- a/abi/rad_runtime.h +++ b/abi/rad_runtime.h @@ -330,6 +330,10 @@ typedef struct RadBatch { /* A shape generator's pass (rad_declare_shape_generator; `gen` is one of its * RAD_GEN_PASS_S*): its payload. Null on every other step. */ const struct RadShapeGenBatch* sgen_b; + + /* ---- appended in ABI 34: host-side max seqused per entry of kv[], for an architecture's + * optional recorded-pass geometry key. Null when the batch has no host summary. */ + const int32_t* kv_used_max_h; } RadBatch; /* What a generator pass carries beside the step batch's own fields, which keep their meanings: @@ -904,6 +908,9 @@ int rad_kv_stage_layer(RadCtx* c, rad_kvgroup group, const RadKVGrou int rad_kv_stage_next(RadCtx* c, rad_kvgroup group, int layer); int rad_rank(RadCtx* c); int rad_world_size(RadCtx* c); +/* Workgroups the current rank's card can run concurrently (HIP multiProcessorCount on RDNA; + * 32 on the host backend or when the backend has no positive count). */ +int rad_ctx_wgp_count(RadCtx* c); RadStream rad_stream(RadCtx* c); /* The routing result for a MoE layer, resolved on device against the current residency table. diff --git a/arch/qwen4exp_fp8/qwen4exp_fp8.cpp b/arch/qwen4exp_fp8/qwen4exp_fp8.cpp index d5cca086..59dbe537 100644 --- a/arch/qwen4exp_fp8/qwen4exp_fp8.cpp +++ b/arch/qwen4exp_fp8/qwen4exp_fp8.cpp @@ -88,6 +88,7 @@ #include #include +#include #include #include #include @@ -1696,6 +1697,44 @@ static void step(RadCtx* c, const RadBatch* batch) { * acceptance stays high at every depth, but each extra verify row costs real time because the * weights do not fit device memory (more experts streamed, more n-gram rows faulted from the * mapped table). */ +uint64_t pass_key(RadCtx* c, const RadBatch* batch, const int32_t* used_max_h) { + if (!c || !batch) return 0; + if (batch->enc) return 1; + /* MTP owns another attention path on the same KV group. The present batch key cannot prove + * whether that head's attention row takes a split grid, so keep only those passes live. */ + if (batch->draft_pass != 0) return 0; + if (batch->phase != RAD_PHASE_PREFILL) return 1; + + Model& m = g_model[rad_rank(c)]; + bool has_full_attention = false; + for (const Layer& layer : m.layers) { + if (!layer.full) continue; + if (layer.attn.kv_block != 4) return 0; + has_full_attention = true; + } + if (!has_full_attention) return 1; + if (m.g.head_dim != 256 || (m.g.gqa() != 6 && m.g.gqa() != 12) || !m.kv_full || + !used_max_h || !batch->kv || batch->n_kv_groups <= 0) + return 0; + + int group = -1; + for (int i = 0; i < batch->n_kv_groups; ++i) + if (batch->kv[i].group == m.kv_full) { group = i; break; } + if (group < 0) return 0; + + const int q_len = (int)batch->max_q_len; + const int total_q = (int)batch->n_tok; + const int num_seqs = (int)batch->n_seq; + const int kv_heads = (int)m.g.n_head_kv; + const int used = used_max_h[group]; + const int wgps = rad_ctx_wgp_count(c); + constexpr int tile = 48, nwarps = 24; + const int bq = nwarps * 16 / m.g.gqa(); + const int splits = rad_prefill_splits_for_live(bq, tile, q_len, total_q, num_seqs, + kv_heads, batch->max_ctx_len, used, wgps); + return rad_prefill_grid_key(q_len, bq, splits, kv_heads, num_seqs, nwarps * 32); +} + static int probe(const RadModelMeta* meta, RadArchProbe* out) { if (!meta || !out) return RAD_E_INVAL; /* The gated attention block declares the streamed store and the resolve @@ -1728,4 +1767,8 @@ RAD_ARCH_PLUGIN(qwen4exp_fp8, "qwen4exp", "", "0.1.0", "Qwen4-Exp (Qwen3.8-Flash-Next): hybrid Gated DeltaNet / gated attention, a gated " "residual four streams wide and 512 routed experts, from a dense bf16 checkpoint " "quantised ahead of time by a recipe") +extern "C" uint64_t rad_arch_pass_key(RadCtx* c, const RadBatch* batch, + const int32_t* used_max_h) { + return qwen4exp_fp8::pass_key(c, batch, used_max_h); +} #endif diff --git a/core/engine.h b/core/engine.h index 1fc219ab..2e555ce7 100644 --- a/core/engine.h +++ b/core/engine.h @@ -169,6 +169,7 @@ private: std::vector meta_kv_kp_, meta_kv_vp_; void (*arch_step_)(RadCtx*, const RadBatch*) = nullptr; + uint64_t (*arch_pass_key_)(RadCtx*, const RadBatch*, const int32_t*) = nullptr; int load_plugins(); int read_metadata(); diff --git a/core/engine_bringup.cpp b/core/engine_bringup.cpp index 4e4a4e89..d23cc0cc 100644 --- a/core/engine_bringup.cpp +++ b/core/engine_bringup.cpp @@ -968,6 +968,7 @@ int Engine::load_plugins() { const Plugin* ap = rad_tools_registry().plugin(plugins_.arch_plugin); arch_step_ = ap ? ap->arch_step : nullptr; + arch_pass_key_ = ap ? ap->arch_pass_key : nullptr; return RAD_OK; } @@ -3866,6 +3867,7 @@ int Engine::start() { /* Null for all-in-VRAM: the mover never runs and lookup uses WeightInfo::ptr. */ d.residency = r->plan.strategy == Strategy::AllVram ? nullptr : r->mover.residency(); d.arch_step = arch_step_; + d.arch_pass_key = arch_pass_key_; d.profile_ops = cfg_.profile_ops; bind_kv_pools(r->program, [&](int32_t g) { return r->kv.plan(g); }, &d.kv); d.registry = &rad_tools_registry(); diff --git a/core/plugin/loader.cpp b/core/plugin/loader.cpp index 2ebcde11..0a1d059f 100644 --- a/core/plugin/loader.cpp +++ b/core/plugin/loader.cpp @@ -217,6 +217,8 @@ int Registry::commit_(Staged& s, int order) { p.arch_declare = dl_fn( s.handle, "rad_arch_declare"); p.arch_step = dl_fn(s.handle, "rad_arch_step"); + p.arch_pass_key = dl_fn( + s.handle, "rad_arch_pass_key"); if (!id_fn || !p.arch_declare || !p.arch_step) { err_("architecture plugin '%s' is missing rad_arch_id, rad_arch_declare or " "rad_arch_step", p.name.c_str()); diff --git a/core/plugin/registry.h b/core/plugin/registry.h index fdced875..3c564e70 100644 --- a/core/plugin/registry.h +++ b/core/plugin/registry.h @@ -82,6 +82,7 @@ struct Plugin { std::string arch_quant; int (*arch_declare)(RadBuilder*, const RadModelMeta*, const RadBuildCtx*) = nullptr; void (*arch_step)(RadCtx*, const RadBatch*) = nullptr; + uint64_t (*arch_pass_key)(RadCtx*, const RadBatch*, const int32_t*) = nullptr; /* Optional (rad_builder.h); null means every question answers zero. Asked before declare, so * model facts that are inputs to declaration are still read by the plugin, not the core. */ int (*arch_probe)(const RadModelMeta*, RadArchProbe*) = nullptr; diff --git a/core/runtime/ctx.cpp b/core/runtime/ctx.cpp index fa8f9d38..bdd48fb1 100644 --- a/core/runtime/ctx.cpp +++ b/core/runtime/ctx.cpp @@ -31,11 +31,15 @@ int Ctx::init(const CtxDesc& d) { registry_ = d.registry; kv_ = d.kv; arch_step_ = d.arch_step; + arch_pass_key_ = d.arch_pass_key; profiling_ = d.profile_ops; /* The rank owns one device. Set it here so allocation lands on the right card, and again from * the rank thread in bind_thread() because the backend's current device is thread-local. */ RAD_TRY(rad_dev_set(device_)); + RadDeviceProps props{}; + if (rad_dev_props(device_, &props) >= 0 && !props.is_host_backend && props.n_cu > 0) + wgp_count_ = props.n_cu; if (d.stream) { stream_ = d.stream; @@ -944,7 +948,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`, `vgen_b` and `sgen_b` are set only on generator passes, which are never * recorded (below). */ -static_assert(sizeof(void*) != 8 || sizeof(RadBatch) == 416, +static_assert(sizeof(void*) != 8 || sizeof(RadBatch) == 424, "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"); @@ -989,6 +993,11 @@ uint64_t Ctx::pass_key(const RadBatch* b) const { /* A streamed group's view by the memory it names, never its address (kvstream_view_key). */ mix(kvstream_view_key(k.stream)); } + if (arch_pass_key_) { + const uint64_t geometry = arch_pass_key_(const_cast(this), b, b->kv_used_max_h); + if (!geometry) return 0; + mix(geometry); + } mix((uint64_t)level_); mix(dense_epoch_); mix(domain_epoch_.load(std::memory_order_relaxed)); diff --git a/core/runtime/ctx.h b/core/runtime/ctx.h index de3bedfd..ee40bc0c 100644 --- a/core/runtime/ctx.h +++ b/core/runtime/ctx.h @@ -192,6 +192,7 @@ struct CtxDesc { RadStream stream = nullptr; /* null: the Ctx creates its own compute stream */ ResidencyTable* residency = nullptr; /* null in the all-in-VRAM case */ ArchStepFn arch_step = nullptr; /* the architecture plugin's rad_arch_step */ + uint64_t (*arch_pass_key)(RadCtx*, const RadBatch*, const int32_t*) = nullptr; bool profile_ops = false; /* spec ยง16, and it changes what you measure */ /* Whether a repeated pass may be recorded and played back. False for a caller whose pass * issues work the batch does not describe (e.g. a test driving another component through @@ -256,6 +257,7 @@ public: } /* This rank's device ordinal, for the calls that name a card rather than a stream. */ int device() const { return device_; } + int wgp_count() const { return wgp_count_; } int world_size() const { return world_size_; } RadStream stream() const { return stream_; } Program* program() const { return program_; } @@ -653,6 +655,8 @@ private: bool staging_ = false; /* the stager armed for the pass being issued */ int issue_body(rad_op h, const RadOperand* opd, int n_opd, int64_t n); ArchStepFn arch_step_ = nullptr; + uint64_t (*arch_pass_key_)(RadCtx*, const RadBatch*, const int32_t*) = nullptr; + int wgp_count_ = 32; /* The expert prefetch's state (xp_state.h), null while it is off. */ std::unique_ptr xp_; diff --git a/core/runtime/issue.cpp b/core/runtime/issue.cpp index 3ffbfef0..bb129afe 100644 --- a/core/runtime/issue.cpp +++ b/core/runtime/issue.cpp @@ -1280,6 +1280,9 @@ int rad_kv_layer_index(RadCtx* c, rad_kvgroup group, int layer) { } int rad_rank(RadCtx* c) { return c ? static_cast(c)->rank() : 0; } int rad_world_size(RadCtx* c) { return c ? static_cast(c)->world_size() : 1; } +int rad_ctx_wgp_count(RadCtx* c) { + return c ? static_cast(c)->wgp_count() : 32; +} RadStream rad_stream(RadCtx* c) { return c ? static_cast(c)->stream() : nullptr; } /* The second lane. `rad_lane` selects the lane subsequent issues launch on; `rad_lane_join` diff --git a/core/sched/batch.cpp b/core/sched/batch.cpp index 06d16d02..6b0e6130 100644 --- a/core/sched/batch.cpp +++ b/core/sched/batch.cpp @@ -245,6 +245,7 @@ int BatchBuilder::alloc_set(Set& s, const std::vector& groups) { o.kv.assign(ranks_.size(), std::vector(s.groups.size())); o.stream.assign(ranks_.size(), std::vector(s.groups.size())); o.batch.assign(ranks_.size(), RadBatch{}); + o.kv_used_max_h.assign(s.groups.size(), 0); o.seqs.clear(); o.seqs.reserve((size_t)max_seqs_); /* a build fills it without allocating */ o.has_seqs = false; @@ -814,6 +815,10 @@ int BatchBuilder::build_groups(const StepPlan& plan, const IKVManager& kv, Set& (int)g.sidx_pitch); } } + int32_t group_used = 0; + for (int64_t i = 0; i < n_seq; ++i) + group_used = std::max(group_used, g.h_used[i]); + o.kv_used_max_h[gi] = group_used; /* A state group's rows are one slot per sequence: n_seq words, restaged whole. */ if (!L.paged) { stage(g.bt_fix, np, n_seq); @@ -1226,6 +1231,7 @@ const RadBatch* BatchBuilder::build(const StepPlan& plan, const IKVManager& kv, B.max_q_len_decode = max_q_dec; B.max_q_len_prefill = max_q_pf; B.max_ctx_len = (int32_t)ctx_bound; + B.kv_used_max_h = o.kv_used_max_h.data(); B.n_kv_groups = (int)o.kv[r].size(); B.kv = o.kv[r].data(); B.draft_pass = plan.draft_pass; diff --git a/core/sched/batch.h b/core/sched/batch.h index 27c93c62..52f9b8e6 100644 --- a/core/sched/batch.h +++ b/core/sched/batch.h @@ -265,6 +265,7 @@ private: /* Per rank and group, the RadKVStream a batch names: each rank's own copy, outside the slab. */ std::vector> stream; std::vector batch; + std::vector kv_used_max_h; /* The build's sequences in row order (step_seqs); none for an encoder pass. */ std::vector seqs; bool has_seqs = false; diff --git a/libr4d/r4d_attn_prefill_split.h b/libr4d/r4d_attn_prefill_split.h index 1b367708..e37ccafb 100644 --- a/libr4d/r4d_attn_prefill_split.h +++ b/libr4d/r4d_attn_prefill_split.h @@ -13,6 +13,8 @@ // Such launches split the KV and finish in the split-KV merge. #pragma once +#include "rad_prefill_split.h" + // Workgroups the card runs at once: one a WGP. HIP's multiProcessorCount counts WGPs on RDNA (32 on // the R9700, see want_wg in r4d_hc_bf16.hip); 32 if it cannot be read. static inline int r4d_prefill_wgps() { @@ -43,16 +45,7 @@ static inline int r4d_prefill_wgps() { // the vectorised merge's limit. static inline int r4d_prefill_splits_law(int bq, int tile, int q_len, int total_q, int num_seqs, int kv_heads, int max_ctx, int wgps) { - const long n = std::max(1, num_seqs); - const long qt = std::min(n * ((std::max(1, q_len) + bq - 1) / bq), - (std::max(1, total_q) + (long)(bq - 1) * n) / bq); - const long units = (long)std::max(1, kv_heads) * std::max(1L, qt); - if (units >= wgps) return 1; - const int s1 = (int)(wgps / units), s2 = (int)(2L * wgps / units); - int sp = s2 > 2 * s1 ? s2 : s1; - const int cap = ((max_ctx > 0 ? max_ctx : (1 << 30) - 1) + tile - 1) / tile / 8; - sp = std::min(sp, std::max(1, cap)); - return std::max(1, std::min(sp, 64)); + return rad_prefill_splits_law(bq, tile, q_len, total_q, num_seqs, kv_heads, max_ctx, wgps); } // The split count of this unit's last prefill launch, 1 unsplit: read by the unit's test hook, which @@ -92,8 +85,9 @@ static inline int r4d_prefill_splits_want(const RadArgs* a, const R4DArgs* r, in wgps); if (sp <= 1) return 1; const int used = r4d_prefill_used_ctx(r); - if (used > 0 && (r->max_ctx <= 0 || used < r->max_ctx)) - sp = r4d_prefill_splits_law(bq, tile, r->q_len, qtot, r->num_seqs, r->kv_heads, used, wgps); + if (used > 0) + sp = rad_prefill_splits_for_live(bq, tile, r->q_len, qtot, r->num_seqs, r->kv_heads, + r->max_ctx, used, wgps); return sp; } diff --git a/tests/runtime_test.cpp b/tests/runtime_test.cpp index f6822d88..b9f160be 100644 --- a/tests/runtime_test.cpp +++ b/tests/runtime_test.cpp @@ -11,6 +11,7 @@ */ #include "rad_test.h" #include "host_kargs_main.h" +#include "rad_prefill_split.h" #include "device/device.h" #include "runtime/arena.h" @@ -234,12 +235,14 @@ struct Fixture { return (rad_op)(P.ops.size() - 1); } - int start(rad::ResidencyTable* res = nullptr, bool profile = false, bool record = false) { + int start(rad::ResidencyTable* res = nullptr, bool profile = false, bool record = false, + uint64_t (*pass_key)(RadCtx*, const RadBatch*, const int32_t*) = nullptr) { CtxDesc d; d.program = &P; d.rank = 1; d.world_size = 2; d.arch_step = arch_step_trampoline; + d.arch_pass_key = pass_key; d.residency = res; d.profile_ops = profile; d.kv = kv; @@ -2328,6 +2331,48 @@ TEST(a_program_with_a_calibration_tap_records_no_passes) { * and runtime state a pass's shape or arguments read, and must not change with the step number * alone. A field missing from the key replays stale arguments until the next audit. A pass that * cannot be recorded (encoder, generator, encoder rows) keys to 0. */ +static uint64_t test_prefill_geometry_key(RadCtx* c, const RadBatch* b, const int32_t* used) { + if (!c || !b || !used) return 0; + const int bq = 192, tile = 48, kv_heads = 4, threads = 768; + const int wgps = rad_ctx_wgp_count(c); + const int splits = rad_prefill_splits_for_live(bq, tile, (int)b->max_q_len, + (int)b->n_tok, (int)b->n_seq, kv_heads, + b->max_ctx_len, used[0], wgps); + return rad_prefill_grid_key((int)b->max_q_len, bq, splits, kv_heads, (int)b->n_seq, threads); +} + +TEST(the_prefill_geometry_key_reuses_equal_grids_and_separates_changed_grids) { + Fixture f; + f.add_buffer("x", RAD_BF16, 64, 4, 0); + rad_op op = loose_op(f); + attach_host_row(f, op, k_host_read, "host_read"); + CHECK_OK(f.start(nullptr, false, false, test_prefill_geometry_key)); + + RadKVGroupBatch kv{}; + kv.group = 3; + RadBatch b{}; + b.phase = RAD_PHASE_PREFILL; b.n_tok = 128; b.n_seq = 1; + b.max_q_len = 128; b.max_ctx_len = 4096; b.n_kv_groups = 1; b.kv = &kv; + int32_t used[1] = {2304}; + b.kv_used_max_h = used; + const int split6 = rad_prefill_splits_law(192, 48, 128, 128, 1, 4, used[0], 32); + const uint64_t k6 = f.ctx.pass_key(&b); + used[0] = 2305; + const int same_split = rad_prefill_splits_law(192, 48, 128, 128, 1, 4, used[0], 32); + CHECK_EQ(split6, 6); + CHECK_EQ(same_split, 6); + CHECK_EQ(f.ctx.pass_key(&b), k6); + + used[0] = 3072; + const int split8 = rad_prefill_splits_law(192, 48, 128, 128, 1, 4, used[0], 32); + const uint64_t k8 = f.ctx.pass_key(&b); + CHECK_EQ(split8, 8); + CHECK(k8 != k6); + CHECK_EQ(((128 + 192 - 1) / 192) * split6 * 768, 4608); + CHECK_EQ(((128 + 192 - 1) / 192) * split8 * 768, 6144); + CHECK_EQ(rad_ctx_wgp_count((RadCtx*)&f.ctx), 32); +} + TEST(the_pass_key_changes_with_every_field_a_pass_depends_on) { Fixture f; f.add_buffer("x", RAD_BF16, 64, 4, 0); @@ -2343,7 +2388,8 @@ TEST(the_pass_key_changes_with_every_field_a_pass_depends_on) { std::memset(&b, 0, sizeof b); std::memset(kv, 0, sizeof kv); b.phase = RAD_PHASE_DECODE; b.step = 5; b.n_tok = 4; b.n_seq = 2; b.n_out = 2; - b.max_q_len = 2; b.max_ctx_len = 1024; b.n_kv_groups = 2; b.kv = kv; b.n_spec = 1; + b.max_q_len = 2; b.max_ctx_len = 1024; + b.n_kv_groups = 2; b.kv = kv; b.n_spec = 1; b.n_seq_decode = 2; b.n_tok_decode = 4; b.max_q_len_decode = 2; int k = 0; b.token_ids = arr[k++]; b.positions = arr[k++]; b.cu_seqlens = arr[k++]; diff --git a/tests/sched_test.cpp b/tests/sched_test.cpp index 4da723de..a5b770a4 100644 --- a/tests/sched_test.cpp +++ b/tests/sched_test.cpp @@ -2060,6 +2060,7 @@ TEST(a_mixed_step_has_the_right_boundaries_and_slot_mapping) { CHECK_EQ(g0.block_table[128], btb[0]); CHECK_EQ(g0.seqused[0], 101); CHECK_EQ(g0.seqused[1], 64); + CHECK_EQ(s2->kv_used_max_h[0], std::max(g0.seqused[0], g0.seqused[1])); /* The linear group is per sequence, not per token: every token of a sequence maps to its one * state instance, and the block table row is that slot. */ @@ -6972,8 +6973,14 @@ TEST(a_preempted_ring_sequence_replays_its_output_a_token_a_step) { * nothing. */ int64_t at = 0; for (int k = 0; k < 8 && at < 160; ++k) { - REQUIRE(f.sch.step() != nullptr); + const RadBatch* chunk = f.sch.step(); + REQUIRE(chunk != nullptr); REQUIRE_EQ(f.sch.plan().e.size(), (size_t)1); + const RadKVGroupBatch& paged = chunk->kv[0]; + int32_t used_max = 0; + for (int64_t i = 0; i < chunk->n_seq; ++i) + used_max = std::max(used_max, paged.seqused[i]); + CHECK_EQ(chunk->kv_used_max_h[0], used_max); CHECK_EQ(f.sch.plan().e[0].ctx_len, at); at += f.sch.plan().e[0].n_tokens; CHECK(at <= 160); @@ -6985,6 +6992,7 @@ TEST(a_preempted_ring_sequence_replays_its_output_a_token_a_step) { /* The replayed output token on its own, which samples the next. */ const RadBatch* rb = f.sch.step(); REQUIRE(rb != nullptr); + CHECK_EQ(rb->kv_used_max_h[0], rb->kv[0].seqused[0]); CHECK_EQ(f.sch.plan().e[0].ctx_len, 160); CHECK_EQ(f.sch.plan().e[0].n_tokens, 1); CHECK(f.sch.plan().e[0].produces_token);