char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
f470bc36b
char/aliceai-t5-moe
1#include "ggml-backend-impl.h" 2 3#if defined(__x86_64__ )|| (defined(_MSC_VER )&& defined(_M_AMD64 )) 4 5#ifdef _MSC_VER 6#include <intrin.h> 7#endif 8 9#include <cstring> 10#include <vector> 11#include <bitset> 12#include <array> 13#include <string> 14 15// ref: https://cdrdv2-public.intel.com/782156/325383-sdm-vol-2abcd.pdf 16struct cpuid_x86 { 17bool SSE3 (void ) {return f_1_ecx [0 ]; } 18bool PCLMULQDQ (void ) {return f_1_ecx [1 ]; } 19bool MONITOR (void ) {return f_1_ecx [3 ]; } 20bool SSSE3 (void ) {return f_1_ecx [9 ]; } 21bool FMA (void ) {return f_1_ecx [12 ]; } 22bool CMPXCHG16B (void ) {return f_1_ecx [13 ]; } 23bool SSE41 (void ) {return f_1_ecx [19 ]; } 24bool SSE42 (void ) {return f_1_ecx [20 ]; } 25bool MOVBE (void ) {return f_1_ecx [22 ]; } 26bool POPCNT (void ) {return f_1_ecx [23 ]; } 27bool AES (void ) {return f_1_ecx [25 ]; } 28bool XSAVE (void ) {return f_1_ecx [26 ]; } 29bool OSXSAVE (void ) {return f_1_ecx [27 ]; } 30bool AVX (void ) {return f_1_ecx [28 ]; } 31bool F16C (void ) {return f_1_ecx [29 ]; } 32bool RDRAND (void ) {return f_1_ecx [30 ]; } 33 34bool MSR (void ) {return f_1_edx [5 ]; } 35bool CX8 (void ) {return f_1_edx [8 ]; } 36bool SEP (void ) {return f_1_edx [11 ]; } 37bool CMOV (void ) {return f_1_edx [15 ]; } 38bool CLFSH (void ) {return f_1_edx [19 ]; } 39bool MMX (void ) {return f_1_edx [23 ]; } 40bool FXSR (void ) {return f_1_edx [24 ]; } 41bool SSE (void ) {return f_1_edx [25 ]; } 42bool SSE2 (void ) {return f_1_edx [26 ]; } 43 44bool FSGSBASE (void ) {return f_7_ebx [0 ]; } 45bool BMI1 (void ) {return f_7_ebx [3 ]; } 46bool HLE (void ) {return is_intel && f_7_ebx [4 ]; } 47bool AVX2 (void ) {return f_7_ebx [5 ]; } 48bool BMI2 (void ) {return f_7_ebx [8 ]; } 49bool ERMS (void ) {return f_7_ebx [9 ]; } 50bool INVPCID (void ) {return f_7_ebx [10 ]; } 51bool RTM (void ) {return is_intel && f_7_ebx [11 ]; } 52bool AVX512F (void ) {return f_7_ebx [16 ]; } 53bool AVX512DQ (void ) {return f_7_ebx [17 ]; } 54bool RDSEED (void ) {return f_7_ebx [18 ]; } 55bool ADX (void ) {return f_7_ebx [19 ]; } 56bool AVX512PF (void ) {return f_7_ebx [26 ]; } 57bool AVX512ER (void ) {return f_7_ebx [27 ]; } 58bool AVX512CD (void ) {return f_7_ebx [28 ]; } 59bool AVX512BW (void ) {return f_7_ebx [30 ]; } 60bool AVX512VL (void ) {return f_7_ebx [31 ]; } 61 62bool SHA (void ) {return f_7_ebx [29 ]; } 63 64bool PREFETCHWT1 (void ) {return f_7_ecx [0 ]; } 65 66bool LAHF (void ) {return f_81_ecx [0 ]; } 67bool LZCNT (void ) {return is_intel && f_81_ecx [5 ]; } 68bool ABM (void ) {return is_amd && f_81_ecx [5 ]; } 69bool SSE4a (void ) {return is_amd && f_81_ecx [6 ]; } 70bool XOP (void ) {return is_amd && f_81_ecx [11 ]; } 71bool TBM (void ) {return is_amd && f_81_ecx [21 ]; } 72 73bool SYSCALL (void ) {return is_intel && f_81_edx [11 ]; } 74bool MMXEXT (void ) {return is_amd && f_81_edx [22 ]; } 75bool RDTSCP (void ) {return is_intel && f_81_edx [27 ]; } 76bool _3DNOWEXT (void ) {return is_amd && f_81_edx [30 ]; } 77bool _3DNOW (void ) {return is_amd && f_81_edx [31 ]; } 78 79bool AVX512_VBMI (void ) {return f_7_ecx [1 ]; } 80bool AVX512_VNNI (void ) {return f_7_ecx [11 ]; } 81bool AVX512_FP16 (void ) {return f_7_edx [23 ]; } 82bool AVX512_BF16 (void ) {return f_7_1_eax [5 ]; } 83bool AVX_VNNI (void ) {return f_7_1_eax [4 ]; } 84 85bool AMX_TILE (void ) {return f_7_edx [24 ]; } 86bool AMX_INT8 (void ) {return f_7_edx [25 ]; } 87bool AMX_FP16 (void ) {return f_7_1_eax [21 ]; } 88bool AMX_BF16 (void ) {return f_7_edx [22 ]; } 89 90#ifdef _MSC_VER 91static void cpuid (int cpu_info [4 ],int eax ) { 92__cpuid (cpu_info ,eax ); 93 } 94static void cpuidex (int cpu_info [4 ],int eax ,int ecx ) { 95__cpuidex (cpu_info ,eax ,ecx ); 96 } 97#else 98static void cpuid (int cpu_info [4 ],int eax ) { 99 __asm__ __volatile__( 100"cpuid" 101 :"=a" (cpu_info [0 ]),"=b" (cpu_info [1 ]),"=c" (cpu_info [2 ]),"=d" (cpu_info [3 ]) 102 :"a" (eax ),"c" (0 )); 103 } 104static void cpuidex (int cpu_info [4 ],int eax ,int ecx ) { 105 __asm__ __volatile__( 106"cpuid" 107 :"=a" (cpu_info [0 ]),"=b" (cpu_info [1 ]),"=c" (cpu_info [2 ]),"=d" (cpu_info [3 ]) 108 :"a" (eax ),"c" (ecx )); 109 } 110#endif 111 112cpuid_x86 () { 113 std::array < int ,4 > cpui ; 114 std::vector < std::array < int ,4 >> data ; 115 116// calling __cpuid with 0x0 as the function_id argument 117// gets the number of the highest valid function ID. 118cpuid (cpui .data (),0 ); 119int n_ids = cpui [0 ]; 120 121for (int i = 0 ;i <=n_ids ;++ i ) { 122cpuidex (cpui .data (),i ,0 ); 123data .push_back (cpui ); 124 } 125 126// capture vendor string 127char vendor [0x20 ]= {}; 128* reinterpret_cast < int *> (vendor )= data [0 ][1 ]; 129* reinterpret_cast < int *> (vendor + 4 )= data [0 ][3 ]; 130* reinterpret_cast < int *> (vendor + 8 )= data [0 ][2 ]; 131this -> vendor = vendor ; 132if (this -> vendor == "GenuineIntel" ) { 133is_intel = true; 134 }else if (this -> vendor == "AuthenticAMD" ) { 135is_amd = true; 136 } 137 138// load bitset with flags for function 0x00000001 139if (n_ids >=1 ) { 140f_1_ecx = data [1 ][2 ]; 141f_1_edx = data [1 ][3 ]; 142 } 143 144// load bitset with flags for function 0x00000007 145if (n_ids >=7 ) { 146f_7_ebx = data [7 ][1 ]; 147f_7_ecx = data [7 ][2 ]; 148f_7_edx = data [7 ][3 ]; 149cpuidex (cpui .data (),7 ,1 ); 150f_7_1_eax = cpui [0 ]; 151 } 152 153// calling __cpuid with 0x80000000 as the function_id argument 154// gets the number of the highest valid extended ID. 155cpuid (cpui .data (),0x80000000 ); 156unsigned int n_ex_ids = cpui [0 ]; 157 158 std::vector < std::array < int ,4 >> ext_data ; 159for (unsigned int i = 0x80000000 ;i <=n_ex_ids ;++ i ) { 160cpuidex (cpui .data (),i ,0 ); 161ext_data .push_back (cpui ); 162 } 163 164// load bitset with flags for function 0x80000001 165if (n_ex_ids >=0x80000001 ) { 166f_81_ecx = ext_data [1 ][2 ]; 167f_81_edx = ext_data [1 ][3 ]; 168 } 169 170// interpret CPU brand string if reported 171char brand [0x40 ]= {}; 172if (n_ex_ids >=0x80000004 ) { 173 std::memcpy (brand ,ext_data [2 ].data (),sizeof (cpui )); 174 std::memcpy (brand + 16 ,ext_data [3 ].data (),sizeof (cpui )); 175 std::memcpy (brand + 32 ,ext_data [4 ].data (),sizeof (cpui )); 176this -> brand = brand ; 177 } 178 } 179 180bool is_intel = false; 181bool is_amd = false; 182 std::string vendor ; 183 std::string brand ; 184 std::bitset < 32 > f_1_ecx ; 185 std::bitset < 32 > f_1_edx ; 186 std::bitset < 32 > f_7_ebx ; 187 std::bitset < 32 > f_7_ecx ; 188 std::bitset < 32 > f_7_edx ; 189 std::bitset < 32 > f_7_1_eax ; 190 std::bitset < 32 > f_81_ecx ; 191 std::bitset < 32 > f_81_edx ; 192}; 193 194#if 0 195void test_x86_is () { 196cpuid_x86 is ; 197printf ("CPU Vendor: %s\n" ,is .vendor .c_str ()); 198printf ("Brand: %s\n" ,is .brand .c_str ()); 199printf ("is_intel: %d\n" ,is .is_intel ); 200printf ("is_amd: %d\n" ,is .is_amd ); 201printf ("sse3: %d\n" ,is .SSE3 ()); 202printf ("pclmulqdq: %d\n" ,is .PCLMULQDQ ()); 203printf ("ssse3: %d\n" ,is .SSSE3 ()); 204printf ("fma: %d\n" ,is .FMA ()); 205printf ("cmpxchg16b: %d\n" ,is .CMPXCHG16B ()); 206printf ("sse41: %d\n" ,is .SSE41 ()); 207printf ("sse42: %d\n" ,is .SSE42 ()); 208printf ("movbe: %d\n" ,is .MOVBE ()); 209printf ("popcnt: %d\n" ,is .POPCNT ()); 210printf ("aes: %d\n" ,is .AES ()); 211printf ("xsave: %d\n" ,is .XSAVE ()); 212printf ("osxsave: %d\n" ,is .OSXSAVE ()); 213printf ("avx: %d\n" ,is .AVX ()); 214printf ("f16c: %d\n" ,is .F16C ()); 215printf ("rdrand: %d\n" ,is .RDRAND ()); 216printf ("msr: %d\n" ,is .MSR ()); 217printf ("cx8: %d\n" ,is .CX8 ()); 218printf ("sep: %d\n" ,is .SEP ()); 219printf ("cmov: %d\n" ,is .CMOV ()); 220printf ("clflush: %d\n" ,is .CLFSH ()); 221printf ("mmx: %d\n" ,is .MMX ()); 222printf ("fxsr: %d\n" ,is .FXSR ()); 223printf ("sse: %d\n" ,is .SSE ()); 224printf ("sse2: %d\n" ,is .SSE2 ()); 225printf ("fsgsbase: %d\n" ,is .FSGSBASE ()); 226printf ("bmi1: %d\n" ,is .BMI1 ()); 227printf ("hle: %d\n" ,is .HLE ()); 228printf ("avx2: %d\n" ,is .AVX2 ()); 229printf ("bmi2: %d\n" ,is .BMI2 ()); 230printf ("erms: %d\n" ,is .ERMS ()); 231printf ("invpcid: %d\n" ,is .INVPCID ()); 232printf ("rtm: %d\n" ,is .RTM ()); 233printf ("avx512f: %d\n" ,is .AVX512F ()); 234printf ("rdseed: %d\n" ,is .RDSEED ()); 235printf ("adx: %d\n" ,is .ADX ()); 236printf ("avx512pf: %d\n" ,is .AVX512PF ()); 237printf ("avx512er: %d\n" ,is .AVX512ER ()); 238printf ("avx512cd: %d\n" ,is .AVX512CD ()); 239printf ("sha: %d\n" ,is .SHA ()); 240printf ("prefetchwt1: %d\n" ,is .PREFETCHWT1 ()); 241printf ("lahf: %d\n" ,is .LAHF ()); 242printf ("lzcnt: %d\n" ,is .LZCNT ()); 243printf ("abm: %d\n" ,is .ABM ()); 244printf ("sse4a: %d\n" ,is .SSE4a ()); 245printf ("xop: %d\n" ,is .XOP ()); 246printf ("tbm: %d\n" ,is .TBM ()); 247printf ("syscall: %d\n" ,is .SYSCALL ()); 248printf ("mmxext: %d\n" ,is .MMXEXT ()); 249printf ("rdtscp: %d\n" ,is .RDTSCP ()); 250printf ("3dnowext: %d\n" ,is ._3DNOWEXT ()); 251printf ("3dnow: %d\n" ,is ._3DNOW ()); 252printf ("avx512_vbmi: %d\n" ,is .AVX512_VBMI ()); 253printf ("avx512_vnni: %d\n" ,is .AVX512_VNNI ()); 254printf ("avx512_fp16: %d\n" ,is .AVX512_FP16 ()); 255printf ("avx512_bf16: %d\n" ,is .AVX512_BF16 ()); 256printf ("amx_tile: %d\n" ,is .AMX_TILE ()); 257printf ("amx_int8: %d\n" ,is .AMX_INT8 ()); 258printf ("amx_fp16: %d\n" ,is .AMX_FP16 ()); 259printf ("amx_bf16: %d\n" ,is .AMX_BF16 ()); 260} 261#endif 262 263static int ggml_backend_cpu_x86_score () { 264// FIXME: this does not check for OS support 265 266int score = 1 ; 267cpuid_x86 is ; 268 269#ifdef GGML_FMA 270if (!is .FMA ()) {return 0 ; } 271score += 1 ; 272#endif 273#ifdef GGML_F16C 274if (!is .F16C ()) {return 0 ; } 275score += 1 <<1 ; 276#endif 277#ifdef GGML_SSE42 278if (!is .SSE42 ()) {return 0 ; } 279score += 1 <<2 ; 280#endif 281#ifdef GGML_BMI2 282if (!is .BMI2 ()) {return 0 ; } 283score += 1 <<3 ; 284#endif 285#ifdef GGML_AVX 286if (!is .AVX ()) {return 0 ; } 287score += 1 <<4 ; 288#endif 289#ifdef GGML_AVX2 290if (!is .AVX2 ()) {return 0 ; } 291score += 1 <<5 ; 292#endif 293#ifdef GGML_AVX_VNNI 294if (!is .AVX_VNNI ()) {return 0 ; } 295score += 1 <<6 ; 296#endif 297#ifdef GGML_AVX512 298if (!is .AVX512F ()) {return 0 ; } 299if (!is .AVX512CD ()) {return 0 ; } 300if (!is .AVX512VL ()) {return 0 ; } 301if (!is .AVX512DQ ()) {return 0 ; } 302if (!is .AVX512BW ()) {return 0 ; } 303score += 1 <<7 ; 304#endif 305#ifdef GGML_AVX512_VBMI 306if (!is .AVX512_VBMI ()) {return 0 ; } 307score += 1 <<8 ; 308#endif 309#ifdef GGML_AVX512_BF16 310if (!is .AVX512_BF16 ()) {return 0 ; } 311score += 1 <<9 ; 312#endif 313#ifdef GGML_AVX512_VNNI 314if (!is .AVX512_VNNI ()) {return 0 ; } 315score += 1 <<10 ; 316#endif 317#ifdef GGML_AMX_INT8 318if (!is .AMX_INT8 ()) {return 0 ; } 319score += 1 <<11 ; 320#endif 321 322return score ; 323} 324 325GGML_BACKEND_DL_SCORE_IMPL (ggml_backend_cpu_x86_score ) 326 327#endif // defined(__x86_64__) || (defined(_MSC_VER) && defined(_M_AMD64))