Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.
C++
STRATA_HC_Q8=1 on a bf16 pack: the hyper-connection down/up projections are quantized to Q8_0 on the GPU at load and read by the Q8 multi read; Q8 up kernel with 32-column blocks that quantize their own q8_1 group (HIP); bench: cold Q8 arm and Q8-vs-bf16 error check
Niko12210a379a1f6240
diff --git a/include/strata/kernels/fused_gr.hpp b/include/strata/kernels/fused_gr.hppindex 982b33bdb..d689652ba 100644--- a/include/strata/kernels/fused_gr.hpp+++ b/include/strata/kernels/fused_gr.hpp@@ -49,6 +49,9 @@ struct FusedGrArgs {bool fused_gr_supported(int64_t n_embd, int64_t hc, int64_t hc_lr);void fused_gr_read(const FusedGrArgs& a, void* stream);+/// STRATA_HC_Q8 on a bf16 pack: `out` (n_blocks * 34 bytes) = the Q8_0 blocks (fp16 d = amax / 127, round(x / d), ggml's+/// reference rounding) of `n_blocks * 32` bf16 values on the device.+void fused_gr_quantize_q8_0(const uint16_t* bf16, uint8_t* out, int64_t n_blocks, void* stream);/// Plan v0.3 P6: the same read for up to 8 tokens that share the weights (a verify window): the weights are read/// once for all of them. `a[t]` is token t's arguments (its own R, pending write, lo, rs, inject, mixed; the fourdiff --git a/src/core/native_dense.cpp b/src/core/native_dense.cppindex e0775a6a1..6cf736f06 100644--- a/src/core/native_dense.cpp+++ b/src/core/native_dense.cpp@@ -2,6 +2,7 @@#include <cstdlib>#include "strata/core/weights.hpp"#include "strata/artifact/gguf_reader.hpp"+#include "strata/kernels/fused_gr.hpp"#include "strata/kernels/native_mmvq.hpp"#include "strata/platform/memory.hpp"@@ -347,6 +348,30 @@ bool NativeDense::load(const std::vector<std::string>& shards, WeightTable& tablhc_q8_bytes += bytes;}}+ if (hc_q8_requested()) {+ // a bf16 pack stores no Q8_0 copy: quantize the layers' down / up projections from the arena's bf16 on the+ // GPU (the inject rows stay bf16, 82 KB; the final mixer stays bf16: it runs once a window)+ uint64_t made = 0;+ for (auto& [name, ref] : table.table_) {+ if (ref.hc_q8 != nullptr || ref.data == nullptr || name.rfind("blk.", 0) != 0 || !in_range(name) ||+ !hc_q8_name(name) || !(name.ends_with("_down.weight") || name.ends_with("_up.weight"))) continue;+ if (ref.ne0 % 32 != 0 || ref.elements != ref.ne0 * ref.ne1 || ref.bytes != (uint64_t) ref.elements * 2) {+ err = "native dense (STRATA_HC_Q8): the arena copy of " + name + " is not bf16"; return false;+ }+ const uint64_t nb = (uint64_t) ref.elements / 32, bytes = nb * 34;+ void* p = nullptr;+ if (cudaMalloc(&p, bytes) != cudaSuccess) { err = "native dense (STRATA_HC_Q8) alloc " + name; return false; }+ strata::kernels::fused_gr_quantize_q8_0((const uint16_t*) ref.data, (uint8_t*) p, (int64_t) nb, nullptr);+ ref.hc_q8 = p;+ weights_.push_back(p);+ hc_q8_bytes += bytes;+ allocated += alloc_bytes(bytes);+ ++made;+ }+ if (made && (cudaGetLastError() != cudaSuccess || cudaDeviceSynchronize() != cudaSuccess)) {+ err = "native dense (STRATA_HC_Q8): quantizing the projections failed"; return false;+ }+ }if (pending.empty()) { err = "native dense: no supported GDN/QSA matrices in supplied shards"; return false; }void* allocation = nullptr;const auto status = cudaMalloc(&allocation, strata::kernels::native_q8_1_bytes(max_in));diff --git a/src/kernels/cuda/fused_gr.cu b/src/kernels/cuda/fused_gr.cuindex 766ac2ede..c5422c0fd 100644--- a/src/kernels/cuda/fused_gr.cu+++ b/src/kernels/cuda/fused_gr.cu@@ -1558,6 +1558,123 @@ __global__ void __launch_bounds__(THREADS) gr_up_q8_kernel(GrMulti m, const floa}+// STRATA_HC_Q8 on HIP gfx12: gr_up_q8_kernel with 32 columns / 512 threads a block, so a block owns a whole q8_1 group+// (STRATA_QFUSE_HC: no counter, no fence; the same arithmetic per value as gr_up_q8_kernel)+template <int T>+__global__ void __launch_bounds__(Q32_THREADS) gr_up_q8_q32_kernel(GrMulti m, const float* __restrict__ part,+ const float* __restrict__ ssg) {+ __shared__ __align__(16) float lo[T][LR];+ __shared__ float rsS[T][HC];+ __shared__ float g[T][HC][Q32_COLS];+ const int t = threadIdx.x, lane = t & 31, warp = t >> 5;+ const int d0 = blockIdx.x * Q32_COLS;+ if (t < T * HC) {+ const int k = t / HC, c = t - k * HC;+ float ss = 0.0f;+#pragma unroll+ for (int j = 0; j < Q8_CPS; ++j) ss += ssg[k * Q8_NKC + c * Q8_CPS + j];+ const float r = rsqrtf(ss / (float) N + m.a[k].eps);+ rsS[k][c] = r;+ if (blockIdx.x == 0) m.a[k].rs[c] = r;+ }+ __syncthreads();+ for (int i = t; i < T * PR; i += Q32_THREADS) {+ const int k = i / PR, r = i - k * PR;+ if (r >= LR && (blockIdx.x != 0 || m.a[k].w_inject == nullptr)) continue;+ float sum = 0.0f;+#pragma unroll+ for (int c = 0; c < HC; ++c) {+ float p = 0.0f;+#pragma unroll+ for (int j = 0; j < Q8_CPS; ++j) p += part[((size_t) k * Q8_NKC + c * Q8_CPS + j) * PR + r];+ sum = fmaf(rsS[k][c], p, sum);+ }+ if (r < LR) {+ const float x = sum / (float) HC;+ lo[k][r] = x / (1.0f + __expf(-x));+ } else {+ m.a[k].inject_out[r - LR] = sum;+ }+ }+ __syncthreads();+ constexpr int RPW8 = HC * Q32_COLS / Q32_WARPS; // 8 rows per warp+#pragma unroll 1+ for (int qq = 0; qq < RPW8; ++qq) {+ const int r = warp + qq * Q32_WARPS;+ const int c = r / Q32_COLS, dd = r - c * Q32_COLS, i = c * N + d0 + dd;+ const uint8_t* row = m.a[0].q8_up + (size_t) i * (LR / 32) * Q8B;+ float4 w[3];+#pragma unroll+ for (int s = 0; s < 3; ++s) { // lane: values 4 lane + 128 s (the third step: lanes 0-15)+ const int v = 4 * lane + 128 * s;+ if (v < LR) {+ const uint8_t* blk = row + (v >> 5) * Q8B;+ w[s] = q8_four(q8_ld16(blk + 2 + (v & 31)) | q8_ld16(blk + 4 + (v & 31)) << 16, q8_half(q8_ld16(blk)));+ } else {+ w[s] = make_float4(0.0f, 0.0f, 0.0f, 0.0f);+ }+ }+ float rv = 0.0f, wn = 0.0f, bo = 0.0f, ip = 0.0f;+ bool apply = false;+ if (lane < T) {+ const FusedGrArgs& a = m.a[lane];+ rv = a.R[i];+ wn = a.w_norm[i];+ apply = a.apply;+ if (apply) { bo = a.bo_prev[d0 + dd]; ip = a.inj_prev[c]; }+ }+ float mine = 0.0f;+#pragma unroll+ for (int k = 0; k < T; ++k) {+ float acc = 0.0f;+#pragma unroll+ for (int s = 0; s < 3; ++s) {+ const int v = 4 * lane + 128 * s;+ if (v < LR) {+ const float4 x = *reinterpret_cast<const float4*>(&lo[k][v]);+ acc = fmaf(w[s].x, x.x, acc); acc = fmaf(w[s].y, x.y, acc);+ acc = fmaf(w[s].z, x.z, acc); acc = fmaf(w[s].w, x.w, acc);+ }+ }+ acc = warp_sum(acc);+ if (lane == k) mine = acc;+ }+ if (lane < T) {+ if (apply) {+ rv = fmaf(bo, 2.0f * sigmoidf_(ip / (float) HC), rv);+ m.a[lane].R_out[i] = rv;+ }+ const float x = rv * wn * rsS[lane][c];+ g[lane][c][dd] = x * sigmoidf_(mine);+ }+ }+ __syncthreads();+ for (int i = t; i < T * Q32_COLS; i += Q32_THREADS) {+ const int k = i / Q32_COLS, col = i - k * Q32_COLS;+ float sum = 0.0f;+#pragma unroll+ for (int c = 0; c < HC; ++c) sum += g[k][c][col];+ m.a[k].mixed[d0 + col] = sum / (float) HC;+ }+ __syncthreads();+ // the block owns the whole q8_1 group (32 columns): quantize it here (gr_up_q32_kernel's tail), no counter, no fence+ if (m.a[0].q8_mixed != nullptr && warp < T) {+ const float xi = m.a[warp].mixed[d0 + lane];+ float amax = fabsf(xi), sum = xi;+#pragma unroll+ for (int o = 16; o > 0; o >>= 1) amax = fmaxf(amax, __shfl_xor_sync(0xffffffffu, amax, o));+#pragma unroll+ for (int o = 16; o > 0; o >>= 1) sum += __shfl_xor_sync(0xffffffffu, sum, o);+ const float d = q8_1_finite(amax / 127.0f);+ const int8_t q = q8_1_quant(xi, d, amax);+ GrQ81* y = reinterpret_cast<GrQ81*>(m.a[warp].q8_mixed) + d0 / 32;+ y->qs[lane] = q;+ if (lane == 0) y->ds = q8_1_ds(d, sum);+ }+}+++// S26 STRATA_TSUM=1: gr_down_q8_kernel / gr_up_q8_kernel with each row's T per-token warp sums (and the sums of// squares) as one transposed butterfly (s26_tsum.cuh): bitwise the same values (S26 ws4 harness, every output equal,// T 1-4: 1.00 / 1.03 / 1.03 / 1.15x per read). PF (all rows' weights in registers before the part / lo phase) was@@ -1790,6 +1907,12 @@ template <int T> void launch_q8_t(const GrMulti& m, float* part, float* ssg, cudif (ts) gr_down_q8_fast_kernel<T, 4><<<dim3(LR / (WARPS * 4) + 1, Q8_NKC), THREADS, 0, st>>>(m, part, ssg);else gr_down_q8_kernel<T><<<dim3(Q8_RG + 1, Q8_NKC), THREADS, 0, st>>>(m, part, ssg);if (stamp_buf) gpu_stamp(stamp_buf, stamp_i0 + 1, (void*) st);+#if !STRATA_GR_FAST_BUILD+ if (m.a[0].q8_mixed != nullptr && qfuse_hc_local()) { // HIP (not gfx906): the block owns a q8_1 group+ gr_up_q8_q32_kernel<T><<<Q32_BLOCKS, Q32_THREADS, 0, st>>>(m, part, ssg);+ return;+ }+#endifif (ts) gr_up_q8_fast_kernel<T, false><<<UPM_BLOCKS, THREADS, 0, st>>>(m, part, ssg);else gr_up_q8_kernel<T><<<UPM_BLOCKS, THREADS, 0, st>>>(m, part, ssg);}@@ -1962,6 +2085,29 @@ bool fused_gr_read_multi(const FusedGrArgs* a, int n_tok, float* xn_scratch, voireturn q8;}+// STRATA_HC_Q8 on a bf16 pack: one warp a Q8_0 block, ggml's reference rounding (d = amax / 127 as fp16, q = round(x * (1 / d)))+__global__ void __launch_bounds__(256) gr_quantize_q8_0_kernel(const uint16_t* __restrict__ w, uint8_t* __restrict__ out, int64_t nb) {+ const int64_t b = (int64_t) blockIdx.x * 8 + (threadIdx.x >> 5);+ const int lane = threadIdx.x & 31;+ if (b >= nb) return;+ const float v = __uint_as_float((uint32_t) w[b * 32 + lane] << 16);+ float amax = fabsf(v);+#pragma unroll+ for (int o = 16; o > 0; o >>= 1) amax = fmaxf(amax, __shfl_xor_sync(0xffffffffu, amax, o));+ const float d = amax / 127.0f, id = d != 0.0f ? 1.0f / d : 0.0f;+ uint8_t* o = out + b * 34;+ if (lane == 0) {+ const unsigned short h = __half_as_ushort(__float2half_rn(d));+ o[0] = (uint8_t) (h & 255u);+ o[1] = (uint8_t) (h >> 8);+ }+ o[2 + lane] = (uint8_t) (int8_t) lrintf(roundf(v * id));+}++void fused_gr_quantize_q8_0(const uint16_t* bf16, uint8_t* out, int64_t n_blocks, void* stream) {+ gr_quantize_q8_0_kernel<<<(unsigned) ((n_blocks + 7) / 8), 256, 0, (cudaStream_t) stream>>>(bf16, out, n_blocks);+}+bool fused_gr_supported(int64_t n_embd, int64_t hc, int64_t hc_lr) {return n_embd == N && hc == HC && hc_lr == LR;}diff --git a/src/kernels/fused_gr_bench.cpp b/src/kernels/fused_gr_bench.cppindex ddbfe1e09..585a27fff 100644--- a/src/kernels/fused_gr_bench.cpp+++ b/src/kernels/fused_gr_bench.cpp@@ -26,6 +26,34 @@ static uint16_t bf16(float x) {return (uint16_t) ((u + 0x7fff + ((u >> 16) & 1)) >> 16);}+// Q8_0 (ggml reference rounding: d = amax / 127 as fp16, q = round(x / d)) of a row-major [rows][cols] bf16 matrix+static std::vector<uint8_t> q8_0_of_bf16(const std::vector<uint16_t>& w, size_t cols) {+ std::vector<uint8_t> out(w.size() / 32 * 34);+ for (size_t b = 0; b < w.size() / 32; ++b) {+ float v[32], amax = 0.f;+ for (int i = 0; i < 32; ++i) {+ uint32_t u = (uint32_t) w[b * 32 + i] << 16;+ std::memcpy(&v[i], &u, 4);+ amax = std::fmax(amax, std::fabs(v[i]));+ }+ const float d = amax / 127.f, id = d != 0.f ? 1.f / d : 0.f;+ // fp16 of d+ uint32_t x; std::memcpy(&x, &d, 4);+ const uint32_t sign = (x >> 16) & 0x8000u;+ int32_t e = (int32_t) ((x >> 23) & 0xff) - 127 + 15;+ uint32_t m = x & 0x7fffffu;+ uint16_t h;+ if (e <= 0) h = (uint16_t) sign; // flush denormals: weights here are far from them+ else if (e >= 31) h = (uint16_t) (sign | 0x7c00u);+ else h = (uint16_t) (sign | ((uint32_t) e << 10) | (m >> 13));+ uint8_t* o = &out[b * 34];+ std::memcpy(o, &h, 2);+ for (int i = 0; i < 32; ++i) o[2 + i] = (uint8_t) (int8_t) std::lround(v[i] * id);+ }+ (void) cols;+ return out;+}+int main(int argc, char** argv) {setvbuf(stdout, nullptr, _IONBF, 0);const int iters = argc > 1 ? std::atoi(argv[1]) : 500;@@ -118,22 +146,86 @@ int main(int argc, char** argv) {us[0], us[1], same ? "bitwise equal" : "DIFFERS", h);if (!same) ++failures;}+ { // the Q8_0 read against the bf16 read: relative RMS error of lo / mixed / R / inject, and the q8_1 image against mixed+ const std::vector<uint8_t> qd = q8_0_of_bf16(wd, D), qu = q8_0_of_bf16(wu, LR);+ uint8_t *dqd, *dqu, *dqm; unsigned* dcnt;+ cudaMalloc((void**) &dqd, qd.size()); cudaMemcpy(dqd, qd.data(), qd.size(), cudaMemcpyHostToDevice);+ cudaMalloc((void**) &dqu, qu.size()); cudaMemcpy(dqu, qu.data(), qu.size(), cudaMemcpyHostToDevice);+ cudaMalloc((void**) &dqm, (size_t) TM * (N / 32) * 36); cudaMalloc((void**) &dcnt, (N / 32) * 4);+ cudaMemset(dcnt, 0, (N / 32) * 4);+ for (int T = t_lo; T <= t_hi; ++T) for (int apply = 0; apply < 2; ++apply) {+ K::FusedGrArgs a[TM];+ for (int t = 0; t < T; ++t) {+ a[t].R = dR + (size_t) t * D; a[t].R_out = dR + (size_t) t * D; a[t].apply = apply;+ a[t].bo_prev = dbo + (size_t) t * N; a[t].inj_prev = dinj + (size_t) t * HC;+ a[t].w_norm = dwn; a[t].w_down = dwd; a[t].w_up = dwu; a[t].w_inject = dwi;+ a[t].lo = dlo + (size_t) t * LR; a[t].rs = drs + (size_t) t * HC;+ a[t].inject_out = dio + (size_t) t * HC; a[t].mixed = dmix + (size_t) t * N;+ }+ std::vector<float> o[2];+ std::vector<uint8_t> img;+ for (int q = 0; q < 2; ++q) {+ for (int t = 0; t < T; ++t) {+ a[t].q8_down = q ? dqd : nullptr; a[t].q8_up = q ? dqu : nullptr;+ a[t].q8_mixed = (uint8_t*) dqm + (size_t) t * (N / 32) * 36; a[t].q8_cnt = dcnt;+ }+ cudaMemcpy(dR, R.data(), R.size() * 4, cudaMemcpyHostToDevice);+ cudaMemset(dqm, 0, (size_t) TM * (N / 32) * 36);+ K::fused_gr_read_multi(a, T, dxn, s);+ cudaStreamSynchronize(s);+ o[q].resize((size_t) T * (LR + N + D + HC));+ float* p = o[q].data();+ cudaMemcpy(p, dlo, (size_t) T * LR * 4, cudaMemcpyDeviceToHost); p += (size_t) T * LR;+ cudaMemcpy(p, dmix, (size_t) T * N * 4, cudaMemcpyDeviceToHost); p += (size_t) T * N;+ cudaMemcpy(p, dR, (size_t) T * D * 4, cudaMemcpyDeviceToHost); p += (size_t) T * D;+ cudaMemcpy(p, dio, (size_t) T * HC * 4, cudaMemcpyDeviceToHost);+ if (q) { img.resize((size_t) T * (N / 32) * 36); cudaMemcpy(img.data(), dqm, img.size(), cudaMemcpyDeviceToHost); }+ }+ double num = 0, den = 0, nm = 0, dm = 0;+ for (size_t i = 0; i < (size_t) T * LR; ++i) { num += std::pow(o[1][i] - o[0][i], 2); den += std::pow(o[0][i], 2); }+ for (size_t i = (size_t) T * LR; i < (size_t) T * (LR + N); ++i) { nm += std::pow(o[1][i] - o[0][i], 2); dm += std::pow(o[0][i], 2); }+ double rn = 0; // R (apply) and inject must be identical or close+ for (size_t i = (size_t) T * (LR + N); i < o[0].size(); ++i) rn = std::fmax(rn, std::fabs(o[1][i] - o[0][i]));+ // q8_1 image: 36 bytes = half2 (d, sum) + 32 int8; dequantized against mixed+ double qn = 0, qd2 = 0;+ for (int t = 0; t < T; ++t) for (int g = 0; g < N / 32; ++g) {+ const uint8_t* b = &img[((size_t) t * (N / 32) + g) * 36];+ uint16_t h; std::memcpy(&h, b, 2);+ const uint32_t sg = (h >> 15) & 1, ex = (h >> 10) & 31, mn = h & 1023;+ const float d = (ex ? std::ldexp(1.0f + mn / 1024.0f, (int) ex - 15) : std::ldexp(mn / 1024.0f, -14)) * (sg ? -1.f : 1.f);+ for (int j = 0; j < 32; ++j) {+ const float ref = o[1][(size_t) T * LR + (size_t) t * N + g * 32 + j];+ qn += std::pow(d * (int8_t) b[4 + j] - ref, 2); qd2 += ref * ref;+ }+ }+ std::printf("Q8 vs bf16 T %d apply %d | rel rms lo %.4f mixed %.4f | max |dR,dinject| %.3g | q8_1 image vs mixed rel rms %.4f\n", T, apply,+ std::sqrt(num / den), std::sqrt(nm / dm), rn, std::sqrt(qn / qd2));+ }+ }if (cold_sets > 1) {// the engine reads a different layer's weights every time: rotate over `cold_sets` copies (13 MB each)std::vector<uint16_t*> cwd(cold_sets), cwu(cold_sets), cwi(cold_sets);+ const std::vector<uint8_t> qd = q8_0_of_bf16(wd, D), qu = q8_0_of_bf16(wu, LR);+ std::vector<uint8_t*> cqd(cold_sets), cqu(cold_sets);std::vector<float*> cwn(cold_sets);for (int c = 0; c < cold_sets; ++c) {cudaMalloc((void**) &cwd[c], wd.size() * 2); cudaMemcpy(cwd[c], wd.data(), wd.size() * 2, cudaMemcpyHostToDevice);cudaMalloc((void**) &cwu[c], wu.size() * 2); cudaMemcpy(cwu[c], wu.data(), wu.size() * 2, cudaMemcpyHostToDevice);cudaMalloc((void**) &cwi[c], wi.size() * 2); cudaMemcpy(cwi[c], wi.data(), wi.size() * 2, cudaMemcpyHostToDevice);+ cudaMalloc((void**) &cqd[c], qd.size()); cudaMemcpy(cqd[c], qd.data(), qd.size(), cudaMemcpyHostToDevice);+ cudaMalloc((void**) &cqu[c], qu.size()); cudaMemcpy(cqu[c], qu.data(), qu.size(), cudaMemcpyHostToDevice);cudaMalloc((void**) &cwn[c], wn.size() * 4); cudaMemcpy(cwn[c], wn.data(), wn.size() * 4, cudaMemcpyHostToDevice);}+ uint8_t* dqm; unsigned* dcnt; // the q8_1 image of mixed (STRATA_QFUSE_HC) as the engine asks for it+ cudaMalloc((void**) &dqm, (size_t) TM * (N / 32) * 36); cudaMalloc((void**) &dcnt, (N / 32) * 4);+ cudaMemset(dcnt, 0, (N / 32) * 4);+ const bool with_q = std::getenv("STRATA_BENCH_QMIXED") != nullptr;unsigned long long* dst;cudaMalloc((void**) &dst, (size_t) iters * 4 * 8);std::vector<unsigned long long> hst((size_t) iters * 4);std::vector<K::FusedGrArgs> as((size_t) cold_sets * 8);- for (int T = t_lo; T <= t_hi; ++T) for (int f = 0; f < 2; ++f) {- K::fused_gr_set_fast(f);+ for (int T = t_lo; T <= t_hi; ++T) for (int f = 0; f < 3; ++f) {+ K::fused_gr_set_fast(f == 1);for (int c = 0; c < cold_sets; ++c)for (int t = 0; t < T; ++t) {K::FusedGrArgs& a = as[(size_t) c * 8 + t];@@ -141,6 +233,8 @@ int main(int argc, char** argv) {a.R = dR + (size_t) t * D; a.R_out = dR + (size_t) t * D; a.apply = 1;a.bo_prev = dbo + (size_t) t * N; a.inj_prev = dinj + (size_t) t * HC;a.w_norm = cwn[c]; a.w_down = cwd[c]; a.w_up = cwu[c]; a.w_inject = cwi[c];+ if (with_q) { a.q8_mixed = dqm + (size_t) t * (N / 32) * 36; a.q8_cnt = dcnt; }+ if (f == 2) { a.q8_down = cqd[c]; a.q8_up = cqu[c]; }a.lo = dlo + (size_t) t * LR; a.rs = drs + (size_t) t * HC;a.inject_out = dio + (size_t) t * HC; a.mixed = dmix + (size_t) t * N;}