okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
8ac8ce32e
main
1// Strata. The trellis routed experts (ds4_iq.cuh's is_tq types) on the CPU lane of the DeepSeek-V4.1 engine path: Strata's 2// own decode-and-dot kernels (include/strata/kernels/cpu/tq_cpu.hpp: AVX-512 / AVX2 / scalar, one result whatever the ISA) 3// over the file's rows read in place. The activations are the device's (ds4_gguf.cu's q8_quant: int8 with an F32 scale a 32 4// values), so an expert gives the same sums on either lane up to the order of the blocks' float sums. 5#include "ds4_gguf_cpu.hpp" 6 7#include <algorithm> 8#include <cmath> 9#include <cstdint> 10#include <cstring> 11#include <string> 12#include <vector> 13 14#if defined(STRATA_DSV41_TQ_CPU ) 15#include "strata/kernels/cpu/tq_cpu.hpp" 16#endif 17 18namespace strata::ds4::gguf { 19 20#if defined(STRATA_DSV41_TQ_CPU ) 21namespace kc= strata::kernels::cpu; 22namespace { 23 24int kbits_of (int type ) { 25switch (type ) { 26case 145 :return 6 ; 27case 146 :return 7 ; 28case 224 :case 226 :return 5 ; 29case 225 :case 227 :return 4 ; 30default :return 0 ; 31 } 32} 33constexpr int kActTq = 100 ;// IqCpu::gu_act / d_act of these types: the layout below 34 35// an activation of k values: k / 32 F32 scales, then k int8 36size_t act_bytes_of (int k ) {return (size_t )k /32 * 4 + (size_t )k ; } 37 38void unpack (const void * act ,int k , kc::tq_detail::TqAct & a ) { 39const uint8_t * p = (const uint8_t * )act ; 40 std::memcpy (a .d ,p , (size_t )k /32 * 4 ); 41const int8_t * q = (const int8_t * ) (p + (size_t )k /32 * 4 ); 42for (int i = 0 ;i < k ;++ i )a .q [i ]= q [i ]; 43} 44 45// dots of rows [r0, r1) of one matrix against nt activations: out[t][r] 46void dots (int type ,const uint8_t * w ,size_t rb ,int n ,const void * const * act ,int nt ,float * const * out ,int r0 ,int r1 ) { 47static thread_local std::vector < kc::tq_detail::TqAct > unp (kc::kTqMaxTokens ); 48const kc::tq_detail::TqAct * ap [kc::kTqMaxTokens ]; 49for (int t = 0 ;t < nt ;++ t ) { 50unpack (act [t ],n ,unp [(size_t )t ]); 51ap [t ]= & unp [(size_t )t ]; 52 } 53const int kb = kc::tq_kbits (type ); 54const int16_t * tab = kc::tq_state_table_for (type ); 55switch (kc::tq_isa ()) { 56case kc::TqIsa ::avx512 : kc::tq_detail::tq_rows_avx512 (kb ,tab ,w ,rb ,n ,ap ,nt ,out ,r0 ,r1 );break ; 57case kc::TqIsa ::avx2 : kc::tq_detail::tq_rows_avx2 (kb ,tab ,w ,rb ,n ,ap ,nt ,out ,r0 ,r1 );break ; 58default : kc::tq_detail::tq_rows_scalar (kb ,tab ,w ,rb ,n ,ap ,nt ,out ,r0 ,r1 );break ; 59 } 60} 61}// namespace 62 63bool tq_cpu_fmt (int tg ,int td ,int n_embd ,int n_ff ,IqCpu & f , std::string & err ) { 64if (kbits_of (tg )== 0 || kbits_of (td )== 0 ) { 65err = "not trellis types" ; 66return false; 67 } 68if (n_embd %128 != 0 || n_ff %128 != 0 || n_embd > kc::kTqMaxN || n_ff > kc::kTqMaxN ) { 69err = "the trellis CPU kernels take widths of 128 blocks up to " + std::to_string (kc::kTqMaxN ); 70return false; 71 } 72f .tg = tg ; 73f .td = td ; 74f .n_embd = n_embd ; 75f .n_ff = n_ff ; 76f .gu_act = f .d_act = kActTq ; 77f .gu_row = (size_t ) (n_embd /128 )* (2 + 4 * kbits_of (tg )); 78f .d_row = (size_t ) (n_ff /128 )* (2 + 4 * kbits_of (td )); 79f .act_bytes = act_bytes_of (n_embd ); 80f .h_bytes = act_bytes_of (n_ff ); 81return true; 82} 83 84// x (k values) -> the layout above (the device's q8_quant: sc = max|x| / 127, q = round-half-even(x / sc)) 85void tq_cpu_quant (const float * x ,int k ,void * dst ) { 86float * d = (float * )dst ; 87int8_t * q = (int8_t * ) ((uint8_t * )dst + (size_t )k /32 * 4 ); 88for (int b = 0 ;b < k /32 ;++ b ) { 89const float * xb = x + b * 32 ; 90float am = 0.0f ; 91for (int j = 0 ;j < 32 ;++ j )am = std::max (am , std::fabs (xb [j ])); 92const float sc = am /127.0f ; 93d [b ]= sc ; 94for (int j = 0 ;j < 32 ;++ j )q [b * 32 + j ]= (int8_t ) (am > 0.0f ? (int ) std::nearbyint (xb [j ] /sc ) :0 ); 95 } 96} 97 98void tq_cpu_gate_up (const IqCpu & f ,const uint8_t * gate ,const uint8_t * up ,const void * const * act ,int nt ,float * const * ff , 99int r0 ,int r1 ,float limit ) { 100static thread_local std::vector < float > tmp [kc::kTqMaxTokens ]; 101float * up_out [kc::kTqMaxTokens ]; 102for (int t = 0 ;t < nt ;++ t ) { 103tmp [t ].resize ((size_t )r1 ); 104up_out [t ]= tmp [t ].data (); 105 } 106dots (f .tg ,gate ,f .gu_row ,f .n_embd ,act ,nt ,ff ,r0 ,r1 ); 107dots (f .tg ,up ,f .gu_row ,f .n_embd ,act ,nt ,up_out ,r0 ,r1 ); 108for (int t = 0 ;t < nt ;++ t ) 109for (int r = r0 ;r < r1 ;++ r ) { 110float g = ff [t ][r ],u = up_out [t ][r ]; 111if (limit > 0.0f ) { 112g = std::min (g ,limit ); 113u = std::min (std::max (u ,- limit ),limit ); 114 } 115ff [t ][r ]= g / (1.0f + std::exp (- g ))* u ; 116 } 117} 118 119void tq_cpu_down (const IqCpu & f ,const uint8_t * down ,const void * const * hq ,int nt ,float * const * out ,int r0 ,int r1 ) { 120dots (f .td ,down ,f .d_row ,f .n_ff ,hq ,nt ,out ,r0 ,r1 ); 121} 122bool tq_cpu_type (int type ) {return kbits_of (type )> 0 ; } 123int tq_cpu_act () {return kActTq ; } 124#else 125bool tq_cpu_fmt (int ,int ,int ,int ,IqCpu & , std::string & err ) { 126err = "this build has no CPU trellis kernels (STRATA_NATIVE_EXPERTS=OFF)" ; 127return false; 128} 129void tq_cpu_quant (const float * ,int ,void * ) {} 130void tq_cpu_gate_up (const IqCpu & ,const uint8_t * ,const uint8_t * ,const void * const * ,int ,float * const * ,int ,int ,float ) {} 131void tq_cpu_down (const IqCpu & ,const uint8_t * ,const void * const * ,int ,float * const * ,int ,int ) {} 132bool tq_cpu_type (int ) {return false; } 133int tq_cpu_act () {return 100 ; } 134#endif 135 136}// namespace strata::ds4::gguf