ggml-metal.m 183 KB

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