okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
6a74320ca
main
1// metal/tests/pair_parity.cpp - gemv_pair (the shared expert's gate + up + SwiGLU in one dispatch) vs double, for 2// format pairs of the packs, 1 / 3 / 8 tokens; and the raw-activation Q2_0 GEMV (type 142) the down then uses. 3#include "mtl_rt.h" 4#include "ref_quant.h" 5 6#include <cmath> 7#include <cstdio> 8#include <string> 9#include <utility> 10#include <vector> 11 12namespace { 13struct GemvArgs {uint32_t n_in ,n_out ,n_tok ,row_bytes ,x_stride ,y_stride ; }; 14int xkind (int t ) { 15switch (t ) { 16case ref::Q2_0 :return 1 ; 17case ref::Q4_K :case ref::Q5_K :return 2 ; 18case ref::Q6_K :case ref::Q3_K :return 3 ; 19default :return 0 ; 20 } 21} 22}// namespace 23 24int main () { 25 mtl::Context cx ; 26const int lib = cx .compile ({"quant.metal" ,"units.metal" ,"gemv.metal" }); 27 mtl::Pipeline pprep = cx .pipeline (lib ,"xprep" ); 28const int NIN = 2560 ,NOUT = 640 ,MAXT = 8 ; 29 mtl::Buffer x = cx .alloc (4 * NIN * MAXT ),xp = cx .alloc (4 * (4 * NIN * MAXT + NIN /16 * MAXT )),h = cx .alloc (4 * NOUT * MAXT ); 30 ref::Rng r (4 ); 31for (int i = 0 ;i < NIN * MAXT ;++ i )x .as < float > ()[i ]= r .sym (); 32 { 33 mtl::Dispatch d ; 34d .p = & pprep ; 35const uint32_t n = NIN ,nt = MAXT ; 36d .args = {mtl::Arg ::B (x ), mtl::Arg ::B (xp ), mtl::Arg ::V (n ), mtl::Arg ::V (nt )}; 37d .tg [0 ]= 256 ; 38d .grid [0 ]= (NIN /16 * MAXT + 255 ) /256 ; 39cx .run ({d }); 40 } 41const size_t plane = (size_t )4 * NIN * MAXT ; 42const std::vector < std::pair < int ,int >> pairs = {{ref::Q2_0 , ref::Q3_K }, {ref::Q3_K , ref::Q2_0 }, {ref::IQ4_XS , ref::IQ3_S }, 43 {ref::Q4_K , ref::Q6_K }, {ref::Q5_K , ref::Q4_K }, {ref::Q6_K , ref::IQ3_S }, 44 {ref::Q2_0 , ref::Q2_0 }, {ref::IQ3_S , ref::Q4_K }}; 45int fails = 0 ; 46for (auto [fg ,fu ] :pairs ) { 47const size_t rg = ref::row_bytes (fg ,NIN ),ru = ref::row_bytes (fu ,NIN ); 48 mtl::Buffer Wg = cx .alloc (rg * NOUT ),Wu = cx .alloc (ru * NOUT ); 49 ref::random_blocks (fg ,Wg .as < uint8_t > (),NOUT ,NIN ,7 + fg ,0.02f ); 50 ref::random_blocks (fu ,Wu .as < uint8_t > (),NOUT ,NIN ,9 + fu ,0.02f ); 51 std::vector < float > wg ((size_t )NIN * NOUT ),wu ((size_t )NIN * NOUT ); 52 ref::dequant (fg ,Wg .ptr ,wg .data (),wg .size ()); 53 ref::dequant (fu ,Wu .ptr ,wu .data (),wu .size ()); 54for (int nt : {1 ,3 ,8 }) { 55 mtl::Pipeline p = cx .pipeline (lib ,"gemv_pair_" + std::to_string (fg )+ "_" + std::to_string (fu ), 56 mtl::Constants ().i (0 ,nt )); 57GemvArgs a {(uint32_t )NIN , (uint32_t )NOUT , (uint32_t )nt , (uint32_t )rg , (uint32_t )NIN , (uint32_t )NOUT }; 58const uint32_t rbu = (uint32_t )ru ; 59 mtl::Dispatch d ; 60d .p = & p ; 61d .args = {mtl::Arg ::B (Wg ), mtl::Arg ::B (xp ,plane * xkind (fg )), mtl::Arg ::B (Wu ), mtl::Arg ::B (xp ,plane * xkind (fu )), 62 mtl::Arg ::B (xp ,plane * 4 ), mtl::Arg ::B (h ), mtl::Arg ::V (a ), mtl::Arg ::V (rbu )}; 63d .grid [0 ]= NOUT /4 ; 64d .tg [0 ]= 128 ; 65cx .run ({d }); 66double se = 0 ,sr = 0 ; 67for (int t = 0 ;t < nt ;++ t ) 68for (int o = 0 ;o < NOUT ;++ o ) { 69double g = 0 ,u = 0 ; 70for (int k = 0 ;k < NIN ;++ k ) { 71g += (double )wg [(size_t )o * NIN + k ]* x .as < float > ()[t * NIN + k ]; 72u += (double )wu [(size_t )o * NIN + k ]* x .as < float > ()[t * NIN + k ]; 73 } 74const double ref = g / (1.0 + std::exp (- g ))* u ; 75const double e = h .as < float > ()[t * NOUT + o ]- ref ; 76se += e * e ; 77sr += ref * ref ; 78 } 79const double rel = std::sqrt (se /sr ); 80fails += !(rel < 1e-5 ); 81 std::printf ("pair %-7s + %-7s ntok %d: rel L2 %.2e %s\n" , ref::type_name (fg ), ref::type_name (fu ),nt ,rel , 82rel < 1e-5 ?"PASS" :"FAIL" ); 83 } 84cx .free (Wg ); 85cx .free (Wu ); 86 } 87// the raw-activation Q2_0 GEMV 88 { 89const int ni = 640 ,no = 2560 ; 90const size_t rb = ref::row_bytes (ref::Q2_0 ,ni ); 91 mtl::Buffer W = cx .alloc (rb * no ),y = cx .alloc (4 * no * MAXT ); 92 ref::random_blocks (ref::Q2_0 ,W .as < uint8_t > (),no ,ni ,3 ,0.02f ); 93 std::vector < float > wf ((size_t )ni * no ); 94 ref::dequant (ref::Q2_0 ,W .ptr ,wf .data (),wf .size ()); 95 mtl::Pipeline p = cx .pipeline (lib ,"gemv_142_8_1_4_1" , mtl::Constants ().i (0 ,3 )); 96GemvArgs a {(uint32_t )ni , (uint32_t )no ,3 , (uint32_t )rb , (uint32_t )ni , (uint32_t )no }; 97 mtl::Dispatch d ; 98d .p = & p ; 99d .args = {mtl::Arg ::B (W ), mtl::Arg ::B (x ), mtl::Arg ::B (x ), mtl::Arg ::B (y ), mtl::Arg ::V (a )}; 100d .grid [0 ]= no /16 ; 101d .tg [0 ]= 128 ; 102cx .run ({d }); 103double se = 0 ,sr = 0 ; 104for (int t = 0 ;t < 3 ;++ t ) 105for (int o = 0 ;o < no ;++ o ) { 106double ref = 0 ; 107for (int k = 0 ;k < ni ;++ k )ref += (double )wf [(size_t )o * ni + k ]* x .as < float > ()[t * ni + k ]; 108const double e = y .as < float > ()[t * no + o ]- ref ; 109se += e * e ; 110sr += ref * ref ; 111 } 112const double rel = std::sqrt (se /sr ); 113fails += !(rel < 1e-5 ); 114 std::printf ("Q2_0 raw-x GEMV 640 -> 2560, 3 tokens: rel L2 %.2e %s\n" ,rel ,rel < 1e-5 ?"PASS" :"FAIL" ); 115 } 116 std::printf ("%s\n" ,fails ?"SOME FAILED" :"ALL PASS" ); 117return fails ?1 :0 ; 118}