okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
36ef9d6e6
main
1// Device parity for the scalar HIP implementation of the optional native QSA scorer. 2#include <hip/hip_runtime.h> 3#include "strata/kernels/native_qsa_score.hpp" 4 5#include <cmath> 6#include <cstdio> 7#include <vector> 8 9#define CHECK (call ) \ 10 do { \ 11 const hipError_t error = (call); \ 12 if (error != hipSuccess) { \ 13 std::fprintf(stderr, "%s: %s\n", #call, hipGetErrorString(error)); \ 14 return 2; \ 15 } \ 16 } while (0) 17 18int main () { 19using namespace strata::kernels ; 20constexpr int kDim = 128 ,kHeads = 4 ,kBlock = 4 ; 21constexpr int kMaxCells = 8 ,kMaxBlocks = 3 ,kCells = 7 ; 22const QsaShapes shapes = qsa_real_shapes (); 23 24 std::vector < float > pooled (kMaxBlocks * kDim ),query (kHeads * kDim ),bias (kMaxBlocks ), 25scores (kMaxCells ,-123.0f ),expected (kMaxCells ,-123.0f ); 26const float head_sign [kHeads ]= {1.0f ,-1.0f ,1.0f ,-1.0f }; 27for (int row = 0 ;row < kMaxBlocks ;++ row ) 28for (int d = 0 ;d < kDim ;++ d ) 29pooled [row * kDim + d ]= static_cast < float > (row + 1 )* (1.0f + static_cast < float > (d %7 ) /8.0f ) /128.0f ; 30for (int head = 0 ;head < kHeads ;++ head ) 31for (int d = 0 ;d < kDim ;++ d ) 32query [head * kDim + d ]= head_sign [head ]* (1.0f + static_cast < float > ((head + d ) %5 ) /16.0f ) /16.0f ; 33for (int row = 0 ;row < kMaxBlocks ;++ row )bias [row ]= (static_cast < float > (row )- 1.0f ) /8.0f ; 34 35int32_t step [kStepCount ]= {kCells - 1 ,kCells ,kCells /kBlock ,static_cast < int32_t > (kCells )}; 36bool saw_positive_head = false,saw_negative_head = false; 37for (int row = 0 ;row <=step [kStepNBid ];++ row ) { 38float sum = 0.0f ; 39for (int head = 0 ;head < kHeads ;++ head ) { 40float dot = 0.0f ; 41for (int d = 0 ;d < kDim ;++ d ) 42dot = std::fma (pooled [row * kDim + d ],query [head * kDim + d ],dot ); 43saw_positive_head = saw_positive_head || dot > 0.0f ; 44saw_negative_head = saw_negative_head || dot < 0.0f ; 45sum = sum + (dot > 0.0f ?dot :0.0f ); 46 } 47sum = sum + bias [row ]; 48if (row == step [kStepNBid ]&& kCells %kBlock != 0 )sum = sum + 1.0e9f ; 49const int lo = row * kBlock ; 50const int hi = (row + 1 )* kBlock < kCells ? (row + 1 )* kBlock :kCells ; 51for (int cell = lo ;cell < hi ;++ cell )expected [cell ]= sum ; 52 } 53if (!saw_positive_head || !saw_negative_head ) { 54 std::fprintf (stderr ,"fixture did not exercise both positive and negative head scores\n" ); 55return 1 ; 56 } 57 58float * device_pooled = nullptr ,* device_query = nullptr ,* device_bias = nullptr ,* device_scores = nullptr ; 59int32_t * device_step = nullptr ; 60CHECK (hipMalloc (reinterpret_cast < void **> (& device_pooled ),pooled .size ()* sizeof (float ))); 61CHECK (hipMalloc (reinterpret_cast < void **> (& device_query ),query .size ()* sizeof (float ))); 62CHECK (hipMalloc (reinterpret_cast < void **> (& device_bias ),bias .size ()* sizeof (float ))); 63CHECK (hipMalloc (reinterpret_cast < void **> (& device_scores ),scores .size ()* sizeof (float ))); 64CHECK (hipMalloc (reinterpret_cast < void **> (& device_step ),sizeof (step ))); 65CHECK (hipMemcpy (device_pooled ,pooled .data (),pooled .size ()* sizeof (float ),hipMemcpyHostToDevice )); 66CHECK (hipMemcpy (device_query ,query .data (),query .size ()* sizeof (float ),hipMemcpyHostToDevice )); 67CHECK (hipMemcpy (device_bias ,bias .data (),bias .size ()* sizeof (float ),hipMemcpyHostToDevice )); 68CHECK (hipMemcpy (device_scores ,scores .data (),scores .size ()* sizeof (float ),hipMemcpyHostToDevice )); 69CHECK (hipMemcpy (device_step ,step ,sizeof (step ),hipMemcpyHostToDevice )); 70 71hipStream_t stream ; 72CHECK (hipStreamCreate (& stream )); 73native_qsa_score (device_pooled ,device_query ,device_bias ,shapes ,device_step ,kMaxBlocks ,kMaxCells , 74device_scores ,static_cast < void *> (stream )); 75CHECK (hipStreamSynchronize (stream )); 76CHECK (hipMemcpy (scores .data (),device_scores ,scores .size ()* sizeof (float ),hipMemcpyDeviceToHost )); 77 78bool ok = true; 79for (int cell = 0 ;cell < kMaxCells ;++ cell ) { 80if (scores [cell ]!= expected [cell ]) { 81 std::fprintf (stderr ,"score[%d] got %.9g, expected %.9g\n" ,cell ,scores [cell ],expected [cell ]); 82ok = false; 83 } 84 } 85 86CHECK (hipStreamDestroy (stream )); 87CHECK (hipFree (device_step )); 88CHECK (hipFree (device_scores )); 89CHECK (hipFree (device_bias )); 90CHECK (hipFree (device_query )); 91CHECK (hipFree (device_pooled )); 92if (!ok )return 1 ; 93 std::puts ("native QSA HIP scalar parity OK: FMA order, per-head ReLU, bias, partial tail and padding" ); 94return 0 ; 95}