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++
nvfp4 CPU rows: no static initialisers in the AVX2/AVX-512 files (an illegal instruction before main on a CPU without AVX/FMA, #2324); check_isa_guard flags AVX-class static initialisers
Niko12210b6fc1cec79f
diff --git a/src/kernels/cpu/nvfp4_avx2.cpp b/src/kernels/cpu/nvfp4_avx2.cppindex 528ab1797..3ffc581f1 100644--- a/src/kernels/cpu/nvfp4_avx2.cpp+++ b/src/kernels/cpu/nvfp4_avx2.cpp@@ -22,24 +22,37 @@namespace strata::kernels::cpu {namespace {+// A compile-time table, not a constructor: this file is compiled for a wide ISA, and a namespace-scope object with a+// constructor runs at program start on EVERY CPU - the compiler vectorises the loop with AVX/FMA, and a CPU without them+// (Westmere, Sandy Bridge, ...) died with an illegal instruction before main (#2324). Powers of two: exact.+constexpr float pow2i(int e) {+ float r = 1.0f;+ for (; e > 0; --e) r *= 2.0f;+ for (; e < 0; ++e) r *= 0.5f;+ return r;+}struct Ue4m3Table { // ggml_ue4m3_to_fp32 (ggml-impl.h): halved for the doubled kvalues_fp4, 0x7F read as 0float v[256];- Ue4m3Table() {+ constexpr Ue4m3Table() : v() {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);+ const float raw = e == 0 ? (float) m * pow2i(-9) : (1.0f + (float) m / 8.0f) * pow2i(e - 7);v[x] = raw * 0.5f;}}};-const Ue4m3Table kUe4m3;+constexpr 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;-}();+// (read on first use, behind the CPU check: a static initialiser here would run on every CPU, see above)+inline int prefetch_distance() {+ static const int v = [] {+ const char* e = std::getenv("STRATA_NVFP4_PREFETCH");+ return e ? std::atoi(e) : 2048;+ }();+ return v;+}inline float h2f(uint16_t h) { return _mm_cvtss_f32(_mm_cvtph_ps(_mm_cvtsi32_si128((int) h))); }@@ -84,6 +97,7 @@ inline void row_dot(const uint8_t* row, const Acts& a, float* res) {__m256 acc[NT];for (int t = 0; t < NT; ++t) acc[t] = _mm256_setzero_ps();const block_nvfp4* x = (const block_nvfp4*) row;+ const int prefetch_ahead = prefetch_distance();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);diff --git a/src/kernels/cpu/nvfp4_avx512.cpp b/src/kernels/cpu/nvfp4_avx512.cppindex b5e2cbf02..3353c2ada 100644--- a/src/kernels/cpu/nvfp4_avx512.cpp+++ b/src/kernels/cpu/nvfp4_avx512.cpp@@ -22,25 +22,38 @@namespace strata::kernels::cpu {namespace {-struct Ue4m3Table {+// A compile-time table, not a constructor: this file is compiled for a wide ISA, and a namespace-scope object with a+// constructor runs at program start on EVERY CPU - the compiler vectorises the loop with AVX/FMA, and a CPU without them+// (Westmere, Sandy Bridge, ...) died with an illegal instruction before main (#2324). Powers of two: exact.+constexpr float pow2i(int e) {+ float r = 1.0f;+ for (; e > 0; --e) r *= 2.0f;+ for (; e < 0; ++e) r *= 0.5f;+ return r;+}+struct Ue4m3Table { // ggml_ue4m3_to_fp32 (ggml-impl.h): halved for the doubled kvalues_fp4, 0x7F read as 0float v[256];- Ue4m3Table() {+ constexpr Ue4m3Table() : v() {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);+ const float raw = e == 0 ? (float) m * pow2i(-9) : (1.0f + (float) m / 8.0f) * pow2i(e - 7);v[x] = raw * 0.5f;}}};-const Ue4m3Table kUe4m3;+constexpr Ue4m3Table kUe4m3;// software prefetch distance in bytes (STRATA_NVFP4_PREFETCH, 0 = off): the rows stream from DRAM through 4 KB// pages, where the hardware prefetchers stop at every page boundary; the IQ kernel's E-2 knob, same default-const int prefetch_ahead = [] {- const char* v = std::getenv("STRATA_NVFP4_PREFETCH");- return v ? std::atoi(v) : 2048;-}();+// (read on first use, behind the CPU check: a static initialiser here would run on every CPU, see above)+inline int prefetch_distance() {+ static const int v = [] {+ const char* e = std::getenv("STRATA_NVFP4_PREFETCH");+ return e ? std::atoi(e) : 2048;+ }();+ return v;+}inline float h2f(uint16_t h) { return _mm_cvtss_f32(_mm_cvtph_ps(_mm_cvtsi32_si128((int) h))); }@@ -92,6 +105,7 @@ inline void row_dot(const uint8_t* row, const Acts& a, float* res) {__m512 acc[NT];for (int t = 0; t < NT; ++t) acc[t] = _mm512_setzero_ps();const block_nvfp4* x = (const block_nvfp4*) row;+ const int prefetch_ahead = prefetch_distance();for (int ib = 0; ib < a.nb; ++ib) {if (prefetch_ahead > 0) _mm_prefetch((const char*) (x + ib) + prefetch_ahead, _MM_HINT_T0);const __m512i codes = codes64(x[ib].qs);diff --git a/tools/check_isa_guard.py b/tools/check_isa_guard.pyindex f5b8d0b9b..9e7a8e450 100644--- a/tools/check_isa_guard.py+++ b/tools/check_isa_guard.py@@ -39,6 +39,12 @@ WIDE_PUBLIC = {"act_quant_q8_1_avx2", "q8k_quant_avx2", "kq256_gu_rows", "kq256_rows", "bf16_rows_dot_multi",}WIDE_OBJECTS = ("expert.cpp", "iq_avx512.cpp", "iq_avx2.cpp", "q2_avx2.cpp", "kq_avx2.cpp", "nvfp4_avx512.cpp")+# Also compiled above the baseline (AVX2/FMA for nvfp4_avx2, AVX for the older CPUs' rows) and reached only behind a CPU+# check. STATIC INITIALISERS in any of these run on EVERY CPU at program start (#2324: nvfp4_avx2.cpp / nvfp4_avx512.cpp+# built a table in a constructor, the compiler used AVX/FMA for it, and a Westmere / Sandy Bridge Xeon died with an+# illegal instruction before main, with nothing in the log). The initialisers must not exist: constexpr tables, or+# function-local statics read behind the CPU check.+INIT_FREE_OBJECTS = WIDE_OBJECTS + ("nvfp4_avx2.cpp", "kq_avx1.cpp", "q2_avx1.cpp", "iq_avx1.cpp")# An instruction is "wide" if it is EVEX encoded (first byte 0x62 in 64-bit mode), or one of these VEX / legacy# extensions that an AVX2 CPU may lack. AVX-VNNI is VEX encoded (vpdpbusd, vpdpwssd ...).@@ -89,7 +95,7 @@ def run_elf(path):weak.add((obj, m.group(2)))dis = subprocess.run(["objdump", "-d", "-C", "-w", "--no-addresses", path], capture_output=True, text=True,check=True).stdout- obj, cur, wide, funcs = None, None, {}, 0+ obj, cur, wide, funcs, inits = None, None, {}, 0, {}for line in dis.splitlines():m = re.match(r"^(\S+\.o):\s+file format", line)if m:@@ -113,6 +119,11 @@ def run_elf(path):if is_wide(int(raw[0], 16), text[0], ops):e = wide.setdefault((obj, cur), [0, text[0]])e[0] += 1+ # a static initialiser of a wide-ISA / older-CPU object: any VEX / EVEX encoding is an illegal instruction on a+ # CPU without AVX (or FMA, AVX2), and the initialiser runs before main+ if cur.startswith("_GLOBAL__sub_I_") and obj and any(obj.endswith(o + ".o") for o in INIT_FREE_OBJECTS) and (int(raw[0], 16) in (0xC4, 0xC5, 0x62) or (text[0].startswith("v") and text[0] not in ("verr", "verw"))):+ e = inits.setdefault((obj, cur), [0, text[0]])+ e[0] += 1bad = []for (obj, name), (n, first) in sorted(wide.items(), key=lambda kv: (kv[0][0] or "", kv[0][1])):# .isra / .part / .cold clones belong to the function they were split from@@ -121,6 +132,9 @@ def run_elf(path):why = judge(root, obj, "", False, is_weak, True)if why:bad.append((name, n, first, why + (" [%s]" % obj if obj else "")))+ for (o, name), (n, first) in sorted(inits.items()):+ bad.append((name, n, first, "static initialiser holding AVX-class instructions in a wide-ISA / older-CPU object: it "+ "runs on every CPU at program start (#2324) [%s]" % o))return report(path, funcs, {k[1]: v for k, v in wide.items()}, bad)