diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp index 49bb2aab5d..e6f414a9a5 100644 --- a/src/prefill/prefill.cpp +++ b/src/prefill/prefill.cpp @@ -5801,7 +5801,12 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri int gg_nslots = 0; // ring slots gathered by the next flush size_t cur_entry = 0; // the streamed walk: the entry compute() is given // P4-SMALL (fz_staged): the open group's expert pointers and the layer's native geometry - const uint8_t* fz_blob[MMQ_GROUP] = {}; + // The layer's tables are built once (fused::group_bounds over every routed expert's rows, below); the experts then + // launch in batches: the open batch (fz_b, local experts [fz_j0, ..)) closes at kMaxBatch experts, at MMQ_GROUP experts + // from the staging ring (their slots stay held until the launch; the ring has STAGE_GRP), or at the layer's last expert. + // Resident experts take no slot, so a layer whose experts all sit in the cache launches ceil(n / kMaxBatch) pairs. + fused::Batch fz_b; + size_t fz_j0 = 0; fused::NativeGeom fz_geom{}; if (fz_staged) { const auto& f = lay.fmt[(size_t) l]; @@ -5868,32 +5873,27 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri : "the MMQ group buffers are not the blob's [gate | up] and down"); #endif pt.mark(kPfDequant, cs); - if (fz_staged) { // P4-SMALL: the group's experts where they are; its two products and SwiGLU at its last expert - const size_t q = j % MMQ_GROUP; - fz_blob[q] = blob_dev; + if (fz_staged) { // P4-SMALL: the batch's experts where they are; its two products and SwiGLU at its last expert + fz_b.blob[j - fz_j0] = blob_dev; if (slot >= 0) { gg_ents[gg_nslots] = cur_entry; gg_slots[gg_nslots++] = slot; } - if (q + 1 < MMQ_GROUP && j + 1 < order.size()) return true; + if (j + 1 - fz_j0 < (size_t) fused::kMaxBatch && gg_nslots < MMQ_GROUP && j + 1 < order.size()) return true; if (gg_nslots > 0) { // the copies land in order: the last one covers the others pt.mark(kPfWaitCopy, cs); wait_copied(gg_ents[gg_nslots - 1], (size_t) gg_slots[gg_nslots - 1]); } - const size_t j0 = j - q, g = j0 / MMQ_GROUP, n = order.size(); - const int ngx = (int) (q + 1); - const int64_t r0 = m.bounds_host[j0], nr = m.bounds_host[j + 1] - r0; + fz_b.e0 = (int) fz_j0; + fz_b.e1 = (int) (j + 1); pt.mark(kPfGemmGU, cs); - fused::group_bounds(m.bounds_dev + n + 1 + g * (MMQ_GROUP + 1), ngx, m.GU, m.cs); - fused::Batch fb; - fb.e0 = 0; - fb.e1 = ngx; - for (int i = 0; i < ngx; ++i) fb.blob[i] = fz_blob[i]; fused::want_dm_f16(false); - fused::experts_native(fb, fz_geom, ngx, nr, m.GU, m.Xq, m.src_dev + r0, m.H, m.Dm + r0 * N, m.cs); - if (gg_nslots > 0) { // the group's staged blobs are read: one event releases their slots + // the layer's tables (group_bounds, before the walk) and rows: the batch's experts are local indices of them + fused::experts_native(fz_b, fz_geom, (int) order.size(), T * K, m.GU, m.Xq, m.src_dev, m.H, m.Dm, m.cs); + if (gg_nslots > 0) { // the batch's staged blobs are read: one event releases their slots const int rel = gg_slots[gg_nslots - 1]; cudaEventRecord(m.used[rel], m.cs); for (int i = 0; i < gg_nslots; ++i) m.used_of[gg_slots[i]] = rel; } gg_nslots = 0; + fz_j0 = j + 1; return true; } if (use_mmq) { @@ -6123,6 +6123,9 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri } else if (!stream_all) { size_t staged = 0; size_t pending = 0; + // P4-SMALL: the layer's tables from the routed experts' row bounds (absolute, uploaded above), once; every + // batch of experts below reads its own experts' tiles out of them (the streamed path's fused::group, from bounds) + if (fz_staged && !order.empty()) fused::group_bounds(m.bounds_dev, (int) order.size(), m.GU, m.cs); const bool stream_ahead = stream_ahead_enabled(); const size_t lookahead = group_resident ? (size_t) (STAGE_GRP - MMQ_GROUP - 1) : STAGE - 1; for (size_t j = 0; j < order.size(); ++j) { diff --git a/tests/cuda/prefill_fused_group_test.cpp b/tests/cuda/prefill_fused_group_test.cpp index 6927eb8b97..13aa38083e 100644 --- a/tests/cuda/prefill_fused_group_test.cpp +++ b/tests/cuda/prefill_fused_group_test.cpp @@ -216,6 +216,36 @@ void run_case(const Pair& p, const Case& c, mmq::Context& ctx, cudaStream_t s, u for (int e = b.e0; e < b.e1; ++e) b.blob[e - b.e0] = blob(e); fused::experts_native(b, ng, E, rows, scrB.p, xa.p, src.as(), haB.p, dmB.as(), s); } + // (D) the staged walk's call: the layer's tables ONCE from the routed experts' absolute row bounds (group_bounds over every routed expert), + // then experts_native on batches of experts (local indices of those tables, the whole layer's rows as `n`), batch sizes of every kind: + // a full kMaxBatch, 16 (the staging ring's share), odd sizes and 1 + std::vector order, abs_b; + { + std::vector cnt((size_t) E, 0); + for (int32_t e : ids) ++cnt[(size_t) e]; + int32_t acc = 0; + for (int e = 0; e < E; ++e) + if (cnt[(size_t) e] > 0) { order.push_back(e); abs_b.push_back(acc); acc += cnt[(size_t) e]; } + abs_b.push_back(acc); + } + const int nord = (int) order.size(); + Dev absD(abs_b.size() * 4), scrD(fused::group_bytes(rows, nord)), haD(fused::act_bytes(rows, FF)), dmD((size_t) rows * N * 4); + ck(cudaMemcpy(absD.p, abs_b.data(), abs_b.size() * 4, cudaMemcpyHostToDevice), "abs bounds"); + ck(cudaMemsetAsync(dmD.p, 0xff, (size_t) rows * N * 4, s), "dmD nan"); + fused::group_bounds(absD.as(), nord, scrD.p, s); + { + const int sizes[] = {fused::kMaxBatch, 16, 37, 1, 5, fused::kMaxBatch, 16, 16}; + int j0 = 0, si = 0; + while (j0 < nord) { + const int j1 = std::min(nord, j0 + sizes[si++ % 8]); + fused::Batch b; + b.e0 = j0; + b.e1 = j1; + for (int j = j0; j < j1; ++j) b.blob[j - j0] = blob(order[(size_t) j]); + fused::experts_native(b, ng, nord, rows, scrD.p, xa.p, row_tok_dev.as(), haD.p, dmD.as(), s); + j0 = j1; + } + } // (C) MMQ in the walk const size_t gub = mmq::matrix_bytes((int) p.gu, FF2, N), db = mmq::matrix_bytes((int) p.d, N, FF); Dev xq(mmq::q8_bytes(cap, N)), gu((size_t) cap * FF2 * 4), h((size_t) cap * FF * 4), hq(mmq::q8_bytes(cap, FF)), @@ -244,19 +274,21 @@ void run_case(const Pair& p, const Case& c, mmq::Context& ctx, cudaStream_t s, u ctx.run(pd, s); } ck(cudaStreamSynchronize(s), "sync"); - std::vector a((size_t) rows * N), b((size_t) rows * N), m((size_t) rows * N); + std::vector a((size_t) rows * N), b((size_t) rows * N), m((size_t) rows * N), dd((size_t) rows * N); std::vector sl((size_t) rows); + ck(cudaMemcpy(dd.data(), dmD.p, dd.size() * 4, cudaMemcpyDeviceToHost), "d"); ck(cudaMemcpy(a.data(), dmA.p, a.size() * 4, cudaMemcpyDeviceToHost), "a"); ck(cudaMemcpy(b.data(), dmB.p, b.size() * 4, cudaMemcpyDeviceToHost), "b"); ck(cudaMemcpy(m.data(), dmC.p, m.size() * 4, cudaMemcpyDeviceToHost), "m"); ck(cudaMemcpy(sl.data(), slot.p, sl.size() * 4, cudaMemcpyDeviceToHost), "slot"); - int64_t differ = 0, nonfinite = 0; + int64_t differ = 0, nonfinite = 0, differ_d = 0; double e2a = 0, e2b = 0, r2 = 0, worst_a = 0, worst_b = 0; for (int64_t i = 0; i < rows; ++i) { const float* ya = a.data() + (size_t) w.row_of[(size_t) i] * N; const float* yb = b.data() + (size_t) sl[(size_t) i] * N; const float* ym = m.data() + (size_t) w.row_of[(size_t) i] * N; if (std::memcmp(ya, yb, (size_t) N * 4) != 0) ++differ; + if (std::memcmp(dd.data() + (size_t) w.row_of[(size_t) i] * N, yb, (size_t) N * 4) != 0) ++differ_d; double mea = 0, meb = 0, mr = 0; for (int o = 0; o < N; ++o) { if (!std::isfinite(ya[o])) { ++nonfinite; break; } @@ -275,6 +307,9 @@ void run_case(const Pair& p, const Case& c, mmq::Context& ctx, cudaStream_t s, u "vs MMQ in the walk: rel RMS %.3e (layer path %.3e), worst row %.3e (%.3e)\n", p.name, T, E, cap, w.grps.size(), w.ents.size(), (long long) differ, (long long) rows, (long long) nonfinite, rms_a, rms_b, worst_a, worst_b); + std::printf("%-18s staged walk's batched call (%d experts, group_bounds once, batches of 128 / 16 / 37 / 1 / 5): %lld of %lld rows differ from the layer fused path\n", + p.name, nord, (long long) differ_d, (long long) rows); + if (differ_d) throw std::runtime_error(std::string(p.name) + ": the staged walk's batched call differs from the layer's fused path"); if (nonfinite) throw std::runtime_error(std::string(p.name) + ": a row of the group entry is not finite (unwritten or overflowed)"); if (differ) throw std::runtime_error(std::string(p.name) + ": the group entry's rows differ from the layer's fused path"); if (!(rms_a < 0.1) || !(worst_a < 0.5)) throw std::runtime_error(std::string(p.name) + ": far from MMQ in the walk");