ggml-metal.m 164 KB

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