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++
cpu: NVFP4 expert rows in 256-bit lanes for CPUs without AVX-512
sergiywithb766cde6315a
diff --git a/CMakeLists.txt b/CMakeLists.txtindex bd5551032..c5ed73fb1 100644--- a/CMakeLists.txt+++ b/CMakeLists.txt@@ -1269,13 +1269,14 @@ if(STRATA_NATIVE_EXPERTS)target_sources(strata_kernels_cpu PRIVATE src/kernels/cpu/native_expert.cpp src/kernels/cpu/iq_avx512.cppsrc/kernels/cpu/iq_avx2.cpp src/kernels/cpu/kq_avx2.cppsrc/kernels/cpu/kq_avx1.cpp src/kernels/cpu/q2_avx1.cpp src/kernels/cpu/iq_avx1.cpp- src/kernels/cpu/nvfp4_avx512.cpp)+ src/kernels/cpu/nvfp4_avx512.cpp src/kernels/cpu/nvfp4_avx2.cpp)target_include_directories(strata_kernels_cpu PRIVATE third_party/ggml)if(MSVC)set_source_files_properties(src/kernels/cpu/iq_avx512.cpp src/kernels/cpu/nvfp4_avx512.cppPROPERTIES COMPILE_OPTIONS "/arch:AVX512")set_source_files_properties(src/kernels/cpu/iq_avx2.cpp PROPERTIES COMPILE_OPTIONS "/arch:AVX2")set_source_files_properties(src/kernels/cpu/kq_avx2.cpp PROPERTIES COMPILE_OPTIONS "/arch:AVX2")+ set_source_files_properties(src/kernels/cpu/nvfp4_avx2.cpp PROPERTIES COMPILE_OPTIONS "/arch:AVX2")# the router lookahead's AVX1 dot (older CPUs; called behind cpu_avx1_ok): no FMA, no 256-bit integer opsset_source_files_properties(src/kernels/cpu/kq_avx1.cpp PROPERTIES COMPILE_OPTIONS "/arch:AVX")# the older CPUs' Q2_0 rows and activation quantizer (q2_avx1.cpp, issue #1699): 128-bit integer lanes,@@ -1288,6 +1289,7 @@ if(STRATA_NATIVE_EXPERTS)"-mavx512f;-mavx512bw;-mavx512vl;-mavx512dq;-mavx512vnni;-mavx512vbmi;-mf16c;-mfma")set_source_files_properties(src/kernels/cpu/iq_avx2.cpp PROPERTIES COMPILE_OPTIONS "-mavx2;-mfma;-mf16c")set_source_files_properties(src/kernels/cpu/kq_avx2.cpp PROPERTIES COMPILE_OPTIONS "-mavx2;-mfma;-mf16c")+ set_source_files_properties(src/kernels/cpu/nvfp4_avx2.cpp PROPERTIES COMPILE_OPTIONS "-mavx2;-mfma;-mf16c")set_source_files_properties(src/kernels/cpu/kq_avx1.cpp PROPERTIES COMPILE_OPTIONS "-mavx")# the older CPUs' Q2_0 rows and activation quantizer (q2_avx1.cpp, issue #1699): SSE4.2 + AVX with# AVX2/FMA/F16C explicitly off, so a -march=native build host cannot leak AVX2 code into the file that@@ -1507,9 +1509,11 @@ if(STRATA_NATIVE_EXPERTS)add_executable(hip_ple_iq4 tests/hip/ple_iq4.cpp)target_link_libraries(hip_ple_iq4 PRIVATE strata_kernels strata_hip_runtime ggml-base)endif()- # the NVFP4 AVX-512 rows vs ggml-cpu's vec_dot, and their speed (CPU only; optional arg: a pack's experts.bin)+ # the NVFP4 AVX-512 and AVX2 rows vs ggml-cpu's vec_dot, and their speed (CPU only; optional arg: a pack's experts.bin)add_executable(nvfp4_avx512_parity src/kernels/cpu/nvfp4_avx512_parity.cpp)target_link_libraries(nvfp4_avx512_parity PRIVATE strata_kernels_cpu)+ add_test(NAME nvfp4_cpu_parity COMMAND nvfp4_avx512_parity) # random blocks; the AVX2 rows bit-equal to ggml's+ set_tests_properties(nvfp4_cpu_parity PROPERTIES TIMEOUT 120)if(STRATA_ENABLE_CUDA) # the NVFP4 decode kernels vs ggml's referenceadd_executable(nvfp4_expert_gpu_parity src/kernels/nvfp4_expert_gpu_parity.cpp)target_link_libraries(nvfp4_expert_gpu_parity PRIVATE strata_kernels ggml-base CUDA::cudart)diff --git a/include/strata/kernels/cpu/nvfp4_avx2.hpp b/include/strata/kernels/cpu/nvfp4_avx2.hppnew file mode 100644index 000000000..803d8b28c--- /dev/null+++ b/include/strata/kernels/cpu/nvfp4_avx2.hpp@@ -0,0 +1,26 @@+// include/strata/kernels/cpu/nvfp4_avx2.hpp - NVFP4 expert rows in 256-bit lanes, several tokens at once, for CPUs+// without AVX-512 (Zen 2/3, Intel 12th-14th gen).+//+// ggml-cpu's NVFP4 dot product (ggml_vec_dot_nvfp4_q8_0) re-decodes a row's blocks for every token of a window;+// these decode a 64-value block once and apply it to each token. Each token's arithmetic is that dot product's AVX2+// path step for step (the same integer sums, the same FMA order, the same horizontal sum), so a row equals+// ggml-cpu's bit for bit - what an AVX2 CPU computed before these existed. Activations are Q8_0.+#pragma once++#include <cstddef>+#include <cstdint>++namespace strata::kernels::cpu {++/// Whether rows of `n` values fit the kernels (whole 64-value blocks, at most 64 of them).+bool nvfp4_256_fits(int n);++/// Gate/up rows [r0, r1) for `nt` tokens: ff[t][r] = silu(s_gate * g) * (s_up * u), as nvfp4_512_gu_rows.+void nvfp4_256_gu_rows(const uint8_t* blob, size_t gu_row, size_t up_off, int n, const void* const* act, int nt,+ float* const* ff, int r0, int r1, float s_gate, float s_up);++/// Plain rows [r0, r1) against `nt` Q8_0 activations of `n` values, times `scale`, as nvfp4_512_rows.+void nvfp4_256_rows(const uint8_t* w, size_t row_bytes, int n, const void* const* act, int nt, float* const* out,+ int r0, int r1, float scale = 1.f);++} // namespace strata::kernels::cpudiff --git a/src/kernels/cpu/native_expert.cpp b/src/kernels/cpu/native_expert.cppindex 63998107a..1ab75b642 100644--- a/src/kernels/cpu/native_expert.cpp+++ b/src/kernels/cpu/native_expert.cpp@@ -7,6 +7,7 @@#include "strata/kernels/cpu/iq_avx2.hpp"#include "strata/kernels/cpu/iq_avx1.hpp"#include "strata/kernels/cpu/kq_avx2.hpp"+#include "strata/kernels/cpu/nvfp4_avx2.hpp"#include "strata/kernels/cpu/nvfp4_avx512.hpp"#include "strata/kernels/cpu/expert_layout.hpp"@@ -207,6 +208,13 @@ void native_gu_rows(const NativeFmt& f, const uint8_t* blob, const void* const*nvfp4_512_gu_rows(blob, f.gu_row, f.up_off, n, act, nt, ff, r0, r1, sg, su);return;}+ // Without AVX-512: the same rows in 256-bit lanes, bit-equal to ggml-cpu's AVX2 dot below; 2.0-2.4x it at 4-8+ // tokens, 2x under DRAM load with 6 threads (nvfp4_avx512_parity). STRATA_NO_NVFP4_256 falls back to ggml-cpu.+ static const bool nvfp4_256 = cpu_avx2_ok() && std::getenv("STRATA_NO_NVFP4_256") == nullptr;+ if (f.gu_type == kNvfp4Type && nvfp4_256 && nvfp4_256_fits(n)) {+ nvfp4_256_gu_rows(blob, f.gu_row, f.up_off, n, act, nt, ff, r0, r1, sg, su);+ return;+ }const ggml_vec_dot_t dot = traits(f.gu_type)->vec_dot;for (int r = r0; r < r1; ++r) {const uint8_t* gr = blob + (size_t) r * f.gu_row;@@ -254,6 +262,11 @@ void native_down_rows(const NativeFmt& f, const uint8_t* blob, const void* constnvfp4_512_rows(blob + f.down_off, f.d_row, n, hq, nt, out, r0, r1, sd);return;}+ static const bool nvfp4_256 = cpu_avx2_ok() && std::getenv("STRATA_NO_NVFP4_256") == nullptr; // see above+ if (f.d_type == kNvfp4Type && nvfp4_256 && nvfp4_256_fits(n)) {+ nvfp4_256_rows(blob + f.down_off, f.d_row, n, hq, nt, out, r0, r1, sd);+ return;+ }const ggml_vec_dot_t dot = traits(f.d_type)->vec_dot;for (int r = r0; r < r1; ++r) {const uint8_t* dr = blob + f.down_off + (size_t) r * f.d_row;diff --git a/src/kernels/cpu/nvfp4_avx2.cpp b/src/kernels/cpu/nvfp4_avx2.cppnew file mode 100644index 000000000..528ab1797--- /dev/null+++ b/src/kernels/cpu/nvfp4_avx2.cpp@@ -0,0 +1,185 @@+// src/kernels/cpu/nvfp4_avx2.cpp - see include/strata/kernels/cpu/nvfp4_avx2.hpp.+//+// Per 64-value block, once: its E2M1 values in element order as two 32-byte registers (ggml's reordering: sub-block+// s keeps value j in the low nibble of qs[8 s + j] and value j + 8 in the high one), their magnitudes, and the four+// sub-block scales. Per token, as ggml_vec_dot_nvfp4_q8_0's AVX2 path: vpsignb moves the sign onto the activation,+// vpmaddubsw + vpmaddwd give 8 int32 lanes (four per 16-value sub-block), one FMA per 32 values applies+// ue4m3(d[s]) * d_q8, and hsum_float_8's order sums the lanes.+//+// Needs AVX2, FMA and F16C - what cpu_avx2_ok() checks before anything here is called.+#include "strata/kernels/cpu/nvfp4_avx2.hpp"++#define GGML_COMMON_DECL_CPP+#define GGML_COMMON_IMPL_CPP+#include "ggml-common.h"++#include <immintrin.h>++#include <cmath>+#include <cstdlib>+#include <cstring>++namespace strata::kernels::cpu {+namespace {++struct Ue4m3Table { // ggml_ue4m3_to_fp32 (ggml-impl.h): halved for the doubled kvalues_fp4, 0x7F read as 0+ float v[256];+ Ue4m3Table() {+ for (int x = 0; x < 256; ++x) {+ if (x == 0 || x == 0x7F) { v[x] = 0.0f; continue; }+ const int e = (x >> 3) & 0xF, m = x & 0x7;+ const float raw = e == 0 ? std::ldexp((float) m, -9) : std::ldexp(1.0f + (float) m / 8.0f, e - 7);+ v[x] = raw * 0.5f;+ }+ }+};+const Ue4m3Table kUe4m3;++// software prefetch distance in bytes (STRATA_NVFP4_PREFETCH, 0 = off), as the AVX-512 rows+const int prefetch_ahead = [] {+ const char* v = std::getenv("STRATA_NVFP4_PREFETCH");+ return v ? std::atoi(v) : 2048;+}();++inline float h2f(uint16_t h) { return _mm_cvtss_f32(_mm_cvtph_ps(_mm_cvtsi32_si128((int) h))); }++// sub-blocks 2 h and 2 h + 1 (16 bytes of qs) as their 32 signed doubled E2M1 values, in element order+inline __m256i values32(const uint8_t* qs, const __m128i kv) {+ const __m128i q = _mm_loadu_si128((const __m128i*) qs);+ const __m128i m4 = _mm_set1_epi8(0x0F);+ const __m128i lo = _mm_shuffle_epi8(kv, _mm_and_si128(q, m4));+ const __m128i hi = _mm_shuffle_epi8(kv, _mm_and_si128(_mm_srli_epi16(q, 4), m4));+ return _mm256_set_m128i(_mm_unpackhi_epi64(lo, hi), _mm_unpacklo_epi64(lo, hi));+}++inline float hsum8(const __m256 x) { // ggml's hsum_float_8+ __m128 res = _mm256_extractf128_ps(x, 1);+ res = _mm_add_ps(res, _mm256_castps256_ps128(x));+ res = _mm_add_ps(res, _mm_movehl_ps(res, res));+ res = _mm_add_ss(res, _mm_movehdup_ps(res));+ return _mm_cvtss_f32(res);+}++constexpr int kMaxBlocks = 64; // rows of up to 4096 values (n_embd 2560)+// tokens per pass: the pool's MAXT, so a window is one pass. Spilled accumulators cost less than another pass over+// the weights: one expert's gate+up, 8 tokens, 0.415 ms in passes of 4 + 4, 0.352 in one of 8 (5 6 7 alike)+constexpr int kMaxW = 8;++struct Acts { // per token and block, the two Q8_0 blocks' scales; built once per call, reused by every row+ int nt = 0, nb = 0;+ const block_q8_0* y[kMaxW] = {};+ float dy[kMaxW * kMaxBlocks * 2];+ Acts(const void* const* act, int nt_, int nb_) : nt(nt_), nb(nb_) {+ for (int t = 0; t < nt; ++t) {+ y[t] = (const block_q8_0*) act[t];+ for (int ib = 0; ib < 2 * nb; ++ib) dy[(size_t) t * 2 * nb + ib] = h2f(y[t][ib].d);+ }+ }+};++template <int NT>+inline void row_dot(const uint8_t* row, const Acts& a, float* res) {+ const __m128i kv = _mm_loadu_si128((const __m128i*) kvalues_fp4);+ const __m256i ones = _mm256_set1_epi16(1);+ __m256 acc[NT];+ for (int t = 0; t < NT; ++t) acc[t] = _mm256_setzero_ps();+ const block_nvfp4* x = (const block_nvfp4*) row;+ for (int ib = 0; ib < a.nb; ++ib) {+ if (prefetch_ahead > 0) _mm_prefetch((const char*) (x + ib) + prefetch_ahead, _MM_HINT_T0);+ const __m256i v01 = values32(x[ib].qs, kv), v23 = values32(x[ib].qs + 16, kv);+ const __m256i a01 = _mm256_sign_epi8(v01, v01), a23 = _mm256_sign_epi8(v23, v23);+ // lanes 0-3 ue4m3(d[0]), 4-7 d[1] (and d[2], d[3]): times a broadcast d_q8, each lane is ggml's scalar+ // ue4m3(d[s]) * d_q8, rounded the same+ const __m256 u01 = _mm256_set_m128(_mm_set1_ps(kUe4m3.v[x[ib].d[1]]), _mm_set1_ps(kUe4m3.v[x[ib].d[0]]));+ const __m256 u23 = _mm256_set_m128(_mm_set1_ps(kUe4m3.v[x[ib].d[3]]), _mm_set1_ps(kUe4m3.v[x[ib].d[2]]));+ for (int t = 0; t < NT; ++t) {+ const block_q8_0* yb = a.y[t] + 2 * ib;+ const __m256i q01 = _mm256_loadu_si256((const __m256i*) yb[0].qs);+ const __m256i q23 = _mm256_loadu_si256((const __m256i*) yb[1].qs);+ const __m256i p1 = _mm256_madd_epi16(_mm256_maddubs_epi16(a01, _mm256_sign_epi8(q01, v01)), ones);+ const __m256i p2 = _mm256_madd_epi16(_mm256_maddubs_epi16(a23, _mm256_sign_epi8(q23, v23)), ones);+ const float* dy = a.dy + (size_t) t * 2 * a.nb + 2 * ib;+ const __m256 s01 = _mm256_mul_ps(u01, _mm256_broadcast_ss(dy));+ const __m256 s23 = _mm256_mul_ps(u23, _mm256_broadcast_ss(dy + 1));+ acc[t] = _mm256_fmadd_ps(s01, _mm256_cvtepi32_ps(p1), acc[t]);+ acc[t] = _mm256_fmadd_ps(s23, _mm256_cvtepi32_ps(p2), acc[t]);+ }+ }+ for (int t = 0; t < NT; ++t) res[t] = hsum8(acc[t]);+}++template <int NT>+void gu_rows(const uint8_t* blob, size_t gu_row, size_t up_off, const Acts& a, float* const* ff, int r0, int r1,+ float sg, float su) {+ float g[NT], u[NT];+ for (int r = r0; r < r1; ++r) {+ row_dot<NT>(blob + (size_t) r * gu_row, a, g);+ row_dot<NT>(blob + up_off + (size_t) r * gu_row, a, u);+ for (int t = 0; t < NT; ++t) { // native_gu_rows' ggml path, operation for operation+ const float gs = g[t] * sg, us = u[t] * su;+ ff[t][r] = (gs / (1.f + std::exp(-gs))) * us;+ }+ }+}++template <int NT>+void dot_rows(const uint8_t* w, size_t row_bytes, const Acts& a, float* const* out, int r0, int r1, float scale) {+ float res[NT];+ for (int r = r0; r < r1; ++r) {+ row_dot<NT>(w + (size_t) r * row_bytes, a, res);+ for (int t = 0; t < NT; ++t) out[t][r] = res[t] * scale;+ }+}++// windows wider than kMaxW (not from the pool) run in balanced slices of at most kMaxW+template <typename F>+inline void slices(int nt, F&& run) {+ const int k = (nt + kMaxW - 1) / kMaxW;+ for (int i = 0, t0 = 0; i < k; ++i) {+ const int w = nt / k + (i < nt % k ? 1 : 0);+ run(t0, w);+ t0 += w;+ }+}++} // namespace++bool nvfp4_256_fits(int n) { return n % QK_NVFP4 == 0 && n / QK_NVFP4 <= kMaxBlocks; }++void nvfp4_256_gu_rows(const uint8_t* blob, size_t gu_row, size_t up_off, int n, const void* const* act, int nt,+ float* const* ff, int r0, int r1, float s_gate, float s_up) {+ slices(nt, [&](int t0, int w) {+ const Acts a(act + t0, w, n / QK_NVFP4);+ float* const* f = ff + t0;+ switch (w) {+ case 1: gu_rows<1>(blob, gu_row, up_off, a, f, r0, r1, s_gate, s_up); break;+ case 2: gu_rows<2>(blob, gu_row, up_off, a, f, r0, r1, s_gate, s_up); break;+ case 3: gu_rows<3>(blob, gu_row, up_off, a, f, r0, r1, s_gate, s_up); break;+ case 4: gu_rows<4>(blob, gu_row, up_off, a, f, r0, r1, s_gate, s_up); break;+ case 5: gu_rows<5>(blob, gu_row, up_off, a, f, r0, r1, s_gate, s_up); break;+ case 6: gu_rows<6>(blob, gu_row, up_off, a, f, r0, r1, s_gate, s_up); break;+ case 7: gu_rows<7>(blob, gu_row, up_off, a, f, r0, r1, s_gate, s_up); break;+ default: gu_rows<8>(blob, gu_row, up_off, a, f, r0, r1, s_gate, s_up); break;+ }+ });+}++void nvfp4_256_rows(const uint8_t* w, size_t row_bytes, int n, const void* const* act, int nt, float* const* out,+ int r0, int r1, float scale) {+ slices(nt, [&](int t0, int k) {+ const Acts a(act + t0, k, n / QK_NVFP4);+ float* const* o = out + t0;+ switch (k) {+ case 1: dot_rows<1>(w, row_bytes, a, o, r0, r1, scale); break;+ case 2: dot_rows<2>(w, row_bytes, a, o, r0, r1, scale); break;+ case 3: dot_rows<3>(w, row_bytes, a, o, r0, r1, scale); break;+ case 4: dot_rows<4>(w, row_bytes, a, o, r0, r1, scale); break;+ case 5: dot_rows<5>(w, row_bytes, a, o, r0, r1, scale); break;+ case 6: dot_rows<6>(w, row_bytes, a, o, r0, r1, scale); break;+ case 7: dot_rows<7>(w, row_bytes, a, o, r0, r1, scale); break;+ default: dot_rows<8>(w, row_bytes, a, o, r0, r1, scale); break;+ }+ });+}++} // namespace strata::kernels::cpudiff --git a/src/kernels/cpu/nvfp4_avx512_parity.cpp b/src/kernels/cpu/nvfp4_avx512_parity.cppindex 4ce78e353..1042967cf 100644--- a/src/kernels/cpu/nvfp4_avx512_parity.cpp+++ b/src/kernels/cpu/nvfp4_avx512_parity.cpp@@ -1,4 +1,4 @@-// src/kernels/cpu/nvfp4_avx512_parity.cpp - the AVX-512 NVFP4 rows against ggml-cpu's own dot product.+// src/kernels/cpu/nvfp4_avx512_parity.cpp - the AVX-512 and AVX2 NVFP4 rows against ggml-cpu's own dot product.//// nvfp4_avx512_parity [pack/experts.bin]//@@ -7,9 +7,11 @@// so the check is a relative tolerance, not equality. Random blocks cover every code and a wide scale range; a real// expert (layer 0, expert 0 of a pack's experts.bin) covers the checkpoint's own distribution.// Also the down rows (n 640) and gate/up through silu with the blob tail's scales, 1..9 tokens (past one slice).-// Speed: gate+up rows of one expert for 1..8 tokens, both ways, as the CPU pool runs them.+// The AVX2 rows follow ggml's AVX2 path step for step, so for them every row must also be bit-equal (ggml-cpu built+// with AVX2: STRATA_PORTABLE, or a native build). Each kernel is checked when this CPU runs it.+// Speed: gate+up rows of one expert for 1..8 tokens, each way, as the CPU pool runs them.//-// nvfp4_avx512_parity --bw THREADS TOKENS [ggml]+// nvfp4_avx512_parity --bw THREADS TOKENS [ggml|avx2]// nvfp4_avx512_parity --expert pack/experts.bin [layer expert]//// --expert: one whole expert as decode runs it (Q8_0 input, gate/up + silu, Q8_0 of the hidden, down) against a@@ -18,6 +20,8 @@//// DRAM rate: THREADS threads each run whole experts (gate/up through silu, then down) out of 4.2 GB of blobs -// 40x the L3 - for TOKENS tokens; GB/s of weights read. STRATA_NVFP4_PREFETCH sets the kernel's prefetch distance.+#include "strata/kernels/cpu/expert_layout.hpp"+#include "strata/kernels/cpu/nvfp4_avx2.hpp"#include "strata/kernels/cpu/nvfp4_avx512.hpp"#include "ggml.h"@@ -34,11 +38,17 @@#include <string>#include <vector>-using strata::kernels::cpu::nvfp4_512_gu_rows;-using strata::kernels::cpu::nvfp4_512_rows;-namespace {+struct Kern {+ const char* name;+ void (*gu)(const uint8_t*, size_t, size_t, int, const void* const*, int, float* const*, int, int, float, float);+ void (*rows)(const uint8_t*, size_t, int, const void* const*, int, float* const*, int, int, float);+ bool exact; // bit-equal to ggml's AVX2 dot+};+const Kern k512{"avx512", strata::kernels::cpu::nvfp4_512_gu_rows, strata::kernels::cpu::nvfp4_512_rows, false};+const Kern k256{"avx2", strata::kernels::cpu::nvfp4_256_gu_rows, strata::kernels::cpu::nvfp4_256_rows, true};+constexpr int N = 2560, ROWS = 1280, BLK = 36; // an expert's gate+up: 1280 rows of 2560 valuesconstexpr int FF = 640; // its down: 2560 rows of 640constexpr size_t ROW_BYTES = (size_t) N / 64 * BLK, D_ROW = (size_t) FF / 64 * BLK;@@ -71,7 +81,7 @@ float ref_dot(const uint8_t* row, int n, const void* a) {}int report(const std::vector<std::vector<float>>& mine, const std::vector<std::vector<float>>& ref,- int nt, int rows, double& worst) {+ int nt, int rows, double& worst, int& differ) {int bad = 0;for (int t = 0; t < nt; ++t) {double scale = 0;@@ -80,33 +90,40 @@ int report(const std::vector<std::vector<float>>& mine, const std::vector<std::vconst double e = std::fabs((double) mine[t][r] - ref[t][r]) / (scale > 0 ? scale : 1);worst = std::fmax(worst, e);if (!(e <= 1e-5)) ++bad;+ differ += std::memcmp(&mine[t][r], &ref[t][r], sizeof(float)) != 0;}}return bad;}+// a kernel that claims bit equality fails on any differing row+int verdict(const Kern& k, const char* what, double worst, int rows, int nt, int bad, int differ) {+ std::printf("%-7s %-36s worst |mine - ggml| / max|ggml| = %.2e over %d rows x 1..%d tokens, %d beyond 1e-5, "+ "%d not bit-equal\n", k.name, what, worst, rows, nt, bad, differ);+ return bad + (k.exact ? differ : 0);+}+// plain rows: `rows` rows of `n` values, `row_bytes` apart-int check_rows(const uint8_t* w, int n, size_t row_bytes, int rows, const char* what, std::mt19937& rng) {+int check_rows(const Kern& k, const uint8_t* w, int n, size_t row_bytes, int rows, const char* what,+ std::mt19937& rng) {constexpr int NT = 9; // past one slice: 9 = 5 + 4Acts a(n, NT, rng);std::vector<std::vector<float>> mine(NT, std::vector<float>(rows)), ref(NT, std::vector<float>(rows));std::vector<float*> op(NT);for (int t = 0; t < NT; ++t) op[t] = mine[t].data();- int bad = 0;+ int bad = 0, differ = 0;double worst = 0;for (int nt = 1; nt <= NT; ++nt) {- nvfp4_512_rows(w, row_bytes, n, a.p.data(), nt, op.data(), 0, rows);+ k.rows(w, row_bytes, n, a.p.data(), nt, op.data(), 0, rows, 1.f);for (int t = 0; t < nt; ++t)for (int r = 0; r < rows; ++r) ref[t][r] = ref_dot(w + (size_t) r * row_bytes, n, a.p[t]);- bad += report(mine, ref, nt, rows, worst);+ bad += report(mine, ref, nt, rows, worst, differ);}- std::printf("%-34s worst |mine - ggml| / max|ggml| = %.2e over %d rows x 1..%d tokens, %d beyond 1e-5\n", what,- worst, rows, NT, bad);- return bad;+ return verdict(k, what, worst, rows, NT, bad, differ);}// gate/up with the blob tail's scales: silu(s_gate g) * (s_up u), as native_gu_rows' ggml path computes it-int check_gu(const uint8_t* blob, std::mt19937& rng) {+int check_gu(const Kern& k, const uint8_t* blob, std::mt19937& rng) {constexpr int NT = 9, F = ROWS / 2;float tail[4];std::memcpy(tail, blob + BLOB - 16, sizeof tail);@@ -116,24 +133,24 @@ int check_gu(const uint8_t* blob, std::mt19937& rng) {std::vector<std::vector<float>> mine(NT, std::vector<float>(F)), ref(NT, std::vector<float>(F));std::vector<float*> op(NT);for (int t = 0; t < NT; ++t) op[t] = mine[t].data();- int bad = 0;+ int bad = 0, differ = 0;double worst = 0;for (int nt = 1; nt <= NT; ++nt) {- nvfp4_512_gu_rows(blob, ROW_BYTES, up_off, N, a.p.data(), nt, op.data(), 0, F, sg, su);+ k.gu(blob, ROW_BYTES, up_off, N, a.p.data(), nt, op.data(), 0, F, sg, su);for (int t = 0; t < nt; ++t)for (int r = 0; r < F; ++r) {const float g = ref_dot(blob + (size_t) r * ROW_BYTES, N, a.p[t]) * sg;const float u = ref_dot(blob + up_off + (size_t) r * ROW_BYTES, N, a.p[t]) * su;ref[t][r] = (g / (1.f + std::exp(-g))) * u;}- bad += report(mine, ref, nt, F, worst);+ bad += report(mine, ref, nt, F, worst, differ);}- std::printf("%-34s worst |mine - ggml| / max|ggml| = %.2e over %d rows x 1..%d tokens, %d beyond 1e-5 "- "(s_gate %.3g, s_up %.3g)\n", "expert 0: silu(gate) * up", worst, F, NT, bad, sg, su);- return bad;+ char what[96];+ std::snprintf(what, sizeof what, "expert 0: silu(gate) * up (s %.2g/%.2g)", sg, su);+ return verdict(k, what, worst, F, NT, bad, differ);}-void speed(const std::vector<uint8_t>& w, std::mt19937& rng) {+void speed(const std::vector<uint8_t>& w, std::mt19937& rng, bool has512, bool has256) {const ggml_type_traits_cpu* tw = ggml_get_type_traits_cpu(GGML_TYPE_NVFP4);const ggml_type_traits_cpu* ta = ggml_get_type_traits_cpu(tw->vec_dot_type);const size_t act_bytes = ggml_row_size(tw->vec_dot_type, N);@@ -148,24 +165,32 @@ void speed(const std::vector<uint8_t>& w, std::mt19937& rng) {ap[t] = act[t].data();op[t] = sink.data() + (size_t) t * ROWS;}- std::printf("\n%-6s %12s %12s %8s (one expert's gate+up, %d rows; best of 20)\n", "tokens", "ggml ms", "avx512 ms",- "speedup", ROWS);+ std::printf("\n%-6s %10s %10s %8s %10s %8s (one expert's gate+up, %d rows; best of 20)\n", "tokens",+ "ggml ms", "avx512 ms", "speedup", "avx2 ms", "speedup", ROWS);for (int nt = 1; nt <= 8; ++nt) {- double tg = 1e30, tm = 1e30;+ double tg = 1e30, tm = 1e30, t2 = 1e30;for (int rep = 0; rep < 20; ++rep) {double t0 = now_ms();for (int t = 0; t < nt; ++t)for (int r = 0; r < ROWS; ++r) tw->vec_dot(N, &op[t][r], 0, w.data() + (size_t) r * ROW_BYTES, 0, ap[t], 0, 1);tg = std::fmin(tg, now_ms() - t0);- t0 = now_ms();- nvfp4_512_rows(w.data(), ROW_BYTES, N, ap, nt, op, 0, ROWS);- tm = std::fmin(tm, now_ms() - t0);+ if (has512) {+ t0 = now_ms();+ k512.rows(w.data(), ROW_BYTES, N, ap, nt, op, 0, ROWS, 1.f);+ tm = std::fmin(tm, now_ms() - t0);+ }+ if (has256) {+ t0 = now_ms();+ k256.rows(w.data(), ROW_BYTES, N, ap, nt, op, 0, ROWS, 1.f);+ t2 = std::fmin(t2, now_ms() - t0);+ }}- std::printf("%-6d %12.3f %12.3f %7.2fx\n", nt, tg, tm, tg / tm);+ std::printf("%-6d %10.3f %10.3f %7.2fx %10.3f %7.2fx\n", nt, tg, has512 ? tm : 0.0, has512 ? tg / tm : 0.0,+ has256 ? t2 : 0.0, has256 ? tg / t2 : 0.0);}}-int bandwidth(int threads, int nt, bool use_ggml) {+int bandwidth(int threads, int nt, bool use_ggml, const Kern& k) {constexpr int E = 1536;std::vector<uint8_t> blobs((size_t) E * BLOB);{@@ -198,8 +223,8 @@ int bandwidth(int threads, int nt, bool use_ggml) {const uint8_t* b = blobs.data() + (size_t) e * BLOB;const size_t up_off = (size_t) (ROWS / 2) * ROW_BYTES, d_off = (size_t) ROWS * ROW_BYTES;if (!use_ggml) {- nvfp4_512_gu_rows(b, ROW_BYTES, up_off, N, ax.p.data(), nt, fp.data(), 0, ROWS / 2, 1.f, 1.f);- nvfp4_512_rows(b + d_off, D_ROW, FF, ah.p.data(), nt, op.data(), 0, N);+ k.gu(b, ROW_BYTES, up_off, N, ax.p.data(), nt, fp.data(), 0, ROWS / 2, 1.f, 1.f);+ k.rows(b + d_off, D_ROW, FF, ah.p.data(), nt, op.data(), 0, N, 1.f);} else {for (int r = 0; r < ROWS / 2; ++r)for (int t = 0; t < nt; ++t) {@@ -218,7 +243,7 @@ int bandwidth(int threads, int nt, bool use_ggml) {}const char* pf = std::getenv("STRATA_NVFP4_PREFETCH");std::printf("%s threads %d tokens %d prefetch %s: %.1f ms for %d experts -> %.1f GB/s, %.1f us/expert/thread\n",- use_ggml ? "ggml " : "avx512", threads, nt, use_ggml ? "-" : (pf ? pf : "2048"), best, E,+ use_ggml ? "ggml " : k.name, threads, nt, use_ggml ? "-" : (pf ? pf : "2048"), best, E,(double) E * BLOB / best / 1e6, best * 1000.0 * threads / E);return 0;}@@ -265,11 +290,12 @@ int expert_e2e(const char* path, int layer, int e) {float* fp[1] = {ff.data()};float* op[1] = {out.data()};for (int mode = 0; mode < 2; ++mode) { // 0: s_down folded into up; 1: s_down on the output- nvfp4_512_gu_rows(b.data(), ROW_BYTES, up_off, N, ap, 1, fp, 0, F, sg, mode == 0 ? s_up * s_down : s_up);+ const Kern& k = strata::kernels::cpu::cpu_avx512_ok() ? k512 : k256;+ k.gu(b.data(), ROW_BYTES, up_off, N, ap, 1, fp, 0, F, sg, mode == 0 ? s_up * s_down : s_up);if (mode == 0) for (float v : ff) hmax_fold = std::fmax(hmax_fold, std::fabs(v));q8->from_float(ff.data(), hq.data(), FF);const void* hp[1] = {hq.data()};- nvfp4_512_rows(b.data() + d_off, D_ROW, FF, hp, 1, op, 0, N);+ k.rows(b.data() + d_off, D_ROW, FF, hp, 1, op, 0, N, 1.f);double err = 0;for (int r = 0; r < N; ++r) {const double got = mode == 0 ? out[r] : (double) out[r] * s_down;@@ -292,8 +318,14 @@ int main(int argc, char** argv) {ggml_cpu_init();if (argc >= 3 && std::string(argv[1]) == "--expert")return expert_e2e(argv[2], argc >= 4 ? std::atoi(argv[3]) : 0, argc >= 5 ? std::atoi(argv[4]) : 0);- if (argc >= 4 && std::string(argv[1]) == "--bw")- return bandwidth(std::atoi(argv[2]), std::atoi(argv[3]), argc >= 5 && std::string(argv[4]) == "ggml");+ const bool has512 = strata::kernels::cpu::cpu_avx512_ok(), has256 = strata::kernels::cpu::cpu_avx2_ok();+ if (argc >= 4 && std::string(argv[1]) == "--bw") {+ const std::string how = argc >= 5 ? argv[4] : "";+ return bandwidth(std::atoi(argv[2]), std::atoi(argv[3]), how == "ggml", how == "avx2" || !has512 ? k256 : k512);+ }+ std::vector<const Kern*> ks;+ if (has512) ks.push_back(&k512);+ if (has256) ks.push_back(&k256);std::mt19937 rng(20260929);std::vector<uint8_t> w(BLOB);// random blocks: every code, scales 0x20..0x5f (sign bit clear, never 0x7F), as ModelOpt writes them@@ -302,19 +334,24 @@ int main(int argc, char** argv) {for (int s = 0; s < 4; ++s) w[b + s] = (uint8_t) sc(rng);for (int q = 4; q < BLK; ++q) w[b + q] = (uint8_t) byte(rng);}- int bad = check_rows(w.data(), N, ROW_BYTES, ROWS, "random blocks, n 2560", rng);- bad += check_rows(w.data(), FF, D_ROW, N, "random blocks, n 640", rng);+ int bad = 0;+ for (const Kern* k : ks) {+ bad += check_rows(*k, w.data(), N, ROW_BYTES, ROWS, "random blocks, n 2560", rng);+ bad += check_rows(*k, w.data(), FF, D_ROW, N, "random blocks, n 640", rng);+ }if (argc >= 2) { // layer 0, expert 0 of the packstd::ifstream f(argv[1], std::ios::binary);if (!f.read((char*) w.data(), (std::streamsize) w.size())) {std::fprintf(stderr, "cannot read %zu bytes of %s\n", w.size(), argv[1]);return 2;}- bad += check_rows(w.data(), N, ROW_BYTES, ROWS, "expert 0: gate+up rows", rng);- bad += check_rows(w.data() + (size_t) ROWS * ROW_BYTES, FF, D_ROW, N, "expert 0: down rows", rng);- bad += check_gu(w.data(), rng);+ for (const Kern* k : ks) {+ bad += check_rows(*k, w.data(), N, ROW_BYTES, ROWS, "expert 0: gate+up rows", rng);+ bad += check_rows(*k, w.data() + (size_t) ROWS * ROW_BYTES, FF, D_ROW, N, "expert 0: down rows", rng);+ bad += check_gu(*k, w.data(), rng);+ }}- speed(w, rng);+ speed(w, rng, has512, has256);std::printf("\nRESULT: %s\n", bad ? "MISMATCH" : "parity OK");return bad ? 1 : 0;}