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++
iq_kernels: Q4_K / Q5_K / Q5_1 / Q8_0 native experts (Unsloth UD-Q4_K_XL), per-role format lists, native_expert_supported at startup, Q8_0 embedding and PLE key
Niko1221ee7c802664c9
diff --git a/CMakeLists.txt b/CMakeLists.txtindex ab2eef403..f2911175f 100644--- a/CMakeLists.txt+++ b/CMakeLists.txt@@ -862,6 +862,19 @@ if(STRATA_NATIVE_EXPERTS)add_executable(native_expert_parity src/kernels/native_expert_parity.cpp)target_include_directories(native_expert_parity PRIVATE ${_strata_gpu_include_directories})target_link_libraries(native_expert_parity PRIVATE strata_kernels strata_kernels_cpu strata_artifact ${_strata_gpu_runtime_target})+ if(STRATA_BUILD_TESTS AND STRATA_ENABLE_CUDA)+ # Unsloth UD-Q4_K_XL's three expert pairs (and Q8_0 alone, its embedding) on synthetic ggml-quantized weights+ # at 2560/640: GPU and CPU experts against the float reference, GPU dequantizers against to_float+ foreach(_pair q4_K/q5_1 q4_K/q8_0 q5_K/q8_0 q8_0/q8_0)+ 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)+ endforeach()+ add_test(NAME native_expert_parity_q5_1_min COMMAND native_expert_parity --q5_1-min)+ # a pair without GPU kernels (Q6_K gate/up) is refused by native_expert_supported, not run+ add_test(NAME native_expert_parity_refuses_q6_K COMMAND native_expert_parity --synthetic q6_K/q8_0)+ set_tests_properties(native_expert_parity_refuses_q6_K PROPERTIES WILL_FAIL TRUE)+ endif()endif()endif()diff --git a/include/strata/kernels/iq_kernels.hpp b/include/strata/kernels/iq_kernels.hppindex 82730f1b7..1beb9a149 100644--- a/include/strata/kernels/iq_kernels.hpp+++ b/include/strata/kernels/iq_kernels.hpp@@ -1,5 +1,6 @@// include/strata/kernels/iq_kernels.hpp - the i-quant formats (IQ1_M, IQ2_XXS, IQ2_XS, IQ2_S, IQ3_XXS, IQ3_S,-// IQ4_NL) and Q2_0 on the GPU for the IQ2_XS / IQ3_XXS model files.+// IQ4_NL) and Q2_0 on the GPU for the IQ2_XS / IQ3_XXS model files, and Q4_K / Q5_K / Q5_1 / Q8_0 for Unsloth's+// UD-Q4_K_XL (gate/up Q4_K or Q5_K, down Q5_1 or Q8_0, a Q8_0 embedding).//// The block layouts, codebook grids and dot products are llama.cpp's (ggml-common.h, ggml-cuda/vecdotq.cuh,// ggml-cuda/dequantize.cuh; MIT, see third_party/ggml/LICENSE and VERSION.txt), so a weight means exactly what it@@ -43,6 +44,9 @@ struct NativeExpertLayout {size_t bytes = 0; // the whole blob};NativeExpertLayout native_expert_layout(int gu_type, int d_type, int64_t n_embd, int64_t n_ff);+/// Whether `native_expert_grouped` has kernels for this gate/up and down type pair at these dimensions, and the+/// prompt path's dequantizer takes both (checked for every layer at startup, before anything is allocated).+bool native_expert_supported(int gu_type, int d_type, int64_t n_embd, int64_t n_ff) noexcept;/// Bytes of scratch `native_expert_grouped` needs for `cap_entries` entries.size_t native_expert_scratch_bytes(int64_t cap_entries, int64_t n_ff);diff --git a/src/core/native_dense.cpp b/src/core/native_dense.cppindex ba3099897..6fa6896f9 100644--- a/src/core/native_dense.cpp+++ b/src/core/native_dense.cpp@@ -16,9 +16,10 @@ namespace {bool eligible(const strata::TensorInfo& tensor, bool include_ple_key) {const auto& name = tensor.name;if (name.rfind("blk.", 0) != 0) return false;- // Match the native PLE kernel: Q2_0, IQ3_XXS and IQ4_XS. Other keys retain the packed BF16 fallback.+ // Match the native PLE kernel: Q2_0, IQ3_XXS, IQ4_XS and Q8_0 (UD-Q4_K_XL). Other keys retain the packed BF16+ // fallback.if (name == "blk.1.ple_key.weight")- return include_ple_key && (tensor.type == 42 || tensor.type == 18 || tensor.type == 23);+ return include_ple_key && (tensor.type == 42 || tensor.type == 18 || tensor.type == 23 || tensor.type == 8);static const char* suffixes[] = {".attn_qkv.weight", ".attn_gate.weight", ".ssm_out.weight",".attn_q.weight", ".attn_k.weight", ".attn_v.weight", ".attn_output.weight",".ffn_gate_shexp.weight", ".ffn_up_shexp.weight", ".ffn_down_shexp.weight"};diff --git a/src/kernels/cuda/iq_kernels.cu b/src/kernels/cuda/iq_kernels.cuindex cb7ec0094..61ace84a1 100644--- a/src/kernels/cuda/iq_kernels.cu+++ b/src/kernels/cuda/iq_kernels.cu@@ -291,6 +291,158 @@ __device__ __forceinline__ float vec_dot_iq4_xs_q8_1(const void* __restrict__ vbreturn d * sumi;}+// ---------------------------------------------------------------- Unsloth's UD-Q4_K_XL experts+// 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_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,+ const half2& dm4, const float* __restrict__ d8) {+ float sumf_d = 0.0f;+ float sumf_m = 0.0f;+#pragma unroll+ for (int i = 0; i < QR4_K; ++i) {+ const int v0i = (v[0] >> (4 * i)) & 0x0F0F0F0F;+ const int v1i = (v[1] >> (4 * i)) & 0x0F0F0F0F;+ const int dot1 = ggml_cuda_dp4a(v1i, u[2 * i + 1], ggml_cuda_dp4a(v0i, u[2 * i + 0], 0));+ const int dot2 = ggml_cuda_dp4a(0x01010101, u[2 * i + 1], ggml_cuda_dp4a(0x01010101, u[2 * i + 0], 0));+ sumf_d += d8[i] * (dot1 * sc[i]);+ sumf_m += d8[i] * (dot2 * m[i]); // the min times the sum of the QUANTIZED activations+ }+ const float2 dm4f = __half22float2(dm4);+ return dm4f.x * sumf_d - dm4f.y * sumf_m;+}+__device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_vmmq(const int* __restrict__ vl, const int* __restrict__ vh,+ const int* __restrict__ u, const uint8_t* __restrict__ sc,+ const uint8_t* __restrict__ m, const half2& dm5,+ const float* __restrict__ d8) {+ float sumf_d = 0.0f;+ float sumf_m = 0.0f;+#pragma unroll+ for (int i = 0; i < QR5_K; ++i) {+ const int vl0i = (vl[0] >> (4 * i)) & 0x0F0F0F0F;+ const int vl1i = (vl[1] >> (4 * i)) & 0x0F0F0F0F;+ const int vh0i = ((vh[0] >> i) << 4) & 0x10101010;+ const int vh1i = ((vh[1] >> i) << 4) & 0x10101010;+ const int v0i = vl0i | vh0i;+ const int v1i = vl1i | vh1i;+ const int dot1 = ggml_cuda_dp4a(v0i, u[2 * i + 0], ggml_cuda_dp4a(v1i, u[2 * i + 1], 0));+ const int dot2 = ggml_cuda_dp4a(0x01010101, u[2 * i + 0], ggml_cuda_dp4a(0x01010101, u[2 * i + 1], 0));+ sumf_d += d8[i] * (dot1 * sc[i]);+ sumf_m += d8[i] * (dot2 * m[i]);+ }+ const float2 dm5f = __half22float2(dm5);+ return dm5f.x * sumf_d - dm5f.y * sumf_m;+}+// the 6-bit scales and mins of the 32-value group pair bq8_offset / 2, branchless (llama.cpp; shared by Q4_K, Q5_K)+__device__ __forceinline__ void k_scale_min(const uint8_t* scales8, int bq8_offset, uint16_t aux[2]) {+ const uint16_t* scales = (const uint16_t*) scales8;+ const int j = bq8_offset / 2;+ const int jm = j & 1;+ const uint32_t s0 = scales[jm + 0];+ const uint32_t s2 = scales[jm + 2];+ const uint32_t s4 = scales[jm + 4];+ const uint32_t hi = (uint32_t) -(int32_t) (j >= 2);+ aux[0] = (uint16_t) (((s0 & 0x3f3f) & ~hi) | ((((s4 >> 0) & 0x0f0f) | ((s0 & 0xc0c0) >> 2)) & hi));+ aux[1] = (uint16_t) (((s2 & 0x3f3f) & ~hi) | ((((s4 >> 4) & 0x0f0f) | ((s2 & 0xc0c0) >> 2)) & hi));+}+__device__ __forceinline__ float vec_dot_q4_K_q8_1(const void* __restrict__ vbq, const block_q8_1* __restrict__ bq8_1,+ const int& kbx, const int& iqs) {+ const block_q4_K* bq4_K = (const block_q4_K*) vbq + kbx;+ int v[2];+ int u[2 * QR4_K];+ float d8[QR4_K];+ const int bq8_offset = QR4_K * ((iqs / 2) / (QI8_1 / 2));+ const int* q4 = (const int*) (bq4_K->qs + 16 * bq8_offset + 4 * ((iqs / 2) % 4));+ v[0] = q4[0];+ v[1] = q4[4];+ uint16_t aux[2];+ k_scale_min(bq4_K->scales, bq8_offset, aux);+ const uint8_t* sc = (const uint8_t*) aux;+ const uint8_t* m = sc + 2;+#pragma unroll+ for (int i = 0; i < QR4_K; ++i) {+ const block_q8_1* bq8i = bq8_1 + bq8_offset + i;+ d8[i] = __low2float(bq8i->ds);+ const int* q8 = (const int*) bq8i->qs + ((iqs / 2) % 4);+ u[2 * i + 0] = q8[0];+ u[2 * i + 1] = q8[4];+ }+ return vec_dot_q4_K_q8_1_impl_vmmq(v, u, sc, m, bq4_K->dm, d8);+}+__device__ __forceinline__ float vec_dot_q5_K_q8_1(const void* __restrict__ vbq, const block_q8_1* __restrict__ bq8_1,+ const int& kbx, const int& iqs) {+ const block_q5_K* bq5_K = (const block_q5_K*) vbq + kbx;+ int vl[2];+ int vh[2];+ int u[2 * QR5_K];+ float d8[QR5_K];+ const int bq8_offset = QR5_K * ((iqs / 2) / (QI8_1 / 2));+ const int* ql = (const int*) (bq5_K->qs + 16 * bq8_offset + 4 * ((iqs / 2) % 4));+ const int* qh = (const int*) (bq5_K->qh + 4 * ((iqs / 2) % 4));+ vl[0] = ql[0];+ vl[1] = ql[4];+ vh[0] = qh[0] >> bq8_offset;+ vh[1] = qh[4] >> bq8_offset;+ uint16_t aux[2];+ k_scale_min(bq5_K->scales, bq8_offset, aux);+ const uint8_t* sc = (const uint8_t*) aux;+ const uint8_t* m = sc + 2;+#pragma unroll+ for (int i = 0; i < QR5_K; ++i) {+ const block_q8_1* bq8i = bq8_1 + bq8_offset + i;+ d8[i] = __low2float(bq8i->ds);+ const int* q8 = (const int*) bq8i->qs + ((iqs / 2) % 4);+ u[2 * i + 0] = q8[0];+ u[2 * i + 1] = q8[4];+ }+ return vec_dot_q5_K_q8_1_impl_vmmq(vl, vh, u, sc, m, bq5_K->dm, d8);+}+// Q5_1: llama.cpp's integer chain, but the min term multiplies the sum of the QUANTIZED activations (dp4a with+// 0x01010101, times d8) instead of the q8_1 block's `ds.y`, which our quantizer (like llama.cpp's) fills with the sum+// of the ORIGINAL activations. That is ggml-cpu's convention (its q8_1 `s` is d * sum(q)) and the one the K-quant+// mins above use; the scaled and the min term then see the same activation (eddoursul/Strata measured 1.1-1.2%+// against 1.9% relative error per expert). Result: sumi * (d5 * d8) + sumu * (m5 * 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;+ int sumi = 0, sumu = 0;+#pragma unroll+ for (int i = 0; i < VDR_Q5_1; ++i) {+ const int vl = get_int_b4(bq5_1->qs, iqs + i);+ const int vh = get_int_b4(bq5_1->qh, 0) >> (4 * (iqs + i));+ const int u0 = get_int_b4(bq8_1->qs, iqs + i), u1 = get_int_b4(bq8_1->qs, iqs + i + QI5_1);+ int vi0 = (vl >> 0) & 0x0F0F0F0F;+ vi0 |= (vh << 4) & 0x00000010;+ vi0 |= (vh << 11) & 0x00001000;+ vi0 |= (vh << 18) & 0x00100000;+ vi0 |= (vh << 25) & 0x10000000;+ sumi = ggml_cuda_dp4a(vi0, u0, sumi);+ int vi1 = (vl >> 4) & 0x0F0F0F0F;+ vi1 |= (vh >> 12) & 0x00000010;+ vi1 |= (vh >> 5) & 0x00001000;+ vi1 |= (vh << 2) & 0x00100000;+ vi1 |= (vh << 9) & 0x10000000;+ sumi = ggml_cuda_dp4a(vi1, u1, sumi);+ sumu = ggml_cuda_dp4a(0x01010101, u1, ggml_cuda_dp4a(0x01010101, u0, sumu));+ }+ const float2 dm5 = __half22float2(bq5_1->dm);+ const float d8 = __low2float(bq8_1->ds);+ return sumi * (dm5.x * d8) + sumu * (dm5.y * d8);+}+__device__ __forceinline__ float vec_dot_q8_0_q8_1(const void* __restrict__ vbq, const block_q8_1* __restrict__ bq8_1,+ const int& kbx, const int& iqs) {+ const block_q8_0* bq8_0 = (const block_q8_0*) vbq + kbx;+ int sumi = 0;+#pragma unroll+ for (int i = 0; i < VDR_Q8_0; ++i)+ sumi = ggml_cuda_dp4a(get_int_b2(bq8_0->qs, iqs + i), get_int_b4(bq8_1->qs, iqs + i), sumi);+ const float d8_0 = __half2float(bq8_0->d), d8_1 = __low2float(bq8_1->ds);+ return d8_0 * d8_1 * ((float) sumi);+}+// ---------------------------------------------------------------- the formats// qk = values per block, ipb = dot calls per block (qi / vdr), step = the iqs stride between calls.template<int TY> struct Fmt;@@ -312,6 +464,20 @@ template<> struct Fmt<29> { static constexpr int qk = 256, ipb = 8, step = 1;__device__ static float dot(const void* v, const block_q8_1* y, int kbx, int iqs) { return vec_dot_iq1_m_q8_1(v, y, kbx, iqs); } };template<> struct Fmt<42> { static constexpr int qk = 64, ipb = 2, step = 1;__device__ static float dot(const void* v, const block_q8_1* y, int kbx, int iqs) { return vec_dot_q2_0_q8_1(v, y, kbx, iqs); } };+template<> struct Fmt<12> { static constexpr int qk = 256, ipb = QI4_K / VDR_Q4_K, step = VDR_Q4_K;+ __device__ static float dot(const void* v, const block_q8_1* y, int kbx, int iqs) { return vec_dot_q4_K_q8_1(v, y, kbx, iqs); } };+template<> struct Fmt<13> { static constexpr int qk = 256, ipb = QI5_K / VDR_Q5_K, step = VDR_Q5_K;+ __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<8> { static constexpr int qk = 32, ipb = QI8_0 / VDR_Q8_0, step = VDR_Q8_0;+ __device__ static float dot(const void* v, const block_q8_1* y, int kbx, int iqs) { return vec_dot_q8_0_q8_1(v, y, kbx, iqs); } };++// 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(8)+#define STRATA_D_FMTS(X) X(20) X(23) X(42) X(7) 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(8)__device__ __forceinline__ float warp_sum(float v) {#pragma unroll@@ -355,6 +521,11 @@ __global__ void __launch_bounds__(128) mmvq_kernel(const uint8_t* __restrict__ w// order on the same values, so a column of the kernels below is BITWISE equal to the same column of mmvq_kernel /// native_gu_kernel / native_down_kernel (iq_multi_parity checks it; STRATA_OLD_IQ_MMVQ=1 keeps the old kernels).template<int TY> struct Split;+// Formats with a Split below take the decode-once kernels; the others (Q4_K, Q5_K, Q5_1, Q8_0: UD-Q4_K_XL) the+// per-entry ones, which call Fmt<TY>::dot per column exactly as before #242 (the launchers test kSplit at compile+// time, so the multi kernels are never instantiated for a type without a Split).+template<int TY> inline constexpr bool kSplit = false;+template<> inline constexpr bool kSplit<16> = true;template<> struct Split<16> { // IQ2_XXSstruct W { int g[8]; int ls; float dw; };__device__ static W load(const void* __restrict__ vbq, int kbx, int iqs) {@@ -399,6 +570,7 @@ struct SplitLs2 {return d * sumi;}};+template<> inline constexpr bool kSplit<17> = true;template<> struct Split<17> : SplitLs2 { // IQ2_XS__device__ static W load(const void* __restrict__ vbq, int kbx, int iqs) {const block_iq2_xs* bq2 = (const block_iq2_xs*) vbq + kbx;@@ -420,6 +592,7 @@ template<> struct Split<17> : SplitLs2 { // IQ2_XSreturn r;}};+template<> inline constexpr bool kSplit<22> = true;template<> struct Split<22> : SplitLs2 { // IQ2_S__device__ static W load(const void* __restrict__ vbq, int kbx, int iqs) {const block_iq2_s* bq2 = (const block_iq2_s*) vbq + kbx;@@ -443,6 +616,7 @@ template<> struct Split<22> : SplitLs2 { // IQ2_Sreturn r;}};+template<> inline constexpr bool kSplit<18> = true;template<> struct Split<18> { // IQ3_XXSstruct W { int g[8]; int ls; float dw; };__device__ static W load(const void* __restrict__ vbq, int kbx, int iqs) {@@ -473,6 +647,7 @@ template<> struct Split<18> { // IQ3_XXSreturn d * sumi;}};+template<> inline constexpr bool kSplit<21> = true;template<> struct Split<21> { // IQ3_Sstruct W { int g[8]; int ls; float dw; };__device__ static W load(const void* __restrict__ vbq, int kbx, int iqs) {@@ -505,6 +680,7 @@ template<> struct Split<21> { // IQ3_Sreturn d * sumi;}};+template<> inline constexpr bool kSplit<29> = true;template<> struct Split<29> { // IQ1_Mstruct W { int g[8]; float delta[4]; int sc0, sc1; float dw; };__device__ static W load(const void* __restrict__ vbq, int kbx, int iqs) {@@ -547,6 +723,7 @@ template<> struct Split<29> { // IQ1_Mreturn d * ((sumi[0] + sumf[0]) * r.sc0 + (sumi[1] + sumf[1]) * r.sc1);}};+template<> inline constexpr bool kSplit<20> = true;template<> struct Split<20> { // IQ4_NLstruct W { int2 v[2]; float dw; };__device__ static W load(const void* __restrict__ vbq, int kbx, int iqs) {@@ -569,6 +746,7 @@ template<> struct Split<20> { // IQ4_NLreturn d * sumi;}};+template<> inline constexpr bool kSplit<23> = true;template<> struct Split<23> { // IQ4_XSstruct W { int2 v[4]; int ls; float dw; };__device__ static W load(const void* __restrict__ vbq, int kbx, int iqs) {@@ -594,6 +772,7 @@ template<> struct Split<23> { // IQ4_XSreturn d * sumi;}};+template<> inline constexpr bool kSplit<42> = true;template<> struct Split<42> { // Q2_0struct W { int qx[4], qy[4]; float d2; };__device__ static W load(const void* __restrict__ vbq, int kbx, int iqs) {@@ -962,6 +1141,84 @@ __device__ void dq_q2_0(const void* vx, int64_t ibs, dst_t* yy, int tid) {}}+// llama.cpp's dequantize_q4_K / dequantize_q5_K (dequantize.cuh; q5_K's 64 threads folded onto 32) and the 32-value+// blocks of Q5_1 / Q8_0, 8 of them per 256-value "superblock" (thread tid writes 8 values of block tid % 8).+__device__ __forceinline__ void get_scale_min_k4(int j, const uint8_t* q, uint8_t& d, uint8_t& m) {+ if (j < 4) {+ d = q[j] & 63; m = q[j + 4] & 63;+ } else {+ d = (q[j + 4] & 0xF) | ((q[j - 4] >> 6) << 4);+ m = (q[j + 4] >> 4) | ((q[j - 0] >> 6) << 4);+ }+}+template<typename dst_t>+__device__ void dq_q4_k(const void* vx, int64_t ibs, dst_t* yy, int tid) {+ const block_q4_K* x = (const block_q4_K*) vx;+ const int64_t il = tid / 8, ir = tid % 8, is = 2 * il;+ const int n = 4;+ dst_t* y = yy + 64 * il + n * ir;+ const float dall = __low2half(x[ibs].dm);+ const float dmin = __high2half(x[ibs].dm);+ const uint8_t* q = x[ibs].qs + 32 * il + n * ir;+ uint8_t sc, m;+ get_scale_min_k4((int) is + 0, x[ibs].scales, sc, m);+ const float d1 = dall * sc, m1 = dmin * m;+ get_scale_min_k4((int) is + 1, x[ibs].scales, sc, m);+ const float d2 = dall * sc, m2 = dmin * m;+ for (int l = 0; l < n; ++l) {+ y[l + 0] = cvt<dst_t>(d1 * (q[l] & 0xF) - m1);+ y[l + 32] = cvt<dst_t>(d2 * (q[l] >> 4) - m2);+ }+}+template<typename dst_t>+__device__ void dq_q5_k(const void* vx, int64_t ibs, dst_t* yy, int tid) {+ const block_q5_K* x = (const block_q5_K*) vx;+ for (int tt = tid; tt < 64; tt += 32) {+ const int il = tt / 16, ir = tt % 16, is = 2 * il;+ dst_t* y = yy + 64 * il + 2 * ir;+ const float dall = __low2half(x[ibs].dm);+ const float dmin = __high2half(x[ibs].dm);+ const uint8_t* ql = x[ibs].qs + 32 * il + 2 * ir;+ const uint8_t* qh = x[ibs].qh + 2 * ir;+ uint8_t sc, m;+ get_scale_min_k4(is + 0, x[ibs].scales, sc, m);+ const float d1 = dall * sc, m1 = dmin * m;+ get_scale_min_k4(is + 1, x[ibs].scales, sc, m);+ const float d2 = dall * sc, m2 = dmin * m;+ uint8_t hm = (uint8_t) (1 << (2 * il));+ y[0] = cvt<dst_t>(d1 * ((ql[0] & 0xF) + (qh[0] & hm ? 16 : 0)) - m1);+ y[1] = cvt<dst_t>(d1 * ((ql[1] & 0xF) + (qh[1] & hm ? 16 : 0)) - m1);+ hm <<= 1;+ y[32] = cvt<dst_t>(d2 * ((ql[0] >> 4) + (qh[0] & hm ? 16 : 0)) - m2);+ y[33] = cvt<dst_t>(d2 * ((ql[1] >> 4) + (qh[1] & hm ? 16 : 0)) - m2);+ }+}+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;+ const float2 dm = __half22float2(x[ib].dm);+ uint32_t qh;+ memcpy(&qh, x[ib].qh, sizeof(qh));+ dst_t* y = yy + 32 * ib;+ for (int j = 0; j < 4; ++j) {+ const int iqs = 4 * il + j; // llama.cpp's dequantize_q5_1 for value pairs iqs, iqs + 16+ const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;+ const int xh_1 = ((qh >> (iqs + 12))) & 0x10;+ y[iqs] = cvt<dst_t>((float) ((x[ib].qs[iqs] & 0xf) | xh_0) * dm.x + dm.y);+ y[iqs + 16] = cvt<dst_t>((float) ((x[ib].qs[iqs] >> 4) | xh_1) * dm.x + dm.y);+ }+}+template<typename dst_t>+__device__ void dq_q8_0(const void* vx, int64_t ibs, dst_t* yy, int tid) {+ const block_q8_0* x = (const block_q8_0*) vx + ibs * (QK_K / QK8_0);+ const int ib = tid % 8, il = tid / 8;+ const float d = __half2float(x[ib].d);+ dst_t* y = yy + 32 * ib + 8 * il;+ for (int j = 0; j < 8; ++j) y[j] = cvt<dst_t>((float) x[ib].qs[8 * il + j] * d);+}++// Every type below must also be in is_iq(): the host entry points refuse the others, so the default is unreachable.template<typename dst_t>__device__ __forceinline__ void dq_dispatch(int ty, const void* vx, int64_t ibs, dst_t* y, int tid) {switch (ty) {@@ -975,6 +1232,10 @@ __device__ __forceinline__ void dq_dispatch(int ty, const void* vx, int64_t ibs,case 23: dq_iq4_xs(vx, ibs, y, tid); break;case 11: dq_q3_k(vx, ibs, y, tid); break;case 42: dq_q2_0(vx, ibs, y, tid); break;+ case 12: dq_q4_k(vx, ibs, y, tid); break;+ case 13: dq_q5_k(vx, ibs, y, tid); break;+ case 7: dq_q5_1(vx, ibs, y, tid); break;+ case 8: dq_q8_0(vx, ibs, y, tid); break;default: break;}}@@ -994,7 +1255,28 @@ __global__ void dequant_gu_kernel(int ty, const void* __restrict__ gate, const vdq_dispatch<__half>(ty, parity ? up : gate, i, y + ((2 * r + parity) * per_row + c) * QK_K, threadIdx.x);}-bool 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; }+// the types dq_dispatch dequantizes+bool 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 == 8;+}+// values per block of the types the grouped expert kernels take (0 = none)+int gu_qk(int t) {+ switch (t) {+#define STRATA_QK(T) case T: return Fmt<T>::qk;+ STRATA_GU_FMTS(STRATA_QK)+#undef STRATA_QK+ default: return 0;+ }+}+int d_qk(int t) {+ switch (t) {+#define STRATA_QK(T) case T: return Fmt<T>::qk;+ STRATA_D_FMTS(STRATA_QK)+#undef STRATA_QK+ default: return 0;+ }+}bool env_on(const char* name) {const char* v = std::getenv(name);@@ -1007,7 +1289,8 @@ template<int TY>void launch_mmvq(const uint8_t* W, size_t rb, const block_q8_1* X, float* y, int n_in, int n_out, int ncols,cudaStream_t s) {const dim3 grid((unsigned) ((n_out + 3) / 4)), block(32, 4);- if (g_old_kernels) mmvq_kernel<TY><<<grid, block, 0, s>>>(W, rb, X, y, n_in, n_out, ncols);+ if constexpr (!kSplit<TY>) mmvq_kernel<TY><<<grid, block, 0, s>>>(W, rb, X, y, n_in, n_out, ncols);+ else if (g_old_kernels) mmvq_kernel<TY><<<grid, block, 0, s>>>(W, rb, X, y, n_in, n_out, ncols);else if (ncols <= 1) mmvq_multi_kernel<TY, 1><<<grid, block, 0, s>>>(W, rb, X, y, n_in, n_out, ncols);else if (ncols == 2) mmvq_multi_kernel<TY, 2><<<grid, block, 0, s>>>(W, rb, X, y, n_in, n_out, ncols);else if (ncols <= 4) mmvq_multi_kernel<TY, 4><<<grid, block, 0, s>>>(W, rb, X, y, n_in, n_out, ncols);@@ -1018,7 +1301,8 @@ template<int TG>void launch_gu(dim3 grid, cudaStream_t s, const unsigned long long* grp_ptr, const int32_t* grp_start,const int32_t* n_groups, const int32_t* ent_tok, const block_q8_1* X, const NativeExpertLayout& L,float* gate, float* up) {- if (g_old_kernels) native_gu_kernel<TG><<<grid, 256, 0, s>>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up);+ if constexpr (!kSplit<TG>) native_gu_kernel<TG><<<grid, 256, 0, s>>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up);+ else if (g_old_kernels) native_gu_kernel<TG><<<grid, 256, 0, s>>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up);else native_gu_multi_kernel<TG><<<grid, 256, 0, s>>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up);}@@ -1026,7 +1310,8 @@ template<int TD>void launch_down(dim3 grid, cudaStream_t s, const unsigned long long* grp_ptr, const int32_t* grp_start,const int32_t* n_groups, const int32_t* ent_dst, const block_q8_1* hq, const NativeExpertLayout& L,float* out) {- if (g_old_kernels) native_down_kernel<TD><<<grid, 256, 0, s>>>(grp_ptr, grp_start, n_groups, ent_dst, hq, L, out);+ if constexpr (!kSplit<TD>) native_down_kernel<TD><<<grid, 256, 0, s>>>(grp_ptr, grp_start, n_groups, ent_dst, hq, L, out);+ else if (g_old_kernels) native_down_kernel<TD><<<grid, 256, 0, s>>>(grp_ptr, grp_start, n_groups, ent_dst, hq, L, out);else native_down_multi_kernel<TD><<<grid, 256, 0, s>>>(grp_ptr, grp_start, n_groups, ent_dst, hq, L, out);}@@ -1049,6 +1334,10 @@ size_t iq_row_bytes(int t, int64_t n) noexcept {case 23: return (size_t) (n / 256) * sizeof(block_iq4_xs);case 11: return (size_t) (n / 256) * sizeof(block_q3_K);case 42: return (size_t) (n / 64) * sizeof(block_q2_0);+ case 12: return (size_t) (n / 256) * sizeof(block_q4_K);+ case 13: return (size_t) (n / 256) * sizeof(block_q5_K);+ case 7: return (size_t) (n / 32) * sizeof(block_q5_1);+ case 8: return (size_t) (n / 32) * sizeof(block_q8_0);default: return 0;}}@@ -1066,15 +1355,9 @@ void iq_mmvq(int t, const void* w, const void* x_q8_1, float* y, int n_in, int nconst auto* W = (const uint8_t*) w;const auto* X = (const block_q8_1*) x_q8_1;switch (t) {- case 16: launch_mmvq<16>(W, rb, X, y, n_in, n_out, ncols, s); break;- case 17: launch_mmvq<17>(W, rb, X, y, n_in, n_out, ncols, s); break;- case 18: launch_mmvq<18>(W, rb, X, y, n_in, n_out, ncols, s); break;- case 20: launch_mmvq<20>(W, rb, X, y, n_in, n_out, ncols, s); break;- case 21: launch_mmvq<21>(W, rb, X, y, n_in, n_out, ncols, s); break;- case 22: launch_mmvq<22>(W, rb, X, y, n_in, n_out, ncols, s); break;- case 23: launch_mmvq<23>(W, rb, X, y, n_in, n_out, ncols, s); break;- case 29: launch_mmvq<29>(W, rb, X, y, n_in, n_out, ncols, s); break;- case 42: launch_mmvq<42>(W, rb, X, y, n_in, n_out, ncols, s); break;+#define STRATA_MMVQ(T) case T: launch_mmvq<T>(W, rb, X, y, n_in, n_out, ncols, s); break;+ STRATA_MMVQ_FMTS(STRATA_MMVQ)+#undef STRATA_MMVQdefault: std::fprintf(stderr, "iq_mmvq: type %d is not supported\n", t); std::exit(1);}check("iq_mmvq");@@ -1112,12 +1395,20 @@ void iq_dequant_f32(int t, const void* src, int64_t n, float* dst, void* stream)}void iq_dequant_gu_f16(int t, const void* gate, const void* up, int64_t n_ff, int64_t n_embd, uint16_t* dst, void* stream) {+ // checked like the other entry points: an unknown type used to leave `dst` unwritten, a wrong prompt and no error+ if (n_embd % 256 != 0 || !is_iq(t)) { std::fprintf(stderr, "iq_dequant_gu_f16: type %d / %lld\n", t, (long long) n_embd); std::exit(1); }const int64_t per_row = n_embd / 256;dequant_gu_kernel<<<dim3((unsigned) (n_ff * per_row), 2), 32, 0, (cudaStream_t) stream>>>(t, gate, up, per_row,(__half*) dst);check("iq_dequant_gu_f16");}+bool native_expert_supported(int gu_type, int d_type, int64_t n_embd, int64_t n_ff) noexcept {+ const int qg = gu_qk(gu_type), qd = d_qk(d_type);+ return qg > 0 && qd > 0 && is_iq(gu_type) && is_iq(d_type) && n_embd % qg == 0 && n_ff % qd == 0 &&+ n_embd % 256 == 0 && (n_ff * n_embd) % 256 == 0;+}+NativeExpertLayout native_expert_layout(int gu_type, int d_type, int64_t n_embd, int64_t n_ff) {NativeExpertLayout L;L.gu_type = gu_type;@@ -1150,14 +1441,9 @@ void native_expert_grouped(const NativeExpertLayout& L, const unsigned long longconst auto* X = (const block_q8_1*) x_q8_1;const dim3 ggu((unsigned) ((2 * L.n_ff + GU_ROWS - 1) / GU_ROWS), (unsigned) cap_groups);switch (L.gu_type) {- case 16: launch_gu<16>(ggu, s, grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up); break;- case 17: launch_gu<17>(ggu, s, grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up); break;- case 18: launch_gu<18>(ggu, s, grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up); break;- case 21: launch_gu<21>(ggu, s, grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up); break;- case 22: launch_gu<22>(ggu, s, grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up); break;- case 23: launch_gu<23>(ggu, s, grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up); break;- case 29: launch_gu<29>(ggu, s, grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up); break;- case 42: launch_gu<42>(ggu, s, grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up); break;+#define STRATA_GU(T) case T: launch_gu<T>(ggu, s, grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up); break;+ STRATA_GU_FMTS(STRATA_GU)+#undef STRATA_GUdefault: std::fprintf(stderr, "native_expert_grouped: gate/up type %d\n", L.gu_type); std::exit(1);}check("native_expert_grouped/gu");@@ -1166,9 +1452,9 @@ void native_expert_grouped(const NativeExpertLayout& L, const unsigned long longquantize_q8_1_kernel<<<(unsigned) ((nh + 255) / 256), 256, 0, s>>>(h, hq, nh);const dim3 gd((unsigned) ((L.n_embd + 7) / 8), (unsigned) cap_groups);switch (L.d_type) {- case 20: launch_down<20>(gd, s, grp_ptr, grp_start, n_groups, ent_dst, hq, L, out); break;- case 23: launch_down<23>(gd, s, grp_ptr, grp_start, n_groups, ent_dst, hq, L, out); break;- case 42: launch_down<42>(gd, s, grp_ptr, grp_start, n_groups, ent_dst, hq, L, out); break;+#define STRATA_DOWN(T) case T: launch_down<T>(gd, s, grp_ptr, grp_start, n_groups, ent_dst, hq, L, out); break;+ STRATA_D_FMTS(STRATA_DOWN)+#undef STRATA_DOWNdefault: std::fprintf(stderr, "native_expert_grouped: down type %d\n", L.d_type); std::exit(1);}check("native_expert_grouped/down");diff --git a/src/kernels/cuda/ple.cu b/src/kernels/cuda/ple.cuindex 66bb42206..6585bbaab 100644--- a/src/kernels/cuda/ple.cu+++ b/src/kernels/cuda/ple.cu@@ -229,8 +229,9 @@ void ple_block(const float* emb, const float* hidden, const float* hist_rows, coconst bool native_key = w.key_native_data != nullptr && w.key_bf16 == nullptr;if (native_key && (!emb || !hidden || !hist_rows || !out.result || !scratch || !stream ||!w.key_native_q8_1 ||- (w.key_native_type != 42 && w.key_native_type != 18 && w.key_native_type != 23)))- throw std::invalid_argument("ple_block: native key requires Q2_0, IQ3_XXS or IQ4_XS weights, input/output, private scratch and explicit stream");+ (w.key_native_type != 42 && w.key_native_type != 18 && w.key_native_type != 23 &&+ w.key_native_type != 8)))+ throw std::invalid_argument("ple_block: native key requires Q2_0, IQ3_XXS, IQ4_XS or Q8_0 weights, input/output, private scratch and explicit stream");if (emb == nullptr || hidden == nullptr || hist_rows == nullptr || out.result == nullptr) return;const int n_embd = NG_N_EMBD, hc = NG_HC, hc_dim = NG_HC_DIM;static_assert(NG_N_EMBD == 2560 && NG_HC_DIM == 10240, "native PLE key geometry changed");diff --git a/src/kernels/native_expert_parity.cpp b/src/kernels/native_expert_parity.cppindex 2faa9c2d5..d6ede3183 100644--- a/src/kernels/native_expert_parity.cpp+++ b/src/kernels/native_expert_parity.cpp@@ -1,11 +1,15 @@-// src/kernels/native_expert_parity.cpp - plan v0.3 P6: one native expert three ways, on real GGUF rows.+// src/kernels/native_expert_parity.cpp - plan v0.3 P6: one native expert three ways.//-// build/native_expert_parity <shard1.gguf> [layer ...]+// build/native_expert_parity <shard.gguf> [layer ...] real rows (a split model's shards are found by name)+// build/native_expert_parity --synthetic GU/DOWN ... random weights quantized by ggml (e.g. q4_K/q5_1)+// build/native_expert_parity --q5_1-min the Q5_1 min term on crafted activations//// (a) float reference: ggml's own dequantizer (`to_float`) and a float SwiGLU expert, (b) the CPU path// (ggml-cpu vec_dot with its quantized activations), (c) the GPU path (`native_expert_grouped`, q8_1// activations). (b) and (c) each differ from (a) by their activation rounding only (a few 1e-3 relative). (c) runs// twice, with the kernels that decode a weight part once for all entries and with the per-entry ones: bitwise equal.+// (d) the GPU dequantizers of the prompt path and the embedding against `to_float` (Q8_0: bit for bit).+// The synthetic mode and (d) follow eddoursul/Strata 8029fa9.#include "strata/artifact/gguf_reader.hpp"#include "strata/kernels/cpu/native_expert.hpp"#include "strata/kernels/cpu/expert.hpp"@@ -19,6 +23,8 @@#include <cuda_runtime.h>+#include <algorithm>+#include <cctype>#include <chrono>#include <cmath>#include <cstdio>@@ -36,268 +42,450 @@ static double rel(const std::vector<float>& a, const std::vector<float>& b) {return n / (d + 1e-30);}-int main(int argc, char** argv) {- setvbuf(stdout, nullptr, _IONBF, 0); // keep the trail on a crash- if (argc < 2) { std::fprintf(stderr, "usage: native_expert_parity <shard1.gguf> [layer ...]\n"); return 2; }- // #152's width check tests the opt-in rule (the multi-token kernels from one token on)- if (std::getenv("STRATA_IQ_MT_MIN") == nullptr) {-#ifdef _WIN32- _putenv_s("STRATA_IQ_MT_MIN", "1");-#else- setenv("STRATA_IQ_MT_MIN", "1", 1);-#endif- }- strata::GgufFile gguf(argv[1]);- std::vector<int> layers;- for (int i = 2; i < argc; ++i) layers.push_back(std::atoi(argv[i]));- if (layers.empty()) layers = {0, 1, 2, 3, 20, 47};- const int NT = 3, E = 7;- const int64_t H = 2560, FF = 640;++namespace {+constexpr int NT = 3, E = 7;+constexpr int64_t H = 2560, FF = 640;++// The three ways and the dequantizers for one expert blob; returns the number of failed checks.+int check_blob(const cpu::NativeFmt& f, const std::vector<uint8_t>& blob, int seed, const std::string& label,+ cudaStream_t s) {int failures = 0;- cudaStream_t s;- cudaStreamCreate(&s);- for (int l : layers) {- const strata::TensorInfo* t[3] = {};- const char* roles[3] = {"gate", "up", "down"};- for (const auto& ti : gguf.tensors())- for (int r = 0; r < 3; ++r)- if (ti.name == "blk." + std::to_string(l) + ".ffn_" + roles[r] + "_exps.weight") t[r] = &ti;- if (!t[0] || !t[1] || !t[2]) { std::printf("layer %d: no expert tensors\n", l); ++failures; continue; }- cpu::NativeFmt f;- std::string err;- if (!cpu::native_fmt((int) t[0]->type, (int) t[2]->type, H, FF, f, err)) {- std::printf("layer %d: %s\n", l, err.c_str()); ++failures; continue;- }- std::vector<uint8_t> blob(f.bytes);- std::memcpy(blob.data(), gguf.tensor_data(*t[0]) + (size_t) E * f.up_off, f.up_off);- std::memcpy(blob.data() + f.up_off, gguf.tensor_data(*t[1]) + (size_t) E * f.up_off, f.up_off);- std::memcpy(blob.data() + f.down_off, gguf.tensor_data(*t[2]) + (size_t) E * (f.bytes - f.down_off),- f.bytes - f.down_off);- // (a) the float reference- const auto* tg = ggml_get_type_traits((ggml_type) f.gu_type);- const auto* td = ggml_get_type_traits((ggml_type) f.d_type);- std::vector<float> G((size_t) FF * H), U((size_t) FF * H), D((size_t) H * FF);+ // (a) the float reference+ const auto* tg = ggml_get_type_traits((ggml_type) f.gu_type);+ const auto* td = ggml_get_type_traits((ggml_type) f.d_type);+ std::vector<float> G((size_t) FF * H), U((size_t) FF * H), D((size_t) H * FF);+ for (int64_t r = 0; r < FF; ++r) {+ tg->to_float(blob.data() + r * f.gu_row, G.data() + r * H, H);+ tg->to_float(blob.data() + f.up_off + r * f.gu_row, U.data() + r * H, H);+ }+ for (int64_t r = 0; r < H; ++r) td->to_float(blob.data() + f.down_off + r * f.d_row, D.data() + r * FF, FF);+ std::mt19937 rng(11 + seed);+ std::normal_distribution<float> nd(0.f, 1.f);+ std::vector<float> x((size_t) NT * H);+ for (auto& v : x) v = nd(rng);+ std::vector<float> ref((size_t) NT * H), got_c((size_t) NT * H), got_g((size_t) NT * H);+ for (int k = 0; k < NT; ++k) {+ std::vector<float> h(FF);for (int64_t r = 0; r < FF; ++r) {- tg->to_float(blob.data() + r * f.gu_row, G.data() + r * H, H);- tg->to_float(blob.data() + f.up_off + r * f.gu_row, U.data() + r * H, H);+ double g = 0, u = 0;+ for (int64_t i = 0; i < H; ++i) { g += (double) G[r * H + i] * x[k * H + i]; u += (double) U[r * H + i] * x[k * H + i]; }+ h[r] = (float) (g / (1.0 + std::exp(-g)) * u);}- for (int64_t r = 0; r < H; ++r) td->to_float(blob.data() + f.down_off + r * f.d_row, D.data() + r * FF, FF);- std::mt19937 rng(11 + l);- std::normal_distribution<float> nd(0.f, 1.f);- std::vector<float> x((size_t) NT * H);- for (auto& v : x) v = nd(rng);- std::vector<float> ref((size_t) NT * H), got_c((size_t) NT * H), got_g((size_t) NT * H);+ for (int64_t r = 0; r < H; ++r) {+ double o = 0;+ for (int64_t i = 0; i < FF; ++i) o += (double) D[r * FF + i] * h[i];+ ref[k * H + r] = (float) o;+ }+ }+ // (b) the CPU+ {+ std::vector<std::vector<uint8_t>> act(NT, std::vector<uint8_t>(cpu::kNativeActBytes));+ std::vector<std::vector<uint8_t>> hq(NT, std::vector<uint8_t>(cpu::kNativeHBytes));+ std::vector<std::vector<float>> ff(NT, std::vector<float>(FF));+ const void* a[NT];+ float* ffp[NT];+ const void* hp[NT];+ float* op[NT];for (int k = 0; k < NT; ++k) {- std::vector<float> h(FF);- for (int64_t r = 0; r < FF; ++r) {- double g = 0, u = 0;- for (int64_t i = 0; i < H; ++i) { g += (double) G[r * H + i] * x[k * H + i]; u += (double) U[r * H + i] * x[k * H + i]; }- h[r] = (float) (g / (1.0 + std::exp(-g)) * u);- }- for (int64_t r = 0; r < H; ++r) {- double o = 0;- for (int64_t i = 0; i < FF; ++i) o += (double) D[r * FF + i] * h[i];- ref[k * H + r] = (float) o;- }+ cpu::native_quant_act(f, x.data() + k * H, act[k].data());+ a[k] = act[k].data();+ ffp[k] = ff[k].data();}- // (b) the CPU- {- std::vector<std::vector<uint8_t>> act(NT, std::vector<uint8_t>(cpu::kNativeActBytes));- std::vector<std::vector<uint8_t>> hq(NT, std::vector<uint8_t>(cpu::kNativeHBytes));- std::vector<std::vector<float>> ff(NT, std::vector<float>(FF));- const void* a[NT];- float* ffp[NT];- const void* hp[NT];- float* op[NT];- for (int k = 0; k < NT; ++k) {- cpu::native_quant_act(f, x.data() + k * H, act[k].data());- a[k] = act[k].data();- ffp[k] = ff[k].data();- }- cpu::native_gu_rows(f, blob.data(), a, NT, ffp, 0, (int) FF);- if (cpu::iq512_supported(f.gu_type)) {- // ggml's own vec_dot, same Q8_K activations: the reference for both multi-token kernels- // (float-order differences only)- const auto* tc = ggml_get_type_traits_cpu((ggml_type) f.gu_type);- std::vector<float> gref((size_t) NT * FF);- for (int k = 0; k < NT; ++k)- for (int64_t r = 0; r < FF; ++r)- tc->vec_dot((int) H, &gref[k * FF + r], 0, blob.data() + r * f.gu_row, 0, a[k], 0, 1);- double usg = 0.0;- { // ggml single-token throughput, the weights cache-resident (compute bound)- const int it = 50;- auto t0 = std::chrono::steady_clock::now();- for (int i = 0; i < it; ++i)- for (int64_t r = 0; r < FF; ++r) {- float gg, uu;- tc->vec_dot((int) H, &gg, 0, blob.data() + r * f.gu_row, 0, a[0], 0, 1);- tc->vec_dot((int) H, &uu, 0, blob.data() + f.up_off + r * f.gu_row, 0, a[0], 0, 1);- }- auto t1 = std::chrono::steady_clock::now();- usg = std::chrono::duration<double, std::micro>(t1 - t0).count() / it;- }- // one multi-token kernel: parity against ggml's vec_dot, then one-token and NT-token throughput- auto check = [&](const char* tag, bool is512) {- std::vector<float> g((size_t) NT * FF);- float* gp[NT];- for (int k = 0; k < NT; ++k) gp[k] = g.data() + k * FF;- (is512 ? cpu::iq512_rows : cpu::iq256_rows)(f.gu_type, blob.data(), f.gu_row, (int) H, a, NT, gp, 0, (int) FF);- const double rg = rel(g, gref);- std::printf(" %s %s gate rows vs ggml vec_dot: rel %.2e\n",- ggml_type_name((ggml_type) f.gu_type), tag, rg);- if (rg > 1e-5) { // ggml's own vec_dot is the reference: above 1e-5 it is a real mismatch,- std::printf(" %s %s gate rows MISMATCH (rel %.2e > 1e-5)\n",- ggml_type_name((ggml_type) f.gu_type), tag, rg); // not rounding (measured 3e-8)- ++failures;+ cpu::native_gu_rows(f, blob.data(), a, NT, ffp, 0, (int) FF);+ if (cpu::iq512_supported(f.gu_type)) {+ // ggml's own vec_dot, same Q8_K activations: the reference for both multi-token kernels+ // (float-order differences only)+ const auto* tc = ggml_get_type_traits_cpu((ggml_type) f.gu_type);+ std::vector<float> gref((size_t) NT * FF);+ for (int k = 0; k < NT; ++k)+ for (int64_t r = 0; r < FF; ++r)+ tc->vec_dot((int) H, &gref[k * FF + r], 0, blob.data() + r * f.gu_row, 0, a[k], 0, 1);+ double usg = 0.0;+ { // ggml single-token throughput, the weights cache-resident (compute bound)+ const int it = 50;+ auto t0 = std::chrono::steady_clock::now();+ for (int i = 0; i < it; ++i)+ for (int64_t r = 0; r < FF; ++r) {+ float gg, uu;+ tc->vec_dot((int) H, &gg, 0, blob.data() + r * f.gu_row, 0, a[0], 0, 1);+ tc->vec_dot((int) H, &uu, 0, blob.data() + f.up_off + r * f.gu_row, 0, a[0], 0, 1);}- float* f1[1] = {ffp[0]};- const int it = 50;- const auto gu = is512 ? cpu::iq512_gu_rows : cpu::iq256_gu_rows;- auto t0 = std::chrono::steady_clock::now();- for (int i = 0; i < it; ++i) gu(f.gu_type, blob.data(), f.gu_row, f.up_off, (int) H, a, 1, f1, 0, (int) FF);- auto t1 = std::chrono::steady_clock::now();- for (int i = 0; i < it; ++i) gu(f.gu_type, blob.data(), f.gu_row, f.up_off, (int) H, a, NT, ffp, 0, (int) FF);- auto t2 = std::chrono::steady_clock::now();- const double us1 = std::chrono::duration<double, std::micro>(t1 - t0).count() / it;- const double usn = std::chrono::duration<double, std::micro>(t2 - t1).count() / it;- std::printf(" gate+up one token one thread: %s %.0f us (%.2f GB/s), ggml %.0f us (%.2f GB/s)\n",- tag, us1, 2.0 * f.up_off / us1 / 1e3, usg, 2.0 * f.up_off / usg / 1e3);- std::printf(" gate+up %d tokens one thread: %s %.0f us vs ggml %d x %.0f us\n", NT, tag, usn, NT, usg);- };- if (cpu::cpu_avx512_ok()) check("avx512", true); // guarded: the binary runs on AVX-2 CPUs too- check("avx2", false);+ auto t1 = std::chrono::steady_clock::now();+ usg = std::chrono::duration<double, std::micro>(t1 - t0).count() / it;}+ // one multi-token kernel: parity against ggml's vec_dot, then one-token and NT-token throughput+ auto check = [&](const char* tag, bool is512) {+ std::vector<float> g((size_t) NT * FF);+ float* gp[NT];+ for (int k = 0; k < NT; ++k) gp[k] = g.data() + k * FF;+ (is512 ? cpu::iq512_rows : cpu::iq256_rows)(f.gu_type, blob.data(), f.gu_row, (int) H, a, NT, gp, 0, (int) FF);+ const double rg = rel(g, gref);+ std::printf(" %s %s gate rows vs ggml vec_dot: rel %.2e\n",+ ggml_type_name((ggml_type) f.gu_type), tag, rg);+ if (rg > 1e-5) { // ggml's own vec_dot is the reference: above 1e-5 it is a real mismatch,+ std::printf(" %s %s gate rows MISMATCH (rel %.2e > 1e-5)\n",+ ggml_type_name((ggml_type) f.gu_type), tag, rg); // not rounding (measured 3e-8)+ ++failures;+ }+ float* f1[1] = {ffp[0]};+ const int it = 50;+ const auto gu = is512 ? cpu::iq512_gu_rows : cpu::iq256_gu_rows;+ auto t0 = std::chrono::steady_clock::now();+ for (int i = 0; i < it; ++i) gu(f.gu_type, blob.data(), f.gu_row, f.up_off, (int) H, a, 1, f1, 0, (int) FF);+ auto t1 = std::chrono::steady_clock::now();+ for (int i = 0; i < it; ++i) gu(f.gu_type, blob.data(), f.gu_row, f.up_off, (int) H, a, NT, ffp, 0, (int) FF);+ auto t2 = std::chrono::steady_clock::now();+ const double us1 = std::chrono::duration<double, std::micro>(t1 - t0).count() / it;+ const double usn = std::chrono::duration<double, std::micro>(t2 - t1).count() / it;+ std::printf(" gate+up one token one thread: %s %.0f us (%.2f GB/s), ggml %.0f us (%.2f GB/s)\n",+ tag, us1, 2.0 * f.up_off / us1 / 1e3, usg, 2.0 * f.up_off / usg / 1e3);+ std::printf(" gate+up %d tokens one thread: %s %.0f us vs ggml %d x %.0f us\n", NT, tag, usn, NT, usg);+ };+ if (cpu::cpu_avx512_ok()) check("avx512", true); // guarded: the binary runs on AVX-2 CPUs too+ check("avx2", false);+ }+ for (int k = 0; k < NT; ++k) {+ cpu::native_quant_h(f, ff[k].data(), hq[k].data());+ hp[k] = hq[k].data();+ op[k] = got_c.data() + k * H;+ }+ cpu::native_down_rows(f, blob.data(), hp, NT, op, 0, (int) H);+ { // #152: a token's rows must not depend on how many tokens share the call - each token alone (nt = 1)+ // against its row of the NT-token call above, bit for bit, gate/up and down+ size_t gu_diff = 0, dn_diff = 0;+ std::vector<float> one_ff(FF), one_out(H);+ // the NT-token gate/up rows again: the throughput loops above overwrote ff with other kernels' rows+ for (int k = 0; k < NT; ++k) ffp[k] = ff[k].data();+ cpu::native_gu_rows(f, blob.data(), a, NT, ffp, 0, (int) FF);for (int k = 0; k < NT; ++k) {- cpu::native_quant_h(f, ff[k].data(), hq[k].data());- hp[k] = hq[k].data();- op[k] = got_c.data() + k * H;+ const void* a1[1] = {a[k]};+ float* f1[1] = {one_ff.data()};+ cpu::native_gu_rows(f, blob.data(), a1, 1, f1, 0, (int) FF);+ for (int64_t r = 0; r < FF; ++r) gu_diff += std::memcmp(&one_ff[r], &ff[k][r], 4) != 0;+ const void* h1[1] = {hp[k]};+ float* o1[1] = {one_out.data()};+ cpu::native_down_rows(f, blob.data(), h1, 1, o1, 0, (int) H);+ for (int64_t r = 0; r < H; ++r) dn_diff += std::memcmp(&one_out[r], &got_c[k * H + r], 4) != 0;}- cpu::native_down_rows(f, blob.data(), hp, NT, op, 0, (int) H);- { // #152: a token's rows must not depend on how many tokens share the call - each token alone (nt = 1)- // against its row of the NT-token call above, bit for bit, gate/up and down- size_t gu_diff = 0, dn_diff = 0;- std::vector<float> one_ff(FF), one_out(H);- // the NT-token gate/up rows again: the throughput loops above overwrote ff with other kernels' rows- for (int k = 0; k < NT; ++k) ffp[k] = ff[k].data();- cpu::native_gu_rows(f, blob.data(), a, NT, ffp, 0, (int) FF);- for (int k = 0; k < NT; ++k) {- const void* a1[1] = {a[k]};- float* f1[1] = {one_ff.data()};- cpu::native_gu_rows(f, blob.data(), a1, 1, f1, 0, (int) FF);- for (int64_t r = 0; r < FF; ++r) gu_diff += std::memcmp(&one_ff[r], &ff[k][r], 4) != 0;- const void* h1[1] = {hp[k]};- float* o1[1] = {one_out.data()};- cpu::native_down_rows(f, blob.data(), h1, 1, o1, 0, (int) H);- for (int64_t r = 0; r < H; ++r) dn_diff += std::memcmp(&one_out[r], &got_c[k * H + r], 4) != 0;- }- std::printf(" width invariance (1 vs %d tokens): gate/up %zu, down %zu rows differ\n", NT,- gu_diff, dn_diff);- if (gu_diff || dn_diff) ++failures;+ std::printf(" width invariance (1 vs %d tokens): gate/up %zu, down %zu rows differ\n", NT,+ gu_diff, dn_diff);+ if (gu_diff || dn_diff) ++failures;+ }+ if (f.d_type == 42) {+ // (b2) the GGUF-layout Q2_0 kernel the pool uses for Q2_0 down projections - the AVX-512 one+ // where the CPU has it, the AVX-2 one (q2_avx2.cpp) where it does not. Calling the AVX-512+ // kernel unconditionally faults on a Zen 2/3 CPU.+ std::vector<cpu::ActQ> a2(NT);+ const cpu::ActQ* ap[NT];+ std::vector<float> alt((size_t) NT * H);+ float* altp[NT];+ for (int k = 0; k < NT; ++k) {+ cpu::act_quant_any(ff[k].data(), (int) FF, a2[k]); // gated: AVX-512 or AVX-2 per CPU+ ap[k] = &a2[k];+ altp[k] = alt.data() + k * H;}- if (f.d_type == 42) {- // (b2) the GGUF-layout Q2_0 kernel the pool uses for Q2_0 down projections - the AVX-512 one- // where the CPU has it, the AVX-2 one (q2_avx2.cpp) where it does not. Calling the AVX-512- // kernel unconditionally faults on a Zen 2/3 CPU.- std::vector<cpu::ActQ> a2(NT);- const cpu::ActQ* ap[NT];- std::vector<float> alt((size_t) NT * H);- float* altp[NT];- for (int k = 0; k < NT; ++k) {- cpu::act_quant_any(ff[k].data(), (int) FF, a2[k]); // gated: AVX-512 or AVX-2 per CPU- ap[k] = &a2[k];- altp[k] = alt.data() + k * H;- }- cpu::q2_rows_any(blob.data() + f.down_off, f.d_row, (int) (FF / 64), ap, NT, altp, 0, (int) H);- std::printf(" q2_0 %s down vs ggml down: rel %.2e\n",- cpu::cpu_avx512_ok() ? "AVX-512" : "AVX-2", rel(alt, got_c));+ cpu::q2_rows_any(blob.data() + f.down_off, f.d_row, (int) (FF / 64), ap, NT, altp, 0, (int) H);+ std::printf(" q2_0 %s down vs ggml down: rel %.2e\n",+ cpu::cpu_avx512_ok() ? "AVX-512" : "AVX-2", rel(alt, got_c));+ }+ if (f.d_type == 20) {+ // (b3) the IQ4_NL multi-token AVX-2 kernel the pool now uses for IQ4_NL down projections,+ // against ggml-cpu's single-token vec_dot on the SAME Q8_0 activations (h), plus timing.+ std::vector<float> alt((size_t) NT * H), refd((size_t) NT * H);+ float* altp[NT];+ for (int k = 0; k < NT; ++k) altp[k] = alt.data() + k * H;+ cpu::iq4nl256_down_rows(blob.data() + f.down_off, f.d_row, (int) FF, hp, NT, altp, 0, (int) H);+ const auto* tdc = ggml_get_type_traits_cpu((ggml_type) f.d_type);+ for (int k = 0; k < NT; ++k)+ for (int64_t r = 0; r < H; ++r)+ tdc->vec_dot((int) FF, refd.data() + k * H + r, 0,+ blob.data() + f.down_off + (size_t) r * f.d_row, 0, hp[k], 0, 1);+ const double rdn = rel(alt, refd);+ std::printf(" iq4_nl AVX-2 multi-token down vs ggml down: rel %.2e\n", rdn);+ if (rdn > 1e-5) { // measured 1e-7; same reasoning as the gate/up rows above+ std::printf(" iq4_nl down MISMATCH (rel %.2e > 1e-5)\n", rdn);+ ++failures;}- if (f.d_type == 20) {- // (b3) the IQ4_NL multi-token AVX-2 kernel the pool now uses for IQ4_NL down projections,- // against ggml-cpu's single-token vec_dot on the SAME Q8_0 activations (h), plus timing.- std::vector<float> alt((size_t) NT * H), refd((size_t) NT * H);- float* altp[NT];- for (int k = 0; k < NT; ++k) altp[k] = alt.data() + k * H;+ const int it = 50;+ const auto t0 = std::chrono::steady_clock::now();+ for (int i = 0; i < it; ++i)cpu::iq4nl256_down_rows(blob.data() + f.down_off, f.d_row, (int) FF, hp, NT, altp, 0, (int) H);- const auto* tdc = ggml_get_type_traits_cpu((ggml_type) f.d_type);+ const auto t1 = std::chrono::steady_clock::now();+ for (int i = 0; i < it; ++i)for (int k = 0; k < NT; ++k)for (int64_t r = 0; r < H; ++r)tdc->vec_dot((int) FF, refd.data() + k * H + r, 0,- blob.data() + f.down_off + (size_t) r * f.d_row, 0, hp[k], 0, 1);- const double rdn = rel(alt, refd);- std::printf(" iq4_nl AVX-2 multi-token down vs ggml down: rel %.2e\n", rdn);- if (rdn > 1e-5) { // measured 1e-7; same reasoning as the gate/up rows above- std::printf(" iq4_nl down MISMATCH (rel %.2e > 1e-5)\n", rdn);- ++failures;- }- const int it = 50;- const auto t0 = std::chrono::steady_clock::now();- for (int i = 0; i < it; ++i)- cpu::iq4nl256_down_rows(blob.data() + f.down_off, f.d_row, (int) FF, hp, NT, altp, 0, (int) H);- const auto t1 = std::chrono::steady_clock::now();- for (int i = 0; i < it; ++i)- for (int k = 0; k < NT; ++k)- for (int64_t r = 0; r < H; ++r)- tdc->vec_dot((int) FF, refd.data() + k * H + r, 0,- blob.data() + f.down_off + (size_t) r * f.d_row, 0, hp[k], 0, 1);- const auto t2 = std::chrono::steady_clock::now();- const double usn = std::chrono::duration<double, std::micro>(t1 - t0).count() / it;- const double usg = std::chrono::duration<double, std::micro>(t2 - t1).count() / it;- std::printf(" down %d tokens one thread: avx2 %.0f us (%.2f GB/s) vs ggml %.0f us (%.2f GB/s)\n",- NT, usn, (double) (f.bytes - f.down_off) / usn / 1e3, usg,- (double) (f.bytes - f.down_off) / usg / 1e3);- }+ blob.data() + f.down_off + (size_t) r * f.d_row, 0, hp[k], 0, 1);+ const auto t2 = std::chrono::steady_clock::now();+ const double usn = std::chrono::duration<double, std::micro>(t1 - t0).count() / it;+ const double usg = std::chrono::duration<double, std::micro>(t2 - t1).count() / it;+ std::printf(" down %d tokens one thread: avx2 %.0f us (%.2f GB/s) vs ggml %.0f us (%.2f GB/s)\n",+ NT, usn, (double) (f.bytes - f.down_off) / usn / 1e3, usg,+ (double) (f.bytes - f.down_off) / usg / 1e3);}- // (c) the GPU: one group holding the NT entries- {- const auto L = strata::kernels::native_expert_layout(f.gu_type, f.d_type, H, FF);- void *dblob, *dx, *dxq, *dscr;- float* dout;- unsigned long long* dptr;- int32_t *dstart, *dn, *ddst, *dtok;- cudaMalloc(&dblob, blob.size());- cudaMalloc(&dx, x.size() * 4);- cudaMalloc(&dxq, (size_t) NT * H / 32 * 36);- cudaMalloc(&dscr, strata::kernels::native_expert_scratch_bytes(NT, FF));- cudaMalloc((void**) &dout, (size_t) NT * H * 4);- cudaMalloc((void**) &dptr, 8);- cudaMalloc((void**) &dstart, 8);- cudaMalloc((void**) &dn, 4);- cudaMalloc((void**) &ddst, NT * 4);- cudaMalloc((void**) &dtok, NT * 4);- cudaMemcpy(dblob, blob.data(), blob.size(), cudaMemcpyHostToDevice);- cudaMemcpy(dx, x.data(), x.size() * 4, cudaMemcpyHostToDevice);- const unsigned long long p = (unsigned long long) dblob;- const int32_t st[2] = {0, NT}, one = 1, idx[NT] = {0, 1, 2};- cudaMemcpy(dptr, &p, 8, cudaMemcpyHostToDevice);- cudaMemcpy(dstart, st, 8, cudaMemcpyHostToDevice);- cudaMemcpy(dn, &one, 4, cudaMemcpyHostToDevice);- cudaMemcpy(ddst, idx, NT * 4, cudaMemcpyHostToDevice);- cudaMemcpy(dtok, idx, NT * 4, cudaMemcpyHostToDevice);- strata::kernels::quantize_q8_1_rows((const float*) dx, NT, H, dxq, s);+ }+ // (c) the GPU: one group holding the NT entries+ {+ const auto L = strata::kernels::native_expert_layout(f.gu_type, f.d_type, H, FF);+ void *dblob, *dx, *dxq, *dscr;+ float* dout;+ unsigned long long* dptr;+ int32_t *dstart, *dn, *ddst, *dtok;+ cudaMalloc(&dblob, blob.size());+ cudaMalloc(&dx, x.size() * 4);+ cudaMalloc(&dxq, (size_t) NT * H / 32 * 36);+ cudaMalloc(&dscr, strata::kernels::native_expert_scratch_bytes(NT, FF));+ cudaMalloc((void**) &dout, (size_t) NT * H * 4);+ cudaMalloc((void**) &dptr, 8);+ cudaMalloc((void**) &dstart, 8);+ cudaMalloc((void**) &dn, 4);+ cudaMalloc((void**) &ddst, NT * 4);+ cudaMalloc((void**) &dtok, NT * 4);+ cudaMemcpy(dblob, blob.data(), blob.size(), cudaMemcpyHostToDevice);+ cudaMemcpy(dx, x.data(), x.size() * 4, cudaMemcpyHostToDevice);+ const unsigned long long p = (unsigned long long) dblob;+ const int32_t st[2] = {0, NT}, one = 1, idx[NT] = {0, 1, 2};+ cudaMemcpy(dptr, &p, 8, cudaMemcpyHostToDevice);+ cudaMemcpy(dstart, st, 8, cudaMemcpyHostToDevice);+ cudaMemcpy(dn, &one, 4, cudaMemcpyHostToDevice);+ cudaMemcpy(ddst, idx, NT * 4, cudaMemcpyHostToDevice);+ cudaMemcpy(dtok, idx, NT * 4, cudaMemcpyHostToDevice);+ strata::kernels::quantize_q8_1_rows((const float*) dx, NT, H, dxq, s);+ strata::kernels::native_expert_grouped(L, dptr, dstart, dn, ddst, dtok, 1, NT, dxq, dscr, dout, s);+ cudaStreamSynchronize(s);+ cudaMemcpy(got_g.data(), dout, got_g.size() * 4, cudaMemcpyDeviceToHost);+ { // the kernels that decode a weight part once for the NT entries, bitwise vs the per-entry ones (#242;+ // the formats without a decode-once split take the per-entry kernels either way)+ std::vector<float> old_g(got_g.size());+ const bool was = strata::kernels::iq_old_kernels();+ strata::kernels::iq_set_old_kernels(!was);strata::kernels::native_expert_grouped(L, dptr, dstart, dn, ddst, dtok, 1, NT, dxq, dscr, dout, s);+ strata::kernels::iq_set_old_kernels(was);cudaStreamSynchronize(s);- cudaMemcpy(got_g.data(), dout, got_g.size() * 4, cudaMemcpyDeviceToHost);- { // the kernels that decode a weight part once for the NT entries, bitwise vs the per-entry ones- std::vector<float> old_g(got_g.size());- const bool was = strata::kernels::iq_old_kernels();- strata::kernels::iq_set_old_kernels(!was);- strata::kernels::native_expert_grouped(L, dptr, dstart, dn, ddst, dtok, 1, NT, dxq, dscr, dout, s);- strata::kernels::iq_set_old_kernels(was);+ cudaMemcpy(old_g.data(), dout, old_g.size() * 4, cudaMemcpyDeviceToHost);+ const bool same = std::memcmp(old_g.data(), got_g.data(), got_g.size() * 4) == 0;+ std::printf(" gpu decode-once vs per-entry kernels: %s\n", same ? "bitwise equal" : "MISMATCH");+ if (!same) ++failures;+ }+ { // (d) the GPU dequantizers (prompt path: iq_dequant_f32/_f16; embedding: iq_embed_rows) against to_float+ float* dq = nullptr;+ uint16_t* dh = nullptr;+ int32_t* dtk = nullptr;+ cudaMalloc((void**) &dq, (size_t) FF * H * 4);+ cudaMalloc((void**) &dh, (size_t) FF * H * 2);+ cudaMalloc((void**) &dtk, (size_t) FF * 4);+ std::vector<float> got_dq((size_t) FF * H);+ std::vector<uint16_t> got_h((size_t) FF * H);+ std::vector<int32_t> tk((size_t) FF);+ for (int64_t r = 0; r < FF; ++r) tk[(size_t) r] = (int32_t) (FF - 1 - r); // rows in reverse order+ cudaMemcpy(dtk, tk.data(), tk.size() * 4, cudaMemcpyHostToDevice);+ double dq_err = 0.0;+ size_t differ = 0, h_differ = 0;+ const struct { const uint8_t* src; int type; const std::vector<float>* want; } mats[3] = {+ {(const uint8_t*) dblob, f.gu_type, &G}, {(const uint8_t*) dblob + f.up_off, f.gu_type, &U},+ {(const uint8_t*) dblob + f.down_off, f.d_type, &D}};+ for (const auto& m : mats) {+ strata::kernels::iq_dequant_f32(m.type, m.src, FF * H, dq, s);+ strata::kernels::iq_dequant_f16(m.type, m.src, FF * H, dh, s);cudaStreamSynchronize(s);- cudaMemcpy(old_g.data(), dout, old_g.size() * 4, cudaMemcpyDeviceToHost);- const bool same = std::memcmp(old_g.data(), got_g.data(), got_g.size() * 4) == 0;- std::printf(" gpu decode-once vs per-entry kernels: %s\n", same ? "bitwise equal" : "MISMATCH");- if (!same) ++failures;+ cudaMemcpy(got_dq.data(), dq, got_dq.size() * 4, cudaMemcpyDeviceToHost);+ cudaMemcpy(got_h.data(), dh, got_h.size() * 2, cudaMemcpyDeviceToHost);+ dq_err = (std::max)(dq_err, rel(got_dq, *m.want));+ for (size_t i = 0; i < got_dq.size(); ++i) {+ differ += std::memcmp(&got_dq[i], &(*m.want)[i], 4) != 0;+ h_differ += got_h[i] != ggml_fp32_to_fp16(got_dq[i]);+ }+ }+ // the gate matrix as an embedding table of FF rows of H values+ strata::kernels::iq_embed_rows(f.gu_type, dblob, f.gu_row, dtk, FF, H, dq, s);+ cudaStreamSynchronize(s);+ cudaMemcpy(got_dq.data(), dq, got_dq.size() * 4, cudaMemcpyDeviceToHost);+ size_t emb_differ = 0;+ for (int64_t r = 0; r < FF; ++r)+ emb_differ += std::memcmp(got_dq.data() + r * H, G.data() + (FF - 1 - r) * H, (size_t) H * 4) != 0;+ cudaFree(dq); cudaFree(dh); cudaFree(dtk);+ std::printf(" GPU dequant vs ggml to_float: rel %.2e, %zu of %lld values differ in any bit; fp16 of "+ "it: %zu differ; embedding rows: %zu of %lld differ\n", dq_err, differ, (long long) (3 * FF * H),+ h_differ, emb_differ, (long long) FF);+ // exact where there is one product (Q8_0), a rounding at most elsewhere (fused multiply-adds)+ const bool exact = f.gu_type == 8 && f.d_type == 8;+ if (!(dq_err < 1e-6) || h_differ || (exact && (differ || emb_differ))) ++failures;+ }+ cudaFree(dblob); cudaFree(dx); cudaFree(dxq); cudaFree(dscr); cudaFree(dout); cudaFree(dptr);+ cudaFree(dstart); cudaFree(dn); cudaFree(ddst); cudaFree(dtok);+ }+ const double ec = rel(got_c, ref), eg = rel(got_g, ref), ecg = rel(got_c, got_g);+ const bool ok = ec < 3e-2 && eg < 3e-2 && std::isfinite(ec) && std::isfinite(eg);+ std::printf("%-9s %-8s/%-7s blob %8zu cpu rel %.2e gpu rel %.2e cpu-gpu %.2e %s\n", label.c_str(),+ ggml_type_name((ggml_type) f.gu_type), ggml_type_name((ggml_type) f.d_type), f.bytes, ec, eg, ecg,+ ok ? "ok" : "FAIL");+ return failures + (ok ? 0 : 1);+}++int type_of(std::string name) {+ for (auto& c : name) c = (char) std::tolower((unsigned char) c);+ for (int t = 0; t < GGML_TYPE_COUNT; ++t) {+ const char* n = ggml_type_name((ggml_type) t);+ if (n == nullptr) continue;+ std::string m(n);+ for (auto& c : m) c = (char) std::tolower((unsigned char) c);+ if (m == name) return t;+ }+ return -1;+}++// Random weights (rows of different magnitudes) quantized by ggml into the blob layout.+std::vector<uint8_t> synthetic_blob(const cpu::NativeFmt& f, int seed) {+ std::vector<uint8_t> blob(f.bytes);+ std::mt19937 rng(seed);+ std::normal_distribution<float> nd(0.f, 1.f);+ auto quant = [&](int type, int64_t rows, int64_t cols, uint8_t* dst) {+ std::vector<float> w((size_t) (rows * cols));+ for (int64_t r = 0; r < rows; ++r) {+ const float scale = 0.02f * (0.5f + (float) (r % 7) / 7.0f);+ for (int64_t c = 0; c < cols; ++c) w[(size_t) (r * cols + c)] = scale * nd(rng);+ }+ const std::vector<float> imatrix((size_t) cols, 1.0f);+ ggml_quantize_chunk((ggml_type) type, w.data(), dst, 0, rows, cols,+ ggml_quantize_requires_imatrix((ggml_type) type) ? imatrix.data() : nullptr);+ };+ quant(f.gu_type, FF, H, blob.data());+ quant(f.gu_type, FF, H, blob.data() + f.up_off);+ quant(f.d_type, H, FF, blob.data() + f.down_off);+ return blob;+}++// The Q5_1 min term. Activations x = d * (q + 0.45) round to q, so each q8_1 block's sum of the ORIGINAL values+// (`ds.y`, what llama.cpp CUDA multiplies the min by) exceeds d * sum(q) by 0.45 * 32 * d. The GPU dot must take+// the quantized sum, as ggml-cpu's vec_dot does: equal to ggml-cpu on the same blocks with s = d * sum(q), and far+// from the ds.y variant. Weights with large mins make the difference visible.+int check_q5_1_min(cudaStream_t s) {+ constexpr int n = 640, rows = 64;+ ggml_cpu_init(); // the fp16 tables vec_dot uses (the other modes get this through native_fmt)+ std::mt19937 rng(5);+ std::uniform_int_distribution<int> qd(-100, 100);+ std::normal_distribution<float> nd(0.f, 1.f);+ std::vector<float> w((size_t) rows * n), x(n);+ for (auto& v : w) v = 0.3f + 0.05f * nd(rng); // positive weights: a large min per block+ const float d = 0.01f;+ for (auto& v : x) v = d * ((float) qd(rng) + 0.45f);+ const size_t row_bytes = ggml_row_size(GGML_TYPE_Q5_1, n);+ std::vector<uint8_t> wq((size_t) rows * row_bytes);+ ggml_quantize_chunk(GGML_TYPE_Q5_1, w.data(), wq.data(), 0, rows, n, nullptr);+ // the GPU: its own q8_1 quantizer, then iq_mmvq on Q5_1+ void *dw, *dx, *dxq;+ float* dy;+ cudaMalloc(&dw, wq.size());+ cudaMalloc(&dx, n * 4);+ cudaMalloc(&dxq, (size_t) n / 32 * 36);+ cudaMalloc((void**) &dy, rows * 4);+ cudaMemcpy(dw, wq.data(), wq.size(), cudaMemcpyHostToDevice);+ cudaMemcpy(dx, x.data(), n * 4, cudaMemcpyHostToDevice);+ strata::kernels::quantize_q8_1_rows((const float*) dx, 1, n, dxq, s);+ strata::kernels::iq_mmvq(GGML_TYPE_Q5_1, dw, dxq, dy, n, rows, 1, s);+ cudaStreamSynchronize(s);+ std::vector<float> gpu(rows);+ std::vector<uint8_t> xq((size_t) n / 32 * 36);+ cudaMemcpy(gpu.data(), dy, rows * 4, cudaMemcpyDeviceToHost);+ cudaMemcpy(xq.data(), dxq, xq.size(), cudaMemcpyDeviceToHost);+ cudaFree(dw); cudaFree(dx); cudaFree(dxq); cudaFree(dy);+ // ggml-cpu on the GPU's own blocks: once with s = d * sum(q) (the quantized sum), once with s = ds.y as stored+ struct Q81 { ggml_fp16_t d, s; int8_t qs[32]; }; // ggml's block_q8_1: {d, s} halves, 32 codes+ static_assert(sizeof(Q81) == 36, "block_q8_1 layout");+ auto* blocks = (Q81*) xq.data();+ std::vector<Q81> qsum(blocks, blocks + n / 32);+ double shift = 0.0;+ for (auto& b : qsum) {+ int sq = 0;+ for (int i = 0; i < 32; ++i) sq += b.qs[i];+ const float dd = ggml_fp16_to_fp32(b.d);+ shift = (std::max)(shift, (double) std::fabs(ggml_fp16_to_fp32(b.s) - dd * sq));+ b.s = ggml_fp32_to_fp16(dd * (float) sq);+ }+ const auto* tc = ggml_get_type_traits_cpu(GGML_TYPE_Q5_1);+ std::vector<float> want(rows), stored(rows);+ for (int r = 0; r < rows; ++r) {+ const uint8_t* row = wq.data() + (size_t) r * row_bytes;+ tc->vec_dot(n, &want[r], 0, row, 0, qsum.data(), 0, 1);+ tc->vec_dot(n, &stored[r], 0, row, 0, blocks, 0, 1);+ }+ const double e_q = rel(gpu, want), e_s = rel(gpu, stored);+ // the quantized sum goes through fp16 in ggml-cpu's block (s is a half), so 1e-3 rather than bitwise+ const bool ok = e_q < 1e-3 && e_s > 10 * e_q && shift > 0.0;+ std::printf("q5_1 min row 0: GPU %.6g, ggml-cpu (quantized sum) %.6g, (stored sum) %.6g\n", gpu[0], want[0],+ stored[0]);+ std::printf("q5_1 min GPU vs ggml-cpu with s = d*sum(q): rel %.2e; vs s = sum(x) (llama.cpp CUDA's): rel %.2e "+ "(largest per-block sum shift %.3g) %s\n", e_q, e_s, shift, ok ? "ok" : "FAIL");+ return ok ? 0 : 1;+}+} // namespace++int main(int argc, char** argv) {+ setvbuf(stdout, nullptr, _IONBF, 0); // keep the trail on a crash+ if (argc < 2) {+ std::fprintf(stderr, "usage: native_expert_parity <shard.gguf> [layer ...]\n"+ " native_expert_parity --synthetic GU/DOWN ... (ggml type names, e.g. q4_K/q5_1)\n"+ " native_expert_parity --q5_1-min\n");+ return 2;+ }+ // #152's width check tests the opt-in rule (the multi-token kernels from one token on)+ if (std::getenv("STRATA_IQ_MT_MIN") == nullptr) {+#ifdef _WIN32+ _putenv_s("STRATA_IQ_MT_MIN", "1");+#else+ setenv("STRATA_IQ_MT_MIN", "1", 1);+#endif+ }+ int failures = 0;+ cudaStream_t s;+ cudaStreamCreate(&s);+ const std::string mode = argv[1];+ if (mode == "--q5_1-min") {+ failures += check_q5_1_min(s);+ } else if (mode == "--synthetic") {+ for (int i = 2; i < argc; ++i) {+ const std::string arg = argv[i];+ const size_t slash = arg.find('/');+ const int gu = slash == std::string::npos ? -1 : type_of(arg.substr(0, slash));+ const int dn = slash == std::string::npos ? -1 : type_of(arg.substr(slash + 1));+ cpu::NativeFmt f;+ std::string err;+ if (gu < 0 || dn < 0 || !cpu::native_fmt(gu, dn, H, FF, f, err) ||+ !strata::kernels::native_expert_supported(gu, dn, H, FF)) {+ std::printf("%s: %s\n", arg.c_str(), err.empty() ? "not a pair the GPU expert kernels take" : err.c_str());+ ++failures;+ continue;+ }+ failures += check_blob(f, synthetic_blob(f, i), i, "synthetic", s);+ }+ } else {+ const strata::GgufModel model(strata::gguf_split_paths(argv[1]));+ std::vector<int> layers;+ for (int i = 2; i < argc; ++i) layers.push_back(std::atoi(argv[i]));+ if (layers.empty()) layers = {0, 1, 2, 3, 20, 47};+ for (int l : layers) {+ const strata::TensorInfo* t[3] = {};+ const uint8_t* data[3] = {};+ const char* roles[3] = {"gate", "up", "down"};+ for (int r = 0; r < 3; ++r) { // each role from the shard that holds it+ size_t at = 0;+ t[r] = model.find("blk." + std::to_string(l) + ".ffn_" + roles[r] + "_exps.weight", &at);+ if (t[r]) data[r] = model.shard(at).tensor_data(*t[r]);+ }+ if (!t[0] || !t[1] || !t[2]) { std::printf("layer %d: no expert tensors\n", l); ++failures; continue; }+ cpu::NativeFmt f;+ std::string err;+ if (!cpu::native_fmt((int) t[0]->type, (int) t[2]->type, H, FF, f, err)) {+ std::printf("layer %d: %s\n", l, err.c_str()); ++failures; continue;}- cudaFree(dblob); cudaFree(dx); cudaFree(dxq); cudaFree(dscr); cudaFree(dout); cudaFree(dptr);- cudaFree(dstart); cudaFree(dn); cudaFree(ddst); cudaFree(dtok);+ std::vector<uint8_t> blob(f.bytes);+ std::memcpy(blob.data(), data[0] + (size_t) E * f.up_off, f.up_off);+ std::memcpy(blob.data() + f.up_off, data[1] + (size_t) E * f.up_off, f.up_off);+ std::memcpy(blob.data() + f.down_off, data[2] + (size_t) E * (f.bytes - f.down_off),+ f.bytes - f.down_off);+ failures += check_blob(f, blob, l, "layer " + std::to_string(l), s);}- const double ec = rel(got_c, ref), eg = rel(got_g, ref), ecg = rel(got_c, got_g);- const bool ok = ec < 3e-2 && eg < 3e-2 && std::isfinite(ec) && std::isfinite(eg);- std::printf("layer %2d %-8s/%-7s blob %8zu cpu rel %.2e gpu rel %.2e cpu-gpu %.2e %s\n", l,- ggml_type_name((ggml_type) f.gu_type), ggml_type_name((ggml_type) f.d_type), f.bytes, ec, eg, ecg,- ok ? "ok" : "FAIL");- if (!ok) ++failures;}std::printf("native_expert_parity: %d failures\n", failures);return failures ? 1 : 0;diff --git a/src/program/generate.cpp b/src/program/generate.cppindex 98de832e0..9b89f5f72 100644--- a/src/program/generate.cpp+++ b/src/program/generate.cpp@@ -29,6 +29,7 @@#include "strata/kernels/cpu/pool.hpp"#include "strata/kernels/cpu/expert_layout.hpp"#include "strata/kernels/ngram.hpp"+#include "strata/kernels/iq_kernels.hpp"#include "strata/kernels/s2_expert_grouped.hpp"#include "strata/kernels/sampler.hpp"#include "strata/kernels/verify_kernels.hpp"@@ -1520,6 +1521,20 @@ int main(int argc, char** argv) {std::fprintf(stderr, "strata generate: %s\n", err.c_str());return 1;}+ // Every layer's formats must have GPU expert kernels and a prompt-path dequantizer, checked here, before+ // anything is allocated: an unsupported down type used to exit from inside the first verify window, and+ // an unsupported dequant type left the prompt path's fp16 buffer unwritten.+ const auto& lay = strata::kernels::cpu::expert_layout();+ for (int64_t l = 0; lay.native && l < (int64_t) lay.fmt.size(); ++l) {+ const auto& f = lay.fmt[(size_t) l];+ if (!strata::kernels::native_expert_supported(f.gu_type, f.d_type, f.n_embd, f.n_ff)) {+ std::fprintf(stderr, "strata generate: layer %lld's experts are %s/%s (ggml types %d/%d), which this "+ "engine has no GPU kernels for\n", (long long) l,+ strata::ggml_type_name((uint32_t) f.gu_type), strata::ggml_type_name((uint32_t) f.d_type),+ f.gu_type, f.d_type);+ return 1;+ }+ }}const bool native_pack = strata::kernels::cpu::expert_layout().native;// STRATA_EARLY_REMOTE_CONTEXTS=1: create EVERY secondary context here, like CUDA1's. Under WSL2 the driver's@@ -1878,7 +1893,8 @@ int main(int argc, char** argv) {ss.ple.w.key_scales = (const float*) ((const uint8_t*) wk->data + wk->codes_bytes);}if (o.native_ple_key && wk->quantized()) {- if (!wk->native_data || (wk->native_type != 42 && wk->native_type != 18 && wk->native_type != 23) || !wk->native_q8_1) {+ if (!wk->native_data || (wk->native_type != 42 && wk->native_type != 18 && wk->native_type != 23 &&+ wk->native_type != 8) || !wk->native_q8_1) {std::fprintf(stderr, "strata generate: native PLE key is absent or incompatible\n");return 1;}