char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
914dde72b
char/aliceai-t5-moe
1#include "unary-ops.h" 2 3static inline float op_abs (float x ) { 4return fabsf (x ); 5} 6 7static inline float op_sgn (float x ) { 8return (x > 0.f ) ?1.f : ((x < 0.f ) ?-1.f :0.f ); 9} 10 11static inline float op_neg (float x ) { 12return - x ; 13} 14 15static inline float op_step (float x ) { 16return (x > 0.f ) ?1.f :0.f ; 17} 18 19static inline float op_tanh (float x ) { 20return tanhf (x ); 21} 22 23static inline float op_elu (float x ) { 24return (x > 0.f ) ?x :expm1f (x ); 25} 26 27static inline float op_relu (float x ) { 28return (x > 0.f ) ?x :0.f ; 29} 30 31static inline float op_sigmoid (float x ) { 32return 1.f / (1.f + expf (- x )); 33} 34 35static inline float op_hardsigmoid (float x ) { 36return fminf (1.0f ,fmaxf (0.0f , (x + 3.0f ) /6.0f )); 37} 38 39static inline float op_exp (float x ) { 40return expf (x ); 41} 42 43static inline float op_hardswish (float x ) { 44return x * fminf (1.0f ,fmaxf (0.0f , (x + 3.0f ) /6.0f )); 45} 46 47static inline float op_sqr (float x ) { 48return x * x ; 49} 50 51static inline float op_sqrt (float x ) { 52return sqrtf (x ); 53} 54 55static inline float op_xielu (float x ,float alpha_n ,float alpha_p ,float beta ,float eps ) { 56if (x > 0.0f ) { 57return alpha_p * x * x + beta * x ; 58 }else { 59const float min_x_eps = fminf (x ,eps ); 60return (expm1f (min_x_eps )- x )* alpha_n + beta * x ; 61 } 62} 63 64static inline float op_sin (float x ) { 65return sinf (x ); 66} 67 68static inline float op_cos (float x ) { 69return cosf (x ); 70} 71 72static inline float op_log (float x ) { 73return logf (x ); 74} 75 76static inline float op_expm1 (float x ) { 77return expf (x )- 1.0f ; 78} 79 80static inline float op_softplus (float x ) { 81return (x > 20.0f ) ?x :logf (1.0f + expf (x )); 82} 83 84static inline float op_floor (float x ) { 85return floorf (x ); 86} 87 88static inline float op_ceil (float x ) { 89return ceilf (x ); 90} 91 92static inline float op_round (float x ) { 93return roundf (x ); 94} 95 96static inline float op_trunc (float x ) { 97return truncf (x ); 98} 99 100template < float (* op )(float ),typename src0_t ,typename dst_t > 101static inline void vec_unary_op (int64_t n ,dst_t * y ,const src0_t * x ) { 102constexpr auto src0_to_f32 = type_conversion_table < src0_t > ::to_f32 ; 103constexpr auto f32_to_dst = type_conversion_table < dst_t > ::from_f32 ; 104 105for (int i = 0 ;i < n ;i ++ ) { 106y [i ]= f32_to_dst (op (src0_to_f32 (x [i ]))); 107 } 108} 109 110template < float (* op )(float ),typename src0_t ,typename dst_t > 111static void apply_unary_op (const ggml_compute_params * params ,ggml_tensor * dst ) { 112const ggml_tensor * src0 = dst -> src [0 ]; 113 114GGML_ASSERT (ggml_is_contiguous_rows (src0 )&& ggml_is_contiguous_rows (dst )&& ggml_are_same_shape (src0 ,dst )); 115 116GGML_TENSOR_UNARY_OP_LOCALS 117 118GGML_ASSERT (nb0 == sizeof (dst_t )); 119GGML_ASSERT (nb00 == sizeof (src0_t )); 120 121const auto [ir0 ,ir1 ]= get_thread_range (params ,src0 ); 122 123for (int64_t ir = ir0 ;ir < ir1 ;++ ir ) { 124const int64_t i03 = ir /(ne02 * ne01 ); 125const int64_t i02 = (ir - i03 * ne02 * ne01 )/ne01 ; 126const int64_t i01 = (ir - i03 * ne02 * ne01 - i02 * ne01 ); 127 128dst_t * dst_ptr = (dst_t * ) ((char * )dst -> data + i03 * nb3 + i02 * nb2 + i01 * nb1 ); 129const src0_t * src0_ptr = (const src0_t * ) ((const char * )src0 -> data + i03 * nb03 + i02 * nb02 + i01 * nb01 ); 130 131vec_unary_op < op > (ne0 ,dst_ptr ,src0_ptr ); 132 } 133} 134 135// TODO: Use the 'traits' lookup table (for type conversion fns), instead of a mass of 'if' conditions with long templates 136template < float (* op )(float )> 137static void unary_op (const ggml_compute_params * params ,ggml_tensor * dst ) { 138const ggml_tensor * src0 = dst -> src [0 ]; 139 140/* */ if (src0 -> type == GGML_TYPE_F32 && dst -> type == GGML_TYPE_F32 ) {// all f32 141apply_unary_op < op ,float ,float > (params ,dst ); 142 }else if (src0 -> type == GGML_TYPE_F16 && dst -> type == GGML_TYPE_F16 ) {// all f16 143apply_unary_op < op ,ggml_fp16_t ,ggml_fp16_t > (params ,dst ); 144 }else if (src0 -> type == GGML_TYPE_BF16 && dst -> type == GGML_TYPE_BF16 ) {// all bf16 145apply_unary_op < op ,ggml_bf16_t ,ggml_bf16_t > (params ,dst ); 146 }else if (src0 -> type == GGML_TYPE_BF16 && dst -> type == GGML_TYPE_F32 ) { 147apply_unary_op < op ,ggml_bf16_t ,float > (params ,dst ); 148 }else if (src0 -> type == GGML_TYPE_F16 && dst -> type == GGML_TYPE_F32 ) { 149apply_unary_op < op ,ggml_fp16_t ,float > (params ,dst ); 150 }else { 151fprintf (stderr ,"%s: unsupported types: dst: %s, src0: %s\n" ,__func__ , 152ggml_type_name (dst -> type ),ggml_type_name (src0 -> type )); 153GGML_ABORT ("fatal error" ); 154 } 155} 156 157template < float (* op )(float ,ggml_tensor * )> 158static void unary_op_params (const ggml_compute_params * params ,ggml_tensor * dst ) { 159const ggml_tensor * src0 = dst -> src [0 ]; 160 161/* */ if (src0 -> type == GGML_TYPE_F32 && dst -> type == GGML_TYPE_F32 ) {// all f32 162apply_unary_op < op ,float ,float > (params ,dst ); 163 }else if (src0 -> type == GGML_TYPE_F16 && dst -> type == GGML_TYPE_F16 ) {// all f16 164apply_unary_op < op ,ggml_fp16_t ,ggml_fp16_t > (params ,dst ); 165 }else if (src0 -> type == GGML_TYPE_BF16 && dst -> type == GGML_TYPE_BF16 ) {// all bf16 166apply_unary_op < op ,ggml_bf16_t ,ggml_bf16_t > (params ,dst ); 167 }else if (src0 -> type == GGML_TYPE_BF16 && dst -> type == GGML_TYPE_F32 ) { 168apply_unary_op < op ,ggml_bf16_t ,float > (params ,dst ); 169 }else if (src0 -> type == GGML_TYPE_F16 && dst -> type == GGML_TYPE_F32 ) { 170apply_unary_op < op ,ggml_fp16_t ,float > (params ,dst ); 171 }else { 172fprintf (stderr ,"%s: unsupported types: dst: %s, src0: %s\n" ,__func__ , 173ggml_type_name (dst -> type ),ggml_type_name (src0 -> type )); 174GGML_ABORT ("fatal error" ); 175 } 176} 177 178// Extend vec_unary_op to support functors 179template < typename Op ,typename src0_t ,typename dst_t > 180static inline void vec_unary_op_functor (int64_t n ,dst_t * y ,const src0_t * x ,Op op ) { 181constexpr auto src0_to_f32 = type_conversion_table < src0_t > ::to_f32 ; 182constexpr auto f32_to_dst = type_conversion_table < dst_t > ::from_f32 ; 183 184for (int i = 0 ;i < n ;i ++ ) { 185y [i ]= f32_to_dst (op (src0_to_f32 (x [i ]))); 186 } 187} 188 189// Extend apply_unary_op to support functors 190template < typename Op ,typename src0_t ,typename dst_t > 191static void apply_unary_op_functor (const ggml_compute_params * params ,ggml_tensor * dst ,Op op ) { 192const ggml_tensor * src0 = dst -> src [0 ]; 193 194GGML_ASSERT (ggml_is_contiguous_1 (src0 )&& ggml_is_contiguous_1 (dst )&& ggml_are_same_shape (src0 ,dst )); 195 196GGML_TENSOR_UNARY_OP_LOCALS 197 198GGML_ASSERT (nb0 == sizeof (dst_t )); 199GGML_ASSERT (nb00 == sizeof (src0_t )); 200 201const auto [ir0 ,ir1 ]= get_thread_range (params ,src0 ); 202 203for (int64_t ir = ir0 ;ir < ir1 ;++ ir ) { 204const int64_t i03 = ir /(ne02 * ne01 ); 205const int64_t i02 = (ir - i03 * ne02 * ne01 )/ne01 ; 206const int64_t i01 = (ir - i03 * ne02 * ne01 - i02 * ne01 ); 207 208dst_t * dst_ptr = (dst_t * ) ((char * )dst -> data + i03 * nb3 + i02 * nb2 + i01 * nb1 ); 209const src0_t * src0_ptr = (const src0_t * ) ((const char * )src0 -> data + i03 * nb03 + i02 * nb02 + i01 * nb01 ); 210 211vec_unary_op_functor (ne0 ,dst_ptr ,src0_ptr ,op ); 212 } 213} 214 215// Generic dispatcher for functors 216template < typename Op > 217static void unary_op_functor (const ggml_compute_params * params ,ggml_tensor * dst ,Op op ) { 218const ggml_tensor * src0 = dst -> src [0 ]; 219 220/* */ if (src0 -> type == GGML_TYPE_F32 && dst -> type == GGML_TYPE_F32 ) {// all f32 221apply_unary_op_functor < Op ,float ,float > (params ,dst ,op ); 222 }else if (src0 -> type == GGML_TYPE_F16 && dst -> type == GGML_TYPE_F16 ) {// all f16 223apply_unary_op_functor < Op ,ggml_fp16_t ,ggml_fp16_t > (params ,dst ,op ); 224 }else if (src0 -> type == GGML_TYPE_BF16 && dst -> type == GGML_TYPE_BF16 ) {// all bf16 225apply_unary_op_functor < Op ,ggml_bf16_t ,ggml_bf16_t > (params ,dst ,op ); 226 }else if (src0 -> type == GGML_TYPE_BF16 && dst -> type == GGML_TYPE_F32 ) { 227apply_unary_op_functor < Op ,ggml_bf16_t ,float > (params ,dst ,op ); 228 }else if (src0 -> type == GGML_TYPE_F16 && dst -> type == GGML_TYPE_F32 ) { 229apply_unary_op_functor < Op ,ggml_fp16_t ,float > (params ,dst ,op ); 230 }else { 231fprintf (stderr ,"%s: unsupported types: dst: %s, src0: %s\n" ,__func__ , 232ggml_type_name (dst -> type ),ggml_type_name (src0 -> type )); 233GGML_ABORT ("fatal error" ); 234 } 235} 236 237void ggml_compute_forward_abs (const ggml_compute_params * params ,ggml_tensor * dst ) { 238unary_op < op_abs > (params ,dst ); 239} 240 241void ggml_compute_forward_sgn (const ggml_compute_params * params ,ggml_tensor * dst ) { 242unary_op < op_sgn > (params ,dst ); 243} 244 245void ggml_compute_forward_neg (const ggml_compute_params * params ,ggml_tensor * dst ) { 246unary_op < op_neg > (params ,dst ); 247} 248 249void ggml_compute_forward_step (const ggml_compute_params * params ,ggml_tensor * dst ) { 250unary_op < op_step > (params ,dst ); 251} 252 253void ggml_compute_forward_tanh (const ggml_compute_params * params ,ggml_tensor * dst ) { 254unary_op < op_tanh > (params ,dst ); 255} 256 257void ggml_compute_forward_elu (const ggml_compute_params * params ,ggml_tensor * dst ) { 258unary_op < op_elu > (params ,dst ); 259} 260 261void ggml_compute_forward_relu (const ggml_compute_params * params ,ggml_tensor * dst ) { 262unary_op < op_relu > (params ,dst ); 263} 264 265void ggml_compute_forward_sigmoid (const ggml_compute_params * params ,ggml_tensor * dst ) { 266unary_op < op_sigmoid > (params ,dst ); 267} 268 269void ggml_compute_forward_hardsigmoid (const ggml_compute_params * params ,ggml_tensor * dst ) { 270unary_op < op_hardsigmoid > (params ,dst ); 271} 272 273void ggml_compute_forward_exp (const ggml_compute_params * params ,ggml_tensor * dst ) { 274unary_op < op_exp > (params ,dst ); 275} 276 277void ggml_compute_forward_hardswish (const ggml_compute_params * params ,ggml_tensor * dst ) { 278unary_op < op_hardswish > (params ,dst ); 279} 280 281void ggml_compute_forward_sqr (const ggml_compute_params * params ,ggml_tensor * dst ) { 282unary_op < op_sqr > (params ,dst ); 283} 284 285void ggml_compute_forward_sqrt (const ggml_compute_params * params ,ggml_tensor * dst ) { 286unary_op < op_sqrt > (params ,dst ); 287} 288 289void ggml_compute_forward_sin (const ggml_compute_params * params ,ggml_tensor * dst ) { 290unary_op < op_sin > (params ,dst ); 291} 292 293void ggml_compute_forward_cos (const ggml_compute_params * params ,ggml_tensor * dst ) { 294unary_op < op_cos > (params ,dst ); 295} 296 297void ggml_compute_forward_log (const ggml_compute_params * params ,ggml_tensor * dst ) { 298unary_op < op_log > (params ,dst ); 299} 300 301void ggml_compute_forward_expm1 (const ggml_compute_params * params ,ggml_tensor * dst ) { 302unary_op < op_expm1 > (params ,dst ); 303} 304 305void ggml_compute_forward_softplus (const ggml_compute_params * params ,ggml_tensor * dst ) { 306unary_op < op_softplus > (params ,dst ); 307} 308 309void ggml_compute_forward_floor (const ggml_compute_params * params ,ggml_tensor * dst ) { 310unary_op < op_floor > (params ,dst ); 311} 312 313void ggml_compute_forward_ceil (const ggml_compute_params * params ,ggml_tensor * dst ) { 314unary_op < op_ceil > (params ,dst ); 315} 316 317void ggml_compute_forward_round (const ggml_compute_params * params ,ggml_tensor * dst ) { 318unary_op < op_round > (params ,dst ); 319} 320 321void ggml_compute_forward_trunc (const ggml_compute_params * params ,ggml_tensor * dst ) { 322unary_op < op_trunc > (params ,dst ); 323} 324 325void ggml_compute_forward_xielu (const ggml_compute_params * params ,ggml_tensor * dst ) { 326const float alpha_n = ggml_get_op_params_f32 (dst ,1 ); 327const float alpha_p = ggml_get_op_params_f32 (dst ,2 ); 328const float beta = ggml_get_op_params_f32 (dst ,3 ); 329const float eps = ggml_get_op_params_f32 (dst ,4 ); 330 331const auto xielu_op_params = [alpha_n ,alpha_p ,beta ,eps ](float f ) { 332return op_xielu (f ,alpha_n ,alpha_p ,beta ,eps ); 333 }; 334 335unary_op_functor (params ,dst ,xielu_op_params ); 336} 337