ggml-metal.metal 104 KB

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