okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
0a9381336
main
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 ) 30int a [4 ]; 31__cpuidex (a , (int )leaf , (int )sub ); 32for (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 ) 39return _xgetbv (0 ); 40#else 41unsigned eax ,edx ; 42 __asm__volatile ("xgetbv" :"=a" (eax ),"=d" (edx ) :"c" (0 )); 43return ((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 () { 50const char * f = std::getenv ("STRATA_FORCE_ISA" ); 51if (f == nullptr || * f == 0 )return 3 ; 52if (!std::strcmp (f ,"avx2" ))return 2 ; 53if (!std::strcmp (f ,"avx" ))return 1 ; 54if (!std::strcmp (f ,"sse" ))return 0 ; 55 std::fprintf (stderr ,"strata: STRATA_FORCE_ISA=%s is not avx2, avx or sse; ignored\n" ,f ); 56return 3 ; 57} 58 59Caps probe () { 60Caps c ; 61#ifdef STRATA_QUANT_X86 62unsigned r [4 ]= {0 ,0 ,0 ,0 }; 63cpuid (0 ,0 ,r ); 64const unsigned maxleaf = r [0 ]; 65cpuid (1 ,0 ,r ); 66const unsigned ecx1 = r [2 ]; 67c .sse42 = (ecx1 >>20 )& 1 ; 68const bool osxsave = (ecx1 >>27 )& 1 ; 69const unsigned long long xcr0 = osxsave ?xgetbv0 () :0 ; 70const bool ymm = (xcr0 & 6 )== 6 ; 71const bool zmm = ymm && (xcr0 & 0xE0 )== 0xE0 ; 72c .avx = ymm && ((ecx1 >>28 )& 1 ); 73const bool fma = (ecx1 >>12 )& 1 ,f16c = (ecx1 >>29 )& 1 ; 74if (maxleaf >=7 ) { 75cpuid (7 ,0 ,r ); 76const unsigned ebx = r [1 ],ecx = r [2 ]; 77c .avx2 = c .avx && fma && f16c && ((ebx >>5 )& 1 ); 78c .bmi2 = (ebx >>8 )& 1 ; 79c .avx512 = zmm && ((ebx >>16 )& 1 )&& ((ebx >>17 )& 1 )&& ((ebx >>30 )& 1 )&& ((ebx >>31 )& 1 );// F, DQ, BW, VL 80c .avx512_vnni = c .avx512 && ((ecx >>11 )& 1 ); 81cpuid (7 ,1 ,r ); 82c .avx_vnni = c .avx2 && ((r [0 ] >>4 )& 1 ); 83 } 84const int cap = isa_cap (); 85if (cap < 3 )c .avx512 = c .avx512_vnni = false; 86if (cap < 2 )c .avx2 = c .avx_vnni = c .bmi2 = false; 87if (cap < 1 )c .avx = false; 88#elif defined(STRATA_QUANT_ARM64 ) 89c .neon = true; 90#if defined(__linux__ ) 91const unsigned long hw = getauxval (AT_HWCAP ); 92c .dotprod = (hw >>20 )& 1 ;// HWCAP_ASIMDDP 93const unsigned long hw2 = getauxval (AT_HWCAP2 ); 94c .i8mm = (hw2 >>13 )& 1 ;// HWCAP2_I8MM 95#elif defined(__APPLE__ ) 96int v = 0 ; 97size_t sz = sizeof v ; 98c .dotprod = sysctlbyname ("hw.optional.arm.FEAT_DotProd" ,& v ,& sz ,nullptr ,0 )== 0 && v ; 99v = 0 ; 100sz = sizeof v ; 101c .i8mm = sysctlbyname ("hw.optional.arm.FEAT_I8MM" ,& v ,& sz ,nullptr ,0 )== 0 && v ; 102#endif 103#endif 104return c ; 105} 106 107}// namespace 108 109const Caps & caps ()noexcept { 110static const Caps c = probe (); 111return c ; 112} 113 114Isa best_isa ()noexcept { 115const Caps & c = caps (); 116if (c .avx512 && c .avx512_vnni )return Isa ::Avx512 ; 117if (c .avx2 )return Isa ::Avx2 ; 118if (c .avx )return Isa ::Avx1 ; 119if (c .neon && c .dotprod )return Isa ::Neon ;// the tier is ggml-cpu's dotprod flavour (the i8mm two-row code is not in it) 120return Isa ::Scalar ; 121} 122 123Arith default_arith ()noexcept { 124const Caps & c = caps (); 125if (c .neon )return c .dotprod ?Arith ::Neon :Arith ::Generic ; 126if (c .avx2 )return Arith ::Avx2 ; 127if (c .avx )return Arith ::Avx ; 128return Arith ::Generic ; 129} 130 131bool isa_ok (Isa i )noexcept { 132const Caps & c = caps (); 133switch (i ) { 134case Isa ::Scalar :return true; 135case Isa ::Neon :return c .neon && c .dotprod ; 136case Isa ::Avx1 :return c .avx ; 137case Isa ::Avx2 :return c .avx2 ; 138case Isa ::Avx512 :return c .avx512 && c .avx512_vnni ; 139 } 140return false; 141} 142 143const char * isa_name (Isa i )noexcept { 144switch (i ) { 145case Isa ::Scalar :return "scalar" ; 146case Isa ::Neon :return "neon" ; 147case Isa ::Avx1 :return "avx" ; 148case Isa ::Avx2 :return "avx2" ; 149case Isa ::Avx512 :return "avx512" ; 150 } 151return "?" ; 152} 153 154const char * arith_name (Arith a )noexcept { 155switch (a ) { 156case Arith ::Generic :return "generic" ; 157case Arith ::Avx :return "avx" ; 158case Arith ::Avx2 :return "avx2" ; 159case Arith ::Neon :return "neon" ; 160 } 161return "?" ; 162} 163 164}// namespace strata::quant