From 73d3983046ea70bddd30d3085c2e41f07207727d Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Tue, 6 Oct 2026 13:25:15 +0200 Subject: [PATCH] prefill: a backstop for the fused layout's MoE buffers - an error naming the layer, not a hang (#954, #958) fused_layout() and fused_ring() already keep the shrunk GU/H/Xq buffers and the per-layer expert path in step. If they ever drift (a layer that takes MMQ or the FP16 path at a chunk of stream_all_min() tokens or more while the buffers are the fused ones), the prompt now stops with 'the fused layout's MoE buffers are too small for layer N' instead of writing past the buffer: an illegal access, or a kernel that never returns and shows up as 'prefill mmq: iota' one prompt later. Same check in the SYCL copy. Co-authored-by: Krypto-Whitehat <41930040+Krypto-Whitehat@users.noreply.github.com> --- src/prefill/prefill.cpp | 15 +++++++++++++++ sycl/src/prefill/prefill.cpp | 15 +++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp index ad8cf511..2d97f6c8 100644 --- a/src/prefill/prefill.cpp +++ b/src/prefill/prefill.cpp @@ -595,6 +595,9 @@ struct Prefill::Impl { uint16_t* dq_d[DQ] = {}; uint8_t* stage_dev[RING_MAX] = {}; int ring = STAGE; // the slots of this layout's ring (ring_slots) + // this layout's GU, H and Xq are the fused path's (moe_bufs' `fused`), so a layer that runs MMQ or the FP16 path may + // only write stream_all_min() - 1 of its rows into them; fused_layout() keeps the two in step (#583, #954) + bool fused_bufs = false; std::unique_ptr stager; // the unpinned experts' host copies (step 4) cudaEvent_t copied[RING_MAX] = {}, used[RING_MAX] = {}; bool stage_live[RING_MAX] = {}; @@ -962,6 +965,7 @@ bool Prefill::carve(size_t T, void* alloc) { { // one region for the attention half's and the MoE half's scratch (see gdn_set_bytes) const bool fz = fused_layout(T, m.src != nullptr); + m.fused_bufs = fz; const MoeBufs mb = moe_bufs(T, m.g->n_expert, fz); const uint64_t region = std::max({gdn_set_bytes(T), qsa_set_bytes(T, m.cap, m.max_blocks, m.sel_batch, m.attn_batch, s), moe_set_bytes(T, m.g->n_expert, fz)}); @@ -2518,6 +2522,17 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri const bool fused_only = mmq_plan().any && mmq_plan().fo[(size_t) l]; const bool fused_nat = (use_mmq || fused_only) && stream_all && no_peer && lay.native && fused::native_supported(mmq_gt, mmq_dt); const bool fused_l = (use_mmq && stream_all && no_peer && !lay.native && fused::enabled()) || fused_nat; + // #583 / #954: the fused layout's GU/H/Xq hold the grouping tables and the int8 rows, sized for + // stream_all_min() - 1 tokens of MMQ's rows, so a layer that takes MMQ or the FP16 path at the FULL + // chunk would write T*K rows into a (stream_all_min() - 1)*K-row buffer: an illegal access, or a kernel + // that never returns (the first ck() to see it is `prefill mmq: iota`, one prompt later). fused_layout() + // and fused_ring() keep the shrink and this decision in step; this is the backstop for anything that + // drifts: a clear error, never a hang. + if (!fused_l && m.fused_bufs && T >= stream_all_min()) { + err = "prefill: the fused layout's MoE buffers are too small for layer " + std::to_string(l) + + "'s expert path at a chunk of " + std::to_string(T) + " tokens"; + return false; + } size_t n_order = 0; // the routed experts (the debug report; unknown when fused) bool peer_now = false; // multi-GPU: the peer computed rows of this layer (MMQ path only) if (fused_l) { diff --git a/sycl/src/prefill/prefill.cpp b/sycl/src/prefill/prefill.cpp index a4fac2c0..574a1841 100644 --- a/sycl/src/prefill/prefill.cpp +++ b/sycl/src/prefill/prefill.cpp @@ -550,6 +550,9 @@ struct Prefill::Impl { uint16_t* dq_d[DQ] = {}; uint8_t* stage_dev[RING_MAX] = {}; int ring = STAGE; // the slots of this layout's ring (ring_slots) + // this layout's GU, H and Xq are the fused path's (moe_bufs' `fused`), so a layer that runs MMQ or the FP16 path may + // only write stream_all_min() - 1 of its rows into them; fused_layout() keeps the two in step (#583, #954) + bool fused_bufs = false; std::unique_ptr stager; // the unpinned experts' host copies (step 4) dpct::event_ptr copied[RING_MAX] = {}, used[RING_MAX] = {}; bool stage_live[RING_MAX] = {}; @@ -972,6 +975,7 @@ bool Prefill::carve(size_t T, void* alloc) { { // one region for the attention half's and the MoE half's scratch (see gdn_set_bytes) const bool fz = fused_layout(T, m.src != nullptr); + m.fused_bufs = fz; const MoeBufs mb = moe_bufs(T, m.g->n_expert, fz); const uint64_t region = std::max({gdn_set_bytes(T), qsa_set_bytes(T, m.cap, m.max_blocks, m.sel_batch, m.attn_batch, s), moe_set_bytes(T, m.g->n_expert, fz)}); @@ -2557,6 +2561,17 @@ bool Prefill::run(const int64_t *tokens, int64_t n, int64_t pos0, const bool no_peer = !core::peer_portable(); const bool fused_nat = use_mmq && stream_all && no_peer && lay.native && fused::native_supported(mmq_gt, mmq_dt); const bool fused_l = (use_mmq && stream_all && no_peer && !lay.native && fused::enabled()) || fused_nat; + // #583 / #954: the fused layout's GU/H/Xq hold the grouping tables and the int8 rows, sized for + // stream_all_min() - 1 tokens of MMQ's rows, so a layer that takes MMQ or the FP16 path at the FULL + // chunk would write T*K rows into a (stream_all_min() - 1)*K-row buffer: an illegal access, or a kernel + // that never returns (the first ck() to see it is `prefill mmq: iota`, one prompt later). fused_layout() + // and fused_ring() keep the shrink and this decision in step; this is the backstop for anything that + // drifts: a clear error, never a hang. + if (!fused_l && m.fused_bufs && T >= stream_all_min()) { + err = "prefill: the fused layout's MoE buffers are too small for layer " + std::to_string(l) + + "'s expert path at a chunk of " + std::to_string(T) + " tokens"; + return false; + } size_t n_order = 0; // the routed experts (the debug report; unknown when fused) bool peer_now = false; // multi-GPU: the peer computed rows of this layer (MMQ path only) if (fused_l) {