ggml-metal.metal 126 KB

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