okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
bcbf2caf3
main
1// src/kernels/cpu/hadamard.cpp - see include/strata/kernels/cpu/hadamard.hpp. 2#include "strata/kernels/cpu/hadamard.hpp" 3 4#include <cmath> 5#include <cstdlib> 6#include <fstream> 7#include <sstream> 8 9namespace strata::kernels::cpu { 10namespace { 11HadamardSpec g_spec ; 12} 13 14const HadamardSpec & hadamard_spec () {return g_spec ; } 15void hadamard_set (HadamardSpec h ) {g_spec = std::move (h ); } 16 17const float * hadamard_signs (int width ) { 18const auto it = g_spec .signs .find (width ); 19return it == g_spec .signs .end () ?nullptr :it -> second .data (); 20} 21 22bool hadamard_parse (const std::string & path ,int64_t n_layers ,HadamardSpec & h , std::string & err ) { 23 std::ifstream in (path ); 24if (!in ) {err = "cannot read " + path ;return false; } 25h = HadamardSpec {}; 26h .roles .assign ((size_t )n_layers ,0 ); 27 std::vector < uint8_t > seen ((size_t )n_layers ,0 ); 28 std::string line ; 29bool versioned = false; 30while (std::getline (in ,line )) { 31if (line .empty ())continue ; 32if (line [0 ]== '#' ) { 33static const char tag []= "# strata hadamard v" ; 34if (line .compare (0 ,sizeof tag - 1 ,tag )== 0 ) { 35if (std::atoi (line .c_str ()+ sizeof tag - 1 )!= 1 ) { 36err = path + ": an unknown hadamard.txt version (this engine reads v1)" ; 37return false; 38 } 39versioned = true; 40 } 41continue ; 42 } 43 std::istringstream ss (line ); 44 std::string key ; 45ss >>key ; 46if (key == "block" ) { 47long long b = 0 ; 48if (!(ss >>b )|| b < 2 || b > 4096 || (b & (b - 1 ))!= 0 || h .block ) { 49err = path + ": a bad or repeated block line: " + line ; 50return false; 51 } 52h .block = (int )b ; 53 }else if (key == "signs" ) { 54long long w = 0 ; 55if (!(ss >>w )|| w <=0 || w > (1 <<20 )|| h .signs .count ((int )w )) { 56err = path + ": a bad or repeated signs line" ; 57return false; 58 } 59 std::vector < float > v ((size_t )w ); 60for (auto & x :v ) { 61int s = 0 ; 62if (!(ss >>s )|| (s != 1 && s != -1 )) {err = path + ": sign values must be +1 or -1" ;return false; } 63x = (float )s ; 64 } 65 std::string extra ; 66if (ss >>extra ) {err = path + ": a signs line longer than its width" ;return false; } 67h .signs .emplace ((int )w , std::move (v )); 68 }else if (key == "layer" ) { 69long long l = -1 ; 70int g = -1 ,u = -1 ,d = -1 ; 71if (!(ss >>l >>g >>u >>d )|| l < 0 || l >=n_layers || (g |u |d )& ~1 || seen [(size_t )l ]) { 72err = path + ": a bad or repeated layer line: " + line ; 73return false; 74 } 75if (g != u ) {// gate and up read one activation: a transform for one of them only is not runnable 76err = path + ": layer " + std::to_string (l )+ " folds only one of gate/up" ; 77return false; 78 } 79seen [(size_t )l ]= 1 ; 80h .roles [(size_t )l ]= (uint8_t ) (g | (u <<1 ) | (d <<2 )); 81 }else { 82err = path + ": an unknown line: " + line ; 83return false; 84 } 85 } 86if (!versioned || h .block == 0 ) {err = path + ": no version header or no block line" ;return false; } 87for (const auto & [w ,v ] :h .signs ) 88if (w %h .block ) {err = path + ": sign width " + std::to_string (w )+ " is not whole blocks" ;return false; } 89return true; 90} 91 92void hadamard_rotate (float * x ,int n ,int block ,const float * signs ) { 93if (signs ) 94for (int i = 0 ;i < n ;++ i )x [i ] *=signs [i ]; 95const float scale = 1.0f / std::sqrt ((float )block ); 96for (int b0 = 0 ;b0 < n ;b0 += block ) { 97float * v = x + b0 ; 98// the in-place fast Walsh-Hadamard transform in natural (Sylvester) order: entry (-1)^popcount(r & c) 99for (int len = 1 ;len < block ;len <<=1 ) 100for (int i = 0 ;i < block ;i += len <<1 ) 101for (int j = i ;j < i + len ;++ j ) { 102const float a = v [j ],c = v [j + len ]; 103v [j ]= a + c ; 104v [j + len ]= a - c ; 105 } 106for (int i = 0 ;i < block ;++ i )v [i ] *=scale ; 107 } 108} 109 110}// namespace strata::kernels::cpu