okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
8ac8ce32e
main
1// Strata. Parity + speed of the gfx12 WMMA trellis GEMV (src/dsv41/ds4_tqw.cuh) on a R9700 (gfx1201): 2// tqw_hip_test [--k 4,5,6,7] [--code 0|1] [--bench] 3// Parity: random legal streams (any bit pattern is one), host-decoded weights (tqenc::decode_blocks: the table codebook, or the 4// computed code when TQE_CODE=3single, with --code 1) against the kernel's y, relative to sum|w x| per output. 5// --bench: the DeepSeek decode shape (12 gate/up matrices of 2304 x 5120), GB/s of weight bytes. 6#include "../../src/dsv41/ds4_tqw.cuh" 7#include "../../tools/dsv41_lab/wmma_emu.hpp" 8#include "../../tools/trellis/tqenc.hpp" 9 10#include <algorithm> 11#include <chrono> 12#include <cmath> 13#include <cstdio> 14#include <cstdlib> 15#include <cstring> 16#include <random> 17#include <string> 18#include <vector> 19 20using namespace strata::ds4::tqw ; 21#define CK (x ) do { hipError_t e_ = (x); if (e_ != hipSuccess) { std::printf("HIP error %s at %d\n", hipGetErrorString(e_), __LINE__); std::exit(2); } } while (0) 22 23template < typename T > static T * up (const std::vector < T >& v ) {T * d ;CK (hipMalloc (& d ,v .size ()* sizeof (T )));CK (hipMemcpy (d ,v .data (),v .size ()* sizeof (T ),hipMemcpyHostToDevice ));return d ; } 24 25template < int K ,int NB ,int CODE > static void launch (const Job * jd ,const uint32_t * lut ,int rows ,int njobs ) { 26constexpr int T = 2 ; 27dim3 grid ((rows + 16 * T * 4 - 1 ) / (16 * T * 4 ),njobs ); 28gemv < K ,NB ,T ,1 ,CODE > <<< grid , 128 > >>( jd , lut ); 29} 30template < int K , int CODE > static void launch_nb( int nb , const Job * jd , const uint32_t * lut , int rows , int njobs ) { 31switch ( nb ) { 32case 1 : launch < K , 1 , CODE > ( jd , lut , rows , njobs ); break ; 33case 3 : launch < K , 3 , CODE > ( jd , lut , rows , njobs ); break ; 34default : launch < K , 5 , CODE > ( jd , lut , rows , njobs ); break ; 35} 36} 37template < int CODE > static void launch_k( int K , int nb , const Job * jd , const uint32_t * lut , int rows , int njobs ) { 38switch ( K ) { 39case 4 : launch_nb < 4 , CODE > ( nb , jd , lut , rows , njobs ); break ; 40case 5 : launch_nb < 5 , CODE > ( nb , jd , lut , rows , njobs ); break ; 41case 6 : launch_nb < 6 , CODE > ( nb , jd , lut , rows , njobs ); break ; 42default : launch_nb < 7 , CODE > ( nb , jd , lut , rows , njobs ); break ; 43} 44} 45static void run( int code , int K , int nb , const Job * jd , const uint32_t * lut , int rows , int njobs ) { 46if ( code ) launch_k < 1 > ( K , nb , jd , lut , rows , njobs ); else launch_k < 0 > ( K , nb , jd , lut , rows , njobs ); 47} 48 49int main( int argc , char ** argv ) { 50int code = std::getenv( "TQE_CODE" ) ? 1 : 0 ; 51bool bench = false; 52std:: vector < int > Ks = { 4 , 5 , 6 , 7 }; 53for ( int i = 1 ; i < argc ; ++ i ) { 54std:: string a = argv [ i ]; 55if ( a == "--bench" ) bench = true; 56else if ( a == "--code" && i + 1 < argc ) code = std::atoi( argv [ ++ i ]); 57else if ( a == "--k" && i + 1 < argc ) { Ks .clear(); for ( const char * p = argv [ ++ i ]; * p ;) { Ks .push_back(std::atoi( p )); while ( * p && * p != ',' ) ++ p ; if ( * p ) ++ p ; } } 58} 59std:: vector < uint32_t > hl ( 2048 ); 60{ 61const float * pt = tqenc::point_table(); 62for ( int i = 0 ; i < 2048 ; ++ i ) hl [ i ] = tqw::f2h_rn( pt [ 2 * i ]) | (( uint32_t ) tqw::f2h_rn( pt [ 2 * i + 1 ]) << 16 ); 63} 64const uint32_t * dlut = up( hl ); 65std:: mt19937_64 rng ( 7 ); 66int fails = 0 ; 67const int types [ 8 ] = { 0 , 0 , 0 , 0 , 225 , 224 , 145 , 146 }; 68for ( int K : Ks ) { 69const int type = types [ K ], bsz = 2 + 4 * K ; 70const int rows = 96 , in = 1536 , nblk = in / 128 ; 71std:: vector < uint8_t > w (( size_t ) rows * nblk * bsz ); 72for ( auto & b : w ) b = ( uint8_t ) rng(); 73for ( int64_t b = 0 ; b < ( int64_t ) rows * nblk ; ++ b ) { const uint16_t d = tqw::f2h_rn( 0.02f + 0.001f * ( float ) (rng() % 400 )); std::memcpy( & w [( size_t ) b * bsz ], & d , 2 ); } 74std:: vector < float > dec (( size_t ) rows * in ); 75tqenc::decode_blocks( type , w .data(), ( int64_t ) rows * nblk , dec .data()); 76std:: vector < uint32_t > words ; std:: vector < uint16_t > sc ; 77tqw::repack_tqw( K , w .data(), rows , nblk , words , sc ); 78for ( int nb : { 1 , 3 , 5 }) { 79std:: vector < float > x (( size_t ) nb * in ); 80std:: uniform_real_distribution < float > u ( -1.f , 1.f ); 81for ( auto & v : x ) v = u( rng ); 82const uint32_t * dw = up( words ); const uint16_t * ds = up( sc ); const float * dx = up( x ); 83half8_t * xb ; CK(hipMalloc( & xb , ( size_t ) nblk * 8 * 2 * nb * 2 * sizeof ( half8_t ))); 84const size_t nall = ( size_t ) nblk * 8 * 2 * nb ; 85prep_b <<<( unsigned ) (( nall + 127 ) / 128 ), 128 > >>( dx , in , nb , in , xb ); 86float * dy ; CK(hipMalloc( & dy , ( size_t ) nb * rows * 4 )); CK(hipMemset( dy , 0 , ( size_t ) nb * rows * 4 )); 87std:: vector < Job > jv = { Job { dw , ds , xb , dy , rows , in , rows }}; 88const Job * dj = up( jv ); 89run( code , K , nb , dj , dlut , rows , 1 ); 90CK(hipDeviceSynchronize()); 91std:: vector < float > y (( size_t ) nb * rows ); 92CK(hipMemcpy( y .data(), dy , y .size() * 4 , hipMemcpyDeviceToHost )); 93double worst = 0 ; 94for ( int c = 0 ; c < nb ; ++ c ) 95for ( int r = 0 ; r < rows ; ++ r ) { 96double s = 0 , sa = 0 ; 97for ( int i = 0 ; i < in ; ++ i ) { const double t = ( double ) dec [( size_t ) r * in + i ] * x [( size_t ) c * in + i ]; s += t ; sa += std::fabs( t ); } 98worst = std::max( worst , std::fabs( y [( size_t ) c * rows + r ] - s ) / ( sa + 1e-30 )); 99} 100const bool ok = worst < 3e-7 ; 101std::printf( "K=%d nb=%d (%s): HIP wmma kernel vs double reference %.2e %s\n" , K , nb , code ? "computed" : "table" , worst , ok ? "ok" : "FAIL" ); 102fails += ! ok ; 103} 104} 105if ( bench ) { 106for ( int K : Ks ) { 107const int bsz = 2 + 4 * K , in = 5120 , rows = 2304 , nblk = in / 128 ; 108std:: vector < uint8_t > w (( size_t ) rows * nblk * bsz ); 109for ( auto & b : w ) b = ( uint8_t ) rng(); 110std:: vector < uint32_t > words ; std:: vector < uint16_t > sc ; 111tqw::repack_tqw( K , w .data(), rows , nblk , words , sc ); 112for ( int nb : { 1 , 5 }) { 113std:: vector < float > x (( size_t ) nb * in , 0.5f ); 114const float * dx = up( x ); 115half8_t * xb ; CK(hipMalloc( & xb , ( size_t ) nblk * 8 * 2 * nb * 2 * sizeof ( half8_t ))); 116const size_t nall = ( size_t ) nblk * 8 * 2 * nb ; 117prep_b <<<( unsigned ) (( nall + 127 ) / 128 ), 128 > >>( dx , in , nb , in , xb ); 118float * dy ; CK(hipMalloc( & dy , ( size_t ) nb * rows * 4 )); 119std:: vector < Job > jv ; // 12 distinct buffers, so the L2 cannot serve a repeated matrix 120for ( int j = 0 ; j < 12 ; ++ j ) jv .push_back( Job {up( words ), up( sc ), xb , dy , rows , in , rows }); 121const Job * dj = up( jv ); 122const double bytes = 12.0 * rows * nblk * bsz ; 123for ( int rep = 0 ; rep < 3 ; ++ rep ) run( code , K , nb , dj , dlut , rows , 12 ); 124CK(hipDeviceSynchronize()); 125const auto t0 = std::chrono::steady_clock:: now (); 126const int reps = 50 ; 127for ( int rep = 0 ; rep < reps ; ++ rep ) run( code , K , nb , dj , dlut , rows , 12 ); 128CK(hipDeviceSynchronize()); 129const double us = std::chrono::duration < double , std:: micro > (std::chrono::steady_clock:: now () - t0 ).count() / reps ; 130std::printf( "bench K=%d nb=%d %s: 12 gate/up matrices %.1f us, %.0f GB/s, %.2f Tw/s\n" , K , nb , code ? "computed" : "table" , us , bytes / us / 1e3 , 13112.0 * rows * in / us / 1e6 ); 132} 133} 134} 135std::printf( "%s\n" , fails ? "FAILED" : "ALL OK" ); 136return fails ; 137}