From b2228c176b991709df1e135751d979e81365dc0b Mon Sep 17 00:00:00 2001 From: Code000 Date: Tue, 17 Nov 2026 09:26:24 +0100 Subject: [PATCH] sched: size the pinned staging ring for 16 builds instead of 3 A speculative step makes four builds (history pass, draft rounds 2-3, the next verify). With room for three, the next verify's reserve() waited in retire_oldest() on one of the current step's draft-pass uploads, so issue-ahead returned only ~0.1-0.5 ms before the card needed the step and every host hiccup became card idle. The ring now holds kRingBuilds = 16 worst-case builds (it must stay below kFlights). On Qwen3.8-Flash-Next IQR-V4 (MTP chain 3) step_next returns 11-13 ms ahead, and the card's idle time before the verify drops from 317 to 74 us (from 1243 to 470 us mean under host load). The pinned host ring grows from 753 KiB to 3.9 MiB; the device slab is unchanged. Outputs are identical. Co-Authored-By: Claude Opus 5.5 --- core/sched/batch.cpp | 10 +++++++--- core/sched/batch.h | 2 ++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/core/sched/batch.cpp b/core/sched/batch.cpp index 8d9fd773..4d54f9dd 100644 --- a/core/sched/batch.cpp +++ b/core/sched/batch.cpp @@ -214,13 +214,17 @@ int BatchBuilder::alloc_set(Set& s, const std::vector& groups) { } RAD_TRY(rad_dev_set(ranks_[0].device)); - /* The ring holds three worst-case builds before a long-context prefill waits on an upload two - * builds back. A decode build's span is far smaller, so hundreds fit. */ + /* The ring holds kRingBuilds worst-case builds. A speculative step takes four or more builds + * (history pass, draft rounds, the next verify), and reserve() must not wait on an upload of + * the step being issued (that would hold step_next back until the card ran this step's draft + * uploads): two steps of builds fit with room over. A decode build's span is far smaller than + * the worst case, so many more fit; this is pinned host memory only, the device slab stays + * one build. */ int64_t worst_pay = 0; for (const Group& g : s.groups) worst_pay += max_seqs_ * g.max_blocks * (int64_t)sizeof(int32_t); const int64_t worst_ranges = worst_pay / k_range_max + max_seqs_ * (int64_t)s.groups.size() + 1; ranges_.reserve((size_t)worst_ranges); - ring_.bytes = 3 * (s.host_bytes + worst_ranges * (int64_t)sizeof(RadCopyRange) + k_carve_align); + ring_.bytes = kRingBuilds * (s.host_bytes + worst_ranges * (int64_t)sizeof(RadCopyRange) + k_carve_align); ring_.host = static_cast(rad_dev_alloc(ring_.bytes, RAD_MEM_HOST_PINNED)); if (!ring_.host) { RAD_ERR("step batch staging: could not reserve %s of pinned host staging", diff --git a/core/sched/batch.h b/core/sched/batch.h index 8cada3c5..e3278de1 100644 --- a/core/sched/batch.h +++ b/core/sched/batch.h @@ -259,6 +259,8 @@ private: int ev = -1; /* row of ev_ holding one event per rank */ }; static constexpr int kFlights = 64; + /* Worst-case builds the staging ring holds (see alloc_set). Must stay below kFlights. */ + static constexpr int kRingBuilds = 16; struct Ring { char* host = nullptr; int64_t bytes = 0;