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++
native experts: Q4_0 and Q4_1 GPU kernels, Q4_0 PLE table
saikiran-rs16bdc7073d41
diff --git a/CMakeLists.txt b/CMakeLists.txtindex ae36e40e9..c7b44c3e8 100644--- a/CMakeLists.txt+++ b/CMakeLists.txt@@ -1166,7 +1166,9 @@ if(STRATA_NATIVE_EXPERTS)# kernel for it before; q5_0/q5_0 (both roles Q5_0) is left out of this list since stacking the same# lower-precision format on both roles pushes the GPU path over this test's 3% threshold (quantization# noise, not a kernel bug -- see the PR).- foreach(_pair q4_K/q5_1 q4_K/q8_0 q5_K/q8_0 q8_0/q8_0 q4_K/q5_0 q5_0/q8_0)+ # q4_0/q4_0 and q4_0/q4_1: a plain llama-quantize Q4_0 file (bartowski's Qwen3.8-Flash-Next-Q4_0: gate/up Q4_0,+ # down Q4_0 on 42 layers and Q4_1 on 6)+ foreach(_pair q4_K/q5_1 q4_K/q8_0 q5_K/q8_0 q8_0/q8_0 q4_K/q5_0 q5_0/q8_0 q4_0/q4_0 q4_0/q4_1)string(REPLACE "/" "_" _name "${_pair}")add_test(NAME native_expert_parity_${_name} COMMAND native_expert_parity --synthetic ${_pair})set_tests_properties(native_expert_parity_${_name} PROPERTIES TIMEOUT 120)diff --git a/docs/UNSLOTH_Q4.md b/docs/UNSLOTH_Q4.mdindex e275dec7c..9e2601019 100644--- a/docs/UNSLOTH_Q4.md+++ b/docs/UNSLOTH_Q4.md@@ -248,13 +248,44 @@ the pack rounds the file's Q8_0 hyper-connection projections to BF16, and on theperplexity 6-9% above llama.cpp's on the same file (teacher-forced, short context). Reading the Q8_0 values insteadcloses most of that gap; it is measured on AMD only and not in this release.+## Q4_0 files++A plain `llama-quantize` Q4_0 file of this model runs the same way: pack shard 1 with `--compat-bf16` as above and+point `--native` at it. Tested:+[bartowski/Qwen3.8-Flash-Next-GGUF, `Qwen3.8-Flash-Next-Q4_0`, revision `928589f`](https://huggingface.co/bartowski/Qwen3.8-Flash-Next-GGUF/tree/928589fdb66c6ff07f22ac561e3fbce76553548f/Qwen3.8-Flash-Next-Q4_0)+(3 shards, 100.6 GB). Its routed experts are Q4_0 for gate/up and for down, except down in layers 0-5, which is Q4_1;+the PLE table and the token embedding are Q4_0. The pack serves 302 tensors from the GGUF as stored and converts 362+(168 exact, 194 rounded to BF16, largest absolute error 0.0156; 1.23 GiB).++The Q4_0 expert kernels centre the codes (q - 8) before the integer dot product, as ggml-cpu's Q4_0 x Q8_0 dot does,+instead of llama.cpp's CUDA form (uncentred codes, minus 8 x the Q8_1 block sum in float); Q4_1 follows the Q5_1+kernel without the fifth bit. `native_expert_parity` on this file (layers 0 and 5: Q4_0/Q4_1, layers 20 and 47:+Q4_0/Q4_0): relative error against the float product 1.1-1.3e-2 on the CPU and the GPU alike, CPU vs GPU 1.1e-7 for+Q4_0/Q4_0 and 5e-4 to 1.1e-3 with Q4_1 down.++Measured on one RTX 3060 12 GB (PCIe 3.0 x16), Xeon E5-2696 v4, 121 GiB DDR4-2400, Linux, CUDA 12.4; every expert in+RAM, `--max-context 131072 --kv int8 --kv-resident 20480`, expert cache `auto` (1,960 slots, 5.06 GiB), prompt chunk+`auto` (6,144 tokens); greedy, `reasoning_effort` medium, one request at a time:++| Prompt | Prompt read | Output |+| --- | --- | --- |+| 63 tokens (prose) | - | 25.4 tok/s |+| 111 tokens (code) | - | 28.0 tok/s |+| 24,461 tokens | 787 tok/s | 24.0 tok/s |+| 67,934 tokens (a fact hidden in the middle: found) | 737 tok/s | 26.4 tok/s |++A coding prompt was answered correctly. Not measured: agreement with llama.cpp on this file (as+[above](#quality-against-llamacpp-on-the-same-file) for UD-Q4_K_XL), AMD, Windows, and bartowski's Swift-1.5 Q4_0,+whose header shows the same formats.+## Scope and validation-- UD-Q4_K_XL and UD-IQ4_XS at revision `38bb39e` are targeted. Other Unsloth quantizations use formats this engine may- not have kernels for; the engine checks every layer's formats at start and refuses an unsupported one by name.+- UD-Q4_K_XL and UD-IQ4_XS at revision `38bb39e` are targeted, and plain Q4_0 files ([above](#q4_0-files)). Other+ quantizations use formats this engine may not have kernels for; the engine checks every layer's formats at start+ and refuses an unsupported one by name.- Tests: the packer's synthetic 4-shard and conversion tests (`.venv/bin/python -m unittest discover -s tools -ptest_iq_pack.py`); CTests `gguf_split_test`, `expert_layout_test`, `native_expert_parity_*` (the three real expert- format pairs against ggml-cpu, the Q5_1 min term, Q8_0 rows), `prefill_mmq_kquant_test` (with `-DSTRATA_MMQ_KQUANTS=ON`: the+ format pairs against ggml-cpu, the Q5_1 min term, Q8_0 rows, Q4_0/Q4_0 and Q4_0/Q4_1), `prefill_mmq_kquant_test` (with `-DSTRATA_MMQ_KQUANTS=ON`: theprompt path's MMQ products for Q4_K / Q5_K / Q5_1 / Q8_0 against ggml's dequantized weights); the in-place mode against `experts.bin` on the Coder(identical tokens and logits).- Real runs: greedy answers to a coding prompt (correct) at every budget and setting above, identical across them;diff --git a/include/strata/kernels/ngram.hpp b/include/strata/kernels/ngram.hppindex c0c858584..da7373896 100644--- a/include/strata/kernels/ngram.hpp+++ b/include/strata/kernels/ngram.hpp@@ -54,7 +54,7 @@ inline constexpr int PLE_ROW_BYTES_MAX = PLE_ROW_BYTES_Q8_0; // the/// 160-value row, how to turn a row into floats) and nothing else: `PleTable::open`, `format()`, the reader's row size,/// the error string and the buffers' size all read this list, so a new format cannot be in one switch and missing from/// another. A row is always `PLE_HEAD_DIM` = 160 values = 5 blocks of 32 for the block formats.-enum class PleFormat : uint8_t { IQ4_NL, Q5_0, F8_E4M3, Q5_1, Q8_0 };+enum class PleFormat : uint8_t { IQ4_NL, Q5_0, F8_E4M3, Q5_1, Q8_0, Q4_0 };struct PleFormatInfo {PleFormat id;diff --git a/src/kernels/cuda/iq_kernels.cu b/src/kernels/cuda/iq_kernels.cuindex 4b662ab06..53bf360bc 100644--- a/src/kernels/cuda/iq_kernels.cu+++ b/src/kernels/cuda/iq_kernels.cu@@ -310,7 +310,7 @@ __device__ __forceinline__ float vec_dot_iq4_xs_q8_1(const void* __restrict__ vb// Q4_K / Q5_K gate/up and Q5_1 / Q8_0 down: llama.cpp's vec_dot_*_q8_1 (vecdotq.cuh, VDR 2 each), transcribed; the// Q5_1 min term is the one departure (below). Via eddoursul/Strata 8029fa9 (iq_dot.cuh) and #255 (Q8_0,// gopinath87607), which agree with llama.cpp and with each other.-constexpr int VDR_Q4_K = 2, VDR_Q5_K = 2, VDR_Q5_1 = 2, VDR_Q5_0 = 2, VDR_Q8_0 = 2;+constexpr int VDR_Q4_K = 2, VDR_Q5_K = 2, VDR_Q5_1 = 2, VDR_Q5_0 = 2, VDR_Q4_1 = 2, VDR_Q4_0 = 2, VDR_Q8_0 = 2;__device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_vmmq(const int* __restrict__ v, const int* __restrict__ u,const uint8_t* __restrict__ sc, const uint8_t* __restrict__ m,@@ -450,6 +450,46 @@ __device__ __forceinline__ float vec_dot_q5_0_q8_1(const void* __restrict__ vbq,const float s8 = __high2float(bq8_1->ds);return d5 * (sumi * d8 - (16.0f * VDR_Q5_0 / QI5_0) * s8);}+// Q4_0 (plain llama-quantize Q4_0 files, e.g. bartowski's: gate/up and most down projections): the codes are centred+// (q - 8 as signed bytes) before the dot, the exact integer form ggml-cpu's q4_0 x q8_0 dot uses, rather than+// llama.cpp's CUDA chain (uncentred codes minus 8 x the q8_1 block's float sum `ds.y`). On a real Q4_0 expert+// (2560 x 640) the CUDA chain measured 1.4e-2 relative error against the float product, the centred form 5.3e-3;+// with the CUDA chain native_expert_parity failed its 3e-2 limit on two layers of a Q4_0 Flash-Next file. The+// centring is the same idea as the Q5_1 min term above: the offset is applied to the activations' own int8 codes.+__device__ __forceinline__ int q4_0_centred(const int v) {+ const int b = v ^ 0x08080808; // per byte: 0..15 -> (q - 8) as a 4-bit two's complement value+ return b | ((b & 0x08080808) * 0x1E); // sign-extend each nibble into its byte (0x08 * 0x1E = 0xF0)+}+__device__ __forceinline__ float vec_dot_q4_0_q8_1(const void* __restrict__ vbq, const block_q8_1* __restrict__ bq8_1,+ const int& kbx, const int& iqs) {+ const block_q4_0* bq4 = (const block_q4_0*) vbq + kbx;+ int sumi = 0;+#pragma unroll+ for (int i = 0; i < VDR_Q4_0; ++i) {+ const int v = get_int_b2(bq4->qs, iqs + i);+ sumi = ggml_cuda_dp4a(q4_0_centred((v >> 0) & 0x0F0F0F0F), get_int_b4(bq8_1->qs, iqs + i), sumi);+ sumi = ggml_cuda_dp4a(q4_0_centred((v >> 4) & 0x0F0F0F0F), get_int_b4(bq8_1->qs, iqs + i + QI4_0), sumi);+ }+ return __half2float(bq4->d) * __low2float(bq8_1->ds) * sumi;+}+// Q4_1 (llama-quantize puts it on a few down projections of its Q4_0 files): the Q5_1 chain above without the fifth+// bit, the same min-term choice (the activations' own int8 codes): sumi * (d4 * d8) + sumu * (m4 * d8).+__device__ __forceinline__ float vec_dot_q4_1_q8_1(const void* __restrict__ vbq, const block_q8_1* __restrict__ bq8_1,+ const int& kbx, const int& iqs) {+ const block_q4_1* bq4_1 = (const block_q4_1*) vbq + kbx;+ int sumi = 0, sumu = 0;+#pragma unroll+ for (int i = 0; i < VDR_Q4_1; ++i) {+ const int v = get_int_b4(bq4_1->qs, iqs + i);+ const int u0 = get_int_b4(bq8_1->qs, iqs + i), u1 = get_int_b4(bq8_1->qs, iqs + i + QI4_1);+ sumi = ggml_cuda_dp4a((v >> 0) & 0x0F0F0F0F, u0, sumi);+ sumi = ggml_cuda_dp4a((v >> 4) & 0x0F0F0F0F, u1, sumi);+ sumu = ggml_cuda_dp4a(0x01010101, u1, ggml_cuda_dp4a(0x01010101, u0, sumu));+ }+ const float2 dm4 = __half22float2(bq4_1->dm);+ const float d8 = __low2float(bq8_1->ds);+ return sumi * (dm4.x * d8) + sumu * (dm4.y * d8);+}__device__ __forceinline__ float vec_dot_q5_1_q8_1(const void* __restrict__ vbq, const block_q8_1* __restrict__ bq8_1,const int& kbx, const int& iqs) {const block_q5_1* bq5_1 = (const block_q5_1*) vbq + kbx;@@ -515,6 +555,10 @@ template<> struct Fmt<13> { static constexpr int qk = 256, ipb = QI5_K / VDR_Q5___device__ static float dot(const void* v, const block_q8_1* y, int kbx, int iqs) { return vec_dot_q5_K_q8_1(v, y, kbx, iqs); } };template<> struct Fmt<7> { static constexpr int qk = 32, ipb = QI5_1 / VDR_Q5_1, step = VDR_Q5_1;__device__ static float dot(const void* v, const block_q8_1* y, int kbx, int iqs) { return vec_dot_q5_1_q8_1(v, y, kbx, iqs); } };+template<> struct Fmt<2> { static constexpr int qk = 32, ipb = QI4_0 / VDR_Q4_0, step = VDR_Q4_0;+ __device__ static float dot(const void* v, const block_q8_1* y, int kbx, int iqs) { return vec_dot_q4_0_q8_1(v, y, kbx, iqs); } };+template<> struct Fmt<3> { static constexpr int qk = 32, ipb = QI4_1 / VDR_Q4_1, step = VDR_Q4_1;+ __device__ static float dot(const void* v, const block_q8_1* y, int kbx, int iqs) { return vec_dot_q4_1_q8_1(v, y, kbx, iqs); } };template<> struct Fmt<6> { static constexpr int qk = 32, ipb = QI5_0 / VDR_Q5_0, step = VDR_Q5_0;__device__ static float dot(const void* v, const block_q8_1* y, int kbx, int iqs) { return vec_dot_q5_0_q8_1(v, y, kbx, iqs); } };template<> struct Fmt<8> { static constexpr int qk = 32, ipb = QI8_0 / VDR_Q8_0, step = VDR_Q8_0;@@ -522,9 +566,9 @@ template<> struct Fmt<8> { static constexpr int qk = 32, ipb = QI8_0 / VDR_Q8_0,// The formats of each role, one list each so a type cannot be in one switch and missing from another. Every// entry is a kernel template for each CUDA architecture of the build, hence two lists rather than one.-#define STRATA_GU_FMTS(X) X(16) X(17) X(18) X(21) X(22) X(23) X(29) X(42) X(12) X(13) X(6) X(8)-#define STRATA_D_FMTS(X) X(20) X(23) X(42) X(7) X(6) X(8)-#define STRATA_MMVQ_FMTS(X) X(16) X(17) X(18) X(20) X(21) X(22) X(23) X(29) X(42) X(12) X(13) X(7) X(6) X(8)+#define STRATA_GU_FMTS(X) X(16) X(17) X(18) X(21) X(22) X(23) X(29) X(42) X(12) X(13) X(6) X(2) X(3) X(8)+#define STRATA_D_FMTS(X) X(20) X(23) X(42) X(7) X(6) X(2) X(3) X(8)+#define STRATA_MMVQ_FMTS(X) X(16) X(17) X(18) X(20) X(21) X(22) X(23) X(29) X(42) X(12) X(13) X(7) X(6) X(2) X(3) X(8)__device__ __forceinline__ float warp_sum(float v) {#pragma unroll@@ -1606,6 +1650,30 @@ __device__ void dq_q5_0(const void* vx, int64_t ibs, dst_t* yy, int tid) {}}template<typename dst_t>+__device__ void dq_q4_0(const void* vx, int64_t ibs, dst_t* yy, int tid) {+ const block_q4_0* x = (const block_q4_0*) vx + ibs * (QK_K / QK4_0);+ const int ib = tid % 8, il = tid / 8;+ const float d = __half2float(x[ib].d);+ dst_t* y = yy + 32 * ib;+ for (int j = 0; j < 4; ++j) {+ const int iqs = 4 * il + j; // llama.cpp's dequantize_q4_0 for value pairs iqs, iqs + 16+ y[iqs] = cvt<dst_t>(((float) (x[ib].qs[iqs] & 0xf) - 8.0f) * d);+ y[iqs + 16] = cvt<dst_t>(((float) (x[ib].qs[iqs] >> 4) - 8.0f) * d);+ }+}+template<typename dst_t>+__device__ void dq_q4_1(const void* vx, int64_t ibs, dst_t* yy, int tid) {+ const block_q4_1* x = (const block_q4_1*) vx + ibs * (QK_K / QK4_1);+ const int ib = tid % 8, il = tid / 8;+ const float2 dm = __half22float2(x[ib].dm);+ dst_t* y = yy + 32 * ib;+ for (int j = 0; j < 4; ++j) {+ const int iqs = 4 * il + j; // llama.cpp's dequantize_q4_1 for value pairs iqs, iqs + 16+ y[iqs] = cvt<dst_t>((float) (x[ib].qs[iqs] & 0xf) * dm.x + dm.y);+ y[iqs + 16] = cvt<dst_t>((float) (x[ib].qs[iqs] >> 4) * dm.x + dm.y);+ }+}+template<typename dst_t>__device__ void dq_q5_1(const void* vx, int64_t ibs, dst_t* yy, int tid) {const block_q5_1* x = (const block_q5_1*) vx + ibs * (QK_K / QK5_1);const int ib = tid % 8, il = tid / 8;@@ -1656,6 +1724,8 @@ __device__ __forceinline__ void dq_dispatch(int ty, const void* vx, int64_t ibs,case 13: dq_q5_k(vx, ibs, y, tid); break;case 7: dq_q5_1(vx, ibs, y, tid); break;case 6: dq_q5_0(vx, ibs, y, tid); break;+ case 2: dq_q4_0(vx, ibs, y, tid); break;+ case 3: dq_q4_1(vx, ibs, y, tid); break;case 8: dq_q8_0(vx, ibs, y, tid); break;case 30: dq_bf16(vx, ibs, y, tid); break;default: break;@@ -1680,7 +1750,7 @@ __global__ void dequant_gu_kernel(int ty, const void* __restrict__ gate, const v// the types dq_dispatch dequantizesbool is_iq(int t) {return t == 16 || t == 17 || t == 18 || t == 20 || t == 21 || t == 22 || t == 23 || t == 29 || t == 42 || t == 11 ||- t == 12 || t == 13 || t == 7 || t == 6 || t == 8;+ t == 12 || t == 13 || t == 7 || t == 6 || t == 2 || t == 3 || t == 8;}// values per block of the types the grouped expert kernels take (0 = none)int gu_qk(int t) {@@ -1793,6 +1863,8 @@ size_t iq_row_bytes(int t, int64_t n) noexcept {case 13: return (size_t) (n / 256) * sizeof(block_q5_K);case 7: return (size_t) (n / 32) * sizeof(block_q5_1);case 6: return (size_t) (n / 32) * sizeof(block_q5_0);+ case 2: return (size_t) (n / 32) * sizeof(block_q4_0);+ case 3: return (size_t) (n / 32) * sizeof(block_q4_1);case 8: return (size_t) (n / 32) * sizeof(block_q8_0);case 30: return (size_t) n * 2; // BF16: the token embedding only (iq_embed_rows, iq_dequant_f32)default: return 0;diff --git a/src/kernels/ngram.cpp b/src/kernels/ngram.cppindex 2d338a640..259bfe88d 100644--- a/src/kernels/ngram.cpp+++ b/src/kernels/ngram.cpp@@ -155,6 +155,8 @@ const PleFormatInfo kPleFormats[] = {// Ordinary GGUFs ship the table in other types: Unsloth's UD-Q6_K_XL and Swift-1.5 Q4_K_L as Q8_0, a Q5_K_M finetune as Q5_1{PleFormat::Q5_1, "Q5_1", "Q5_1", PLE_ROW_BYTES_Q5_1, false, dequant_blocks<strata::dequantize_q5_1, 24>},{PleFormat::Q8_0, "Q8_0", "Q8_0", PLE_ROW_BYTES_Q8_0, false, dequant_blocks<strata::dequantize_q8_0, 34>},+ // Q4_0 (plain llama-quantize Q4_0 files, #599): 90-byte rows like IQ4_NL (18-byte blocks), a linear 4-bit grid+ {PleFormat::Q4_0, "Q4_0", "Q4_0", PLE_ROW_BYTES, false, dequant_blocks<strata::dequantize_q4_0, 18>},};constexpr int kPleFormatCount = (int) (sizeof kPleFormats / sizeof kPleFormats[0]);} // namespace