diff --git a/include/strata/kernels/cpu/expert_layout.hpp b/include/strata/kernels/cpu/expert_layout.hpp index 126549ae..b98db74c 100644 --- a/include/strata/kernels/cpu/expert_layout.hpp +++ b/include/strata/kernels/cpu/expert_layout.hpp @@ -41,6 +41,11 @@ struct ExpertLayout { /// Plan v0.3 P6: whether this CPU (and its OS) runs the AVX-512 kernels (F, BW, VL, VNNI, VBMI). Probed in a /// file compiled without AVX-512, so asking is safe everywhere; STRATA_FORCE_AVX2=1 answers no (for tests). bool cpu_avx512_ok(); +/// Whether this CPU (and its OS) runs the AVX2 kernels (AVX, AVX2, FMA, F16C): the floor of every expert kernel +/// (q2_avx2.cpp, iq_avx2.cpp, and ggml-cpu in the portable build). STRATA_FORCE_AVX2 does not change it. +bool cpu_avx2_ok(); +/// The CPU's brand string (CPUID 0x80000002..4), for messages; "unknown" when it has none. +std::string cpu_name(); /// Q2_0 GGUF rows / activation quantizer on the kernels this CPU has. 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); diff --git a/src/kernels/cpu/expert_layout.cpp b/src/kernels/cpu/expert_layout.cpp index ec78774c..65891c84 100644 --- a/src/kernels/cpu/expert_layout.cpp +++ b/src/kernels/cpu/expert_layout.cpp @@ -3,6 +3,8 @@ #include #include +#include +#include #if defined(_MSC_VER) #include #include @@ -51,6 +53,61 @@ bool cpu_avx512_ok() { return ok; } +bool cpu_avx2_ok() { + static const bool ok = [] { + unsigned r[4] = {0, 0, 0, 0}; + auto cpuid = [&](unsigned leaf, unsigned sub) { +#if defined(_MSC_VER) + int x[4]; + __cpuidex(x, (int) leaf, (int) sub); + for (int i = 0; i < 4; ++i) r[i] = (unsigned) x[i]; +#else + __cpuid_count(leaf, sub, r[0], r[1], r[2], r[3]); +#endif + }; + cpuid(0, 0); + if (r[0] < 7) return false; + cpuid(1, 0); + const unsigned ecx1 = r[2]; + // FMA (12), OSXSAVE (27), AVX (28), F16C (29) + if (!((ecx1 >> 12) & 1u) || !((ecx1 >> 27) & 1u) || !((ecx1 >> 28) & 1u) || !((ecx1 >> 29) & 1u)) return false; +#if defined(_MSC_VER) + const unsigned long long xcr0 = _xgetbv(0); +#else + unsigned lo = 0, hi = 0; + __asm__ volatile("xgetbv" : "=a"(lo), "=d"(hi) : "c"(0)); + const unsigned long long xcr0 = ((unsigned long long) hi << 32) | lo; +#endif + if ((xcr0 & 0x6) != 0x6) return false; // the OS saves the SSE and AVX state + cpuid(7, 0); + return ((r[1] >> 5) & 1u) != 0; // AVX2 + }(); + return ok; +} + +std::string cpu_name() { + unsigned r[12] = {}; +#if defined(_MSC_VER) + int x[4]; + __cpuid(x, (int) 0x80000000u); + if ((unsigned) x[0] < 0x80000004u) return "unknown"; + for (unsigned i = 0; i < 3; ++i) { + __cpuid(x, (int) (0x80000002u + i)); + for (int j = 0; j < 4; ++j) r[i * 4 + j] = (unsigned) x[j]; + } +#else + unsigned a = 0, b = 0, c = 0, d = 0; + __cpuid(0x80000000u, a, b, c, d); + if (a < 0x80000004u) return "unknown"; + for (unsigned i = 0; i < 3; ++i) __cpuid(0x80000002u + i, r[i * 4], r[i * 4 + 1], r[i * 4 + 2], r[i * 4 + 3]); +#endif + char s[49] = {}; + std::memcpy(s, r, 48); + std::string name(s); + const size_t b0 = name.find_first_not_of(' '), b1 = name.find_last_not_of(' '); + return b0 == std::string::npos ? std::string("unknown") : name.substr(b0, b1 - b0 + 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/program/generate.cpp b/src/program/generate.cpp index 15aaffce..64adda3b 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -1501,6 +1501,15 @@ int main(int argc, char** argv) { // compiled `/arch:AVX512`, so on a CPU without those features it does not fail - it executes an illegal // instruction at some unpredictable token. Refusing at second zero is the whole point of P2.S3's check. strata::kernels::cpu::expert_set_oracle_q8_0(o.cpu_oracle_q8_0); + // ... and nothing runs on a CPU without AVX2: every CPU expert kernel is AVX2 at least (the AVX-512 ones are + // chosen above it), and so is ggml-cpu in the release build, which the native pack's layout load initializes + // next. Refused here, by name, rather than an illegal instruction in the first expert. + if (!strata::kernels::cpu::cpu_avx2_ok()) { + std::fprintf(stderr, "strata generate: this CPU (%s) does not support AVX2 with FMA and F16C, which every CPU " + "expert kernel needs; Strata runs on Intel Haswell (2013), AMD Zen (2017) or newer\n", + strata::kernels::cpu::cpu_name().c_str()); + return 2; + } std::string err; if (!o.native_head_gguf.empty() && !o.stream_token) {