char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
192067b72
char/aliceai-t5-moe
1#ifndef HVX_QUANT_H 2#define HVX_QUANT_H 3 4#include <math.h> 5#include <stdint.h> 6#include <string.h> 7 8#include "hvx-arith.h" 9#include "hvx-base.h" 10#include "hvx-reduce.h" 11#include "hvx-repl.h" 12#include "hvx-utils.h" 13 14#ifndef GGML_COMMON_DECL_C 15#define GGML_COMMON_DECL_C 16#endif 17#include "ggml-common.h" 18#include "ggml-impl.h" 19 20static inline void hvx_quantize_row_q8_0_f32 (void * restrictdst_ptr ,const float * restrictsrc_ptr ,int n ) { 21const int nb = n /QK8_0 ; 22block_q8_0 * dst = (block_q8_0 * )dst_ptr ; 23HVX_Vector zero = Q6_V_vzero (); 24 25int i = 0 ; 26for (;i + 3 < nb ;i += 4 ) { 27HVX_Vector * vx = (HVX_Vector * ) (src_ptr + i * QK8_0 ); 28 29HVX_Vector vmax0_sf = hvx_vec_reduce_max_f32 (hvx_vec_abs_f32 (vx [0 ])); 30HVX_Vector vmax1_sf = hvx_vec_reduce_max_f32 (hvx_vec_abs_f32 (vx [1 ])); 31HVX_Vector vmax2_sf = hvx_vec_reduce_max_f32 (hvx_vec_abs_f32 (vx [2 ])); 32HVX_Vector vmax3_sf = hvx_vec_reduce_max_f32 (hvx_vec_abs_f32 (vx [3 ])); 33 34HVX_Vector vx0_qf = Q6_Vqf32_vsub_VsfVsf (vx [0 ],zero ); 35HVX_Vector vx1_qf = Q6_Vqf32_vsub_VsfVsf (vx [1 ],zero ); 36HVX_Vector vx2_qf = Q6_Vqf32_vsub_VsfVsf (vx [2 ],zero ); 37HVX_Vector vx3_qf = Q6_Vqf32_vsub_VsfVsf (vx [3 ],zero ); 38 39HVX_Vector vmax0_qf = Q6_Vqf32_vsub_VsfVsf (vmax0_sf ,zero ); 40HVX_Vector vmax1_qf = Q6_Vqf32_vsub_VsfVsf (vmax1_sf ,zero ); 41HVX_Vector vmax2_qf = Q6_Vqf32_vsub_VsfVsf (vmax2_sf ,zero ); 42HVX_Vector vmax3_qf = Q6_Vqf32_vsub_VsfVsf (vmax3_sf ,zero ); 43 44HVX_Vector vmax01_hf = Q6_Vh_vdeal_Vh (Q6_Vhf_equals_Wqf32 (Q6_W_vcombine_VV (vmax1_qf ,vmax0_qf ))); 45HVX_Vector vmax23_hf = Q6_Vh_vdeal_Vh (Q6_Vhf_equals_Wqf32 (Q6_W_vcombine_VV (vmax3_qf ,vmax2_qf ))); 46 47HVX_Vector vx01_hf = Q6_Vh_vdeal_Vh (Q6_Vhf_equals_Wqf32 (Q6_W_vcombine_VV (vx1_qf ,vx0_qf ))); 48HVX_Vector vx23_hf = Q6_Vh_vdeal_Vh (Q6_Vhf_equals_Wqf32 (Q6_W_vcombine_VV (vx3_qf ,vx2_qf ))); 49 50HVX_Vector vd01_qf16 = Q6_Vqf16_vmpy_VhfVhf (vmax01_hf ,Q6_Vh_vsplat_R (0x2008 ));// 1.0 / 127.0 51HVX_Vector vd23_qf16 = Q6_Vqf16_vmpy_VhfVhf (vmax23_hf ,Q6_Vh_vsplat_R (0x2008 ));// 1.0 / 127.0 52HVX_Vector vd01_hf = Q6_Vhf_equals_Vqf16 (vd01_qf16 ); 53HVX_Vector vd23_hf = Q6_Vhf_equals_Vqf16 (vd23_qf16 ); 54 55HVX_Vector vd01_inv_hf = hvx_vec_inverse_f16 (vd01_hf ); 56HVX_Vector vd23_inv_hf = hvx_vec_inverse_f16 (vd23_hf ); 57vx01_hf = Q6_Vhf_equals_Vqf16 (Q6_Vqf16_vmpy_VhfVhf (vx01_hf ,vd01_inv_hf )); 58vx23_hf = Q6_Vhf_equals_Vqf16 (Q6_Vqf16_vmpy_VhfVhf (vx23_hf ,vd23_inv_hf )); 59 60HVX_Vector vx01_i16 = hvx_vec_i16_from_hf_rnd_sat (vx01_hf ); 61HVX_Vector vx23_i16 = hvx_vec_i16_from_hf_rnd_sat (vx23_hf ); 62HVX_Vector vx_i8 = Q6_Vb_vpack_VhVh_sat (vx23_i16 ,vx01_i16 ); 63 64hvx_vec_store_u (& dst [i + 0 ].d ,2 ,vd01_hf ); 65hvx_vec_store_u (dst [i + 0 ].qs ,32 ,vx_i8 ); 66 67hvx_vec_store_u (& dst [i + 1 ].d ,2 ,Q6_V_vror_VR (vd01_hf ,64 )); 68hvx_vec_store_u (dst [i + 1 ].qs ,32 ,Q6_V_vror_VR (vx_i8 ,32 )); 69 70hvx_vec_store_u (& dst [i + 2 ].d ,2 ,vd23_hf ); 71hvx_vec_store_u (dst [i + 2 ].qs ,32 ,Q6_V_vror_VR (vx_i8 ,64 )); 72 73hvx_vec_store_u (& dst [i + 3 ].d ,2 ,Q6_V_vror_VR (vd23_hf ,64 )); 74hvx_vec_store_u (dst [i + 3 ].qs ,32 ,Q6_V_vror_VR (vx_i8 ,96 )); 75 } 76 77for (;i < nb ;i ++ ) { 78const float * block_src = src_ptr + i * QK8_0 ; 79HVX_Vector vx = * (const HVX_UVector * )block_src ; 80HVX_Vector v_abs = hvx_vec_abs_f32 (vx ); 81HVX_Vector v_max = hvx_vec_reduce_max_f32 (v_abs ); 82float amax = hvx_vec_get_f32 (v_max ); 83 84const float d = amax /127.0f ; 85const float id = d ? (1.0f /d ) :0.0f ; 86dst [i ].d = GGML_FP32_TO_FP16 (d ); 87 88HVX_Vector vid = hvx_vec_splat_f32 (id ); 89HVX_Vector v_scaled = hvx_vec_mul_f32_f32 (vx ,vid ); 90HVX_Vector v_scaled_qf = Q6_Vqf32_vsub_VsfVsf (v_scaled ,zero ); 91HVX_Vector v_scaled_hf = Q6_Vh_vdeal_Vh (Q6_Vhf_equals_Wqf32 (Q6_W_vcombine_VV (zero ,v_scaled_qf ))); 92HVX_Vector v_i16 = hvx_vec_i16_from_hf_rnd_sat (v_scaled_hf ); 93HVX_Vector v_i8 = Q6_Vb_vpack_VhVh_sat (zero ,v_i16 ); 94 95hvx_vec_store_u (dst [i ].qs ,32 ,v_i8 ); 96 } 97} 98 99static inline void hvx_dequantize_row_q8_0_f32 (float * restrictdst_ptr ,const void * restrictsrc_ptr ,int n ) { 100const int nb = n /QK8_0 ; 101const block_q8_0 * src = (const block_q8_0 * )src_ptr ; 102 103for (int i = 0 ;i < nb ;i ++ ) { 104HVX_Vector vd_f16 = Q6_Vh_vsplat_R (* (const int16_t * )& src [i ].d ); 105HVX_VectorPair vp_f32 = hvx_vec_f16_to_f32 (vd_f16 ); 106HVX_Vector vd = Q6_V_lo_W (vp_f32 ); 107 108HVX_Vector vq_i8 = * (const HVX_UVector * )src [i ].qs ; 109 110HVX_VectorPair p16 = Q6_Wh_vunpack_Vb (vq_i8 ); 111HVX_Vector v_i16 = Q6_V_lo_W (p16 ); 112HVX_VectorPair p32 = Q6_Ww_vunpack_Vh (v_i16 ); 113HVX_Vector v_i32 = Q6_V_lo_W (p32 ); 114 115HVX_Vector v_f32 = Q6_Vsf_equals_Vw (v_i32 ); 116HVX_Vector res = hvx_vec_mul_f32_f32 (v_f32 ,vd ); 117 118float * block_dst = dst_ptr + i * QK8_0 ; 119hvx_vmem (block_dst )= res ; 120 } 121} 122 123static inline void hvx_dequantize_row_q8_0_f16 (__fp16 * restrictdst_ptr ,const void * restrictsrc_ptr ,int n ) { 124const int nb = n /QK8_0 ; 125const block_q8_0 * src = (const block_q8_0 * )src_ptr ; 126 127for (int i = nb - 1 ;i >=0 ;i -- ) { 128HVX_Vector vd_f16 = Q6_Vh_vsplat_R (* (const int16_t * )& src [i ].d ); 129HVX_VectorPair vp_f32 = hvx_vec_f16_to_f32 (vd_f16 ); 130HVX_Vector vd = Q6_V_lo_W (vp_f32 ); 131 132HVX_Vector vq_i8 = * (const HVX_UVector * )src [i ].qs ; 133 134HVX_VectorPair p16 = Q6_Wh_vunpack_Vb (vq_i8 ); 135HVX_Vector v_i16 = Q6_V_lo_W (p16 ); 136HVX_VectorPair p32 = Q6_Ww_vunpack_Vh (v_i16 ); 137HVX_Vector v_i32 = Q6_V_lo_W (p32 ); 138 139HVX_Vector v_f32 = Q6_Vsf_equals_Vw (v_i32 ); 140HVX_Vector res_f32 = hvx_vec_mul_f32_f32 (v_f32 ,vd ); 141 142HVX_Vector res_f16 = hvx_vec_f32_to_f16 (res_f32 ,Q6_V_vzero ()); 143 144__fp16 * block_dst = dst_ptr + i * QK8_0 ; 145hvx_vec_store_u (block_dst ,QK8_0 * sizeof (__fp16 ),res_f16 ); 146 } 147} 148 149static inline void hvx_dequantize_row_f16_f32 (float * restrictdst_ptr ,const void * restrictsrc_ptr ,int n ) { 150const int nb = n /32 ; 151const _Float16 * src = (const _Float16 * )src_ptr ; 152 153for (int i = 0 ;i < nb ;i ++ ) { 154HVX_Vector v_f16 = * (const HVX_UVector * ) (src + i * 32 ); 155HVX_VectorPair vp_f32 = hvx_vec_f16_to_f32 (v_f16 ); 156HVX_Vector res = Q6_V_lo_W (vp_f32 ); 157 158float * block_dst = dst_ptr + i * 32 ; 159hvx_vmem (block_dst )= res ; 160 } 161} 162 163 164 165#endif // HVX_QUANT_H