okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
e51fa6c36
main
1// STRATA_HCD_EXACT's kernel against hipBLASLt, bit for bit. Gemm::bf16_hcd_exact (the hyper-connection read's down 2// projection, N 320 x K 10240, BF16 in, FP32 out) reproduces hipBLASLt solutions 1176 / 1177's k order, StaggerU 3// included; it launches only when the tuning table makes hipBLASLt take one of them for the shape. This test runs both 4// for several chunk sizes T on the same random inputs (the activations at the token stride the engine uses, K + 64) and 5// requires every output to be equal: a hipBLASLt update that renumbers its solutions or changes their order shows up 6// here as a mismatch (or as the kernel declining, which is reported). 7// 8// Needs STRATA_HIPBLASLT_TUNING (the gfx1151 table in tools/hip) on a gfx11 card: exit 77 (SKIP) otherwise, or when no 9// T is taken by the kernel at all. 10#include <cuda_runtime.h> 11 12#include "strata/kernels/bf16_bits.hpp" 13#include "strata/prefill/gemm.hpp" 14 15#include <cstdint> 16#include <cstdio> 17#include <cstdlib> 18#include <cstring> 19#include <random> 20#include <string> 21#include <vector> 22 23#define HIP_CHECK (call ) do { \ 24 const hipError_t status_ = (call); \ 25 if (status_ != hipSuccess) { \ 26 std::fprintf(stderr, "HIP failure at %s:%d: %s: %s\n", __FILE__, __LINE__, #call, hipGetErrorString(status_)); \ 27 std::exit(2); \ 28 } \ 29} while (0) 30 31int main () { 32if (std::getenv ("STRATA_HIPBLASLT_TUNING" )== nullptr ) { 33 std::printf ("SKIP: STRATA_HIPBLASLT_TUNING is not set (the kernel runs only where the table picks solution 1176 / 1177)\n" ); 34return 77 ; 35 } 36hipDeviceProp_t prop {}; 37int dev = 0 ; 38HIP_CHECK (hipGetDevice (& dev )); 39HIP_CHECK (hipGetDeviceProperties (& prop ,dev )); 40if (std::strncmp (prop .gcnArchName ,"gfx11" ,5 )!= 0 ) { 41 std::printf ("SKIP: %s is not a gfx11 card\n" ,prop .gcnArchName ); 42return 77 ; 43 } 44constexpr int64_t N = 320 ,K = 10240 ,LDX = K + 64 ; 45const int64_t Ts []= {64 ,1000 ,2047 ,2048 ,3333 ,4096 ,6000 ,8191 ,16384 }; 46hipStream_t stream ; 47HIP_CHECK (hipStreamCreate (& stream )); 48 strata::prefill::Gemm gemm ; 49 std::string err ; 50if (!gemm .init (stream ,1 <<20 ,err )) { 51 std::fprintf (stderr ,"Gemm::init: %s\n" ,err .c_str ()); 52return 2 ; 53 } 54 std::mt19937 rng (12345 ); 55 std::uniform_real_distribution < float > dist (-1.0f ,1.0f ); 56 std::vector < uint16_t > w ((size_t )N * K ); 57for (auto & v :w )v = strata::kernels::bf16_from_f32 (0.05f * dist (rng )); 58uint16_t * dw = nullptr ; 59HIP_CHECK (hipMalloc ((void ** )& dw ,w .size ()* 2 )); 60HIP_CHECK (hipMemcpy (dw ,w .data (),w .size ()* 2 ,hipMemcpyHostToDevice )); 61int ran = 0 ,bad = 0 ,declined = 0 ; 62for (const int64_t T :Ts ) { 63 std::vector < uint16_t > x ((size_t )T * LDX ,0 ); 64for (int64_t t = 0 ;t < T ;++ t ) 65for (int64_t k = 0 ;k < K ;++ k )x [(size_t )t * LDX + k ]= strata::kernels::bf16_from_f32 (dist (rng )); 66uint16_t * dx = nullptr ; 67float * dy_ref = nullptr ,* dy = nullptr ; 68HIP_CHECK (hipMalloc ((void ** )& dx ,x .size ()* 2 )); 69HIP_CHECK (hipMalloc ((void ** )& dy_ref , (size_t )T * N * 4 )); 70HIP_CHECK (hipMalloc ((void ** )& dy , (size_t )T * N * 4 )); 71HIP_CHECK (hipMemcpy (dx ,x .data (),x .size ()* 2 ,hipMemcpyHostToDevice )); 72HIP_CHECK (hipMemset (dy_ref ,0xff , (size_t )T * N * 4 )); 73HIP_CHECK (hipMemset (dy ,0xee , (size_t )T * N * 4 )); 74gemm .bf16 (dx ,dw ,dy_ref ,T ,N ,K ,0 ,0.0f ,LDX );// hipBLASLt (the table's solution), the same stride 75HIP_CHECK (hipStreamSynchronize (stream )); 76if (!gemm .bf16_hcd_exact (dx ,LDX ,dw ,dy ,T ,N ,K )) { 77 std::printf ("T %5lld: the kernel declines (the table does not pick solution 1176 / 1177 here): hipBLASLt runs it\n" , (long long )T ); 78++ declined ; 79 }else { 80HIP_CHECK (hipStreamSynchronize (stream )); 81 std::vector < float > a ((size_t )T * N ),b ((size_t )T * N ); 82HIP_CHECK (hipMemcpy (a .data (),dy_ref ,a .size ()* 4 ,hipMemcpyDeviceToHost )); 83HIP_CHECK (hipMemcpy (b .data (),dy ,b .size ()* 4 ,hipMemcpyDeviceToHost )); 84int64_t diff = 0 ; 85for (size_t i = 0 ;i < a .size ();++ i ) 86if (std::memcmp (& a [i ],& b [i ],4 )!= 0 )++ diff ; 87 std::printf ("T %5lld: %lld of %lld outputs differ\n" , (long long )T , (long long )diff , (long long )a .size ()); 88if (diff )++ bad ; 89++ ran ; 90 } 91HIP_CHECK (hipFree (dx )); 92HIP_CHECK (hipFree (dy_ref )); 93HIP_CHECK (hipFree (dy )); 94 } 95HIP_CHECK (hipFree (dw )); 96if (ran == 0 ) { 97 std::printf ("SKIP: the table sends no tested T to solution 1176 / 1177\n" ); 98return 77 ; 99 } 100 std::printf ("%s: %d chunk sizes compared, %d declined, %d with differences\n" ,bad ?"FAIL" :"PASS" ,ran ,declined ,bad ); 101return bad ?1 :0 ; 102}