okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
8f70a52cf
main
1// gemm_launch: what does one expert-sized FP16 GEMM (oneMKL, half in, float out) cost back to back, by row count? 2// The prompt path issues one gate/up and one down GEMM per routed expert (~25k experts per 4K prompt on the 48 layers); if the 3// per-call time barely moves with the rows, the path is bound by launches, not by the XMX units. 4// gemm_launch [calls=2000] 5#include <sycl/sycl.hpp> 6#include <oneapi/mkl.hpp> 7#include <dpct/dpct.hpp> 8#include <dpct/blas_utils.hpp> 9#include <chrono> 10#include <cstdio> 11#include <cstdlib> 12#include <vector> 13 14int main (int argc ,char ** argv ) { 15const int calls = argc > 1 ? std::atoi (argv [1 ]) :2000 ; 16 sycl::queue q {sycl::gpu_selector_v , sycl::property::queue::in_order {}}; 17const int64_t N = 2560 ; 18struct Shape {const char * name ;int64_t n ,k ; }; 19const Shape shapes []= {{"gate/up 1280x2560" ,1280 ,N }, {"down 2560x640" ,N ,640 }}; 20const int64_t Ts []= {8 ,32 ,80 ,160 ,320 ,640 ,1280 }; 21const int64_t maxT = 1280 ; 22using half = sycl::half ; 23half * X = sycl::malloc_device < half > (maxT * N ,q ); 24half * W = sycl::malloc_device < half > (N * 1280 * 4 ,q ); 25float * Y = sycl::malloc_device < float > (maxT * N ,q ); 26q .memset (X ,0 ,maxT * N * sizeof (half )); 27q .memset (W ,0 ,N * 1280 * 4 * sizeof (half )); 28q .wait (); 29for (const Shape & s :shapes ) { 30for (int64_t T :Ts ) { 31auto run = [& ](int n ) { 32for (int i = 0 ;i < n ;++ i ) 33 oneapi::mkl::blas::column_major::gemm (q , oneapi::mkl::transpose::trans , oneapi::mkl::transpose::nontrans ,s .n ,T ,s .k ,1.0f , 34W ,s .k ,X ,s .k ,0.0f ,Y ,s .n ); 35q .wait (); 36 }; 37run (50 ); 38const auto t0 = std::chrono::steady_clock::now (); 39run (calls ); 40const double us = std::chrono::duration < double , std::micro > (std::chrono::steady_clock::now ()- t0 ).count () /calls ; 41auto * h = new dpct::blas::descriptor (); 42h -> set_queue (& q ); 43const float alpha = 1.0f ,beta = 0.0f ; 44auto run2 = [& ](int cnt ) { 45for (int i = 0 ;i < cnt ;++ i ) 46 dpct::blas::gemm (h , oneapi::mkl::transpose::trans , oneapi::mkl::transpose::nontrans , (int )s .n , (int )T , (int )s .k ,& alpha ,W , 47 dpct::library_data_t::real_half , (int )s .k ,X , dpct::library_data_t::real_half , (int )s .k ,& beta ,Y , 48 dpct::library_data_t::real_float , (int )s .n , dpct::compute_type::f32 ); 49q .wait (); 50 }; 51run2 (50 ); 52const auto t1 = std::chrono::steady_clock::now (); 53run2 (calls ); 54const double us2 = std::chrono::duration < double , std::micro > (std::chrono::steady_clock::now ()- t1 ).count () /calls ; 55delete h ; 56 std::printf ("%-20s T=%5lld: mkl %7.1f us/call dpct::blas::gemm %7.1f us/call\n" ,s .name , (long long )T ,us ,us2 ); 57 } 58 } 59return 0 ; 60}