ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-hexagon / htp / hvx-erf.h
| 1 | #ifndef HVX_ERF_H |
| 2 | #define HVX_ERF_H |
| 3 | |
| 4 | #include "hvx-base.h" |
| 5 | #include "hvx-exp.h" |
| 6 | #include "hvx-inverse.h" |
| 7 | |
| 8 | // Maximum error is about 1.5e-7 for the Abramowitz-Stegun approximation. |
| 9 | static __attribute__((noinline)) HVX_Vector hvx_vec_erf_f32(HVX_Vector x) { |
| 10 | const HVX_Vector zero = hvx_vec_splat_f32(0.0f); |
| 11 | const HVX_Vector ax = hvx_vec_abs_f32(x); |
| 12 | HVX_Vector t = hvx_vec_inverse_f32(hvx_vec_add_f32_f32( |
| 13 | hvx_vec_splat_f32(1.0f), hvx_vec_mul_f32_f32(hvx_vec_splat_f32(0.3275911f), ax))); |
| 14 | |
| 15 | HVX_Vector poly = hvx_vec_splat_f32(1.061405429f); |
| 16 | poly = hvx_vec_add_f32_f32(hvx_vec_splat_f32(-1.453152027f), hvx_vec_mul_f32_f32(poly, t)); |
| 17 | poly = hvx_vec_add_f32_f32(hvx_vec_splat_f32(1.421413741f), hvx_vec_mul_f32_f32(poly, t)); |
| 18 | poly = hvx_vec_add_f32_f32(hvx_vec_splat_f32(-0.284496736f), hvx_vec_mul_f32_f32(poly, t)); |
| 19 | poly = hvx_vec_add_f32_f32(hvx_vec_splat_f32(0.254829592f), hvx_vec_mul_f32_f32(poly, t)); |
| 20 | |
| 21 | const HVX_Vector exp_term = hvx_vec_exp_f32(hvx_vec_neg_f32(hvx_vec_mul_f32_f32(ax, ax))); |
| 22 | HVX_Vector result = hvx_vec_sub_f32_f32(hvx_vec_splat_f32(1.0f), |
| 23 | hvx_vec_mul_f32_f32(hvx_vec_mul_f32_f32(poly, t), exp_term)); |
| 24 | |
| 25 | const HVX_VectorPred neg = Q6_Q_vcmp_gt_VsfVsf(zero, x); |
| 26 | result = Q6_V_vmux_QVV(neg, hvx_vec_neg_f32(result), result); |
| 27 | return result; |
| 28 | } |
| 29 | |
| 30 | // GELU_ERF uses the erf definition. |
| 31 | static inline HVX_Vector hvx_vec_gelu_erf_f32(HVX_Vector x) { |
| 32 | const HVX_Vector scale = hvx_vec_splat_f32(0.7071067811865475f); |
| 33 | const HVX_Vector half = hvx_vec_splat_f32(0.5f); |
| 34 | const HVX_Vector one = hvx_vec_splat_f32(1.0f); |
| 35 | const HVX_Vector max_x = hvx_vec_splat_f32(10.0f); |
| 36 | const HVX_Vector min_x = hvx_vec_splat_f32(-10.0f); |
| 37 | const HVX_VectorPred neg_large = Q6_Q_vcmp_gt_VsfVsf(min_x, x); |
| 38 | const HVX_VectorPred pos_large = Q6_Q_vcmp_gt_VsfVsf(x, max_x); |
| 39 | HVX_Vector x_calc = Q6_V_vmux_QVV(neg_large, min_x, x); |
| 40 | x_calc = Q6_V_vmux_QVV(pos_large, max_x, x_calc); |
| 41 | const HVX_Vector erf = hvx_vec_erf_f32(hvx_vec_mul_f32_f32(x_calc, scale)); |
| 42 | |
| 43 | HVX_Vector result = hvx_vec_mul_f32_f32(hvx_vec_mul_f32_f32(half, x_calc), hvx_vec_add_f32_f32(one, erf)); |
| 44 | |
| 45 | result = Q6_V_vmux_QVV(neg_large, hvx_vec_splat_f32(0.0f), result); |
| 46 | result = Q6_V_vmux_QVV(pos_large, x, result); |
| 47 | return result; |
| 48 | } |
| 49 | |
| 50 | static inline void hvx_gelu_erf_f32_aa(uint8_t * restrict dst, const uint8_t * restrict src, uint32_t n) { |
| 51 | assert((unsigned long) dst % 128 == 0); |
| 52 | assert((unsigned long) src % 128 == 0); |
| 53 | |
| 54 | HVX_Vector * restrict vdst = (HVX_Vector *) dst; |
| 55 | HVX_Vector * restrict vsrc = (HVX_Vector *) src; |
| 56 | |
| 57 | const uint32_t elem_size = sizeof(float); |
| 58 | const uint32_t epv = 128 / elem_size; |
| 59 | const uint32_t nvec = n / epv; |
| 60 | const uint32_t nloe = n % epv; |
| 61 | |
| 62 | uint32_t i = 0; |
| 63 | |
| 64 | _Pragma("unroll(4)") |
| 65 | for (; i < nvec; i++) { |
| 66 | vdst[i] = hvx_vec_gelu_erf_f32(vsrc[i]); |
| 67 | } |
| 68 | if (nloe) { |
| 69 | HVX_Vector v = hvx_vec_gelu_erf_f32(vsrc[i]); |
| 70 | hvx_vec_store_a((void *) &vdst[i], nloe * elem_size, v); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | #endif /* HVX_ERF_H */ |