ggml-metal.m 183 KB

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