okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
870ea9763
main
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 { 25bool sse41 = false,avx = false;// avx: the CPU has it and the OS saves the ymm state 26bool 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 () { 32Caps c ; 33int r [4 ]; 34__cpuid (r ,0 ); 35const int n = r [0 ]; 36if (n < 7 )return c ; 37__cpuid (r ,1 ); 38const bool osxsave = (r [2 ] >>27 )& 1 ,fma = (r [2 ] >>12 )& 1 ,f16c = (r [2 ] >>29 )& 1 ,avx = (r [2 ] >>28 )& 1 ; 39c .sse41 = (r [2 ] >>19 )& 1 ; 40unsigned long long xcr = osxsave ?_xgetbv (0 ) :0 ; 41const bool ymm = (xcr & 6 )== 6 ,zmm = (xcr & 0xe6 )== 0xe6 ; 42c .avx = ymm && avx ; 43__cpuidex (r ,7 ,0 ); 44c .avx2 = ymm && ((r [1 ] >>5 )& 1 ); 45c .fma = ymm && fma ; 46c .f16c = ymm && f16c ; 47c .avx512 = zmm && ((r [1 ] >>16 )& 1 )&& ((r [1 ] >>17 )& 1 )&& ((r [1 ] >>30 )& 1 )&& ((r [1 ] >>31 )& 1 ); 48return c ; 49} 50#else 51inline Caps probe () { 52Caps c ; 53__builtin_cpu_init (); 54c .sse41 = __builtin_cpu_supports ("sse4.1" ); 55c .avx = __builtin_cpu_supports ("avx" );// libgcc tests the OS ymm state for it 56c .avx2 = __builtin_cpu_supports ("avx2" ); 57c .fma = __builtin_cpu_supports ("fma" ); 58c .f16c = __builtin_cpu_supports ("f16c" ); 59c .avx512 = __builtin_cpu_supports ("avx512f" )&& __builtin_cpu_supports ("avx512bw" )&& __builtin_cpu_supports ("avx512dq" )&& __builtin_cpu_supports ("avx512vl" ); 60return c ; 61} 62#endif 63#else 64inline Caps probe () {return Caps (); } 65#endif 66 67inline const Caps & caps () { 68static const Caps c = probe (); 69return 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 ) { 74if (i == Isa ::Avx512 )return c .avx512 && c .avx2 && c .fma ; 75if (i == Isa ::Avx2 )return c .avx2 && c .fma && c .f16c ; 76if (i == Isa ::Avx )return c .avx && c .sse41 ; 77if (i == Isa ::Sse41 )return c .sse41 ; 78#if defined(__aarch64__ )|| defined(_M_ARM64 ) 79if (i == Isa ::Neon )return true;// part of every AArch64 CPU 80#else 81if (i == Isa ::Neon )return false; 82#endif 83return 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 ; 92if (force && std ::string (force )== "scalar" )return Isa ::Scalar ; 93return Isa ::Neon ; 94#else 95Isa 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 ; 96if (force ) { 97const std ::string s (force ); 98const Isa cap = s == "scalar" ?Isa ::Scalar :s == "sse41" ?Isa ::Sse41 :s == "avx" ?Isa ::Avx :s == "avx2" ?Isa ::Avx2 :Isa ::Avx512 ; 99if ((int )cap < (int )best )best = cap ; 100 } 101return 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 ) { 109return 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 ) { 114const uint32_t u = (uint32_t )h <<16 ; 115float f ; 116std ::memcpy (& f ,& u ,4 ); 117return f ; 118} 119 120inline float f16_to_f32 (uint16_t h ) { 121const uint32_t sign = (uint32_t ) (h & 0x8000 ) <<16 ; 122uint32_t e = (h >>10 )& 0x1f ,m = h & 0x3ff ,u ; 123if (e == 0 ) { 124if (m == 0 )u = sign ; 125else {// subnormal: renormalise 126e = 127 - 15 + 1 ; 127while (!(m & 0x400 )) {m <<=1 ;-- e ; } 128u = sign | (e <<23 ) | ((m & 0x3ff ) <<13 ); 129 } 130 }else if (e == 31 ) { 131u = sign |0x7f800000u | (m <<13 ); 132 }else { 133u = sign | ((e + 127 - 15 ) <<23 ) | (m <<13 ); 134 } 135float f ; 136std ::memcpy (& f ,& u ,4 ); 137return f ; 138} 139 140}// namespace quant 141}// namespace strata_vit