ggml-metal.metal 87 KB

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