diff --git a/src/prefill/gemm.cu b/src/prefill/gemm.cu index 25910b2e17..595846a969 100644 --- a/src/prefill/gemm.cu +++ b/src/prefill/gemm.cu @@ -2,6 +2,7 @@ #include "strata/prefill/gemm.hpp" #include "strata/kernels/dequant_bf16.hpp" #if defined(STRATA_PREFILL_MMQ) && defined(__HIPCC__) +#include "strata/prefill/mmq_ids_margin.hpp" #include "strata/prefill/moe_mmq.hpp" #endif @@ -1294,8 +1295,12 @@ bool Gemm::native_mmq(const uint16_t* X, int type, const void* W, float* Y, int6 if (ldy <= 0) ldy = N; const auto up = [](size_t v) { return (v + 255) & ~(size_t) 255; }; + // The row table is read past its end by llama.cpp's MMQ (mul_mat_q: a whole J-column tile from the start of each tile, J up to 128; the stream-k fixup: + // up to roundup(max_rows, J) from the expert's first row): kRows + the margin of mmq_ids_margin.hpp (max_rows <= kRows), the identity written over all of it. + // Exact, with only the 256 bytes of the bounds after it, a tile of 112 columns on a full chunk read 384 bytes past the table, 128 past the allocation. + const size_t ids_rows = (size_t) mmq::mmq_ids_table_rows((uint64_t) kRows, (uint64_t) kRows); const size_t ident_off = up(mmq::q8_bytes(kRows, kMaxK)); - const size_t bounds_off = ident_off + up((size_t) kRows * 4); + const size_t bounds_off = ident_off + up(ids_rows * 4); if (!mmq_buf_) { if (cudaMalloc(&mmq_buf_, bounds_off + 256) != cudaSuccess) { cudaGetLastError(); @@ -1303,7 +1308,7 @@ bool Gemm::native_mmq(const uint16_t* X, int type, const void* W, float* Y, int6 return false; } mmq_ctx_ = new mmq::Context(); - mmq::iota((int32_t*) ((uint8_t*) mmq_buf_ + ident_off), kRows, stream_); + mmq::iota((int32_t*) ((uint8_t*) mmq_buf_ + ident_off), (int64_t) ids_rows, stream_); std::fprintf(stderr, "prefill gemm: dense MMQ on (STRATA_DENSE_MMQ=1)\n"); } void* xq = mmq_buf_; diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp index 370a665f15..2244e74006 100644 --- a/src/prefill/prefill.cpp +++ b/src/prefill/prefill.cpp @@ -2388,7 +2388,7 @@ bool Prefill::draft_kv(core::MtpDrafter& mtp, const float* R_rows, const int32_t static const bool f16_only = [] { const char* v = std::getenv("STRATA_MTP_BATCH_F16"); return v == nullptr || v[0] != '0'; }(); 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); + (q8 ? (uint64_t) mmq::q8_bytes(g.hc, Nn) + 8 * g.hc : 0); // (8 * hc: the row table twice, see its margin below) 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); @@ -2413,7 +2413,10 @@ bool Prefill::draft_kv(core::MtpDrafter& mtp, const float* R_rows, const int32_t float* Vc = (float*) carve((size_t) B * KV * 4); int32_t* tok = (int32_t*) carve((size_t) B * 4); void* xq = q8 ? carve(mmq::q8_bytes(B * g.hc, Nn)) : nullptr; - int32_t* ident = q8 ? (int32_t*) carve((size_t) B * g.hc * 4) : nullptr; + // The row table is read past its end by llama.cpp's MMQ (a whole J-column tile from the start of each tile; the stream-k fixup up to roundup(max_rows, J) + // from the expert's first row; J up to 128): rows + the margin of mmq_ids_margin.hpp, max_rows <= B * hc (one expert, p.n = 1), the identity over all of it. + const uint64_t ids_rows = q8 ? mmq::mmq_ids_table_rows((uint64_t) (B * g.hc), (uint64_t) (B * g.hc)) : 0; + int32_t* ident = q8 ? (int32_t*) carve((size_t) ids_rows * 4) : nullptr; int32_t* bnd = q8 ? (int32_t*) carve(16) : nullptr; if ((uint64_t) (q - m.region) > m.region_bytes) return decline("the batch buffers do not fit the scratch region"); static const bool timing = std::getenv("STRATA_DRAFT_TIMING") != nullptr; // debug: where this pass's time goes @@ -2426,7 +2429,7 @@ bool Prefill::draft_kv(core::MtpDrafter& mtp, const float* R_rows, const int32_t std::vector tk((size_t) B); if (q8) { if (!m.mmq_ctx) m.mmq_ctx = std::make_unique(); - mmq::iota(ident, B * g.hc, m.cs); + mmq::iota(ident, (int64_t) ids_rows, m.cs); } // y[rows, n_out] = x[rows, k] . w^T for a Q8_0 matrix: MMQ from the FP32 rows (bounds slot 0: rows, 1: rows*hc) auto proj = [&](const float* x, const uint16_t* x16, const void* w, float* y, int64_t rows, int64_t n_out,