ggml-metal.m 200 KB

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