ggml-metal.metal 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443
  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. constant float & m1,
  733. constant int & n_heads_log2_floor,
  734. uint3 tgpig[[threadgroup_position_in_grid]],
  735. uint3 tpitg[[thread_position_in_threadgroup]],
  736. uint3 ntg[[threads_per_threadgroup]]) {
  737. const int64_t i03 = tgpig[2];
  738. const int64_t i02 = tgpig[1];
  739. const int64_t i01 = tgpig[0];
  740. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  741. const int64_t i3 = n / (ne2*ne1*ne0);
  742. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  743. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  744. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  745. device float * dst_data = (device float *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  746. float m_k;
  747. if (i2 < n_heads_log2_floor) {
  748. m_k = pow(m0, i2 + 1);
  749. } else {
  750. m_k = pow(m1, 2 * (i2 - n_heads_log2_floor) + 1);
  751. }
  752. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  753. device const float * src = (device float *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  754. dst_data[i00] = src[0] + m_k * (i00 - ne00 + 1);
  755. }
  756. }
  757. typedef void (rope_t)(
  758. device const void * src0,
  759. device const int32_t * src1,
  760. device float * dst,
  761. constant int64_t & ne00,
  762. constant int64_t & ne01,
  763. constant int64_t & ne02,
  764. constant int64_t & ne03,
  765. constant uint64_t & nb00,
  766. constant uint64_t & nb01,
  767. constant uint64_t & nb02,
  768. constant uint64_t & nb03,
  769. constant int64_t & ne0,
  770. constant int64_t & ne1,
  771. constant int64_t & ne2,
  772. constant int64_t & ne3,
  773. constant uint64_t & nb0,
  774. constant uint64_t & nb1,
  775. constant uint64_t & nb2,
  776. constant uint64_t & nb3,
  777. constant int & n_past,
  778. constant int & n_dims,
  779. constant int & mode,
  780. constant float & freq_base,
  781. constant float & freq_scale,
  782. uint tiitg[[thread_index_in_threadgroup]],
  783. uint3 tptg[[threads_per_threadgroup]],
  784. uint3 tgpig[[threadgroup_position_in_grid]]);
  785. template<typename T>
  786. kernel void kernel_rope(
  787. device const void * src0,
  788. device const int32_t * src1,
  789. device float * dst,
  790. constant int64_t & ne00,
  791. constant int64_t & ne01,
  792. constant int64_t & ne02,
  793. constant int64_t & ne03,
  794. constant uint64_t & nb00,
  795. constant uint64_t & nb01,
  796. constant uint64_t & nb02,
  797. constant uint64_t & nb03,
  798. constant int64_t & ne0,
  799. constant int64_t & ne1,
  800. constant int64_t & ne2,
  801. constant int64_t & ne3,
  802. constant uint64_t & nb0,
  803. constant uint64_t & nb1,
  804. constant uint64_t & nb2,
  805. constant uint64_t & nb3,
  806. constant int & n_past,
  807. constant int & n_dims,
  808. constant int & mode,
  809. constant float & freq_base,
  810. constant float & freq_scale,
  811. uint tiitg[[thread_index_in_threadgroup]],
  812. uint3 tptg[[threads_per_threadgroup]],
  813. uint3 tgpig[[threadgroup_position_in_grid]]) {
  814. const int64_t i3 = tgpig[2];
  815. const int64_t i2 = tgpig[1];
  816. const int64_t i1 = tgpig[0];
  817. const bool is_neox = mode & 2;
  818. device const int32_t * pos = src1;
  819. const int64_t p = pos[i2];
  820. const float theta_0 = freq_scale * (float)p;
  821. const float inv_ndims = -1.f/n_dims;
  822. if (!is_neox) {
  823. for (int64_t i0 = 2*tiitg; i0 < ne0; i0 += 2*tptg.x) {
  824. const float theta = theta_0 * pow(freq_base, inv_ndims*i0);
  825. const float cos_theta = cos(theta);
  826. const float sin_theta = sin(theta);
  827. device const T * const src = (device T *)((device char *) src0 + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
  828. device T * dst_data = (device T *)((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  829. const T x0 = src[0];
  830. const T x1 = src[1];
  831. dst_data[0] = x0*cos_theta - x1*sin_theta;
  832. dst_data[1] = x0*sin_theta + x1*cos_theta;
  833. }
  834. } else {
  835. for (int64_t ib = 0; ib < ne0/n_dims; ++ib) {
  836. for (int64_t ic = 2*tiitg; ic < n_dims; ic += 2*tptg.x) {
  837. const float theta = theta_0 * pow(freq_base, inv_ndims*ic - ib);
  838. const float cos_theta = cos(theta);
  839. const float sin_theta = sin(theta);
  840. const int64_t i0 = ib*n_dims + ic/2;
  841. device const T * const src = (device T *)((device char *) src0 + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
  842. device T * dst_data = (device T *)((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  843. const float x0 = src[0];
  844. const float x1 = src[n_dims/2];
  845. dst_data[0] = x0*cos_theta - x1*sin_theta;
  846. dst_data[n_dims/2] = x0*sin_theta + x1*cos_theta;
  847. }
  848. }
  849. }
  850. }
  851. template [[host_name("kernel_rope_f32")]] kernel rope_t kernel_rope<float>;
  852. template [[host_name("kernel_rope_f16")]] kernel rope_t kernel_rope<half>;
  853. kernel void kernel_cpy_f16_f16(
  854. device const half * src0,
  855. device half * dst,
  856. constant int64_t & ne00,
  857. constant int64_t & ne01,
  858. constant int64_t & ne02,
  859. constant int64_t & ne03,
  860. constant uint64_t & nb00,
  861. constant uint64_t & nb01,
  862. constant uint64_t & nb02,
  863. constant uint64_t & nb03,
  864. constant int64_t & ne0,
  865. constant int64_t & ne1,
  866. constant int64_t & ne2,
  867. constant int64_t & ne3,
  868. constant uint64_t & nb0,
  869. constant uint64_t & nb1,
  870. constant uint64_t & nb2,
  871. constant uint64_t & nb3,
  872. uint3 tgpig[[threadgroup_position_in_grid]],
  873. uint3 tpitg[[thread_position_in_threadgroup]],
  874. uint3 ntg[[threads_per_threadgroup]]) {
  875. const int64_t i03 = tgpig[2];
  876. const int64_t i02 = tgpig[1];
  877. const int64_t i01 = tgpig[0];
  878. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  879. const int64_t i3 = n / (ne2*ne1*ne0);
  880. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  881. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  882. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  883. device half * dst_data = (device half *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  884. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  885. device const half * src = (device half *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  886. dst_data[i00] = src[0];
  887. }
  888. }
  889. kernel void kernel_cpy_f32_f16(
  890. device const float * src0,
  891. device half * dst,
  892. constant int64_t & ne00,
  893. constant int64_t & ne01,
  894. constant int64_t & ne02,
  895. constant int64_t & ne03,
  896. constant uint64_t & nb00,
  897. constant uint64_t & nb01,
  898. constant uint64_t & nb02,
  899. constant uint64_t & nb03,
  900. constant int64_t & ne0,
  901. constant int64_t & ne1,
  902. constant int64_t & ne2,
  903. constant int64_t & ne3,
  904. constant uint64_t & nb0,
  905. constant uint64_t & nb1,
  906. constant uint64_t & nb2,
  907. constant uint64_t & nb3,
  908. uint3 tgpig[[threadgroup_position_in_grid]],
  909. uint3 tpitg[[thread_position_in_threadgroup]],
  910. uint3 ntg[[threads_per_threadgroup]]) {
  911. const int64_t i03 = tgpig[2];
  912. const int64_t i02 = tgpig[1];
  913. const int64_t i01 = tgpig[0];
  914. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  915. const int64_t i3 = n / (ne2*ne1*ne0);
  916. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  917. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  918. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  919. device half * dst_data = (device half *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  920. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  921. device const float * src = (device float *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  922. dst_data[i00] = src[0];
  923. }
  924. }
  925. kernel void kernel_cpy_f32_f32(
  926. device const float * src0,
  927. device float * dst,
  928. constant int64_t & ne00,
  929. constant int64_t & ne01,
  930. constant int64_t & ne02,
  931. constant int64_t & ne03,
  932. constant uint64_t & nb00,
  933. constant uint64_t & nb01,
  934. constant uint64_t & nb02,
  935. constant uint64_t & nb03,
  936. constant int64_t & ne0,
  937. constant int64_t & ne1,
  938. constant int64_t & ne2,
  939. constant int64_t & ne3,
  940. constant uint64_t & nb0,
  941. constant uint64_t & nb1,
  942. constant uint64_t & nb2,
  943. constant uint64_t & nb3,
  944. uint3 tgpig[[threadgroup_position_in_grid]],
  945. uint3 tpitg[[thread_position_in_threadgroup]],
  946. uint3 ntg[[threads_per_threadgroup]]) {
  947. const int64_t i03 = tgpig[2];
  948. const int64_t i02 = tgpig[1];
  949. const int64_t i01 = tgpig[0];
  950. const int64_t n = i03*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00;
  951. const int64_t i3 = n / (ne2*ne1*ne0);
  952. const int64_t i2 = (n - i3*ne2*ne1*ne0) / (ne1*ne0);
  953. const int64_t i1 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0) / ne0;
  954. const int64_t i0 = (n - i3*ne2*ne1*ne0 - i2*ne1*ne0 - i1*ne0);
  955. device float * dst_data = (device float *) ((device char *) dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  956. for (int64_t i00 = tpitg.x; i00 < ne00; i00 += ntg.x) {
  957. device const float * src = (device float *)((device char *) src0 + i03*nb03 + i02*nb02 + i01*nb01 + i00*nb00);
  958. dst_data[i00] = src[0];
  959. }
  960. }
  961. //============================================ k-quants ======================================================
  962. #ifndef QK_K
  963. #define QK_K 256
  964. #else
  965. static_assert(QK_K == 256 || QK_K == 64, "QK_K must be 256 or 64");
  966. #endif
  967. #if QK_K == 256
  968. #define K_SCALE_SIZE 12
  969. #else
  970. #define K_SCALE_SIZE 4
  971. #endif
  972. typedef struct {
  973. uint8_t scales[QK_K/16]; // scales and mins, quantized with 4 bits
  974. uint8_t qs[QK_K/4]; // quants
  975. half d; // super-block scale for quantized scales
  976. half dmin; // super-block scale for quantized mins
  977. } block_q2_K;
  978. // 84 bytes / block
  979. typedef struct {
  980. uint8_t hmask[QK_K/8]; // quants - high bit
  981. uint8_t qs[QK_K/4]; // quants - low 2 bits
  982. #if QK_K == 64
  983. uint8_t scales[2];
  984. #else
  985. uint8_t scales[K_SCALE_SIZE]; // scales, quantized with 6 bits
  986. #endif
  987. half d; // super-block scale
  988. } block_q3_K;
  989. #if QK_K == 64
  990. typedef struct {
  991. half d[2]; // super-block scales/mins
  992. uint8_t scales[2];
  993. uint8_t qs[QK_K/2]; // 4-bit quants
  994. } block_q4_K;
  995. #else
  996. typedef struct {
  997. half d; // super-block scale for quantized scales
  998. half dmin; // super-block scale for quantized mins
  999. uint8_t scales[K_SCALE_SIZE]; // scales and mins, quantized with 6 bits
  1000. uint8_t qs[QK_K/2]; // 4--bit quants
  1001. } block_q4_K;
  1002. #endif
  1003. #if QK_K == 64
  1004. typedef struct {
  1005. half d; // super-block scales/mins
  1006. int8_t scales[QK_K/16]; // 8-bit block scales
  1007. uint8_t qh[QK_K/8]; // quants, high bit
  1008. uint8_t qs[QK_K/2]; // quants, low 4 bits
  1009. } block_q5_K;
  1010. #else
  1011. typedef struct {
  1012. half d; // super-block scale for quantized scales
  1013. half dmin; // super-block scale for quantized mins
  1014. uint8_t scales[3*QK_K/64]; // scales and mins, quantized with 6 bits
  1015. uint8_t qh[QK_K/8]; // quants, high bit
  1016. uint8_t qs[QK_K/2]; // quants, low 4 bits
  1017. } block_q5_K;
  1018. // 176 bytes / block
  1019. #endif
  1020. typedef struct {
  1021. uint8_t ql[QK_K/2]; // quants, lower 4 bits
  1022. uint8_t qh[QK_K/4]; // quants, upper 2 bits
  1023. int8_t scales[QK_K/16]; // scales, quantized with 8 bits
  1024. half d; // super-block scale
  1025. } block_q6_K;
  1026. // 210 bytes / block
  1027. static inline uchar4 get_scale_min_k4(int j, device const uint8_t * q) {
  1028. uchar4 r;
  1029. if (j < 4) {
  1030. r[0] = q[j+0] & 63;
  1031. r[2] = q[j+1] & 63;
  1032. r[1] = q[j+4] & 63;
  1033. r[3] = q[j+5] & 63;
  1034. } else {
  1035. r[0] = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  1036. r[2] = (q[j+5] & 0xF) | ((q[j-3] >> 6) << 4);
  1037. r[1] = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  1038. r[3] = (q[j+5] >> 4) | ((q[j+1] >> 6) << 4);
  1039. }
  1040. return r;
  1041. }
  1042. //====================================== dot products =========================
  1043. kernel void kernel_mul_mat_q2_K_f32(
  1044. device const void * src0,
  1045. device const float * src1,
  1046. device float * dst,
  1047. constant int64_t & ne00,
  1048. constant int64_t & ne01[[buffer(4)]],
  1049. constant int64_t & ne02[[buffer(5)]],
  1050. constant int64_t & ne10[[buffer(9)]],
  1051. constant int64_t & ne12[[buffer(11)]],
  1052. constant int64_t & ne0[[buffer(15)]],
  1053. constant int64_t & ne1[[buffer(16)]],
  1054. constant uint & gqa[[buffer(17)]],
  1055. uint3 tgpig[[threadgroup_position_in_grid]],
  1056. uint tiisg[[thread_index_in_simdgroup]],
  1057. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1058. const int nb = ne00/QK_K;
  1059. const int r0 = tgpig.x;
  1060. const int r1 = tgpig.y;
  1061. const int r2 = tgpig.z;
  1062. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  1063. const int ib_row = first_row * nb;
  1064. const uint offset0 = r2/gqa*(nb*ne0);
  1065. device const block_q2_K * x = (device const block_q2_K *) src0 + ib_row + offset0;
  1066. device const float * y = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  1067. float yl[32];
  1068. float sumf[N_DST]={0.f}, all_sum;
  1069. const int step = sizeof(block_q2_K) * nb;
  1070. #if QK_K == 256
  1071. const int ix = tiisg/8; // 0...3
  1072. const int it = tiisg%8; // 0...7
  1073. const int im = it/4; // 0 or 1
  1074. const int ir = it%4; // 0...3
  1075. const int is = (8*ir)/16;// 0 or 1
  1076. device const float * y4 = y + ix * QK_K + 128 * im + 8 * ir;
  1077. for (int ib = ix; ib < nb; ib += 4) {
  1078. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  1079. for (int i = 0; i < 8; ++i) {
  1080. yl[i+ 0] = y4[i+ 0]; sumy[0] += yl[i+ 0];
  1081. yl[i+ 8] = y4[i+32]; sumy[1] += yl[i+ 8];
  1082. yl[i+16] = y4[i+64]; sumy[2] += yl[i+16];
  1083. yl[i+24] = y4[i+96]; sumy[3] += yl[i+24];
  1084. }
  1085. device const uint8_t * sc = (device const uint8_t *)x[ib].scales + 8*im + is;
  1086. device const uint16_t * qs = (device const uint16_t *)x[ib].qs + 16 * im + 4 * ir;
  1087. device const half * dh = &x[ib].d;
  1088. for (int row = 0; row < N_DST; row++) {
  1089. float4 acc1 = {0.f, 0.f, 0.f, 0.f};
  1090. float4 acc2 = {0.f, 0.f, 0.f, 0.f};
  1091. for (int i = 0; i < 8; i += 2) {
  1092. acc1[0] += yl[i+ 0] * (qs[i/2] & 0x0003);
  1093. acc2[0] += yl[i+ 1] * (qs[i/2] & 0x0300);
  1094. acc1[1] += yl[i+ 8] * (qs[i/2] & 0x000c);
  1095. acc2[1] += yl[i+ 9] * (qs[i/2] & 0x0c00);
  1096. acc1[2] += yl[i+16] * (qs[i/2] & 0x0030);
  1097. acc2[2] += yl[i+17] * (qs[i/2] & 0x3000);
  1098. acc1[3] += yl[i+24] * (qs[i/2] & 0x00c0);
  1099. acc2[3] += yl[i+25] * (qs[i/2] & 0xc000);
  1100. }
  1101. float dall = dh[0];
  1102. float dmin = dh[1] * 1.f/16.f;
  1103. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc2[0]) * (sc[0] & 0xF) * 1.f/ 1.f +
  1104. (acc1[1] + 1.f/256.f * acc2[1]) * (sc[2] & 0xF) * 1.f/ 4.f +
  1105. (acc1[2] + 1.f/256.f * acc2[2]) * (sc[4] & 0xF) * 1.f/16.f +
  1106. (acc1[3] + 1.f/256.f * acc2[3]) * (sc[6] & 0xF) * 1.f/64.f) -
  1107. dmin * (sumy[0] * (sc[0] & 0xF0) + sumy[1] * (sc[2] & 0xF0) + sumy[2] * (sc[4] & 0xF0) + sumy[3] * (sc[6] & 0xF0));
  1108. qs += step/2;
  1109. sc += step;
  1110. dh += step/2;
  1111. }
  1112. y4 += 4 * QK_K;
  1113. }
  1114. #else
  1115. const int ix = tiisg/2; // 0...15
  1116. const int it = tiisg%2; // 0...1
  1117. device const float * y4 = y + ix * QK_K + 8 * it;
  1118. for (int ib = ix; ib < nb; ib += 16) {
  1119. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  1120. for (int i = 0; i < 8; ++i) {
  1121. yl[i+ 0] = y4[i+ 0]; sumy[0] += yl[i+ 0];
  1122. yl[i+ 8] = y4[i+16]; sumy[1] += yl[i+ 8];
  1123. yl[i+16] = y4[i+32]; sumy[2] += yl[i+16];
  1124. yl[i+24] = y4[i+48]; sumy[3] += yl[i+24];
  1125. }
  1126. device const uint8_t * sc = (device const uint8_t *)x[ib].scales;
  1127. device const uint16_t * qs = (device const uint16_t *)x[ib].qs + 4 * it;
  1128. device const half * dh = &x[ib].d;
  1129. for (int row = 0; row < N_DST; row++) {
  1130. float4 acc1 = {0.f, 0.f, 0.f, 0.f};
  1131. float4 acc2 = {0.f, 0.f, 0.f, 0.f};
  1132. for (int i = 0; i < 8; i += 2) {
  1133. acc1[0] += yl[i+ 0] * (qs[i/2] & 0x0003);
  1134. acc2[0] += yl[i+ 1] * (qs[i/2] & 0x0300);
  1135. acc1[1] += yl[i+ 8] * (qs[i/2] & 0x000c);
  1136. acc2[1] += yl[i+ 9] * (qs[i/2] & 0x0c00);
  1137. acc1[2] += yl[i+16] * (qs[i/2] & 0x0030);
  1138. acc2[2] += yl[i+17] * (qs[i/2] & 0x3000);
  1139. acc1[3] += yl[i+24] * (qs[i/2] & 0x00c0);
  1140. acc2[3] += yl[i+25] * (qs[i/2] & 0xc000);
  1141. }
  1142. float dall = dh[0];
  1143. float dmin = dh[1];
  1144. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc2[0]) * (sc[0] & 0xF) * 1.f/ 1.f +
  1145. (acc1[1] + 1.f/256.f * acc2[1]) * (sc[1] & 0xF) * 1.f/ 4.f +
  1146. (acc1[2] + 1.f/256.f * acc2[2]) * (sc[2] & 0xF) * 1.f/16.f +
  1147. (acc1[3] + 1.f/256.f * acc2[3]) * (sc[3] & 0xF) * 1.f/64.f) -
  1148. dmin * (sumy[0] * (sc[0] >> 4) + sumy[1] * (sc[1] >> 4) + sumy[2] * (sc[2] >> 4) + sumy[3] * (sc[3] >> 4));
  1149. qs += step/2;
  1150. sc += step;
  1151. dh += step/2;
  1152. }
  1153. y4 += 16 * QK_K;
  1154. }
  1155. #endif
  1156. for (int row = 0; row < N_DST; ++row) {
  1157. all_sum = simd_sum(sumf[row]);
  1158. if (tiisg == 0) {
  1159. dst[r1*ne0 + r2*ne0*ne1 + first_row + row] = all_sum;
  1160. }
  1161. }
  1162. }
  1163. #if QK_K == 256
  1164. kernel void kernel_mul_mat_q3_K_f32(
  1165. device const void * src0,
  1166. device const float * src1,
  1167. device float * dst,
  1168. constant int64_t & ne00,
  1169. constant int64_t & ne01[[buffer(4)]],
  1170. constant int64_t & ne02[[buffer(5)]],
  1171. constant int64_t & ne10[[buffer(9)]],
  1172. constant int64_t & ne12[[buffer(11)]],
  1173. constant int64_t & ne0[[buffer(15)]],
  1174. constant int64_t & ne1[[buffer(16)]],
  1175. constant uint & gqa[[buffer(17)]],
  1176. uint3 tgpig[[threadgroup_position_in_grid]],
  1177. uint tiisg[[thread_index_in_simdgroup]],
  1178. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1179. const int nb = ne00/QK_K;
  1180. const int64_t r0 = tgpig.x;
  1181. const int64_t r1 = tgpig.y;
  1182. const int64_t r2 = tgpig.z;
  1183. const int first_row = (r0 * N_SIMDGROUP + sgitg) * 2;
  1184. const uint offset0 = r2/gqa*(nb*ne0);
  1185. device const block_q3_K * x = (device const block_q3_K *) src0 + first_row*nb + offset0;
  1186. device const float * yy = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  1187. float yl[32];
  1188. //const uint16_t kmask1 = 0x3030;
  1189. //const uint16_t kmask2 = 0x0f0f;
  1190. const int tid = tiisg/4;
  1191. const int ix = tiisg%4;
  1192. const int ip = tid/4; // 0 or 1
  1193. const int il = 2*((tid%4)/2); // 0 or 2
  1194. const int ir = tid%2;
  1195. const int n = 8;
  1196. const int l0 = n*ir;
  1197. // One would think that the Metal compiler would figure out that ip and il can only have
  1198. // 4 possible states, and optimize accordingly. Well, no. It needs help, and we do it
  1199. // with these two tales.
  1200. //
  1201. // Possible masks for the high bit
  1202. const ushort4 mm[4] = {{0x0001, 0x0100, 0x0002, 0x0200}, // ip = 0, il = 0
  1203. {0x0004, 0x0400, 0x0008, 0x0800}, // ip = 0, il = 2
  1204. {0x0010, 0x1000, 0x0020, 0x2000}, // ip = 1, il = 0
  1205. {0x0040, 0x4000, 0x0080, 0x8000}}; // ip = 1, il = 2
  1206. // Possible masks for the low 2 bits
  1207. const int4 qm[2] = {{0x0003, 0x0300, 0x000c, 0x0c00}, {0x0030, 0x3000, 0x00c0, 0xc000}};
  1208. const ushort4 hm = mm[2*ip + il/2];
  1209. const int shift = 2*il;
  1210. const float v1 = il == 0 ? 4.f : 64.f;
  1211. const float v2 = 4.f * v1;
  1212. const uint16_t s_shift1 = 4*ip;
  1213. const uint16_t s_shift2 = s_shift1 + il;
  1214. const int q_offset = 32*ip + l0;
  1215. const int y_offset = 128*ip + 32*il + l0;
  1216. const int step = sizeof(block_q3_K) * nb / 2;
  1217. device const float * y1 = yy + ix*QK_K + y_offset;
  1218. uint32_t scales32, aux32;
  1219. thread uint16_t * scales16 = (thread uint16_t *)&scales32;
  1220. thread const int8_t * scales = (thread const int8_t *)&scales32;
  1221. float sumf1[2] = {0.f};
  1222. float sumf2[2] = {0.f};
  1223. for (int i = ix; i < nb; i += 4) {
  1224. for (int l = 0; l < 8; ++l) {
  1225. yl[l+ 0] = y1[l+ 0];
  1226. yl[l+ 8] = y1[l+16];
  1227. yl[l+16] = y1[l+32];
  1228. yl[l+24] = y1[l+48];
  1229. }
  1230. device const uint16_t * q = (device const uint16_t *)(x[i].qs + q_offset);
  1231. device const uint16_t * h = (device const uint16_t *)(x[i].hmask + l0);
  1232. device const uint16_t * a = (device const uint16_t *)(x[i].scales);
  1233. device const half * dh = &x[i].d;
  1234. for (int row = 0; row < 2; ++row) {
  1235. const float d_all = (float)dh[0];
  1236. scales16[0] = a[4];
  1237. scales16[1] = a[5];
  1238. aux32 = ((scales32 >> s_shift2) << 4) & 0x30303030;
  1239. scales16[0] = a[il+0];
  1240. scales16[1] = a[il+1];
  1241. scales32 = ((scales32 >> s_shift1) & 0x0f0f0f0f) | aux32;
  1242. float s1 = 0, s2 = 0, s3 = 0, s4 = 0, s5 = 0, s6 = 0;
  1243. for (int l = 0; l < n; l += 2) {
  1244. const int32_t qs = q[l/2];
  1245. s1 += yl[l+0] * (qs & qm[il/2][0]);
  1246. s2 += yl[l+1] * (qs & qm[il/2][1]);
  1247. s3 += ((h[l/2] & hm[0]) ? 0.f : yl[l+0]) + ((h[l/2] & hm[1]) ? 0.f : yl[l+1]);
  1248. s4 += yl[l+16] * (qs & qm[il/2][2]);
  1249. s5 += yl[l+17] * (qs & qm[il/2][3]);
  1250. s6 += ((h[l/2] & hm[2]) ? 0.f : yl[l+16]) + ((h[l/2] & hm[3]) ? 0.f : yl[l+17]);
  1251. }
  1252. float d1 = d_all * (s1 + 1.f/256.f * s2 - s3*v1);
  1253. float d2 = d_all * (s4 + 1.f/256.f * s5 - s6*v2);
  1254. sumf1[row] += d1 * (scales[0] - 32);
  1255. sumf2[row] += d2 * (scales[2] - 32);
  1256. s1 = s2 = s3 = s4 = s5 = s6 = 0;
  1257. for (int l = 0; l < n; l += 2) {
  1258. const int32_t qs = q[l/2+8];
  1259. s1 += yl[l+8] * (qs & qm[il/2][0]);
  1260. s2 += yl[l+9] * (qs & qm[il/2][1]);
  1261. s3 += ((h[l/2+8] & hm[0]) ? 0.f : yl[l+8]) + ((h[l/2+8] & hm[1]) ? 0.f : yl[l+9]);
  1262. s4 += yl[l+24] * (qs & qm[il/2][2]);
  1263. s5 += yl[l+25] * (qs & qm[il/2][3]);
  1264. s6 += ((h[l/2+8] & hm[2]) ? 0.f : yl[l+24]) + ((h[l/2+8] & hm[3]) ? 0.f : yl[l+25]);
  1265. }
  1266. d1 = d_all * (s1 + 1.f/256.f * s2 - s3*v1);
  1267. d2 = d_all * (s4 + 1.f/256.f * s5 - s6*v2);
  1268. sumf1[row] += d1 * (scales[1] - 32);
  1269. sumf2[row] += d2 * (scales[3] - 32);
  1270. q += step;
  1271. h += step;
  1272. a += step;
  1273. dh += step;
  1274. }
  1275. y1 += 4 * QK_K;
  1276. }
  1277. for (int row = 0; row < 2; ++row) {
  1278. const float sumf = (sumf1[row] + 0.25f * sumf2[row]) / (1 << shift);
  1279. sumf1[row] = simd_sum(sumf);
  1280. }
  1281. if (tiisg == 0) {
  1282. for (int row = 0; row < 2; ++row) {
  1283. dst[r1*ne0 + r2*ne0*ne1 + first_row + row] = sumf1[row];
  1284. }
  1285. }
  1286. }
  1287. #else
  1288. kernel void kernel_mul_mat_q3_K_f32(
  1289. device const void * src0,
  1290. device const float * src1,
  1291. device float * dst,
  1292. constant int64_t & ne00,
  1293. constant int64_t & ne01[[buffer(4)]],
  1294. constant int64_t & ne02[[buffer(5)]],
  1295. constant int64_t & ne10[[buffer(9)]],
  1296. constant int64_t & ne12[[buffer(11)]],
  1297. constant int64_t & ne0[[buffer(15)]],
  1298. constant int64_t & ne1[[buffer(16)]],
  1299. constant uint & gqa[[buffer(17)]],
  1300. uint3 tgpig[[threadgroup_position_in_grid]],
  1301. uint tiisg[[thread_index_in_simdgroup]],
  1302. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1303. const int nb = ne00/QK_K;
  1304. const int64_t r0 = tgpig.x;
  1305. const int64_t r1 = tgpig.y;
  1306. const int64_t r2 = tgpig.z;
  1307. const int row = 2 * r0 + sgitg;
  1308. const uint offset0 = r2/gqa*(nb*ne0);
  1309. device const block_q3_K * x = (device const block_q3_K *) src0 + row*nb + offset0;
  1310. device const float * yy = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  1311. const int ix = tiisg/4;
  1312. const int il = 4 * (tiisg%4);// 0, 4, 8, 12
  1313. const int im = il/8; // 0, 0, 1, 1
  1314. const int in = il%8; // 0, 4, 0, 4
  1315. float2 sum = {0.f, 0.f};
  1316. for (int i = ix; i < nb; i += 8) {
  1317. const float d_all = (float)(x[i].d);
  1318. device const uint16_t * q = (device const uint16_t *)(x[i].qs + il);
  1319. device const uint16_t * h = (device const uint16_t *)(x[i].hmask + in);
  1320. device const uint16_t * s = (device const uint16_t *)(x[i].scales);
  1321. device const float * y = yy + i * QK_K + il;
  1322. const float d1 = d_all * ((int32_t)(s[0] & 0x000F) - 8);
  1323. const float d2 = d_all * ((int32_t)(s[0] & 0x00F0) - 128) * 1.f/64.f;
  1324. const float d3 = d_all * ((int32_t)(s[0] & 0x0F00) - 2048) * 1.f/4096.f;
  1325. const float d4 = d_all * ((int32_t)(s[0] & 0xF000) - 32768) * 1.f/262144.f;
  1326. for (int l = 0; l < 4; l += 2) {
  1327. const uint16_t hm = h[l/2] >> im;
  1328. sum[0] += y[l+ 0] * d1 * ((int32_t)(q[l/2] & 0x0003) - ((hm & 0x0001) ? 0 : 4))
  1329. + y[l+16] * d2 * ((int32_t)(q[l/2] & 0x000c) - ((hm & 0x0004) ? 0 : 16))
  1330. + y[l+32] * d3 * ((int32_t)(q[l/2] & 0x0030) - ((hm & 0x0010) ? 0 : 64))
  1331. + y[l+48] * d4 * ((int32_t)(q[l/2] & 0x00c0) - ((hm & 0x0040) ? 0 : 256));
  1332. sum[1] += y[l+ 1] * d1 * ((int32_t)(q[l/2] & 0x0300) - ((hm & 0x0100) ? 0 : 1024))
  1333. + y[l+17] * d2 * ((int32_t)(q[l/2] & 0x0c00) - ((hm & 0x0400) ? 0 : 4096))
  1334. + y[l+33] * d3 * ((int32_t)(q[l/2] & 0x3000) - ((hm & 0x1000) ? 0 : 16384))
  1335. + y[l+49] * d4 * ((int32_t)(q[l/2] & 0xc000) - ((hm & 0x4000) ? 0 : 65536));
  1336. }
  1337. }
  1338. const float sumf = sum[0] + sum[1] * 1.f/256.f;
  1339. const float tot = simd_sum(sumf);
  1340. if (tiisg == 0) {
  1341. dst[r1*ne0 + r2*ne0*ne1 + row] = tot;
  1342. }
  1343. }
  1344. #endif
  1345. #if QK_K == 256
  1346. kernel void kernel_mul_mat_q4_K_f32(
  1347. device const void * src0,
  1348. device const float * src1,
  1349. device float * dst,
  1350. constant int64_t & ne00,
  1351. constant int64_t & ne01 [[buffer(4)]],
  1352. constant int64_t & ne02 [[buffer(5)]],
  1353. constant int64_t & ne10 [[buffer(9)]],
  1354. constant int64_t & ne12 [[buffer(11)]],
  1355. constant int64_t & ne0 [[buffer(15)]],
  1356. constant int64_t & ne1 [[buffer(16)]],
  1357. constant uint & gqa [[buffer(17)]],
  1358. uint3 tgpig[[threadgroup_position_in_grid]],
  1359. uint tiisg[[thread_index_in_simdgroup]],
  1360. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1361. const uint16_t kmask1 = 0x3f3f;
  1362. const uint16_t kmask2 = 0x0f0f;
  1363. const uint16_t kmask3 = 0xc0c0;
  1364. const int ix = tiisg/8; // 0...3
  1365. const int it = tiisg%8; // 0...7
  1366. const int im = it/4; // 0 or 1
  1367. const int ir = it%4; // 0...3
  1368. const int nb = ne00/QK_K;
  1369. const int r0 = tgpig.x;
  1370. const int r1 = tgpig.y;
  1371. const int r2 = tgpig.z;
  1372. //const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  1373. const int first_row = r0 * N_DST;
  1374. const int ib_row = first_row * nb;
  1375. const uint offset0 = r2/gqa*(nb*ne0);
  1376. device const block_q4_K * x = (device const block_q4_K *) src0 + ib_row + offset0;
  1377. device const float * y = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  1378. float yl[16];
  1379. float yh[16];
  1380. float sumf[N_DST]={0.f}, all_sum;
  1381. const int step = sizeof(block_q4_K) * nb / 2;
  1382. device const float * y4 = y + ix * QK_K + 64 * im + 8 * ir;
  1383. uint16_t sc16[4];
  1384. thread const uint8_t * sc8 = (thread const uint8_t *)sc16;
  1385. for (int ib = ix; ib < nb; ib += 4) {
  1386. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  1387. for (int i = 0; i < 8; ++i) {
  1388. yl[i+0] = y4[i+ 0]; sumy[0] += yl[i+0];
  1389. yl[i+8] = y4[i+ 32]; sumy[1] += yl[i+8];
  1390. yh[i+0] = y4[i+128]; sumy[2] += yh[i+0];
  1391. yh[i+8] = y4[i+160]; sumy[3] += yh[i+8];
  1392. }
  1393. device const uint16_t * sc = (device const uint16_t *)x[ib].scales + im;
  1394. device const uint16_t * q1 = (device const uint16_t *)x[ib].qs + 16 * im + 4 * ir;
  1395. device const half * dh = &x[ib].d;
  1396. for (int row = 0; row < N_DST; row++) {
  1397. sc16[0] = sc[0] & kmask1;
  1398. sc16[1] = sc[2] & kmask1;
  1399. sc16[2] = ((sc[4] >> 0) & kmask2) | ((sc[0] & kmask3) >> 2);
  1400. sc16[3] = ((sc[4] >> 4) & kmask2) | ((sc[2] & kmask3) >> 2);
  1401. device const uint16_t * q2 = q1 + 32;
  1402. float4 acc1 = {0.f, 0.f, 0.f, 0.f};
  1403. float4 acc2 = {0.f, 0.f, 0.f, 0.f};
  1404. for (int i = 0; i < 8; i += 2) {
  1405. acc1[0] += yl[i+0] * (q1[i/2] & 0x000F);
  1406. acc1[1] += yl[i+1] * (q1[i/2] & 0x0F00);
  1407. acc1[2] += yl[i+8] * (q1[i/2] & 0x00F0);
  1408. acc1[3] += yl[i+9] * (q1[i/2] & 0xF000);
  1409. acc2[0] += yh[i+0] * (q2[i/2] & 0x000F);
  1410. acc2[1] += yh[i+1] * (q2[i/2] & 0x0F00);
  1411. acc2[2] += yh[i+8] * (q2[i/2] & 0x00F0);
  1412. acc2[3] += yh[i+9] * (q2[i/2] & 0xF000);
  1413. }
  1414. float dall = dh[0];
  1415. float dmin = dh[1];
  1416. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc1[1]) * sc8[0] +
  1417. (acc1[2] + 1.f/256.f * acc1[3]) * sc8[1] * 1.f/16.f +
  1418. (acc2[0] + 1.f/256.f * acc2[1]) * sc8[4] +
  1419. (acc2[2] + 1.f/256.f * acc2[3]) * sc8[5] * 1.f/16.f) -
  1420. dmin * (sumy[0] * sc8[2] + sumy[1] * sc8[3] + sumy[2] * sc8[6] + sumy[3] * sc8[7]);
  1421. q1 += step;
  1422. sc += step;
  1423. dh += step;
  1424. }
  1425. y4 += 4 * QK_K;
  1426. }
  1427. for (int row = 0; row < N_DST; ++row) {
  1428. all_sum = simd_sum(sumf[row]);
  1429. if (tiisg == 0) {
  1430. dst[r1*ne0 + r2*ne0*ne1 + first_row + row] = all_sum;
  1431. }
  1432. }
  1433. }
  1434. #else
  1435. kernel void kernel_mul_mat_q4_K_f32(
  1436. device const void * src0,
  1437. device const float * src1,
  1438. device float * dst,
  1439. constant int64_t & ne00,
  1440. constant int64_t & ne01[[buffer(4)]],
  1441. constant int64_t & ne02[[buffer(5)]],
  1442. constant int64_t & ne10[[buffer(9)]],
  1443. constant int64_t & ne12[[buffer(11)]],
  1444. constant int64_t & ne0[[buffer(15)]],
  1445. constant int64_t & ne1[[buffer(16)]],
  1446. constant uint & gqa[[buffer(17)]],
  1447. uint3 tgpig[[threadgroup_position_in_grid]],
  1448. uint tiisg[[thread_index_in_simdgroup]],
  1449. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1450. const int ix = tiisg/4; // 0...7
  1451. const int it = tiisg%4; // 0...3
  1452. const int nb = ne00/QK_K;
  1453. const int r0 = tgpig.x;
  1454. const int r1 = tgpig.y;
  1455. const int r2 = tgpig.z;
  1456. const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;
  1457. const int ib_row = first_row * nb;
  1458. const uint offset0 = r2/gqa*(nb*ne0);
  1459. device const block_q4_K * x = (device const block_q4_K *) src0 + ib_row + offset0;
  1460. device const float * y = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  1461. float yl[8];
  1462. float yh[8];
  1463. float sumf[N_DST]={0.f}, all_sum;
  1464. const int step = sizeof(block_q4_K) * nb / 2;
  1465. device const float * y4 = y + ix * QK_K + 8 * it;
  1466. uint16_t sc16[4];
  1467. for (int ib = ix; ib < nb; ib += 8) {
  1468. float2 sumy = {0.f, 0.f};
  1469. for (int i = 0; i < 8; ++i) {
  1470. yl[i] = y4[i+ 0]; sumy[0] += yl[i];
  1471. yh[i] = y4[i+32]; sumy[1] += yh[i];
  1472. }
  1473. device const uint16_t * sc = (device const uint16_t *)x[ib].scales;
  1474. device const uint16_t * qs = (device const uint16_t *)x[ib].qs + 4 * it;
  1475. device const half * dh = x[ib].d;
  1476. for (int row = 0; row < N_DST; row++) {
  1477. sc16[0] = sc[0] & 0x000f;
  1478. sc16[1] = sc[0] & 0x0f00;
  1479. sc16[2] = sc[0] & 0x00f0;
  1480. sc16[3] = sc[0] & 0xf000;
  1481. float2 acc1 = {0.f, 0.f};
  1482. float2 acc2 = {0.f, 0.f};
  1483. for (int i = 0; i < 8; i += 2) {
  1484. acc1[0] += yl[i+0] * (qs[i/2] & 0x000F);
  1485. acc1[1] += yl[i+1] * (qs[i/2] & 0x0F00);
  1486. acc2[0] += yh[i+0] * (qs[i/2] & 0x00F0);
  1487. acc2[1] += yh[i+1] * (qs[i/2] & 0xF000);
  1488. }
  1489. float dall = dh[0];
  1490. float dmin = dh[1];
  1491. sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc1[1]) * sc16[0] +
  1492. (acc2[0] + 1.f/256.f * acc2[1]) * sc16[1] * 1.f/4096.f) -
  1493. dmin * 1.f/16.f * (sumy[0] * sc16[2] + sumy[1] * sc16[3] * 1.f/256.f);
  1494. qs += step;
  1495. sc += step;
  1496. dh += step;
  1497. }
  1498. y4 += 8 * QK_K;
  1499. }
  1500. for (int row = 0; row < N_DST; ++row) {
  1501. all_sum = simd_sum(sumf[row]);
  1502. if (tiisg == 0) {
  1503. dst[r1*ne0+ r2*ne0*ne1 + first_row + row] = all_sum;
  1504. }
  1505. }
  1506. }
  1507. #endif
  1508. kernel void kernel_mul_mat_q5_K_f32(
  1509. device const void * src0,
  1510. device const float * src1,
  1511. device float * dst,
  1512. constant int64_t & ne00,
  1513. constant int64_t & ne01[[buffer(4)]],
  1514. constant int64_t & ne02[[buffer(5)]],
  1515. constant int64_t & ne10[[buffer(9)]],
  1516. constant int64_t & ne12[[buffer(11)]],
  1517. constant int64_t & ne0[[buffer(15)]],
  1518. constant int64_t & ne1[[buffer(16)]],
  1519. constant uint & gqa[[buffer(17)]],
  1520. uint3 tgpig[[threadgroup_position_in_grid]],
  1521. uint tiisg[[thread_index_in_simdgroup]],
  1522. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1523. const int nb = ne00/QK_K;
  1524. const int64_t r0 = tgpig.x;
  1525. const int64_t r1 = tgpig.y;
  1526. const int r2 = tgpig.z;
  1527. const int first_row = (r0 * N_SIMDGROUP + sgitg) * 2;
  1528. const uint offset0 = r2/gqa*(nb*ne0);
  1529. device const block_q5_K * x = (device const block_q5_K *) src0 + first_row*nb + offset0;
  1530. device const float * yy = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  1531. float sumf[2]={0.f};
  1532. const int step = sizeof(block_q5_K) * nb;
  1533. #if QK_K == 256
  1534. #
  1535. float yl[16], yh[16];
  1536. const uint16_t kmask1 = 0x3f3f;
  1537. const uint16_t kmask2 = 0x0f0f;
  1538. const uint16_t kmask3 = 0xc0c0;
  1539. const int tid = tiisg/4;
  1540. const int ix = tiisg%4;
  1541. const int im = tid/4;
  1542. const int ir = tid%4;
  1543. const int n = 8;
  1544. const int l0 = n*ir;
  1545. const int q_offset = 32*im + l0;
  1546. const int y_offset = 64*im + l0;
  1547. const uint8_t hm1 = 1u << (2*im);
  1548. const uint8_t hm2 = hm1 << 1;
  1549. const uint8_t hm3 = hm1 << 4;
  1550. const uint8_t hm4 = hm2 << 4;
  1551. uint16_t sc16[4];
  1552. thread const uint8_t * sc8 = (thread const uint8_t *)sc16;
  1553. device const float * y1 = yy + ix*QK_K + y_offset;
  1554. for (int i = ix; i < nb; i += 4) {
  1555. device const uint8_t * q1 = x[i].qs + q_offset;
  1556. device const uint8_t * qh = x[i].qh + l0;
  1557. device const half * dh = &x[i].d;
  1558. device const uint16_t * a = (device const uint16_t *)x[i].scales + im;
  1559. device const float * y2 = y1 + 128;
  1560. float4 sumy = {0.f, 0.f, 0.f, 0.f};
  1561. for (int l = 0; l < 8; ++l) {
  1562. yl[l+0] = y1[l+ 0]; sumy[0] += yl[l+0];
  1563. yl[l+8] = y1[l+32]; sumy[1] += yl[l+8];
  1564. yh[l+0] = y2[l+ 0]; sumy[2] += yh[l+0];
  1565. yh[l+8] = y2[l+32]; sumy[3] += yh[l+8];
  1566. }
  1567. for (int row = 0; row < 2; ++row) {
  1568. device const uint8_t * q2 = q1 + 64;
  1569. sc16[0] = a[0] & kmask1;
  1570. sc16[1] = a[2] & kmask1;
  1571. sc16[2] = ((a[4] >> 0) & kmask2) | ((a[0] & kmask3) >> 2);
  1572. sc16[3] = ((a[4] >> 4) & kmask2) | ((a[2] & kmask3) >> 2);
  1573. float4 acc1 = {0.f};
  1574. float4 acc2 = {0.f};
  1575. for (int l = 0; l < n; ++l) {
  1576. uint8_t h = qh[l];
  1577. acc1[0] += yl[l+0] * (q1[l] & 0x0F);
  1578. acc1[1] += yl[l+8] * (q1[l] & 0xF0);
  1579. acc1[2] += yh[l+0] * (q2[l] & 0x0F);
  1580. acc1[3] += yh[l+8] * (q2[l] & 0xF0);
  1581. acc2[0] += h & hm1 ? yl[l+0] : 0.f;
  1582. acc2[1] += h & hm2 ? yl[l+8] : 0.f;
  1583. acc2[2] += h & hm3 ? yh[l+0] : 0.f;
  1584. acc2[3] += h & hm4 ? yh[l+8] : 0.f;
  1585. }
  1586. const float dall = dh[0];
  1587. const float dmin = dh[1];
  1588. sumf[row] += dall * (sc8[0] * (acc1[0] + 16.f*acc2[0]) +
  1589. sc8[1] * (acc1[1]/16.f + 16.f*acc2[1]) +
  1590. sc8[4] * (acc1[2] + 16.f*acc2[2]) +
  1591. sc8[5] * (acc1[3]/16.f + 16.f*acc2[3])) -
  1592. dmin * (sumy[0] * sc8[2] + sumy[1] * sc8[3] + sumy[2] * sc8[6] + sumy[3] * sc8[7]);
  1593. q1 += step;
  1594. qh += step;
  1595. dh += step/2;
  1596. a += step/2;
  1597. }
  1598. y1 += 4 * QK_K;
  1599. }
  1600. #else
  1601. float yl[8], yh[8];
  1602. const int il = 4 * (tiisg/8); // 0, 4, 8, 12
  1603. const int ix = tiisg%8;
  1604. const int im = il/8; // 0, 0, 1, 1
  1605. const int in = il%8; // 0, 4, 0, 4
  1606. device const float * y = yy + ix*QK_K + il;
  1607. for (int i = ix; i < nb; i += 8) {
  1608. for (int l = 0; l < 4; ++l) {
  1609. yl[l+0] = y[l+ 0];
  1610. yl[l+4] = y[l+16];
  1611. yh[l+0] = y[l+32];
  1612. yh[l+4] = y[l+48];
  1613. }
  1614. device const half * dh = &x[i].d;
  1615. device const uint8_t * q = x[i].qs + il;
  1616. device const uint8_t * h = x[i].qh + in;
  1617. device const int8_t * s = x[i].scales;
  1618. for (int row = 0; row < 2; ++row) {
  1619. const float d = dh[0];
  1620. float2 acc = {0.f, 0.f};
  1621. for (int l = 0; l < 4; ++l) {
  1622. const uint8_t hl = h[l] >> im;
  1623. acc[0] += yl[l+0] * s[0] * ((int16_t)(q[l+ 0] & 0x0F) - (hl & 0x01 ? 0 : 16))
  1624. + yl[l+4] * s[1] * ((int16_t)(q[l+16] & 0x0F) - (hl & 0x04 ? 0 : 16));
  1625. acc[1] += yh[l+0] * s[2] * ((int16_t)(q[l+ 0] & 0xF0) - (hl & 0x10 ? 0 : 256))
  1626. + yh[l+4] * s[3] * ((int16_t)(q[l+16] & 0xF0) - (hl & 0x40 ? 0 : 256));
  1627. }
  1628. sumf[row] += d * (acc[0] + 1.f/16.f * acc[1]);
  1629. q += step;
  1630. h += step;
  1631. s += step;
  1632. dh += step/2;
  1633. }
  1634. y += 8 * QK_K;
  1635. }
  1636. #endif
  1637. for (int row = 0; row < 2; ++row) {
  1638. const float tot = simd_sum(sumf[row]);
  1639. if (tiisg == 0) {
  1640. dst[r1*ne0 + r2*ne0*ne1 + first_row + row] = tot;
  1641. }
  1642. }
  1643. }
  1644. kernel void kernel_mul_mat_q6_K_f32(
  1645. device const void * src0,
  1646. device const float * src1,
  1647. device float * dst,
  1648. constant int64_t & ne00,
  1649. constant int64_t & ne01[[buffer(4)]],
  1650. constant int64_t & ne02[[buffer(5)]],
  1651. constant int64_t & ne10[[buffer(9)]],
  1652. constant int64_t & ne12[[buffer(11)]],
  1653. constant int64_t & ne0[[buffer(15)]],
  1654. constant int64_t & ne1[[buffer(16)]],
  1655. constant uint & gqa[[buffer(17)]],
  1656. uint3 tgpig[[threadgroup_position_in_grid]],
  1657. uint tiisg[[thread_index_in_simdgroup]],
  1658. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1659. const uint8_t kmask1 = 0x03;
  1660. const uint8_t kmask2 = 0x0C;
  1661. const uint8_t kmask3 = 0x30;
  1662. const uint8_t kmask4 = 0xC0;
  1663. const int nb = ne00/QK_K;
  1664. const int64_t r0 = tgpig.x;
  1665. const int64_t r1 = tgpig.y;
  1666. const int r2 = tgpig.z;
  1667. const int row = 2 * r0 + sgitg;
  1668. const uint offset0 = r2/gqa*(nb*ne0);
  1669. device const block_q6_K * x = (device const block_q6_K *) src0 + row * nb + offset0;
  1670. device const float * yy = (device const float *) src1 + r1*ne10 + r2*ne00*ne1;
  1671. float sumf = 0;
  1672. #if QK_K == 256
  1673. const int tid = tiisg/2;
  1674. const int ix = tiisg%2;
  1675. const int ip = tid/8; // 0 or 1
  1676. const int il = tid%8;
  1677. const int n = 4;
  1678. const int l0 = n*il;
  1679. const int is = 8*ip + l0/16;
  1680. const int y_offset = 128*ip + l0;
  1681. const int q_offset_l = 64*ip + l0;
  1682. const int q_offset_h = 32*ip + l0;
  1683. for (int i = ix; i < nb; i += 2) {
  1684. device const uint8_t * q1 = x[i].ql + q_offset_l;
  1685. device const uint8_t * q2 = q1 + 32;
  1686. device const uint8_t * qh = x[i].qh + q_offset_h;
  1687. device const int8_t * sc = x[i].scales + is;
  1688. device const float * y = yy + i * QK_K + y_offset;
  1689. const float dall = x[i].d;
  1690. float4 sums = {0.f, 0.f, 0.f, 0.f};
  1691. for (int l = 0; l < n; ++l) {
  1692. sums[0] += y[l+ 0] * ((int8_t)((q1[l] & 0xF) | ((qh[l] & kmask1) << 4)) - 32);
  1693. sums[1] += y[l+32] * ((int8_t)((q2[l] & 0xF) | ((qh[l] & kmask2) << 2)) - 32);
  1694. sums[2] += y[l+64] * ((int8_t)((q1[l] >> 4) | ((qh[l] & kmask3) << 0)) - 32);
  1695. sums[3] += y[l+96] * ((int8_t)((q2[l] >> 4) | ((qh[l] & kmask4) >> 2)) - 32);
  1696. }
  1697. sumf += dall * (sums[0] * sc[0] + sums[1] * sc[2] + sums[2] * sc[4] + sums[3] * sc[6]);
  1698. }
  1699. #else
  1700. const int ix = tiisg/4;
  1701. const int il = 4*(tiisg%4);
  1702. for (int i = ix; i < nb; i += 8) {
  1703. device const float * y = yy + i * QK_K + il;
  1704. device const uint8_t * ql = x[i].ql + il;
  1705. device const uint8_t * qh = x[i].qh + il;
  1706. device const int8_t * s = x[i].scales;
  1707. const float d = x[i].d;
  1708. float4 sums = {0.f, 0.f, 0.f, 0.f};
  1709. for (int l = 0; l < 4; ++l) {
  1710. sums[0] += y[l+ 0] * ((int8_t)((ql[l+ 0] & 0xF) | ((qh[l] & kmask1) << 4)) - 32);
  1711. sums[1] += y[l+16] * ((int8_t)((ql[l+16] & 0xF) | ((qh[l] & kmask2) << 2)) - 32);
  1712. sums[2] += y[l+32] * ((int8_t)((ql[l+ 0] >> 4) | ((qh[l] & kmask3) >> 0)) - 32);
  1713. sums[3] += y[l+48] * ((int8_t)((ql[l+16] >> 4) | ((qh[l] & kmask4) >> 2)) - 32);
  1714. }
  1715. sumf += d * (sums[0] * s[0] + sums[1] * s[1] + sums[2] * s[2] + sums[3] * s[3]);
  1716. }
  1717. #endif
  1718. const float tot = simd_sum(sumf);
  1719. if (tiisg == 0) {
  1720. dst[r1*ne0 + r2*ne0*ne1 + row] = tot;
  1721. }
  1722. }
  1723. //============================= templates and their specializations =============================
  1724. // NOTE: this is not dequantizing - we are simply fitting the template
  1725. template <typename type4x4>
  1726. void dequantize_f32(device const float4x4 * src, short il, thread type4x4 & reg) {
  1727. float4x4 temp = *(((device float4x4 *)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_f16(device const half4x4 * src, short il, thread type4x4 & reg) {
  1734. half4x4 temp = *(((device half4x4 *)src));
  1735. for (int i = 0; i < 16; i++){
  1736. reg[i/4][i%4] = temp[i/4][i%4];
  1737. }
  1738. }
  1739. template <typename type4x4>
  1740. void dequantize_q4_0(device const block_q4_0 *xb, short il, thread type4x4 & reg) {
  1741. device const uint16_t * qs = ((device const uint16_t *)xb + 1);
  1742. const float d1 = il ? (xb->d / 16.h) : xb->d;
  1743. const float d2 = d1 / 256.f;
  1744. const float md = -8.h * xb->d;
  1745. const ushort mask0 = il ? 0x00F0 : 0x000F;
  1746. const ushort mask1 = mask0 << 8;
  1747. for (int i=0;i<8;i++) {
  1748. reg[i/2][2*(i%2)+0] = d1 * (qs[i] & mask0) + md;
  1749. reg[i/2][2*(i%2)+1] = d2 * (qs[i] & mask1) + md;
  1750. }
  1751. }
  1752. template <typename type4x4>
  1753. void dequantize_q4_1(device const block_q4_1 *xb, short il, thread type4x4 & reg) {
  1754. device const uint16_t * qs = ((device const uint16_t *)xb + 2);
  1755. const float d1 = il ? (xb->d / 16.h) : xb->d;
  1756. const float d2 = d1 / 256.f;
  1757. const float m = xb->m;
  1758. const ushort mask0 = il ? 0x00F0 : 0x000F;
  1759. const ushort mask1 = mask0 << 8;
  1760. for (int i=0;i<8;i++) {
  1761. reg[i/2][2*(i%2)+0] = ((qs[i] & mask0) * d1) + m;
  1762. reg[i/2][2*(i%2)+1] = ((qs[i] & mask1) * d2) + m;
  1763. }
  1764. }
  1765. template <typename type4x4>
  1766. void dequantize_q8_0(device const block_q8_0 *xb, short il, thread type4x4 & reg) {
  1767. device const int8_t * qs = ((device const int8_t *)xb->qs);
  1768. const half d = xb->d;
  1769. for (int i=0;i<16;i++) {
  1770. reg[i/4][i%4] = (qs[i + 16*il] * d);
  1771. }
  1772. }
  1773. template <typename type4x4>
  1774. void dequantize_q2_K(device const block_q2_K *xb, short il, thread type4x4 & reg) {
  1775. const half d = xb->d;
  1776. const half min = xb->dmin;
  1777. device const uint8_t * q = (device const uint8_t *)xb->qs;
  1778. half dl, ml;
  1779. uint8_t sc = xb->scales[il];
  1780. #if QK_K == 256
  1781. q = q + 32*(il/8) + 16*(il&1);
  1782. il = (il/2)%4;
  1783. #endif
  1784. half coef = il>1 ? (il>2 ? 1/64.h : 1/16.h) : (il>0 ? 1/4.h : 1.h);
  1785. uchar mask = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  1786. dl = d * (sc & 0xF) * coef, ml = min * (sc >> 4);
  1787. for (int i = 0; i < 16; ++i) {
  1788. reg[i/4][i%4] = dl * (q[i] & mask) - ml;
  1789. }
  1790. }
  1791. template <typename type4x4>
  1792. void dequantize_q3_K(device const block_q3_K *xb, short il, thread type4x4 & reg) {
  1793. const half d_all = xb->d;
  1794. device const uint8_t * q = (device const uint8_t *)xb->qs;
  1795. device const uint8_t * h = (device const uint8_t *)xb->hmask;
  1796. device const int8_t * scales = (device const int8_t *)xb->scales;
  1797. #if QK_K == 256
  1798. q = q + 32 * (il/8) + 16 * (il&1);
  1799. h = h + 16 * (il&1);
  1800. uint8_t m = 1 << (il/2);
  1801. uint16_t kmask1 = (il/4)>1 ? ((il/4)>2 ? 192 : 48) : \
  1802. ((il/4)>0 ? 12 : 3);
  1803. uint16_t kmask2 = il/8 ? 0xF0 : 0x0F;
  1804. uint16_t scale_2 = scales[il%8], scale_1 = scales[8 + il%4];
  1805. int16_t dl_int = (il/4)&1 ? (scale_2&kmask2) | ((scale_1&kmask1) << 2)
  1806. : (scale_2&kmask2) | ((scale_1&kmask1) << 4);
  1807. half dl = il<8 ? d_all * (dl_int - 32.h) : d_all * (dl_int / 16.h - 32.h);
  1808. const half ml = 4.h * dl;
  1809. il = (il/2) & 3;
  1810. const half coef = il>1 ? (il>2 ? 1/64.h : 1/16.h) : (il>0 ? 1/4.h : 1.h);
  1811. const uint8_t mask = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  1812. dl *= coef;
  1813. for (int i = 0; i < 16; ++i) {
  1814. reg[i/4][i%4] = dl * (q[i] & mask) - (h[i] & m ? 0 : ml);
  1815. }
  1816. #else
  1817. float kcoef = il&1 ? 1.f/16.f : 1.f;
  1818. uint16_t kmask = il&1 ? 0xF0 : 0x0F;
  1819. float dl = d_all * ((scales[il/2] & kmask) * kcoef - 8);
  1820. float coef = il>1 ? (il>2 ? 1/64.h : 1/16.h) : (il>0 ? 1/4.h : 1.h);
  1821. uint8_t mask = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  1822. uint8_t m = 1<<(il*2);
  1823. for (int i = 0; i < 16; ++i) {
  1824. reg[i/4][i%4] = coef * dl * ((q[i] & mask) - ((h[i%8] & (m * (1 + i/8))) ? 0 : 4.f/coef));
  1825. }
  1826. #endif
  1827. }
  1828. static inline uchar2 get_scale_min_k4_just2(int j, int k, device const uchar * q) {
  1829. return j < 4 ? uchar2{uchar(q[j+0+k] & 63), uchar(q[j+4+k] & 63)}
  1830. : 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))};
  1831. }
  1832. template <typename type4x4>
  1833. void dequantize_q4_K(device const block_q4_K *xb, short il, thread type4x4 & reg) {
  1834. device const uchar * q = xb->qs;
  1835. #if QK_K == 256
  1836. short is = (il/4) * 2;
  1837. q = q + (il/4) * 32 + 16 * (il&1);
  1838. il = il & 3;
  1839. const uchar2 sc = get_scale_min_k4_just2(is, il/2, xb->scales);
  1840. const half d = il < 2 ? xb->d : xb->d / 16.h;
  1841. const half min = xb->dmin;
  1842. const half dl = d * sc[0];
  1843. const half ml = min * sc[1];
  1844. #else
  1845. q = q + 16 * (il&1);
  1846. device const uint8_t * s = xb->scales;
  1847. device const half2 * dh = (device const half2 *)xb->d;
  1848. const float2 d = (float2)dh[0];
  1849. const float dl = il<2 ? d[0] * (s[0]&0xF) : d[0] * (s[1]&0xF)/16.h;
  1850. const float ml = il<2 ? d[1] * (s[0]>>4) : d[1] * (s[1]>>4);
  1851. #endif
  1852. const ushort mask = il<2 ? 0x0F : 0xF0;
  1853. for (int i = 0; i < 16; ++i) {
  1854. reg[i/4][i%4] = dl * (q[i] & mask) - ml;
  1855. }
  1856. }
  1857. template <typename type4x4>
  1858. void dequantize_q5_K(device const block_q5_K *xb, short il, thread type4x4 & reg) {
  1859. device const uint8_t * q = xb->qs;
  1860. device const uint8_t * qh = xb->qh;
  1861. #if QK_K == 256
  1862. short is = (il/4) * 2;
  1863. q = q + 32 * (il/4) + 16 * (il&1);
  1864. qh = qh + 16 * (il&1);
  1865. uint8_t ul = 1 << (il/2);
  1866. il = il & 3;
  1867. const uchar2 sc = get_scale_min_k4_just2(is, il/2, xb->scales);
  1868. const half d = il < 2 ? xb->d : xb->d / 16.h;
  1869. const half min = xb->dmin;
  1870. const half dl = d * sc[0];
  1871. const half ml = min * sc[1];
  1872. const ushort mask = il<2 ? 0x0F : 0xF0;
  1873. const half qh_val = il<2 ? 16.h : 256.h;
  1874. for (int i = 0; i < 16; ++i) {
  1875. reg[i/4][i%4] = dl * ((q[i] & mask) + (qh[i] & ul ? qh_val : 0)) - ml;
  1876. }
  1877. #else
  1878. q = q + 16 * (il&1);
  1879. device const int8_t * s = xb->scales;
  1880. const float dl = xb->d * s[il];
  1881. uint8_t m = 1<<(il*2);
  1882. const float coef = il<2 ? 1.f : 1.f/16.f;
  1883. const ushort mask = il<2 ? 0x0F : 0xF0;
  1884. for (int i = 0; i < 16; ++i) {
  1885. reg[i/4][i%4] = coef * dl * ((q[i] & mask) - (qh[i%8] & (m*(1+i/8)) ? 0.f : 16.f/coef));
  1886. }
  1887. #endif
  1888. }
  1889. template <typename type4x4>
  1890. void dequantize_q6_K(device const block_q6_K *xb, short il, thread type4x4 & reg) {
  1891. const half d_all = xb->d;
  1892. device const uint8_t * ql = (device const uint8_t *)xb->ql;
  1893. device const uint8_t * qh = (device const uint8_t *)xb->qh;
  1894. device const int8_t * scales = (device const int8_t *)xb->scales;
  1895. #if QK_K == 256
  1896. ql = ql + 64*(il/8) + 32*((il/2)&1) + 16*(il&1);
  1897. qh = qh + 32*(il/8) + 16*(il&1);
  1898. half sc = scales[(il%2) + 2 * ((il/2))];
  1899. il = (il/2) & 3;
  1900. #else
  1901. ql = ql + 16 * (il&1);
  1902. half sc = scales[il];
  1903. #endif
  1904. const uint16_t kmask1 = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);
  1905. const uint16_t kmask2 = il>1 ? 0xF0 : 0x0F;
  1906. const half coef = il>1 ? 1.f/16.h : 1.h;
  1907. const half ml = d_all * sc * 32.h;
  1908. const half dl = d_all * sc * coef;
  1909. for (int i = 0; i < 16; ++i) {
  1910. const half q = il&1 ? ((ql[i] & kmask2) | ((qh[i] & kmask1) << 2))
  1911. : ((ql[i] & kmask2) | ((qh[i] & kmask1) << 4));
  1912. reg[i/4][i%4] = dl * q - ml;
  1913. }
  1914. }
  1915. template<typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread float4x4 &)>
  1916. kernel void kernel_get_rows(
  1917. device const void * src0,
  1918. device const int * src1,
  1919. device float * dst,
  1920. constant int64_t & ne00,
  1921. constant uint64_t & nb01,
  1922. constant uint64_t & nb1,
  1923. uint tgpig[[threadgroup_position_in_grid]],
  1924. uint tiitg[[thread_index_in_threadgroup]],
  1925. uint tptg[[threads_per_threadgroup]]) {
  1926. const int i = tgpig;
  1927. const int r = ((device int32_t *) src1)[i];
  1928. for (int ind = tiitg; ind < ne00/16; ind += tptg) {
  1929. float4x4 temp;
  1930. dequantize_func(
  1931. ((device const block_q *) ((device char *) src0 + r*nb01)) + ind/nl, ind%nl, temp);
  1932. *(((device float4x4 *) ((device char *) dst + i*nb1)) + ind) = temp;
  1933. }
  1934. }
  1935. #define BLOCK_SIZE_M 64 // 8 simdgroup matrices from matrix A
  1936. #define BLOCK_SIZE_N 32 // 4 simdgroup matrices from matrix A
  1937. #define BLOCK_SIZE_K 32
  1938. #define THREAD_MAT_M 4 // each thread take 4 simdgroup matrices from matrix A
  1939. #define THREAD_MAT_N 2 // each thread take 2 simdgroup matrices from matrix B
  1940. #define THREAD_PER_BLOCK 128
  1941. #define THREAD_PER_ROW 2 // 2 thread for each row in matrix A to load numbers
  1942. #define THREAD_PER_COL 4 // 4 thread for each row in matrix B to load numbers
  1943. #define SG_MAT_SIZE 64 // simdgroup matrix is of shape 8x8
  1944. #define SG_MAT_ROW 8
  1945. // each block_q contains 16*nl weights
  1946. template<typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread half4x4 &)>
  1947. kernel void kernel_mul_mm(device const uchar * src0,
  1948. device const uchar * src1,
  1949. device float * dst,
  1950. constant int64_t & ne00,
  1951. constant int64_t & ne02,
  1952. constant int64_t & nb01,
  1953. constant int64_t & nb02,
  1954. constant int64_t & ne12,
  1955. constant int64_t & nb10,
  1956. constant int64_t & nb11,
  1957. constant int64_t & nb12,
  1958. constant int64_t & ne0,
  1959. constant int64_t & ne1,
  1960. constant uint & gqa,
  1961. threadgroup uchar * shared_memory [[threadgroup(0)]],
  1962. uint3 tgpig[[threadgroup_position_in_grid]],
  1963. uint tiitg[[thread_index_in_threadgroup]],
  1964. uint sgitg[[simdgroup_index_in_threadgroup]]) {
  1965. threadgroup half * sa = (threadgroup half *)(shared_memory);
  1966. threadgroup float * sb = (threadgroup float *)(shared_memory + 4096);
  1967. const uint r0 = tgpig.y;
  1968. const uint r1 = tgpig.x;
  1969. const uint im = tgpig.z;
  1970. // if this block is of 64x32 shape or smaller
  1971. short n_rows = (ne0 - r0 * BLOCK_SIZE_M < BLOCK_SIZE_M) ? (ne0 - r0 * BLOCK_SIZE_M) : BLOCK_SIZE_M;
  1972. short n_cols = (ne1 - r1 * BLOCK_SIZE_N < BLOCK_SIZE_N) ? (ne1 - r1 * BLOCK_SIZE_N) : BLOCK_SIZE_N;
  1973. // a thread shouldn't load data outside of the matrix
  1974. short thread_row = ((short)tiitg/THREAD_PER_ROW) < n_rows ? ((short)tiitg/THREAD_PER_ROW) : n_rows - 1;
  1975. short thread_col = ((short)tiitg/THREAD_PER_COL) < n_cols ? ((short)tiitg/THREAD_PER_COL) : n_cols - 1;
  1976. simdgroup_half8x8 ma[4];
  1977. simdgroup_float8x8 mb[2];
  1978. simdgroup_float8x8 c_res[8];
  1979. for (int i = 0; i < 8; i++){
  1980. c_res[i] = make_filled_simdgroup_matrix<float, 8>(0.f);
  1981. }
  1982. short il = (tiitg % THREAD_PER_ROW);
  1983. uint offset0 = im/gqa*nb02;
  1984. ushort offset1 = il/nl;
  1985. device const block_q * x = (device const block_q *)(src0 + (r0 * BLOCK_SIZE_M + thread_row) * nb01 + offset0) + offset1;
  1986. device const float * y = (device const float *)(src1
  1987. + nb12 * im
  1988. + nb11 * (r1 * BLOCK_SIZE_N + thread_col)
  1989. + nb10 * (BLOCK_SIZE_K / THREAD_PER_COL * (tiitg % THREAD_PER_COL)));
  1990. for (int loop_k = 0; loop_k < ne00; loop_k += BLOCK_SIZE_K) {
  1991. //load data and store to threadgroup memory
  1992. half4x4 temp_a;
  1993. dequantize_func(x, il, temp_a);
  1994. threadgroup_barrier(mem_flags::mem_threadgroup);
  1995. #pragma unroll(16)
  1996. for (int i = 0; i < 16; i++) {
  1997. *(sa + SG_MAT_SIZE * ((tiitg / THREAD_PER_ROW / 8) \
  1998. + 16 * (tiitg % THREAD_PER_ROW) + 8 * (i / 8)) \
  1999. + (tiitg / THREAD_PER_ROW) % 8 + (i & 7) * 8) = temp_a[i/4][i%4];
  2000. }
  2001. *(threadgroup float2x4 *)(sb + (tiitg % THREAD_PER_COL) * 8 * 32 + 8 * (tiitg / THREAD_PER_COL)) \
  2002. = *((device float2x4 *)y);
  2003. il = (il + 2 < nl) ? il + 2 : il % 2;
  2004. x = (il < 2) ? x + (2+nl-1)/nl : x;
  2005. y += BLOCK_SIZE_K;
  2006. threadgroup_barrier(mem_flags::mem_threadgroup);
  2007. //load matrices from threadgroup memory and conduct outer products
  2008. threadgroup half * lsma = (sa + THREAD_MAT_M * SG_MAT_SIZE * (sgitg % 2));
  2009. threadgroup float * lsmb = (sb + THREAD_MAT_N * SG_MAT_SIZE * (sgitg / 2));
  2010. #pragma unroll(4)
  2011. for (int ik = 0; ik < BLOCK_SIZE_K / 8; ik++) {
  2012. #pragma unroll(4)
  2013. for (int i = 0; i < 4; i++) {
  2014. simdgroup_load(ma[i],lsma + SG_MAT_SIZE * i);
  2015. }
  2016. simdgroup_barrier(mem_flags::mem_none);
  2017. #pragma unroll(2)
  2018. for (int i = 0; i < 2; i++) {
  2019. simdgroup_load(mb[i],lsmb + SG_MAT_SIZE * i);
  2020. }
  2021. lsma += BLOCK_SIZE_M / SG_MAT_ROW * SG_MAT_SIZE;
  2022. lsmb += BLOCK_SIZE_N / SG_MAT_ROW * SG_MAT_SIZE;
  2023. #pragma unroll(8)
  2024. for (int i = 0; i < 8; i++){
  2025. simdgroup_multiply_accumulate(c_res[i], mb[i/4], ma[i%4], c_res[i]);
  2026. }
  2027. }
  2028. }
  2029. if ((r0 + 1) * BLOCK_SIZE_M <= ne0 && (r1 + 1) * BLOCK_SIZE_N <= ne1) {
  2030. device float *C = dst + BLOCK_SIZE_M * r0 + 32 * (sgitg&1) \
  2031. + (BLOCK_SIZE_N * r1 + 16 * (sgitg>>1)) * ne0 + im*ne1*ne0;
  2032. for (int i = 0; i < 8; i++) {
  2033. simdgroup_store(c_res[i], C + 8 * (i%4) + 8 * ne0 * (i/4), ne0);
  2034. }
  2035. } else {
  2036. // block is smaller than 64x32, we should avoid writing data outside of the matrix
  2037. threadgroup_barrier(mem_flags::mem_threadgroup);
  2038. threadgroup float *temp_str = ((threadgroup float *)shared_memory) \
  2039. + 32 * (sgitg&1) + (16 * (sgitg>>1)) * BLOCK_SIZE_M;
  2040. for (int i = 0; i < 8; i++) {
  2041. simdgroup_store(c_res[i], temp_str + 8 * (i%4) + 8 * BLOCK_SIZE_M * (i/4), BLOCK_SIZE_M);
  2042. }
  2043. threadgroup_barrier(mem_flags::mem_threadgroup);
  2044. device float *C = dst + BLOCK_SIZE_M * r0 + (BLOCK_SIZE_N * r1) * ne0 + im*ne1*ne0;
  2045. if (sgitg==0) {
  2046. for (int i = 0; i < n_rows; i++) {
  2047. for (int j = tiitg; j< n_cols; j += BLOCK_SIZE_N) {
  2048. *(C + i + j * ne0) = *(temp_str + i + j * BLOCK_SIZE_M);
  2049. }
  2050. }
  2051. }
  2052. }
  2053. }
  2054. #if QK_K == 256
  2055. #define QK_NL 16
  2056. #else
  2057. #define QK_NL 4
  2058. #endif
  2059. typedef void (get_rows_t)(device const void *, device const int *, device float *, constant int64_t &, \
  2060. constant uint64_t &, constant uint64_t &, uint, uint, uint);
  2061. template [[host_name("kernel_get_rows_f32")]] kernel get_rows_t kernel_get_rows<float4x4, 1, dequantize_f32>;
  2062. template [[host_name("kernel_get_rows_f16")]] kernel get_rows_t kernel_get_rows<half4x4, 1, dequantize_f16>;
  2063. template [[host_name("kernel_get_rows_q4_0")]] kernel get_rows_t kernel_get_rows<block_q4_0, 2, dequantize_q4_0>;
  2064. template [[host_name("kernel_get_rows_q4_1")]] kernel get_rows_t kernel_get_rows<block_q4_1, 2, dequantize_q4_1>;
  2065. template [[host_name("kernel_get_rows_q8_0")]] kernel get_rows_t kernel_get_rows<block_q8_0, 2, dequantize_q8_0>;
  2066. template [[host_name("kernel_get_rows_q2_K")]] kernel get_rows_t kernel_get_rows<block_q2_K, QK_NL, dequantize_q2_K>;
  2067. template [[host_name("kernel_get_rows_q3_K")]] kernel get_rows_t kernel_get_rows<block_q3_K, QK_NL, dequantize_q3_K>;
  2068. template [[host_name("kernel_get_rows_q4_K")]] kernel get_rows_t kernel_get_rows<block_q4_K, QK_NL, dequantize_q4_K>;
  2069. template [[host_name("kernel_get_rows_q5_K")]] kernel get_rows_t kernel_get_rows<block_q5_K, QK_NL, dequantize_q5_K>;
  2070. template [[host_name("kernel_get_rows_q6_K")]] kernel get_rows_t kernel_get_rows<block_q6_K, QK_NL, dequantize_q6_K>;
  2071. typedef void (mat_mm_t)(
  2072. device const uchar * src0,
  2073. device const uchar * src1,
  2074. device float * dst,
  2075. constant int64_t & ne00,
  2076. constant int64_t & ne02,
  2077. constant int64_t & nb01,
  2078. constant int64_t & nb02,
  2079. constant int64_t & ne12,
  2080. constant int64_t & nb10,
  2081. constant int64_t & nb11,
  2082. constant int64_t & nb12,
  2083. constant int64_t & ne0,
  2084. constant int64_t & ne1,
  2085. constant uint & gqa,
  2086. threadgroup uchar *, uint3, uint, uint);
  2087. template [[host_name("kernel_mul_mm_f32_f32")]] kernel mat_mm_t kernel_mul_mm<float4x4, 1, dequantize_f32>;
  2088. template [[host_name("kernel_mul_mm_f16_f32")]] kernel mat_mm_t kernel_mul_mm<half4x4, 1, dequantize_f16>;
  2089. template [[host_name("kernel_mul_mm_q4_0_f32")]] kernel mat_mm_t kernel_mul_mm<block_q4_0, 2, dequantize_q4_0>;
  2090. template [[host_name("kernel_mul_mm_q4_1_f32")]] kernel mat_mm_t kernel_mul_mm<block_q4_1, 2, dequantize_q4_1>;
  2091. template [[host_name("kernel_mul_mm_q8_0_f32")]] kernel mat_mm_t kernel_mul_mm<block_q8_0, 2, dequantize_q8_0>;
  2092. template [[host_name("kernel_mul_mm_q2_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q2_K, QK_NL, dequantize_q2_K>;
  2093. template [[host_name("kernel_mul_mm_q3_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q3_K, QK_NL, dequantize_q3_K>;
  2094. template [[host_name("kernel_mul_mm_q4_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q4_K, QK_NL, dequantize_q4_K>;
  2095. template [[host_name("kernel_mul_mm_q5_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q5_K, QK_NL, dequantize_q5_K>;
  2096. template [[host_name("kernel_mul_mm_q6_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q6_K, QK_NL, dequantize_q6_K>;