Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/ggml/blob/master/src/ggml-hexagon/htp/hvx-sigmoid.h # ggml [Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `ggml / src / ggml-hexagon / htp / hvx-sigmoid.h` 290 lines ยท 15302 bytes Raw Blame History 1 #ifndef HVX_SIGMOID_H 2 #define HVX_SIGMOID_H 3 4 #include "hvx-base.h" 5 #include "hvx-inverse.h" 6 #include "hvx-exp.h" 7 8 #define FAST_SIGMOID_LOG2F (0x3fb8aa3b) // 1.`442695022` 9 #define FAST_SIGMOID_C1 (0x3d009076) // 0.`03138777` 10 #define FAST_SIGMOID_C2 (0x3e8d74bd) // 0.`276281267` 11 #define FAST_SIGMOID_C3 (0x3f000000) // 0.5 12 13 static inline HVX_Vector hvx_vec_fast_sigmoid_f32(HVX_Vector v) { 14 v = Q6_Vqf32_vmpy_VsfVsf(v, Q6_V_vsplat_R(FAST_SIGMOID_LOG2F)); 15 v = Q6_Vqf32_vmpy_VsfVsf(Q6_Vsf_equals_Vqf32(v), Q6_V_vsplat_R(FAST_SIGMOID_C3)); 16 17 HVX_Vector in_int = hvx_vec_truncate_f32(Q6_Vsf_equals_Vqf32(v)); 18 HVX_Vector x = Q6_Vqf32_vsub_Vqf32Vsf(v, Q6_Vsf_equals_Vw(in_int)); 19 HVX_Vector xx = Q6_Vqf32_vmpy_Vqf32Vqf32(x, x); 20 21 HVX_Vector v1 = Q6_Vqf32_vmpy_VsfVsf(Q6_Vsf_equals_Vqf32(xx), Q6_V_vsplat_R(FAST_SIGMOID_C2)); 22 v1 = Q6_Vqf32_vadd_Vqf32Vsf(v1, Q6_V_vsplat_R(FAST_SIGMOID_LOG2F)); 23 24 HVX_Vector v2 = Q6_Vqf32_vmpy_VsfVsf(Q6_Vsf_equals_Vqf32(x), Q6_V_vsplat_R(FAST_SIGMOID_C1)); 25 v2 = Q6_Vqf32_vmpy_Vqf32Vqf32(v2, xx); 26 v2 = Q6_Vqf32_vadd_Vqf32Vqf32(v2, x); 27 28 HVX_Vector v3 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vqf32(v2, v1)); 29 HVX_Vector v3_exponent = Q6_Vw_vasl_VwR(v3, 1); 30 v3_exponent = Q6_Vuw_vlsr_VuwR(v3_exponent, 24); 31 v3_exponent = Q6_Vw_vadd_VwVw(in_int, v3_exponent); 32 v3 = Q6_Vw_vaslacc_VwVwR(v3, in_int, 24); 33 34 HVX_Vector v4 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vsub_Vqf32Vqf32(v2, v1)); 35 HVX_Vector v5 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vsub_VsfVsf(v3, v4)); 36 37 HVX_Vector res = hvx_vec_inverse_f32(v5); 38 res = Q6_Vqf32_vmpy_VsfVsf(v3, res); 39 40 return Q6_Vsf_equals_Vqf32(res); 41 } 42 43 static inline HVX_Vector hvx_vec_fast_sigmoid_f32_guard(HVX_Vector v, 44 HVX_Vector one, 45 HVX_Vector max_exp, 46 HVX_Vector min_exp) { 47 const HVX_VectorPred pred_max = Q6_Q_vcmp_gt_VsfVsf(max_exp, v); 48 const HVX_VectorPred pred_min = Q6_Q_vcmp_gt_VsfVsf(v, min_exp); 49 50 HVX_Vector out = hvx_vec_fast_sigmoid_f32(v); 51 out = Q6_V_vmux_QVV(pred_max, out, one); 52 return Q6_V_vmux_QVV(pred_min, out, Q6_V_vzero()); 53 } 54 55 static inline HVX_Vector hvx_vec_fast_sigmoid_f32_2it(HVX_Vector v) { 56 v = Q6_Vqf32_vmpy_VsfVsf(v, Q6_V_vsplat_R(FAST_SIGMOID_LOG2F)); 57 v = Q6_Vqf32_vmpy_VsfVsf(Q6_Vsf_equals_Vqf32(v), Q6_V_vsplat_R(FAST_SIGMOID_C3)); 58 59 HVX_Vector in_int = hvx_vec_truncate_f32(Q6_Vsf_equals_Vqf32(v)); 60 HVX_Vector x = Q6_Vqf32_vsub_Vqf32Vsf(v, Q6_Vsf_equals_Vw(in_int)); 61 HVX_Vector xx = Q6_Vqf32_vmpy_Vqf32Vqf32(x, x); 62 63 HVX_Vector v1 = Q6_Vqf32_vmpy_VsfVsf(Q6_Vsf_equals_Vqf32(xx), Q6_V_vsplat_R(FAST_SIGMOID_C2)); 64 v1 = Q6_Vqf32_vadd_Vqf32Vsf(v1, Q6_V_vsplat_R(FAST_SIGMOID_LOG2F)); 65 66 HVX_Vector v2 = Q6_Vqf32_vmpy_VsfVsf(Q6_Vsf_equals_Vqf32(x), Q6_V_vsplat_R(FAST_SIGMOID_C1)); 67 v2 = Q6_Vqf32_vmpy_Vqf32Vqf32(v2, xx); 68 v2 = Q6_Vqf32_vadd_Vqf32Vqf32(v2, x); 69 70 HVX_Vector v3 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vqf32(v2, v1)); 71 v3 = Q6_Vw_vaslacc_VwVwR(v3, in_int, 24); 72 73 HVX_Vector v4 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vsub_Vqf32Vqf32(v2, v1)); 74 HVX_Vector v5 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vsub_VsfVsf(v3, v4)); 75 76 // Newton-Raphson with 2 iterations 77 HVX_Vector two_sf = hvx_vec_splat_f32(2.0f); 78 HVX_Vector i_sf = Q6_Vw_vsub_VwVw(Q6_V_vsplat_R(0x7EEEEBB3), v5); 79 HVX_Vector r_qf = Q6_Vqf32_vmpy_VsfVsf( 80 i_sf, Q6_Vsf_equals_Vqf32(Q6_Vqf32_vsub_VsfVsf(two_sf, Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(i_sf, v5))))); 81 r_qf = Q6_Vqf32_vmpy_Vqf32Vqf32( 82 r_qf, Q6_Vqf32_vsub_VsfVsf(two_sf, Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(Q6_Vsf_equals_Vqf32(r_qf), v5)))); 83 HVX_Vector res = Q6_Vsf_equals_Vqf32(r_qf); 84 85 res = Q6_Vqf32_vmpy_VsfVsf(v3, res); 86 87 return Q6_Vsf_equals_Vqf32(res); 88 } 89 90 static inline HVX_Vector hvx_vec_fast_sigmoid_f32_guard_2it(HVX_Vector v, 91 HVX_Vector one, 92 HVX_Vector max_exp, 93 HVX_Vector min_exp) { 94 const HVX_VectorPred pred_max = Q6_Q_vcmp_gt_VsfVsf(max_exp, v); 95 const HVX_VectorPred pred_min = Q6_Q_vcmp_gt_VsfVsf(v, min_exp); 96 97 HVX_Vector out = hvx_vec_fast_sigmoid_f32_2it(v); 98 out = Q6_V_vmux_QVV(pred_max, out, one); 99 return Q6_V_vmux_QVV(pred_min, out, Q6_V_vzero()); 100 } 101 102 static inline HVX_Vector hvx_vec_tanh_f32(HVX_Vector x) { 103 // tanh(x) = 2 * sigmoid(2x) - 1 104 HVX_Vector two = hvx_vec_splat_f32(2.0f); 105 HVX_Vector one = hvx_vec_splat_f32(1.0f); 106 HVX_Vector x2 = Q6_Vqf32_vmpy_VsfVsf(x, two); 107 108 HVX_Vector max_exp = hvx_vec_splat_f32(87.f); 109 HVX_Vector min_exp = hvx_vec_splat_f32(-87.f); 110 111 HVX_Vector sig2x = hvx_vec_fast_sigmoid_f32_guard(Q6_Vsf_equals_Vqf32(x2), one, max_exp, min_exp); 112 113 HVX_Vector res = Q6_Vqf32_vmpy_VsfVsf(sig2x, two); 114 res = Q6_Vqf32_vsub_Vqf32Vsf(res, one); 115 return Q6_Vsf_equals_Vqf32(res); 116 } 117 118 static inline HVX_Vector hvx_vec_silu_f32(HVX_Vector x) { 119 HVX_Vector one = hvx_vec_splat_f32(1.0f); 120 HVX_Vector max_exp = hvx_vec_splat_f32(87.f); 121 HVX_Vector min_exp = hvx_vec_splat_f32(-87.f); 122 HVX_Vector sig = hvx_vec_fast_sigmoid_f32_guard(x, one, max_exp, min_exp); 123 return Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(x, sig)); 124 } 125 126 #define hvx_sigmoid_loop_body(dst_type, src_type, vec_store) \ 127 do { \ 128 dst_type * restrict vdst = (dst_type *) dst; \ 129 src_type * restrict vsrc = (src_type *) src; \ 130 \ 131 const HVX_Vector one = hvx_vec_splat_f32(1.f); \ 132 const HVX_Vector max_exp = hvx_vec_splat_f32(87.f); \ 133 const HVX_Vector min_exp = hvx_vec_splat_f32(-87.f); \ 134 \ 135 const uint32_t epv = 128 / sizeof(float); \ 136 const uint32_t nvec = n / epv; \ 137 const uint32_t nloe = n % epv; \ 138 \ 139 uint32_t i = 0; \ 140 \ 141 _Pragma("unroll(4)") \ 142 for (; i < nvec; i++) { \ 143 vdst[i] = hvx_vec_fast_sigmoid_f32_guard(vsrc[i], one, max_exp, min_exp); \ 144 } \ 145 if (nloe) { \ 146 HVX_Vector tmp = hvx_vec_fast_sigmoid_f32_guard(vsrc[i], one, max_exp, min_exp); \ 147 vec_store((void *) &vdst[i], nloe * sizeof(float), tmp); \ 148 } \ 149 } while(0) 150 151 #define hvx_tanh_loop_body(dst_type, src_type, vec_store) \ 152 do { \ 153 dst_type * restrict vdst = (dst_type *) dst; \ 154 src_type * restrict vsrc = (src_type *) src; \ 155 \ 156 const uint32_t epv = 128 / sizeof(float); \ 157 const uint32_t nvec = n / epv; \ 158 const uint32_t nloe = n % epv; \ 159 \ 160 uint32_t i = 0; \ 161 \ 162 _Pragma("unroll(4)") \ 163 for (; i < nvec; i++) { \ 164 vdst[i] = hvx_vec_tanh_f32(vsrc[i]); \ 165 } \ 166 if (nloe) { \ 167 HVX_Vector tmp = hvx_vec_tanh_f32(vsrc[i]); \ 168 vec_store((void *) &vdst[i], nloe * sizeof(float), tmp); \ 169 } \ 170 } while(0) 171 172 static inline void hvx_sigmoid_f32_aa(uint8_t * restrict dst, const uint8_t * restrict src, uint32_t n) { 173 assert((unsigned long) dst % 128 == 0); 174 assert((unsigned long) src % 128 == 0); 175 hvx_sigmoid_loop_body(HVX_Vector, HVX_Vector, hvx_vec_store_a); 176 } 177 178 static inline void hvx_sigmoid_f32_au(uint8_t * restrict dst, const uint8_t * restrict src, uint32_t n) { 179 assert((unsigned long) dst % 128 == 0); 180 hvx_sigmoid_loop_body(HVX_Vector, HVX_UVector, hvx_vec_store_a); 181 } 182 183 static inline void hvx_sigmoid_f32_ua(uint8_t * restrict dst, const uint8_t * restrict src, uint32_t n) { 184 assert((unsigned long) src % 128 == 0); 185 hvx_sigmoid_loop_body(HVX_UVector, HVX_Vector, hvx_vec_store_u); 186 } 187 188 static inline void hvx_sigmoid_f32_uu(uint8_t * restrict dst, const uint8_t * restrict src, uint32_t n) { 189 hvx_sigmoid_loop_body(HVX_UVector, HVX_UVector, hvx_vec_store_u); 190 } 191 192 static inline void hvx_tanh_f32_aa(uint8_t * restrict dst, const uint8_t * restrict src, uint32_t n) { 193 assert((unsigned long) dst % 128 == 0); 194 assert((unsigned long) src % 128 == 0); 195 hvx_tanh_loop_body(HVX_Vector, HVX_Vector, hvx_vec_store_a); 196 } 197 198 static inline HVX_Vector hvx_vec_fast_sigmoid_f16(HVX_Vector x_v) { 199 const HVX_Vector v_one = hvx_vec_splat_f16(1.0f); 200 const HVX_Vector v_neg_log2e = hvx_vec_splat_f16(-EXP_LOG2E_F); 201 const HVX_Vector em_mask = Q6_Vh_vsplat_R(0x7FFF); 202 203 // Compute absolute value of x_v 204 HVX_Vector abs_x = Q6_V_vand_VV(x_v, em_mask); 205 206 // Compute u = -abs_x * log2(e) <= 0. 207 HVX_Vector u = hvx_vec_mul_f16_f16(abs_x, v_neg_log2e); 208 209 // Clamp input to prevent underflow in exp2 210 const HVX_Vector v_clamp_min = hvx_vec_splat_f16(-24.0f); 211 u = Q6_Vhf_vmax_VhfVhf(v_clamp_min, u); 212 213 HVX_Vector exp_val = hvx_vec_exp2_f16(u); 214 HVX_Vector denom = hvx_vec_add_f16_f16(v_one, exp_val); 215 HVX_Vector sig_abs = hvx_vec_inverse_f16(denom); 216 217 // check if x_v < 0 (using integer comparison on absolute value) 218 HVX_VectorPred is_neg = Q6_Q_vcmp_gt_VhVh(abs_x, x_v); 219 220 // If x_v < 0, return 1.0f - sig_abs 221 HVX_Vector sig_neg = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vsub_VhfVhf(v_one, sig_abs)); 222 return Q6_V_vmux_QVV(is_neg, sig_neg, sig_abs); 223 } 224 225 static inline HVX_Vector hvx_vec_tanh_f16(HVX_Vector x) { 226 // tanh(x) = 2 * sigmoid(2x) - 1 227 const HVX_Vector v_two = hvx_vec_splat_f16(2.0f); 228 229 HVX_Vector x2 = hvx_vec_mul_f16_f16(x, v_two); 230 HVX_Vector sig2x = hvx_vec_fast_sigmoid_f16(x2); 231 232 const HVX_Vector v_neg_one = hvx_vec_splat_f16(-1.0f); 233 return hvx_vec_add_f16_f16(hvx_vec_mul_f16_f16(sig2x, v_two), v_neg_one); 234 } 235 236 #define hvx_sigmoid_f16_loop_body(dst_type, src_type, vec_store) \ 237 do { \ 238 dst_type * restrict vdst = (dst_type *) dst; \ 239 src_type * restrict vsrc = (src_type *) src; \ 240 \ 241 const uint32_t epv = 128 / sizeof(_Float16); \ 242 const uint32_t nvec = n / epv; \ 243 const uint32_t nloe = n % epv; \ 244 \ 245 uint32_t i = 0; \ 246 \ 247 _Pragma("unroll(4)") \ 248 for (; i < nvec; i++) { \ 249 vdst[i] = hvx_vec_fast_sigmoid_f16(vsrc[i]); \ 250 } \ 251 if (nloe) { \ 252 HVX_Vector tmp = hvx_vec_fast_sigmoid_f16(vsrc[i]); \ 253 vec_store((void *) &vdst[i], nloe * sizeof(_Float16), tmp); \ 254 } \ 255 } while(0) 256 257 #define hvx_tanh_f16_loop_body(dst_type, src_type, vec_store) \ 258 do { \ 259 dst_type * restrict vdst = (dst_type *) dst; \ 260 src_type * restrict vsrc = (src_type *) src; \ 261 \ 262 const uint32_t epv = 128 / sizeof(_Float16); \ 263 const uint32_t nvec = n / epv; \ 264 const uint32_t nloe = n % epv; \ 265 \ 266 uint32_t i = 0; \ 267 \ 268 _Pragma("unroll(4)") \ 269 for (; i < nvec; i++) { \ 270 vdst[i] = hvx_vec_tanh_f16(vsrc[i]); \ 271 } \ 272 if (nloe) { \ 273 HVX_Vector tmp = hvx_vec_tanh_f16(vsrc[i]); \ 274 vec_store((void *) &vdst[i], nloe * sizeof(_Float16), tmp); \ 275 } \ 276 } while(0) 277 278 static inline void hvx_sigmoid_f16_aa(uint8_t * restrict dst, const uint8_t * restrict src, uint32_t n) { 279 assert((unsigned long) dst % 128 == 0); 280 assert((unsigned long) src % 128 == 0); 281 hvx_sigmoid_f16_loop_body(HVX_Vector, HVX_Vector, hvx_vec_store_a); 282 } 283 284 static inline void hvx_tanh_f16_aa(uint8_t * restrict dst, const uint8_t * restrict src, uint32_t n) { 285 assert((unsigned long) dst % 128 == 0); 286 assert((unsigned long) src % 128 == 0); 287 hvx_tanh_f16_loop_body(HVX_Vector, HVX_Vector, hvx_vec_store_a); 288 } 289 290 #endif /* HVX_SIGMOID_H */