From 62853e78dedaa429caee92e3f42268a01955f076 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Tue, 3 Nov 2026 11:29:44 +0100 Subject: [PATCH] FP16 weight cache on by default when the engine runs batch slots on Linux CUDA (3 GiB kept free); gemm_w16_parity test gemm_dequant_side_parity failed with the cache because its synthetic scratch (10.5 M elements) is smaller than four of its weights, so uncached they take the sliced path and cached one full-N GEMM. The engine's scratch is 32 M elements and every dense projection fits it. gemm_w16_parity runs the engine's scratch, shapes and types, cache off against on, bitwise. STRATA_PF_W16_CACHE_MIB=0 turns it off. --- CMakeLists.txt | 4 + include/strata/prefill/gemm.hpp | 7 ++ src/prefill/gemm.cu | 12 +-- src/prefill/gemm_w16_parity.cu | 127 ++++++++++++++++++++++++++++++++ src/program/generate.cpp | 7 ++ 5 files changed, 152 insertions(+), 5 deletions(-) create mode 100644 src/prefill/gemm_w16_parity.cu diff --git a/CMakeLists.txt b/CMakeLists.txt index f4f6a30da5..80aed3047d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1404,6 +1404,10 @@ if(STRATA_ENABLE_CUDA OR STRATA_ENABLE_HIP) target_link_libraries(gemm_dequant_side_parity PRIVATE strata_prefill ) add_test(NAME gemm_dequant_side_parity COMMAND gemm_dequant_side_parity) set_tests_properties(gemm_dequant_side_parity PROPERTIES SKIP_RETURN_CODE 77) + add_executable(gemm_w16_parity src/prefill/gemm_w16_parity.cu) + target_link_libraries(gemm_w16_parity PRIVATE strata_prefill ) + add_test(NAME gemm_w16_parity COMMAND gemm_w16_parity) + set_tests_properties(gemm_w16_parity PROPERTIES SKIP_RETURN_CODE 77) # Pascal, opt-in STRATA_DENSE_F32=1: the dense projections' FP16 GEMMs as cublasSgemm on FP32 copies, bitwise vs # the cuBLAS FP16 call over a sweep of chunk sizes (GPU, synthetic, no model; --full sweeps every size, --bench # adds timings). Not a ctest: whether the two agree is a property of the cuBLAS build, which this checks. diff --git a/include/strata/prefill/gemm.hpp b/include/strata/prefill/gemm.hpp index 48f66003b4..d977a86ab1 100644 --- a/include/strata/prefill/gemm.hpp +++ b/include/strata/prefill/gemm.hpp @@ -6,6 +6,7 @@ // rounded to BF16, which is also what llama.cpp's batched CUDA path does. Tensor-core GEMM through cuBLAS. #pragma once +#include #include #include #include @@ -103,6 +104,12 @@ private: int64_t w16_reserve_ = 2ll << 30; // VRAM kept free after a cached weight bool w16_logged_ = false; static constexpr int64_t kW16AutoReserve = 3ll << 30; +public: + /// The engine's own default for the FP16 weight cache when STRATA_PF_W16_CACHE_MIB is not set: true -> sized by the card (weights are cached while + /// kW16AutoReserve stays free). generate.cpp sets it on Linux CUDA when the engine runs batch slots (round0212 item 7). =0 in the environment wins. + static void set_w16_auto_default(bool on) { w16_auto_default_().store(on); } +private: + static std::atomic& w16_auto_default_() { static std::atomic v{false}; return v; } 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/prefill/gemm.cu b/src/prefill/gemm.cu index 17608fbcf6..ebc558f5f0 100644 --- a/src/prefill/gemm.cu +++ b/src/prefill/gemm.cu @@ -1420,11 +1420,13 @@ void Gemm::native(const uint16_t* X, int ggml_type, const void* W_blocks, float* w16_budget_ = (int64_t) std::atoll(v) * (1ll << 20); w16_reserve_ = 2ll << 30; } else { - // OFF by default (INT3, 11-01; owner's rule: a default that changes values needs 5%+, KL and a yes). cda6463e8 had turned it on for - // Linux CUDA as 'bit-exact', but gemm_dequant_side_parity fails with it on sm_86 (z440, boston), sm_120 (l620) and sm_121 (Spark): a - // cached weight runs one full-N GEMM where the uncached form runs the sliced ones, and the roundings differ. v0.2.9 had it off. - // STRATA_PF_W16_CACHE_MIB=N (above) turns it on by hand; the automatic sizing (kW16AutoReserve) is kept for when it is shown exact. - w16_budget_ = 0; + // INT3 turned the default off (11-01) because gemm_dequant_side_parity failed with it on sm_86 / sm_120 / sm_121. That test builds its Gemm + // with a 10.5 M-element scratch, smaller than four of its weights, so uncached those take the sliced path (several GEMMs) and cached they + // take one full-N GEMM: not the engine. The engine's scratch is GEMM_SCRATCH = 32 M elements and every dense projection of Qwen3.8 + // (largest attn_q, 31.5 M) fits it, so cached and uncached are the same GEMM on the same FP16 values (gemm_w16_parity: the engine's scratch, + // shapes and types, bitwise). Round0212 item 7: on when the engine says so (set_w16_auto_default: Linux CUDA with batch slots), else off + // as in v0.2.9. It takes only VRAM beyond kW16AutoReserve, so a card whose expert cache fills it caches nothing. =0 turns it off. + w16_budget_ = w16_auto_default_().load() ? (16ll << 30) : 0; w16_reserve_ = kW16AutoReserve; } } diff --git a/src/prefill/gemm_w16_parity.cu b/src/prefill/gemm_w16_parity.cu new file mode 100644 index 0000000000..1d48229659 --- /dev/null +++ b/src/prefill/gemm_w16_parity.cu @@ -0,0 +1,127 @@ +// src/prefill/gemm_w16_parity.cu - the FP16 weight cache (STRATA_PF_W16_CACHE_MIB) against the uncached Gemm::native, bitwise +// (GPU, synthetic weights, no model). +// +// gemm_dequant_side_parity runs Gemm::native twice, with the cache and without the side-stream scratch, and its scratch (10.5 M elements) is +// smaller than three of its eight weights, so those take the SLICED path (several GEMMs of N/k rows) uncached but ONE full-N GEMM cached: +// two different cuBLAS calls that need not round alike. The engine's scratch is GEMM_SCRATCH = 32 M elements (src/prefill/prefill.cpp) and every +// dense projection of Qwen3.8-Flash-Next (the largest is attn_q, 12288 x 2560 = 31.5 M) fits it, so in the engine the cached and the uncached +// forms are the same GEMM on the same FP16 values. This test runs exactly that: the engine's scratch size, the engine's dense shapes and types +// (Q8_0 on IQ3_S, Q4_K / Q5_K / Q6_K on the Unsloth sets), several token counts, with the cache off (STRATA_PF_W16_CACHE_MIB=0) and on +// ("auto"), each cached weight used twice. Every output must be identical bit for bit. A weight larger than the scratch (the 248320 x 2560 +// output head) is reported separately and is NOT part of the verdict: it never goes through Gemm::native in the prompt path. +// Exit 77 without a CUDA device. +#include "strata/prefill/gemm.hpp" + +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +namespace { +__global__ void fill_f16(__half* p, int64_t n, unsigned seed, float scale) { + const int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x; + if (i >= n) return; + unsigned x = (unsigned) i * 2654435761u + seed; + x ^= x >> 16; x *= 2246822519u; x ^= x >> 13; x *= 3266489917u; x ^= x >> 16; + p[i] = __float2half(((float) (x & 0xffff) / 32768.0f - 1.0f) * scale); +} +bool ok(cudaError_t e, const char* what) { + if (e != cudaSuccess) std::printf("FAIL: %s: %s\n", what, cudaGetErrorString(e)); + return e == cudaSuccess; +} +uint16_t sane_half(std::mt19937& rng) { + const uint32_t r = rng(); + return (uint16_t) (((r >> 31) << 15) | ((5u + (r >> 10) % 5u) << 10) | (r & 0x3ffu)); +} +// one quantization type: ggml type id, elements and bytes per block, offsets of the FP16 scales inside a block +struct QType { int type, elems, bytes; int at[2]; const char* name; }; +struct Shape { int qt; int64_t N, K; const char* what; bool production; }; +struct Weight { Shape s; void* dev = nullptr; }; +} // namespace + +int main() { + setvbuf(stdout, nullptr, _IONBF, 0); + int n_dev = 0; + if (cudaGetDeviceCount(&n_dev) != cudaSuccess || n_dev < 1) { std::printf("no CUDA device (skipped)\n"); return 77; } + cudaStream_t st = nullptr; + if (!ok(cudaStreamCreate(&st), "stream")) return 1; + const int64_t scratch_elems = 32ll << 20; // GEMM_SCRATCH in src/prefill/prefill.cpp + const QType qts[] = {{8, 32, 34, {0, -1}, "Q8_0"}, {12, 256, 144, {0, 2}, "Q4_K"}, {13, 256, 176, {0, 2}, "Q5_K"}, {14, 256, 210, {208, -1}, "Q6_K"}}; + // the dense projections of Qwen3.8-Flash-Next (index of the IQ3_S pack: attn_q 12288 x 2560, attn_qkv 10240, attn_gate 6144, ssm_out / attn_output + // 2560 x 6144, the shared expert 640, attn_k / attn_v 512), in each type; the 248320-row head last (larger than the scratch) + std::vector shapes; + for (int q = 0; q < 4; ++q) { + shapes.push_back({q, 12288, 2560, "attn_q", true}); + shapes.push_back({q, 10240, 2560, "attn_qkv", true}); + shapes.push_back({q, 6144, 2560, "attn_gate", true}); + shapes.push_back({q, 2560, 6144, "ssm_out", true}); + shapes.push_back({q, 640, 2560, "shexp_gate", true}); + shapes.push_back({q, 2560, 640, "shexp_down", true}); + shapes.push_back({q, 512, 2560, "attn_k", true}); + } + shapes.push_back({0, 248320, 2560, "output head (bigger than the scratch)", false}); + std::mt19937 rng(20261103u); + std::vector ws; + for (const Shape& s : shapes) { + const QType& q = qts[s.qt]; + if (s.K % q.elems != 0) continue; + const size_t blocks = (size_t) (s.N * s.K / q.elems), bytes = blocks * (size_t) q.bytes; + std::vector h(bytes); + for (auto& b : h) b = (uint8_t) (rng() & 0xff); + for (size_t k = 0; k < blocks; ++k) + for (int at : q.at) + if (at >= 0) { const uint16_t v = sane_half(rng); std::memcpy(&h[k * (size_t) q.bytes + (size_t) at], &v, 2); } + Weight w; w.s = s; + if (!ok(cudaMalloc(&w.dev, bytes), "W") || !ok(cudaMemcpy(w.dev, h.data(), bytes, cudaMemcpyHostToDevice), "W copy")) return 1; + ws.push_back(w); + } + int64_t max_k = 0, max_n = 0; + for (const Weight& w : ws) { if (w.s.K > max_k) max_k = w.s.K; if (w.s.N > max_n) max_n = w.s.N; } + const int64_t Ts[] = {16, 64, 512, 4096}; + const int64_t Tmax = 4096; + uint16_t* X = nullptr; + float* Y[2] = {nullptr, nullptr}; + if (!ok(cudaMalloc((void**) &X, (size_t) Tmax * max_k * 2), "X") || !ok(cudaMalloc((void**) &Y[0], (size_t) Tmax * max_n * 4), "Y0") || + !ok(cudaMalloc((void**) &Y[1], (size_t) Tmax * max_n * 4), "Y1")) + return 1; + fill_f16<<<(unsigned) ((Tmax * max_k + 255) / 256), 256, 0, st>>>((__half*) X, Tmax * max_k, 17u, 2.0f); + if (!ok(cudaStreamSynchronize(st), "fill")) return 1; + + int bad = 0, bad_other = 0, checked = 0; + for (const Weight& w : ws) { + for (const int64_t T : Ts) { + std::vector out[2]; + for (int on = 0; on < 2; ++on) { + setenv("STRATA_PF_W16_CACHE_MIB", on ? "auto" : "0", 1); + strata::prefill::Gemm gemm; // a fresh one reads the variable at its first native() + std::string err; + if (!gemm.init(st, scratch_elems, err)) { std::printf("FAIL: %s\n", err.c_str()); return 1; } + cudaMemset(Y[on], 0xff, (size_t) T * max_n * 4); + for (int rep = 0; rep < (on ? 2 : 1); ++rep) // cached: the first call fills the cache, the second reads it + gemm.native(X, qts[w.s.qt].type, w.dev, Y[on], T, w.s.N, w.s.K, max_n, 0.0f); + if (!ok(cudaStreamSynchronize(st), "run") || !ok(cudaDeviceSynchronize(), "device")) return 1; + out[on].resize((size_t) T * max_n * 4); + cudaMemcpy(out[on].data(), Y[on], out[on].size(), cudaMemcpyDeviceToHost); + } + long long diff = 0; + for (int64_t r = 0; r < T; ++r) + if (std::memcmp(&out[0][(size_t) r * max_n * 4], &out[1][(size_t) r * max_n * 4], (size_t) w.s.N * 4) != 0) ++diff; + const bool fits = w.s.N * w.s.K <= scratch_elems; + if (w.s.production) ++checked; + if (diff || !w.s.production) + std::printf(" %-5s %-22s N %6lld K %4lld T %5lld%s: %s\n", qts[w.s.qt].name, w.s.what, (long long) w.s.N, (long long) w.s.K, (long long) T, + fits ? "" : " (sliced when uncached)", diff ? "*** DIFFERS ***" : "bitwise equal"); + if (diff) { if (w.s.production) ++bad; else ++bad_other; } + } + } + std::printf("gemm_w16_parity: %d production (shape, type, T) combinations checked, %d differ; head (not part of the verdict) %s\n", checked, bad, + bad_other ? "differs" : "equal"); + std::printf("gemm_w16_parity: %s\n", bad ? "FAILED" : "OK"); + return bad ? 1 : 0; +} diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 5392c9fa94..b2e767532d 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -84,6 +84,7 @@ #include "strata/core/dflash2.hpp" #include "strata/core/draft_cap.hpp" #endif +#include "strata/prefill/gemm.hpp" #include "strata/prefill/kva.hpp" #include "strata/prefill/kva_floor.hpp" #include "strata/prefill/prefill.hpp" @@ -7865,6 +7866,12 @@ int main(int argc, char** argv) { } #endif } +#if !defined(STRATA_USE_HIP) && !defined(STRATA_GPU_METAL) && !defined(_WIN32) + // 0.2.10.1 (round0212 item 7): the FP16 weight cache is on when the engine runs batch slots on Linux CUDA. Bit-exact against the uncached + // form (gemm_w16_parity, greedy identity 12/12, first-token KL 0); +3.8% (c=1) .. +6.6% (c=8) total tokens/s on l620. It takes only VRAM beyond + // 3 GiB kept free; STRATA_PF_W16_CACHE_MIB=0 turns it off, =N is an explicit budget. + if (o.batch >= 2) strata::prefill::Gemm::set_w16_auto_default(true); +#endif // #533: --vram-elastic: the cache in physical segments (the VRAM command resizes it between requests). One GPU, // no helper caches, serve mode: anything else keeps the one cudaMalloc, said once. if (o.vram_elastic) {