okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
6a74320ca
main
1// metal/tests/mm_bench.cpp - matrix throughput (TFLOP/s) of MPP matmul2d and simdgroup_matrix, fp16/bf16/fp32 2// operands, fp32 results, on a square 4096^3 and on prompt-expert shapes; checks one product against the CPU. 3// mm_bench 4#include "mtl_rt.h" 5#include "ref_quant.h" 6 7#include <algorithm> 8#include <cmath> 9#include <cstdio> 10#include <cstdlib> 11#include <cstring> 12#include <string> 13#include <vector> 14 15namespace { 16struct MmArgs {int M ,N ,K ; }; 17double median (std::vector < double > v ) { std::sort (v .begin (),v .end ());return v [v .size () /2 ]; } 18}// namespace 19 20int main () { 21 mtl::Context cx ; 22 std::printf ("device %s, metal4 %d\n" ,cx .device_name ().c_str (),cx .supports_metal4 ()); 23const int lib = cx .compile ({"mm_bench.metal" },"#define NO_GGML_COMMON 1\n" ); 24const int MAX = 4096 ; 25 mtl::Buffer A = cx .alloc ((size_t )MAX * MAX * 4 ),B = cx .alloc ((size_t )MAX * MAX * 4 ),C = cx .alloc ((size_t )MAX * MAX * 4 ); 26 ref::Rng r (1 ); 27// fill as half and float views of the same random values (each kernel reads its own type) 28auto fill = [& ](int elem ) { 29for (size_t i = 0 ;i < (size_t )MAX * MAX ;++ i ) { 30const float v = r .sym (); 31if (elem == 4 ) {A .as < float > ()[i ]= v ;B .as < float > ()[i ]= r .sym (); } 32else if (elem == 2 ) {A .as < uint16_t > ()[i ]= ref::f32_to_f16 (v );B .as < uint16_t > ()[i ]= ref::f32_to_f16 (r .sym ()); } 33else {A .as < uint16_t > ()[i ]= ref::f32_to_bf16 (v );B .as < uint16_t > ()[i ]= ref::f32_to_bf16 (r .sym ()); } 34 } 35 }; 36struct K { std::string name ;int elem ;int tm ,tn ,sg ;bool mpp ; }; 37const std::vector < K > ks = { 38 {"mpp_mm_half_64_32_4" ,2 ,64 ,32 ,4 , true}, {"mpp_mm_half_64_64_4" ,2 ,64 ,64 ,4 , true}, 39 {"mpp_mm_half_128_64_4" ,2 ,128 ,64 ,4 , true}, {"mpp_mm_half_32_64_4" ,2 ,32 ,64 ,4 , true}, 40 {"mpp_mm_half_64_128_4" ,2 ,64 ,128 ,4 , true}, {"mpp_mm_half_128_128_4" ,2 ,128 ,128 ,4 , true}, 41 {"mpp_mm_half_16_64_2" ,2 ,16 ,64 ,2 , true}, {"mpp_mm_bfloat_64_64_4" ,3 ,64 ,64 ,4 , true}, 42 {"mpp_mm_bfloat_128_64_4" ,3 ,128 ,64 ,4 , true}, {"mpp_mm_float_64_32_4" ,4 ,64 ,32 ,4 , true}, 43 {"mpp_mm_float_64_64_4" ,4 ,64 ,64 ,4 , true}, {"mpp_mm_float_128_64_4" ,4 ,128 ,64 ,4 , true}, 44 {"sg_mm_half" ,2 ,64 ,64 ,4 , false}, {"sg_mm_float" ,4 ,64 ,64 ,4 , false}, 45 {"mpp_mm_half_64_16_2" ,2 ,64 ,16 ,2 , true}, {"mpp_mm_half_64_16_4" ,2 ,64 ,16 ,4 , true}, 46 {"mpp_mm_half_128_16_4" ,2 ,128 ,16 ,4 , true}, {"mpp_mm_half_128_32_4" ,2 ,128 ,32 ,4 , true}, 47 {"mpp_mm_half_64_32_2" ,2 ,64 ,32 ,2 , true}, {"mpp_mm_half_32_16_2" ,2 ,32 ,16 ,2 , true}, 48 {"mpp_mm_half_32_32_2" ,2 ,32 ,32 ,2 , true}, {"mpp_mm_half_16_32_1" ,2 ,16 ,32 ,1 , true}, 49 {"mpp_mm_half_16_16_1" ,2 ,16 ,16 ,1 , true}, {"mpp_mm_half_128_48_4" ,2 ,128 ,48 ,4 , true}, 50 {"mpp_mm_half_64_48_4" ,2 ,64 ,48 ,4 , true}}; 51struct Shape {const char * name ;int M ,N ,K ; }; 52const std::vector < Shape > shapes = {{"square 4096^3" ,4096 ,4096 ,4096 }, 53 {"expert gu 128 tok x 1280 x 2560" ,128 ,1280 ,2560 }, 54 {"expert down 128 tok x 2560 x 640" ,128 ,2560 ,640 }, 55 {"dense qkv 2048 tok x 10240 x 2560" ,2048 ,4096 ,2560 }, 56 {"expert gu 16 tok x 1280 x 2560" ,16 ,1280 ,2560 }, 57 {"expert gu^T 1280 x 48 tok x 2560" ,1280 ,48 ,2560 }, 58 {"expert gu^T 1280 x 16 tok x 2560" ,1280 ,16 ,2560 }, 59 {"many gu^T 12800 x 48 tok x 2560" ,12800 ,48 ,2560 }, 60 {"many gu 48 tok x 12800 x 2560" ,48 ,12800 ,2560 }}; 61int filled = 0 ; 62const char * only = std::getenv ("MMB_ONLY" );// a substring of the kernel names to run 63for (const K & k :ks ) { 64if (only && k .name .find (only )== std::string::npos )continue ; 65if (filled != k .elem ) {fill (k .elem );filled = k .elem ; } 66 mtl::Pipeline p = cx .pipeline (lib ,k .name ); 67// correctness on a small product (M = tm*2, N = tn*2, K = 256) against the CPU 68 { 69const int M = k .tm * 2 ,N = k .tn * 2 ,KK = 256 ; 70MmArgs a {M ,N ,KK }; 71 mtl::Dispatch d ; 72d .p = & p ; 73d .args = {mtl::Arg ::B (A ), mtl::Arg ::B (B ), mtl::Arg ::B (C ), mtl::Arg ::V (a )}; 74d .grid [0 ]= N /k .tn ; 75d .grid [1 ]= M /k .tm ; 76d .tg [0 ]= 32 * k .sg ; 77cx .run ({d }); 78double se = 0 ,sr = 0 ; 79for (int i = 0 ;i < M ;++ i ) 80for (int j = 0 ;j < N ;++ j ) { 81double ref = 0 ; 82for (int q = 0 ;q < KK ;++ q ) { 83auto val = [& ](const mtl::Buffer & X ,size_t idx )-> double { 84if (k .elem == 4 )return X .as < float > ()[idx ]; 85if (k .elem == 2 )return ref::f16_to_f32 (X .as < uint16_t > ()[idx ]); 86return ref::bf16_to_f32 (X .as < uint16_t > ()[idx ]); 87 }; 88ref += val (A , (size_t )i * KK + q )* val (B , (size_t )j * KK + q ); 89 } 90const double e = C .as < float > ()[i * N + j ]- ref ; 91se += e * e ; 92sr += ref * ref ; 93 } 94 std::printf ("%-26s check rel L2 %.2e\n" ,k .name .c_str (), std::sqrt (se /sr )); 95 } 96for (const Shape & s :shapes ) { 97if (s .M %k .tm || s .N %k .tn )continue ; 98MmArgs a {s .M ,s .N ,s .K }; 99 mtl::Dispatch d ; 100d .p = & p ; 101d .args = {mtl::Arg ::B (A ), mtl::Arg ::B (B ), mtl::Arg ::B (C ), mtl::Arg ::V (a )}; 102d .grid [0 ]= s .N /k .tn ; 103d .grid [1 ]= s .M /k .tm ; 104d .tg [0 ]= 32 * k .sg ; 105const int reps = std::max (1 , (int )(2e10 / (2.0 * s .M * s .N * s .K ))); 106 std::vector < mtl::Dispatch > ds (std::min (reps ,200 ),d ); 107cx .warm (ds ,0.2 ); 108 std::vector < double > t ; 109for (int i = 0 ;i < 5 ;++ i )t .push_back (cx .run (ds ) /ds .size ()); 110const double m = median (t ); 111 std::printf ("%-26s %-36s %8.1f us %6.2f TFLOP/s\n" ,k .name .c_str (),s .name ,m * 1e6 , 1122.0 * s .M * s .N * s .K /m /1e12 ); 113 } 114 } 115return 0 ; 116}