ggml-metal.m 165 KB

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