okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
bd34d0d87
main
1// Q2_0 dequant must preserve signed zero when a negative scale meets code 1. 2// This caught HIP 5.7/gfx1012 folding the half product to positive zero. 3#include "strata/kernels/iq_kernels.hpp" 4#include <hip/hip_runtime.h> 5#include <hip/hip_fp16.h> 6#include <cstdint> 7#include <cstdio> 8#include <cstdlib> 9#include <cstring> 10#include <vector> 11 12#define CHECK (call ) do { const auto e = (call); if (e != hipSuccess) { \ 13 std::fprintf(stderr, "%s: %s\n", #call, hipGetErrorString(e)); std::exit(2); } } while (0) 14 15int main () { 16// GGUF Q2_0: one half scale followed by sixteen packed 2-bit bytes. 17struct Block {uint16_t d ;uint8_t qs [16 ]; }; 18 static_assert(sizeof (Block )== 18 ); 19constexpr int blocks = 16 ,values = blocks * 64 ; 20 std::vector < Block > input (blocks ); 21 std::vector < uint16_t > expected (values ),got (values ); 22for (int b = 0 ;b < blocks ;++ b ) { 23const float scale = (b & 1 ) ?-0.5f :0.5f ; 24const __half half_scale = __float2half (scale ); 25 std::memcpy (& input [b ].d ,& half_scale ,2 ); 26for (int i = 0 ;i < 16 ;++ i )input [b ].qs [i ]= 0xe4 ;// codes 0,1,2,3 27for (int i = 0 ;i < 64 ;++ i ) { 28const __half value = __float2half (scale * float ((i %4 )- 1 )); 29 std::memcpy (& expected [b * 64 + i ],& value ,2 ); 30 } 31 } 32void * weights = nullptr ; 33uint16_t * output = nullptr ; 34CHECK (hipMalloc (& weights ,input .size ()* sizeof (Block ))); 35CHECK (hipMalloc (reinterpret_cast < void **> (& output ),values * sizeof (uint16_t ))); 36CHECK (hipMemcpy (weights ,input .data (),input .size ()* sizeof (Block ),hipMemcpyHostToDevice )); 37 strata::kernels::iq_dequant_f16 (42 ,weights ,values ,output ,nullptr ); 38CHECK (hipDeviceSynchronize ()); 39CHECK (hipMemcpy (got .data (),output ,values * sizeof (uint16_t ),hipMemcpyDeviceToHost )); 40int failures = 0 ; 41for (int i = 0 ;i < values ;++ i ) { 42if (got [i ]== expected [i ])continue ; 43if (failures ++ < 4 ) 44 std::printf ("index=%d got=0x%04x expected=0x%04x\n" ,i ,unsigned (got [i ]),unsigned (expected [i ])); 45 } 46CHECK (hipFree (weights )); 47CHECK (hipFree (output )); 48 std::printf ("hip_q2_zero: %d values, %d failures\n" ,values ,failures ); 49return failures ?1 :0 ; 50}