okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fd19246dc
main
1#include "strata/latent/latent_moe.hpp" 2 3#include "strata/kernels/iq_kernels.hpp" 4 5#include <cuda_runtime.h> 6 7#include <cstring> 8#include <vector> 9 10namespace strata::latent { 11 12Plan build_plan (const Geometry & g ,const int32_t * topk ,const int32_t * labels ,int T ) { 13Plan p ; 14p .cap = T * g .K ; 15p .groups = g .E ; 16p .start .assign ((size_t )g .E + 1 ,0 ); 17p .dst .resize ((size_t )p .cap ); 18p .tok .resize ((size_t )p .cap ); 19 std::vector < int32_t > pos ((size_t )g .E ,0 ); 20for (int r = 0 ;r < p .cap ;++ r )p .start [(size_t )topk [r ]+ 1 ]++ ; 21for (int e = 0 ;e < g .E ;++ e )p .start [(size_t )e + 1 ]+= p .start [(size_t )e ]; 22pos = std::vector < int32_t > (p .start .begin (),p .start .end ()- 1 ); 23for (int r = 0 ;r < p .cap ;++ r ) { 24const int e = topk [r ]; 25const int q = pos [(size_t )e ]++ ; 26p .dst [(size_t )q ]= r ; 27p .tok [(size_t )q ]= (r /g .K )* g .G + labels [e ]; 28 } 29return p ; 30} 31 32bool run_layer (const Geometry & g ,const Inputs & in ,const DevicePlan & plan ,const Buffers & b ,void * stream ) { 33using namespace strata::kernels ; 34if (!in .d_q8 || !in .u_q8 || !in .x || (!in .route_group && (!in .topk || !in .labels ))|| !plan .ptr || 35 !plan .start || !plan .n || 36 !plan .dst || !plan .tok || !b .xq || !b .xlq || !b .uq || !b .scratch || !b .xl || !b .yl || !b .uin || 37 !b .uout || !b .y ) 38return false; 39 40// D: [T,H] Q8 x [G*ELL,H]^T -> [T,G*ELL] (Q4DU: fmt 8 Q8_0 / 4 Q4_0, from Inputs) 41quantize_q8_1_rows (in .x ,in .T , (int )g .H ,b .xq ,stream ); 42iq_mmvq (strata::kernels::lat_dense_fmt_to_type (in .fmt ),in .d_q8 ,b .xq ,b .xl , (int )g .H , (int ) (g .G * g .ELL ),in .T ,stream ); 43quantize_q8_1_rows (b .xl ,in .T * g .G , (int )g .ELL ,b .xlq ,stream ); 44 45// A: every expert is a group; activation row is token*G + group. 46const NativeExpertLayout L = native_expert_layout (40 ,40 , (int )g .ELL , (int )g .I ); 47if (!native_expert_supported (40 ,40 , (int )g .ELL , (int )g .I ))return false; 48native_expert_grouped (L ,plan .ptr ,plan .start ,plan .n ,plan .dst ,plan .tok ,plan .groups ,plan .cap ,b .xlq , 49b .scratch ,b .yl ,stream ,plan .groups ); 50 51// U: per group, gather the route rows, Q8 GEMM, scatter back. 52const size_t urow = (size_t ) (g .ELL /32 )* (in .fmt == 4 ?18 :34 );// Q4DU 53 std::vector < int32_t > routes ; 54routes .reserve ((size_t )plan .cap ); 55for (int grp = 0 ;grp < g .G ;++ grp ) { 56routes .clear (); 57for (int r = 0 ;r < plan .cap ;++ r ) { 58const int rg = in .route_group ?in .route_group [r ] :in .labels [in .topk [r ]]; 59if (rg == grp )routes .push_back (r ); 60 } 61const int n = (int )routes .size (); 62if (!n )continue ; 63for (int i = 0 ;i < n ;++ i ) 64cudaMemcpyAsync (b .uin + (size_t )i * g .ELL ,b .yl + (size_t )routes [i ]* g .ELL , (size_t )g .ELL * 4 , 65cudaMemcpyDeviceToDevice , (cudaStream_t )stream ); 66quantize_q8_1_rows (b .uin ,n , (int )g .ELL ,b .uq ,stream ); 67iq_mmvq (strata::kernels::lat_dense_fmt_to_type (in .fmt ),in .u_q8 + (size_t )grp * (size_t )g .H * urow ,b .uq ,b .uout , (int )g .ELL , (int )g .H ,n ,stream ); 68for (int i = 0 ;i < n ;++ i ) 69cudaMemcpyAsync (b .y + (size_t )routes [i ]* g .H ,b .uout + (size_t )i * g .H , (size_t )g .H * 4 , 70cudaMemcpyDeviceToDevice , (cudaStream_t )stream ); 71 } 72return true; 73} 74 75void combine (const Geometry & g ,const float * w ,const float * y ,float * out ,int T ) { 76 std::memset (out ,0 , (size_t )T * g .H * sizeof (float )); 77for (int t = 0 ;t < T ;++ t ) 78for (int j = 0 ;j < g .K ;++ j ) 79for (int h = 0 ;h < g .H ;++ h ) 80out [(size_t )t * g .H + h ]+= w [(size_t )t * g .K + j ]* y [(size_t ) (t * g .K + j )* g .H + h ]; 81} 82 83}// namespace strata::latent