ggml-metal_darwin_arm64.m 260 KB

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