char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
b615f5b4b
char/aliceai-t5-moe
1#pragma once 2 3#include "common.h" 4 5void quantize_q1_0 (device const float * src ,device block_q1_0 & dst ) { 6float sum_abs = 0.0f ; 7for (int j = 0 ;j < QK1_0 ;j ++ ) { 8sum_abs += fabs (src [j ]); 9 } 10dst .d = sum_abs /QK1_0 ; 11 12for (int j = 0 ;j < QK1_0 /8 ;j ++ ) { 13dst .qs [j ]= 0 ; 14 } 15for (int j = 0 ;j < QK1_0 ;j ++ ) { 16if (src [j ] >=0.0f ) { 17dst .qs [j /8 ] |= (1 << (j %8 )); 18 } 19 } 20} 21 22void quantize_q2_0 (device const float * src ,device block_q2_0 & dst ) { 23float amax = 0.0f ; 24for (int j = 0 ;j < QK2_0 ;j ++ ) { 25float a = fabs (src [j ]); 26if (a > amax )amax = a ; 27 } 28const float d = amax ; 29dst .d = d ; 30 31const float id = d > 0.0f ?1.0f /d :0.0f ; 32 33for (int j = 0 ;j < QK2_0 /4 ;j ++ ) { 34dst .qs [j ]= 0 ; 35 } 36for (int j = 0 ;j < QK2_0 ;j ++ ) { 37int q = (int )round (src [j ]* id )+ 1 ; 38q = max (0 ,min (3 ,q )); 39dst .qs [j /4 ] |= (q << (2 * (j %4 ))); 40 } 41} 42 43void quantize_q4_0 (device const float * src ,device block_q4_0 & dst ) { 44#pragma METAL fp math_mode(safe) 45float amax = 0.0f ;// absolute max 46float max = 0.0f ; 47 48for (int j = 0 ;j < QK4_0 ;j ++ ) { 49const float v = src [j ]; 50if (amax < fabs (v )) { 51amax = fabs (v ); 52max = v ; 53 } 54 } 55 56const float d = max /-8 ; 57const float id = d ?1.0f /d :0.0f ; 58 59dst .d = d ; 60 61for (int j = 0 ;j < QK4_0 /2 ;++ j ) { 62const float x0 = src [0 + j ]* id ; 63const float x1 = src [QK4_0 /2 + j ]* id ; 64 65const uint8_t xi0 = MIN (15 , (int8_t )(x0 + 8.5f )); 66const uint8_t xi1 = MIN (15 , (int8_t )(x1 + 8.5f )); 67 68dst .qs [j ]= xi0 ; 69dst .qs [j ] |=xi1 <<4 ; 70 } 71} 72 73void quantize_q4_1 (device const float * src ,device block_q4_1 & dst ) { 74#pragma METAL fp math_mode(safe) 75float min = FLT_MAX ; 76float max = - FLT_MAX ; 77 78for (int j = 0 ;j < QK4_1 ;j ++ ) { 79const float v = src [j ]; 80if (min > v )min = v ; 81if (max < v )max = v ; 82 } 83 84const float d = (max - min ) / ((1 <<4 )- 1 ); 85const float id = d ?1.0f /d :0.0f ; 86 87dst .d = d ; 88dst .m = min ; 89 90for (int j = 0 ;j < QK4_1 /2 ;++ j ) { 91const float x0 = (src [0 + j ]- min )* id ; 92const float x1 = (src [QK4_1 /2 + j ]- min )* id ; 93 94const uint8_t xi0 = MIN (15 , (int8_t )(x0 + 0.5f )); 95const uint8_t xi1 = MIN (15 , (int8_t )(x1 + 0.5f )); 96 97dst .qs [j ]= xi0 ; 98dst .qs [j ] |=xi1 <<4 ; 99 } 100} 101 102void quantize_q5_0 (device const float * src ,device block_q5_0 & dst ) { 103#pragma METAL fp math_mode(safe) 104float amax = 0.0f ;// absolute max 105float max = 0.0f ; 106 107for (int j = 0 ;j < QK5_0 ;j ++ ) { 108const float v = src [j ]; 109if (amax < fabs (v )) { 110amax = fabs (v ); 111max = v ; 112 } 113 } 114 115const float d = max /-16 ; 116const float id = d ?1.0f /d :0.0f ; 117 118dst .d = d ; 119 120uint32_t qh = 0 ; 121for (int j = 0 ;j < QK5_0 /2 ;++ j ) { 122const float x0 = src [0 + j ]* id ; 123const float x1 = src [QK5_0 /2 + j ]* id ; 124 125const uint8_t xi0 = MIN (31 , (int8_t )(x0 + 16.5f )); 126const uint8_t xi1 = MIN (31 , (int8_t )(x1 + 16.5f )); 127 128dst .qs [j ]= (xi0 & 0xf ) | ((xi1 & 0xf ) <<4 ); 129qh |= ((xi0 & 0x10u ) >>4 ) << (j + 0 ); 130qh |= ((xi1 & 0x10u ) >>4 ) << (j + QK5_0 /2 ); 131 } 132 133thread const uint8_t * qh8 = (thread const uint8_t * )& qh ; 134 135for (int j = 0 ;j < 4 ;++ j ) { 136dst .qh [j ]= qh8 [j ]; 137 } 138} 139 140void quantize_q5_1 (device const float * src ,device block_q5_1 & dst ) { 141#pragma METAL fp math_mode(safe) 142float max = src [0 ]; 143float min = src [0 ]; 144 145for (int j = 1 ;j < QK5_1 ;j ++ ) { 146const float v = src [j ]; 147min = v < min ?v :min ; 148max = v > max ?v :max ; 149 } 150 151const float d = (max - min ) /31 ; 152const float id = d ?1.0f /d :0.0f ; 153 154dst .d = d ; 155dst .m = min ; 156 157uint32_t qh = 0 ; 158for (int j = 0 ;j < QK5_1 /2 ;++ j ) { 159const float x0 = (src [0 + j ]- min )* id ; 160const float x1 = (src [QK5_1 /2 + j ]- min )* id ; 161 162const uint8_t xi0 = (uint8_t )(x0 + 0.5f ); 163const uint8_t xi1 = (uint8_t )(x1 + 0.5f ); 164 165dst .qs [j ]= (xi0 & 0xf ) | ((xi1 & 0xf ) <<4 ); 166qh |= ((xi0 & 0x10u ) >>4 ) << (j + 0 ); 167qh |= ((xi1 & 0x10u ) >>4 ) << (j + QK5_1 /2 ); 168 } 169 170thread const uint8_t * qh8 = (thread const uint8_t * )& qh ; 171 172for (int j = 0 ;j < 4 ;++ j ) { 173dst .qh [j ]= qh8 [j ]; 174 } 175} 176 177void quantize_q8_0 (device const float * src ,device block_q8_0 & dst ) { 178#pragma METAL fp math_mode(safe) 179float amax = 0.0f ;// absolute max 180 181for (int j = 0 ;j < QK8_0 ;j ++ ) { 182const float v = src [j ]; 183amax = MAX (amax ,fabs (v )); 184 } 185 186const float d = amax / ((1 <<7 )- 1 ); 187const float id = d ?1.0f /d :0.0f ; 188 189dst .d = d ; 190 191for (int j = 0 ;j < QK8_0 ;++ j ) { 192const float x0 = src [j ]* id ; 193 194dst .qs [j ]= round (x0 ); 195 } 196} 197 198void quantize_iq4_nl (device const float * src ,device block_iq4_nl & dst ) { 199#pragma METAL fp math_mode(safe) 200float amax = 0.0f ;// absolute max 201float max = 0.0f ; 202 203for (int j = 0 ;j < QK4_NL ;j ++ ) { 204const float v = src [j ]; 205if (amax < fabs (v )) { 206amax = fabs (v ); 207max = v ; 208 } 209 } 210 211const float d = max /kvalues_iq4nl_f [0 ]; 212const float id = d ?1.0f /d :0.0f ; 213 214float sumqx = 0 ,sumq2 = 0 ; 215for (int j = 0 ;j < QK4_NL /2 ;++ j ) { 216const float x0 = src [0 + j ]* id ; 217const float x1 = src [QK4_NL /2 + j ]* id ; 218 219const uint8_t xi0 = best_index_int8 (16 ,kvalues_iq4nl_f ,x0 ); 220const uint8_t xi1 = best_index_int8 (16 ,kvalues_iq4nl_f ,x1 ); 221 222dst .qs [j ]= xi0 | (xi1 <<4 ); 223 224const float v0 = kvalues_iq4nl_f [xi0 ]; 225const float v1 = kvalues_iq4nl_f [xi1 ]; 226const float w0 = src [0 + j ]* src [0 + j ]; 227const float w1 = src [QK4_NL /2 + j ]* src [QK4_NL /2 + j ]; 228sumqx += w0 * v0 * src [j ]+ w1 * v1 * src [QK4_NL /2 + j ]; 229sumq2 += w0 * v0 * v0 + w1 * v1 * v1 ; 230 231 } 232 233dst .d = sumq2 > 0 ?sumqx /sumq2 :d ; 234} 235 236void quantize_tq2_0 (device const float * src ,device block_tq2_0 & dst ) { 237#pragma METAL fp math_mode(safe) 238float amax = 0.0f ;// absolute max 239 240for (int j = 0 ;j < QK_K ;j ++ ) { 241const float v = src [j ]; 242amax = MAX (amax ,fabs (v )); 243 } 244 245const float d = amax ; 246const float id = d ?1.0f /d :0.0f ; 247 248dst .d = (half )d ; 249 250for (int j = 0 ;j < QK_K /4 ;j += 32 ) { 251for (int m = 0 ;m < 32 ;++ m ) { 252uint8_t q = 0 ; 253for (int n = 0 ;n < 4 ;++ n ) { 254// -1, 0, 1 -> 0, 1, 2 255int xi = (int )round (src [m + n * 32 ]* id )+ 1 ; 256q += (uint8_t )((xi & 3 ) << (2 * n )); 257 } 258dst .qs [j + m ]= q ; 259 } 260src += 4 * 32 ; 261 } 262}