From a4845834ea0905bc05162b7c39917b1a12bebad3 Mon Sep 17 00:00:00 2001 From: Deadcode Date: Mon, 5 Oct 2026 18:27:23 +0200 Subject: [PATCH] radiance 1.0.12 - Wrong answers under concurrency on models with gated delta net layers (Qwen3.8-Flash-Next, Qwen3.8-27B, Qwen3.6-35B-A3B). A conv state slot was one entry wider in the allocation than in the state the kernels index, so clearing the slot a newly admitted request was given -- and a prefix-cache checkpoint's save and restore -- reached into the next request's conv and n-gram windows. A request decoding beside an admission then lost part of its recent context: a right first token followed by a context-free continuation ("1. **Identify the numbers..." for "What is 65 times 3?"), repeated or skipped digits. A single request was never affected. One definition of the slot now, and the server refuses to start if the allocation and the kernels ever disagree on it. Present since 1.0.0; reported with a strict concurrent sanity check. - --num-speculative-tokens 0 no longer stops the engine at the first multi-sequence decode step: an all-reduce argument struct's padding was uninitialised, and a recorded pass's audit read it as a changed argument. --- CMakeLists.txt | 2 +- core/build/rad_builder.cpp | 26 +++++++---------- core/mem/kv.cpp | 21 ++++++++++++-- core/rad_core.h | 12 ++++++++ core/sched/batch.cpp | 8 ++--- core/sched/scheduler.cpp | 2 +- libr4d/r4d_ar_oneshot_2rank_exact.hip | 3 ++ libr4d/r4d_ar_oneshot_Nrank_exact.hip | 4 +++ tests/build_test.cpp | 17 ++++++----- tests/mem_test.cpp | 42 +++++++++++++++++++++++++++ 10 files changed, 106 insertions(+), 31 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2b3c6f85..d38fd738 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.21) # VERSION is what /version reports and what a client checks compatibility against. It is NOT the # ABI version -- that lives in abi/rad_abi.h and is read out of the header below, because # a plugin contract and a server release do not move together. -project(radiance VERSION 1.0.11 LANGUAGES C CXX) +project(radiance VERSION 1.0.12 LANGUAGES C CXX) set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) diff --git a/core/build/rad_builder.cpp b/core/build/rad_builder.cpp index fcf5f8f6..153cf42f 100644 --- a/core/build/rad_builder.cpp +++ b/core/build/rad_builder.cpp @@ -1382,22 +1382,18 @@ void Builder::finish_kv_groups() { * (d.state_dim[1] ? d.state_dim[1] : 1)) * n_bound; break; case RAD_KV_CONV: { - /* A rolling window of conv_width-1 + n_spec + 1 entries, so a speculative - * rejection is a change of read offset rather than a recompute (spec §10). The - * window is sized for the largest speculative window this deployment was - * configured for. + /* A rolling window of conv_width-1 + n_spec entries, so a speculative rejection + * is a change of read offset rather than a recompute (spec §10). The window is + * sized for the largest speculative window this deployment was configured for. * - * THE +1 IS THE VERIFY STEP'S OWN OUTPUT, and it is not optional. A verify writes - * 1 + n_spec conv outputs before anyone knows which survive, so peak occupancy is - * the conv_width-1 of retained history PLUS all 1 + n_spec of them. Sizing this - * `conv_width - 1 + max_spec` instead -- six entries at width 4 depth 3 -- puts it - * one short of the modulus BatchBuilder::init and Scheduler::conv_window cycle - * `conv_cursor` by, which is `conv_width - 1 + n_spec + 1`, seven: `(cursor + 1 + - * a) % 7` reaches 6, one past the end of the allocation and into the slot that - * belongs to the next sequence. At n_spec 0 the two agree (modulus conv_width, - * history conv_width-1 plus the one output), so the discrepancy hides there. */ - int64_t entries = (d.conv_width > 0 ? d.conv_width - 1 : 0) + - (prog_.ctx.max_spec > 0 ? prog_.ctx.max_spec : 0) + 1; + * EXACTLY THE STATE TENSOR'S EXTENT, conv_window_entries(), and not one more. The + * kernels lay the slots out from the tensor, so an allocation one entry wider a + * slot puts the manager's slot `s` -- the bytes it clears when it hands the slot + * out, and saves and restores at a checkpoint -- over the start of the kernels' + * slot `s + 1`. Handing a slot to a new sequence then zeroes part of a running + * sequence's window. conv_state_index's cursor cycles by the same number, so it + * stays inside the slot. */ + int64_t entries = conv_window_entries(d, prog_.ctx.max_spec); if (entries <= 0) entries = 1; gi.bytes_per_block = 0; gi.bytes_per_state = diff --git a/core/mem/kv.cpp b/core/mem/kv.cpp index e74c4874..d119b494 100644 --- a/core/mem/kv.cpp +++ b/core/mem/kv.cpp @@ -191,8 +191,7 @@ int KVManager::plan_groups(const std::vector& groups, const Config& * a change of read offset rather than a recompute (spec §10). The engine's * contract is that the kernel pair supports that; the manager's job is to make the * slot big enough for it. */ - p.conv_slots = (g.decl.conv_width > 0 ? g.decl.conv_width - 1 : 0) - + (cfg.n_spec > 0 ? cfg.n_spec : 0); + p.conv_slots = conv_window_entries(g.decl, cfg.n_spec); if (p.conv_slots <= 0) { RAD_ERR("KV group '%s': conv_width %lld with n_spec %d gives 0 slots", p.name.c_str(), (long long)g.decl.conv_width, cfg.n_spec); @@ -231,6 +230,24 @@ int KVManager::plan_groups(const std::vector& groups, const Config& RAD_ERR("KV group '%s': per-sequence state computes to 0", p.name.c_str()); return RAD_E_INVAL; } + /* THE SLOT THE KERNELS INDEX, which is the slot this manager has to hand out. The + * kernels lay slots out from the state tensor ([n_states, channels, conv_slots] or + * [n_states, n_head_kv, state_dim]); this manager lays them out from bytes_per_state, + * and clears, saves and restores slots in that layout. Two different sizes make every + * one of those touch a neighbouring sequence's state. */ + const int64_t indexed = + (p.kind == RAD_KV_CONV + ? p.conv_slots * g.decl.n_head_kv * g.decl.head_dim + : g.decl.n_head_kv * g.decl.state_dim[0] * g.decl.state_dim[1]) + * elt * p.n_layers; + if (p.bytes_per_state != indexed) { + RAD_ERR("KV group '%s': %lld bytes a sequence are allocated, and the state the " + "kernels index is %lld bytes over its %lld layers -- the slot this manager " + "clears, checkpoints and restores would not be the slot the kernels read", + p.name.c_str(), (long long)p.bytes_per_state, (long long)indexed, + (long long)p.n_layers); + return RAD_E_SHAPE; + } /* Exactly one slot per concurrent sequence, times whatever the kernel needs beside * it. Not a heuristic: a sequence owns exactly one recurrent state, so max_seqs is * the count of LOGICAL states, full stop. */ diff --git a/core/rad_core.h b/core/rad_core.h index 2fc73725..f2d4c9df 100644 --- a/core/rad_core.h +++ b/core/rad_core.h @@ -285,6 +285,18 @@ struct KVGroupInfo { std::vector layers; }; +/* A RAD_KV_CONV GROUP'S WINDOW, IN ENTRIES: conv_width - 1 of history and one more for every + * speculative position. It is the last extent of the state tensor the kernels index, and the + * kernels take the layout from that extent -- a slot is `entries` consecutive entries a channel, + * and libr4d's conv update derives the verify step's query length from it as + * entries - (conv_width - 2). So the allocation's slot, the KV manager's view of a slot (its clear + * when one is handed out, a checkpoint's save and restore) and the cursor conv_state_index derives + * all take this one number. A slot sized larger on the manager's side than in the tensor puts + * each of those views across the start of the next sequence's window. */ +inline int64_t conv_window_entries(const RadKVGroupDecl& d, int64_t n_spec) { + return (d.conv_width > 0 ? d.conv_width - 1 : 0) + (n_spec > 0 ? n_spec : 0); +} + /* Everything declare produced. The core owns it; the plugin only ever sees handles into it. */ class Program { public: diff --git a/core/sched/batch.cpp b/core/sched/batch.cpp index b91a8e95..f090bdfc 100644 --- a/core/sched/batch.cpp +++ b/core/sched/batch.cpp @@ -399,8 +399,9 @@ int BatchBuilder::layout(Program& prog, const Config& cfg) { if (max_ctx_ <= 0) max_ctx_ = cfg.max_tok; /* The conv rolling window: width-1 entries of history plus room for the whole speculative - * window, because a verify writes num_spec+1 conv outputs before anything is accepted, and a - * rejection then reads back at the slot the last accepted token left (spec §10). */ + * window, and a rejection then reads back at the slot the last accepted token left (spec §10). + * The state tensor's own extent (conv_window_entries), so the cursor derived from it stays + * inside the slot the kernels index. */ conv_window_.assign(prog.kv_groups.size(), 1); conv_group_ = -1; n_conv_ = 0; @@ -410,8 +411,7 @@ int BatchBuilder::layout(Program& prog, const Config& cfg) { ++n_conv_; conv_group_ = n_conv_ == 1 ? (int32_t)i : -1; /* -1 once there is more than one */ if (g.decl.conv_width > 0) - conv_window_[i] = (int32_t)(g.decl.conv_width - 1 + - (cfg.n_spec > 0 ? cfg.n_spec : 0) + 1); + conv_window_[i] = (int32_t)conv_window_entries(g.decl, cfg.n_spec); } /* ------------------------------------------------------------ derived buffers */ diff --git a/core/sched/scheduler.cpp b/core/sched/scheduler.cpp index 5b4cf9d5..b798d8d0 100644 --- a/core/sched/scheduler.cpp +++ b/core/sched/scheduler.cpp @@ -2089,7 +2089,7 @@ int32_t Scheduler::conv_window() const { int32_t W = 1; for (const KVGroupInfo& g : prog_->kv_groups) if (g.decl.kind == RAD_KV_CONV && g.decl.conv_width > 0) - W = (int32_t)(g.decl.conv_width - 1 + (cfg_.n_spec > 0 ? cfg_.n_spec : 0) + 1); + W = (int32_t)conv_window_entries(g.decl, cfg_.n_spec); return W; } diff --git a/libr4d/r4d_ar_oneshot_2rank_exact.hip b/libr4d/r4d_ar_oneshot_2rank_exact.hip index 41274888..ce44cd95 100644 --- a/libr4d/r4d_ar_oneshot_2rank_exact.hip +++ b/libr4d/r4d_ar_oneshot_2rank_exact.hip @@ -449,7 +449,10 @@ int r4d_ar_gather_hc_write(long peer_scratch, long my_scratch, long peer_flags, int nt = (int)nthreads; if (nt < 64) nt = 64; if (nt > 1024) nt = 1024; + /* Zeroed whole, padding included: the struct is passed by value, so its tail padding is + * kernel-argument bytes too, and a recorded pass's audit compares those bytes. */ R4dArGather G; + std::memset(&G, 0, sizeof G); G.ye = (const unsigned short*)ye; G.ew = (const unsigned short*)ew; G.sorted = (const int*)sorted; diff --git a/libr4d/r4d_ar_oneshot_Nrank_exact.hip b/libr4d/r4d_ar_oneshot_Nrank_exact.hip index ebf329ae..944f90f9 100644 --- a/libr4d/r4d_ar_oneshot_Nrank_exact.hip +++ b/libr4d/r4d_ar_oneshot_Nrank_exact.hip @@ -15,6 +15,7 @@ // // The IPC helpers live in r4d_ar_oneshot_2rank_exact.hip; this unit links beside it. #include "r4d.h" +#include #include "r4d_ar_wide.h" #include "r4d_ar_hc.h" // the gather and hc write words, shared with the two-rank fused forms #include @@ -521,7 +522,10 @@ static void ar_gather_hc_write_wide_launch( r4d_ar_check_extent(peer_flags[i], flag_bytes, "ar_gather_hc_write", "peer flags"); } + /* Zeroed whole, padding included: the struct is passed by value, so its tail padding is + * kernel-argument bytes too, and a recorded pass's audit compares those bytes. */ R4dArGather G; + std::memset(&G, 0, sizeof G); G.ye = (const unsigned short*)ye; G.ew = (const unsigned short*)ew; G.sorted = (const int*)sorted; diff --git a/tests/build_test.cpp b/tests/build_test.cpp index 7e4b0320..f11c687e 100644 --- a/tests/build_test.cpp +++ b/tests/build_test.cpp @@ -773,15 +773,16 @@ TEST(linear_state_is_sized_per_sequence) { CHECK_EQ(b.program().kv_groups[gl].bytes_per_state, 32LL * 128 * 128 * 4); CHECK_EQ(b.program().kv_groups[gl].bytes_per_block, 0); - /* A rolling window of conv_width-1 + n_spec + 1 entries, so a speculative rejection is a - * change of read offset rather than a recompute (spec §10). + /* A rolling window of conv_width-1 + n_spec entries, so a speculative rejection is a change of + * read offset rather than a recompute (spec §10). * - * THE +1 IS THE VERIFY STEP'S OWN OUTPUT. A verify writes 1 + n_spec conv outputs before - * anyone knows which survive, so peak occupancy is the retained history PLUS all of them -- - * which is the modulus BatchBuilder::init and Scheduler::conv_window cycle the cursor by. A - * state sized without it is overrun by one entry every verify. rad_builder.cpp's own note on - * `entries` is the argument. */ - CHECK_EQ(b.program().kv_groups[gc].bytes_per_state, 32LL * 128 * (4 - 1 + 4 + 1) * 4); + * EXACTLY THE STATE TENSOR'S EXTENT. The kernels lay a slot out from the tensor's last extent, + * conv_width-1 + n_spec, and the KV manager clears, checkpoints and restores the slot this + * figure describes; one entry more here put each of those over the start of the next + * sequence's window. rad_builder.cpp's note on `entries` is the argument. */ + CHECK_EQ(b.program().kv_groups[gc].bytes_per_state, 32LL * 128 * (4 - 1 + 4) * 4); + CHECK_EQ(b.program().kv_groups[gc].bytes_per_state, + 32LL * 128 * conv_window_entries(cd, 4) * 4); } /* ================================================================== names, notes, the C surface */ diff --git a/tests/mem_test.cpp b/tests/mem_test.cpp index 915bd1f4..d66e6a41 100644 --- a/tests/mem_test.cpp +++ b/tests/mem_test.cpp @@ -731,6 +731,48 @@ TEST(kv_conv_slot_is_a_rolling_window) { CHECK_EQ(kv.free_states(G_CONV), s0); } +/* THE SLOT THE MANAGER HANDS OUT IS THE SLOT THE KERNELS INDEX. The kernels take a conv slot's + * layout from the state tensor's last extent, conv_width-1 + n_spec; the manager clears a slot + * when it hands it out and saves and restores it at a checkpoint in the layout bytes_per_state + * describes. A builder figure one entry wider a slot -- what the allocation once carried -- + * made handing slot s to a new sequence zero the start of slot s+1's window, the window of a + * sequence still decoding. The manager refuses the disagreement rather than serve with it. */ +TEST(kv_refuses_a_state_slot_the_kernels_would_not_index) { + auto group = [](int kind, int64_t bytes_per_state) { + KVGroupInfo k; + k.name = "state"; + k.decl.kind = kind; + k.decl.dtype = RAD_BF16; + k.decl.n_head_kv = 2; + k.decl.head_dim = 8; + k.decl.state_dim[0] = 4; + k.decl.state_dim[1] = 8; + k.decl.conv_width = 4; + k.layers = { 0, 1 }; + k.bytes_per_state = bytes_per_state; + return k; + }; + auto configure = [](const KVGroupInfo& k) -> int { + std::vector g; + g.push_back(null_group()); + g.push_back(k); + Config cfg = small_config(); + cfg.n_spec = 3; + cfg.checkpoint_slots = 0; + Pools p; + if (p.configure(cfg, 0, &rad_malloc_allocator(), &rad_malloc_allocator()) < 0) + return RAD_E_NOMEM; + KVManager kv; + return kv.configure(g, cfg, p.kv()); + }; + /* Conv: (4-1) + 3 = 6 entries of 2 x 8 bf16 channels, over two layers. */ + CHECK_OK(configure(group(RAD_KV_CONV, 6LL * 2 * 8 * 2 * 2))); + CHECK_EQ(configure(group(RAD_KV_CONV, 7LL * 2 * 8 * 2 * 2)), RAD_E_SHAPE); + /* Linear: 2 heads of 4 x 8 bf16, over two layers. */ + CHECK_OK(configure(group(RAD_KV_LINEAR, 2LL * 4 * 8 * 2 * 2))); + CHECK_EQ(configure(group(RAD_KV_LINEAR, 2LL * 4 * 8 * 2 * 2 + 64)), RAD_E_SHAPE); +} + TEST(kv_adopts_the_blocks_a_cache_hit_found) { Fixture f; CHECK_OK(f.build(/*with_linear=*/false));