ggml-metal.m 163 KB

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