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