ggml-metal.m 184 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214
  1. #import "ggml-metal.h"
  2. #import "ggml-backend-impl.h"
  3. #import "ggml.h"
  4. #import <Foundation/Foundation.h>
  5. #import <Metal/Metal.h>
  6. #undef MIN
  7. #undef MAX
  8. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  9. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  10. #ifdef GGML_METAL_NDEBUG
  11. #define GGML_METAL_LOG_INFO(...)
  12. #define GGML_METAL_LOG_WARN(...)
  13. #define GGML_METAL_LOG_ERROR(...)
  14. #else
  15. #define GGML_METAL_LOG_INFO(...) ggml_metal_log(GGML_LOG_LEVEL_INFO, __VA_ARGS__)
  16. #define GGML_METAL_LOG_WARN(...) ggml_metal_log(GGML_LOG_LEVEL_WARN, __VA_ARGS__)
  17. #define GGML_METAL_LOG_ERROR(...) ggml_metal_log(GGML_LOG_LEVEL_ERROR, __VA_ARGS__)
  18. #endif
  19. #define UNUSED(x) (void)(x)
  20. struct ggml_metal_kernel {
  21. id<MTLComputePipelineState> pipeline;
  22. };
  23. enum ggml_metal_kernel_type {
  24. GGML_METAL_KERNEL_TYPE_ADD,
  25. GGML_METAL_KERNEL_TYPE_ADD_ROW,
  26. GGML_METAL_KERNEL_TYPE_MUL,
  27. GGML_METAL_KERNEL_TYPE_MUL_ROW,
  28. GGML_METAL_KERNEL_TYPE_DIV,
  29. GGML_METAL_KERNEL_TYPE_DIV_ROW,
  30. GGML_METAL_KERNEL_TYPE_SCALE,
  31. GGML_METAL_KERNEL_TYPE_SCALE_4,
  32. GGML_METAL_KERNEL_TYPE_CLAMP,
  33. GGML_METAL_KERNEL_TYPE_TANH,
  34. GGML_METAL_KERNEL_TYPE_RELU,
  35. GGML_METAL_KERNEL_TYPE_GELU,
  36. GGML_METAL_KERNEL_TYPE_GELU_4,
  37. GGML_METAL_KERNEL_TYPE_GELU_QUICK,
  38. GGML_METAL_KERNEL_TYPE_GELU_QUICK_4,
  39. GGML_METAL_KERNEL_TYPE_SILU,
  40. GGML_METAL_KERNEL_TYPE_SILU_4,
  41. GGML_METAL_KERNEL_TYPE_SOFT_MAX_F16,
  42. GGML_METAL_KERNEL_TYPE_SOFT_MAX_F16_4,
  43. GGML_METAL_KERNEL_TYPE_SOFT_MAX_F32,
  44. GGML_METAL_KERNEL_TYPE_SOFT_MAX_F32_4,
  45. GGML_METAL_KERNEL_TYPE_DIAG_MASK_INF,
  46. GGML_METAL_KERNEL_TYPE_DIAG_MASK_INF_8,
  47. GGML_METAL_KERNEL_TYPE_GET_ROWS_F32,
  48. GGML_METAL_KERNEL_TYPE_GET_ROWS_F16,
  49. GGML_METAL_KERNEL_TYPE_GET_ROWS_Q4_0,
  50. GGML_METAL_KERNEL_TYPE_GET_ROWS_Q4_1,
  51. GGML_METAL_KERNEL_TYPE_GET_ROWS_Q5_0,
  52. GGML_METAL_KERNEL_TYPE_GET_ROWS_Q5_1,
  53. GGML_METAL_KERNEL_TYPE_GET_ROWS_Q8_0,
  54. GGML_METAL_KERNEL_TYPE_GET_ROWS_Q2_K,
  55. GGML_METAL_KERNEL_TYPE_GET_ROWS_Q3_K,
  56. GGML_METAL_KERNEL_TYPE_GET_ROWS_Q4_K,
  57. GGML_METAL_KERNEL_TYPE_GET_ROWS_Q5_K,
  58. GGML_METAL_KERNEL_TYPE_GET_ROWS_Q6_K,
  59. GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ2_XXS,
  60. GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ2_XS,
  61. GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ3_XXS,
  62. GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ3_S,
  63. GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ2_S,
  64. GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ1_S,
  65. GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ1_M,
  66. GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ4_NL,
  67. GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ4_XS,
  68. GGML_METAL_KERNEL_TYPE_GET_ROWS_I32,
  69. GGML_METAL_KERNEL_TYPE_RMS_NORM,
  70. GGML_METAL_KERNEL_TYPE_GROUP_NORM,
  71. GGML_METAL_KERNEL_TYPE_NORM,
  72. GGML_METAL_KERNEL_TYPE_MUL_MV_F32_F32,
  73. GGML_METAL_KERNEL_TYPE_MUL_MV_F16_F16,
  74. GGML_METAL_KERNEL_TYPE_MUL_MV_F16_F32,
  75. GGML_METAL_KERNEL_TYPE_MUL_MV_F16_F32_1ROW,
  76. GGML_METAL_KERNEL_TYPE_MUL_MV_F16_F32_L4,
  77. GGML_METAL_KERNEL_TYPE_MUL_MV_Q4_0_F32,
  78. GGML_METAL_KERNEL_TYPE_MUL_MV_Q4_1_F32,
  79. GGML_METAL_KERNEL_TYPE_MUL_MV_Q5_0_F32,
  80. GGML_METAL_KERNEL_TYPE_MUL_MV_Q5_1_F32,
  81. GGML_METAL_KERNEL_TYPE_MUL_MV_Q8_0_F32,
  82. GGML_METAL_KERNEL_TYPE_MUL_MV_Q2_K_F32,
  83. GGML_METAL_KERNEL_TYPE_MUL_MV_Q3_K_F32,
  84. GGML_METAL_KERNEL_TYPE_MUL_MV_Q4_K_F32,
  85. GGML_METAL_KERNEL_TYPE_MUL_MV_Q5_K_F32,
  86. GGML_METAL_KERNEL_TYPE_MUL_MV_Q6_K_F32,
  87. GGML_METAL_KERNEL_TYPE_MUL_MV_IQ2_XXS_F32,
  88. GGML_METAL_KERNEL_TYPE_MUL_MV_IQ2_XS_F32,
  89. GGML_METAL_KERNEL_TYPE_MUL_MV_IQ3_XXS_F32,
  90. GGML_METAL_KERNEL_TYPE_MUL_MV_IQ3_S_F32,
  91. GGML_METAL_KERNEL_TYPE_MUL_MV_IQ2_S_F32,
  92. GGML_METAL_KERNEL_TYPE_MUL_MV_IQ1_S_F32,
  93. GGML_METAL_KERNEL_TYPE_MUL_MV_IQ1_M_F32,
  94. GGML_METAL_KERNEL_TYPE_MUL_MV_IQ4_NL_F32,
  95. GGML_METAL_KERNEL_TYPE_MUL_MV_IQ4_XS_F32,
  96. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_F32_F32,
  97. //GGML_METAL_KERNEL_TYPE_MUL_MV_ID_F16_F16,
  98. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_F16_F32,
  99. //GGML_METAL_KERNEL_TYPE_MUL_MV_ID_F16_F32_1ROW,
  100. //GGML_METAL_KERNEL_TYPE_MUL_MV_ID_F16_F32_L4,
  101. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q4_0_F32,
  102. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q4_1_F32,
  103. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q5_0_F32,
  104. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q5_1_F32,
  105. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q8_0_F32,
  106. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q2_K_F32,
  107. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q3_K_F32,
  108. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q4_K_F32,
  109. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q5_K_F32,
  110. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q6_K_F32,
  111. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ2_XXS_F32,
  112. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ2_XS_F32,
  113. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ3_XXS_F32,
  114. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ3_S_F32,
  115. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ2_S_F32,
  116. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ1_S_F32,
  117. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ1_M_F32,
  118. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ4_NL_F32,
  119. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ4_XS_F32,
  120. GGML_METAL_KERNEL_TYPE_MUL_MM_F32_F32,
  121. GGML_METAL_KERNEL_TYPE_MUL_MM_F16_F32,
  122. GGML_METAL_KERNEL_TYPE_MUL_MM_Q4_0_F32,
  123. GGML_METAL_KERNEL_TYPE_MUL_MM_Q4_1_F32,
  124. GGML_METAL_KERNEL_TYPE_MUL_MM_Q5_0_F32,
  125. GGML_METAL_KERNEL_TYPE_MUL_MM_Q5_1_F32,
  126. GGML_METAL_KERNEL_TYPE_MUL_MM_Q8_0_F32,
  127. GGML_METAL_KERNEL_TYPE_MUL_MM_Q2_K_F32,
  128. GGML_METAL_KERNEL_TYPE_MUL_MM_Q3_K_F32,
  129. GGML_METAL_KERNEL_TYPE_MUL_MM_Q4_K_F32,
  130. GGML_METAL_KERNEL_TYPE_MUL_MM_Q5_K_F32,
  131. GGML_METAL_KERNEL_TYPE_MUL_MM_Q6_K_F32,
  132. GGML_METAL_KERNEL_TYPE_MUL_MM_IQ2_XXS_F32,
  133. GGML_METAL_KERNEL_TYPE_MUL_MM_IQ2_XS_F32,
  134. GGML_METAL_KERNEL_TYPE_MUL_MM_IQ3_XXS_F32,
  135. GGML_METAL_KERNEL_TYPE_MUL_MM_IQ3_S_F32,
  136. GGML_METAL_KERNEL_TYPE_MUL_MM_IQ2_S_F32,
  137. GGML_METAL_KERNEL_TYPE_MUL_MM_IQ1_S_F32,
  138. GGML_METAL_KERNEL_TYPE_MUL_MM_IQ1_M_F32,
  139. GGML_METAL_KERNEL_TYPE_MUL_MM_IQ4_NL_F32,
  140. GGML_METAL_KERNEL_TYPE_MUL_MM_IQ4_XS_F32,
  141. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_F32_F32,
  142. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_F16_F32,
  143. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q4_0_F32,
  144. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q4_1_F32,
  145. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q5_0_F32,
  146. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q5_1_F32,
  147. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q8_0_F32,
  148. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q2_K_F32,
  149. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q3_K_F32,
  150. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q4_K_F32,
  151. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q5_K_F32,
  152. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q6_K_F32,
  153. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ2_XXS_F32,
  154. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ2_XS_F32,
  155. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ3_XXS_F32,
  156. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ3_S_F32,
  157. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ2_S_F32,
  158. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ1_S_F32,
  159. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ1_M_F32,
  160. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ4_NL_F32,
  161. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ4_XS_F32,
  162. GGML_METAL_KERNEL_TYPE_ROPE_F32,
  163. GGML_METAL_KERNEL_TYPE_ROPE_F16,
  164. GGML_METAL_KERNEL_TYPE_ALIBI_F32,
  165. GGML_METAL_KERNEL_TYPE_IM2COL_F16,
  166. GGML_METAL_KERNEL_TYPE_IM2COL_F32,
  167. GGML_METAL_KERNEL_TYPE_UPSCALE_F32,
  168. GGML_METAL_KERNEL_TYPE_PAD_F32,
  169. GGML_METAL_KERNEL_TYPE_ARANGE_F32,
  170. GGML_METAL_KERNEL_TYPE_TIMESTEP_EMBEDDING_F32,
  171. GGML_METAL_KERNEL_TYPE_ARGSORT_F32_I32_ASC,
  172. GGML_METAL_KERNEL_TYPE_ARGSORT_F32_I32_DESC,
  173. GGML_METAL_KERNEL_TYPE_LEAKY_RELU_F32,
  174. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_F16_H64,
  175. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_F16_H80,
  176. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_F16_H96,
  177. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_F16_H112,
  178. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_F16_H128,
  179. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_F16_H256,
  180. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_VEC_F16_H128,
  181. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_VEC_F16_H256,
  182. GGML_METAL_KERNEL_TYPE_CPY_F32_F16,
  183. GGML_METAL_KERNEL_TYPE_CPY_F32_F32,
  184. GGML_METAL_KERNEL_TYPE_CPY_F32_Q8_0,
  185. GGML_METAL_KERNEL_TYPE_CPY_F32_Q4_0,
  186. GGML_METAL_KERNEL_TYPE_CPY_F32_Q4_1,
  187. GGML_METAL_KERNEL_TYPE_CPY_F32_Q5_0,
  188. GGML_METAL_KERNEL_TYPE_CPY_F32_Q5_1,
  189. GGML_METAL_KERNEL_TYPE_CPY_F32_IQ4_NL,
  190. GGML_METAL_KERNEL_TYPE_CPY_F16_F16,
  191. GGML_METAL_KERNEL_TYPE_CPY_F16_F32,
  192. GGML_METAL_KERNEL_TYPE_CONCAT,
  193. GGML_METAL_KERNEL_TYPE_SQR,
  194. GGML_METAL_KERNEL_TYPE_SUM_ROWS,
  195. GGML_METAL_KERNEL_TYPE_COUNT
  196. };
  197. struct ggml_metal_context {
  198. int n_cb;
  199. id<MTLDevice> device;
  200. id<MTLCommandQueue> queue;
  201. dispatch_queue_t d_queue;
  202. struct ggml_metal_kernel kernels[GGML_METAL_KERNEL_TYPE_COUNT];
  203. bool support_simdgroup_reduction;
  204. bool support_simdgroup_mm;
  205. bool should_capture_next_compute;
  206. };
  207. // MSL code
  208. // TODO: move the contents here when ready
  209. // for now it is easier to work in a separate file
  210. // static NSString * const msl_library_source = @"see metal.metal";
  211. // Here to assist with NSBundle Path Hack
  212. @interface GGMLMetalClass : NSObject
  213. @end
  214. @implementation GGMLMetalClass
  215. @end
  216. static void ggml_metal_default_log_callback(enum ggml_log_level level, const char * msg, void * user_data) {
  217. fprintf(stderr, "%s", msg);
  218. UNUSED(level);
  219. UNUSED(user_data);
  220. }
  221. ggml_log_callback ggml_metal_log_callback = ggml_metal_default_log_callback;
  222. void * ggml_metal_log_user_data = NULL;
  223. GGML_ATTRIBUTE_FORMAT(2, 3)
  224. static void ggml_metal_log(enum ggml_log_level level, const char * format, ...){
  225. if (ggml_metal_log_callback != NULL) {
  226. va_list args;
  227. va_start(args, format);
  228. char buffer[128];
  229. int len = vsnprintf(buffer, 128, format, args);
  230. if (len < 128) {
  231. ggml_metal_log_callback(level, buffer, ggml_metal_log_user_data);
  232. } else {
  233. char* buffer2 = malloc(len+1);
  234. va_end(args);
  235. va_start(args, format);
  236. vsnprintf(buffer2, len+1, format, args);
  237. buffer2[len] = 0;
  238. ggml_metal_log_callback(level, buffer2, ggml_metal_log_user_data);
  239. free(buffer2);
  240. }
  241. va_end(args);
  242. }
  243. }
  244. static void * ggml_metal_host_malloc(size_t n) {
  245. void * data = NULL;
  246. #if TARGET_OS_OSX
  247. kern_return_t err = vm_allocate((vm_map_t) mach_task_self(), (void *) &data, n, VM_FLAGS_ANYWHERE);
  248. if (err != KERN_SUCCESS) {
  249. GGML_METAL_LOG_ERROR("%s: error: vm_allocate failed\n", __func__);
  250. return NULL;
  251. }
  252. #else
  253. const int result = posix_memalign((void **) &data, sysconf(_SC_PAGESIZE), n);
  254. if (result != 0) {
  255. GGML_METAL_LOG_ERROR("%s: error: posix_memalign failed\n", __func__);
  256. return NULL;
  257. }
  258. #endif
  259. return data;
  260. }
  261. static struct ggml_metal_context * ggml_metal_init(int n_cb) {
  262. GGML_METAL_LOG_INFO("%s: allocating\n", __func__);
  263. #if TARGET_OS_OSX && !GGML_METAL_NDEBUG
  264. // Show all the Metal device instances in the system
  265. NSArray * devices = MTLCopyAllDevices();
  266. for (id<MTLDevice> device in devices) {
  267. GGML_METAL_LOG_INFO("%s: found device: %s\n", __func__, [[device name] UTF8String]);
  268. }
  269. [devices release]; // since it was created by a *Copy* C method
  270. #endif
  271. // Pick and show default Metal device
  272. id<MTLDevice> device = MTLCreateSystemDefaultDevice();
  273. GGML_METAL_LOG_INFO("%s: picking default device: %s\n", __func__, [[device name] UTF8String]);
  274. // Configure context
  275. struct ggml_metal_context * ctx = malloc(sizeof(struct ggml_metal_context));
  276. ctx->device = device;
  277. ctx->n_cb = MIN(n_cb, GGML_METAL_MAX_BUFFERS);
  278. ctx->queue = [ctx->device newCommandQueue];
  279. ctx->d_queue = dispatch_queue_create("ggml-metal", DISPATCH_QUEUE_CONCURRENT);
  280. id<MTLLibrary> metal_library;
  281. // load library
  282. //
  283. // - first check if the library is embedded
  284. // - then check if the library is in the bundle
  285. // - if not found, load the source and compile it
  286. // - if that fails, return NULL
  287. {
  288. NSBundle * bundle = nil;
  289. #ifdef SWIFT_PACKAGE
  290. bundle = SWIFTPM_MODULE_BUNDLE;
  291. #else
  292. bundle = [NSBundle bundleForClass:[GGMLMetalClass class]];
  293. #endif
  294. NSError * error = nil;
  295. #if GGML_METAL_EMBED_LIBRARY
  296. const bool try_metallib = false;
  297. #else
  298. const bool try_metallib = true;
  299. #endif
  300. NSString * path_lib = [bundle pathForResource:@"default" ofType:@"metallib"];
  301. if (try_metallib && path_lib != nil) {
  302. // pre-compiled library found
  303. NSURL * libURL = [NSURL fileURLWithPath:path_lib];
  304. GGML_METAL_LOG_INFO("%s: loading '%s'\n", __func__, [path_lib UTF8String]);
  305. metal_library = [ctx->device newLibraryWithURL:libURL error:&error];
  306. if (error) {
  307. GGML_METAL_LOG_ERROR("%s: error: %s\n", __func__, [[error description] UTF8String]);
  308. return NULL;
  309. }
  310. } else {
  311. #if GGML_METAL_EMBED_LIBRARY
  312. GGML_METAL_LOG_INFO("%s: using embedded metal library\n", __func__);
  313. extern const char ggml_metallib_start[];
  314. extern const char ggml_metallib_end[];
  315. NSString * src = [[NSString alloc] initWithBytes:ggml_metallib_start length:(ggml_metallib_end-ggml_metallib_start) encoding:NSUTF8StringEncoding];
  316. #else
  317. GGML_METAL_LOG_INFO("%s: default.metallib not found, loading from source\n", __func__);
  318. NSString * path_source;
  319. NSString * path_resource = [[NSProcessInfo processInfo].environment objectForKey:@"GGML_METAL_PATH_RESOURCES"];
  320. GGML_METAL_LOG_INFO("%s: GGML_METAL_PATH_RESOURCES = %s\n", __func__, path_resource ? [path_resource UTF8String] : "nil");
  321. if (path_resource) {
  322. path_source = [path_resource stringByAppendingPathComponent:@"ggml-metal.metal"];
  323. } else {
  324. path_source = [bundle pathForResource:@"ggml-metal" ofType:@"metal"];
  325. }
  326. if (path_source == nil) {
  327. GGML_METAL_LOG_WARN("%s: error: could not use bundle path to find ggml-metal.metal, falling back to trying cwd\n", __func__);
  328. path_source = @"ggml-metal.metal";
  329. }
  330. GGML_METAL_LOG_INFO("%s: loading '%s'\n", __func__, [path_source UTF8String]);
  331. NSString * src = [NSString stringWithContentsOfFile:path_source encoding:NSUTF8StringEncoding error:&error];
  332. if (error) {
  333. GGML_METAL_LOG_ERROR("%s: error: %s\n", __func__, [[error description] UTF8String]);
  334. return NULL;
  335. }
  336. #endif // GGML_METAL_EMBED_LIBRARY
  337. @autoreleasepool {
  338. // dictionary of preprocessor macros
  339. NSMutableDictionary * prep = [NSMutableDictionary dictionary];
  340. #ifdef GGML_QKK_64
  341. prep[@"GGML_QKK_64"] = @(1);
  342. #endif
  343. MTLCompileOptions* options = [MTLCompileOptions new];
  344. options.preprocessorMacros = prep;
  345. //[options setFastMathEnabled:false];
  346. metal_library = [ctx->device newLibraryWithSource:src options:options error:&error];
  347. if (error) {
  348. GGML_METAL_LOG_ERROR("%s: error: %s\n", __func__, [[error description] UTF8String]);
  349. return NULL;
  350. }
  351. }
  352. }
  353. }
  354. // print MTL GPU family:
  355. GGML_METAL_LOG_INFO("%s: GPU name: %s\n", __func__, [[ctx->device name] UTF8String]);
  356. const NSInteger MTLGPUFamilyMetal3 = 5001;
  357. // determine max supported GPU family
  358. // https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf
  359. // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf
  360. {
  361. for (int i = MTLGPUFamilyApple1 + 20; i >= MTLGPUFamilyApple1; --i) {
  362. if ([ctx->device supportsFamily:i]) {
  363. GGML_METAL_LOG_INFO("%s: GPU family: MTLGPUFamilyApple%d (%d)\n", __func__, i - (int) MTLGPUFamilyApple1 + 1, i);
  364. break;
  365. }
  366. }
  367. for (int i = MTLGPUFamilyCommon1 + 5; i >= MTLGPUFamilyCommon1; --i) {
  368. if ([ctx->device supportsFamily:i]) {
  369. GGML_METAL_LOG_INFO("%s: GPU family: MTLGPUFamilyCommon%d (%d)\n", __func__, i - (int) MTLGPUFamilyCommon1 + 1, i);
  370. break;
  371. }
  372. }
  373. for (int i = MTLGPUFamilyMetal3 + 5; i >= MTLGPUFamilyMetal3; --i) {
  374. if ([ctx->device supportsFamily:i]) {
  375. GGML_METAL_LOG_INFO("%s: GPU family: MTLGPUFamilyMetal%d (%d)\n", __func__, i - (int) MTLGPUFamilyMetal3 + 3, i);
  376. break;
  377. }
  378. }
  379. }
  380. ctx->support_simdgroup_reduction = [ctx->device supportsFamily:MTLGPUFamilyApple7];
  381. ctx->support_simdgroup_reduction |= [ctx->device supportsFamily:MTLGPUFamilyMetal3];
  382. ctx->support_simdgroup_mm = [ctx->device supportsFamily:MTLGPUFamilyApple7];
  383. GGML_METAL_LOG_INFO("%s: simdgroup reduction support = %s\n", __func__, ctx->support_simdgroup_reduction ? "true" : "false");
  384. GGML_METAL_LOG_INFO("%s: simdgroup matrix mul. support = %s\n", __func__, ctx->support_simdgroup_mm ? "true" : "false");
  385. GGML_METAL_LOG_INFO("%s: hasUnifiedMemory = %s\n", __func__, ctx->device.hasUnifiedMemory ? "true" : "false");
  386. ctx->should_capture_next_compute = false;
  387. #if TARGET_OS_OSX || (TARGET_OS_IOS && __clang_major__ >= 15)
  388. if (@available(macOS 10.12, iOS 16.0, *)) {
  389. GGML_METAL_LOG_INFO("%s: recommendedMaxWorkingSetSize = %8.2f MB\n", __func__, ctx->device.recommendedMaxWorkingSetSize / 1e6);
  390. }
  391. #elif TARGET_OS_OSX
  392. if (ctx->device.maxTransferRate != 0) {
  393. GGML_METAL_LOG_INFO("%s: maxTransferRate = %8.2f MB/s\n", __func__, ctx->device.maxTransferRate / 1e6);
  394. } else {
  395. GGML_METAL_LOG_INFO("%s: maxTransferRate = built-in GPU\n", __func__);
  396. }
  397. #endif
  398. // load kernels
  399. {
  400. NSError * error = nil;
  401. for (int i = 0; i < GGML_METAL_KERNEL_TYPE_COUNT; ++i) {
  402. ctx->kernels[i].pipeline = nil;
  403. }
  404. /*
  405. GGML_METAL_LOG_INFO("%s: loaded %-40s %16p | th_max = %4d | th_width = %4d\n", __func__, "kernel_"#name, (void *) kernel->pipeline, \
  406. (int) kernel->pipeline.maxTotalThreadsPerThreadgroup, \
  407. (int) kernel->pipeline.threadExecutionWidth); \
  408. */
  409. #define GGML_METAL_ADD_KERNEL(e, name, supported) \
  410. if (supported) { \
  411. struct ggml_metal_kernel * kernel = &ctx->kernels[e]; \
  412. id<MTLFunction> metal_function = [metal_library newFunctionWithName:@"kernel_"#name]; \
  413. kernel->pipeline = [ctx->device newComputePipelineStateWithFunction:metal_function error:&error]; \
  414. [metal_function release]; \
  415. if (error) { \
  416. GGML_METAL_LOG_ERROR("%s: error: load pipeline error: %s\n", __func__, [[error description] UTF8String]); \
  417. [metal_library release]; \
  418. return NULL; \
  419. } \
  420. } else { \
  421. GGML_METAL_LOG_WARN("%s: skipping %-40s (not supported)\n", __func__, "kernel_"#name); \
  422. }
  423. // simd_sum and simd_max requires MTLGPUFamilyApple7
  424. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_ADD, add, true);
  425. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_ADD_ROW, add_row, true);
  426. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL, mul, true);
  427. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_ROW, mul_row, true);
  428. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_DIV, div, true);
  429. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_DIV_ROW, div_row, true);
  430. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_SCALE, scale, true);
  431. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_SCALE_4, scale_4, true);
  432. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_CLAMP, clamp, true);
  433. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_TANH, tanh, true);
  434. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_RELU, relu, true);
  435. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GELU, gelu, true);
  436. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GELU_4, gelu_4, true);
  437. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GELU_QUICK, gelu_quick, true);
  438. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GELU_QUICK_4, gelu_quick_4, true);
  439. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_SILU, silu, true);
  440. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_SILU_4, silu_4, true);
  441. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_SOFT_MAX_F16, soft_max_f16, ctx->support_simdgroup_reduction);
  442. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_SOFT_MAX_F16_4, soft_max_f16_4, ctx->support_simdgroup_reduction);
  443. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_SOFT_MAX_F32, soft_max_f32, ctx->support_simdgroup_reduction);
  444. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_SOFT_MAX_F32_4, soft_max_f32_4, ctx->support_simdgroup_reduction);
  445. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_DIAG_MASK_INF, diag_mask_inf, true);
  446. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_DIAG_MASK_INF_8, diag_mask_inf_8, true);
  447. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_F32, get_rows_f32, true);
  448. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_F16, get_rows_f16, true);
  449. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_Q4_0, get_rows_q4_0, true);
  450. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_Q4_1, get_rows_q4_1, true);
  451. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_Q5_0, get_rows_q5_0, true);
  452. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_Q5_1, get_rows_q5_1, true);
  453. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_Q8_0, get_rows_q8_0, true);
  454. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_Q2_K, get_rows_q2_K, true);
  455. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_Q3_K, get_rows_q3_K, true);
  456. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_Q4_K, get_rows_q4_K, true);
  457. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_Q5_K, get_rows_q5_K, true);
  458. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_Q6_K, get_rows_q6_K, true);
  459. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ2_XXS, get_rows_iq2_xxs, true);
  460. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ2_XS, get_rows_iq2_xs, true);
  461. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ3_XXS, get_rows_iq3_xxs, true);
  462. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ3_S, get_rows_iq3_s, true);
  463. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ2_S, get_rows_iq2_s, true);
  464. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ1_S, get_rows_iq1_s, true);
  465. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ1_M, get_rows_iq1_m, true);
  466. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ4_NL, get_rows_iq4_nl, true);
  467. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ4_XS, get_rows_iq4_xs, true);
  468. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_I32, get_rows_i32, true);
  469. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_RMS_NORM, rms_norm, ctx->support_simdgroup_reduction);
  470. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GROUP_NORM, group_norm, ctx->support_simdgroup_reduction);
  471. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_NORM, norm, true);
  472. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_F32_F32, mul_mv_f32_f32, ctx->support_simdgroup_reduction);
  473. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_F16_F16, mul_mv_f16_f16, ctx->support_simdgroup_reduction);
  474. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_F16_F32, mul_mv_f16_f32, ctx->support_simdgroup_reduction);
  475. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_F16_F32_1ROW, mul_mv_f16_f32_1row, ctx->support_simdgroup_reduction);
  476. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_F16_F32_L4, mul_mv_f16_f32_l4, ctx->support_simdgroup_reduction);
  477. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_Q4_0_F32, mul_mv_q4_0_f32, ctx->support_simdgroup_reduction);
  478. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_Q4_1_F32, mul_mv_q4_1_f32, ctx->support_simdgroup_reduction);
  479. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_Q5_0_F32, mul_mv_q5_0_f32, ctx->support_simdgroup_reduction);
  480. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_Q5_1_F32, mul_mv_q5_1_f32, ctx->support_simdgroup_reduction);
  481. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_Q8_0_F32, mul_mv_q8_0_f32, ctx->support_simdgroup_reduction);
  482. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_Q2_K_F32, mul_mv_q2_K_f32, ctx->support_simdgroup_reduction);
  483. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_Q3_K_F32, mul_mv_q3_K_f32, ctx->support_simdgroup_reduction);
  484. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_Q4_K_F32, mul_mv_q4_K_f32, ctx->support_simdgroup_reduction);
  485. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_Q5_K_F32, mul_mv_q5_K_f32, ctx->support_simdgroup_reduction);
  486. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_Q6_K_F32, mul_mv_q6_K_f32, ctx->support_simdgroup_reduction);
  487. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_IQ2_XXS_F32, mul_mv_iq2_xxs_f32, ctx->support_simdgroup_reduction);
  488. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_IQ2_XS_F32, mul_mv_iq2_xs_f32, ctx->support_simdgroup_reduction);
  489. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_IQ3_XXS_F32, mul_mv_iq3_xxs_f32, ctx->support_simdgroup_reduction);
  490. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_IQ3_S_F32, mul_mv_iq3_s_f32, ctx->support_simdgroup_reduction);
  491. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_IQ2_S_F32, mul_mv_iq2_s_f32, ctx->support_simdgroup_reduction);
  492. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_IQ1_S_F32, mul_mv_iq1_s_f32, ctx->support_simdgroup_reduction);
  493. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_IQ1_M_F32, mul_mv_iq1_m_f32, ctx->support_simdgroup_reduction);
  494. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_IQ4_NL_F32, mul_mv_iq4_nl_f32, ctx->support_simdgroup_reduction);
  495. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_IQ4_XS_F32, mul_mv_iq4_xs_f32, ctx->support_simdgroup_reduction);
  496. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_F32_F32, mul_mv_id_f32_f32, ctx->support_simdgroup_reduction);
  497. //GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_F16_F16, mul_mv_id_f16_f16, ctx->support_simdgroup_reduction);
  498. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_F16_F32, mul_mv_id_f16_f32, ctx->support_simdgroup_reduction);
  499. //GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_F16_F32_1ROW, mul_mv_id_f16_f32_1row, ctx->support_simdgroup_reduction);
  500. //GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_F16_F32_L4, mul_mv_id_f16_f32_l4, ctx->support_simdgroup_reduction);
  501. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q4_0_F32, mul_mv_id_q4_0_f32, ctx->support_simdgroup_reduction);
  502. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q4_1_F32, mul_mv_id_q4_1_f32, ctx->support_simdgroup_reduction);
  503. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q5_0_F32, mul_mv_id_q5_0_f32, ctx->support_simdgroup_reduction);
  504. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q5_1_F32, mul_mv_id_q5_1_f32, ctx->support_simdgroup_reduction);
  505. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q8_0_F32, mul_mv_id_q8_0_f32, ctx->support_simdgroup_reduction);
  506. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q2_K_F32, mul_mv_id_q2_K_f32, ctx->support_simdgroup_reduction);
  507. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q3_K_F32, mul_mv_id_q3_K_f32, ctx->support_simdgroup_reduction);
  508. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q4_K_F32, mul_mv_id_q4_K_f32, ctx->support_simdgroup_reduction);
  509. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q5_K_F32, mul_mv_id_q5_K_f32, ctx->support_simdgroup_reduction);
  510. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q6_K_F32, mul_mv_id_q6_K_f32, ctx->support_simdgroup_reduction);
  511. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ2_XXS_F32, mul_mv_id_iq2_xxs_f32, ctx->support_simdgroup_reduction);
  512. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ2_XS_F32, mul_mv_id_iq2_xs_f32, ctx->support_simdgroup_reduction);
  513. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ3_XXS_F32, mul_mv_id_iq3_xxs_f32, ctx->support_simdgroup_reduction);
  514. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ3_S_F32, mul_mv_id_iq3_s_f32, ctx->support_simdgroup_reduction);
  515. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ2_S_F32, mul_mv_id_iq2_s_f32, ctx->support_simdgroup_reduction);
  516. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ1_S_F32, mul_mv_id_iq1_s_f32, ctx->support_simdgroup_reduction);
  517. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ1_M_F32, mul_mv_id_iq1_m_f32, ctx->support_simdgroup_reduction);
  518. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ4_NL_F32, mul_mv_id_iq4_nl_f32, ctx->support_simdgroup_reduction);
  519. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ4_XS_F32, mul_mv_id_iq4_xs_f32, ctx->support_simdgroup_reduction);
  520. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_F32_F32, mul_mm_f32_f32, ctx->support_simdgroup_mm);
  521. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_F16_F32, mul_mm_f16_f32, ctx->support_simdgroup_mm);
  522. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_Q4_0_F32, mul_mm_q4_0_f32, ctx->support_simdgroup_mm);
  523. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_Q4_1_F32, mul_mm_q4_1_f32, ctx->support_simdgroup_mm);
  524. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_Q5_0_F32, mul_mm_q5_0_f32, ctx->support_simdgroup_mm);
  525. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_Q5_1_F32, mul_mm_q5_1_f32, ctx->support_simdgroup_mm);
  526. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_Q8_0_F32, mul_mm_q8_0_f32, ctx->support_simdgroup_mm);
  527. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_Q2_K_F32, mul_mm_q2_K_f32, ctx->support_simdgroup_mm);
  528. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_Q3_K_F32, mul_mm_q3_K_f32, ctx->support_simdgroup_mm);
  529. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_Q4_K_F32, mul_mm_q4_K_f32, ctx->support_simdgroup_mm);
  530. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_Q5_K_F32, mul_mm_q5_K_f32, ctx->support_simdgroup_mm);
  531. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_Q6_K_F32, mul_mm_q6_K_f32, ctx->support_simdgroup_mm);
  532. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_IQ2_XXS_F32, mul_mm_iq2_xxs_f32, ctx->support_simdgroup_mm);
  533. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_IQ2_XS_F32, mul_mm_iq2_xs_f32, ctx->support_simdgroup_mm);
  534. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_IQ3_XXS_F32, mul_mm_iq3_xxs_f32, ctx->support_simdgroup_mm);
  535. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_IQ3_S_F32, mul_mm_iq3_s_f32, ctx->support_simdgroup_mm);
  536. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_IQ2_S_F32, mul_mm_iq2_s_f32, ctx->support_simdgroup_mm);
  537. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_IQ1_S_F32, mul_mm_iq1_s_f32, ctx->support_simdgroup_mm);
  538. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_IQ1_M_F32, mul_mm_iq1_m_f32, ctx->support_simdgroup_mm);
  539. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_IQ4_NL_F32, mul_mm_iq4_nl_f32, ctx->support_simdgroup_mm);
  540. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_IQ4_XS_F32, mul_mm_iq4_xs_f32, ctx->support_simdgroup_mm);
  541. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_F32_F32, mul_mm_id_f32_f32, ctx->support_simdgroup_mm);
  542. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_F16_F32, mul_mm_id_f16_f32, ctx->support_simdgroup_mm);
  543. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q4_0_F32, mul_mm_id_q4_0_f32, ctx->support_simdgroup_mm);
  544. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q4_1_F32, mul_mm_id_q4_1_f32, ctx->support_simdgroup_mm);
  545. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q5_0_F32, mul_mm_id_q5_0_f32, ctx->support_simdgroup_mm);
  546. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q5_1_F32, mul_mm_id_q5_1_f32, ctx->support_simdgroup_mm);
  547. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q8_0_F32, mul_mm_id_q8_0_f32, ctx->support_simdgroup_mm);
  548. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q2_K_F32, mul_mm_id_q2_K_f32, ctx->support_simdgroup_mm);
  549. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q3_K_F32, mul_mm_id_q3_K_f32, ctx->support_simdgroup_mm);
  550. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q4_K_F32, mul_mm_id_q4_K_f32, ctx->support_simdgroup_mm);
  551. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q5_K_F32, mul_mm_id_q5_K_f32, ctx->support_simdgroup_mm);
  552. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q6_K_F32, mul_mm_id_q6_K_f32, ctx->support_simdgroup_mm);
  553. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ2_XXS_F32, mul_mm_id_iq2_xxs_f32, ctx->support_simdgroup_mm);
  554. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ2_XS_F32, mul_mm_id_iq2_xs_f32, ctx->support_simdgroup_mm);
  555. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ3_XXS_F32, mul_mm_id_iq3_xxs_f32, ctx->support_simdgroup_mm);
  556. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ3_S_F32, mul_mm_id_iq3_s_f32, ctx->support_simdgroup_mm);
  557. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ2_S_F32, mul_mm_id_iq2_s_f32, ctx->support_simdgroup_mm);
  558. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ1_S_F32, mul_mm_id_iq1_s_f32, ctx->support_simdgroup_mm);
  559. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ1_M_F32, mul_mm_id_iq1_m_f32, ctx->support_simdgroup_mm);
  560. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ4_NL_F32, mul_mm_id_iq4_nl_f32, ctx->support_simdgroup_mm);
  561. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ4_XS_F32, mul_mm_id_iq4_xs_f32, ctx->support_simdgroup_mm);
  562. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_ROPE_F32, rope_f32, true);
  563. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_ROPE_F16, rope_f16, true);
  564. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_ALIBI_F32, alibi_f32, true);
  565. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_IM2COL_F16, im2col_f16, true);
  566. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_IM2COL_F32, im2col_f32, true);
  567. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_UPSCALE_F32, upscale_f32, true);
  568. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_PAD_F32, pad_f32, true);
  569. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_TIMESTEP_EMBEDDING_F32, timestep_embedding_f32, true);
  570. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_ARANGE_F32, arange_f32, true);
  571. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_ARGSORT_F32_I32_ASC, argsort_f32_i32_asc, true);
  572. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_ARGSORT_F32_I32_DESC, argsort_f32_i32_desc, true);
  573. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_LEAKY_RELU_F32, leaky_relu_f32, true);
  574. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_F16_H64, flash_attn_ext_f16_h64, true);
  575. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_F16_H80, flash_attn_ext_f16_h80, true);
  576. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_F16_H96, flash_attn_ext_f16_h96, true);
  577. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_F16_H112, flash_attn_ext_f16_h112, true);
  578. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_F16_H128, flash_attn_ext_f16_h128, true);
  579. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_F16_H256, flash_attn_ext_f16_h256, true);
  580. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_VEC_F16_H128, flash_attn_ext_vec_f16_h128, true);
  581. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_VEC_F16_H256, flash_attn_ext_vec_f16_h256, true);
  582. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_CPY_F32_F16, cpy_f32_f16, true);
  583. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_CPY_F32_F32, cpy_f32_f32, true);
  584. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_CPY_F32_Q8_0, cpy_f32_q8_0, true);
  585. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_CPY_F32_Q4_0, cpy_f32_q4_0, true);
  586. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_CPY_F32_Q4_1, cpy_f32_q4_1, true);
  587. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_CPY_F32_Q5_0, cpy_f32_q5_0, true);
  588. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_CPY_F32_Q5_1, cpy_f32_q5_1, true);
  589. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_CPY_F32_IQ4_NL, cpy_f32_iq4_nl, true);
  590. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_CPY_F16_F16, cpy_f16_f16, true);
  591. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_CPY_F16_F32, cpy_f16_f32, true);
  592. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_CONCAT, concat, true);
  593. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_SQR, sqr, true);
  594. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_SUM_ROWS, sum_rows, true);
  595. }
  596. [metal_library release];
  597. return ctx;
  598. }
  599. static void ggml_metal_free(struct ggml_metal_context * ctx) {
  600. GGML_METAL_LOG_INFO("%s: deallocating\n", __func__);
  601. for (int i = 0; i < GGML_METAL_KERNEL_TYPE_COUNT; ++i) {
  602. [ctx->kernels[i].pipeline release];
  603. }
  604. [ctx->queue release];
  605. [ctx->device release];
  606. dispatch_release(ctx->d_queue);
  607. free(ctx);
  608. }
  609. // temporarily defined here for compatibility between ggml-backend and the old API
  610. struct ggml_backend_metal_buffer {
  611. void * data;
  612. size_t size;
  613. id<MTLBuffer> metal;
  614. };
  615. struct ggml_backend_metal_buffer_context {
  616. void * all_data;
  617. size_t all_size;
  618. bool owned;
  619. // multiple buffers are used only to avoid the maximum buffer size limitation when using mmap
  620. int n_buffers;
  621. struct ggml_backend_metal_buffer buffers[GGML_METAL_MAX_BUFFERS];
  622. };
  623. // finds the Metal buffer that contains the tensor data on the GPU device
  624. // the assumption is that there is 1-to-1 mapping between the host and device memory buffers, so we can find the
  625. // Metal buffer based on the host memory pointer
  626. //
  627. static id<MTLBuffer> ggml_metal_get_buffer(struct ggml_tensor * t, size_t * offs) {
  628. //GGML_METAL_LOG_INFO("%s: data tensor '%16s', offs_data = %8ld, offs_eval = %8ld, offs_cach = %8ld\n", __func__, t->name, offs_data, offs_eval, offs_cach);
  629. const int64_t tsize = ggml_nbytes(t);
  630. ggml_backend_buffer_t buffer = t->view_src ? t->view_src->buffer : t->buffer;
  631. struct ggml_backend_metal_buffer_context * buf_ctx = (struct ggml_backend_metal_buffer_context *) buffer->context;
  632. // find the view that contains the tensor fully
  633. for (int i = 0; i < buf_ctx->n_buffers; ++i) {
  634. const int64_t ioffs = (int64_t) t->data - (int64_t) buf_ctx->buffers[i].data;
  635. //GGML_METAL_LOG_INFO("ioffs = %10ld, tsize = %10ld, sum = %10ld, buf_ctx->buffers[%d].size = %10ld\n", ioffs, tsize, ioffs + tsize, i, buf_ctx->buffers[i].size);
  636. if (ioffs >= 0 && ioffs + tsize <= (int64_t) buf_ctx->buffers[i].size) {
  637. *offs = (size_t) ioffs;
  638. //GGML_METAL_LOG_INFO("%s: tensor '%16s', offs = %8ld\n", __func__, t->name, *offs);
  639. return buf_ctx->buffers[i].metal;
  640. }
  641. }
  642. GGML_METAL_LOG_ERROR("%s: error: tensor '%s' buffer is nil\n", __func__, t->name);
  643. return nil;
  644. }
  645. static bool ggml_metal_supports_op(const struct ggml_metal_context * ctx, const struct ggml_tensor * op) {
  646. switch (op->op) {
  647. case GGML_OP_UNARY:
  648. switch (ggml_get_unary_op(op)) {
  649. case GGML_UNARY_OP_TANH:
  650. case GGML_UNARY_OP_RELU:
  651. case GGML_UNARY_OP_GELU:
  652. case GGML_UNARY_OP_GELU_QUICK:
  653. case GGML_UNARY_OP_SILU:
  654. return true;
  655. default:
  656. return false;
  657. }
  658. case GGML_OP_NONE:
  659. case GGML_OP_RESHAPE:
  660. case GGML_OP_VIEW:
  661. case GGML_OP_TRANSPOSE:
  662. case GGML_OP_PERMUTE:
  663. case GGML_OP_CONCAT:
  664. case GGML_OP_ADD:
  665. case GGML_OP_ACC:
  666. case GGML_OP_MUL:
  667. case GGML_OP_DIV:
  668. case GGML_OP_SCALE:
  669. case GGML_OP_CLAMP:
  670. case GGML_OP_SQR:
  671. case GGML_OP_SUM_ROWS:
  672. return true;
  673. case GGML_OP_SOFT_MAX:
  674. case GGML_OP_RMS_NORM:
  675. case GGML_OP_GROUP_NORM:
  676. return ctx->support_simdgroup_reduction;
  677. case GGML_OP_NORM:
  678. case GGML_OP_ALIBI:
  679. case GGML_OP_ROPE:
  680. case GGML_OP_IM2COL:
  681. return true;
  682. case GGML_OP_POOL_1D:
  683. case GGML_OP_POOL_2D:
  684. return false;
  685. case GGML_OP_UPSCALE:
  686. case GGML_OP_PAD:
  687. case GGML_OP_ARANGE:
  688. case GGML_OP_TIMESTEP_EMBEDDING:
  689. case GGML_OP_ARGSORT:
  690. case GGML_OP_LEAKY_RELU:
  691. case GGML_OP_FLASH_ATTN_EXT:
  692. return true;
  693. case GGML_OP_MUL_MAT:
  694. case GGML_OP_MUL_MAT_ID:
  695. return ctx->support_simdgroup_reduction &&
  696. (op->src[0]->type != GGML_TYPE_F32 || op->src[1]->type == GGML_TYPE_F32);
  697. case GGML_OP_CPY:
  698. case GGML_OP_DUP:
  699. case GGML_OP_CONT:
  700. {
  701. switch (op->src[0]->type) {
  702. case GGML_TYPE_F32:
  703. switch (op->type) {
  704. case GGML_TYPE_F16:
  705. case GGML_TYPE_F32:
  706. case GGML_TYPE_Q8_0:
  707. case GGML_TYPE_Q4_0:
  708. case GGML_TYPE_Q4_1:
  709. case GGML_TYPE_Q5_0:
  710. case GGML_TYPE_Q5_1:
  711. case GGML_TYPE_IQ4_NL:
  712. return true;
  713. default:
  714. return false;
  715. }
  716. case GGML_TYPE_F16:
  717. switch (op->type) {
  718. case GGML_TYPE_F16:
  719. case GGML_TYPE_F32:
  720. return true;
  721. default:
  722. return false;
  723. }
  724. default:
  725. return false;
  726. };
  727. }
  728. case GGML_OP_DIAG_MASK_INF:
  729. case GGML_OP_GET_ROWS:
  730. {
  731. return op->src[0]->type != GGML_TYPE_BF16 && op->ne[3] == 1;
  732. }
  733. default:
  734. return false;
  735. }
  736. }
  737. static enum ggml_status ggml_metal_graph_compute(
  738. struct ggml_metal_context * ctx,
  739. struct ggml_cgraph * gf) {
  740. @autoreleasepool {
  741. MTLComputePassDescriptor * edesc = MTLComputePassDescriptor.computePassDescriptor;
  742. edesc.dispatchType = MTLDispatchTypeSerial;
  743. // create multiple command buffers and enqueue them
  744. // then, we encode the graph into the command buffers in parallel
  745. const int n_nodes = gf->n_nodes;
  746. const int n_cb = ctx->n_cb;
  747. const int n_nodes_per_cb = (n_nodes + n_cb - 1) / n_cb;
  748. const bool should_capture = ctx->should_capture_next_compute;
  749. if (should_capture) {
  750. ctx->should_capture_next_compute = false;
  751. MTLCaptureDescriptor * descriptor = [MTLCaptureDescriptor new];
  752. descriptor.captureObject = ctx->queue;
  753. NSError * error = nil;
  754. if (![[MTLCaptureManager sharedCaptureManager] startCaptureWithDescriptor:descriptor error:&error]) {
  755. GGML_METAL_LOG_ERROR("%s: error: unable to start capture '%s'\n", __func__, [[error localizedDescription] UTF8String]);
  756. GGML_ASSERT(!"capture failed");
  757. }
  758. }
  759. id<MTLCommandBuffer> command_buffer_builder[n_cb];
  760. for (int cb_idx = 0; cb_idx < n_cb; ++cb_idx) {
  761. id<MTLCommandBuffer> command_buffer = [ctx->queue commandBufferWithUnretainedReferences];
  762. command_buffer_builder[cb_idx] = command_buffer;
  763. // enqueue the command buffers in order to specify their execution order
  764. [command_buffer enqueue];
  765. }
  766. const id<MTLCommandBuffer> *command_buffers = command_buffer_builder;
  767. dispatch_apply(n_cb, ctx->d_queue, ^(size_t iter) {
  768. const int cb_idx = iter;
  769. size_t offs_src0 = 0;
  770. size_t offs_src1 = 0;
  771. size_t offs_src2 = 0;
  772. size_t offs_dst = 0;
  773. id<MTLCommandBuffer> command_buffer = command_buffers[cb_idx];
  774. id<MTLComputeCommandEncoder> encoder = [command_buffer computeCommandEncoderWithDescriptor: edesc];
  775. const int node_start = (cb_idx + 0) * n_nodes_per_cb;
  776. const int node_end = MIN((cb_idx == n_cb - 1) ? n_nodes : (cb_idx + 1) * n_nodes_per_cb, n_nodes);
  777. for (int i = node_start; i < node_end; ++i) {
  778. if (i == -1) {
  779. [encoder memoryBarrierWithScope:MTLBarrierScopeBuffers];
  780. continue;
  781. }
  782. //GGML_METAL_LOG_INFO("%s: encoding node %3d, op = %8s\n", __func__, i, ggml_op_name(gf->nodes[i]->op));
  783. struct ggml_tensor * src0 = gf->nodes[i]->src[0];
  784. struct ggml_tensor * src1 = gf->nodes[i]->src[1];
  785. struct ggml_tensor * src2 = gf->nodes[i]->src[2];
  786. struct ggml_tensor * dst = gf->nodes[i];
  787. if (ggml_is_empty(dst)) {
  788. continue;
  789. }
  790. switch (dst->op) {
  791. case GGML_OP_NONE:
  792. case GGML_OP_RESHAPE:
  793. case GGML_OP_VIEW:
  794. case GGML_OP_TRANSPOSE:
  795. case GGML_OP_PERMUTE:
  796. {
  797. // noop -> next node
  798. } continue;
  799. default:
  800. {
  801. } break;
  802. }
  803. if (!ggml_metal_supports_op(ctx, dst)) {
  804. GGML_METAL_LOG_ERROR("%s: error: unsupported op '%s'\n", __func__, ggml_op_desc(dst));
  805. GGML_ASSERT(!"unsupported op");
  806. }
  807. if (should_capture) {
  808. [encoder pushDebugGroup:[NSString stringWithCString:ggml_op_desc(dst) encoding:NSUTF8StringEncoding]];
  809. }
  810. const int64_t ne00 = src0 ? src0->ne[0] : 0;
  811. const int64_t ne01 = src0 ? src0->ne[1] : 0;
  812. const int64_t ne02 = src0 ? src0->ne[2] : 0;
  813. const int64_t ne03 = src0 ? src0->ne[3] : 0;
  814. const uint64_t nb00 = src0 ? src0->nb[0] : 0;
  815. const uint64_t nb01 = src0 ? src0->nb[1] : 0;
  816. const uint64_t nb02 = src0 ? src0->nb[2] : 0;
  817. const uint64_t nb03 = src0 ? src0->nb[3] : 0;
  818. const int64_t ne10 = src1 ? src1->ne[0] : 0;
  819. const int64_t ne11 = src1 ? src1->ne[1] : 0;
  820. const int64_t ne12 = src1 ? src1->ne[2] : 0;
  821. const int64_t ne13 = src1 ? src1->ne[3] : 0; UNUSED(ne13);
  822. const uint64_t nb10 = src1 ? src1->nb[0] : 0;
  823. const uint64_t nb11 = src1 ? src1->nb[1] : 0;
  824. const uint64_t nb12 = src1 ? src1->nb[2] : 0;
  825. const uint64_t nb13 = src1 ? src1->nb[3] : 0; UNUSED(nb13);
  826. const int64_t ne0 = dst ? dst->ne[0] : 0;
  827. const int64_t ne1 = dst ? dst->ne[1] : 0;
  828. const int64_t ne2 = dst ? dst->ne[2] : 0;
  829. const int64_t ne3 = dst ? dst->ne[3] : 0;
  830. const uint64_t nb0 = dst ? dst->nb[0] : 0;
  831. const uint64_t nb1 = dst ? dst->nb[1] : 0;
  832. const uint64_t nb2 = dst ? dst->nb[2] : 0;
  833. const uint64_t nb3 = dst ? dst->nb[3] : 0;
  834. const enum ggml_type src0t = src0 ? src0->type : GGML_TYPE_COUNT;
  835. const enum ggml_type src1t = src1 ? src1->type : GGML_TYPE_COUNT;
  836. const enum ggml_type dstt = dst ? dst->type : GGML_TYPE_COUNT;
  837. id<MTLBuffer> id_src0 = src0 ? ggml_metal_get_buffer(src0, &offs_src0) : nil;
  838. id<MTLBuffer> id_src1 = src1 ? ggml_metal_get_buffer(src1, &offs_src1) : nil;
  839. id<MTLBuffer> id_src2 = src2 ? ggml_metal_get_buffer(src2, &offs_src2) : nil;
  840. id<MTLBuffer> id_dst = dst ? ggml_metal_get_buffer(dst, &offs_dst) : nil;
  841. //GGML_METAL_LOG_INFO("%s: op - %s\n", __func__, ggml_op_name(dst->op));
  842. //if (src0) {
  843. // GGML_METAL_LOG_INFO("%s: src0 - %4s [%5lld, %5lld, %5lld], %d, %s\n", __func__, ggml_type_name(src0t), ne00, ne01, ne02,
  844. // ggml_is_contiguous(src0), src0->name);
  845. //}
  846. //if (src1) {
  847. // GGML_METAL_LOG_INFO("%s: src1 - %4s [%5lld, %5lld, %5lld], %d, %s\n", __func__, ggml_type_name(src1t), ne10, ne11, ne12,
  848. // ggml_is_contiguous(src1), src1->name);
  849. //}
  850. //if (dst) {
  851. // GGML_METAL_LOG_INFO("%s: dst - %4s [%5lld, %5lld, %5lld], 1, %s\n", __func__, ggml_type_name(dstt), ne0, ne1, ne2,
  852. // dst->name);
  853. //}
  854. switch (dst->op) {
  855. case GGML_OP_CONCAT:
  856. {
  857. const int64_t nb = ne00;
  858. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_CONCAT].pipeline;
  859. [encoder setComputePipelineState:pipeline];
  860. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  861. [encoder setBuffer:id_src1 offset:offs_src1 atIndex:1];
  862. [encoder setBuffer:id_dst offset:offs_dst atIndex:2];
  863. [encoder setBytes:&ne00 length:sizeof(ne00) atIndex:3];
  864. [encoder setBytes:&ne01 length:sizeof(ne01) atIndex:4];
  865. [encoder setBytes:&ne02 length:sizeof(ne02) atIndex:5];
  866. [encoder setBytes:&ne03 length:sizeof(ne03) atIndex:6];
  867. [encoder setBytes:&nb00 length:sizeof(nb00) atIndex:7];
  868. [encoder setBytes:&nb01 length:sizeof(nb01) atIndex:8];
  869. [encoder setBytes:&nb02 length:sizeof(nb02) atIndex:9];
  870. [encoder setBytes:&nb03 length:sizeof(nb03) atIndex:10];
  871. [encoder setBytes:&ne10 length:sizeof(ne10) atIndex:11];
  872. [encoder setBytes:&ne11 length:sizeof(ne11) atIndex:12];
  873. [encoder setBytes:&ne12 length:sizeof(ne12) atIndex:13];
  874. [encoder setBytes:&ne13 length:sizeof(ne13) atIndex:14];
  875. [encoder setBytes:&nb10 length:sizeof(nb10) atIndex:15];
  876. [encoder setBytes:&nb11 length:sizeof(nb11) atIndex:16];
  877. [encoder setBytes:&nb12 length:sizeof(nb12) atIndex:17];
  878. [encoder setBytes:&nb13 length:sizeof(nb13) atIndex:18];
  879. [encoder setBytes:&ne0 length:sizeof(ne0) atIndex:19];
  880. [encoder setBytes:&ne1 length:sizeof(ne1) atIndex:20];
  881. [encoder setBytes:&ne2 length:sizeof(ne2) atIndex:21];
  882. [encoder setBytes:&ne3 length:sizeof(ne3) atIndex:22];
  883. [encoder setBytes:&nb0 length:sizeof(nb0) atIndex:23];
  884. [encoder setBytes:&nb1 length:sizeof(nb1) atIndex:24];
  885. [encoder setBytes:&nb2 length:sizeof(nb2) atIndex:25];
  886. [encoder setBytes:&nb3 length:sizeof(nb3) atIndex:26];
  887. [encoder setBytes:&nb length:sizeof(nb) atIndex:27];
  888. const int nth = MIN(1024, ne0);
  889. [encoder dispatchThreadgroups:MTLSizeMake(ne1, ne2, ne3) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  890. } break;
  891. case GGML_OP_ADD:
  892. case GGML_OP_MUL:
  893. case GGML_OP_DIV:
  894. {
  895. const size_t offs = 0;
  896. bool bcast_row = false;
  897. int64_t nb = ne00;
  898. id<MTLComputePipelineState> pipeline = nil;
  899. if (ggml_nelements(src1) == ne10 && ggml_is_contiguous(src1) && ne00 % 4 == 0 && ne10 % 4 == 0) {
  900. GGML_ASSERT(ggml_is_contiguous(src0));
  901. // src1 is a row
  902. GGML_ASSERT(ne11 == 1);
  903. nb = ne00 / 4;
  904. switch (dst->op) {
  905. case GGML_OP_ADD: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_ADD_ROW].pipeline; break;
  906. case GGML_OP_MUL: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_ROW].pipeline; break;
  907. case GGML_OP_DIV: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_DIV_ROW].pipeline; break;
  908. default: GGML_ASSERT(false);
  909. }
  910. bcast_row = true;
  911. } else {
  912. switch (dst->op) {
  913. case GGML_OP_ADD: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_ADD].pipeline; break;
  914. case GGML_OP_MUL: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL].pipeline; break;
  915. case GGML_OP_DIV: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_DIV].pipeline; break;
  916. default: GGML_ASSERT(false);
  917. }
  918. }
  919. [encoder setComputePipelineState:pipeline];
  920. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  921. [encoder setBuffer:id_src1 offset:offs_src1 atIndex:1];
  922. [encoder setBuffer:id_dst offset:offs_dst atIndex:2];
  923. [encoder setBytes:&ne00 length:sizeof(ne00) atIndex:3];
  924. [encoder setBytes:&ne01 length:sizeof(ne01) atIndex:4];
  925. [encoder setBytes:&ne02 length:sizeof(ne02) atIndex:5];
  926. [encoder setBytes:&ne03 length:sizeof(ne03) atIndex:6];
  927. [encoder setBytes:&nb00 length:sizeof(nb00) atIndex:7];
  928. [encoder setBytes:&nb01 length:sizeof(nb01) atIndex:8];
  929. [encoder setBytes:&nb02 length:sizeof(nb02) atIndex:9];
  930. [encoder setBytes:&nb03 length:sizeof(nb03) atIndex:10];
  931. [encoder setBytes:&ne10 length:sizeof(ne10) atIndex:11];
  932. [encoder setBytes:&ne11 length:sizeof(ne11) atIndex:12];
  933. [encoder setBytes:&ne12 length:sizeof(ne12) atIndex:13];
  934. [encoder setBytes:&ne13 length:sizeof(ne13) atIndex:14];
  935. [encoder setBytes:&nb10 length:sizeof(nb10) atIndex:15];
  936. [encoder setBytes:&nb11 length:sizeof(nb11) atIndex:16];
  937. [encoder setBytes:&nb12 length:sizeof(nb12) atIndex:17];
  938. [encoder setBytes:&nb13 length:sizeof(nb13) atIndex:18];
  939. [encoder setBytes:&ne0 length:sizeof(ne0) atIndex:19];
  940. [encoder setBytes:&ne1 length:sizeof(ne1) atIndex:20];
  941. [encoder setBytes:&ne2 length:sizeof(ne2) atIndex:21];
  942. [encoder setBytes:&ne3 length:sizeof(ne3) atIndex:22];
  943. [encoder setBytes:&nb0 length:sizeof(nb0) atIndex:23];
  944. [encoder setBytes:&nb1 length:sizeof(nb1) atIndex:24];
  945. [encoder setBytes:&nb2 length:sizeof(nb2) atIndex:25];
  946. [encoder setBytes:&nb3 length:sizeof(nb3) atIndex:26];
  947. [encoder setBytes:&offs length:sizeof(offs) atIndex:27];
  948. [encoder setBytes:&nb length:sizeof(nb) atIndex:28];
  949. if (bcast_row) {
  950. const int64_t n = ggml_nelements(dst)/4;
  951. [encoder dispatchThreadgroups:MTLSizeMake(n, 1, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  952. } else {
  953. const int nth = MIN((int) pipeline.maxTotalThreadsPerThreadgroup, ne0);
  954. [encoder dispatchThreadgroups:MTLSizeMake(ne01, ne02, ne03) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  955. }
  956. } break;
  957. case GGML_OP_ACC:
  958. {
  959. GGML_ASSERT(src0t == GGML_TYPE_F32);
  960. GGML_ASSERT(src1t == GGML_TYPE_F32);
  961. GGML_ASSERT(dstt == GGML_TYPE_F32);
  962. GGML_ASSERT(ggml_is_contiguous(src0));
  963. GGML_ASSERT(ggml_is_contiguous(src1));
  964. const size_t pnb1 = ((int32_t *) dst->op_params)[0];
  965. const size_t pnb2 = ((int32_t *) dst->op_params)[1];
  966. const size_t pnb3 = ((int32_t *) dst->op_params)[2];
  967. const size_t offs = ((int32_t *) dst->op_params)[3];
  968. const bool inplace = (bool) ((int32_t *) dst->op_params)[4];
  969. if (!inplace) {
  970. // run a separete kernel to cpy src->dst
  971. // not sure how to avoid this
  972. // TODO: make a simpler cpy_bytes kernel
  973. const id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_CPY_F32_F32].pipeline;
  974. [encoder setComputePipelineState:pipeline];
  975. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  976. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  977. [encoder setBytes:&ne00 length:sizeof( int64_t) atIndex:2];
  978. [encoder setBytes:&ne01 length:sizeof( int64_t) atIndex:3];
  979. [encoder setBytes:&ne02 length:sizeof( int64_t) atIndex:4];
  980. [encoder setBytes:&ne03 length:sizeof( int64_t) atIndex:5];
  981. [encoder setBytes:&nb00 length:sizeof(uint64_t) atIndex:6];
  982. [encoder setBytes:&nb01 length:sizeof(uint64_t) atIndex:7];
  983. [encoder setBytes:&nb02 length:sizeof(uint64_t) atIndex:8];
  984. [encoder setBytes:&nb03 length:sizeof(uint64_t) atIndex:9];
  985. [encoder setBytes:&ne0 length:sizeof( int64_t) atIndex:10];
  986. [encoder setBytes:&ne1 length:sizeof( int64_t) atIndex:11];
  987. [encoder setBytes:&ne2 length:sizeof( int64_t) atIndex:12];
  988. [encoder setBytes:&ne3 length:sizeof( int64_t) atIndex:13];
  989. [encoder setBytes:&nb0 length:sizeof(uint64_t) atIndex:14];
  990. [encoder setBytes:&nb1 length:sizeof(uint64_t) atIndex:15];
  991. [encoder setBytes:&nb2 length:sizeof(uint64_t) atIndex:16];
  992. [encoder setBytes:&nb3 length:sizeof(uint64_t) atIndex:17];
  993. const int nth = MIN((int) pipeline.maxTotalThreadsPerThreadgroup, ne00);
  994. [encoder dispatchThreadgroups:MTLSizeMake(ne01, ne02, ne03) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  995. }
  996. const id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_ADD].pipeline;
  997. [encoder setComputePipelineState:pipeline];
  998. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  999. [encoder setBuffer:id_src1 offset:offs_src1 atIndex:1];
  1000. [encoder setBuffer:id_dst offset:offs_dst atIndex:2];
  1001. [encoder setBytes:&ne00 length:sizeof(ne00) atIndex:3];
  1002. [encoder setBytes:&ne01 length:sizeof(ne01) atIndex:4];
  1003. [encoder setBytes:&ne02 length:sizeof(ne02) atIndex:5];
  1004. [encoder setBytes:&ne03 length:sizeof(ne03) atIndex:6];
  1005. [encoder setBytes:&nb00 length:sizeof(nb00) atIndex:7];
  1006. [encoder setBytes:&pnb1 length:sizeof(pnb1) atIndex:8];
  1007. [encoder setBytes:&pnb2 length:sizeof(pnb2) atIndex:9];
  1008. [encoder setBytes:&pnb3 length:sizeof(pnb3) atIndex:10];
  1009. [encoder setBytes:&ne10 length:sizeof(ne10) atIndex:11];
  1010. [encoder setBytes:&ne11 length:sizeof(ne11) atIndex:12];
  1011. [encoder setBytes:&ne12 length:sizeof(ne12) atIndex:13];
  1012. [encoder setBytes:&ne13 length:sizeof(ne13) atIndex:14];
  1013. [encoder setBytes:&nb10 length:sizeof(nb10) atIndex:15];
  1014. [encoder setBytes:&nb11 length:sizeof(nb11) atIndex:16];
  1015. [encoder setBytes:&nb12 length:sizeof(nb12) atIndex:17];
  1016. [encoder setBytes:&nb13 length:sizeof(nb13) atIndex:18];
  1017. [encoder setBytes:&ne0 length:sizeof(ne0) atIndex:19];
  1018. [encoder setBytes:&ne1 length:sizeof(ne1) atIndex:20];
  1019. [encoder setBytes:&ne2 length:sizeof(ne2) atIndex:21];
  1020. [encoder setBytes:&ne3 length:sizeof(ne3) atIndex:22];
  1021. [encoder setBytes:&nb0 length:sizeof(nb0) atIndex:23];
  1022. [encoder setBytes:&pnb1 length:sizeof(pnb1) atIndex:24];
  1023. [encoder setBytes:&pnb2 length:sizeof(pnb2) atIndex:25];
  1024. [encoder setBytes:&pnb3 length:sizeof(pnb3) atIndex:26];
  1025. [encoder setBytes:&offs length:sizeof(offs) atIndex:27];
  1026. const int nth = MIN((int) pipeline.maxTotalThreadsPerThreadgroup, ne00);
  1027. [encoder dispatchThreadgroups:MTLSizeMake(ne11, ne12, ne13) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  1028. } break;
  1029. case GGML_OP_SCALE:
  1030. {
  1031. GGML_ASSERT(ggml_is_contiguous(src0));
  1032. float scale;
  1033. memcpy(&scale, dst->op_params, sizeof(scale));
  1034. int64_t n = ggml_nelements(dst);
  1035. id<MTLComputePipelineState> pipeline = nil;
  1036. if (n % 4 == 0) {
  1037. n /= 4;
  1038. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_SCALE_4].pipeline;
  1039. } else {
  1040. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_SCALE].pipeline;
  1041. }
  1042. [encoder setComputePipelineState:pipeline];
  1043. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1044. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  1045. [encoder setBytes:&scale length:sizeof(scale) atIndex:2];
  1046. [encoder dispatchThreadgroups:MTLSizeMake(n, 1, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  1047. } break;
  1048. case GGML_OP_CLAMP:
  1049. {
  1050. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_CLAMP].pipeline;
  1051. float min;
  1052. float max;
  1053. memcpy(&min, ((int32_t *) dst->op_params) + 0, sizeof(float));
  1054. memcpy(&max, ((int32_t *) dst->op_params) + 1, sizeof(float));
  1055. [encoder setComputePipelineState:pipeline];
  1056. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1057. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  1058. [encoder setBytes:&min length:sizeof(min) atIndex:2];
  1059. [encoder setBytes:&max length:sizeof(max) atIndex:3];
  1060. const int64_t n = ggml_nelements(dst);
  1061. [encoder dispatchThreadgroups:MTLSizeMake(n, 1, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  1062. } break;
  1063. case GGML_OP_UNARY:
  1064. switch (ggml_get_unary_op(gf->nodes[i])) {
  1065. // we are not taking into account the strides, so for now require contiguous tensors
  1066. GGML_ASSERT(ggml_is_contiguous(src0));
  1067. case GGML_UNARY_OP_TANH:
  1068. {
  1069. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_TANH].pipeline;
  1070. [encoder setComputePipelineState:pipeline];
  1071. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1072. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  1073. const int64_t n = ggml_nelements(dst);
  1074. [encoder dispatchThreadgroups:MTLSizeMake(n, 1, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  1075. } break;
  1076. case GGML_UNARY_OP_RELU:
  1077. {
  1078. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_RELU].pipeline;
  1079. [encoder setComputePipelineState:pipeline];
  1080. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1081. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  1082. const int64_t n = ggml_nelements(dst);
  1083. [encoder dispatchThreadgroups:MTLSizeMake(n, 1, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  1084. } break;
  1085. case GGML_UNARY_OP_GELU:
  1086. {
  1087. int64_t n = ggml_nelements(dst);
  1088. id<MTLComputePipelineState> pipeline = nil;
  1089. if (n % 4 == 0) {
  1090. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GELU_4].pipeline;
  1091. n /= 4;
  1092. } else {
  1093. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GELU].pipeline;
  1094. }
  1095. [encoder setComputePipelineState:pipeline];
  1096. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1097. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  1098. [encoder dispatchThreadgroups:MTLSizeMake(n, 1, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  1099. } break;
  1100. case GGML_UNARY_OP_GELU_QUICK:
  1101. {
  1102. int64_t n = ggml_nelements(dst);
  1103. id<MTLComputePipelineState> pipeline = nil;
  1104. if (n % 4 == 0) {
  1105. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GELU_QUICK_4].pipeline;
  1106. n /= 4;
  1107. } else {
  1108. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GELU_QUICK].pipeline;
  1109. }
  1110. [encoder setComputePipelineState:pipeline];
  1111. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1112. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  1113. [encoder dispatchThreadgroups:MTLSizeMake(n, 1, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  1114. } break;
  1115. case GGML_UNARY_OP_SILU:
  1116. {
  1117. int64_t n = ggml_nelements(dst);
  1118. id<MTLComputePipelineState> pipeline = nil;
  1119. if (n % 4 == 0) {
  1120. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_SILU_4].pipeline;
  1121. n /= 4;
  1122. } else {
  1123. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_SILU].pipeline;
  1124. }
  1125. [encoder setComputePipelineState:pipeline];
  1126. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1127. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  1128. [encoder dispatchThreadgroups:MTLSizeMake(n, 1, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  1129. } break;
  1130. default:
  1131. {
  1132. GGML_METAL_LOG_WARN("%s: node %3d, op = %8s not implemented\n", __func__, i, ggml_op_name(dst->op));
  1133. GGML_ASSERT(false);
  1134. }
  1135. } break;
  1136. case GGML_OP_SQR:
  1137. {
  1138. GGML_ASSERT(ggml_is_contiguous(src0));
  1139. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_SQR].pipeline;
  1140. [encoder setComputePipelineState:pipeline];
  1141. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1142. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  1143. const int64_t n = ggml_nelements(dst);
  1144. [encoder dispatchThreadgroups:MTLSizeMake(n, 1, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  1145. } break;
  1146. case GGML_OP_SUM_ROWS:
  1147. {
  1148. GGML_ASSERT(src0->nb[0] == ggml_type_size(src0->type));
  1149. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_SUM_ROWS].pipeline;
  1150. [encoder setComputePipelineState:pipeline];
  1151. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1152. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  1153. [encoder setBytes:&ne00 length:sizeof(ne00) atIndex:2];
  1154. [encoder setBytes:&ne01 length:sizeof(ne01) atIndex:3];
  1155. [encoder setBytes:&ne02 length:sizeof(ne02) atIndex:4];
  1156. [encoder setBytes:&ne03 length:sizeof(ne03) atIndex:5];
  1157. [encoder setBytes:&nb00 length:sizeof(nb00) atIndex:6];
  1158. [encoder setBytes:&nb01 length:sizeof(nb01) atIndex:7];
  1159. [encoder setBytes:&nb02 length:sizeof(nb02) atIndex:8];
  1160. [encoder setBytes:&nb03 length:sizeof(nb03) atIndex:9];
  1161. [encoder setBytes:&ne10 length:sizeof(ne10) atIndex:10];
  1162. [encoder setBytes:&ne11 length:sizeof(ne11) atIndex:11];
  1163. [encoder setBytes:&ne12 length:sizeof(ne12) atIndex:12];
  1164. [encoder setBytes:&ne13 length:sizeof(ne13) atIndex:13];
  1165. [encoder setBytes:&nb10 length:sizeof(nb10) atIndex:14];
  1166. [encoder setBytes:&nb11 length:sizeof(nb11) atIndex:15];
  1167. [encoder setBytes:&nb12 length:sizeof(nb12) atIndex:16];
  1168. [encoder setBytes:&nb13 length:sizeof(nb13) atIndex:17];
  1169. [encoder setBytes:&ne0 length:sizeof(ne0) atIndex:18];
  1170. [encoder setBytes:&ne1 length:sizeof(ne1) atIndex:19];
  1171. [encoder setBytes:&ne2 length:sizeof(ne2) atIndex:20];
  1172. [encoder setBytes:&ne3 length:sizeof(ne3) atIndex:21];
  1173. [encoder setBytes:&nb0 length:sizeof(nb0) atIndex:22];
  1174. [encoder setBytes:&nb1 length:sizeof(nb1) atIndex:23];
  1175. [encoder setBytes:&nb2 length:sizeof(nb2) atIndex:24];
  1176. [encoder setBytes:&nb3 length:sizeof(nb3) atIndex:25];
  1177. [encoder dispatchThreadgroups:MTLSizeMake(ne01, ne02, ne03) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  1178. } break;
  1179. case GGML_OP_SOFT_MAX:
  1180. {
  1181. GGML_ASSERT(!src1 || src1->type == GGML_TYPE_F16 || src1->type == GGML_TYPE_F32);
  1182. GGML_ASSERT(!src2 || src2->type == GGML_TYPE_F16 || src2->type == GGML_TYPE_F32);
  1183. int nth = 32; // SIMD width
  1184. id<MTLComputePipelineState> pipeline = nil;
  1185. const bool use_f16 = (src1 && src1->type == GGML_TYPE_F16) || (src2 && src2->type == GGML_TYPE_F16);
  1186. if (ne00%4 == 0) {
  1187. while (nth < ne00/4 && nth < 256) {
  1188. nth *= 2;
  1189. }
  1190. if (use_f16) {
  1191. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_SOFT_MAX_F16_4].pipeline;
  1192. } else {
  1193. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_SOFT_MAX_F32_4].pipeline;
  1194. }
  1195. } else {
  1196. while (nth < ne00 && nth < 1024) {
  1197. nth *= 2;
  1198. }
  1199. if (use_f16) {
  1200. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_SOFT_MAX_F16].pipeline;
  1201. } else {
  1202. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_SOFT_MAX_F32].pipeline;
  1203. }
  1204. }
  1205. float scale;
  1206. float max_bias;
  1207. memcpy(&scale, ((int32_t *) dst->op_params) + 0, sizeof(scale));
  1208. memcpy(&max_bias, ((int32_t *) dst->op_params) + 1, sizeof(max_bias));
  1209. const int64_t nrows_x = ggml_nrows(src0);
  1210. const int64_t nrows_y = src0->ne[1];
  1211. const uint32_t n_head_kv = nrows_x/nrows_y;
  1212. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head_kv));
  1213. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  1214. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  1215. [encoder setComputePipelineState:pipeline];
  1216. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1217. if (id_src1) {
  1218. [encoder setBuffer:id_src1 offset:offs_src1 atIndex:1];
  1219. } else {
  1220. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:1];
  1221. }
  1222. if (id_src2) {
  1223. [encoder setBuffer:id_src2 offset:offs_src2 atIndex:2];
  1224. } else {
  1225. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:2];
  1226. }
  1227. [encoder setBuffer:id_dst offset:offs_dst atIndex:3];
  1228. [encoder setBytes:&ne00 length:sizeof(ne00) atIndex:4];
  1229. [encoder setBytes:&ne01 length:sizeof(ne01) atIndex:5];
  1230. [encoder setBytes:&ne02 length:sizeof(ne02) atIndex:6];
  1231. [encoder setBytes:&scale length:sizeof(scale) atIndex:7];
  1232. [encoder setBytes:&max_bias length:sizeof(max_bias) atIndex:8];
  1233. [encoder setBytes:&m0 length:sizeof(m0) atIndex:9];
  1234. [encoder setBytes:&m1 length:sizeof(m1) atIndex:10];
  1235. [encoder setBytes:&n_head_log2 length:sizeof(n_head_log2) atIndex:11];
  1236. [encoder setThreadgroupMemoryLength:32*sizeof(float) atIndex:0];
  1237. [encoder dispatchThreadgroups:MTLSizeMake(ne01*ne02*ne03, 1, 1) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  1238. } break;
  1239. case GGML_OP_DIAG_MASK_INF:
  1240. {
  1241. const int n_past = ((int32_t *)(dst->op_params))[0];
  1242. id<MTLComputePipelineState> pipeline = nil;
  1243. if (ne00%8 == 0) {
  1244. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_DIAG_MASK_INF_8].pipeline;
  1245. } else {
  1246. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_DIAG_MASK_INF].pipeline;
  1247. }
  1248. [encoder setComputePipelineState:pipeline];
  1249. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1250. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  1251. [encoder setBytes:&ne00 length:sizeof(ne00) atIndex:2];
  1252. [encoder setBytes:&ne01 length:sizeof(ne01) atIndex:3];
  1253. [encoder setBytes:&n_past length:sizeof(int) atIndex:4];
  1254. if (ne00%8 == 0) {
  1255. [encoder dispatchThreadgroups:MTLSizeMake(ne00*ne01*ne02/8, 1, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  1256. }
  1257. else {
  1258. [encoder dispatchThreadgroups:MTLSizeMake(ne00, ne01, ne02) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  1259. }
  1260. } break;
  1261. case GGML_OP_MUL_MAT:
  1262. {
  1263. GGML_ASSERT(ne00 == ne10);
  1264. // TODO: assert that dim2 and dim3 are contiguous
  1265. GGML_ASSERT(ne12 % ne02 == 0);
  1266. GGML_ASSERT(ne13 % ne03 == 0);
  1267. const uint r2 = ne12/ne02;
  1268. const uint r3 = ne13/ne03;
  1269. // find the break-even point where the matrix-matrix kernel becomes more efficient compared
  1270. // to the matrix-vector kernel
  1271. int ne11_mm_min = 1;
  1272. #if 0
  1273. // the numbers below are measured on M2 Ultra for 7B and 13B models
  1274. // these numbers do not translate to other devices or model sizes
  1275. // TODO: need to find a better approach
  1276. if ([ctx->device.name isEqualToString:@"Apple M2 Ultra"]) {
  1277. switch (src0t) {
  1278. case GGML_TYPE_F16: ne11_mm_min = 2; break;
  1279. case GGML_TYPE_Q8_0: ne11_mm_min = 7; break;
  1280. case GGML_TYPE_Q2_K: ne11_mm_min = 15; break;
  1281. case GGML_TYPE_Q3_K: ne11_mm_min = 7; break;
  1282. case GGML_TYPE_Q4_0:
  1283. case GGML_TYPE_Q4_1: ne11_mm_min = 15; break;
  1284. case GGML_TYPE_Q4_K: ne11_mm_min = 11; break;
  1285. case GGML_TYPE_Q5_0: // not tested yet
  1286. case GGML_TYPE_Q5_1: ne11_mm_min = 13; break; // not tested yet
  1287. case GGML_TYPE_Q5_K: ne11_mm_min = 7; break;
  1288. case GGML_TYPE_Q6_K: ne11_mm_min = 7; break;
  1289. default: ne11_mm_min = 1; break;
  1290. }
  1291. }
  1292. #endif
  1293. // for now the matrix-matrix multiplication kernel only works on A14+/M1+ SoCs
  1294. // AMD GPU and older A-chips will reuse matrix-vector multiplication kernel
  1295. if ([ctx->device supportsFamily:MTLGPUFamilyApple7] &&
  1296. !ggml_is_transposed(src0) &&
  1297. !ggml_is_transposed(src1) &&
  1298. src1t == GGML_TYPE_F32 &&
  1299. ne00 % 32 == 0 && ne00 >= 64 &&
  1300. (ne11 > ne11_mm_min || (ggml_is_quantized(src0t) && ne12 > 1))) {
  1301. //printf("matrix: ne00 = %6d, ne01 = %6d, ne02 = %6d, ne11 = %6d, ne12 = %6d\n", ne00, ne01, ne02, ne11, ne12);
  1302. // some Metal matrix data types require aligned pointers
  1303. // ref: https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf (Table 2.5)
  1304. switch (src0->type) {
  1305. case GGML_TYPE_F32: GGML_ASSERT(nb01 % 16 == 0); break;
  1306. case GGML_TYPE_F16: GGML_ASSERT(nb01 % 8 == 0); break;
  1307. default: break;
  1308. }
  1309. id<MTLComputePipelineState> pipeline = nil;
  1310. switch (src0->type) {
  1311. case GGML_TYPE_F32: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_F32_F32 ].pipeline; break;
  1312. case GGML_TYPE_F16: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_F16_F32 ].pipeline; break;
  1313. case GGML_TYPE_Q4_0: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_Q4_0_F32 ].pipeline; break;
  1314. case GGML_TYPE_Q4_1: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_Q4_1_F32 ].pipeline; break;
  1315. case GGML_TYPE_Q5_0: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_Q5_0_F32 ].pipeline; break;
  1316. case GGML_TYPE_Q5_1: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_Q5_1_F32 ].pipeline; break;
  1317. case GGML_TYPE_Q8_0: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_Q8_0_F32 ].pipeline; break;
  1318. case GGML_TYPE_Q2_K: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_Q2_K_F32 ].pipeline; break;
  1319. case GGML_TYPE_Q3_K: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_Q3_K_F32 ].pipeline; break;
  1320. case GGML_TYPE_Q4_K: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_Q4_K_F32 ].pipeline; break;
  1321. case GGML_TYPE_Q5_K: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_Q5_K_F32 ].pipeline; break;
  1322. case GGML_TYPE_Q6_K: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_Q6_K_F32 ].pipeline; break;
  1323. case GGML_TYPE_IQ2_XXS: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_IQ2_XXS_F32].pipeline; break;
  1324. case GGML_TYPE_IQ2_XS: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_IQ2_XS_F32 ].pipeline; break;
  1325. case GGML_TYPE_IQ3_XXS: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_IQ3_XXS_F32].pipeline; break;
  1326. case GGML_TYPE_IQ3_S: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_IQ3_S_F32 ].pipeline; break;
  1327. case GGML_TYPE_IQ2_S: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_IQ2_S_F32 ].pipeline; break;
  1328. case GGML_TYPE_IQ1_S: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_IQ1_S_F32 ].pipeline; break;
  1329. case GGML_TYPE_IQ1_M: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_IQ1_M_F32 ].pipeline; break;
  1330. case GGML_TYPE_IQ4_NL: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_IQ4_NL_F32 ].pipeline; break;
  1331. case GGML_TYPE_IQ4_XS: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_IQ4_XS_F32 ].pipeline; break;
  1332. default: GGML_ASSERT(false && "MUL MAT-MAT not implemented");
  1333. }
  1334. [encoder setComputePipelineState:pipeline];
  1335. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1336. [encoder setBuffer:id_src1 offset:offs_src1 atIndex:1];
  1337. [encoder setBuffer:id_dst offset:offs_dst atIndex:2];
  1338. [encoder setBytes:&ne00 length:sizeof(ne00) atIndex:3];
  1339. [encoder setBytes:&ne02 length:sizeof(ne02) atIndex:4];
  1340. [encoder setBytes:&nb01 length:sizeof(nb01) atIndex:5];
  1341. [encoder setBytes:&nb02 length:sizeof(nb02) atIndex:6];
  1342. [encoder setBytes:&ne12 length:sizeof(ne12) atIndex:7];
  1343. [encoder setBytes:&nb10 length:sizeof(nb10) atIndex:8];
  1344. [encoder setBytes:&nb11 length:sizeof(nb11) atIndex:9];
  1345. [encoder setBytes:&nb12 length:sizeof(nb12) atIndex:10];
  1346. [encoder setBytes:&ne0 length:sizeof(ne0) atIndex:11];
  1347. [encoder setBytes:&ne1 length:sizeof(ne1) atIndex:12];
  1348. [encoder setBytes:&r2 length:sizeof(r2) atIndex:13];
  1349. [encoder setBytes:&r3 length:sizeof(r3) atIndex:14];
  1350. [encoder setThreadgroupMemoryLength:8192 atIndex:0];
  1351. [encoder dispatchThreadgroups:MTLSizeMake( (ne11 + 31)/32, (ne01 + 63)/64, ne12*ne13) threadsPerThreadgroup:MTLSizeMake(128, 1, 1)];
  1352. } else {
  1353. int nth0 = 32;
  1354. int nth1 = 1;
  1355. int nrows = 1;
  1356. //printf("vector: ne00 = %6d, ne01 = %6d, ne02 = %6d, ne11 = %6d, ne12 = %6d\n", ne00, ne01, ne02, ne11, ne12);
  1357. id<MTLComputePipelineState> pipeline = nil;
  1358. // use custom matrix x vector kernel
  1359. switch (src0t) {
  1360. case GGML_TYPE_F32:
  1361. {
  1362. GGML_ASSERT(src1t == GGML_TYPE_F32);
  1363. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_F32_F32].pipeline;
  1364. nrows = 4;
  1365. } break;
  1366. case GGML_TYPE_F16:
  1367. {
  1368. nth0 = 32;
  1369. nth1 = 1;
  1370. if (src1t == GGML_TYPE_F32) {
  1371. if (ne11 * ne12 < 4) {
  1372. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_F16_F32_1ROW].pipeline;
  1373. } else if (ne00 >= 128 && ne01 >= 8 && ne00%4 == 0) {
  1374. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_F16_F32_L4].pipeline;
  1375. nrows = ne11;
  1376. } else {
  1377. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_F16_F32].pipeline;
  1378. nrows = 4;
  1379. }
  1380. } else {
  1381. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_F16_F16].pipeline;
  1382. nrows = 4;
  1383. }
  1384. } break;
  1385. case GGML_TYPE_Q4_0:
  1386. {
  1387. nth0 = 8;
  1388. nth1 = 8;
  1389. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_Q4_0_F32].pipeline;
  1390. } break;
  1391. case GGML_TYPE_Q4_1:
  1392. {
  1393. nth0 = 8;
  1394. nth1 = 8;
  1395. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_Q4_1_F32].pipeline;
  1396. } break;
  1397. case GGML_TYPE_Q5_0:
  1398. {
  1399. nth0 = 8;
  1400. nth1 = 8;
  1401. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_Q5_0_F32].pipeline;
  1402. } break;
  1403. case GGML_TYPE_Q5_1:
  1404. {
  1405. nth0 = 8;
  1406. nth1 = 8;
  1407. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_Q5_1_F32].pipeline;
  1408. } break;
  1409. case GGML_TYPE_Q8_0:
  1410. {
  1411. nth0 = 8;
  1412. nth1 = 8;
  1413. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_Q8_0_F32].pipeline;
  1414. } break;
  1415. case GGML_TYPE_Q2_K:
  1416. {
  1417. nth0 = 2;
  1418. nth1 = 32;
  1419. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_Q2_K_F32].pipeline;
  1420. } break;
  1421. case GGML_TYPE_Q3_K:
  1422. {
  1423. nth0 = 2;
  1424. nth1 = 32;
  1425. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_Q3_K_F32].pipeline;
  1426. } break;
  1427. case GGML_TYPE_Q4_K:
  1428. {
  1429. nth0 = 4; //1;
  1430. nth1 = 8; //32;
  1431. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_Q4_K_F32].pipeline;
  1432. } break;
  1433. case GGML_TYPE_Q5_K:
  1434. {
  1435. nth0 = 2;
  1436. nth1 = 32;
  1437. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_Q5_K_F32].pipeline;
  1438. } break;
  1439. case GGML_TYPE_Q6_K:
  1440. {
  1441. nth0 = 2;
  1442. nth1 = 32;
  1443. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_Q6_K_F32].pipeline;
  1444. } break;
  1445. case GGML_TYPE_IQ2_XXS:
  1446. {
  1447. nth0 = 4;
  1448. nth1 = 16;
  1449. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_IQ2_XXS_F32].pipeline;
  1450. } break;
  1451. case GGML_TYPE_IQ2_XS:
  1452. {
  1453. nth0 = 4;
  1454. nth1 = 16;
  1455. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_IQ2_XS_F32].pipeline;
  1456. } break;
  1457. case GGML_TYPE_IQ3_XXS:
  1458. {
  1459. nth0 = 4;
  1460. nth1 = 16;
  1461. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_IQ3_XXS_F32].pipeline;
  1462. } break;
  1463. case GGML_TYPE_IQ3_S:
  1464. {
  1465. nth0 = 4;
  1466. nth1 = 16;
  1467. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_IQ3_S_F32].pipeline;
  1468. } break;
  1469. case GGML_TYPE_IQ2_S:
  1470. {
  1471. nth0 = 4;
  1472. nth1 = 16;
  1473. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_IQ2_S_F32].pipeline;
  1474. } break;
  1475. case GGML_TYPE_IQ1_S:
  1476. {
  1477. nth0 = 4;
  1478. nth1 = 16;
  1479. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_IQ1_S_F32].pipeline;
  1480. } break;
  1481. case GGML_TYPE_IQ1_M:
  1482. {
  1483. nth0 = 4;
  1484. nth1 = 16;
  1485. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_IQ1_M_F32].pipeline;
  1486. } break;
  1487. case GGML_TYPE_IQ4_NL:
  1488. {
  1489. nth0 = 4;
  1490. nth1 = 16;
  1491. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_IQ4_NL_F32].pipeline;
  1492. } break;
  1493. case GGML_TYPE_IQ4_XS:
  1494. {
  1495. nth0 = 4;
  1496. nth1 = 16;
  1497. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_IQ4_XS_F32].pipeline;
  1498. } break;
  1499. default:
  1500. {
  1501. GGML_METAL_LOG_ERROR("Asserting on type %d\n", (int)src0t);
  1502. GGML_ASSERT(false && "not implemented");
  1503. }
  1504. };
  1505. if (ggml_is_quantized(src0t)) {
  1506. GGML_ASSERT(ne00 >= nth0*nth1);
  1507. }
  1508. [encoder setComputePipelineState:pipeline];
  1509. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1510. [encoder setBuffer:id_src1 offset:offs_src1 atIndex:1];
  1511. [encoder setBuffer:id_dst offset:offs_dst atIndex:2];
  1512. [encoder setBytes:&ne00 length:sizeof(ne00) atIndex:3];
  1513. [encoder setBytes:&ne01 length:sizeof(ne01) atIndex:4];
  1514. [encoder setBytes:&ne02 length:sizeof(ne02) atIndex:5];
  1515. [encoder setBytes:&nb00 length:sizeof(nb00) atIndex:6];
  1516. [encoder setBytes:&nb01 length:sizeof(nb01) atIndex:7];
  1517. [encoder setBytes:&nb02 length:sizeof(nb02) atIndex:8];
  1518. [encoder setBytes:&ne10 length:sizeof(ne10) atIndex:9];
  1519. [encoder setBytes:&ne11 length:sizeof(ne11) atIndex:10];
  1520. [encoder setBytes:&ne12 length:sizeof(ne12) atIndex:11];
  1521. [encoder setBytes:&nb10 length:sizeof(nb10) atIndex:12];
  1522. [encoder setBytes:&nb11 length:sizeof(nb11) atIndex:13];
  1523. [encoder setBytes:&nb12 length:sizeof(nb12) atIndex:14];
  1524. [encoder setBytes:&ne0 length:sizeof(ne0) atIndex:15];
  1525. [encoder setBytes:&ne1 length:sizeof(ne1) atIndex:16];
  1526. [encoder setBytes:&r2 length:sizeof(r2) atIndex:17];
  1527. [encoder setBytes:&r3 length:sizeof(r3) atIndex:18];
  1528. if (src0t == GGML_TYPE_Q4_0 || src0t == GGML_TYPE_Q4_1 || src0t == GGML_TYPE_Q5_0 ||
  1529. src0t == GGML_TYPE_Q5_1 || src0t == GGML_TYPE_Q8_0 || src0t == GGML_TYPE_Q2_K ||
  1530. src0t == GGML_TYPE_IQ1_S || src0t == GGML_TYPE_IQ1_M || src0t == GGML_TYPE_IQ2_S) {
  1531. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + 7)/8, ne11, ne12*ne13) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  1532. }
  1533. else if (src0t == GGML_TYPE_IQ2_XXS || src0t == GGML_TYPE_IQ2_XS) {
  1534. const int mem_size = src0t == GGML_TYPE_IQ2_XXS ? 256*8+128 : 512*8+128;
  1535. [encoder setThreadgroupMemoryLength:mem_size atIndex:0];
  1536. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + 7)/8, ne11, ne12*ne13) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  1537. }
  1538. else if (src0t == GGML_TYPE_IQ3_XXS || src0t == GGML_TYPE_IQ3_S) {
  1539. const int mem_size = src0t == GGML_TYPE_IQ3_XXS ? 256*4+128 : 512*4;
  1540. [encoder setThreadgroupMemoryLength:mem_size atIndex:0];
  1541. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + 7)/8, ne11, ne12*ne13) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  1542. }
  1543. else if (src0t == GGML_TYPE_IQ4_NL || src0t == GGML_TYPE_IQ4_XS) {
  1544. const int mem_size = 32*sizeof(float);
  1545. [encoder setThreadgroupMemoryLength:mem_size atIndex:0];
  1546. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + 3)/4, ne11, ne12*ne13) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  1547. }
  1548. else if (src0t == GGML_TYPE_Q4_K) {
  1549. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + 3)/4, ne11, ne12*ne13) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  1550. }
  1551. else if (src0t == GGML_TYPE_Q3_K) {
  1552. #ifdef GGML_QKK_64
  1553. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + 1)/2, ne11, ne12*ne13) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  1554. #else
  1555. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + 3)/4, ne11, ne12*ne13) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  1556. #endif
  1557. }
  1558. else if (src0t == GGML_TYPE_Q5_K) {
  1559. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + 3)/4, ne11, ne12*ne13) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  1560. }
  1561. else if (src0t == GGML_TYPE_Q6_K) {
  1562. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + 1)/2, ne11, ne12*ne13) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  1563. } else {
  1564. const int64_t ny = (ne11 + nrows - 1)/nrows;
  1565. [encoder dispatchThreadgroups:MTLSizeMake(ne01, ny, ne12*ne13) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  1566. }
  1567. }
  1568. } break;
  1569. case GGML_OP_MUL_MAT_ID:
  1570. {
  1571. const int n_as = src0->ne[2];
  1572. // src2 = ids
  1573. const int64_t ne20 = src2->ne[0];
  1574. const int64_t ne21 = src2->ne[1];
  1575. const int64_t ne22 = src2->ne[2]; GGML_UNUSED(ne22);
  1576. const int64_t ne23 = src2->ne[3]; GGML_UNUSED(ne23);
  1577. const uint64_t nb20 = src2->nb[0]; GGML_UNUSED(nb20);
  1578. const uint64_t nb21 = src2->nb[1];
  1579. const uint64_t nb22 = src2->nb[2]; GGML_UNUSED(nb22);
  1580. const uint64_t nb23 = src2->nb[3]; GGML_UNUSED(nb23);
  1581. const enum ggml_type src2t = src2->type; GGML_UNUSED(src2t);
  1582. GGML_ASSERT(src2t == GGML_TYPE_I32);
  1583. GGML_ASSERT(!ggml_is_transposed(src0));
  1584. GGML_ASSERT(!ggml_is_transposed(src1));
  1585. GGML_ASSERT(src1t == GGML_TYPE_F32);
  1586. // find the break-even point where the matrix-matrix kernel becomes more efficient compared
  1587. // to the matrix-vector kernel
  1588. // ne20 = n_used_experts
  1589. // ne21 = n_rows
  1590. const int dst_rows = ne20*ne21;
  1591. const int dst_rows_min = n_as;
  1592. // max size of the rowids array in the kernel shared buffer
  1593. GGML_ASSERT(dst_rows <= 2048);
  1594. // for now the matrix-matrix multiplication kernel only works on A14+/M1+ SoCs
  1595. // AMD GPU and older A-chips will reuse matrix-vector multiplication kernel
  1596. // !!!
  1597. // TODO: for now, always use mat-vec kernels until we figure out how to improve the
  1598. // indirect matrix multiplication
  1599. // !!!
  1600. if ([ctx->device supportsFamily:MTLGPUFamilyApple7] &&
  1601. ne00 % 32 == 0 && ne00 >= 64 &&
  1602. dst_rows > dst_rows_min) {
  1603. // some Metal matrix data types require aligned pointers
  1604. // ref: https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf (Table 2.5)
  1605. switch (src0->type) {
  1606. case GGML_TYPE_F32: GGML_ASSERT(nb01 % 16 == 0); break;
  1607. case GGML_TYPE_F16: GGML_ASSERT(nb01 % 8 == 0); break;
  1608. default: break;
  1609. }
  1610. id<MTLComputePipelineState> pipeline = nil;
  1611. switch (src0->type) {
  1612. case GGML_TYPE_F32: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_F32_F32 ].pipeline; break;
  1613. case GGML_TYPE_F16: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_F16_F32 ].pipeline; break;
  1614. case GGML_TYPE_Q4_0: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q4_0_F32 ].pipeline; break;
  1615. case GGML_TYPE_Q4_1: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q4_1_F32 ].pipeline; break;
  1616. case GGML_TYPE_Q5_0: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q5_0_F32 ].pipeline; break;
  1617. case GGML_TYPE_Q5_1: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q5_1_F32 ].pipeline; break;
  1618. case GGML_TYPE_Q8_0: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q8_0_F32 ].pipeline; break;
  1619. case GGML_TYPE_Q2_K: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q2_K_F32 ].pipeline; break;
  1620. case GGML_TYPE_Q3_K: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q3_K_F32 ].pipeline; break;
  1621. case GGML_TYPE_Q4_K: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q4_K_F32 ].pipeline; break;
  1622. case GGML_TYPE_Q5_K: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q5_K_F32 ].pipeline; break;
  1623. case GGML_TYPE_Q6_K: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q6_K_F32 ].pipeline; break;
  1624. case GGML_TYPE_IQ2_XXS: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ2_XXS_F32].pipeline; break;
  1625. case GGML_TYPE_IQ2_XS: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ2_XS_F32 ].pipeline; break;
  1626. case GGML_TYPE_IQ3_XXS: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ3_XXS_F32].pipeline; break;
  1627. case GGML_TYPE_IQ3_S: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ3_S_F32 ].pipeline; break;
  1628. case GGML_TYPE_IQ2_S: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ2_S_F32 ].pipeline; break;
  1629. case GGML_TYPE_IQ1_S: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ1_S_F32 ].pipeline; break;
  1630. case GGML_TYPE_IQ1_M: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ1_M_F32 ].pipeline; break;
  1631. case GGML_TYPE_IQ4_NL: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ4_NL_F32 ].pipeline; break;
  1632. case GGML_TYPE_IQ4_XS: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ4_XS_F32 ].pipeline; break;
  1633. default: GGML_ASSERT(false && "MUL_MAT_ID not implemented");
  1634. }
  1635. [encoder setComputePipelineState:pipeline];
  1636. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1637. [encoder setBuffer:id_src1 offset:offs_src1 atIndex:1];
  1638. [encoder setBuffer:id_dst offset:offs_dst atIndex:2];
  1639. [encoder setBuffer:id_src2 offset:offs_src2 atIndex:3];
  1640. [encoder setBytes:&ne20 length:sizeof(ne20) atIndex:4];
  1641. [encoder setBytes:&ne21 length:sizeof(ne21) atIndex:5];
  1642. [encoder setBytes:&nb21 length:sizeof(nb21) atIndex:6];
  1643. [encoder setBytes:&ne00 length:sizeof(ne00) atIndex:7];
  1644. [encoder setBytes:&ne02 length:sizeof(ne02) atIndex:8];
  1645. [encoder setBytes:&nb01 length:sizeof(nb01) atIndex:9];
  1646. [encoder setBytes:&nb02 length:sizeof(nb02) atIndex:10];
  1647. [encoder setBytes:&ne11 length:sizeof(ne11) atIndex:11];
  1648. [encoder setBytes:&ne12 length:sizeof(ne12) atIndex:12];
  1649. [encoder setBytes:&ne13 length:sizeof(ne13) atIndex:13];
  1650. [encoder setBytes:&nb10 length:sizeof(nb10) atIndex:14];
  1651. [encoder setBytes:&nb11 length:sizeof(nb11) atIndex:15];
  1652. [encoder setBytes:&nb12 length:sizeof(nb12) atIndex:16];
  1653. [encoder setBytes:&ne0 length:sizeof(ne0) atIndex:17];
  1654. [encoder setBytes:&ne1 length:sizeof(ne1) atIndex:18];
  1655. [encoder setBytes:&nb1 length:sizeof(nb1) atIndex:19];
  1656. [encoder setThreadgroupMemoryLength:GGML_PAD(8192 + dst_rows*4/*sizeof(ushort2)*/, 16) atIndex:0];
  1657. [encoder dispatchThreadgroups:MTLSizeMake((ne21 + 31)/32, (ne01 + 63)/64, n_as) threadsPerThreadgroup:MTLSizeMake(128, 1, 1)];
  1658. } else {
  1659. int nth0 = 32;
  1660. int nth1 = 1;
  1661. int nrows = 1;
  1662. //printf("vector: ne00 = %6d, ne01 = %6d, ne02 = %6d, ne11 = %6d, ne12 = %6d\n", ne00, ne01, ne02, ne11, ne12);
  1663. id<MTLComputePipelineState> pipeline = nil;
  1664. // use custom matrix x vector kernel
  1665. switch (src0t) {
  1666. case GGML_TYPE_F32:
  1667. {
  1668. GGML_ASSERT(src1t == GGML_TYPE_F32);
  1669. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_F32_F32].pipeline;
  1670. } break;
  1671. case GGML_TYPE_F16:
  1672. {
  1673. GGML_ASSERT(src1t == GGML_TYPE_F32);
  1674. nth0 = 32;
  1675. nth1 = 1;
  1676. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_F16_F32].pipeline;
  1677. } break;
  1678. case GGML_TYPE_Q4_0:
  1679. {
  1680. nth0 = 8;
  1681. nth1 = 8;
  1682. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q4_0_F32].pipeline;
  1683. } break;
  1684. case GGML_TYPE_Q4_1:
  1685. {
  1686. nth0 = 8;
  1687. nth1 = 8;
  1688. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q4_1_F32].pipeline;
  1689. } break;
  1690. case GGML_TYPE_Q5_0:
  1691. {
  1692. nth0 = 8;
  1693. nth1 = 8;
  1694. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q5_0_F32].pipeline;
  1695. } break;
  1696. case GGML_TYPE_Q5_1:
  1697. {
  1698. nth0 = 8;
  1699. nth1 = 8;
  1700. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q5_1_F32].pipeline;
  1701. } break;
  1702. case GGML_TYPE_Q8_0:
  1703. {
  1704. nth0 = 8;
  1705. nth1 = 8;
  1706. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q8_0_F32].pipeline;
  1707. } break;
  1708. case GGML_TYPE_Q2_K:
  1709. {
  1710. nth0 = 2;
  1711. nth1 = 32;
  1712. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q2_K_F32].pipeline;
  1713. } break;
  1714. case GGML_TYPE_Q3_K:
  1715. {
  1716. nth0 = 2;
  1717. nth1 = 32;
  1718. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q3_K_F32].pipeline;
  1719. } break;
  1720. case GGML_TYPE_Q4_K:
  1721. {
  1722. nth0 = 4; //1;
  1723. nth1 = 8; //32;
  1724. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q4_K_F32].pipeline;
  1725. } break;
  1726. case GGML_TYPE_Q5_K:
  1727. {
  1728. nth0 = 2;
  1729. nth1 = 32;
  1730. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q5_K_F32].pipeline;
  1731. } break;
  1732. case GGML_TYPE_Q6_K:
  1733. {
  1734. nth0 = 2;
  1735. nth1 = 32;
  1736. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q6_K_F32].pipeline;
  1737. } break;
  1738. case GGML_TYPE_IQ2_XXS:
  1739. {
  1740. nth0 = 4;
  1741. nth1 = 16;
  1742. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ2_XXS_F32].pipeline;
  1743. } break;
  1744. case GGML_TYPE_IQ2_XS:
  1745. {
  1746. nth0 = 4;
  1747. nth1 = 16;
  1748. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ2_XS_F32].pipeline;
  1749. } break;
  1750. case GGML_TYPE_IQ3_XXS:
  1751. {
  1752. nth0 = 4;
  1753. nth1 = 16;
  1754. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ3_XXS_F32].pipeline;
  1755. } break;
  1756. case GGML_TYPE_IQ3_S:
  1757. {
  1758. nth0 = 4;
  1759. nth1 = 16;
  1760. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ3_S_F32].pipeline;
  1761. } break;
  1762. case GGML_TYPE_IQ2_S:
  1763. {
  1764. nth0 = 4;
  1765. nth1 = 16;
  1766. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ2_S_F32].pipeline;
  1767. } break;
  1768. case GGML_TYPE_IQ1_S:
  1769. {
  1770. nth0 = 4;
  1771. nth1 = 16;
  1772. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ1_S_F32].pipeline;
  1773. } break;
  1774. case GGML_TYPE_IQ1_M:
  1775. {
  1776. nth0 = 4;
  1777. nth1 = 16;
  1778. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ1_M_F32].pipeline;
  1779. } break;
  1780. case GGML_TYPE_IQ4_NL:
  1781. {
  1782. nth0 = 4;
  1783. nth1 = 16;
  1784. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ4_NL_F32].pipeline;
  1785. } break;
  1786. case GGML_TYPE_IQ4_XS:
  1787. {
  1788. nth0 = 4;
  1789. nth1 = 16;
  1790. #if QK_K == 64
  1791. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ4_NL_F32].pipeline;
  1792. #else
  1793. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ4_XS_F32].pipeline;
  1794. #endif
  1795. } break;
  1796. default:
  1797. {
  1798. GGML_METAL_LOG_ERROR("Asserting on type %d\n", (int)src2t);
  1799. GGML_ASSERT(false && "not implemented");
  1800. }
  1801. };
  1802. if (ggml_is_quantized(src0t)) {
  1803. GGML_ASSERT(ne00 >= nth0*nth1);
  1804. }
  1805. [encoder setComputePipelineState:pipeline];
  1806. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1807. [encoder setBuffer:id_src1 offset:offs_src1 atIndex:1];
  1808. [encoder setBuffer:id_dst offset:offs_dst atIndex:2];
  1809. [encoder setBuffer:id_src2 offset:offs_src2 atIndex:3];
  1810. [encoder setBytes:&ne20 length:sizeof(ne20) atIndex:4];
  1811. [encoder setBytes:&ne21 length:sizeof(ne21) atIndex:5];
  1812. [encoder setBytes:&nb21 length:sizeof(nb21) atIndex:6];
  1813. [encoder setBytes:&ne00 length:sizeof(ne00) atIndex:7];
  1814. [encoder setBytes:&ne01 length:sizeof(ne01) atIndex:8];
  1815. [encoder setBytes:&ne02 length:sizeof(ne02) atIndex:9];
  1816. [encoder setBytes:&nb00 length:sizeof(nb00) atIndex:10];
  1817. [encoder setBytes:&nb01 length:sizeof(nb01) atIndex:11];
  1818. [encoder setBytes:&nb02 length:sizeof(nb02) atIndex:12];
  1819. [encoder setBytes:&ne10 length:sizeof(ne10) atIndex:13];
  1820. [encoder setBytes:&ne11 length:sizeof(ne11) atIndex:14];
  1821. [encoder setBytes:&ne12 length:sizeof(ne12) atIndex:15];
  1822. [encoder setBytes:&ne13 length:sizeof(ne13) atIndex:16];
  1823. [encoder setBytes:&nb10 length:sizeof(nb10) atIndex:17];
  1824. [encoder setBytes:&nb11 length:sizeof(nb11) atIndex:18];
  1825. [encoder setBytes:&nb12 length:sizeof(nb12) atIndex:19];
  1826. [encoder setBytes:&ne0 length:sizeof(ne0) atIndex:20];
  1827. [encoder setBytes:&ne1 length:sizeof(ne1) atIndex:21];
  1828. [encoder setBytes:&nb1 length:sizeof(nb1) atIndex:22];
  1829. const int64_t _ne1 = 1;
  1830. const int tgz = dst_rows;
  1831. if (src0t == GGML_TYPE_Q4_0 || src0t == GGML_TYPE_Q4_1 || src0t == GGML_TYPE_Q5_0 ||
  1832. src0t == GGML_TYPE_Q5_1 || src0t == GGML_TYPE_Q8_0 || src0t == GGML_TYPE_Q2_K ||
  1833. src0t == GGML_TYPE_IQ1_S || src0t == GGML_TYPE_IQ1_M || src0t == GGML_TYPE_IQ2_S) {
  1834. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + 7)/8, _ne1, tgz) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  1835. }
  1836. else if (src0t == GGML_TYPE_IQ2_XXS || src0t == GGML_TYPE_IQ2_XS) {
  1837. const int mem_size = src0t == GGML_TYPE_IQ2_XXS ? 256*8+128 : 512*8+128;
  1838. [encoder setThreadgroupMemoryLength:mem_size atIndex:0];
  1839. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + 7)/8, _ne1, tgz) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  1840. }
  1841. else if (src0t == GGML_TYPE_IQ3_XXS || src0t == GGML_TYPE_IQ3_S) {
  1842. const int mem_size = src0t == GGML_TYPE_IQ3_XXS ? 256*4+128 : 512*4;
  1843. [encoder setThreadgroupMemoryLength:mem_size atIndex:0];
  1844. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + 7)/8, _ne1, tgz) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  1845. }
  1846. else if (src0t == GGML_TYPE_IQ4_NL || src0t == GGML_TYPE_IQ4_XS) {
  1847. const int mem_size = 32*sizeof(float);
  1848. [encoder setThreadgroupMemoryLength:mem_size atIndex:0];
  1849. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + 3)/4, _ne1, tgz) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  1850. }
  1851. else if (src0t == GGML_TYPE_Q4_K) {
  1852. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + 3)/4, _ne1, tgz) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  1853. }
  1854. else if (src0t == GGML_TYPE_Q3_K) {
  1855. #ifdef GGML_QKK_64
  1856. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + 1)/2, _ne1, tgz) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  1857. #else
  1858. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + 3)/4, _ne1, tgz) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  1859. #endif
  1860. }
  1861. else if (src0t == GGML_TYPE_Q5_K) {
  1862. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + 3)/4, _ne1, tgz) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  1863. }
  1864. else if (src0t == GGML_TYPE_Q6_K) {
  1865. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + 1)/2, _ne1, tgz) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  1866. } else {
  1867. const int64_t ny = (_ne1 + nrows - 1)/nrows; // = _ne1
  1868. [encoder dispatchThreadgroups:MTLSizeMake(ne01, ny, tgz) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  1869. }
  1870. }
  1871. } break;
  1872. case GGML_OP_GET_ROWS:
  1873. {
  1874. id<MTLComputePipelineState> pipeline = nil;
  1875. switch (src0->type) {
  1876. case GGML_TYPE_F32: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_F32 ].pipeline; break;
  1877. case GGML_TYPE_F16: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_F16 ].pipeline; break;
  1878. case GGML_TYPE_Q4_0: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_Q4_0 ].pipeline; break;
  1879. case GGML_TYPE_Q4_1: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_Q4_1 ].pipeline; break;
  1880. case GGML_TYPE_Q5_0: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_Q5_0 ].pipeline; break;
  1881. case GGML_TYPE_Q5_1: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_Q5_1 ].pipeline; break;
  1882. case GGML_TYPE_Q8_0: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_Q8_0 ].pipeline; break;
  1883. case GGML_TYPE_Q2_K: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_Q2_K ].pipeline; break;
  1884. case GGML_TYPE_Q3_K: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_Q3_K ].pipeline; break;
  1885. case GGML_TYPE_Q4_K: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_Q4_K ].pipeline; break;
  1886. case GGML_TYPE_Q5_K: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_Q5_K ].pipeline; break;
  1887. case GGML_TYPE_Q6_K: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_Q6_K ].pipeline; break;
  1888. case GGML_TYPE_IQ2_XXS: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ2_XXS].pipeline; break;
  1889. case GGML_TYPE_IQ2_XS: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ2_XS ].pipeline; break;
  1890. case GGML_TYPE_IQ3_XXS: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ3_XXS].pipeline; break;
  1891. case GGML_TYPE_IQ3_S: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ3_S ].pipeline; break;
  1892. case GGML_TYPE_IQ2_S: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ2_S ].pipeline; break;
  1893. case GGML_TYPE_IQ1_S: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ1_S ].pipeline; break;
  1894. case GGML_TYPE_IQ1_M: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ1_M ].pipeline; break;
  1895. case GGML_TYPE_IQ4_NL: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ4_NL ].pipeline; break;
  1896. case GGML_TYPE_IQ4_XS: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ4_XS ].pipeline; break;
  1897. case GGML_TYPE_I32: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_I32 ].pipeline; break;
  1898. default: GGML_ASSERT(false && "not implemented");
  1899. }
  1900. [encoder setComputePipelineState:pipeline];
  1901. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1902. [encoder setBuffer:id_src1 offset:offs_src1 atIndex:1];
  1903. [encoder setBuffer:id_dst offset:offs_dst atIndex:2];
  1904. [encoder setBytes:&ne00 length:sizeof( int64_t) atIndex:3];
  1905. [encoder setBytes:&nb01 length:sizeof(uint64_t) atIndex:4];
  1906. [encoder setBytes:&nb02 length:sizeof(uint64_t) atIndex:5];
  1907. [encoder setBytes:&ne10 length:sizeof( int64_t) atIndex:6];
  1908. [encoder setBytes:&nb10 length:sizeof( int64_t) atIndex:7];
  1909. [encoder setBytes:&nb11 length:sizeof( int64_t) atIndex:8];
  1910. [encoder setBytes:&nb1 length:sizeof(uint64_t) atIndex:9];
  1911. [encoder setBytes:&nb2 length:sizeof(uint64_t) atIndex:10];
  1912. [encoder dispatchThreadgroups:MTLSizeMake(ne10, ne11, 1) threadsPerThreadgroup:MTLSizeMake(32, 1, 1)];
  1913. } break;
  1914. case GGML_OP_RMS_NORM:
  1915. {
  1916. GGML_ASSERT(ne00 % 4 == 0);
  1917. float eps;
  1918. memcpy(&eps, dst->op_params, sizeof(float));
  1919. int nth = 32; // SIMD width
  1920. while (nth < ne00/4 && nth < 1024) {
  1921. nth *= 2;
  1922. }
  1923. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_RMS_NORM].pipeline;
  1924. [encoder setComputePipelineState:pipeline];
  1925. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1926. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  1927. [encoder setBytes:&ne00 length:sizeof( int64_t) atIndex:2];
  1928. [encoder setBytes:&nb01 length:sizeof(uint64_t) atIndex:3];
  1929. [encoder setBytes:&eps length:sizeof( float) atIndex:4];
  1930. [encoder setThreadgroupMemoryLength:32*sizeof(float) atIndex:0];
  1931. const int64_t nrows = ggml_nrows(src0);
  1932. [encoder dispatchThreadgroups:MTLSizeMake(nrows, 1, 1) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  1933. } break;
  1934. case GGML_OP_GROUP_NORM:
  1935. {
  1936. GGML_ASSERT(ne00 % 4 == 0);
  1937. //float eps;
  1938. //memcpy(&eps, dst->op_params, sizeof(float));
  1939. const float eps = 1e-6f; // TODO: temporarily hardcoded
  1940. const int32_t n_groups = ((int32_t *) dst->op_params)[0];
  1941. int nth = 32; // SIMD width
  1942. //while (nth < ne00/4 && nth < 1024) {
  1943. // nth *= 2;
  1944. //}
  1945. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GROUP_NORM].pipeline;
  1946. [encoder setComputePipelineState:pipeline];
  1947. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1948. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  1949. [encoder setBytes:&ne00 length:sizeof( int64_t) atIndex:2];
  1950. [encoder setBytes:&ne01 length:sizeof( int64_t) atIndex:3];
  1951. [encoder setBytes:&ne02 length:sizeof( int64_t) atIndex:4];
  1952. [encoder setBytes:&nb00 length:sizeof(uint64_t) atIndex:5];
  1953. [encoder setBytes:&nb01 length:sizeof(uint64_t) atIndex:6];
  1954. [encoder setBytes:&nb02 length:sizeof(uint64_t) atIndex:7];
  1955. [encoder setBytes:&n_groups length:sizeof( int32_t) atIndex:8];
  1956. [encoder setBytes:&eps length:sizeof( float) atIndex:9];
  1957. [encoder setThreadgroupMemoryLength:32*sizeof(float) atIndex:0];
  1958. [encoder dispatchThreadgroups:MTLSizeMake(n_groups, 1, 1) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  1959. } break;
  1960. case GGML_OP_NORM:
  1961. {
  1962. float eps;
  1963. memcpy(&eps, dst->op_params, sizeof(float));
  1964. const int nth = MIN(256, ne00);
  1965. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_NORM].pipeline;
  1966. [encoder setComputePipelineState:pipeline];
  1967. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1968. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  1969. [encoder setBytes:&ne00 length:sizeof( int64_t) atIndex:2];
  1970. [encoder setBytes:&nb01 length:sizeof(uint64_t) atIndex:3];
  1971. [encoder setBytes:&eps length:sizeof( float) atIndex:4];
  1972. [encoder setThreadgroupMemoryLength:GGML_PAD(nth*sizeof(float), 16) atIndex:0];
  1973. const int64_t nrows = ggml_nrows(src0);
  1974. [encoder dispatchThreadgroups:MTLSizeMake(nrows, 1, 1) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  1975. } break;
  1976. case GGML_OP_ALIBI:
  1977. {
  1978. GGML_ASSERT((src0t == GGML_TYPE_F32));
  1979. const int nth = MIN(1024, ne00);
  1980. //const int n_past = ((int32_t *) dst->op_params)[0];
  1981. const int n_head = ((int32_t *) dst->op_params)[1];
  1982. float max_bias;
  1983. memcpy(&max_bias, (int32_t *) dst->op_params + 2, sizeof(float));
  1984. const int n_heads_log2_floor = 1 << (int) floor(log2(n_head));
  1985. const float m0 = powf(2.0f, -(max_bias) / n_heads_log2_floor);
  1986. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_heads_log2_floor);
  1987. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_ALIBI_F32].pipeline;
  1988. [encoder setComputePipelineState:pipeline];
  1989. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1990. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  1991. [encoder setBytes:&ne00 length:sizeof( int64_t) atIndex:2];
  1992. [encoder setBytes:&ne01 length:sizeof( int64_t) atIndex:3];
  1993. [encoder setBytes:&ne02 length:sizeof( int64_t) atIndex:4];
  1994. [encoder setBytes:&ne03 length:sizeof( int64_t) atIndex:5];
  1995. [encoder setBytes:&nb00 length:sizeof(uint64_t) atIndex:6];
  1996. [encoder setBytes:&nb01 length:sizeof(uint64_t) atIndex:7];
  1997. [encoder setBytes:&nb02 length:sizeof(uint64_t) atIndex:8];
  1998. [encoder setBytes:&nb03 length:sizeof(uint64_t) atIndex:9];
  1999. [encoder setBytes:&ne0 length:sizeof( int64_t) atIndex:10];
  2000. [encoder setBytes:&ne1 length:sizeof( int64_t) atIndex:11];
  2001. [encoder setBytes:&ne2 length:sizeof( int64_t) atIndex:12];
  2002. [encoder setBytes:&ne3 length:sizeof( int64_t) atIndex:13];
  2003. [encoder setBytes:&nb0 length:sizeof(uint64_t) atIndex:14];
  2004. [encoder setBytes:&nb1 length:sizeof(uint64_t) atIndex:15];
  2005. [encoder setBytes:&nb2 length:sizeof(uint64_t) atIndex:16];
  2006. [encoder setBytes:&nb3 length:sizeof(uint64_t) atIndex:17];
  2007. [encoder setBytes:&m0 length:sizeof( float) atIndex:18];
  2008. [encoder setBytes:&m1 length:sizeof( float) atIndex:19];
  2009. [encoder setBytes:&n_heads_log2_floor length:sizeof(int) atIndex:20];
  2010. [encoder dispatchThreadgroups:MTLSizeMake(ne01, ne02, ne03) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  2011. } break;
  2012. case GGML_OP_ROPE:
  2013. {
  2014. GGML_ASSERT(ne10 == ne02);
  2015. const int nth = MIN(1024, ne00);
  2016. const int n_past = ((int32_t *) dst->op_params)[0];
  2017. const int n_dims = ((int32_t *) dst->op_params)[1];
  2018. const int mode = ((int32_t *) dst->op_params)[2];
  2019. // skip 3, n_ctx, used in GLM RoPE, unimplemented in metal
  2020. const int n_orig_ctx = ((int32_t *) dst->op_params)[4];
  2021. float freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow;
  2022. memcpy(&freq_base, (int32_t *) dst->op_params + 5, sizeof(float));
  2023. memcpy(&freq_scale, (int32_t *) dst->op_params + 6, sizeof(float));
  2024. memcpy(&ext_factor, (int32_t *) dst->op_params + 7, sizeof(float));
  2025. memcpy(&attn_factor, (int32_t *) dst->op_params + 8, sizeof(float));
  2026. memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float));
  2027. memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float));
  2028. id<MTLComputePipelineState> pipeline = nil;
  2029. switch (src0->type) {
  2030. case GGML_TYPE_F32: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_ROPE_F32].pipeline; break;
  2031. case GGML_TYPE_F16: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_ROPE_F16].pipeline; break;
  2032. default: GGML_ASSERT(false);
  2033. };
  2034. [encoder setComputePipelineState:pipeline];
  2035. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  2036. [encoder setBuffer:id_src1 offset:offs_src1 atIndex:1];
  2037. [encoder setBuffer:id_dst offset:offs_dst atIndex:2];
  2038. [encoder setBytes:&ne00 length:sizeof( int64_t) atIndex:3];
  2039. [encoder setBytes:&ne01 length:sizeof( int64_t) atIndex:4];
  2040. [encoder setBytes:&ne02 length:sizeof( int64_t) atIndex:5];
  2041. [encoder setBytes:&ne03 length:sizeof( int64_t) atIndex:6];
  2042. [encoder setBytes:&nb00 length:sizeof(uint64_t) atIndex:7];
  2043. [encoder setBytes:&nb01 length:sizeof(uint64_t) atIndex:8];
  2044. [encoder setBytes:&nb02 length:sizeof(uint64_t) atIndex:9];
  2045. [encoder setBytes:&nb03 length:sizeof(uint64_t) atIndex:10];
  2046. [encoder setBytes:&ne0 length:sizeof( int64_t) atIndex:11];
  2047. [encoder setBytes:&ne1 length:sizeof( int64_t) atIndex:12];
  2048. [encoder setBytes:&ne2 length:sizeof( int64_t) atIndex:13];
  2049. [encoder setBytes:&ne3 length:sizeof( int64_t) atIndex:14];
  2050. [encoder setBytes:&nb0 length:sizeof(uint64_t) atIndex:15];
  2051. [encoder setBytes:&nb1 length:sizeof(uint64_t) atIndex:16];
  2052. [encoder setBytes:&nb2 length:sizeof(uint64_t) atIndex:17];
  2053. [encoder setBytes:&nb3 length:sizeof(uint64_t) atIndex:18];
  2054. [encoder setBytes:&n_past length:sizeof( int) atIndex:19];
  2055. [encoder setBytes:&n_dims length:sizeof( int) atIndex:20];
  2056. [encoder setBytes:&mode length:sizeof( int) atIndex:21];
  2057. [encoder setBytes:&n_orig_ctx length:sizeof( int) atIndex:22];
  2058. [encoder setBytes:&freq_base length:sizeof( float) atIndex:23];
  2059. [encoder setBytes:&freq_scale length:sizeof( float) atIndex:24];
  2060. [encoder setBytes:&ext_factor length:sizeof( float) atIndex:25];
  2061. [encoder setBytes:&attn_factor length:sizeof( float) atIndex:26];
  2062. [encoder setBytes:&beta_fast length:sizeof( float) atIndex:27];
  2063. [encoder setBytes:&beta_slow length:sizeof( float) atIndex:28];
  2064. [encoder dispatchThreadgroups:MTLSizeMake(ne01, ne02, ne03) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  2065. } break;
  2066. case GGML_OP_IM2COL:
  2067. {
  2068. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  2069. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  2070. GGML_ASSERT( dst->type == GGML_TYPE_F16 || dst->type == GGML_TYPE_F32);
  2071. const int32_t s0 = ((const int32_t *)(dst->op_params))[0];
  2072. const int32_t s1 = ((const int32_t *)(dst->op_params))[1];
  2073. const int32_t p0 = ((const int32_t *)(dst->op_params))[2];
  2074. const int32_t p1 = ((const int32_t *)(dst->op_params))[3];
  2075. const int32_t d0 = ((const int32_t *)(dst->op_params))[4];
  2076. const int32_t d1 = ((const int32_t *)(dst->op_params))[5];
  2077. const bool is_2D = ((const int32_t *)(dst->op_params))[6] == 1;
  2078. const int32_t N = src1->ne[is_2D ? 3 : 2];
  2079. const int32_t IC = src1->ne[is_2D ? 2 : 1];
  2080. const int32_t IH = is_2D ? src1->ne[1] : 1;
  2081. const int32_t IW = src1->ne[0];
  2082. const int32_t KH = is_2D ? src0->ne[1] : 1;
  2083. const int32_t KW = src0->ne[0];
  2084. const int32_t OH = is_2D ? dst->ne[2] : 1;
  2085. const int32_t OW = dst->ne[1];
  2086. const int32_t CHW = IC * KH * KW;
  2087. const int32_t ofs0 = src1->nb[is_2D ? 3 : 2] / 4;
  2088. const int32_t ofs1 = src1->nb[is_2D ? 2 : 1] / 4;
  2089. id<MTLComputePipelineState> pipeline = nil;
  2090. switch (dst->type) {
  2091. case GGML_TYPE_F32: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_IM2COL_F32].pipeline; break;
  2092. case GGML_TYPE_F16: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_IM2COL_F16].pipeline; break;
  2093. default: GGML_ASSERT(false);
  2094. };
  2095. [encoder setComputePipelineState:pipeline];
  2096. [encoder setBuffer:id_src1 offset:offs_src1 atIndex:0];
  2097. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  2098. [encoder setBytes:&ofs0 length:sizeof( int32_t) atIndex:2];
  2099. [encoder setBytes:&ofs1 length:sizeof( int32_t) atIndex:3];
  2100. [encoder setBytes:&IW length:sizeof( int32_t) atIndex:4];
  2101. [encoder setBytes:&IH length:sizeof( int32_t) atIndex:5];
  2102. [encoder setBytes:&CHW length:sizeof( int32_t) atIndex:6];
  2103. [encoder setBytes:&s0 length:sizeof( int32_t) atIndex:7];
  2104. [encoder setBytes:&s1 length:sizeof( int32_t) atIndex:8];
  2105. [encoder setBytes:&p0 length:sizeof( int32_t) atIndex:9];
  2106. [encoder setBytes:&p1 length:sizeof( int32_t) atIndex:10];
  2107. [encoder setBytes:&d0 length:sizeof( int32_t) atIndex:11];
  2108. [encoder setBytes:&d1 length:sizeof( int32_t) atIndex:12];
  2109. [encoder dispatchThreadgroups:MTLSizeMake(IC, OH, OW) threadsPerThreadgroup:MTLSizeMake(N, KH, KW)];
  2110. } break;
  2111. case GGML_OP_UPSCALE:
  2112. {
  2113. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  2114. const int sf = dst->op_params[0];
  2115. const id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_UPSCALE_F32].pipeline;
  2116. [encoder setComputePipelineState:pipeline];
  2117. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  2118. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  2119. [encoder setBytes:&ne00 length:sizeof(ne00) atIndex:2];
  2120. [encoder setBytes:&ne01 length:sizeof(ne01) atIndex:3];
  2121. [encoder setBytes:&ne02 length:sizeof(ne02) atIndex:4];
  2122. [encoder setBytes:&ne03 length:sizeof(ne03) atIndex:5];
  2123. [encoder setBytes:&nb00 length:sizeof(nb00) atIndex:6];
  2124. [encoder setBytes:&nb01 length:sizeof(nb01) atIndex:7];
  2125. [encoder setBytes:&nb02 length:sizeof(nb02) atIndex:8];
  2126. [encoder setBytes:&nb03 length:sizeof(nb03) atIndex:9];
  2127. [encoder setBytes:&ne0 length:sizeof(ne0) atIndex:10];
  2128. [encoder setBytes:&ne1 length:sizeof(ne1) atIndex:11];
  2129. [encoder setBytes:&ne2 length:sizeof(ne2) atIndex:12];
  2130. [encoder setBytes:&ne3 length:sizeof(ne3) atIndex:13];
  2131. [encoder setBytes:&nb0 length:sizeof(nb0) atIndex:14];
  2132. [encoder setBytes:&nb1 length:sizeof(nb1) atIndex:15];
  2133. [encoder setBytes:&nb2 length:sizeof(nb2) atIndex:16];
  2134. [encoder setBytes:&nb3 length:sizeof(nb3) atIndex:17];
  2135. [encoder setBytes:&sf length:sizeof(sf) atIndex:18];
  2136. const int nth = MIN((int) pipeline.maxTotalThreadsPerThreadgroup, ne0);
  2137. [encoder dispatchThreadgroups:MTLSizeMake(ne1, ne2, ne3) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  2138. } break;
  2139. case GGML_OP_PAD:
  2140. {
  2141. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  2142. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_PAD_F32].pipeline;
  2143. [encoder setComputePipelineState:pipeline];
  2144. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  2145. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  2146. [encoder setBytes:&ne00 length:sizeof(ne00) atIndex:2];
  2147. [encoder setBytes:&ne01 length:sizeof(ne01) atIndex:3];
  2148. [encoder setBytes:&ne02 length:sizeof(ne02) atIndex:4];
  2149. [encoder setBytes:&ne03 length:sizeof(ne03) atIndex:5];
  2150. [encoder setBytes:&nb00 length:sizeof(nb00) atIndex:6];
  2151. [encoder setBytes:&nb01 length:sizeof(nb01) atIndex:7];
  2152. [encoder setBytes:&nb02 length:sizeof(nb02) atIndex:8];
  2153. [encoder setBytes:&nb03 length:sizeof(nb03) atIndex:9];
  2154. [encoder setBytes:&ne0 length:sizeof(ne0) atIndex:10];
  2155. [encoder setBytes:&ne1 length:sizeof(ne1) atIndex:11];
  2156. [encoder setBytes:&ne2 length:sizeof(ne2) atIndex:12];
  2157. [encoder setBytes:&ne3 length:sizeof(ne3) atIndex:13];
  2158. [encoder setBytes:&nb0 length:sizeof(nb0) atIndex:14];
  2159. [encoder setBytes:&nb1 length:sizeof(nb1) atIndex:15];
  2160. [encoder setBytes:&nb2 length:sizeof(nb2) atIndex:16];
  2161. [encoder setBytes:&nb3 length:sizeof(nb3) atIndex:17];
  2162. const int nth = MIN(1024, ne0);
  2163. [encoder dispatchThreadgroups:MTLSizeMake(ne1, ne2, ne3) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  2164. } break;
  2165. case GGML_OP_ARANGE:
  2166. {
  2167. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  2168. float start;
  2169. float step;
  2170. memcpy(&start, ((int32_t *) dst->op_params) + 0, sizeof(float));
  2171. memcpy(&step, ((int32_t *) dst->op_params) + 2, sizeof(float));
  2172. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_ARANGE_F32].pipeline;
  2173. [encoder setComputePipelineState:pipeline];
  2174. [encoder setBuffer:id_dst offset:offs_dst atIndex:0];
  2175. [encoder setBytes:&ne0 length:sizeof(ne0) atIndex:1];
  2176. [encoder setBytes:&start length:sizeof(start) atIndex:2];
  2177. [encoder setBytes:&step length:sizeof(step) atIndex:3];
  2178. const int nth = MIN(1024, ne0);
  2179. [encoder dispatchThreadgroups:MTLSizeMake(1, 1, 1) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  2180. } break;
  2181. case GGML_OP_TIMESTEP_EMBEDDING:
  2182. {
  2183. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  2184. const int dim = dst->op_params[0];
  2185. const int max_period = dst->op_params[1];
  2186. const int half = dim / 2;
  2187. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_TIMESTEP_EMBEDDING_F32].pipeline;
  2188. [encoder setComputePipelineState:pipeline];
  2189. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  2190. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  2191. [encoder setBytes:&nb1 length:sizeof(nb1) atIndex:2];
  2192. [encoder setBytes:&dim length:sizeof(dim) atIndex:3];
  2193. [encoder setBytes:&max_period length:sizeof(max_period) atIndex:4];
  2194. const int nth = MIN(1024, half);
  2195. [encoder dispatchThreadgroups:MTLSizeMake(ne00, 1, 1) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  2196. } break;
  2197. case GGML_OP_ARGSORT:
  2198. {
  2199. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  2200. GGML_ASSERT( dst->type == GGML_TYPE_I32);
  2201. const int nrows = ggml_nrows(src0);
  2202. enum ggml_sort_order order = (enum ggml_sort_order) dst->op_params[0];
  2203. // bitonic sort requires the number of elements to be power of 2
  2204. int64_t ne00_padded = 1;
  2205. while (ne00_padded < ne00) {
  2206. ne00_padded *= 2;
  2207. }
  2208. // Metal kernels require the buffer size to be multiple of 16 bytes
  2209. // https://developer.apple.com/documentation/metal/mtlcomputecommandencoder/1443142-setthreadgroupmemorylength
  2210. const int mem_size = GGML_PAD(ne00_padded*sizeof(int32_t), 16);
  2211. id<MTLComputePipelineState> pipeline = nil;
  2212. switch (order) {
  2213. case GGML_SORT_ORDER_ASC: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_ARGSORT_F32_I32_ASC].pipeline; break;
  2214. case GGML_SORT_ORDER_DESC: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_ARGSORT_F32_I32_DESC].pipeline; break;
  2215. default: GGML_ASSERT(false);
  2216. };
  2217. [encoder setComputePipelineState:pipeline];
  2218. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  2219. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  2220. [encoder setBytes:&ne00 length:sizeof( int64_t) atIndex:2];
  2221. [encoder setBytes:&ne00_padded length:sizeof( int64_t) atIndex:3];
  2222. [encoder setThreadgroupMemoryLength:mem_size atIndex:0];
  2223. [encoder dispatchThreadgroups:MTLSizeMake(1, nrows, 1) threadsPerThreadgroup:MTLSizeMake(ne00_padded, 1, 1)];
  2224. } break;
  2225. case GGML_OP_LEAKY_RELU:
  2226. {
  2227. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  2228. float slope;
  2229. memcpy(&slope, dst->op_params, sizeof(float));
  2230. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_LEAKY_RELU_F32].pipeline;
  2231. [encoder setComputePipelineState:pipeline];
  2232. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  2233. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  2234. [encoder setBytes:&slope length:sizeof(slope) atIndex:2];
  2235. const int64_t n = ggml_nelements(dst);
  2236. [encoder dispatchThreadgroups:MTLSizeMake(n, 1, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  2237. } break;
  2238. case GGML_OP_FLASH_ATTN_EXT:
  2239. {
  2240. GGML_ASSERT(ne00 % 4 == 0);
  2241. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  2242. struct ggml_tensor * src3 = gf->nodes[i]->src[3];
  2243. GGML_ASSERT(ggml_are_same_shape(src1, src2));
  2244. GGML_ASSERT(src3);
  2245. size_t offs_src3 = 0;
  2246. id<MTLBuffer> id_src3 = src3 ? ggml_metal_get_buffer(src3, &offs_src3) : nil;
  2247. GGML_ASSERT(!src3 || src3->type == GGML_TYPE_F16);
  2248. GGML_ASSERT(!src3 || src3->ne[1] >= GGML_PAD(src0->ne[1], 8) &&
  2249. "the Flash-Attention Metal kernel requires the mask to be padded to 8 and at least n_queries big");
  2250. const int64_t ne30 = src3 ? src3->ne[0] : 0; GGML_UNUSED(ne30);
  2251. const int64_t ne31 = src3 ? src3->ne[1] : 0;
  2252. const int64_t ne32 = src3 ? src3->ne[2] : 0; GGML_UNUSED(ne32);
  2253. const int64_t ne33 = src3 ? src3->ne[3] : 0; GGML_UNUSED(ne33);
  2254. const uint64_t nb30 = src3 ? src3->nb[0] : 0; GGML_UNUSED(nb30);
  2255. const uint64_t nb31 = src3 ? src3->nb[1] : 0;
  2256. const uint64_t nb32 = src3 ? src3->nb[2] : 0; GGML_UNUSED(nb32);
  2257. const uint64_t nb33 = src3 ? src3->nb[3] : 0; GGML_UNUSED(nb33);
  2258. const enum ggml_type src2t = src2 ? src2->type : GGML_TYPE_COUNT; GGML_UNUSED(src2t);
  2259. float scale;
  2260. memcpy(&scale, dst->op_params, sizeof(float));
  2261. id<MTLComputePipelineState> pipeline = nil;
  2262. bool use_vec_kernel = false;
  2263. if (ne01 >= 4 || (ne00%128 != 0)) {
  2264. switch (ne00) {
  2265. case 64: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_F16_H64 ].pipeline; break;
  2266. case 80: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_F16_H80 ].pipeline; break;
  2267. case 96: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_F16_H96 ].pipeline; break;
  2268. case 112: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_F16_H112].pipeline; break;
  2269. case 128: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_F16_H128].pipeline; break;
  2270. case 256: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_F16_H256].pipeline; break;
  2271. default:
  2272. {
  2273. GGML_METAL_LOG_ERROR("unsupported size: %lld\n", ne00);
  2274. GGML_METAL_LOG_ERROR("add template specialization for this size\n");
  2275. GGML_ASSERT(false && "add template specialization for this size");
  2276. }
  2277. }
  2278. } else {
  2279. use_vec_kernel = true;
  2280. switch (ne00) {
  2281. case 128: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_VEC_F16_H128].pipeline; break;
  2282. case 256: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_VEC_F16_H256].pipeline; break;
  2283. default:
  2284. {
  2285. GGML_METAL_LOG_ERROR("unsupported size: %lld\n", ne00);
  2286. GGML_METAL_LOG_ERROR("add template specialization for this size\n");
  2287. GGML_ASSERT(false && "add template specialization for this size");
  2288. }
  2289. }
  2290. }
  2291. [encoder setComputePipelineState:pipeline];
  2292. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  2293. [encoder setBuffer:id_src1 offset:offs_src1 atIndex:1];
  2294. [encoder setBuffer:id_src2 offset:offs_src2 atIndex:2];
  2295. [encoder setBuffer:id_src3 offset:offs_src3 atIndex:3];
  2296. [encoder setBuffer:id_dst offset:offs_dst atIndex:4];
  2297. [encoder setBytes:&ne00 length:sizeof( int64_t) atIndex:5];
  2298. [encoder setBytes:&ne01 length:sizeof( int64_t) atIndex:6];
  2299. [encoder setBytes:&ne02 length:sizeof( int64_t) atIndex:7];
  2300. [encoder setBytes:&ne03 length:sizeof( int64_t) atIndex:8];
  2301. [encoder setBytes:&nb00 length:sizeof(uint64_t) atIndex:9];
  2302. [encoder setBytes:&nb01 length:sizeof(uint64_t) atIndex:10];
  2303. [encoder setBytes:&nb02 length:sizeof(uint64_t) atIndex:11];
  2304. [encoder setBytes:&nb03 length:sizeof(uint64_t) atIndex:12];
  2305. [encoder setBytes:&ne10 length:sizeof( int64_t) atIndex:13];
  2306. [encoder setBytes:&ne11 length:sizeof( int64_t) atIndex:14];
  2307. [encoder setBytes:&ne12 length:sizeof( int64_t) atIndex:15];
  2308. [encoder setBytes:&ne13 length:sizeof( int64_t) atIndex:16];
  2309. [encoder setBytes:&nb10 length:sizeof(uint64_t) atIndex:17];
  2310. [encoder setBytes:&nb11 length:sizeof(uint64_t) atIndex:18];
  2311. [encoder setBytes:&nb12 length:sizeof(uint64_t) atIndex:19];
  2312. [encoder setBytes:&nb13 length:sizeof(uint64_t) atIndex:20];
  2313. [encoder setBytes:&ne31 length:sizeof( int64_t) atIndex:21];
  2314. [encoder setBytes:&nb31 length:sizeof(uint64_t) atIndex:22];
  2315. [encoder setBytes:&ne0 length:sizeof( int64_t) atIndex:23];
  2316. [encoder setBytes:&ne1 length:sizeof( int64_t) atIndex:24];
  2317. [encoder setBytes:&ne2 length:sizeof( int64_t) atIndex:25];
  2318. [encoder setBytes:&ne3 length:sizeof( int64_t) atIndex:26];
  2319. [encoder setBytes:&scale length:sizeof( float) atIndex:27];
  2320. if (!use_vec_kernel) {
  2321. // half8x8 kernel
  2322. const int64_t nqptg = 8; // queries per threadgroup !! sync with kernel template arguments !!
  2323. const int64_t ncpsg = 32; // cache values per simdgroup !! sync with kernel template arguments !!
  2324. GGML_ASSERT(nqptg <= 32);
  2325. GGML_ASSERT(nqptg % 8 == 0);
  2326. GGML_ASSERT(ncpsg % 32 == 0);
  2327. int64_t nsgmax = 2;
  2328. while (true) {
  2329. const size_t smem = nqptg*(ne00 + 2*nsgmax*(ncpsg + nqptg))*(sizeof(float)/2);
  2330. if (smem > ctx->device.maxThreadgroupMemoryLength) {
  2331. break;
  2332. }
  2333. nsgmax *= 2;
  2334. }
  2335. nsgmax /= 2;
  2336. // simdgroups per threadgroup (a.k.a. warps)
  2337. const int64_t nsg = ne01 <= nqptg ? MAX(4, MIN(nsgmax, MIN(ne11/ncpsg, (int64_t) pipeline.maxTotalThreadsPerThreadgroup/32))) : 4;
  2338. const size_t smem = nqptg*(ne00 + 2*nsg*(ncpsg + nqptg))*(sizeof(float)/2);
  2339. //printf("smem: %zu, max: %zu\n", smem, ctx->device.maxThreadgroupMemoryLength);
  2340. GGML_ASSERT(smem <= ctx->device.maxThreadgroupMemoryLength);
  2341. [encoder setThreadgroupMemoryLength:GGML_PAD(smem, 16) atIndex:0];
  2342. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + nqptg - 1)/nqptg, ne02, ne03) threadsPerThreadgroup:MTLSizeMake(32, nsg, 1)];
  2343. } else {
  2344. // half1x4 kernel
  2345. const int64_t nqptg = 1; // queries per threadgroup !! sync with kernel template arguments !!
  2346. const int64_t ncpsg = 32; // cache values per simdgroup !! sync with kernel template arguments !!
  2347. GGML_ASSERT(nqptg <= 32);
  2348. GGML_ASSERT(nqptg % 1 == 0);
  2349. GGML_ASSERT(ncpsg % 32 == 0);
  2350. // simdgroups per threadgroup (a.k.a. warps)
  2351. const int64_t nsgt = MAX(2, MIN(ne11/ncpsg, (int64_t) pipeline.maxTotalThreadsPerThreadgroup/32));
  2352. int64_t nsg = 1;
  2353. while (nsg <= nsgt) {
  2354. nsg *= 2;
  2355. }
  2356. nsg /= 2;
  2357. const size_t smem = (nqptg*(ne00 + 2*nsg*(ncpsg + nqptg)) + nsg*ne00)*(sizeof(float)/2);
  2358. //printf("smem: %zu, max: %zu\n", smem, ctx->device.maxThreadgroupMemoryLength);
  2359. GGML_ASSERT(smem <= ctx->device.maxThreadgroupMemoryLength);
  2360. [encoder setThreadgroupMemoryLength:GGML_PAD(smem, 16) atIndex:0];
  2361. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + nqptg - 1)/nqptg, ne02, ne03) threadsPerThreadgroup:MTLSizeMake(32, nsg, 1)];
  2362. }
  2363. } break;
  2364. case GGML_OP_DUP:
  2365. case GGML_OP_CPY:
  2366. case GGML_OP_CONT:
  2367. {
  2368. GGML_ASSERT(ne00 % ggml_blck_size(src0->type) == 0);
  2369. int nth = MIN(1024, ne00/ggml_blck_size(src0->type));
  2370. id<MTLComputePipelineState> pipeline = nil;
  2371. switch (src0t) {
  2372. case GGML_TYPE_F32:
  2373. {
  2374. GGML_ASSERT(ne0 % ggml_blck_size(dst->type) == 0);
  2375. switch (dstt) {
  2376. case GGML_TYPE_F16: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_CPY_F32_F16].pipeline; break;
  2377. case GGML_TYPE_F32: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_CPY_F32_F32].pipeline; break;
  2378. case GGML_TYPE_Q8_0: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_CPY_F32_Q8_0].pipeline; break;
  2379. case GGML_TYPE_Q4_0: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_CPY_F32_Q4_0].pipeline; break;
  2380. case GGML_TYPE_Q4_1: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_CPY_F32_Q4_1].pipeline; break;
  2381. case GGML_TYPE_Q5_0: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_CPY_F32_Q5_0].pipeline; break;
  2382. case GGML_TYPE_Q5_1: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_CPY_F32_Q5_1].pipeline; break;
  2383. case GGML_TYPE_IQ4_NL: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_CPY_F32_IQ4_NL].pipeline; break;
  2384. default: GGML_ASSERT(false && "not implemented");
  2385. };
  2386. } break;
  2387. case GGML_TYPE_F16:
  2388. {
  2389. switch (dstt) {
  2390. case GGML_TYPE_F16: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_CPY_F16_F16].pipeline; break;
  2391. case GGML_TYPE_F32: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_CPY_F16_F32].pipeline; break;
  2392. default: GGML_ASSERT(false && "not implemented");
  2393. };
  2394. } break;
  2395. default: GGML_ASSERT(false && "not implemented");
  2396. }
  2397. [encoder setComputePipelineState:pipeline];
  2398. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  2399. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  2400. [encoder setBytes:&ne00 length:sizeof( int64_t) atIndex:2];
  2401. [encoder setBytes:&ne01 length:sizeof( int64_t) atIndex:3];
  2402. [encoder setBytes:&ne02 length:sizeof( int64_t) atIndex:4];
  2403. [encoder setBytes:&ne03 length:sizeof( int64_t) atIndex:5];
  2404. [encoder setBytes:&nb00 length:sizeof(uint64_t) atIndex:6];
  2405. [encoder setBytes:&nb01 length:sizeof(uint64_t) atIndex:7];
  2406. [encoder setBytes:&nb02 length:sizeof(uint64_t) atIndex:8];
  2407. [encoder setBytes:&nb03 length:sizeof(uint64_t) atIndex:9];
  2408. [encoder setBytes:&ne0 length:sizeof( int64_t) atIndex:10];
  2409. [encoder setBytes:&ne1 length:sizeof( int64_t) atIndex:11];
  2410. [encoder setBytes:&ne2 length:sizeof( int64_t) atIndex:12];
  2411. [encoder setBytes:&ne3 length:sizeof( int64_t) atIndex:13];
  2412. [encoder setBytes:&nb0 length:sizeof(uint64_t) atIndex:14];
  2413. [encoder setBytes:&nb1 length:sizeof(uint64_t) atIndex:15];
  2414. [encoder setBytes:&nb2 length:sizeof(uint64_t) atIndex:16];
  2415. [encoder setBytes:&nb3 length:sizeof(uint64_t) atIndex:17];
  2416. [encoder dispatchThreadgroups:MTLSizeMake(ne01, ne02, ne03) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  2417. } break;
  2418. default:
  2419. {
  2420. GGML_METAL_LOG_ERROR("%s: error: node %3d, op = %8s not implemented\n", __func__, i, ggml_op_name(dst->op));
  2421. GGML_ASSERT(false);
  2422. }
  2423. }
  2424. if (should_capture) {
  2425. [encoder popDebugGroup];
  2426. }
  2427. }
  2428. [encoder endEncoding];
  2429. [command_buffer commit];
  2430. });
  2431. // Wait for completion and check status of each command buffer
  2432. // needed to detect if the device ran out-of-memory for example (#1881)
  2433. for (int i = 0; i < n_cb; ++i) {
  2434. id<MTLCommandBuffer> command_buffer = command_buffers[i];
  2435. [command_buffer waitUntilCompleted];
  2436. MTLCommandBufferStatus status = [command_buffer status];
  2437. if (status != MTLCommandBufferStatusCompleted) {
  2438. GGML_METAL_LOG_INFO("%s: command buffer %d failed with status %lu\n", __func__, i, status);
  2439. if (status == MTLCommandBufferStatusError) {
  2440. NSString * error_code = [command_buffer error].localizedDescription;
  2441. GGML_METAL_LOG_INFO("error: %s\n", [error_code UTF8String]);
  2442. }
  2443. return GGML_STATUS_FAILED;
  2444. }
  2445. }
  2446. if (should_capture) {
  2447. [[MTLCaptureManager sharedCaptureManager] stopCapture];
  2448. }
  2449. }
  2450. return GGML_STATUS_SUCCESS;
  2451. }
  2452. ////////////////////////////////////////////////////////////////////////////////
  2453. // backend interface
  2454. // default buffer
  2455. static id<MTLDevice> g_backend_device = nil;
  2456. static int g_backend_device_ref_count = 0;
  2457. static id<MTLDevice> ggml_backend_metal_get_device(void) {
  2458. if (g_backend_device == nil) {
  2459. g_backend_device = MTLCreateSystemDefaultDevice();
  2460. }
  2461. g_backend_device_ref_count++;
  2462. return g_backend_device;
  2463. }
  2464. static void ggml_backend_metal_free_device(void) {
  2465. assert(g_backend_device_ref_count > 0);
  2466. g_backend_device_ref_count--;
  2467. if (g_backend_device_ref_count == 0) {
  2468. [g_backend_device release];
  2469. g_backend_device = nil;
  2470. }
  2471. }
  2472. GGML_CALL static const char * ggml_backend_metal_buffer_get_name(ggml_backend_buffer_t buffer) {
  2473. return "Metal";
  2474. UNUSED(buffer);
  2475. }
  2476. GGML_CALL static void ggml_backend_metal_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  2477. struct ggml_backend_metal_buffer_context * ctx = (struct ggml_backend_metal_buffer_context *)buffer->context;
  2478. for (int i = 0; i < ctx->n_buffers; i++) {
  2479. [ctx->buffers[i].metal release];
  2480. }
  2481. ggml_backend_metal_free_device();
  2482. if (ctx->owned) {
  2483. #if TARGET_OS_OSX
  2484. vm_deallocate((vm_map_t)mach_task_self(), (vm_address_t)ctx->all_data, ctx->all_size);
  2485. #else
  2486. free(ctx->all_data);
  2487. #endif
  2488. }
  2489. free(ctx);
  2490. }
  2491. GGML_CALL static void * ggml_backend_metal_buffer_get_base(ggml_backend_buffer_t buffer) {
  2492. struct ggml_backend_metal_buffer_context * ctx = (struct ggml_backend_metal_buffer_context *)buffer->context;
  2493. return ctx->all_data;
  2494. }
  2495. GGML_CALL static void ggml_backend_metal_buffer_set_tensor(ggml_backend_buffer_t buffer, struct ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
  2496. memcpy((char *)tensor->data + offset, data, size);
  2497. UNUSED(buffer);
  2498. }
  2499. GGML_CALL static void ggml_backend_metal_buffer_get_tensor(ggml_backend_buffer_t buffer, const struct ggml_tensor * tensor, void * data, size_t offset, size_t size) {
  2500. memcpy(data, (const char *)tensor->data + offset, size);
  2501. UNUSED(buffer);
  2502. }
  2503. GGML_CALL static bool ggml_backend_metal_buffer_cpy_tensor(ggml_backend_buffer_t buffer, const struct ggml_tensor * src, struct ggml_tensor * dst) {
  2504. if (ggml_backend_buffer_is_host(src->buffer)) {
  2505. memcpy(dst->data, src->data, ggml_nbytes(src));
  2506. return true;
  2507. }
  2508. return false;
  2509. UNUSED(buffer);
  2510. }
  2511. GGML_CALL static void ggml_backend_metal_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  2512. struct ggml_backend_metal_buffer_context * ctx = (struct ggml_backend_metal_buffer_context *)buffer->context;
  2513. memset(ctx->all_data, value, ctx->all_size);
  2514. }
  2515. static struct ggml_backend_buffer_i ggml_backend_metal_buffer_i = {
  2516. /* .get_name = */ ggml_backend_metal_buffer_get_name,
  2517. /* .free_buffer = */ ggml_backend_metal_buffer_free_buffer,
  2518. /* .get_base = */ ggml_backend_metal_buffer_get_base,
  2519. /* .init_tensor = */ NULL,
  2520. /* .set_tensor = */ ggml_backend_metal_buffer_set_tensor,
  2521. /* .get_tensor = */ ggml_backend_metal_buffer_get_tensor,
  2522. /* .cpy_tensor = */ ggml_backend_metal_buffer_cpy_tensor,
  2523. /* .clear = */ ggml_backend_metal_buffer_clear,
  2524. /* .reset = */ NULL,
  2525. };
  2526. // default buffer type
  2527. GGML_CALL static const char * ggml_backend_metal_buffer_type_get_name(ggml_backend_buffer_type_t buft) {
  2528. return "Metal";
  2529. UNUSED(buft);
  2530. }
  2531. static void ggml_backend_metal_log_allocated_size(id<MTLDevice> device, size_t size_aligned) {
  2532. #ifndef GGML_METAL_NDEBUG
  2533. #if TARGET_OS_OSX || (TARGET_OS_IOS && __clang_major__ >= 15)
  2534. if (@available(macOS 10.12, iOS 16.0, *)) {
  2535. GGML_METAL_LOG_INFO("%s: allocated buffer, size = %8.2f MiB, (%8.2f / %8.2f)",
  2536. __func__,
  2537. size_aligned / 1024.0 / 1024.0,
  2538. device.currentAllocatedSize / 1024.0 / 1024.0,
  2539. device.recommendedMaxWorkingSetSize / 1024.0 / 1024.0);
  2540. if (device.currentAllocatedSize > device.recommendedMaxWorkingSetSize) {
  2541. GGML_METAL_LOG_WARN("%s: warning: current allocated size is greater than the recommended max working set size\n", __func__);
  2542. } else {
  2543. GGML_METAL_LOG_INFO("\n");
  2544. }
  2545. } else {
  2546. GGML_METAL_LOG_INFO("%s: allocated buffer, size = %8.2f MiB, (%8.2f)\n",
  2547. __func__,
  2548. size_aligned / 1024.0 / 1024.0,
  2549. device.currentAllocatedSize / 1024.0 / 1024.0);
  2550. }
  2551. #endif
  2552. #endif
  2553. UNUSED(device);
  2554. UNUSED(size_aligned);
  2555. }
  2556. GGML_CALL static ggml_backend_buffer_t ggml_backend_metal_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  2557. struct ggml_backend_metal_buffer_context * ctx = malloc(sizeof(struct ggml_backend_metal_buffer_context));
  2558. const size_t size_page = sysconf(_SC_PAGESIZE);
  2559. size_t size_aligned = size;
  2560. if ((size_aligned % size_page) != 0) {
  2561. size_aligned += (size_page - (size_aligned % size_page));
  2562. }
  2563. id<MTLDevice> device = ggml_backend_metal_get_device();
  2564. ctx->all_data = ggml_metal_host_malloc(size_aligned);
  2565. ctx->all_size = size_aligned;
  2566. ctx->owned = true;
  2567. ctx->n_buffers = 1;
  2568. if (ctx->all_data != NULL) {
  2569. ctx->buffers[0].data = ctx->all_data;
  2570. ctx->buffers[0].size = size;
  2571. ctx->buffers[0].metal = [device newBufferWithBytesNoCopy:ctx->all_data
  2572. length:size_aligned
  2573. options:MTLResourceStorageModeShared
  2574. deallocator:nil];
  2575. }
  2576. if (ctx->all_data == NULL || ctx->buffers[0].metal == nil) {
  2577. GGML_METAL_LOG_ERROR("%s: error: failed to allocate buffer, size = %8.2f MiB\n", __func__, size_aligned / 1024.0 / 1024.0);
  2578. free(ctx);
  2579. ggml_backend_metal_free_device();
  2580. return NULL;
  2581. }
  2582. //ggml_backend_metal_log_allocated_size(device, size_aligned);
  2583. return ggml_backend_buffer_init(buft, ggml_backend_metal_buffer_i, ctx, size);
  2584. }
  2585. GGML_CALL static size_t ggml_backend_metal_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  2586. return 32;
  2587. UNUSED(buft);
  2588. }
  2589. GGML_CALL static size_t ggml_backend_metal_buffer_type_get_max_size(ggml_backend_buffer_type_t buft) {
  2590. id<MTLDevice> device = ggml_backend_metal_get_device();
  2591. size_t max_size = device.maxBufferLength;
  2592. ggml_backend_metal_free_device();
  2593. return max_size;
  2594. UNUSED(buft);
  2595. }
  2596. GGML_CALL static bool ggml_backend_metal_buffer_type_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  2597. return ggml_backend_is_metal(backend) || ggml_backend_is_cpu(backend);
  2598. UNUSED(buft);
  2599. }
  2600. GGML_CALL static bool ggml_backend_metal_buffer_type_is_host(ggml_backend_buffer_type_t buft) {
  2601. return true;
  2602. UNUSED(buft);
  2603. }
  2604. GGML_CALL ggml_backend_buffer_type_t ggml_backend_metal_buffer_type(void) {
  2605. static struct ggml_backend_buffer_type ggml_backend_buffer_type_metal = {
  2606. /* .iface = */ {
  2607. /* .get_name = */ ggml_backend_metal_buffer_type_get_name,
  2608. /* .alloc_buffer = */ ggml_backend_metal_buffer_type_alloc_buffer,
  2609. /* .get_alignment = */ ggml_backend_metal_buffer_type_get_alignment,
  2610. /* .get_max_size = */ ggml_backend_metal_buffer_type_get_max_size,
  2611. /* .get_alloc_size = */ NULL, // defaults to ggml_nbytes
  2612. /* .supports_backend = */ ggml_backend_metal_buffer_type_supports_backend,
  2613. /* .is_host = */ ggml_backend_metal_buffer_type_is_host,
  2614. },
  2615. /* .context = */ NULL,
  2616. };
  2617. return &ggml_backend_buffer_type_metal;
  2618. }
  2619. // buffer from ptr
  2620. GGML_CALL ggml_backend_buffer_t ggml_backend_metal_buffer_from_ptr(void * data, size_t size, size_t max_size) {
  2621. struct ggml_backend_metal_buffer_context * ctx = malloc(sizeof(struct ggml_backend_metal_buffer_context));
  2622. ctx->all_data = data;
  2623. ctx->all_size = size;
  2624. ctx->owned = false;
  2625. ctx->n_buffers = 0;
  2626. const size_t size_page = sysconf(_SC_PAGESIZE);
  2627. // page-align the data ptr
  2628. {
  2629. const uintptr_t offs = (uintptr_t) data % size_page;
  2630. data = (void *) ((char *) data - offs);
  2631. size += offs;
  2632. }
  2633. size_t size_aligned = size;
  2634. if ((size_aligned % size_page) != 0) {
  2635. size_aligned += (size_page - (size_aligned % size_page));
  2636. }
  2637. id<MTLDevice> device = ggml_backend_metal_get_device();
  2638. // the buffer fits into the max buffer size allowed by the device
  2639. if (size_aligned <= device.maxBufferLength) {
  2640. ctx->buffers[ctx->n_buffers].data = data;
  2641. ctx->buffers[ctx->n_buffers].size = size;
  2642. ctx->buffers[ctx->n_buffers].metal = [device newBufferWithBytesNoCopy:data length:size_aligned options:MTLResourceStorageModeShared deallocator:nil];
  2643. if (ctx->buffers[ctx->n_buffers].metal == nil) {
  2644. GGML_METAL_LOG_ERROR("%s: error: failed to allocate buffer, size = %8.2f MiB\n", __func__, size_aligned / 1024.0 / 1024.0);
  2645. return false;
  2646. }
  2647. ggml_backend_metal_log_allocated_size(device, size_aligned);
  2648. ++ctx->n_buffers;
  2649. } else {
  2650. // this overlap between the views will guarantee that the tensor with the maximum size will fully fit into
  2651. // one of the views
  2652. const size_t size_ovlp = ((max_size + size_page - 1) / size_page + 1) * size_page; // round-up 2 pages just in case
  2653. const size_t size_step = device.maxBufferLength - size_ovlp;
  2654. const size_t size_view = device.maxBufferLength;
  2655. for (size_t i = 0; i < size; i += size_step) {
  2656. const size_t size_step_aligned = (i + size_view <= size) ? size_view : (size_aligned - i);
  2657. ctx->buffers[ctx->n_buffers].data = (void *) ((uint8_t *) data + i);
  2658. ctx->buffers[ctx->n_buffers].size = size_step_aligned;
  2659. ctx->buffers[ctx->n_buffers].metal = [device newBufferWithBytesNoCopy:(void *) ((uint8_t *) data + i) length:size_step_aligned options:MTLResourceStorageModeShared deallocator:nil];
  2660. if (ctx->buffers[ctx->n_buffers].metal == nil) {
  2661. GGML_METAL_LOG_ERROR("%s: error: failed to allocate buffer, size = %8.2f MiB\n", __func__, size_step_aligned / 1024.0 / 1024.0);
  2662. return false;
  2663. }
  2664. ggml_backend_metal_log_allocated_size(device, size_step_aligned);
  2665. if (i + size_step < size) {
  2666. GGML_METAL_LOG_INFO("\n");
  2667. }
  2668. ++ctx->n_buffers;
  2669. }
  2670. }
  2671. return ggml_backend_buffer_init(ggml_backend_metal_buffer_type(), ggml_backend_metal_buffer_i, ctx, size);
  2672. }
  2673. // backend
  2674. GGML_CALL static const char * ggml_backend_metal_name(ggml_backend_t backend) {
  2675. return "Metal";
  2676. UNUSED(backend);
  2677. }
  2678. GGML_CALL static void ggml_backend_metal_free(ggml_backend_t backend) {
  2679. struct ggml_metal_context * ctx = (struct ggml_metal_context *)backend->context;
  2680. ggml_metal_free(ctx);
  2681. free(backend);
  2682. }
  2683. GGML_CALL static ggml_backend_buffer_type_t ggml_backend_metal_get_default_buffer_type(ggml_backend_t backend) {
  2684. return ggml_backend_metal_buffer_type();
  2685. UNUSED(backend);
  2686. }
  2687. GGML_CALL static enum ggml_status ggml_backend_metal_graph_compute(ggml_backend_t backend, struct ggml_cgraph * cgraph) {
  2688. struct ggml_metal_context * metal_ctx = (struct ggml_metal_context *)backend->context;
  2689. return ggml_metal_graph_compute(metal_ctx, cgraph);
  2690. }
  2691. GGML_CALL static bool ggml_backend_metal_supports_op(ggml_backend_t backend, const struct ggml_tensor * op) {
  2692. struct ggml_metal_context * metal_ctx = (struct ggml_metal_context *)backend->context;
  2693. return ggml_metal_supports_op(metal_ctx, op);
  2694. }
  2695. static struct ggml_backend_i ggml_backend_metal_i = {
  2696. /* .get_name = */ ggml_backend_metal_name,
  2697. /* .free = */ ggml_backend_metal_free,
  2698. /* .get_default_buffer_type = */ ggml_backend_metal_get_default_buffer_type,
  2699. /* .set_tensor_async = */ NULL,
  2700. /* .get_tensor_async = */ NULL,
  2701. /* .cpy_tensor_async = */ NULL,
  2702. /* .synchronize = */ NULL,
  2703. /* .graph_plan_create = */ NULL,
  2704. /* .graph_plan_free = */ NULL,
  2705. /* .graph_plan_compute = */ NULL,
  2706. /* .graph_compute = */ ggml_backend_metal_graph_compute,
  2707. /* .supports_op = */ ggml_backend_metal_supports_op,
  2708. /* .offload_op = */ NULL,
  2709. /* .event_new = */ NULL,
  2710. /* .event_free = */ NULL,
  2711. /* .event_record = */ NULL,
  2712. /* .event_wait = */ NULL,
  2713. /* .event_synchronize = */ NULL,
  2714. };
  2715. void ggml_backend_metal_log_set_callback(ggml_log_callback log_callback, void * user_data) {
  2716. ggml_metal_log_callback = log_callback;
  2717. ggml_metal_log_user_data = user_data;
  2718. }
  2719. static ggml_guid_t ggml_backend_metal_guid(void) {
  2720. static ggml_guid guid = { 0x81, 0xa1, 0x8b, 0x1e, 0x71, 0xec, 0x79, 0xed, 0x2b, 0x85, 0xdc, 0x8a, 0x61, 0x98, 0x30, 0xe6 };
  2721. return &guid;
  2722. }
  2723. ggml_backend_t ggml_backend_metal_init(void) {
  2724. struct ggml_metal_context * ctx = ggml_metal_init(GGML_DEFAULT_N_THREADS);
  2725. if (ctx == NULL) {
  2726. return NULL;
  2727. }
  2728. ggml_backend_t metal_backend = malloc(sizeof(struct ggml_backend));
  2729. *metal_backend = (struct ggml_backend) {
  2730. /* .guid = */ ggml_backend_metal_guid(),
  2731. /* .interface = */ ggml_backend_metal_i,
  2732. /* .context = */ ctx,
  2733. };
  2734. return metal_backend;
  2735. }
  2736. bool ggml_backend_is_metal(ggml_backend_t backend) {
  2737. return backend != NULL && ggml_guid_matches(backend->guid, ggml_backend_metal_guid());
  2738. }
  2739. void ggml_backend_metal_set_n_cb(ggml_backend_t backend, int n_cb) {
  2740. GGML_ASSERT(ggml_backend_is_metal(backend));
  2741. struct ggml_metal_context * ctx = (struct ggml_metal_context *)backend->context;
  2742. ctx->n_cb = MIN(n_cb, GGML_METAL_MAX_BUFFERS);
  2743. }
  2744. bool ggml_backend_metal_supports_family(ggml_backend_t backend, int family) {
  2745. GGML_ASSERT(ggml_backend_is_metal(backend));
  2746. struct ggml_metal_context * ctx = (struct ggml_metal_context *)backend->context;
  2747. return [ctx->device supportsFamily:(MTLGPUFamilyApple1 + family - 1)];
  2748. }
  2749. void ggml_backend_metal_capture_next_compute(ggml_backend_t backend) {
  2750. GGML_ASSERT(ggml_backend_is_metal(backend));
  2751. struct ggml_metal_context * ctx = (struct ggml_metal_context *)backend->context;
  2752. ctx->should_capture_next_compute = true;
  2753. }
  2754. GGML_CALL ggml_backend_t ggml_backend_reg_metal_init(const char * params, void * user_data); // silence warning
  2755. GGML_CALL ggml_backend_t ggml_backend_reg_metal_init(const char * params, void * user_data) {
  2756. return ggml_backend_metal_init();
  2757. GGML_UNUSED(params);
  2758. GGML_UNUSED(user_data);
  2759. }