diff --git a/include/strata/prefill/gemm.hpp b/include/strata/prefill/gemm.hpp index 80f625ee..cf43f79d 100644 --- a/include/strata/prefill/gemm.hpp +++ b/include/strata/prefill/gemm.hpp @@ -9,6 +9,7 @@ #include #include #include +#include namespace strata::prefill { @@ -92,6 +93,13 @@ private: void* side_ready_[2] = {}; void* side_free_[2] = {}; int side_cur_ = 0; + // STRATA_PF_W16_CACHE_MIB=N (CUDA, opt-in): native() keeps the FP16 dequantization of up to N MiB of weights, so a + // repeat product skips the dequant kernel; the FP16 values and the product are the uncached form's, bit for bit. + // A weight is keyed by its device pointer and shape (the engine's dense weights never move). + struct W16Entry { const void* p; int type; int64_t n, k; uint16_t* d; }; + std::vector w16_; + int64_t w16_budget_ = -1; // bytes; -1: env not read yet, 0: off + int64_t w16_used_ = 0; void* hipblaslt_state_ = nullptr; void* rocblas_state_ = nullptr; // HIP: the rocBLAS solution table of f16_inplace (STRATA_ROCBLAS_TUNING) bool f16_io_ = false; diff --git a/src/core/verify.cpp b/src/core/verify.cpp index 69f9cf33..50d50883 100644 --- a/src/core/verify.cpp +++ b/src/core/verify.cpp @@ -1626,10 +1626,11 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) { gg.state[gg.n] = sx.gdn_state + (size_t) (gi - sx.gdn_ord0) * gdn_floats; gg.first[gg.n] = first; gg.ntok[gg.n] = t - first; + gg.keep[gg.n] = keep_rows ? one_ : nullptr; // STRATA_BATCH_SELF_COMMIT: the window keeps its one-row groups itself ++gg.n; } strata::kernels::gdn_step_norm_groups(gg, hb, (int) C, gate, beta, z_, (const float*) wnm->data, EPS, - y_, (int) HK, (int) HV, false, cs); + y_, (int) HK, (int) HV, keep_rows, cs); } else if (batch_rec_) { // each slot's recurrence over its own proposed-token group for (int t = tb; t < te;) { diff --git a/src/kernels/cuda/fused_gr.cu b/src/kernels/cuda/fused_gr.cu index 24b00143..9e504694 100644 --- a/src/kernels/cuda/fused_gr.cu +++ b/src/kernels/cuda/fused_gr.cu @@ -50,6 +50,28 @@ __device__ __forceinline__ float warp_sum(float v) { for (int o = 16; o > 0; o >>= 1) v += __shfl_xor_sync(0xffffffffu, v, o); return v; } +// warp_sum of 8 independent values at once, token t's total ending in every lane whose bits 4..2 spell t ((lane >> 2) & 7). The same +// additions as warp_sum(a[t]) in the same order (offset 16, 8, 4, 2, 1; a + b = b + a, so a lane adding its partner's value to its own gets the +// bits the plain butterfly gets): each level only exchanges the half of the values the partner keeps, 4 + 2 + 1 + 1 + 1 shuffles instead of 40. +__device__ __forceinline__ float warp_sum8(const float (&a)[8], int lane) { + const bool h4 = (lane & 16) != 0, h3 = (lane & 8) != 0, h2 = (lane & 4) != 0; + float b[4], c[2]; +#pragma unroll + for (int j = 0; j < 4; ++j) { + const float send = h4 ? a[j] : a[j + 4], keep = h4 ? a[j + 4] : a[j]; + b[j] = keep + __shfl_xor_sync(0xffffffffu, send, 16); + } +#pragma unroll + for (int j = 0; j < 2; ++j) { + const float send = h3 ? b[j] : b[j + 2], keep = h3 ? b[j + 2] : b[j]; + c[j] = keep + __shfl_xor_sync(0xffffffffu, send, 8); + } + const float send = h2 ? c[0] : c[1], keep = h2 ? c[1] : c[0]; + float d = keep + __shfl_xor_sync(0xffffffffu, send, 4); + d += __shfl_xor_sync(0xffffffffu, d, 2); + d += __shfl_xor_sync(0xffffffffu, d, 1); + return d; +} __device__ __forceinline__ float sigmoidf_(float x) { return 1.0f / (1.0f + __expf(-x)); } // 8 bf16 packed in a uint4, unpacked to two float4 once when reused across tokens. @@ -709,10 +731,10 @@ __global__ void __launch_bounds__(Q32_THREADS) gr_up_q32_kernel(GrMulti m) { constexpr int PR = LR + HC; // partial rows per (token, stream): 320 down + 4 inject constexpr int TQ3 = N / 8 / 32; // uint4 weight chunks per lane in one stream's slice (10) -template +template __global__ void __launch_bounds__(THREADS) gr_down_v3_kernel(GrMulti m, float* __restrict__ part, float* __restrict__ ssg) { extern __shared__ __align__(16) float xs[]; // [T][N / S] - constexpr int R2 = 1; // down rows per warp + constexpr int R2 = R2V; // down rows per warp (R2V > 1: fewer blocks, each stages its stream slice once for R2V x the rows; the same dots) __shared__ float red[WARPS][MAX_T]; const int t = threadIdx.x, lane = t & 31, warp = t >> 5; const int T = EXACT_T ? MAX_T : m.T; @@ -778,12 +800,21 @@ __global__ void __launch_bounds__(THREADS) gr_down_v3_kernel(GrMulti m, float* _ if (EXACT_T || k < T) acc[k] += dot8u_ptr(wvq, xs + (size_t) k * SL + j * 8); } const int prow = inject_block ? LR + warp : row0 + r; + if constexpr (EXACT_T && MAX_T >= 5 && MAX_T <= 8) { // warp_sum8: token k's total sits in lanes 4k..4k+3 + float accs[8] = {0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f}; +#pragma unroll + for (int k = 0; k < MAX_T; ++k) accs[k] = acc[k]; + const float d8 = warp_sum8(accs, lane); + const int k = (lane >> 2) & 7; + if ((lane & 3) == 0 && k < MAX_T) part[(((size_t) k * HC + c) * S + h) * PR + prow] = d8; + } else { #pragma unroll for (int k = 0; k < MAX_T; ++k) { if (!EXACT_T && k >= T) break; const float v = warp_sum(acc[k]); if (lane == 0) part[(((size_t) k * HC + c) * S + h) * PR + prow] = v; } + } } } @@ -852,6 +883,17 @@ __global__ void __launch_bounds__(THREADS) gr_up_v3_kernel(GrMulti m, const floa if (apply) { bo = a.bo_prev[d0 + dd]; ip = a.inj_prev[c]; } } float mine = 0.0f; + if constexpr (EXACT_T && MAX_T >= 5 && MAX_T <= 8) { // the tokens' warp sums together (warp_sum8, missing tokens 0): the same bits + float accs[8] = {0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f}; +#pragma unroll + for (int k = 0; k < MAX_T; ++k) { + float acc = dot8u_ptr(wa, lo[k] + lane * 8); + if (lane < LR / 8 - 32) acc += dot8u_ptr(wb, lo[k] + (32 + lane) * 8); + accs[k] = acc; + } + const float d8 = warp_sum8(accs, lane); + mine = __shfl_sync(0xffffffffu, d8, (lane & 7) << 2); + } else { #pragma unroll for (int k = 0; k < MAX_T; ++k) { if (!EXACT_T && k >= T) break; @@ -860,6 +902,7 @@ __global__ void __launch_bounds__(THREADS) gr_up_v3_kernel(GrMulti m, const floa acc = warp_sum(acc); if (lane == k) mine = acc; } + } if (lane < T) { if (apply) { rv = fmaf(bo, 2.0f * sigmoidf_(ip / (float) HC), rv); @@ -1331,6 +1374,12 @@ int down_tg(int ct) { return per_dev[dev] == 2 && (ct == 4 || ct == 6) ? 2 : 0; #endif } +// STRATA_GR_R2 (v3 read, 5..8-token launches; default 2): 0 = the generic kernels as before, 1 / 2 / 4 = the exact-T kernels with that many down +// rows per warp (the same dots in the same order, so the same bits; fewer blocks stage the same activations less often) +static int gr_r2() { + static const int v = [] { const char* e = std::getenv("STRATA_GR_R2"); return e != nullptr ? std::atoi(e) : 2; }(); + return v == 1 || v == 2 || v == 4 ? v : 0; +} void launch_multi(const GrMulti& m, int variant, cudaStream_t st, unsigned long long* stamp_buf, int stamp_i0) { const int n_tok = m.T; const bool q8w = m.a[0].s_down != nullptr; // STRATA_HC_REQ8: w_down / w_up hold int8 codes @@ -2385,6 +2434,17 @@ static bool fused_gr_read_multi_slice(const FusedGrArgs* a, int n_tok, float* xn cudaFuncSetAttribute(gr_down_v3_kernel<1, kFusedGrMaxT, false>, cudaFuncAttributeMaxDynamicSharedMemorySize, need1) == cudaSuccess && cudaFuncSetAttribute(gr_down_v3_kernel<1, 6, true>, cudaFuncAttributeMaxDynamicSharedMemorySize, need6) == cudaSuccess && cudaFuncSetAttribute(gr_down_v3_kernel<1, 5, true>, cudaFuncAttributeMaxDynamicSharedMemorySize, need5) == cudaSuccess) { + if (kFusedGrMaxT >= 8) { // the exact 7- and 8-token instantiations (STRATA_GR_R2: rows per warp 1 / 2 / 4) + const int need7 = (int) (7 * N * sizeof(float)), need8 = (int) (8 * N * sizeof(float)); + cudaFuncSetAttribute(gr_down_v3_kernel<1, 5, true, 2>, cudaFuncAttributeMaxDynamicSharedMemorySize, need5); + cudaFuncSetAttribute(gr_down_v3_kernel<1, 6, true, 2>, cudaFuncAttributeMaxDynamicSharedMemorySize, need6); + cudaFuncSetAttribute(gr_down_v3_kernel<1, 7, true, 1>, cudaFuncAttributeMaxDynamicSharedMemorySize, need7); + cudaFuncSetAttribute(gr_down_v3_kernel<1, 7, true, 2>, cudaFuncAttributeMaxDynamicSharedMemorySize, need7); + cudaFuncSetAttribute(gr_down_v3_kernel<1, 7, true, 4>, cudaFuncAttributeMaxDynamicSharedMemorySize, need7); + cudaFuncSetAttribute(gr_down_v3_kernel<1, 8, true, 1>, cudaFuncAttributeMaxDynamicSharedMemorySize, need8); + cudaFuncSetAttribute(gr_down_v3_kernel<1, 8, true, 2>, cudaFuncAttributeMaxDynamicSharedMemorySize, need8); + cudaFuncSetAttribute(gr_down_v3_kernel<1, 8, true, 4>, cudaFuncAttributeMaxDynamicSharedMemorySize, need8); + } cudaFuncSetAttribute(gr_down_v3_kernel<1, 1, true>, cudaFuncAttributePreferredSharedMemoryCarveout, 100); cudaFuncSetAttribute(gr_down_v3_kernel<1, 2, true>, cudaFuncAttributePreferredSharedMemoryCarveout, 100); cudaFuncSetAttribute(gr_down_v3_kernel<1, 3, true>, cudaFuncAttributePreferredSharedMemoryCarveout, 100); @@ -2414,8 +2474,16 @@ static bool fused_gr_read_multi_slice(const FusedGrArgs* a, int n_tok, float* xn else if (n_tok == 2) gr_down_v3_kernel<1, 2, true><<>>(m, part, ssg); else if (n_tok == 3) gr_down_v3_kernel<1, 3, true><<>>(m, part, ssg); else if (n_tok == 4) gr_down_v3_kernel<1, 4, true><<>>(m, part, ssg); + else if (n_tok == 5 && gr_r2() == 2) gr_down_v3_kernel<1, 5, true, 2><<>>(m, part, ssg); else if (n_tok == 5) gr_down_v3_kernel<1, 5, true><<>>(m, part, ssg); + else if (n_tok == 6 && gr_r2() == 2) gr_down_v3_kernel<1, 6, true, 2><<>>(m, part, ssg); else if (n_tok == 6) gr_down_v3_kernel<1, 6, true><<>>(m, part, ssg); + else if (n_tok == 7 && kFusedGrMaxT >= 7 && gr_r2() == 2) gr_down_v3_kernel<1, 7, true, 2><<>>(m, part, ssg); + else if (n_tok == 7 && kFusedGrMaxT >= 7 && gr_r2() == 4) gr_down_v3_kernel<1, 7, true, 4><<>>(m, part, ssg); + else if (n_tok == 7 && kFusedGrMaxT >= 7 && gr_r2() == 1) gr_down_v3_kernel<1, 7, true, 1><<>>(m, part, ssg); + else if (n_tok == 8 && kFusedGrMaxT >= 8 && gr_r2() == 2) gr_down_v3_kernel<1, 8, true, 2><<>>(m, part, ssg); + else if (n_tok == 8 && kFusedGrMaxT >= 8 && gr_r2() == 4) gr_down_v3_kernel<1, 8, true, 4><<>>(m, part, ssg); + else if (n_tok == 8 && kFusedGrMaxT >= 8 && gr_r2() == 1) gr_down_v3_kernel<1, 8, true, 1><<>>(m, part, ssg); else gr_down_v3_kernel<1, kFusedGrMaxT, false><<>>(m, part, ssg); if (stamp_buf) gpu_stamp(stamp_buf, stamp_i0 + 1, stream); if (split == 2) gr_up_v3_kernel<2><<>>(m, part, ssg); @@ -2425,6 +2493,8 @@ static bool fused_gr_read_multi_slice(const FusedGrArgs* a, int n_tok, float* xn else if (n_tok == 4) gr_up_v3_kernel<1, 4, true><<>>(m, part, ssg); else if (n_tok == 5) gr_up_v3_kernel<1, 5, true><<>>(m, part, ssg); else if (n_tok == 6) gr_up_v3_kernel<1, 6, true><<>>(m, part, ssg); + else if (n_tok == 7 && kFusedGrMaxT >= 7 && gr_r2() > 0) gr_up_v3_kernel<1, 7, true><<>>(m, part, ssg); + else if (n_tok == 8 && kFusedGrMaxT >= 8 && gr_r2() > 0) gr_up_v3_kernel<1, 8, true><<>>(m, part, ssg); else gr_up_v3_kernel<1, kFusedGrMaxT, false><<>>(m, part, ssg); const cudaError_t e3 = cudaGetLastError(); if (e3 != cudaSuccess) { diff --git a/src/prefill/gemm.cu b/src/prefill/gemm.cu index 6d7816c9..6ee6b0c3 100644 --- a/src/prefill/gemm.cu +++ b/src/prefill/gemm.cu @@ -637,6 +637,7 @@ bool gemm_dequant_side() { } Gemm::~Gemm() { + for (W16Entry& e : w16_) if (e.d) (void) cudaFree(e.d); for (void* e : side_ready_) if (e) (void) cudaEventDestroy((cudaEvent_t) e); for (void* e : side_free_) @@ -1391,6 +1392,36 @@ void Gemm::native(const uint16_t* X, int ggml_type, const void* W_blocks, float* } #if defined(STRATA_PREFILL_MMQ) && defined(__HIPCC__) if (beta == 0.0f && (ldx == 0 || ldx == K) && native_mmq(X, ggml_type, W_blocks, Y, T, N, K, ldy)) return; +#endif +#ifndef STRATA_USE_HIP + // STRATA_PF_W16_CACHE_MIB=N: the FP16 weight kept on the device after its first dequant (same values, same product) + if (w16_budget_ < 0) { + const char* v = std::getenv("STRATA_PF_W16_CACHE_MIB"); + w16_budget_ = v ? (int64_t) std::atoll(v) * (1ll << 20) : 0; + } + if (w16_budget_ > 0 && !side_scratch_) { + const int64_t bytes = N * K * 2; + uint16_t* cw = nullptr; + for (const W16Entry& e : w16_) + if (e.p == W_blocks && e.type == ggml_type && e.n == N && e.k == K) { cw = e.d; break; } + if (cw == nullptr && w16_used_ + bytes <= w16_budget_) { + size_t fb = 0, tb = 0; + if (cudaMemGetInfo(&fb, &tb) == cudaSuccess && fb > (size_t) bytes + (2ull << 30) && + cudaMalloc((void**) &cw, (size_t) bytes) == cudaSuccess) { + strata::kernels::dequant_f16(ggml_type, W_blocks, 0, N, K, cw, stream_); + w16_.push_back({W_blocks, ggml_type, N, K, cw}); + w16_used_ += bytes; + } else { + (void) cudaGetLastError(); + cw = nullptr; + w16_budget_ = w16_used_ > 0 ? w16_used_ : 0; // out of room: stop trying + } + } + if (cw != nullptr) { + f16(X, cw, Y, T, N, K, ldy, beta); + return; + } + } #endif if (N * K > scratch_elems_) { // Too large for the scratch at once: in row slices. diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 956d2c56..6102542c 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -15070,6 +15070,70 @@ int main(int argc, char** argv) { } return run_ok; }; + // STRATA_BATCH_CONCURRENT_READ=1 (opt-in, one GPU, --batch with --no-prefill-borrow): the prompt is read on a + // thread of its own while this thread keeps the slots' batch windows going, so the decoding slots do not + // stop for an admission. The prompt writes the main session, the windows the slots' sessions; the prompt + // borrows no cache slots (--no-prefill-borrow), so the windows' experts stay where they are. The same + // bytes are computed as in the serial form (a window's rows do not depend on what else is on the GPU). + static const bool conc1 = [] { + const char* v = std::getenv("STRATA_BATCH_CONCURRENT_READ"); + return v != nullptr && std::atoi(v) != 0; + }(); + auto run_conc = [&](int64_t q, int64_t r) -> bool { + if (!conc1 || piped || !stages.empty() || o.batch <= 0 || !o.no_prefill_borrow || !batch_on() || + !req_imgs.empty() || kvg.on) + return run(q, r); + std::atomic done{false}; + bool read_ok = false; + in_prompt_read = true; + std::thread reader([&] { + try { // (an exception leaving a std::thread ends the process: a failed read instead) + read_ok = run(q, r); + } catch (const std::exception& x) { + read_ok = false; + e = std::string("the prompt read failed: ") + x.what(); + } catch (...) { + read_ok = false; + e = "the prompt read failed"; + } + done.store(true, std::memory_order_release); + { std::lock_guard lk(in_mu); } + in_cv.notify_all(); + }); + bool step_ok = true; + int64_t windows0 = bt_windows; + const auto tc = Clock::now(); + while (!done.load(std::memory_order_acquire)) { + if (step_ok && batch_on()) { + step_ok = batch_step(); + } else { + std::unique_lock lk(in_mu); + in_cv.wait_for(lk, std::chrono::milliseconds(2), + [&] { return done.load(std::memory_order_acquire); }); + } + std::lock_guard lk(in_mu); // a BSTOP that came meanwhile ends its slot at its next window + for (auto it = in_lines.begin(); it != in_lines.end();) { + if (it->rfind("BSTOP ", 0) == 0) { + const int b = std::atoi(it->c_str() + 6); + if (b >= 0 && b < (int) bs.size()) bs[(size_t) b].stop = true; + it = in_lines.erase(it); + } else { + ++it; + } + } + } + reader.join(); + in_prompt_read = false; + std::fprintf(stderr, "strata batch: read %lld tokens on a thread of its own in %.0f ms; the slots ran %lld windows meanwhile\n", + (long long) (r - q), std::chrono::duration(Clock::now() - tc).count(), + (long long) (bt_windows - windows0)); + if (!step_ok) { + batch_fatal = true; + e = "a batch window failed"; + return false; + } + return read_ok; + }; // (a layer split reads its stages as a pipeline over one run's chunks: beside slots a chunk at a time, with // none decoding in pieces of STRATA_BATCH_YIELD_CHUNKS chunks (default 8; 0 = one run) so a BYIELD is taken) static const int64_t yield_chunks = [] { @@ -15450,7 +15514,7 @@ int main(int argc, char** argv) { for (int64_t q = a0; q < b0;) { const int64_t r = std::min(b0, q + piece); const auto tq = Clock::now(); - if (!run(q, r)) return false; + if (!run_conc(q, r)) return false; const double piece_ms = std::chrono::duration(Clock::now() - tq).count(); if (pr_on && r < b0) pr_prev_piece_ms = piece_ms; const int64_t piece_tokens = r - q;