ggml-metal_darwin_arm64.m 196 KB

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