okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-own-2n (ead5a32ae) into f-own-int: the Arm NEON tier of the quant layer0a9381336

main
4.8 KiB164 linesraw
1// src/quant/isa.cpp - the CPU probe of the quant layer (own-stack phase 2).  No ggml.
2#include "strata/quant/isa.hpp"
3
4#include <cstdio>
5#include <cstdlib>
6#include <cstring>
7
8#if defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86)
9#define STRATA_QUANT_X86 1
10#if defined(_MSC_VER)
11#include <intrin.h>
12#else
13#include <cpuid.h>
14#endif
15#elif defined(__aarch64__) || defined(_M_ARM64)
16#define STRATA_QUANT_ARM64 1
17#if defined(__linux__)
18#include <sys/auxv.h>
19#elif defined(__APPLE__)
20#include <sys/sysctl.h>
21#endif
22#endif
23
24namespace strata::quant {
25namespace {
26
27#ifdef STRATA_QUANT_X86
28void cpuid(unsigned leaf, unsigned sub, unsigned r[4]) {
29#if defined(_MSC_VER)
30    int a[4];
31    __cpuidex(a, (int) leaf, (int) sub);
32    for (int i = 0; i < 4; ++i) r[i] = (unsigned) a[i];
33#else
34    __cpuid_count(leaf, sub, r[0], r[1], r[2], r[3]);
35#endif
36}
37unsigned long long xgetbv0() {
38#if defined(_MSC_VER)
39    return _xgetbv(0);
40#else
41    unsigned eax, edx;
42    __asm__ volatile("xgetbv" : "=a"(eax), "=d"(edx) : "c"(0));
43    return ((unsigned long long) edx << 32) | eax;
44#endif
45}
46#endif
47
48// STRATA_FORCE_ISA caps the probes like the pool's own (expert_layout.cpp): avx2 = no AVX-512, avx = AVX only, sse = SSE4.2 only.
49int isa_cap() {
50    const char* f = std::getenv("STRATA_FORCE_ISA");
51    if (f == nullptr || *f == 0) return 3;
52    if (!std::strcmp(f, "avx2")) return 2;
53    if (!std::strcmp(f, "avx")) return 1;
54    if (!std::strcmp(f, "sse")) return 0;
55    std::fprintf(stderr, "strata: STRATA_FORCE_ISA=%s is not avx2, avx or sse; ignored\n", f);
56    return 3;
57}
58
59Caps probe() {
60    Caps c;
61#ifdef STRATA_QUANT_X86
62    unsigned r[4] = {0, 0, 0, 0};
63    cpuid(0, 0, r);
64    const unsigned maxleaf = r[0];
65    cpuid(1, 0, r);
66    const unsigned ecx1 = r[2];
67    c.sse42 = (ecx1 >> 20) & 1;
68    const bool osxsave = (ecx1 >> 27) & 1;
69    const unsigned long long xcr0 = osxsave ? xgetbv0() : 0;
70    const bool ymm = (xcr0 & 6) == 6;
71    const bool zmm = ymm && (xcr0 & 0xE0) == 0xE0;
72    c.avx = ymm && ((ecx1 >> 28) & 1);
73    const bool fma = (ecx1 >> 12) & 1, f16c = (ecx1 >> 29) & 1;
74    if (maxleaf >= 7) {
75        cpuid(7, 0, r);
76        const unsigned ebx = r[1], ecx = r[2];
77        c.avx2 = c.avx && fma && f16c && ((ebx >> 5) & 1);
78        c.bmi2 = (ebx >> 8) & 1;
79        c.avx512 = zmm && ((ebx >> 16) & 1) && ((ebx >> 17) & 1) && ((ebx >> 30) & 1) && ((ebx >> 31) & 1);   // F, DQ, BW, VL
80        c.avx512_vnni = c.avx512 && ((ecx >> 11) & 1);
81        cpuid(7, 1, r);
82        c.avx_vnni = c.avx2 && ((r[0] >> 4) & 1);
83    }
84    const int cap = isa_cap();
85    if (cap < 3) c.avx512 = c.avx512_vnni = false;
86    if (cap < 2) c.avx2 = c.avx_vnni = c.bmi2 = false;
87    if (cap < 1) c.avx = false;
88#elif defined(STRATA_QUANT_ARM64)
89    c.neon = true;
90#if defined(__linux__)
91    const unsigned long hw = getauxval(AT_HWCAP);
92    c.dotprod = (hw >> 20) & 1;   // HWCAP_ASIMDDP
93    const unsigned long hw2 = getauxval(AT_HWCAP2);
94    c.i8mm = (hw2 >> 13) & 1;     // HWCAP2_I8MM
95#elif defined(__APPLE__)
96    int v = 0;
97    size_t sz = sizeof v;
98    c.dotprod = sysctlbyname("hw.optional.arm.FEAT_DotProd", &v, &sz, nullptr, 0) == 0 && v;
99    v = 0;
100    sz = sizeof v;
101    c.i8mm = sysctlbyname("hw.optional.arm.FEAT_I8MM", &v, &sz, nullptr, 0) == 0 && v;
102#endif
103#endif
104    return c;
105}
106
107}  // namespace
108
109const Caps& caps() noexcept {
110    static const Caps c = probe();
111    return c;
112}
113
114Isa best_isa() noexcept {
115    const Caps& c = caps();
116    if (c.avx512 && c.avx512_vnni) return Isa::Avx512;
117    if (c.avx2) return Isa::Avx2;
118    if (c.avx) return Isa::Avx1;
119    if (c.neon && c.dotprod) return Isa::Neon;   // the tier is ggml-cpu's dotprod flavour (the i8mm two-row code is not in it)
120    return Isa::Scalar;
121}
122
123Arith default_arith() noexcept {
124    const Caps& c = caps();
125    if (c.neon) return c.dotprod ? Arith::Neon : Arith::Generic;
126    if (c.avx2) return Arith::Avx2;
127    if (c.avx) return Arith::Avx;
128    return Arith::Generic;
129}
130
131bool isa_ok(Isa i) noexcept {
132    const Caps& c = caps();
133    switch (i) {
134        case Isa::Scalar: return true;
135        case Isa::Neon: return c.neon && c.dotprod;
136        case Isa::Avx1: return c.avx;
137        case Isa::Avx2: return c.avx2;
138        case Isa::Avx512: return c.avx512 && c.avx512_vnni;
139    }
140    return false;
141}
142
143const char* isa_name(Isa i) noexcept {
144    switch (i) {
145        case Isa::Scalar: return "scalar";
146        case Isa::Neon: return "neon";
147        case Isa::Avx1: return "avx";
148        case Isa::Avx2: return "avx2";
149        case Isa::Avx512: return "avx512";
150    }
151    return "?";
152}
153
154const char* arith_name(Arith a) noexcept {
155    switch (a) {
156        case Arith::Generic: return "generic";
157        case Arith::Avx: return "avx";
158        case Arith::Avx2: return "avx2";
159        case Arith::Neon: return "neon";
160    }
161    return "?";
162}
163
164}  // namespace strata::quant