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++
dequant: a thread per 8 values for Q4_K / Q5_K / Q6_K / IQ4_XS, sm_61's default (bitwise; Q6_K 7.2 -> 0.33 ms on a Tesla P40)
Techannel9c5bbc36d019
diff --git a/CMakeLists.txt b/CMakeLists.txtindex 7723a0d23..f55befec0 100644--- a/CMakeLists.txt+++ b/CMakeLists.txt@@ -786,6 +786,12 @@ if(STRATA_ENABLE_CUDA OR STRATA_ENABLE_HIP)target_include_directories(mmvq_il_parity PRIVATE ${_strata_gpu_include_directories})target_link_libraries(mmvq_il_parity PRIVATE strata_kernels ${_strata_gpu_runtime_target})add_test(NAME mmvq_il_parity COMMAND mmvq_il_parity)+ # the wide dequant kernels (a thread per 8 values; sm_61's default), bitwise vs the per-group ones (GPU, synthetic,+ # no model; --bench adds old/new timings)+ add_executable(dequant_wide_parity src/kernels/dequant_wide_parity.cpp)+ target_include_directories(dequant_wide_parity PRIVATE ${_strata_gpu_include_directories})+ target_link_libraries(dequant_wide_parity PRIVATE strata_kernels ${_strata_gpu_runtime_target})+ add_test(NAME dequant_wide_parity COMMAND dequant_wide_parity)endif()# programmatic dependent launch (pdl.hpp) in a captured verify-shaped chain: bitwise vs ordinary launches over# replays with poisoned buffers, and only kernel-to-kernel programmatic edges (GPU, synthetic, no model; the edgesdiff --git a/include/strata/kernels/dequant_bf16.hpp b/include/strata/kernels/dequant_bf16.hppindex bcc7e38ff..ee88298f0 100644--- a/include/strata/kernels/dequant_bf16.hpp+++ b/include/strata/kernels/dequant_bf16.hpp@@ -29,4 +29,8 @@ bool dequant_f16_ld(int ggml_type, const void* blocks, int64_t row0, int64_t row/// The same into FP32 (tests and small tensors).void dequant_f32(int ggml_type, const void* blocks, int64_t row0, int64_t rows, int64_t cols, float* out, void* stream);+/// The wide kernels (a thread per 8 values, bitwise the same output; Q4_K, Q5_K, Q6_K, IQ4_XS): 0 off, 1 on, -1 the+/// default (STRATA_DEQUANT_WIDE, else on for compute capability 6.1). For dequant_wide_parity.+void dequant_set_wide(int mode);+} // namespace strata::kernelsdiff --git a/src/kernels/cuda/dequant_bf16.cu b/src/kernels/cuda/dequant_bf16.cuindex a6fe910ec..95e9d1523 100644--- a/src/kernels/cuda/dequant_bf16.cu+++ b/src/kernels/cuda/dequant_bf16.cu@@ -175,6 +175,112 @@ __global__ void dequant_kernel(const uint8_t* __restrict__ blocks, int64_t row_bgroup32<TYPE>(blocks + (row0 + r) * row_bytes, (int) gi, out + r * ld + gi * 32);}+// ---- the wide kernels: a thread per 8 values (Pascal, sm_61) --------------------------------------------------------+// dequant_kernel's thread reads its 32-value group a byte at a time and writes 32 two-byte values one by one, 64 bytes+// from its neighbour's. On a Tesla P40 (sm_61) that is 7.2 ms for a 10240 x 2560 Q6_K weight (10 GB/s), and the IQ3_S+// pack's dense weights cost every prompt chunk ~0.5 s of it, whatever the chunk's length (a fifth of a 560-token prompt).+// Here a thread takes 8 consecutive values: its code bytes as 16-bit loads (the GGUF blocks are 2-byte aligned) and+// one 16-byte store (two for FP32), so a warp writes 512 consecutive bytes. The arithmetic per value is group32's -+// the same expression on the same operands - so the output is bitwise the same (dequant_wide_parity). On the P40:+// Q6_K 7.2 -> 0.33 ms (227 GB/s), Q4_K 1.67 -> 0.32 ms, Q5_K 1.24 -> 0.33 ms, IQ4_XS 1.21 -> 0.34 ms for that shape.+// Q4_K, Q5_K, Q6_K and IQ4_XS. On by default on compute capability 6.1, where it was measured;+// STRATA_DEQUANT_WIDE=0 / =1 turns it off / on for every card.+__device__ __forceinline__ uint64_t ld8(const uint8_t* p) { // 8 bytes at a 2-byte aligned address+ const uint16_t* q = reinterpret_cast<const uint16_t*>(p);+ return (uint64_t) q[0] | ((uint64_t) q[1] << 16) | ((uint64_t) q[2] << 32) | ((uint64_t) q[3] << 48);+}+// values [8 piece, 8 piece + 8) of the 32-element group `gi_in_row`, as group32 computes them+template <int TYPE, typename T>+__device__ __forceinline__ void wide8(const uint8_t* row_blocks, int gi_in_row, int piece, T* v) {+ const int l0 = 8 * piece;+ if constexpr (TYPE == 12) { // Q4_K: d dmin scales[12] qs[128]+ const uint8_t* b = row_blocks + (size_t) (gi_in_row / 8) * 144;+ const int gi = gi_in_row % 8, j64 = gi / 2, hi = gi % 2;+ const float d = h2f(b), dmin = h2f(b + 2);+ int sc, m;+ scale_min_k4(gi, b + 4, sc, m);+ const float d1 = d * (float) sc, m1 = dmin * (float) m;+ const uint64_t q = ld8(b + 16 + 32 * j64 + l0);+#pragma unroll+ for (int k = 0; k < 8; ++k) {+ const int lb = (int) (q >> (8 * k)) & 0xFF;+ put(v, k, d1 * (float) (hi ? (lb >> 4) : (lb & 0xF)) - m1);+ }+ } else if constexpr (TYPE == 13) { // Q5_K: d dmin scales[12] qh[32] qs[128]+ const uint8_t* b = row_blocks + (size_t) (gi_in_row / 8) * 176;+ const int gi = gi_in_row % 8, j64 = gi / 2, hi = gi % 2;+ const float d = h2f(b), dmin = h2f(b + 2);+ int sc, m;+ scale_min_k4(gi, b + 4, sc, m);+ const float d1 = d * (float) sc, m1 = dmin * (float) m;+ const uint64_t qh = ld8(b + 16 + l0), ql = ld8(b + 48 + 32 * j64 + l0);+ const int ub = 2 * j64 + hi;+#pragma unroll+ for (int k = 0; k < 8; ++k) {+ const int lb = (int) (ql >> (8 * k)) & 0xFF, hb = (int) (qh >> (8 * k)) & 0xFF;+ const int nib = hi ? (lb >> 4) : (lb & 0xF);+ put(v, k, d1 * (float) (nib + (((hb >> ub) & 1) ? 16 : 0)) - m1);+ }+ } else if constexpr (TYPE == 14) { // Q6_K: ql[128] qh[64] scales[16] d+ const uint8_t* b = row_blocks + (size_t) (gi_in_row / 8) * 210;+ const int gi = gi_in_row % 8, n = gi / 4, qu = gi % 4;+ const uint64_t ql = ld8(b + 64 * n + ((qu & 1) ? 32 : 0) + l0);+ const uint64_t qh = ld8(b + 128 + 32 * n + l0);+ const int8_t* sc = reinterpret_cast<const int8_t*>(b + 192) + 8 * n;+ const float d = h2f(b + 208);+ const float ds = d * (float) sc[l0 / 16 + 2 * qu];+#pragma unroll+ for (int k = 0; k < 8; ++k) {+ const int lb = (int) (ql >> (8 * k)) & 0xFF, hb = (int) (qh >> (8 * k)) & 0xFF;+ const int q = ((qu >= 2) ? (lb >> 4) : (lb & 0xF)) | (((hb >> (2 * qu)) & 3) << 4);+ put(v, k, ds * (float) (q - 32));+ }+ } else if constexpr (TYPE == 23) { // IQ4_XS: d scales_h scales_l[4] qs[128]+ const uint8_t* b = row_blocks + (size_t) (gi_in_row / 8) * 136;+ const int ib = gi_in_row % 8;+ const float d = h2f(b);+ const uint16_t scales_h = (uint16_t) (b[2] | (b[3] << 8));+ const int ls = ((b[4 + ib / 2] >> (4 * (ib % 2))) & 0xf) | (((scales_h >> (2 * ib)) & 3) << 4);+ const float dl = d * (float) (ls - 32);+ const uint64_t qs = ld8(b + 8 + 16 * ib + 8 * (piece & 1)); // pieces 0, 1: the low nibbles; 2, 3: the high ones+#pragma unroll+ for (int k = 0; k < 8; ++k) {+ const int lb = (int) (qs >> (8 * k)) & 0xFF;+ put(v, k, dl * (float) kv_iq4nl[piece >= 2 ? (lb >> 4) : (lb & 0xf)]);+ }+ }+}+template <int TYPE, typename T>+__global__ void dequant_wide_kernel(const uint8_t* __restrict__ blocks, int64_t row_bytes, int64_t row0, uint32_t rows,+ uint32_t groups_per_row, int64_t ld, T* __restrict__ out) {+ const uint32_t i = blockIdx.x * blockDim.x + threadIdx.x; // launch(): 4 * rows * groups_per_row < 2^31+ const uint32_t g = i >> 2, r = g / groups_per_row, gi = g - r * groups_per_row;+ if (r >= rows) return;+ const int piece = (int) (i & 3);+ __align__(16) T v[8];+ wide8<TYPE>(blocks + (row0 + (int64_t) r) * row_bytes, (int) gi, piece, v);+ uint4* o = (uint4*) (out + (int64_t) r * ld + (int64_t) gi * 32 + 8 * piece);+#pragma unroll+ for (int k = 0; k < (int) (8 * sizeof(T) / 16); ++k) o[k] = ((const uint4*) v)[k];+}+bool wide_type(int type) { return type == 12 || type == 13 || type == 14 || type == 23; }+int g_dequant_wide = -1; // dequant_set_wide (the parity test); -1 = STRATA_DEQUANT_WIDE, else the card's default+bool dequant_wide_on() {+ static const int env = [] { const char* v = std::getenv("STRATA_DEQUANT_WIDE"); return v != nullptr && v[0] != '\0' ? (std::atoi(v) != 0 ? 1 : 0) : -1; }();+ if (g_dequant_wide >= 0) return g_dequant_wide != 0;+ if (env >= 0) return env != 0;+ static int per_dev[64]; // 0 unknown, 1 no, 2 yes: sm_61, measured on a Tesla P40+ int dev = 0;+ if (cudaGetDevice(&dev) != cudaSuccess || dev < 0 || dev >= 64) { cudaGetLastError(); return false; }+ if (!per_dev[dev]) {+ int major = 0, minor = 0;+ if (cudaDeviceGetAttribute(&major, cudaDevAttrComputeCapabilityMajor, dev) != cudaSuccess ||+ cudaDeviceGetAttribute(&minor, cudaDevAttrComputeCapabilityMinor, dev) != cudaSuccess) { cudaGetLastError(); return false; }+ per_dev[dev] = major == 6 && minor == 1 ? 2 : 1;+ }+ return per_dev[dev] == 2;+}+// Q8_0 with four threads per block, 8 values each: the warp reads 8 consecutive blocks and writes 512 consecutive// bytes (a thread per block wrote 2-byte values 64 bytes apart: ~110 GB/s, a quarter of a prompt projection's time)template <typename T>@@ -237,6 +343,22 @@ void launch(int type, const void* blocks, int64_t row0, int64_t rows, int64_t coif (e != cudaSuccess) { std::fprintf(stderr, "dequant launch: %s\n", cudaGetErrorString(e)); std::exit(1); }return;}+ // the wide kernels: even block addresses, 16-byte stores at every row (contiguous or a stride of whole stores)+ if (wide_type(type) && (reinterpret_cast<uintptr_t>(blocks) & 1) == 0 && (reinterpret_cast<uintptr_t>(out) & 15) == 0 &&+ ((size_t) ld * sizeof(T)) % 16 == 0 && 4 * total < (1ll << 31) && dequant_wide_on()) {+ const unsigned wgrid = (unsigned) ((4 * total + 255) / 256);+#define STRATA_DQW(TY) dequant_wide_kernel<TY, T><<<wgrid, 256, 0, st>>>(p, row_bytes, row0, (uint32_t) rows, (uint32_t) gpr, ld, out); break+ switch (type) {+ case 12: STRATA_DQW(12);+ case 13: STRATA_DQW(13);+ case 14: STRATA_DQW(14);+ case 23: STRATA_DQW(23);+ }+#undef STRATA_DQW+ const cudaError_t e = cudaGetLastError();+ if (e != cudaSuccess) { std::fprintf(stderr, "dequant launch: %s\n", cudaGetErrorString(e)); std::exit(1); }+ return;+ }#define STRATA_DQ(TY) dequant_kernel<TY, T><<<grid, 256, 0, st>>>(p, row_bytes, row0, rows, gpr, ld, out); breakswitch (type) {case 2: STRATA_DQ(2);@@ -258,6 +380,8 @@ void launch(int type, const void* blocks, int64_t row0, int64_t rows, int64_t co} // namespace+void dequant_set_wide(int mode) { g_dequant_wide = mode; }+bool dequant_bf16_supported(int ggml_type) noexcept {int a, b;return geometry(ggml_type, a, b);diff --git a/src/kernels/dequant_wide_parity.cpp b/src/kernels/dequant_wide_parity.cppnew file mode 100644index 000000000..b462eb298--- /dev/null+++ b/src/kernels/dequant_wide_parity.cpp@@ -0,0 +1,145 @@+// src/kernels/dequant_wide_parity.cpp - the wide dequant kernels (a thread per 8 values, dequant_bf16.cu) against the+// per-group ones, bitwise, for Q4_K / Q5_K / Q6_K / IQ4_XS into FP16, BF16 and FP32: whole tensors, row slices and a+// padded row stride, on blocks with sane fields and on raw random bytes (NaN and Inf scales included).+//+// build/dequant_wide_parity [--bench]+// --bench adds each kernel's time per call on the prompt path's shapes (FP16 out). Exit 0: every case bitwise equal.+#include "strata/kernels/dequant_bf16.hpp"++#include <cuda_runtime.h>++#include <cstdint>+#include <cstdio>+#include <cstdlib>+#include <cstring>+#include <random>+#include <string>+#include <vector>++namespace K = strata::kernels;++static void ck(cudaError_t e, const char* what) {+ if (e != cudaSuccess) { std::fprintf(stderr, "dequant_wide_parity: %s: %s\n", what, cudaGetErrorString(e)); std::exit(2); }+}+static uint16_t f2h(float f) { // FP32 -> FP16 bits, round to nearest even (normal range only: the scales below)+ uint32_t u;+ std::memcpy(&u, &f, 4);+ const uint32_t sign = (u >> 16) & 0x8000u;+ int e = (int) ((u >> 23) & 0xff) - 127 + 15;+ uint32_t m = u & 0x7fffffu;+ if (e <= 0) return (uint16_t) sign;+ if (e >= 31) return (uint16_t) (sign | 0x7c00u);+ uint32_t h = sign | ((uint32_t) e << 10) | (m >> 13);+ if ((m & 0x1fffu) > 0x1000u || ((m & 0x1fffu) == 0x1000u && (h & 1u))) ++h;+ return (uint16_t) h;+}+struct Type { int id; const char* name; int block_bytes; };+static const Type kTypes[] = {{12, "Q4_K", 144}, {13, "Q5_K", 176}, {14, "Q6_K", 210}, {23, "IQ4_XS", 136}};++// `sane`: random codes and scales with a block scale in a weight's range; else raw random bytes+static std::vector<uint8_t> make_blocks(const Type& t, int64_t rows, int64_t cols, bool sane, uint32_t seed) {+ std::mt19937 rng(seed);+ const int64_t nb = rows * (cols / 256);+ std::vector<uint8_t> b((size_t) nb * t.block_bytes);+ for (size_t i = 0; i + 4 <= b.size(); i += 4) { const uint32_t r = rng(); std::memcpy(&b[i], &r, 4); }+ for (size_t i = b.size() & ~(size_t) 3; i < b.size(); ++i) b[i] = (uint8_t) rng();+ if (sane) {+ std::uniform_real_distribution<float> ud(1e-4f, 3e-2f);+ for (int64_t k = 0; k < nb; ++k) {+ uint8_t* p = b.data() + (size_t) k * t.block_bytes;+ const uint16_t d = f2h((rng() & 1 ? 1.0f : -1.0f) * ud(rng)), dm = f2h(-ud(rng));+ if (t.id == 14) std::memcpy(p + 208, &d, 2);+ else if (t.id == 23) std::memcpy(p, &d, 2);+ else { std::memcpy(p, &d, 2); std::memcpy(p + 2, &dm, 2); }+ }+ }+ return b;+}++int main(int argc, char** argv) {+ setvbuf(stdout, nullptr, _IONBF, 0);+ const bool bench = argc > 1 && std::string(argv[1]) == "--bench";+ cudaStream_t s;+ ck(cudaStreamCreate(&s), "stream");+ struct Shape { int64_t rows, cols; };+ const Shape shapes[] = {{10240, 2560}, {6144, 2560}, {2560, 6144}, {12288, 2560}, {512, 2560}, {2560, 4096}, {320, 10240}, {7, 256}, {33, 768}};+ int cases = 0, failures = 0;+ for (const Type& t : kTypes) {+ for (const Shape& sh : shapes) {+ for (int sane = 1; sane >= 0; --sane) {+ const int64_t rows = sh.rows, cols = sh.cols, n = rows * cols;+ const std::vector<uint8_t> hb = make_blocks(t, rows, cols, sane != 0, (uint32_t) (t.id * 1000003 + rows * 31 + cols + sane));+ uint8_t* db = nullptr;+ ck(cudaMalloc((void**) &db, hb.size()), "blocks");+ ck(cudaMemcpy(db, hb.data(), hb.size(), cudaMemcpyHostToDevice), "upload");+ const int64_t ldp = cols + 64; // the padded stride (dequant_f16_ld)+ const size_t cap = (size_t) rows * (size_t) ldp * 4;+ uint8_t *o0 = nullptr, *o1 = nullptr;+ ck(cudaMalloc((void**) &o0, cap), "out0");+ ck(cudaMalloc((void**) &o1, cap), "out1");+ std::vector<uint8_t> h0(cap), h1(cap);+ // what: 0 FP16, 1 BF16, 2 FP32, 3 FP16 with the padded stride, 4 FP16 of a row slice+ for (int what = 0; what < 5; ++what) {+ const int64_t r0 = what == 4 ? rows / 3 : 0, nr = what == 4 ? rows - rows / 3 - rows / 5 : rows;+ if (nr <= 0) continue;+ for (int wide = 0; wide < 2; ++wide) {+ uint8_t* o = wide ? o1 : o0;+ ck(cudaMemsetAsync(o, 0xA5, cap, s), "memset");+ K::dequant_set_wide(wide);+ if (what == 0 || what == 4) K::dequant_f16(t.id, db, r0, nr, cols, (uint16_t*) o, s);+ else if (what == 1) K::dequant_bf16(t.id, db, r0, nr, cols, (uint16_t*) o, s);+ else if (what == 2) K::dequant_f32(t.id, db, r0, nr, cols, (float*) o, s);+ else if (!K::dequant_f16_ld(t.id, db, r0, nr, cols, ldp, (uint16_t*) o, s)) { std::printf("FAIL %s: dequant_f16_ld refused\n", t.name); ++failures; }+ ck(cudaMemcpyAsync(wide ? h1.data() : h0.data(), o, cap, cudaMemcpyDeviceToHost, s), "download");+ }+ ck(cudaStreamSynchronize(s), "sync");+ ++cases;+ if (std::memcmp(h0.data(), h1.data(), cap) != 0) {+ size_t nd = 0, first = 0;+ for (size_t i = 0; i < cap; ++i) if (h0[i] != h1[i]) { if (!nd) first = i; ++nd; }+ std::printf("FAIL %s %lldx%lld %s what %d: %zu bytes differ, first at %zu\n", t.name, (long long) rows, (long long) cols,+ sane ? "sane" : "raw", what, nd, first);+ ++failures;+ }+ // the default-pattern bytes outside the written values must be untouched by both (the padded stride's gaps)+ if (what == 3) {+ size_t bad = 0;+ for (int64_t r = 0; r < nr; ++r)+ for (int64_t c = cols; c < ldp; ++c)+ if (h1[(size_t) (r * ldp + c) * 2] != 0xA5 || h1[(size_t) (r * ldp + c) * 2 + 1] != 0xA5) ++bad;+ if (bad) { std::printf("FAIL %s %lldx%lld: the wide kernel wrote %zu values into the row padding\n", t.name, (long long) rows, (long long) cols, bad); ++failures; }+ }+ }+ if (bench && sane && rows >= 512) {+ cudaEvent_t e0, e1;+ cudaEventCreate(&e0);+ cudaEventCreate(&e1);+ double ms[2] = {0, 0};+ const int reps = 20;+ for (int wide = 0; wide < 2; ++wide) {+ K::dequant_set_wide(wide);+ for (int i = 0; i < 3; ++i) K::dequant_f16(t.id, db, 0, rows, cols, (uint16_t*) o0, s);+ cudaEventRecord(e0, s);+ for (int i = 0; i < reps; ++i) K::dequant_f16(t.id, db, 0, rows, cols, (uint16_t*) o0, s);+ cudaEventRecord(e1, s);+ ck(cudaEventSynchronize(e1), "bench sync");+ float tms = 0;+ cudaEventElapsedTime(&tms, e0, e1);+ ms[wide] = tms / reps;+ }+ const double bytes = (double) hb.size() + (double) n * 2;+ std::printf("BENCH %-6s %6lldx%-6lld per-group %8.3f ms (%5.1f GB/s) | wide %8.3f ms (%5.1f GB/s) | %.2fx\n", t.name, (long long) rows,+ (long long) cols, ms[0], bytes / ms[0] / 1e6, ms[1], bytes / ms[1] / 1e6, ms[0] / ms[1]);+ cudaEventDestroy(e0);+ cudaEventDestroy(e1);+ }+ cudaFree(db);+ cudaFree(o0);+ cudaFree(o1);+ }+ }+ }+ K::dequant_set_wide(-1);+ std::printf("dequant_wide_parity: %d cases, %s\n", cases, failures ? "FAILED" : "OK (bitwise)");+ return failures ? 1 : 0;+}