From 9ee557f781fa5680e4a614b41262612d18378d0e Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Mon, 28 Sep 2026 04:19:42 +0200 Subject: [PATCH] prompt path: zero the tail after the last gathered expert before MMQ (garbage answers after the first request) MMQ reads up to one 256-value tile past a matrix's last row when the row is not a multiple of it (the down product: 640 values). Those weights meet zero activations, harmless only if they decode finite; llama.cpp zero-pads after every tensor. The group buffers held leftovers - after the first request's refill, other experts' bytes read as IQ4_NL blocks - and a non-finite scale times zero gave NaN logits ("!!!!" answers from the second request on). The group buffers get a 4 KB tail, zeroed after the last expert before each launch. The MMQ row table (in the lent slots the refill overwrites) is written at every prompt. STRATA_DBG_NAN=1 reports the first layer of a chunk whose MoE produced non-finite values. --- src/prefill/prefill.cpp | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp index a7fcab66..9f7b20d2 100644 --- a/src/prefill/prefill.cpp +++ b/src/prefill/prefill.cpp @@ -25,6 +25,7 @@ #include #include +#include #include #include #include @@ -376,6 +377,10 @@ uint64_t qsa_set_bytes(size_t T, int64_t cap, int64_t max_blocks, int64_t sel_ba // is converted to GGUF Q2_0 blocks on the gather), whether any layer keeps the FP16 path (IQ1_M), and the largest // gate/up and down matrices a group buffer slot holds. STRATA_PREFILL_MMQ=0: the FP16 path everywhere (the A/B). constexpr int MMQ_GROUP = 16; // experts per MMQ launch (the gather is per expert, as blobs arrive) +// MMQ reads up to one 256-value tile past a matrix's last row when the row length is not a multiple of it (the down +// product: 640 values). Those bytes meet zero activations, which is harmless only if they decode to finite numbers - +// llama.cpp zero-pads after every tensor, and so does a group buffer: this many zeroed bytes follow its last expert. +constexpr size_t MMQ_TAIL = 4096; struct MmqPlan { bool any = false, fallback = true; std::vector layer; // per layer: MMQ @@ -548,8 +553,8 @@ bool Prefill::carve(size_t T, void* alloc) { const MmqPlan& mp = mmq_plan(); m.ids_identity = o.take(T * K, ok); m.bounds_dev = o.take((size_t) (2 * (NE + NE / MMQ_GROUP + 2)), ok); - m.grp_gu = o.take(MMQ_GROUP * mp.gu_max, ok); - m.grp_d = o.take(MMQ_GROUP * mp.d_max, ok); + m.grp_gu = o.take(MMQ_GROUP * mp.gu_max + MMQ_TAIL, ok); + m.grp_d = o.take(MMQ_GROUP * mp.d_max + MMQ_TAIL, ok); if (ok) mmq::iota(m.ids_identity, (int64_t) (T * K), m.cs); if (!m.mmq_ctx) m.mmq_ctx = std::make_unique(); } @@ -618,8 +623,8 @@ uint64_t Prefill::bytes_needed(const core::ModelGeometry& g, const core::Session const MmqPlan& mp = mmq_plan(); o.take(T * K, ok); o.take((size_t) (2 * (NE + NE / MMQ_GROUP + 2)), ok); - o.take(MMQ_GROUP * mp.gu_max, ok); - o.take(MMQ_GROUP * mp.d_max, ok); + o.take(MMQ_GROUP * mp.gu_max + MMQ_TAIL, ok); + o.take(MMQ_GROUP * mp.d_max + MMQ_TAIL, ok); } for (int i = 0; i < ring_slots(T); ++i) o.take((size_t) MAXBLOB(), ok); f(T * N); @@ -1218,6 +1223,9 @@ bool Prefill::run(const int64_t* tokens, int64_t n, int64_t pos0, std::string& e int64_t maxr = 0; for (size_t i = j0; i <= j; ++i) maxr = std::max(maxr, m.cnt[(size_t) order[i]]); pt.mark(kPfGemmGU, cs); + // the zeroed tail after the group's last expert (see MMQ_TAIL) + cudaMemsetAsync(m.grp_gu + (size_t) ngx * mmq_gub, 0, MMQ_TAIL, m.cs); + cudaMemsetAsync(m.grp_d + (size_t) ngx * mmq_db, 0, MMQ_TAIL, m.cs); mmq::Product gu; gu.w = m.grp_gu; gu.type = mmq_gt; gu.w_rows = 1280; gu.w_cols = N; gu.expert_bytes = mmq_gub; gu.n = ngx; gu.xq = m.Xq; gu.bounds = m.bounds_dev + j0; gu.ids = m.ids_identity; @@ -1302,6 +1310,27 @@ bool Prefill::run(const int64_t* tokens, int64_t n, int64_t pos0, std::string& e } pt.mark(kPfCombine, cs); moe_combine(m.Dm, m.slot_dev, m.w, m.shared, m.sg, m.bo, T, m.cs); + // debug: STRATA_DBG_NAN=1 reports the first layer of a chunk whose MoE produced non-finite values + if (static const bool dbg = std::getenv("STRATA_DBG_NAN") != nullptr; dbg) { + cudaStreamSynchronize(m.cs); + auto bad = [&](const float* d, int64_t n) { + std::vector h((size_t) n); + cudaMemcpy(h.data(), d, (size_t) n * 4, cudaMemcpyDeviceToHost); + int64_t c = 0; + for (float v : h) c += !std::isfinite(v); + return c; + }; + const int64_t bgu = bad(m.GU, T * K * 1280), bdm = bad(m.Dm, T * K * N), bbo = bad(m.bo, T * N); + const int64_t bh = m.H ? bad(m.H, T * K * 640) : -1; + static int64_t reported = -1; + if ((bgu || bdm || bbo || bh > 0) && reported != stats_.chunks) { + reported = stats_.chunks; + std::fprintf(stderr, "strata dbg: layer %lld (mmq %d, types %d/%d, %zu experts): non-finite GU %lld " + "H %lld Dm %lld bo %lld of T %lld\n", (long long) l, (int) use_mmq, mmq_gt, mmq_dt, + order.size(), (long long) bgu, (long long) bh, (long long) bdm, (long long) bbo, + (long long) T); + } + } } // ---- the hyper-connection write of this half gr_write(m.R, m.bo, m.inj, HC, T, m.cs);