okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
78c5a834e
main
1// metal/tests/mm_i8_bench.cpp - matmul2d throughput for int8 x int8 -> int32 and fp16 -> fp16 against fp16 -> fp32 (4096^3 and a 2// prompt-sized dense shape). Answers: does the M5's matrix unit run int8 faster than fp16? 3// mm_i8_bench 4#include "mtl_rt.h" 5 6#include <algorithm> 7#include <cstdio> 8#include <cstdlib> 9#include <cstring> 10#include <string> 11#include <vector> 12 13namespace { 14struct MmArgs {int M ,N ,K ; }; 15double median (std::vector < double > v ) { std::sort (v .begin (),v .end ());return v [v .size () /2 ]; } 16}// namespace 17 18int main () { 19 mtl::Context cx ; 20 std::printf ("device %s\n" ,cx .device_name ().c_str ()); 21const int lib = cx .compile ({"mm_i8.metal" },"#define NO_GGML_COMMON 1\n" ); 22const int MAX = 4096 ; 23 mtl::Buffer A = cx .alloc ((size_t )MAX * MAX * 2 ),B = cx .alloc ((size_t )MAX * MAX * 2 ),C = cx .alloc ((size_t )MAX * MAX * 4 ); 24for (size_t i = 0 ;i < (size_t )MAX * MAX * 2 ;++ i ) {A .as < uint8_t > ()[i ]= (uint8_t ) (i * 2654435761u >>24 );B .as < uint8_t > ()[i ]= (uint8_t ) (i * 40503u >>8 ); } 25struct K { std::string name ;int tm ,tn ,sg ; }; 26const std::vector < K > ks = {{"mpp_char_int_64_64_4" ,64 ,64 ,4 }, {"mpp_char_int_128_64_4" ,128 ,64 ,4 }, {"mpp_char_int_64_128_4" ,64 ,128 ,4 }, 27 {"mpp_half_half_64_64_4" ,64 ,64 ,4 }, {"mpp_half_half_128_64_4" ,128 ,64 ,4 }, 28 {"mpp_half_float_64_64_4" ,64 ,64 ,4 }, {"mpp_half_float_128_64_4" ,128 ,64 ,4 }}; 29struct Shape {const char * name ;int M ,N ,K ; }; 30const std::vector < Shape > shapes = {{"square 4096^3" ,4096 ,4096 ,4096 }, {"dense 2048 x 4096 x 2560" ,2048 ,4096 ,2560 }}; 31for (const K & k :ks ) { 32 mtl::Pipeline p = cx .pipeline (lib ,k .name ); 33for (const Shape & s :shapes ) { 34MmArgs a {s .M ,s .N ,s .K }; 35 mtl::Dispatch d ; 36d .p = & p ; 37d .args = {mtl::Arg ::B (A ), mtl::Arg ::B (B ), mtl::Arg ::B (C ), mtl::Arg ::V (a )}; 38d .grid [0 ]= s .N /k .tn ; 39d .grid [1 ]= s .M /k .tm ; 40d .tg [0 ]= 32 * k .sg ; 41 std::vector < mtl::Dispatch > ds (std::max (1 , (int ) (4e10 / (2.0 * s .M * s .N * s .K ))),d ); 42cx .warm (ds ,0.2 ); 43 std::vector < double > t ; 44for (int i = 0 ;i < 5 ;++ i )t .push_back (cx .run (ds ) /ds .size ()); 45const double m = median (t ); 46 std::printf ("%-26s %-28s %8.1f us %6.2f T(FL)OP/s\n" ,k .name .c_str (),s .name ,m * 1e6 ,2.0 * s .M * s .N * s .K /m /1e12 ); 47 } 48 } 49return 0 ; 50}