char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
be2c6d7d1
char/aliceai-t5-moe
1#define GGML_COMMON_IMPL_C 2#include "ggml-common.h" 3#include "ggml-quants.h" 4#include "ggml-impl.h" 5#include "ggml-cpu.h" 6#include "simd-mappings.h" 7 8#include "../../quants.h" 9#include "../../ggml-cpu-impl.h" 10 11#include <math.h> 12#include <string.h> 13#include <assert.h> 14#include <float.h> 15#include <stdlib.h> // for qsort 16#include <stdio.h> // for GGML_ASSERT 17 18#define GROUP_MAX_EPS 1e-15f 19#define GROUP_MAX_EPS_IQ3_XXS 1e-8f 20#define GROUP_MAX_EPS_IQ2_S 1e-8f 21#define GROUP_MAX_EPS_IQ1_M 1e-7f 22#define GROUP_MAX_EPS_IQ1_S 1e-12f 23 24#define UNUSED GGML_UNUSED 25 26#if defined(__VXE__ )|| defined(__VXE2__ ) 27#define B1 (c ,s ,n ) 0x ## n ## c , 0x ## n ## s 28#define B2 (c ,s ,n ) B1(c,s,n ## c), B1(c,s,n ## s) 29#define B3 (c ,s ,n ) B2(c,s,n ## c), B2(c,s,n ## s) 30#define B4 (c ,s ,n ) B3(c,s,n ## c), B3(c,s,n ## s) 31#define B5 (c ,s ,n ) B4(c,s,n ## c), B4(c,s,n ## s) 32#define B6 (c ,s ,n ) B5(c,s,n ## c), B5(c,s,n ## s) 33#define B7 (c ,s ,n ) B6(c,s,n ## c), B6(c,s,n ## s) 34#define B8 (c ,s ) B7(c,s, c), B7(c,s, s) 35 36// precomputed tables for expanding 8bits to 8 bytes: 37static const __attribute__((aligned (16 )))uint64_t table_b2b_0 [1 <<8 ]= {B8 (00 ,10 ) };// ( b ) << 4 38static const __attribute__((aligned (16 )))uint64_t table_b2b_1 [1 <<8 ]= {B8 (10 ,00 ) };// (!b) << 4 39 40// permute mask for byteswapping 41static const uint8x16_t v_kperm = (const uint8x16_t ){ 427 ,6 ,5 ,4 ,3 ,2 ,1 ,0 , 4315 ,14 ,13 ,12 ,11 ,10 ,9 ,8 44}; 45#endif 46 47void quantize_row_q8_0 (const float * GGML_RESTRICT x ,void * GGML_RESTRICT vy ,int64_t k ) { 48assert (QK8_0 == 32 ); 49assert (k %QK8_0 == 0 ); 50const int nb = k /QK8_0 ; 51 52block_q8_0 * GGML_RESTRICT y = vy ; 53 54#if defined(__VXE__ )|| defined(__VXE2__ ) 55for (int i = 0 ;i < nb ;i ++ ) { 56float32x4_t srcv [8 ]; 57float32x4_t asrcv [8 ]; 58float32x4_t amaxv [8 ]; 59 60for (int j = 0 ;j < 8 ;j ++ )srcv [j ]= vec_xl (0 ,x + i * 32 + 4 * j ); 61for (int j = 0 ;j < 8 ;j ++ )asrcv [j ]= vec_abs (srcv [j ]); 62for (int j = 0 ;j < 4 ;j ++ )amaxv [2 * j ]= vec_max (asrcv [2 * j ],asrcv [2 * j + 1 ]); 63for (int j = 0 ;j < 2 ;j ++ )amaxv [4 * j ]= vec_max (amaxv [4 * j ],amaxv [4 * j + 2 ]); 64for (int j = 0 ;j < 1 ;j ++ )amaxv [8 * j ]= vec_max (amaxv [8 * j ],amaxv [8 * j + 4 ]); 65 66const float amax = MAX (MAX (vec_extract (amaxv [0 ],0 ), 67vec_extract (amaxv [0 ],1 )), 68MAX (vec_extract (amaxv [0 ],2 ), 69vec_extract (amaxv [0 ],3 ))); 70 71const float d = amax / ((1 <<7 )- 1 ); 72const float id = d ?1.0f /d :0.0f ; 73 74y [i ].d = GGML_CPU_FP32_TO_FP16 (d ); 75 76for (int j = 0 ;j < 8 ;j ++ ) { 77const float32x4_t v = vec_mul (srcv [j ],vec_splats (id )); 78/* Uses non-default rounding for vec_signed or vec_round */ 79const int32x4_t vi = vec_signed (__builtin_s390_vfisb (v ,4 ,1 )); 80 81y [i ].qs [4 * j + 0 ]= vec_extract (vi ,0 ); 82y [i ].qs [4 * j + 1 ]= vec_extract (vi ,1 ); 83y [i ].qs [4 * j + 2 ]= vec_extract (vi ,2 ); 84y [i ].qs [4 * j + 3 ]= vec_extract (vi ,3 ); 85 } 86 } 87#else 88GGML_UNUSED (nb ); 89// scalar 90quantize_row_q8_0_ref (x ,y ,k ); 91#endif 92} 93 94void quantize_row_q8_1 (const float * GGML_RESTRICT x ,void * GGML_RESTRICT vy ,int64_t k ) { 95assert (k %QK8_1 == 0 ); 96const int nb = k /QK8_1 ; 97 98block_q8_1 * GGML_RESTRICT y = vy ; 99 100#if defined(__VXE__ )|| defined(__VXE2__ ) 101for (int i = 0 ;i < nb ;i ++ ) { 102float32x4_t srcv [8 ]; 103float32x4_t asrcv [8 ]; 104float32x4_t amaxv [8 ]; 105 106for (int j = 0 ;j < 8 ;j ++ )srcv [j ]= vec_xl (0 ,x + i * 32 + 4 * j ); 107for (int j = 0 ;j < 8 ;j ++ )asrcv [j ]= vec_abs (srcv [j ]); 108for (int j = 0 ;j < 4 ;j ++ )amaxv [2 * j ]= vec_max (asrcv [2 * j ],asrcv [2 * j + 1 ]); 109for (int j = 0 ;j < 2 ;j ++ )amaxv [4 * j ]= vec_max (amaxv [4 * j ],amaxv [4 * j + 2 ]); 110for (int j = 0 ;j < 1 ;j ++ )amaxv [8 * j ]= vec_max (amaxv [8 * j ],amaxv [8 * j + 4 ]); 111 112const float amax = MAX (MAX (vec_extract (amaxv [0 ],0 ), 113vec_extract (amaxv [0 ],1 )), 114MAX (vec_extract (amaxv [0 ],2 ), 115vec_extract (amaxv [0 ],3 ))); 116 117const float d = amax / ((1 <<7 )- 1 ); 118const float id = d ?1.0f /d :0.0f ; 119 120y [i ].d = GGML_CPU_FP32_TO_FP16 (d ); 121 122int32x4_t acc = vec_splats (0 ); 123 124for (int j = 0 ;j < 8 ;j ++ ) { 125const float32x4_t v = vec_mul (srcv [j ],vec_splats (id )); 126/* Uses non-default rounding for vec_signed or vec_round */ 127const int32x4_t vi = vec_signed (__builtin_s390_vfisb (v ,4 ,1 )); 128 129y [i ].qs [4 * j + 0 ]= vec_extract (vi ,0 ); 130y [i ].qs [4 * j + 1 ]= vec_extract (vi ,1 ); 131y [i ].qs [4 * j + 2 ]= vec_extract (vi ,2 ); 132y [i ].qs [4 * j + 3 ]= vec_extract (vi ,3 ); 133 134acc = vec_add (acc ,vi ); 135 } 136 137y [i ].s = GGML_CPU_FP32_TO_FP16 (d * (acc [0 ]+ acc [1 ]+ acc [2 ]+ acc [3 ])); 138 } 139#else 140GGML_UNUSED (nb ); 141// scalar 142quantize_row_q8_1_ref (x ,y ,k ); 143#endif 144} 145 146 147//===================================== Dot products ================================= 148 149void ggml_vec_dot_q1_0_q8_0 (int n ,float * GGML_RESTRICT s ,size_t bs ,const void * GGML_RESTRICT vx ,size_t bx ,const void * GGML_RESTRICT vy ,size_t by ,int nrc ) { 150const int qk = QK1_0 ;// 128 151const int nb = n /qk ; 152 153assert (n %qk == 0 ); 154assert (nrc == 1 ); 155UNUSED (nrc ); 156UNUSED (bx ); 157UNUSED (by ); 158UNUSED (bs ); 159 160const block_q1_0 * GGML_RESTRICT x = vx ; 161const block_q8_0 * GGML_RESTRICT y = vy ; 162 163#if defined(__VXE__ )|| defined(__VXE2__ ) 164float32x4_t v_sumf = vec_splats (0.0f ); 165 166const uint8x16_t v_zero = vec_splats ((uint8_t )0x00 );// zero 167const uint8x16_t v_bias = vec_splats ((uint8_t )0x80 );// bias from signed to unsigned 168// v ^ 0x80 == v + 128 169 170const uint8x16_t v_idx = (const uint8x16_t ){0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 }; 171const uint8x16_t v_bit = (const uint8x16_t ){1 ,2 ,4 ,8 ,16 ,32 ,64 ,128 ,1 ,2 ,4 ,8 ,16 ,32 ,64 ,128 }; 172 173for (int i = 0 ;i < nb ;++ i ) { 174const uint8x16_t v_x = vec_xl (0 , (const uint8_t * )x [i ].qs ); 175const float32x4_t v_xd = vec_splats (GGML_CPU_FP16_TO_FP32 (x [i ].d )); 176 177for (int k = 0 ;k < 4 ;++ k ) { 178// sub-block k holds elements 32k .. 32k+31 179const block_q8_0 * GGML_RESTRICT yb = & y [i * 4 + k ]; 180const float32x4_t v_yd = vec_splats (GGML_CPU_FP16_TO_FP32 (yb -> d )); 181 182const uint8x16_t v_xrl = vec_perm (v_x ,v_x ,vec_add (v_idx ,vec_splats ((uint8_t )(k * 4 + 0 )))); 183const uint8x16_t v_xrh = vec_perm (v_x ,v_x ,vec_add (v_idx ,vec_splats ((uint8_t )(k * 4 + 2 )))); 184 185// isolate each lane's bit, then set all ones where that bit is clear, the -d case 186const int8x16_t v_ml = (int8x16_t )vec_cmpeq (vec_and (v_xrl ,v_bit ),v_zero ); 187const int8x16_t v_mh = (int8x16_t )vec_cmpeq (vec_and (v_xrh ,v_bit ),v_zero ); 188 189const int8x16_t v_yl = vec_xl (0 , (const int8_t * )yb -> qs ); 190const int8x16_t v_yh = vec_xl (QK8_0 /2 , (const int8_t * )yb -> qs ); 191 192// weights are only +1 or -1, so negate y 193const int8x16_t v_ysl = vec_sub (vec_xor (v_yl ,v_ml ),v_ml ); 194const int8x16_t v_ysh = vec_sub (vec_xor (v_yh ,v_mh ),v_mh ); 195 196// bias to unsigned, then vec_sum4 adds each group of 4 bytes into one word 197const uint32x4_t v_p = vec_add (vec_sum4 (vec_xor ((uint8x16_t )v_ysl ,v_bias ),v_zero ), 198vec_sum4 (vec_xor ((uint8x16_t )v_ysh ,v_bias ),v_zero )); 199 200// each word summed 8 biased bytes, so take back 8 * 128 201const int32x4_t v_xy = vec_sub ((int32x4_t )v_p ,vec_splats ((int32_t )1024 )); 202 203// apply both block scales and add into the running total 204v_sumf = vec_madd (vec_float (v_xy ),vec_mul (v_xd ,v_yd ),v_sumf ); 205 } 206 } 207 208* s = vec_hsum_f32x4 (v_sumf ); 209#else 210UNUSED (nb ); 211UNUSED (x ); 212UNUSED (y ); 213ggml_vec_dot_q1_0_q8_0_generic (n ,s ,bs ,vx ,bx ,vy ,by ,nrc ); 214#endif 215} 216 217void ggml_vec_dot_q4_0_q8_0 (int n ,float * GGML_RESTRICT s ,size_t bs ,const void * GGML_RESTRICT vx ,size_t bx ,const void * GGML_RESTRICT vy ,size_t by ,int nrc ) { 218const int qk = QK8_0 ; 219const int nb = n /qk ; 220 221assert (n %qk == 0 ); 222assert (nrc == 1 ); 223UNUSED (nrc ); 224UNUSED (bx ); 225UNUSED (by ); 226UNUSED (bs ); 227 228const block_q4_0 * GGML_RESTRICT x = vx ; 229const block_q8_0 * GGML_RESTRICT y = vy ; 230 231int ib = 0 ; 232float sumf = 0 ; 233 234#if defined(__VXE__ )|| defined(__VXE2__ ) 235float32x4_t acc = vec_splats (0.0f ); 236 237const uint8x16_t v_m = vec_splats ((const uint8_t )0x0F ); 238const int8x16_t v_s = vec_splats ( (const int8_t )0x08 ); 239 240for (;ib < nb ;++ ib ) { 241const uint8x16_t v_x = vec_xl (0 ,x [ib ].qs ); 242const int8x16_t v_xl = (const int8x16_t )(v_x & v_m ); 243const int8x16_t v_xh = (const int8x16_t )(v_x >>4 ); 244 245const int8x16_t v_xls = vec_sub (v_xl ,v_s ); 246const int8x16_t v_xhs = vec_sub (v_xh ,v_s ); 247 248const int8x16_t v_yl = vec_xl (0 ,y [ib ].qs ); 249const int8x16_t v_yh = vec_xl (QK8_0 /2 ,y [ib ].qs ); 250 251const int16x8_t v_xylso = vec_mulo (v_xls ,v_yl ); 252const int16x8_t v_xyl = vec_meadd (v_xls ,v_yl ,v_xylso ); 253const int16x8_t v_xyhso = vec_mulo (v_xhs ,v_yh ); 254const int16x8_t v_xyh = vec_meadd (v_xhs ,v_yh ,v_xyhso ); 255 256int16x8_t v_xy_ = v_xyl + v_xyh ;v_xy_ += vec_reve (v_xy_ ); 257 258const float32x4_t v_xy = vec_float (vec_unpackh (v_xy_ )); 259const float32x4_t v_d = vec_splats (GGML_CPU_FP16_TO_FP32 (x [ib ].d )* GGML_CPU_FP16_TO_FP32 (y [ib ].d )); 260 261acc = vec_madd (v_xy ,v_d ,acc ); 262 } 263 264sumf = vec_hsum_f32x4 (acc ); 265* s = sumf ; 266#else 267UNUSED (nb ); 268UNUSED (x ); 269UNUSED (y ); 270UNUSED (ib ); 271UNUSED (sumf ); 272ggml_vec_dot_q4_0_q8_0_generic (n ,s ,bs ,vx ,bx ,vy ,by ,nrc ); 273#endif 274} 275 276void ggml_vec_dot_q4_1_q8_1 (int n ,float * GGML_RESTRICT s ,size_t bs ,const void * GGML_RESTRICT vx ,size_t bx ,const void * GGML_RESTRICT vy ,size_t by ,int nrc ) { 277const int qk = QK8_1 ; 278const int nb = n /qk ; 279 280assert (n %qk == 0 ); 281assert (nrc == 1 ); 282UNUSED (nrc ); 283UNUSED (bx ); 284UNUSED (by ); 285UNUSED (bs ); 286 287const block_q4_1 * GGML_RESTRICT x = vx ; 288const block_q8_1 * GGML_RESTRICT y = vy ; 289 290int ib = 0 ; 291float sumf = 0 ; 292 293#if defined(__VXE__ )|| defined(__VXE2__ ) 294float summs = 0 ; 295float32x4_t acc = vec_splats (0.0f ); 296 297const uint8x16_t v_m = vec_splat_u8 (0x0F ); 298 299#pragma GCC unroll 4 300for (;ib < nb ;++ ib ) { 301__builtin_prefetch (x [ib ].qs ,0 ,1 ); 302__builtin_prefetch (y [ib ].qs ,0 ,1 ); 303 304summs += GGML_CPU_FP16_TO_FP32 (x [ib ].m )* GGML_CPU_FP16_TO_FP32 (y [ib ].s ); 305 306const uint8x16_t v_x = vec_xl (0 ,x [ib ].qs ); 307const int8x16_t v_xl = (const int8x16_t )(v_x & v_m ); 308const int8x16_t v_xh = (const int8x16_t )(v_x >>4 ); 309 310const int8x16_t v_yl = vec_xl (0 ,y [ib ].qs ); 311const int8x16_t v_yh = vec_xl (QK8_1 /2 ,y [ib ].qs ); 312 313const int32x4_t v_xy_ = ggml_vec_dot (ggml_vec_dot (vec_splats (0 ),v_xl ,v_yl ),v_xh ,v_yh ); 314const float32x4_t v_xy = vec_float (v_xy_ ); 315 316const float32x4_t v_d = vec_splats (GGML_CPU_FP16_TO_FP32 (x [ib ].d )* GGML_CPU_FP16_TO_FP32 (y [ib ].d )); 317 318acc = vec_madd (v_xy ,v_d ,acc ); 319 } 320 321sumf = vec_hsum_f32x4 (acc )+ summs ; 322* s = sumf ; 323#else 324UNUSED (nb ); 325UNUSED (x ); 326UNUSED (y ); 327UNUSED (ib ); 328UNUSED (sumf ); 329ggml_vec_dot_q4_1_q8_1_generic (n ,s ,bs ,vx ,bx ,vy ,by ,nrc ); 330#endif 331} 332 333void ggml_vec_dot_mxfp4_q8_0 (int n ,float * GGML_RESTRICT s ,size_t bs ,const void * GGML_RESTRICT vx ,size_t bx ,const void * GGML_RESTRICT vy ,size_t by ,int nrc ) { 334assert (nrc == 1 ); 335UNUSED (nrc ); 336UNUSED (bx ); 337UNUSED (by ); 338UNUSED (bs ); 339assert (n %QK_MXFP4 == 0 ); 340static_assert (QK_MXFP4 == QK8_0 ,"QK_MXFP4 and QK8_0 must be the same" ); 341 342const int qk = QK_MXFP4 ; 343const int nb = n /qk ; 344 345const block_mxfp4 * GGML_RESTRICT x = vx ; 346const block_q8_0 * GGML_RESTRICT y = vy ; 347 348int ib = 0 ; 349float sumf = 0.0f ; 350 351#if defined(__VXE__ )|| defined(__VXE2__ ) 352const int8x16_t v_k = vec_xl (0 ,kvalues_mxfp4 ); 353const uint8x16_t v_m = vec_splats ((const uint8_t )0x0F ); 354 355float32x4_t v_acc = vec_splats (0.0f ); 356 357#pragma GCC unroll 8 358for (;ib + 1 < nb ;ib += 2 ) { 359const block_mxfp4 * GGML_RESTRICT x0 = & x [ib + 0 ]; 360const block_mxfp4 * GGML_RESTRICT x1 = & x [ib + 1 ]; 361const block_q8_0 * GGML_RESTRICT y0 = & y [ib + 0 ]; 362const block_q8_0 * GGML_RESTRICT y1 = & y [ib + 1 ]; 363 364const uint8x16_t v_x0 = vec_xl (0 ,x0 -> qs ); 365const uint8x16_t v_x1 = vec_xl (0 ,x1 -> qs ); 366 367int8x16_t v_x0l = (int8x16_t )vec_and (v_x0 ,v_m ); 368int8x16_t v_x0h = (int8x16_t )vec_sr (v_x0 ,4 ); 369int8x16_t v_x1l = (int8x16_t )vec_and (v_x1 ,v_m ); 370int8x16_t v_x1h = (int8x16_t )vec_sr (v_x1 ,4 ); 371 372v_x0l = vec_perm (v_k ,v_k , (uchar8x16_t )v_x0l ); 373v_x0h = vec_perm (v_k ,v_k , (uchar8x16_t )v_x0h ); 374v_x1l = vec_perm (v_k ,v_k , (uchar8x16_t )v_x1l ); 375v_x1h = vec_perm (v_k ,v_k , (uchar8x16_t )v_x1h ); 376 377const int8x16_t v_y0l = vec_xl (0 ,y0 -> qs ); 378const int8x16_t v_y0h = vec_xl (QK8_0 /2 ,y0 -> qs ); 379const int8x16_t v_y1l = vec_xl (0 ,y1 -> qs ); 380const int8x16_t v_y1h = vec_xl (QK8_0 /2 ,y1 -> qs ); 381 382const int32x4_t v_xy0 = ggml_vec_dot (ggml_vec_dot (vec_splats (0 ),v_x0l ,v_y0l ),v_x0h ,v_y0h ); 383const int32x4_t v_xy1 = ggml_vec_dot (ggml_vec_dot (vec_splats (0 ),v_x1l ,v_y1l ),v_x1h ,v_y1h ); 384 385const float32x4_t v_xy0f = vec_float (v_xy0 ); 386const float32x4_t v_xy1f = vec_float (v_xy1 ); 387 388const float32x4_t v_d0 = vec_splats (GGML_E8M0_TO_FP32_HALF (x0 -> e )* GGML_CPU_FP16_TO_FP32 (y0 -> d )); 389const float32x4_t v_d1 = vec_splats (GGML_E8M0_TO_FP32_HALF (x1 -> e )* GGML_CPU_FP16_TO_FP32 (y1 -> d )); 390 391v_acc = vec_madd (v_xy0f ,v_d0 ,v_acc ); 392v_acc = vec_madd (v_xy1f ,v_d1 ,v_acc ); 393 } 394 395for (;ib < nb ;++ ib ) { 396const block_mxfp4 * GGML_RESTRICT x0 = & x [ib + 0 ]; 397const block_q8_0 * GGML_RESTRICT y0 = & y [ib + 0 ]; 398 399const uint8x16_t v_x = vec_xl (0 ,x0 -> qs ); 400 401int8x16_t v_xl = (int8x16_t )vec_and (v_x ,v_m ); 402int8x16_t v_xh = (int8x16_t )vec_sr (v_x ,4 ); 403 404v_xl = vec_perm (v_k ,v_k , (uchar8x16_t )v_xl ); 405v_xh = vec_perm (v_k ,v_k , (uchar8x16_t )v_xh ); 406 407const int8x16_t v_yl = vec_xl (0 ,y0 -> qs ); 408const int8x16_t v_yh = vec_xl (QK8_0 /2 ,y0 -> qs ); 409 410const int32x4_t v_xy = ggml_vec_dot (ggml_vec_dot (vec_splats (0 ),v_xl ,v_yl ),v_xh ,v_yh ); 411const float32x4_t v_xyf = vec_float (v_xy ); 412 413const float32x4_t v_d = vec_splats (GGML_E8M0_TO_FP32_HALF (x0 -> e )* GGML_CPU_FP16_TO_FP32 (y0 -> d )); 414v_acc = vec_madd (v_xyf ,v_d ,v_acc ); 415 } 416 417sumf = vec_hsum_f32x4 (v_acc ); 418* s = sumf ; 419#else 420UNUSED (nb ); 421UNUSED (x ); 422UNUSED (y ); 423UNUSED (ib ); 424UNUSED (sumf ); 425ggml_vec_dot_mxfp4_q8_0_generic (n ,s ,bs ,vx ,bx ,vy ,by ,nrc ); 426#endif 427} 428 429void ggml_vec_dot_q5_0_q8_0 (int n ,float * GGML_RESTRICT s ,size_t bs ,const void * GGML_RESTRICT vx ,size_t bx ,const void * GGML_RESTRICT vy ,size_t by ,int nrc ) { 430const int qk = QK8_0 ; 431const int nb = n /qk ; 432 433assert (n %qk == 0 ); 434assert (qk == QK5_0 ); 435assert (nrc == 1 ); 436UNUSED (nrc ); 437UNUSED (bx ); 438UNUSED (by ); 439UNUSED (bs ); 440 441const block_q5_0 * GGML_RESTRICT x = vx ; 442const block_q8_0 * GGML_RESTRICT y = vy ; 443 444int ib = 0 ; 445float sumf = 0.0f ; 446 447#if defined(__VXE__ )|| defined(__VXE2__ ) 448float32x4_t v_sum0 = vec_splats (0.0f ); 449float32x4_t v_sum1 = vec_splats (0.0f ); 450 451uint32_t qh0 ,qh1 ; 452uint64_t tmp0 [4 ],tmp1 [4 ]; 453 454const uint8x16_t v_m = vec_splats ((uint8_t )0x0F ); 455 456#pragma GCC unroll 4 457for (;ib + 1 < nb ;ib += 2 ) { 458const block_q5_0 * GGML_RESTRICT x0 = & x [ib + 0 ]; 459const block_q5_0 * GGML_RESTRICT x1 = & x [ib + 1 ]; 460const block_q8_0 * GGML_RESTRICT y0 = & y [ib + 0 ]; 461const block_q8_0 * GGML_RESTRICT y1 = & y [ib + 1 ]; 462 463memcpy (& qh0 ,x0 -> qh ,sizeof (qh0 )); 464memcpy (& qh1 ,x1 -> qh ,sizeof (qh1 )); 465 466tmp0 [0 ]= table_b2b_1 [(qh0 >>0 )& 0xFF ]; 467tmp0 [1 ]= table_b2b_1 [(qh0 >>8 )& 0xFF ]; 468tmp0 [2 ]= table_b2b_1 [(qh0 >>16 )& 0xFF ]; 469tmp0 [3 ]= table_b2b_1 [(qh0 >>24 ) ]; 470 471tmp1 [0 ]= table_b2b_1 [(qh1 >>0 )& 0xFF ]; 472tmp1 [1 ]= table_b2b_1 [(qh1 >>8 )& 0xFF ]; 473tmp1 [2 ]= table_b2b_1 [(qh1 >>16 )& 0xFF ]; 474tmp1 [3 ]= table_b2b_1 [(qh1 >>24 ) ]; 475 476int8x16_t v_qh0l = vec_xl (0 , (const int8_t * )(tmp0 + 0 )); 477int8x16_t v_qh0h = vec_xl (0 , (const int8_t * )(tmp0 + 2 )); 478int8x16_t v_qh1l = vec_xl (0 , (const int8_t * )(tmp1 + 0 )); 479int8x16_t v_qh1h = vec_xl (0 , (const int8_t * )(tmp1 + 2 )); 480 481// required for fixing the byteorder 482v_qh0l = vec_perm (v_qh0l ,v_qh0l ,v_kperm ); 483v_qh0h = vec_perm (v_qh0h ,v_qh0h ,v_kperm ); 484v_qh1l = vec_perm (v_qh1l ,v_qh1l ,v_kperm ); 485v_qh1h = vec_perm (v_qh1h ,v_qh1h ,v_kperm ); 486 487const uint8x16_t v_x0 = vec_xl (0 , (const uint8_t * )x0 -> qs ); 488const uint8x16_t v_x1 = vec_xl (0 , (const uint8_t * )x1 -> qs ); 489 490int8x16_t v_x0l = (int8x16_t )vec_and (v_x0 ,v_m ); 491int8x16_t v_x0h = (int8x16_t )vec_sr (v_x0 ,4 ); 492int8x16_t v_x1l = (int8x16_t )vec_and (v_x1 ,v_m ); 493int8x16_t v_x1h = (int8x16_t )vec_sr (v_x1 ,4 ); 494 495const int8x16_t v_x0lf = vec_sub (v_x0l ,v_qh0l ); 496const int8x16_t v_x0hf = vec_sub (v_x0h ,v_qh0h ); 497const int8x16_t v_x1lf = vec_sub (v_x1l ,v_qh1l ); 498const int8x16_t v_x1hf = vec_sub (v_x1h ,v_qh1h ); 499 500const int8x16_t v_y0l = vec_xl (0 , (const int8_t * )y0 -> qs ); 501const int8x16_t v_y0h = vec_xl (QK8_0 /2 , (const int8_t * )y0 -> qs ); 502const int8x16_t v_y1l = vec_xl (0 , (const int8_t * )y1 -> qs ); 503const int8x16_t v_y1h = vec_xl (QK8_0 /2 , (const int8_t * )y1 -> qs ); 504 505const int32x4_t v_xy0 = ggml_vec_dot (ggml_vec_dot (vec_splats (0 ),v_x0lf ,v_y0l ),v_x0hf ,v_y0h ); 506const int32x4_t v_xy1 = ggml_vec_dot (ggml_vec_dot (vec_splats (0 ),v_x1lf ,v_y1l ),v_x1hf ,v_y1h ); 507 508const float32x4_t v_xy0f = vec_float (v_xy0 ); 509const float32x4_t v_xy1f = vec_float (v_xy1 ); 510 511const float32x4_t v_d0 = vec_splats (GGML_CPU_FP16_TO_FP32 (x0 -> d )* GGML_CPU_FP16_TO_FP32 (y0 -> d )); 512const float32x4_t v_d1 = vec_splats (GGML_CPU_FP16_TO_FP32 (x1 -> d )* GGML_CPU_FP16_TO_FP32 (y1 -> d )); 513 514v_sum0 = vec_madd (v_xy0f ,v_d0 ,v_sum0 ); 515v_sum1 = vec_madd (v_xy1f ,v_d1 ,v_sum1 ); 516 } 517 518sumf += vec_hsum_f32x4 (v_sum0 )+ vec_hsum_f32x4 (v_sum1 ); 519 520#pragma GCC unroll 4 521for (;ib < nb ;++ ib ) { 522const block_q5_0 * GGML_RESTRICT x0 = & x [ib ]; 523const block_q8_0 * GGML_RESTRICT y0 = & y [ib ]; 524 525uint32_t qh ; 526memcpy (& qh ,x0 -> qh ,sizeof (qh )); 527 528uint64_t tmp [4 ]; 529tmp [0 ]= table_b2b_1 [(qh >>0 )& 0xFF ]; 530tmp [1 ]= table_b2b_1 [(qh >>8 )& 0xFF ]; 531tmp [2 ]= table_b2b_1 [(qh >>16 )& 0xFF ]; 532tmp [3 ]= table_b2b_1 [(qh >>24 ) ]; 533 534int8x16_t v_qhl = vec_xl (0 , (const int8_t * )(tmp + 0 )); 535int8x16_t v_qhh = vec_xl (0 , (const int8_t * )(tmp + 2 )); 536 537// required for fixing the byteorder 538v_qhl = vec_perm (v_qhl ,v_qhl ,v_kperm ); 539v_qhh = vec_perm (v_qhh ,v_qhh ,v_kperm ); 540 541const uint8x16_t v_x = vec_xl (0 , (const uint8_t * )x0 -> qs ); 542int8x16_t v_xl = (int8x16_t )vec_and (v_x ,v_m ); 543int8x16_t v_xh = (int8x16_t )vec_sr (v_x ,4 ); 544 545const int8x16_t v_xlf = vec_sub (v_xl ,v_qhl ); 546const int8x16_t v_xhf = vec_sub (v_xh ,v_qhh ); 547 548const int8x16_t v_yl = vec_xl (0 , (const int8_t * )y0 -> qs ); 549const int8x16_t v_yh = vec_xl (QK8_0 /2 , (const int8_t * )y0 -> qs ); 550 551const int32x4_t v_xy = ggml_vec_dot (ggml_vec_dot (vec_splats (0 ),v_xlf ,v_yl ),v_xhf ,v_yh ); 552const float32x4_t v_xyf = vec_float (v_xy ); 553 554const float32x4_t v_d = vec_splats (GGML_CPU_FP16_TO_FP32 (x0 -> d )* GGML_CPU_FP16_TO_FP32 (y0 -> d )); 555const float32x4_t v_acc = vec_madd (v_xyf ,v_d ,vec_splats (0.0f )); 556 557sumf += vec_hsum_f32x4 (v_acc ); 558 } 559 560* s = sumf ; 561#else 562UNUSED (nb ); 563UNUSED (x ); 564UNUSED (y ); 565UNUSED (ib ); 566UNUSED (sumf ); 567ggml_vec_dot_q5_0_q8_0_generic (n ,s ,bs ,vx ,bx ,vy ,by ,nrc ); 568#endif 569} 570 571void ggml_vec_dot_q5_1_q8_1 (int n ,float * GGML_RESTRICT s ,size_t bs ,const void * GGML_RESTRICT vx ,size_t bx ,const void * GGML_RESTRICT vy ,size_t by ,int nrc ) { 572const int qk = QK8_1 ; 573const int nb = n /qk ; 574 575assert (n %qk == 0 ); 576assert (qk == QK5_1 ); 577assert (nrc == 1 ); 578UNUSED (nrc ); 579UNUSED (bx ); 580UNUSED (by ); 581UNUSED (bs ); 582 583const block_q5_1 * GGML_RESTRICT x = vx ; 584const block_q8_1 * GGML_RESTRICT y = vy ; 585 586int ib = 0 ; 587float sumf = 0.0f ; 588 589#if defined(__VXE__ )|| defined(__VXE2__ ) 590float32x4_t v_sum0 = vec_splats (0.0f ); 591float32x4_t v_sum1 = vec_splats (0.0f ); 592 593float summs0 = 0.0f ; 594float summs1 = 0.0f ; 595 596uint32_t qh0 ; 597uint32_t qh1 ; 598 599uint64_t tmp0 [4 ]; 600uint64_t tmp1 [4 ]; 601 602const uint8x16_t v_m = vec_splats ((uint8_t )0x0F ); 603 604#pragma GCC unroll 4 605for (;ib + 1 < nb ;ib += 2 ) { 606const block_q5_1 * GGML_RESTRICT x0 = & x [ib + 0 ]; 607const block_q5_1 * GGML_RESTRICT x1 = & x [ib + 1 ]; 608const block_q8_1 * GGML_RESTRICT y0 = & y [ib + 0 ]; 609const block_q8_1 * GGML_RESTRICT y1 = & y [ib + 1 ]; 610 611summs0 += GGML_CPU_FP16_TO_FP32 (x0 -> m )* GGML_CPU_FP16_TO_FP32 (y0 -> s ); 612summs1 += GGML_CPU_FP16_TO_FP32 (x1 -> m )* GGML_CPU_FP16_TO_FP32 (y1 -> s ); 613 614memcpy (& qh0 ,x0 -> qh ,sizeof (qh0 )); 615memcpy (& qh1 ,x1 -> qh ,sizeof (qh1 )); 616 617tmp0 [0 ]= table_b2b_0 [(qh0 >>0 )& 0xFF ]; 618tmp0 [1 ]= table_b2b_0 [(qh0 >>8 )& 0xFF ]; 619tmp0 [2 ]= table_b2b_0 [(qh0 >>16 )& 0xFF ]; 620tmp0 [3 ]= table_b2b_0 [(qh0 >>24 ) ]; 621 622tmp1 [0 ]= table_b2b_0 [(qh1 >>0 )& 0xFF ]; 623tmp1 [1 ]= table_b2b_0 [(qh1 >>8 )& 0xFF ]; 624tmp1 [2 ]= table_b2b_0 [(qh1 >>16 )& 0xFF ]; 625tmp1 [3 ]= table_b2b_0 [(qh1 >>24 ) ]; 626 627int8x16_t v_qh0l = vec_xl (0 , (const int8_t * )(tmp0 + 0 )); 628int8x16_t v_qh0h = vec_xl (0 , (const int8_t * )(tmp0 + 2 )); 629int8x16_t v_qh1l = vec_xl (0 , (const int8_t * )(tmp1 + 0 )); 630int8x16_t v_qh1h = vec_xl (0 , (const int8_t * )(tmp1 + 2 )); 631 632// required for fixing the byteorder 633v_qh0l = vec_perm (v_qh0l ,v_qh0l ,v_kperm ); 634v_qh0h = vec_perm (v_qh0h ,v_qh0h ,v_kperm ); 635v_qh1l = vec_perm (v_qh1l ,v_qh1l ,v_kperm ); 636v_qh1h = vec_perm (v_qh1h ,v_qh1h ,v_kperm ); 637 638const uint8x16_t v_x0 = vec_xl (0 ,x0 -> qs ); 639const uint8x16_t v_x1 = vec_xl (0 ,x1 -> qs ); 640 641const int8x16_t v_x0l = (int8x16_t )vec_and (v_x0 ,v_m ); 642const int8x16_t v_x0h = (int8x16_t )vec_sr (v_x0 ,4 ); 643const int8x16_t v_x1l = (int8x16_t )vec_and (v_x1 ,v_m ); 644const int8x16_t v_x1h = (int8x16_t )vec_sr (v_x1 ,4 ); 645 646const int8x16_t v_x0lf = vec_or (v_x0l ,v_qh0l ); 647const int8x16_t v_x0hf = vec_or (v_x0h ,v_qh0h ); 648const int8x16_t v_x1lf = vec_or (v_x1l ,v_qh1l ); 649const int8x16_t v_x1hf = vec_or (v_x1h ,v_qh1h ); 650 651const int8x16_t v_y0l = vec_xl (0 ,y0 -> qs ); 652const int8x16_t v_y0h = vec_xl (QK8_1 /2 ,y0 -> qs ); 653const int8x16_t v_y1l = vec_xl (0 ,y1 -> qs ); 654const int8x16_t v_y1h = vec_xl (QK8_1 /2 ,y1 -> qs ); 655 656const int32x4_t v_xy0 = ggml_vec_dot (ggml_vec_dot (vec_splats (0 ),v_x0lf ,v_y0l ),v_x0hf ,v_y0h ); 657const int32x4_t v_xy1 = ggml_vec_dot (ggml_vec_dot (vec_splats (0 ),v_x1lf ,v_y1l ),v_x1hf ,v_y1h ); 658 659const float32x4_t v_xy0f = vec_float (v_xy0 ); 660const float32x4_t v_xy1f = vec_float (v_xy1 ); 661 662const float32x4_t v_d0 = vec_splats (GGML_CPU_FP16_TO_FP32 (x0 -> d )* GGML_CPU_FP16_TO_FP32 (y0 -> d )); 663const float32x4_t v_d1 = vec_splats (GGML_CPU_FP16_TO_FP32 (x1 -> d )* GGML_CPU_FP16_TO_FP32 (y1 -> d )); 664 665v_sum0 = vec_madd (v_xy0f ,v_d0 ,v_sum0 ); 666v_sum1 = vec_madd (v_xy1f ,v_d1 ,v_sum1 ); 667 } 668 669sumf += vec_hsum_f32x4 (v_sum0 )+ vec_hsum_f32x4 (v_sum1 )+ summs0 + summs1 ; 670 671#pragma GCC unroll 4 672for (;ib < nb ;++ ib ) { 673const block_q5_1 * GGML_RESTRICT x0 = & x [ib ]; 674const block_q8_1 * GGML_RESTRICT y0 = & y [ib ]; 675 676float summs = GGML_CPU_FP16_TO_FP32 (x0 -> m )* GGML_CPU_FP16_TO_FP32 (y0 -> s ); 677 678uint32_t qh ; 679memcpy (& qh ,x0 -> qh ,sizeof (qh )); 680 681uint64_t tmp [4 ]; 682tmp [0 ]= table_b2b_0 [(qh >>0 )& 0xFF ]; 683tmp [1 ]= table_b2b_0 [(qh >>8 )& 0xFF ]; 684tmp [2 ]= table_b2b_0 [(qh >>16 )& 0xFF ]; 685tmp [3 ]= table_b2b_0 [(qh >>24 ) ]; 686 687int8x16_t v_qhl = vec_xl (0 , (const int8_t * )(tmp + 0 )); 688int8x16_t v_qhh = vec_xl (0 , (const int8_t * )(tmp + 2 )); 689 690// required for fixing the byteorder 691v_qhl = vec_perm (v_qhl ,v_qhl ,v_kperm ); 692v_qhh = vec_perm (v_qhh ,v_qhh ,v_kperm ); 693 694const uint8x16_t v_x = vec_xl (0 ,x0 -> qs ); 695const int8x16_t v_xl = (int8x16_t )vec_and (v_x ,v_m ); 696const int8x16_t v_xh = (int8x16_t )vec_sr (v_x ,4 ); 697 698const int8x16_t v_xlf = vec_or (v_xl ,v_qhl ); 699const int8x16_t v_xhf = vec_or (v_xh ,v_qhh ); 700 701const int8x16_t v_yl = vec_xl (0 ,y0 -> qs ); 702const int8x16_t v_yh = vec_xl (QK8_1 /2 ,y0 -> qs ); 703 704const int32x4_t v_xy = ggml_vec_dot (ggml_vec_dot (vec_splats (0 ),v_xlf ,v_yl ),v_xhf ,v_yh ); 705const float32x4_t v_xyf = vec_float (v_xy ); 706 707const float32x4_t v_d = vec_splats (GGML_CPU_FP16_TO_FP32 (x0 -> d )* GGML_CPU_FP16_TO_FP32 (y0 -> d )); 708const float32x4_t v_acc = vec_madd (v_xyf ,v_d ,vec_splats (0.0f )); 709 710sumf += vec_hsum_f32x4 (v_acc )+ summs ; 711 } 712 713* s = sumf ; 714#else 715UNUSED (nb ); 716UNUSED (x ); 717UNUSED (y ); 718UNUSED (ib ); 719UNUSED (sumf ); 720ggml_vec_dot_q5_1_q8_1_generic (n ,s ,bs ,vx ,bx ,vy ,by ,nrc ); 721#endif 722} 723 724void ggml_vec_dot_q8_0_q8_0 (int n ,float * GGML_RESTRICT s ,size_t bs ,const void * GGML_RESTRICT vx ,size_t bx ,const void * GGML_RESTRICT vy ,size_t by ,int nrc ) { 725const int qk = QK8_0 ; 726const int nb = n /qk ; 727 728assert (n %qk == 0 ); 729assert (nrc == 1 ); 730UNUSED (nrc ); 731UNUSED (bx ); 732UNUSED (by ); 733UNUSED (bs ); 734 735const block_q8_0 * GGML_RESTRICT x = vx ; 736const block_q8_0 * GGML_RESTRICT y = vy ; 737 738int ib = 0 ; 739float sumf = 0 ; 740 741#if defined(__VXE__ )|| defined(__VXE2__ ) 742float32x4_t acc = vec_splats (0.0f ); 743 744#pragma GCC unroll 8 745for (;ib < nb ;++ ib ) { 746__builtin_prefetch (x [ib ].qs ,0 ,1 ); 747__builtin_prefetch (y [ib ].qs ,0 ,1 ); 748 749const int8x16_t v_xl = vec_xl (0 ,x [ib ].qs ); 750const int8x16_t v_xh = vec_xl (QK8_0 /2 ,x [ib ].qs ); 751const int8x16_t v_yl = vec_xl (0 ,y [ib ].qs ); 752const int8x16_t v_yh = vec_xl (QK8_0 /2 ,y [ib ].qs ); 753 754const int32x4_t v_xy_ = ggml_vec_dot (ggml_vec_dot (vec_splats (0 ),v_xl ,v_yl ),v_xh ,v_yh ); 755const float32x4_t v_xy = vec_float (v_xy_ ); 756const float32x4_t v_d = vec_splats (GGML_CPU_FP16_TO_FP32 (x [ib ].d )* GGML_CPU_FP16_TO_FP32 (y [ib ].d )); 757 758acc = vec_madd (v_xy ,v_d ,acc ); 759 } 760 761sumf = vec_hsum_f32x4 (acc ); 762 763* s = sumf ; 764#else 765UNUSED (nb ); 766UNUSED (x ); 767UNUSED (y ); 768UNUSED (ib ); 769UNUSED (sumf ); 770ggml_vec_dot_q8_0_q8_0_generic (n ,s ,bs ,vx ,bx ,vy ,by ,nrc ); 771#endif 772} 773 774void ggml_vec_dot_q3_K_q8_K (int n ,float * GGML_RESTRICT s ,size_t bs ,const void * GGML_RESTRICT vx ,size_t bx ,const void * GGML_RESTRICT vy ,size_t by ,int nrc ) { 775assert (n %QK_K == 0 ); 776assert (nrc == 1 ); 777UNUSED (nrc ); 778UNUSED (bx ); 779UNUSED (by ); 780UNUSED (bs ); 781 782const uint32_t kmask1 = 0x03030303 ; 783const uint32_t kmask2 = 0x0f0f0f0f ; 784 785const block_q3_K * GGML_RESTRICT x = vx ; 786const block_q8_K * GGML_RESTRICT y = vy ; 787 788const int nb = n /QK_K ; 789 790#if defined(__VXE__ )|| defined(__VXE2__ ) 791uint32_t aux [3 ]; 792uint32_t utmp [4 ]; 793 794const int32x4_t v_z = vec_splat_s32 (0 ); 795const uint8x16_t v_3m = vec_splat_u8 (0x03 ); 796 797const uint8x16_t v_0c = vec_splat_u8 (1 ); 798const uint8x16_t v_1c = vec_sl (v_0c ,1 ); 799const uint8x16_t v_2c = vec_sl (v_0c ,2 ); 800const uint8x16_t v_3c = vec_sl (v_0c ,3 ); 801 802uint8x16_t q3h [4 ]; 803uint8x16_t q3b [2 ]; 804int8x16_t q3bytes [4 ]; 805int8x16_t q8bytes [8 ]; 806uint8x16_t qhbits [2 ]; 807 808float sum = 0 ; 809 810for (int i = 0 ;i < nb ;++ i ) { 811const float d = y [i ].d * GGML_CPU_FP16_TO_FP32 (x [i ].d ); 812 813const uint8_t * restrictx0l = x [i ].qs ; 814const uint8_t * restrictx0h = x [i ].hmask ; 815const int8_t * restricty0 = y [i ].qs ; 816 817qhbits [0 ]= vec_xl (0 ,x0h ); 818qhbits [1 ]= vec_xl (16 ,x0h ); 819 820int32_t isum = 0 ; 821 822memcpy (aux ,x [i ].scales ,12 ); 823utmp [3 ]= ((aux [1 ] >>4 )& kmask2 ) | (((aux [2 ] >>6 )& kmask1 ) <<4 ); 824utmp [2 ]= ((aux [0 ] >>4 )& kmask2 ) | (((aux [2 ] >>4 )& kmask1 ) <<4 ); 825utmp [1 ]= (aux [1 ]& kmask2 ) | (((aux [2 ] >>2 )& kmask1 ) <<4 ); 826utmp [0 ]= (aux [0 ]& kmask2 ) | (((aux [2 ] >>0 )& kmask1 ) <<4 ); 827 828int8_t * scale = (int8_t * )utmp ; 829for (int j = 0 ;j < 16 ;++ j )scale [j ]-= 32 ; 830 831for (int j = 0 ;j < QK_K /128 ;++ j ) { 832int32x4_t isum0 ,isum1 ,isum2 ,isum3 ; 833 834q3b [0 ]= vec_xl (0 ,x0l ); 835q3b [1 ]= vec_xl (16 ,x0l ); 836x0l += 32 ; 837 838q8bytes [0 ]= vec_xl (0 ,y0 ); 839q8bytes [1 ]= vec_xl (16 ,y0 ); 840q8bytes [2 ]= vec_xl (32 ,y0 ); 841q8bytes [3 ]= vec_xl (48 ,y0 ); 842q8bytes [4 ]= vec_xl (64 ,y0 ); 843q8bytes [5 ]= vec_xl (80 ,y0 ); 844q8bytes [6 ]= vec_xl (96 ,y0 ); 845q8bytes [7 ]= vec_xl (112 ,y0 ); 846y0 += 128 ; 847 848q3h [0 ]= vec_sl (vec_andc (v_0c ,qhbits [0 ]),2 ); 849q3h [1 ]= vec_sl (vec_andc (v_0c ,qhbits [1 ]),2 ); 850q3h [2 ]= vec_sl (vec_andc (v_1c ,qhbits [0 ]),1 ); 851q3h [3 ]= vec_sl (vec_andc (v_1c ,qhbits [1 ]),1 ); 852 853q3bytes [0 ]= vec_sub ((int8x16_t )vec_and (q3b [0 ],v_3m ), (int8x16_t )q3h [0 ]); 854q3bytes [1 ]= vec_sub ((int8x16_t )vec_and (q3b [1 ],v_3m ), (int8x16_t )q3h [1 ]); 855q3bytes [2 ]= vec_sub ((int8x16_t )vec_and (vec_sr (q3b [0 ],2 ),v_3m ), (int8x16_t )q3h [2 ]); 856q3bytes [3 ]= vec_sub ((int8x16_t )vec_and (vec_sr (q3b [1 ],2 ),v_3m ), (int8x16_t )q3h [3 ]); 857 858isum0 = ggml_vec_dot (v_z ,q3bytes [0 ],q8bytes [0 ]); 859isum1 = ggml_vec_dot (v_z ,q3bytes [1 ],q8bytes [1 ]); 860isum2 = ggml_vec_dot (v_z ,q3bytes [2 ],q8bytes [2 ]); 861isum3 = ggml_vec_dot (v_z ,q3bytes [3 ],q8bytes [3 ]); 862 863isum += (isum0 [0 ]+ isum0 [1 ]+ isum0 [2 ]+ isum0 [3 ])* scale [0 ]; 864isum += (isum1 [0 ]+ isum1 [1 ]+ isum1 [2 ]+ isum1 [3 ])* scale [1 ]; 865isum += (isum2 [0 ]+ isum2 [1 ]+ isum2 [2 ]+ isum2 [3 ])* scale [2 ]; 866isum += (isum3 [0 ]+ isum3 [1 ]+ isum3 [2 ]+ isum3 [3 ])* scale [3 ]; 867 868scale += 4 ; 869 870q3h [0 ]= vec_andc (v_2c ,qhbits [0 ]); 871q3h [1 ]= vec_andc (v_2c ,qhbits [1 ]); 872q3h [2 ]= vec_sr (vec_andc (v_3c ,qhbits [0 ]),1 ); 873q3h [3 ]= vec_sr (vec_andc (v_3c ,qhbits [1 ]),1 ); 874 875q3bytes [0 ]= vec_sub ((int8x16_t )vec_and (vec_sr (q3b [0 ],4 ),v_3m ), (int8x16_t )q3h [0 ]); 876q3bytes [1 ]= vec_sub ((int8x16_t )vec_and (vec_sr (q3b [1 ],4 ),v_3m ), (int8x16_t )q3h [1 ]); 877q3bytes [2 ]= vec_sub ((int8x16_t )vec_and (vec_sr (q3b [0 ],6 ),v_3m ), (int8x16_t )q3h [2 ]); 878q3bytes [3 ]= vec_sub ((int8x16_t )vec_and (vec_sr (q3b [1 ],6 ),v_3m ), (int8x16_t )q3h [3 ]); 879 880isum0 = ggml_vec_dot (v_z ,q3bytes [0 ],q8bytes [4 ]); 881isum1 = ggml_vec_dot (v_z ,q3bytes [1 ],q8bytes [5 ]); 882isum2 = ggml_vec_dot (v_z ,q3bytes [2 ],q8bytes [6 ]); 883isum3 = ggml_vec_dot (v_z ,q3bytes [3 ],q8bytes [7 ]); 884 885isum += vec_hsum_i32x4 (isum0 )* scale [0 ]; 886isum += vec_hsum_i32x4 (isum1 )* scale [1 ]; 887isum += vec_hsum_i32x4 (isum2 )* scale [2 ]; 888isum += vec_hsum_i32x4 (isum3 )* scale [3 ]; 889 890scale += 4 ; 891 892if (j == 0 ) { 893qhbits [0 ]= vec_sr (qhbits [0 ],4 ); 894qhbits [1 ]= vec_sr (qhbits [1 ],4 ); 895 } 896 } 897 898sum += d * isum ; 899 } 900 901* s = sum ; 902 903#else 904UNUSED (kmask1 ); 905UNUSED (kmask2 ); 906UNUSED (x ); 907UNUSED (y ); 908UNUSED (nb ); 909ggml_vec_dot_q3_K_q8_K_generic (n ,s ,bs ,vx ,bx ,vy ,by ,nrc ); 910#endif 911} 912 913void ggml_vec_dot_q4_K_q8_K (int n ,float * GGML_RESTRICT s ,size_t bs ,const void * GGML_RESTRICT vx ,size_t bx ,const void * GGML_RESTRICT vy ,size_t by ,int nrc ) { 914assert (n %QK_K == 0 ); 915assert (nrc == 1 ); 916UNUSED (nrc ); 917UNUSED (bx ); 918UNUSED (by ); 919UNUSED (bs ); 920 921const block_q4_K * GGML_RESTRICT x = vx ; 922const block_q8_K * GGML_RESTRICT y = vy ; 923 924const int nb = n /QK_K ; 925 926static const uint32_t kmask1 = 0x3f3f3f3f ; 927static const uint32_t kmask2 = 0x0f0f0f0f ; 928static const uint32_t kmask3 = 0x03030303 ; 929 930uint32_t utmp [4 ]; 931 932#if defined(__VXE__ )|| defined(__VXE2__ ) 933const uint8x16_t v_lm = vec_splat_u8 (0x0F ); 934const int32x4_t v_z = vec_splat_s32 (0 ); 935 936uint8x16_t v_x [2 ]; 937int8x16_t v_xl [2 ]; 938int8x16_t v_y [2 ]; 939 940float sumf = 0 ; 941 942for (int i = 0 ;i < nb ;++ i ) { 943const float d = y [i ].d * GGML_CPU_FP16_TO_FP32 (x [i ].d ); 944const float dmin = y [i ].d * GGML_CPU_FP16_TO_FP32 (x [i ].dmin ); 945 946const int16x8_t v_ysumsl = vec_xl (0 ,y [i ].bsums ); 947const int16x8_t v_ysumsh = vec_xl (16 ,y [i ].bsums ); 948const int16x8_t v_ysums = vec_padd_s16 (v_ysumsl ,v_ysumsh ); 949 950memcpy (utmp ,x [i ].scales ,12 ); 951 952uint32x4_t v_mins8 = {0 }; 953v_mins8 = vec_insert (utmp [1 ]& kmask1 ,v_mins8 ,0 ); 954v_mins8 = vec_insert (((utmp [2 ] >>4 )& kmask2 ) | (((utmp [1 ] >>6 )& kmask3 ) <<4 ),v_mins8 ,1 ); 955 956utmp [1 ]= (utmp [2 ]& kmask2 ) | (((utmp [0 ] >>6 )& kmask3 ) <<4 ); 957utmp [0 ] &=kmask1 ; 958 959const int16x8_t v_minsh = (int16x8_t )vec_unpackh ((uint8x16_t )v_mins8 ); 960 961const int32x4_t v_minso = vec_mulo (v_ysums ,v_minsh ); 962const int32x4_t v_mins = vec_meadd (v_ysums ,v_minsh ,v_minso ); 963sumf -= dmin * (v_mins [0 ]+ v_mins [1 ]+ v_mins [2 ]+ v_mins [3 ]); 964 965const uint8_t * scales = (const uint8_t * )utmp ; 966const uint8_t * GGML_RESTRICT x0 = x [i ].qs ; 967const int8_t * GGML_RESTRICT y0 = y [i ].qs ; 968 969int32_t sumi1 = 0 ; 970int32_t sumi2 = 0 ; 971 972for (int j = 0 ;j < QK_K /64 ;++ j ) { 973v_x [0 ]= vec_xl (0 ,x0 ); 974v_x [1 ]= vec_xl (16 ,x0 ); 975x0 += 32 ; 976 977v_y [0 ]= vec_xl (0 ,y0 ); 978v_y [1 ]= vec_xl (16 ,y0 ); 979y0 += 32 ; 980 981v_xl [0 ]= (int8x16_t )vec_and (v_x [0 ],v_lm ); 982v_xl [1 ]= (int8x16_t )vec_and (v_x [1 ],v_lm ); 983 984const int32x4_t p1 = ggml_vec_dot (ggml_vec_dot (v_z ,v_xl [0 ],v_y [0 ]),v_xl [1 ],v_y [1 ]); 985sumi1 += vec_hsum_i32x4 (p1 )* scales [2 * j + 0 ]; 986 987v_y [0 ]= vec_xl (0 ,y0 ); 988v_y [1 ]= vec_xl (16 ,y0 ); 989y0 += 32 ; 990 991v_xl [0 ]= (int8x16_t )vec_sr (v_x [0 ],4 ); 992v_xl [1 ]= (int8x16_t )vec_sr (v_x [1 ],4 ); 993 994const int32x4_t p2 = ggml_vec_dot (ggml_vec_dot (v_z ,v_xl [0 ],v_y [0 ]),v_xl [1 ],v_y [1 ]); 995sumi2 += vec_hsum_i32x4 (p2 )* scales [2 * j + 1 ]; 996 } 997 998sumf += d * (sumi1 + sumi2 ); 999 } 1000 1001* s = sumf ; 1002 1003#else 1004UNUSED (x ); 1005UNUSED (y ); 1006UNUSED (nb ); 1007UNUSED (kmask1 ); 1008UNUSED (kmask2 ); 1009UNUSED (kmask3 ); 1010UNUSED (utmp ); 1011ggml_vec_dot_q4_K_q8_K_generic (n ,s ,bs ,vx ,bx ,vy ,by ,nrc ); 1012#endif 1013} 1014 1015void ggml_vec_dot_q5_K_q8_K (int n ,float * GGML_RESTRICT s ,size_t bs ,const void * GGML_RESTRICT vx ,size_t bx ,const void * GGML_RESTRICT vy ,size_t by ,int nrc ) { 1016assert (n %QK_K == 0 ); 1017assert (nrc == 1 ); 1018UNUSED (nrc ); 1019UNUSED (bx ); 1020UNUSED (by ); 1021UNUSED (bs ); 1022 1023const block_q5_K * GGML_RESTRICT x = vx ; 1024const block_q8_K * GGML_RESTRICT y = vy ; 1025 1026const int nb = n /QK_K ; 1027 1028static const uint32_t kmask1 = 0x3f3f3f3f ; 1029static const uint32_t kmask2 = 0x0f0f0f0f ; 1030static const uint32_t kmask3 = 0x03030303 ; 1031 1032uint32_t utmp [4 ]; 1033 1034#if defined(__VXE__ )|| defined(__VXE2__ ) 1035const uint8x16_t v_lm = vec_splat_u8 (0x0F ); 1036const uint8x16_t v_1m = vec_splat_u8 (0x01 ); 1037const uint8x16_t v_2m = vec_splat_u8 (0x02 ); 1038 1039const int32x4_t v_z = vec_splat_s32 (0 ); 1040 1041const uchar8x16_t v_minsm = { 10420x08 ,0x09 ,0x0A ,0x0B ,0x0C ,0x0D ,0x0E ,0x0F , 10430xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF 1044 }; 1045 1046int8x16_t q5b [4 ]; 1047uint8x16_t q5h [4 ]; 1048 1049uint8x16_t v_xl [2 ]; 1050uint8x16_t v_xh [2 ]; 1051int8x16_t v_y [4 ]; 1052 1053float sumf = 0 ; 1054 1055for (int i = 0 ;i < nb ;++ i ) { 1056const float d = y [i ].d * GGML_CPU_FP16_TO_FP32 (x [i ].d ); 1057const float dmin = y [i ].d * GGML_CPU_FP16_TO_FP32 (x [i ].dmin ); 1058 1059const int16x8_t v_ysumsl = vec_xl (0 ,y [i ].bsums ); 1060const int16x8_t v_ysumsh = vec_xl (16 ,y [i ].bsums ); 1061const int16x8_t v_ysums = vec_padd_s16 (v_ysumsl ,v_ysumsh ); 1062 1063memcpy (utmp ,x [i ].scales ,12 ); 1064utmp [3 ]= ((utmp [2 ] >>4 )& kmask2 ) | (((utmp [1 ] >>6 )& kmask3 ) <<4 ); 1065const uint32_t uaux = utmp [1 ]& kmask1 ; 1066utmp [1 ]= (utmp [2 ]& kmask2 ) | (((utmp [0 ] >>6 )& kmask3 ) <<4 ); 1067utmp [2 ]= uaux ; 1068utmp [0 ] &=kmask1 ; 1069 1070const uint8x16_t v_mins16 = vec_xl (0 , (const uint8_t * )utmp ); 1071const uint8x16_t v_mins8 = vec_perm (v_mins16 ,v_mins16 ,v_minsm ); 1072const int16x8_t v_minsh = (int16x8_t )vec_unpackh (v_mins8 ); 1073 1074const int32x4_t v_minsho = vec_mulo (v_ysums ,v_minsh ); 1075const int32x4_t v_mins = vec_meadd (v_ysums ,v_minsh ,v_minsho ); 1076const int32_t mins = vec_hsum_i32x4 (v_mins ); 1077 1078const uint8_t * scales = (const uint8_t * )utmp ; 1079const uint8_t * GGML_RESTRICT x0l = x [i ].qs ; 1080const uint8_t * GGML_RESTRICT x0h = x [i ].qh ; 1081const int8_t * GGML_RESTRICT y0 = y [i ].qs ; 1082 1083v_xh [0 ]= vec_xl (0 ,x0h ); 1084v_xh [1 ]= vec_xl (16 ,x0h ); 1085 1086int32_t sumi = 0 ; 1087for (int j = 0 ;j < QK_K /64 ;++ j ) { 1088v_xl [0 ]= vec_xl (0 ,x0l ); 1089v_xl [1 ]= vec_xl (16 ,x0l ); 1090x0l += 32 ; 1091 1092v_y [0 ]= vec_xl (0 ,y0 ); 1093v_y [1 ]= vec_xl (16 ,y0 ); 1094v_y [2 ]= vec_xl (32 ,y0 ); 1095v_y [3 ]= vec_xl (48 ,y0 ); 1096y0 += 64 ; 1097 1098q5h [0 ]= vec_sl (vec_and (v_1m ,v_xh [0 ]),4 ); 1099q5h [1 ]= vec_sl (vec_and (v_1m ,v_xh [1 ]),4 ); 1100q5h [2 ]= vec_sl (vec_and (v_2m ,v_xh [0 ]),3 ); 1101q5h [3 ]= vec_sl (vec_and (v_2m ,v_xh [1 ]),3 ); 1102v_xh [0 ]= vec_sr (v_xh [0 ],2 ); 1103v_xh [1 ]= vec_sr (v_xh [1 ],2 ); 1104 1105q5b [0 ]= (int8x16_t )vec_or (vec_and (v_xl [0 ],v_lm ),q5h [0 ]); 1106q5b [1 ]= (int8x16_t )vec_or (vec_and (v_xl [1 ],v_lm ),q5h [1 ]); 1107q5b [2 ]= (int8x16_t )vec_or (vec_sr (v_xl [0 ],4 ),q5h [2 ]); 1108q5b [3 ]= (int8x16_t )vec_or (vec_sr (v_xl [1 ],4 ),q5h [3 ]); 1109 1110int32x4_t sumi0 = ggml_vec_dot (ggml_vec_dot (v_z ,q5b [0 ],v_y [0 ]),q5b [1 ],v_y [1 ]); 1111int32x4_t sumi1 = ggml_vec_dot (ggml_vec_dot (v_z ,q5b [2 ],v_y [2 ]),q5b [3 ],v_y [3 ]); 1112 1113sumi += vec_hsum_i32x4 (sumi0 )* * scales ++ ; 1114sumi += vec_hsum_i32x4 (sumi1 )* * scales ++ ; 1115 } 1116 1117sumf += d * sumi - dmin * mins ; 1118 } 1119 1120* s = sumf ; 1121 1122#else 1123UNUSED (x ); 1124UNUSED (y ); 1125UNUSED (nb ); 1126UNUSED (kmask1 ); 1127UNUSED (kmask2 ); 1128UNUSED (kmask3 ); 1129UNUSED (utmp ); 1130ggml_vec_dot_q5_K_q8_K_generic (n ,s ,bs ,vx ,bx ,vy ,by ,nrc ); 1131#endif 1132} 1133 1134void ggml_vec_dot_q6_K_q8_K (int n ,float * GGML_RESTRICT s ,size_t bs ,const void * GGML_RESTRICT vx ,size_t bx ,const void * GGML_RESTRICT vy ,size_t by ,int nrc ) { 1135assert (n %QK_K == 0 ); 1136assert (nrc == 1 ); 1137UNUSED (nrc ); 1138UNUSED (bx ); 1139UNUSED (by ); 1140UNUSED (bs ); 1141 1142const block_q6_K * GGML_RESTRICT x = vx ; 1143const block_q8_K * GGML_RESTRICT y = vy ; 1144 1145const int nb = n /QK_K ; 1146 1147#if defined(__VXE__ )|| defined(__VXE2__ ) 1148float sum = 0 ; 1149 1150// Lower 4-bit and upper 2-bit masks 1151const uint8x16_t v_lm = vec_splat_u8 (0x0F ); 1152const uint8x16_t v_um = vec_splat_u8 (0x03 ); 1153 1154const int32x4_t v_z = vec_splat_s32 (0 ); 1155 1156int8x16_t q6b [4 ]; 1157uint8x16_t q6h [4 ]; 1158 1159uint8x16_t v_xl [4 ]; 1160uint8x16_t v_xh [2 ]; 1161int8x16_t v_y [4 ]; 1162 1163for (int i = 0 ;i < nb ;++ i ) { 1164const float d_all = GGML_CPU_FP16_TO_FP32 (x [i ].d ); 1165 1166const uint8_t * GGML_RESTRICT x0l = x [i ].ql ; 1167const uint8_t * GGML_RESTRICT x0h = x [i ].qh ; 1168const int8_t * GGML_RESTRICT y0 = y [i ].qs ; 1169 1170const int8_t * GGML_RESTRICT scale = x [i ].scales ; 1171 1172const int16x8_t v_ysumsl = vec_xl (0 ,y [i ].bsums ); 1173const int16x8_t v_ysumsh = vec_xl (16 ,y [i ].bsums ); 1174 1175const int8x16_t v_scale = vec_xl (0 ,scale ); 1176const int16x8_t v_scalel = vec_unpackh (v_scale ); 1177const int16x8_t v_scaleh = vec_unpackl (v_scale ); 1178 1179const int32x4_t v_minslo = vec_mulo (v_ysumsl ,v_scalel ); 1180const int32x4_t v_minsl = vec_meadd (v_ysumsl ,v_scalel ,v_minslo ); 1181const int32x4_t v_minsho = vec_mulo (v_ysumsh ,v_scaleh ); 1182const int32x4_t v_minsh = vec_meadd (v_ysumsh ,v_scaleh ,v_minsho ); 1183const int32x4_t v_mins = vec_add (v_minsl ,v_minsh ); 1184 1185const int32_t mins = vec_hsum_i32x4 (v_mins ); 1186 1187int32_t isum = 0 ; 1188for (int j = 0 ;j < QK_K /128 ;++ j ) { 1189// Load model upper 2 bits 1190v_xh [0 ]= vec_xl (0 ,x0h ); 1191v_xh [1 ]= vec_xl (16 ,x0h ); 1192x0h += 32 ; 1193 1194// Load model lower 4 bits 1195v_xl [0 ]= vec_xl (0 ,x0l ); 1196v_xl [1 ]= vec_xl (16 ,x0l ); 1197v_xl [2 ]= vec_xl (32 ,x0l ); 1198v_xl [3 ]= vec_xl (48 ,x0l ); 1199x0l += 64 ; 1200 1201// Load activation quants 1202v_y [0 ]= vec_xl (0 ,y0 ); 1203v_y [1 ]= vec_xl (16 ,y0 ); 1204v_y [2 ]= vec_xl (32 ,y0 ); 1205v_y [3 ]= vec_xl (48 ,y0 ); 1206y0 += 64 ; 1207 1208q6h [0 ]= vec_sl (vec_and (v_um ,v_xh [0 ]),4 ); 1209q6h [1 ]= vec_sl (vec_and (v_um ,v_xh [1 ]),4 ); 1210uint8x16_t shifted = vec_sr (v_xh [0 ],2 ); 1211q6h [2 ]= vec_sl (vec_and (v_um ,shifted ),4 ); 1212shifted = vec_sr (v_xh [1 ],2 ); 1213q6h [3 ]= vec_sl (vec_and (v_um ,shifted ),4 ); 1214 1215q6b [0 ]= (int8x16_t )(vec_or (vec_and (v_xl [0 ],v_lm ),q6h [0 ])); 1216q6b [1 ]= (int8x16_t )(vec_or (vec_and (v_xl [1 ],v_lm ),q6h [1 ])); 1217q6b [2 ]= (int8x16_t )(vec_or (vec_and (v_xl [2 ],v_lm ),q6h [2 ])); 1218q6b [3 ]= (int8x16_t )(vec_or (vec_and (v_xl [3 ],v_lm ),q6h [3 ])); 1219 1220int32x4_t summs0 = ggml_vec_dot (v_z ,q6b [0 ],v_y [0 ]); 1221int32x4_t summs1 = ggml_vec_dot (v_z ,q6b [1 ],v_y [1 ]); 1222int32x4_t summs2 = ggml_vec_dot (v_z ,q6b [2 ],v_y [2 ]); 1223int32x4_t summs3 = ggml_vec_dot (v_z ,q6b [3 ],v_y [3 ]); 1224 1225isum += vec_hsum_i32x4 (summs0 )* scale [0 ]+ 1226vec_hsum_i32x4 (summs1 )* scale [1 ]+ 1227vec_hsum_i32x4 (summs2 )* scale [2 ]+ 1228vec_hsum_i32x4 (summs3 )* scale [3 ]; 1229 1230scale += 4 ; 1231 1232 1233// Load activation quants 1234v_y [0 ]= vec_xl (0 ,y0 ); 1235v_y [1 ]= vec_xl (16 ,y0 ); 1236v_y [2 ]= vec_xl (32 ,y0 ); 1237v_y [3 ]= vec_xl (48 ,y0 ); 1238y0 += 64 ; 1239 1240shifted = vec_sr (v_xh [0 ],4 ); 1241q6h [0 ]= vec_sl (vec_and (v_um ,shifted ),4 ); 1242shifted = vec_sr (v_xh [1 ],4 ); 1243q6h [1 ]= vec_sl (vec_and (v_um ,shifted ),4 ); 1244shifted = vec_sr (v_xh [0 ],6 ); 1245q6h [2 ]= vec_sl (vec_and (v_um ,shifted ),4 ); 1246shifted = vec_sr (v_xh [1 ],6 ); 1247q6h [3 ]= vec_sl (vec_and (v_um ,shifted ),4 ); 1248 1249q6b [0 ]= (int8x16_t )(vec_or (vec_sr (v_xl [0 ],4 ),q6h [0 ])); 1250q6b [1 ]= (int8x16_t )(vec_or (vec_sr (v_xl [1 ],4 ),q6h [1 ])); 1251q6b [2 ]= (int8x16_t )(vec_or (vec_sr (v_xl [2 ],4 ),q6h [2 ])); 1252q6b [3 ]= (int8x16_t )(vec_or (vec_sr (v_xl [3 ],4 ),q6h [3 ])); 1253 1254summs0 = ggml_vec_dot (v_z ,q6b [0 ],v_y [0 ]); 1255summs1 = ggml_vec_dot (v_z ,q6b [1 ],v_y [1 ]); 1256summs2 = ggml_vec_dot (v_z ,q6b [2 ],v_y [2 ]); 1257summs3 = ggml_vec_dot (v_z ,q6b [3 ],v_y [3 ]); 1258 1259isum += vec_hsum_i32x4 (summs0 )* scale [0 ]+ 1260vec_hsum_i32x4 (summs1 )* scale [1 ]+ 1261vec_hsum_i32x4 (summs2 )* scale [2 ]+ 1262vec_hsum_i32x4 (summs3 )* scale [3 ]; 1263 1264scale += 4 ; 1265 } 1266 1267sum += d_all * y [i ].d * (isum - 32 * mins ); 1268 } 1269 1270* s = sum ; 1271 1272#else 1273UNUSED (x ); 1274UNUSED (y ); 1275UNUSED (nb ); 1276ggml_vec_dot_q6_K_q8_K_generic (n ,s ,bs ,vx ,bx ,vy ,by ,nrc ); 1277#endif 1278} 1279 1280// #if defined(__VXE__) || defined(__VXE2__) 1281// static const int8_t keven_signs_q2xs[1024] = { 1282// 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1283// 1, 1, -1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, -1, 1284// 1, 1, 1, -1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, -1, 1285// 1, 1, -1, -1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1286// 1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, 1, -1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, -1, 1287// 1, 1, -1, 1, -1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, 1, 1288// 1, 1, 1, -1, -1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, 1, 1289// 1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, -1, 1290// 1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, 1, -1, 1, 1, 1, -1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, 1, -1, 1, -1, 1291// 1, 1, -1, 1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, 1, 1292// 1, 1, 1, -1, 1, -1, 1, 1, -1, 1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, 1, 1293// 1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, -1, 1294// 1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, -1, -1, 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, 1, 1295// 1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, -1, 1296// 1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, -1, 1297// 1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1298// 1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, 1, -1, -1, 1299// 1, 1, -1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, 1, 1300// 1, 1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, 1, -1, -1, 1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, 1, 1301// 1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, -1, 1302// 1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, 1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, 1, 1303// 1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, -1, 1304// 1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, -1, 1305// 1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, -1, 1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, 1, 1306// 1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, -1, 1, 1307// 1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, -1, 1308// 1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, -1, 1309// 1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, 1, 1310// 1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, -1, 1311// 1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, -1, 1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, 1, 1312// 1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, 1, 1313// 1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, 1, 1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, 1314// }; 1315// #endif 1316 1317// void ggml_vec_dot_iq2_xxs_q8_K(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) { 1318// assert(n % QK_K == 0); 1319// assert(nrc == 1); 1320// UNUSED(nrc); 1321// UNUSED(bx); 1322// UNUSED(by); 1323// UNUSED(bs); 1324 1325// const block_iq2_xxs * GGML_RESTRICT x = vx; 1326// const block_q8_K * GGML_RESTRICT y = vy; 1327 1328// const int nb = n / QK_K; 1329 1330// #if defined(__VXE__) || defined(__VXE2__) 1331// const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs; 1332 1333// uint32_t aux32[4]; 1334// const uint8_t * aux8 = (const uint8_t *)aux32; 1335 1336// float sumf = 0; 1337 1338// for (int i = 0; i < nb; ++i) { 1339// const float d = GGML_CPU_FP16_TO_FP32(x[i].d) * y[i].d; 1340// const uint16_t * GGML_RESTRICT q2 = x[i].qs; 1341// const int8_t * GGML_RESTRICT q8 = y[i].qs; 1342 1343// float sumf1 = 0, sumf2 = 0; 1344 1345// for (int ib32 = 0; ib32 < QK_K/32; ib += 2) { 1346// int8x16_t q8b0 = vec_xl( 0, q8); 1347// int8x16_t qb81 = vec_xl(16, q8); 1348// int8x16_t q8b2 = vec_xl(32, q8); 1349// int8x16_t q8b3 = vec_xl(48, q8); 1350// q8 += 64; 1351 1352// memcpy(aux32, q2, 4 * sizeof(uint32_t)); 1353// q2 += 8; 1354 1355// int8x16_t q2u0 = { *(const int64_t *)(iq2xxs_grid + aux8[ 0]), *(const int64_t *)(iq2xxs_grid + aux8[ 1]) }; 1356// int8x16_t q2u1 = { *(const int64_t *)(iq2xxs_grid + aux8[ 2]), *(const int64_t *)(iq2xxs_grid + aux8[ 3]) }; 1357// int8x16_t q2u2 = { *(const int64_t *)(iq2xxs_grid + aux8[ 8]), *(const int64_t *)(iq2xxs_grid + aux8[ 9]) }; 1358// int8x16_t q2u3 = { *(const int64_t *)(iq2xxs_grid + aux8[10]), *(const int64_t *)(iq2xxs_grid + aux8[11]) }; 1359 1360// int8x16_t q2s0 = { *(const int64_t *)(signs64 + ((aux32[1] >> 0) & 127)), *(const int64_t *)(signs64 + ((aux32[1] >> 7) & 127)) }; 1361// int8x16_t q2s1 = { *(const int64_t *)(signs64 + ((aux32[1] >> 14) & 127)), *(const int64_t *)(signs64 + ((aux32[1] >> 21) & 127)) }; 1362// int8x16_t q2s2 = { *(const int64_t *)(signs64 + ((aux32[3] >> 0) & 127)), *(const int64_t *)(signs64 + ((aux32[3] >> 7) & 127)) }; 1363// int8x16_t q2s3 = { *(const int64_t *)(signs64 + ((aux32[3] >> 14) & 127)), *(const int64_t *)(signs64 + ((aux32[3] >> 21) & 127)) }; 1364 1365// q2u0 = vec_mul(q2u0, q2s0); 1366// q2u1 = vec_mul(q2u1, q2s1); 1367// q2u2 = vec_mul(q2u2, q2s2); 1368// q2u3 = vec_mul(q2u3, q2s3); 1369 1370// const int32x4_t p1 = ggml_vec_dot(ggml_vec_dot(vec_splat_s32(0), q2u0, q8b0), q2u1, q8b1); 1371// const int32x4_t p2 = ggml_vec_dot(ggml_vec_dot(vec_splat_s32(0), q2u2, q8b2), q2u3, q8b3); 1372 1373// sumf1 += (p1[0] + p1[1] + p1[2] + p1[3]) * (0.5f + (aux32[1] >> 28)); 1374// sumf2 += (p2[0] + p2[1] + p2[2] + p2[3]) * (0.5f + (aux32[3] >> 28)); 1375// } 1376 1377// sumf += d * (sumf1 + sumf2); 1378// } 1379 1380// *s = 0.25f * sumf; 1381 1382// #else 1383 1384// uint32_t aux32[2]; 1385// const uint8_t * aux8 = (const uint8_t *)aux32; 1386 1387// float sumf = 0.f; 1388// for (int i = 0; i < nb; ++i) { 1389// const float d = GGML_CPU_FP16_TO_FP32(x[i].d) * y[i].d; 1390// const uint16_t * GGML_RESTRICT q2 = x[i].qs; 1391// const int8_t * GGML_RESTRICT q8 = y[i].qs; 1392// int32_t bsum = 0; 1393// for (int ib32 = 0; ib32 < QK_K/32; ++ib32) { 1394// memcpy(aux32, q2, 2*sizeof(uint32_t)); 1395// q2 += 4; 1396// const uint32_t ls = 2*(aux32[1] >> 28) + 1; 1397// int32_t sumi = 0; 1398// for (int l = 0; l < 4; ++l) { 1399// const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]); 1400// const uint8_t signs = ksigns_iq2xs[(aux32[1] >> 7*l) & 127]; 1401// for (int j = 0; j < 8; ++j) { 1402// sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1); 1403// } 1404// q8 += 8; 1405// } 1406// bsum += sumi * ls; 1407// } 1408// sumf += d * bsum; 1409// } 1410// *s = 0.125f * sumf; 1411// #endif 1412// } 1413 1414void ggml_vec_dot_iq4_nl_q8_0 (int n ,float * GGML_RESTRICT s ,size_t bs ,const void * GGML_RESTRICT vx ,size_t bx ,const void * GGML_RESTRICT vy ,size_t by ,int nrc ) { 1415assert (nrc == 1 ); 1416UNUSED (nrc ); 1417UNUSED (bx ); 1418UNUSED (by ); 1419UNUSED (bs ); 1420assert (n %QK4_NL == 0 ); 1421static_assert (QK4_NL == QK8_0 ,"QK4_NL and QK8_0 must be the same" ); 1422 1423const block_iq4_nl * GGML_RESTRICT x = vx ; 1424const block_q8_0 * GGML_RESTRICT y = vy ; 1425 1426const int nb = n /QK4_NL ; 1427 1428int ib = 0 ; 1429float sumf = 0 ; 1430 1431#if defined(__VXE__ )|| defined(__VXE2__ ) 1432const int8x16_t v_k = vec_xl (0 ,kvalues_iq4nl ); 1433const uint8x16_t v_m = vec_splat_u8 (0x0F ); 1434 1435for (;ib < nb ;++ ib ) { 1436const block_iq4_nl * GGML_RESTRICT x0 = & x [ib ]; 1437const block_q8_0 * GGML_RESTRICT y0 = & y [ib ]; 1438 1439const uint8x16_t v_x = vec_xl (0 ,x0 -> qs ); 1440int8x16_t v_xl = (int8x16_t )vec_and (v_x ,v_m ); 1441int8x16_t v_xh = (int8x16_t )vec_sr (v_x ,4 ); 1442 1443v_xl = vec_perm (v_k ,v_k , (uchar8x16_t )v_xl ); 1444v_xh = vec_perm (v_k ,v_k , (uchar8x16_t )v_xh ); 1445 1446const int8x16_t v_yl = vec_xl (0 ,y0 -> qs ); 1447const int8x16_t v_yh = vec_xl (QK8_0 /2 ,y0 -> qs ); 1448const int32x4_t v_xy = ggml_vec_dot (ggml_vec_dot (vec_splats (0 ),v_xl ,v_yl ),v_xh ,v_yh ); 1449 1450sumf += GGML_CPU_FP16_TO_FP32 (x0 -> d )* GGML_CPU_FP16_TO_FP32 (y0 -> d )* vec_hsum_i32x4 (v_xy ); 1451 } 1452 1453* s = sumf ; 1454#else 1455UNUSED (x ); 1456UNUSED (y ); 1457UNUSED (nb ); 1458UNUSED (ib ); 1459UNUSED (sumf ); 1460ggml_vec_dot_iq4_nl_q8_0_generic (n ,s ,bs ,vx ,bx ,vy ,by ,nrc ); 1461#endif 1462} 1463 1464void ggml_vec_dot_iq4_xs_q8_K (int n ,float * GGML_RESTRICT s ,size_t bs ,const void * GGML_RESTRICT vx ,size_t bx ,const void * GGML_RESTRICT vy ,size_t by ,int nrc ) { 1465assert (nrc == 1 ); 1466UNUSED (nrc ); 1467UNUSED (bx ); 1468UNUSED (by ); 1469UNUSED (bs ); 1470assert (n %QK_K == 0 ); 1471 1472const block_iq4_xs * GGML_RESTRICT x = vx ; 1473const block_q8_K * GGML_RESTRICT y = vy ; 1474 1475const int nb = n /QK_K ; 1476 1477#if defined(__VXE__ )|| defined(__VXE2__ ) 1478const int8x16_t v_k = vec_xl (0 ,kvalues_iq4nl ); 1479const uint8x16_t v_m = vec_splat_u8 (0x0F ); 1480 1481float sumf = 0 ; 1482 1483for (int ibl = 0 ;ibl < nb ;++ ibl ) { 1484const uint8_t * GGML_RESTRICT q4 = x [ibl ].qs ; 1485const int8_t * GGML_RESTRICT q8 = y [ibl ].qs ; 1486 1487uint16_t h = x [ibl ].scales_h ; 1488 1489int sumi1 = 0 ,sumi2 = 0 ; 1490for (int ib = 0 ;ib < QK_K /64 ;++ ib ) { 1491const uint8x16_t v_x0 = vec_xl (0 ,q4 ); 1492const uint8x16_t v_x1 = vec_xl (QK4_NL /2 ,q4 ); 1493q4 += 32 ; 1494 1495int8x16_t v_x0l = (int8x16_t )vec_and (v_x0 ,v_m ); 1496int8x16_t v_x0h = (int8x16_t )vec_sr (v_x0 ,4 ); 1497int8x16_t v_x1l = (int8x16_t )vec_and (v_x1 ,v_m ); 1498int8x16_t v_x1h = (int8x16_t )vec_sr (v_x1 ,4 ); 1499 1500v_x0l = vec_perm (v_k ,v_k , (uchar8x16_t )v_x0l ); 1501v_x0h = vec_perm (v_k ,v_k , (uchar8x16_t )v_x0h ); 1502v_x1l = vec_perm (v_k ,v_k , (uchar8x16_t )v_x1l ); 1503v_x1h = vec_perm (v_k ,v_k , (uchar8x16_t )v_x1h ); 1504 1505const int8x16_t v_y0 = vec_xl (0 ,q8 ); 1506const int8x16_t v_y1 = vec_xl (16 ,q8 ); 1507const int8x16_t v_y2 = vec_xl (32 ,q8 ); 1508const int8x16_t v_y3 = vec_xl (48 ,q8 ); 1509q8 += 64 ; 1510 1511int32x4_t vsumi0 = ggml_vec_dot (ggml_vec_dot (vec_splats (0 ),v_x0l ,v_y0 ),v_x0h ,v_y1 ); 1512int32x4_t vsumi1 = ggml_vec_dot (ggml_vec_dot (vec_splats (0 ),v_x1l ,v_y2 ),v_x1h ,v_y3 ); 1513 1514int ls1 = ((x [ibl ].scales_l [ib ]& 0xF ) | ((h <<4 )& 0x30 ))- 32 ; 1515int ls2 = ((x [ibl ].scales_l [ib ] >>4 ) | ((h <<2 )& 0x30 ))- 32 ; 1516 1517h >>=4 ; 1518 1519sumi1 += vec_hsum_i32x4 (vsumi0 )* ls1 ; 1520sumi2 += vec_hsum_i32x4 (vsumi1 )* ls2 ; 1521 } 1522 1523sumf += GGML_CPU_FP16_TO_FP32 (x [ibl ].d )* y [ibl ].d * (sumi1 + sumi2 ); 1524 } 1525 1526* s = sumf ; 1527 1528#else 1529UNUSED (x ); 1530UNUSED (y ); 1531UNUSED (nb ); 1532ggml_vec_dot_iq4_xs_q8_K_generic (n ,s ,bs ,vx ,bx ,vy ,by ,nrc ); 1533#endif 1534}