okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
52d2d3ee2
main
1// tests/quant/nvfp4_decode_model_test.cpp - the CPU model of the fused prompt kernels' NVFP4 weight decode (src/prefill/moe_fused_iq.cu: load_unit / 2// convert<T_NVFP4>, ue4m3_half) against the engine's own CPU dequantizer (strata::quant, which is ggml's dequantize_row_nvfp4), no GPU, no ggml. 3// 4// The kernels turn a 36-byte block {uint8 d[4] (UE4M3, one per 16 values); uint8 qs[32] (E2M1 codes, split-half inside each 16-value group: byte j 5// of a group holds value j in its low and value 8 + j in its high nibble)} into, per 32-value sub-block ib (0, 1): eight 32-bit words of int8 (natural 6// value order; the E2M1 value doubled: 0, 1, 2, 3, 4, 6, 8, 12, signs by bit 3) and two scales s0 (values 0-15) and s1 (16-31) = the UE4M3 byte 7// halved. The weight is s * q, exact in FP32. This file holds the same decode written from the format's definition (not from the kernel's bit 8// tricks) and checks, for every scale byte and every code and for seeded random blocks, that 9// - the kernel's scale construction (the bit form of ue4m3_half, copied here word for word) equals the definition for all 256 bytes, 10// - model value s * q equals strata::quant::dequant_row (bit for bit), 11// - the int8 sums the mma forms stay inside 2^22 (the MAGIC-bias trick of the kernels needs |sum q a| < 2^22): worst case 16 * 12 * 127 per scale group. 12// The GPU side of the same statement (the real kernel code against ggml's own dequantizer) is tests/cuda/prefill_decode_golden.cu. 13#include <cmath> 14#include <cstdint> 15#include <cstdio> 16#include <cstring> 17#include <vector> 18 19#include "strata/quant/dequant.hpp" 20#include "strata/quant/types.hpp" 21 22namespace sq= strata::quant; 23 24static int g_fail = 0 ; 25#define CHECK (c , ...) \ 26 do { \ 27 if (!(c)) { \ 28 ++g_fail; \ 29 if (g_fail < 20) { \ 30 std::printf("FAIL line %d: ", __LINE__); \ 31 std::printf(__VA_ARGS__); \ 32 std::printf("\n"); \ 33 } \ 34 } \ 35 } while (0) 36 37namespace { 38 39// the format's definition: E2M1 (1 sign, 2 exponent, 1 mantissa bit), doubled, as an int 40int e2m1_doubled (int code ) { 41const int s = (code >>3 )& 1 ,e = (code >>1 )& 3 ,m = code & 1 ; 42// value = (-1)^s * (e == 0 ? m * 0.5 : (1 + m / 2) * 2^(e - 1)); doubled: e == 0: m, else (2 + m) * 2^(e - 1) 43const int mag = e == 0 ?m : (2 + m ) << (e - 1 ); 44return s ?- mag :mag ; 45} 46// UE4M3 (4 exponent bits bias 7, 3 mantissa bits, no sign, no infinity, 0x7F = NaN) halved; NaN reads as 0 (ggml's CPU and CUDA decoders) 47float ue4m3_half_def (uint8_t x ) { 48if (x == 0 || x == 0x7F )return 0.0f ; 49const int e = (x >>3 )& 0xF ,m = x & 7 ; 50const double v = e == 0 ? std::ldexp ((double )m ,-9 ) : std::ldexp (1.0 + m /8.0 ,e - 7 ); 51return (float ) (v * 0.5 ); 52} 53// moe_fused_iq.cu's ue4m3_half, the same statements 54float ue4m3_half_kernel (uint32_t x ) { 55x &=0xFF ; 56if (x == 0 || x == 0x7F )return 0.0f ; 57const uint32_t ex = (x >>3 )& 0xF ,man = x & 0x7 ; 58const uint32_t bits = ((ex + 119u ) <<23 ) | (man <<20 ); 59float f ; 60 std::memcpy (& f ,& bits ,4 ); 61return ex == 0 ? (float )man * 0.0009765625f :f ; 62} 63 64struct Sub { 65int8_t q [32 ]; 66float s0 ,s1 ; 67}; 68// sub-block ib of one block, from the definition 69Sub model_sub (const uint8_t * blk ,int ib ) { 70Sub r ; 71for (int g = 0 ;g < 2 ;++ g ) {// the two 16-value groups of the sub-block 72const int grp = 2 * ib + g ;// group 0..3 of the block 73for (int j = 0 ;j < 8 ;++ j ) { 74const uint8_t b = blk [4 + 8 * grp + j ]; 75r .q [16 * g + j ]= (int8_t )e2m1_doubled (b & 15 ); 76r .q [16 * g + 8 + j ]= (int8_t )e2m1_doubled (b >>4 ); 77 } 78 } 79r .s0 = ue4m3_half_def (blk [2 * ib ]); 80r .s1 = ue4m3_half_def (blk [2 * ib + 1 ]); 81return r ; 82} 83 84struct SplitMix { 85uint64_t s ; 86uint64_t next () { 87s += 0x9e3779b97f4a7c15ull ; 88uint64_t z = s ; 89z = (z ^ (z >>30 ))* 0xbf58476d1ce4e5b9ull ; 90z = (z ^ (z >>27 ))* 0x94d049bb133111ebull ; 91return z ^ (z >>31 ); 92 } 93}; 94 95}// namespace 96 97int main () { 98// 1. the scale byte: the kernel's bit construction against the definition, all 256 values 99for (int x = 0 ;x < 256 ;++ x ) { 100const float a = ue4m3_half_def ((uint8_t )x ),b = ue4m3_half_kernel ((uint32_t )x ); 101CHECK (std::memcmp (& a ,& b ,4 )== 0 ,"UE4M3 byte 0x%02x: definition %.9g, kernel form %.9g" ,x ,a ,b ); 102 } 103// the format's own anchors: 0x38 = 1.0 (exp 7, man 0) -> 0.5 halved; 0x7E = 448 -> 224; 0x01 = 2^-9 -> 2^-10 104CHECK (ue4m3_half_def (0x38 )== 0.5f && ue4m3_half_def (0x7E )== 224.0f && ue4m3_half_def (0x01 )== std::ldexp (1.0f ,-10 ),"anchors" ); 105 106// 2. every (scale byte, code) pair through the engine's dequantizer: one block whose four scale bytes and all 64 codes are controlled 107int64_t pairs = 0 ; 108for (int x = 0 ;x < 256 ;++ x ) { 109uint8_t blk [36 ]; 110for (int s = 0 ;s < 4 ;++ s )blk [s ]= (uint8_t )x ; 111for (int j = 0 ;j < 32 ;++ j )blk [4 + j ]= (uint8_t ) (((j & 7 )* 2 ) | (((j & 7 )* 2 + 1 ) <<4 ));// codes 0..15, each value position gets some code 112// rotate the codes over the 32 bytes so that over the 16 rounds every code meets every position 113for (int rot = 0 ;rot < 16 ;++ rot ) { 114for (int j = 0 ;j < 32 ;++ j )blk [4 + j ]= (uint8_t ) (((j + rot )& 15 ) | (((j + 2 * rot + 7 )& 15 ) <<4 )); 115float ref [64 ]; 116CHECK (sq::dequant_row (sq::Type ::NVFP4 ,blk ,ref ,64 ),"dequant_row NVFP4" ); 117for (int ib = 0 ;ib < 2 ;++ ib ) { 118const Sub m = model_sub (blk ,ib ); 119for (int j = 0 ;j < 32 ;++ j ) { 120const float mine = (j < 16 ?m .s0 :m .s1 )* (float )m .q [j ],want = ref [32 * ib + j ]; 121CHECK (std::memcmp (& mine ,& want ,4 )== 0 || (mine == 0.0f && want == 0.0f ),"scale 0x%02x rot %d sub %d value %d: model %.9g dequant %.9g" ,x ,rot ,ib ,j ,mine ,want ); 122++ pairs ; 123 } 124 } 125 } 126 } 127// 3. seeded random blocks (every byte random, as the golden test's): 4096 blocks, and the integer sums' bound 128SplitMix g {0x4e564650344d3300ull }; 129int64_t maxsum = 0 ; 130for (int b = 0 ;b < 4096 ;++ b ) { 131uint8_t blk [36 ]; 132for (int i = 0 ;i < 36 ;i += 8 ) { 133const uint64_t r = g .next (); 134for (int k = 0 ;k < 8 && i + k < 36 ;++ k )blk [i + k ]= (uint8_t ) (r >> (8 * k )); 135 } 136float ref [64 ]; 137CHECK (sq::dequant_row (sq::Type ::NVFP4 ,blk ,ref ,64 ),"dequant_row NVFP4 (random)" ); 138for (int ib = 0 ;ib < 2 ;++ ib ) { 139const Sub m = model_sub (blk ,ib ); 140for (int j = 0 ;j < 32 ;++ j ) { 141const float mine = (j < 16 ?m .s0 :m .s1 )* (float )m .q [j ],want = ref [32 * ib + j ]; 142CHECK (std::memcmp (& mine ,& want ,4 )== 0 || (mine == 0.0f && want == 0.0f ),"random block %d sub %d value %d: model %.9g dequant %.9g" ,b ,ib ,j ,mine ,want ); 143 } 144// worst int8 sum of the kernel's m16n8k16 (one scale group of 16) against codes +-127 145for (int h = 0 ;h < 2 ;++ h ) { 146int64_t sum = 0 ; 147for (int j = 16 * h ;j < 16 * h + 16 ;++ j )sum += std::abs ((int )m .q [j ])* 127 ; 148if (sum > maxsum )maxsum = sum ; 149 } 150 } 151 } 152CHECK (maxsum <=16 * 12 * 127 && 16 * 12 * 127 < (1 <<22 ),"int8 sum bound %lld" , (long long )maxsum ); 153 std::printf ("nvfp4_decode_model_test: %s (%lld value pairs, scale bytes 256 of 256, max |sum q a| per 16 values %lld of 2^22)\n" ,g_fail ?"FAIL" :"ok" , 154 (long long )pairs , (long long )maxsum ); 155return g_fail ?1 :0 ; 156}