okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
3a76d5eaf
main
1// metal/tests/gdn_tprobe.cpp - times kernels/gdn_tprobe.metal: the five MPP matmul2d products of a GDN chunk step, NC chunk steps a 2// threadgroup, 192 threadgroups (h_v * 4). Compare with gdn_scanr_4's stage (chunks of 32 -> T = 32 * NC). 3// gdn_tprobe [NC] 4#include "mtl_rt.h" 5#include "ref_quant.h" 6 7#include <algorithm> 8#include <cstdio> 9#include <cstdlib> 10#include <vector> 11 12int main (int argc ,char ** argv ) { 13const int NC = argc > 1 ? std::atoi (argv [1 ]) :64 ; 14 mtl::Context cx ; 15const int lib = cx .compile ({"gdn_tprobe.metal" },"#define NO_GGML_COMMON 1\n" ); 16 std::printf ("device %s, NC %d (T %d)\n" ,cx .device_name ().c_str (),NC ,NC * 32 ); 17const size_t NB = (size_t )48 * NC ; 18 mtl::Buffer out = cx .alloc (4 * 192 * 4096 ),W = cx .alloc (2 * NB * 32 * 128 ),Q = cx .alloc (2 * NB * 32 * 128 ), 19K = cx .alloc (2 * NB * 32 * 128 ),M = cx .alloc (2 * NB * 32 * 32 ); 20 ref::Rng r (3 ); 21for (auto * b : {& W ,& Q ,& K })for (size_t i = 0 ;i < NB * 32 * 128 ;++ i )b -> as < uint16_t > ()[i ]= ref::f32_to_f16 (r .sym ()* 0.1f ); 22for (size_t i = 0 ;i < NB * 32 * 32 ;++ i )M .as < uint16_t > ()[i ]= ref::f32_to_f16 (r .sym ()* 0.1f ); 23for (int pr : {0 ,1 }) { 24 mtl::Pipeline p = cx .pipeline (lib ,"gdn_tprobe" ,pr ? mtl::Constants ().i (0 ,pr ) : mtl::Constants ()); 25 mtl::Dispatch d ;d .p = & p ; 26d .args = {mtl::Arg ::B (out ), mtl::Arg ::B (W ), mtl::Arg ::B (Q ), mtl::Arg ::B (K ), mtl::Arg ::B (M ), mtl::Arg ::V (NC )}; 27d .grid [0 ]= 192 ;d .tg [0 ]= 128 ; 28cx .warm ({d },0.3 ); 29 std::vector < double > t ; 30for (int i = 0 ;i < 7 ;++ i )t .push_back (cx .run ({d })* 1e3 ); 31 std::sort (t .begin (),t .end ()); 32 std::printf ("probe %d (%s): %.3f ms for %d chunk steps x 192 threadgroups (max threads %d, tg mem %d)\n" ,pr , 33pr ?"SIMT parts only" :"with the five products" ,t [3 ],NC ,p .max_threads ,p .static_tg_mem ); 34 } 35return 0 ; 36}