ggml-metal.metal 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163
  1. #include <metal_stdlib>
  2. using namespace metal;
  3. #define MAX(x, y) ((x) > (y) ? (x) : (y))
  4. #define QK4_0 32
  5. #define QR4_0 2
  6. typedef struct {
  7. half d; // delta
  8. uint8_t qs[QK4_0 / 2]; // nibbles / quants
  9. } block_q4_0;
  10. #define QK4_1 32
  11. typedef struct {
  12. half d; // delta
  13. half m; // min
  14. uint8_t qs[QK4_1 / 2]; // nibbles / quants
  15. } block_q4_1;
  16. #define QK8_0 32
  17. typedef struct {
  18. half d; // delta
  19. int8_t qs[QK8_0]; // quants
  20. } block_q8_0;
  21. kernel void kernel_add(
  22. device const float4 * src0,
  23. device const float4 * src1,
  24. device float4 * dst,
  25. uint tpig[[thread_position_in_grid]]) {
  26. dst[tpig] = src0[tpig] + src1[tpig];
  27. }
  28. // assumption: src1 is a row
  29. // broadcast src1 into src0
  30. kernel void kernel_add_row(
  31. device const float4 * src0,
  32. device const float4 * src1,
  33. device float4 * dst,
  34. constant int64_t & nb,
  35. uint tpig[[thread_position_in_grid]]) {
  36. dst[tpig] = src0[tpig] + src1[tpig % nb];
  37. }
  38. kernel void kernel_mul(
  39. device const float4 * src0,
  40. device const float4 * src1,
  41. device float4 * dst,
  42. uint tpig[[thread_position_in_grid]]) {
  43. dst[tpig] = src0[tpig] * src1[tpig];
  44. }
  45. // assumption: src1 is a row
  46. // broadcast src1 into src0
  47. kernel void kernel_mul_row(
  48. device const float4 * src0,
  49. device const float4 * src1,
  50. device float4 * dst,
  51. constant int64_t & nb,
  52. uint tpig[[thread_position_in_grid]]) {
  53. dst[tpig] = src0[tpig] * src1[tpig % nb];
  54. }
  55. kernel void kernel_scale(
  56. device const float * src0,
  57. device float * dst,
  58. constant float & scale,
  59. uint tpig[[thread_position_in_grid]]) {
  60. dst[tpig] = src0[tpig] * scale;
  61. }
  62. kernel void kernel_silu(
  63. device const float * src0,
  64. device float * dst,
  65. uint tpig[[thread_position_in_grid]]) {
  66. float x = src0[tpig];
  67. dst[tpig] = x / (1.0f + exp(-x));
  68. }
  69. kernel void kernel_relu(
  70. device const float * src0,
  71. device float * dst,
  72. uint tpig[[thread_position_in_grid]]) {
  73. dst[tpig] = max(0.0f, src0[tpig]);
  74. }
  75. constant float GELU_COEF_A = 0.044715f;
  76. constant float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  77. kernel void kernel_gelu(
  78. device const float * src0,
  79. device float * dst,
  80. uint tpig[[thread_position_in_grid]]) {
  81. float x = src0[tpig];
  82. // BEWARE !!!
  83. // Simply using "tanh" instead of "precise::tanh" will sometimes results in NaNs!
  84. // This was observed with Falcon 7B and 40B models
  85. //
  86. dst[tpig] = 0.5f*x*(1.0f + precise::tanh(SQRT_2_OVER_PI*x*(1.0f + GELU_COEF_A*x*x)));
  87. }
  88. kernel void kernel_soft_max(
  89. device const float * src0,
  90. device float * dst,
  91. constant int64_t & ne00,
  92. constant int64_t & ne01,
  93. constant int64_t & ne02,
  94. threadgroup float * buf [[threadgroup(0)]],
  95. uint3 tgpig[[threadgroup_position_in_grid]],
  96. uint3 tpitg[[thread_position_in_threadgroup]],
  97. uint3 ntg[[threads_per_threadgroup]]) {
  98. const int64_t i03 = tgpig[2];
  99. const int64_t i02 = tgpig[1];
  100. const int64_t i01 = tgpig[0];
  101. device const float * psrc0 = src0 + i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  102. device float * pdst = dst + i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  103. // parallel max
  104. buf[tpitg[0]] = -INFINITY;
  105. for (int i00 = tpitg[0]; i00 < ne00; i00 += ntg[0]) {
  106. buf[tpitg[0]] = MAX(buf[tpitg[0]], psrc0[i00]);
  107. }
  108. // reduce
  109. threadgroup_barrier(mem_flags::mem_threadgroup);
  110. for (uint i = ntg[0]/2; i > 0; i /= 2) {
  111. if (tpitg[0] < i) {
  112. buf[tpitg[0]] = MAX(buf[tpitg[0]], buf[tpitg[0] + i]);
  113. }
  114. threadgroup_barrier(mem_flags::mem_threadgroup);
  115. }
  116. //// broadcast - not needed. There is a threadgroup barrier above in the last iteration of
  117. // the loop, and when that is done, buf[0] has the correct (synchronized) value
  118. //if (tpitg[0] == 0) {
  119. // buf[0] = buf[0];
  120. //}
  121. //threadgroup_barrier(mem_flags::mem_threadgroup);
  122. const float max = buf[0];
  123. // parallel sum
  124. buf[tpitg[0]] = 0.0f;
  125. for (int i00 = tpitg[0]; i00 < ne00; i00 += ntg[0]) {
  126. const float exp_psrc0 = exp(psrc0[i00] - max);
  127. buf[tpitg[0]] += exp_psrc0;
  128. // Remember the result of exp here. exp is expensive, so we really do not
  129. // whish to compute it twice.
  130. pdst[i00] = exp_psrc0;
  131. }
  132. // reduce
  133. threadgroup_barrier(mem_flags::mem_threadgroup);
  134. for (uint i = ntg[0]/2; i > 0; i /= 2) {
  135. if (tpitg[0] < i) {
  136. buf[tpitg[0]] += buf[tpitg[0] + i];
  137. }
  138. threadgroup_barrier(mem_flags::mem_threadgroup);
  139. }
  140. // broadcast - not needed, see above
  141. //// broadcast
  142. //if (tpitg[0] == 0) {
  143. // buf[0] = buf[0];
  144. //}
  145. //threadgroup_barrier(mem_flags::mem_threadgroup);
  146. const float sum = buf[0];
  147. for (int i00 = tpitg[0]; i00 < ne00; i00 += ntg[0]) {
  148. pdst[i00] /= sum;
  149. }
  150. }
  151. kernel void kernel_diag_mask_inf(
  152. device const float * src0,
  153. device float * dst,
  154. constant int64_t & ne00,
  155. constant int64_t & ne01,
  156. constant int & n_past,
  157. uint3 tpig[[thread_position_in_grid]]) {
  158. const int64_t i02 = tpig[2];
  159. const int64_t i01 = tpig[1];
  160. const int64_t i00 = tpig[0];
  161. if (i00 > n_past + i01) {
  162. dst[i02*ne01*ne00 + i01*ne00 + i00] = -INFINITY;
  163. } else {
  164. dst[i02*ne01*ne00 + i01*ne00 + i00] = src0[i02*ne01*ne00 + i01*ne00 + i00];
  165. }
  166. }
  167. kernel void kernel_norm(
  168. device const void * src0,
  169. device float * dst,
  170. constant int64_t & ne00,
  171. constant uint64_t & nb01,
  172. constant float & eps,
  173. threadgroup float * sum [[threadgroup(0)]],
  174. uint tgpig[[threadgroup_position_in_grid]],
  175. uint tpitg[[thread_position_in_threadgroup]],
  176. uint ntg[[threads_per_threadgroup]]) {
  177. device const float * x = (device const float *) ((device const char *) src0 + tgpig*nb01);
  178. // MEAN
  179. // parallel sum
  180. sum[tpitg] = 0.0f;
  181. for (int i00 = tpitg; i00 < ne00; i00 += ntg) {
  182. sum[tpitg] += x[i00];
  183. }
  184. // reduce
  185. threadgroup_barrier(mem_flags::mem_threadgroup);
  186. for (uint i = ntg/2; i > 0; i /= 2) {
  187. if (tpitg < i) {
  188. sum[tpitg] += sum[tpitg + i];
  189. }
  190. threadgroup_barrier(mem_flags::mem_threadgroup);
  191. }
  192. const float mean = sum[0] / ne00;
  193. // recenter and VARIANCE
  194. threadgroup_barrier(mem_flags::mem_threadgroup);
  195. device float * y = dst + tgpig*ne00;
  196. sum[tpitg] = 0.0f;
  197. for (int i00 = tpitg; i00 < ne00; i00 += ntg) {
  198. y[i00] = x[i00] - mean;
  199. sum[tpitg] += y[i00] * y[i00];
  200. }
  201. // reduce
  202. threadgroup_barrier(mem_flags::mem_threadgroup);
  203. for (uint i = ntg/2; i > 0; i /= 2) {
  204. if (tpitg < i) {
  205. sum[tpitg] += sum[tpitg + i];
  206. }
  207. threadgroup_barrier(mem_flags::mem_threadgroup);
  208. }
  209. const float variance = sum[0] / ne00;
  210. const float scale = 1.0f/sqrt(variance + eps);
  211. for (int i00 = tpitg; i00 < ne00; i00 += ntg) {
  212. y[i00] = y[i00] * scale;
  213. }
  214. }
  215. kernel void kernel_rms_norm(
  216. device const void * src0,
  217. device float * dst,
  218. constant int64_t & ne00,
  219. constant uint64_t & nb01,
  220. constant float & eps,
  221. threadgroup float * sum [[threadgroup(0)]],
  222. uint tgpig[[threadgroup_position_in_grid]],
  223. uint tpitg[[thread_position_in_threadgroup]],
  224. uint sgitg[[simdgroup_index_in_threadgroup]],
  225. uint tiisg[[thread_index_in_simdgroup]],
  226. uint ntg[[threads_per_threadgroup]]) {
  227. device const float4 * x = (device const float4 *) ((device const char *) src0 + tgpig*nb01);
  228. device const float * x_scalar = (device const float *) x;
  229. float4 sumf=0;
  230. float all_sum=0;
  231. // parallel sum
  232. for (int i00 = tpitg; i00 < ne00/4; i00 += ntg) {
  233. sumf += x[i00] * x[i00];
  234. }
  235. all_sum = sumf[0] + sumf[1] + sumf[2] + sumf[3];
  236. all_sum = simd_sum(all_sum);
  237. if (tiisg == 0) {
  238. sum[sgitg] = all_sum;
  239. }
  240. threadgroup_barrier(mem_flags::mem_threadgroup);
  241. // broadcast, simd group number is ntg / 32
  242. for (uint i = ntg / 32 / 2; i > 0; i /= 2) {
  243. if (tpitg < i) {
  244. sum[tpitg] += sum[tpitg + i];
  245. }
  246. }
  247. if (tpitg == 0) {
  248. for (int i = 4 * (ne00 / 4); i < ne00; i++) {sum[0] += x_scalar[i];}
  249. sum[0] /= ne00;
  250. }
  251. threadgroup_barrier(mem_flags::mem_threadgroup);
  252. const float mean = sum[0];
  253. const float scale = 1.0f/sqrt(mean + eps);
  254. device float4 * y = (device float4 *) (dst + tgpig*ne00);
  255. device float * y_scalar = (device float *) y;
  256. for (int i00 = tpitg; i00 < ne00/4; i00 += ntg) {
  257. y[i00] = x[i00] * scale;
  258. }
  259. if (tpitg == 0) {
  260. for (int i00 = 4 * (ne00 / 4); i00 < ne00; i00++) {y_scalar[i00] = x_scalar[i00] * scale;}
  261. }
  262. }
  263. // function for calculate inner product between half a q4_0 block and 16 floats (yl), sumy is SUM(yl[i])
  264. // il indicates where the q4 quants begin (0 or QK4_0/4)
  265. // we assume that the yl's have been multiplied with the appropriate scale factor
  266. // that corresponds to the missing bit shifts (1, 1/16, 1/256, 1/4096)
  267. inline float block_q_n_dot_y(device const block_q4_0 * qb_curr, float sumy, thread float * yl, int il) {
  268. float d = qb_curr->d;
  269. float2 acc = 0.f;
  270. device const uint16_t * qs = ((device const uint16_t *)qb_curr + 1 + il/2);
  271. for (int i = 0; i < 8; i+=2) {
  272. acc[0] += yl[i + 0] * (qs[i / 2] & 0x000F)
  273. + yl[i + 1] * (qs[i / 2] & 0x0F00);
  274. acc[1] += yl[i + 8] * (qs[i / 2] & 0x00F0)
  275. + yl[i + 9] * (qs[i / 2] & 0xF000);
  276. }
  277. return d * (sumy * -8.f + acc[0] + acc[1]);
  278. }
  279. // function for calculate inner product between half a q4_1 block and 16 floats (yl), sumy is SUM(yl[i])
  280. // il indicates where the q4 quants begin (0 or QK4_0/4)
  281. // we assume that the yl's have been multiplied with the appropriate scale factor
  282. // that corresponds to the missing bit shifts (1, 1/16, 1/256, 1/4096)
  283. inline float block_q_n_dot_y(device const block_q4_1 * qb_curr, float sumy, thread float * yl, int il) {
  284. float d = qb_curr->d;
  285. float m = qb_curr->m;
  286. device const uint16_t * qs = ((device const uint16_t *)qb_curr + 2 + il/2);
  287. float2 acc = 0.f;
  288. for (int i = 0; i < 8; i+=2) {
  289. acc[0] += yl[i + 0] * (qs[i / 2] & 0x000F)
  290. + yl[i + 1] * (qs[i / 2] & 0x0F00);
  291. acc[1] += yl[i + 8] * (qs[i / 2] & 0x00F0)
  292. + yl[i + 9] * (qs[i / 2] & 0xF000);
  293. }
  294. return d * (acc[0] + acc[1]) + sumy * m;
  295. }
  296. // putting them in the kernel cause a significant performance penalty
  297. #define N_DST 4 // each SIMD group works on 4 rows
  298. #define N_SIMDGROUP 2 // number of SIMD groups in a thread group
  299. #define N_SIMDWIDTH 32 // assuming SIMD group size is 32
  300. //Note: This is a template, but strictly speaking it only applies to
  301. // quantizations where the block size is 32. It also does not
  302. // giard against the number of rows not being divisible by
  303. // N_DST, so this is another explicit assumption of the implementation.
  304. template<typename block_q_type, int nr, int nsg, int nw>
  305. void mul_vec_q_n_f32(device const void * src0, device const float * src1, device float * dst,
  306. int64_t ne00, int64_t ne01, int64_t ne02, int64_t ne10, int64_t ne12, int64_t ne0, int64_t ne1, uint gqa,
  307. uint3 tgpig, uint tiisg, uint sgitg) {
  308. const int nb = ne00/QK4_0;
  309. const int r0 = tgpig.x;
  310. const int r1 = tgpig.y;
  311. const int im = tgpig.z;
  312. const int first_row = (r0 * nsg + sgitg) * nr;
  313. const uint offset0 = first_row * nb + im/gqa*(nb*ne0);
  314. device const block_q_type * x = (device const block_q_type *) src0 + offset0;
  315. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  316. float yl[16]; // src1 vector cache
  317. float sumf[nr]={0.f};
  318. const int ix = tiisg/2;
  319. const int il = 8*(tiisg%2);
  320. device const float * yb = y + ix * QK4_0 + il;
  321. // each thread in a SIMD group deals with half a block.
  322. for (int ib = ix; ib < nb; ib += nw/2) {
  323. float sumy = 0;
  324. for (int i = 0; i < 8; i += 2) {
  325. sumy += yb[i] + yb[i+1];
  326. yl[i+0] = yb[i+ 0];
  327. yl[i+1] = yb[i+ 1]/256.f;
  328. sumy += yb[i+16] + yb[i+17];
  329. yl[i+8] = yb[i+16]/16.f;
  330. yl[i+9] = yb[i+17]/4096.f;
  331. }
  332. for (int row = 0; row < nr; row++) {
  333. sumf[row] += block_q_n_dot_y(x+ib+row*nb, sumy, yl, il);
  334. }
  335. yb += QK4_0 * 16;
  336. }
  337. for (int row = 0; row < nr; ++row) {
  338. const float tot = simd_sum(sumf[row]);
  339. if (tiisg == 0 && first_row + row < ne01) {
  340. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = tot;
  341. }
  342. }
  343. }
  344. kernel void kernel_mul_mat_q4_0_f32(
  345. device const void * src0,
  346. device const float * src1,
  347. device float * dst,
  348. constant int64_t & ne00,
  349. constant int64_t & ne01[[buffer(4)]],
  350. constant int64_t & ne02[[buffer(5)]],
  351. constant int64_t & ne10[[buffer(9)]],
  352. constant int64_t & ne12[[buffer(11)]],
  353. constant int64_t & ne0[[buffer(15)]],
  354. constant int64_t & ne1[[buffer(16)]],
  355. constant uint & gqa[[buffer(17)]],
  356. uint3 tgpig[[threadgroup_position_in_grid]],
  357. uint tiisg[[thread_index_in_simdgroup]],
  358. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  359. mul_vec_q_n_f32<block_q4_0, N_DST, N_SIMDGROUP, N_SIMDWIDTH>(src0,src1,dst,ne00,ne01,ne02,ne10,ne12,ne0,ne1,gqa,tgpig,tiisg,sgitg);
  360. }
  361. kernel void kernel_mul_mat_q4_1_f32(
  362. device const void * src0,
  363. device const float * src1,
  364. device float * dst,
  365. constant int64_t & ne00,
  366. constant int64_t & ne01[[buffer(4)]],
  367. constant int64_t & ne02[[buffer(5)]],
  368. constant int64_t & ne10[[buffer(9)]],
  369. constant int64_t & ne12[[buffer(11)]],
  370. constant int64_t & ne0[[buffer(15)]],
  371. constant int64_t & ne1[[buffer(16)]],
  372. constant uint & gqa[[buffer(17)]],
  373. uint3 tgpig[[threadgroup_position_in_grid]],
  374. uint tiisg[[thread_index_in_simdgroup]],
  375. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  376. mul_vec_q_n_f32<block_q4_1, N_DST, N_SIMDGROUP, N_SIMDWIDTH>(src0,src1,dst,ne00,ne01,ne02,ne10,ne12,ne0,ne1,gqa,tgpig,tiisg,sgitg);
  377. }
  378. #define NB_Q8_0 8
  379. kernel void kernel_mul_mat_q8_0_f32(
  380. device const void * src0,
  381. device const float * src1,
  382. device float * dst,
  383. constant int64_t & ne00,
  384. constant int64_t & ne01[[buffer(4)]],
  385. constant int64_t & ne02[[buffer(5)]],
  386. constant int64_t & ne10[[buffer(9)]],
  387. constant int64_t & ne12[[buffer(11)]],
  388. constant int64_t & ne0[[buffer(15)]],
  389. constant int64_t & ne1[[buffer(16)]],
  390. constant uint & gqa[[buffer(17)]],
  391. uint3 tgpig[[threadgroup_position_in_grid]],
  392. uint tiisg[[thread_index_in_simdgroup]],
  393. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  394. const int nr = N_DST;
  395. const int nsg = N_SIMDGROUP;
  396. const int nw = N_SIMDWIDTH;
  397. const int nb = ne00/QK8_0;
  398. const int r0 = tgpig.x;
  399. const int r1 = tgpig.y;
  400. const int im = tgpig.z;
  401. const int first_row = (r0 * nsg + sgitg) * nr;
  402. const uint offset0 = first_row * nb + im/gqa*(nb*ne0);
  403. device const block_q8_0 * x = (device const block_q8_0 *) src0 + offset0;
  404. device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;
  405. float yl[NB_Q8_0];
  406. float sumf[nr]={0.f};
  407. const int ix = tiisg/4;
  408. const int il = tiisg%4;
  409. device const float * yb = y + ix * QK8_0 + NB_Q8_0*il;
  410. // each thread in a SIMD group deals with NB_Q8_0 quants at a time
  411. for (int ib = ix; ib < nb; ib += nw/4) {
  412. for (int i = 0; i < NB_Q8_0; ++i) {
  413. yl[i] = yb[i];
  414. }
  415. for (int row = 0; row < nr; row++) {
  416. device const int8_t * qs = x[ib+row*nb].qs + NB_Q8_0*il;
  417. float sumq = 0.f;
  418. for (int iq = 0; iq < NB_Q8_0; ++iq) {
  419. sumq += qs[iq] * yl[iq];
  420. }
  421. sumf[row] += sumq*x[ib+row*nb].d;
  422. }
  423. yb += NB_Q8_0 * nw;
  424. }
  425. for (int row = 0; row < nr; ++row) {
  426. const float tot = simd_sum(sumf[row]);
  427. if (tiisg == 0 && first_row + row < ne01) {
  428. dst[r1*ne0 + im*ne0*ne1 + first_row + row] = tot;
  429. }
  430. }
  431. }
  432. kernel void kernel_mul_mat_f16_f32_1row(
  433. device const char * src0,
  434. device const char * src1,
  435. device float * dst,
  436. constant int64_t & ne00,
  437. constant int64_t & ne01,
  438. constant int64_t & ne02,
  439. constant uint64_t & nb00,
  440. constant uint64_t & nb01,
  441. constant uint64_t & nb02,
  442. constant int64_t & ne10,
  443. constant int64_t & ne11,
  444. constant int64_t & ne12,
  445. constant uint64_t & nb10,
  446. constant uint64_t & nb11,
  447. constant uint64_t & nb12,
  448. constant int64_t & ne0,
  449. constant int64_t & ne1,
  450. uint3 tgpig[[threadgroup_position_in_grid]],
  451. uint tiisg[[thread_index_in_simdgroup]]) {
  452. const int64_t r0 = tgpig.x;
  453. const int64_t r1 = tgpig.y;
  454. const int64_t im = tgpig.z;
  455. device const half * x = (device const half *) (src0 + r0*nb01 + im/(ne12/ne02)*nb02);
  456. device const float * y = (device const float *) (src1 + r1*nb11 + im*nb12);
  457. float sumf = 0;
  458. if (ne00 < 128) {
  459. for (int i = tiisg; i < ne00; i += 32) {
  460. sumf += (float) x[i] * (float) y[i];
  461. }
  462. float all_sum = simd_sum(sumf);
  463. if (tiisg == 0) {
  464. dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum;
  465. }
  466. } else {
  467. device const half4 * x4 = (device const half4 *) x;
  468. device const float4 * y4 = (device const float4 *) y;
  469. for (int i = tiisg; i < ne00/4; i += 32) {
  470. for (int k = 0; k < 4; ++k) sumf += (float)x4[i][k] * y4[i][k];
  471. }
  472. float all_sum = simd_sum(sumf);
  473. if (tiisg == 0) {
  474. for (int i = 4*(ne00/4); i < ne00; ++i) all_sum += (float) x[i] * y[i];
  475. dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum;
  476. }
  477. }
  478. }
  479. #define N_F16_F32 4
  480. kernel void kernel_mul_mat_f16_f32(
  481. device const char * src0,
  482. device const char * src1,
  483. device float * dst,
  484. constant int64_t & ne00,
  485. constant int64_t & ne01,
  486. constant int64_t & ne02,
  487. constant uint64_t & nb00,
  488. constant uint64_t & nb01,
  489. constant uint64_t & nb02,
  490. constant int64_t & ne10,
  491. constant int64_t & ne11,
  492. constant int64_t & ne12,
  493. constant uint64_t & nb10,
  494. constant uint64_t & nb11,
  495. constant uint64_t & nb12,
  496. constant int64_t & ne0,
  497. constant int64_t & ne1,
  498. uint3 tgpig[[threadgroup_position_in_grid]],
  499. uint tiisg[[thread_index_in_simdgroup]]) {
  500. const int64_t r0 = tgpig.x;
  501. const int64_t rb = tgpig.y*N_F16_F32;
  502. const int64_t im = tgpig.z;
  503. device const half * x = (device const half *) (src0 + r0*nb01 + im/(ne12/ne02)*nb02);
  504. if (ne00 < 128) {
  505. for (int row = 0; row < N_F16_F32; ++row) {
  506. int r1 = rb + row;
  507. if (r1 >= ne11) {
  508. break;
  509. }
  510. device const float * y = (device const float *) (src1 + r1*nb11 + im*nb12);
  511. float sumf = 0;
  512. for (int i = tiisg; i < ne00; i += 32) {
  513. sumf += (float) x[i] * (float) y[i];
  514. }
  515. float all_sum = simd_sum(sumf);
  516. if (tiisg == 0) {
  517. dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum;
  518. }
  519. }
  520. } else {
  521. device const half4 * x4 = (device const half4 *)x;
  522. for (int row = 0; row < N_F16_F32; ++row) {
  523. int r1 = rb + row;
  524. if (r1 >= ne11) {
  525. break;
  526. }
  527. device const float * y = (device const float *) (src1 + r1*nb11 + im*nb12);
  528. device const float4 * y4 = (device const float4 *) y;
  529. float sumf = 0;
  530. for (int i = tiisg; i < ne00/4; i += 32) {
  531. for (int k = 0; k < 4; ++k) sumf += (float) x4[i][k] * y4[i][k];
  532. }
  533. float all_sum = simd_sum(sumf);
  534. if (tiisg == 0) {
  535. for (int i = 4*(ne00/4); i < ne00; ++i) all_sum += (float) x[i] * y[i];
  536. dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum;
  537. }
  538. }
  539. }
  540. }
  541. kernel void kernel_alibi_f32(
  542. device const float * src0,
  543. device float * dst,
  544. constant int64_t & ne00,
  545. constant int64_t & ne01,
  546. constant int64_t & ne02,
  547. constant int64_t & ne03,
  548. constant uint64_t & nb00,
  549. constant uint64_t & nb01,
  550. constant uint64_t & nb02,
  551. constant uint64_t & nb03,
  552. constant int64_t & ne0,
  553. constant int64_t & ne1,
  554. constant int64_t & ne2,
  555. constant int64_t & ne3,
  556. constant uint64_t & nb0,
  557. constant uint64_t & nb1,
  558. constant uint64_t & nb2,
  559. constant uint64_t & nb3,
  560. constant float & m0,
  561. uint3 tgpig[[threadgroup_position_in_grid]],
  562. uint3 tpitg[[thread_position_in_threadgroup]],
  563. uint3 ntg[[threads_per_threadgroup]]) {
  564. const int64_t i03 = tgpig[2];
  565. const int64_t i02 = tgpig[1];
  566. const int64_t i01 = tgpig[0];
  567. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  568. const int64_t i3 = n / (ne2*ne1*ne0);
  569. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  570. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  571. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  572. device float * dst_data = (device float *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  573. float m_k = pow(m0, i2 + 1);
  574. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  575. device const float * src = (device float *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  576. dst_data[i00] = src[0] + m_k * (i00 - ne00 + 1);
  577. }
  578. }
  579. kernel void kernel_rope(
  580. device const void * src0,
  581. device float * dst,
  582. constant int64_t & ne00,
  583. constant int64_t & ne01,
  584. constant int64_t & ne02,
  585. constant int64_t & ne03,
  586. constant uint64_t & nb00,
  587. constant uint64_t & nb01,
  588. constant uint64_t & nb02,
  589. constant uint64_t & nb03,
  590. constant int64_t & ne0,
  591. constant int64_t & ne1,
  592. constant int64_t & ne2,
  593. constant int64_t & ne3,
  594. constant uint64_t & nb0,
  595. constant uint64_t & nb1,
  596. constant uint64_t & nb2,
  597. constant uint64_t & nb3,
  598. constant int & n_past,
  599. constant int & n_dims,
  600. constant int & mode,
  601. constant float & freq_base,
  602. constant float & freq_scale,
  603. uint tiitg[[thread_index_in_threadgroup]],
  604. uint3 tptg[[threads_per_threadgroup]],
  605. uint3 tgpig[[threadgroup_position_in_grid]]) {
  606. const int64_t i3 = tgpig[2];
  607. const int64_t i2 = tgpig[1];
  608. const int64_t i1 = tgpig[0];
  609. const bool is_neox = mode & 2;
  610. const int64_t p = ((mode & 1) == 0 ? n_past + i2 : i2);
  611. const float theta_0 = freq_scale * (float)p;
  612. const float inv_ndims = -1.f/n_dims;
  613. if (!is_neox) {
  614. for (int64_t i0 = 2*tiitg; i0 < ne0; i0 += 2*tptg.x) {
  615. const float theta = theta_0 * pow(freq_base, inv_ndims*i0);
  616. const float cos_theta = cos(theta);
  617. const float sin_theta = sin(theta);
  618. device const float * const src = (device float *)((device char *) src0 + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
  619. device float * dst_data = (device float *)((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  620. const float x0 = src[0];
  621. const float x1 = src[1];
  622. dst_data[0] = x0*cos_theta - x1*sin_theta;
  623. dst_data[1] = x0*sin_theta + x1*cos_theta;
  624. }
  625. } else {
  626. for (int64_t ib = 0; ib < ne0/n_dims; ++ib) {
  627. for (int64_t ic = 2*tiitg; ic < n_dims; ic += 2*tptg.x) {
  628. const float theta = theta_0 * pow(freq_base, inv_ndims*ic - ib);
  629. const float cos_theta = cos(theta);
  630. const float sin_theta = sin(theta);
  631. const int64_t i0 = ib*n_dims + ic/2;
  632. device const float * const src = (device float *)((device char *) src0 + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
  633. device float * dst_data = (device float *)((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  634. const float x0 = src[0];
  635. const float x1 = src[n_dims/2];
  636. dst_data[0] = x0*cos_theta - x1*sin_theta;
  637. dst_data[n_dims/2] = x0*sin_theta + x1*cos_theta;
  638. }
  639. }
  640. }
  641. }
  642. kernel void kernel_cpy_f16_f16(
  643. device const half * src0,
  644. device half * dst,
  645. constant int64_t & ne00,
  646. constant int64_t & ne01,
  647. constant int64_t & ne02,
  648. constant int64_t & ne03,
  649. constant uint64_t & nb00,
  650. constant uint64_t & nb01,
  651. constant uint64_t & nb02,
  652. constant uint64_t & nb03,
  653. constant int64_t & ne0,
  654. constant int64_t & ne1,
  655. constant int64_t & ne2,
  656. constant int64_t & ne3,
  657. constant uint64_t & nb0,
  658. constant uint64_t & nb1,
  659. constant uint64_t & nb2,
  660. constant uint64_t & nb3,
  661. uint3 tgpig[[threadgroup_position_in_grid]],
  662. uint3 tpitg[[thread_position_in_threadgroup]],
  663. uint3 ntg[[threads_per_threadgroup]]) {
  664. const int64_t i03 = tgpig[2];
  665. const int64_t i02 = tgpig[1];
  666. const int64_t i01 = tgpig[0];
  667. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  668. const int64_t i3 = n / (ne2*ne1*ne0);
  669. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  670. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  671. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  672. device half * dst_data = (device half *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  673. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  674. device const half * src = (device half *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  675. dst_data[i00] = src[0];
  676. }
  677. }
  678. kernel void kernel_cpy_f32_f16(
  679. device const float * src0,
  680. device half * dst,
  681. constant int64_t & ne00,
  682. constant int64_t & ne01,
  683. constant int64_t & ne02,
  684. constant int64_t & ne03,
  685. constant uint64_t & nb00,
  686. constant uint64_t & nb01,
  687. constant uint64_t & nb02,
  688. constant uint64_t & nb03,
  689. constant int64_t & ne0,
  690. constant int64_t & ne1,
  691. constant int64_t & ne2,
  692. constant int64_t & ne3,
  693. constant uint64_t & nb0,
  694. constant uint64_t & nb1,
  695. constant uint64_t & nb2,
  696. constant uint64_t & nb3,
  697. uint3 tgpig[[threadgroup_position_in_grid]],
  698. uint3 tpitg[[thread_position_in_threadgroup]],
  699. uint3 ntg[[threads_per_threadgroup]]) {
  700. const int64_t i03 = tgpig[2];
  701. const int64_t i02 = tgpig[1];
  702. const int64_t i01 = tgpig[0];
  703. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  704. const int64_t i3 = n / (ne2*ne1*ne0);
  705. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  706. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  707. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  708. device half * dst_data = (device half *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  709. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  710. device const float * src = (device float *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  711. dst_data[i00] = src[0];
  712. }
  713. }
  714. kernel void kernel_cpy_f32_f32(
  715. device const float * src0,
  716. device float * dst,
  717. constant int64_t & ne00,
  718. constant int64_t & ne01,
  719. constant int64_t & ne02,
  720. constant int64_t & ne03,
  721. constant uint64_t & nb00,
  722. constant uint64_t & nb01,
  723. constant uint64_t & nb02,
  724. constant uint64_t & nb03,
  725. constant int64_t & ne0,
  726. constant int64_t & ne1,
  727. constant int64_t & ne2,
  728. constant int64_t & ne3,
  729. constant uint64_t & nb0,
  730. constant uint64_t & nb1,
  731. constant uint64_t & nb2,
  732. constant uint64_t & nb3,
  733. uint3 tgpig[[threadgroup_position_in_grid]],
  734. uint3 tpitg[[thread_position_in_threadgroup]],
  735. uint3 ntg[[threads_per_threadgroup]]) {
  736. const int64_t i03 = tgpig[2];
  737. const int64_t i02 = tgpig[1];
  738. const int64_t i01 = tgpig[0];
  739. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  740. const int64_t i3 = n / (ne2*ne1*ne0);
  741. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  742. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  743. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  744. device float * dst_data = (device float *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  745. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  746. device const float * src = (device float *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  747. dst_data[i00] = src[0];
  748. }
  749. }
  750. //============================================ k-quants ======================================================
  751. #ifndef QK_K
  752. #define QK_K 256
  753. #else
  754. static_assert(QK_K == 256 || QK_K == 64, "QK_K must be 256 or 64");
  755. #endif
  756. #if QK_K == 256
  757. #define K_SCALE_SIZE 12
  758. #else
  759. #define K_SCALE_SIZE 4
  760. #endif
  761. typedef struct {
  762. uint8_t scales[QK_K/16]; // scales and mins, quantized with 4 bits
  763. uint8_t qs[QK_K/4]; // quants
  764. half d; // super-block scale for quantized scales
  765. half dmin; // super-block scale for quantized mins
  766. } block_q2_K;
  767. // 84 bytes / block
  768. typedef struct {
  769. uint8_t hmask[QK_K/8]; // quants - high bit
  770. uint8_t qs[QK_K/4]; // quants - low 2 bits
  771. #if QK_K == 64
  772. uint8_t scales[2];
  773. #else
  774. uint8_t scales[K_SCALE_SIZE]; // scales, quantized with 6 bits
  775. #endif
  776. half d; // super-block scale
  777. } block_q3_K;
  778. #if QK_K == 64
  779. typedef struct {
  780. half d[2]; // super-block scales/mins
  781. uint8_t scales[2];
  782. uint8_t qs[QK_K/2]; // 4-bit quants
  783. } block_q4_K;
  784. #else
  785. typedef struct {
  786. half d; // super-block scale for quantized scales
  787. half dmin; // super-block scale for quantized mins
  788. uint8_t scales[K_SCALE_SIZE]; // scales and mins, quantized with 6 bits
  789. uint8_t qs[QK_K/2]; // 4--bit quants
  790. } block_q4_K;
  791. #endif
  792. #if QK_K == 64
  793. typedef struct {
  794. half d; // super-block scales/mins
  795. int8_t scales[QK_K/16]; // 8-bit block scales
  796. uint8_t qh[QK_K/8]; // quants, high bit
  797. uint8_t qs[QK_K/2]; // quants, low 4 bits
  798. } block_q5_K;
  799. #else
  800. typedef struct {
  801. half d; // super-block scale for quantized scales
  802. half dmin; // super-block scale for quantized mins
  803. uint8_t scales[3*QK_K/64]; // scales and mins, quantized with 6 bits
  804. uint8_t qh[QK_K/8]; // quants, high bit
  805. uint8_t qs[QK_K/2]; // quants, low 4 bits
  806. } block_q5_K;
  807. // 176 bytes / block
  808. #endif
  809. typedef struct {
  810. uint8_t ql[QK_K/2]; // quants, lower 4 bits
  811. uint8_t qh[QK_K/4]; // quants, upper 2 bits
  812. int8_t scales[QK_K/16]; // scales, quantized with 8 bits
  813. half d; // super-block scale
  814. } block_q6_K;
  815. // 210 bytes / block
  816. static inline uchar4 get_scale_min_k4(int j, device const uint8_t * q) {
  817. uchar4 r;
  818. if (j < 4) {
  819. r[0] = q[j+0] & 63;
  820. r[2] = q[j+1] & 63;
  821. r[1] = q[j+4] & 63;
  822. r[3] = q[j+5] & 63;
  823. } else {
  824. r[0] = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  825. r[2] = (q[j+5] & 0xF) | ((q[j-3] >> 6) << 4);
  826. r[1] = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  827. r[3] = (q[j+5] >> 4) | ((q[j+1] >> 6) << 4);
  828. }
  829. return r;
  830. }
  831. //====================================== dot products =========================
  832. kernel void kernel_mul_mat_q2_K_f32(
  833. device const void * src0,
  834. device const float * src1,
  835. device float * dst,
  836. constant int64_t & ne00,
  837. constant int64_t & ne01[[buffer(4)]],
  838. constant int64_t & ne02[[buffer(5)]],
  839. constant int64_t & ne10[[buffer(9)]],
  840. constant int64_t & ne12[[buffer(11)]],
  841. constant int64_t & ne0[[buffer(15)]],
  842. constant int64_t & ne1[[buffer(16)]],
  843. constant uint & gqa[[buffer(17)]],
  844. uint3 tgpig[[threadgroup_position_in_grid]],
  845. uint tiisg[[thread_index_in_simdgroup]],
  846. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  847. const int nb = ne00/QK_K;
  848. const int r0 = tgpig.x;
  849. const int r1 = tgpig.y;
  850. const int r2 = tgpig.z;
  851. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  852. const int ib_row = first_row * nb;
  853. const uint offset0 = r2/gqa*(nb*ne0);
  854. device const block_q2_K * x = (device const block_q2_K *) src0 + ib_row + offset0;
  855. device const float * y = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  856. float yl[32];
  857. float sumf[N_DST]={0.f}, all_sum;
  858. const int step = sizeof(block_q2_K) * nb;
  859. #if QK_K == 256
  860. const int ix = tiisg/8; // 0...3
  861. const int it = tiisg%8; // 0...7
  862. const int im = it/4; // 0 or 1
  863. const int ir = it%4; // 0...3
  864. const int is = (8*ir)/16;// 0 or 1
  865. device const float * y4 = y + ix * QK_K + 128 * im + 8 * ir;
  866. for (int ib = ix; ib < nb; ib += 4) {
  867. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  868. for (int i = 0; i < 8; ++i) {
  869. yl[i+ 0] = y4[i+ 0]; sumy[0] += yl[i+ 0];
  870. yl[i+ 8] = y4[i+32]; sumy[1] += yl[i+ 8];
  871. yl[i+16] = y4[i+64]; sumy[2] += yl[i+16];
  872. yl[i+24] = y4[i+96]; sumy[3] += yl[i+24];
  873. }
  874. device const uint8_t * sc = (device const uint8_t *)x[ib].scales + 8*im + is;
  875. device const uint16_t * qs = (device const uint16_t *)x[ib].qs + 16 * im + 4 * ir;
  876. device const half * dh = &x[ib].d;
  877. for (int row = 0; row < N_DST; row++) {
  878. float4 acc1 = {0.f, 0.f, 0.f, 0.f};
  879. float4 acc2 = {0.f, 0.f, 0.f, 0.f};
  880. for (int i = 0; i < 8; i += 2) {
  881. acc1[0] += yl[i+ 0] * (qs[i/2] & 0x0003);
  882. acc2[0] += yl[i+ 1] * (qs[i/2] & 0x0300);
  883. acc1[1] += yl[i+ 8] * (qs[i/2] & 0x000c);
  884. acc2[1] += yl[i+ 9] * (qs[i/2] & 0x0c00);
  885. acc1[2] += yl[i+16] * (qs[i/2] & 0x0030);
  886. acc2[2] += yl[i+17] * (qs[i/2] & 0x3000);
  887. acc1[3] += yl[i+24] * (qs[i/2] & 0x00c0);
  888. acc2[3] += yl[i+25] * (qs[i/2] & 0xc000);
  889. }
  890. float dall = dh[0];
  891. float dmin = dh[1] * 1.f/16.f;
  892. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc2[0]) * (sc[0] & 0xF) * 1.f/ 1.f +
  893. (acc1[1] + 1.f/256.f * acc2[1]) * (sc[2] & 0xF) * 1.f/ 4.f +
  894. (acc1[2] + 1.f/256.f * acc2[2]) * (sc[4] & 0xF) * 1.f/16.f +
  895. (acc1[3] + 1.f/256.f * acc2[3]) * (sc[6] & 0xF) * 1.f/64.f) -
  896. dmin * (sumy[0] * (sc[0] & 0xF0) + sumy[1] * (sc[2] & 0xF0) + sumy[2] * (sc[4] & 0xF0) + sumy[3] * (sc[6] & 0xF0));
  897. qs += step/2;
  898. sc += step;
  899. dh += step/2;
  900. }
  901. y4 += 4 * QK_K;
  902. }
  903. #else
  904. const int ix = tiisg/2; // 0...15
  905. const int it = tiisg%2; // 0...1
  906. device const float * y4 = y + ix * QK_K + 8 * it;
  907. for (int ib = ix; ib < nb; ib += 16) {
  908. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  909. for (int i = 0; i < 8; ++i) {
  910. yl[i+ 0] = y4[i+ 0]; sumy[0] += yl[i+ 0];
  911. yl[i+ 8] = y4[i+16]; sumy[1] += yl[i+ 8];
  912. yl[i+16] = y4[i+32]; sumy[2] += yl[i+16];
  913. yl[i+24] = y4[i+48]; sumy[3] += yl[i+24];
  914. }
  915. device const uint8_t * sc = (device const uint8_t *)x[ib].scales;
  916. device const uint16_t * qs = (device const uint16_t *)x[ib].qs + 4 * it;
  917. device const half * dh = &x[ib].d;
  918. for (int row = 0; row < N_DST; row++) {
  919. float4 acc1 = {0.f, 0.f, 0.f, 0.f};
  920. float4 acc2 = {0.f, 0.f, 0.f, 0.f};
  921. for (int i = 0; i < 8; i += 2) {
  922. acc1[0] += yl[i+ 0] * (qs[i/2] & 0x0003);
  923. acc2[0] += yl[i+ 1] * (qs[i/2] & 0x0300);
  924. acc1[1] += yl[i+ 8] * (qs[i/2] & 0x000c);
  925. acc2[1] += yl[i+ 9] * (qs[i/2] & 0x0c00);
  926. acc1[2] += yl[i+16] * (qs[i/2] & 0x0030);
  927. acc2[2] += yl[i+17] * (qs[i/2] & 0x3000);
  928. acc1[3] += yl[i+24] * (qs[i/2] & 0x00c0);
  929. acc2[3] += yl[i+25] * (qs[i/2] & 0xc000);
  930. }
  931. float dall = dh[0];
  932. float dmin = dh[1];
  933. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc2[0]) * (sc[0] & 0xF) * 1.f/ 1.f +
  934. (acc1[1] + 1.f/256.f * acc2[1]) * (sc[1] & 0xF) * 1.f/ 4.f +
  935. (acc1[2] + 1.f/256.f * acc2[2]) * (sc[2] & 0xF) * 1.f/16.f +
  936. (acc1[3] + 1.f/256.f * acc2[3]) * (sc[3] & 0xF) * 1.f/64.f) -
  937. dmin * (sumy[0] * (sc[0] >> 4) + sumy[1] * (sc[1] >> 4) + sumy[2] * (sc[2] >> 4) + sumy[3] * (sc[3] >> 4));
  938. qs += step/2;
  939. sc += step;
  940. dh += step/2;
  941. }
  942. y4 += 16 * QK_K;
  943. }
  944. #endif
  945. for (int row = 0; row < N_DST; ++row) {
  946. all_sum = simd_sum(sumf[row]);
  947. if (tiisg == 0) {
  948. dst[r1*ne0 + r2*ne0*ne1 + first_row + row] = all_sum;
  949. }
  950. }
  951. }
  952. #if QK_K == 256
  953. kernel void kernel_mul_mat_q3_K_f32(
  954. device const void * src0,
  955. device const float * src1,
  956. device float * dst,
  957. constant int64_t & ne00,
  958. constant int64_t & ne01[[buffer(4)]],
  959. constant int64_t & ne02[[buffer(5)]],
  960. constant int64_t & ne10[[buffer(9)]],
  961. constant int64_t & ne12[[buffer(11)]],
  962. constant int64_t & ne0[[buffer(15)]],
  963. constant int64_t & ne1[[buffer(16)]],
  964. constant uint & gqa[[buffer(17)]],
  965. uint3 tgpig[[threadgroup_position_in_grid]],
  966. uint tiisg[[thread_index_in_simdgroup]],
  967. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  968. const int nb = ne00/QK_K;
  969. const int64_t r0 = tgpig.x;
  970. const int64_t r1 = tgpig.y;
  971. const int64_t r2 = tgpig.z;
  972. const int first_row = (r0 * N_SIMDGROUP + sgitg) * 2;
  973. const uint offset0 = r2/gqa*(nb*ne0);
  974. device const block_q3_K * x = (device const block_q3_K *) src0 + first_row*nb + offset0;
  975. device const float * yy = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  976. float yl[32];
  977. const uint16_t kmask1 = 0x3030;
  978. const uint16_t kmask2 = 0x0f0f;
  979. const int tid = tiisg/4;
  980. const int ix = tiisg%4;
  981. const int ip = tid/4; // 0 or 1
  982. const int il = 2*((tid%4)/2); // 0 or 2
  983. const int ir = tid%2;
  984. const int n = 8;
  985. const int l0 = n*ir;
  986. // One would think that the Metal compiler would figure out that ip and il can only have
  987. // 4 possible states, and optimize accordingly. Well, no. It needs help, and we do it
  988. // with these two tales.
  989. //
  990. // Possible masks for the high bit
  991. const ushort4 mm[4] = {{0x0001, 0x0100, 0x0002, 0x0200}, // ip = 0, il = 0
  992. {0x0004, 0x0400, 0x0008, 0x0800}, // ip = 0, il = 2
  993. {0x0010, 0x1000, 0x0020, 0x2000}, // ip = 1, il = 0
  994. {0x0040, 0x4000, 0x0080, 0x8000}}; // ip = 1, il = 2
  995. // Possible masks for the low 2 bits
  996. const int4 qm[2] = {{0x0003, 0x0300, 0x000c, 0x0c00}, {0x0030, 0x3000, 0x00c0, 0xc000}};
  997. const ushort4 hm = mm[2*ip + il/2];
  998. const int shift = 2*il;
  999. const float v1 = il == 0 ? 4.f : 64.f;
  1000. const float v2 = 4.f * v1;
  1001. const uint16_t s_shift1 = 4*ip;
  1002. const uint16_t s_shift2 = s_shift1 + il;
  1003. const int q_offset = 32*ip + l0;
  1004. const int y_offset = 128*ip + 32*il + l0;
  1005. const int step = sizeof(block_q3_K) * nb / 2;
  1006. device const float * y1 = yy + ix*QK_K + y_offset;
  1007. uint32_t scales32, aux32;
  1008. thread uint16_t * scales16 = (thread uint16_t *)&scales32;
  1009. thread const int8_t * scales = (thread const int8_t *)&scales32;
  1010. float sumf1[2] = {0.f};
  1011. float sumf2[2] = {0.f};
  1012. for (int i = ix; i < nb; i += 4) {
  1013. for (int l = 0; l < 8; ++l) {
  1014. yl[l+ 0] = y1[l+ 0];
  1015. yl[l+ 8] = y1[l+16];
  1016. yl[l+16] = y1[l+32];
  1017. yl[l+24] = y1[l+48];
  1018. }
  1019. device const uint16_t * q = (device const uint16_t *)(x[i].qs + q_offset);
  1020. device const uint16_t * h = (device const uint16_t *)(x[i].hmask + l0);
  1021. device const uint16_t * a = (device const uint16_t *)(x[i].scales);
  1022. device const half * dh = &x[i].d;
  1023. for (int row = 0; row < 2; ++row) {
  1024. const float d_all = (float)dh[0];
  1025. scales16[0] = a[4];
  1026. scales16[1] = a[5];
  1027. aux32 = ((scales32 >> s_shift2) << 4) & 0x30303030;
  1028. scales16[0] = a[il+0];
  1029. scales16[1] = a[il+1];
  1030. scales32 = ((scales32 >> s_shift1) & 0x0f0f0f0f) | aux32;
  1031. float s1 = 0, s2 = 0, s3 = 0, s4 = 0, s5 = 0, s6 = 0;
  1032. for (int l = 0; l < n; l += 2) {
  1033. const int32_t qs = q[l/2];
  1034. s1 += yl[l+0] * (qs & qm[il/2][0]);
  1035. s2 += yl[l+1] * (qs & qm[il/2][1]);
  1036. s3 += ((h[l/2] & hm[0]) ? 0.f : yl[l+0]) + ((h[l/2] & hm[1]) ? 0.f : yl[l+1]);
  1037. s4 += yl[l+16] * (qs & qm[il/2][2]);
  1038. s5 += yl[l+17] * (qs & qm[il/2][3]);
  1039. s6 += ((h[l/2] & hm[2]) ? 0.f : yl[l+16]) + ((h[l/2] & hm[3]) ? 0.f : yl[l+17]);
  1040. }
  1041. float d1 = d_all * (s1 + 1.f/256.f * s2 - s3*v1);
  1042. float d2 = d_all * (s4 + 1.f/256.f * s5 - s6*v2);
  1043. sumf1[row] += d1 * (scales[0] - 32);
  1044. sumf2[row] += d2 * (scales[2] - 32);
  1045. s1 = s2 = s3 = s4 = s5 = s6 = 0;
  1046. for (int l = 0; l < n; l += 2) {
  1047. const int32_t qs = q[l/2+8];
  1048. s1 += yl[l+8] * (qs & qm[il/2][0]);
  1049. s2 += yl[l+9] * (qs & qm[il/2][1]);
  1050. s3 += ((h[l/2+8] & hm[0]) ? 0.f : yl[l+8]) + ((h[l/2+8] & hm[1]) ? 0.f : yl[l+9]);
  1051. s4 += yl[l+24] * (qs & qm[il/2][2]);
  1052. s5 += yl[l+25] * (qs & qm[il/2][3]);
  1053. s6 += ((h[l/2+8] & hm[2]) ? 0.f : yl[l+24]) + ((h[l/2+8] & hm[3]) ? 0.f : yl[l+25]);
  1054. }
  1055. d1 = d_all * (s1 + 1.f/256.f * s2 - s3*v1);
  1056. d2 = d_all * (s4 + 1.f/256.f * s5 - s6*v2);
  1057. sumf1[row] += d1 * (scales[1] - 32);
  1058. sumf2[row] += d2 * (scales[3] - 32);
  1059. q += step;
  1060. h += step;
  1061. a += step;
  1062. dh += step;
  1063. }
  1064. y1 += 4 * QK_K;
  1065. }
  1066. for (int row = 0; row < 2; ++row) {
  1067. const float sumf = (sumf1[row] + 0.25f * sumf2[row]) / (1 << shift);
  1068. sumf1[row] = simd_sum(sumf);
  1069. }
  1070. if (tiisg == 0) {
  1071. for (int row = 0; row < 2; ++row) {
  1072. dst[r1*ne0 + r2*ne0*ne1 + first_row + row] = sumf1[row];
  1073. }
  1074. }
  1075. }
  1076. #else
  1077. kernel void kernel_mul_mat_q3_K_f32(
  1078. device const void * src0,
  1079. device const float * src1,
  1080. device float * dst,
  1081. constant int64_t & ne00,
  1082. constant int64_t & ne01[[buffer(4)]],
  1083. constant int64_t & ne02[[buffer(5)]],
  1084. constant int64_t & ne10[[buffer(9)]],
  1085. constant int64_t & ne12[[buffer(11)]],
  1086. constant int64_t & ne0[[buffer(15)]],
  1087. constant int64_t & ne1[[buffer(16)]],
  1088. constant uint & gqa[[buffer(17)]],
  1089. uint3 tgpig[[threadgroup_position_in_grid]],
  1090. uint tiisg[[thread_index_in_simdgroup]],
  1091. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1092. const int nb = ne00/QK_K;
  1093. const int64_t r0 = tgpig.x;
  1094. const int64_t r1 = tgpig.y;
  1095. const int64_t r2 = tgpig.z;
  1096. const int row = 2 * r0 + sgitg;
  1097. const uint offset0 = r2/gqa*(nb*ne0);
  1098. device const block_q3_K * x = (device const block_q3_K *) src0 + row*nb + offset0;
  1099. device const float * yy = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  1100. const int ix = tiisg/4;
  1101. const int il = 4 * (tiisg%4);// 0, 4, 8, 12
  1102. const int im = il/8; // 0, 0, 1, 1
  1103. const int in = il%8; // 0, 4, 0, 4
  1104. float2 sum = {0.f, 0.f};
  1105. for (int i = ix; i < nb; i += 8) {
  1106. const float d_all = (float)(x[i].d);
  1107. device const uint16_t * q = (device const uint16_t *)(x[i].qs + il);
  1108. device const uint16_t * h = (device const uint16_t *)(x[i].hmask + in);
  1109. device const uint16_t * s = (device const uint16_t *)(x[i].scales);
  1110. device const float * y = yy + i * QK_K + il;
  1111. const float d1 = d_all * ((int32_t)(s[0] & 0x000F) - 8);
  1112. const float d2 = d_all * ((int32_t)(s[0] & 0x00F0) - 128) * 1.f/64.f;
  1113. const float d3 = d_all * ((int32_t)(s[0] & 0x0F00) - 2048) * 1.f/4096.f;
  1114. const float d4 = d_all * ((int32_t)(s[0] & 0xF000) - 32768) * 1.f/262144.f;
  1115. for (int l = 0; l < 4; l += 2) {
  1116. const uint16_t hm = h[l/2] >> im;
  1117. sum[0] += y[l+ 0] * d1 * ((int32_t)(q[l/2] & 0x0003) - ((hm & 0x0001) ? 0 : 4))
  1118. + y[l+16] * d2 * ((int32_t)(q[l/2] & 0x000c) - ((hm & 0x0004) ? 0 : 16))
  1119. + y[l+32] * d3 * ((int32_t)(q[l/2] & 0x0030) - ((hm & 0x0010) ? 0 : 64))
  1120. + y[l+48] * d4 * ((int32_t)(q[l/2] & 0x00c0) - ((hm & 0x0040) ? 0 : 256));
  1121. sum[1] += y[l+ 1] * d1 * ((int32_t)(q[l/2] & 0x0300) - ((hm & 0x0100) ? 0 : 1024))
  1122. + y[l+17] * d2 * ((int32_t)(q[l/2] & 0x0c00) - ((hm & 0x0400) ? 0 : 4096))
  1123. + y[l+33] * d3 * ((int32_t)(q[l/2] & 0x3000) - ((hm & 0x1000) ? 0 : 16384))
  1124. + y[l+49] * d4 * ((int32_t)(q[l/2] & 0xc000) - ((hm & 0x4000) ? 0 : 65536));
  1125. }
  1126. }
  1127. const float sumf = sum[0] + sum[1] * 1.f/256.f;
  1128. const float tot = simd_sum(sumf);
  1129. if (tiisg == 0) {
  1130. dst[r1*ne0 + r2*ne0*ne1 + row] = tot;
  1131. }
  1132. }
  1133. #endif
  1134. #if QK_K == 256
  1135. kernel void kernel_mul_mat_q4_K_f32(
  1136. device const void * src0,
  1137. device const float * src1,
  1138. device float * dst,
  1139. constant int64_t & ne00,
  1140. constant int64_t & ne01[[buffer(4)]],
  1141. constant int64_t & ne02[[buffer(5)]],
  1142. constant int64_t & ne10[[buffer(9)]],
  1143. constant int64_t & ne12[[buffer(11)]],
  1144. constant int64_t & ne0[[buffer(15)]],
  1145. constant int64_t & ne1[[buffer(16)]],
  1146. constant uint & gqa[[buffer(17)]],
  1147. uint3 tgpig[[threadgroup_position_in_grid]],
  1148. uint tiisg[[thread_index_in_simdgroup]],
  1149. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1150. const uint16_t kmask1 = 0x3f3f;
  1151. const uint16_t kmask2 = 0x0f0f;
  1152. const uint16_t kmask3 = 0xc0c0;
  1153. const int ix = tiisg/8; // 0...3
  1154. const int it = tiisg%8; // 0...7
  1155. const int im = it/4; // 0 or 1
  1156. const int ir = it%4; // 0...3
  1157. const int nb = ne00/QK_K;
  1158. const int r0 = tgpig.x;
  1159. const int r1 = tgpig.y;
  1160. const int r2 = tgpig.z;
  1161. //const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  1162. const int first_row = r0 * N_DST;
  1163. const int ib_row = first_row * nb;
  1164. const uint offset0 = r2/gqa*(nb*ne0);
  1165. device const block_q4_K * x = (device const block_q4_K *) src0 + ib_row + offset0;
  1166. device const float * y = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  1167. float yl[16];
  1168. float yh[16];
  1169. float sumf[N_DST]={0.f}, all_sum;
  1170. const int step = sizeof(block_q4_K) * nb / 2;
  1171. device const float * y4 = y + ix * QK_K + 64 * im + 8 * ir;
  1172. uint16_t sc16[4];
  1173. thread const uint8_t * sc8 = (thread const uint8_t *)sc16;
  1174. for (int ib = ix; ib < nb; ib += 4) {
  1175. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  1176. for (int i = 0; i < 8; ++i) {
  1177. yl[i+0] = y4[i+ 0]; sumy[0] += yl[i+0];
  1178. yl[i+8] = y4[i+ 32]; sumy[1] += yl[i+8];
  1179. yh[i+0] = y4[i+128]; sumy[2] += yh[i+0];
  1180. yh[i+8] = y4[i+160]; sumy[3] += yh[i+8];
  1181. }
  1182. device const uint16_t * sc = (device const uint16_t *)x[ib].scales + im;
  1183. device const uint16_t * q1 = (device const uint16_t *)x[ib].qs + 16 * im + 4 * ir;
  1184. device const half * dh = &x[ib].d;
  1185. for (int row = 0; row < N_DST; row++) {
  1186. sc16[0] = sc[0] & kmask1;
  1187. sc16[1] = sc[2] & kmask1;
  1188. sc16[2] = ((sc[4] >> 0) & kmask2) | ((sc[0] & kmask3) >> 2);
  1189. sc16[3] = ((sc[4] >> 4) & kmask2) | ((sc[2] & kmask3) >> 2);
  1190. device const uint16_t * q2 = q1 + 32;
  1191. float4 acc1 = {0.f, 0.f, 0.f, 0.f};
  1192. float4 acc2 = {0.f, 0.f, 0.f, 0.f};
  1193. for (int i = 0; i < 8; i += 2) {
  1194. acc1[0] += yl[i+0] * (q1[i/2] & 0x000F);
  1195. acc1[1] += yl[i+1] * (q1[i/2] & 0x0F00);
  1196. acc1[2] += yl[i+8] * (q1[i/2] & 0x00F0);
  1197. acc1[3] += yl[i+9] * (q1[i/2] & 0xF000);
  1198. acc2[0] += yh[i+0] * (q2[i/2] & 0x000F);
  1199. acc2[1] += yh[i+1] * (q2[i/2] & 0x0F00);
  1200. acc2[2] += yh[i+8] * (q2[i/2] & 0x00F0);
  1201. acc2[3] += yh[i+9] * (q2[i/2] & 0xF000);
  1202. }
  1203. float dall = dh[0];
  1204. float dmin = dh[1];
  1205. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc1[1]) * sc8[0] +
  1206. (acc1[2] + 1.f/256.f * acc1[3]) * sc8[1] * 1.f/16.f +
  1207. (acc2[0] + 1.f/256.f * acc2[1]) * sc8[4] +
  1208. (acc2[2] + 1.f/256.f * acc2[3]) * sc8[5] * 1.f/16.f) -
  1209. dmin * (sumy[0] * sc8[2] + sumy[1] * sc8[3] + sumy[2] * sc8[6] + sumy[3] * sc8[7]);
  1210. q1 += step;
  1211. sc += step;
  1212. dh += step;
  1213. }
  1214. y4 += 4 * QK_K;
  1215. }
  1216. for (int row = 0; row < N_DST; ++row) {
  1217. all_sum = simd_sum(sumf[row]);
  1218. if (tiisg == 0) {
  1219. dst[r1*ne0 + r2*ne0*ne1 + first_row + row] = all_sum;
  1220. }
  1221. }
  1222. }
  1223. #else
  1224. kernel void kernel_mul_mat_q4_K_f32(
  1225. device const void * src0,
  1226. device const float * src1,
  1227. device float * dst,
  1228. constant int64_t & ne00,
  1229. constant int64_t & ne01[[buffer(4)]],
  1230. constant int64_t & ne02[[buffer(5)]],
  1231. constant int64_t & ne10[[buffer(9)]],
  1232. constant int64_t & ne12[[buffer(11)]],
  1233. constant int64_t & ne0[[buffer(15)]],
  1234. constant int64_t & ne1[[buffer(16)]],
  1235. constant uint & gqa[[buffer(17)]],
  1236. uint3 tgpig[[threadgroup_position_in_grid]],
  1237. uint tiisg[[thread_index_in_simdgroup]],
  1238. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1239. const int ix = tiisg/4; // 0...7
  1240. const int it = tiisg%4; // 0...3
  1241. const int nb = ne00/QK_K;
  1242. const int r0 = tgpig.x;
  1243. const int r1 = tgpig.y;
  1244. const int r2 = tgpig.z;
  1245. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  1246. const int ib_row = first_row * nb;
  1247. const uint offset0 = r2/gqa*(nb*ne0);
  1248. device const block_q4_K * x = (device const block_q4_K *) src0 + ib_row + offset0;
  1249. device const float * y = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  1250. float yl[8];
  1251. float yh[8];
  1252. float sumf[N_DST]={0.f}, all_sum;
  1253. const int step = sizeof(block_q4_K) * nb / 2;
  1254. device const float * y4 = y + ix * QK_K + 8 * it;
  1255. uint16_t sc16[4];
  1256. for (int ib = ix; ib < nb; ib += 8) {
  1257. float2 sumy = {0.f, 0.f};
  1258. for (int i = 0; i < 8; ++i) {
  1259. yl[i] = y4[i+ 0]; sumy[0] += yl[i];
  1260. yh[i] = y4[i+32]; sumy[1] += yh[i];
  1261. }
  1262. device const uint16_t * sc = (device const uint16_t *)x[ib].scales;
  1263. device const uint16_t * qs = (device const uint16_t *)x[ib].qs + 4 * it;
  1264. device const half * dh = x[ib].d;
  1265. for (int row = 0; row < N_DST; row++) {
  1266. sc16[0] = sc[0] & 0x000f;
  1267. sc16[1] = sc[0] & 0x0f00;
  1268. sc16[2] = sc[0] & 0x00f0;
  1269. sc16[3] = sc[0] & 0xf000;
  1270. float2 acc1 = {0.f, 0.f};
  1271. float2 acc2 = {0.f, 0.f};
  1272. for (int i = 0; i < 8; i += 2) {
  1273. acc1[0] += yl[i+0] * (qs[i/2] & 0x000F);
  1274. acc1[1] += yl[i+1] * (qs[i/2] & 0x0F00);
  1275. acc2[0] += yh[i+0] * (qs[i/2] & 0x00F0);
  1276. acc2[1] += yh[i+1] * (qs[i/2] & 0xF000);
  1277. }
  1278. float dall = dh[0];
  1279. float dmin = dh[1];
  1280. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc1[1]) * sc16[0] +
  1281. (acc2[0] + 1.f/256.f * acc2[1]) * sc16[1] * 1.f/4096.f) -
  1282. dmin * 1.f/16.f * (sumy[0] * sc16[2] + sumy[1] * sc16[3] * 1.f/256.f);
  1283. qs += step;
  1284. sc += step;
  1285. dh += step;
  1286. }
  1287. y4 += 8 * QK_K;
  1288. }
  1289. for (int row = 0; row < N_DST; ++row) {
  1290. all_sum = simd_sum(sumf[row]);
  1291. if (tiisg == 0) {
  1292. dst[r1*ne0+ r2*ne0*ne1 + first_row + row] = all_sum;
  1293. }
  1294. }
  1295. }
  1296. #endif
  1297. kernel void kernel_mul_mat_q5_K_f32(
  1298. device const void * src0,
  1299. device const float * src1,
  1300. device float * dst,
  1301. constant int64_t & ne00,
  1302. constant int64_t & ne01[[buffer(4)]],
  1303. constant int64_t & ne02[[buffer(5)]],
  1304. constant int64_t & ne10[[buffer(9)]],
  1305. constant int64_t & ne12[[buffer(11)]],
  1306. constant int64_t & ne0[[buffer(15)]],
  1307. constant int64_t & ne1[[buffer(16)]],
  1308. constant uint & gqa[[buffer(17)]],
  1309. uint3 tgpig[[threadgroup_position_in_grid]],
  1310. uint tiisg[[thread_index_in_simdgroup]],
  1311. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1312. const int nb = ne00/QK_K;
  1313. const int64_t r0 = tgpig.x;
  1314. const int64_t r1 = tgpig.y;
  1315. const int r2 = tgpig.z;
  1316. const int first_row = (r0 * N_SIMDGROUP + sgitg) * 2;
  1317. const uint offset0 = r2/gqa*(nb*ne0);
  1318. device const block_q5_K * x = (device const block_q5_K *) src0 + first_row*nb + offset0;
  1319. device const float * yy = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  1320. float sumf[2]={0.f};
  1321. const int step = sizeof(block_q5_K) * nb;
  1322. #if QK_K == 256
  1323. #
  1324. float yl[16], yh[16];
  1325. const uint16_t kmask1 = 0x3f3f;
  1326. const uint16_t kmask2 = 0x0f0f;
  1327. const uint16_t kmask3 = 0xc0c0;
  1328. const int tid = tiisg/4;
  1329. const int ix = tiisg%4;
  1330. const int im = tid/4;
  1331. const int ir = tid%4;
  1332. const int n = 8;
  1333. const int l0 = n*ir;
  1334. const int q_offset = 32*im + l0;
  1335. const int y_offset = 64*im + l0;
  1336. const uint8_t hm1 = 1u << (2*im);
  1337. const uint8_t hm2 = hm1 << 1;
  1338. const uint8_t hm3 = hm1 << 4;
  1339. const uint8_t hm4 = hm2 << 4;
  1340. uint16_t sc16[4];
  1341. thread const uint8_t * sc8 = (thread const uint8_t *)sc16;
  1342. device const float * y1 = yy + ix*QK_K + y_offset;
  1343. for (int i = ix; i < nb; i += 4) {
  1344. device const uint8_t * q1 = x[i].qs + q_offset;
  1345. device const uint8_t * qh = x[i].qh + l0;
  1346. device const half * dh = &x[i].d;
  1347. device const uint16_t * a = (device const uint16_t *)x[i].scales + im;
  1348. device const float * y2 = y1 + 128;
  1349. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  1350. for (int l = 0; l < 8; ++l) {
  1351. yl[l+0] = y1[l+ 0]; sumy[0] += yl[l+0];
  1352. yl[l+8] = y1[l+32]; sumy[1] += yl[l+8];
  1353. yh[l+0] = y2[l+ 0]; sumy[2] += yh[l+0];
  1354. yh[l+8] = y2[l+32]; sumy[3] += yh[l+8];
  1355. }
  1356. for (int row = 0; row < 2; ++row) {
  1357. device const uint8_t * q2 = q1 + 64;
  1358. sc16[0] = a[0] & kmask1;
  1359. sc16[1] = a[2] & kmask1;
  1360. sc16[2] = ((a[4] >> 0) & kmask2) | ((a[0] & kmask3) >> 2);
  1361. sc16[3] = ((a[4] >> 4) & kmask2) | ((a[2] & kmask3) >> 2);
  1362. float4 acc1 = {0.f};
  1363. float4 acc2 = {0.f};
  1364. for (int l = 0; l < n; ++l) {
  1365. uint8_t h = qh[l];
  1366. acc1[0] += yl[l+0] * (q1[l] & 0x0F);
  1367. acc1[1] += yl[l+8] * (q1[l] & 0xF0);
  1368. acc1[2] += yh[l+0] * (q2[l] & 0x0F);
  1369. acc1[3] += yh[l+8] * (q2[l] & 0xF0);
  1370. acc2[0] += h & hm1 ? yl[l+0] : 0.f;
  1371. acc2[1] += h & hm2 ? yl[l+8] : 0.f;
  1372. acc2[2] += h & hm3 ? yh[l+0] : 0.f;
  1373. acc2[3] += h & hm4 ? yh[l+8] : 0.f;
  1374. }
  1375. const float dall = dh[0];
  1376. const float dmin = dh[1];
  1377. sumf[row] += dall * (sc8[0] * (acc1[0] + 16.f*acc2[0]) +
  1378. sc8[1] * (acc1[1]/16.f + 16.f*acc2[1]) +
  1379. sc8[4] * (acc1[2] + 16.f*acc2[2]) +
  1380. sc8[5] * (acc1[3]/16.f + 16.f*acc2[3])) -
  1381. dmin * (sumy[0] * sc8[2] + sumy[1] * sc8[3] + sumy[2] * sc8[6] + sumy[3] * sc8[7]);
  1382. q1 += step;
  1383. qh += step;
  1384. dh += step/2;
  1385. a += step/2;
  1386. }
  1387. y1 += 4 * QK_K;
  1388. }
  1389. #else
  1390. float yl[8], yh[8];
  1391. const int il = 4 * (tiisg/8); // 0, 4, 8, 12
  1392. const int ix = tiisg%8;
  1393. const int im = il/8; // 0, 0, 1, 1
  1394. const int in = il%8; // 0, 4, 0, 4
  1395. device const float * y = yy + ix*QK_K + il;
  1396. for (int i = ix; i < nb; i += 8) {
  1397. for (int l = 0; l < 4; ++l) {
  1398. yl[l+0] = y[l+ 0];
  1399. yl[l+4] = y[l+16];
  1400. yh[l+0] = y[l+32];
  1401. yh[l+4] = y[l+48];
  1402. }
  1403. device const half * dh = &x[i].d;
  1404. device const uint8_t * q = x[i].qs + il;
  1405. device const uint8_t * h = x[i].qh + in;
  1406. device const int8_t * s = x[i].scales;
  1407. for (int row = 0; row < 2; ++row) {
  1408. const float d = dh[0];
  1409. float2 acc = {0.f, 0.f};
  1410. for (int l = 0; l < 4; ++l) {
  1411. const uint8_t hl = h[l] >> im;
  1412. acc[0] += yl[l+0] * s[0] * ((int16_t)(q[l+ 0] & 0x0F) - (hl & 0x01 ? 0 : 16))
  1413. + yl[l+4] * s[1] * ((int16_t)(q[l+16] & 0x0F) - (hl & 0x04 ? 0 : 16));
  1414. acc[1] += yh[l+0] * s[2] * ((int16_t)(q[l+ 0] & 0xF0) - (hl & 0x10 ? 0 : 256))
  1415. + yh[l+4] * s[3] * ((int16_t)(q[l+16] & 0xF0) - (hl & 0x40 ? 0 : 256));
  1416. }
  1417. sumf[row] += d * (acc[0] + 1.f/16.f * acc[1]);
  1418. q += step;
  1419. h += step;
  1420. s += step;
  1421. dh += step/2;
  1422. }
  1423. y += 8 * QK_K;
  1424. }
  1425. #endif
  1426. for (int row = 0; row < 2; ++row) {
  1427. const float tot = simd_sum(sumf[row]);
  1428. if (tiisg == 0) {
  1429. dst[r1*ne0 + r2*ne0*ne1 + first_row + row] = tot;
  1430. }
  1431. }
  1432. }
  1433. kernel void kernel_mul_mat_q6_K_f32(
  1434. device const void * src0,
  1435. device const float * src1,
  1436. device float * dst,
  1437. constant int64_t & ne00,
  1438. constant int64_t & ne01[[buffer(4)]],
  1439. constant int64_t & ne02[[buffer(5)]],
  1440. constant int64_t & ne10[[buffer(9)]],
  1441. constant int64_t & ne12[[buffer(11)]],
  1442. constant int64_t & ne0[[buffer(15)]],
  1443. constant int64_t & ne1[[buffer(16)]],
  1444. constant uint & gqa[[buffer(17)]],
  1445. uint3 tgpig[[threadgroup_position_in_grid]],
  1446. uint tiisg[[thread_index_in_simdgroup]],
  1447. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1448. const uint8_t kmask1 = 0x03;
  1449. const uint8_t kmask2 = 0x0C;
  1450. const uint8_t kmask3 = 0x30;
  1451. const uint8_t kmask4 = 0xC0;
  1452. const int nb = ne00/QK_K;
  1453. const int64_t r0 = tgpig.x;
  1454. const int64_t r1 = tgpig.y;
  1455. const int r2 = tgpig.z;
  1456. const int row = 2 * r0 + sgitg;
  1457. const uint offset0 = r2/gqa*(nb*ne0);
  1458. device const block_q6_K * x = (device const block_q6_K *) src0 + row * nb + offset0;
  1459. device const float * yy = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  1460. float sumf = 0;
  1461. #if QK_K == 256
  1462. const int tid = tiisg/2;
  1463. const int ix = tiisg%2;
  1464. const int ip = tid/8; // 0 or 1
  1465. const int il = tid%8;
  1466. const int n = 4;
  1467. const int l0 = n*il;
  1468. const int is = 8*ip + l0/16;
  1469. const int y_offset = 128*ip + l0;
  1470. const int q_offset_l = 64*ip + l0;
  1471. const int q_offset_h = 32*ip + l0;
  1472. for (int i = ix; i < nb; i += 2) {
  1473. device const uint8_t * q1 = x[i].ql + q_offset_l;
  1474. device const uint8_t * q2 = q1 + 32;
  1475. device const uint8_t * qh = x[i].qh + q_offset_h;
  1476. device const int8_t * sc = x[i].scales + is;
  1477. device const float * y = yy + i * QK_K + y_offset;
  1478. const float dall = x[i].d;
  1479. float4 sums = {0.f, 0.f, 0.f, 0.f};
  1480. for (int l = 0; l < n; ++l) {
  1481. sums[0] += y[l+ 0] * ((int8_t)((q1[l] & 0xF) | ((qh[l] & kmask1) << 4)) - 32);
  1482. sums[1] += y[l+32] * ((int8_t)((q2[l] & 0xF) | ((qh[l] & kmask2) << 2)) - 32);
  1483. sums[2] += y[l+64] * ((int8_t)((q1[l] >> 4) | ((qh[l] & kmask3) << 0)) - 32);
  1484. sums[3] += y[l+96] * ((int8_t)((q2[l] >> 4) | ((qh[l] & kmask4) >> 2)) - 32);
  1485. }
  1486. sumf += dall * (sums[0] * sc[0] + sums[1] * sc[2] + sums[2] * sc[4] + sums[3] * sc[6]);
  1487. }
  1488. #else
  1489. const int ix = tiisg/4;
  1490. const int il = 4*(tiisg%4);
  1491. for (int i = ix; i < nb; i += 8) {
  1492. device const float * y = yy + i * QK_K + il;
  1493. device const uint8_t * ql = x[i].ql + il;
  1494. device const uint8_t * qh = x[i].qh + il;
  1495. device const int8_t * s = x[i].scales;
  1496. const float d = x[i].d;
  1497. float4 sums = {0.f, 0.f, 0.f, 0.f};
  1498. for (int l = 0; l < 4; ++l) {
  1499. sums[0] += y[l+ 0] * ((int8_t)((ql[l+ 0] & 0xF) | ((qh[l] & kmask1) << 4)) - 32);
  1500. sums[1] += y[l+16] * ((int8_t)((ql[l+16] & 0xF) | ((qh[l] & kmask2) << 2)) - 32);
  1501. sums[2] += y[l+32] * ((int8_t)((ql[l+ 0] >> 4) | ((qh[l] & kmask3) >> 0)) - 32);
  1502. sums[3] += y[l+48] * ((int8_t)((ql[l+16] >> 4) | ((qh[l] & kmask4) >> 2)) - 32);
  1503. }
  1504. sumf += d * (sums[0] * s[0] + sums[1] * s[1] + sums[2] * s[2] + sums[3] * s[3]);
  1505. }
  1506. #endif
  1507. const float tot = simd_sum(sumf);
  1508. if (tiisg == 0) {
  1509. dst[r1*ne0 + r2*ne0*ne1 + row] = tot;
  1510. }
  1511. }
  1512. //============================= templates and their specializations =============================
  1513. template <typename type4x4>
  1514. void dequantize_f16(device const half4x4 * src, short il, thread type4x4 & reg) {
  1515. half4x4 temp = *(((device half4x4 *)src));
  1516. for (int i = 0; i < 16; i++){
  1517. reg[i/4][i%4] = temp[i/4][i%4];
  1518. }
  1519. }
  1520. template <typename type4x4>
  1521. void dequantize_q4_0(device const block_q4_0 *xb, short il, thread type4x4 & reg) {
  1522. device const uint16_t * qs = ((device const uint16_t *)xb + 1);
  1523. const half d = il ? (xb->d / 16.h) : xb->d;
  1524. const half m = il ? ( -8.h * 16.h) : -8.h;
  1525. const ushort mask0 = il ? 0x00F0 : 0x000F;
  1526. const ushort mask1 = il ? 0xF000 : 0x0F00;
  1527. for (int i=0;i<8;i++) {
  1528. reg[i/2][2*(i%2)] = (((qs[i] & mask0) ) + m) * d;
  1529. reg[i/2][2*(i%2)+1] = (((qs[i] & mask1) >> 8) + m) * d;
  1530. }
  1531. }
  1532. template <typename type4x4>
  1533. void dequantize_q4_1(device const block_q4_1 *xb, short il, thread type4x4 & reg) {
  1534. device const uint16_t * qs = ((device const uint16_t *)xb + 2);
  1535. const half d = il ? (xb->d / 16.h) : xb->d;
  1536. const half m = xb->m;
  1537. const ushort mask0 = il ? 0x00F0 : 0x000F;
  1538. const ushort mask1 = il ? 0xF000 : 0x0F00;
  1539. for (int i=0;i<8;i++) {
  1540. reg[i/2][2*(i%2)] = (((qs[i] & mask0) ) * d) + m;
  1541. reg[i/2][2*(i%2)+1] = (((qs[i] & mask1) >> 8) * d) + m;
  1542. }
  1543. }
  1544. template <typename type4x4>
  1545. void dequantize_q8_0(device const block_q8_0 *xb, short il, thread type4x4 & reg) {
  1546. device const int8_t * qs = ((device const int8_t *)xb->qs);
  1547. const half d = xb->d;
  1548. for (int i=0;i<16;i++) {
  1549. reg[i/4][i%4] = (qs[i + 16*il] * d);
  1550. }
  1551. }
  1552. template <typename type4x4>
  1553. void dequantize_q2_K(device const block_q2_K *xb, short il, thread type4x4 & reg) {
  1554. const half d = xb->d;
  1555. const half min = xb->dmin;
  1556. device const uint8_t * q = (device const uint8_t *)xb->qs;
  1557. half dl, ml;
  1558. uint8_t sc = xb->scales[il];
  1559. #if QK_K == 256
  1560. q = q + 32*(il/8) + 16*(il&1);
  1561. il = (il/2)%4;
  1562. #endif
  1563. half coef = il>1 ? (il>2 ? 1/64.h : 1/16.h) : (il>0 ? 1/4.h : 1.h);
  1564. uchar mask = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  1565. dl = d * (sc & 0xF) * coef, ml = min * (sc >> 4);
  1566. for (int i = 0; i < 16; ++i) {
  1567. reg[i/4][i%4] = dl * (q[i] & mask) - ml;
  1568. }
  1569. }
  1570. template <typename type4x4>
  1571. void dequantize_q3_K(device const block_q3_K *xb, short il, thread type4x4 & reg) {
  1572. const float d_all = (float)(xb->d);
  1573. device const uint8_t * q = (device const uint8_t *)xb->qs;
  1574. device const uint8_t * h = (device const uint8_t *)xb->hmask;
  1575. device const int8_t * scales = (device const int8_t *)xb->scales;
  1576. #if QK_K == 256
  1577. q = q + 32 * (il/8) + 16 * (il&1);
  1578. h = h + 16 * (il&1);
  1579. uint8_t m = 1 << (il/2);
  1580. uint16_t kmask1 = (il/4)>1 ? ((il/4)>2 ? 192 : 48) : \
  1581. ((il/4)>0 ? 12 : 3);
  1582. uint16_t kmask2 = il/8 ? 0xF0 : 0x0F;
  1583. uint16_t scale_2 = scales[il%8], scale_1 = scales[8 + il%4];
  1584. int16_t dl_int = (il/4)&1 ? (scale_2&kmask2) | ((scale_1&kmask1) << 2) : \
  1585. (scale_2&kmask2) | ((scale_1&kmask1) << 4);
  1586. float dl = il<8 ? d_all * (dl_int - 32.f) : d_all * (dl_int / 16.f - 32.f);
  1587. il = (il/2)%4;
  1588. float coef = il>1 ? (il>2 ? 1/64.h : 1/16.h) : (il>0 ? 1/4.h : 1.h);
  1589. uint8_t mask = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  1590. for (int i = 0; i < 16; ++i) {
  1591. reg[i/4][i%4] = coef * dl * ((q[i] & mask) - ((h[i] & m) ? 0 : 4.f/coef));
  1592. }
  1593. #else
  1594. float kcoef = il&1 ? 1.f/16.f : 1.f;
  1595. uint16_t kmask = il&1 ? 0xF0 : 0x0F;
  1596. float dl = d_all * ((scales[il/2] & kmask) * kcoef - 8);
  1597. float coef = il>1 ? (il>2 ? 1/64.h : 1/16.h) : (il>0 ? 1/4.h : 1.h);
  1598. uint8_t mask = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  1599. uint8_t m = 1<<(il*2);
  1600. for (int i = 0; i < 16; ++i) {
  1601. reg[i/4][i%4] = coef * dl * ((q[i] & mask) - ((h[i%8] & (m * (1 + i/8))) ? 0 : 4.f/coef));
  1602. }
  1603. #endif
  1604. }
  1605. template <typename type4x4>
  1606. void dequantize_q4_K(device const block_q4_K *xb, short il, thread type4x4 & reg) {
  1607. device const uint8_t * q = xb->qs;
  1608. #if QK_K == 256
  1609. const float d = (float)(xb->d);
  1610. const float min = (float)(xb->dmin);
  1611. short is = (il/4) * 2;
  1612. q = q + (il/4) * 32 + 16 * (il&1);
  1613. il = il%4;
  1614. const uchar4 sc = get_scale_min_k4(is, xb->scales);
  1615. const float dl = il<2 ? d * sc[0] : d * sc[2]/16.h;
  1616. const float ml = il<2 ? min * sc[1] : min * sc[3];
  1617. #else
  1618. q = q + 16 * (il&1);
  1619. device const uint8_t * s = xb->scales;
  1620. device const half2 * dh = (device const half2 *)xb->d;
  1621. const float2 d = (float2)dh[0];
  1622. const float dl = il<2 ? d[0] * (s[0]&0xF) : d[0] * (s[1]&0xF)/16.h;
  1623. const float ml = il<2 ? d[1] * (s[0]>>4) : d[1 ]* (s[1]>>4);
  1624. #endif
  1625. const ushort mask = il<2 ? 0x0F : 0xF0;
  1626. for (int i = 0; i < 16; ++i) {
  1627. reg[i/4][i%4] = dl * (q[i] & mask) - ml;
  1628. }
  1629. }
  1630. template <typename type4x4>
  1631. void dequantize_q5_K(device const block_q5_K *xb, short il, thread type4x4 & reg) {
  1632. device const uint8_t * q = xb->qs;
  1633. device const uint8_t * qh = xb->qh;
  1634. #if QK_K == 256
  1635. const float d = (float)(xb->d);
  1636. const float min = (float)(xb->dmin);
  1637. short is = (il/4) * 2;
  1638. q = q + 32 * (il/4) + 16 * (il&1);
  1639. qh = qh + 16 * (il&1);
  1640. uint8_t ul = 1 << (il/2);
  1641. il = il%4;
  1642. const uchar4 sc = get_scale_min_k4(is, xb->scales);
  1643. const float dl = il<2 ? d * sc[0] : d * sc[2]/16.h;
  1644. const float ml = il<2 ? min * sc[1] : min * sc[3];
  1645. const ushort mask = il<2 ? 0x0F : 0xF0;
  1646. const float qh_val = il<2 ? 16.f : 256.f;
  1647. for (int i = 0; i < 16; ++i) {
  1648. reg[i/4][i%4] = dl * ((q[i] & mask) + (qh[i] & ul ? qh_val : 0)) - ml;
  1649. }
  1650. #else
  1651. q = q + 16 * (il&1);
  1652. device const int8_t * s = xb->scales;
  1653. const float dl = xb->d * s[il];
  1654. uint8_t m = 1<<(il*2);
  1655. const float coef = il<2 ? 1.f : 1.f/16.f;
  1656. const ushort mask = il<2 ? 0x0F : 0xF0;
  1657. for (int i = 0; i < 16; ++i) {
  1658. reg[i/4][i%4] = coef * dl * ((q[i] & mask) - (qh[i%8] & (m*(1+i/8)) ? 0.f : 16.f/coef));
  1659. }
  1660. #endif
  1661. }
  1662. template <typename type4x4>
  1663. void dequantize_q6_K(device const block_q6_K *xb, short il, thread type4x4 & reg) {
  1664. const float d_all = (float)(xb->d);
  1665. device const uint8_t * ql = (device const uint8_t *)xb->ql;
  1666. device const uint8_t * qh = (device const uint8_t *)xb->qh;
  1667. device const int8_t * scales = (device const int8_t *)xb->scales;
  1668. #if QK_K == 256
  1669. ql = ql + 64*(il/8) + 32*((il/2)&1) + 16*(il&1);
  1670. qh = qh + 32*(il/8) + 16*(il&1);
  1671. float sc = scales[(il%2) + 2 * ((il/2))];
  1672. il = (il/2)%4;
  1673. #else
  1674. ql = ql + 16 * (il&1);
  1675. float sc = scales[il];
  1676. #endif
  1677. for (int i = 0; i < 16; ++i) {
  1678. uint16_t kmask1 = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  1679. uint16_t kmask2 = il>1 ? 0xF0 : 0x0F;
  1680. const float coef = il>1 ? 1.f/16.f : 1.f;
  1681. float q = il&1 ? ((ql[i]&kmask2)|((qh[i]&kmask1)<<2)) - 32.f/coef : \
  1682. ((ql[i]&kmask2)|((qh[i]&kmask1)<<4)) - 32.f/coef;
  1683. reg[i/4][i%4] = d_all * sc * q * coef;
  1684. }
  1685. }
  1686. template<typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread float4x4 &)>
  1687. kernel void kernel_get_rows(
  1688. device const void * src0,
  1689. device const int * src1,
  1690. device float * dst,
  1691. constant int64_t & ne00,
  1692. constant uint64_t & nb01,
  1693. constant uint64_t & nb1,
  1694. uint tgpig[[threadgroup_position_in_grid]],
  1695. uint tiitg[[thread_index_in_threadgroup]],
  1696. uint tptg[[threads_per_threadgroup]]) {
  1697. const int i = tgpig;
  1698. const int r = ((device int32_t *) src1)[i];
  1699. for (int ind = tiitg; ind < ne00/16; ind += tptg) {
  1700. float4x4 temp;
  1701. dequantize_func(
  1702. ((device const block_q *) ((device char *) src0 + r*nb01)) + ind/nl, ind%nl, temp);
  1703. *(((device float4x4 *) ((device char *) dst + i*nb1)) + ind) = temp;
  1704. }
  1705. }
  1706. #define BLOCK_SIZE_M 64 // 8 simdgroup matrices from matrix A
  1707. #define BLOCK_SIZE_N 32 // 4 simdgroup matrices from matrix A
  1708. #define BLOCK_SIZE_K 32
  1709. #define THREAD_MAT_M 4 // each thread take 4 simdgroup matrices from matrix A
  1710. #define THREAD_MAT_N 2 // each thread take 2 simdgroup matrices from matrix B
  1711. #define THREAD_PER_BLOCK 128
  1712. #define THREAD_PER_ROW 2 // 2 thread for each row in matrix A to load numbers
  1713. #define THREAD_PER_COL 4 // 4 thread for each row in matrix B to load numbers
  1714. #define SG_MAT_SIZE 64 // simdgroup matrix is of shape 8x8
  1715. #define SG_MAT_ROW 8
  1716. // each block_q contains 16*nl weights
  1717. template<typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread half4x4 &)>
  1718. kernel void kernel_mul_mm(device const uchar * src0,
  1719. device const float * src1,
  1720. device float * dst,
  1721. constant int64_t & ne00,
  1722. constant int64_t & ne02,
  1723. constant int64_t & nb01,
  1724. constant int64_t & nb02,
  1725. constant int64_t & ne12,
  1726. constant int64_t & ne0,
  1727. constant int64_t & ne1,
  1728. constant uint & gqa,
  1729. threadgroup uchar * shared_memory [[threadgroup(0)]],
  1730. uint3 tgpig[[threadgroup_position_in_grid]],
  1731. uint tiitg[[thread_index_in_threadgroup]],
  1732. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1733. threadgroup half * sa = ((threadgroup half *)shared_memory);
  1734. threadgroup float * sb = (threadgroup float *)(shared_memory + 4096);
  1735. const uint r0 = tgpig.y;
  1736. const uint r1 = tgpig.x;
  1737. const uint im = tgpig.z;
  1738. // if this block is of 64x32 shape or smaller
  1739. short n_rows = (ne0 - r0 * BLOCK_SIZE_M < BLOCK_SIZE_M) ? (ne0 - r0 * BLOCK_SIZE_M) : BLOCK_SIZE_M;
  1740. short n_cols = (ne1 - r1 * BLOCK_SIZE_N < BLOCK_SIZE_N) ? (ne1 - r1 * BLOCK_SIZE_N) : BLOCK_SIZE_N;
  1741. // a thread shouldn't load data outside of the matrix
  1742. short thread_row = ((short)tiitg/THREAD_PER_ROW) < n_rows ? ((short)tiitg/THREAD_PER_ROW) : n_rows - 1;
  1743. short thread_col = ((short)tiitg/THREAD_PER_COL) < n_cols ? ((short)tiitg/THREAD_PER_COL) : n_cols - 1;
  1744. simdgroup_half8x8 ma[4];
  1745. simdgroup_float8x8 mb[2];
  1746. simdgroup_float8x8 c_res[8];
  1747. for (int i = 0; i < 8; i++){
  1748. c_res[i] = make_filled_simdgroup_matrix<float, 8>(0.f);
  1749. }
  1750. short il = (tiitg % THREAD_PER_ROW);
  1751. uint offset0 = im/gqa*nb02; ushort offset1 = il/nl;
  1752. device const block_q * x = (device const block_q *)(src0 + (r0 * BLOCK_SIZE_M + thread_row) * nb01 + offset0) + offset1;
  1753. device const float * y = src1 + (r1 * BLOCK_SIZE_N + thread_col) * ne00 \
  1754. + BLOCK_SIZE_K / THREAD_PER_COL * (tiitg % THREAD_PER_COL) + im * ne00 * ne1;
  1755. for (int loop_k = 0; loop_k < ne00; loop_k += BLOCK_SIZE_K) {
  1756. //load data and store to threadgroup memory
  1757. half4x4 temp_a;
  1758. dequantize_func(x, il, temp_a);
  1759. threadgroup_barrier(mem_flags::mem_threadgroup);
  1760. #pragma unroll(16)
  1761. for (int i = 0; i < 16; i++) {
  1762. *(sa + SG_MAT_SIZE * ((tiitg / THREAD_PER_ROW / 8) \
  1763. + 16 * (tiitg % THREAD_PER_ROW) + 8 * (i / 8)) \
  1764. + (tiitg / THREAD_PER_ROW) % 8 + (i & 7) * 8) = temp_a[i/4][i%4];
  1765. }
  1766. *(threadgroup float2x4 *)(sb + (tiitg % THREAD_PER_COL) * 8 * 32 + 8 * (tiitg / THREAD_PER_COL)) \
  1767. = *((device float2x4 *)y);
  1768. il = (il + 2 < nl) ? il + 2 : il % 2;
  1769. x = (il < 2) ? x + (2+nl-1)/nl : x;
  1770. y += BLOCK_SIZE_K;
  1771. threadgroup_barrier(mem_flags::mem_threadgroup);
  1772. //load matrices from threadgroup memory and conduct outer products
  1773. threadgroup half * lsma = (sa + THREAD_MAT_M * SG_MAT_SIZE * (sgitg % 2));
  1774. threadgroup float * lsmb = (sb + THREAD_MAT_N * SG_MAT_SIZE * (sgitg / 2));
  1775. #pragma unroll(4)
  1776. for (int ik = 0; ik < BLOCK_SIZE_K / 8; ik++) {
  1777. #pragma unroll(4)
  1778. for (int i = 0; i < 4; i++) {
  1779. simdgroup_load(ma[i],lsma + SG_MAT_SIZE * i);
  1780. }
  1781. simdgroup_barrier(mem_flags::mem_none);
  1782. #pragma unroll(2)
  1783. for (int i = 0; i < 2; i++) {
  1784. simdgroup_load(mb[i],lsmb + SG_MAT_SIZE * i);
  1785. }
  1786. lsma += BLOCK_SIZE_M / SG_MAT_ROW * SG_MAT_SIZE;
  1787. lsmb += BLOCK_SIZE_N / SG_MAT_ROW * SG_MAT_SIZE;
  1788. #pragma unroll(8)
  1789. for (int i = 0; i < 8; i++){
  1790. simdgroup_multiply_accumulate(c_res[i], mb[i/4], ma[i%4], c_res[i]);
  1791. }
  1792. }
  1793. }
  1794. if ((r0 + 1) * BLOCK_SIZE_M <= ne0 && (r1 + 1) * BLOCK_SIZE_N <= ne1) {
  1795. device float *C = dst + BLOCK_SIZE_M * r0 + 32 * (sgitg&1) \
  1796. + (BLOCK_SIZE_N * r1 + 16 * (sgitg>>1)) * ne0 + im*ne1*ne0;
  1797. for (int i = 0; i < 8; i++) {
  1798. simdgroup_store(c_res[i], C + 8 * (i%4) + 8 * ne0 * (i/4), ne0);
  1799. }
  1800. } else {
  1801. // block is smaller than 64x32, we should avoid writing data outside of the matrix
  1802. threadgroup_barrier(mem_flags::mem_threadgroup);
  1803. threadgroup float *temp_str = ((threadgroup float *)shared_memory) \
  1804. + 32 * (sgitg&1) + (16 * (sgitg>>1)) * BLOCK_SIZE_M;
  1805. for (int i = 0; i < 8; i++) {
  1806. simdgroup_store(c_res[i], temp_str + 8 * (i%4) + 8 * BLOCK_SIZE_M * (i/4), BLOCK_SIZE_M);
  1807. }
  1808. threadgroup_barrier(mem_flags::mem_threadgroup);
  1809. device float *C = dst + BLOCK_SIZE_M * r0 + (BLOCK_SIZE_N * r1) * ne0 + im*ne1*ne0;
  1810. if (sgitg==0) {
  1811. for (int i = 0; i < n_rows; i++) {
  1812. for (int j = tiitg; j< n_cols; j += BLOCK_SIZE_N) {
  1813. *(C + i + j * ne0) = *(temp_str + i + j * BLOCK_SIZE_M);
  1814. }
  1815. }
  1816. }
  1817. }
  1818. }
  1819. #if QK_K == 256
  1820. #define QK_NL 16
  1821. #else
  1822. #define QK_NL 4
  1823. #endif
  1824. typedef void (get_rows_t)(device const void *, device const int *, device float *, constant int64_t &, \
  1825. constant uint64_t &, constant uint64_t &, uint, uint, uint);
  1826. template [[host_name("kernel_get_rows_f16")]] kernel get_rows_t kernel_get_rows<half4x4, 1, dequantize_f16>;
  1827. template [[host_name("kernel_get_rows_q4_0")]] kernel get_rows_t kernel_get_rows<block_q4_0, 2, dequantize_q4_0>;
  1828. template [[host_name("kernel_get_rows_q4_1")]] kernel get_rows_t kernel_get_rows<block_q4_1, 2, dequantize_q4_1>;
  1829. template [[host_name("kernel_get_rows_q8_0")]] kernel get_rows_t kernel_get_rows<block_q8_0, 2, dequantize_q8_0>;
  1830. template [[host_name("kernel_get_rows_q2_K")]] kernel get_rows_t kernel_get_rows<block_q2_K, QK_NL, dequantize_q2_K>;
  1831. template [[host_name("kernel_get_rows_q3_K")]] kernel get_rows_t kernel_get_rows<block_q3_K, QK_NL, dequantize_q3_K>;
  1832. template [[host_name("kernel_get_rows_q4_K")]] kernel get_rows_t kernel_get_rows<block_q4_K, QK_NL, dequantize_q4_K>;
  1833. template [[host_name("kernel_get_rows_q5_K")]] kernel get_rows_t kernel_get_rows<block_q5_K, QK_NL, dequantize_q5_K>;
  1834. template [[host_name("kernel_get_rows_q6_K")]] kernel get_rows_t kernel_get_rows<block_q6_K, QK_NL, dequantize_q6_K>;
  1835. typedef void (mat_mm_t)(device const uchar *, device const float *, device float *, constant int64_t &,\
  1836. constant int64_t &, constant int64_t &, constant int64_t &, constant int64_t &, \
  1837. constant int64_t &, constant int64_t &, constant uint &, threadgroup uchar *, uint3, uint, uint);
  1838. template [[host_name("kernel_mul_mm_f16_f32")]] kernel mat_mm_t kernel_mul_mm<half4x4, 1, dequantize_f16>;
  1839. template [[host_name("kernel_mul_mm_q4_0_f32")]] kernel mat_mm_t kernel_mul_mm<block_q4_0, 2, dequantize_q4_0>;
  1840. template [[host_name("kernel_mul_mm_q4_1_f32")]] kernel mat_mm_t kernel_mul_mm<block_q4_1, 2, dequantize_q4_1>;
  1841. template [[host_name("kernel_mul_mm_q8_0_f32")]] kernel mat_mm_t kernel_mul_mm<block_q8_0, 2, dequantize_q8_0>;
  1842. template [[host_name("kernel_mul_mm_q2_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q2_K, QK_NL, dequantize_q2_K>;
  1843. template [[host_name("kernel_mul_mm_q3_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q3_K, QK_NL, dequantize_q3_K>;
  1844. template [[host_name("kernel_mul_mm_q4_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q4_K, QK_NL, dequantize_q4_K>;
  1845. template [[host_name("kernel_mul_mm_q5_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q5_K, QK_NL, dequantize_q5_K>;
  1846. template [[host_name("kernel_mul_mm_q6_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q6_K, QK_NL, dequantize_q6_K>;