ggml-metal.metal 74 KB

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