okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1e3a9a470
main
1// #835: the FP16-in / FP16-out prompt GEMMs (Gemm::set_f16_io, STRATA_HIP_PROMPT_F16=1) against a CPU reference. 2// f16() and bf16() (its weights BF16, its activations the FP16 image) write FP16 into Y's own rows and widen them in 3// place; the columns past N must stay untouched and a beta = 1 call must keep the ordinary FP32 path. The tolerance 4// is FP16's: one rounding of each output. 5#include <hip/hip_runtime.h> 6#include <hip/hip_bfloat16.h> 7#include <hip/hip_fp16.h> 8#include "strata/prefill/gemm.hpp" 9#include <algorithm> 10#include <cmath> 11#include <cstdio> 12#include <cstdlib> 13#include <cstring> 14#include <random> 15#include <string> 16#include <vector> 17 18#define CHECK (call ) do { const auto e = (call); if (e != hipSuccess) { \ 19 std::fprintf(stderr, "%s: %s\n", #call, hipGetErrorString(e)); std::exit(2); } } while (0) 20 21struct Buffer { 22void * p = nullptr ; 23explicit Buffer (size_t bytes ) {CHECK (hipMalloc (& p ,bytes )); } 24 ~Buffer () {if (p ) (void )hipFree (p ); } 25}; 26 27// bf16_w: the weights are BF16 (the bf16() entry); the activations are always the FP16 image on this path. 28bool run (strata::prefill::Gemm & gemm ,hipStream_t stream ,bool bf16_w ,int t ,int n ,int k ,int ldy ,float beta ) { 29 std::mt19937 rng (835 + t + n + (bf16_w ?1 :0 )); 30 std::uniform_real_distribution < float > dist (-0.25f ,0.25f ); 31 std::vector < uint16_t > x ((size_t )t * k ),w ((size_t )n * k ); 32 std::vector < float > xf (x .size ()),wf (w .size ()); 33for (size_t i = 0 ;i < x .size ();++ i ) { 34const __half v = __float2half_rn (dist (rng )); 35 std::memcpy (& x [i ],& v ,2 ); 36xf [i ]= __half2float (v ); 37 } 38for (size_t i = 0 ;i < w .size ();++ i ) { 39if (bf16_w ) { 40// round to nearest even by hand (hip_bfloat16's host constructor gave zeros on ROCm 7.14) 41const float f = dist (rng ); 42uint32_t bits ; 43 std::memcpy (& bits ,& f ,4 ); 44bits += 0x7fffu + ((bits >>16 )& 1u ); 45w [i ]= (uint16_t ) (bits >>16 ); 46const uint32_t wide = static_cast < uint32_t > (w [i ]) <<16 ; 47 std::memcpy (& wf [i ],& wide ,4 ); 48 }else { 49const __half v = __float2half_rn (dist (rng )); 50 std::memcpy (& w [i ],& v ,2 ); 51wf [i ]= __half2float (v ); 52 } 53 } 54constexpr int offset = 5 ; 55 std::vector < float > initial (offset + (size_t )t * ldy + 8 ,-777.25f ); 56for (int r = 0 ;r < t ;++ r ) 57for (int c = 0 ;c < n ;++ c )initial [offset + (size_t )r * ldy + c ]= (r + c ) %17 * 0.03125f ; 58Buffer dx (x .size ()* 2 ),dw (w .size ()* 2 ),dy (initial .size ()* 4 ); 59CHECK (hipMemcpyAsync (dx .p ,x .data (),x .size ()* 2 ,hipMemcpyHostToDevice ,stream )); 60CHECK (hipMemcpyAsync (dw .p ,w .data (),w .size ()* 2 ,hipMemcpyHostToDevice ,stream )); 61CHECK (hipMemcpyAsync (dy .p ,initial .data (),initial .size ()* 4 ,hipMemcpyHostToDevice ,stream )); 62if (bf16_w )gemm .bf16 ((const uint16_t * )dx .p , (const uint16_t * )dw .p , (float * )dy .p + offset ,t ,n ,k ,ldy ,beta ); 63else gemm .f16 ((const uint16_t * )dx .p , (const uint16_t * )dw .p , (float * )dy .p + offset ,t ,n ,k ,ldy ,beta ); 64CHECK (hipStreamSynchronize (stream )); 65 std::vector < float > got (initial .size ()); 66CHECK (hipMemcpy (got .data (),dy .p ,got .size ()* 4 ,hipMemcpyDeviceToHost )); 67 std::vector < bool > active (got .size ()); 68double diff2 = 0 ,ref2 = 0 ,maximum = 0 ; 69bool ok = true; 70for (int r = 0 ;r < t ;++ r ) 71for (int c = 0 ;c < n ;++ c ) { 72const size_t j = offset + (size_t )r * ldy + c ; 73active [j ]= true; 74double ref = beta * initial [j ]; 75for (int i = 0 ;i < k ;++ i )ref += (double )xf [(size_t )r * k + i ]* wf [(size_t )c * k + i ]; 76const double d = (double )got [j ]- ref ; 77ok = ok && std::isfinite (got [j ]); 78diff2 += d * d ; 79ref2 += ref * ref ; 80maximum = std::max (maximum , std::abs (d )); 81 } 82for (size_t j = 0 ;j < got .size ();++ j ) 83if (!active [j ])ok = ok && got [j ]== initial [j ]; 84const double rel = std::sqrt (diff2 / std::max (ref2 ,1e-300 )); 85// one FP16 rounding of the output (beta = 0), or the ordinary FP32 path (beta = 1) 86ok = ok && rel < (beta == 0.0f ?2e-3 :1e-4 ); 87ok = ok && ref2 > 0 ;// an all-zero reference would pass anything 88 std::printf ("%s %s T=%d N=%d K=%d ldy=%d beta=%.1f rel_l2=%.3g max_abs=%.3g ref_rms=%.3g\n" ,ok ?"PASS" :"FAIL" , 89bf16_w ?"BF16W" :"F16" ,t ,n ,k ,ldy ,beta ,rel ,maximum , std::sqrt (ref2 / ((double )t * n ))); 90return ok ; 91} 92 93int main () { 94setvbuf (stdout ,nullptr ,_IONBF ,0 ); 95hipStream_t stream ; 96CHECK (hipStreamCreateWithFlags (& stream ,hipStreamNonBlocking )); 97bool ok = true; 98 { 99 strata::prefill::Gemm gemm ; 100 std::string error ; 101if (!gemm .init (stream ,2560 * 640 ,error )) { std::fprintf (stderr ,"%s\n" ,error .c_str ());return 2 ; } 102gemm .set_f16_io (true); 103ok = run (gemm ,stream , false,32 ,640 ,2560 ,648 ,0 )&& ok ;// N < ldy: the columns past N stay untouched 104ok = run (gemm ,stream , false,64 ,2560 ,640 ,2560 ,0 )&& ok ; 105ok = run (gemm ,stream , false,17 ,96 ,2560 ,96 ,0 )&& ok ;// T and N not multiples of the block 106ok = run (gemm ,stream , true,16 ,96 ,2560 ,100 ,0 )&& ok ;// BF16 weights through the FP16 scratch 107ok = run (gemm ,stream , true,48 ,640 ,2560 ,640 ,0 )&& ok ; 108ok = run (gemm ,stream , false,64 ,640 ,640 ,640 ,1 )&& ok ;// beta = 1 keeps the FP32 path 109 } 110CHECK (hipStreamDestroy (stream )); 111return ok ?0 :1 ; 112}