char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
89181c0b6
char/aliceai-t5-moe
1#include "binary-ops.h" 2 3#if defined(GGML_USE_ACCELERATE ) 4#include <Accelerate/Accelerate.h> 5 6using vDSP_fn_t = void (* )(const float * ,vDSP_Stride ,const float * ,vDSP_Stride ,float * ,vDSP_Stride ,vDSP_Length ); 7#endif 8 9static inline float op_add (float a ,float b ) { 10return a + b ; 11} 12 13static inline float op_sub (float a ,float b ) { 14return a - b ; 15} 16 17static inline float op_mul (float a ,float b ) { 18return a * b ; 19} 20 21static inline float op_div (float a ,float b ) { 22return a /b ; 23} 24 25template < float (* op )(float ,float ),typename src0_t ,typename src1_t ,typename dst_t > 26static inline void vec_binary_op_contiguous (const int64_t n ,dst_t * z ,const src0_t * x ,const src1_t * y ) { 27constexpr auto src0_to_f32 = type_conversion_table < src0_t > ::to_f32 ; 28constexpr auto src1_to_f32 = type_conversion_table < src1_t > ::to_f32 ; 29constexpr auto f32_to_dst = type_conversion_table < dst_t > ::from_f32 ; 30 31for (int i = 0 ;i < n ;i ++ ) { 32z [i ]= f32_to_dst (op (src0_to_f32 (x [i ]),src1_to_f32 (y [i ]))); 33 } 34} 35 36template < float (* op )(float ,float ),typename src0_t ,typename src1_t ,typename dst_t > 37static inline void vec_binary_op_non_contiguous (const int64_t n ,const int64_t ne10 ,const int64_t nb10 ,dst_t * z ,const src0_t * x ,const src1_t * y ) { 38constexpr auto src0_to_f32 = type_conversion_table < src0_t > ::to_f32 ; 39constexpr auto src1_to_f32 = type_conversion_table < src1_t > ::to_f32 ; 40constexpr auto f32_to_dst = type_conversion_table < dst_t > ::from_f32 ; 41 42for (int i = 0 ;i < n ;i ++ ) { 43int i10 = i %ne10 ; 44const src1_t * y_ptr = (const src1_t * )((const char * )y + i10 * nb10 ); 45z [i ]= f32_to_dst (op (src0_to_f32 (x [i ]),src1_to_f32 (* y_ptr ))); 46 } 47} 48 49template < float (* op )(float ,float ),typename src0_t ,typename src1_t ,typename dst_t > 50static void apply_binary_op (const ggml_compute_params * params ,ggml_tensor * dst ) { 51const ggml_tensor * src0 = dst -> src [0 ]; 52const ggml_tensor * src1 = dst -> src [1 ]; 53 54GGML_ASSERT (ggml_can_repeat (src1 ,src0 )&& ggml_are_same_shape (src0 ,dst )); 55 56GGML_TENSOR_BINARY_OP_LOCALS 57 58GGML_ASSERT (nb0 == sizeof (dst_t )); 59GGML_ASSERT (nb00 == sizeof (src0_t )); 60 61const auto [ir0 ,ir1 ]= get_thread_range (params ,src0 ); 62const bool is_src1_contiguous_rows = ggml_is_contiguous_rows (src1 ); 63 64#ifdef GGML_USE_ACCELERATE 65vDSP_fn_t vDSP_op = nullptr ; 66// TODO - avoid the f32-only check using type 'trait' lookup tables and row-based src-to-float conversion functions 67if (src0 -> type == GGML_TYPE_F32 && src1 -> type == GGML_TYPE_F32 && dst -> type == GGML_TYPE_F32 ) { 68if (op == op_add ) { 69vDSP_op = vDSP_vadd ; 70 }else if (op == op_sub ) { 71vDSP_op = vDSP_vsub ; 72 }else if (op == op_mul ) { 73vDSP_op = vDSP_vmul ; 74 }else if (op == op_div ) { 75vDSP_op = vDSP_vdiv ; 76 } 77 } 78#endif 79 80for (int64_t ir = ir0 ;ir < ir1 ;++ ir ) { 81const int64_t i03 = ir /(ne02 * ne01 ); 82const int64_t i02 = (ir - i03 * ne02 * ne01 )/ne01 ; 83const int64_t i01 = (ir - i03 * ne02 * ne01 - i02 * ne01 ); 84 85const int64_t i13 = i03 %ne13 ; 86const int64_t i12 = i02 %ne12 ; 87const int64_t i11 = i01 %ne11 ; 88 89dst_t * dst_ptr = (dst_t * ) ((char * )dst -> data + i03 * nb3 + i02 * nb2 + i01 * nb1 ); 90const src0_t * src0_ptr = (const src0_t * ) ((const char * )src0 -> data + i03 * nb03 + i02 * nb02 + i01 * nb01 ); 91const src1_t * src1_ptr = (const src1_t * ) ((const char * )src1 -> data + i13 * nb13 + i12 * nb12 + i11 * nb11 ); 92 93if (is_src1_contiguous_rows ) { 94// src1 is broadcastable across src0 and dst in i1, i2, i3 95const int64_t nr0 = ne00 /ne10 ; 96 97for (int64_t r = 0 ;r < nr0 ;++ r ) { 98#ifdef GGML_USE_ACCELERATE 99if constexpr (std::is_same_v < src0_t ,float > && std::is_same_v < src1_t ,float > && std::is_same_v < dst_t ,float > ) { 100if (vDSP_op != nullptr ) { 101vDSP_op (src1_ptr ,1 ,src0_ptr + r * ne10 ,1 ,dst_ptr + r * ne10 ,1 ,ne10 ); 102continue ; 103 } 104 } 105#endif 106vec_binary_op_contiguous < op > (ne10 ,dst_ptr + r * ne10 ,src0_ptr + r * ne10 ,src1_ptr ); 107 } 108 }else { 109vec_binary_op_non_contiguous < op > (ne0 ,ne10 ,nb10 ,dst_ptr ,src0_ptr ,src1_ptr ); 110 } 111 } 112} 113 114// TODO: Use the 'traits' lookup table (for type conversion fns), instead of a mass of 'if' conditions with long templates 115template < float (* op )(float ,float )> 116static void binary_op (const ggml_compute_params * params ,ggml_tensor * dst ) { 117const ggml_tensor * src0 = dst -> src [0 ]; 118const ggml_tensor * src1 = dst -> src [1 ]; 119 120/* */ if (src0 -> type == GGML_TYPE_F32 && src1 -> type == GGML_TYPE_F32 && dst -> type == GGML_TYPE_F32 ) {// all f32 121apply_binary_op < op ,float ,float ,float > (params ,dst ); 122 }else if (src0 -> type == GGML_TYPE_F16 && src1 -> type == GGML_TYPE_F16 && dst -> type == GGML_TYPE_F16 ) {// all f16 123apply_binary_op < op ,ggml_fp16_t ,ggml_fp16_t ,ggml_fp16_t > (params ,dst ); 124 }else if (src0 -> type == GGML_TYPE_BF16 && src1 -> type == GGML_TYPE_BF16 && dst -> type == GGML_TYPE_BF16 ) {// all bf16 125apply_binary_op < op ,ggml_bf16_t ,ggml_bf16_t ,ggml_bf16_t > (params ,dst ); 126 }else if (src0 -> type == GGML_TYPE_BF16 && src1 -> type == GGML_TYPE_F32 && dst -> type == GGML_TYPE_BF16 ) { 127apply_binary_op < op ,ggml_bf16_t ,float ,ggml_bf16_t > (params ,dst ); 128 }else if (src0 -> type == GGML_TYPE_BF16 && src1 -> type == GGML_TYPE_F32 && dst -> type == GGML_TYPE_F32 ) { 129apply_binary_op < op ,ggml_bf16_t ,float ,float > (params ,dst ); 130 }else if (src0 -> type == GGML_TYPE_F16 && src1 -> type == GGML_TYPE_F32 && dst -> type == GGML_TYPE_F16 ) { 131apply_binary_op < op ,ggml_fp16_t ,float ,ggml_fp16_t > (params ,dst ); 132 }else if (src0 -> type == GGML_TYPE_F16 && src1 -> type == GGML_TYPE_F32 && dst -> type == GGML_TYPE_F32 ) { 133apply_binary_op < op ,ggml_fp16_t ,float ,float > (params ,dst ); 134 }else { 135GGML_ABORT ("%s: unsupported types: dst: %s, src0: %s, src1: %s\n" ,__func__ , 136ggml_type_name (dst -> type ),ggml_type_name (src0 -> type ),ggml_type_name (src1 -> type )); 137 } 138} 139 140void ggml_compute_forward_add_non_quantized (const ggml_compute_params * params ,ggml_tensor * dst ) { 141binary_op < op_add > (params ,dst ); 142} 143 144void ggml_compute_forward_sub (const ggml_compute_params * params ,ggml_tensor * dst ) { 145binary_op < op_sub > (params ,dst ); 146} 147 148void ggml_compute_forward_mul (const ggml_compute_params * params ,ggml_tensor * dst ) { 149binary_op < op_mul > (params ,dst ); 150} 151 152void ggml_compute_forward_div (const ggml_compute_params * params ,ggml_tensor * dst ) { 153binary_op < op_div > (params ,dst ); 154}