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++

HIP: the packed Q8_0 decode layout (opt-in STRATA_Q8_PACKED=1), bitwise

Niko122196f4065aae49
diff --git a/include/strata/core/native_dense.hpp b/include/strata/core/native_dense.hpp
index 8330fac55..9f3a8ad93 100644
--- a/include/strata/core/native_dense.hpp
+++ b/include/strata/core/native_dense.hpp
@@ -30,10 +30,11 @@ public:
/// 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);
uint64_t weight_bytes() const { return bytes_; }
- size_t tensor_count() const { return weights_.size(); }
+ size_t tensor_count() const { return weights_.size() - packed_keys_.size(); }
private:
std::vector<void*> weights_;
+ std::vector<const void*> packed_keys_; // GGUF-layout pointers registered with STRATA_Q8_PACKED=1
void* scratch_ = nullptr;
uint64_t bytes_ = 0;
};
diff --git a/include/strata/kernels/native_mmvq.hpp b/include/strata/kernels/native_mmvq.hpp
index 9f69dd940..a8894d73a 100644
--- a/include/strata/kernels/native_mmvq.hpp
+++ b/include/strata/kernels/native_mmvq.hpp
@@ -90,6 +90,16 @@ void native_q5_0_f32(const void* weights, const float* x, void* scratch_q8_1,
void native_q8_0_mmvq(const void* weights, const void* x_q8_1, float* y,
int n_in, int n_out, int ncols, void* stream);
+/// STRATA_Q8_PACKED=1 (opt-in): a lossless load-time repack of a Q8_0 matrix into a qs plane (n_out x n_in int8)
+/// followed by a d plane (n_out x n_in / 32 fp16), the same bytes (34 per 32 values). A registered matrix's
+/// native_q8_0_mmvq calls (keyed by its GGUF-layout device pointer, which stays valid for the prompt path) read the
+/// packed copy instead; every output is bitwise equal to the GGUF-layout kernels.
+bool native_q8_0_packed_enabled();
+bool native_q8_0_packed_eligible(int n_in, int n_out);
+void native_q8_0_pack_host(const void* gguf_blocks, void* out, int n_in, int n_out);
+void native_q8_0_packed_register(const void* gguf_weights, const void* packed, int n_in, int n_out);
+void native_q8_0_packed_unregister(const void* gguf_weights);
+
void native_q8_0_f32(const void* weights, const float* x, void* scratch_q8_1,
float* y, int n_in, int n_out, int ncols, void* stream);
diff --git a/src/core/native_dense.cpp b/src/core/native_dense.cpp
index 4a86d48c2..c5778e02b 100644
--- a/src/core/native_dense.cpp
+++ b/src/core/native_dense.cpp
@@ -6,6 +6,7 @@
#include <cuda_runtime.h>
#include <algorithm>
#include <climits>
+#include <cstdio>
#include <exception>
#include <limits>
#include <memory>
@@ -33,6 +34,7 @@ struct Pending {
int type;
uint64_t bytes;
DevicePtr data;
+ DevicePtr packed; // STRATA_Q8_PACKED=1 copy, or null
};
}
@@ -64,6 +66,7 @@ bool NativeDense::keep_unquantized_ple_key(const std::string& pack_dir, std::set
}
NativeDense::~NativeDense() {
+ for (const void* p : packed_keys_) strata::kernels::native_q8_0_packed_unregister(p);
if (scratch_) cudaFree(scratch_);
for (void* p : weights_) cudaFree(p);
}
@@ -168,7 +171,26 @@ bool NativeDense::load(const std::vector<std::string>& shards, WeightTable& tabl
}
max_in = (std::max)(max_in, (int) ref.ne0);
total += bytes;
- pending.push_back(Pending{&ref, (int) tensor.type, bytes, std::move(data)});
+ // STRATA_Q8_PACKED=1: a second, packed copy of an eligible Q8_0 matrix for the decode MMVQ (the GGUF
+ // copy stays: the prompt path's GEMMs read it).
+ DevicePtr packed;
+ if (tensor.type == 8 && tensor.name != "blk.1.ple_key.weight" && // the PLE has its own kernel
+ strata::kernels::native_q8_0_packed_enabled() &&
+ strata::kernels::native_q8_0_packed_eligible((int) ref.ne0, (int) ref.ne1)) {
+ std::vector<uint8_t> host(bytes);
+ strata::kernels::native_q8_0_pack_host(gguf.tensor_data(tensor), host.data(), (int) ref.ne0,
+ (int) ref.ne1);
+ void* packed_allocation = nullptr;
+ auto packed_status = cudaMalloc(&packed_allocation, bytes);
+ packed.reset(packed_allocation);
+ if (packed_status == cudaSuccess)
+ packed_status = cudaMemcpy(packed.get(), host.data(), bytes, cudaMemcpyHostToDevice);
+ if (packed_status != cudaSuccess) {
+ err = "native dense packed upload " + tensor.name + ": " + cudaGetErrorString(packed_status);
+ return false;
+ }
+ }
+ pending.push_back(Pending{&ref, (int) tensor.type, bytes, std::move(data), std::move(packed)});
}
}
if (pending.empty()) { err = "native dense: no supported GDN/QSA matrices in supplied shards"; return false; }
@@ -178,12 +200,25 @@ bool NativeDense::load(const std::vector<std::string>& shards, WeightTable& tabl
if (status != cudaSuccess) { err = std::string("native dense scratch: ") + cudaGetErrorString(status); return false; }
// All checks and allocations finish before publishing any reference.
weights_.reserve(pending.size());
+ uint64_t packed_bytes = 0;
+ size_t packed_count = 0;
for (auto& item : pending) {
item.ref->native_data = item.data.get();
item.ref->native_type = item.type;
item.ref->native_q8_1 = scratch.get();
+ if (item.packed) {
+ strata::kernels::native_q8_0_packed_register(item.data.get(), item.packed.get(), (int) item.ref->ne0,
+ (int) item.ref->ne1);
+ packed_keys_.push_back(item.data.get());
+ packed_bytes += item.bytes;
+ ++packed_count;
+ weights_.push_back(item.packed.release());
+ }
weights_.push_back(item.data.release());
}
+ if (packed_count)
+ std::fprintf(stderr, "native dense: STRATA_Q8_PACKED=1 packed %zu Q8_0 matrices for decode (+%.1f MiB)\n",
+ packed_count, packed_bytes / 1048576.0);
scratch_ = scratch.release();
bytes_ = total;
return true;
diff --git a/src/kernels/cuda/native_mmvq.cu b/src/kernels/cuda/native_mmvq.cu
index e0a4c1bdb..eac781e45 100644
--- a/src/kernels/cuda/native_mmvq.cu
+++ b/src/kernels/cuda/native_mmvq.cu
@@ -30,10 +30,14 @@
#include <cuda_fp16.h>
#include <cuda_runtime.h>
+#include <algorithm>
#include <cstdint>
+#include <cstdlib>
+#include <cstring>
#include <limits>
#include <stdexcept>
#include <string>
+#include <unordered_map>
namespace strata::kernels {
namespace {
@@ -1139,8 +1143,150 @@ void small_f32(const void* weights, const float* x, void* scratch_q8_1,
small_mmvq<Weight, Qi>(weights, scratch_q8_1, y, n_in, n_out, ncols, stream);
}
+// ============================ STRATA_Q8_PACKED=1: the packed Q8_0 decode layout (opt-in) ============================
+//
+// A lossless load-time repack of a Q8_0 matrix: the SAME int8 values in a qs plane (row-major, n_in bytes per row)
+// followed by the SAME fp16 scales in a d plane (row-major, n_in / 32 per row) - no requantization, same bytes.
+// The GGUF's 34-byte blocks leave every int 2-byte aligned (load_int_b2 = two 16-bit loads); the planes give each
+// thread one aligned 8-byte run, read with streaming (non-temporal) loads. Every thread owns exactly the bytes of
+// the ncols = 1 / multi-column EXACT layout (block kbx = tid / 4 + 32 i, ints 2 (tid % 4) and +1), accumulates them
+// in the same order with the same expression, and the cross-warp sum and XOR tree are the same: every output is
+// BITWISE equal to native_small_mmvq_kernel / native_mmvq_multi_kernel<SmallTraits<Q80Block, 8>, NC, 4, 1>.
+// Eligible: 1024 <= n_in <= 4096 and n_out >= 2048, run as 640 persistent workgroups (a grid-stride over rows, two
+// partial buffers). Measured on gfx1151 (S26, 8 real UD-IQ4_XS layers per shape, paired, 1-5 columns): 2560 ->
+// 6144/10240/12288 +10-12% per call (engine trace: -10.7% on those calls). 6144 -> 2560 (one workgroup per row)
+// was +2-4% standalone but -3% in the engine, and rows < 2048 lose: neither is packed.
+#if defined(__HIPCC__)
+#define STRATA_Q8P_LOAD(p) __builtin_nontemporal_load(p)
+#else
+#define STRATA_Q8P_LOAD(p) __ldcs(p)
+#endif
+constexpr int Q8P_PERSIST_GRID = 640;
+constexpr int Q8P_PERSIST_MAX_IN = 4096;
+
+template<int NCOLS>
+__launch_bounds__(WARPS * WARP, 1)
+__global__ void native_q8_0_packed_kernel(const int8_t* __restrict__ qs, const half* __restrict__ dpl,
+ const Q81Block* __restrict__ x, float* __restrict__ y,
+ int n_in, int n_out, int row_step) {
+ const int tid = WARP * int(threadIdx.y) + int(threadIdx.x);
+ const int nb = n_in / 32;
+ const int kqs = 2 * (tid % 4);
+ __shared__ float partial[2][WARPS - 1][NCOLS][WARP];
+ int buf = 0;
+ for (int row = int(blockIdx.x); row < n_out; row += row_step, buf ^= 1) {
+ float tmp[NCOLS] = {};
+ for (int kbx = tid / 4; kbx < nb; kbx += 2 * WARPS * WARP / 8) {
+ const std::size_t block = std::size_t(row) * nb + kbx;
+ const int* p = reinterpret_cast<const int*>(qs + block * 32) + kqs;
+ const int v0 = STRATA_Q8P_LOAD(p);
+ const int v1 = STRATA_Q8P_LOAD(p + 1);
+ const float d0 = dpl[block];
+#pragma unroll
+ for (int j = 0; j < NCOLS; ++j) {
+ const Q81Block* xb = x + std::size_t(j) * nb + kbx;
+ const int* u = reinterpret_cast<const int*>(xb->qs);
+ int sumi = 0;
+ sumi = STRATA_DP4A(v0, u[kqs], sumi);
+ sumi = STRATA_DP4A(v1, u[kqs + 1], sumi);
+ const float d1 = __low2float(xb->ds);
+ tmp[j] += d0 * d1 * float(sumi);
+ }
+ }
+ if (threadIdx.y > 0) {
+#pragma unroll
+ for (int j = 0; j < NCOLS; ++j) partial[buf][threadIdx.y - 1][j][threadIdx.x] = tmp[j];
+ }
+ __syncthreads();
+ if (threadIdx.y == 0) {
+#pragma unroll
+ for (int j = 0; j < NCOLS; ++j) {
+#pragma unroll
+ for (int l = 0; l < WARPS - 1; ++l) tmp[j] += partial[buf][l][j][threadIdx.x];
+ tmp[j] = warp_sum(tmp[j]);
+ if (threadIdx.x == 0) y[std::size_t(j) * n_out + row] = tmp[j];
+ }
+ }
+ }
+}
+
+struct Q8Packed { const int8_t* qs; const half* d; int n_in, n_out; };
+std::unordered_map<const void*, Q8Packed>& q8_packed_registry() {
+ static std::unordered_map<const void*, Q8Packed> registry;
+ return registry;
+}
+
+template<int NCOLS>
+void q8_packed_launch(const Q8Packed& w, const Q81Block* x, float* y, cudaStream_t s) {
+ const bool persist = w.n_in <= Q8P_PERSIST_MAX_IN;
+ const int grid = persist ? (std::min)(w.n_out, Q8P_PERSIST_GRID) : w.n_out;
+ native_q8_0_packed_kernel<NCOLS><<<unsigned(grid), dim3(WARP, WARPS), 0, s>>>(w.qs, w.d, x, y, w.n_in, w.n_out,
+ persist ? grid : w.n_out);
+}
+
+bool q8_packed_mmvq(const void* weights, const void* x_q8_1, float* y, int n_in, int n_out, int ncols, void* stream) {
+ auto& registry = q8_packed_registry();
+ if (registry.empty() || (ncols > 1 && !g_multi_exact)) return false; // packed = the EXACT layout only
+ const auto found = registry.find(weights);
+ if (found == registry.end() || found->second.n_in != n_in || found->second.n_out != n_out) return false;
+ validate_shape(n_in, ncols, 32);
+ validate_pointer(x_q8_1);
+ validate_pointer(y);
+ validate_stream(stream);
+ const auto* x = static_cast<const Q81Block*>(x_q8_1);
+ const auto s = static_cast<cudaStream_t>(stream);
+ switch (ncols) {
+ case 1: q8_packed_launch<1>(found->second, x, y, s); break;
+ case 2: q8_packed_launch<2>(found->second, x, y, s); break;
+ case 3: q8_packed_launch<3>(found->second, x, y, s); break;
+ case 4: q8_packed_launch<4>(found->second, x, y, s); break;
+ case 5: q8_packed_launch<5>(found->second, x, y, s); break;
+ case 6: q8_packed_launch<6>(found->second, x, y, s); break;
+ case 7: q8_packed_launch<7>(found->second, x, y, s); break;
+ case 8: q8_packed_launch<8>(found->second, x, y, s); break;
+ default: throw std::invalid_argument("native MMVQ requires 1 <= ncols <= 8");
+ }
+ launch_check();
+ return true;
+}
+
} // namespace
+bool native_q8_0_packed_enabled() {
+ static const bool enabled = [] {
+ const char* v = std::getenv("STRATA_Q8_PACKED");
+ return v && v[0] == '1' && v[1] == '\0';
+ }();
+ return enabled;
+}
+
+bool native_q8_0_packed_eligible(int n_in, int n_out) {
+ return n_in > 0 && n_in % 32 == 0 && n_in / 32 >= 2 * WARPS * WARP / 8 && n_in <= Q8P_PERSIST_MAX_IN &&
+ n_out >= 2048;
+}
+
+void native_q8_0_pack_host(const void* gguf_blocks, void* out, int n_in, int n_out) {
+ const auto* blocks = static_cast<const Q80Block*>(gguf_blocks);
+ auto* qs = static_cast<int8_t*>(out);
+ auto* d = reinterpret_cast<half*>(qs + std::size_t(n_in) * n_out);
+ const std::size_t count = std::size_t(n_in / 32) * n_out;
+ for (std::size_t b = 0; b < count; ++b) {
+ std::memcpy(qs + b * 32, blocks[b].qs, 32);
+ std::memcpy(d + b, &blocks[b].d, sizeof(half));
+ }
+}
+
+void native_q8_0_packed_register(const void* gguf_weights, const void* packed, int n_in, int n_out) {
+ validate_pointer(gguf_weights);
+ validate_pointer(packed);
+ if (!native_q8_0_packed_eligible(n_in, n_out)) throw std::invalid_argument("Q8_0 packed: ineligible shape");
+ const auto* qs = static_cast<const int8_t*>(packed);
+ q8_packed_registry()[gguf_weights] =
+ Q8Packed{qs, reinterpret_cast<const half*>(qs + std::size_t(n_in) * n_out), n_in, n_out};
+}
+
+void native_q8_0_packed_unregister(const void* gguf_weights) { q8_packed_registry().erase(gguf_weights); }
+
void native_mmvq_set_multi_exact(bool exact) { g_multi_exact = exact; }
bool native_mmvq_multi_exact() { return g_multi_exact; }
@@ -1420,6 +1566,7 @@ void native_q5_0_f32(const void* weights, const float* x, void* scratch_q8_1,
void native_q8_0_mmvq(const void* weights, const void* x_q8_1, float* y,
int n_in, int n_out, int ncols, void* stream) {
+ if (q8_packed_mmvq(weights, x_q8_1, y, n_in, n_out, ncols, stream)) return; // STRATA_Q8_PACKED=1 tensors
small_mmvq<Q80Block, 8>(weights, x_q8_1, y, n_in, n_out, ncols, stream);
}