ggml-metal.m 167 KB

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