okami.mom/strata

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

Niko1221Merge f-own-3c 971bff01a into f-p4-int870ea9763

main
5.1 KiB141 linesraw
1// tools/vision/own/cpu_caps.h - the instruction-set probe and the 16-bit float helpers of the CPU tower.
2//
3// TODO(0.2.15): use strata_quant.  This is a stand-in with the same names as the OWN-2A layer (quant::caps(), best_isa(), isa_ok(), the STRATA_FORCE_ISA cap,
4// the fp16 helpers), written here because f-own-3v is not based on f-own-2a; when the branches meet the file is deleted and the includes point at
5// include/strata/quant/.
6#pragma once
7
8#include <cstdint>
9#include <cstdlib>
10#include <cstring>
11#include <string>
12
13#if defined(_MSC_VER) && (defined(__x86_64__) || defined(_M_X64))
14#include <immintrin.h>
15#include <intrin.h>
16#endif
17
18namespace strata_vit {
19namespace quant {
20
21// ranked: an x86 tier includes every lower one (STRATA_FORCE_ISA caps by rank).  Neon: AArch64 only (baseline Advanced SIMD), outside the x86 ranking.
22enum class Isa { Scalar = 0, Sse41 = 1, Avx = 2, Avx2 = 3, Avx512 = 4, Neon = 5 };
23
24struct Caps {
25    bool sse41 = false, avx = false;   // avx: the CPU has it and the OS saves the ymm state
26    bool avx2 = false, fma = false, f16c = false, avx512 = false;   // avx512: F, BW, DQ, VL (what the tower's kernels use) and the OS saves the state
27};
28
29#if defined(__x86_64__) || defined(_M_X64)
30#if defined(_MSC_VER)
31inline Caps probe() {
32    Caps c;
33    int r[4];
34    __cpuid(r, 0);
35    const int n = r[0];
36    if (n < 7) return c;
37    __cpuid(r, 1);
38    const bool osxsave = (r[2] >> 27) & 1, fma = (r[2] >> 12) & 1, f16c = (r[2] >> 29) & 1, avx = (r[2] >> 28) & 1;
39    c.sse41 = (r[2] >> 19) & 1;
40    unsigned long long xcr = osxsave ? _xgetbv(0) : 0;
41    const bool ymm = (xcr & 6) == 6, zmm = (xcr & 0xe6) == 0xe6;
42    c.avx = ymm && avx;
43    __cpuidex(r, 7, 0);
44    c.avx2 = ymm && ((r[1] >> 5) & 1);
45    c.fma = ymm && fma;
46    c.f16c = ymm && f16c;
47    c.avx512 = zmm && ((r[1] >> 16) & 1) && ((r[1] >> 17) & 1) && ((r[1] >> 30) & 1) && ((r[1] >> 31) & 1);
48    return c;
49}
50#else
51inline Caps probe() {
52    Caps c;
53    __builtin_cpu_init();
54    c.sse41 = __builtin_cpu_supports("sse4.1");
55    c.avx = __builtin_cpu_supports("avx");      // libgcc tests the OS ymm state for it
56    c.avx2 = __builtin_cpu_supports("avx2");
57    c.fma = __builtin_cpu_supports("fma");
58    c.f16c = __builtin_cpu_supports("f16c");
59    c.avx512 = __builtin_cpu_supports("avx512f") && __builtin_cpu_supports("avx512bw") && __builtin_cpu_supports("avx512dq") && __builtin_cpu_supports("avx512vl");
60    return c;
61}
62#endif
63#else
64inline Caps probe() { return Caps(); }
65#endif
66
67inline const Caps& caps() {
68    static const Caps c = probe();
69    return c;
70}
71
72// the same two questions for any feature set (the tests give the sets of the CPU classes; the program asks caps())
73inline bool isa_ok_for(const Caps& c, Isa i) {
74    if (i == Isa::Avx512) return c.avx512 && c.avx2 && c.fma;
75    if (i == Isa::Avx2) return c.avx2 && c.fma && c.f16c;
76    if (i == Isa::Avx) return c.avx && c.sse41;
77    if (i == Isa::Sse41) return c.sse41;
78#if defined(__aarch64__) || defined(_M_ARM64)
79    if (i == Isa::Neon) return true;             // part of every AArch64 CPU
80#else
81    if (i == Isa::Neon) return false;
82#endif
83    return true;
84}
85
86inline bool isa_ok(Isa i) { return isa_ok_for(caps(), i); }
87
88// the best tier a CPU with feature set `c` runs, capped by `force` (STRATA_FORCE_ISA's value, may be null): scalar|sse41|avx|avx2|avx512 (on AArch64: neon, or scalar)
89inline Isa best_isa_for(const Caps& c, const char* force) {
90#if defined(__aarch64__) || defined(_M_ARM64)
91    (void) c;
92    if (force && std::string(force) == "scalar") return Isa::Scalar;
93    return Isa::Neon;
94#else
95    Isa best = isa_ok_for(c, Isa::Avx512) ? Isa::Avx512 : isa_ok_for(c, Isa::Avx2) ? Isa::Avx2 : isa_ok_for(c, Isa::Avx) ? Isa::Avx : isa_ok_for(c, Isa::Sse41) ? Isa::Sse41 : Isa::Scalar;
96    if (force) {
97        const std::string s(force);
98        const Isa cap = s == "scalar" ? Isa::Scalar : s == "sse41" ? Isa::Sse41 : s == "avx" ? Isa::Avx : s == "avx2" ? Isa::Avx2 : Isa::Avx512;
99        if ((int) cap < (int) best) best = cap;
100    }
101    return best;
102#endif
103}
104
105// the best tier this CPU runs, capped by STRATA_FORCE_ISA
106inline Isa best_isa() { return best_isa_for(caps(), std::getenv("STRATA_FORCE_ISA")); }
107
108inline const char* isa_name(Isa i) {
109    return i == Isa::Neon ? "neon" : i == Isa::Avx512 ? "avx512" : i == Isa::Avx2 ? "avx2" : i == Isa::Avx ? "avx" : i == Isa::Sse41 ? "sse41" : "scalar";
110}
111
112// ---- 16-bit floats (bit exact, no hardware needed) ----
113inline float bf16_to_f32(uint16_t h) {
114    const uint32_t u = (uint32_t) h << 16;
115    float f;
116    std::memcpy(&f, &u, 4);
117    return f;
118}
119
120inline float f16_to_f32(uint16_t h) {
121    const uint32_t sign = (uint32_t) (h & 0x8000) << 16;
122    uint32_t e = (h >> 10) & 0x1f, m = h & 0x3ff, u;
123    if (e == 0) {
124        if (m == 0) u = sign;
125        else {   // subnormal: renormalise
126            e = 127 - 15 + 1;
127            while (!(m & 0x400)) { m <<= 1; --e; }
128            u = sign | (e << 23) | ((m & 0x3ff) << 13);
129        }
130    } else if (e == 31) {
131        u = sign | 0x7f800000u | (m << 13);
132    } else {
133        u = sign | ((e + 127 - 15) << 23) | (m << 13);
134    }
135    float f;
136    std::memcpy(&f, &u, 4);
137    return f;
138}
139
140}  // namespace quant
141}  // namespace strata_vit