ggml-metal.m 169 KB

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