okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
6a74320ca
main
1// metal/tests/rt_bench.cpp - the M-series GPU's achievable read bandwidth and the cost of a dispatch. 2// 3// rt_bench bw [MiB] streaming read kernels over a buffer (default 2048 MiB, far beyond the SLC), per 4// threadgroup size and grid size; read+write (copy); small sizes (cache-resident) 5// rt_bench dispatch empty / dependent / small kernels: N dispatches in one serial or concurrent encoder, 6// with or without barriers, through an indirect command buffer, and one command buffer 7// per dispatch (commit + wait round trip) 8#include "mtl_rt.h" 9 10#include <algorithm> 11#include <cstdio> 12#include <cstdlib> 13#include <cstring> 14#include <string> 15#include <vector> 16 17static double median (std::vector < double > v ) { 18 std::sort (v .begin (),v .end ()); 19return v [v .size () /2 ]; 20} 21 22template < class F > static void timed (int warm ,int reps ,F f ,double & med ,double & best ) { 23for (int i = 0 ;i < warm ;++ i )f (); 24 std::vector < double > t ; 25for (int i = 0 ;i < reps ;++ i )t .push_back (f ()); 26med = median (t ); 27best = * std::min_element (t .begin (),t .end ()); 28} 29 30static void bw (mtl::Context & cx ,int lib ,size_t mib ) { 31const size_t bytes = mib <<20 ; 32 mtl::Buffer src = cx .alloc (bytes ); 33for (size_t i = 0 ;i < bytes /4 ;++ i )src .as < uint32_t > ()[i ]= (uint32_t )(i * 2654435761u ); 34 mtl::Buffer out = cx .alloc (64u <<20 ); 35const uint32_t n = (uint32_t )(bytes /16 ); 36 mtl::Pipeline pr = cx .pipeline (lib ,"k_read" ),pu = cx .pipeline (lib ,"k_read_u4" ), 37pc = cx .pipeline (lib ,"k_read_chunk" ); 38 std::printf ("buffer %zu MiB; k_read max threads %d, simd width %d\n" ,mib ,pr .max_threads ,pr .simd_width ); 39 std::printf ("%-12s %6s %8s %10s %10s\n" ,"kernel" ,"tg" ,"groups" ,"GB/s med" ,"GB/s best" ); 40double best_all = 0 ; 41 std::string best_cfg ; 42for (const mtl::Pipeline * p : {& pr ,& pu }) { 43for (uint32_t tg : {64u ,128u ,256u ,512u ,1024u }) { 44if ((int )tg > p -> max_threads )continue ; 45for (uint32_t groups : {80u ,160u ,320u ,640u ,1280u ,2560u ,5120u }) { 46if ((size_t )groups * tg * 4 > out .bytes )continue ; 47 mtl::Dispatch d ; 48d .p = p ; 49d .args = {mtl::Arg ::B (src ), mtl::Arg ::B (out ), mtl::Arg ::V (n )}; 50d .grid [0 ]= groups ; 51d .tg [0 ]= tg ; 52double med ,best ; 53timed (2 ,7 , [& ] {return cx .run ({d }); },med ,best ); 54const double gm = bytes /med /1e9 ,gb = bytes /best /1e9 ; 55 std::printf ("%-12s %6u %8u %10.1f %10.1f\n" ,p == & pr ?"read" :"read_u4" ,tg ,groups ,gm ,gb ); 56if (gm > best_all ) { 57best_all = gm ; 58best_cfg = std::string (p == & pr ?"read" :"read_u4" )+ " tg " + std::to_string (tg )+ " groups " + 59 std::to_string (groups ); 60 } 61 } 62 } 63 } 64// contiguous chunk per threadgroup 65for (uint32_t tg : {256u ,512u ,1024u }) { 66for (uint32_t chunk_kib : {16u ,64u ,256u ,1024u }) { 67const uint32_t chunk = chunk_kib * 1024 /16 ; 68const uint32_t groups = (n + chunk - 1 ) /chunk ; 69if ((size_t )groups * tg * 4 > out .bytes )continue ; 70 mtl::Dispatch d ; 71d .p = & pc ; 72d .args = {mtl::Arg ::B (src ), mtl::Arg ::B (out ), mtl::Arg ::V (n ), mtl::Arg ::V (chunk )}; 73d .grid [0 ]= groups ; 74d .tg [0 ]= tg ; 75double med ,best ; 76timed (2 ,7 , [& ] {return cx .run ({d }); },med ,best ); 77const double gm = bytes /med /1e9 ; 78 std::printf ("chunk %4uK %6u %8u %10.1f %10.1f\n" ,chunk_kib ,tg ,groups ,gm ,bytes /best /1e9 ); 79if (gm > best_all ) { 80best_all = gm ; 81best_cfg = "chunk " + std::to_string (chunk_kib )+ "K tg " + std::to_string (tg ); 82 } 83 } 84 } 85 std::printf ("BEST read: %.1f GB/s (%s)\n" ,best_all ,best_cfg .c_str ()); 86 87// read + write 88 { 89 mtl::Buffer dst = cx .alloc (bytes /2 ); 90 mtl::Pipeline pk = cx .pipeline (lib ,"k_copy" ); 91const uint32_t nn = (uint32_t )(bytes /2 /16 ); 92double bestc = 0 ; 93for (uint32_t tg : {256u ,1024u }) 94for (uint32_t groups : {640u ,2560u }) { 95 mtl::Dispatch d ; 96d .p = & pk ; 97d .args = {mtl::Arg ::B (src ), mtl::Arg ::B (dst ), mtl::Arg ::V (nn )}; 98d .grid [0 ]= groups ; 99d .tg [0 ]= tg ; 100double med ,best ; 101timed (2 ,7 , [& ] {return cx .run ({d }); },med ,best ); 102bestc = std::max (bestc , (double )bytes /med /1e9 ); 103 } 104 std::printf ("BEST copy (read+write, both counted): %.1f GB/s\n" ,bestc ); 105cx .free (dst ); 106 } 107// cache-resident sizes: the same kernel on a small prefix 108for (size_t kib : {1024ul ,4096ul ,16384ul ,32768ul ,65536ul ,131072ul ,262144ul }) { 109if (kib * 1024 > bytes )break ; 110const uint32_t nn = (uint32_t )(kib * 1024 /16 ); 111double bestg = 0 ; 112for (uint32_t groups : {160u ,640u ,2560u }) { 113 mtl::Dispatch d ; 114d .p = & pu ; 115d .args = {mtl::Arg ::B (src ), mtl::Arg ::B (out ), mtl::Arg ::V (nn )}; 116d .grid [0 ]= groups ; 117d .tg [0 ]= 256 ; 118// many repetitions in one command buffer, so the launch is amortised 119const int reps = (int )std::max < size_t > (1 , (256ul <<10 ) /kib * 4 ); 120double med ,best ; 121timed (1 ,5 , [& ] {return cx .run ({d },reps ) /reps ; },med ,best ); 122bestg = std::max (bestg ,kib * 1024.0 /med /1e9 ); 123 } 124 std::printf ("resident %7zu KiB: %.1f GB/s\n" ,kib ,bestg ); 125 } 126cx .free (src ); 127cx .free (out ); 128} 129 130// A read of `kib` from DRAM per dispatch (each dispatch a different, uncached chunk of a 4 GiB buffer), dependent 131// dispatches in one serial encoder: the bound for a kernel that streams that much once. 132static void bwsize (mtl::Context & cx ,int lib ) { 133const size_t big = 4ull <<30 ; 134 mtl::Buffer src = cx .alloc (big ),out = cx .alloc (64u <<20 ); 135 mtl::Pipeline pr = cx .pipeline (lib ,"k_read" ); 136 std::printf ("%10s %8s %10s %10s\n" ,"chunk KiB" ,"tg x grp" ,"us/disp" ,"GB/s" ); 137for (size_t kib : {1152ul ,2304ul ,4500ul ,9000ul ,13500ul ,18000ul ,36000ul ,72000ul }) { 138const size_t bytes = kib * 1024 ; 139const int R = (int )std::min < size_t > (400 ,big /bytes ); 140double bestt = 1e9 ; 141uint32_t bg = 0 ,btg = 0 ; 142for (uint32_t tg : {256u ,1024u }) 143for (uint32_t groups : {80u ,160u ,320u ,640u ,1280u }) { 144 std::vector < mtl::Dispatch > ds ; 145const uint32_t n = (uint32_t )(bytes /16 ); 146for (int r = 0 ;r < R ;++ r ) { 147 mtl::Dispatch d ; 148d .p = & pr ; 149d .args = {mtl::Arg ::B (src , (size_t )r * ((big /R )& ~4095ull )), mtl::Arg ::B (out ), mtl::Arg ::V (n )}; 150d .grid [0 ]= groups ; 151d .tg [0 ]= tg ; 152ds .push_back (d ); 153 } 154double med ,best ; 155timed (1 ,5 , [& ] {return cx .run (ds ) /R ; },med ,best ); 156if (med < bestt ) {bestt = med ;bg = groups ;btg = tg ; } 157 } 158 std::printf ("%10zu %4ux%-4u %10.2f %10.1f\n" ,kib ,btg ,bg ,bestt * 1e6 ,bytes /bestt /1e9 ); 159 } 160cx .free (src ); 161cx .free (out ); 162} 163 164static void dispatch (mtl::Context & cx ,int lib ) { 165 mtl::Buffer buf = cx .alloc (1 <<20 ),x = cx .alloc (4 * 2560 ),y = cx .alloc (4 * 2560 ); 166 mtl::Pipeline pe = cx .pipeline (lib ,"k_empty" ),pch = cx .pipeline (lib ,"k_chain" ),ps = cx .pipeline (lib ,"k_small" ); 167auto mk = [& ](const mtl::Pipeline & p ,int kind ) { 168 mtl::Dispatch d ; 169d .p = & p ; 170if (kind == 2 ) { 171d .args = {mtl::Arg ::B (x ), mtl::Arg ::B (y )}; 172d .grid [0 ]= 10 ; 173d .tg [0 ]= 256 ; 174 }else { 175d .args = {mtl::Arg ::B (buf )}; 176d .grid [0 ]= 1 ; 177d .tg [0 ]= 32 ; 178 } 179return d ; 180 }; 181const int N = 2000 ; 182struct K {const char * name ;const mtl::Pipeline * p ;int kind ; }ks []= { 183 {"empty" ,& pe ,0 }, {"chain" ,& pch ,1 }, {"small" ,& ps ,2 }}; 184 std::printf ("%-8s %-36s %10s\n" ,"kernel" ,"mode" ,"us/dispatch" ); 185for (const K & k :ks ) { 186 std::vector < mtl::Dispatch > ds (N ,mk (* k .p ,k .kind )); 187double med ,best ; 188timed (2 ,7 , [& ] {return cx .run (ds ,1 , false, false); },med ,best ); 189 std::printf ("%-8s %-36s %10.2f\n" ,k .name ,"serial encoder" ,med /N * 1e6 ); 190timed (2 ,7 , [& ] {return cx .run (ds ,1 , true, false); },med ,best ); 191 std::printf ("%-8s %-36s %10.2f\n" ,k .name ,"concurrent encoder, no barrier" ,med /N * 1e6 ); 192timed (2 ,7 , [& ] {return cx .run (ds ,1 , true, true); },med ,best ); 193 std::printf ("%-8s %-36s %10.2f\n" ,k .name ,"concurrent encoder + barrier each" ,med /N * 1e6 ); 194timed (2 ,7 , [& ] {return cx .run (ds ,1 , false, true); },med ,best ); 195 std::printf ("%-8s %-36s %10.2f\n" ,k .name ,"serial encoder + barrier each" ,med /N * 1e6 ); 196timed (2 ,7 , [& ] {return cx .run_icb (ds ,1 , false); },med ,best ); 197 std::printf ("%-8s %-36s %10.2f\n" ,k .name ,"ICB, no barrier" ,med /N * 1e6 ); 198timed (2 ,7 , [& ] {return cx .run_icb (ds ,1 , true); },med ,best ); 199 std::printf ("%-8s %-36s %10.2f\n" ,k .name ,"ICB + barrier each" ,med /N * 1e6 ); 200 } 201// command buffer round trips 202 { 203 std::vector < mtl::Dispatch > one (1 ,mk (pe ,0 )); 204double gpu = 0 ; 205const int R = 300 ; 206cx .run_wall (one ,20 ); 207const double wall = cx .run_wall (one ,R ,& gpu ); 208 std::printf ("%-8s %-36s %10.2f (GPU %.2f us)\n" ,"empty" ,"1 cmdbuf/dispatch, commit+wait wall" ,wall /R * 1e6 , 209gpu /R * 1e6 ); 210 std::vector < mtl::Dispatch > d100 (100 ,mk (pe ,0 )); 211const double wall2 = cx .run_wall (d100 ,100 ,& gpu ); 212 std::printf ("%-8s %-36s %10.2f (GPU %.2f us)\n" ,"empty" ,"100 dispatches/cmdbuf, wall per cb" ,wall2 /100 * 1e6 , 213gpu /100 * 1e6 ); 214 } 215// the fixed cost of one command buffer as seen by the GPU timer (1 dispatch) 216 { 217 std::vector < mtl::Dispatch > one (1 ,mk (pe ,0 )); 218double med ,best ; 219timed (5 ,21 , [& ] {return cx .run (one ); },med ,best ); 220 std::printf ("%-8s %-36s %10.2f\n" ,"empty" ,"single dispatch cmdbuf GPU time" ,med * 1e6 ); 221 } 222cx .free (buf ); 223cx .free (x ); 224cx .free (y ); 225} 226 227int main (int argc ,char ** argv ) { 228 mtl::Context cx ; 229 std::printf ("device: %s apple9 %d apple10 %d metal4 %d working set %.1f GiB\n" ,cx .device_name ().c_str (), 230cx .has_family_apple (9 ),cx .has_family_apple (10 ),cx .supports_metal4 (), 231cx .recommended_working_set () /1073741824.0 ); 232const int lib = cx .compile ({"rt_bench.metal" },"#define NO_GGML_COMMON 1\n" ); 233const std::string mode = argc > 1 ?argv [1 ] :"all" ; 234if (mode == "bw" || mode == "all" )bw (cx ,lib ,argc > 2 ? std::strtoul (argv [2 ],nullptr ,10 ) :2048 ); 235if (mode == "dispatch" || mode == "all" )dispatch (cx ,lib ); 236if (mode == "bwsize" || mode == "all" )bwsize (cx ,lib ); 237return 0 ; 238}