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: no bf16 copy of the layers' hyper-connection down/up projections (single GPU): they are quantized to Q8_0 from the pack file at load and left out of the arena; the prompt path and the single-token read dequantize a half's two matrices into a per-stream scratch
Niko1221b60aab1ba27c
diff --git a/include/strata/core/native_dense.hpp b/include/strata/core/native_dense.hppindex 7b2b4a793..4a2610687 100644--- a/include/strata/core/native_dense.hpp+++ b/include/strata/core/native_dense.hpp@@ -7,6 +7,7 @@namespace strata::core {class WeightTable;+struct WeightRef;// Experimental GDN/QSA/shared-expert projection overrides. Upload unchanged native GGUF// blocks once, then attach them to the matching canonical WeightRef. Unsupported@@ -39,6 +40,14 @@ public:/// the native kernel also reads, e.g. OrcaRouter's IQ3_XXS) serves the PLE from that row, so it is taken out/// of `skip` and `load` does not upload the GGUF key over it. A quantized row leaves `skip` unchanged.static bool keep_unquantized_ple_key(const std::string& pack_dir, std::set<std::string>& skip, std::string& err);+ /// STRATA_HC_Q8=1: the hyper-connection down / up projections of the layers live only as Q8_0 (the bf16 copy is not+ /// loaded: `add_hc_skip` puts their rows in the arena's skip set, `load` quantizes them from the pack file).+ static bool hc_q8_requested();+ static bool add_hc_skip(const std::string& pack_dir, std::set<std::string>& skip);+ /// A projection's bf16 values for a path that has no Q8 kernel (the prompt path's GEMMs, the single-token read): the+ /// resident copy, or - when the row was skipped - the Q8_0 copy dequantized into a per-stream scratch (`slot` 0 / 1:+ /// two matrices alive at once). nullptr if there is neither.+ static const uint16_t* hc_bf16_view(const WeightRef& w, void* stream, int slot);uint64_t weight_bytes() const { return bytes_; }/// What the device lost to `load`: each matrix rounded up to the granule `cudaMalloc` backs it with.uint64_t allocated_bytes() const { return allocated_; }diff --git a/include/strata/core/weights.hpp b/include/strata/core/weights.hppindex a9949123a..d352ba641 100644--- a/include/strata/core/weights.hpp+++ b/include/strata/core/weights.hpp@@ -131,6 +131,10 @@ public:bool load(const std::string& pack_dir, void* arena_base, uint64_t arena_bytes, std::string& err,const std::set<std::string>* skip = nullptr);+ /// A SKIPPED bf16 tensor's values read back from the pack file (the promoted-f32 and the raw 16-bit forms), for+ /// the host-side Q8_0 quantizer of STRATA_HC_Q8 (the tensor then has no copy in the arena).+ 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;const std::map<std::string, WeightRef>& all() const { return table_; }const LoadReport& report() const { return report_; }@@ -139,6 +143,7 @@ private:friend class NativeDense;std::map<std::string, WeightRef> table_;LoadReport report_;+ std::string pack_dir_;};} // namespace strata::corediff --git a/include/strata/kernels/fused_gr.hpp b/include/strata/kernels/fused_gr.hppindex d689652ba..003242473 100644--- a/include/strata/kernels/fused_gr.hpp+++ b/include/strata/kernels/fused_gr.hpp@@ -51,6 +51,8 @@ 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.+/// The inverse for a path with no Q8 kernel: bf16 (round to nearest even) of d * q for `n_blocks` Q8_0 blocks.+void fused_gr_dequant_q8_0(const uint8_t* q8, uint16_t* bf16, int64_t n_blocks, void* stream);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 readdiff --git a/src/core/layer.cpp b/src/core/layer.cppindex 3132871c6..c6dc2080e 100644--- a/src/core/layer.cpp+++ b/src/core/layer.cpp@@ -1,6 +1,7 @@// src/core/layer.cpp - the GDN layer, composed. See the header for the operation order and its traps.#include "strata/core/layer.hpp"+#include "strata/core/native_dense.hpp"#include "strata/core/native_head.hpp"#include "strata/kernels/bf16_bits.hpp"#include "strata/kernels/bf16_gemv.hpp"@@ -1408,12 +1409,12 @@ st_begin(layer, 0, stream);if (fused) {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 = (const uint16_t*) w_down[0]->data;- fa.w_up = (const uint16_t*) w_up[0]->data; fa.w_inject = (const uint16_t*) w_inject[0]->data;+ fa.w_norm = (const float*) w_norm[0]->data; fa.w_down = NativeDense::hc_bf16_view(*w_down[0], stream, 0);+ fa.w_up = NativeDense::hc_bf16_view(*w_up[0], stream, 1); 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, (const uint16_t*) w_down[0]->data, (const uint16_t*) w_up[0]->data, (const uint16_t*) w_inject[0]->data, RMS_EPS, gs, bb.gr, bb.mixed, bb.inject, stream);+ gr_read(R, (const float*) w_norm[0]->data, NativeDense::hc_bf16_view(*w_down[0], stream, 0), NativeDense::hc_bf16_view(*w_up[0], stream, 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,12 +1432,12 @@ st_begin(layer, 3, stream);if (fused) {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 = (const uint16_t*) w_down[1]->data;- fa.w_up = (const uint16_t*) w_up[1]->data; fa.w_inject = (const uint16_t*) w_inject[1]->data;+ fa.w_norm = (const float*) w_norm[1]->data; fa.w_down = NativeDense::hc_bf16_view(*w_down[1], stream, 0);+ fa.w_up = NativeDense::hc_bf16_view(*w_up[1], stream, 1); 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, (const uint16_t*) w_down[1]->data, (const uint16_t*) w_up[1]->data, (const uint16_t*) w_inject[1]->data, RMS_EPS, gs, bb.gr, bb.mixed, bb.inject, stream);+ gr_read(R, (const float*) w_norm[1]->data, NativeDense::hc_bf16_view(*w_down[1], stream, 0), NativeDense::hc_bf16_view(*w_up[1], stream, 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/native_dense.cpp b/src/core/native_dense.cppindex 6cf736f06..1eaee7a1b 100644--- a/src/core/native_dense.cpp+++ b/src/core/native_dense.cpp@@ -17,6 +17,9 @@#include <set>#include <cmath>#include <cstring>+#include <map>+#include <mutex>+#include <thread>#include <vector>namespace strata::core {@@ -39,7 +42,7 @@ bool in_range(const std::string& name) {return l >= g_layer_lb && l < g_layer_le;}// S23 experiment (STRATA_HC_Q8=1): the hyper-connection projections' Q8_0 bytes for the verify read-bool hc_q8_requested() {+bool hc_q8_env() {static const bool on = [] { const char* v = std::getenv("STRATA_HC_Q8"); return v != nullptr && v[0] == '1'; }();return on;}@@ -87,6 +90,31 @@ std::vector<uint8_t> q8_0_of(const float* x, uint64_t n) {}return out;}+// A bf16 matrix (the pack file's values) as Q8_0, ggml's reference rounding; the blocks are independent, so a few threads+std::vector<uint8_t> q8_0_of_bf16(const uint16_t* x, uint64_t n) {+ std::vector<uint8_t> out((size_t) (n / 32 * 34));+ const uint64_t nb = n / 32;+ auto work = [&](uint64_t b0, uint64_t b1) {+ for (uint64_t b = b0; b < b1; ++b) {+ float v[32], amax = 0.0f;+ for (int j = 0; j < 32; ++j) {+ const uint32_t u = (uint32_t) x[b * 32 + j] << 16;+ std::memcpy(&v[j], &u, 4);+ amax = (std::max)(amax, std::fabs(v[j]));+ }+ const float d = amax / 127.0f, id = d != 0.0f ? 1.0f / d : 0.0f;+ uint8_t* o = out.data() + b * 34;+ const uint16_t hb = half_bits(d);+ std::memcpy(o, &hb, 2);+ for (int j = 0; j < 32; ++j) o[2 + j] = (uint8_t) (int8_t) std::lround(v[j] * id);+ }+ };+ const unsigned nt = (std::min)(8u, (std::max)(1u, std::thread::hardware_concurrency()));+ std::vector<std::thread> pool;+ for (unsigned i = 0; i < nt; ++i) pool.emplace_back(work, nb * i / nt, nb * (i + 1) / nt);+ for (auto& th : pool) th.join();+ return out;+}bool eligible(const strata::TensorInfo& tensor, bool include_ple_key) {const auto& name = tensor.name;if (name.rfind("blk.", 0) != 0) return false;@@ -321,7 +349,7 @@ bool NativeDense::load(const std::vector<std::string>& shards, WeightTable& tabl}pending.push_back(Pending{&ref, (int) tensor.type, bytes, std::move(data), std::move(packed)});}- if (hc_q8_requested())+ if (hc_q8_env())for (const auto& tensor : gguf.tensors()) {// S25: the F32 inject rows hold BF16-exact values: the read takes the pack's BF16 rows unless// STRATA_HC_Q8_INJECT=1 asks for a Q8_0 copy@@ -348,22 +376,35 @@ 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)+ if (hc_q8_env()) {+ // a bf16 pack stores no Q8_0 copy: quantize the layers' down / up projections. A row the arena kept+ // (--keep-canonical, a layer split) is quantized on the GPU from the arena's bf16; a skipped row (the+ // single-GPU default: add_hc_skip) from the pack file on the host, and has no bf16 copy at all. 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) ||+ if (ref.hc_q8 != 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 bool skipped = ref.data == nullptr;+ if (skipped && ref.resident) continue;+ if (ref.ne0 % 32 != 0 || ref.elements != ref.ne0 * ref.ne1 || (!skipped && ref.bytes != (uint64_t) ref.elements * 2)) {+ err = "native dense (STRATA_HC_Q8): " + name + " is not a bf16 matrix"; 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);+ if (skipped) {+ std::vector<uint16_t> host;+ if (!table.read_bf16_host(name, host, err)) return false;+ const std::vector<uint8_t> q8 = q8_0_of_bf16(host.data(), (uint64_t) host.size());+ if (cudaMemcpy(p, q8.data(), bytes, cudaMemcpyHostToDevice) != cudaSuccess) {+ err = "native dense (STRATA_HC_Q8) upload " + name; return false;+ }+ } else {+ strata::kernels::fused_gr_quantize_q8_0((const uint16_t*) ref.data, (uint8_t*) p, (int64_t) nb, nullptr);+ }+ ref.hc_q8 = p;hc_q8_bytes += bytes;allocated += alloc_bytes(bytes);++made;@@ -401,7 +442,7 @@ bool NativeDense::load(const std::vector<std::string>& shards, WeightTable& tablscratch_ = scratch.release();bytes_ = total + hc_q8_bytes;allocated_ = allocated;- if (hc_q8_requested())+ if (hc_q8_env())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;@@ -410,4 +451,34 @@ bool NativeDense::load(const std::vector<std::string>& shards, WeightTable& tablreturn false;}}+bool NativeDense::hc_q8_requested() { return hc_q8_env(); }++bool NativeDense::add_hc_skip(const std::string& pack_dir, std::set<std::string>& skip) {+ std::FILE* f = std::fopen((pack_dir + "/index.txt").c_str(), "rb");+ if (!f) return false;+ char line[1024], name[256];+ while (std::fgets(line, sizeof line, f)) {+ if (line[0] == '#' || std::sscanf(line, "%255s", name) != 1) continue;+ const std::string n = name;+ if (n.rfind("blk.", 0) == 0 && hc_q8_name(n) && (n.ends_with("_down.weight") || n.ends_with("_up.weight"))) skip.insert(n);+ }+ std::fclose(f);+ return true;+}++const uint16_t* NativeDense::hc_bf16_view(const WeightRef& w, void* stream, int slot) {+ if (w.data != nullptr) return (const uint16_t*) w.data;+ if (w.hc_q8 == nullptr) return nullptr;+ static std::mutex mu;+ static std::map<std::pair<void*, int>, void*> scratch; // per stream and slot: one matrix of bf16 (6.5 MB)+ void* buf = nullptr;+ {+ std::lock_guard<std::mutex> lock(mu);+ void*& b = scratch[{stream, slot}];+ if (b == nullptr && cudaMalloc(&b, (size_t) w.elements * 2) != cudaSuccess) { cudaGetLastError(); b = nullptr; return nullptr; }+ buf = b;+ }+ strata::kernels::fused_gr_dequant_q8_0((const uint8_t*) w.hc_q8, (uint16_t*) buf, w.elements / 32, stream);+ return (const uint16_t*) buf;+}} // namespace strata::corediff --git a/src/core/weights.cpp b/src/core/weights.cppindex 1943723c0..a46c485d3 100644--- a/src/core/weights.cpp+++ b/src/core/weights.cpp@@ -143,6 +143,34 @@ bool WeightTable::index_code_bits(const std::string& pack_dir, const std::stringreturn true;}+bool WeightTable::read_bf16_host(const std::string& name, std::vector<uint16_t>& out, std::string& err) const {+ const auto it = table_.find(name);+ if (it == table_.end()) { err = name + ": no such tensor"; return false; }+ const WeightRef& r = it->second;+ const char* fn = file_name(r.file_id);+ if (fn == nullptr || r.kind != WeightKind::Bf16InF32) { err = name + ": not a bf16 row of a pack file"; return false; }+ const uint64_t elems = (uint64_t) r.elements;+ const bool raw16 = r.src_bytes == elems * 2;+ if (!raw16 && r.src_bytes != elems * 4) { err = name + ": unexpected source size"; return false; }+ std::FILE* f = std::fopen((pack_dir_ + "/" + fn).c_str(), "rb");+ if (!f) { err = "cannot open " + pack_dir_ + "/" + fn; return false; }+ out.resize((size_t) elems);+ bool ok = true;+ if (raw16) {+ ok = read_at(f, r.src_off, out.data(), (size_t) (elems * 2), err, name.c_str());+ } else {+ std::vector<uint32_t> tmp((size_t) std::min<uint64_t>(elems, 1u << 22));+ for (uint64_t done = 0; ok && done < elems;) {+ const uint64_t n = std::min<uint64_t>(tmp.size(), elems - done);+ ok = read_at(f, r.src_off + done * 4, tmp.data(), (size_t) (n * 4), err, name.c_str());+ for (uint64_t i = 0; ok && i < n; ++i) out[(size_t) (done + i)] = (uint16_t) (tmp[(size_t) i] >> 16);+ done += n;+ }+ }+ std::fclose(f);+ return ok;+}+bool WeightTable::load(const std::string& pack_dir, void* arena_base, uint64_t arena_bytes, std::string& err,const std::set<std::string>* skip) {const std::string path = pack_dir + "/index.txt";@@ -237,6 +265,7 @@ bool WeightTable::load(const std::string& pack_dir, void* arena_base, uint64_t aint cur_file = -1;table_.clear();report_ = LoadReport{};+ pack_dir_ = pack_dir;const double t_read0 = now_ms();double upload_ms = 0;diff --git a/src/kernels/cuda/fused_gr.cu b/src/kernels/cuda/fused_gr.cuindex c5422c0fd..ab9842406 100644--- a/src/kernels/cuda/fused_gr.cu+++ b/src/kernels/cuda/fused_gr.cu@@ -1960,7 +1960,7 @@ bool fused_gr_read_multi(const FusedGrArgs* a, int n_tok, float* xn_scratch, voifor (int t = 0; t < n_tok; ++t) {m.a[t] = a[t];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) ||+ if (!x.R || !x.w_norm || !((x.w_down && x.w_up) || (x.q8_down && x.q8_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.q8_down != a[0].q8_down ||x.q8_up != a[0].q8_up || x.q8_inject != a[0].q8_inject) {@@ -2104,6 +2104,19 @@ __global__ void __launch_bounds__(256) gr_quantize_q8_0_kernel(const uint16_t* _o[2 + lane] = (uint8_t) (int8_t) lrintf(roundf(v * id));}+__global__ void __launch_bounds__(256) gr_dequant_q8_0_kernel(const uint8_t* __restrict__ q8, uint16_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 uint8_t* p = q8 + b * 34;+ const float d = __half2float(__ushort_as_half((unsigned short) (p[0] | (p[1] << 8))));+ out[b * 32 + lane] = bf16_from_f32(d * (float) (int8_t) p[2 + lane]);+}++void fused_gr_dequant_q8_0(const uint8_t* q8, uint16_t* bf16, int64_t n_blocks, void* stream) {+ gr_dequant_q8_0_kernel<<<(unsigned) ((n_blocks + 7) / 8), 256, 0, (cudaStream_t) stream>>>(q8, bf16, n_blocks);+}+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);}diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cppindex 841644452..91c8141bc 100644--- a/src/prefill/prefill.cpp+++ b/src/prefill/prefill.cpp@@ -28,6 +28,7 @@#include "strata/kernels/cvec.hpp"#include "strata/kernels/kv_q4.hpp"#include "strata/core/layer.hpp"+#include "strata/core/native_dense.hpp"#include "strata/core/native_head.hpp"#include "strata/kernels/verify_kernels.hpp"#include "strata/kernels/qsa_decode_attn.hpp"@@ -2565,6 +2566,15 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::striconst core::WeightRef *wn = need(v, sn.c_str(), err), *wd = need(v, sd.c_str(), err),*wu = need(v, su.c_str(), err), *wi = need(v, si.c_str(), err);if (!wn || !wd || !wu || !wi) return false;+ // STRATA_HC_Q8 without a bf16 copy: this path's GEMMs read bf16, so the matrices are dequantized per half+ core::WeightRef wd_q8, wu_q8;+ if (!wd->data && wd->hc_q8 && !wu->data && wu->hc_q8) {+ wd_q8 = *wd; wu_q8 = *wu;+ wd_q8.data = core::NativeDense::hc_bf16_view(*wd, m.cs, 0);+ wu_q8.data = core::NativeDense::hc_bf16_view(*wu, m.cs, 1);+ if (!wd_q8.data || !wu_q8.data) { err = "prefill: the Q8_0 hyper-connection projections could not be dequantized"; return false; }+ wd = &wd_q8; wu = &wu_q8;+ }pt.mark(kPfHc, cs);// the xn16 token stride of this chunk (the writers of both halves and of the fused write-backs use it)const int64_t ldx = hc_pad() && T >= std::max<int64_t>(pf_switch_min_t(), 64) && !gr_unfused() &&diff --git a/src/program/generate.cpp b/src/program/generate.cppindex 994898935..52afe668a 100644--- a/src/program/generate.cpp+++ b/src/program/generate.cpp@@ -2933,6 +2933,10 @@ int main(int argc, char** argv) {return 1;}if (native_pack) skip.insert("token_embd.weight");+ // STRATA_HC_Q8=1 (single GPU): the layers' hyper-connection down / up projections live only as Q8_0 (NativeDense+ // quantizes them from the pack file), so their bf16 copy is not loaded+ if (!o.native_dense_gguf.empty() && !multi_gpu && strata::core::NativeDense::hc_q8_requested())+ strata::core::NativeDense::add_hc_skip(o.pack, skip);}// Layer split: every GPU holds only the dense weights of ITS layers (the PLE tensors stay everywhere).// Without this each card keeps a full copy (~3.4 GB for the Coder) that its stage never reads - VRAM the