ggml-metal.m 187 KB

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