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++
decode: NVFP4 experts decode each weight part once per call for every token of the window (Split<40>), its UE4M3 scales from bits, down on 16-lane rows - bitwise the same, 1.14-2.5x
Niko1221546d767543a3
diff --git a/src/kernels/cuda/iq_kernels.cu b/src/kernels/cuda/iq_kernels.cuindex c62d7f382..6f8b65a94 100644--- a/src/kernels/cuda/iq_kernels.cu+++ b/src/kernels/cuda/iq_kernels.cu@@ -570,10 +570,12 @@ __device__ __forceinline__ float vec_dot_q8_0_q8_1(const void* __restrict__ vbq,// kvalues_fp4 holds the E2M1 values doubled (0, 1, 2, 3, 4, 6, 8, 12), so the scale is halved here - exactly// ggml's ggml_cuda_ue4m3_to_fp32, decoded in software so the GPU and ggml-cpu agree bit for bit (NaN -> 0).__device__ __forceinline__ float ue4m3_to_f32_half(uint8_t x) {+ // The same value as ldexpf(1 + man/8, exp - 7) * 0.5 (ldexpf(man, -9) * 0.5 for exp 0), built from its bits: every+ // such value is exact in FP32, so this is equal for all 256 inputs - without the ldexpf and the branches it cost+ // in the decode experts' inner loop. Normal: 2^(exp-8) * (1 + man/8) = exponent field exp + 119, mantissa man<<20.if (x == 0 || x == 0x7F) return 0.0f;- const int exp = (x >> 3) & 0xF, man = x & 0x7;- const float raw = exp == 0 ? ldexpf((float) man, -9) : ldexpf(1.0f + (float) man / 8.0f, exp - 7);- return raw * 0.5f;+ const uint32_t exp = (x >> 3) & 0xF, man = x & 0x7;+ return exp == 0 ? (float) man * 0.0009765625f : __uint_as_float(((exp + 119u) << 23) | (man << 20));}// One call = two sub-blocks (32 values); `bq8_1` is the block's first q8_1 block (two per 64 values).@@ -995,6 +997,38 @@ template<> struct Split<42> { // Q2_0return r.d2 * d8 * sumi;}};+template<> inline constexpr bool kSplit<40> = true;+template<> struct Split<40> { // NVFP4: vec_dot_nvfp4_q8_1's two 16-value sub-blocks, weights decoded once per call+ struct W { int2 v0[2], v1[2]; float sc[2]; };+ template<bool STAGE_GRID = false>+ __device__ static W load(const void* __restrict__ vbq, int kbx, int iqs, const uint32_t* __restrict__ = nullptr) {+ const block_nvfp4* bq4 = (const block_nvfp4*) vbq + kbx;+ W r;+#pragma unroll+ for (int i = 0; i < 2; i++) {+ const int iqs0 = iqs + 2 * i, iqs1 = iqs0 + 1, is = iqs0 >> 1;+ r.v0[i] = get_int_from_table_16(get_int_b4(bq4->qs, iqs0), kvalues_fp4);+ r.v1[i] = get_int_from_table_16(get_int_b4(bq4->qs, iqs1), kvalues_fp4);+ r.sc[i] = ue4m3_to_f32_half(bq4->d[is]);+ }+ return r;+ }+ __device__ static float apply(const W& r, const block_q8_1* __restrict__ bq8_1, int iqs) {+ float sum = 0.0f; // the same operations, in the same order, as the dot+#pragma unroll+ for (int i = 0; i < 2; i++) {+ const int is = (iqs + 2 * i) >> 1;+ const block_q8_1* bq8 = bq8_1 + (is >> 1);+ const int i8 = (is & 1) << 2;+ int sumi = ggml_cuda_dp4a(r.v0[i].x, get_int_b4(bq8->qs, i8 + 0), 0);+ sumi = ggml_cuda_dp4a(r.v0[i].y, get_int_b4(bq8->qs, i8 + 2), sumi);+ sumi = ggml_cuda_dp4a(r.v1[i].x, get_int_b4(bq8->qs, i8 + 1), sumi);+ sumi = ggml_cuda_dp4a(r.v1[i].y, get_int_b4(bq8->qs, i8 + 3), sumi);+ sum += r.sc[i] * __low2float(bq8->ds) * (float) sumi;+ }+ return sum;+ }+};// One row against the n <= NC activations x + off[0..n) (n >= 1, warp-uniform; offsets in q8_1 blocks, 32-bit to// spare registers), the whole warp. Per activation this is row_dot: the same calls k, lane-strided the same way,@@ -1077,7 +1111,7 @@ __device__ __forceinline__ void row_dot_40_sub8(const uint8_t* row, const block_}}-// 16-lane sub-warp row dot for K = nb * ipb == 20 (TD == 42, Q2_0 with n_ff == 640):+// 16-lane sub-warp row dot for K = nb * ipb == 20 (TD == 42, Q2_0, and TD == 40, NVFP4, with n_ff == 640):// 2 rows per warp (16 rows per 256-thread block), bitwise identical addition tree to row_dot_multi.template<int TY, int NC, bool EXACT_N = false>__device__ __forceinline__ void row_dot_20_sub16(const uint8_t* row, const block_q8_1* x, const int (&off)[NC], int n,@@ -1301,6 +1335,8 @@ __global__ void __launch_bounds__(256) native_gu_multi_kernel(const unsigned lonfloat* dst = is_up ? up : gate;for (int g = blockIdx.y; g < ng; g += gridDim.y) {const uint8_t* wr = (const uint8_t*) grp_ptr[g] + w_off;+ // NVFP4: the expert's s_gate / s_up on the FP32 output, as native_gu_kernel (1 otherwise: exact)+ const float sc = L.tail_off ? ((const float*) (grp_ptr[g] + L.tail_off))[is_up ? 1 : 0] : 1.0f;const int e0 = grp_start[g], e1 = grp_start[g + 1];for (int e = e0; e < e1; e += GRP_NC) {const int n = min(GRP_NC, e1 - e);@@ -1308,22 +1344,22 @@ __global__ void __launch_bounds__(256) native_gu_multi_kernel(const unsigned lonconst int off1[1] = { ent_tok[e] * 80 };float s1[1];row_dot_80_sub16<TG, 1, true, STAGE_GRID>(wr, xq, off1, 1, t, s1, s_grid);- if (t == 0) dst[(size_t) e * L.n_ff + r] = s1[0];+ if (t == 0) dst[(size_t) e * L.n_ff + r] = s1[0] * sc;} else if (n == 2) {const int off2[2] = { ent_tok[e] * 80, ent_tok[e + 1] * 80 };float s2[2];row_dot_80_sub16<TG, 2, true, STAGE_GRID>(wr, xq, off2, 2, t, s2, s_grid);- if (t < 2) dst[(size_t) (e + t) * L.n_ff + r] = s2[t];+ if (t < 2) dst[(size_t) (e + t) * L.n_ff + r] = s2[t] * sc;} else if (n == 3) {const int off3[3] = { ent_tok[e] * 80, ent_tok[e + 1] * 80, ent_tok[e + 2] * 80 };float s3[3];row_dot_80_sub16<TG, 3, true, STAGE_GRID>(wr, xq, off3, 3, t, s3, s_grid);- if (t < 3) dst[(size_t) (e + t) * L.n_ff + r] = s3[t];+ if (t < 3) dst[(size_t) (e + t) * L.n_ff + r] = s3[t] * sc;} else {const int off4[4] = { ent_tok[e] * 80, ent_tok[e + 1] * 80, ent_tok[e + 2] * 80, ent_tok[e + 3] * 80 };float s4[4];row_dot_80_sub16<TG, 4, true, STAGE_GRID>(wr, xq, off4, 4, t, s4, s_grid);- if (t < 4) dst[(size_t) (e + t) * L.n_ff + r] = s4[t];+ if (t < 4) dst[(size_t) (e + t) * L.n_ff + r] = s4[t] * sc;}}}@@ -1339,6 +1375,8 @@ __global__ void __launch_bounds__(256) native_gu_multi_kernel(const unsigned lonfloat* dst = is_up ? up : gate;for (int g = blockIdx.y; g < ng; g += gridDim.y) { // the group stride, as native_gu_kernelconst uint8_t* wr = (const uint8_t*) grp_ptr[g] + w_off;+ // NVFP4: the expert's s_gate / s_up on the FP32 output, as native_gu_kernel (1 otherwise: exact)+ const float sc = L.tail_off ? ((const float*) (grp_ptr[g] + L.tail_off))[is_up ? 1 : 0] : 1.0f;const int e0 = grp_start[g], e1 = grp_start[g + 1];for (int e = e0; e < e1; e += GRP_NC) {const int n = min(GRP_NC, e1 - e);@@ -1346,22 +1384,22 @@ __global__ void __launch_bounds__(256) native_gu_multi_kernel(const unsigned lonconst int off1[1] = { ent_tok[e] * xb };float s1[1];row_dot_multi<TG, 1, true, STAGE_GRID>(wr, xq, off1, 1, nb, lane, s1, s_grid);- if (lane == 0) dst[(size_t) e * L.n_ff + r] = s1[0];+ if (lane == 0) dst[(size_t) e * L.n_ff + r] = s1[0] * sc;} else if (n == 2) {const int off2[2] = { ent_tok[e] * xb, ent_tok[e + 1] * xb };float s2[2];row_dot_multi<TG, 2, true, STAGE_GRID>(wr, xq, off2, 2, nb, lane, s2, s_grid);- if (lane < 2) dst[(size_t) (e + lane) * L.n_ff + r] = s2[lane];+ if (lane < 2) dst[(size_t) (e + lane) * L.n_ff + r] = s2[lane] * sc;} else if (n == 3) {const int off3[3] = { ent_tok[e] * xb, ent_tok[e + 1] * xb, ent_tok[e + 2] * xb };float s3[3];row_dot_multi<TG, 3, true, STAGE_GRID>(wr, xq, off3, 3, nb, lane, s3, s_grid);- if (lane < 3) dst[(size_t) (e + lane) * L.n_ff + r] = s3[lane];+ if (lane < 3) dst[(size_t) (e + lane) * L.n_ff + r] = s3[lane] * sc;} else {const int off4[4] = { ent_tok[e] * xb, ent_tok[e + 1] * xb, ent_tok[e + 2] * xb, ent_tok[e + 3] * xb };float s4[4];row_dot_multi<TG, 4, true, STAGE_GRID>(wr, xq, off4, 4, nb, lane, s4, s_grid);- if (lane < 4) dst[(size_t) (e + lane) * L.n_ff + r] = s4[lane];+ if (lane < 4) dst[(size_t) (e + lane) * L.n_ff + r] = s4[lane] * sc;}}}@@ -1421,6 +1459,7 @@ __global__ void __launch_bounds__(256) native_down_multi_kernel(const unsigned lconst block_q8_1* s_hq = reinterpret_cast<const block_q8_1*>(s_hq_buf);for (int g = blockIdx.y; g < ng; g += gridDim.y) {const uint8_t* wr = (const uint8_t*) grp_ptr[g] + w_off;+ const float sd = L.tail_off ? ((const float*) (grp_ptr[g] + L.tail_off))[2] : 1.0f; // NVFP4: s_downconst int e0 = grp_start[g], e1 = grp_start[g + 1];for (int e = e0; e < e1; e += GRP_NC) {const int n = min(GRP_NC, e1 - e);@@ -1434,29 +1473,29 @@ __global__ void __launch_bounds__(256) native_down_multi_kernel(const unsigned lconst int off1[1] = { 0 };float s1[1];row_dot_40_sub8<TD, 1, true>(wr, s_hq, off1, 1, t, s1);- if (t == 0) out[(size_t) ent_dst[e] * L.n_embd + r] = s1[0];+ if (t == 0) out[(size_t) ent_dst[e] * L.n_embd + r] = s1[0] * sd;} else if (n == 2) {const int off2[2] = { 0, 20 };float s2[2];row_dot_40_sub8<TD, 2, true>(wr, s_hq, off2, 2, t, s2);- if (t < 2) out[(size_t) ent_dst[e + t] * L.n_embd + r] = s2[t];+ if (t < 2) out[(size_t) ent_dst[e + t] * L.n_embd + r] = s2[t] * sd;} else if (n == 3) {const int off3[3] = { 0, 20, 40 };float s3[3];row_dot_40_sub8<TD, 3, true>(wr, s_hq, off3, 3, t, s3);- if (t < 3) out[(size_t) ent_dst[e + t] * L.n_embd + r] = s3[t];+ if (t < 3) out[(size_t) ent_dst[e + t] * L.n_embd + r] = s3[t] * sd;} else {const int off4[4] = { 0, 20, 40, 60 };float s4[4];row_dot_40_sub8<TD, 4, true>(wr, s_hq, off4, 4, t, s4);- if (t < 4) out[(size_t) ent_dst[e + t] * L.n_embd + r] = s4[t];+ if (t < 4) out[(size_t) ent_dst[e + t] * L.n_embd + r] = s4[t] * sd;}}}}return;}- } else if constexpr (SUB_DOWN && TD == 42) {+ } else if constexpr (SUB_DOWN && (TD == 42 || TD == 40)) { // Q2_0 and NVFP4: K = 20 calls a row at n_ff 640if (hb == 20) {const int r = blockIdx.x * 16 + (threadIdx.x >> 4);const int t = threadIdx.x & 15;@@ -1465,6 +1504,7 @@ __global__ void __launch_bounds__(256) native_down_multi_kernel(const unsigned lconst block_q8_1* s_hq = reinterpret_cast<const block_q8_1*>(s_hq_buf);for (int g = blockIdx.y; g < ng; g += gridDim.y) {const uint8_t* wr = (const uint8_t*) grp_ptr[g] + w_off;+ const float sd = L.tail_off ? ((const float*) (grp_ptr[g] + L.tail_off))[2] : 1.0f; // NVFP4: s_downconst int e0 = grp_start[g], e1 = grp_start[g + 1];for (int e = e0; e < e1; e += GRP_NC) {const int n = min(GRP_NC, e1 - e);@@ -1478,22 +1518,22 @@ __global__ void __launch_bounds__(256) native_down_multi_kernel(const unsigned lconst int off1[1] = { 0 };float s1[1];row_dot_20_sub16<TD, 1, true>(wr, s_hq, off1, 1, t, s1);- if (t == 0) out[(size_t) ent_dst[e] * L.n_embd + r] = s1[0];+ if (t == 0) out[(size_t) ent_dst[e] * L.n_embd + r] = s1[0] * sd;} else if (n == 2) {const int off2[2] = { 0, 20 };float s2[2];row_dot_20_sub16<TD, 2, true>(wr, s_hq, off2, 2, t, s2);- if (t < 2) out[(size_t) ent_dst[e + t] * L.n_embd + r] = s2[t];+ if (t < 2) out[(size_t) ent_dst[e + t] * L.n_embd + r] = s2[t] * sd;} else if (n == 3) {const int off3[3] = { 0, 20, 40 };float s3[3];row_dot_20_sub16<TD, 3, true>(wr, s_hq, off3, 3, t, s3);- if (t < 3) out[(size_t) ent_dst[e + t] * L.n_embd + r] = s3[t];+ if (t < 3) out[(size_t) ent_dst[e + t] * L.n_embd + r] = s3[t] * sd;} else {const int off4[4] = { 0, 20, 40, 60 };float s4[4];row_dot_20_sub16<TD, 4, true>(wr, s_hq, off4, 4, t, s4);- if (t < 4) out[(size_t) ent_dst[e + t] * L.n_embd + r] = s4[t];+ if (t < 4) out[(size_t) ent_dst[e + t] * L.n_embd + r] = s4[t] * sd;}}}@@ -1507,6 +1547,7 @@ __global__ void __launch_bounds__(256) native_down_multi_kernel(const unsigned lconst size_t w_off = L.down_off + (size_t) (valid_r ? r : 0) * L.d_row;for (int g = blockIdx.y; g < ng; g += gridDim.y) { // the group stride, as native_gu_kernelconst uint8_t* wr = (const uint8_t*) grp_ptr[g] + w_off;+ const float sd = L.tail_off ? ((const float*) (grp_ptr[g] + L.tail_off))[2] : 1.0f; // NVFP4: s_downconst int e0 = grp_start[g], e1 = grp_start[g + 1];if (hb == 20) {const block_q8_1* s_hq = reinterpret_cast<const block_q8_1*>(s_hq_buf);@@ -1522,12 +1563,12 @@ __global__ void __launch_bounds__(256) native_down_multi_kernel(const unsigned lconst int off1[1] = { 0 };float s1[1];row_dot_multi<TD, 1, true>(wr, s_hq, off1, 1, nb, lane, s1);- if (lane == 0) out[(size_t) ent_dst[e] * L.n_embd + r] = s1[0];+ if (lane == 0) out[(size_t) ent_dst[e] * L.n_embd + r] = s1[0] * sd;} else if (n == 2) {const int off2[2] = { 0, 20 };float s2[2];row_dot_multi<TD, 2, true>(wr, s_hq, off2, 2, nb, lane, s2);- if (lane < 2) out[(size_t) ent_dst[e + lane] * L.n_embd + r] = s2[lane];+ if (lane < 2) out[(size_t) ent_dst[e + lane] * L.n_embd + r] = s2[lane] * sd;} else {int off[GRP_NC];#pragma unroll@@ -1536,7 +1577,7 @@ __global__ void __launch_bounds__(256) native_down_multi_kernel(const unsigned lrow_dot_multi<TD, GRP_NC>(wr, s_hq, off, n, nb, lane, s);#pragma unrollfor (int c = 0; c < GRP_NC; ++c)- if (c < n && lane == c) out[(size_t) ent_dst[e + c] * L.n_embd + r] = s[c];+ if (c < n && lane == c) out[(size_t) ent_dst[e + c] * L.n_embd + r] = s[c] * sd;}}}@@ -1550,7 +1591,7 @@ __global__ void __launch_bounds__(256) native_down_multi_kernel(const unsigned lrow_dot_multi<TD, GRP_NC>(wr, hq, off, n, nb, lane, s);#pragma unrollfor (int c = 0; c < GRP_NC; ++c)- if (c < n && lane == c) out[(size_t) ent_dst[e + c] * L.n_embd + r] = s[c];+ if (c < n && lane == c) out[(size_t) ent_dst[e + c] * L.n_embd + r] = s[c] * sd;}}}@@ -3663,7 +3704,7 @@ void native_expert_grouped(const NativeExpertLayout& L, const unsigned long longcheck("native_expert_grouped/swiglu");}if (g_exp_phase == 1) return;- const int d_rows = (!g_old_kernels && !g_no_sub16_gu && L.n_ff == 640) ? (L.d_type == 20 ? 32 : (L.d_type == 42 ? 16 : 8)) : 8;+ const int d_rows = (!g_old_kernels && !g_no_sub16_gu && L.n_ff == 640) ? (L.d_type == 20 ? 32 : (L.d_type == 42 || L.d_type == 40 ? 16 : 8)) : 8;const dim3 gd((unsigned) ((L.n_embd + d_rows - 1) / d_rows), (unsigned) gy);#if STRATA_EXP_LAYOUTSif ((em0 == 7 || em0 == 8) && (L.d_type == 20 || L.d_type == 42)) {