From fe6c5260ec34b9557f2ab979bd0ca0fb58305ec9 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Sat, 3 Oct 2026 03:40:48 +0200 Subject: [PATCH] Verify: the hyper-connection read from the GGUF's Q8_0 projections (opt-in STRATA_HC_Q8=1; S23's parked patch) NativeDense keeps the six hc_* projections per layer as stored (Q8_0, 0.65 GiB) beside the pack's BF16 copy (1.20 GiB, --compat-bf16's rounding); the verify window's fused_gr_read_multi then reads them with split-K kernels: gr_down_q8 (10 row groups + the inject rows x 16 chunks of 640 columns, the norm folded in as unscaled partials + per-chunk sums of squares) and gr_up_q8. Every other path keeps BF16. Another summation order and the GGUF's own values: output-changing, so opt-in and quality-checked separately. Also the STRATA_GR_V3=2 knob (the two-half v3 split where only 64 KiB of LDS is available). Taken over from S23 (aurora_s23/hc_q8_grv3s2.diff) unchanged. --- include/strata/core/weights.hpp | 3 + include/strata/kernels/fused_gr.hpp | 5 + src/core/native_dense.cpp | 40 ++++- src/core/verify.cpp | 2 + src/kernels/cuda/fused_gr.cu | 237 +++++++++++++++++++++++++++- 5 files changed, 284 insertions(+), 3 deletions(-) diff --git a/include/strata/core/weights.hpp b/include/strata/core/weights.hpp index 5c6414f8..a9949123 100644 --- a/include/strata/core/weights.hpp +++ b/include/strata/core/weights.hpp @@ -90,6 +90,9 @@ struct WeightRef { const void* native_data = nullptr; void* native_q8_1 = nullptr; int native_type = -1; + /// S23 experiment (STRATA_HC_Q8=1): a hyper-connection projection's Q8_0 bytes as the GGUF stores them, owned by + /// NativeDense, for the verify window's read (every other path keeps the pack's BF16 `data`) + const void* hc_q8 = nullptr; /// Plan v0.3 P1: false when the loader SKIPPED this tensor's canonical bytes because another form serves it /// (native GGUF projections, the native head). The metadata above stays valid; `data` is null. bool resident = true; diff --git a/include/strata/kernels/fused_gr.hpp b/include/strata/kernels/fused_gr.hpp index 40eff30f..427eae20 100644 --- a/include/strata/kernels/fused_gr.hpp +++ b/include/strata/kernels/fused_gr.hpp @@ -36,6 +36,11 @@ struct FusedGrArgs { float* rs = nullptr; ///< workspace, hc floats float* inject_out = nullptr; ///< hc floats (when w_inject) float* mixed = nullptr; ///< n_embd + /// S23 experiment (STRATA_HC_Q8=1): the GGUF's Q8_0 projections (null: the BF16 ones above); fused_gr_read_multi + /// only + const uint8_t* q8_down = nullptr; ///< Q8_0 [hc_lr][hc*n_embd] + const uint8_t* q8_up = nullptr; ///< Q8_0 [hc*n_embd][hc_lr] + const uint8_t* q8_inject = nullptr;///< Q8_0 [hc][hc*n_embd] }; bool fused_gr_supported(int64_t n_embd, int64_t hc, int64_t hc_lr); diff --git a/src/core/native_dense.cpp b/src/core/native_dense.cpp index 4a86d48c..6abc9f26 100644 --- a/src/core/native_dense.cpp +++ b/src/core/native_dense.cpp @@ -6,6 +6,8 @@ #include #include #include +#include +#include #include #include #include @@ -13,6 +15,18 @@ namespace strata::core { namespace { +// S23 experiment (STRATA_HC_Q8=1): the hyper-connection projections' Q8_0 bytes for the verify read +bool hc_q8_requested() { + static const bool on = [] { const char* v = std::getenv("STRATA_HC_Q8"); return v != nullptr && v[0] == '1'; }(); + return on; +} +bool hc_q8_name(const std::string& name) { + if (name.rfind("blk.", 0) != 0) return false; + static const char* suffixes[] = {".hc_attn_down.weight", ".hc_attn_up.weight", ".hc_attn_inject.weight", + ".hc_ffn_down.weight", ".hc_ffn_up.weight", ".hc_ffn_inject.weight"}; + for (const char* suffix : suffixes) if (name.ends_with(suffix)) return true; + return false; +} bool eligible(const strata::TensorInfo& tensor, bool include_ple_key) { const auto& name = tensor.name; if (name.rfind("blk.", 0) != 0) return false; @@ -76,7 +90,7 @@ bool NativeDense::load(const std::vector& shards, WeightTable& tabl std::vector pending; std::set seen; int max_in = 0; - uint64_t total = 0; + uint64_t total = 0, hc_q8_bytes = 0; uint64_t split_count = 0, split_tensors = 0; std::set split_numbers; bool have_architecture = false; @@ -170,6 +184,25 @@ bool NativeDense::load(const std::vector& shards, WeightTable& tabl total += bytes; pending.push_back(Pending{&ref, (int) tensor.type, bytes, std::move(data)}); } + if (hc_q8_requested()) + for (const auto& tensor : gguf.tensors()) { + if (tensor.type != 8 || !hc_q8_name(tensor.name) || tensor.shape.size() != 2) continue; + auto found = table.table_.find(tensor.name); + if (found == table.table_.end() || found->second.hc_q8 != nullptr) continue; + auto& ref = found->second; + if (tensor.shape[0] != (uint64_t) ref.ne0 || tensor.shape[1] != (uint64_t) ref.ne1 || ref.ne0 % 32 != 0) { + err = "native dense (STRATA_HC_Q8): incompatible matrix " + tensor.name; return false; + } + const uint64_t bytes = (uint64_t) ref.ne0 / 32 * 34 * (uint64_t) ref.ne1; + void* p = nullptr; + if (cudaMalloc(&p, bytes) != cudaSuccess || + cudaMemcpy(p, gguf.tensor_data(tensor), bytes, cudaMemcpyHostToDevice) != cudaSuccess) { + err = "native dense (STRATA_HC_Q8) upload " + tensor.name; return false; + } + ref.hc_q8 = p; + weights_.push_back(p); + hc_q8_bytes += bytes; + } } if (pending.empty()) { err = "native dense: no supported GDN/QSA matrices in supplied shards"; return false; } void* allocation = nullptr; @@ -185,7 +218,10 @@ bool NativeDense::load(const std::vector& shards, WeightTable& tabl weights_.push_back(item.data.release()); } scratch_ = scratch.release(); - bytes_ = total; + bytes_ = total + hc_q8_bytes; + if (hc_q8_requested()) + std::fprintf(stderr, "strata: STRATA_HC_Q8=1: %.2f GiB of Q8_0 hyper-connection projections for the verify read\n", + (double) hc_q8_bytes / 1073741824.0); return true; } catch (const std::exception& error) { err = std::string("native dense: ") + error.what(); diff --git a/src/core/verify.cpp b/src/core/verify.cpp index 999f85d8..e631ee74 100644 --- a/src/core/verify.cpp +++ b/src/core/verify.cpp @@ -496,6 +496,8 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) { a.bo_prev = bo_ + t * N; a.inj_prev = inj_prev + t * HC; a.w_norm = (const float*) wn[half]->data; a.w_down = (const uint16_t*) wd[half]->data; a.w_up = (const uint16_t*) wu[half]->data; a.w_inject = (const uint16_t*) wi[half]->data; + a.q8_down = (const uint8_t*) wd[half]->hc_q8; a.q8_up = (const uint8_t*) wu[half]->hc_q8; + a.q8_inject = (const uint8_t*) wi[half]->hc_q8; a.eps = EPS; a.lo = lo_ + t * g.hc_lr; a.rs = rs_ + t * HC; a.inject_out = inj_out + t * HC; a.mixed = mixed_ + t * N; } diff --git a/src/kernels/cuda/fused_gr.cu b/src/kernels/cuda/fused_gr.cu index e7766fb7..d6515ac3 100644 --- a/src/kernels/cuda/fused_gr.cu +++ b/src/kernels/cuda/fused_gr.cu @@ -4,6 +4,7 @@ #include "strata/kernels/bf16_bits.hpp" #include "strata/kernels/verify_kernels.hpp" +#include #include #include @@ -798,6 +799,228 @@ int env_variant() { // per device: the variant `fused_gr_check` chose (0 = not checked yet) std::atomic g_variant[64]; +// ================================ S23 experiment (STRATA_HC_Q8=1): the read with the GGUF's Q8_0 projections ====== +// The pack holds the hyper-connection projections as BF16 (iq_pack.py --compat-bf16 rounds the GGUF's Q8_0 values to +// BF16: 1.20 GiB per verify step); this read takes the Q8_0 bytes as stored (0.65 GiB - and the GGUF's own values, +// not a rounding of them). The layout is the stream split of the v3 read above, at 4 chunks per stream: `down` runs +// on (10 groups of 32 rows + the inject rows) x 16 chunks of 640 columns = 176 blocks, each staging its chunk of +// R' * w_norm (unscaled) for the T tokens and writing the chunk's partial dots and sums of squares; `up` sums them +// per stream in a fixed order, applies rs, SwiGLU-free silu, and runs the default epilogue. Another summation order +// and other weights than the default read: an output-changing step (measured, quality-checked). +constexpr int Q8B = 34; // Q8_0 block: fp16 d + 32 int8 +constexpr int Q8_RPW = 4; // down rows per warp +constexpr int Q8_RG = LR / (WARPS * Q8_RPW); // 10 row groups (+1: the inject rows) +constexpr int Q8_KC = 640, Q8_NKC = D / Q8_KC; // 16 chunks, 4 per stream +constexpr int Q8_CPS = N / Q8_KC; // chunks per stream +constexpr int Q8_SPB = Q8_KC / 128; // 5 steps of 4 Q8_0 blocks (8 lanes x 4 values each) +__device__ __forceinline__ uint32_t q8_ld16(const uint8_t* p) { return *(const uint16_t*) p; } +__device__ __forceinline__ float q8_half(uint32_t h) { return __half2float(__ushort_as_half((unsigned short) h)); } +__device__ __forceinline__ float4 q8_four(uint32_t q, float d) { + return make_float4(d * (float) (int8_t) (q & 255u), d * (float) (int8_t) ((q >> 8) & 255u), + d * (float) (int8_t) ((q >> 16) & 255u), d * (float) (int8_t) (q >> 24)); +} + +template +__global__ void __launch_bounds__(THREADS) gr_down_q8_kernel(GrMulti m, float* __restrict__ part, float* __restrict__ ssg) { + __shared__ __align__(16) float xs[T][Q8_KC]; + __shared__ float red[WARPS][T]; + const int t = threadIdx.x, lane = t & 31, warp = t >> 5; + const int rg = blockIdx.x, kc = blockIdx.y, c = kc / Q8_CPS; + const bool inj = rg == Q8_RG; + const int nrows = inj ? ((m.a[0].q8_inject != nullptr && warp < HC) ? 1 : 0) : Q8_RPW; + const int row0 = inj ? warp : (rg * WARPS + warp) * Q8_RPW; + const uint8_t* wb = inj ? m.a[0].q8_inject : m.a[0].q8_down; + const int sub = lane & 7, bl = lane >> 3; // lanes 8b..8b+7: one Q8_0 block, 4 values each + uint32_t q[Q8_RPW][Q8_SPB]; + float dq[Q8_RPW][Q8_SPB]; +#pragma unroll + for (int r = 0; r < Q8_RPW; ++r) { + if (r >= nrows) break; + const uint8_t* row = wb + (size_t) (row0 + r) * (D / 32) * Q8B + (size_t) kc * (Q8_KC / 32) * Q8B; +#pragma unroll + for (int s = 0; s < Q8_SPB; ++s) { + const uint8_t* blk = row + (4 * s + bl) * Q8B; + dq[r][s] = q8_half(q8_ld16(blk)); + q[r][s] = q8_ld16(blk + 2 + 4 * sub) | q8_ld16(blk + 4 + 4 * sub) << 16; + } + } + float ssp[T]; +#pragma unroll + for (int k = 0; k < T; ++k) { + ssp[k] = 0.0f; + const FusedGrArgs& a = m.a[k]; + const float gw = a.apply ? 2.0f * sigmoidf_(a.inj_prev[c] / (float) HC) : 0.0f; + for (int i = t; i < Q8_KC / 4; i += THREADS) { + const int col = kc * Q8_KC + 4 * i; + float4 r = *reinterpret_cast(a.R + col); + if (a.apply) { + const float4 b = *reinterpret_cast(a.bo_prev + (col - c * N)); + r.x = fmaf(b.x, gw, r.x); r.y = fmaf(b.y, gw, r.y); + r.z = fmaf(b.z, gw, r.z); r.w = fmaf(b.w, gw, r.w); + } + const float4 g = *reinterpret_cast(a.w_norm + col); + ssp[k] += r.x * r.x + r.y * r.y + r.z * r.z + r.w * r.w; + *reinterpret_cast(&xs[k][4 * i]) = make_float4(r.x * g.x, r.y * g.y, r.z * g.z, r.w * g.w); + } + } +#pragma unroll + for (int k = 0; k < T; ++k) { + const float v = warp_sum(ssp[k]); + if (lane == 0) red[warp][k] = v; + } + __syncthreads(); + if (rg == 0 && t < T) { + float s = 0.0f; + for (int w = 0; w < WARPS; ++w) s += red[w][t]; + ssg[t * Q8_NKC + kc] = s; + } + if (nrows == 0) return; +#pragma unroll + for (int r = 0; r < Q8_RPW; ++r) { + if (r >= nrows) break; + float acc[T]; +#pragma unroll + for (int k = 0; k < T; ++k) acc[k] = 0.0f; +#pragma unroll + for (int s = 0; s < Q8_SPB; ++s) { + const float4 w = q8_four(q[r][s], dq[r][s]); + const int v = 32 * (4 * s + bl) + 4 * sub; +#pragma unroll + for (int k = 0; k < T; ++k) { + const float4 x = *reinterpret_cast(&xs[k][v]); + acc[k] = fmaf(w.x, x.x, acc[k]); acc[k] = fmaf(w.y, x.y, acc[k]); + acc[k] = fmaf(w.z, x.z, acc[k]); acc[k] = fmaf(w.w, x.w, acc[k]); + } + } + const int prow = inj ? LR + warp : row0 + r; +#pragma unroll + for (int k = 0; k < T; ++k) { + const float v = warp_sum(acc[k]); + if (lane == 0) part[((size_t) k * Q8_NKC + kc) * PR + prow] = v; + } + } +} + +template +__global__ void __launch_bounds__(THREADS) gr_up_q8_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][UPM_COLS]; + const int t = threadIdx.x, lane = t & 31, warp = t >> 5; + const int d0 = blockIdx.x * UPM_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 += 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 * UPM_COLS / WARPS; // 8 rows per warp +#pragma unroll 1 + for (int qq = 0; qq < RPW8; ++qq) { + const int r = warp + qq * WARPS; + const int c = r / UPM_COLS, dd = r - c * UPM_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(&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 * UPM_COLS; i += THREADS) { + const int k = i / UPM_COLS, col = i - k * UPM_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; + } +} + +template void launch_q8_t(const GrMulti& m, float* part, float* ssg, cudaStream_t st, unsigned long long* stamp_buf, + int stamp_i0) { + if (stamp_buf) gpu_stamp(stamp_buf, stamp_i0, (void*) st); + gr_down_q8_kernel<<>>(m, part, ssg); + if (stamp_buf) gpu_stamp(stamp_buf, stamp_i0 + 1, (void*) st); + gr_up_q8_kernel<<>>(m, part, ssg); +} +void launch_q8(const GrMulti& m, float* scratch, cudaStream_t st, unsigned long long* stamp_buf, int stamp_i0) { + float* part = scratch; + float* ssg = scratch + (size_t) m.T * Q8_NKC * PR; + switch (m.T) { + case 1: launch_q8_t<1>(m, part, ssg, st, stamp_buf, stamp_i0); break; + case 2: launch_q8_t<2>(m, part, ssg, st, stamp_buf, stamp_i0); break; + case 3: launch_q8_t<3>(m, part, ssg, st, stamp_buf, stamp_i0); break; + case 4: launch_q8_t<4>(m, part, ssg, st, stamp_buf, stamp_i0); break; + case 5: launch_q8_t<5>(m, part, ssg, st, stamp_buf, stamp_i0); break; + case 6: launch_q8_t<6>(m, part, ssg, st, stamp_buf, stamp_i0); break; + case 7: launch_q8_t<7>(m, part, ssg, st, stamp_buf, stamp_i0); break; + default: launch_q8_t<8>(m, part, ssg, st, stamp_buf, stamp_i0); break; + } +} + } // namespace void fused_gr_read_multi(const FusedGrArgs* a, int n_tok, float* xn_scratch, void* stream, unsigned long long* stamp_buf, @@ -812,7 +1035,8 @@ void fused_gr_read_multi(const FusedGrArgs* a, int n_tok, float* xn_scratch, voi const FusedGrArgs& x = a[t]; if (!x.R || !x.w_norm || !x.w_down || !x.w_up || !x.lo || !x.rs || !x.mixed || (x.w_inject && !x.inject_out) || (x.apply && (!x.bo_prev || !x.inj_prev || !x.R_out)) || x.w_down != a[0].w_down || x.w_up != a[0].w_up || - x.w_inject != a[0].w_inject || x.w_norm != a[0].w_norm) { + x.w_inject != a[0].w_inject || x.w_norm != a[0].w_norm || x.q8_down != a[0].q8_down || + x.q8_up != a[0].q8_up || x.q8_inject != a[0].q8_inject) { std::fprintf(stderr, "fused_gr_read_multi: invalid arguments for token %d\n", t); std::exit(1); } @@ -820,6 +1044,15 @@ void fused_gr_read_multi(const FusedGrArgs* a, int n_tok, float* xn_scratch, voi m.xn = xn_scratch; m.T = n_tok; cudaStream_t st = (cudaStream_t) stream; + if (a[0].q8_down != nullptr && a[0].q8_up != nullptr && (a[0].w_inject == nullptr || a[0].q8_inject != nullptr)) { + launch_q8(m, xn_scratch, st, stamp_buf, stamp_i0); // S23 experiment: STRATA_HC_Q8=1 + const cudaError_t eq = cudaGetLastError(); + if (eq != cudaSuccess) { + std::fprintf(stderr, "fused_gr_read_multi q8: %s\n", cudaGetErrorString(eq)); + std::exit(1); + } + return; + } // STRATA_GR_V3=1: the two-kernel read above (another summation order - opt-in) static const bool v3 = [] { const char* v = std::getenv("STRATA_GR_V3"); return v != nullptr && std::atoi(v) != 0; }(); static int split3[64] = {}; // per device: 0 = not decided yet, 1 / 2 = column halves S, -1 = does not fit @@ -835,6 +1068,8 @@ void fused_gr_read_multi(const FusedGrArgs* a, int n_tok, float* xn_scratch, voi if (need1 <= limit && cudaFuncSetAttribute(gr_down_v3_kernel<1>, cudaFuncAttributeMaxDynamicSharedMemorySize, need1) == cudaSuccess) split = 1; + else if (need2 <= limit && std::getenv("STRATA_GR_V3") != nullptr && std::atoi(std::getenv("STRATA_GR_V3")) == 2) + split = 2; // S23 experiment: STRATA_GR_V3=2 takes the two-half split anyway (gfx1151: 64 KiB) else if (need2 <= limit) // #375 (kenh0u): the S = 2 split (a 64 KB opt-in card: Turing) disagrees with itself in gr_parity // (graph replay vs direct call) - such a card keeps the default read until that split is fixed