char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
704d90c98
char/aliceai-t5-moe
1#include <sycl/sycl.hpp> 2 3#include "common.hpp" 4 5template < u_int HEAD_SIZE > 6static void gated_linear_attn_f32_kernel (const dpct::queue_ptr stream ,u_int B ,u_int T ,u_int C ,u_int H ,float scale , 7const float * k ,const float * v ,const float * r ,const float * td , 8const float * s ,float * dst ) { 9const u_int head_size = HEAD_SIZE ; 10const u_int state_size = C * head_size ; 11const u_int n_seq_tokens = T /B ; 12 sycl::range < 1 > block_dims ((C /H )); 13 sycl::range < 1 > grid_dims ((B * H )); 14stream -> submit ([& ](sycl::handler & cgh ) { 15/* local memory accessors*/ 16auto _k = sycl::local_accessor < float ,1 > (sycl::range < 1 > (head_size ),cgh ); 17auto _r = sycl::local_accessor < float ,1 > (sycl::range < 1 > (head_size ),cgh ); 18auto _td = sycl::local_accessor < float ,1 > (sycl::range < 1 > (head_size ),cgh ); 19 20cgh .parallel_for (sycl::nd_range < 1 > (grid_dims * block_dims ,block_dims ), [= ](sycl::nd_item < 1 > item ) { 21u_int tid = item .get_local_id (0 ); 22u_int bid = item .get_group (0 ); 23 24u_int batch_i = bid /H ; 25u_int head_i = bid %H ; 26 27float state [head_size ]; 28 29#pragma unroll 30for (u_int i = 0 ;i < head_size ;i ++ ) { 31state [i ]= s [batch_i * state_size + head_i * head_size * head_size + i * head_size + tid ]; 32 } 33 34for (u_int t = batch_i * n_seq_tokens * C + head_i * head_size + tid ; 35t < (batch_i + 1 )* n_seq_tokens * C + head_i * head_size + tid ;t += C ) { 36 37item .barrier (sycl::access::fence_space::local_space );//sync threads 38_k [tid ]= k [t ]; 39_r [tid ]= r [t ]; 40_td [tid ]= td [t ]; 41item .barrier (sycl::access::fence_space::local_space );//sync threads 42 43const float _v = v [t ]; 44float y = 0 ; 45 46for (u_int j = 0 ;j < head_size ;j += 4 ) { 47const sycl::float4 & k = (sycl::float4 & ) (_k [j ]); 48const sycl::float4 & r = (sycl::float4 & ) (_r [j ]); 49const sycl::float4 & td = (sycl::float4 & ) (_td [j ]); 50 sycl::float4 & s = (sycl::float4 & ) (state [j ]); 51 sycl::float4 kv ; 52 53kv .x ()= k .x ()* _v ; 54kv .y ()= k .y ()* _v ; 55kv .z ()= k .z ()* _v ; 56kv .w ()= k .w ()* _v ; 57 58s .x ()= s .x ()* td .x ()+ kv .x (); 59s .y ()= s .y ()* td .y ()+ kv .y (); 60s .z ()= s .z ()* td .z ()+ kv .z (); 61s .w ()= s .w ()* td .w ()+ kv .w (); 62 63y += r .x ()* s .x (); 64y += r .y ()* s .y (); 65y += r .z ()* s .z (); 66y += r .w ()* s .w (); 67 } 68dst [t ]= y * scale ; 69 } 70#pragma unroll 71for (u_int i = 0 ;i < head_size ;i ++ ) { 72dst [T * C + batch_i * state_size + head_i * head_size * head_size + i * head_size + tid ]= state [i ]; 73 } 74 }); 75 }); 76} 77 78void ggml_sycl_op_gated_linear_attn (ggml_backend_sycl_context & ctx ,ggml_tensor * dst ) { 79scope_op_debug_print scope_dbg_print (__func__ ,dst ,/*num_src=*/ 5 ); 80const float * k_d = static_cast < const float *> (dst -> src [0 ]-> data ); 81const float * v_d = static_cast < const float *> (dst -> src [1 ]-> data ); 82const float * r_d = static_cast < const float *> (dst -> src [2 ]-> data ); 83const float * td_d = static_cast < const float *> (dst -> src [3 ]-> data ); 84const float * s_d = static_cast < const float *> (dst -> src [4 ]-> data ); 85 86const int64_t B = dst -> src [4 ]-> ne [1 ]; 87const int64_t T = dst -> src [0 ]-> ne [2 ]; 88const int64_t C = dst -> ne [0 ]; 89const int64_t H = dst -> src [0 ]-> ne [1 ]; 90 91 dpct::queue_ptr stream = ctx .stream (); 92GGML_ASSERT (dst -> src [4 ]-> type == GGML_TYPE_F32 ); 93GGML_ASSERT (C %H == 0 ); 94GGML_ASSERT (C /H == 64 || C /H == 128 ); 95 96float scale ; 97memcpy (& scale ,dst -> op_params ,sizeof (float )); 98 99float * dst_d = (float * )dst -> data ; 100 101if (C /H == 64 ) { 102gated_linear_attn_f32_kernel < 64 > (stream ,B ,T ,C ,H ,scale ,k_d ,v_d ,r_d ,td_d ,s_d ,dst_d ); 103 }else { 104gated_linear_attn_f32_kernel < 128 > (stream ,B ,T ,C ,H ,scale ,k_d ,v_d ,r_d ,td_d ,s_d ,dst_d ); 105 } 106}