okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b2de2641f
main
1#include "ssm_conv.hpp" 2#include "common.hpp" 3#include "element_wise.hpp" 4 5#include <cstdio> 6 7using namespace sycl ; 8 9// One output element of the conv. DC is d_conv as a compile-time constant (0 keeps the 10// runtime loop); unfused callers pass literal false/nullptr so the epilogue folds away. 11template < int DC > 12static __dpct_inline__ void ssm_conv_element ( 13size_t idx , 14const float * src_data , 15const float * weights , 16float * dst_data , 17int d_conv , 18int d_inner , 19int n_t , 20int src_stride_inner , 21int src_stride_seq , 22int dst_stride_token , 23int dst_stride_seq , 24bool apply_silu , 25const float * bias 26) { 27// src is token-contiguous per channel, dst is channel-contiguous per token, 28// so indexing token-fastest coalesces the d_conv loads. 29const int token = static_cast < int > (idx %n_t ); 30const int channel = static_cast < int > ((idx /n_t ) %d_inner ); 31const int seq = static_cast < int > (idx / (static_cast < size_t > (n_t )* static_cast < size_t > (d_inner ))); 32 33const float * s = src_data 34+ static_cast < size_t > (seq )* static_cast < size_t > (src_stride_seq ) 35+ static_cast < size_t > (channel )* static_cast < size_t > (src_stride_inner ) 36+ static_cast < size_t > (token ); 37 38const float * c = weights + static_cast < size_t > (channel )* static_cast < size_t > (d_conv ); 39 40float sumf = 0.0f ; 41if constexpr (DC > 0 ) { 42#pragma unroll 43for (int i0 = 0 ;i0 < DC ;++ i0 ) { 44sumf += s [i0 ]* c [i0 ]; 45 } 46 }else { 47for (int i0 = 0 ;i0 < d_conv ;++ i0 ) { 48sumf += s [i0 ]* c [i0 ]; 49 } 50 } 51 52// fused bias add: the ADD node broadcasts a 1-D channel bias over tokens 53if (bias != nullptr ) { 54sumf += bias [channel ]; 55 } 56 57const size_t dst_idx = 58static_cast < size_t > (seq )* static_cast < size_t > (dst_stride_seq )+ 59static_cast < size_t > (token )* static_cast < size_t > (dst_stride_token )+ 60static_cast < size_t > (channel ); 61 62dst_data [dst_idx ]= apply_silu ?op_silu (sumf ) :sumf ; 63} 64 65// FUSED=false keeps apply_silu/bias out of the kernel capture list, so the unfused launch 66// takes the pre-fusion argument list; matters at n_t == 1, where the op is launch-bound. 67template < int DC ,bool FUSED > 68static void kernel_ssm_conv_impl ( 69queue & q , 70const float * src_data , 71const float * weights , 72float * dst_data , 73int d_conv , 74int d_inner , 75int n_t , 76int n_s , 77int ncs __attribute__((unused )), 78int src_stride_inner , 79int src_stride_seq , 80int dst_stride_token , 81int dst_stride_seq , 82bool apply_silu , 83const float * bias 84) { 85const size_t total_work = static_cast < size_t > (d_inner )* static_cast < size_t > (n_t )* static_cast < size_t > (n_s ); 86const size_t work_group_size = 256 ; 87const size_t num_work_groups = (total_work + work_group_size - 1 ) /work_group_size ; 88 89const range < 1 > global_range (num_work_groups * work_group_size ); 90const range < 1 > local_range (work_group_size ); 91 92if constexpr (FUSED ) { 93q .submit ([& ](handler & h ) { 94h .parallel_for ( 95nd_range < 1 > (global_range ,local_range ), 96 [= ](nd_item < 1 > item ) { 97const size_t idx = item .get_global_id (0 ); 98if (idx >=total_work ) { 99return ; 100 } 101 102ssm_conv_element < DC > (idx ,src_data ,weights ,dst_data ,d_conv ,d_inner ,n_t , 103src_stride_inner ,src_stride_seq ,dst_stride_token , 104dst_stride_seq ,apply_silu ,bias ); 105 } 106 ); 107 }); 108 }else { 109GGML_UNUSED (apply_silu ); 110GGML_UNUSED (bias ); 111 112q .submit ([& ](handler & h ) { 113h .parallel_for ( 114nd_range < 1 > (global_range ,local_range ), 115 [= ](nd_item < 1 > item ) { 116const size_t idx = item .get_global_id (0 ); 117if (idx >=total_work ) { 118return ; 119 } 120 121ssm_conv_element < DC > (idx ,src_data ,weights ,dst_data ,d_conv ,d_inner ,n_t , 122src_stride_inner ,src_stride_seq ,dst_stride_token , 123dst_stride_seq , false,nullptr ); 124 } 125 ); 126 }); 127 } 128} 129 130// SLM transpose tile: coalesces both the loads and the stores. The +1 pad makes the row 131// stride 33, coprime with 32 banks, so both phases are bank-conflict-free. 132template < int DC ,int TT ,int TC ,int WG > 133static __dpct_inline__ void ssm_conv_tile ( 134nd_item < 1 > it ,local_accessor < float ,1 > tile ,const float * src_data ,const float * weights , 135float * dst_data ,int n_t ,int nt_tiles ,int nc_tiles ,int src_stride_inner , 136int src_stride_seq ,int dst_stride_token ,int dst_stride_seq ,bool apply_silu , 137const float * bias 138) { 139const int lid = static_cast < int > (it .get_local_id (0 )); 140const size_t g = it .get_group (0 ); 141const int tt = static_cast < int > (g %nt_tiles ); 142const int ct = static_cast < int > ((g /nt_tiles ) %nc_tiles ); 143const int seq = static_cast < int > (g / (static_cast < size_t > (nt_tiles )* nc_tiles )); 144const int t0 = tt * TT ,c0 = ct * TC ; 145 146const int ti = lid %TT ; 147const int cj = lid /TT ; 148#pragma unroll 149for (int r = 0 ;r < TC / (WG /TT );++ r ) { 150const int c = cj + r * (WG /TT ); 151const int tok = t0 + ti ; 152float sumf = 0.0f ; 153if (tok < n_t ) { 154const float * s = src_data + static_cast < size_t > (seq )* src_stride_seq 155+ static_cast < size_t > (c0 + c )* src_stride_inner + tok ; 156const float * cw = weights + static_cast < size_t > (c0 + c )* DC ; 157#pragma unroll 158for (int i = 0 ;i < DC ;++ i )sumf += s [i ]* cw [i ]; 159if (bias != nullptr )sumf += bias [c0 + c ]; 160if (apply_silu )sumf = op_silu (sumf ); 161 } 162tile [c * (TT + 1 )+ ti ]= sumf ; 163 } 164it .barrier (access::fence_space::local_space ); 165 166const int cc = lid %TC ; 167const int tj = lid /TC ; 168#pragma unroll 169for (int r = 0 ;r < TT / (WG /TC );++ r ) { 170const int t = tj + r * (WG /TC ); 171const int tok = t0 + t ; 172if (tok < n_t ) { 173dst_data [static_cast < size_t > (seq )* dst_stride_seq 174+ static_cast < size_t > (tok )* dst_stride_token + c0 + cc ] 175= tile [cc * (TT + 1 )+ t ]; 176 } 177 } 178} 179 180// Same FUSED split as kernel_ssm_conv_impl. The fused instantiation keeps the runtime 181// apply_silu/bias branches: at n_t >= 32 they are amortized over the whole tile. 182template < int DC ,bool FUSED > 183static void kernel_ssm_conv_tiled ( 184queue & q ,const float * src_data ,const float * weights ,float * dst_data , 185int d_inner ,int n_t ,int n_s ,int src_stride_inner ,int src_stride_seq , 186int dst_stride_token ,int dst_stride_seq ,bool apply_silu ,const float * bias 187) { 188constexpr int TT = 32 ,TC = 32 ,WG = 256 ; 189const int nt_tiles = (n_t + TT - 1 ) /TT ; 190const int nc_tiles = d_inner /TC ; 191const size_t groups = static_cast < size_t > (nt_tiles )* nc_tiles * n_s ; 192 193if constexpr (FUSED ) { 194q .submit ([& ](handler & h ) { 195local_accessor < float ,1 > tile (range < 1 > (TC * (TT + 1 )),h ); 196h .parallel_for (nd_range < 1 > (range < 1 > (groups * WG ),range < 1 > (WG )), [= ](nd_item < 1 > it ) { 197ssm_conv_tile < DC ,TT ,TC ,WG > (it ,tile ,src_data ,weights ,dst_data ,n_t ,nt_tiles , 198nc_tiles ,src_stride_inner ,src_stride_seq , 199dst_stride_token ,dst_stride_seq ,apply_silu ,bias ); 200 }); 201 }); 202 }else { 203GGML_UNUSED (apply_silu ); 204GGML_UNUSED (bias ); 205 206q .submit ([& ](handler & h ) { 207local_accessor < float ,1 > tile (range < 1 > (TC * (TT + 1 )),h ); 208h .parallel_for (nd_range < 1 > (range < 1 > (groups * WG ),range < 1 > (WG )), [= ](nd_item < 1 > it ) { 209ssm_conv_tile < DC ,TT ,TC ,WG > (it ,tile ,src_data ,weights ,dst_data ,n_t ,nt_tiles , 210nc_tiles ,src_stride_inner ,src_stride_seq , 211dst_stride_token ,dst_stride_seq , false,nullptr ); 212 }); 213 }); 214 } 215} 216 217static void kernel_ssm_conv ( 218queue & q , 219const float * src_data , 220const float * weights , 221float * dst_data , 222int d_conv , 223int d_inner , 224int n_t , 225int n_s , 226int ncs , 227int src_stride_inner , 228int src_stride_seq , 229int dst_stride_token , 230int dst_stride_seq , 231bool apply_silu , 232const float * bias 233) { 234// Only the fused instantiations carry apply_silu/bias as kernel arguments; the plain 235// ssm_conv launch keeps the argument list it had before the fusion landed. 236const bool fused = apply_silu || bias != nullptr ; 237 238// d_inner must be a multiple of 32 so the channel tiles are exact; the transpose is only 239// worth it for n_t >= 32. d_conv == 4 is the only window with a DC-specialized kernel. 240if (d_conv == 4 && n_t >=32 && (d_inner %32 )== 0 ) { 241if (fused ) { 242kernel_ssm_conv_tiled < 4 , true> (q ,src_data ,weights ,dst_data ,d_inner ,n_t ,n_s , 243src_stride_inner ,src_stride_seq ,dst_stride_token , 244dst_stride_seq ,apply_silu ,bias ); 245 }else { 246kernel_ssm_conv_tiled < 4 , false> (q ,src_data ,weights ,dst_data ,d_inner ,n_t ,n_s , 247src_stride_inner ,src_stride_seq ,dst_stride_token , 248dst_stride_seq ,apply_silu ,bias ); 249 } 250return ; 251 } 252 253if (d_conv == 4 ) { 254if (fused ) { 255kernel_ssm_conv_impl < 4 , true> (q ,src_data ,weights ,dst_data ,d_conv ,d_inner ,n_t ,n_s , 256ncs ,src_stride_inner ,src_stride_seq ,dst_stride_token , 257dst_stride_seq ,apply_silu ,bias ); 258 }else { 259kernel_ssm_conv_impl < 4 , false> (q ,src_data ,weights ,dst_data ,d_conv ,d_inner ,n_t ,n_s , 260ncs ,src_stride_inner ,src_stride_seq ,dst_stride_token , 261dst_stride_seq ,apply_silu ,bias ); 262 } 263return ; 264 } 265 266if (fused ) { 267kernel_ssm_conv_impl < 0 , true> (q ,src_data ,weights ,dst_data ,d_conv ,d_inner ,n_t ,n_s , 268ncs ,src_stride_inner ,src_stride_seq ,dst_stride_token , 269dst_stride_seq ,apply_silu ,bias ); 270 }else { 271kernel_ssm_conv_impl < 0 , false> (q ,src_data ,weights ,dst_data ,d_conv ,d_inner ,n_t ,n_s , 272ncs ,src_stride_inner ,src_stride_seq ,dst_stride_token , 273dst_stride_seq ,apply_silu ,bias ); 274 } 275} 276 277inline void ggml_sycl_op_ssm_conv (ggml_backend_sycl_context & ctx ,ggml_tensor * dst ,ggml_tensor * silu_dst = nullptr ,const float * bias = nullptr ) { 278ggml_tensor * src0 = dst -> src [0 ]; 279ggml_tensor * src1 = dst -> src [1 ]; 280 281GGML_ASSERT (src0 -> type == GGML_TYPE_F32 ); 282GGML_ASSERT (src1 -> type == GGML_TYPE_F32 ); 283GGML_ASSERT (dst -> type == GGML_TYPE_F32 ); 284GGML_ASSERT (bias == nullptr || silu_dst != nullptr ); 285 286const int d_conv = src1 -> ne [0 ]; 287const int ncs = src0 -> ne [0 ]; 288const int d_inner = src0 -> ne [1 ]; 289const int n_t = dst -> ne [1 ]; 290const int n_s = dst -> ne [2 ]; 291 292GGML_ASSERT (src0 -> ne [0 ]== d_conv - 1 + n_t ); 293GGML_ASSERT (src0 -> ne [1 ]== d_inner ); 294GGML_ASSERT (src1 -> ne [1 ]== d_inner ); 295 296GGML_ASSERT (dst -> ne [0 ]== d_inner ); 297GGML_ASSERT (dst -> ne [1 ]== n_t ); 298GGML_ASSERT (dst -> ne [2 ]== n_s ); 299 300GGML_ASSERT (src0 -> nb [0 ]== sizeof (float )); 301GGML_ASSERT (src1 -> nb [0 ]== sizeof (float )); 302 303GGML_ASSERT (src0 -> nb [1 ]== src0 -> ne [0 ]* sizeof (float )); 304 305const int src_stride_inner = ncs ; 306const int src_stride_seq = ncs * d_inner ; 307const int dst_stride_token = d_inner ; 308const int dst_stride_seq = d_inner * n_t ; 309 310try { 311queue * q = ctx .stream (); 312 313const float * src_data = static_cast < const float *> (src0 -> data ); 314const float * weights = static_cast < const float *> (src1 -> data ); 315const bool apply_silu = silu_dst != nullptr ; 316float * dst_data = static_cast < float *> ((silu_dst ?silu_dst :dst )-> data ); 317 318GGML_ASSERT (src_data && weights && dst_data ); 319 320kernel_ssm_conv ( 321* q , 322src_data , 323weights , 324dst_data , 325d_conv , 326d_inner , 327n_t , 328n_s , 329ncs , 330src_stride_inner , 331src_stride_seq , 332dst_stride_token , 333dst_stride_seq , 334apply_silu , 335bias 336 ); 337 338 }catch (const std::exception & e ) { 339 std::fprintf (stderr ,"[SYCL-SSM_CONV] ERROR: %s\n" ,e .what ()); 340throw ; 341 } 342} 343 344void ggml_sycl_ssm_conv (ggml_backend_sycl_context & ctx ,ggml_tensor * dst ) { 345scope_op_debug_print scope_dbg_print (__func__ ,dst ,/*num_src=*/ 2 ); 346ggml_sycl_op_ssm_conv (ctx ,dst ); 347} 348 349// Fused ssm_conv + ADD + SiLU: write silu(conv(x) + b) straight into silu_dst, eliding the 350// standalone SiLU launch and its HBM round-trip of the conv output. 351void ggml_sycl_ssm_conv_fused (ggml_backend_sycl_context & ctx ,ggml_tensor * dst ,ggml_tensor * add ,ggml_tensor * silu_dst ) { 352scope_op_debug_print scope_dbg_print (__func__ ,dst ,/*num_src=*/ 2 ); 353GGML_ASSERT (silu_dst && ggml_are_same_shape (dst ,silu_dst )&& silu_dst -> type == GGML_TYPE_F32 ); 354// the fused kernel reads only the ADD's bias operand; the ADD result is never written 355const float * bias = nullptr ; 356if (add != nullptr ) { 357const ggml_tensor * bias_t = (add -> src [0 ]== dst ) ?add -> src [1 ] :add -> src [0 ]; 358bias = static_cast < const float *> (bias_t -> data ); 359 } 360ggml_sycl_op_ssm_conv (ctx ,dst ,silu_dst ,bias ); 361}