okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
a8a51b280
main
1#include "strata/prefill/gemm.hpp" 2#include "strata/sycl_queue.hpp" 3 4#include <bit> 5#include <cmath> 6#include <cstdint> 7#include <cstdio> 8#include <cstring> 9#include <limits> 10#include <string> 11#include <vector> 12 13namespace { 14constexpr int64_t T = 3 ,N = 5 ,K = 32 ,LDY = N + 3 ; 15constexpr float guard = -123.0f ; 16 17uint16_t bf16 (float value ) { 18return uint16_t (std::bit_cast < uint32_t > (value ) >>16 ); 19} 20 21bool run (bool native ,int64_t ldx ,float beta ,int64_t scratch_rows ) { 22auto & queue = dpct::get_in_order_queue (); 23 strata::prefill::Gemm gemm ; 24 std::string error ; 25if (!gemm .init (& queue ,scratch_rows * K ,error )) { 26 std::fprintf (stderr ,"%s\n" ,error .c_str ()); 27return false; 28 } 29 std::vector < uint16_t > x (size_t (T * ldx ),native ?0x7e00 :0x7fc0 ); 30 std::vector < uint16_t > weight (size_t (N * K )); 31 std::vector < uint8_t > blocks (size_t (N * 34 )); 32const uint16_t half_x [T ]= {0x3800 ,0x3c00 ,0x3e00 }; 33for (int64_t t = 0 ;t < T ;++ t ) 34for (int64_t k = 0 ;k < K ;++ k ) 35x [size_t (t * ldx + k )]= native ?half_x [t ] :bf16 (float (t + 1 ) /2 ); 36for (int64_t n = 0 ;n < N ;++ n ) { 37for (int64_t k = 0 ;k < K ;++ k ) { 38weight [size_t (n * K + k )]= bf16 (float (n + 1 ) /4 ); 39blocks [size_t (n * 34 + 2 + k )]= uint8_t (n + 1 ); 40 } 41const uint16_t scale = 0x2800 ;// FP16 1/32, exactly representable. 42 std::memcpy (blocks .data ()+ n * 34 ,& scale ,sizeof (scale )); 43 } 44 std::vector < float > y (size_t (T * LDY ),guard ); 45for (int64_t t = 0 ;t < T ;++ t ) 46for (int64_t n = 0 ;n < N ;++ n )y [size_t (t * LDY + n )]= 0.25f ; 47auto * dx = sycl::malloc_device < uint16_t > (x .size (),queue ); 48auto * dw = sycl::malloc_device < uint16_t > (weight .size (),queue ); 49auto * db = sycl::malloc_device < uint8_t > (blocks .size (),queue ); 50auto * dy = sycl::malloc_device < float > (y .size (),queue ); 51queue .memcpy (dx ,x .data (),x .size ()* sizeof (uint16_t )); 52queue .memcpy (dw ,weight .data (),weight .size ()* sizeof (uint16_t )); 53queue .memcpy (db ,blocks .data (),blocks .size ()); 54queue .memcpy (dy ,y .data (),y .size ()* sizeof (float )).wait (); 55if (native )gemm .native (dx ,8 /* GGML_TYPE_Q8_0 */ ,db ,dy ,T ,N ,K ,LDY ,beta ,ldx ); 56else gemm .bf16 (dx ,dw ,dy ,T ,N ,K ,LDY ,beta ,ldx ); 57queue .memcpy (y .data (),dy ,y .size ()* sizeof (float )).wait (); 58bool good = true; 59for (int64_t t = 0 ;t < T ;++ t ) 60for (int64_t n = 0 ;n < LDY ;++ n ) { 61const float expected = n < N 62 ?float (t + 1 ) /2 * float (n + 1 )* (native ?1.0f :8.0f )+ beta * 0.25f 63 :guard ; 64const float actual = y [size_t (t * LDY + n )]; 65if (!std::isfinite (actual )|| std::abs (actual - expected )> 1e-5f ) { 66 std::fprintf (stderr ,"%s stride=%lld beta=%.0f scratch=%lld [%lld,%lld]: %.9g != %.9g\n" , 67native ?"native" :"bf16" , (long long )ldx ,beta , (long long )scratch_rows , 68 (long long )t , (long long )n ,actual ,expected ); 69good = false; 70 } 71 } 72 sycl::free (dx ,queue ); 73 sycl::free (dw ,queue ); 74 sycl::free (db ,queue ); 75 sycl::free (dy ,queue ); 76return good ; 77} 78}// namespace 79 80int main ()try { 81for (int64_t stride : {K ,K + 8 }) 82for (float beta : {0.0f ,1.0f }) { 83if (!run (false,stride ,beta ,N ))return 1 ; 84for (int64_t scratch_rows : {N ,int64_t (2 )}) 85if (!run (true,stride ,beta ,scratch_rows ))return 1 ; 86 } 87 std::puts ("GEMM tight/padded input, padded output, beta 0/1, and native row slices: PASS" ); 88return 0 ; 89}catch (const std::exception & error ) { 90 std::fprintf (stderr ,"%s\n" ,error .what ()); 91return 1 ; 92}