okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
90a1a9e1c
main
1#pragma once 2#include <algorithm> 3#include <cmath> 4#include <cstdint> 5#include <cstring> 6#include <stdexcept> 7#include <vector> 8 9namespace strata::ds4::scr_cpu { 10inline float e4value (unsigned q ) { 11unsigned e = q >>3 ,m = q & 7 ; 12return e == 0 ?m * 0.001953125f : std::ldexp (1.0f + m /8.0f , (int )e - 7 ); 13} 14inline uint8_t e4code (float x ) { 15unsigned best = 0 ; 16float dist = std::fabs (x ); 17for (unsigned q = 1 ;q < 127 ;++ q ) { 18float d = std::fabs (e4value (q )- x ); 19if (d < dist || (d == dist && (q & 1 )== 0 )) {best = q ;dist = d ; } 20 } 21return (uint8_t )best ; 22} 23inline uint8_t e2code (float x ) { 24const float a = std::fabs (x ); 25const unsigned m = a <=.25f ?0 :a < .75f ?1 :a <=1.25f ?2 :a < 1.75f ?3 : 26a <=2.5f ?4 :a < 3.5f ?5 :a <=5.f ?6 :7 ; 27return (uint8_t ) (m | (std::signbit (x ) ?8 :0 )); 28} 29inline size_t row_bytes (int fmt ,int len ) { 30return ((size_t )len /2 + len / (fmt == 2 ?16 :32 )+ 15 ) /16 * 16 ; 31} 32inline void geometry (int fmt ,int len ,int rd ) { 33if ((fmt != 2 && fmt != 3 )|| rd <=0 || rd > len || len %32 || rd %32 ) 34throw std::invalid_argument ("SCR requires whole RoPE quantization groups" ); 35} 36// Fresh finish without normalization: the input is the F32 activation captured after norm. 37inline std::vector < uint8_t > fresh (std::vector < float > x ,int rd ,int fmt ,int pos ,const std::vector < float >& freqs ) { 38const int len = (int )x .size (),group = fmt == 2 ?16 :32 ; 39geometry (fmt ,len ,rd ); 40for (int i = 0 ;i < rd /2 ;++ i ) { 41const float angle = (float )pos * freqs .at ((size_t )i ),c = std::cos (angle ),s = std::sin (angle ); 42const size_t k = (size_t ) (len - rd + 2 * i ); 43const float a = x [k ],b = x [k + 1 ]; 44x [k ]= std::fma (- b ,s ,a * c ); 45x [k + 1 ]= std::fma (a ,s ,b * c ); 46 } 47 std::vector < uint8_t > out (row_bytes (fmt ,len ),0 ); 48for (int k = 0 ;k < len ;k += group ) { 49float a = 0 ; 50for (int j = 0 ;j < group ;++ j )a = std::max (a , std::fabs (x [(size_t )k + j ])); 51float scale ; 52uint8_t code ; 53if (fmt == 2 ) { 54code = e4code (std::max (a ,6.f * .001953125f ) /6.f ); 55scale = e4value (code ); 56 }else { 57const float v = std::max (a ,6.f * 1.1754943508222875e-38f )* (1.f /6.f ); 58int e ; 59const float m = std::frexp (v ,& e ); 60scale = std::ldexp (1.f ,e - (m == .5f ?1 :0 )); 61uint32_t bits ; std::memcpy (& bits ,& scale ,4 ); 62code = (uint8_t ) (bits >>23 ); 63 } 64out [(size_t )len /2 + k /group ]= code ; 65for (int j = 0 ;j < group ;j += 2 ) { 66const uint8_t lo = e2code (std::clamp (x [(size_t )k + j ] /scale ,-6.f ,6.f )); 67const uint8_t hi = e2code (std::clamp (x [(size_t )k + j + 1 ] /scale ,-6.f ,6.f )); 68out [((size_t )k + j ) /2 ]= (uint8_t ) (lo |hi <<4 ); 69 } 70 } 71return out ; 72} 73inline std::vector < uint8_t > relocate (std::vector < uint8_t > packed ,const std::vector < float >& master , 74int len ,int fmt ,int pos ,const std::vector < float >& freqs ) { 75const int rd = (int )master .size (),group = fmt == 2 ?16 :32 ; 76geometry (fmt ,len ,rd ); 77if (packed .size ()!= row_bytes (fmt ,len ))throw std::invalid_argument ("SCR packed row size" ); 78auto tail = fresh (master ,rd ,fmt ,pos ,freqs ); 79 std::copy_n (tail .begin (),rd /2 ,packed .begin ()+ (len - rd ) /2 ); 80 std::copy_n (tail .begin ()+ rd /2 ,rd /group ,packed .begin ()+ len /2 + (len - rd ) /group ); 81return packed ; 82} 83}// namespace strata::ds4::scr_cpu