Skip to content

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_REQ8: hyper-connection projections requantized to int8 + fp32 scale per 32 at load

Benjamin Larsson2f44bf51dd22
diff --git a/include/strata/core/weights.hpp b/include/strata/core/weights.hpp
index d352ba641..fe3723120 100644
--- a/include/strata/core/weights.hpp
+++ b/include/strata/core/weights.hpp
@@ -96,6 +96,9 @@ struct WeightRef {
/// 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;
+ /// STRATA_HC_Q8 (experiment): non-null when `data` holds int8 codes [ne1][ne0] of a BF16 hyper-connection
+ /// projection quantized Q8_0-style (one fp32 scale per 32 contiguous values, `q8_scale[ne1][ne0 / 32]`).
+ const float* q8_scale = nullptr;
};
struct LoadReport {
@@ -136,6 +139,12 @@ public:
bool read_bf16_host(const std::string& name, std::vector<uint16_t>& out, std::string& err) const;
const WeightRef* find(const std::string& name) const;
+ /// STRATA_HC_Q8: the hyper-connection projections (`blk.*.hc_{attn,ffn}_{down,up}.weight`) the loader SKIPPED
+ /// are read from the pack, quantized to int8 + one fp32 scale per 32 values, and uploaded to their own device
+ /// allocation (`q8_scale` set). Returns the bytes uploaded in `bytes`.
+ bool load_hc_q8(const std::string& pack_dir, uint64_t& bytes, std::string& err);
+ /// The tensor names `load_hc_q8` converts, for the loader's skip set.
+ static void hc_q8_names(int64_t n_layers, std::set<std::string>& out);
const std::map<std::string, WeightRef>& all() const { return table_; }
const LoadReport& report() const { return report_; }
diff --git a/include/strata/kernels/fused_gr.hpp b/include/strata/kernels/fused_gr.hpp
index 003242473..95b510161 100644
--- a/include/strata/kernels/fused_gr.hpp
+++ b/include/strata/kernels/fused_gr.hpp
@@ -45,9 +45,15 @@ struct FusedGrArgs {
/// bytes native_quantize_q8_1 would write); q8_cnt = n_embd / 32 zeroed counters owned by the caller (token 0's)
uint8_t* q8_mixed = nullptr;
unsigned* q8_cnt = nullptr;
+ /// STRATA_HC_Q8: non-null when w_down / w_up hold int8 codes (same [row][col] layout) with one fp32 scale per
+ /// 32 contiguous values here ([row][col / 32]). w_inject stays bf16.
+ const float* s_down = nullptr;
+ const float* s_up = nullptr;
};
bool fused_gr_supported(int64_t n_embd, int64_t hc, int64_t hc_lr);
+/// STRATA_HC_Q8: n int8 codes with one fp32 scale per 32 -> bf16 (the prompt path's GEMMs take bf16).
+void hc_q8_to_bf16(const void* codes, const float* scales, uint16_t* out, long long n, void* stream);
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.
diff --git a/src/core/layer.cpp b/src/core/layer.cpp
index 1e6b3b930..7eeb8a0ac 100644
--- a/src/core/layer.cpp
+++ b/src/core/layer.cpp
@@ -1408,11 +1408,11 @@ st_begin(layer, 0, stream);
strata::kernels::FusedGrArgs fa;
fa.R = R; fa.R_out = R; fa.apply = pending_ffn; fa.bo_prev = bb.block_out; fa.inj_prev = bb.inject2;
fa.w_norm = (const float*) w_norm[0]->data; fa.w_down = NativeDense::hc_bf16_view(*w_down[0], stream, 0, 1);
- fa.w_up = NativeDense::hc_bf16_view(*w_up[0], stream, 1, 1); fa.w_inject = (const uint16_t*) w_inject[0]->data;
+ fa.w_up = NativeDense::hc_bf16_view(*w_up[0], stream, 1, 1); fa.s_down = w_down[0]->q8_scale; fa.s_up = w_up[0]->q8_scale; fa.w_inject = (const uint16_t*) w_inject[0]->data;
fa.eps = RMS_EPS; fa.lo = bb.gr.lo; fa.rs = bb.gr_rs; fa.inject_out = bb.inject; fa.mixed = bb.mixed;
strata::kernels::fused_gr_read(fa, stream);
} else {
- gr_read(R, (const float*) w_norm[0]->data, NativeDense::hc_bf16_view(*w_down[0], stream, 0, 1), NativeDense::hc_bf16_view(*w_up[0], stream, 1, 1), (const uint16_t*) w_inject[0]->data, RMS_EPS, gs, bb.gr, bb.mixed, bb.inject, stream);
+ if (w_down[0]->q8_scale || w_up[0]->q8_scale) { err = "STRATA_HC_REQ8 needs the fused hyper-connection read"; return false; } gr_read(R, (const float*) w_norm[0]->data, NativeDense::hc_bf16_view(*w_down[0], stream, 0, 1), NativeDense::hc_bf16_view(*w_up[0], stream, 1, 1), (const uint16_t*) w_inject[0]->data, RMS_EPS, gs, bb.gr, bb.mixed, bb.inject, stream);
}
st_end(layer, 0, stream); dump_half(bb, g, layer, bb.inject, 2 * g.n_embd, g.hc, stream); }
if (run1) {
@@ -1431,11 +1431,11 @@ st_begin(layer, 3, stream);
strata::kernels::FusedGrArgs fa;
fa.R = R; fa.R_out = R; fa.apply = true; fa.bo_prev = bb.block_out; fa.inj_prev = bb.inject;
fa.w_norm = (const float*) w_norm[1]->data; fa.w_down = NativeDense::hc_bf16_view(*w_down[1], stream, 0, 1);
- fa.w_up = NativeDense::hc_bf16_view(*w_up[1], stream, 1, 1); fa.w_inject = (const uint16_t*) w_inject[1]->data;
+ fa.w_up = NativeDense::hc_bf16_view(*w_up[1], stream, 1, 1); fa.s_down = w_down[1]->q8_scale; fa.s_up = w_up[1]->q8_scale; fa.w_inject = (const uint16_t*) w_inject[1]->data;
fa.eps = RMS_EPS; fa.lo = bb.gr.lo; fa.rs = bb.gr_rs; fa.inject_out = bb.inject2; fa.mixed = bb.mixed;
strata::kernels::fused_gr_read(fa, stream);
} else {
- gr_read(R, (const float*) w_norm[1]->data, NativeDense::hc_bf16_view(*w_down[1], stream, 0, 1), NativeDense::hc_bf16_view(*w_up[1], stream, 1, 1), (const uint16_t*) w_inject[1]->data, RMS_EPS, gs, bb.gr, bb.mixed, bb.inject, stream);
+ if (w_down[1]->q8_scale || w_up[1]->q8_scale) { err = "STRATA_HC_REQ8 needs the fused hyper-connection read"; return false; } gr_read(R, (const float*) w_norm[1]->data, NativeDense::hc_bf16_view(*w_down[1], stream, 0, 1), NativeDense::hc_bf16_view(*w_up[1], stream, 1, 1), (const uint16_t*) w_inject[1]->data, RMS_EPS, gs, bb.gr, bb.mixed, bb.inject, stream);
}
st_end(layer, 3, stream); }
if (run4) {
diff --git a/src/core/verify.cpp b/src/core/verify.cpp
index a81181a4e..9237b0d07 100644
--- a/src/core/verify.cpp
+++ b/src/core/verify.cpp
@@ -1124,6 +1124,7 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
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.s_down = wd[half]->q8_scale; a.s_up = wu[half]->q8_scale;
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;
if (qcnt_ != nullptr) { // S26 STRATA_QFUSE: the consumer's q8_1 image written by the read itself
diff --git a/src/core/weights.cpp b/src/core/weights.cpp
index a46c485d3..bb28f0997 100644
--- a/src/core/weights.cpp
+++ b/src/core/weights.cpp
@@ -1,5 +1,7 @@
// src/core/weights.cpp - the dense-weight loader. See the header for the engine-vs-pack distinction.
#include "strata/core/weights.hpp"
+#include <vector>
+#include <cmath>
#include "strata/kernels/f16_bits.hpp"
#include "strata/platform/memory.hpp"
@@ -513,4 +515,88 @@ const WeightRef* WeightTable::find(const std::string& name) const {
return it == table_.end() ? nullptr : &it->second;
}
+void WeightTable::hc_q8_names(int64_t n_layers, std::set<std::string>& out) {
+ for (int64_t l = 0; l < n_layers; ++l)
+ for (const char* h : {"hc_attn_", "hc_ffn_"})
+ for (const char* m : {"down", "up"})
+ out.insert("blk." + std::to_string(l) + "." + h + m + ".weight");
+}
+
+bool WeightTable::load_hc_q8(const std::string& pack_dir, uint64_t& bytes, std::string& err) {
+ bytes = 0;
+ std::vector<WeightRef*> todo;
+ for (auto& [name, w] : table_) {
+ const bool hc = name.find(".hc_attn_down.") != std::string::npos || name.find(".hc_attn_up.") != std::string::npos ||
+ name.find(".hc_ffn_down.") != std::string::npos || name.find(".hc_ffn_up.") != std::string::npos;
+ if (!hc || w.resident) continue;
+ // the pack holds BF16 either promoted to f32 (4 B/elem) or as it is in the GGUF (a native pack, 2 B/elem)
+ if (w.kind != WeightKind::Bf16InF32 || w.ne0 % 32 != 0 ||
+ (w.src_bytes != (uint64_t) w.elements * 4 && w.src_bytes != (uint64_t) w.elements * 2)) {
+ err = "STRATA_HC_REQ8: " + name + " is not a BF16 tensor of whole 32-value blocks";
+ return false;
+ }
+ todo.push_back(&w);
+ }
+ if (todo.empty()) return true;
+ auto al = [](uint64_t b) { return (b + 255) & ~(uint64_t) 255; };
+ uint64_t total = 0;
+ for (const WeightRef* w : todo) total += al((uint64_t) w->elements) + al((uint64_t) w->elements / 32 * 4);
+ uint8_t* dev = nullptr;
+ if (cudaMalloc((void**) &dev, total) != cudaSuccess) { err = "STRATA_HC_REQ8: cudaMalloc failed"; return false; }
+ std::vector<float> src;
+ std::vector<int8_t> q;
+ std::vector<float> sc;
+ uint64_t at = 0;
+ for (WeightRef* w : todo) {
+ const char* fn = file_name(w->file_id);
+ const std::string path = pack_dir + "/" + (fn ? fn : "?");
+ std::FILE* f = std::fopen(path.c_str(), "rb");
+ if (!f) { err = "STRATA_HC_REQ8: cannot open " + path; return false; }
+ src.resize((size_t) w->elements);
+ const bool raw16 = w->src_bytes == (uint64_t) w->elements * 2;
+#if defined(_WIN32)
+ bool ok = _fseeki64(f, (long long) w->src_off, SEEK_SET) == 0;
+#else
+ bool ok = fseeko(f, (off_t) w->src_off, SEEK_SET) == 0; // 64-bit: a pack's dense file can pass 2 GiB
+#endif
+ if (ok && raw16) {
+ std::vector<uint16_t> h(src.size());
+ ok = std::fread(h.data(), 2, h.size(), f) == h.size();
+ for (size_t i = 0; ok && i < h.size(); ++i) {
+ const uint32_t b = (uint32_t) h[i] << 16; // bf16 -> f32 is exact
+ std::memcpy(&src[i], &b, 4);
+ }
+ } else if (ok) {
+ ok = std::fread(src.data(), 4, src.size(), f) == src.size();
+ }
+ std::fclose(f);
+ if (!ok) { err = "STRATA_HC_REQ8: cannot read the source bytes"; return false; }
+ const size_t nb = src.size() / 32;
+ q.resize(src.size());
+ sc.resize(nb);
+ for (size_t b = 0; b < nb; ++b) { // Q8_0's rule: d = amax / 127, q = round-to-nearest-even(x / d)
+ float amax = 0.f;
+ for (int j = 0; j < 32; ++j) amax = std::max(amax, std::fabs(src[b * 32 + j]));
+ const float d = amax / 127.f, id = d > 0.f ? 1.f / d : 0.f;
+ sc[b] = d;
+ for (int j = 0; j < 32; ++j) q[b * 32 + j] = (int8_t) std::nearbyint(src[b * 32 + j] * id);
+ }
+ uint8_t* codes = dev + at;
+ at += al(q.size());
+ float* scales = (float*) (dev + at);
+ at += al(sc.size() * 4);
+ if (cudaMemcpy(codes, q.data(), q.size(), cudaMemcpyHostToDevice) != cudaSuccess ||
+ cudaMemcpy(scales, sc.data(), sc.size() * 4, cudaMemcpyHostToDevice) != cudaSuccess) {
+ err = "STRATA_HC_REQ8: upload failed";
+ return false;
+ }
+ w->data = codes;
+ w->q8_scale = scales;
+ w->resident = true;
+ w->bytes = q.size() + sc.size() * 4;
+ }
+ bytes = total;
+ return true;
+}
+
} // namespace strata::core
diff --git a/src/kernels/cuda/fused_gr.cu b/src/kernels/cuda/fused_gr.cu
index c3c2fc8d9..8adc141e2 100644
--- a/src/kernels/cuda/fused_gr.cu
+++ b/src/kernels/cuda/fused_gr.cu
@@ -78,6 +78,38 @@ __device__ __forceinline__ float dot8(const uint4 w, const float* x) {
return dot8u_ptr(unpack8(w), x);
}
+// STRATA_HC_Q8: a weight chunk of 8 values in one uint4 register either way - bf16 (4 words), or Q8 (8 int8 codes in
+// .x/.y and the chunk's 32-value block scale in .z), so the kernels keep their register budget.
+__device__ __forceinline__ float dot8q(const uint32_t a, const uint32_t b, const float* x) {
+ float acc = 0.0f;
+#pragma unroll
+ for (int i = 0; i < 4; ++i) acc = fmaf((float) (int8_t) (a >> (8 * i)), x[i], acc);
+#pragma unroll
+ for (int i = 0; i < 4; ++i) acc = fmaf((float) (int8_t) (b >> (8 * i)), x[4 + i], acc);
+ return acc;
+}
+/// STRATA_HC_REQ8: the chunk at element `e` (a multiple of 8) of a matrix - bf16 `wb`, or (Q8 and `sq` non-null)
+/// int8 codes with scales `sq`, packed into the same uint4 register as 8 int8 codes in .x/.y and the 32-value block's
+/// scale in .z. Q8 is a template parameter: the BF16 instantiations compile exactly as before.
+template <bool Q8>
+__device__ __forceinline__ uint4 ldw(const uint16_t* wb, const float* sq, size_t e) {
+ if constexpr (Q8) {
+ if (sq != nullptr) {
+ const uint2 v = __ldg(reinterpret_cast<const uint2*>(reinterpret_cast<const int8_t*>(wb) + e));
+ return make_uint4(v.x, v.y, __float_as_uint(__ldg(sq + e / 32)), 0u);
+ }
+ }
+ return __ldg(reinterpret_cast<const uint4*>(wb + e));
+}
+template <bool Q8>
+__device__ __forceinline__ float dotw(const bool q8, const uint4 w, const float* x) {
+ if constexpr (Q8) {
+ if (q8) return __uint_as_float(w.z) * dot8q(w.x, w.y, x);
+ }
+ return dot8(w, x);
+}
+
+template <bool Q8 = false>
__global__ void __launch_bounds__(THREADS) gr_down_kernel(FusedGrArgs a) {
__shared__ __align__(16) float xn[D];
__shared__ float part[WARPS][HC];
@@ -121,11 +153,12 @@ __global__ void __launch_bounds__(THREADS) gr_down_kernel(FusedGrArgs a) {
const bool inject_block = blockIdx.x == DOWN_BLOCKS;
const int row = inject_block ? warp : blockIdx.x * WARPS + warp;
if (inject_block && (a.w_inject == nullptr || warp >= HC)) return;
- const uint16_t* wrow = (inject_block ? a.w_inject : a.w_down) + (size_t) row * D;
- const uint4* w4 = reinterpret_cast<const uint4*>(wrow);
+ const uint16_t* wmat = inject_block ? a.w_inject : a.w_down;
+ const float* sq = (Q8 && !inject_block) ? a.s_down : nullptr; // the inject rows stay bf16
+ const bool q8 = Q8 && !inject_block;
float acc = 0.0f;
#pragma unroll 4
- for (int j = lane; j < D / 8; j += 32) acc += dot8(__ldg(w4 + j), xn + j * 8);
+ for (int j = lane; j < D / 8; j += 32) acc += dotw<Q8>(q8, ldw<Q8>(wmat, sq, (size_t) row * D + (size_t) j * 8), xn + j * 8);
acc = warp_sum(acc);
if (lane != 0) return;
if (inject_block) {
@@ -136,6 +169,7 @@ __global__ void __launch_bounds__(THREADS) gr_down_kernel(FusedGrArgs a) {
}
}
+template <bool Q8 = false>
__global__ void __launch_bounds__(THREADS) gr_up_kernel(FusedGrArgs a) {
__shared__ __align__(16) float lo[LR];
__shared__ float g[HC][UP_COLS];
@@ -146,9 +180,10 @@ __global__ void __launch_bounds__(THREADS) gr_up_kernel(FusedGrArgs a) {
// 128 rows (4 streams x 32 columns), 16 per warp: 320 bf16 = 40 chunks of 8.
for (int r = warp; r < HC * UP_COLS; r += WARPS) {
const int c = r / UP_COLS, dd = r - c * UP_COLS, i = c * N + d0 + dd;
- const uint4* w4 = reinterpret_cast<const uint4*>(a.w_up + (size_t) i * LR);
- float acc = dot8(__ldg(w4 + lane), lo + lane * 8);
- if (lane < LR / 8 - 32) acc += dot8(__ldg(w4 + 32 + lane), lo + (32 + lane) * 8);
+ const bool q8 = Q8;
+ float acc = dotw<Q8>(q8, ldw<Q8>(a.w_up, a.s_up, (size_t) i * LR + (size_t) lane * 8), lo + lane * 8);
+ if (lane < LR / 8 - 32)
+ acc += dotw<Q8>(q8, ldw<Q8>(a.w_up, a.s_up, (size_t) i * LR + (size_t) (32 + lane) * 8), lo + (32 + lane) * 8);
acc = warp_sum(acc);
if (lane == 0) {
float rv = a.R[i];
@@ -263,7 +298,7 @@ __global__ void __launch_bounds__(THREADS) gr_norm_multi_kernel(GrMulti m) {
// which fits all eight tokens in one 40 KiB launch and stages 160 chunks of 8 (5 per lane, half the registers held
// for the weight prefetch); smaller tiles raise how many blocks share an SM (the 41-block grid), e.g. three blocks
// of four tokens instead of one on sm_75.
-template <int TILEV, int MAX_T = kFusedGrMaxT>
+template <int TILEV, int MAX_T = kFusedGrMaxT, bool Q8 = false>
__global__ void __launch_bounds__(THREADS) gr_down_multi_kernel(GrMulti m) {
constexpr int TQ = TILEV / 8 / 32; // uint4 weight chunks per lane per tile
extern __shared__ __align__(16) float tile[]; // [T][TILEV]
@@ -272,8 +307,10 @@ __global__ void __launch_bounds__(THREADS) gr_down_multi_kernel(GrMulti m) {
const bool inject_block = blockIdx.x == DOWN_BLOCKS;
const int row = inject_block ? warp : blockIdx.x * WARPS + warp;
const bool active = !(inject_block && (m.a[0].w_inject == nullptr || warp >= HC));
- const uint16_t* wrow = (inject_block ? m.a[0].w_inject : m.a[0].w_down) + (size_t) (active ? row : 0) * D;
- const uint4* w4 = reinterpret_cast<const uint4*>(wrow);
+ const uint16_t* wmat = inject_block ? m.a[0].w_inject : m.a[0].w_down;
+ const float* sq = (Q8 && !inject_block) ? m.a[0].s_down : nullptr; // the inject rows stay bf16
+ const bool q8 = Q8 && !inject_block;
+ const size_t row0 = (size_t) (active ? row : 0) * D;
float acc[MAX_T];
#pragma unroll
for (int k = 0; k < MAX_T; ++k) acc[k] = 0.0f;
@@ -281,7 +318,7 @@ __global__ void __launch_bounds__(THREADS) gr_down_multi_kernel(GrMulti m) {
uint4 wv[TQ];
if (active) {
#pragma unroll
- for (int q = 0; q < TQ; ++q) wv[q] = __ldg(w4 + base / 8 + lane + 32 * q);
+ for (int q = 0; q < TQ; ++q) wv[q] = ldw<Q8>(wmat, sq, row0 + (size_t) base + (size_t) (lane + 32 * q) * 8);
}
__syncthreads(); // the previous tile is consumed
const float4* src4 = reinterpret_cast<const float4*>(m.xn);
@@ -297,7 +334,7 @@ __global__ void __launch_bounds__(THREADS) gr_down_multi_kernel(GrMulti m) {
const int j = lane + 32 * q;
#pragma unroll
for (int k = 0; k < MAX_T; ++k)
- if (k < T) acc[k] += dot8(wv[q], tile + k * TILEV + j * 8);
+ if (k < T) acc[k] += dotw<Q8>(q8, wv[q], tile + k * TILEV + j * 8);
}
}
if (!active) return;
@@ -322,7 +359,7 @@ constexpr int UPM_BLOCKS = N / UPM_COLS; // 160
// `gr_up_kernel` for T tokens: each row of w_up read once; the T dots reduced by xor so every lane holds every
// sum, and lane k runs token k's epilogue - the T epilogues in parallel instead of one after another.
-template <int MAX_T = kFusedGrMaxT, bool EXACT_T = false>
+template <int MAX_T = kFusedGrMaxT, bool EXACT_T = false, bool Q8 = false>
__global__ void __launch_bounds__(THREADS) gr_up_multi_kernel(GrMulti m) {
__shared__ __align__(16) float lo[MAX_T][LR];
__shared__ float g[MAX_T][HC][UPM_COLS];
@@ -333,9 +370,16 @@ __global__ void __launch_bounds__(THREADS) gr_up_multi_kernel(GrMulti m) {
__syncthreads();
for (int r = warp; r < HC * UPM_COLS; r += WARPS) {
const int c = r / UPM_COLS, dd = r - c * UPM_COLS, i = c * N + d0 + dd;
- const uint4* w4 = reinterpret_cast<const uint4*>(m.a[0].w_up + (size_t) i * LR);
- const Bf16x8 wa = unpack8(__ldg(w4 + lane));
- const Bf16x8 wb = unpack8(lane < LR / 8 - 32 ? __ldg(w4 + 32 + lane) : make_uint4(0, 0, 0, 0));
+ uint4 qa = make_uint4(0, 0, 0, 0), qb = make_uint4(0, 0, 0, 0);
+ Bf16x8 wa{}, wb{};
+ if constexpr (Q8) {
+ qa = ldw<true>(m.a[0].w_up, m.a[0].s_up, (size_t) i * LR + (size_t) lane * 8);
+ if (lane < LR / 8 - 32) qb = ldw<true>(m.a[0].w_up, m.a[0].s_up, (size_t) i * LR + (size_t) (32 + lane) * 8);
+ } else {
+ const uint4* w4 = reinterpret_cast<const uint4*>(m.a[0].w_up + (size_t) i * LR);
+ wa = unpack8(__ldg(w4 + lane));
+ wb = unpack8(lane < LR / 8 - 32 ? __ldg(w4 + 32 + lane) : make_uint4(0, 0, 0, 0));
+ }
// the epilogue inputs of this lane's token, fetched while the dots run
float rv = 0.0f, wn = 0.0f, rsc = 0.0f, bo = 0.0f, ip = 0.0f;
bool apply = false;
@@ -351,8 +395,14 @@ __global__ void __launch_bounds__(THREADS) gr_up_multi_kernel(GrMulti m) {
#pragma unroll
for (int k = 0; k < MAX_T; ++k) {
if (!EXACT_T && k >= T) break;
- float acc = dot8u_ptr(wa, lo[k] + lane * 8);
- if (lane < LR / 8 - 32) acc += dot8u_ptr(wb, lo[k] + (32 + lane) * 8);
+ float acc;
+ if constexpr (Q8) {
+ acc = dotw<true>(true, qa, lo[k] + lane * 8);
+ if (lane < LR / 8 - 32) acc += dotw<true>(true, qb, lo[k] + (32 + lane) * 8);
+ } else {
+ acc = dot8u_ptr(wa, lo[k] + lane * 8);
+ if (lane < LR / 8 - 32) acc += dot8u_ptr(wb, lo[k] + (32 + lane) * 8);
+ }
acc = warp_sum(acc);
if (lane == k) mine = acc;
}
@@ -941,8 +991,12 @@ int down_chunk(bool staged, int* tile_out) {
if (optin > 0 && want > optin) want = optin;
if (small_tile) {
cudaFuncSetAttribute(gr_down_multi_kernel<1280>, cudaFuncAttributeMaxDynamicSharedMemorySize, want);
+ cudaFuncSetAttribute(gr_down_multi_kernel<1280, kFusedGrMaxT, true>, cudaFuncAttributeMaxDynamicSharedMemorySize, want);
+ cudaFuncSetAttribute(gr_down_multi_kernel<1280, 4, true>, cudaFuncAttributeMaxDynamicSharedMemorySize, want);
} else {
cudaFuncSetAttribute(gr_down_multi_kernel<2560>, cudaFuncAttributeMaxDynamicSharedMemorySize, want);
+ cudaFuncSetAttribute(gr_down_multi_kernel<2560, kFusedGrMaxT, true>, cudaFuncAttributeMaxDynamicSharedMemorySize, want);
+ cudaFuncSetAttribute(gr_down_multi_kernel<2560, 4, true>, cudaFuncAttributeMaxDynamicSharedMemorySize, want);
}
auto set_staged_attr = [&](auto kernel, int max_t) {
int ws = (int) (2 * max_t * H_TILE * sizeof(float));
@@ -1031,6 +1085,7 @@ bool qfuse_hc_local() {
}
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
#if STRATA_GR_FAST_BUILD
// gfx906: the latency-hidden norm/up (STRATA_GR_FAST=0: off) - the same sums in the same order as the kernels
// they replace (gr_parity checks the multi-token read against single-token calls bitwise)
@@ -1047,7 +1102,8 @@ void launch_multi(const GrMulti& m, int variant, cudaStream_t st, unsigned long
} else gr_norm_multi_kernel<<<n_tok, THREADS, 0, st>>>(m);
#endif
if (stamp_buf) gpu_stamp(stamp_buf, stamp_i0, (void*) st);
- const bool staged = variant >= kHcStaged;
+ // STRATA_HC_Q8: the staged down kernel reads bf16 only, so Q8 weights take split's down kernel (the plain one)
+ const bool staged = variant >= kHcStaged && m.a[0].s_down == nullptr;
int tv = 2560;
const int chunk_tok = down_chunk(staged, &tv);
const size_t per_tok = (staged ? (size_t) 2 * H_TILE : (size_t) tv) * sizeof(float);
@@ -1083,20 +1139,26 @@ void launch_multi(const GrMulti& m, int variant, cudaStream_t st, unsigned long
else if (max4) gr_down_staged_kernel<4><<<DOWN_BLOCKS + 1, THREADS, smem, st>>>(c);
else gr_down_staged_kernel<><<<DOWN_BLOCKS + 1, THREADS, smem, st>>>(c);
} else if (tv == 1280) {
- if (max4) gr_down_multi_kernel<1280, 4><<<DOWN_BLOCKS + 1, THREADS, smem, st>>>(c);
+ if (q8w) { // STRATA_HC_REQ8
+ if (max4) gr_down_multi_kernel<1280, 4, true><<<DOWN_BLOCKS + 1, THREADS, smem, st>>>(c);
+ else gr_down_multi_kernel<1280, kFusedGrMaxT, true><<<DOWN_BLOCKS + 1, THREADS, smem, st>>>(c);
+ } else if (max4) gr_down_multi_kernel<1280, 4><<<DOWN_BLOCKS + 1, THREADS, smem, st>>>(c);
else gr_down_multi_kernel<1280><<<DOWN_BLOCKS + 1, THREADS, smem, st>>>(c);
} else {
- if (max4) gr_down_multi_kernel<2560, 4><<<DOWN_BLOCKS + 1, THREADS, smem, st>>>(c);
+ if (q8w) { // STRATA_HC_REQ8
+ if (max4) gr_down_multi_kernel<2560, 4, true><<<DOWN_BLOCKS + 1, THREADS, smem, st>>>(c);
+ else gr_down_multi_kernel<2560, kFusedGrMaxT, true><<<DOWN_BLOCKS + 1, THREADS, smem, st>>>(c);
+ } else if (max4) gr_down_multi_kernel<2560, 4><<<DOWN_BLOCKS + 1, THREADS, smem, st>>>(c);
else gr_down_multi_kernel<2560><<<DOWN_BLOCKS + 1, THREADS, smem, st>>>(c);
}
}
if (stamp_buf) gpu_stamp(stamp_buf, stamp_i0 + 1, (void*) st);
#if STRATA_GR_FAST_BUILD
- if (fast) gr_up_fast_kernel<<<UPM_BLOCKS, THREADS, 0, st>>>(m);
+ if (fast && !q8w) gr_up_fast_kernel<<<UPM_BLOCKS, THREADS, 0, st>>>(m);
else
#endif
#if !STRATA_GR_FAST_BUILD
- if (m.a[0].q8_mixed != nullptr && qfuse_hc_local()) {
+ if (!q8w && m.a[0].q8_mixed != nullptr && qfuse_hc_local()) {
switch (no_multi_gr ? kFusedGrMaxT : n_tok) {
case 1: gr_up_q32_kernel<1, true><<<Q32_BLOCKS, Q32_THREADS, 0, st>>>(m); break;
case 2: gr_up_q32_kernel<2, true><<<Q32_BLOCKS, Q32_THREADS, 0, st>>>(m); break;
@@ -1109,6 +1171,17 @@ void launch_multi(const GrMulti& m, int variant, cudaStream_t st, unsigned long
return;
}
#endif
+ if (q8w) { // STRATA_HC_REQ8: the up kernel on int8 rows
+ switch (no_multi_gr ? kFusedGrMaxT : n_tok) {
+ case 1: gr_up_multi_kernel<1, true, true><<<UPM_BLOCKS, THREADS, 0, st>>>(m); break;
+ case 2: gr_up_multi_kernel<2, true, true><<<UPM_BLOCKS, THREADS, 0, st>>>(m); break;
+ case 3: gr_up_multi_kernel<3, true, true><<<UPM_BLOCKS, THREADS, 0, st>>>(m); break;
+ case 4: gr_up_multi_kernel<4, true, true><<<UPM_BLOCKS, THREADS, 0, st>>>(m); break;
+ case 5: gr_up_multi_kernel<5, true, true><<<UPM_BLOCKS, THREADS, 0, st>>>(m); break;
+ case 6: gr_up_multi_kernel<6, true, true><<<UPM_BLOCKS, THREADS, 0, st>>>(m); break;
+ default: gr_up_multi_kernel<kFusedGrMaxT, false, true><<<UPM_BLOCKS, THREADS, 0, st>>>(m); break;
+ }
+ } else
switch (no_multi_gr ? kFusedGrMaxT : n_tok) {
case 1: gr_up_multi_kernel<1, true><<<UPM_BLOCKS, THREADS, 0, st>>>(m); break;
case 2: gr_up_multi_kernel<2, true><<<UPM_BLOCKS, THREADS, 0, st>>>(m); break;
@@ -2022,7 +2095,7 @@ bool fused_gr_read_multi(const FusedGrArgs* a, int n_tok, float* xn_scratch, voi
split3[dev3] = split;
}
}
- const int split = v3 && dev3 >= 0 && dev3 < 64 ? split3[dev3] : -1;
+ const int split = v3 && dev3 >= 0 && dev3 < 64 && a[0].s_down == nullptr && a[0].s_up == nullptr ? split3[dev3] : -1;
if (split > 0) { // 2 kernels; scratch = partials + sums of squares
float* part = xn_scratch;
float* ssg = xn_scratch + (size_t) n_tok * HC * 2 * PR; // room for S = 2
@@ -2058,7 +2131,7 @@ bool fused_gr_read_multi(const FusedGrArgs* a, int n_tok, float* xn_scratch, voi
// fused_gr_read, so the multi-token read is then no longer bitwise equal to T single-token calls (gr_parity's
// contract); off by default for that reason.
static const bool split_on = std::getenv("STRATA_GR_SPLIT") && std::string(std::getenv("STRATA_GR_SPLIT")) == "1";
- if (split_on) {
+ if (split_on && a[0].s_down == nullptr) { // its K-split down reads bf16 rows (not STRATA_HC_REQ8)
const bool fast = gr_fast();
if (fast) gr_norm_fast_kernel<<<n_tok, THREADS, 0, st>>>(m);
else gr_norm_multi_kernel<<<n_tok, THREADS, 0, st>>>(m);
@@ -2135,8 +2208,13 @@ void fused_gr_read(const FusedGrArgs& a, void* stream) {
std::exit(1);
}
cudaStream_t st = (cudaStream_t) stream;
- gr_down_kernel<<<DOWN_BLOCKS + 1, THREADS, 0, st>>>(a);
- gr_up_kernel<<<UP_BLOCKS, THREADS, 0, st>>>(a);
+ if (a.s_down != nullptr) { // STRATA_HC_REQ8
+ gr_down_kernel<true><<<DOWN_BLOCKS + 1, THREADS, 0, st>>>(a);
+ gr_up_kernel<true><<<UP_BLOCKS, THREADS, 0, st>>>(a);
+ } else {
+ gr_down_kernel<<<DOWN_BLOCKS + 1, THREADS, 0, st>>>(a);
+ gr_up_kernel<<<UP_BLOCKS, THREADS, 0, st>>>(a);
+ }
const cudaError_t e = cudaGetLastError();
if (e != cudaSuccess) {
std::fprintf(stderr, "fused_gr_read: %s\n", cudaGetErrorString(e));
@@ -2330,4 +2408,20 @@ void fused_gr_check() {
}
+namespace {
+__global__ void hc_q8_bf16_kernel(const int8_t* __restrict__ q, const float* __restrict__ sc, uint16_t* __restrict__ out,
+ long long n) {
+ const long long i = (long long) blockIdx.x * blockDim.x + threadIdx.x;
+ if (i >= n) return;
+ const float v = sc[i / 32] * (float) q[i];
+ uint32_t b = __float_as_uint(v);
+ b += 0x7fffu + ((b >> 16) & 1u); // round to nearest even (the values are finite)
+ out[i] = (uint16_t) (b >> 16);
+}
+} // namespace
+
+void hc_q8_to_bf16(const void* codes, const float* scales, uint16_t* out, long long n, void* stream) {
+ hc_q8_bf16_kernel<<<(unsigned) ((n + 255) / 256), 256, 0, (cudaStream_t) stream>>>((const int8_t*) codes, scales, out, n);
+}
+
} // namespace strata::kernels
diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp
index 681d1d55b..01e870500 100644
--- a/src/prefill/prefill.cpp
+++ b/src/prefill/prefill.cpp
@@ -1,4 +1,5 @@
// src/prefill/prefill.cpp - see include/strata/prefill/prefill.hpp.
+#include "strata/kernels/fused_gr.hpp"
#include "strata/prefill/prefill.hpp"
#include <atomic>
#if defined(__cpp_lib_atomic_wait)
@@ -2006,10 +2007,26 @@ bool native_proj(Gemm& gm, const core::WeightRef* w, const uint16_t* X, float* Y
return true;
}
bool bf16_proj(Gemm& gm, const core::WeightRef* w, const uint16_t* X, float* Y, int64_t T, const std::string& name,
- std::string& err, int64_t ldy = 0, const uint16_t* X_lo = nullptr, int64_t ldx = 0) {
+ std::string& err, int64_t ldy = 0, const uint16_t* X_lo = nullptr, int64_t ldx = 0,
+ void* stream = nullptr) {
if (w->kind != core::WeightKind::Bf16InF32 || !w->data) { err = "prefill: " + name + " is not a resident BF16 tensor"; return false; }
- gm.bf16(X, (const uint16_t*) w->data, Y, T, w->ne1 > 0 ? w->ne1 : 1, w->ne0, ldy, 0.0f, ldx);
- if (X_lo) gm.bf16(X_lo, (const uint16_t*) w->data, Y, T, w->ne1 > 0 ? w->ne1 : 1, w->ne0, ldy, 1.0f);
+ const uint16_t* wb = (const uint16_t*) w->data;
+ if (w->q8_scale != nullptr) {
+ // STRATA_HC_REQ8: the GEMM takes bf16, so the int8 codes are expanded into one reused scratch on this stream
+ static uint16_t* scratch = nullptr;
+ static int64_t cap = 0;
+ if (w->elements > cap) {
+ if (scratch) cudaFree(scratch);
+ if (cudaMalloc((void**) &scratch, (size_t) w->elements * 2) != cudaSuccess) {
+ scratch = nullptr; cap = 0; err = "prefill: the STRATA_HC_REQ8 scratch"; return false;
+ }
+ cap = w->elements;
+ }
+ strata::kernels::hc_q8_to_bf16(w->data, w->q8_scale, scratch, (long long) w->elements, stream);
+ wb = scratch;
+ }
+ gm.bf16(X, wb, Y, T, w->ne1 > 0 ? w->ne1 : 1, w->ne0, ldy, 0.0f, ldx);
+ if (X_lo) gm.bf16(X_lo, wb, Y, T, w->ne1 > 0 ? w->ne1 : 1, w->ne0, ldy, 1.0f);
return true;
}
@@ -2641,10 +2658,10 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
normed = false;
bool hcd = false;
bool hdown = false;
- if (ldx != D && !pf_hcdown()) { // STRATA_HCD_EXACT: the exact-order down kernel when hipBLASLt would take 1176 / 1177
+ if (wd->q8_scale == nullptr && ldx != D && !pf_hcdown()) { // not with STRATA_HC_REQ8's int8 rows // STRATA_HCD_EXACT: the exact-order down kernel when hipBLASLt would take 1176 / 1177
hdown = wd->kind == core::WeightKind::Bf16InF32 && wd->data && wd->ne0 == D && wd->ne1 == LR &&
m.gemm.bf16_hcd_exact(m.xn16, ldx, (const uint16_t*) wd->data, m.lo, T, LR, D);
- } else if (ldx != D) {
+ } else if (wd->q8_scale == nullptr && ldx != D) {
if (wd->kind != core::WeightKind::Bf16InF32 || !wd->data || wd->ne0 != D || wd->ne1 != LR ||
wi->kind != core::WeightKind::Bf16InF32 || !wi->data || wi->ne0 != D || wi->ne1 != HC ||
!strata_pf_hcdown_bf16(m.xn16, ldx, (const uint16_t*) wd->data, (const uint16_t*) wi->data, LR,
@@ -2654,10 +2671,10 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
}
hcd = true;
}
- if (!hcd && !hdown && !bf16_proj(m.gemm, wd, m.xn16, m.lo, T, sd, err, 0, m.xn16_lo, ldx != D ? ldx : 0)) return false;
+ if (!hcd && !hdown && !bf16_proj(m.gemm, wd, m.xn16, m.lo, T, sd, err, 0, m.xn16_lo, ldx != D ? ldx : 0, m.cs)) return false;
gr_silu(m.lo, m.lo16, T, m.cs, m.lo16_lo);
bool upmixed = false;
- if (hc_upmix() && T >= std::max(pf_switch_min_t(), hc_upmix_min_t()) && !gr_unfused() && !m.lo16_lo && !m.mixed_bf_lo &&
+ if (wu->q8_scale == nullptr && hc_upmix() && T >= std::max(pf_switch_min_t(), hc_upmix_min_t()) && !gr_unfused() && !m.lo16_lo && !m.mixed_bf_lo &&
wu->kind == core::WeightKind::Bf16InF32 && wu->data && wu->ne0 == LR && wu->ne1 == D) {
static int checks = [] { const char* e = std::getenv("STRATA_HC_UPMIX_CHECK"); return e ? std::atoi(e) : 0; }();
if (checks > 0) { // the default pair first, kept for the comparison
@@ -2683,8 +2700,8 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
m.mixed, m.mixed_bf, m.mixed_h, T, m.cs);
}
}
- if (!upmixed && !bf16_proj(m.gemm, wu, m.lo16, m.gated, T, su, err, 0, m.lo16_lo)) return false;
- if (!hcd && !bf16_proj(m.gemm, wi, m.xn16, m.inj, T, si, err, 0, m.xn16_lo, ldx != D ? ldx : 0)) return false;
+ if (!upmixed && !bf16_proj(m.gemm, wu, m.lo16, m.gated, T, su, err, 0, m.lo16_lo, 0, m.cs)) return false;
+ if (!hcd && !bf16_proj(m.gemm, wi, m.xn16, m.inj, T, si, err, 0, m.xn16_lo, ldx != D ? ldx : 0, m.cs)) return false;
if (upmixed) {
} else if (gr_unfused()) {
gr_mix(m.xn, m.gated, m.mixed, m.mixed_bf, T, m.cs, m.mixed_h, m.mixed_bf_lo);
diff --git a/src/program/generate.cpp b/src/program/generate.cpp
index ba00f94d7..935f140cc 100644
--- a/src/program/generate.cpp
+++ b/src/program/generate.cpp
@@ -3110,6 +3110,15 @@ int main(int argc, char** argv) {
std::fprintf(stderr, "strata generate: layer split: CUDA0 loads the dense weights of layers 0-%lld only\n",
(long long) split_at[0] - 1);
}
+ // STRATA_HC_REQ8=1 (opt-in, changes the output): the hyper-connection projections are not loaded as BF16;
+ // load_hc_q8 below requantizes them to int8 + an fp32 scale per 32 values (~0.56x the bytes), and the freed VRAM
+ // goes to the expert cache like any other. (Upstream's STRATA_HC_Q8 is a different thing: the GGUF's Q8_0 bytes.)
+ const bool hc_req8_env = std::getenv("STRATA_HC_REQ8") != nullptr && std::atoi(std::getenv("STRATA_HC_REQ8")) != 0;
+ if (hc_req8_env && multi_gpu)
+ std::fprintf(stderr, "strata generate: WARNING: STRATA_HC_REQ8 does not support a layer split yet - the BF16 "
+ "projections are used\n");
+ const bool hc_q8 = hc_req8_env && !multi_gpu;
+ if (hc_q8) strata::core::WeightTable::hc_q8_names(g.n_layers, skip);
uint64_t pool_bytes = 0;
if (!strata::core::WeightTable::pool_bytes(o.pack, pool_bytes, err, skip.empty() ? nullptr : &skip)) {
std::fprintf(stderr, "strata generate: %s\n", err.c_str());
@@ -3159,6 +3168,12 @@ int main(int argc, char** argv) {
std::fprintf(stderr, "strata generate: %s\n", err.c_str());
return 1;
}
+ if (hc_q8) {
+ uint64_t qb = 0;
+ if (!wt.load_hc_q8(o.pack, qb, err)) { std::fprintf(stderr, "strata generate: %s\n", err.c_str()); return 1; }
+ std::fprintf(stderr, "strata generate: STRATA_HC_REQ8: hyper-connection projections as int8 + fp32/32 scales, "
+ "%llu MiB\n", (unsigned long long) (qb >> 20));
+ }
std::fprintf(stderr, "strata generate: %llu MiB of weights loaded from %s in %.1f s (%zu canonical tensors "
"skipped: served natively)\n",
(unsigned long long) (pool_bytes >> 20), o.pack.c_str(), load_s(), skip.size());