ggml-metal.m 186 KB

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