okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1a2649206
main
1// metal/src/ref_quant.h - CPU reference dequantizers for the parity programs, transcribed from ggml's 2// dequantize_row_* (ggml-quants.c at the pinned 3cf03257) and Strata's src/artifact/dequant.hpp (Q2_0), plus a 3// deterministic generator of random-but-valid blocks for synthetic weights. 4#pragma once 5 6#include <cstddef> 7#include <cstdint> 8#include <vector> 9 10namespace ref { 11 12// GGML type ids (stable, as in native_mmvq.hpp) 13enum :int { 14F32 = 0 ,F16 = 1 ,Q4_0 = 2 ,Q5_0 = 6 ,Q5_1 = 7 ,Q8_0 = 8 ,Q3_K = 11 ,Q4_K = 12 ,Q5_K = 13 ,Q6_K = 14 , 15IQ2_XXS = 16 ,IQ2_XS = 17 ,IQ3_XXS = 18 ,IQ1_S = 19 ,IQ1_M = 29 ,IQ3_S = 21 ,IQ2_S = 22 ,IQ4_NL = 20 ,IQ4_XS = 23 ,BF16 = 30 ,Q2_0 = 42 , 16TQ2_T = 144 ,TQK6 = 145 ,TQK7 = 146 ,// agentionai's trellis types (Gyro): 128 values a block, 34 / 26 / 30 bytes 17}; 18 19const char * type_name (int t ); 20int block_elems (int t ); 21int block_bytes (int t ); 22inline size_t row_bytes (int t ,int n ) {return (size_t )n /block_elems (t )* block_bytes (t ); } 23 24float f16_to_f32 (uint16_t h ); 25uint16_t f32_to_f16 (float f ); 26float bf16_to_f32 (uint16_t h ); 27uint16_t f32_to_bf16 (float f );// round to nearest even 28 29// Dequantizes `n` elements (a whole number of blocks) of type t. 30void dequant (int t ,const void * src ,float * dst ,size_t n ); 31 32// ggml's quantize_row_q8_0_ref: per 32 values d = max|x| / 127 (stored fp16), q = roundf(x * (1 / d)). 33void quantize_q8_0 (const float * x ,size_t n ,uint8_t * out ); 34 35// Fills `bytes` with random valid blocks of type t for a matrix of rows x cols: random codes, block scales drawn so 36// that the weights have roughly unit-free magnitude `scale` (the d fields are finite FP16). 37void random_blocks (int t ,uint8_t * dst ,int rows ,int cols ,uint64_t seed ,float scale = 0.02f ); 38 39struct Rng { 40uint64_t s ; 41explicit Rng (uint64_t seed ) :s (seed * 0x9E3779B97F4A7C15ull + 1 ) {} 42uint64_t next () {s ^=s <<13 ;s ^=s >>7 ;s ^=s <<17 ;return s ; } 43float uni () {return (next () >>40 )* (1.0f /16777216.0f ); }// [0,1) 44float sym () {return uni ()* 2.0f - 1.0f ; }// [-1,1) 45}; 46 47}// namespace ref