okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
0a9381336
main
1// src/quant/dispatch.cpp - kernel lookup: the best kernel of an arithmetic contract that this CPU can run. 2#include <algorithm> 3 4#include "registry.hpp" 5#include "strata/quant/act.hpp" 6#include "strata/quant/dot.hpp" 7 8namespace strata::quant { 9namespace { 10 11template < typename K ,typename T ,int N > 12int gather (const T (& tables )[N ],Type t ,Arith a ,const K ** out ,int cap ,Type K ::* field ) { 13int n = 0 ; 14for (const T & tb :tables ) 15for (int i = 0 ;i < tb .n ;++ i ) { 16const K & k = tb .v [i ]; 17if (k .*field == t && k .arith == a && n < cap )out [n ++ ]= & k ; 18 } 19// best (widest hardware) first 20 std::stable_sort (out ,out + n , [](const K * x ,const K * y ) {return (int )x -> isa > (int )y -> isa ; }); 21return n ; 22} 23 24constexpr int kMaxK = 16 ; 25 26int collect_dots (Type w ,Arith a ,const DotKernel ** out ,int cap ) { 27const DotTable tables []= { 28generic_dot_table (), 29#ifdef STRATA_QUANT_X86_TIERS 30avx_dot_table (),avx2_dot_table (),avx512_dot_table (), 31#endif 32#ifdef STRATA_QUANT_ARM_TIERS 33neon_dot_table (), 34#endif 35 }; 36return gather (tables ,w ,a ,out ,cap ,& DotKernel ::weight ); 37} 38 39int collect_acts (Type act ,Arith a ,const ActKernel ** out ,int cap ) { 40const ActTable tables []= { 41generic_act_table (), 42#ifdef STRATA_QUANT_X86_TIERS 43avx_act_table (),avx2_act_table (), 44#endif 45#ifdef STRATA_QUANT_ARM_TIERS 46neon_act_table (), 47#endif 48 }; 49return gather (tables ,act ,a ,out ,cap ,& ActKernel ::act ); 50} 51 52}// namespace 53 54int dot_kernels (Type w ,Arith a ,const DotKernel ** out ,int cap )noexcept {return collect_dots (w ,a ,out ,cap ); } 55 56const DotKernel * dot_kernel (Type w ,Arith a ,Isa max_isa )noexcept { 57const DotKernel * v [kMaxK ]; 58const int n = collect_dots (w ,a ,v ,kMaxK ); 59for (int i = 0 ;i < n ;++ i ) 60if ((int )v [i ]-> isa <= (int )max_isa && isa_ok (v [i ]-> isa ))return v [i ]; 61return nullptr ; 62} 63 64const DotKernel * dot_kernel (Type w ,Arith a )noexcept {return dot_kernel (w ,a ,best_isa ()); } 65 66const ActKernel * act_kernel (Type act ,Arith a ,Isa max_isa )noexcept { 67const ActKernel * v [kMaxK ]; 68const int n = collect_acts (act ,a ,v ,kMaxK ); 69for (int i = 0 ;i < n ;++ i ) 70if ((int )v [i ]-> isa <= (int )max_isa && isa_ok (v [i ]-> isa ))return v [i ]; 71return nullptr ; 72} 73 74const ActKernel * act_kernel (Type act ,Arith a )noexcept {return act_kernel (act ,a ,best_isa ()); } 75 76}// namespace strata::quant