From b7639e8c9047f83ffe5acce6c889f63e5d243faa Mon Sep 17 00:00:00 2001 From: Chen Bao Date: Thu, 1 Oct 2026 21:25:27 -0500 Subject: [PATCH] prefill: the draft layer's batched K/V for a ring too (KV streaming), and STRATA_DRAFT_TIMING With KV streaming (--kv-resident) the MTP drafter's K/V is a ring of its window (kv_mode 2: page p in slot p % n_slots over a host copy), and draft_kv refused it, so every prompt chunk fell back to the drafter's own pass - one graph per few rows. The batched path takes the ring with its own page table and host copy (as a streamed main layer appends); the cells it writes are those the window can still reach, which the ring holds, and one batch never spans more than the ring's slots, so no two cells of a batch share a slot. STRATA_MTP_BATCH_RING=0: the drafter's own pass (A/B). STRATA_DRAFT_TIMING=1 (debug): the pass's time, synced. --- src/prefill/prefill.cpp | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp index d292e4ff..ad831d00 100644 --- a/src/prefill/prefill.cpp +++ b/src/prefill/prefill.cpp @@ -811,9 +811,14 @@ bool Prefill::draft_kv(core::MtpDrafter& mtp, const float* R_rows, const int32_t std::string& err) { Impl& m = *impl_; static const bool off = [] { const char* v = std::getenv("STRATA_MTP_BATCH"); return v != nullptr && v[0] == '0'; }(); + // A ring (KV streaming: the drafter's window, page p in slot p % n_slots over a host copy) takes the same appends + // with its own page table and host copy, as a streamed main layer does; the cells written are those the window can + // still reach (r0 below), which the ring holds, so no two of them share a slot. STRATA_MTP_BATCH_RING=0: the + // drafter's own pass for a ring (the A/B). + static const bool ring_ok = [] { const char* v = std::getenv("STRATA_MTP_BATCH_RING"); return v == nullptr || v[0] != '0'; }(); core::QsaState& st = mtp.kv_state_rw(); - if (off || n <= 0 || m.g == nullptr || m.region == nullptr || st.kv_mode != 0 || st.kv_hybrid || - mtp.device() != m.device) + if (off || n <= 0 || m.g == nullptr || m.region == nullptr || (st.kv_mode != 0 && !(st.kv_mode == 2 && ring_ok)) || + st.kv_hybrid || mtp.device() != m.device) return false; const auto t0 = Clock::now(); const core::ModelGeometry& g = *m.g; @@ -846,7 +851,9 @@ bool Prefill::draft_kv(core::MtpDrafter& mtp, const float* R_rows, const int32_t const bool q8 = !f16_only && mmq::built() && mmq::fits(kQ8_0, Nn) && mmq::fits(kQ8_0, KV); // #420 const uint64_t per_row = 4 * (2 * Nn + 4 * HCN + LR + HC + Nn + 2 * KV + 1) + 2 * (Nn + 2 * HCN + LR + Nn) + 64 + (q8 ? (uint64_t) mmq::q8_bytes(g.hc, Nn) + 4 * g.hc : 0); - const int64_t B = std::min(n - r0, (int64_t) (m.region_bytes / per_row) & ~(int64_t) 63); + int64_t B = std::min(n - r0, (int64_t) (m.region_bytes / per_row) & ~(int64_t) 63); + if (st.kv_mode == 2) // a ring: one batch's cells must not share a slot (a batch can straddle one page more) + B = std::min(B, ((st.n_slots - 1) * strata::kernels::qsa_real_shapes().page_size) & ~(int64_t) 63); if (B < 64) return false; uint8_t* q = m.region; auto carve = [&](size_t bytes) { void* p = q; q += (bytes + 255) & ~(size_t) 255; return p; }; @@ -871,7 +878,12 @@ bool Prefill::draft_kv(core::MtpDrafter& mtp, const float* R_rows, const int32_t int32_t* ident = q8 ? (int32_t*) carve((size_t) B * g.hc * 4) : nullptr; int32_t* bnd = q8 ? (int32_t*) carve(16) : nullptr; if ((uint64_t) (q - m.region) > m.region_bytes) return false; + static const bool timing = std::getenv("STRATA_DRAFT_TIMING") != nullptr; // debug: where this pass's time goes + if (timing) cudaStreamSynchronize(m.cs); + const auto ti0 = Clock::now(); if (!mtp.idle(err)) return false; // the drafter's own stream (its graph uploads) before this writes its K/V + const double ms_idle = ms_since(ti0); + const auto tl0 = Clock::now(); strata::kernels::QsaShapes s = strata::kernels::qsa_real_shapes(); s.n_head = g.n_head; s.n_head_kv = g.n_head_kv; s.head_dim = g.head_dim; s.idx_n_head = g.idx_q_heads; s.idx_dim = g.idx_key_dim; @@ -951,6 +963,10 @@ bool Prefill::draft_kv(core::MtpDrafter& mtp, const float* R_rows, const int32_t return false; } mtp.ms_prefill += ms_since(t0); + if (timing) + std::fprintf(stderr, "strata draft kv: %lld cells from %lld (first needed %lld), batch %lld: drafter idle %.1f ms, " + "the batches %.1f ms, all %.1f ms\n", (long long) n, (long long) cell0, (long long) (cell0 + r0), + (long long) B, ms_idle, ms_since(tl0), ms_since(t0)); return true; } void Prefill::set_pinned_share(double share) { g_pinned_share = share; }