okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
6ca60e9a0
main
1// src/kernels/ple_iq4nl_dequant_test.cpp - STRATA_PLE_COMPACT: ple_iq4nl_dequant (raw IQ4_NL PLE rows to floats on the 2// device) against iq4nl_dequant_row (the host decode the prompt path used before), bit for bit. Synthetic bytes, no model. 3// 4// - every one of the 65,536 fp16 scale patterns (infinities and NaNs included: those are compared as "both NaN") in 5// every block position, with random codes; 6// - rows of random bytes; 7// - the row the table gives for an index past its end (scale 0, code 8 in every nibble: +0.0f, what PleTable::read_row 8// writes for such a row); 9// - an odd row count (not a multiple of the 256-thread block). 10// Exit 77 without a CUDA device. 11#include "strata/kernels/ngram.hpp" 12#include "strata/kernels/ple.hpp" 13 14#include <cuda_runtime.h> 15 16#include <cmath> 17#include <cstdint> 18#include <cstdio> 19#include <cstring> 20#include <random> 21#include <vector> 22 23namespace { 24using namespace strata::kernels ; 25 26int check (const std::vector < uint8_t >& raw ,int64_t rows ,const char * what ) { 27float * dout = nullptr ; 28uint8_t * draw = nullptr ; 29if (cudaMalloc ((void ** )& dout , (size_t )rows * PLE_HEAD_DIM * 4 )!= cudaSuccess || 30cudaMalloc ((void ** )& draw ,raw .size ())!= cudaSuccess || 31cudaMemcpy (draw ,raw .data (),raw .size (),cudaMemcpyHostToDevice )!= cudaSuccess ) { 32 std::printf ("CUDA setup failed for %s\n" ,what ); 33return 1 ; 34 } 35cudaMemset (dout ,0xff , (size_t )rows * PLE_HEAD_DIM * 4 ); 36ple_iq4nl_dequant (draw ,rows ,dout ,nullptr ); 37if (cudaDeviceSynchronize ()!= cudaSuccess ) { std::printf ("CUDA error for %s: %s\n" ,what ,cudaGetErrorString (cudaGetLastError ()));return 1 ; } 38 std::vector < float > got ((size_t )rows * PLE_HEAD_DIM ),want (got .size ()); 39cudaMemcpy (got .data (),dout ,got .size ()* 4 ,cudaMemcpyDeviceToHost ); 40cudaFree (dout ); 41cudaFree (draw ); 42for (int64_t r = 0 ;r < rows ;++ r )iq4nl_dequant_row (raw .data ()+ (size_t )r * PLE_ROW_BYTES ,want .data ()+ (size_t )r * PLE_HEAD_DIM ); 43long long diff = 0 ,first = -1 ; 44for (size_t i = 0 ;i < got .size ();++ i ) { 45uint32_t a ,b ; 46 std::memcpy (& a ,& got [i ],4 ); 47 std::memcpy (& b ,& want [i ],4 ); 48if (std::isnan (got [i ])&& std::isnan (want [i ]))continue ;// a NaN scale: any payload is a NaN 49if (a != b ) {if (first < 0 )first = (long long )i ;++ diff ; } 50 } 51 std::printf (" %-52s %lld rows: %s\n" ,what , (long long )rows ,diff ?"*** DIFFERS ***" :"bitwise equal" ); 52if (diff ) std::printf (" %lld values differ, first at %lld\n" ,diff ,first ); 53return diff ?1 :0 ; 54} 55}// namespace 56 57int main () { 58int n_dev = 0 ; 59if (cudaGetDeviceCount (& n_dev )!= cudaSuccess || n_dev < 1 ) { std::printf ("no CUDA device (skipped)\n" );return 77 ; } 60int bad = 0 ; 61 std::mt19937 rng (20261026u ); 62 {// all fp16 patterns: 65,536 scales over 5 blocks a row = 13,108 rows (the last row pads with 0) 63const int64_t rows = (65536 + 4 ) /5 ; 64 std::vector < uint8_t > raw ((size_t )rows * PLE_ROW_BYTES ); 65for (auto & b :raw )b = (uint8_t ) (rng ()& 0xff ); 66uint32_t d = 0 ; 67for (int64_t r = 0 ;r < rows ;++ r ) 68for (int b = 0 ;b < PLE_HEAD_DIM /32 ;++ b ,++ d ) { 69const uint16_t h = (uint16_t ) (d & 0xffff ); 70 std::memcpy (& raw [(size_t )r * PLE_ROW_BYTES + (size_t )b * 18 ],& h ,2 ); 71 } 72bad += check (raw ,rows ,"every fp16 scale pattern, random codes" ); 73 } 74 {// random rows 75const int64_t rows = 4096 + 37 ; 76 std::vector < uint8_t > raw ((size_t )rows * PLE_ROW_BYTES ); 77for (auto & b :raw )b = (uint8_t ) (rng ()& 0xff ); 78bad += check (raw ,rows ,"random bytes (odd row count)" ); 79 } 80 {// the out-of-table row of PleTable::gather_raw_iq4nl 81const int64_t rows = 16 ; 82 std::vector < uint8_t > raw ((size_t )rows * PLE_ROW_BYTES ); 83for (int64_t r = 0 ;r < rows ;++ r ) 84for (int b = 0 ;b < PLE_HEAD_DIM /32 ;++ b ) { 85uint8_t * blk = & raw [(size_t )r * PLE_ROW_BYTES + (size_t )b * 18 ]; 86 std::memset (blk ,0 ,2 ); 87 std::memset (blk + 2 ,0x88 ,16 ); 88 } 89bad += check (raw ,rows ,"out-of-table row (scale 0, code 8)" ); 90// and it is +0.0f, not -0.0f, on both sides 91 std::vector < float > want (PLE_HEAD_DIM ); 92iq4nl_dequant_row (raw .data (),want .data ()); 93for (float v :want ) { 94uint32_t a ; 95 std::memcpy (& a ,& v ,4 ); 96if (a != 0 ) { std::printf (" *** the out-of-table row does not decode to +0.0f\n" );++ bad ;break ; } 97 } 98 } 99 std::printf ("ple_iq4nl_dequant_test: %s\n" ,bad ?"FAILED" :"OK" ); 100return bad ?1 :0 ; 101}