Skip to content

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: keep the AVX-512 probe and the "supported?" helpers out of the wide-ISA files (#795)

Niko1221680d499291b3
diff --git a/src/kernels/cpu/expert.cpp b/src/kernels/cpu/expert.cpp
index 045e3d58a..2436bbc96 100644
--- a/src/kernels/cpu/expert.cpp
+++ b/src/kernels/cpu/expert.cpp
@@ -21,8 +21,6 @@
namespace strata::kernels::cpu {
namespace {
-std::atomic<bool> oracle_q8_0{false};
-
/// fp16 -> fp32, written out rather than using `_cvtsh_ss`, because the F16C intrinsic's behaviour on
/// subnormals is the one place the two can differ and the scales in this artifact are small.
inline float h2f(const uint8_t* p) {
@@ -338,53 +336,10 @@ void expert_oracle_q8_0(const uint8_t* blob, const ActQ& a1, float* out, ExpertS
} // namespace
-const char* CpuFeatures::reason() const {
- if (usable()) return "ok";
- // Named individually: "AVX-512 not supported" sends a user looking for a new CPU when the machine may have
- // AVX-512F and be missing only VNNI, which is a much narrower and more explicable gap.
- static char buf[160];
- std::snprintf(buf, sizeof buf, "missing %s%s%s%s%s", avx512f ? "" : "AVX512F ",
- avx512bw ? "" : "AVX512BW ", avx512vl ? "" : "AVX512VL ",
- avx512_vnni ? "" : "AVX512-VNNI ", avx512_vbmi ? "" : "AVX512-VBMI");
- return buf;
-}
-
-CpuFeatures cpu_features() {
- CpuFeatures f;
- int reg[4] = {0, 0, 0, 0};
-#if defined(_MSC_VER)
- __cpuid(reg, 0);
- if (reg[0] < 7) return f;
- __cpuidex(reg, 7, 0);
-#else
- unsigned r[4] = {0, 0, 0, 0};
- __cpuid_count(0, 0, r[0], r[1], r[2], r[3]);
- if (r[0] < 7) return f;
- __cpuid_count(7, 0, r[0], r[1], r[2], r[3]);
- for (int i = 0; i < 4; ++i) reg[i] = (int) r[i];
-#endif
- const unsigned ebx = (unsigned) reg[1], ecx = (unsigned) reg[2];
- f.avx512f = (ebx >> 16) & 1u;
- f.avx512bw = (ebx >> 30) & 1u;
- f.avx512vl = (ebx >> 31) & 1u;
- f.avx512_vnni = (ecx >> 11) & 1u;
- f.avx512_vbmi = (ecx >> 1) & 1u;
- return f;
-}
-
-void cpu_require_expert_support() {
- const CpuFeatures f = cpu_features();
- if (f.usable()) return;
- std::fprintf(stderr,
- "strata: this CPU cannot run the expert kernel: %s.\n"
- " The engine needs AVX512-VNNI and AVX512-VBMI (Intel Ice Lake / AMD Zen 4 or newer).\n"
- " The scalar fallback exists for tests only and is far too slow to decode with.\n",
- f.reason());
- std::exit(1);
-}
-
+// CpuFeatures::reason, cpu_features, cpu_require_expert_support and the oracle flag live in expert_layout.cpp: this
+// file is compiled for AVX-512, and the code that finds out whether the CPU has AVX-512 must not be in it (#795).
void act_quant_q8_1(const float* x, int n, ActQ& a) {
- if (oracle_q8_0.load(std::memory_order_relaxed)) {
+ if (expert_oracle_q8_0_enabled()) {
quantize_oracle_q8_0(x, n, a);
return;
}
@@ -450,17 +405,13 @@ void act_quant_q8_1(const float* x, int n, ActQ& a) {
}
}
-void expert_set_oracle_q8_0(bool enabled) {
- oracle_q8_0.store(enabled, std::memory_order_relaxed);
-}
-
void s2_expert_vnni(const uint8_t* blob, const float* x, float* out, ExpertScratch& ws) {
act_quant_q8_1(x, H, ws.a1);
s2_expert_vnni_q(blob, ws.a1, out, ws);
}
void s2_expert_vnni_q(const uint8_t* blob, const ActQ& a1, float* out, ExpertScratch& ws) {
- if (oracle_q8_0.load(std::memory_order_relaxed)) {
+ if (expert_oracle_q8_0_enabled()) {
expert_oracle_q8_0(blob, a1, out, ws);
return;
}
@@ -479,7 +430,6 @@ void s2_expert_vnni_q(const uint8_t* blob, const ActQ& a1, float* out, ExpertScr
blob + O_D_SCALES + (size_t) r * SC_D * 2, ws.a2, SC_D);
}
-bool expert_oracle_q8_0_enabled() { return oracle_q8_0.load(std::memory_order_relaxed); }
void s2_expert_gu_rows(const uint8_t* blob, const ActQ& a1, float* ff, int r0, int r1) {
for (int r = r0; r < r1; ++r) {
@@ -610,7 +560,7 @@ void q2_0_gguf_rows_multi(const uint8_t* w, size_t row_bytes, int nblocks, const
void s2_expert_vnni_multi(const uint8_t* blob, const ActQ* const* a1, int n_tokens, float* const* out,
ExpertScratchMulti& ws) {
- if (oracle_q8_0.load(std::memory_order_relaxed) || n_tokens < 1 || n_tokens > MAXT) {
+ if (expert_oracle_q8_0_enabled() || n_tokens < 1 || n_tokens > MAXT) {
for (int t = 0; t < n_tokens; ++t) s2_expert_vnni_q(blob, *a1[t], out[t], ws.single);
return;
}
diff --git a/src/kernels/cpu/expert_layout.cpp b/src/kernels/cpu/expert_layout.cpp
index cc218f272..4e13167a0 100644
--- a/src/kernels/cpu/expert_layout.cpp
+++ b/src/kernels/cpu/expert_layout.cpp
@@ -1,6 +1,7 @@
// src/kernels/cpu/expert_layout.cpp - plan v0.3 P6: the per-layer expert table. See the header.
#include "strata/kernels/cpu/expert_layout.hpp"
+#include <atomic>
#include <cstdio>
#include <cstdlib>
#include <cstring>
@@ -286,6 +287,64 @@ std::string cpu_name() {
return b0 == std::string::npos ? std::string("unknown") : name.substr(b0, b1 - b0 + 1);
}
+// ---- the AVX-512 probe and the oracle flag (#795) ----
+//
+// expert.cpp and iq_avx512.cpp are compiled for AVX-512, and a TU compiled that way may use AVX-512 in ANY of its
+// code. The code that decides whether this CPU has AVX-512 - and the startup flag every CPU, AVX2-only ones
+// included, runs before that decision - therefore lives here, in a file compiled for the x86-64 baseline.
+namespace {
+std::atomic<bool> g_oracle_q8_0{false};
+}
+
+void expert_set_oracle_q8_0(bool enabled) { g_oracle_q8_0.store(enabled, std::memory_order_relaxed); }
+
+bool expert_oracle_q8_0_enabled() { return g_oracle_q8_0.load(std::memory_order_relaxed); }
+
+const char* CpuFeatures::reason() const {
+ if (usable()) return "ok";
+ // Named individually: "AVX-512 not supported" sends a user looking for a new CPU when the machine may have
+ // AVX-512F and be missing only VNNI, which is a much narrower and more explicable gap.
+ static char buf[160];
+ std::snprintf(buf, sizeof buf, "missing %s%s%s%s%s", avx512f ? "" : "AVX512F ",
+ avx512bw ? "" : "AVX512BW ", avx512vl ? "" : "AVX512VL ",
+ avx512_vnni ? "" : "AVX512-VNNI ", avx512_vbmi ? "" : "AVX512-VBMI");
+ return buf;
+}
+
+CpuFeatures cpu_features() {
+ CpuFeatures f;
+ int reg[4] = {0, 0, 0, 0};
+#if defined(_MSC_VER)
+ __cpuid(reg, 0);
+ if (reg[0] < 7) return f;
+ __cpuidex(reg, 7, 0);
+#else
+ unsigned r[4] = {0, 0, 0, 0};
+ __cpuid_count(0, 0, r[0], r[1], r[2], r[3]);
+ if (r[0] < 7) return f;
+ __cpuid_count(7, 0, r[0], r[1], r[2], r[3]);
+ for (int i = 0; i < 4; ++i) reg[i] = (int) r[i];
+#endif
+ const unsigned ebx = (unsigned) reg[1], ecx = (unsigned) reg[2];
+ f.avx512f = (ebx >> 16) & 1u;
+ f.avx512bw = (ebx >> 30) & 1u;
+ f.avx512vl = (ebx >> 31) & 1u;
+ f.avx512_vnni = (ecx >> 11) & 1u;
+ f.avx512_vbmi = (ecx >> 1) & 1u;
+ return f;
+}
+
+void cpu_require_expert_support() {
+ const CpuFeatures f = cpu_features();
+ if (f.usable()) return;
+ std::fprintf(stderr,
+ "strata: this CPU cannot run the expert kernel: %s.\n"
+ " The engine needs AVX512-VNNI and AVX512-VBMI (Intel Ice Lake / AMD Zen 4 or newer).\n"
+ " The scalar fallback exists for tests only and is far too slow to decode with.\n",
+ f.reason());
+ std::exit(1);
+}
+
void q2_rows_any(const uint8_t* w, size_t row_bytes, int nblocks, const ActQ* const* a, int nt, float* const* out,
int r0, int r1) {
if (cpu_avx512_ok()) q2_0_gguf_rows_multi(w, row_bytes, nblocks, a, nt, out, r0, r1);
diff --git a/src/kernels/cpu/iq_avx2.cpp b/src/kernels/cpu/iq_avx2.cpp
index ca722a582..2884fcde1 100644
--- a/src/kernels/cpu/iq_avx2.cpp
+++ b/src/kernels/cpu/iq_avx2.cpp
@@ -453,10 +453,6 @@ void q8k_quant_avx2(const float* x, void* vy, int64_t k) {
}
}
-bool iq256_supported(int type) noexcept {
- return type == 16 || type == 17 || type == 18 || type == 21 || type == 22 || type == 23;
-}
-
int iq256_variant() noexcept {
// STRATA_IQ256_GATHER=0/1 for every core; unset, where the calling thread's core gathers faster (per thread: a
// hybrid CPU's pool runs on P- and E-cores). AVX-VNNI where cpu_avxvnni_ok(). The same bits either way.
diff --git a/src/kernels/cpu/iq_avx512.cpp b/src/kernels/cpu/iq_avx512.cpp
index 26d2c5a71..e21a4d431 100644
--- a/src/kernels/cpu/iq_avx512.cpp
+++ b/src/kernels/cpu/iq_avx512.cpp
@@ -242,10 +242,6 @@ void dot_rows_nt(int nt, const uint8_t* w, size_t row_bytes, int n, const void*
} // namespace
-bool iq512_supported(int type) noexcept {
- return type == 16 || type == 17 || type == 18 || type == 21 || type == 22;
-}
-
void iq512_gu_rows(int type, 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) {
switch (type) {
diff --git a/src/kernels/cpu/kq_avx2.cpp b/src/kernels/cpu/kq_avx2.cpp
index a34731198..f72287326 100644
--- a/src/kernels/cpu/kq_avx2.cpp
+++ b/src/kernels/cpu/kq_avx2.cpp
@@ -199,8 +199,6 @@ void dot_rows(int type, const uint8_t* row, int n, const void* const* act, int n
} // namespace
-bool kq256_supported(int type) noexcept { return type == 12 || type == 7 || type == 8; }
-
void bf16_rows_dot_multi(const uint16_t* w, int rows, int cols, const float* x, int nt, float* out) {
// each row is read once for all `nt` (<= 8) tokens: the router (2.6 MB per layer) streams once per prediction
for (int r = 0; r < rows; ++r) {
diff --git a/src/kernels/cpu/native_expert.cpp b/src/kernels/cpu/native_expert.cpp
index 066afe0d4..8baead060 100644
--- a/src/kernels/cpu/native_expert.cpp
+++ b/src/kernels/cpu/native_expert.cpp
@@ -29,6 +29,20 @@ void init_once() {
bool native_experts_available() noexcept { return true; }
+// The "is this format handled by that kernel" questions, answered here and not next to the kernels: iq_avx512.cpp is
+// compiled for AVX-512 and iq_avx2.cpp / kq_avx2.cpp for AVX2, and native_gu_rows asks these on every CPU before it
+// has checked what the CPU can run. A function in a wide-ISA file may use that ISA anywhere in its body, so an
+// answer that is only a comparison must not come from one (#795).
+bool iq512_supported(int type) noexcept {
+ return type == 16 || type == 17 || type == 18 || type == 21 || type == 22;
+}
+
+bool iq256_supported(int type) noexcept {
+ return type == 16 || type == 17 || type == 18 || type == 21 || type == 22 || type == 23;
+}
+
+bool kq256_supported(int type) noexcept { return type == 12 || type == 7 || type == 8; }
+
bool native_fmt(int gu_type, int d_type, int64_t n_embd, int64_t n_ff, NativeFmt& f, std::string& err) {
init_once();
const ggml_type_traits_cpu* tg = traits(gu_type);