ggml-metal_darwin_arm64.m 252 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791
  1. /**
  2. * llama.cpp - commit 40c6d79fb52f995f47507fedfeaae2ac05d9b35c - 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 "ggml-metal-impl.h"
  30. #import <Foundation/Foundation.h>
  31. #import <Metal/Metal.h>
  32. #undef MIN
  33. #undef MAX
  34. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  35. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  36. // max memory buffers that can be mapped to the device
  37. #define GGML_METAL_MAX_BUFFERS 64
  38. // max number of MTLCommandBuffer used to submit a graph for processing
  39. #define GGML_METAL_MAX_COMMAND_BUFFERS 8
  40. #define UNUSED(x) (void)(x)
  41. // globals
  42. // overload of MTLGPUFamilyMetal3 (not available in some environments)
  43. static const NSInteger MTLGPUFamilyMetal3_GGML = 5001;
  44. // initialized in ggml_backend_metal_reg
  45. static struct ggml_backend_reg g_ggml_backend_metal_reg;
  46. static struct ggml_backend_device g_ggml_backend_metal_device;
  47. // information about a Metal device
  48. // note: assumes single GPU device - the default one
  49. // TODO: support multiple GPU devices
  50. static struct ggml_backend_metal_device_context {
  51. id<MTLDevice> mtl_device;
  52. int mtl_device_ref_count;
  53. bool has_simdgroup_reduction;
  54. bool has_simdgroup_mm;
  55. bool has_bfloat;
  56. bool use_bfloat;
  57. char name[128];
  58. } g_ggml_ctx_dev_main = {
  59. /*.mtl_device =*/ nil,
  60. /*.mtl_device_ref_count =*/ 0,
  61. /*.has_simdgroup_reduction =*/ false,
  62. /*.has_simdgroup_mm =*/ false,
  63. /*.has_bfloat =*/ false,
  64. /*.use_bfloat =*/ false,
  65. /*.name =*/ "",
  66. };
  67. // acquire
  68. static id<MTLDevice> ggml_backend_metal_device_acq(struct ggml_backend_metal_device_context * ctx) {
  69. assert(ctx != NULL);
  70. if (ctx->mtl_device == nil) {
  71. ctx->mtl_device = MTLCreateSystemDefaultDevice();
  72. ctx->has_simdgroup_reduction = [ctx->mtl_device supportsFamily:MTLGPUFamilyApple7];
  73. ctx->has_simdgroup_reduction |= [ctx->mtl_device supportsFamily:MTLGPUFamilyMetal3_GGML];
  74. ctx->has_simdgroup_mm = [ctx->mtl_device supportsFamily:MTLGPUFamilyApple7];
  75. ctx->has_bfloat = [ctx->mtl_device supportsFamily:MTLGPUFamilyMetal3_GGML];
  76. ctx->has_bfloat |= [ctx->mtl_device supportsFamily:MTLGPUFamilyApple6];
  77. #if defined(GGML_METAL_USE_BF16)
  78. ctx->use_bfloat = ctx->has_bfloat;
  79. #else
  80. ctx->use_bfloat = false;
  81. #endif
  82. strncpy(ctx->name, [[ctx->mtl_device name] UTF8String], sizeof(ctx->name) - 1);
  83. }
  84. ctx->mtl_device_ref_count++;
  85. return ctx->mtl_device;
  86. }
  87. // release
  88. static void ggml_backend_metal_device_rel(struct ggml_backend_metal_device_context * ctx) {
  89. assert(ctx != NULL);
  90. assert(ctx->mtl_device_ref_count > 0);
  91. ctx->mtl_device_ref_count--;
  92. if (ctx->mtl_device_ref_count == 0) {
  93. [ctx->mtl_device release];
  94. ctx->mtl_device = nil;
  95. }
  96. }
  97. // kernels
  98. struct ggml_metal_kernel {
  99. id<MTLComputePipelineState> pipeline;
  100. };
  101. enum ggml_metal_kernel_type {
  102. GGML_METAL_KERNEL_TYPE_ADD,
  103. GGML_METAL_KERNEL_TYPE_ADD_ROW,
  104. GGML_METAL_KERNEL_TYPE_SUB,
  105. GGML_METAL_KERNEL_TYPE_SUB_ROW,
  106. GGML_METAL_KERNEL_TYPE_MUL,
  107. GGML_METAL_KERNEL_TYPE_MUL_ROW,
  108. GGML_METAL_KERNEL_TYPE_DIV,
  109. GGML_METAL_KERNEL_TYPE_DIV_ROW,
  110. GGML_METAL_KERNEL_TYPE_REPEAT_F32,
  111. GGML_METAL_KERNEL_TYPE_REPEAT_F16,
  112. GGML_METAL_KERNEL_TYPE_REPEAT_I32,
  113. GGML_METAL_KERNEL_TYPE_REPEAT_I16,
  114. GGML_METAL_KERNEL_TYPE_SCALE,
  115. GGML_METAL_KERNEL_TYPE_SCALE_4,
  116. GGML_METAL_KERNEL_TYPE_CLAMP,
  117. GGML_METAL_KERNEL_TYPE_TANH,
  118. GGML_METAL_KERNEL_TYPE_RELU,
  119. GGML_METAL_KERNEL_TYPE_SIGMOID,
  120. GGML_METAL_KERNEL_TYPE_GELU,
  121. GGML_METAL_KERNEL_TYPE_GELU_4,
  122. GGML_METAL_KERNEL_TYPE_GELU_QUICK,
  123. GGML_METAL_KERNEL_TYPE_GELU_QUICK_4,
  124. GGML_METAL_KERNEL_TYPE_SILU,
  125. GGML_METAL_KERNEL_TYPE_SILU_4,
  126. GGML_METAL_KERNEL_TYPE_ELU,
  127. GGML_METAL_KERNEL_TYPE_SOFT_MAX_F16,
  128. GGML_METAL_KERNEL_TYPE_SOFT_MAX_F16_4,
  129. GGML_METAL_KERNEL_TYPE_SOFT_MAX_F32,
  130. GGML_METAL_KERNEL_TYPE_SOFT_MAX_F32_4,
  131. GGML_METAL_KERNEL_TYPE_DIAG_MASK_INF,
  132. GGML_METAL_KERNEL_TYPE_DIAG_MASK_INF_8,
  133. GGML_METAL_KERNEL_TYPE_GET_ROWS_F32,
  134. GGML_METAL_KERNEL_TYPE_GET_ROWS_F16,
  135. GGML_METAL_KERNEL_TYPE_GET_ROWS_BF16,
  136. GGML_METAL_KERNEL_TYPE_GET_ROWS_Q4_0,
  137. GGML_METAL_KERNEL_TYPE_GET_ROWS_Q4_1,
  138. GGML_METAL_KERNEL_TYPE_GET_ROWS_Q5_0,
  139. GGML_METAL_KERNEL_TYPE_GET_ROWS_Q5_1,
  140. GGML_METAL_KERNEL_TYPE_GET_ROWS_Q8_0,
  141. GGML_METAL_KERNEL_TYPE_GET_ROWS_Q2_K,
  142. GGML_METAL_KERNEL_TYPE_GET_ROWS_Q3_K,
  143. GGML_METAL_KERNEL_TYPE_GET_ROWS_Q4_K,
  144. GGML_METAL_KERNEL_TYPE_GET_ROWS_Q5_K,
  145. GGML_METAL_KERNEL_TYPE_GET_ROWS_Q6_K,
  146. GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ2_XXS,
  147. GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ2_XS,
  148. GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ3_XXS,
  149. GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ3_S,
  150. GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ2_S,
  151. GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ1_S,
  152. GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ1_M,
  153. GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ4_NL,
  154. GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ4_XS,
  155. GGML_METAL_KERNEL_TYPE_GET_ROWS_I32,
  156. GGML_METAL_KERNEL_TYPE_RMS_NORM,
  157. GGML_METAL_KERNEL_TYPE_GROUP_NORM,
  158. GGML_METAL_KERNEL_TYPE_NORM,
  159. GGML_METAL_KERNEL_TYPE_SSM_CONV_F32,
  160. GGML_METAL_KERNEL_TYPE_SSM_SCAN_F32,
  161. GGML_METAL_KERNEL_TYPE_MUL_MV_F32_F32,
  162. GGML_METAL_KERNEL_TYPE_MUL_MV_F16_F32,
  163. GGML_METAL_KERNEL_TYPE_MUL_MV_F16_F32_1ROW,
  164. GGML_METAL_KERNEL_TYPE_MUL_MV_F16_F32_L4,
  165. GGML_METAL_KERNEL_TYPE_MUL_MV_F16_F16,
  166. GGML_METAL_KERNEL_TYPE_MUL_MV_BF16_F32,
  167. GGML_METAL_KERNEL_TYPE_MUL_MV_BF16_F32_1ROW,
  168. GGML_METAL_KERNEL_TYPE_MUL_MV_BF16_F32_L4,
  169. GGML_METAL_KERNEL_TYPE_MUL_MV_BF16_BF16,
  170. GGML_METAL_KERNEL_TYPE_MUL_MV_Q4_0_F32,
  171. GGML_METAL_KERNEL_TYPE_MUL_MV_Q4_1_F32,
  172. GGML_METAL_KERNEL_TYPE_MUL_MV_Q5_0_F32,
  173. GGML_METAL_KERNEL_TYPE_MUL_MV_Q5_1_F32,
  174. GGML_METAL_KERNEL_TYPE_MUL_MV_Q8_0_F32,
  175. GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_F16_F32_R1_2,
  176. GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_F16_F32_R1_3,
  177. GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_F16_F32_R1_4,
  178. GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_F16_F32_R1_5,
  179. GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q4_0_F32_R1_2,
  180. GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q4_0_F32_R1_3,
  181. GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q4_0_F32_R1_4,
  182. GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q4_0_F32_R1_5,
  183. GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q4_1_F32_R1_2,
  184. GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q4_1_F32_R1_3,
  185. GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q4_1_F32_R1_4,
  186. GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q4_1_F32_R1_5,
  187. GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q5_0_F32_R1_2,
  188. GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q5_0_F32_R1_3,
  189. GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q5_0_F32_R1_4,
  190. GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q5_0_F32_R1_5,
  191. GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q5_1_F32_R1_2,
  192. GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q5_1_F32_R1_3,
  193. GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q5_1_F32_R1_4,
  194. GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q5_1_F32_R1_5,
  195. GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q8_0_F32_R1_2,
  196. GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q8_0_F32_R1_3,
  197. GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q8_0_F32_R1_4,
  198. GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q8_0_F32_R1_5,
  199. GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q4_K_F32_R1_2,
  200. GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q4_K_F32_R1_3,
  201. GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q4_K_F32_R1_4,
  202. GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q4_K_F32_R1_5,
  203. GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q5_K_F32_R1_2,
  204. GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q5_K_F32_R1_3,
  205. GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q5_K_F32_R1_4,
  206. GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q5_K_F32_R1_5,
  207. GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q6_K_F32_R1_2,
  208. GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q6_K_F32_R1_3,
  209. GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q6_K_F32_R1_4,
  210. GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q6_K_F32_R1_5,
  211. GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_IQ4_NL_F32_R1_2,
  212. GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_IQ4_NL_F32_R1_3,
  213. GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_IQ4_NL_F32_R1_4,
  214. GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_IQ4_NL_F32_R1_5,
  215. GGML_METAL_KERNEL_TYPE_MUL_MV_Q2_K_F32,
  216. GGML_METAL_KERNEL_TYPE_MUL_MV_Q3_K_F32,
  217. GGML_METAL_KERNEL_TYPE_MUL_MV_Q4_K_F32,
  218. GGML_METAL_KERNEL_TYPE_MUL_MV_Q5_K_F32,
  219. GGML_METAL_KERNEL_TYPE_MUL_MV_Q6_K_F32,
  220. GGML_METAL_KERNEL_TYPE_MUL_MV_IQ2_XXS_F32,
  221. GGML_METAL_KERNEL_TYPE_MUL_MV_IQ2_XS_F32,
  222. GGML_METAL_KERNEL_TYPE_MUL_MV_IQ3_XXS_F32,
  223. GGML_METAL_KERNEL_TYPE_MUL_MV_IQ3_S_F32,
  224. GGML_METAL_KERNEL_TYPE_MUL_MV_IQ2_S_F32,
  225. GGML_METAL_KERNEL_TYPE_MUL_MV_IQ1_S_F32,
  226. GGML_METAL_KERNEL_TYPE_MUL_MV_IQ1_M_F32,
  227. GGML_METAL_KERNEL_TYPE_MUL_MV_IQ4_NL_F32,
  228. GGML_METAL_KERNEL_TYPE_MUL_MV_IQ4_XS_F32,
  229. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_F32_F32,
  230. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_F16_F32,
  231. //GGML_METAL_KERNEL_TYPE_MUL_MV_ID_F16_F32_1ROW,
  232. //GGML_METAL_KERNEL_TYPE_MUL_MV_ID_F16_F32_L4,
  233. //GGML_METAL_KERNEL_TYPE_MUL_MV_ID_F16_F16,
  234. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_BF16_F32,
  235. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q4_0_F32,
  236. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q4_1_F32,
  237. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q5_0_F32,
  238. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q5_1_F32,
  239. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q8_0_F32,
  240. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q2_K_F32,
  241. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q3_K_F32,
  242. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q4_K_F32,
  243. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q5_K_F32,
  244. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q6_K_F32,
  245. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ2_XXS_F32,
  246. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ2_XS_F32,
  247. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ3_XXS_F32,
  248. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ3_S_F32,
  249. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ2_S_F32,
  250. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ1_S_F32,
  251. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ1_M_F32,
  252. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ4_NL_F32,
  253. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ4_XS_F32,
  254. GGML_METAL_KERNEL_TYPE_MUL_MM_F32_F32,
  255. GGML_METAL_KERNEL_TYPE_MUL_MM_F16_F32,
  256. GGML_METAL_KERNEL_TYPE_MUL_MM_BF16_F32,
  257. GGML_METAL_KERNEL_TYPE_MUL_MM_Q4_0_F32,
  258. GGML_METAL_KERNEL_TYPE_MUL_MM_Q4_1_F32,
  259. GGML_METAL_KERNEL_TYPE_MUL_MM_Q5_0_F32,
  260. GGML_METAL_KERNEL_TYPE_MUL_MM_Q5_1_F32,
  261. GGML_METAL_KERNEL_TYPE_MUL_MM_Q8_0_F32,
  262. GGML_METAL_KERNEL_TYPE_MUL_MM_Q2_K_F32,
  263. GGML_METAL_KERNEL_TYPE_MUL_MM_Q3_K_F32,
  264. GGML_METAL_KERNEL_TYPE_MUL_MM_Q4_K_F32,
  265. GGML_METAL_KERNEL_TYPE_MUL_MM_Q5_K_F32,
  266. GGML_METAL_KERNEL_TYPE_MUL_MM_Q6_K_F32,
  267. GGML_METAL_KERNEL_TYPE_MUL_MM_IQ2_XXS_F32,
  268. GGML_METAL_KERNEL_TYPE_MUL_MM_IQ2_XS_F32,
  269. GGML_METAL_KERNEL_TYPE_MUL_MM_IQ3_XXS_F32,
  270. GGML_METAL_KERNEL_TYPE_MUL_MM_IQ3_S_F32,
  271. GGML_METAL_KERNEL_TYPE_MUL_MM_IQ2_S_F32,
  272. GGML_METAL_KERNEL_TYPE_MUL_MM_IQ1_S_F32,
  273. GGML_METAL_KERNEL_TYPE_MUL_MM_IQ1_M_F32,
  274. GGML_METAL_KERNEL_TYPE_MUL_MM_IQ4_NL_F32,
  275. GGML_METAL_KERNEL_TYPE_MUL_MM_IQ4_XS_F32,
  276. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_F32_F32,
  277. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_F16_F32,
  278. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_BF16_F32,
  279. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q4_0_F32,
  280. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q4_1_F32,
  281. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q5_0_F32,
  282. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q5_1_F32,
  283. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q8_0_F32,
  284. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q2_K_F32,
  285. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q3_K_F32,
  286. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q4_K_F32,
  287. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q5_K_F32,
  288. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q6_K_F32,
  289. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ2_XXS_F32,
  290. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ2_XS_F32,
  291. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ3_XXS_F32,
  292. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ3_S_F32,
  293. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ2_S_F32,
  294. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ1_S_F32,
  295. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ1_M_F32,
  296. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ4_NL_F32,
  297. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ4_XS_F32,
  298. GGML_METAL_KERNEL_TYPE_ROPE_NORM_F32,
  299. GGML_METAL_KERNEL_TYPE_ROPE_NORM_F16,
  300. GGML_METAL_KERNEL_TYPE_ROPE_NEOX_F32,
  301. GGML_METAL_KERNEL_TYPE_ROPE_NEOX_F16,
  302. GGML_METAL_KERNEL_TYPE_IM2COL_F16,
  303. GGML_METAL_KERNEL_TYPE_IM2COL_F32,
  304. GGML_METAL_KERNEL_TYPE_IM2COL_EXT_F16,
  305. GGML_METAL_KERNEL_TYPE_IM2COL_EXT_F32,
  306. GGML_METAL_KERNEL_TYPE_CONV_TRANSPOSE_1D_F32_F32,
  307. GGML_METAL_KERNEL_TYPE_CONV_TRANSPOSE_1D_F16_F32,
  308. GGML_METAL_KERNEL_TYPE_UPSCALE_F32,
  309. GGML_METAL_KERNEL_TYPE_PAD_F32,
  310. GGML_METAL_KERNEL_TYPE_UNPAD_F32,
  311. GGML_METAL_KERNEL_TYPE_ARANGE_F32,
  312. GGML_METAL_KERNEL_TYPE_TIMESTEP_EMBEDDING_F32,
  313. GGML_METAL_KERNEL_TYPE_ARGSORT_F32_I32_ASC,
  314. GGML_METAL_KERNEL_TYPE_ARGSORT_F32_I32_DESC,
  315. GGML_METAL_KERNEL_TYPE_LEAKY_RELU_F32,
  316. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_F16_H64,
  317. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_F16_H80,
  318. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_F16_H96,
  319. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_F16_H112,
  320. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_F16_H128,
  321. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_F16_H256,
  322. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_BF16_H64,
  323. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_BF16_H80,
  324. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_BF16_H96,
  325. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_BF16_H112,
  326. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_BF16_H128,
  327. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_BF16_H256,
  328. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q4_0_H64,
  329. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q4_0_H80,
  330. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q4_0_H96,
  331. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q4_0_H112,
  332. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q4_0_H128,
  333. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q4_0_H256,
  334. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q4_1_H64,
  335. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q4_1_H80,
  336. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q4_1_H96,
  337. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q4_1_H112,
  338. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q4_1_H128,
  339. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q4_1_H256,
  340. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q5_0_H64,
  341. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q5_0_H80,
  342. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q5_0_H96,
  343. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q5_0_H112,
  344. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q5_0_H128,
  345. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q5_0_H256,
  346. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q5_1_H64,
  347. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q5_1_H80,
  348. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q5_1_H96,
  349. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q5_1_H112,
  350. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q5_1_H128,
  351. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q5_1_H256,
  352. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q8_0_H64,
  353. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q8_0_H80,
  354. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q8_0_H96,
  355. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q8_0_H112,
  356. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q8_0_H128,
  357. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q8_0_H256,
  358. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_VEC_F16_H128,
  359. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_VEC_BF16_H128,
  360. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_VEC_Q4_0_H128,
  361. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_VEC_Q4_1_H128,
  362. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_VEC_Q5_0_H128,
  363. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_VEC_Q5_1_H128,
  364. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_VEC_Q8_0_H128,
  365. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_VEC_F16_H256,
  366. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_VEC_BF16_H256,
  367. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_VEC_Q4_0_H256,
  368. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_VEC_Q4_1_H256,
  369. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_VEC_Q5_0_H256,
  370. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_VEC_Q5_1_H256,
  371. GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_VEC_Q8_0_H256,
  372. GGML_METAL_KERNEL_TYPE_CPY_F32_F32,
  373. GGML_METAL_KERNEL_TYPE_CPY_F32_F16,
  374. GGML_METAL_KERNEL_TYPE_CPY_F32_BF16,
  375. GGML_METAL_KERNEL_TYPE_CPY_F16_F16,
  376. GGML_METAL_KERNEL_TYPE_CPY_F16_F32,
  377. GGML_METAL_KERNEL_TYPE_CPY_BF16_F32,
  378. GGML_METAL_KERNEL_TYPE_CPY_BF16_BF16,
  379. GGML_METAL_KERNEL_TYPE_CPY_F32_Q8_0,
  380. GGML_METAL_KERNEL_TYPE_CPY_F32_Q4_0,
  381. GGML_METAL_KERNEL_TYPE_CPY_F32_Q4_1,
  382. GGML_METAL_KERNEL_TYPE_CPY_F32_Q5_0,
  383. GGML_METAL_KERNEL_TYPE_CPY_F32_Q5_1,
  384. GGML_METAL_KERNEL_TYPE_CPY_F32_IQ4_NL,
  385. GGML_METAL_KERNEL_TYPE_CONCAT,
  386. GGML_METAL_KERNEL_TYPE_SQR,
  387. GGML_METAL_KERNEL_TYPE_SQRT,
  388. GGML_METAL_KERNEL_TYPE_SIN,
  389. GGML_METAL_KERNEL_TYPE_COS,
  390. GGML_METAL_KERNEL_TYPE_SUM_ROWS,
  391. GGML_METAL_KERNEL_TYPE_POOL_2D_AVG_F32,
  392. GGML_METAL_KERNEL_TYPE_POOL_2D_MAX_F32,
  393. GGML_METAL_KERNEL_TYPE_ARGMAX,
  394. GGML_METAL_KERNEL_TYPE_COUNT
  395. };
  396. struct ggml_backend_metal_context {
  397. id<MTLCommandQueue> queue;
  398. dispatch_queue_t d_queue;
  399. struct ggml_metal_kernel kernels[GGML_METAL_KERNEL_TYPE_COUNT];
  400. // capture state
  401. bool capture_next_compute;
  402. bool capture_started;
  403. id<MTLCaptureScope> capture_scope;
  404. // command buffer state
  405. int n_cb; // number of extra threads used to submit the command buffers
  406. int n_nodes_0; // number of nodes submitted by the main thread
  407. int n_nodes_1; // remaining number of nodes submitted by the n_cb threads
  408. int n_nodes_per_cb;
  409. struct ggml_cgraph * gf;
  410. // the callback given to the thread pool
  411. void (^encode_async)(size_t ith);
  412. // n_cb command buffers + 1 used by the main thread
  413. id<MTLCommandBuffer> command_buffers[GGML_METAL_MAX_COMMAND_BUFFERS + 1];
  414. // abort ggml_metal_graph_compute if callback returns true
  415. ggml_abort_callback abort_callback;
  416. void * abort_callback_data;
  417. };
  418. // MSL code
  419. // TODO: move the contents here when ready
  420. // for now it is easier to work in a separate file
  421. // static NSString * const msl_library_source = @"see metal.metal";
  422. // Here to assist with NSBundle Path Hack
  423. @interface GGMLMetalClass : NSObject
  424. @end
  425. @implementation GGMLMetalClass
  426. @end
  427. static void * ggml_metal_host_malloc(size_t n) {
  428. void * data = NULL;
  429. #if TARGET_OS_OSX
  430. kern_return_t err = vm_allocate((vm_map_t) mach_task_self(), (void *) &data, n, VM_FLAGS_ANYWHERE);
  431. if (err != KERN_SUCCESS) {
  432. GGML_LOG_ERROR("%s: error: vm_allocate failed\n", __func__);
  433. return NULL;
  434. }
  435. #else
  436. const int result = posix_memalign((void **) &data, sysconf(_SC_PAGESIZE), n);
  437. if (result != 0) {
  438. GGML_LOG_ERROR("%s: error: posix_memalign failed\n", __func__);
  439. return NULL;
  440. }
  441. #endif
  442. return data;
  443. }
  444. static struct ggml_backend_metal_context * ggml_metal_init(ggml_backend_dev_t dev) {
  445. GGML_LOG_INFO("%s: allocating\n", __func__);
  446. #if TARGET_OS_OSX && !GGML_METAL_NDEBUG
  447. // Show all the Metal device instances in the system
  448. NSArray * devices = MTLCopyAllDevices();
  449. for (id<MTLDevice> device in devices) {
  450. GGML_LOG_INFO("%s: found device: %s\n", __func__, [[device name] UTF8String]);
  451. }
  452. [devices release]; // since it was created by a *Copy* C method
  453. #endif
  454. // init context
  455. struct ggml_backend_metal_context * ctx = calloc(1, sizeof(struct ggml_backend_metal_context));
  456. struct ggml_backend_metal_device_context * ctx_dev = dev->context;
  457. id<MTLDevice> device = ggml_backend_metal_device_acq(ctx_dev);
  458. GGML_LOG_INFO("%s: picking default device: %s\n", __func__, [[device name] UTF8String]);
  459. ctx->queue = [device newCommandQueue];
  460. ctx->d_queue = dispatch_queue_create("ggml-metal", DISPATCH_QUEUE_CONCURRENT);
  461. id<MTLLibrary> metal_library;
  462. // load library
  463. //
  464. // - first check if the library is embedded
  465. // - then check if the library is in the bundle
  466. // - if not found, load the source and compile it
  467. // - if that fails, return NULL
  468. {
  469. NSBundle * bundle = nil;
  470. #ifdef SWIFT_PACKAGE
  471. bundle = SWIFTPM_MODULE_BUNDLE;
  472. #else
  473. bundle = [NSBundle bundleForClass:[GGMLMetalClass class]];
  474. #endif
  475. NSError * error = nil;
  476. #if GGML_METAL_EMBED_LIBRARY
  477. const bool try_metallib = false;
  478. #else
  479. const bool try_metallib = true;
  480. #endif
  481. NSString * path_lib = [bundle pathForResource:@"default" ofType:@"metallib"];
  482. if (try_metallib && path_lib != nil) {
  483. // pre-compiled library found
  484. NSURL * libURL = [NSURL fileURLWithPath:path_lib];
  485. GGML_LOG_INFO("%s: loading '%s'\n", __func__, [path_lib UTF8String]);
  486. metal_library = [device newLibraryWithURL:libURL error:&error];
  487. if (error) {
  488. GGML_LOG_ERROR("%s: error: %s\n", __func__, [[error description] UTF8String]);
  489. return NULL;
  490. }
  491. } else {
  492. #if GGML_METAL_EMBED_LIBRARY
  493. GGML_LOG_INFO("%s: using embedded metal library\n", __func__);
  494. extern const char ggml_metallib_start[];
  495. extern const char ggml_metallib_end[];
  496. NSString * src = [[NSString alloc] initWithBytes:ggml_metallib_start length:(ggml_metallib_end-ggml_metallib_start) encoding:NSUTF8StringEncoding];
  497. #else
  498. GGML_LOG_INFO("%s: default.metallib not found, loading from source\n", __func__);
  499. NSString * path_source;
  500. NSString * path_resource = [[NSProcessInfo processInfo].environment objectForKey:@"GGML_METAL_PATH_RESOURCES"];
  501. GGML_LOG_INFO("%s: GGML_METAL_PATH_RESOURCES = %s\n", __func__, path_resource ? [path_resource UTF8String] : "nil");
  502. if (path_resource) {
  503. path_source = [path_resource stringByAppendingPathComponent:@"ggml-metal.metal"];
  504. } else {
  505. path_source = [bundle pathForResource:@"ggml-metal" ofType:@"metal"];
  506. }
  507. if (path_source == nil) {
  508. GGML_LOG_WARN("%s: error: could not use bundle path to find ggml-metal.metal, falling back to trying cwd\n", __func__);
  509. path_source = @"ggml-metal.metal";
  510. }
  511. GGML_LOG_INFO("%s: loading '%s'\n", __func__, [path_source UTF8String]);
  512. NSString * src = [NSString stringWithContentsOfFile:path_source encoding:NSUTF8StringEncoding error:&error];
  513. if (error) {
  514. GGML_LOG_ERROR("%s: error: %s\n", __func__, [[error description] UTF8String]);
  515. return NULL;
  516. }
  517. #endif // GGML_METAL_EMBED_LIBRARY
  518. @autoreleasepool {
  519. // dictionary of preprocessor macros
  520. NSMutableDictionary * prep = [NSMutableDictionary dictionary];
  521. if (ctx_dev->use_bfloat) {
  522. [prep setObject:@"1" forKey:@"GGML_METAL_USE_BF16"];
  523. }
  524. #if GGML_METAL_EMBED_LIBRARY
  525. [prep setObject:@"1" forKey:@"GGML_METAL_EMBED_LIBRARY"];
  526. #endif
  527. MTLCompileOptions * options = [MTLCompileOptions new];
  528. options.preprocessorMacros = prep;
  529. //[options setFastMathEnabled:false];
  530. metal_library = [device newLibraryWithSource:src options:options error:&error];
  531. if (error) {
  532. GGML_LOG_ERROR("%s: error: %s\n", __func__, [[error description] UTF8String]);
  533. return NULL;
  534. }
  535. #if !__has_feature(objc_arc)
  536. [options release];
  537. #endif
  538. }
  539. #if GGML_METAL_EMBED_LIBRARY
  540. [src release];
  541. #endif // GGML_METAL_EMBED_LIBRARY
  542. }
  543. }
  544. // print MTL GPU family:
  545. GGML_LOG_INFO("%s: GPU name: %s\n", __func__, [[device name] UTF8String]);
  546. // determine max supported GPU family
  547. // https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf
  548. // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf
  549. {
  550. for (int i = MTLGPUFamilyApple1 + 20; i >= MTLGPUFamilyApple1; --i) {
  551. if ([device supportsFamily:i]) {
  552. GGML_LOG_INFO("%s: GPU family: MTLGPUFamilyApple%d (%d)\n", __func__, i - (int) MTLGPUFamilyApple1 + 1, i);
  553. break;
  554. }
  555. }
  556. for (int i = MTLGPUFamilyCommon1 + 5; i >= MTLGPUFamilyCommon1; --i) {
  557. if ([device supportsFamily:i]) {
  558. GGML_LOG_INFO("%s: GPU family: MTLGPUFamilyCommon%d (%d)\n", __func__, i - (int) MTLGPUFamilyCommon1 + 1, i);
  559. break;
  560. }
  561. }
  562. for (int i = MTLGPUFamilyMetal3_GGML + 5; i >= MTLGPUFamilyMetal3_GGML; --i) {
  563. if ([device supportsFamily:i]) {
  564. GGML_LOG_INFO("%s: GPU family: MTLGPUFamilyMetal%d (%d)\n", __func__, i - (int) MTLGPUFamilyMetal3_GGML + 3, i);
  565. break;
  566. }
  567. }
  568. }
  569. GGML_LOG_INFO("%s: simdgroup reduction = %s\n", __func__, ctx_dev->has_simdgroup_reduction ? "true" : "false");
  570. GGML_LOG_INFO("%s: simdgroup matrix mul. = %s\n", __func__, ctx_dev->has_simdgroup_mm ? "true" : "false");
  571. GGML_LOG_INFO("%s: has bfloat = %s\n", __func__, ctx_dev->has_bfloat ? "true" : "false");
  572. GGML_LOG_INFO("%s: use bfloat = %s\n", __func__, ctx_dev->use_bfloat ? "true" : "false");
  573. GGML_LOG_INFO("%s: hasUnifiedMemory = %s\n", __func__, ctx_dev->mtl_device.hasUnifiedMemory ? "true" : "false");
  574. ctx->capture_next_compute = false;
  575. ctx->capture_started = false;
  576. ctx->capture_scope = nil;
  577. ctx->gf = nil;
  578. ctx->encode_async = nil;
  579. for (int i = 0; i < GGML_METAL_MAX_COMMAND_BUFFERS; ++i) {
  580. ctx->command_buffers[i] = nil;
  581. }
  582. #if TARGET_OS_OSX || (TARGET_OS_IOS && __clang_major__ >= 15)
  583. if (@available(macOS 10.12, iOS 16.0, *)) {
  584. GGML_LOG_INFO("%s: recommendedMaxWorkingSetSize = %8.2f MB\n", __func__, device.recommendedMaxWorkingSetSize / 1e6);
  585. }
  586. #endif
  587. // load kernels
  588. {
  589. NSError * error = nil;
  590. for (int i = 0; i < GGML_METAL_KERNEL_TYPE_COUNT; ++i) {
  591. ctx->kernels[i].pipeline = nil;
  592. }
  593. #define GGML_METAL_ADD_KERNEL(e, name, supported) \
  594. if (supported) { \
  595. struct ggml_metal_kernel * kernel = &ctx->kernels[e]; \
  596. id<MTLFunction> metal_function = [metal_library newFunctionWithName:@"kernel_"#name]; \
  597. kernel->pipeline = [device newComputePipelineStateWithFunction:metal_function error:&error]; \
  598. GGML_LOG_DEBUG("%s: loaded %-40s %16p | th_max = %4d | th_width = %4d\n", __func__, "kernel_"#name, (void *) kernel->pipeline, \
  599. (int) kernel->pipeline.maxTotalThreadsPerThreadgroup, \
  600. (int) kernel->pipeline.threadExecutionWidth); \
  601. [metal_function release]; \
  602. if (error) { \
  603. GGML_LOG_ERROR("%s: error: load pipeline error: %s\n", __func__, [[error description] UTF8String]); \
  604. [metal_library release]; \
  605. return NULL; \
  606. } \
  607. } else { \
  608. GGML_LOG_WARN("%s: skipping %-40s (not supported)\n", __func__, "kernel_"#name); \
  609. }
  610. const bool has_simdgroup_mm = ctx_dev->has_simdgroup_mm;
  611. const bool has_simdgroup_reduction = ctx_dev->has_simdgroup_reduction;
  612. const bool use_bfloat = ctx_dev->use_bfloat;
  613. // simd_sum and simd_max requires MTLGPUFamilyApple7
  614. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_ADD, add, true);
  615. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_ADD_ROW, add_row, true);
  616. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_SUB, sub, true);
  617. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_SUB_ROW, sub_row, true);
  618. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL, mul, true);
  619. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_ROW, mul_row, true);
  620. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_DIV, div, true);
  621. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_DIV_ROW, div_row, true);
  622. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_REPEAT_F32, repeat_f32, true);
  623. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_REPEAT_F16, repeat_f16, true);
  624. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_REPEAT_I32, repeat_i32, true);
  625. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_REPEAT_I16, repeat_i16, true);
  626. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_SCALE, scale, true);
  627. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_SCALE_4, scale_4, true);
  628. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_CLAMP, clamp, true);
  629. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_TANH, tanh, true);
  630. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_RELU, relu, true);
  631. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_SIGMOID, sigmoid, true);
  632. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GELU, gelu, true);
  633. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GELU_4, gelu_4, true);
  634. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GELU_QUICK, gelu_quick, true);
  635. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GELU_QUICK_4, gelu_quick_4, true);
  636. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_SILU, silu, true);
  637. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_SILU_4, silu_4, true);
  638. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_ELU, elu, true);
  639. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_SOFT_MAX_F16, soft_max_f16, has_simdgroup_reduction);
  640. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_SOFT_MAX_F16_4, soft_max_f16_4, has_simdgroup_reduction);
  641. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_SOFT_MAX_F32, soft_max_f32, has_simdgroup_reduction);
  642. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_SOFT_MAX_F32_4, soft_max_f32_4, has_simdgroup_reduction);
  643. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_DIAG_MASK_INF, diag_mask_inf, true);
  644. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_DIAG_MASK_INF_8, diag_mask_inf_8, true);
  645. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_F32, get_rows_f32, true);
  646. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_F16, get_rows_f16, true);
  647. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_BF16, get_rows_bf16, use_bfloat);
  648. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_Q4_0, get_rows_q4_0, true);
  649. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_Q4_1, get_rows_q4_1, true);
  650. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_Q5_0, get_rows_q5_0, true);
  651. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_Q5_1, get_rows_q5_1, true);
  652. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_Q8_0, get_rows_q8_0, true);
  653. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_Q2_K, get_rows_q2_K, true);
  654. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_Q3_K, get_rows_q3_K, true);
  655. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_Q4_K, get_rows_q4_K, true);
  656. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_Q5_K, get_rows_q5_K, true);
  657. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_Q6_K, get_rows_q6_K, true);
  658. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ2_XXS, get_rows_iq2_xxs, true);
  659. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ2_XS, get_rows_iq2_xs, true);
  660. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ3_XXS, get_rows_iq3_xxs, true);
  661. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ3_S, get_rows_iq3_s, true);
  662. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ2_S, get_rows_iq2_s, true);
  663. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ1_S, get_rows_iq1_s, true);
  664. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ1_M, get_rows_iq1_m, true);
  665. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ4_NL, get_rows_iq4_nl, true);
  666. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ4_XS, get_rows_iq4_xs, true);
  667. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_I32, get_rows_i32, true);
  668. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_RMS_NORM, rms_norm, has_simdgroup_reduction);
  669. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GROUP_NORM, group_norm, has_simdgroup_reduction);
  670. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_NORM, norm, true);
  671. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_SSM_CONV_F32, ssm_conv_f32, true);
  672. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_SSM_SCAN_F32, ssm_scan_f32, true);
  673. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_F32_F32, mul_mv_f32_f32, has_simdgroup_reduction);
  674. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_BF16_F32, mul_mv_bf16_f32, has_simdgroup_reduction && use_bfloat);
  675. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_BF16_F32_1ROW, mul_mv_bf16_f32_1row, has_simdgroup_reduction && use_bfloat);
  676. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_BF16_F32_L4, mul_mv_bf16_f32_l4, has_simdgroup_reduction && use_bfloat);
  677. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_BF16_BF16, mul_mv_bf16_bf16, has_simdgroup_reduction && use_bfloat);
  678. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_F16_F32, mul_mv_f16_f32, has_simdgroup_reduction);
  679. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_F16_F32_1ROW, mul_mv_f16_f32_1row, has_simdgroup_reduction);
  680. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_F16_F32_L4, mul_mv_f16_f32_l4, has_simdgroup_reduction);
  681. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_F16_F16, mul_mv_f16_f16, has_simdgroup_reduction);
  682. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_Q4_0_F32, mul_mv_q4_0_f32, has_simdgroup_reduction);
  683. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_Q4_1_F32, mul_mv_q4_1_f32, has_simdgroup_reduction);
  684. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_Q5_0_F32, mul_mv_q5_0_f32, has_simdgroup_reduction);
  685. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_Q5_1_F32, mul_mv_q5_1_f32, has_simdgroup_reduction);
  686. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_Q8_0_F32, mul_mv_q8_0_f32, has_simdgroup_reduction);
  687. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_F16_F32_R1_2, mul_mv_ext_f16_f32_r1_2, has_simdgroup_reduction);
  688. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_F16_F32_R1_3, mul_mv_ext_f16_f32_r1_3, has_simdgroup_reduction);
  689. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_F16_F32_R1_4, mul_mv_ext_f16_f32_r1_4, has_simdgroup_reduction);
  690. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_F16_F32_R1_5, mul_mv_ext_f16_f32_r1_5, has_simdgroup_reduction);
  691. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q4_0_F32_R1_2, mul_mv_ext_q4_0_f32_r1_2, has_simdgroup_reduction);
  692. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q4_0_F32_R1_3, mul_mv_ext_q4_0_f32_r1_3, has_simdgroup_reduction);
  693. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q4_0_F32_R1_4, mul_mv_ext_q4_0_f32_r1_4, has_simdgroup_reduction);
  694. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q4_0_F32_R1_5, mul_mv_ext_q4_0_f32_r1_5, has_simdgroup_reduction);
  695. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q4_1_F32_R1_2, mul_mv_ext_q4_1_f32_r1_2, has_simdgroup_reduction);
  696. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q4_1_F32_R1_3, mul_mv_ext_q4_1_f32_r1_3, has_simdgroup_reduction);
  697. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q4_1_F32_R1_4, mul_mv_ext_q4_1_f32_r1_4, has_simdgroup_reduction);
  698. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q4_1_F32_R1_5, mul_mv_ext_q4_1_f32_r1_5, has_simdgroup_reduction);
  699. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q5_0_F32_R1_2, mul_mv_ext_q5_0_f32_r1_2, has_simdgroup_reduction);
  700. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q5_0_F32_R1_3, mul_mv_ext_q5_0_f32_r1_3, has_simdgroup_reduction);
  701. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q5_0_F32_R1_4, mul_mv_ext_q5_0_f32_r1_4, has_simdgroup_reduction);
  702. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q5_0_F32_R1_5, mul_mv_ext_q5_0_f32_r1_5, has_simdgroup_reduction);
  703. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q5_1_F32_R1_2, mul_mv_ext_q5_1_f32_r1_2, has_simdgroup_reduction);
  704. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q5_1_F32_R1_3, mul_mv_ext_q5_1_f32_r1_3, has_simdgroup_reduction);
  705. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q5_1_F32_R1_4, mul_mv_ext_q5_1_f32_r1_4, has_simdgroup_reduction);
  706. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q5_1_F32_R1_5, mul_mv_ext_q5_1_f32_r1_5, has_simdgroup_reduction);
  707. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q8_0_F32_R1_2, mul_mv_ext_q8_0_f32_r1_2, has_simdgroup_reduction);
  708. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q8_0_F32_R1_3, mul_mv_ext_q8_0_f32_r1_3, has_simdgroup_reduction);
  709. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q8_0_F32_R1_4, mul_mv_ext_q8_0_f32_r1_4, has_simdgroup_reduction);
  710. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q8_0_F32_R1_5, mul_mv_ext_q8_0_f32_r1_5, has_simdgroup_reduction);
  711. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q4_K_F32_R1_2, mul_mv_ext_q4_K_f32_r1_2, has_simdgroup_reduction);
  712. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q4_K_F32_R1_3, mul_mv_ext_q4_K_f32_r1_3, has_simdgroup_reduction);
  713. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q4_K_F32_R1_4, mul_mv_ext_q4_K_f32_r1_4, has_simdgroup_reduction);
  714. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q4_K_F32_R1_5, mul_mv_ext_q4_K_f32_r1_5, has_simdgroup_reduction);
  715. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q5_K_F32_R1_2, mul_mv_ext_q5_K_f32_r1_2, has_simdgroup_reduction);
  716. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q5_K_F32_R1_3, mul_mv_ext_q5_K_f32_r1_3, has_simdgroup_reduction);
  717. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q5_K_F32_R1_4, mul_mv_ext_q5_K_f32_r1_4, has_simdgroup_reduction);
  718. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q5_K_F32_R1_5, mul_mv_ext_q5_K_f32_r1_5, has_simdgroup_reduction);
  719. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q6_K_F32_R1_2, mul_mv_ext_q6_K_f32_r1_2, has_simdgroup_reduction);
  720. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q6_K_F32_R1_3, mul_mv_ext_q6_K_f32_r1_3, has_simdgroup_reduction);
  721. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q6_K_F32_R1_4, mul_mv_ext_q6_K_f32_r1_4, has_simdgroup_reduction);
  722. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q6_K_F32_R1_5, mul_mv_ext_q6_K_f32_r1_5, has_simdgroup_reduction);
  723. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_IQ4_NL_F32_R1_2, mul_mv_ext_iq4_nl_f32_r1_2, has_simdgroup_reduction);
  724. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_IQ4_NL_F32_R1_3, mul_mv_ext_iq4_nl_f32_r1_3, has_simdgroup_reduction);
  725. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_IQ4_NL_F32_R1_4, mul_mv_ext_iq4_nl_f32_r1_4, has_simdgroup_reduction);
  726. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_IQ4_NL_F32_R1_5, mul_mv_ext_iq4_nl_f32_r1_5, has_simdgroup_reduction);
  727. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_Q2_K_F32, mul_mv_q2_K_f32, has_simdgroup_reduction);
  728. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_Q3_K_F32, mul_mv_q3_K_f32, has_simdgroup_reduction);
  729. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_Q4_K_F32, mul_mv_q4_K_f32, has_simdgroup_reduction);
  730. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_Q5_K_F32, mul_mv_q5_K_f32, has_simdgroup_reduction);
  731. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_Q6_K_F32, mul_mv_q6_K_f32, has_simdgroup_reduction);
  732. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_IQ2_XXS_F32, mul_mv_iq2_xxs_f32, has_simdgroup_reduction);
  733. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_IQ2_XS_F32, mul_mv_iq2_xs_f32, has_simdgroup_reduction);
  734. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_IQ3_XXS_F32, mul_mv_iq3_xxs_f32, has_simdgroup_reduction);
  735. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_IQ3_S_F32, mul_mv_iq3_s_f32, has_simdgroup_reduction);
  736. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_IQ2_S_F32, mul_mv_iq2_s_f32, has_simdgroup_reduction);
  737. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_IQ1_S_F32, mul_mv_iq1_s_f32, has_simdgroup_reduction);
  738. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_IQ1_M_F32, mul_mv_iq1_m_f32, has_simdgroup_reduction);
  739. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_IQ4_NL_F32, mul_mv_iq4_nl_f32, has_simdgroup_reduction);
  740. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_IQ4_XS_F32, mul_mv_iq4_xs_f32, has_simdgroup_reduction);
  741. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_F32_F32, mul_mv_id_f32_f32, has_simdgroup_reduction);
  742. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_F16_F32, mul_mv_id_f16_f32, has_simdgroup_reduction);
  743. //GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_F16_F32_1ROW, mul_mv_id_f16_f32_1row, has_simdgroup_reduction);
  744. //GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_F16_F32_L4, mul_mv_id_f16_f32_l4, has_simdgroup_reduction);
  745. //GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_F16_F16, mul_mv_id_f16_f16, has_simdgroup_reduction);
  746. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_BF16_F32, mul_mv_id_bf16_f32, has_simdgroup_reduction && use_bfloat);
  747. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q4_0_F32, mul_mv_id_q4_0_f32, has_simdgroup_reduction);
  748. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q4_1_F32, mul_mv_id_q4_1_f32, has_simdgroup_reduction);
  749. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q5_0_F32, mul_mv_id_q5_0_f32, has_simdgroup_reduction);
  750. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q5_1_F32, mul_mv_id_q5_1_f32, has_simdgroup_reduction);
  751. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q8_0_F32, mul_mv_id_q8_0_f32, has_simdgroup_reduction);
  752. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q2_K_F32, mul_mv_id_q2_K_f32, has_simdgroup_reduction);
  753. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q3_K_F32, mul_mv_id_q3_K_f32, has_simdgroup_reduction);
  754. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q4_K_F32, mul_mv_id_q4_K_f32, has_simdgroup_reduction);
  755. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q5_K_F32, mul_mv_id_q5_K_f32, has_simdgroup_reduction);
  756. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q6_K_F32, mul_mv_id_q6_K_f32, has_simdgroup_reduction);
  757. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ2_XXS_F32, mul_mv_id_iq2_xxs_f32, has_simdgroup_reduction);
  758. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ2_XS_F32, mul_mv_id_iq2_xs_f32, has_simdgroup_reduction);
  759. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ3_XXS_F32, mul_mv_id_iq3_xxs_f32, has_simdgroup_reduction);
  760. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ3_S_F32, mul_mv_id_iq3_s_f32, has_simdgroup_reduction);
  761. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ2_S_F32, mul_mv_id_iq2_s_f32, has_simdgroup_reduction);
  762. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ1_S_F32, mul_mv_id_iq1_s_f32, has_simdgroup_reduction);
  763. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ1_M_F32, mul_mv_id_iq1_m_f32, has_simdgroup_reduction);
  764. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ4_NL_F32, mul_mv_id_iq4_nl_f32, has_simdgroup_reduction);
  765. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ4_XS_F32, mul_mv_id_iq4_xs_f32, has_simdgroup_reduction);
  766. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_F32_F32, mul_mm_f32_f32, has_simdgroup_mm);
  767. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_F16_F32, mul_mm_f16_f32, has_simdgroup_mm);
  768. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_BF16_F32, mul_mm_bf16_f32, has_simdgroup_mm && use_bfloat);
  769. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_Q4_0_F32, mul_mm_q4_0_f32, has_simdgroup_mm);
  770. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_Q4_1_F32, mul_mm_q4_1_f32, has_simdgroup_mm);
  771. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_Q5_0_F32, mul_mm_q5_0_f32, has_simdgroup_mm);
  772. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_Q5_1_F32, mul_mm_q5_1_f32, has_simdgroup_mm);
  773. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_Q8_0_F32, mul_mm_q8_0_f32, has_simdgroup_mm);
  774. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_Q2_K_F32, mul_mm_q2_K_f32, has_simdgroup_mm);
  775. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_Q3_K_F32, mul_mm_q3_K_f32, has_simdgroup_mm);
  776. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_Q4_K_F32, mul_mm_q4_K_f32, has_simdgroup_mm);
  777. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_Q5_K_F32, mul_mm_q5_K_f32, has_simdgroup_mm);
  778. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_Q6_K_F32, mul_mm_q6_K_f32, has_simdgroup_mm);
  779. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_IQ2_XXS_F32, mul_mm_iq2_xxs_f32, has_simdgroup_mm);
  780. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_IQ2_XS_F32, mul_mm_iq2_xs_f32, has_simdgroup_mm);
  781. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_IQ3_XXS_F32, mul_mm_iq3_xxs_f32, has_simdgroup_mm);
  782. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_IQ3_S_F32, mul_mm_iq3_s_f32, has_simdgroup_mm);
  783. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_IQ2_S_F32, mul_mm_iq2_s_f32, has_simdgroup_mm);
  784. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_IQ1_S_F32, mul_mm_iq1_s_f32, has_simdgroup_mm);
  785. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_IQ1_M_F32, mul_mm_iq1_m_f32, has_simdgroup_mm);
  786. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_IQ4_NL_F32, mul_mm_iq4_nl_f32, has_simdgroup_mm);
  787. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_IQ4_XS_F32, mul_mm_iq4_xs_f32, has_simdgroup_mm);
  788. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_F32_F32, mul_mm_id_f32_f32, has_simdgroup_mm);
  789. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_F16_F32, mul_mm_id_f16_f32, has_simdgroup_mm);
  790. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_BF16_F32, mul_mm_id_bf16_f32, has_simdgroup_mm && use_bfloat);
  791. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q4_0_F32, mul_mm_id_q4_0_f32, has_simdgroup_mm);
  792. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q4_1_F32, mul_mm_id_q4_1_f32, has_simdgroup_mm);
  793. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q5_0_F32, mul_mm_id_q5_0_f32, has_simdgroup_mm);
  794. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q5_1_F32, mul_mm_id_q5_1_f32, has_simdgroup_mm);
  795. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q8_0_F32, mul_mm_id_q8_0_f32, has_simdgroup_mm);
  796. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q2_K_F32, mul_mm_id_q2_K_f32, has_simdgroup_mm);
  797. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q3_K_F32, mul_mm_id_q3_K_f32, has_simdgroup_mm);
  798. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q4_K_F32, mul_mm_id_q4_K_f32, has_simdgroup_mm);
  799. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q5_K_F32, mul_mm_id_q5_K_f32, has_simdgroup_mm);
  800. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q6_K_F32, mul_mm_id_q6_K_f32, has_simdgroup_mm);
  801. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ2_XXS_F32, mul_mm_id_iq2_xxs_f32, has_simdgroup_mm);
  802. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ2_XS_F32, mul_mm_id_iq2_xs_f32, has_simdgroup_mm);
  803. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ3_XXS_F32, mul_mm_id_iq3_xxs_f32, has_simdgroup_mm);
  804. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ3_S_F32, mul_mm_id_iq3_s_f32, has_simdgroup_mm);
  805. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ2_S_F32, mul_mm_id_iq2_s_f32, has_simdgroup_mm);
  806. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ1_S_F32, mul_mm_id_iq1_s_f32, has_simdgroup_mm);
  807. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ1_M_F32, mul_mm_id_iq1_m_f32, has_simdgroup_mm);
  808. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ4_NL_F32, mul_mm_id_iq4_nl_f32, has_simdgroup_mm);
  809. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ4_XS_F32, mul_mm_id_iq4_xs_f32, has_simdgroup_mm);
  810. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_ROPE_NORM_F32, rope_norm_f32, true);
  811. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_ROPE_NORM_F16, rope_norm_f16, true);
  812. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_ROPE_NEOX_F32, rope_neox_f32, true);
  813. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_ROPE_NEOX_F16, rope_neox_f16, true);
  814. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_IM2COL_F16, im2col_f16, true);
  815. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_IM2COL_F32, im2col_f32, true);
  816. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_IM2COL_EXT_F16, im2col_ext_f16, true);
  817. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_IM2COL_EXT_F32, im2col_ext_f32, true);
  818. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_CONV_TRANSPOSE_1D_F32_F32, conv_transpose_1d_f32_f32, true);
  819. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_CONV_TRANSPOSE_1D_F16_F32, conv_transpose_1d_f16_f32, true);
  820. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_UPSCALE_F32, upscale_f32, true);
  821. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_PAD_F32, pad_f32, true);
  822. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_UNPAD_F32, unpad_f32, true);
  823. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_TIMESTEP_EMBEDDING_F32, timestep_embedding_f32, true);
  824. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_ARANGE_F32, arange_f32, true);
  825. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_ARGSORT_F32_I32_ASC, argsort_f32_i32_asc, true);
  826. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_ARGSORT_F32_I32_DESC, argsort_f32_i32_desc, true);
  827. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_LEAKY_RELU_F32, leaky_relu_f32, true);
  828. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_F16_H64, flash_attn_ext_f16_h64, has_simdgroup_mm);
  829. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_F16_H80, flash_attn_ext_f16_h80, has_simdgroup_mm);
  830. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_F16_H96, flash_attn_ext_f16_h96, has_simdgroup_mm);
  831. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_F16_H112, flash_attn_ext_f16_h112, has_simdgroup_mm);
  832. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_F16_H128, flash_attn_ext_f16_h128, has_simdgroup_mm);
  833. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_F16_H256, flash_attn_ext_f16_h256, has_simdgroup_mm);
  834. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_BF16_H64, flash_attn_ext_bf16_h64, has_simdgroup_mm && use_bfloat);
  835. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_BF16_H80, flash_attn_ext_bf16_h80, has_simdgroup_mm && use_bfloat);
  836. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_BF16_H96, flash_attn_ext_bf16_h96, has_simdgroup_mm && use_bfloat);
  837. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_BF16_H112, flash_attn_ext_bf16_h112, has_simdgroup_mm && use_bfloat);
  838. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_BF16_H128, flash_attn_ext_bf16_h128, has_simdgroup_mm && use_bfloat);
  839. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_BF16_H256, flash_attn_ext_bf16_h256, has_simdgroup_mm && use_bfloat);
  840. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q4_0_H64, flash_attn_ext_q4_0_h64, has_simdgroup_mm);
  841. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q4_0_H80, flash_attn_ext_q4_0_h80, has_simdgroup_mm);
  842. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q4_0_H96, flash_attn_ext_q4_0_h96, has_simdgroup_mm);
  843. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q4_0_H112, flash_attn_ext_q4_0_h112, has_simdgroup_mm);
  844. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q4_0_H128, flash_attn_ext_q4_0_h128, has_simdgroup_mm);
  845. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q4_0_H256, flash_attn_ext_q4_0_h256, has_simdgroup_mm);
  846. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q4_1_H64, flash_attn_ext_q4_1_h64, has_simdgroup_mm);
  847. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q4_1_H80, flash_attn_ext_q4_1_h80, has_simdgroup_mm);
  848. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q4_1_H96, flash_attn_ext_q4_1_h96, has_simdgroup_mm);
  849. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q4_1_H112, flash_attn_ext_q4_1_h112, has_simdgroup_mm);
  850. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q4_1_H128, flash_attn_ext_q4_1_h128, has_simdgroup_mm);
  851. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q4_1_H256, flash_attn_ext_q4_1_h256, has_simdgroup_mm);
  852. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q5_0_H64, flash_attn_ext_q5_0_h64, has_simdgroup_mm);
  853. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q5_0_H80, flash_attn_ext_q5_0_h80, has_simdgroup_mm);
  854. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q5_0_H96, flash_attn_ext_q5_0_h96, has_simdgroup_mm);
  855. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q5_0_H112, flash_attn_ext_q5_0_h112, has_simdgroup_mm);
  856. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q5_0_H128, flash_attn_ext_q5_0_h128, has_simdgroup_mm);
  857. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q5_0_H256, flash_attn_ext_q5_0_h256, has_simdgroup_mm);
  858. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q5_1_H64, flash_attn_ext_q5_1_h64, has_simdgroup_mm);
  859. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q5_1_H80, flash_attn_ext_q5_1_h80, has_simdgroup_mm);
  860. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q5_1_H96, flash_attn_ext_q5_1_h96, has_simdgroup_mm);
  861. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q5_1_H112, flash_attn_ext_q5_1_h112, has_simdgroup_mm);
  862. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q5_1_H128, flash_attn_ext_q5_1_h128, has_simdgroup_mm);
  863. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q5_1_H256, flash_attn_ext_q5_1_h256, has_simdgroup_mm);
  864. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q8_0_H64, flash_attn_ext_q8_0_h64, has_simdgroup_mm);
  865. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q8_0_H80, flash_attn_ext_q8_0_h80, has_simdgroup_mm);
  866. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q8_0_H96, flash_attn_ext_q8_0_h96, has_simdgroup_mm);
  867. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q8_0_H112, flash_attn_ext_q8_0_h112, has_simdgroup_mm);
  868. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q8_0_H128, flash_attn_ext_q8_0_h128, has_simdgroup_mm);
  869. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q8_0_H256, flash_attn_ext_q8_0_h256, has_simdgroup_mm);
  870. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_VEC_F16_H128, flash_attn_ext_vec_f16_h128, has_simdgroup_reduction);
  871. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_VEC_BF16_H128, flash_attn_ext_vec_bf16_h128, has_simdgroup_reduction && use_bfloat);
  872. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_VEC_Q4_0_H128, flash_attn_ext_vec_q4_0_h128, has_simdgroup_reduction);
  873. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_VEC_Q4_1_H128, flash_attn_ext_vec_q4_1_h128, has_simdgroup_reduction);
  874. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_VEC_Q5_0_H128, flash_attn_ext_vec_q5_0_h128, has_simdgroup_reduction);
  875. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_VEC_Q5_1_H128, flash_attn_ext_vec_q5_1_h128, has_simdgroup_reduction);
  876. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_VEC_Q8_0_H128, flash_attn_ext_vec_q8_0_h128, has_simdgroup_reduction);
  877. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_VEC_F16_H256, flash_attn_ext_vec_f16_h256, has_simdgroup_reduction);
  878. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_VEC_BF16_H256, flash_attn_ext_vec_bf16_h256, has_simdgroup_reduction && use_bfloat);
  879. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_VEC_Q4_0_H256, flash_attn_ext_vec_q4_0_h256, has_simdgroup_reduction);
  880. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_VEC_Q4_1_H256, flash_attn_ext_vec_q4_1_h256, has_simdgroup_reduction);
  881. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_VEC_Q5_0_H256, flash_attn_ext_vec_q5_0_h256, has_simdgroup_reduction);
  882. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_VEC_Q5_1_H256, flash_attn_ext_vec_q5_1_h256, has_simdgroup_reduction);
  883. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_VEC_Q8_0_H256, flash_attn_ext_vec_q8_0_h256, has_simdgroup_reduction);
  884. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_CPY_F32_F32, cpy_f32_f32, true);
  885. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_CPY_F32_F16, cpy_f32_f16, true);
  886. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_CPY_F32_BF16, cpy_f32_bf16, use_bfloat);
  887. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_CPY_F16_F32, cpy_f16_f32, true);
  888. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_CPY_F16_F16, cpy_f16_f16, true);
  889. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_CPY_BF16_F32, cpy_bf16_f32, use_bfloat);
  890. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_CPY_BF16_BF16, cpy_bf16_bf16, use_bfloat);
  891. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_CPY_F32_Q8_0, cpy_f32_q8_0, true);
  892. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_CPY_F32_Q4_0, cpy_f32_q4_0, true);
  893. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_CPY_F32_Q4_1, cpy_f32_q4_1, true);
  894. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_CPY_F32_Q5_0, cpy_f32_q5_0, true);
  895. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_CPY_F32_Q5_1, cpy_f32_q5_1, true);
  896. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_CPY_F32_IQ4_NL, cpy_f32_iq4_nl, true);
  897. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_CONCAT, concat, true);
  898. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_SQR, sqr, true);
  899. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_SQRT, sqrt, true);
  900. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_SIN, sin, true);
  901. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_COS, cos, true);
  902. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_SUM_ROWS, sum_rows, true);
  903. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_ARGMAX, argmax, true);
  904. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_POOL_2D_AVG_F32, pool_2d_avg_f32, true);
  905. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_POOL_2D_MAX_F32, pool_2d_max_f32, true);
  906. }
  907. [metal_library release];
  908. return ctx;
  909. }
  910. static void ggml_metal_free(struct ggml_backend_metal_context * ctx) {
  911. GGML_LOG_INFO("%s: deallocating\n", __func__);
  912. for (int i = 0; i < GGML_METAL_KERNEL_TYPE_COUNT; ++i) {
  913. [ctx->kernels[i].pipeline release];
  914. }
  915. Block_release(ctx->encode_async);
  916. [ctx->queue release];
  917. dispatch_release(ctx->d_queue);
  918. free(ctx);
  919. }
  920. // temporarily defined here for compatibility between ggml-backend and the old API
  921. struct ggml_backend_metal_buffer {
  922. void * data;
  923. size_t size;
  924. id<MTLBuffer> metal;
  925. };
  926. struct ggml_backend_metal_buffer_context {
  927. void * all_data;
  928. size_t all_size;
  929. bool owned;
  930. // multiple buffers are used only to avoid the maximum buffer size limitation when using mmap
  931. int n_buffers;
  932. struct ggml_backend_metal_buffer buffers[GGML_METAL_MAX_BUFFERS];
  933. };
  934. // finds the Metal buffer that contains the tensor data on the GPU device
  935. // the assumption is that there is 1-to-1 mapping between the host and device memory buffers, so we can find the
  936. // Metal buffer based on the host memory pointer
  937. //
  938. static id<MTLBuffer> ggml_metal_get_buffer(struct ggml_tensor * t, size_t * offs) {
  939. //GGML_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);
  940. const int64_t tsize = ggml_nbytes(t);
  941. ggml_backend_buffer_t buffer = t->view_src ? t->view_src->buffer : t->buffer;
  942. struct ggml_backend_metal_buffer_context * buf_ctx = (struct ggml_backend_metal_buffer_context *) buffer->context;
  943. // find the view that contains the tensor fully
  944. for (int i = 0; i < buf_ctx->n_buffers; ++i) {
  945. const int64_t ioffs = (int64_t) t->data - (int64_t) buf_ctx->buffers[i].data;
  946. //GGML_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);
  947. if (ioffs >= 0 && ioffs + tsize <= (int64_t) buf_ctx->buffers[i].size) {
  948. *offs = (size_t) ioffs;
  949. //GGML_LOG_INFO("%s: tensor '%16s', offs = %8ld\n", __func__, t->name, *offs);
  950. return buf_ctx->buffers[i].metal;
  951. }
  952. }
  953. GGML_LOG_ERROR("%s: error: tensor '%s' buffer is nil\n", __func__, t->name);
  954. return nil;
  955. }
  956. static bool ggml_metal_supports_op(const struct ggml_backend_metal_device_context * ctx_dev, const struct ggml_tensor * op) {
  957. const bool has_simdgroup_mm = ctx_dev->has_simdgroup_mm;
  958. const bool has_simdgroup_reduction = ctx_dev->has_simdgroup_reduction;
  959. const bool use_bfloat = ctx_dev->use_bfloat;
  960. if (!use_bfloat) {
  961. for (size_t i = 0, n = 3; i < n; ++i) {
  962. if (op->src[i] != NULL && op->src[i]->type == GGML_TYPE_BF16) {
  963. return false;
  964. }
  965. }
  966. }
  967. switch (op->op) {
  968. case GGML_OP_UNARY:
  969. switch (ggml_get_unary_op(op)) {
  970. case GGML_UNARY_OP_TANH:
  971. case GGML_UNARY_OP_RELU:
  972. case GGML_UNARY_OP_SIGMOID:
  973. case GGML_UNARY_OP_GELU:
  974. case GGML_UNARY_OP_GELU_QUICK:
  975. case GGML_UNARY_OP_SILU:
  976. case GGML_UNARY_OP_ELU:
  977. return ggml_is_contiguous(op->src[0]);
  978. default:
  979. return false;
  980. }
  981. case GGML_OP_NONE:
  982. case GGML_OP_RESHAPE:
  983. case GGML_OP_VIEW:
  984. case GGML_OP_TRANSPOSE:
  985. case GGML_OP_PERMUTE:
  986. case GGML_OP_CONCAT:
  987. case GGML_OP_ADD:
  988. case GGML_OP_SUB:
  989. case GGML_OP_ACC:
  990. case GGML_OP_MUL:
  991. case GGML_OP_DIV:
  992. case GGML_OP_REPEAT:
  993. case GGML_OP_SCALE:
  994. case GGML_OP_CLAMP:
  995. case GGML_OP_CONV_TRANSPOSE_1D:
  996. return true;
  997. case GGML_OP_SQR:
  998. case GGML_OP_SQRT:
  999. case GGML_OP_SIN:
  1000. case GGML_OP_COS:
  1001. return ggml_is_contiguous(op->src[0]);
  1002. case GGML_OP_SUM_ROWS:
  1003. case GGML_OP_SOFT_MAX:
  1004. case GGML_OP_GROUP_NORM:
  1005. return has_simdgroup_reduction;
  1006. case GGML_OP_RMS_NORM:
  1007. return has_simdgroup_reduction && (op->ne[0] % 4 == 0);
  1008. case GGML_OP_ARGMAX:
  1009. case GGML_OP_NORM:
  1010. case GGML_OP_ROPE:
  1011. return true;
  1012. case GGML_OP_IM2COL:
  1013. return op->src[0]->type == GGML_TYPE_F16;
  1014. case GGML_OP_POOL_1D:
  1015. return false;
  1016. case GGML_OP_POOL_2D:
  1017. case GGML_OP_UPSCALE:
  1018. case GGML_OP_PAD:
  1019. case GGML_OP_UNPAD:
  1020. case GGML_OP_ARANGE:
  1021. case GGML_OP_TIMESTEP_EMBEDDING:
  1022. case GGML_OP_ARGSORT:
  1023. case GGML_OP_LEAKY_RELU:
  1024. return true;
  1025. case GGML_OP_FLASH_ATTN_EXT:
  1026. if (op->src[1]->type != op->src[2]->type) {
  1027. return false;
  1028. }
  1029. return has_simdgroup_mm; // TODO: over-restricted for vec-kernels
  1030. case GGML_OP_SSM_CONV:
  1031. case GGML_OP_SSM_SCAN:
  1032. return true;
  1033. case GGML_OP_MUL_MAT:
  1034. case GGML_OP_MUL_MAT_ID:
  1035. return has_simdgroup_reduction &&
  1036. (op->src[0]->type != GGML_TYPE_F32 || op->src[1]->type == GGML_TYPE_F32);
  1037. case GGML_OP_CPY:
  1038. case GGML_OP_DUP:
  1039. case GGML_OP_CONT:
  1040. {
  1041. switch (op->src[0]->type) {
  1042. case GGML_TYPE_F32:
  1043. switch (op->type) {
  1044. case GGML_TYPE_F32:
  1045. case GGML_TYPE_F16:
  1046. case GGML_TYPE_BF16:
  1047. case GGML_TYPE_Q8_0:
  1048. case GGML_TYPE_Q4_0:
  1049. case GGML_TYPE_Q4_1:
  1050. case GGML_TYPE_Q5_0:
  1051. case GGML_TYPE_Q5_1:
  1052. case GGML_TYPE_IQ4_NL:
  1053. return true;
  1054. default:
  1055. return false;
  1056. }
  1057. case GGML_TYPE_F16:
  1058. switch (op->type) {
  1059. case GGML_TYPE_F32:
  1060. case GGML_TYPE_F16:
  1061. return true;
  1062. default:
  1063. return false;
  1064. }
  1065. case GGML_TYPE_BF16:
  1066. switch (op->type) {
  1067. case GGML_TYPE_F32:
  1068. case GGML_TYPE_BF16:
  1069. return true;
  1070. default:
  1071. return false;
  1072. }
  1073. default:
  1074. return false;
  1075. };
  1076. }
  1077. case GGML_OP_DIAG_MASK_INF:
  1078. case GGML_OP_GET_ROWS:
  1079. {
  1080. return op->ne[3] == 1;
  1081. }
  1082. default:
  1083. return false;
  1084. }
  1085. }
  1086. static void ggml_metal_encode_node(
  1087. ggml_backend_t backend,
  1088. int idx,
  1089. id<MTLComputeCommandEncoder> encoder) {
  1090. struct ggml_backend_metal_context * ctx = backend->context;
  1091. struct ggml_backend_metal_device_context * ctx_dev = backend->device->context;
  1092. struct ggml_cgraph * gf = ctx->gf;
  1093. struct ggml_tensor * node = ggml_graph_node(gf, idx);
  1094. //GGML_LOG_INFO("%s: encoding node %3d, op = %8s\n", __func__, idx, ggml_op_name(node->op));
  1095. struct ggml_tensor * src0 = node->src[0];
  1096. struct ggml_tensor * src1 = node->src[1];
  1097. struct ggml_tensor * src2 = node->src[2];
  1098. struct ggml_tensor * dst = node;
  1099. if (ggml_is_empty(dst)) {
  1100. return;
  1101. }
  1102. switch (dst->op) {
  1103. case GGML_OP_NONE:
  1104. case GGML_OP_RESHAPE:
  1105. case GGML_OP_VIEW:
  1106. case GGML_OP_TRANSPOSE:
  1107. case GGML_OP_PERMUTE:
  1108. {
  1109. // noop -> next node
  1110. } return;
  1111. default:
  1112. {
  1113. } break;
  1114. }
  1115. if (!ggml_metal_supports_op(ctx_dev, dst)) {
  1116. GGML_LOG_ERROR("%s: error: unsupported op '%s'\n", __func__, ggml_op_desc(dst));
  1117. GGML_ABORT("unsupported op");
  1118. }
  1119. const int64_t ne00 = src0 ? src0->ne[0] : 0;
  1120. const int64_t ne01 = src0 ? src0->ne[1] : 0;
  1121. const int64_t ne02 = src0 ? src0->ne[2] : 0;
  1122. const int64_t ne03 = src0 ? src0->ne[3] : 0;
  1123. const uint64_t nb00 = src0 ? src0->nb[0] : 0;
  1124. const uint64_t nb01 = src0 ? src0->nb[1] : 0;
  1125. const uint64_t nb02 = src0 ? src0->nb[2] : 0;
  1126. const uint64_t nb03 = src0 ? src0->nb[3] : 0;
  1127. const int64_t ne10 = src1 ? src1->ne[0] : 0;
  1128. const int64_t ne11 = src1 ? src1->ne[1] : 0;
  1129. const int64_t ne12 = src1 ? src1->ne[2] : 0;
  1130. const int64_t ne13 = src1 ? src1->ne[3] : 0;
  1131. const uint64_t nb10 = src1 ? src1->nb[0] : 0;
  1132. const uint64_t nb11 = src1 ? src1->nb[1] : 0;
  1133. const uint64_t nb12 = src1 ? src1->nb[2] : 0;
  1134. const uint64_t nb13 = src1 ? src1->nb[3] : 0;
  1135. const int64_t ne20 = src2 ? src2->ne[0] : 0;
  1136. const int64_t ne21 = src2 ? src2->ne[1] : 0;
  1137. const int64_t ne22 = src2 ? src2->ne[2] : 0; GGML_UNUSED(ne22);
  1138. const int64_t ne23 = src2 ? src2->ne[3] : 0; GGML_UNUSED(ne23);
  1139. const uint64_t nb20 = src2 ? src2->nb[0] : 0; GGML_UNUSED(nb20);
  1140. const uint64_t nb21 = src2 ? src2->nb[1] : 0;
  1141. const uint64_t nb22 = src2 ? src2->nb[2] : 0;
  1142. const uint64_t nb23 = src2 ? src2->nb[3] : 0; GGML_UNUSED(nb23);
  1143. const int64_t ne0 = dst ? dst->ne[0] : 0;
  1144. const int64_t ne1 = dst ? dst->ne[1] : 0;
  1145. const int64_t ne2 = dst ? dst->ne[2] : 0;
  1146. const int64_t ne3 = dst ? dst->ne[3] : 0;
  1147. const uint64_t nb0 = dst ? dst->nb[0] : 0;
  1148. const uint64_t nb1 = dst ? dst->nb[1] : 0;
  1149. const uint64_t nb2 = dst ? dst->nb[2] : 0;
  1150. const uint64_t nb3 = dst ? dst->nb[3] : 0;
  1151. const enum ggml_type src0t = src0 ? src0->type : GGML_TYPE_COUNT;
  1152. const enum ggml_type src1t = src1 ? src1->type : GGML_TYPE_COUNT;
  1153. const enum ggml_type dstt = dst ? dst->type : GGML_TYPE_COUNT;
  1154. size_t offs_src0 = 0;
  1155. size_t offs_src1 = 0;
  1156. size_t offs_src2 = 0;
  1157. size_t offs_dst = 0;
  1158. id<MTLBuffer> id_src0 = src0 ? ggml_metal_get_buffer(src0, &offs_src0) : nil;
  1159. id<MTLBuffer> id_src1 = src1 ? ggml_metal_get_buffer(src1, &offs_src1) : nil;
  1160. id<MTLBuffer> id_src2 = src2 ? ggml_metal_get_buffer(src2, &offs_src2) : nil;
  1161. id<MTLBuffer> id_dst = dst ? ggml_metal_get_buffer(dst, &offs_dst) : nil;
  1162. #if 0
  1163. GGML_LOG_INFO("%s: op - %s\n", __func__, ggml_op_name(dst->op));
  1164. if (src0) {
  1165. GGML_LOG_INFO("%s: src0 - %4s [%5lld, %5lld, %5lld, %5lld] [%5lld, %5lld, %5lld, %5lld], %d, %s\n", __func__, ggml_type_name(src0t), ne00, ne01, ne02, ne03, nb00, nb01, nb02, nb03,
  1166. ggml_is_contiguous(src0), src0->name);
  1167. }
  1168. if (src1) {
  1169. GGML_LOG_INFO("%s: src1 - %4s [%5lld, %5lld, %5lld, %5lld] [%5lld, %5lld, %5lld, %5lld], %d, %s\n", __func__, ggml_type_name(src1t), ne10, ne11, ne12, ne13, nb10, nb11, nb12, nb13,
  1170. ggml_is_contiguous(src1), src1->name);
  1171. }
  1172. if (dst) {
  1173. GGML_LOG_INFO("%s: dst - %4s [%5lld, %5lld, %5lld, %5lld] [%5lld, %5lld, %5lld, %5lld], 1, %s\n", __func__, ggml_type_name(dstt), ne0, ne1, ne2, ne3, nb0, nb1, nb2, nb3,
  1174. dst->name);
  1175. }
  1176. #endif
  1177. id<MTLDevice> device = ctx_dev->mtl_device;
  1178. switch (dst->op) {
  1179. case GGML_OP_CONCAT:
  1180. {
  1181. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_CONCAT].pipeline;
  1182. const int32_t dim = ((const int32_t *) dst->op_params)[0];
  1183. ggml_metal_kargs_concat args = {
  1184. /*.ne00 =*/ ne00,
  1185. /*.ne01 =*/ ne01,
  1186. /*.ne02 =*/ ne02,
  1187. /*.ne03 =*/ ne03,
  1188. /*.nb00 =*/ nb00,
  1189. /*.nb01 =*/ nb01,
  1190. /*.nb02 =*/ nb02,
  1191. /*.nb03 =*/ nb03,
  1192. /*.ne10 =*/ ne10,
  1193. /*.ne11 =*/ ne11,
  1194. /*.ne12 =*/ ne12,
  1195. /*.ne13 =*/ ne13,
  1196. /*.nb10 =*/ nb10,
  1197. /*.nb11 =*/ nb11,
  1198. /*.nb12 =*/ nb12,
  1199. /*.nb13 =*/ nb13,
  1200. /*.ne0 =*/ ne0,
  1201. /*.ne1 =*/ ne1,
  1202. /*.ne2 =*/ ne2,
  1203. /*.ne3 =*/ ne3,
  1204. /*.nb0 =*/ nb0,
  1205. /*.nb1 =*/ nb1,
  1206. /*.nb2 =*/ nb2,
  1207. /*.nb3 =*/ nb3,
  1208. /*.dim =*/ dim,
  1209. };
  1210. [encoder setComputePipelineState:pipeline];
  1211. [encoder setBytes:&args length:sizeof(args) atIndex:0];
  1212. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:1];
  1213. [encoder setBuffer:id_src1 offset:offs_src1 atIndex:2];
  1214. [encoder setBuffer:id_dst offset:offs_dst atIndex:3];
  1215. const int nth = MIN(1024, ne0);
  1216. [encoder dispatchThreadgroups:MTLSizeMake(ne1, ne2, ne3) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  1217. } break;
  1218. case GGML_OP_ADD:
  1219. case GGML_OP_SUB:
  1220. case GGML_OP_MUL:
  1221. case GGML_OP_DIV:
  1222. {
  1223. GGML_ASSERT(src0t == GGML_TYPE_F32);
  1224. GGML_ASSERT(src1t == GGML_TYPE_F32);
  1225. const size_t offs = 0;
  1226. bool bcast_row = false;
  1227. id<MTLComputePipelineState> pipeline = nil;
  1228. if (ggml_nelements(src1) == ne10 && ggml_is_contiguous(src1) && ne00 % 4 == 0 && ne10 % 4 == 0) {
  1229. GGML_ASSERT(ggml_is_contiguous(src0));
  1230. // src1 is a row
  1231. GGML_ASSERT(ne11 == 1);
  1232. switch (dst->op) {
  1233. case GGML_OP_ADD: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_ADD_ROW].pipeline; break;
  1234. case GGML_OP_SUB: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_SUB_ROW].pipeline; break;
  1235. case GGML_OP_MUL: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_ROW].pipeline; break;
  1236. case GGML_OP_DIV: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_DIV_ROW].pipeline; break;
  1237. default: GGML_ABORT("fatal error");
  1238. }
  1239. bcast_row = true;
  1240. } else {
  1241. switch (dst->op) {
  1242. case GGML_OP_ADD: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_ADD].pipeline; break;
  1243. case GGML_OP_SUB: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_SUB].pipeline; break;
  1244. case GGML_OP_MUL: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL].pipeline; break;
  1245. case GGML_OP_DIV: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_DIV].pipeline; break;
  1246. default: GGML_ABORT("fatal error");
  1247. }
  1248. }
  1249. ggml_metal_kargs_bin args = {
  1250. /*.ne00 =*/ ne00,
  1251. /*.ne01 =*/ ne01,
  1252. /*.ne02 =*/ ne02,
  1253. /*.ne03 =*/ ne03,
  1254. /*.nb00 =*/ nb00,
  1255. /*.nb01 =*/ nb01,
  1256. /*.nb02 =*/ nb02,
  1257. /*.nb03 =*/ nb03,
  1258. /*.ne10 =*/ ne10,
  1259. /*.ne11 =*/ ne11,
  1260. /*.ne12 =*/ ne12,
  1261. /*.ne13 =*/ ne13,
  1262. /*.nb10 =*/ nb10,
  1263. /*.nb11 =*/ nb11,
  1264. /*.nb12 =*/ nb12,
  1265. /*.nb13 =*/ nb13,
  1266. /*.ne0 =*/ ne0,
  1267. /*.ne1 =*/ ne1,
  1268. /*.ne2 =*/ ne2,
  1269. /*.ne3 =*/ ne3,
  1270. /*.nb0 =*/ nb0,
  1271. /*.nb1 =*/ nb1,
  1272. /*.nb2 =*/ nb2,
  1273. /*.nb3 =*/ nb3,
  1274. /*.offs =*/ offs,
  1275. };
  1276. [encoder setComputePipelineState:pipeline];
  1277. [encoder setBytes:&args length:sizeof(args) atIndex:0];
  1278. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:1];
  1279. [encoder setBuffer:id_src1 offset:offs_src1 atIndex:2];
  1280. [encoder setBuffer:id_dst offset:offs_dst atIndex:3];
  1281. if (bcast_row) {
  1282. const int64_t n = ggml_nelements(dst)/4;
  1283. [encoder dispatchThreadgroups:MTLSizeMake(n, 1, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  1284. } else {
  1285. const int nth = MIN((int) pipeline.maxTotalThreadsPerThreadgroup, ne0);
  1286. [encoder dispatchThreadgroups:MTLSizeMake(ne01, ne02, ne03) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  1287. }
  1288. } break;
  1289. case GGML_OP_REPEAT:
  1290. {
  1291. id<MTLComputePipelineState> pipeline;
  1292. switch (src0t) {
  1293. case GGML_TYPE_F32: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_REPEAT_F32].pipeline; break;
  1294. case GGML_TYPE_F16: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_REPEAT_F16].pipeline; break;
  1295. case GGML_TYPE_I32: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_REPEAT_I32].pipeline; break;
  1296. case GGML_TYPE_I16: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_REPEAT_I16].pipeline; break;
  1297. default: GGML_ABORT("fatal error");
  1298. }
  1299. ggml_metal_kargs_repeat args = {
  1300. /*.ne00 =*/ ne00,
  1301. /*.ne01 =*/ ne01,
  1302. /*.ne02 =*/ ne02,
  1303. /*.ne03 =*/ ne03,
  1304. /*.nb00 =*/ nb00,
  1305. /*.nb01 =*/ nb01,
  1306. /*.nb02 =*/ nb02,
  1307. /*.nb03 =*/ nb03,
  1308. /*.ne0 =*/ ne0,
  1309. /*.ne1 =*/ ne1,
  1310. /*.ne2 =*/ ne2,
  1311. /*.ne3 =*/ ne3,
  1312. /*.nb0 =*/ nb0,
  1313. /*.nb1 =*/ nb1,
  1314. /*.nb2 =*/ nb2,
  1315. /*.nb3 =*/ nb3,
  1316. };
  1317. [encoder setComputePipelineState:pipeline];
  1318. [encoder setBytes:&args length:sizeof(args) atIndex:0];
  1319. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:1];
  1320. [encoder setBuffer:id_dst offset:offs_dst atIndex:2];
  1321. const int nth = MIN((int) pipeline.maxTotalThreadsPerThreadgroup, ne0);
  1322. [encoder dispatchThreadgroups:MTLSizeMake(ne1, ne2, ne3) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  1323. } break;
  1324. case GGML_OP_ACC:
  1325. {
  1326. GGML_ASSERT(src0t == GGML_TYPE_F32);
  1327. GGML_ASSERT(src1t == GGML_TYPE_F32);
  1328. GGML_ASSERT(dstt == GGML_TYPE_F32);
  1329. GGML_ASSERT(ggml_is_contiguous(src0));
  1330. GGML_ASSERT(ggml_is_contiguous(src1));
  1331. const size_t pnb1 = ((const int32_t *) dst->op_params)[0];
  1332. const size_t pnb2 = ((const int32_t *) dst->op_params)[1];
  1333. const size_t pnb3 = ((const int32_t *) dst->op_params)[2];
  1334. const size_t offs = ((const int32_t *) dst->op_params)[3];
  1335. const bool inplace = (bool) ((const int32_t *) dst->op_params)[4];
  1336. if (!inplace) {
  1337. // run a separete kernel to cpy src->dst
  1338. // not sure how to avoid this
  1339. // TODO: make a simpler cpy_bytes kernel
  1340. const id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_CPY_F32_F32].pipeline;
  1341. ggml_metal_kargs_cpy args = {
  1342. /*.ne00 =*/ ne00,
  1343. /*.ne01 =*/ ne01,
  1344. /*.ne02 =*/ ne02,
  1345. /*.ne03 =*/ ne03,
  1346. /*.nb00 =*/ nb00,
  1347. /*.nb01 =*/ nb01,
  1348. /*.nb02 =*/ nb02,
  1349. /*.nb03 =*/ nb03,
  1350. /*.ne0 =*/ ne0,
  1351. /*.ne1 =*/ ne1,
  1352. /*.ne2 =*/ ne2,
  1353. /*.ne3 =*/ ne3,
  1354. /*.nb0 =*/ nb0,
  1355. /*.nb1 =*/ nb1,
  1356. /*.nb2 =*/ nb2,
  1357. /*.nb3 =*/ nb3,
  1358. };
  1359. [encoder setComputePipelineState:pipeline];
  1360. [encoder setBytes:&args length:sizeof(args) atIndex:0];
  1361. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:1];
  1362. [encoder setBuffer:id_dst offset:offs_dst atIndex:2];
  1363. const int nth = MIN((int) pipeline.maxTotalThreadsPerThreadgroup, ne00);
  1364. [encoder dispatchThreadgroups:MTLSizeMake(ne01, ne02, ne03) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  1365. }
  1366. const id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_ADD].pipeline;
  1367. ggml_metal_kargs_bin args = {
  1368. /*.ne00 =*/ ne00,
  1369. /*.ne01 =*/ ne01,
  1370. /*.ne02 =*/ ne02,
  1371. /*.ne03 =*/ ne03,
  1372. /*.nb00 =*/ nb00,
  1373. /*.nb01 =*/ pnb1,
  1374. /*.nb02 =*/ pnb2,
  1375. /*.nb03 =*/ pnb3,
  1376. /*.ne10 =*/ ne10,
  1377. /*.ne11 =*/ ne11,
  1378. /*.ne12 =*/ ne12,
  1379. /*.ne13 =*/ ne13,
  1380. /*.nb10 =*/ nb10,
  1381. /*.nb11 =*/ nb11,
  1382. /*.nb12 =*/ nb12,
  1383. /*.nb13 =*/ nb13,
  1384. /*.ne0 =*/ ne0,
  1385. /*.ne1 =*/ ne1,
  1386. /*.ne2 =*/ ne2,
  1387. /*.ne3 =*/ ne3,
  1388. /*.nb0 =*/ nb0,
  1389. /*.nb1 =*/ pnb1,
  1390. /*.nb2 =*/ pnb2,
  1391. /*.nb3 =*/ pnb3,
  1392. /*.offs =*/ offs,
  1393. };
  1394. [encoder setComputePipelineState:pipeline];
  1395. [encoder setBytes:&args length:sizeof(args) atIndex:0];
  1396. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:1];
  1397. [encoder setBuffer:id_src1 offset:offs_src1 atIndex:2];
  1398. [encoder setBuffer:id_dst offset:offs_dst atIndex:3];
  1399. const int nth = MIN((int) pipeline.maxTotalThreadsPerThreadgroup, ne00);
  1400. [encoder dispatchThreadgroups:MTLSizeMake(ne11, ne12, ne13) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  1401. } break;
  1402. case GGML_OP_SCALE:
  1403. {
  1404. GGML_ASSERT(ggml_is_contiguous(src0));
  1405. float scale;
  1406. memcpy(&scale, dst->op_params, sizeof(scale));
  1407. int64_t n = ggml_nelements(dst);
  1408. id<MTLComputePipelineState> pipeline = nil;
  1409. if (n % 4 == 0) {
  1410. n /= 4;
  1411. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_SCALE_4].pipeline;
  1412. } else {
  1413. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_SCALE].pipeline;
  1414. }
  1415. [encoder setComputePipelineState:pipeline];
  1416. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1417. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  1418. [encoder setBytes:&scale length:sizeof(scale) atIndex:2];
  1419. [encoder dispatchThreadgroups:MTLSizeMake(n, 1, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  1420. } break;
  1421. case GGML_OP_CLAMP:
  1422. {
  1423. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_CLAMP].pipeline;
  1424. float min;
  1425. float max;
  1426. memcpy(&min, ((const int32_t *) dst->op_params) + 0, sizeof(float));
  1427. memcpy(&max, ((const int32_t *) dst->op_params) + 1, sizeof(float));
  1428. [encoder setComputePipelineState:pipeline];
  1429. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1430. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  1431. [encoder setBytes:&min length:sizeof(min) atIndex:2];
  1432. [encoder setBytes:&max length:sizeof(max) atIndex:3];
  1433. const int64_t n = ggml_nelements(dst);
  1434. [encoder dispatchThreadgroups:MTLSizeMake(n, 1, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  1435. } break;
  1436. case GGML_OP_UNARY:
  1437. switch (ggml_get_unary_op(node)) {
  1438. // we are not taking into account the strides, so for now require contiguous tensors
  1439. GGML_ASSERT(ggml_is_contiguous(src0));
  1440. case GGML_UNARY_OP_TANH:
  1441. {
  1442. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_TANH].pipeline;
  1443. [encoder setComputePipelineState:pipeline];
  1444. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1445. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  1446. const int64_t n = ggml_nelements(dst);
  1447. [encoder dispatchThreadgroups:MTLSizeMake(n, 1, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  1448. } break;
  1449. case GGML_UNARY_OP_RELU:
  1450. {
  1451. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_RELU].pipeline;
  1452. [encoder setComputePipelineState:pipeline];
  1453. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1454. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  1455. const int64_t n = ggml_nelements(dst);
  1456. [encoder dispatchThreadgroups:MTLSizeMake(n, 1, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  1457. } break;
  1458. case GGML_UNARY_OP_SIGMOID:
  1459. {
  1460. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_SIGMOID].pipeline;
  1461. [encoder setComputePipelineState:pipeline];
  1462. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1463. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  1464. const int64_t n = ggml_nelements(dst);
  1465. [encoder dispatchThreadgroups:MTLSizeMake(n, 1, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  1466. } break;
  1467. case GGML_UNARY_OP_GELU:
  1468. {
  1469. int64_t n = ggml_nelements(dst);
  1470. id<MTLComputePipelineState> pipeline = nil;
  1471. if (n % 4 == 0) {
  1472. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GELU_4].pipeline;
  1473. n /= 4;
  1474. } else {
  1475. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GELU].pipeline;
  1476. }
  1477. [encoder setComputePipelineState:pipeline];
  1478. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1479. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  1480. [encoder dispatchThreadgroups:MTLSizeMake(n, 1, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  1481. } break;
  1482. case GGML_UNARY_OP_GELU_QUICK:
  1483. {
  1484. int64_t n = ggml_nelements(dst);
  1485. id<MTLComputePipelineState> pipeline = nil;
  1486. if (n % 4 == 0) {
  1487. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GELU_QUICK_4].pipeline;
  1488. n /= 4;
  1489. } else {
  1490. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GELU_QUICK].pipeline;
  1491. }
  1492. [encoder setComputePipelineState:pipeline];
  1493. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1494. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  1495. [encoder dispatchThreadgroups:MTLSizeMake(n, 1, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  1496. } break;
  1497. case GGML_UNARY_OP_SILU:
  1498. {
  1499. int64_t n = ggml_nelements(dst);
  1500. id<MTLComputePipelineState> pipeline = nil;
  1501. if (n % 4 == 0) {
  1502. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_SILU_4].pipeline;
  1503. n /= 4;
  1504. } else {
  1505. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_SILU].pipeline;
  1506. }
  1507. [encoder setComputePipelineState:pipeline];
  1508. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1509. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  1510. [encoder dispatchThreadgroups:MTLSizeMake(n, 1, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  1511. } break;
  1512. case GGML_UNARY_OP_ELU:
  1513. {
  1514. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_ELU].pipeline;
  1515. [encoder setComputePipelineState:pipeline];
  1516. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1517. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  1518. const int64_t n = ggml_nelements(dst);
  1519. [encoder dispatchThreadgroups:MTLSizeMake(n, 1, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  1520. } break;
  1521. default:
  1522. {
  1523. GGML_LOG_WARN("%s: node %3d, op = %8s not implemented\n", __func__, idx, ggml_op_name(dst->op));
  1524. GGML_ABORT("fatal error");
  1525. }
  1526. } break;
  1527. case GGML_OP_SQR:
  1528. {
  1529. GGML_ASSERT(ggml_is_contiguous(src0));
  1530. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_SQR].pipeline;
  1531. [encoder setComputePipelineState:pipeline];
  1532. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1533. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  1534. const int64_t n = ggml_nelements(dst);
  1535. [encoder dispatchThreadgroups:MTLSizeMake(n, 1, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  1536. } break;
  1537. case GGML_OP_SQRT:
  1538. {
  1539. GGML_ASSERT(ggml_is_contiguous(src0));
  1540. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_SQRT].pipeline;
  1541. [encoder setComputePipelineState:pipeline];
  1542. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1543. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  1544. const int64_t n = ggml_nelements(dst);
  1545. [encoder dispatchThreadgroups:MTLSizeMake(n, 1, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  1546. } break;
  1547. case GGML_OP_SIN:
  1548. {
  1549. GGML_ASSERT(ggml_is_contiguous(src0));
  1550. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_SIN].pipeline;
  1551. [encoder setComputePipelineState:pipeline];
  1552. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1553. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  1554. const int64_t n = ggml_nelements(dst);
  1555. [encoder dispatchThreadgroups:MTLSizeMake(n, 1, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  1556. } break;
  1557. case GGML_OP_COS:
  1558. {
  1559. GGML_ASSERT(ggml_is_contiguous(src0));
  1560. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_COS].pipeline;
  1561. [encoder setComputePipelineState:pipeline];
  1562. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1563. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  1564. const int64_t n = ggml_nelements(dst);
  1565. [encoder dispatchThreadgroups:MTLSizeMake(n, 1, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  1566. } break;
  1567. case GGML_OP_SUM_ROWS:
  1568. {
  1569. GGML_ASSERT(src0->nb[0] == ggml_type_size(src0->type));
  1570. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_SUM_ROWS].pipeline;
  1571. // TODO: add ggml_metal_kargs struct
  1572. [encoder setComputePipelineState:pipeline];
  1573. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1574. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  1575. [encoder setBytes:&ne00 length:sizeof(ne00) atIndex:2];
  1576. [encoder setBytes:&ne01 length:sizeof(ne01) atIndex:3];
  1577. [encoder setBytes:&ne02 length:sizeof(ne02) atIndex:4];
  1578. [encoder setBytes:&ne03 length:sizeof(ne03) atIndex:5];
  1579. [encoder setBytes:&nb00 length:sizeof(nb00) atIndex:6];
  1580. [encoder setBytes:&nb01 length:sizeof(nb01) atIndex:7];
  1581. [encoder setBytes:&nb02 length:sizeof(nb02) atIndex:8];
  1582. [encoder setBytes:&nb03 length:sizeof(nb03) atIndex:9];
  1583. [encoder setBytes:&ne10 length:sizeof(ne10) atIndex:10];
  1584. [encoder setBytes:&ne11 length:sizeof(ne11) atIndex:11];
  1585. [encoder setBytes:&ne12 length:sizeof(ne12) atIndex:12];
  1586. [encoder setBytes:&ne13 length:sizeof(ne13) atIndex:13];
  1587. [encoder setBytes:&nb10 length:sizeof(nb10) atIndex:14];
  1588. [encoder setBytes:&nb11 length:sizeof(nb11) atIndex:15];
  1589. [encoder setBytes:&nb12 length:sizeof(nb12) atIndex:16];
  1590. [encoder setBytes:&nb13 length:sizeof(nb13) atIndex:17];
  1591. [encoder setBytes:&ne0 length:sizeof(ne0) atIndex:18];
  1592. [encoder setBytes:&ne1 length:sizeof(ne1) atIndex:19];
  1593. [encoder setBytes:&ne2 length:sizeof(ne2) atIndex:20];
  1594. [encoder setBytes:&ne3 length:sizeof(ne3) atIndex:21];
  1595. [encoder setBytes:&nb0 length:sizeof(nb0) atIndex:22];
  1596. [encoder setBytes:&nb1 length:sizeof(nb1) atIndex:23];
  1597. [encoder setBytes:&nb2 length:sizeof(nb2) atIndex:24];
  1598. [encoder setBytes:&nb3 length:sizeof(nb3) atIndex:25];
  1599. [encoder dispatchThreadgroups:MTLSizeMake(ne01, ne02, ne03) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  1600. } break;
  1601. case GGML_OP_SOFT_MAX:
  1602. {
  1603. GGML_ASSERT(!src1 || src1->type == GGML_TYPE_F16 || src1->type == GGML_TYPE_F32);
  1604. int nth = 32; // SIMD width
  1605. id<MTLComputePipelineState> pipeline = nil;
  1606. const bool use_f16 = (src1 && src1->type == GGML_TYPE_F16);
  1607. if (ne00%4 == 0) {
  1608. while (nth < ne00/4 && nth*ne01*ne02*ne03 < 256) {
  1609. nth *= 2;
  1610. }
  1611. if (use_f16) {
  1612. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_SOFT_MAX_F16_4].pipeline;
  1613. } else {
  1614. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_SOFT_MAX_F32_4].pipeline;
  1615. }
  1616. } else {
  1617. while (nth < ne00 && nth*ne01*ne02*ne03 < 256) {
  1618. nth *= 2;
  1619. }
  1620. if (use_f16) {
  1621. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_SOFT_MAX_F16].pipeline;
  1622. } else {
  1623. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_SOFT_MAX_F32].pipeline;
  1624. }
  1625. }
  1626. float scale;
  1627. float max_bias;
  1628. memcpy(&scale, ((const int32_t *) dst->op_params) + 0, sizeof(scale));
  1629. memcpy(&max_bias, ((const int32_t *) dst->op_params) + 1, sizeof(max_bias));
  1630. const int64_t nrows_x = ggml_nrows(src0);
  1631. const int64_t nrows_y = src0->ne[1];
  1632. const uint32_t n_head = nrows_x/nrows_y;
  1633. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head));
  1634. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  1635. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  1636. // TODO: add ggml_metal_kargs struct
  1637. // TODO: optimize (see https://github.com/ggerganov/llama.cpp/pull/10238/commits/7941b6b9ec29a2866fec6fa6c51612515ca509f6)
  1638. [encoder setComputePipelineState:pipeline];
  1639. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1640. if (id_src1) {
  1641. [encoder setBuffer:id_src1 offset:offs_src1 atIndex:1];
  1642. } else {
  1643. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:1];
  1644. }
  1645. [encoder setBuffer:id_dst offset:offs_dst atIndex:2];
  1646. [encoder setBytes:&ne00 length:sizeof(ne00) atIndex:3];
  1647. [encoder setBytes:&ne01 length:sizeof(ne01) atIndex:4];
  1648. [encoder setBytes:&ne02 length:sizeof(ne02) atIndex:5];
  1649. [encoder setBytes:&scale length:sizeof(scale) atIndex:6];
  1650. [encoder setBytes:&max_bias length:sizeof(max_bias) atIndex:7];
  1651. [encoder setBytes:&m0 length:sizeof(m0) atIndex:8];
  1652. [encoder setBytes:&m1 length:sizeof(m1) atIndex:9];
  1653. [encoder setBytes:&n_head_log2 length:sizeof(n_head_log2) atIndex:10];
  1654. [encoder setThreadgroupMemoryLength:32*sizeof(float) atIndex:0];
  1655. [encoder dispatchThreadgroups:MTLSizeMake(ne01*ne02*ne03, 1, 1) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  1656. } break;
  1657. case GGML_OP_DIAG_MASK_INF:
  1658. {
  1659. const int n_past = ((const int32_t *)(dst->op_params))[0];
  1660. id<MTLComputePipelineState> pipeline = nil;
  1661. if (ne00%8 == 0) {
  1662. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_DIAG_MASK_INF_8].pipeline;
  1663. } else {
  1664. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_DIAG_MASK_INF].pipeline;
  1665. }
  1666. // TODO: add ggml_metal_kargs struct
  1667. [encoder setComputePipelineState:pipeline];
  1668. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1669. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  1670. [encoder setBytes:&ne00 length:sizeof(ne00) atIndex:2];
  1671. [encoder setBytes:&ne01 length:sizeof(ne01) atIndex:3];
  1672. [encoder setBytes:&n_past length:sizeof(int) atIndex:4];
  1673. if (ne00%8 == 0) {
  1674. [encoder dispatchThreadgroups:MTLSizeMake(ne00*ne01*ne02/8, 1, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  1675. }
  1676. else {
  1677. [encoder dispatchThreadgroups:MTLSizeMake(ne00, ne01, ne02) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  1678. }
  1679. } break;
  1680. case GGML_OP_SSM_CONV:
  1681. {
  1682. GGML_ASSERT(src0t == GGML_TYPE_F32);
  1683. GGML_ASSERT(src1t == GGML_TYPE_F32);
  1684. GGML_ASSERT(ggml_is_contiguous(src0));
  1685. GGML_ASSERT(ggml_is_contiguous(src1));
  1686. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_SSM_CONV_F32].pipeline;
  1687. // TODO: add ggml_metal_kargs struct
  1688. [encoder setComputePipelineState:pipeline];
  1689. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1690. [encoder setBuffer:id_src1 offset:offs_src1 atIndex:1];
  1691. [encoder setBuffer:id_dst offset:offs_dst atIndex:2];
  1692. [encoder setBytes:&ne00 length:sizeof(ne00) atIndex:3];
  1693. [encoder setBytes:&ne01 length:sizeof(ne01) atIndex:4];
  1694. [encoder setBytes:&ne02 length:sizeof(ne02) atIndex:5];
  1695. [encoder setBytes:&nb00 length:sizeof(nb00) atIndex:6];
  1696. [encoder setBytes:&nb01 length:sizeof(nb01) atIndex:7];
  1697. [encoder setBytes:&nb02 length:sizeof(nb02) atIndex:8];
  1698. [encoder setBytes:&ne10 length:sizeof(ne10) atIndex:9];
  1699. [encoder setBytes:&ne11 length:sizeof(ne11) atIndex:10];
  1700. [encoder setBytes:&nb10 length:sizeof(nb10) atIndex:11];
  1701. [encoder setBytes:&nb11 length:sizeof(nb11) atIndex:12];
  1702. [encoder setBytes:&ne0 length:sizeof(ne0) atIndex:13];
  1703. [encoder setBytes:&ne1 length:sizeof(ne1) atIndex:14];
  1704. [encoder setBytes:&ne2 length:sizeof(ne2) atIndex:15];
  1705. [encoder setBytes:&nb0 length:sizeof(nb0) atIndex:16];
  1706. [encoder setBytes:&nb1 length:sizeof(nb1) atIndex:17];
  1707. [encoder setBytes:&nb2 length:sizeof(nb2) atIndex:18];
  1708. [encoder dispatchThreadgroups:MTLSizeMake(ne01, ne1, ne02) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  1709. } break;
  1710. case GGML_OP_SSM_SCAN:
  1711. {
  1712. struct ggml_tensor * src3 = node->src[3];
  1713. struct ggml_tensor * src4 = node->src[4];
  1714. struct ggml_tensor * src5 = node->src[5];
  1715. GGML_ASSERT(src3);
  1716. GGML_ASSERT(src4);
  1717. GGML_ASSERT(src5);
  1718. size_t offs_src3 = 0;
  1719. size_t offs_src4 = 0;
  1720. size_t offs_src5 = 0;
  1721. id<MTLBuffer> id_src3 = src3 ? ggml_metal_get_buffer(src3, &offs_src3) : nil;
  1722. id<MTLBuffer> id_src4 = src4 ? ggml_metal_get_buffer(src4, &offs_src4) : nil;
  1723. id<MTLBuffer> id_src5 = src5 ? ggml_metal_get_buffer(src5, &offs_src5) : nil;
  1724. const int64_t ne30 = src3->ne[0]; GGML_UNUSED(ne30);
  1725. const int64_t ne31 = src3->ne[1]; GGML_UNUSED(ne31);
  1726. const uint64_t nb30 = src3->nb[0];
  1727. const uint64_t nb31 = src3->nb[1];
  1728. const int64_t ne40 = src4->ne[0]; GGML_UNUSED(ne40);
  1729. const int64_t ne41 = src4->ne[1]; GGML_UNUSED(ne41);
  1730. const int64_t ne42 = src4->ne[2]; GGML_UNUSED(ne42);
  1731. const uint64_t nb40 = src4->nb[0];
  1732. const uint64_t nb41 = src4->nb[1];
  1733. const uint64_t nb42 = src4->nb[2];
  1734. const int64_t ne50 = src5->ne[0]; GGML_UNUSED(ne50);
  1735. const int64_t ne51 = src5->ne[1]; GGML_UNUSED(ne51);
  1736. const int64_t ne52 = src5->ne[2]; GGML_UNUSED(ne52);
  1737. const uint64_t nb50 = src5->nb[0];
  1738. const uint64_t nb51 = src5->nb[1];
  1739. const uint64_t nb52 = src5->nb[2];
  1740. const int64_t d_state = ne00;
  1741. const int64_t d_inner = ne01;
  1742. const int64_t n_seq_tokens = ne11;
  1743. const int64_t n_seqs = ne02;
  1744. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_SSM_SCAN_F32].pipeline;
  1745. // TODO: add ggml_metal_kargs struct
  1746. [encoder setComputePipelineState:pipeline];
  1747. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1748. [encoder setBuffer:id_src1 offset:offs_src1 atIndex:1];
  1749. [encoder setBuffer:id_src2 offset:offs_src2 atIndex:2];
  1750. [encoder setBuffer:id_src3 offset:offs_src3 atIndex:3];
  1751. [encoder setBuffer:id_src4 offset:offs_src4 atIndex:4];
  1752. [encoder setBuffer:id_src5 offset:offs_src5 atIndex:5];
  1753. [encoder setBuffer:id_dst offset:offs_dst atIndex:6];
  1754. [encoder setBytes:&d_state length:sizeof(d_state) atIndex:7];
  1755. [encoder setBytes:&d_inner length:sizeof(d_inner) atIndex:8];
  1756. [encoder setBytes:&n_seq_tokens length:sizeof(n_seq_tokens) atIndex:9];
  1757. [encoder setBytes:&n_seqs length:sizeof(n_seqs) atIndex:10];
  1758. [encoder setBytes:&nb00 length:sizeof(nb00) atIndex:11];
  1759. [encoder setBytes:&nb01 length:sizeof(nb01) atIndex:12];
  1760. [encoder setBytes:&nb02 length:sizeof(nb02) atIndex:13];
  1761. [encoder setBytes:&nb10 length:sizeof(nb10) atIndex:14];
  1762. [encoder setBytes:&nb11 length:sizeof(nb11) atIndex:15];
  1763. [encoder setBytes:&nb12 length:sizeof(nb12) atIndex:16];
  1764. [encoder setBytes:&nb13 length:sizeof(nb13) atIndex:17];
  1765. [encoder setBytes:&nb20 length:sizeof(nb20) atIndex:18];
  1766. [encoder setBytes:&nb21 length:sizeof(nb21) atIndex:19];
  1767. [encoder setBytes:&nb22 length:sizeof(nb22) atIndex:20];
  1768. [encoder setBytes:&nb30 length:sizeof(nb30) atIndex:21];
  1769. [encoder setBytes:&nb31 length:sizeof(nb31) atIndex:22];
  1770. [encoder setBytes:&nb40 length:sizeof(nb40) atIndex:23];
  1771. [encoder setBytes:&nb41 length:sizeof(nb41) atIndex:24];
  1772. [encoder setBytes:&nb42 length:sizeof(nb42) atIndex:25];
  1773. [encoder setBytes:&nb50 length:sizeof(nb50) atIndex:26];
  1774. [encoder setBytes:&nb51 length:sizeof(nb51) atIndex:27];
  1775. [encoder setBytes:&nb52 length:sizeof(nb52) atIndex:28];
  1776. [encoder dispatchThreadgroups:MTLSizeMake(d_inner, n_seqs, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  1777. } break;
  1778. case GGML_OP_MUL_MAT:
  1779. {
  1780. GGML_ASSERT(ne00 == ne10);
  1781. GGML_ASSERT(ne12 % ne02 == 0);
  1782. GGML_ASSERT(ne13 % ne03 == 0);
  1783. const uint r2 = ne12/ne02;
  1784. const uint r3 = ne13/ne03;
  1785. // find the break-even point where the matrix-matrix kernel becomes more efficient compared
  1786. // to the matrix-vector kernel
  1787. const int ne11_mm_min = 4;
  1788. // first try to use small-batch mat-mv kernels
  1789. // these should be efficient for BS [2, ~8]
  1790. if (src1t == GGML_TYPE_F32 && (ne00%256 == 0) &&
  1791. (
  1792. (
  1793. (
  1794. src0t == GGML_TYPE_F16 || // TODO: helper function
  1795. src0t == GGML_TYPE_Q4_0 ||
  1796. src0t == GGML_TYPE_Q4_1 ||
  1797. src0t == GGML_TYPE_Q5_0 ||
  1798. src0t == GGML_TYPE_Q5_1 ||
  1799. src0t == GGML_TYPE_Q8_0 ||
  1800. src0t == GGML_TYPE_IQ4_NL ||
  1801. false) && (ne11 >= 2 && ne11 <= 8)
  1802. ) ||
  1803. (
  1804. (
  1805. src0t == GGML_TYPE_Q4_K ||
  1806. src0t == GGML_TYPE_Q5_K ||
  1807. src0t == GGML_TYPE_Q6_K ||
  1808. false) && (ne11 >= 4 && ne11 <= 8)
  1809. )
  1810. )
  1811. ) {
  1812. // TODO: determine the optimal parameters based on grid utilization
  1813. // I still don't know why we should not always use the maximum available threads:
  1814. //
  1815. // nsg = pipeline.maxTotalThreadsPerThreadgroup / 32
  1816. //
  1817. // my current hypothesis is that the work grid is not evenly divisible for different nsg
  1818. // values and there can be some tail effects when nsg is high. need to confirm this
  1819. //
  1820. const int nsg = 2; // num simdgroups per threadgroup
  1821. const int nxpsg = ne11 < 3 ? 16 : 8; // num threads along row per simdgroup
  1822. const int nypsg = 32/nxpsg; // num threads along col per simdgroup (i.e. a simdgroup processes that many src0 rows at a time)
  1823. const int r0ptg = nypsg*nsg; // num src0 rows per threadgroup
  1824. int r1ptg = 4; // num src1 rows per threadgroup
  1825. // note: not sure how optimal are those across all different hardware. there might be someting cleverer
  1826. switch (ne11) {
  1827. case 2:
  1828. r1ptg = 2; break;
  1829. case 3:
  1830. case 6:
  1831. r1ptg = 3; break;
  1832. case 4:
  1833. case 7:
  1834. case 8:
  1835. r1ptg = 4; break;
  1836. case 5:
  1837. r1ptg = 5; break;
  1838. };
  1839. id<MTLComputePipelineState> pipeline = nil;
  1840. switch (src0->type) {
  1841. case GGML_TYPE_F16:
  1842. switch (r1ptg) {
  1843. case 2: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_F16_F32_R1_2].pipeline; break;
  1844. case 3: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_F16_F32_R1_3].pipeline; break;
  1845. case 4: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_F16_F32_R1_4].pipeline; break;
  1846. case 5: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_F16_F32_R1_5].pipeline; break;
  1847. default: GGML_ABORT("not implemented");
  1848. } break;
  1849. case GGML_TYPE_Q4_0:
  1850. switch (r1ptg) {
  1851. case 2: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q4_0_F32_R1_2].pipeline; break;
  1852. case 3: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q4_0_F32_R1_3].pipeline; break;
  1853. case 4: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q4_0_F32_R1_4].pipeline; break;
  1854. case 5: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q4_0_F32_R1_5].pipeline; break;
  1855. default: GGML_ABORT("not implemented");
  1856. } break;
  1857. case GGML_TYPE_Q4_1:
  1858. switch (r1ptg) {
  1859. case 2: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q4_1_F32_R1_2].pipeline; break;
  1860. case 3: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q4_1_F32_R1_3].pipeline; break;
  1861. case 4: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q4_1_F32_R1_4].pipeline; break;
  1862. case 5: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q4_1_F32_R1_5].pipeline; break;
  1863. default: GGML_ABORT("not implemented");
  1864. } break;
  1865. case GGML_TYPE_Q5_0:
  1866. switch (r1ptg) {
  1867. case 2: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q5_0_F32_R1_2].pipeline; break;
  1868. case 3: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q5_0_F32_R1_3].pipeline; break;
  1869. case 4: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q5_0_F32_R1_4].pipeline; break;
  1870. case 5: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q5_0_F32_R1_5].pipeline; break;
  1871. default: GGML_ABORT("not implemented");
  1872. } break;
  1873. case GGML_TYPE_Q5_1:
  1874. switch (r1ptg) {
  1875. case 2: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q5_1_F32_R1_2].pipeline; break;
  1876. case 3: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q5_1_F32_R1_3].pipeline; break;
  1877. case 4: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q5_1_F32_R1_4].pipeline; break;
  1878. case 5: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q5_1_F32_R1_5].pipeline; break;
  1879. default: GGML_ABORT("not implemented");
  1880. } break;
  1881. case GGML_TYPE_Q8_0:
  1882. switch (r1ptg) {
  1883. case 2: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q8_0_F32_R1_2].pipeline; break;
  1884. case 3: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q8_0_F32_R1_3].pipeline; break;
  1885. case 4: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q8_0_F32_R1_4].pipeline; break;
  1886. case 5: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q8_0_F32_R1_5].pipeline; break;
  1887. default: GGML_ABORT("not implemented");
  1888. } break;
  1889. case GGML_TYPE_Q4_K:
  1890. switch (r1ptg) {
  1891. case 2: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q4_K_F32_R1_2].pipeline; break;
  1892. case 3: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q4_K_F32_R1_3].pipeline; break;
  1893. case 4: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q4_K_F32_R1_4].pipeline; break;
  1894. case 5: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q4_K_F32_R1_5].pipeline; break;
  1895. default: GGML_ABORT("not implemented");
  1896. } break;
  1897. case GGML_TYPE_Q5_K:
  1898. switch (r1ptg) {
  1899. case 2: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q5_K_F32_R1_2].pipeline; break;
  1900. case 3: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q5_K_F32_R1_3].pipeline; break;
  1901. case 4: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q5_K_F32_R1_4].pipeline; break;
  1902. case 5: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q5_K_F32_R1_5].pipeline; break;
  1903. default: GGML_ABORT("not implemented");
  1904. } break;
  1905. case GGML_TYPE_Q6_K:
  1906. switch (r1ptg) {
  1907. case 2: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q6_K_F32_R1_2].pipeline; break;
  1908. case 3: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q6_K_F32_R1_3].pipeline; break;
  1909. case 4: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q6_K_F32_R1_4].pipeline; break;
  1910. case 5: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_Q6_K_F32_R1_5].pipeline; break;
  1911. default: GGML_ABORT("not implemented");
  1912. } break;
  1913. case GGML_TYPE_IQ4_NL:
  1914. switch (r1ptg) {
  1915. case 2: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_IQ4_NL_F32_R1_2].pipeline; break;
  1916. case 3: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_IQ4_NL_F32_R1_3].pipeline; break;
  1917. case 4: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_IQ4_NL_F32_R1_4].pipeline; break;
  1918. case 5: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_EXT_IQ4_NL_F32_R1_5].pipeline; break;
  1919. default: GGML_ABORT("not implemented");
  1920. } break;
  1921. default: GGML_ABORT("not implemented");
  1922. }
  1923. ggml_metal_kargs_mul_mv_ext args = {
  1924. /*.ne00 =*/ ne00,
  1925. /*.ne01 =*/ ne01,
  1926. /*.ne02 =*/ ne02,
  1927. /*.nb00 =*/ nb00,
  1928. /*.nb01 =*/ nb01,
  1929. /*.nb02 =*/ nb02,
  1930. /*.nb03 =*/ nb03,
  1931. /*.ne10 =*/ ne10,
  1932. /*.ne11 =*/ ne11,
  1933. /*.ne12 =*/ ne12,
  1934. /*.nb10 =*/ nb10,
  1935. /*.nb11 =*/ nb11,
  1936. /*.nb12 =*/ nb12,
  1937. /*.nb13 =*/ nb13,
  1938. /*.ne0 =*/ ne0,
  1939. /*.ne1 =*/ ne1,
  1940. /*.r2 =*/ r2,
  1941. /*.r3 =*/ r3,
  1942. /*.nsg =*/ nsg,
  1943. /*.nxpsg =*/ nxpsg,
  1944. /*.r1ptg =*/ r1ptg,
  1945. };
  1946. [encoder setComputePipelineState:pipeline];
  1947. [encoder setBytes:&args length:sizeof(args) atIndex:0];
  1948. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:1];
  1949. [encoder setBuffer:id_src1 offset:offs_src1 atIndex:2];
  1950. [encoder setBuffer:id_dst offset:offs_dst atIndex:3];
  1951. //printf("ne01 = %lld nr0ptg = %d\n", ne01, nr0ptg);
  1952. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + r0ptg - 1)/r0ptg, (ne11 + r1ptg - 1)/r1ptg, ne12*ne13) threadsPerThreadgroup:MTLSizeMake(32, nsg, 1)];
  1953. } else
  1954. // for now the matrix-matrix multiplication kernel only works on A14+/M1+ SoCs
  1955. // AMD GPU and older A-chips will reuse matrix-vector multiplication kernel
  1956. if ([device supportsFamily:MTLGPUFamilyApple7] &&
  1957. !ggml_is_transposed(src0) &&
  1958. !ggml_is_transposed(src1) &&
  1959. src1t == GGML_TYPE_F32 &&
  1960. ne00 % 32 == 0 && ne00 >= 64 &&
  1961. (ne11 > ne11_mm_min || (ggml_is_quantized(src0t) && ne12 > 1))) {
  1962. //printf("matrix: ne00 = %6d, ne01 = %6d, ne02 = %6d, ne11 = %6d, ne12 = %6d\n", ne00, ne01, ne02, ne11, ne12);
  1963. // some Metal matrix data types require aligned pointers
  1964. // ref: https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf (Table 2.5)
  1965. switch (src0->type) {
  1966. case GGML_TYPE_F32: GGML_ASSERT(nb01 % 16 == 0); break;
  1967. case GGML_TYPE_F16: GGML_ASSERT(nb01 % 8 == 0); break;
  1968. case GGML_TYPE_BF16: GGML_ASSERT(nb01 % 8 == 0); break;
  1969. default: break;
  1970. }
  1971. id<MTLComputePipelineState> pipeline = nil;
  1972. switch (src0->type) {
  1973. case GGML_TYPE_F32: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_F32_F32 ].pipeline; break;
  1974. case GGML_TYPE_F16: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_F16_F32 ].pipeline; break;
  1975. case GGML_TYPE_BF16: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_BF16_F32 ].pipeline; break;
  1976. case GGML_TYPE_Q4_0: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_Q4_0_F32 ].pipeline; break;
  1977. case GGML_TYPE_Q4_1: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_Q4_1_F32 ].pipeline; break;
  1978. case GGML_TYPE_Q5_0: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_Q5_0_F32 ].pipeline; break;
  1979. case GGML_TYPE_Q5_1: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_Q5_1_F32 ].pipeline; break;
  1980. case GGML_TYPE_Q8_0: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_Q8_0_F32 ].pipeline; break;
  1981. case GGML_TYPE_Q2_K: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_Q2_K_F32 ].pipeline; break;
  1982. case GGML_TYPE_Q3_K: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_Q3_K_F32 ].pipeline; break;
  1983. case GGML_TYPE_Q4_K: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_Q4_K_F32 ].pipeline; break;
  1984. case GGML_TYPE_Q5_K: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_Q5_K_F32 ].pipeline; break;
  1985. case GGML_TYPE_Q6_K: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_Q6_K_F32 ].pipeline; break;
  1986. case GGML_TYPE_IQ2_XXS: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_IQ2_XXS_F32].pipeline; break;
  1987. case GGML_TYPE_IQ2_XS: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_IQ2_XS_F32 ].pipeline; break;
  1988. case GGML_TYPE_IQ3_XXS: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_IQ3_XXS_F32].pipeline; break;
  1989. case GGML_TYPE_IQ3_S: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_IQ3_S_F32 ].pipeline; break;
  1990. case GGML_TYPE_IQ2_S: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_IQ2_S_F32 ].pipeline; break;
  1991. case GGML_TYPE_IQ1_S: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_IQ1_S_F32 ].pipeline; break;
  1992. case GGML_TYPE_IQ1_M: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_IQ1_M_F32 ].pipeline; break;
  1993. case GGML_TYPE_IQ4_NL: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_IQ4_NL_F32 ].pipeline; break;
  1994. case GGML_TYPE_IQ4_XS: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_IQ4_XS_F32 ].pipeline; break;
  1995. default: GGML_ABORT("MUL MAT-MAT not implemented");
  1996. }
  1997. ggml_metal_kargs_mul_mm args = {
  1998. /*.ne00 =*/ ne00,
  1999. /*.ne02 =*/ ne02,
  2000. /*.nb01 =*/ nb01,
  2001. /*.nb02 =*/ nb02,
  2002. /*.nb03 =*/ nb03,
  2003. /*.ne12 =*/ ne12,
  2004. /*.nb10 =*/ nb10,
  2005. /*.nb11 =*/ nb11,
  2006. /*.nb12 =*/ nb12,
  2007. /*.nb13 =*/ nb13,
  2008. /*.ne0 =*/ ne0,
  2009. /*.ne1 =*/ ne1,
  2010. /*.r2 =*/ r2,
  2011. /*.r3 =*/ r3,
  2012. };
  2013. [encoder setComputePipelineState:pipeline];
  2014. [encoder setBytes:&args length:sizeof(args) atIndex:0];
  2015. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:1];
  2016. [encoder setBuffer:id_src1 offset:offs_src1 atIndex:2];
  2017. [encoder setBuffer:id_dst offset:offs_dst atIndex:3];
  2018. [encoder setThreadgroupMemoryLength:8192 atIndex:0];
  2019. [encoder dispatchThreadgroups:MTLSizeMake( (ne11 + 31)/32, (ne01 + 63)/64, ne12*ne13) threadsPerThreadgroup:MTLSizeMake(128, 1, 1)];
  2020. } else {
  2021. int nth0 = 32;
  2022. int nth1 = 1;
  2023. int nrows = 1;
  2024. //printf("vector: ne00 = %6d, ne01 = %6d, ne02 = %6d, ne11 = %6d, ne12 = %6d\n", ne00, ne01, ne02, ne11, ne12);
  2025. id<MTLComputePipelineState> pipeline = nil;
  2026. // use custom matrix x vector kernel
  2027. switch (src0t) {
  2028. case GGML_TYPE_F32:
  2029. {
  2030. GGML_ASSERT(src1t == GGML_TYPE_F32);
  2031. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_F32_F32].pipeline;
  2032. nrows = 4;
  2033. } break;
  2034. case GGML_TYPE_F16:
  2035. {
  2036. nth0 = 32;
  2037. nth1 = 1;
  2038. if (src1t == GGML_TYPE_F32) {
  2039. if (ne11 * ne12 < 4) {
  2040. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_F16_F32_1ROW].pipeline;
  2041. } else if (ne00 >= 128 && ne01 >= 8 && ne00%4 == 0) {
  2042. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_F16_F32_L4].pipeline;
  2043. nrows = ne11;
  2044. } else {
  2045. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_F16_F32].pipeline;
  2046. nrows = 4;
  2047. }
  2048. } else {
  2049. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_F16_F16].pipeline;
  2050. nrows = 4;
  2051. }
  2052. } break;
  2053. case GGML_TYPE_BF16:
  2054. {
  2055. nth0 = 32;
  2056. nth1 = 1;
  2057. if (src1t == GGML_TYPE_F32) {
  2058. if (ne11 * ne12 < 4) {
  2059. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_BF16_F32_1ROW].pipeline;
  2060. } else if (ne00 >= 128 && ne01 >= 8 && ne00%4 == 0) {
  2061. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_BF16_F32_L4].pipeline;
  2062. nrows = ne11;
  2063. } else {
  2064. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_BF16_F32].pipeline;
  2065. nrows = 4;
  2066. }
  2067. } else {
  2068. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_BF16_BF16].pipeline;
  2069. nrows = 4;
  2070. }
  2071. } break;
  2072. case GGML_TYPE_Q4_0:
  2073. {
  2074. nth0 = 8;
  2075. nth1 = 8;
  2076. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_Q4_0_F32].pipeline;
  2077. } break;
  2078. case GGML_TYPE_Q4_1:
  2079. {
  2080. nth0 = 8;
  2081. nth1 = 8;
  2082. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_Q4_1_F32].pipeline;
  2083. } break;
  2084. case GGML_TYPE_Q5_0:
  2085. {
  2086. nth0 = 8;
  2087. nth1 = 8;
  2088. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_Q5_0_F32].pipeline;
  2089. } break;
  2090. case GGML_TYPE_Q5_1:
  2091. {
  2092. nth0 = 8;
  2093. nth1 = 8;
  2094. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_Q5_1_F32].pipeline;
  2095. } break;
  2096. case GGML_TYPE_Q8_0:
  2097. {
  2098. nth0 = 8;
  2099. nth1 = 8;
  2100. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_Q8_0_F32].pipeline;
  2101. } break;
  2102. case GGML_TYPE_Q2_K:
  2103. {
  2104. nth0 = 2;
  2105. nth1 = 32;
  2106. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_Q2_K_F32].pipeline;
  2107. } break;
  2108. case GGML_TYPE_Q3_K:
  2109. {
  2110. nth0 = 2;
  2111. nth1 = 32;
  2112. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_Q3_K_F32].pipeline;
  2113. } break;
  2114. case GGML_TYPE_Q4_K:
  2115. {
  2116. nth0 = 4; //1;
  2117. nth1 = 8; //32;
  2118. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_Q4_K_F32].pipeline;
  2119. } break;
  2120. case GGML_TYPE_Q5_K:
  2121. {
  2122. nth0 = 2;
  2123. nth1 = 32;
  2124. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_Q5_K_F32].pipeline;
  2125. } break;
  2126. case GGML_TYPE_Q6_K:
  2127. {
  2128. nth0 = 2;
  2129. nth1 = 32;
  2130. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_Q6_K_F32].pipeline;
  2131. } break;
  2132. case GGML_TYPE_IQ2_XXS:
  2133. {
  2134. nth0 = 4;
  2135. nth1 = 16;
  2136. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_IQ2_XXS_F32].pipeline;
  2137. } break;
  2138. case GGML_TYPE_IQ2_XS:
  2139. {
  2140. nth0 = 4;
  2141. nth1 = 16;
  2142. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_IQ2_XS_F32].pipeline;
  2143. } break;
  2144. case GGML_TYPE_IQ3_XXS:
  2145. {
  2146. nth0 = 4;
  2147. nth1 = 16;
  2148. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_IQ3_XXS_F32].pipeline;
  2149. } break;
  2150. case GGML_TYPE_IQ3_S:
  2151. {
  2152. nth0 = 4;
  2153. nth1 = 16;
  2154. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_IQ3_S_F32].pipeline;
  2155. } break;
  2156. case GGML_TYPE_IQ2_S:
  2157. {
  2158. nth0 = 4;
  2159. nth1 = 16;
  2160. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_IQ2_S_F32].pipeline;
  2161. } break;
  2162. case GGML_TYPE_IQ1_S:
  2163. {
  2164. nth0 = 4;
  2165. nth1 = 16;
  2166. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_IQ1_S_F32].pipeline;
  2167. } break;
  2168. case GGML_TYPE_IQ1_M:
  2169. {
  2170. nth0 = 4;
  2171. nth1 = 16;
  2172. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_IQ1_M_F32].pipeline;
  2173. } break;
  2174. case GGML_TYPE_IQ4_NL:
  2175. {
  2176. nth0 = 4;
  2177. nth1 = 16;
  2178. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_IQ4_NL_F32].pipeline;
  2179. } break;
  2180. case GGML_TYPE_IQ4_XS:
  2181. {
  2182. nth0 = 4;
  2183. nth1 = 16;
  2184. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_IQ4_XS_F32].pipeline;
  2185. } break;
  2186. default:
  2187. {
  2188. GGML_LOG_ERROR("Asserting on type %d\n", (int)src0t);
  2189. GGML_ABORT("not implemented");
  2190. }
  2191. };
  2192. ggml_metal_kargs_mul_mv args = {
  2193. /*.ne00 =*/ ne00,
  2194. /*.ne01 =*/ ne01,
  2195. /*.ne02 =*/ ne02,
  2196. /*.nb00 =*/ nb00,
  2197. /*.nb01 =*/ nb01,
  2198. /*.nb02 =*/ nb02,
  2199. /*.nb03 =*/ nb03,
  2200. /*.ne10 =*/ ne10,
  2201. /*.ne11 =*/ ne11,
  2202. /*.ne12 =*/ ne12,
  2203. /*.nb10 =*/ nb10,
  2204. /*.nb11 =*/ nb11,
  2205. /*.nb12 =*/ nb12,
  2206. /*.nb13 =*/ nb13,
  2207. /*.ne0 =*/ ne0,
  2208. /*.ne1 =*/ ne1,
  2209. /*.r2 =*/ r2,
  2210. /*.r3 =*/ r3,
  2211. };
  2212. [encoder setComputePipelineState:pipeline];
  2213. [encoder setBytes:&args length:sizeof(args) atIndex:0];
  2214. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:1];
  2215. [encoder setBuffer:id_src1 offset:offs_src1 atIndex:2];
  2216. [encoder setBuffer:id_dst offset:offs_dst atIndex:3];
  2217. if (src0t == GGML_TYPE_Q4_0 || src0t == GGML_TYPE_Q4_1 || src0t == GGML_TYPE_Q5_0 ||
  2218. src0t == GGML_TYPE_Q5_1 || src0t == GGML_TYPE_Q8_0 || src0t == GGML_TYPE_Q2_K ||
  2219. src0t == GGML_TYPE_IQ1_S || src0t == GGML_TYPE_IQ1_M || src0t == GGML_TYPE_IQ2_S) {
  2220. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + 7)/8, ne11, ne12*ne13) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  2221. }
  2222. else if (src0t == GGML_TYPE_IQ2_XXS || src0t == GGML_TYPE_IQ2_XS) {
  2223. const int mem_size = src0t == GGML_TYPE_IQ2_XXS ? 256*8+128 : 512*8+128;
  2224. [encoder setThreadgroupMemoryLength:mem_size atIndex:0];
  2225. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + 7)/8, ne11, ne12*ne13) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  2226. }
  2227. else if (src0t == GGML_TYPE_IQ3_XXS || src0t == GGML_TYPE_IQ3_S) {
  2228. const int mem_size = src0t == GGML_TYPE_IQ3_XXS ? 256*4+128 : 512*4;
  2229. [encoder setThreadgroupMemoryLength:mem_size atIndex:0];
  2230. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + 7)/8, ne11, ne12*ne13) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  2231. }
  2232. else if (src0t == GGML_TYPE_IQ4_NL || src0t == GGML_TYPE_IQ4_XS) {
  2233. const int mem_size = 32*sizeof(float);
  2234. [encoder setThreadgroupMemoryLength:mem_size atIndex:0];
  2235. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + 3)/4, ne11, ne12*ne13) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  2236. }
  2237. else if (src0t == GGML_TYPE_Q4_K) {
  2238. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + 3)/4, ne11, ne12*ne13) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  2239. }
  2240. else if (src0t == GGML_TYPE_Q3_K) {
  2241. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + 3)/4, ne11, ne12*ne13) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  2242. }
  2243. else if (src0t == GGML_TYPE_Q5_K) {
  2244. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + 3)/4, ne11, ne12*ne13) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  2245. }
  2246. else if (src0t == GGML_TYPE_Q6_K) {
  2247. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + 1)/2, ne11, ne12*ne13) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  2248. } else {
  2249. const int64_t ny = (ne11 + nrows - 1)/nrows;
  2250. [encoder dispatchThreadgroups:MTLSizeMake(ne01, ny, ne12*ne13) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  2251. }
  2252. }
  2253. } break;
  2254. case GGML_OP_MUL_MAT_ID:
  2255. {
  2256. const int n_as = src0->ne[2];
  2257. // src2 = ids
  2258. const enum ggml_type src2t = src2->type; GGML_UNUSED(src2t);
  2259. GGML_ASSERT(src2t == GGML_TYPE_I32);
  2260. GGML_ASSERT(!ggml_is_transposed(src0));
  2261. GGML_ASSERT(!ggml_is_transposed(src1));
  2262. GGML_ASSERT(src1t == GGML_TYPE_F32);
  2263. GGML_ASSERT(ne03 == 1);
  2264. GGML_ASSERT(ne13 == 1);
  2265. // find the break-even point where the matrix-matrix kernel becomes more efficient compared
  2266. // to the matrix-vector kernel
  2267. // ne20 = n_used_experts
  2268. // ne21 = n_rows
  2269. const int dst_rows = ne20*ne21;
  2270. const int dst_rows_min = n_as;
  2271. const int dst_rows_max = (device.maxThreadgroupMemoryLength - 32 - 8192)/4;
  2272. // max size of the rowids array in the kernel shared buffer
  2273. GGML_ASSERT(dst_rows <= dst_rows_max);
  2274. // for now the matrix-matrix multiplication kernel only works on A14+/M1+ SoCs
  2275. // AMD GPU and older A-chips will reuse matrix-vector multiplication kernel
  2276. // !!!
  2277. // TODO: for now, always use mat-vec kernels until we figure out how to improve the
  2278. // indirect matrix multiplication
  2279. // !!!
  2280. if ([device supportsFamily:MTLGPUFamilyApple7] &&
  2281. ne00 % 32 == 0 && ne00 >= 64 &&
  2282. dst_rows > dst_rows_min) {
  2283. // some Metal matrix data types require aligned pointers
  2284. // ref: https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf (Table 2.5)
  2285. switch (src0->type) {
  2286. case GGML_TYPE_F32: GGML_ASSERT(nb01 % 16 == 0); break;
  2287. case GGML_TYPE_F16: GGML_ASSERT(nb01 % 8 == 0); break;
  2288. case GGML_TYPE_BF16: GGML_ASSERT(nb01 % 8 == 0); break;
  2289. default: break;
  2290. }
  2291. id<MTLComputePipelineState> pipeline = nil;
  2292. switch (src0->type) {
  2293. case GGML_TYPE_F32: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_F32_F32 ].pipeline; break;
  2294. case GGML_TYPE_F16: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_F16_F32 ].pipeline; break;
  2295. case GGML_TYPE_BF16: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_BF16_F32 ].pipeline; break;
  2296. case GGML_TYPE_Q4_0: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q4_0_F32 ].pipeline; break;
  2297. case GGML_TYPE_Q4_1: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q4_1_F32 ].pipeline; break;
  2298. case GGML_TYPE_Q5_0: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q5_0_F32 ].pipeline; break;
  2299. case GGML_TYPE_Q5_1: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q5_1_F32 ].pipeline; break;
  2300. case GGML_TYPE_Q8_0: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q8_0_F32 ].pipeline; break;
  2301. case GGML_TYPE_Q2_K: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q2_K_F32 ].pipeline; break;
  2302. case GGML_TYPE_Q3_K: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q3_K_F32 ].pipeline; break;
  2303. case GGML_TYPE_Q4_K: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q4_K_F32 ].pipeline; break;
  2304. case GGML_TYPE_Q5_K: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q5_K_F32 ].pipeline; break;
  2305. case GGML_TYPE_Q6_K: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q6_K_F32 ].pipeline; break;
  2306. case GGML_TYPE_IQ2_XXS: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ2_XXS_F32].pipeline; break;
  2307. case GGML_TYPE_IQ2_XS: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ2_XS_F32 ].pipeline; break;
  2308. case GGML_TYPE_IQ3_XXS: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ3_XXS_F32].pipeline; break;
  2309. case GGML_TYPE_IQ3_S: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ3_S_F32 ].pipeline; break;
  2310. case GGML_TYPE_IQ2_S: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ2_S_F32 ].pipeline; break;
  2311. case GGML_TYPE_IQ1_S: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ1_S_F32 ].pipeline; break;
  2312. case GGML_TYPE_IQ1_M: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ1_M_F32 ].pipeline; break;
  2313. case GGML_TYPE_IQ4_NL: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ4_NL_F32 ].pipeline; break;
  2314. case GGML_TYPE_IQ4_XS: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ4_XS_F32 ].pipeline; break;
  2315. default: GGML_ABORT("MUL_MAT_ID not implemented");
  2316. }
  2317. ggml_metal_kargs_mul_mm_id args = {
  2318. /*.nei0 =*/ ne20,
  2319. /*.nei1 =*/ ne21,
  2320. /*.nbi1 =*/ nb21,
  2321. /*.ne00 =*/ ne00,
  2322. /*.ne02 =*/ ne02,
  2323. /*.nb01 =*/ nb01,
  2324. /*.nb02 =*/ nb02,
  2325. /*.ne11 =*/ ne11,
  2326. /*.ne12 =*/ ne12,
  2327. /*.ne13 =*/ ne13,
  2328. /*.nb10 =*/ nb10,
  2329. /*.nb11 =*/ nb11,
  2330. /*.nb12 =*/ nb12,
  2331. /*.ne0 =*/ ne0,
  2332. /*.ne1 =*/ ne1,
  2333. };
  2334. [encoder setComputePipelineState:pipeline];
  2335. [encoder setBytes:&args length:sizeof(args) atIndex:0];
  2336. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:1];
  2337. [encoder setBuffer:id_src1 offset:offs_src1 atIndex:2];
  2338. [encoder setBuffer:id_dst offset:offs_dst atIndex:3];
  2339. [encoder setBuffer:id_src2 offset:offs_src2 atIndex:4];
  2340. [encoder setThreadgroupMemoryLength:GGML_PAD(8192 + dst_rows*4/*sizeof(ushort2)*/, 16) atIndex:0];
  2341. [encoder dispatchThreadgroups:MTLSizeMake((ne21 + 31)/32, (ne01 + 63)/64, n_as) threadsPerThreadgroup:MTLSizeMake(128, 1, 1)];
  2342. } else {
  2343. int nth0 = 32;
  2344. int nth1 = 1;
  2345. int nrows = 1;
  2346. //printf("vector: ne00 = %6d, ne01 = %6d, ne02 = %6d, ne11 = %6d, ne12 = %6d\n", ne00, ne01, ne02, ne11, ne12);
  2347. id<MTLComputePipelineState> pipeline = nil;
  2348. // use custom matrix x vector kernel
  2349. switch (src0t) {
  2350. case GGML_TYPE_F32:
  2351. {
  2352. GGML_ASSERT(src1t == GGML_TYPE_F32);
  2353. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_F32_F32].pipeline;
  2354. } break;
  2355. case GGML_TYPE_F16:
  2356. {
  2357. GGML_ASSERT(src1t == GGML_TYPE_F32);
  2358. nth0 = 32;
  2359. nth1 = 1;
  2360. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_F16_F32].pipeline;
  2361. } break;
  2362. case GGML_TYPE_BF16:
  2363. {
  2364. GGML_ASSERT(src1t == GGML_TYPE_F32);
  2365. nth0 = 32;
  2366. nth1 = 1;
  2367. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_BF16_F32].pipeline;
  2368. } break;
  2369. case GGML_TYPE_Q4_0:
  2370. {
  2371. nth0 = 8;
  2372. nth1 = 8;
  2373. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q4_0_F32].pipeline;
  2374. } break;
  2375. case GGML_TYPE_Q4_1:
  2376. {
  2377. nth0 = 8;
  2378. nth1 = 8;
  2379. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q4_1_F32].pipeline;
  2380. } break;
  2381. case GGML_TYPE_Q5_0:
  2382. {
  2383. nth0 = 8;
  2384. nth1 = 8;
  2385. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q5_0_F32].pipeline;
  2386. } break;
  2387. case GGML_TYPE_Q5_1:
  2388. {
  2389. nth0 = 8;
  2390. nth1 = 8;
  2391. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q5_1_F32].pipeline;
  2392. } break;
  2393. case GGML_TYPE_Q8_0:
  2394. {
  2395. nth0 = 8;
  2396. nth1 = 8;
  2397. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q8_0_F32].pipeline;
  2398. } break;
  2399. case GGML_TYPE_Q2_K:
  2400. {
  2401. nth0 = 2;
  2402. nth1 = 32;
  2403. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q2_K_F32].pipeline;
  2404. } break;
  2405. case GGML_TYPE_Q3_K:
  2406. {
  2407. nth0 = 2;
  2408. nth1 = 32;
  2409. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q3_K_F32].pipeline;
  2410. } break;
  2411. case GGML_TYPE_Q4_K:
  2412. {
  2413. nth0 = 4; //1;
  2414. nth1 = 8; //32;
  2415. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q4_K_F32].pipeline;
  2416. } break;
  2417. case GGML_TYPE_Q5_K:
  2418. {
  2419. nth0 = 2;
  2420. nth1 = 32;
  2421. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q5_K_F32].pipeline;
  2422. } break;
  2423. case GGML_TYPE_Q6_K:
  2424. {
  2425. nth0 = 2;
  2426. nth1 = 32;
  2427. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q6_K_F32].pipeline;
  2428. } break;
  2429. case GGML_TYPE_IQ2_XXS:
  2430. {
  2431. nth0 = 4;
  2432. nth1 = 16;
  2433. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ2_XXS_F32].pipeline;
  2434. } break;
  2435. case GGML_TYPE_IQ2_XS:
  2436. {
  2437. nth0 = 4;
  2438. nth1 = 16;
  2439. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ2_XS_F32].pipeline;
  2440. } break;
  2441. case GGML_TYPE_IQ3_XXS:
  2442. {
  2443. nth0 = 4;
  2444. nth1 = 16;
  2445. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ3_XXS_F32].pipeline;
  2446. } break;
  2447. case GGML_TYPE_IQ3_S:
  2448. {
  2449. nth0 = 4;
  2450. nth1 = 16;
  2451. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ3_S_F32].pipeline;
  2452. } break;
  2453. case GGML_TYPE_IQ2_S:
  2454. {
  2455. nth0 = 4;
  2456. nth1 = 16;
  2457. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ2_S_F32].pipeline;
  2458. } break;
  2459. case GGML_TYPE_IQ1_S:
  2460. {
  2461. nth0 = 4;
  2462. nth1 = 16;
  2463. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ1_S_F32].pipeline;
  2464. } break;
  2465. case GGML_TYPE_IQ1_M:
  2466. {
  2467. nth0 = 4;
  2468. nth1 = 16;
  2469. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ1_M_F32].pipeline;
  2470. } break;
  2471. case GGML_TYPE_IQ4_NL:
  2472. {
  2473. nth0 = 4;
  2474. nth1 = 16;
  2475. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ4_NL_F32].pipeline;
  2476. } break;
  2477. case GGML_TYPE_IQ4_XS:
  2478. {
  2479. nth0 = 4;
  2480. nth1 = 16;
  2481. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ4_XS_F32].pipeline;
  2482. } break;
  2483. default:
  2484. {
  2485. GGML_LOG_ERROR("Asserting on type %d\n", (int)src2t);
  2486. GGML_ABORT("not implemented");
  2487. }
  2488. };
  2489. if (ggml_is_quantized(src0t)) {
  2490. GGML_ASSERT(ne00 >= nth0*nth1);
  2491. }
  2492. ggml_metal_kargs_mul_mv_id args = {
  2493. /*.nei0 =*/ ne20,
  2494. /*.nei1 =*/ ne21,
  2495. /*.nbi1 =*/ nb21,
  2496. /*.ne00 =*/ ne00,
  2497. /*.ne01 =*/ ne01,
  2498. /*.ne02 =*/ ne02,
  2499. /*.nb00 =*/ nb00,
  2500. /*.nb01 =*/ nb01,
  2501. /*.nb02 =*/ nb02,
  2502. /*.ne10 =*/ ne10,
  2503. /*.ne11 =*/ ne11,
  2504. /*.ne12 =*/ ne12,
  2505. /*.ne13 =*/ ne13,
  2506. /*.nb10 =*/ nb10,
  2507. /*.nb11 =*/ nb11,
  2508. /*.nb12 =*/ nb12,
  2509. /*.ne0 =*/ ne0,
  2510. /*.ne1 =*/ ne1,
  2511. /*.nb1 =*/ nb1,
  2512. };
  2513. [encoder setComputePipelineState:pipeline];
  2514. [encoder setBytes:&args length:sizeof(args) atIndex:0];
  2515. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:1];
  2516. [encoder setBuffer:id_src1 offset:offs_src1 atIndex:2];
  2517. [encoder setBuffer:id_dst offset:offs_dst atIndex:3];
  2518. [encoder setBuffer:id_src2 offset:offs_src2 atIndex:4];
  2519. const int64_t _ne1 = 1;
  2520. const int tgz = dst_rows;
  2521. if (src0t == GGML_TYPE_Q4_0 || src0t == GGML_TYPE_Q4_1 || src0t == GGML_TYPE_Q5_0 ||
  2522. src0t == GGML_TYPE_Q5_1 || src0t == GGML_TYPE_Q8_0 || src0t == GGML_TYPE_Q2_K ||
  2523. src0t == GGML_TYPE_IQ1_S || src0t == GGML_TYPE_IQ1_M || src0t == GGML_TYPE_IQ2_S) {
  2524. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + 7)/8, _ne1, tgz) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  2525. }
  2526. else if (src0t == GGML_TYPE_IQ2_XXS || src0t == GGML_TYPE_IQ2_XS) {
  2527. const int mem_size = src0t == GGML_TYPE_IQ2_XXS ? 256*8+128 : 512*8+128;
  2528. [encoder setThreadgroupMemoryLength:mem_size atIndex:0];
  2529. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + 7)/8, _ne1, tgz) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  2530. }
  2531. else if (src0t == GGML_TYPE_IQ3_XXS || src0t == GGML_TYPE_IQ3_S) {
  2532. const int mem_size = src0t == GGML_TYPE_IQ3_XXS ? 256*4+128 : 512*4;
  2533. [encoder setThreadgroupMemoryLength:mem_size atIndex:0];
  2534. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + 7)/8, _ne1, tgz) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  2535. }
  2536. else if (src0t == GGML_TYPE_IQ4_NL || src0t == GGML_TYPE_IQ4_XS) {
  2537. const int mem_size = 32*sizeof(float);
  2538. [encoder setThreadgroupMemoryLength:mem_size atIndex:0];
  2539. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + 3)/4, _ne1, tgz) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  2540. }
  2541. else if (src0t == GGML_TYPE_Q4_K) {
  2542. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + 3)/4, _ne1, tgz) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  2543. }
  2544. else if (src0t == GGML_TYPE_Q3_K) {
  2545. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + 3)/4, _ne1, tgz) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  2546. }
  2547. else if (src0t == GGML_TYPE_Q5_K) {
  2548. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + 3)/4, _ne1, tgz) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  2549. }
  2550. else if (src0t == GGML_TYPE_Q6_K) {
  2551. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + 1)/2, _ne1, tgz) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  2552. } else {
  2553. const int64_t ny = (_ne1 + nrows - 1)/nrows; // = _ne1
  2554. [encoder dispatchThreadgroups:MTLSizeMake(ne01, ny, tgz) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  2555. }
  2556. }
  2557. } break;
  2558. case GGML_OP_GET_ROWS:
  2559. {
  2560. id<MTLComputePipelineState> pipeline = nil;
  2561. switch (src0->type) {
  2562. case GGML_TYPE_F32: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_F32 ].pipeline; break;
  2563. case GGML_TYPE_F16: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_F16 ].pipeline; break;
  2564. case GGML_TYPE_BF16: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_BF16 ].pipeline; break;
  2565. case GGML_TYPE_Q4_0: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_Q4_0 ].pipeline; break;
  2566. case GGML_TYPE_Q4_1: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_Q4_1 ].pipeline; break;
  2567. case GGML_TYPE_Q5_0: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_Q5_0 ].pipeline; break;
  2568. case GGML_TYPE_Q5_1: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_Q5_1 ].pipeline; break;
  2569. case GGML_TYPE_Q8_0: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_Q8_0 ].pipeline; break;
  2570. case GGML_TYPE_Q2_K: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_Q2_K ].pipeline; break;
  2571. case GGML_TYPE_Q3_K: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_Q3_K ].pipeline; break;
  2572. case GGML_TYPE_Q4_K: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_Q4_K ].pipeline; break;
  2573. case GGML_TYPE_Q5_K: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_Q5_K ].pipeline; break;
  2574. case GGML_TYPE_Q6_K: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_Q6_K ].pipeline; break;
  2575. case GGML_TYPE_IQ2_XXS: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ2_XXS].pipeline; break;
  2576. case GGML_TYPE_IQ2_XS: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ2_XS ].pipeline; break;
  2577. case GGML_TYPE_IQ3_XXS: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ3_XXS].pipeline; break;
  2578. case GGML_TYPE_IQ3_S: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ3_S ].pipeline; break;
  2579. case GGML_TYPE_IQ2_S: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ2_S ].pipeline; break;
  2580. case GGML_TYPE_IQ1_S: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ1_S ].pipeline; break;
  2581. case GGML_TYPE_IQ1_M: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ1_M ].pipeline; break;
  2582. case GGML_TYPE_IQ4_NL: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ4_NL ].pipeline; break;
  2583. case GGML_TYPE_IQ4_XS: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ4_XS ].pipeline; break;
  2584. case GGML_TYPE_I32: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_I32 ].pipeline; break;
  2585. default: GGML_ABORT("not implemented");
  2586. }
  2587. // TODO: add ggml_metal_kargs struct
  2588. [encoder setComputePipelineState:pipeline];
  2589. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  2590. [encoder setBuffer:id_src1 offset:offs_src1 atIndex:1];
  2591. [encoder setBuffer:id_dst offset:offs_dst atIndex:2];
  2592. [encoder setBytes:&ne00 length:sizeof( int64_t) atIndex:3];
  2593. [encoder setBytes:&nb01 length:sizeof(uint64_t) atIndex:4];
  2594. [encoder setBytes:&nb02 length:sizeof(uint64_t) atIndex:5];
  2595. [encoder setBytes:&ne10 length:sizeof( int64_t) atIndex:6];
  2596. [encoder setBytes:&nb10 length:sizeof( int64_t) atIndex:7];
  2597. [encoder setBytes:&nb11 length:sizeof( int64_t) atIndex:8];
  2598. [encoder setBytes:&nb1 length:sizeof(uint64_t) atIndex:9];
  2599. [encoder setBytes:&nb2 length:sizeof(uint64_t) atIndex:10];
  2600. [encoder dispatchThreadgroups:MTLSizeMake(ne10, ne11, 1) threadsPerThreadgroup:MTLSizeMake(32, 1, 1)];
  2601. } break;
  2602. case GGML_OP_RMS_NORM:
  2603. {
  2604. GGML_ASSERT(ne00 % 4 == 0);
  2605. GGML_ASSERT(ggml_is_contiguous_1(src0));
  2606. float eps;
  2607. memcpy(&eps, dst->op_params, sizeof(float));
  2608. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_RMS_NORM].pipeline;
  2609. int nth = 32; // SIMD width
  2610. while (nth < ne00/4 && nth < (int) pipeline.maxTotalThreadsPerThreadgroup) {
  2611. nth *= 2;
  2612. }
  2613. nth = MIN(nth, ne00/4);
  2614. ggml_metal_kargs_rms_norm args = {
  2615. /*.ne00 =*/ ne00,
  2616. /*.ne00_4 =*/ ne00/4,
  2617. /*.nb01 =*/ nb01,
  2618. /*.eps =*/ eps,
  2619. };
  2620. [encoder setComputePipelineState:pipeline];
  2621. [encoder setBytes:&args length:sizeof(args) atIndex:0];
  2622. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:1];
  2623. [encoder setBuffer:id_dst offset:offs_dst atIndex:2];
  2624. [encoder setThreadgroupMemoryLength:32*sizeof(float) atIndex:0];
  2625. const int64_t nrows = ggml_nrows(src0);
  2626. [encoder dispatchThreadgroups:MTLSizeMake(nrows, 1, 1) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  2627. } break;
  2628. case GGML_OP_GROUP_NORM:
  2629. {
  2630. GGML_ASSERT(ggml_is_contiguous(src0));
  2631. float eps;
  2632. memcpy(&eps, dst->op_params + 1, sizeof(float));
  2633. const int32_t n_groups = ((const int32_t *) dst->op_params)[0];
  2634. int nth = 32; // SIMD width
  2635. //while (nth < ne00/4 && nth < 1024) {
  2636. // nth *= 2;
  2637. //}
  2638. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GROUP_NORM].pipeline;
  2639. // TODO: add ggml_metal_kargs struct
  2640. [encoder setComputePipelineState:pipeline];
  2641. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  2642. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  2643. [encoder setBytes:&ne00 length:sizeof( int64_t) atIndex:2];
  2644. [encoder setBytes:&ne01 length:sizeof( int64_t) atIndex:3];
  2645. [encoder setBytes:&ne02 length:sizeof( int64_t) atIndex:4];
  2646. [encoder setBytes:&nb00 length:sizeof(uint64_t) atIndex:5];
  2647. [encoder setBytes:&nb01 length:sizeof(uint64_t) atIndex:6];
  2648. [encoder setBytes:&nb02 length:sizeof(uint64_t) atIndex:7];
  2649. [encoder setBytes:&n_groups length:sizeof( int32_t) atIndex:8];
  2650. [encoder setBytes:&eps length:sizeof( float) atIndex:9];
  2651. [encoder setThreadgroupMemoryLength:32*sizeof(float) atIndex:0];
  2652. [encoder dispatchThreadgroups:MTLSizeMake(n_groups, 1, 1) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  2653. } break;
  2654. case GGML_OP_NORM:
  2655. {
  2656. GGML_ASSERT(ne00 % 4 == 0);
  2657. GGML_ASSERT(ggml_is_contiguous_1(src0));
  2658. float eps;
  2659. memcpy(&eps, dst->op_params, sizeof(float));
  2660. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_NORM].pipeline;
  2661. int nth = 32; // SIMD width
  2662. while (nth < ne00/4 && nth < (int) pipeline.maxTotalThreadsPerThreadgroup) {
  2663. nth *= 2;
  2664. }
  2665. nth = MIN(nth, ne00/4);
  2666. ggml_metal_kargs_norm args = {
  2667. /*.ne00 =*/ ne00,
  2668. /*.ne00_4 =*/ ne00/4,
  2669. /*.nb01 =*/ nb01,
  2670. /*.eps =*/ eps,
  2671. };
  2672. [encoder setComputePipelineState:pipeline];
  2673. [encoder setBytes:&args length:sizeof(args) atIndex:0];
  2674. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:1];
  2675. [encoder setBuffer:id_dst offset:offs_dst atIndex:2];
  2676. [encoder setThreadgroupMemoryLength:32*sizeof(float) atIndex:0];
  2677. const int64_t nrows = ggml_nrows(src0);
  2678. [encoder dispatchThreadgroups:MTLSizeMake(nrows, 1, 1) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  2679. } break;
  2680. case GGML_OP_ROPE:
  2681. {
  2682. GGML_ASSERT(ne10 == ne02);
  2683. const int nth = MIN(1024, ne00);
  2684. const int n_past = ((const int32_t *) dst->op_params)[0];
  2685. const int n_dims = ((const int32_t *) dst->op_params)[1];
  2686. const int mode = ((const int32_t *) dst->op_params)[2];
  2687. // skip 3, n_ctx, used in GLM RoPE, unimplemented in metal
  2688. const int n_ctx_orig = ((const int32_t *) dst->op_params)[4];
  2689. float freq_base;
  2690. float freq_scale;
  2691. float ext_factor;
  2692. float attn_factor;
  2693. float beta_fast;
  2694. float beta_slow;
  2695. memcpy(&freq_base, (const int32_t *) dst->op_params + 5, sizeof(float));
  2696. memcpy(&freq_scale, (const int32_t *) dst->op_params + 6, sizeof(float));
  2697. memcpy(&ext_factor, (const int32_t *) dst->op_params + 7, sizeof(float));
  2698. memcpy(&attn_factor, (const int32_t *) dst->op_params + 8, sizeof(float));
  2699. memcpy(&beta_fast, (const int32_t *) dst->op_params + 9, sizeof(float));
  2700. memcpy(&beta_slow, (const int32_t *) dst->op_params + 10, sizeof(float));
  2701. const bool is_neox = mode & GGML_ROPE_TYPE_NEOX;
  2702. id<MTLComputePipelineState> pipeline = nil;
  2703. if (!is_neox) {
  2704. switch (src0->type) {
  2705. case GGML_TYPE_F32: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_ROPE_NORM_F32].pipeline; break;
  2706. case GGML_TYPE_F16: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_ROPE_NORM_F16].pipeline; break;
  2707. default: GGML_ABORT("fatal error");
  2708. };
  2709. } else {
  2710. switch (src0->type) {
  2711. case GGML_TYPE_F32: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_ROPE_NEOX_F32].pipeline; break;
  2712. case GGML_TYPE_F16: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_ROPE_NEOX_F16].pipeline; break;
  2713. default: GGML_ABORT("fatal error");
  2714. };
  2715. }
  2716. ggml_metal_kargs_rope args = {
  2717. /*.ne00 =*/ ne00,
  2718. /*.ne01 =*/ ne01,
  2719. /*.ne02 =*/ ne02,
  2720. /*.ne03 =*/ ne03,
  2721. /*.nb00 =*/ nb00,
  2722. /*.nb01 =*/ nb01,
  2723. /*.nb02 =*/ nb02,
  2724. /*.nb03 =*/ nb03,
  2725. /*.ne0 =*/ ne0,
  2726. /*.ne1 =*/ ne1,
  2727. /*.ne2 =*/ ne2,
  2728. /*.ne3 =*/ ne3,
  2729. /*.nb0 =*/ nb0,
  2730. /*.nb1 =*/ nb1,
  2731. /*.nb2 =*/ nb2,
  2732. /*.nb3 =*/ nb3,
  2733. /*.n_past =*/ n_past,
  2734. /*.n_dims =*/ n_dims,
  2735. /*.n_ctx_orig =*/ n_ctx_orig,
  2736. /*.freq_base =*/ freq_base,
  2737. /*.freq_scale =*/ freq_scale,
  2738. /*.ext_factor =*/ ext_factor,
  2739. /*.attn_factor =*/ attn_factor,
  2740. /*.beta_fast =*/ beta_fast,
  2741. /*.beta_slow =*/ beta_slow,
  2742. };
  2743. [encoder setComputePipelineState:pipeline];
  2744. [encoder setBytes:&args length:sizeof(args) atIndex:0];
  2745. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:1];
  2746. [encoder setBuffer:id_src1 offset:offs_src1 atIndex:2];
  2747. if (id_src2 != nil) {
  2748. [encoder setBuffer:id_src2 offset:offs_src2 atIndex:3];
  2749. } else {
  2750. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:3];
  2751. }
  2752. [encoder setBuffer:id_dst offset:offs_dst atIndex:4];
  2753. [encoder dispatchThreadgroups:MTLSizeMake(ne01, ne02, ne03) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  2754. } break;
  2755. case GGML_OP_IM2COL:
  2756. {
  2757. GGML_ASSERT(ggml_is_contiguous(src0));
  2758. GGML_ASSERT(ggml_is_contiguous(src1));
  2759. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  2760. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  2761. GGML_ASSERT( dst->type == GGML_TYPE_F16 || dst->type == GGML_TYPE_F32);
  2762. const int32_t s0 = ((const int32_t *)(dst->op_params))[0];
  2763. const int32_t s1 = ((const int32_t *)(dst->op_params))[1];
  2764. const int32_t p0 = ((const int32_t *)(dst->op_params))[2];
  2765. const int32_t p1 = ((const int32_t *)(dst->op_params))[3];
  2766. const int32_t d0 = ((const int32_t *)(dst->op_params))[4];
  2767. const int32_t d1 = ((const int32_t *)(dst->op_params))[5];
  2768. const bool is_2D = ((const int32_t *)(dst->op_params))[6] == 1;
  2769. const int32_t N = src1->ne[is_2D ? 3 : 2];
  2770. const int32_t IC = src1->ne[is_2D ? 2 : 1];
  2771. const int32_t IH = is_2D ? src1->ne[1] : 1;
  2772. const int32_t IW = src1->ne[0];
  2773. const int32_t KH = is_2D ? src0->ne[1] : 1;
  2774. const int32_t KW = src0->ne[0];
  2775. const int32_t OH = is_2D ? dst->ne[2] : 1;
  2776. const int32_t OW = dst->ne[1];
  2777. const int32_t CHW = IC * KH * KW;
  2778. const int32_t ofs0 = src1->nb[is_2D ? 3 : 2] / 4;
  2779. const int32_t ofs1 = src1->nb[is_2D ? 2 : 1] / 4;
  2780. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_IM2COL_F32].pipeline;
  2781. const bool is_gt_mttpt = ((size_t)(N * KH * KW)) > pipeline.maxTotalThreadsPerThreadgroup;
  2782. switch (dst->type) {
  2783. case GGML_TYPE_F32: {
  2784. pipeline = (is_gt_mttpt ?
  2785. ctx->kernels[GGML_METAL_KERNEL_TYPE_IM2COL_EXT_F32].pipeline
  2786. :
  2787. ctx->kernels[GGML_METAL_KERNEL_TYPE_IM2COL_F32].pipeline);
  2788. } break;
  2789. case GGML_TYPE_F16: {
  2790. pipeline = (is_gt_mttpt ?
  2791. ctx->kernels[GGML_METAL_KERNEL_TYPE_IM2COL_EXT_F16].pipeline
  2792. :
  2793. ctx->kernels[GGML_METAL_KERNEL_TYPE_IM2COL_F16].pipeline);
  2794. } break;
  2795. default: GGML_ABORT("fatal error");
  2796. };
  2797. // TODO: add ggml_metal_kargs struct
  2798. [encoder setComputePipelineState:pipeline];
  2799. [encoder setBuffer:id_src1 offset:offs_src1 atIndex:0];
  2800. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  2801. [encoder setBytes:&ofs0 length:sizeof(int32_t) atIndex:2];
  2802. [encoder setBytes:&ofs1 length:sizeof(int32_t) atIndex:3];
  2803. [encoder setBytes:&IW length:sizeof(int32_t) atIndex:4];
  2804. [encoder setBytes:&IH length:sizeof(int32_t) atIndex:5];
  2805. [encoder setBytes:&CHW length:sizeof(int32_t) atIndex:6];
  2806. [encoder setBytes:&s0 length:sizeof(int32_t) atIndex:7];
  2807. [encoder setBytes:&s1 length:sizeof(int32_t) atIndex:8];
  2808. [encoder setBytes:&p0 length:sizeof(int32_t) atIndex:9];
  2809. [encoder setBytes:&p1 length:sizeof(int32_t) atIndex:10];
  2810. [encoder setBytes:&d0 length:sizeof(int32_t) atIndex:11];
  2811. [encoder setBytes:&d1 length:sizeof(int32_t) atIndex:12];
  2812. if (is_gt_mttpt) {
  2813. [encoder setBytes:&N length:sizeof(int32_t) atIndex:13];
  2814. [encoder setBytes:&KH length:sizeof(int32_t) atIndex:14];
  2815. [encoder setBytes:&KW length:sizeof(int32_t) atIndex:15];
  2816. const uint64_t n_threads = MIN(pipeline.maxTotalThreadsPerThreadgroup, (uint64_t)N);
  2817. const int64_t quotient = N / n_threads + (N % n_threads > 0 ? 1 : 0);
  2818. [encoder dispatchThreadgroups:MTLSizeMake(quotient * CHW, OH, OW) threadsPerThreadgroup:MTLSizeMake(n_threads, 1, 1)];
  2819. } else {
  2820. [encoder dispatchThreadgroups:MTLSizeMake(IC, OH, OW) threadsPerThreadgroup:MTLSizeMake(N, KH, KW)];
  2821. }
  2822. } break;
  2823. case GGML_OP_CONV_TRANSPOSE_1D:
  2824. {
  2825. GGML_ASSERT(ggml_is_contiguous(src0));
  2826. GGML_ASSERT(ggml_is_contiguous(src1));
  2827. GGML_ASSERT(src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_F32);
  2828. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  2829. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  2830. const int32_t s0 = ((const int32_t *)(dst->op_params))[0];
  2831. const int32_t IC = src1->ne[1];
  2832. const int32_t IL = src1->ne[0];
  2833. const int32_t K = src0->ne[0];
  2834. const int32_t OL = dst->ne[0];
  2835. const int32_t OC = dst->ne[1];
  2836. id<MTLComputePipelineState> pipeline;
  2837. switch (src0->type) {
  2838. case GGML_TYPE_F32: {
  2839. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_CONV_TRANSPOSE_1D_F32_F32].pipeline;
  2840. } break;
  2841. case GGML_TYPE_F16: {
  2842. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_CONV_TRANSPOSE_1D_F16_F32].pipeline;
  2843. } break;
  2844. default: GGML_ABORT("fatal error");
  2845. };
  2846. [encoder setComputePipelineState:pipeline];
  2847. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  2848. [encoder setBuffer:id_src1 offset:offs_src1 atIndex:1];
  2849. [encoder setBuffer:id_dst offset:offs_dst atIndex:2];
  2850. [encoder setBytes:&IC length:sizeof( int32_t) atIndex:3];
  2851. [encoder setBytes:&IL length:sizeof( int32_t) atIndex:4];
  2852. [encoder setBytes:&K length:sizeof( int32_t) atIndex:5];
  2853. [encoder setBytes:&s0 length:sizeof( int32_t) atIndex:6];
  2854. [encoder setBytes:&nb0 length:sizeof(uint64_t) atIndex:7];
  2855. [encoder setBytes:&nb1 length:sizeof(uint64_t) atIndex:8];
  2856. [encoder dispatchThreadgroups:MTLSizeMake(OL, OC, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  2857. } break;
  2858. case GGML_OP_UPSCALE:
  2859. {
  2860. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  2861. const float sf0 = (float)ne0/src0->ne[0];
  2862. const float sf1 = (float)ne1/src0->ne[1];
  2863. const float sf2 = (float)ne2/src0->ne[2];
  2864. const float sf3 = (float)ne3/src0->ne[3];
  2865. const id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_UPSCALE_F32].pipeline;
  2866. // TODO: add ggml_metal_kargs struct
  2867. [encoder setComputePipelineState:pipeline];
  2868. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  2869. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  2870. [encoder setBytes:&ne00 length:sizeof(ne00) atIndex:2];
  2871. [encoder setBytes:&ne01 length:sizeof(ne01) atIndex:3];
  2872. [encoder setBytes:&ne02 length:sizeof(ne02) atIndex:4];
  2873. [encoder setBytes:&ne03 length:sizeof(ne03) atIndex:5];
  2874. [encoder setBytes:&nb00 length:sizeof(nb00) atIndex:6];
  2875. [encoder setBytes:&nb01 length:sizeof(nb01) atIndex:7];
  2876. [encoder setBytes:&nb02 length:sizeof(nb02) atIndex:8];
  2877. [encoder setBytes:&nb03 length:sizeof(nb03) atIndex:9];
  2878. [encoder setBytes:&ne0 length:sizeof(ne0) atIndex:10];
  2879. [encoder setBytes:&ne1 length:sizeof(ne1) atIndex:11];
  2880. [encoder setBytes:&ne2 length:sizeof(ne2) atIndex:12];
  2881. [encoder setBytes:&ne3 length:sizeof(ne3) atIndex:13];
  2882. [encoder setBytes:&nb0 length:sizeof(nb0) atIndex:14];
  2883. [encoder setBytes:&nb1 length:sizeof(nb1) atIndex:15];
  2884. [encoder setBytes:&nb2 length:sizeof(nb2) atIndex:16];
  2885. [encoder setBytes:&nb3 length:sizeof(nb3) atIndex:17];
  2886. [encoder setBytes:&sf0 length:sizeof(sf0) atIndex:18];
  2887. [encoder setBytes:&sf1 length:sizeof(sf1) atIndex:19];
  2888. [encoder setBytes:&sf2 length:sizeof(sf2) atIndex:20];
  2889. [encoder setBytes:&sf3 length:sizeof(sf3) atIndex:21];
  2890. const int nth = MIN((int) pipeline.maxTotalThreadsPerThreadgroup, ne0);
  2891. [encoder dispatchThreadgroups:MTLSizeMake(ne1, ne2, ne3) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  2892. } break;
  2893. case GGML_OP_PAD:
  2894. {
  2895. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  2896. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_PAD_F32].pipeline;
  2897. // TODO: add ggml_metal_kargs struct
  2898. [encoder setComputePipelineState:pipeline];
  2899. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  2900. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  2901. [encoder setBytes:&ne00 length:sizeof(ne00) atIndex:2];
  2902. [encoder setBytes:&ne01 length:sizeof(ne01) atIndex:3];
  2903. [encoder setBytes:&ne02 length:sizeof(ne02) atIndex:4];
  2904. [encoder setBytes:&ne03 length:sizeof(ne03) atIndex:5];
  2905. [encoder setBytes:&nb00 length:sizeof(nb00) atIndex:6];
  2906. [encoder setBytes:&nb01 length:sizeof(nb01) atIndex:7];
  2907. [encoder setBytes:&nb02 length:sizeof(nb02) atIndex:8];
  2908. [encoder setBytes:&nb03 length:sizeof(nb03) atIndex:9];
  2909. [encoder setBytes:&ne0 length:sizeof(ne0) atIndex:10];
  2910. [encoder setBytes:&ne1 length:sizeof(ne1) atIndex:11];
  2911. [encoder setBytes:&ne2 length:sizeof(ne2) atIndex:12];
  2912. [encoder setBytes:&ne3 length:sizeof(ne3) atIndex:13];
  2913. [encoder setBytes:&nb0 length:sizeof(nb0) atIndex:14];
  2914. [encoder setBytes:&nb1 length:sizeof(nb1) atIndex:15];
  2915. [encoder setBytes:&nb2 length:sizeof(nb2) atIndex:16];
  2916. [encoder setBytes:&nb3 length:sizeof(nb3) atIndex:17];
  2917. const int nth = MIN(1024, ne0);
  2918. [encoder dispatchThreadgroups:MTLSizeMake(ne1, ne2, ne3) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  2919. } break;
  2920. case GGML_OP_UNPAD:
  2921. {
  2922. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  2923. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_UNPAD_F32].pipeline;
  2924. [encoder setComputePipelineState:pipeline];
  2925. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  2926. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  2927. [encoder setBytes:&ne00 length:sizeof(ne00) atIndex:2];
  2928. [encoder setBytes:&ne01 length:sizeof(ne01) atIndex:3];
  2929. [encoder setBytes:&ne02 length:sizeof(ne02) atIndex:4];
  2930. [encoder setBytes:&ne03 length:sizeof(ne03) atIndex:5];
  2931. [encoder setBytes:&nb00 length:sizeof(nb00) atIndex:6];
  2932. [encoder setBytes:&nb01 length:sizeof(nb01) atIndex:7];
  2933. [encoder setBytes:&nb02 length:sizeof(nb02) atIndex:8];
  2934. [encoder setBytes:&nb03 length:sizeof(nb03) atIndex:9];
  2935. [encoder setBytes:&ne0 length:sizeof(ne0) atIndex:10];
  2936. [encoder setBytes:&ne1 length:sizeof(ne1) atIndex:11];
  2937. [encoder setBytes:&ne2 length:sizeof(ne2) atIndex:12];
  2938. [encoder setBytes:&ne3 length:sizeof(ne3) atIndex:13];
  2939. [encoder setBytes:&nb0 length:sizeof(nb0) atIndex:14];
  2940. [encoder setBytes:&nb1 length:sizeof(nb1) atIndex:15];
  2941. [encoder setBytes:&nb2 length:sizeof(nb2) atIndex:16];
  2942. [encoder setBytes:&nb3 length:sizeof(nb3) atIndex:17];
  2943. const int nth = MIN(1024, ne0);
  2944. [encoder dispatchThreadgroups:MTLSizeMake(ne1, ne2, ne3) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  2945. } break;
  2946. case GGML_OP_ARANGE:
  2947. {
  2948. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  2949. float start;
  2950. float step;
  2951. memcpy(&start, ((const int32_t *) dst->op_params) + 0, sizeof(float));
  2952. memcpy(&step, ((const int32_t *) dst->op_params) + 2, sizeof(float));
  2953. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_ARANGE_F32].pipeline;
  2954. // TODO: add ggml_metal_kargs struct
  2955. [encoder setComputePipelineState:pipeline];
  2956. [encoder setBuffer:id_dst offset:offs_dst atIndex:0];
  2957. [encoder setBytes:&ne0 length:sizeof(ne0) atIndex:1];
  2958. [encoder setBytes:&start length:sizeof(start) atIndex:2];
  2959. [encoder setBytes:&step length:sizeof(step) atIndex:3];
  2960. const int nth = MIN(1024, ne0);
  2961. [encoder dispatchThreadgroups:MTLSizeMake(1, 1, 1) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  2962. } break;
  2963. case GGML_OP_TIMESTEP_EMBEDDING:
  2964. {
  2965. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  2966. const int dim = dst->op_params[0];
  2967. const int max_period = dst->op_params[1];
  2968. const int half = dim / 2;
  2969. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_TIMESTEP_EMBEDDING_F32].pipeline;
  2970. // TODO: add ggml_metal_kargs struct
  2971. [encoder setComputePipelineState:pipeline];
  2972. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  2973. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  2974. [encoder setBytes:&nb1 length:sizeof(nb1) atIndex:2];
  2975. [encoder setBytes:&dim length:sizeof(dim) atIndex:3];
  2976. [encoder setBytes:&max_period length:sizeof(max_period) atIndex:4];
  2977. const int nth = MIN(1024, half);
  2978. [encoder dispatchThreadgroups:MTLSizeMake(ne00, 1, 1) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  2979. } break;
  2980. case GGML_OP_ARGSORT:
  2981. {
  2982. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  2983. GGML_ASSERT( dst->type == GGML_TYPE_I32);
  2984. const int nrows = ggml_nrows(src0);
  2985. enum ggml_sort_order order = (enum ggml_sort_order) dst->op_params[0];
  2986. // bitonic sort requires the number of elements to be power of 2
  2987. int64_t ne00_padded = 1;
  2988. while (ne00_padded < ne00) {
  2989. ne00_padded *= 2;
  2990. }
  2991. // Metal kernels require the buffer size to be multiple of 16 bytes
  2992. // https://developer.apple.com/documentation/metal/mtlcomputecommandencoder/1443142-setthreadgroupmemorylength
  2993. const int mem_size = GGML_PAD(ne00_padded*sizeof(int32_t), 16);
  2994. id<MTLComputePipelineState> pipeline = nil;
  2995. switch (order) {
  2996. case GGML_SORT_ORDER_ASC: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_ARGSORT_F32_I32_ASC].pipeline; break;
  2997. case GGML_SORT_ORDER_DESC: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_ARGSORT_F32_I32_DESC].pipeline; break;
  2998. default: GGML_ABORT("fatal error");
  2999. };
  3000. // TODO: add ggml_metal_kargs struct
  3001. [encoder setComputePipelineState:pipeline];
  3002. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  3003. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  3004. [encoder setBytes:&ne00 length:sizeof( int64_t) atIndex:2];
  3005. [encoder setBytes:&ne00_padded length:sizeof( int64_t) atIndex:3];
  3006. [encoder setThreadgroupMemoryLength:mem_size atIndex:0];
  3007. [encoder dispatchThreadgroups:MTLSizeMake(1, nrows, 1) threadsPerThreadgroup:MTLSizeMake(ne00_padded, 1, 1)];
  3008. } break;
  3009. case GGML_OP_LEAKY_RELU:
  3010. {
  3011. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  3012. float slope;
  3013. memcpy(&slope, dst->op_params, sizeof(float));
  3014. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_LEAKY_RELU_F32].pipeline;
  3015. // TODO: add ggml_metal_kargs struct
  3016. [encoder setComputePipelineState:pipeline];
  3017. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  3018. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  3019. [encoder setBytes:&slope length:sizeof(slope) atIndex:2];
  3020. const int64_t n = ggml_nelements(dst);
  3021. [encoder dispatchThreadgroups:MTLSizeMake(n, 1, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  3022. } break;
  3023. case GGML_OP_FLASH_ATTN_EXT:
  3024. {
  3025. GGML_ASSERT(ne00 % 4 == 0);
  3026. GGML_ASSERT(ne11 % 32 == 0);
  3027. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  3028. GGML_ASSERT(src1->type == src2->type);
  3029. GGML_ASSERT(ggml_are_same_shape (src1, src2));
  3030. struct ggml_tensor * src3 = node->src[3];
  3031. size_t offs_src3 = 0;
  3032. id<MTLBuffer> id_src3 = src3 ? ggml_metal_get_buffer(src3, &offs_src3) : nil;
  3033. GGML_ASSERT(!src3 || src3->type == GGML_TYPE_F16);
  3034. GGML_ASSERT(!src3 || src3->ne[1] >= GGML_PAD(src0->ne[1], 8) &&
  3035. "the Flash-Attention Metal kernel requires the mask to be padded to 8 and at least n_queries big");
  3036. const int64_t ne30 = src3 ? src3->ne[0] : 0; GGML_UNUSED(ne30);
  3037. //const int64_t ne31 = src3 ? src3->ne[1] : 0;
  3038. const int64_t ne32 = src3 ? src3->ne[2] : 0; GGML_UNUSED(ne32);
  3039. const int64_t ne33 = src3 ? src3->ne[3] : 0; GGML_UNUSED(ne33);
  3040. const uint64_t nb30 = src3 ? src3->nb[0] : 0; GGML_UNUSED(nb30);
  3041. const uint64_t nb31 = src3 ? src3->nb[1] : 0;
  3042. const uint64_t nb32 = src3 ? src3->nb[2] : 0; GGML_UNUSED(nb32);
  3043. const uint64_t nb33 = src3 ? src3->nb[3] : 0; GGML_UNUSED(nb33);
  3044. const enum ggml_type src2t = src2 ? src2->type : GGML_TYPE_COUNT; GGML_UNUSED(src2t);
  3045. float scale;
  3046. float max_bias;
  3047. float logit_softcap;
  3048. memcpy(&scale, ((const int32_t *) dst->op_params) + 0, sizeof(scale));
  3049. memcpy(&max_bias, ((const int32_t *) dst->op_params) + 1, sizeof(max_bias));
  3050. memcpy(&logit_softcap, ((const int32_t *) dst->op_params) + 2, sizeof(logit_softcap));
  3051. if (logit_softcap != 0.0f) {
  3052. scale /= logit_softcap;
  3053. }
  3054. const uint32_t n_head = src0->ne[2];
  3055. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head));
  3056. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  3057. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  3058. id<MTLComputePipelineState> pipeline = nil;
  3059. bool use_vec_kernel = false;
  3060. // TODO: add vec kernels for (ne00%64 == 0) and maybe also for (ne00%32 == 0)
  3061. // for now avoiding mainly to keep the number of templates/kernels a bit lower
  3062. if (ne01 >= 4 || (ne00%128 != 0)) {
  3063. switch (src1->type) {
  3064. case GGML_TYPE_F16:
  3065. {
  3066. switch (ne00) {
  3067. case 64: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_F16_H64 ].pipeline; break;
  3068. case 80: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_F16_H80 ].pipeline; break;
  3069. case 96: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_F16_H96 ].pipeline; break;
  3070. case 112: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_F16_H112].pipeline; break;
  3071. case 128: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_F16_H128].pipeline; break;
  3072. case 256: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_F16_H256].pipeline; break;
  3073. default:
  3074. {
  3075. GGML_LOG_ERROR("unsupported size: %lld\n", ne00);
  3076. GGML_LOG_ERROR("add template specialization for this size\n");
  3077. GGML_ABORT("add template specialization for this size");
  3078. }
  3079. }
  3080. } break;
  3081. case GGML_TYPE_BF16:
  3082. {
  3083. switch (ne00) {
  3084. case 64: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_BF16_H64 ].pipeline; break;
  3085. case 80: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_BF16_H80 ].pipeline; break;
  3086. case 96: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_BF16_H96 ].pipeline; break;
  3087. case 112: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_BF16_H112].pipeline; break;
  3088. case 128: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_BF16_H128].pipeline; break;
  3089. case 256: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_BF16_H256].pipeline; break;
  3090. default:
  3091. {
  3092. GGML_LOG_ERROR("unsupported size: %lld\n", ne00);
  3093. GGML_LOG_ERROR("add template specialization for this size\n");
  3094. GGML_ABORT("add template specialization for this size");
  3095. }
  3096. }
  3097. } break;
  3098. case GGML_TYPE_Q4_0:
  3099. {
  3100. switch (ne00) {
  3101. case 64: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q4_0_H64 ].pipeline; break;
  3102. case 80: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q4_0_H80 ].pipeline; break;
  3103. case 96: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q4_0_H96 ].pipeline; break;
  3104. case 112: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q4_0_H112].pipeline; break;
  3105. case 128: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q4_0_H128].pipeline; break;
  3106. case 256: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q4_0_H256].pipeline; break;
  3107. default:
  3108. {
  3109. GGML_LOG_ERROR("unsupported size: %lld\n", ne00);
  3110. GGML_LOG_ERROR("add template specialization for this size\n");
  3111. GGML_ABORT("add template specialization for this size");
  3112. }
  3113. }
  3114. } break;
  3115. case GGML_TYPE_Q4_1:
  3116. {
  3117. switch (ne00) {
  3118. case 64: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q4_1_H64 ].pipeline; break;
  3119. case 80: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q4_1_H80 ].pipeline; break;
  3120. case 96: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q4_1_H96 ].pipeline; break;
  3121. case 112: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q4_1_H112].pipeline; break;
  3122. case 128: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q4_1_H128].pipeline; break;
  3123. case 256: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q4_1_H256].pipeline; break;
  3124. default:
  3125. {
  3126. GGML_LOG_ERROR("unsupported size: %lld\n", ne00);
  3127. GGML_LOG_ERROR("add template specialization for this size\n");
  3128. GGML_ABORT("add template specialization for this size");
  3129. }
  3130. }
  3131. } break;
  3132. case GGML_TYPE_Q5_0:
  3133. {
  3134. switch (ne00) {
  3135. case 64: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q5_0_H64 ].pipeline; break;
  3136. case 80: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q5_0_H80 ].pipeline; break;
  3137. case 96: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q5_0_H96 ].pipeline; break;
  3138. case 112: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q5_0_H112].pipeline; break;
  3139. case 128: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q5_0_H128].pipeline; break;
  3140. case 256: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q5_0_H256].pipeline; break;
  3141. default:
  3142. {
  3143. GGML_LOG_ERROR("unsupported size: %lld\n", ne00);
  3144. GGML_LOG_ERROR("add template specialization for this size\n");
  3145. GGML_ABORT("add template specialization for this size");
  3146. }
  3147. }
  3148. } break;
  3149. case GGML_TYPE_Q5_1:
  3150. {
  3151. switch (ne00) {
  3152. case 64: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q5_1_H64 ].pipeline; break;
  3153. case 80: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q5_1_H80 ].pipeline; break;
  3154. case 96: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q5_1_H96 ].pipeline; break;
  3155. case 112: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q5_1_H112].pipeline; break;
  3156. case 128: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q5_1_H128].pipeline; break;
  3157. case 256: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q5_1_H256].pipeline; break;
  3158. default:
  3159. {
  3160. GGML_LOG_ERROR("unsupported size: %lld\n", ne00);
  3161. GGML_LOG_ERROR("add template specialization for this size\n");
  3162. GGML_ABORT("add template specialization for this size");
  3163. }
  3164. }
  3165. } break;
  3166. case GGML_TYPE_Q8_0:
  3167. {
  3168. switch (ne00) {
  3169. case 64: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q8_0_H64 ].pipeline; break;
  3170. case 80: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q8_0_H80 ].pipeline; break;
  3171. case 96: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q8_0_H96 ].pipeline; break;
  3172. case 112: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q8_0_H112].pipeline; break;
  3173. case 128: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q8_0_H128].pipeline; break;
  3174. case 256: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_Q8_0_H256].pipeline; break;
  3175. default:
  3176. {
  3177. GGML_LOG_ERROR("unsupported size: %lld\n", ne00);
  3178. GGML_LOG_ERROR("add template specialization for this size\n");
  3179. GGML_ABORT("add template specialization for this size");
  3180. }
  3181. }
  3182. } break;
  3183. default:
  3184. {
  3185. GGML_LOG_ERROR("unsupported type: %d\n", src1->type);
  3186. GGML_LOG_ERROR("add template specialization for this type\n");
  3187. GGML_ABORT("add template specialization for this type");
  3188. }
  3189. }
  3190. } else {
  3191. use_vec_kernel = true;
  3192. switch (ne00) {
  3193. case 128:
  3194. {
  3195. switch (src1->type) {
  3196. case GGML_TYPE_F16: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_VEC_F16_H128].pipeline; break;
  3197. case GGML_TYPE_BF16: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_VEC_BF16_H128].pipeline; break;
  3198. case GGML_TYPE_Q4_0: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_VEC_Q4_0_H128].pipeline; break;
  3199. case GGML_TYPE_Q4_1: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_VEC_Q4_1_H128].pipeline; break;
  3200. case GGML_TYPE_Q5_0: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_VEC_Q5_0_H128].pipeline; break;
  3201. case GGML_TYPE_Q5_1: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_VEC_Q5_1_H128].pipeline; break;
  3202. case GGML_TYPE_Q8_0: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_VEC_Q8_0_H128].pipeline; break;
  3203. default:
  3204. {
  3205. GGML_LOG_ERROR("unsupported type: %d\n", src1->type);
  3206. GGML_LOG_ERROR("add template specialization for this type\n");
  3207. GGML_ABORT("add template specialization for this type");
  3208. }
  3209. }
  3210. } break;
  3211. case 256:
  3212. {
  3213. switch (src1->type) {
  3214. case GGML_TYPE_F16: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_VEC_F16_H256].pipeline; break;
  3215. case GGML_TYPE_BF16: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_VEC_BF16_H256].pipeline; break;
  3216. case GGML_TYPE_Q4_0: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_VEC_Q4_0_H256].pipeline; break;
  3217. case GGML_TYPE_Q4_1: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_VEC_Q4_1_H256].pipeline; break;
  3218. case GGML_TYPE_Q5_0: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_VEC_Q5_0_H256].pipeline; break;
  3219. case GGML_TYPE_Q5_1: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_VEC_Q5_1_H256].pipeline; break;
  3220. case GGML_TYPE_Q8_0: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_FLASH_ATTN_EXT_VEC_Q8_0_H256].pipeline; break;
  3221. default:
  3222. {
  3223. GGML_LOG_ERROR("unsupported type: %d\n", src1->type);
  3224. GGML_LOG_ERROR("add template specialization for this type\n");
  3225. GGML_ABORT("add template specialization for this type");
  3226. }
  3227. }
  3228. } break;
  3229. default:
  3230. {
  3231. GGML_LOG_ERROR("unsupported size: %lld\n", ne00);
  3232. GGML_LOG_ERROR("add template specialization for this size\n");
  3233. GGML_ABORT("add template specialization for this size");
  3234. }
  3235. }
  3236. }
  3237. ggml_metal_kargs_flash_attn_ext args = {
  3238. /*.ne01 =*/ ne01,
  3239. /*.ne02 =*/ ne02,
  3240. /*.ne03 =*/ ne03,
  3241. /*.nb01 =*/ nb01,
  3242. /*.nb02 =*/ nb02,
  3243. /*.nb03 =*/ nb03,
  3244. /*.ne11 =*/ ne11,
  3245. /*.ne_12_2 =*/ ne12,
  3246. /*.ne_12_3 =*/ ne13,
  3247. /*.nb_12_1 =*/ nb11,
  3248. /*.nb_12_2 =*/ nb12,
  3249. /*.nb_12_3 =*/ nb13,
  3250. /*.nb31 =*/ nb31,
  3251. /*.ne1 =*/ ne1,
  3252. /*.ne2 =*/ ne2,
  3253. /*.scale =*/ scale,
  3254. /*.max_bias =*/ max_bias,
  3255. /*.m0 =*/ m0,
  3256. /*.m1 =*/ m1,
  3257. /*.n_head_log2 =*/ n_head_log2,
  3258. /*.logit_softcap =*/ logit_softcap,
  3259. };
  3260. [encoder setComputePipelineState:pipeline];
  3261. [encoder setBytes:&args length:sizeof(args) atIndex:0];
  3262. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:1];
  3263. [encoder setBuffer:id_src1 offset:offs_src1 atIndex:2];
  3264. [encoder setBuffer:id_src2 offset:offs_src2 atIndex:3];
  3265. if (id_src3) {
  3266. [encoder setBuffer:id_src3 offset:offs_src3 atIndex:4];
  3267. } else {
  3268. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:4];
  3269. }
  3270. [encoder setBuffer:id_dst offset:offs_dst atIndex:5];
  3271. if (!use_vec_kernel) {
  3272. // half8x8 kernel
  3273. const int64_t nqptg = 8; // queries per threadgroup !! sync with kernel template arguments !!
  3274. const int64_t ncpsg = 32; // cache values per simdgroup !! sync with kernel template arguments !!
  3275. GGML_ASSERT(nqptg <= 32);
  3276. GGML_ASSERT(nqptg % 8 == 0);
  3277. GGML_ASSERT(ncpsg % 32 == 0);
  3278. // 2*(2*ncpsg + nqptg)*(nsg)
  3279. // ncpsg soft_max values + ncpsg mask values + a diagonal scaling matrix (in float)
  3280. //
  3281. // 16*32*(nsg)
  3282. // the shared memory needed for the simdgroups to load the KV cache
  3283. // each thread loads (dequantizes) 16 head elements, there are 32 threads in th SG
  3284. //
  3285. #define FATTN_SMEM(nsg) (GGML_PAD((nqptg*(ne00 + 2*(2*ncpsg + nqptg)*(nsg)) + 16*32*(nsg))*(sizeof(float)/2), 16))
  3286. int64_t nsgmax = 2;
  3287. while (true) {
  3288. const size_t smem = FATTN_SMEM(nsgmax);
  3289. if (smem > device.maxThreadgroupMemoryLength) {
  3290. break;
  3291. }
  3292. nsgmax *= 2;
  3293. }
  3294. nsgmax /= 2;
  3295. // simdgroups per threadgroup (a.k.a. warps)
  3296. const int64_t nsg = ne01 <= nqptg ? MAX(4, MIN(nsgmax, MIN(ne11/ncpsg, (int64_t) pipeline.maxTotalThreadsPerThreadgroup/32))) : 4;
  3297. const size_t smem = FATTN_SMEM(nsg);
  3298. //printf("smem: %zu, max: %zu, nsg = %d\n", smem, device.maxThreadgroupMemoryLength, (int) nsg);
  3299. GGML_ASSERT(smem <= device.maxThreadgroupMemoryLength);
  3300. [encoder setThreadgroupMemoryLength:smem atIndex:0];
  3301. #undef FATTN_SMEM
  3302. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + nqptg - 1)/nqptg, ne02, ne03) threadsPerThreadgroup:MTLSizeMake(32, nsg, 1)];
  3303. } else {
  3304. // half4x4 kernel
  3305. const int64_t nqptg = 1; // queries per threadgroup !! sync with kernel template arguments !!
  3306. const int64_t ncpsg = 32; // cache values per simdgroup !! sync with kernel template arguments !!
  3307. GGML_ASSERT(nqptg <= 32);
  3308. GGML_ASSERT(nqptg % 1 == 0);
  3309. GGML_ASSERT(ncpsg % 32 == 0);
  3310. // ne00 + 2*ncpsg*(nsg)
  3311. // for each query, we load it as f16 in shared memory (ne00)
  3312. // and store the soft_max values and the mask
  3313. //
  3314. // ne00*(nsg)
  3315. // each simdgroup has a full f16 head vector in shared mem to accumulate results
  3316. //
  3317. #define FATTN_SMEM(nsg) (GGML_PAD((nqptg*(ne00 + 2*ncpsg*(nsg)) + ne00*(nsg))*(sizeof(float)/2), 16))
  3318. int64_t nsgmax = 2;
  3319. while (true) {
  3320. const size_t smem = FATTN_SMEM(nsgmax);
  3321. if (smem > device.maxThreadgroupMemoryLength) {
  3322. break;
  3323. }
  3324. nsgmax *= 2;
  3325. }
  3326. nsgmax /= 2;
  3327. // simdgroups per threadgroup (a.k.a. warps)
  3328. const int64_t nsgt = MAX(2, MIN(nsgmax, MIN(ne11/ncpsg, (int64_t) pipeline.maxTotalThreadsPerThreadgroup/32)));
  3329. int64_t nsg = 1;
  3330. while (nsg <= nsgt) {
  3331. nsg *= 2;
  3332. }
  3333. nsg /= 2;
  3334. const size_t smem = FATTN_SMEM(nsg);
  3335. //printf("smem: %zu, max: %zu, nsg = %d\n", smem, device.maxThreadgroupMemoryLength, (int) nsg);
  3336. GGML_ASSERT(smem <= device.maxThreadgroupMemoryLength);
  3337. [encoder setThreadgroupMemoryLength:smem atIndex:0];
  3338. #undef FATTN_SMEM
  3339. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + nqptg - 1)/nqptg, ne02, ne03) threadsPerThreadgroup:MTLSizeMake(32, nsg, 1)];
  3340. }
  3341. } break;
  3342. case GGML_OP_DUP:
  3343. case GGML_OP_CPY:
  3344. case GGML_OP_CONT:
  3345. {
  3346. GGML_ASSERT(ne00 % ggml_blck_size(src0->type) == 0);
  3347. int nth = MIN(1024, ne00/ggml_blck_size(src0->type));
  3348. id<MTLComputePipelineState> pipeline = nil;
  3349. switch (src0t) {
  3350. case GGML_TYPE_F32:
  3351. {
  3352. GGML_ASSERT(ne0 % ggml_blck_size(dst->type) == 0);
  3353. switch (dstt) {
  3354. case GGML_TYPE_F32: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_CPY_F32_F32].pipeline; break;
  3355. case GGML_TYPE_F16: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_CPY_F32_F16].pipeline; break;
  3356. case GGML_TYPE_BF16: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_CPY_F32_BF16].pipeline; break;
  3357. case GGML_TYPE_Q8_0: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_CPY_F32_Q8_0].pipeline; break;
  3358. case GGML_TYPE_Q4_0: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_CPY_F32_Q4_0].pipeline; break;
  3359. case GGML_TYPE_Q4_1: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_CPY_F32_Q4_1].pipeline; break;
  3360. case GGML_TYPE_Q5_0: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_CPY_F32_Q5_0].pipeline; break;
  3361. case GGML_TYPE_Q5_1: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_CPY_F32_Q5_1].pipeline; break;
  3362. case GGML_TYPE_IQ4_NL: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_CPY_F32_IQ4_NL].pipeline; break;
  3363. default: GGML_ABORT("not implemented");
  3364. };
  3365. } break;
  3366. case GGML_TYPE_F16:
  3367. {
  3368. switch (dstt) {
  3369. case GGML_TYPE_F32: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_CPY_F16_F32].pipeline; break;
  3370. case GGML_TYPE_F16: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_CPY_F16_F16].pipeline; break;
  3371. default: GGML_ABORT("not implemented");
  3372. };
  3373. } break;
  3374. case GGML_TYPE_BF16:
  3375. {
  3376. switch (dstt) {
  3377. case GGML_TYPE_F32: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_CPY_BF16_F32].pipeline; break;
  3378. case GGML_TYPE_BF16: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_CPY_BF16_BF16].pipeline; break;
  3379. default: GGML_ASSERT(false && "not implemented");
  3380. };
  3381. } break;
  3382. default: GGML_ABORT("not implemented");
  3383. }
  3384. ggml_metal_kargs_cpy args = {
  3385. /*.ne00 =*/ ne00,
  3386. /*.ne01 =*/ ne01,
  3387. /*.ne02 =*/ ne02,
  3388. /*.ne03 =*/ ne03,
  3389. /*.nb00 =*/ nb00,
  3390. /*.nb01 =*/ nb01,
  3391. /*.nb02 =*/ nb02,
  3392. /*.nb03 =*/ nb03,
  3393. /*.ne0 =*/ ne0,
  3394. /*.ne1 =*/ ne1,
  3395. /*.ne2 =*/ ne2,
  3396. /*.ne3 =*/ ne3,
  3397. /*.nb0 =*/ nb0,
  3398. /*.nb1 =*/ nb1,
  3399. /*.nb2 =*/ nb2,
  3400. /*.nb3 =*/ nb3,
  3401. };
  3402. [encoder setComputePipelineState:pipeline];
  3403. [encoder setBytes:&args length:sizeof(args) atIndex:0];
  3404. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:1];
  3405. [encoder setBuffer:id_dst offset:offs_dst atIndex:2];
  3406. [encoder dispatchThreadgroups:MTLSizeMake(ne01, ne02, ne03) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  3407. } break;
  3408. case GGML_OP_POOL_2D:
  3409. {
  3410. GGML_ASSERT(ggml_is_contiguous(src0));
  3411. GGML_ASSERT(src0t == GGML_TYPE_F32 && src0t == dstt);
  3412. const int32_t * opts = dst->op_params;
  3413. enum ggml_op_pool op = opts[0];
  3414. id<MTLComputePipelineState> pipeline = nil;
  3415. switch (src0t) {
  3416. case GGML_TYPE_F32: {
  3417. switch(op) {
  3418. case GGML_OP_POOL_AVG:
  3419. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_POOL_2D_AVG_F32].pipeline; break;
  3420. case GGML_OP_POOL_MAX:
  3421. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_POOL_2D_MAX_F32].pipeline; break;
  3422. default: GGML_ASSERT(false && "not implemented");
  3423. }
  3424. } break;
  3425. default: GGML_ASSERT(false && "not implemented");
  3426. }
  3427. const int32_t k0 = opts[1];
  3428. const int32_t k1 = opts[2];
  3429. const int32_t s0 = opts[3];
  3430. const int32_t s1 = opts[4];
  3431. const int32_t p0 = opts[5];
  3432. const int32_t p1 = opts[6];
  3433. const int64_t IH = src0->ne[1];
  3434. const int64_t IW = src0->ne[0];
  3435. const int64_t N = dst->ne[3];
  3436. const int64_t OC = dst->ne[2];
  3437. const int64_t OH = dst->ne[1];
  3438. const int64_t OW = dst->ne[0];
  3439. const int64_t parallel_elements = N * OC * OH * OW;
  3440. const int64_t n_threads = MIN((int64_t)[pipeline maxTotalThreadsPerThreadgroup], parallel_elements);
  3441. const int64_t n_tg = (parallel_elements + n_threads - 1) / n_threads;
  3442. // TODO: add ggml_metal_kargs struct
  3443. [encoder setComputePipelineState:pipeline];
  3444. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  3445. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  3446. [encoder setBytes:&k0 length:sizeof(int32_t) atIndex:2];
  3447. [encoder setBytes:&k1 length:sizeof(int32_t) atIndex:3];
  3448. [encoder setBytes:&s0 length:sizeof(int32_t) atIndex:4];
  3449. [encoder setBytes:&s1 length:sizeof(int32_t) atIndex:5];
  3450. [encoder setBytes:&p0 length:sizeof(int32_t) atIndex:6];
  3451. [encoder setBytes:&p1 length:sizeof(int32_t) atIndex:7];
  3452. [encoder setBytes:&IH length:sizeof(int64_t) atIndex:8];
  3453. [encoder setBytes:&IW length:sizeof(int64_t) atIndex:9];
  3454. [encoder setBytes:&OH length:sizeof(int64_t) atIndex:10];
  3455. [encoder setBytes:&OW length:sizeof(int64_t) atIndex:11];
  3456. [encoder setBytes:&parallel_elements length:sizeof(int64_t) atIndex:12];
  3457. [encoder dispatchThreadgroups:MTLSizeMake(n_tg, 1, 1) threadsPerThreadgroup:MTLSizeMake(n_threads, 1, 1)];
  3458. } break;
  3459. case GGML_OP_ARGMAX:
  3460. {
  3461. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  3462. GGML_ASSERT(ggml_is_contiguous_1(src0));
  3463. GGML_ASSERT(nb00 == ggml_type_size(src0->type));
  3464. const int64_t nrows = ggml_nrows(src0);
  3465. int nth = 32; // SIMD width
  3466. while (nth < ne00 && nth*ne01*ne02*ne03 < 256) {
  3467. nth *= 2;
  3468. }
  3469. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_ARGMAX].pipeline;
  3470. [encoder setComputePipelineState:pipeline];
  3471. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  3472. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  3473. [encoder setBytes:&ne00 length:sizeof( int64_t) atIndex:2];
  3474. [encoder setBytes:&nb01 length:sizeof(uint64_t) atIndex:3];
  3475. [encoder setThreadgroupMemoryLength:32*sizeof(float) atIndex:0];
  3476. [encoder setThreadgroupMemoryLength:32*sizeof(int32_t) atIndex:1];
  3477. [encoder dispatchThreadgroups:MTLSizeMake(nrows, 1, 1) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  3478. } break;
  3479. default:
  3480. {
  3481. GGML_LOG_ERROR("%s: error: node %3d, op = %8s not implemented\n", __func__, idx, ggml_op_name(dst->op));
  3482. GGML_ABORT("fatal error");
  3483. }
  3484. }
  3485. }
  3486. static enum ggml_status ggml_metal_graph_compute(
  3487. ggml_backend_t backend,
  3488. struct ggml_cgraph * gf) {
  3489. struct ggml_backend_metal_context * ctx = backend->context;
  3490. struct ggml_backend_metal_device_context * ctx_dev = backend->device->context;
  3491. // number of nodes encoded by the main thread (empirically determined)
  3492. const int n_main = 128;
  3493. // number of threads in addition to the main thread
  3494. const int n_cb = ctx->n_cb;
  3495. // submit the ggml compute graph to the GPU by creating command buffers and encoding the ops in them
  3496. // the first n_nodes_0 are encoded and submitted for processing directly by the calling thread
  3497. // while these nodes are processing, we start n_cb threads to enqueue the rest of the nodes
  3498. // each thread creates it's own command buffer and enqueues the ops in parallel
  3499. //
  3500. // tests on M1 Pro and M2 Ultra using LLaMA models, show that optimal values for n_cb are 1 or 2
  3501. @autoreleasepool {
  3502. ctx->gf = gf;
  3503. ctx->n_nodes_0 = MIN(n_main, gf->n_nodes);
  3504. ctx->n_nodes_1 = gf->n_nodes - ctx->n_nodes_0;
  3505. ctx->n_nodes_per_cb = (ctx->n_nodes_1 + ctx->n_cb - 1) / ctx->n_cb;
  3506. const bool should_capture = ctx->capture_next_compute;
  3507. if (should_capture) {
  3508. ctx->capture_next_compute = false;
  3509. if (!ctx->capture_started) {
  3510. // create capture scope
  3511. ctx->capture_scope = [[MTLCaptureManager sharedCaptureManager] newCaptureScopeWithDevice:ctx_dev->mtl_device];
  3512. MTLCaptureDescriptor * descriptor = [MTLCaptureDescriptor new];
  3513. descriptor.captureObject = ctx->capture_scope;
  3514. descriptor.destination = MTLCaptureDestinationGPUTraceDocument;
  3515. descriptor.outputURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"/tmp/perf-metal.gputrace"]];
  3516. NSError * error = nil;
  3517. if (![[MTLCaptureManager sharedCaptureManager] startCaptureWithDescriptor:descriptor error:&error]) {
  3518. GGML_LOG_ERROR("%s: error: unable to start capture '%s'\n", __func__, [[error localizedDescription] UTF8String]);
  3519. } else {
  3520. [ctx->capture_scope beginScope];
  3521. ctx->capture_started = true;
  3522. }
  3523. }
  3524. }
  3525. // the main thread commits the first few commands immediately
  3526. // command_buffer[n_cb]
  3527. {
  3528. id<MTLCommandBuffer> command_buffer = [ctx->queue commandBufferWithUnretainedReferences];
  3529. ctx->command_buffers[n_cb] = command_buffer;
  3530. [command_buffer enqueue];
  3531. ctx->encode_async(n_cb);
  3532. }
  3533. // prepare the rest of the command buffers asynchronously
  3534. // command_buffer[0.. n_cb)
  3535. for (int cb_idx = 0; cb_idx < n_cb; ++cb_idx) {
  3536. id<MTLCommandBuffer> command_buffer = [ctx->queue commandBufferWithUnretainedReferences];
  3537. ctx->command_buffers[cb_idx] = command_buffer;
  3538. // always enqueue the first two command buffers
  3539. // enqueue all of the command buffers if we don't need to abort
  3540. if (cb_idx < 2 || ctx->abort_callback == NULL) {
  3541. [command_buffer enqueue];
  3542. }
  3543. }
  3544. dispatch_apply(n_cb, ctx->d_queue, ctx->encode_async);
  3545. // wait for completion and check status of each command buffer
  3546. // needed to detect if the device ran out-of-memory for example (#1881)
  3547. {
  3548. id<MTLCommandBuffer> command_buffer = ctx->command_buffers[n_cb];
  3549. [command_buffer waitUntilCompleted];
  3550. MTLCommandBufferStatus status = [command_buffer status];
  3551. if (status != MTLCommandBufferStatusCompleted) {
  3552. GGML_LOG_INFO("%s: command buffer %d failed with status %lu\n", __func__, n_cb, status);
  3553. if (status == MTLCommandBufferStatusError) {
  3554. GGML_LOG_INFO("error: %s\n", [[command_buffer error].localizedDescription UTF8String]);
  3555. }
  3556. return GGML_STATUS_FAILED;
  3557. }
  3558. }
  3559. for (int i = 0; i < n_cb; ++i) {
  3560. id<MTLCommandBuffer> command_buffer = ctx->command_buffers[i];
  3561. [command_buffer waitUntilCompleted];
  3562. MTLCommandBufferStatus status = [command_buffer status];
  3563. if (status != MTLCommandBufferStatusCompleted) {
  3564. GGML_LOG_INFO("%s: command buffer %d failed with status %lu\n", __func__, i, status);
  3565. if (status == MTLCommandBufferStatusError) {
  3566. GGML_LOG_INFO("error: %s\n", [[command_buffer error].localizedDescription UTF8String]);
  3567. }
  3568. return GGML_STATUS_FAILED;
  3569. }
  3570. id<MTLCommandBuffer> next_buffer = (i + 1 < n_cb ? ctx->command_buffers[i + 1] : nil);
  3571. if (!next_buffer) {
  3572. continue;
  3573. }
  3574. const bool next_queued = ([next_buffer status] != MTLCommandBufferStatusNotEnqueued);
  3575. if (next_queued) {
  3576. continue;
  3577. }
  3578. if (ctx->abort_callback && ctx->abort_callback(ctx->abort_callback_data)) {
  3579. GGML_LOG_INFO("%s: command buffer %d aborted", __func__, i);
  3580. return GGML_STATUS_ABORTED;
  3581. }
  3582. [next_buffer commit];
  3583. }
  3584. if (!should_capture && ctx->capture_started) {
  3585. [ctx->capture_scope endScope];
  3586. [[MTLCaptureManager sharedCaptureManager] stopCapture];
  3587. }
  3588. }
  3589. return GGML_STATUS_SUCCESS;
  3590. }
  3591. ////////////////////////////////////////////////////////////////////////////////
  3592. // backend interface
  3593. static void ggml_backend_metal_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  3594. struct ggml_backend_metal_buffer_context * ctx = (struct ggml_backend_metal_buffer_context *)buffer->context;
  3595. for (int i = 0; i < ctx->n_buffers; i++) {
  3596. [ctx->buffers[i].metal release];
  3597. }
  3598. ggml_backend_metal_device_rel(buffer->buft->device->context);
  3599. if (ctx->owned) {
  3600. #if TARGET_OS_OSX
  3601. vm_deallocate((vm_map_t)mach_task_self(), (vm_address_t)ctx->all_data, ctx->all_size);
  3602. #else
  3603. free(ctx->all_data);
  3604. #endif
  3605. }
  3606. free(ctx);
  3607. }
  3608. static void * ggml_backend_metal_buffer_get_base(ggml_backend_buffer_t buffer) {
  3609. struct ggml_backend_metal_buffer_context * ctx = (struct ggml_backend_metal_buffer_context *)buffer->context;
  3610. return ctx->all_data;
  3611. }
  3612. static void ggml_backend_metal_buffer_memset_tensor(ggml_backend_buffer_t buffer, struct ggml_tensor * tensor, uint8_t value, size_t offset, size_t size) {
  3613. memset((char *)tensor->data + offset, value, size);
  3614. UNUSED(buffer);
  3615. }
  3616. 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) {
  3617. memcpy((char *)tensor->data + offset, data, size);
  3618. UNUSED(buffer);
  3619. }
  3620. 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) {
  3621. memcpy(data, (const char *)tensor->data + offset, size);
  3622. UNUSED(buffer);
  3623. }
  3624. static bool ggml_backend_metal_buffer_cpy_tensor(ggml_backend_buffer_t buffer, const struct ggml_tensor * src, struct ggml_tensor * dst) {
  3625. if (ggml_backend_buffer_is_host(src->buffer)) {
  3626. memcpy(dst->data, src->data, ggml_nbytes(src));
  3627. return true;
  3628. }
  3629. return false;
  3630. UNUSED(buffer);
  3631. }
  3632. static void ggml_backend_metal_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  3633. struct ggml_backend_metal_buffer_context * ctx = (struct ggml_backend_metal_buffer_context *)buffer->context;
  3634. memset(ctx->all_data, value, ctx->all_size);
  3635. }
  3636. static struct ggml_backend_buffer_i ggml_backend_metal_buffer_i = {
  3637. /* .free_buffer = */ ggml_backend_metal_buffer_free_buffer,
  3638. /* .get_base = */ ggml_backend_metal_buffer_get_base,
  3639. /* .init_tensor = */ NULL,
  3640. /* .memset_tensor = */ ggml_backend_metal_buffer_memset_tensor,
  3641. /* .set_tensor = */ ggml_backend_metal_buffer_set_tensor,
  3642. /* .get_tensor = */ ggml_backend_metal_buffer_get_tensor,
  3643. /* .cpy_tensor = */ ggml_backend_metal_buffer_cpy_tensor,
  3644. /* .clear = */ ggml_backend_metal_buffer_clear,
  3645. /* .reset = */ NULL,
  3646. };
  3647. // default buffer type
  3648. static const char * ggml_backend_metal_buffer_type_get_name(ggml_backend_buffer_type_t buft) {
  3649. return "Metal";
  3650. UNUSED(buft);
  3651. }
  3652. static void ggml_backend_metal_log_allocated_size(id<MTLDevice> device, size_t size_aligned) {
  3653. #ifndef GGML_METAL_NDEBUG
  3654. #if TARGET_OS_OSX || (TARGET_OS_IOS && __clang_major__ >= 15)
  3655. if (@available(macOS 10.12, iOS 16.0, *)) {
  3656. GGML_LOG_DEBUG("%s: allocated buffer, size = %8.2f MiB, (%8.2f / %8.2f)\n",
  3657. __func__,
  3658. size_aligned / 1024.0 / 1024.0,
  3659. device.currentAllocatedSize / 1024.0 / 1024.0,
  3660. device.recommendedMaxWorkingSetSize / 1024.0 / 1024.0);
  3661. if (device.currentAllocatedSize > device.recommendedMaxWorkingSetSize) {
  3662. GGML_LOG_WARN("%s: warning: current allocated size is greater than the recommended max working set size\n", __func__);
  3663. }
  3664. } else {
  3665. GGML_LOG_INFO("%s: allocated buffer, size = %8.2f MiB, (%8.2f)\n",
  3666. __func__,
  3667. size_aligned / 1024.0 / 1024.0,
  3668. device.currentAllocatedSize / 1024.0 / 1024.0);
  3669. }
  3670. #endif
  3671. #endif
  3672. UNUSED(device);
  3673. UNUSED(size_aligned);
  3674. }
  3675. static ggml_backend_buffer_t ggml_backend_metal_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  3676. struct ggml_backend_metal_buffer_context * ctx = calloc(1, sizeof(struct ggml_backend_metal_buffer_context));
  3677. const size_t size_page = sysconf(_SC_PAGESIZE);
  3678. size_t size_aligned = size;
  3679. if ((size_aligned % size_page) != 0) {
  3680. size_aligned += (size_page - (size_aligned % size_page));
  3681. }
  3682. id<MTLDevice> device = ggml_backend_metal_device_acq(buft->device->context);
  3683. ctx->all_data = ggml_metal_host_malloc(size_aligned);
  3684. ctx->all_size = size_aligned;
  3685. ctx->owned = true;
  3686. ctx->n_buffers = 1;
  3687. if (ctx->all_data != NULL) {
  3688. ctx->buffers[0].data = ctx->all_data;
  3689. ctx->buffers[0].size = size;
  3690. ctx->buffers[0].metal = nil;
  3691. if (size_aligned > 0) {
  3692. ctx->buffers[0].metal = [device newBufferWithBytesNoCopy:ctx->all_data
  3693. length:size_aligned
  3694. options:MTLResourceStorageModeShared
  3695. deallocator:nil];
  3696. }
  3697. }
  3698. if (size_aligned > 0 && (ctx->all_data == NULL || ctx->buffers[0].metal == nil)) {
  3699. GGML_LOG_ERROR("%s: error: failed to allocate buffer, size = %8.2f MiB\n", __func__, size_aligned / 1024.0 / 1024.0);
  3700. free(ctx);
  3701. ggml_backend_metal_device_rel(buft->device->context);
  3702. return NULL;
  3703. }
  3704. //ggml_backend_metal_log_allocated_size(device, size_aligned);
  3705. return ggml_backend_buffer_init(buft, ggml_backend_metal_buffer_i, ctx, size);
  3706. }
  3707. static size_t ggml_backend_metal_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  3708. return 32;
  3709. UNUSED(buft);
  3710. }
  3711. static size_t ggml_backend_metal_buffer_type_get_max_size(ggml_backend_buffer_type_t buft) {
  3712. id<MTLDevice> device = ggml_backend_metal_device_acq(buft->device->context);
  3713. const size_t max_size = device.maxBufferLength;
  3714. ggml_backend_metal_device_rel(buft->device->context);
  3715. return max_size;
  3716. UNUSED(buft);
  3717. }
  3718. static bool ggml_backend_metal_buffer_type_is_host(ggml_backend_buffer_type_t buft) {
  3719. return true;
  3720. UNUSED(buft);
  3721. }
  3722. ggml_backend_buffer_type_t ggml_backend_metal_buffer_type(void) {
  3723. static struct ggml_backend_buffer_type ggml_backend_buffer_type_metal = {
  3724. /* .iface = */ {
  3725. /* .get_name = */ ggml_backend_metal_buffer_type_get_name,
  3726. /* .alloc_buffer = */ ggml_backend_metal_buffer_type_alloc_buffer,
  3727. /* .get_alignment = */ ggml_backend_metal_buffer_type_get_alignment,
  3728. /* .get_max_size = */ ggml_backend_metal_buffer_type_get_max_size,
  3729. /* .get_alloc_size = */ NULL, // defaults to ggml_nbytes
  3730. /* .is_host = */ ggml_backend_metal_buffer_type_is_host,
  3731. },
  3732. /* .device = */ &g_ggml_backend_metal_device,
  3733. /* .context = */ NULL,
  3734. };
  3735. return &ggml_backend_buffer_type_metal;
  3736. }
  3737. static const char * ggml_backend_metal_buffer_from_ptr_type_get_name(ggml_backend_buffer_type_t buft) {
  3738. return "Metal_Mapped";
  3739. UNUSED(buft);
  3740. }
  3741. static ggml_backend_buffer_type_t ggml_backend_metal_buffer_from_ptr_type(void) {
  3742. static struct ggml_backend_buffer_type ggml_backend_buffer_from_ptr_type_metal = {
  3743. /* .iface = */ {
  3744. /* .get_name = */ ggml_backend_metal_buffer_from_ptr_type_get_name,
  3745. /* .alloc_buffer = */ ggml_backend_metal_buffer_type_alloc_buffer,
  3746. /* .get_alignment = */ ggml_backend_metal_buffer_type_get_alignment,
  3747. /* .get_max_size = */ ggml_backend_metal_buffer_type_get_max_size,
  3748. /* .get_alloc_size = */ NULL, // defaults to ggml_nbytes
  3749. /* .is_host = */ ggml_backend_metal_buffer_type_is_host,
  3750. },
  3751. /* .device = */ &g_ggml_backend_metal_device,
  3752. /* .context = */ NULL,
  3753. };
  3754. return &ggml_backend_buffer_from_ptr_type_metal;
  3755. }
  3756. // TODO: obsoleted by ggml_backend_metal_device_buffer_from_ptr
  3757. ggml_backend_buffer_t ggml_backend_metal_buffer_from_ptr(void * data, size_t size, size_t max_size) {
  3758. struct ggml_backend_metal_buffer_context * ctx = calloc(1, sizeof(struct ggml_backend_metal_buffer_context));
  3759. ctx->all_data = data;
  3760. ctx->all_size = size;
  3761. ctx->owned = false;
  3762. ctx->n_buffers = 0;
  3763. const size_t size_page = sysconf(_SC_PAGESIZE);
  3764. // page-align the data ptr
  3765. {
  3766. const uintptr_t offs = (uintptr_t) data % size_page;
  3767. data = (void *) ((char *) data - offs);
  3768. size += offs;
  3769. }
  3770. size_t size_aligned = size;
  3771. if ((size_aligned % size_page) != 0) {
  3772. size_aligned += (size_page - (size_aligned % size_page));
  3773. }
  3774. id<MTLDevice> device = ggml_backend_metal_device_acq(&g_ggml_ctx_dev_main);
  3775. // the buffer fits into the max buffer size allowed by the device
  3776. if (size_aligned <= device.maxBufferLength) {
  3777. ctx->buffers[ctx->n_buffers].data = data;
  3778. ctx->buffers[ctx->n_buffers].size = size;
  3779. ctx->buffers[ctx->n_buffers].metal = nil;
  3780. if (size_aligned > 0) {
  3781. ctx->buffers[ctx->n_buffers].metal = [device newBufferWithBytesNoCopy:data length:size_aligned options:MTLResourceStorageModeShared deallocator:nil];
  3782. if (ctx->buffers[ctx->n_buffers].metal == nil) {
  3783. GGML_LOG_ERROR("%s: error: failed to allocate buffer, size = %8.2f MiB\n", __func__, size_aligned / 1024.0 / 1024.0);
  3784. return false;
  3785. }
  3786. }
  3787. ggml_backend_metal_log_allocated_size(device, size_aligned);
  3788. ++ctx->n_buffers;
  3789. } else {
  3790. // this overlap between the views will guarantee that the tensor with the maximum size will fully fit into
  3791. // one of the views
  3792. const size_t size_ovlp = ((max_size + size_page - 1) / size_page + 1) * size_page; // round-up 2 pages just in case
  3793. const size_t size_step = device.maxBufferLength - size_ovlp;
  3794. const size_t size_view = device.maxBufferLength;
  3795. for (size_t i = 0; i < size; i += size_step) {
  3796. const size_t size_step_aligned = (i + size_view <= size) ? size_view : (size_aligned - i);
  3797. ctx->buffers[ctx->n_buffers].data = (void *) ((uint8_t *) data + i);
  3798. ctx->buffers[ctx->n_buffers].size = size_step_aligned;
  3799. ctx->buffers[ctx->n_buffers].metal = nil;
  3800. if (size_step_aligned > 0) {
  3801. ctx->buffers[ctx->n_buffers].metal = [device newBufferWithBytesNoCopy:(void *) ((uint8_t *) data + i) length:size_step_aligned options:MTLResourceStorageModeShared deallocator:nil];
  3802. if (ctx->buffers[ctx->n_buffers].metal == nil) {
  3803. GGML_LOG_ERROR("%s: error: failed to allocate buffer, size = %8.2f MiB\n", __func__, size_step_aligned / 1024.0 / 1024.0);
  3804. return false;
  3805. }
  3806. }
  3807. ggml_backend_metal_log_allocated_size(device, size_step_aligned);
  3808. if (i + size_step < size) {
  3809. GGML_LOG_INFO("\n");
  3810. }
  3811. ++ctx->n_buffers;
  3812. }
  3813. }
  3814. return ggml_backend_buffer_init(ggml_backend_metal_buffer_from_ptr_type(), ggml_backend_metal_buffer_i, ctx, size);
  3815. }
  3816. // backend
  3817. static const char * ggml_backend_metal_name(ggml_backend_t backend) {
  3818. return "Metal";
  3819. UNUSED(backend);
  3820. }
  3821. static void ggml_backend_metal_free(ggml_backend_t backend) {
  3822. struct ggml_backend_metal_context * ctx = backend->context;
  3823. struct ggml_backend_metal_device_context * ctx_dev = backend->device->context;
  3824. ggml_backend_metal_device_rel(ctx_dev);
  3825. ggml_metal_free(ctx);
  3826. free(backend);
  3827. }
  3828. static enum ggml_status ggml_backend_metal_graph_compute(ggml_backend_t backend, struct ggml_cgraph * cgraph) {
  3829. return ggml_metal_graph_compute(backend, cgraph);
  3830. }
  3831. static void ggml_backend_metal_set_n_cb(ggml_backend_t backend, int n_cb) {
  3832. GGML_ASSERT(ggml_backend_is_metal(backend));
  3833. struct ggml_backend_metal_context * ctx = (struct ggml_backend_metal_context *)backend->context;
  3834. if (ctx->n_cb != n_cb) {
  3835. ctx->n_cb = MIN(n_cb, GGML_METAL_MAX_COMMAND_BUFFERS);
  3836. if (ctx->n_cb > 2) {
  3837. GGML_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);
  3838. }
  3839. }
  3840. if (ctx->encode_async) {
  3841. Block_release(ctx->encode_async);
  3842. }
  3843. ctx->encode_async = Block_copy(^(size_t iter) {
  3844. const int cb_idx = iter;
  3845. const int n_cb_l = ctx->n_cb;
  3846. const int n_nodes_0 = ctx->n_nodes_0;
  3847. const int n_nodes_1 = ctx->n_nodes_1;
  3848. const int n_nodes_per_cb = ctx->n_nodes_per_cb;
  3849. id<MTLCommandBuffer> command_buffer = ctx->command_buffers[cb_idx];
  3850. id<MTLComputeCommandEncoder> encoder = [command_buffer computeCommandEncoder];
  3851. int node_start = 0;
  3852. int node_end = n_nodes_0;
  3853. if (cb_idx < n_cb_l) {
  3854. node_start = n_nodes_0 + ( (cb_idx + 0) * n_nodes_per_cb);
  3855. 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));
  3856. }
  3857. const bool should_capture = ctx->capture_next_compute;
  3858. for (int idx = node_start; idx < node_end; ++idx) {
  3859. if (should_capture) {
  3860. [encoder pushDebugGroup:[NSString stringWithCString:ggml_op_desc(ggml_graph_node(ctx->gf, idx)) encoding:NSUTF8StringEncoding]];
  3861. }
  3862. ggml_metal_encode_node(backend, idx, encoder);
  3863. if (should_capture) {
  3864. [encoder popDebugGroup];
  3865. }
  3866. }
  3867. [encoder endEncoding];
  3868. if (cb_idx < 2 || ctx->abort_callback == NULL) {
  3869. [command_buffer commit];
  3870. }
  3871. });
  3872. }
  3873. static struct ggml_backend_i ggml_backend_metal_i = {
  3874. /* .get_name = */ ggml_backend_metal_name,
  3875. /* .free = */ ggml_backend_metal_free,
  3876. /* .set_tensor_async = */ NULL,
  3877. /* .get_tensor_async = */ NULL,
  3878. /* .cpy_tensor_async = */ NULL,
  3879. /* .synchronize = */ NULL,
  3880. /* .graph_plan_create = */ NULL,
  3881. /* .graph_plan_free = */ NULL,
  3882. /* .graph_plan_update = */ NULL,
  3883. /* .graph_plan_compute = */ NULL,
  3884. /* .graph_compute = */ ggml_backend_metal_graph_compute,
  3885. /* .event_record = */ NULL,
  3886. /* .event_wait = */ NULL,
  3887. };
  3888. static ggml_guid_t ggml_backend_metal_guid(void) {
  3889. static ggml_guid guid = { 0x81, 0xa1, 0x8b, 0x1e, 0x71, 0xec, 0x79, 0xed, 0x2b, 0x85, 0xdc, 0x8a, 0x61, 0x98, 0x30, 0xe6 };
  3890. return &guid;
  3891. }
  3892. // TODO: remove in the future
  3893. ggml_backend_t ggml_backend_metal_init(void) {
  3894. ggml_backend_dev_t dev = ggml_backend_reg_dev_get(ggml_backend_metal_reg(), 0);
  3895. struct ggml_backend_metal_context * ctx = ggml_metal_init(dev);
  3896. if (ctx == NULL) {
  3897. GGML_LOG_ERROR("%s: error: failed to allocate context\n", __func__);
  3898. return NULL;
  3899. }
  3900. ggml_backend_t backend = malloc(sizeof(struct ggml_backend));
  3901. *backend = (struct ggml_backend) {
  3902. /* .guid = */ ggml_backend_metal_guid(),
  3903. /* .interface = */ ggml_backend_metal_i,
  3904. /* .device = */ dev,
  3905. /* .context = */ ctx,
  3906. };
  3907. ggml_backend_metal_set_n_cb(backend, 1);
  3908. return backend;
  3909. }
  3910. bool ggml_backend_is_metal(ggml_backend_t backend) {
  3911. return backend != NULL && ggml_guid_matches(backend->guid, ggml_backend_metal_guid());
  3912. }
  3913. void ggml_backend_metal_set_abort_callback(ggml_backend_t backend, ggml_abort_callback abort_callback, void * user_data) {
  3914. GGML_ASSERT(ggml_backend_is_metal(backend));
  3915. struct ggml_backend_metal_context * ctx = (struct ggml_backend_metal_context *)backend->context;
  3916. ctx->abort_callback = abort_callback;
  3917. ctx->abort_callback_data = user_data;
  3918. }
  3919. bool ggml_backend_metal_supports_family(ggml_backend_t backend, int family) {
  3920. GGML_ASSERT(ggml_backend_is_metal(backend));
  3921. struct ggml_backend_metal_device_context * ctx_dev = backend->device->context;
  3922. return [ctx_dev->mtl_device supportsFamily:(MTLGPUFamilyApple1 + family - 1)];
  3923. }
  3924. void ggml_backend_metal_capture_next_compute(ggml_backend_t backend) {
  3925. GGML_ASSERT(ggml_backend_is_metal(backend));
  3926. struct ggml_backend_metal_context * ctx = (struct ggml_backend_metal_context *)backend->context;
  3927. ctx->capture_next_compute = true;
  3928. }
  3929. // backend device
  3930. static const char * ggml_backend_metal_device_get_name(ggml_backend_dev_t dev) {
  3931. return "Metal";
  3932. GGML_UNUSED(dev);
  3933. }
  3934. static const char * ggml_backend_metal_device_get_description(ggml_backend_dev_t dev) {
  3935. // acq/rel just to populate ctx->name in case it hasn't been done yet
  3936. struct ggml_backend_metal_device_context * ctx_dev = (struct ggml_backend_metal_device_context *)dev->context;
  3937. ggml_backend_metal_device_acq(ctx_dev);
  3938. ggml_backend_metal_device_rel(ctx_dev);
  3939. return ctx_dev->name;
  3940. }
  3941. static void ggml_backend_metal_device_get_memory(ggml_backend_dev_t dev, size_t * free, size_t * total) {
  3942. if (@available(macOS 10.12, iOS 16.0, *)) {
  3943. struct ggml_backend_metal_device_context * ctx_dev = (struct ggml_backend_metal_device_context *)dev->context;
  3944. id<MTLDevice> device = ggml_backend_metal_device_acq(ctx_dev);
  3945. *total = device.recommendedMaxWorkingSetSize;
  3946. *free = *total - device.currentAllocatedSize;
  3947. ggml_backend_metal_device_rel(ctx_dev);
  3948. } else {
  3949. *free = 1;
  3950. *total = 1;
  3951. }
  3952. }
  3953. static enum ggml_backend_dev_type ggml_backend_metal_device_get_type(ggml_backend_dev_t dev) {
  3954. return GGML_BACKEND_DEVICE_TYPE_GPU;
  3955. GGML_UNUSED(dev);
  3956. }
  3957. static void ggml_backend_metal_device_get_props(ggml_backend_dev_t dev, struct ggml_backend_dev_props * props) {
  3958. props->name = ggml_backend_metal_device_get_name(dev);
  3959. props->description = ggml_backend_metal_device_get_description(dev);
  3960. props->type = ggml_backend_metal_device_get_type(dev);
  3961. ggml_backend_metal_device_get_memory(dev, &props->memory_free, &props->memory_total);
  3962. props->caps = (struct ggml_backend_dev_caps) {
  3963. /* .async = */ false,
  3964. /* .host_buffer = */ false,
  3965. /* .buffer_from_host_ptr = */ true,
  3966. /* .events = */ false,
  3967. };
  3968. }
  3969. static ggml_backend_t ggml_backend_metal_device_init(ggml_backend_dev_t dev, const char * params) {
  3970. struct ggml_backend_metal_context * ctx = ggml_metal_init(dev);
  3971. if (ctx == NULL) {
  3972. GGML_LOG_ERROR("%s: error: failed to allocate context\n", __func__);
  3973. return NULL;
  3974. }
  3975. ggml_backend_t backend = malloc(sizeof(struct ggml_backend));
  3976. *backend = (struct ggml_backend) {
  3977. /* .guid = */ ggml_backend_metal_guid(),
  3978. /* .interface = */ ggml_backend_metal_i,
  3979. /* .device = */ dev,
  3980. /* .context = */ ctx,
  3981. };
  3982. ggml_backend_metal_set_n_cb(backend, 1);
  3983. return backend;
  3984. GGML_UNUSED(params);
  3985. }
  3986. static ggml_backend_buffer_type_t ggml_backend_metal_device_get_buffer_type(ggml_backend_dev_t dev) {
  3987. return ggml_backend_metal_buffer_type();
  3988. GGML_UNUSED(dev);
  3989. }
  3990. static ggml_backend_buffer_t ggml_backend_metal_device_buffer_from_ptr(ggml_backend_dev_t dev, void * ptr, size_t size, size_t max_tensor_size) {
  3991. struct ggml_backend_metal_buffer_context * ctx = calloc(1, sizeof(struct ggml_backend_metal_buffer_context));
  3992. ctx->all_data = ptr;
  3993. ctx->all_size = size;
  3994. ctx->owned = false;
  3995. ctx->n_buffers = 0;
  3996. const size_t size_page = sysconf(_SC_PAGESIZE);
  3997. // page-align the data ptr
  3998. {
  3999. const uintptr_t offs = (uintptr_t) ptr % size_page;
  4000. ptr = (void *) ((char *) ptr - offs);
  4001. size += offs;
  4002. }
  4003. size_t size_aligned = size;
  4004. if ((size_aligned % size_page) != 0) {
  4005. size_aligned += (size_page - (size_aligned % size_page));
  4006. }
  4007. struct ggml_backend_metal_device_context * ctx_dev = (struct ggml_backend_metal_device_context *)dev->context;
  4008. id<MTLDevice> device = ggml_backend_metal_device_acq(ctx_dev);
  4009. // the buffer fits into the max buffer size allowed by the device
  4010. if (size_aligned <= device.maxBufferLength) {
  4011. ctx->buffers[ctx->n_buffers].data = ptr;
  4012. ctx->buffers[ctx->n_buffers].size = size;
  4013. ctx->buffers[ctx->n_buffers].metal = nil;
  4014. if (size_aligned > 0) {
  4015. ctx->buffers[ctx->n_buffers].metal = [device newBufferWithBytesNoCopy:ptr length:size_aligned options:MTLResourceStorageModeShared deallocator:nil];
  4016. if (ctx->buffers[ctx->n_buffers].metal == nil) {
  4017. GGML_LOG_ERROR("%s: error: failed to allocate buffer, size = %8.2f MiB\n", __func__, size_aligned / 1024.0 / 1024.0);
  4018. return false;
  4019. }
  4020. }
  4021. ggml_backend_metal_log_allocated_size(device, size_aligned);
  4022. ++ctx->n_buffers;
  4023. } else {
  4024. // this overlap between the views will guarantee that the tensor with the maximum size will fully fit into
  4025. // one of the views
  4026. const size_t size_ovlp = ((max_tensor_size + size_page - 1) / size_page + 1) * size_page; // round-up 2 pages just in case
  4027. const size_t size_step = device.maxBufferLength - size_ovlp;
  4028. const size_t size_view = device.maxBufferLength;
  4029. for (size_t i = 0; i < size; i += size_step) {
  4030. const size_t size_step_aligned = (i + size_view <= size) ? size_view : (size_aligned - i);
  4031. ctx->buffers[ctx->n_buffers].data = (void *) ((uint8_t *) ptr + i);
  4032. ctx->buffers[ctx->n_buffers].size = size_step_aligned;
  4033. ctx->buffers[ctx->n_buffers].metal = nil;
  4034. if (size_step_aligned > 0) {
  4035. ctx->buffers[ctx->n_buffers].metal = [device newBufferWithBytesNoCopy:(void *) ((uint8_t *) ptr + i) length:size_step_aligned options:MTLResourceStorageModeShared deallocator:nil];
  4036. if (ctx->buffers[ctx->n_buffers].metal == nil) {
  4037. GGML_LOG_ERROR("%s: error: failed to allocate buffer, size = %8.2f MiB\n", __func__, size_step_aligned / 1024.0 / 1024.0);
  4038. return false;
  4039. }
  4040. }
  4041. ggml_backend_metal_log_allocated_size(device, size_step_aligned);
  4042. if (i + size_step < size) {
  4043. GGML_LOG_INFO("\n");
  4044. }
  4045. ++ctx->n_buffers;
  4046. }
  4047. }
  4048. return ggml_backend_buffer_init(ggml_backend_metal_buffer_from_ptr_type(), ggml_backend_metal_buffer_i, ctx, size);
  4049. }
  4050. static bool ggml_backend_metal_device_supports_op(ggml_backend_dev_t dev, const struct ggml_tensor * op) {
  4051. struct ggml_backend_metal_device_context * ctx_dev = dev->context;
  4052. return ggml_metal_supports_op(ctx_dev, op);
  4053. }
  4054. static bool ggml_backend_metal_device_supports_buft(ggml_backend_dev_t dev, ggml_backend_buffer_type_t buft) {
  4055. return buft->iface.get_name == ggml_backend_metal_buffer_type_get_name ||
  4056. buft->iface.get_name == ggml_backend_metal_buffer_from_ptr_type_get_name;
  4057. UNUSED(dev);
  4058. }
  4059. static bool ggml_backend_metal_device_offload_op(ggml_backend_dev_t dev, const struct ggml_tensor * op) {
  4060. return false;
  4061. GGML_UNUSED(dev);
  4062. GGML_UNUSED(op);
  4063. }
  4064. static struct ggml_backend_device_i ggml_backend_metal_device_i = {
  4065. /* .get_name = */ ggml_backend_metal_device_get_name,
  4066. /* .get_description = */ ggml_backend_metal_device_get_description,
  4067. /* .get_memory = */ ggml_backend_metal_device_get_memory,
  4068. /* .get_type = */ ggml_backend_metal_device_get_type,
  4069. /* .get_props = */ ggml_backend_metal_device_get_props,
  4070. /* .init_backend = */ ggml_backend_metal_device_init,
  4071. /* .get_buffer_type = */ ggml_backend_metal_device_get_buffer_type,
  4072. /* .get_host_buffer_type = */ NULL,
  4073. /* .buffer_from_host_ptr = */ ggml_backend_metal_device_buffer_from_ptr,
  4074. /* .supports_op = */ ggml_backend_metal_device_supports_op,
  4075. /* .supports_buft = */ ggml_backend_metal_device_supports_buft,
  4076. /* .offload_op = */ ggml_backend_metal_device_offload_op,
  4077. /* .event_new = */ NULL,
  4078. /* .event_free = */ NULL,
  4079. /* .event_synchronize = */ NULL,
  4080. };
  4081. // backend registry
  4082. static const char * ggml_backend_metal_reg_get_name(ggml_backend_reg_t reg) {
  4083. return "Metal";
  4084. GGML_UNUSED(reg);
  4085. }
  4086. static size_t ggml_backend_metal_reg_device_count(ggml_backend_reg_t reg) {
  4087. return 1;
  4088. GGML_UNUSED(reg);
  4089. }
  4090. static ggml_backend_dev_t ggml_backend_metal_reg_device_get(ggml_backend_reg_t reg, size_t index) {
  4091. GGML_ASSERT(index == 0);
  4092. return &g_ggml_backend_metal_device;
  4093. GGML_UNUSED(reg);
  4094. GGML_UNUSED(index);
  4095. }
  4096. static struct ggml_backend_feature g_ggml_backend_metal_features[] = {
  4097. #if defined(GGML_METAL_EMBED_LIBRARY)
  4098. { "EMBED_LIBRARY", "1" },
  4099. #endif
  4100. #if defined(GGML_METAL_USE_BF16)
  4101. { "BF16", "1" },
  4102. #endif
  4103. { nil, nil },
  4104. };
  4105. static struct ggml_backend_feature * ggml_backend_metal_get_features(ggml_backend_reg_t reg) {
  4106. return g_ggml_backend_metal_features;
  4107. GGML_UNUSED(reg);
  4108. }
  4109. static void * ggml_backend_metal_get_proc_address(ggml_backend_reg_t reg, const char * name) {
  4110. if (strcmp(name, "ggml_backend_get_features") == 0) {
  4111. return (void *)ggml_backend_metal_get_features;
  4112. }
  4113. return NULL;
  4114. GGML_UNUSED(reg);
  4115. }
  4116. static struct ggml_backend_reg_i ggml_backend_metal_reg_i = {
  4117. /* .get_name = */ ggml_backend_metal_reg_get_name,
  4118. /* .device_count = */ ggml_backend_metal_reg_device_count,
  4119. /* .device_get = */ ggml_backend_metal_reg_device_get,
  4120. /* .get_proc_address = */ ggml_backend_metal_get_proc_address,
  4121. };
  4122. ggml_backend_reg_t ggml_backend_metal_reg(void) {
  4123. // TODO: make this thread-safe somehow?
  4124. {
  4125. g_ggml_backend_metal_reg = (struct ggml_backend_reg) {
  4126. /* .api_version = */ GGML_BACKEND_API_VERSION,
  4127. /* .iface = */ ggml_backend_metal_reg_i,
  4128. /* .context = */ NULL,
  4129. };
  4130. g_ggml_backend_metal_device = (struct ggml_backend_device) {
  4131. /* .iface = */ ggml_backend_metal_device_i,
  4132. /* .reg = */ &g_ggml_backend_metal_reg,
  4133. /* .context = */ &g_ggml_ctx_dev_main,
  4134. };
  4135. }
  4136. return &g_ggml_backend_metal_reg;
  4137. }
  4138. GGML_BACKEND_DL_IMPL(ggml_backend_metal_reg)