1
0

ggml-metal.metal 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118
  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. uint3 tpig[[thread_position_in_grid]]) {
  604. const int64_t i3 = tpig[2];
  605. const int64_t i2 = tpig[1];
  606. const int64_t i1 = tpig[0];
  607. const bool is_neox = mode & 2;
  608. const float theta_scale = pow(freq_base, -2.0f/n_dims);
  609. const int64_t p = ((mode & 1) == 0 ? n_past + i2 : i2);
  610. float theta = freq_scale * (float)p;
  611. if (!is_neox) {
  612. for (int64_t i0 = 0; i0 < ne0; i0 += 2) {
  613. const float cos_theta = cos(theta);
  614. const float sin_theta = sin(theta);
  615. theta *= theta_scale;
  616. device const float * const src = (device float *)((device char *) src0 + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
  617. device float * dst_data = (device float *)((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  618. const float x0 = src[0];
  619. const float x1 = src[1];
  620. dst_data[0] = x0*cos_theta - x1*sin_theta;
  621. dst_data[1] = x0*sin_theta + x1*cos_theta;
  622. }
  623. } else {
  624. for (int64_t ib = 0; ib < ne0/n_dims; ++ib) {
  625. for (int64_t ic = 0; ic < n_dims; ic += 2) {
  626. const float cos_theta = cos(theta);
  627. const float sin_theta = sin(theta);
  628. theta *= theta_scale;
  629. const int64_t i0 = ib*n_dims + ic/2;
  630. device const float * const src = (device float *)((device char *) src0 + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
  631. device float * dst_data = (device float *)((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  632. const float x0 = src[0];
  633. const float x1 = src[n_dims/2];
  634. dst_data[0] = x0*cos_theta - x1*sin_theta;
  635. dst_data[n_dims/2] = x0*sin_theta + x1*cos_theta;
  636. }
  637. }
  638. }
  639. }
  640. kernel void kernel_cpy_f16_f16(
  641. device const half * src0,
  642. device half * dst,
  643. constant int64_t & ne00,
  644. constant int64_t & ne01,
  645. constant int64_t & ne02,
  646. constant int64_t & ne03,
  647. constant uint64_t & nb00,
  648. constant uint64_t & nb01,
  649. constant uint64_t & nb02,
  650. constant uint64_t & nb03,
  651. constant int64_t & ne0,
  652. constant int64_t & ne1,
  653. constant int64_t & ne2,
  654. constant int64_t & ne3,
  655. constant uint64_t & nb0,
  656. constant uint64_t & nb1,
  657. constant uint64_t & nb2,
  658. constant uint64_t & nb3,
  659. uint3 tgpig[[threadgroup_position_in_grid]],
  660. uint3 tpitg[[thread_position_in_threadgroup]],
  661. uint3 ntg[[threads_per_threadgroup]]) {
  662. const int64_t i03 = tgpig[2];
  663. const int64_t i02 = tgpig[1];
  664. const int64_t i01 = tgpig[0];
  665. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  666. const int64_t i3 = n / (ne2*ne1*ne0);
  667. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  668. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  669. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  670. device half * dst_data = (device half *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  671. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  672. device const half * src = (device half *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  673. dst_data[i00] = src[0];
  674. }
  675. }
  676. kernel void kernel_cpy_f32_f16(
  677. device const float * src0,
  678. device half * dst,
  679. constant int64_t & ne00,
  680. constant int64_t & ne01,
  681. constant int64_t & ne02,
  682. constant int64_t & ne03,
  683. constant uint64_t & nb00,
  684. constant uint64_t & nb01,
  685. constant uint64_t & nb02,
  686. constant uint64_t & nb03,
  687. constant int64_t & ne0,
  688. constant int64_t & ne1,
  689. constant int64_t & ne2,
  690. constant int64_t & ne3,
  691. constant uint64_t & nb0,
  692. constant uint64_t & nb1,
  693. constant uint64_t & nb2,
  694. constant uint64_t & nb3,
  695. uint3 tgpig[[threadgroup_position_in_grid]],
  696. uint3 tpitg[[thread_position_in_threadgroup]],
  697. uint3 ntg[[threads_per_threadgroup]]) {
  698. const int64_t i03 = tgpig[2];
  699. const int64_t i02 = tgpig[1];
  700. const int64_t i01 = tgpig[0];
  701. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  702. const int64_t i3 = n / (ne2*ne1*ne0);
  703. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  704. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  705. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  706. device half * dst_data = (device half *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  707. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  708. device const float * src = (device float *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  709. dst_data[i00] = src[0];
  710. }
  711. }
  712. kernel void kernel_cpy_f32_f32(
  713. device const float * src0,
  714. device float * dst,
  715. constant int64_t & ne00,
  716. constant int64_t & ne01,
  717. constant int64_t & ne02,
  718. constant int64_t & ne03,
  719. constant uint64_t & nb00,
  720. constant uint64_t & nb01,
  721. constant uint64_t & nb02,
  722. constant uint64_t & nb03,
  723. constant int64_t & ne0,
  724. constant int64_t & ne1,
  725. constant int64_t & ne2,
  726. constant int64_t & ne3,
  727. constant uint64_t & nb0,
  728. constant uint64_t & nb1,
  729. constant uint64_t & nb2,
  730. constant uint64_t & nb3,
  731. uint3 tgpig[[threadgroup_position_in_grid]],
  732. uint3 tpitg[[thread_position_in_threadgroup]],
  733. uint3 ntg[[threads_per_threadgroup]]) {
  734. const int64_t i03 = tgpig[2];
  735. const int64_t i02 = tgpig[1];
  736. const int64_t i01 = tgpig[0];
  737. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  738. const int64_t i3 = n / (ne2*ne1*ne0);
  739. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  740. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  741. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  742. device float * dst_data = (device float *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  743. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  744. device const float * src = (device float *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  745. dst_data[i00] = src[0];
  746. }
  747. }
  748. //============================================ k-quants ======================================================
  749. #ifndef QK_K
  750. #define QK_K 256
  751. #else
  752. static_assert(QK_K == 256 || QK_K == 64, "QK_K must be 256 or 64");
  753. #endif
  754. #if QK_K == 256
  755. #define K_SCALE_SIZE 12
  756. #else
  757. #define K_SCALE_SIZE 4
  758. #endif
  759. typedef struct {
  760. uint8_t scales[QK_K/16]; // scales and mins, quantized with 4 bits
  761. uint8_t qs[QK_K/4]; // quants
  762. half d; // super-block scale for quantized scales
  763. half dmin; // super-block scale for quantized mins
  764. } block_q2_K;
  765. // 84 bytes / block
  766. typedef struct {
  767. uint8_t hmask[QK_K/8]; // quants - high bit
  768. uint8_t qs[QK_K/4]; // quants - low 2 bits
  769. #if QK_K == 64
  770. uint8_t scales[2];
  771. #else
  772. uint8_t scales[K_SCALE_SIZE]; // scales, quantized with 6 bits
  773. #endif
  774. half d; // super-block scale
  775. } block_q3_K;
  776. #if QK_K == 64
  777. typedef struct {
  778. half d[2]; // super-block scales/mins
  779. uint8_t scales[2];
  780. uint8_t qs[QK_K/2]; // 4-bit quants
  781. } block_q4_K;
  782. #else
  783. typedef struct {
  784. half d; // super-block scale for quantized scales
  785. half dmin; // super-block scale for quantized mins
  786. uint8_t scales[K_SCALE_SIZE]; // scales and mins, quantized with 6 bits
  787. uint8_t qs[QK_K/2]; // 4--bit quants
  788. } block_q4_K;
  789. #endif
  790. #if QK_K == 64
  791. typedef struct {
  792. half d; // super-block scales/mins
  793. int8_t scales[QK_K/16]; // 8-bit block scales
  794. uint8_t qh[QK_K/8]; // quants, high bit
  795. uint8_t qs[QK_K/2]; // quants, low 4 bits
  796. } block_q5_K;
  797. #else
  798. typedef struct {
  799. half d; // super-block scale for quantized scales
  800. half dmin; // super-block scale for quantized mins
  801. uint8_t scales[3*QK_K/64]; // scales and mins, quantized with 6 bits
  802. uint8_t qh[QK_K/8]; // quants, high bit
  803. uint8_t qs[QK_K/2]; // quants, low 4 bits
  804. } block_q5_K;
  805. // 176 bytes / block
  806. #endif
  807. typedef struct {
  808. uint8_t ql[QK_K/2]; // quants, lower 4 bits
  809. uint8_t qh[QK_K/4]; // quants, upper 2 bits
  810. int8_t scales[QK_K/16]; // scales, quantized with 8 bits
  811. half d; // super-block scale
  812. } block_q6_K;
  813. // 210 bytes / block
  814. static inline uchar4 get_scale_min_k4(int j, device const uint8_t * q) {
  815. uchar4 r;
  816. if (j < 4) {
  817. r[0] = q[j+0] & 63;
  818. r[2] = q[j+1] & 63;
  819. r[1] = q[j+4] & 63;
  820. r[3] = q[j+5] & 63;
  821. } else {
  822. r[0] = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  823. r[2] = (q[j+5] & 0xF) | ((q[j-3] >> 6) << 4);
  824. r[1] = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  825. r[3] = (q[j+5] >> 4) | ((q[j+1] >> 6) << 4);
  826. }
  827. return r;
  828. }
  829. //====================================== dot products =========================
  830. kernel void kernel_mul_mat_q2_K_f32(
  831. device const void * src0,
  832. device const float * src1,
  833. device float * dst,
  834. constant int64_t & ne00,
  835. constant int64_t & ne01[[buffer(4)]],
  836. constant int64_t & ne02[[buffer(5)]],
  837. constant int64_t & ne10[[buffer(9)]],
  838. constant int64_t & ne12[[buffer(11)]],
  839. constant int64_t & ne0[[buffer(15)]],
  840. constant int64_t & ne1[[buffer(16)]],
  841. constant uint & gqa[[buffer(17)]],
  842. uint3 tgpig[[threadgroup_position_in_grid]],
  843. uint tiisg[[thread_index_in_simdgroup]],
  844. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  845. const int nb = ne00/QK_K;
  846. const int r0 = tgpig.x;
  847. const int r1 = tgpig.y;
  848. const int r2 = tgpig.z;
  849. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  850. const int ib_row = first_row * nb;
  851. const uint offset0 = r2/gqa*(nb*ne0);
  852. device const block_q2_K * x = (device const block_q2_K *) src0 + ib_row + offset0;
  853. device const float * y = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  854. float yl[32];
  855. float sumf[N_DST]={0.f}, all_sum;
  856. const int step = sizeof(block_q2_K) * nb;
  857. #if QK_K == 256
  858. const int ix = tiisg/8; // 0...3
  859. const int it = tiisg%8; // 0...7
  860. const int im = it/4; // 0 or 1
  861. const int ir = it%4; // 0...3
  862. const int is = (8*ir)/16;// 0 or 1
  863. device const float * y4 = y + ix * QK_K + 128 * im + 8 * ir;
  864. for (int ib = ix; ib < nb; ib += 4) {
  865. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  866. for (int i = 0; i < 8; ++i) {
  867. yl[i+ 0] = y4[i+ 0]; sumy[0] += yl[i+ 0];
  868. yl[i+ 8] = y4[i+32]; sumy[1] += yl[i+ 8];
  869. yl[i+16] = y4[i+64]; sumy[2] += yl[i+16];
  870. yl[i+24] = y4[i+96]; sumy[3] += yl[i+24];
  871. }
  872. device const uint8_t * sc = (device const uint8_t *)x[ib].scales + 8*im + is;
  873. device const uint16_t * qs = (device const uint16_t *)x[ib].qs + 16 * im + 4 * ir;
  874. device const half * dh = &x[ib].d;
  875. for (int row = 0; row < N_DST; row++) {
  876. float4 acc1 = {0.f, 0.f, 0.f, 0.f};
  877. float4 acc2 = {0.f, 0.f, 0.f, 0.f};
  878. for (int i = 0; i < 8; i += 2) {
  879. acc1[0] += yl[i+ 0] * (qs[i/2] & 0x0003);
  880. acc2[0] += yl[i+ 1] * (qs[i/2] & 0x0300);
  881. acc1[1] += yl[i+ 8] * (qs[i/2] & 0x000c);
  882. acc2[1] += yl[i+ 9] * (qs[i/2] & 0x0c00);
  883. acc1[2] += yl[i+16] * (qs[i/2] & 0x0030);
  884. acc2[2] += yl[i+17] * (qs[i/2] & 0x3000);
  885. acc1[3] += yl[i+24] * (qs[i/2] & 0x00c0);
  886. acc2[3] += yl[i+25] * (qs[i/2] & 0xc000);
  887. }
  888. float dall = dh[0];
  889. float dmin = dh[1] * 1.f/16.f;
  890. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc2[0]) * (sc[0] & 0xF) * 1.f/ 1.f +
  891. (acc1[1] + 1.f/256.f * acc2[1]) * (sc[2] & 0xF) * 1.f/ 4.f +
  892. (acc1[2] + 1.f/256.f * acc2[2]) * (sc[4] & 0xF) * 1.f/16.f +
  893. (acc1[3] + 1.f/256.f * acc2[3]) * (sc[6] & 0xF) * 1.f/64.f) -
  894. dmin * (sumy[0] * (sc[0] & 0xF0) + sumy[1] * (sc[2] & 0xF0) + sumy[2] * (sc[4] & 0xF0) + sumy[3] * (sc[6] & 0xF0));
  895. qs += step/2;
  896. sc += step;
  897. dh += step/2;
  898. }
  899. y4 += 4 * QK_K;
  900. }
  901. #else
  902. const int ix = tiisg/2; // 0...15
  903. const int it = tiisg%2; // 0...1
  904. device const float * y4 = y + ix * QK_K + 8 * it;
  905. for (int ib = ix; ib < nb; ib += 16) {
  906. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  907. for (int i = 0; i < 8; ++i) {
  908. yl[i+ 0] = y4[i+ 0]; sumy[0] += yl[i+ 0];
  909. yl[i+ 8] = y4[i+16]; sumy[1] += yl[i+ 8];
  910. yl[i+16] = y4[i+32]; sumy[2] += yl[i+16];
  911. yl[i+24] = y4[i+48]; sumy[3] += yl[i+24];
  912. }
  913. device const uint8_t * sc = (device const uint8_t *)x[ib].scales;
  914. device const uint16_t * qs = (device const uint16_t *)x[ib].qs + 4 * it;
  915. device const half * dh = &x[ib].d;
  916. for (int row = 0; row < N_DST; row++) {
  917. float4 acc1 = {0.f, 0.f, 0.f, 0.f};
  918. float4 acc2 = {0.f, 0.f, 0.f, 0.f};
  919. for (int i = 0; i < 8; i += 2) {
  920. acc1[0] += yl[i+ 0] * (qs[i/2] & 0x0003);
  921. acc2[0] += yl[i+ 1] * (qs[i/2] & 0x0300);
  922. acc1[1] += yl[i+ 8] * (qs[i/2] & 0x000c);
  923. acc2[1] += yl[i+ 9] * (qs[i/2] & 0x0c00);
  924. acc1[2] += yl[i+16] * (qs[i/2] & 0x0030);
  925. acc2[2] += yl[i+17] * (qs[i/2] & 0x3000);
  926. acc1[3] += yl[i+24] * (qs[i/2] & 0x00c0);
  927. acc2[3] += yl[i+25] * (qs[i/2] & 0xc000);
  928. }
  929. float dall = dh[0];
  930. float dmin = dh[1];
  931. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc2[0]) * (sc[0] & 0xF) * 1.f/ 1.f +
  932. (acc1[1] + 1.f/256.f * acc2[1]) * (sc[1] & 0xF) * 1.f/ 4.f +
  933. (acc1[2] + 1.f/256.f * acc2[2]) * (sc[2] & 0xF) * 1.f/16.f +
  934. (acc1[3] + 1.f/256.f * acc2[3]) * (sc[3] & 0xF) * 1.f/64.f) -
  935. dmin * (sumy[0] * (sc[0] >> 4) + sumy[1] * (sc[1] >> 4) + sumy[2] * (sc[2] >> 4) + sumy[3] * (sc[3] >> 4));
  936. qs += step/2;
  937. sc += step;
  938. dh += step/2;
  939. }
  940. y4 += 16 * QK_K;
  941. }
  942. #endif
  943. for (int row = 0; row < N_DST; ++row) {
  944. all_sum = simd_sum(sumf[row]);
  945. if (tiisg == 0) {
  946. dst[r1*ne0 + r2*ne0*ne1 + first_row + row] = all_sum;
  947. }
  948. }
  949. }
  950. #if QK_K == 256
  951. kernel void kernel_mul_mat_q3_K_f32(
  952. device const void * src0,
  953. device const float * src1,
  954. device float * dst,
  955. constant int64_t & ne00,
  956. constant int64_t & ne01[[buffer(4)]],
  957. constant int64_t & ne02[[buffer(5)]],
  958. constant int64_t & ne10[[buffer(9)]],
  959. constant int64_t & ne12[[buffer(11)]],
  960. constant int64_t & ne0[[buffer(15)]],
  961. constant int64_t & ne1[[buffer(16)]],
  962. constant uint & gqa[[buffer(17)]],
  963. uint3 tgpig[[threadgroup_position_in_grid]],
  964. uint tiisg[[thread_index_in_simdgroup]],
  965. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  966. const int nb = ne00/QK_K;
  967. const int64_t r0 = tgpig.x;
  968. const int64_t r1 = tgpig.y;
  969. const int64_t r2 = tgpig.z;
  970. const int first_row = (r0 * N_SIMDGROUP + sgitg) * 2;
  971. const uint offset0 = r2/gqa*(nb*ne0);
  972. device const block_q3_K * x = (device const block_q3_K *) src0 + first_row*nb + offset0;
  973. device const float * yy = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  974. float yl[16];
  975. const uint16_t kmask1 = 0x0303;
  976. const uint16_t kmask2 = 0x0f0f;
  977. const int tid = tiisg/2;
  978. const int ix = tiisg%2;
  979. const int ip = tid/8; // 0 or 1
  980. const int il = tid/2 - 4*ip; // 0...3
  981. const int ir = tid%2;
  982. const int n = 8;
  983. const int l0 = n*ir;
  984. const uint16_t m1 = 1 << (4*ip + il);
  985. const uint16_t m2 = m1 << 8;
  986. const int shift = 2*il;
  987. const uint16_t qm1 = 0x0003 << shift;
  988. const uint16_t qm2 = 0x0300 << shift;
  989. const int32_t v1 = 4 << shift;
  990. const int32_t v2 = 1024 << shift;
  991. const uint16_t s_shift1 = 4*ip;
  992. const uint16_t s_shift2 = s_shift1 + 2*(il/2);
  993. const int ik = 4 + (il%2);
  994. const int q_offset = 32*ip + l0;
  995. const int y_offset = 128*ip + 32*il + l0;
  996. const int step = sizeof(block_q3_K) * nb / 2;
  997. device const float * y1 = yy + ix*QK_K + y_offset;
  998. float sumf1[2] = {0.f}, sumf2[2] = {0.f};
  999. for (int i = ix; i < nb; i += 2) {
  1000. for (int l = 0; l < 8; ++l) {
  1001. yl[l+0] = y1[l+ 0];
  1002. yl[l+8] = y1[l+16];
  1003. }
  1004. device const uint16_t * q = (device const uint16_t *)(x[i].qs + q_offset);
  1005. device const uint16_t * h = (device const uint16_t *)(x[i].hmask + l0);
  1006. device const uint16_t * a = (device const uint16_t *)(x[i].scales);
  1007. device const half * dh = &x[i].d;
  1008. for (int row = 0; row < 2; ++row) {
  1009. const float d_all = (float)dh[0];
  1010. const char2 scales = as_type<char2>((uint16_t)(((a[il] >> s_shift1) & kmask2) | (((a[ik] >> s_shift2) & kmask1) << 4)));
  1011. float s1 = 0, s2 = 0;
  1012. for (int l = 0; l < n; l += 2) {
  1013. const uint16_t qs = q[l/2];
  1014. s1 += yl[l+0] * ((int32_t)(qs & qm1) - ((h[l/2] & m1) ? 0 : v1));
  1015. s2 += yl[l+1] * ((int32_t)(qs & qm2) - ((h[l/2] & m2) ? 0 : v2));
  1016. }
  1017. float d = d_all * (s1 + 1.f/256.f * s2);
  1018. sumf1[row] += d * scales[0];
  1019. sumf2[row] += d;
  1020. s1 = s2 = 0;
  1021. for (int l = 0; l < n; l += 2) {
  1022. const uint16_t qs = q[l/2+8];
  1023. s1 += yl[l+8] * ((int32_t)(qs & qm1) - ((h[l/2+8] & m1) ? 0 : v1));
  1024. s2 += yl[l+9] * ((int32_t)(qs & qm2) - ((h[l/2+8] & m2) ? 0 : v2));
  1025. }
  1026. d = d_all * (s1 + 1.f/256.f * s2);
  1027. sumf1[row] += d * scales[1];
  1028. sumf2[row] += d;
  1029. q += step;
  1030. h += step;
  1031. a += step;
  1032. dh += step;
  1033. }
  1034. y1 += 2 * QK_K;
  1035. }
  1036. for (int row = 0; row < 2; ++row) {
  1037. const float sumf = (sumf1[row] - 32.f*sumf2[row]) / (1 << shift);
  1038. const float tot = simd_sum(sumf);
  1039. if (tiisg == 0) {
  1040. dst[r1*ne0 + r2*ne0*ne1 + first_row + row] = tot;
  1041. }
  1042. }
  1043. }
  1044. #else
  1045. kernel void kernel_mul_mat_q3_K_f32(
  1046. device const void * src0,
  1047. device const float * src1,
  1048. device float * dst,
  1049. constant int64_t & ne00,
  1050. constant int64_t & ne01[[buffer(4)]],
  1051. constant int64_t & ne02[[buffer(5)]],
  1052. constant int64_t & ne10[[buffer(9)]],
  1053. constant int64_t & ne12[[buffer(11)]],
  1054. constant int64_t & ne0[[buffer(15)]],
  1055. constant int64_t & ne1[[buffer(16)]],
  1056. constant uint & gqa[[buffer(17)]],
  1057. uint3 tgpig[[threadgroup_position_in_grid]],
  1058. uint tiisg[[thread_index_in_simdgroup]],
  1059. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1060. const int nb = ne00/QK_K;
  1061. const int64_t r0 = tgpig.x;
  1062. const int64_t r1 = tgpig.y;
  1063. const int64_t r2 = tgpig.z;
  1064. const int row = 2 * r0 + sgitg;
  1065. const uint offset0 = r2/gqa*(nb*ne0);
  1066. device const block_q3_K * x = (device const block_q3_K *) src0 + row*nb + offset0;
  1067. device const float * yy = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  1068. const int ix = tiisg/4;
  1069. const int il = 4 * (tiisg%4);// 0, 4, 8, 12
  1070. const int im = il/8; // 0, 0, 1, 1
  1071. const int in = il%8; // 0, 4, 0, 4
  1072. float2 sum = {0.f, 0.f};
  1073. for (int i = ix; i < nb; i += 8) {
  1074. const float d_all = (float)(x[i].d);
  1075. device const uint16_t * q = (device const uint16_t *)(x[i].qs + il);
  1076. device const uint16_t * h = (device const uint16_t *)(x[i].hmask + in);
  1077. device const uint16_t * s = (device const uint16_t *)(x[i].scales);
  1078. device const float * y = yy + i * QK_K + il;
  1079. const float d1 = d_all * ((int32_t)(s[0] & 0x000F) - 8);
  1080. const float d2 = d_all * ((int32_t)(s[0] & 0x00F0) - 128) * 1.f/64.f;
  1081. const float d3 = d_all * ((int32_t)(s[0] & 0x0F00) - 2048) * 1.f/4096.f;
  1082. const float d4 = d_all * ((int32_t)(s[0] & 0xF000) - 32768) * 1.f/262144.f;
  1083. for (int l = 0; l < 4; l += 2) {
  1084. const uint16_t hm = h[l/2] >> im;
  1085. sum[0] += y[l+ 0] * d1 * ((int32_t)(q[l/2] & 0x0003) - ((hm & 0x0001) ? 0 : 4))
  1086. + y[l+16] * d2 * ((int32_t)(q[l/2] & 0x000c) - ((hm & 0x0004) ? 0 : 16))
  1087. + y[l+32] * d3 * ((int32_t)(q[l/2] & 0x0030) - ((hm & 0x0010) ? 0 : 64))
  1088. + y[l+48] * d4 * ((int32_t)(q[l/2] & 0x00c0) - ((hm & 0x0040) ? 0 : 256));
  1089. sum[1] += y[l+ 1] * d1 * ((int32_t)(q[l/2] & 0x0300) - ((hm & 0x0100) ? 0 : 1024))
  1090. + y[l+17] * d2 * ((int32_t)(q[l/2] & 0x0c00) - ((hm & 0x0400) ? 0 : 4096))
  1091. + y[l+33] * d3 * ((int32_t)(q[l/2] & 0x3000) - ((hm & 0x1000) ? 0 : 16384))
  1092. + y[l+49] * d4 * ((int32_t)(q[l/2] & 0xc000) - ((hm & 0x4000) ? 0 : 65536));
  1093. }
  1094. }
  1095. const float sumf = sum[0] + sum[1] * 1.f/256.f;
  1096. const float tot = simd_sum(sumf);
  1097. if (tiisg == 0) {
  1098. dst[r1*ne0 + r2*ne0*ne1 + row] = tot;
  1099. }
  1100. }
  1101. #endif
  1102. #if QK_K == 256
  1103. kernel void kernel_mul_mat_q4_K_f32(
  1104. device const void * src0,
  1105. device const float * src1,
  1106. device float * dst,
  1107. constant int64_t & ne00,
  1108. constant int64_t & ne01[[buffer(4)]],
  1109. constant int64_t & ne02[[buffer(5)]],
  1110. constant int64_t & ne10[[buffer(9)]],
  1111. constant int64_t & ne12[[buffer(11)]],
  1112. constant int64_t & ne0[[buffer(15)]],
  1113. constant int64_t & ne1[[buffer(16)]],
  1114. constant uint & gqa[[buffer(17)]],
  1115. uint3 tgpig[[threadgroup_position_in_grid]],
  1116. uint tiisg[[thread_index_in_simdgroup]],
  1117. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1118. const uint16_t kmask1 = 0x3f3f;
  1119. const uint16_t kmask2 = 0x0f0f;
  1120. const uint16_t kmask3 = 0xc0c0;
  1121. const int ix = tiisg/8; // 0...3
  1122. const int it = tiisg%8; // 0...7
  1123. const int im = it/4; // 0 or 1
  1124. const int ir = it%4; // 0...3
  1125. const int nb = ne00/QK_K;
  1126. const int r0 = tgpig.x;
  1127. const int r1 = tgpig.y;
  1128. const int r2 = tgpig.z;
  1129. //const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  1130. const int first_row = r0 * N_DST;
  1131. const int ib_row = first_row * nb;
  1132. const uint offset0 = r2/gqa*(nb*ne0);
  1133. device const block_q4_K * x = (device const block_q4_K *) src0 + ib_row + offset0;
  1134. device const float * y = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  1135. float yl[16];
  1136. float yh[16];
  1137. float sumf[N_DST]={0.f}, all_sum;
  1138. const int step = sizeof(block_q4_K) * nb / 2;
  1139. device const float * y4 = y + ix * QK_K + 64 * im + 8 * ir;
  1140. uint16_t sc16[4];
  1141. thread const uint8_t * sc8 = (thread const uint8_t *)sc16;
  1142. for (int ib = ix; ib < nb; ib += 4) {
  1143. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  1144. for (int i = 0; i < 8; ++i) {
  1145. yl[i+0] = y4[i+ 0]; sumy[0] += yl[i+0];
  1146. yl[i+8] = y4[i+ 32]; sumy[1] += yl[i+8];
  1147. yh[i+0] = y4[i+128]; sumy[2] += yh[i+0];
  1148. yh[i+8] = y4[i+160]; sumy[3] += yh[i+8];
  1149. }
  1150. device const uint16_t * sc = (device const uint16_t *)x[ib].scales + im;
  1151. device const uint16_t * q1 = (device const uint16_t *)x[ib].qs + 16 * im + 4 * ir;
  1152. device const half * dh = &x[ib].d;
  1153. for (int row = 0; row < N_DST; row++) {
  1154. sc16[0] = sc[0] & kmask1;
  1155. sc16[1] = sc[2] & kmask1;
  1156. sc16[2] = ((sc[4] >> 0) & kmask2) | ((sc[0] & kmask3) >> 2);
  1157. sc16[3] = ((sc[4] >> 4) & kmask2) | ((sc[2] & kmask3) >> 2);
  1158. device const uint16_t * q2 = q1 + 32;
  1159. float4 acc1 = {0.f, 0.f, 0.f, 0.f};
  1160. float4 acc2 = {0.f, 0.f, 0.f, 0.f};
  1161. for (int i = 0; i < 8; i += 2) {
  1162. acc1[0] += yl[i+0] * (q1[i/2] & 0x000F);
  1163. acc1[1] += yl[i+1] * (q1[i/2] & 0x0F00);
  1164. acc1[2] += yl[i+8] * (q1[i/2] & 0x00F0);
  1165. acc1[3] += yl[i+9] * (q1[i/2] & 0xF000);
  1166. acc2[0] += yh[i+0] * (q2[i/2] & 0x000F);
  1167. acc2[1] += yh[i+1] * (q2[i/2] & 0x0F00);
  1168. acc2[2] += yh[i+8] * (q2[i/2] & 0x00F0);
  1169. acc2[3] += yh[i+9] * (q2[i/2] & 0xF000);
  1170. }
  1171. float dall = dh[0];
  1172. float dmin = dh[1];
  1173. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc1[1]) * sc8[0] +
  1174. (acc1[2] + 1.f/256.f * acc1[3]) * sc8[1] * 1.f/16.f +
  1175. (acc2[0] + 1.f/256.f * acc2[1]) * sc8[4] +
  1176. (acc2[2] + 1.f/256.f * acc2[3]) * sc8[5] * 1.f/16.f) -
  1177. dmin * (sumy[0] * sc8[2] + sumy[1] * sc8[3] + sumy[2] * sc8[6] + sumy[3] * sc8[7]);
  1178. q1 += step;
  1179. sc += step;
  1180. dh += step;
  1181. }
  1182. y4 += 4 * QK_K;
  1183. }
  1184. for (int row = 0; row < N_DST; ++row) {
  1185. all_sum = simd_sum(sumf[row]);
  1186. if (tiisg == 0) {
  1187. dst[r1*ne0 + r2*ne0*ne1 + first_row + row] = all_sum;
  1188. }
  1189. }
  1190. }
  1191. #else
  1192. kernel void kernel_mul_mat_q4_K_f32(
  1193. device const void * src0,
  1194. device const float * src1,
  1195. device float * dst,
  1196. constant int64_t & ne00,
  1197. constant int64_t & ne01[[buffer(4)]],
  1198. constant int64_t & ne02[[buffer(5)]],
  1199. constant int64_t & ne10[[buffer(9)]],
  1200. constant int64_t & ne12[[buffer(11)]],
  1201. constant int64_t & ne0[[buffer(15)]],
  1202. constant int64_t & ne1[[buffer(16)]],
  1203. constant uint & gqa[[buffer(17)]],
  1204. uint3 tgpig[[threadgroup_position_in_grid]],
  1205. uint tiisg[[thread_index_in_simdgroup]],
  1206. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1207. const int ix = tiisg/4; // 0...7
  1208. const int it = tiisg%4; // 0...3
  1209. const int nb = ne00/QK_K;
  1210. const int r0 = tgpig.x;
  1211. const int r1 = tgpig.y;
  1212. const int r2 = tgpig.z;
  1213. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  1214. const int ib_row = first_row * nb;
  1215. const uint offset0 = r2/gqa*(nb*ne0);
  1216. device const block_q4_K * x = (device const block_q4_K *) src0 + ib_row + offset0;
  1217. device const float * y = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  1218. float yl[8];
  1219. float yh[8];
  1220. float sumf[N_DST]={0.f}, all_sum;
  1221. const int step = sizeof(block_q4_K) * nb / 2;
  1222. device const float * y4 = y + ix * QK_K + 8 * it;
  1223. uint16_t sc16[4];
  1224. for (int ib = ix; ib < nb; ib += 8) {
  1225. float2 sumy = {0.f, 0.f};
  1226. for (int i = 0; i < 8; ++i) {
  1227. yl[i] = y4[i+ 0]; sumy[0] += yl[i];
  1228. yh[i] = y4[i+32]; sumy[1] += yh[i];
  1229. }
  1230. device const uint16_t * sc = (device const uint16_t *)x[ib].scales;
  1231. device const uint16_t * qs = (device const uint16_t *)x[ib].qs + 4 * it;
  1232. device const half * dh = x[ib].d;
  1233. for (int row = 0; row < N_DST; row++) {
  1234. sc16[0] = sc[0] & 0x000f;
  1235. sc16[1] = sc[0] & 0x0f00;
  1236. sc16[2] = sc[0] & 0x00f0;
  1237. sc16[3] = sc[0] & 0xf000;
  1238. float2 acc1 = {0.f, 0.f};
  1239. float2 acc2 = {0.f, 0.f};
  1240. for (int i = 0; i < 8; i += 2) {
  1241. acc1[0] += yl[i+0] * (qs[i/2] & 0x000F);
  1242. acc1[1] += yl[i+1] * (qs[i/2] & 0x0F00);
  1243. acc2[0] += yh[i+0] * (qs[i/2] & 0x00F0);
  1244. acc2[1] += yh[i+1] * (qs[i/2] & 0xF000);
  1245. }
  1246. float dall = dh[0];
  1247. float dmin = dh[1];
  1248. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc1[1]) * sc16[0] +
  1249. (acc2[0] + 1.f/256.f * acc2[1]) * sc16[1] * 1.f/4096.f) -
  1250. dmin * 1.f/16.f * (sumy[0] * sc16[2] + sumy[1] * sc16[3] * 1.f/256.f);
  1251. qs += step;
  1252. sc += step;
  1253. dh += step;
  1254. }
  1255. y4 += 8 * QK_K;
  1256. }
  1257. for (int row = 0; row < N_DST; ++row) {
  1258. all_sum = simd_sum(sumf[row]);
  1259. if (tiisg == 0) {
  1260. dst[r1*ne0+ r2*ne0*ne1 + first_row + row] = all_sum;
  1261. }
  1262. }
  1263. }
  1264. #endif
  1265. kernel void kernel_mul_mat_q5_K_f32(
  1266. device const void * src0,
  1267. device const float * src1,
  1268. device float * dst,
  1269. constant int64_t & ne00,
  1270. constant int64_t & ne01[[buffer(4)]],
  1271. constant int64_t & ne02[[buffer(5)]],
  1272. constant int64_t & ne10[[buffer(9)]],
  1273. constant int64_t & ne12[[buffer(11)]],
  1274. constant int64_t & ne0[[buffer(15)]],
  1275. constant int64_t & ne1[[buffer(16)]],
  1276. constant uint & gqa[[buffer(17)]],
  1277. uint3 tgpig[[threadgroup_position_in_grid]],
  1278. uint tiisg[[thread_index_in_simdgroup]],
  1279. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1280. const int nb = ne00/QK_K;
  1281. const int64_t r0 = tgpig.x;
  1282. const int64_t r1 = tgpig.y;
  1283. const int r2 = tgpig.z;
  1284. const int first_row = (r0 * N_SIMDGROUP + sgitg) * 2;
  1285. const uint offset0 = r2/gqa*(nb*ne0);
  1286. device const block_q5_K * x = (device const block_q5_K *) src0 + first_row*nb + offset0;
  1287. device const float * yy = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  1288. float sumf[2]={0.f};
  1289. const int step = sizeof(block_q5_K) * nb;
  1290. #if QK_K == 256
  1291. #
  1292. float yl[16], yh[16];
  1293. const uint16_t kmask1 = 0x3f3f;
  1294. const uint16_t kmask2 = 0x0f0f;
  1295. const uint16_t kmask3 = 0xc0c0;
  1296. const int tid = tiisg/4;
  1297. const int ix = tiisg%4;
  1298. const int im = tid/4;
  1299. const int ir = tid%4;
  1300. const int n = 8;
  1301. const int l0 = n*ir;
  1302. const int q_offset = 32*im + l0;
  1303. const int y_offset = 64*im + l0;
  1304. const uint8_t hm1 = 1u << (2*im);
  1305. const uint8_t hm2 = hm1 << 1;
  1306. const uint8_t hm3 = hm1 << 4;
  1307. const uint8_t hm4 = hm2 << 4;
  1308. uint16_t sc16[4];
  1309. thread const uint8_t * sc8 = (thread const uint8_t *)sc16;
  1310. device const float * y1 = yy + ix*QK_K + y_offset;
  1311. for (int i = ix; i < nb; i += 4) {
  1312. device const uint8_t * q1 = x[i].qs + q_offset;
  1313. device const uint8_t * qh = x[i].qh + l0;
  1314. device const half * dh = &x[i].d;
  1315. device const uint16_t * a = (device const uint16_t *)x[i].scales + im;
  1316. device const float * y2 = y1 + 128;
  1317. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  1318. for (int l = 0; l < 8; ++l) {
  1319. yl[l+0] = y1[l+ 0]; sumy[0] += yl[l+0];
  1320. yl[l+8] = y1[l+32]; sumy[1] += yl[l+8];
  1321. yh[l+0] = y2[l+ 0]; sumy[2] += yh[l+0];
  1322. yh[l+8] = y2[l+32]; sumy[3] += yh[l+8];
  1323. }
  1324. for (int row = 0; row < 2; ++row) {
  1325. device const uint8_t * q2 = q1 + 64;
  1326. sc16[0] = a[0] & kmask1;
  1327. sc16[1] = a[2] & kmask1;
  1328. sc16[2] = ((a[4] >> 0) & kmask2) | ((a[0] & kmask3) >> 2);
  1329. sc16[3] = ((a[4] >> 4) & kmask2) | ((a[2] & kmask3) >> 2);
  1330. float4 acc = {0.f, 0.f, 0.f, 0.f};
  1331. for (int l = 0; l < n; ++l) {
  1332. uint8_t h = qh[l];
  1333. acc[0] += yl[l+0] * ((uint16_t)(q1[l] & 0x0F) + (h & hm1 ? 16 : 0));
  1334. acc[1] += yl[l+8] * ((uint16_t)(q1[l] & 0xF0) + (h & hm2 ? 256 : 0));
  1335. acc[2] += yh[l+0] * ((uint16_t)(q2[l] & 0x0F) + (h & hm3 ? 16 : 0));
  1336. acc[3] += yh[l+8] * ((uint16_t)(q2[l] & 0xF0) + (h & hm4 ? 256 : 0));
  1337. }
  1338. const float dall = dh[0];
  1339. const float dmin = dh[1];
  1340. sumf[row] += dall * (acc[0] * sc8[0] + acc[1] * sc8[1] * 1.f/16.f + acc[2] * sc8[4] + acc[3] * sc8[5] * 1.f/16.f) -
  1341. dmin * (sumy[0] * sc8[2] + sumy[1] * sc8[3] + sumy[2] * sc8[6] + sumy[3] * sc8[7]);
  1342. q1 += step;
  1343. qh += step;
  1344. dh += step/2;
  1345. a += step/2;
  1346. }
  1347. y1 += 4 * QK_K;
  1348. }
  1349. #else
  1350. float yl[8], yh[8];
  1351. const int il = 4 * (tiisg/8); // 0, 4, 8, 12
  1352. const int ix = tiisg%8;
  1353. const int im = il/8; // 0, 0, 1, 1
  1354. const int in = il%8; // 0, 4, 0, 4
  1355. device const float * y = yy + ix*QK_K + il;
  1356. for (int i = ix; i < nb; i += 8) {
  1357. for (int l = 0; l < 4; ++l) {
  1358. yl[l+0] = y[l+ 0];
  1359. yl[l+4] = y[l+16];
  1360. yh[l+0] = y[l+32];
  1361. yh[l+4] = y[l+48];
  1362. }
  1363. device const half * dh = &x[i].d;
  1364. device const uint8_t * q = x[i].qs + il;
  1365. device const uint8_t * h = x[i].qh + in;
  1366. device const int8_t * s = x[i].scales;
  1367. for (int row = 0; row < 2; ++row) {
  1368. const float d = dh[0];
  1369. float2 acc = {0.f, 0.f};
  1370. for (int l = 0; l < 4; ++l) {
  1371. const uint8_t hl = h[l] >> im;
  1372. acc[0] += yl[l+0] * s[0] * ((int16_t)(q[l+ 0] & 0x0F) - (hl & 0x01 ? 0 : 16))
  1373. + yl[l+4] * s[1] * ((int16_t)(q[l+16] & 0x0F) - (hl & 0x04 ? 0 : 16));
  1374. acc[1] += yh[l+0] * s[2] * ((int16_t)(q[l+ 0] & 0xF0) - (hl & 0x10 ? 0 : 256))
  1375. + yh[l+4] * s[3] * ((int16_t)(q[l+16] & 0xF0) - (hl & 0x40 ? 0 : 256));
  1376. }
  1377. sumf[row] += d * (acc[0] + 1.f/16.f * acc[1]);
  1378. q += step;
  1379. h += step;
  1380. s += step;
  1381. dh += step/2;
  1382. }
  1383. y += 8 * QK_K;
  1384. }
  1385. #endif
  1386. for (int row = 0; row < 2; ++row) {
  1387. const float tot = simd_sum(sumf[row]);
  1388. if (tiisg == 0) {
  1389. dst[r1*ne0 + r2*ne0*ne1 + first_row + row] = tot;
  1390. }
  1391. }
  1392. }
  1393. kernel void kernel_mul_mat_q6_K_f32(
  1394. device const void * src0,
  1395. device const float * src1,
  1396. device float * dst,
  1397. constant int64_t & ne00,
  1398. constant int64_t & ne01[[buffer(4)]],
  1399. constant int64_t & ne02[[buffer(5)]],
  1400. constant int64_t & ne10[[buffer(9)]],
  1401. constant int64_t & ne12[[buffer(11)]],
  1402. constant int64_t & ne0[[buffer(15)]],
  1403. constant int64_t & ne1[[buffer(16)]],
  1404. constant uint & gqa[[buffer(17)]],
  1405. uint3 tgpig[[threadgroup_position_in_grid]],
  1406. uint tiisg[[thread_index_in_simdgroup]],
  1407. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1408. const uint8_t kmask1 = 0x03;
  1409. const uint8_t kmask2 = 0x0C;
  1410. const uint8_t kmask3 = 0x30;
  1411. const uint8_t kmask4 = 0xC0;
  1412. const int nb = ne00/QK_K;
  1413. const int64_t r0 = tgpig.x;
  1414. const int64_t r1 = tgpig.y;
  1415. const int r2 = tgpig.z;
  1416. const int row = 2 * r0 + sgitg;
  1417. const uint offset0 = r2/gqa*(nb*ne0);
  1418. device const block_q6_K * x = (device const block_q6_K *) src0 + row * nb + offset0;
  1419. device const float * yy = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  1420. float sumf = 0;
  1421. #if QK_K == 256
  1422. const int tid = tiisg/2;
  1423. const int ix = tiisg%2;
  1424. const int ip = tid/8; // 0 or 1
  1425. const int il = tid%8;
  1426. const int n = 4;
  1427. const int l0 = n*il;
  1428. const int is = 8*ip + l0/16;
  1429. const int y_offset = 128*ip + l0;
  1430. const int q_offset_l = 64*ip + l0;
  1431. const int q_offset_h = 32*ip + l0;
  1432. for (int i = ix; i < nb; i += 2) {
  1433. device const uint8_t * q1 = x[i].ql + q_offset_l;
  1434. device const uint8_t * q2 = q1 + 32;
  1435. device const uint8_t * qh = x[i].qh + q_offset_h;
  1436. device const int8_t * sc = x[i].scales + is;
  1437. device const float * y = yy + i * QK_K + y_offset;
  1438. const float dall = x[i].d;
  1439. float4 sums = {0.f, 0.f, 0.f, 0.f};
  1440. for (int l = 0; l < n; ++l) {
  1441. sums[0] += y[l+ 0] * ((int8_t)((q1[l] & 0xF) | ((qh[l] & kmask1) << 4)) - 32);
  1442. sums[1] += y[l+32] * ((int8_t)((q2[l] & 0xF) | ((qh[l] & kmask2) << 2)) - 32);
  1443. sums[2] += y[l+64] * ((int8_t)((q1[l] >> 4) | ((qh[l] & kmask3) << 0)) - 32);
  1444. sums[3] += y[l+96] * ((int8_t)((q2[l] >> 4) | ((qh[l] & kmask4) >> 2)) - 32);
  1445. }
  1446. sumf += dall * (sums[0] * sc[0] + sums[1] * sc[2] + sums[2] * sc[4] + sums[3] * sc[6]);
  1447. }
  1448. #else
  1449. const int ix = tiisg/4;
  1450. const int il = 4*(tiisg%4);
  1451. for (int i = ix; i < nb; i += 8) {
  1452. device const float * y = yy + i * QK_K + il;
  1453. device const uint8_t * ql = x[i].ql + il;
  1454. device const uint8_t * qh = x[i].qh + il;
  1455. device const int8_t * s = x[i].scales;
  1456. const float d = x[i].d;
  1457. float4 sums = {0.f, 0.f, 0.f, 0.f};
  1458. for (int l = 0; l < 4; ++l) {
  1459. sums[0] += y[l+ 0] * ((int8_t)((ql[l+ 0] & 0xF) | ((qh[l] & kmask1) << 4)) - 32);
  1460. sums[1] += y[l+16] * ((int8_t)((ql[l+16] & 0xF) | ((qh[l] & kmask2) << 2)) - 32);
  1461. sums[2] += y[l+32] * ((int8_t)((ql[l+ 0] >> 4) | ((qh[l] & kmask3) >> 0)) - 32);
  1462. sums[3] += y[l+48] * ((int8_t)((ql[l+16] >> 4) | ((qh[l] & kmask4) >> 2)) - 32);
  1463. }
  1464. sumf += d * (sums[0] * s[0] + sums[1] * s[1] + sums[2] * s[2] + sums[3] * s[3]);
  1465. }
  1466. #endif
  1467. const float tot = simd_sum(sumf);
  1468. if (tiisg == 0) {
  1469. dst[r1*ne0 + r2*ne0*ne1 + row] = tot;
  1470. }
  1471. }
  1472. //============================= templates and their specializations =============================
  1473. template <typename type4x4>
  1474. void dequantize_f16(device const half4x4 * src, short il, thread type4x4 & reg) {
  1475. half4x4 temp = *(((device half4x4 *)src));
  1476. for (int i = 0; i < 16; i++){
  1477. reg[i/4][i%4] = temp[i/4][i%4];
  1478. }
  1479. }
  1480. template <typename type4x4>
  1481. void dequantize_q4_0(device const block_q4_0 *xb, short il, thread type4x4 & reg) {
  1482. device const uint16_t * qs = ((device const uint16_t *)xb + 1);
  1483. const half d = il ? (xb->d / 16.h) : xb->d;
  1484. const half m = il ? ( -8.h * 16.h) : -8.h;
  1485. const ushort mask0 = il ? 0x00F0 : 0x000F;
  1486. const ushort mask1 = il ? 0xF000 : 0x0F00;
  1487. for (int i=0;i<8;i++) {
  1488. reg[i/2][2*(i%2)] = (((qs[i] & mask0) ) + m) * d;
  1489. reg[i/2][2*(i%2)+1] = (((qs[i] & mask1) >> 8) + m) * d;
  1490. }
  1491. }
  1492. template <typename type4x4>
  1493. void dequantize_q4_1(device const block_q4_1 *xb, short il, thread type4x4 & reg) {
  1494. device const uint16_t * qs = ((device const uint16_t *)xb + 2);
  1495. const half d = il ? (xb->d / 16.h) : xb->d;
  1496. const half m = xb->m;
  1497. const ushort mask0 = il ? 0x00F0 : 0x000F;
  1498. const ushort mask1 = il ? 0xF000 : 0x0F00;
  1499. for (int i=0;i<8;i++) {
  1500. reg[i/2][2*(i%2)] = (((qs[i] & mask0) ) * d) + m;
  1501. reg[i/2][2*(i%2)+1] = (((qs[i] & mask1) >> 8) * d) + m;
  1502. }
  1503. }
  1504. template <typename type4x4>
  1505. void dequantize_q8_0(device const block_q8_0 *xb, short il, thread type4x4 & reg) {
  1506. device const int8_t * qs = ((device const int8_t *)xb->qs);
  1507. const half d = xb->d;
  1508. for (int i=0;i<16;i++) {
  1509. reg[i/4][i%4] = (qs[i + 16*il] * d);
  1510. }
  1511. }
  1512. template <typename type4x4>
  1513. void dequantize_q2_K(device const block_q2_K *xb, short il, thread type4x4 & reg) {
  1514. const half d = xb->d;
  1515. const half min = xb->dmin;
  1516. device const uint8_t * q = (device const uint8_t *)xb->qs;
  1517. half dl, ml;
  1518. uint8_t sc = xb->scales[il];
  1519. #if QK_K == 256
  1520. q = q + 32*(il/8) + 16*(il&1);
  1521. il = (il/2)%4;
  1522. #endif
  1523. half coef = il>1 ? (il>2 ? 1/64.h : 1/16.h) : (il>0 ? 1/4.h : 1.h);
  1524. uchar mask = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  1525. dl = d * (sc & 0xF) * coef, ml = min * (sc >> 4);
  1526. for (int i = 0; i < 16; ++i) {
  1527. reg[i/4][i%4] = dl * (q[i] & mask) - ml;
  1528. }
  1529. }
  1530. template <typename type4x4>
  1531. void dequantize_q3_K(device const block_q3_K *xb, short il, thread type4x4 & reg) {
  1532. const float d_all = (float)(xb->d);
  1533. device const uint8_t * q = (device const uint8_t *)xb->qs;
  1534. device const uint8_t * h = (device const uint8_t *)xb->hmask;
  1535. device const int8_t * scales = (device const int8_t *)xb->scales;
  1536. #if QK_K == 256
  1537. q = q + 32 * (il/8) + 16 * (il&1);
  1538. h = h + 16 * (il&1);
  1539. uint8_t m = 1 << (il/2);
  1540. uint16_t kmask1 = (il/4)>1 ? ((il/4)>2 ? 192 : 48) : \
  1541. ((il/4)>0 ? 12 : 3);
  1542. uint16_t kmask2 = il/8 ? 0xF0 : 0x0F;
  1543. uint16_t scale_2 = scales[il%8], scale_1 = scales[8 + il%4];
  1544. int16_t dl_int = (il/4)&1 ? (scale_2&kmask2) | ((scale_1&kmask1) << 2) : \
  1545. (scale_2&kmask2) | ((scale_1&kmask1) << 4);
  1546. float dl = il<8 ? d_all * (dl_int - 32.f) : d_all * (dl_int / 16.f - 32.f);
  1547. il = (il/2)%4;
  1548. float coef = il>1 ? (il>2 ? 1/64.h : 1/16.h) : (il>0 ? 1/4.h : 1.h);
  1549. uint8_t mask = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  1550. for (int i = 0; i < 16; ++i) {
  1551. reg[i/4][i%4] = coef * dl * ((q[i] & mask) - ((h[i] & m) ? 0 : 4.f/coef));
  1552. }
  1553. #else
  1554. float kcoef = il&1 ? 1.f/16.f : 1.f;
  1555. uint16_t kmask = il&1 ? 0xF0 : 0x0F;
  1556. float dl = d_all * ((scales[il/2] & kmask) * kcoef - 8);
  1557. float coef = il>1 ? (il>2 ? 1/64.h : 1/16.h) : (il>0 ? 1/4.h : 1.h);
  1558. uint8_t mask = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  1559. uint8_t m = 1<<(il*2);
  1560. for (int i = 0; i < 16; ++i) {
  1561. reg[i/4][i%4] = coef * dl * ((q[i] & mask) - ((h[i%8] & (m * (1 + i/8))) ? 0 : 4.f/coef));
  1562. }
  1563. #endif
  1564. }
  1565. template <typename type4x4>
  1566. void dequantize_q4_K(device const block_q4_K *xb, short il, thread type4x4 & reg) {
  1567. device const uint8_t * q = xb->qs;
  1568. #if QK_K == 256
  1569. const float d = (float)(xb->d);
  1570. const float min = (float)(xb->dmin);
  1571. short is = (il/4) * 2;
  1572. q = q + (il/4) * 32 + 16 * (il&1);
  1573. il = il%4;
  1574. const uchar4 sc = get_scale_min_k4(is, xb->scales);
  1575. const float dl = il<2 ? d * sc[0] : d * sc[2]/16.h;
  1576. const float ml = il<2 ? min * sc[1] : min * sc[3];
  1577. #else
  1578. q = q + 16 * (il&1);
  1579. device const uint8_t * s = xb->scales;
  1580. device const half2 * dh = (device const half2 *)xb->d;
  1581. const float2 d = (float2)dh[0];
  1582. const float dl = il<2 ? d[0] * (s[0]&0xF) : d[0] * (s[1]&0xF)/16.h;
  1583. const float ml = il<2 ? d[1] * (s[0]>>4) : d[1 ]* (s[1]>>4);
  1584. #endif
  1585. const ushort mask = il<2 ? 0x0F : 0xF0;
  1586. for (int i = 0; i < 16; ++i) {
  1587. reg[i/4][i%4] = dl * (q[i] & mask) - ml;
  1588. }
  1589. }
  1590. template <typename type4x4>
  1591. void dequantize_q5_K(device const block_q5_K *xb, short il, thread type4x4 & reg) {
  1592. device const uint8_t * q = xb->qs;
  1593. device const uint8_t * qh = xb->qh;
  1594. #if QK_K == 256
  1595. const float d = (float)(xb->d);
  1596. const float min = (float)(xb->dmin);
  1597. short is = (il/4) * 2;
  1598. q = q + 32 * (il/4) + 16 * (il&1);
  1599. qh = qh + 16 * (il&1);
  1600. uint8_t ul = 1 << (il/2);
  1601. il = il%4;
  1602. const uchar4 sc = get_scale_min_k4(is, xb->scales);
  1603. const float dl = il<2 ? d * sc[0] : d * sc[2]/16.h;
  1604. const float ml = il<2 ? min * sc[1] : min * sc[3];
  1605. const ushort mask = il<2 ? 0x0F : 0xF0;
  1606. const float qh_val = il<2 ? 16.f : 256.f;
  1607. for (int i = 0; i < 16; ++i) {
  1608. reg[i/4][i%4] = dl * ((q[i] & mask) + (qh[i] & ul ? qh_val : 0)) - ml;
  1609. }
  1610. #else
  1611. q = q + 16 * (il&1);
  1612. device const int8_t * s = xb->scales;
  1613. const float dl = xb->d * s[il];
  1614. uint8_t m = 1<<(il*2);
  1615. const float coef = il<2 ? 1.f : 1.f/16.f;
  1616. const ushort mask = il<2 ? 0x0F : 0xF0;
  1617. for (int i = 0; i < 16; ++i) {
  1618. reg[i/4][i%4] = coef * dl * ((q[i] & mask) - (qh[i%8] & (m*(1+i/8)) ? 0.f : 16.f/coef));
  1619. }
  1620. #endif
  1621. }
  1622. template <typename type4x4>
  1623. void dequantize_q6_K(device const block_q6_K *xb, short il, thread type4x4 & reg) {
  1624. const float d_all = (float)(xb->d);
  1625. device const uint8_t * ql = (device const uint8_t *)xb->ql;
  1626. device const uint8_t * qh = (device const uint8_t *)xb->qh;
  1627. device const int8_t * scales = (device const int8_t *)xb->scales;
  1628. #if QK_K == 256
  1629. ql = ql + 64*(il/8) + 32*((il/2)&1) + 16*(il&1);
  1630. qh = qh + 32*(il/8) + 16*(il&1);
  1631. float sc = scales[(il%2) + 2 * ((il/2))];
  1632. il = (il/2)%4;
  1633. #else
  1634. ql = ql + 16 * (il&1);
  1635. float sc = scales[il];
  1636. #endif
  1637. for (int i = 0; i < 16; ++i) {
  1638. uint16_t kmask1 = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  1639. uint16_t kmask2 = il>1 ? 0xF0 : 0x0F;
  1640. const float coef = il>1 ? 1.f/16.f : 1.f;
  1641. float q = il&1 ? ((ql[i]&kmask2)|((qh[i]&kmask1)<<2)) - 32.f/coef : \
  1642. ((ql[i]&kmask2)|((qh[i]&kmask1)<<4)) - 32.f/coef;
  1643. reg[i/4][i%4] = d_all * sc * q * coef;
  1644. }
  1645. }
  1646. template<typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread float4x4 &)>
  1647. kernel void kernel_get_rows(
  1648. device const void * src0,
  1649. device const int * src1,
  1650. device float * dst,
  1651. constant int64_t & ne00,
  1652. constant uint64_t & nb01,
  1653. constant uint64_t & nb1,
  1654. uint tgpig[[threadgroup_position_in_grid]],
  1655. uint tiitg[[thread_index_in_threadgroup]],
  1656. uint tptg[[threads_per_threadgroup]]) {
  1657. const int i = tgpig;
  1658. const int r = ((device int32_t *) src1)[i];
  1659. for (int ind = tiitg; ind < ne00/16; ind += tptg) {
  1660. float4x4 temp;
  1661. dequantize_func(
  1662. ((device const block_q *) ((device char *) src0 + r*nb01)) + ind/nl, ind%nl, temp);
  1663. *(((device float4x4 *) ((device char *) dst + i*nb1)) + ind) = temp;
  1664. }
  1665. }
  1666. #define BLOCK_SIZE_M 64 // 8 simdgroup matrices from matrix A
  1667. #define BLOCK_SIZE_N 32 // 4 simdgroup matrices from matrix A
  1668. #define BLOCK_SIZE_K 32
  1669. #define THREAD_MAT_M 4 // each thread take 4 simdgroup matrices from matrix A
  1670. #define THREAD_MAT_N 2 // each thread take 2 simdgroup matrices from matrix B
  1671. #define THREAD_PER_BLOCK 128
  1672. #define THREAD_PER_ROW 2 // 2 thread for each row in matrix A to load numbers
  1673. #define THREAD_PER_COL 4 // 4 thread for each row in matrix B to load numbers
  1674. #define SG_MAT_SIZE 64 // simdgroup matrix is of shape 8x8
  1675. #define SG_MAT_ROW 8
  1676. // each block_q contains 16*nl weights
  1677. template<typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread half4x4 &)>
  1678. kernel void kernel_mul_mm(device const uchar * src0,
  1679. device const float * src1,
  1680. device float * dst,
  1681. constant int64_t & ne00,
  1682. constant int64_t & ne02,
  1683. constant int64_t & nb01,
  1684. constant int64_t & nb02,
  1685. constant int64_t & ne12,
  1686. constant int64_t & ne0,
  1687. constant int64_t & ne1,
  1688. constant uint & gqa,
  1689. threadgroup uchar * shared_memory [[threadgroup(0)]],
  1690. uint3 tgpig[[threadgroup_position_in_grid]],
  1691. uint tiitg[[thread_index_in_threadgroup]],
  1692. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1693. threadgroup half * sa = ((threadgroup half *)shared_memory);
  1694. threadgroup float * sb = (threadgroup float *)(shared_memory + 4096);
  1695. const uint r0 = tgpig.y;
  1696. const uint r1 = tgpig.x;
  1697. const uint im = tgpig.z;
  1698. // if this block is of 64x32 shape or smaller
  1699. short n_rows = (ne0 - r0 * BLOCK_SIZE_M < BLOCK_SIZE_M) ? (ne0 - r0 * BLOCK_SIZE_M) : BLOCK_SIZE_M;
  1700. short n_cols = (ne1 - r1 * BLOCK_SIZE_N < BLOCK_SIZE_N) ? (ne1 - r1 * BLOCK_SIZE_N) : BLOCK_SIZE_N;
  1701. // a thread shouldn't load data outside of the matrix
  1702. short thread_row = ((short)tiitg/THREAD_PER_ROW) < n_rows ? ((short)tiitg/THREAD_PER_ROW) : n_rows - 1;
  1703. short thread_col = ((short)tiitg/THREAD_PER_COL) < n_cols ? ((short)tiitg/THREAD_PER_COL) : n_cols - 1;
  1704. simdgroup_half8x8 ma[4];
  1705. simdgroup_float8x8 mb[2];
  1706. simdgroup_float8x8 c_res[8];
  1707. for (int i = 0; i < 8; i++){
  1708. c_res[i] = make_filled_simdgroup_matrix<float, 8>(0.f);
  1709. }
  1710. short il = (tiitg % THREAD_PER_ROW);
  1711. uint offset0 = im/gqa*nb02; ushort offset1 = il/nl;
  1712. device const block_q * x = (device const block_q *)(src0 + (r0 * BLOCK_SIZE_M + thread_row) * nb01 + offset0) + offset1;
  1713. device const float * y = src1 + (r1 * BLOCK_SIZE_N + thread_col) * ne00 \
  1714. + BLOCK_SIZE_K / THREAD_PER_COL * (tiitg % THREAD_PER_COL) + im * ne00 * ne1;
  1715. for (int loop_k = 0; loop_k < ne00; loop_k += BLOCK_SIZE_K) {
  1716. //load data and store to threadgroup memory
  1717. half4x4 temp_a;
  1718. dequantize_func(x, il, temp_a);
  1719. threadgroup_barrier(mem_flags::mem_threadgroup);
  1720. #pragma unroll(16)
  1721. for (int i = 0; i < 16; i++) {
  1722. *(sa + SG_MAT_SIZE * ((tiitg / THREAD_PER_ROW / 8) \
  1723. + 16 * (tiitg % THREAD_PER_ROW) + 8 * (i / 8)) \
  1724. + (tiitg / THREAD_PER_ROW) % 8 + (i & 7) * 8) = temp_a[i/4][i%4];
  1725. }
  1726. *(threadgroup float2x4 *)(sb + (tiitg % THREAD_PER_COL) * 8 * 32 + 8 * (tiitg / THREAD_PER_COL)) \
  1727. = *((device float2x4 *)y);
  1728. il = (il + 2 < nl) ? il + 2 : il % 2;
  1729. x = (il < 2) ? x + (2+nl-1)/nl : x;
  1730. y += BLOCK_SIZE_K;
  1731. threadgroup_barrier(mem_flags::mem_threadgroup);
  1732. //load matrices from threadgroup memory and conduct outer products
  1733. threadgroup half * lsma = (sa + THREAD_MAT_M * SG_MAT_SIZE * (sgitg % 2));
  1734. threadgroup float * lsmb = (sb + THREAD_MAT_N * SG_MAT_SIZE * (sgitg / 2));
  1735. #pragma unroll(4)
  1736. for (int ik = 0; ik < BLOCK_SIZE_K / 8; ik++) {
  1737. #pragma unroll(4)
  1738. for (int i = 0; i < 4; i++) {
  1739. simdgroup_load(ma[i],lsma + SG_MAT_SIZE * i);
  1740. }
  1741. simdgroup_barrier(mem_flags::mem_none);
  1742. #pragma unroll(2)
  1743. for (int i = 0; i < 2; i++) {
  1744. simdgroup_load(mb[i],lsmb + SG_MAT_SIZE * i);
  1745. }
  1746. lsma += BLOCK_SIZE_M / SG_MAT_ROW * SG_MAT_SIZE;
  1747. lsmb += BLOCK_SIZE_N / SG_MAT_ROW * SG_MAT_SIZE;
  1748. #pragma unroll(8)
  1749. for (int i = 0; i < 8; i++){
  1750. simdgroup_multiply_accumulate(c_res[i], mb[i/4], ma[i%4], c_res[i]);
  1751. }
  1752. }
  1753. }
  1754. if ((r0 + 1) * BLOCK_SIZE_M <= ne0 && (r1 + 1) * BLOCK_SIZE_N <= ne1) {
  1755. device float *C = dst + BLOCK_SIZE_M * r0 + 32 * (sgitg&1) \
  1756. + (BLOCK_SIZE_N * r1 + 16 * (sgitg>>1)) * ne0 + im*ne1*ne0;
  1757. for (int i = 0; i < 8; i++) {
  1758. simdgroup_store(c_res[i], C + 8 * (i%4) + 8 * ne0 * (i/4), ne0);
  1759. }
  1760. } else {
  1761. // block is smaller than 64x32, we should avoid writing data outside of the matrix
  1762. threadgroup_barrier(mem_flags::mem_threadgroup);
  1763. threadgroup float *temp_str = ((threadgroup float *)shared_memory) \
  1764. + 32 * (sgitg&1) + (16 * (sgitg>>1)) * BLOCK_SIZE_M;
  1765. for (int i = 0; i < 8; i++) {
  1766. simdgroup_store(c_res[i], temp_str + 8 * (i%4) + 8 * BLOCK_SIZE_M * (i/4), BLOCK_SIZE_M);
  1767. }
  1768. threadgroup_barrier(mem_flags::mem_threadgroup);
  1769. device float *C = dst + BLOCK_SIZE_M * r0 + (BLOCK_SIZE_N * r1) * ne0 + im*ne1*ne0;
  1770. if (sgitg==0) {
  1771. for (int i = 0; i < n_rows; i++) {
  1772. for (int j = tiitg; j< n_cols; j += BLOCK_SIZE_N) {
  1773. *(C + i + j * ne0) = *(temp_str + i + j * BLOCK_SIZE_M);
  1774. }
  1775. }
  1776. }
  1777. }
  1778. }
  1779. #if QK_K == 256
  1780. #define QK_NL 16
  1781. #else
  1782. #define QK_NL 4
  1783. #endif
  1784. typedef void (get_rows_t)(device const void *, device const int *, device float *, constant int64_t &, \
  1785. constant uint64_t &, constant uint64_t &, uint, uint, uint);
  1786. template [[host_name("kernel_get_rows_f16")]] kernel get_rows_t kernel_get_rows<half4x4, 1, dequantize_f16>;
  1787. template [[host_name("kernel_get_rows_q4_0")]] kernel get_rows_t kernel_get_rows<block_q4_0, 2, dequantize_q4_0>;
  1788. template [[host_name("kernel_get_rows_q4_1")]] kernel get_rows_t kernel_get_rows<block_q4_1, 2, dequantize_q4_1>;
  1789. template [[host_name("kernel_get_rows_q8_0")]] kernel get_rows_t kernel_get_rows<block_q8_0, 2, dequantize_q8_0>;
  1790. template [[host_name("kernel_get_rows_q2_K")]] kernel get_rows_t kernel_get_rows<block_q2_K, QK_NL, dequantize_q2_K>;
  1791. template [[host_name("kernel_get_rows_q3_K")]] kernel get_rows_t kernel_get_rows<block_q3_K, QK_NL, dequantize_q3_K>;
  1792. template [[host_name("kernel_get_rows_q4_K")]] kernel get_rows_t kernel_get_rows<block_q4_K, QK_NL, dequantize_q4_K>;
  1793. template [[host_name("kernel_get_rows_q5_K")]] kernel get_rows_t kernel_get_rows<block_q5_K, QK_NL, dequantize_q5_K>;
  1794. template [[host_name("kernel_get_rows_q6_K")]] kernel get_rows_t kernel_get_rows<block_q6_K, QK_NL, dequantize_q6_K>;
  1795. typedef void (mat_mm_t)(device const uchar *, device const float *, device float *, constant int64_t &,\
  1796. constant int64_t &, constant int64_t &, constant int64_t &, constant int64_t &, \
  1797. constant int64_t &, constant int64_t &, constant uint &, threadgroup uchar *, uint3, uint, uint);
  1798. template [[host_name("kernel_mul_mm_f16_f32")]] kernel mat_mm_t kernel_mul_mm<half4x4, 1, dequantize_f16>;
  1799. template [[host_name("kernel_mul_mm_q4_0_f32")]] kernel mat_mm_t kernel_mul_mm<block_q4_0, 2, dequantize_q4_0>;
  1800. template [[host_name("kernel_mul_mm_q4_1_f32")]] kernel mat_mm_t kernel_mul_mm<block_q4_1, 2, dequantize_q4_1>;
  1801. template [[host_name("kernel_mul_mm_q8_0_f32")]] kernel mat_mm_t kernel_mul_mm<block_q8_0, 2, dequantize_q8_0>;
  1802. template [[host_name("kernel_mul_mm_q2_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q2_K, QK_NL, dequantize_q2_K>;
  1803. template [[host_name("kernel_mul_mm_q3_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q3_K, QK_NL, dequantize_q3_K>;
  1804. template [[host_name("kernel_mul_mm_q4_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q4_K, QK_NL, dequantize_q4_K>;
  1805. template [[host_name("kernel_mul_mm_q5_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q5_K, QK_NL, dequantize_q5_K>;
  1806. template [[host_name("kernel_mul_mm_q6_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q6_K, QK_NL, dequantize_q6_K>;