char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
31558dbb7
char/aliceai-t5-moe
1#include "ggml-impl.h" 2#include "dsv4-hc.hpp" 3 4#include <cmath> 5 6static constexpr int DSV4_HC = 4 ; 7 8static void dsv4_hc_pre_f32_sycl ( 9const float * x ,const float * weights ,float * dst , 10int64_t n_embd ,int64_t hc ,int64_t n_tokens , 11int64_t sx0 ,int64_t sx1 ,int64_t sx2 , 12int64_t sw0 ,int64_t sw1 , 13int64_t sd0 ,int64_t sd1 , 14queue_ptr stream ) { 15const int64_t nr = n_embd * n_tokens ; 16const int64_t block_size = 256 ; 17const int64_t num_blocks = (nr + block_size - 1 ) /block_size ; 18 19stream -> parallel_for ( 20 sycl::nd_range < 1 > (sycl::range < 1 > (num_blocks * block_size ), sycl::range < 1 > (block_size )), 21 [= ](sycl::nd_item < 1 > item ) { 22const int64_t ir = item .get_global_id (0 ); 23if (ir >=nr ) { 24return ; 25 } 26 27const int64_t i0 = ir %n_embd ; 28const int64_t it = ir /n_embd ; 29 30float sum = x [i0 * sx0 + it * sx2 ]* weights [it * sw1 ]; 31for (int64_t ih = 1 ;ih < hc ;++ ih ) { 32const float xv = x [i0 * sx0 + ih * sx1 + it * sx2 ]; 33const float wv = weights [ih * sw0 + it * sw1 ]; 34sum += xv * wv ; 35 } 36 37dst [i0 * sd0 + it * sd1 ]= sum ; 38 }); 39} 40 41static void dsv4_hc_comb_norm_cols (float * comb ,float eps ) { 42for (int idst = 0 ;idst < DSV4_HC ;++ idst ) { 43float sum = eps ; 44for (int isrc = 0 ;isrc < DSV4_HC ;++ isrc ) { 45sum += comb [idst + DSV4_HC * isrc ]; 46 } 47 48const float inv_sum = 1.0f /sum ; 49for (int isrc = 0 ;isrc < DSV4_HC ;++ isrc ) { 50comb [idst + DSV4_HC * isrc ] *=inv_sum ; 51 } 52 } 53} 54 55static void dsv4_hc_comb_norm_rows (float * comb ,float eps ) { 56for (int isrc = 0 ;isrc < DSV4_HC ;++ isrc ) { 57float sum = eps ; 58for (int idst = 0 ;idst < DSV4_HC ;++ idst ) { 59sum += comb [idst + DSV4_HC * isrc ]; 60 } 61 62const float inv_sum = 1.0f /sum ; 63for (int idst = 0 ;idst < DSV4_HC ;++ idst ) { 64comb [idst + DSV4_HC * isrc ] *=inv_sum ; 65 } 66 } 67} 68 69static void dsv4_hc_comb_f32_sycl ( 70const float * mixes , 71const float * scale , 72const float * base , 73float * dst , 74int64_t n_tokens , 75int64_t sm0 , 76int64_t sm1 , 77int64_t ss0 , 78int64_t sb0 , 79int64_t sd0 , 80int64_t sd1 , 81int64_t sd2 , 82float eps , 83int32_t n_iter , 84queue_ptr stream ) { 85constexpr int comb_offset = 2 * DSV4_HC ; 86 87const int64_t block_size = 256 ; 88const int64_t num_blocks = (n_tokens + block_size - 1 ) /block_size ; 89 90stream -> parallel_for ( 91 sycl::nd_range < 1 > (sycl::range < 1 > (num_blocks * block_size ), sycl::range < 1 > (block_size )), 92 [= ](sycl::nd_item < 1 > item_ct1 ) { 93const int64_t it = item_ct1 .get_global_id (0 ); 94 95if (it >=n_tokens ) { 96return ; 97 } 98 99const float scale_comb = scale [2 * ss0 ]; 100float comb [DSV4_HC * DSV4_HC ]; 101 102for (int isrc = 0 ;isrc < DSV4_HC ;++ isrc ) { 103float max = - INFINITY ; 104for (int idst = 0 ;idst < DSV4_HC ;++ idst ) { 105const int idx = idst + DSV4_HC * isrc ; 106const float v = mixes [(comb_offset + idx )* sm0 + it * sm1 ]* scale_comb + base [(comb_offset + idx )* sb0 ]; 107comb [idx ]= v ; 108max = fmaxf (max ,v ); 109 } 110 111float sum = 0.0f ; 112for (int idst = 0 ;idst < DSV4_HC ;++ idst ) { 113const int idx = idst + DSV4_HC * isrc ; 114const float v = expf (comb [idx ]- max ); 115comb [idx ]= v ; 116sum += v ; 117 } 118 119const float inv_sum = 1.0f /sum ; 120for (int idst = 0 ;idst < DSV4_HC ;++ idst ) { 121const int idx = idst + DSV4_HC * isrc ; 122comb [idx ]= comb [idx ]* inv_sum + eps ; 123 } 124 } 125 126dsv4_hc_comb_norm_cols (comb ,eps ); 127for (int32_t i = 1 ;i < n_iter ;++ i ) { 128dsv4_hc_comb_norm_rows (comb ,eps ); 129dsv4_hc_comb_norm_cols (comb ,eps ); 130 } 131 132for (int isrc = 0 ;isrc < DSV4_HC ;++ isrc ) { 133for (int idst = 0 ;idst < DSV4_HC ;++ idst ) { 134const int idx = idst + DSV4_HC * isrc ; 135dst [idst * sd0 + isrc * sd1 + it * sd2 ]= comb [idx ]; 136 } 137 } 138 }); 139} 140 141static void dsv4_hc_post_f32_sycl ( 142const float * x ,const float * residual ,const float * post ,const float * comb ,float * dst , 143int64_t n_embd ,int64_t hc ,int64_t n_tokens , 144int64_t sx0 ,int64_t sx1 , 145int64_t sr0 ,int64_t sr1 ,int64_t sr2 , 146int64_t sp0 ,int64_t sp1 , 147int64_t sc0 ,int64_t sc1 ,int64_t sc2 , 148int64_t sd0 ,int64_t sd1 ,int64_t sd2 , 149queue_ptr stream ) { 150const int64_t nr = n_embd * hc * n_tokens ; 151const int64_t block_size = 256 ; 152const int64_t num_blocks = (nr + block_size - 1 ) /block_size ; 153 154stream -> parallel_for ( 155 sycl::nd_range < 1 > (sycl::range < 1 > (num_blocks * block_size ), sycl::range < 1 > (block_size )), 156 [= ](sycl::nd_item < 1 > item ) { 157const int64_t ir = item .get_global_id (0 ); 158if (ir >=nr ) { 159return ; 160 } 161 162const int64_t i0 = ir %n_embd ; 163const int64_t idst = (ir /n_embd ) %hc ; 164const int64_t it = ir / (n_embd * hc ); 165 166float sum = x [i0 * sx0 + it * sx1 ]* post [idst * sp0 + it * sp1 ]; 167for (int64_t isrc = 0 ;isrc < hc ;++ isrc ) { 168sum += residual [i0 * sr0 + isrc * sr1 + it * sr2 ]* comb [idst * sc0 + isrc * sc1 + it * sc2 ]; 169 } 170 171dst [i0 * sd0 + idst * sd1 + it * sd2 ]= sum ; 172 }); 173} 174 175void ggml_sycl_op_dsv4_hc_pre (ggml_backend_sycl_context & ctx ,ggml_tensor * dst ) { 176scope_op_debug_print scope_dbg_print (__func__ ,dst ,/*num_src=*/ 2 ); 177const ggml_tensor * x = dst -> src [0 ]; 178const ggml_tensor * weights = dst -> src [1 ]; 179 180GGML_ASSERT (x -> type == GGML_TYPE_F32 ); 181GGML_ASSERT (weights -> type == GGML_TYPE_F32 ); 182GGML_ASSERT (dst -> type == GGML_TYPE_F32 ); 183 184GGML_TENSOR_LOCALS (size_t ,nbx ,x ,nb ); 185GGML_TENSOR_LOCALS (size_t ,nbw ,weights ,nb ); 186GGML_TENSOR_LOCALS (size_t ,nbd ,dst ,nb ); 187 188const int64_t n_embd = x -> ne [0 ]; 189const int64_t hc = x -> ne [1 ]; 190const int64_t n_tokens = x -> ne [2 ]; 191 192queue_ptr stream = ctx .stream (); 193 194dsv4_hc_pre_f32_sycl ( 195 (const float * )x -> data , (const float * )weights -> data , (float * )dst -> data , 196n_embd ,hc ,n_tokens , 197nbx0 /sizeof (float ),nbx1 /sizeof (float ),nbx2 /sizeof (float ), 198nbw0 /sizeof (float ),nbw1 /sizeof (float ), 199nbd0 /sizeof (float ),nbd1 /sizeof (float ), 200stream ); 201} 202 203void ggml_sycl_op_dsv4_hc_comb (ggml_backend_sycl_context & ctx ,ggml_tensor * dst ) { 204scope_op_debug_print scope_dbg_print (__func__ ,dst ,/*num_src=*/ 3 ); 205 206const ggml_tensor * mixes = dst -> src [0 ]; 207const ggml_tensor * scale = dst -> src [1 ]; 208const ggml_tensor * base = dst -> src [2 ]; 209 210GGML_ASSERT (mixes -> type == GGML_TYPE_F32 ); 211GGML_ASSERT (scale -> type == GGML_TYPE_F32 ); 212GGML_ASSERT (base -> type == GGML_TYPE_F32 ); 213GGML_ASSERT (dst -> type == GGML_TYPE_F32 ); 214 215constexpr int64_t hc_mix_dim = (2 + DSV4_HC )* DSV4_HC ; 216 217GGML_ASSERT (mixes -> ne [0 ]== hc_mix_dim ); 218GGML_ASSERT (dst -> ne [0 ]== DSV4_HC ); 219GGML_ASSERT (dst -> ne [1 ]== DSV4_HC ); 220GGML_ASSERT (dst -> ne [2 ]== mixes -> ne [1 ]); 221GGML_ASSERT (scale -> ne [0 ] >=3 ); 222GGML_ASSERT (base -> ne [0 ]== hc_mix_dim ); 223 224GGML_TENSOR_LOCALS (size_t ,nbm ,mixes ,nb ); 225GGML_TENSOR_LOCALS (size_t ,nbs ,scale ,nb ); 226GGML_TENSOR_LOCALS (size_t ,nbb ,base ,nb ); 227GGML_TENSOR_LOCALS (size_t ,nbd ,dst ,nb ); 228 229const int64_t n_tokens = mixes -> ne [1 ]; 230const float eps = ggml_get_op_params_f32 (dst ,0 ); 231const int32_t n_iter = ggml_get_op_params_i32 (dst ,1 ); 232 233queue_ptr stream = ctx .stream (); 234 235dsv4_hc_comb_f32_sycl ( 236 (const float * )mixes -> data , (const float * )scale -> data , (const float * )base -> data , (float * )dst -> data , 237n_tokens , 238nbm0 /sizeof (float ),nbm1 /sizeof (float ), 239nbs0 /sizeof (float ), 240nbb0 /sizeof (float ), 241nbd0 /sizeof (float ),nbd1 /sizeof (float ),nbd2 /sizeof (float ), 242eps ,n_iter ,stream ); 243} 244 245void ggml_sycl_op_dsv4_hc_post (ggml_backend_sycl_context & ctx ,ggml_tensor * dst ) { 246scope_op_debug_print scope_dbg_print (__func__ ,dst ,/*num_src=*/ 4 ); 247const ggml_tensor * x = dst -> src [0 ]; 248const ggml_tensor * residual = dst -> src [1 ]; 249const ggml_tensor * post = dst -> src [2 ]; 250const ggml_tensor * comb = dst -> src [3 ]; 251 252GGML_ASSERT (x -> type == GGML_TYPE_F32 ); 253GGML_ASSERT (residual -> type == GGML_TYPE_F32 ); 254GGML_ASSERT (post -> type == GGML_TYPE_F32 ); 255GGML_ASSERT (comb -> type == GGML_TYPE_F32 ); 256GGML_ASSERT (dst -> type == GGML_TYPE_F32 ); 257 258GGML_TENSOR_LOCALS (size_t ,nbx ,x ,nb ); 259GGML_TENSOR_LOCALS (size_t ,nbr ,residual ,nb ); 260GGML_TENSOR_LOCALS (size_t ,nbp ,post ,nb ); 261GGML_TENSOR_LOCALS (size_t ,nbc ,comb ,nb ); 262GGML_TENSOR_LOCALS (size_t ,nbd ,dst ,nb ); 263 264const int64_t n_embd = x -> ne [0 ]; 265const int64_t n_tokens = x -> ne [1 ]; 266const int64_t hc = residual -> ne [1 ]; 267 268queue_ptr stream = ctx .stream (); 269 270dsv4_hc_post_f32_sycl ( 271 (const float * )x -> data , (const float * )residual -> data , 272 (const float * )post -> data , (const float * )comb -> data , (float * )dst -> data , 273n_embd ,hc ,n_tokens , 274nbx0 /sizeof (float ),nbx1 /sizeof (float ), 275nbr0 /sizeof (float ),nbr1 /sizeof (float ),nbr2 /sizeof (float ), 276nbp0 /sizeof (float ),nbp1 /sizeof (float ), 277nbc0 /sizeof (float ),nbc1 /sizeof (float ),nbc2 /sizeof (float ), 278nbd0 /sizeof (float ),nbd1 /sizeof (float ),nbd2 /sizeof (float ), 279stream ); 280}