ggml-vulkan.cpp 647 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927
  1. #include "ggml-vulkan.h"
  2. #include <vulkan/vulkan_core.h>
  3. #if defined(GGML_VULKAN_RUN_TESTS) || defined(GGML_VULKAN_CHECK_RESULTS)
  4. #include <chrono>
  5. #include "ggml-cpu.h"
  6. #endif
  7. #include <vulkan/vulkan.hpp>
  8. #include <algorithm>
  9. #include <cmath>
  10. #include <iomanip>
  11. #include <iostream>
  12. #include <tuple>
  13. #include <vector>
  14. #include <sstream>
  15. #include <utility>
  16. #include <memory>
  17. #include <limits>
  18. #include <map>
  19. #include <unordered_map>
  20. #include <memory>
  21. #include <mutex>
  22. #include <future>
  23. #include <thread>
  24. #if defined(_MSC_VER)
  25. # define NOMINMAX 1
  26. # include <windows.h>
  27. # define YIELD() YieldProcessor()
  28. #elif defined(__clang__) || defined(__GNUC__)
  29. # if defined(__x86_64__) ||defined(__i386__)
  30. # include <immintrin.h>
  31. # define YIELD() _mm_pause()
  32. # elif defined(__arm__) || defined(__aarch64__)
  33. # if defined(__clang__)
  34. # include <arm_acle.h>
  35. # define YIELD() __yield()
  36. # else
  37. # define YIELD() asm volatile("yield")
  38. # endif
  39. # endif
  40. #endif
  41. #if !defined(YIELD)
  42. #define YIELD()
  43. #endif
  44. #include "ggml-impl.h"
  45. #include "ggml-backend-impl.h"
  46. #include "ggml-vulkan-shaders.hpp"
  47. // remove this once it's more widely available in the SDK
  48. #if !defined(VK_KHR_shader_bfloat16)
  49. #define VK_KHR_shader_bfloat16 1
  50. #define VK_KHR_SHADER_BFLOAT16_SPEC_VERSION 1
  51. #define VK_KHR_SHADER_BFLOAT16_EXTENSION_NAME "VK_KHR_shader_bfloat16"
  52. #define VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_BFLOAT16_FEATURES_KHR ((VkStructureType)1000141000)
  53. #define VK_COMPONENT_TYPE_BFLOAT16_KHR ((VkComponentTypeKHR)1000141000)
  54. typedef struct VkPhysicalDeviceShaderBfloat16FeaturesKHR {
  55. VkStructureType sType;
  56. void* pNext;
  57. VkBool32 shaderBFloat16Type;
  58. VkBool32 shaderBFloat16DotProduct;
  59. VkBool32 shaderBFloat16CooperativeMatrix;
  60. } VkPhysicalDeviceShaderBfloat16FeaturesKHR;
  61. #endif
  62. #define ROUNDUP_POW2(M, N) (((M) + (N) - 1) & ~((N) - 1))
  63. #define CEIL_DIV(M, N) (((M) + (N)-1) / (N))
  64. static bool is_pow2(uint32_t x) { return x > 1 && (x & (x-1)) == 0; }
  65. #define VK_VENDOR_ID_AMD 0x1002
  66. #define VK_VENDOR_ID_APPLE 0x106b
  67. #define VK_VENDOR_ID_INTEL 0x8086
  68. #define VK_VENDOR_ID_NVIDIA 0x10de
  69. #define VK_DEVICE_DESCRIPTOR_POOL_SIZE 256
  70. #define GGML_VK_MAX_NODES 8192
  71. #define MAX_VK_BUFFERS 256
  72. #define VK_CHECK(err, msg) \
  73. do { \
  74. vk::Result err_ = (err); \
  75. if (err_ != vk::Result::eSuccess) { \
  76. fprintf(stderr, "ggml_vulkan: %s error %s at %s:%d\n", \
  77. #err, to_string(err_).c_str(), __FILE__, __LINE__); \
  78. exit(1); \
  79. } \
  80. } while (0)
  81. #ifdef GGML_VULKAN_DEBUG
  82. #define VK_LOG_DEBUG(msg) std::cerr << msg << std::endl
  83. #else
  84. #define VK_LOG_DEBUG(msg) ((void) 0)
  85. #endif // GGML_VULKAN_DEBUG
  86. struct ggml_backend_vk_context;
  87. #define MAX_PARAMETER_COUNT 12
  88. // Max number of adds that can be fused without exceeding MAX_PARAMETER_COUNT.
  89. #define MAX_FUSED_ADDS (MAX_PARAMETER_COUNT - 3)
  90. struct vk_pipeline_struct {
  91. std::string name;
  92. vk::ShaderModule shader_module;
  93. vk::PipelineLayout layout;
  94. vk::Pipeline pipeline;
  95. uint32_t push_constant_size;
  96. uint32_t parameter_count;
  97. std::array<uint32_t, 3> wg_denoms;
  98. uint32_t align;
  99. // true if fields have been set by ggml_vk_create_pipeline
  100. bool initialized {};
  101. // set to true to request the pipeline is compiled after the dryrun
  102. bool needed {};
  103. // set to true when the shader has been compiled
  104. bool compiled {};
  105. };
  106. typedef std::shared_ptr<vk_pipeline_struct> vk_pipeline;
  107. typedef std::weak_ptr<vk_pipeline_struct> vk_pipeline_ref;
  108. static void ggml_vk_destroy_pipeline(vk::Device& device, vk_pipeline& pipeline);
  109. struct vk_matmul_pipeline_struct {
  110. vk_pipeline l, m, s;
  111. vk_pipeline a_l, a_m, a_s;
  112. };
  113. typedef std::shared_ptr<vk_matmul_pipeline_struct> vk_matmul_pipeline;
  114. struct vk_matmul_pipeline2 {
  115. vk_matmul_pipeline2() {
  116. f16acc = std::make_shared<vk_matmul_pipeline_struct>();
  117. f32acc = std::make_shared<vk_matmul_pipeline_struct>();
  118. }
  119. vk_matmul_pipeline f32acc;
  120. vk_matmul_pipeline f16acc;
  121. };
  122. struct vk_device_struct;
  123. typedef std::shared_ptr<vk_device_struct> vk_device;
  124. typedef std::weak_ptr<vk_device_struct> vk_device_ref;
  125. struct vk_buffer_struct;
  126. typedef std::shared_ptr<vk_buffer_struct> vk_buffer;
  127. typedef std::weak_ptr<vk_buffer_struct> vk_buffer_ref;
  128. struct ggml_backend_vk_buffer_type_context {
  129. std::string name;
  130. vk_device device;
  131. };
  132. struct vk_queue;
  133. // Stores command pool/buffers. There's an instance of this
  134. // for each (context,queue) pair and for each (device,queue) pair.
  135. struct vk_command_pool {
  136. void init(vk_device& device, vk_queue *q_);
  137. void destroy(vk::Device& device);
  138. vk::CommandPool pool;
  139. uint32_t cmd_buffer_idx;
  140. std::vector<vk::CommandBuffer> cmd_buffers;
  141. vk_queue *q;
  142. };
  143. // Prevent simultaneous submissions to the same queue.
  144. // This could be per vk_queue if we stopped having two vk_queue structures
  145. // sharing the same vk::Queue.
  146. static std::mutex queue_mutex;
  147. struct vk_queue {
  148. uint32_t queue_family_index;
  149. vk::Queue queue;
  150. vk_command_pool cmd_pool;
  151. vk::PipelineStageFlags stage_flags;
  152. bool transfer_only;
  153. // copy everything except the cmd_pool
  154. void copyFrom(vk_queue &other) {
  155. queue_family_index = other.queue_family_index;
  156. queue = other.queue;
  157. stage_flags = other.stage_flags;
  158. transfer_only = other.transfer_only;
  159. }
  160. };
  161. static const char * ggml_backend_vk_buffer_type_name(ggml_backend_buffer_type_t buft);
  162. static ggml_backend_buffer_t ggml_backend_vk_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size);
  163. static size_t ggml_backend_vk_buffer_type_get_alignment(ggml_backend_buffer_type_t buft);
  164. static size_t ggml_backend_vk_buffer_type_get_max_size(ggml_backend_buffer_type_t buft);
  165. static size_t ggml_backend_vk_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor);
  166. static ggml_backend_buffer_type_i ggml_backend_vk_buffer_type_interface = {
  167. /* .get_name = */ ggml_backend_vk_buffer_type_name,
  168. /* .alloc_buffer = */ ggml_backend_vk_buffer_type_alloc_buffer,
  169. /* .get_alignment = */ ggml_backend_vk_buffer_type_get_alignment,
  170. /* .get_max_size = */ ggml_backend_vk_buffer_type_get_max_size,
  171. /* .get_alloc_size = */ ggml_backend_vk_buffer_type_get_alloc_size,
  172. /* .is_host = */ NULL,
  173. };
  174. #ifdef GGML_VULKAN_MEMORY_DEBUG
  175. class vk_memory_logger;
  176. #endif
  177. class vk_perf_logger;
  178. static void ggml_vk_destroy_buffer(vk_buffer& buf);
  179. static constexpr uint32_t mul_mat_vec_max_cols = 8;
  180. static constexpr uint32_t p021_max_gqa_ratio = 8;
  181. enum vk_device_architecture {
  182. OTHER,
  183. AMD_GCN,
  184. AMD_RDNA1,
  185. AMD_RDNA2,
  186. AMD_RDNA3,
  187. INTEL_XE2,
  188. NVIDIA_PRE_TURING,
  189. };
  190. static vk_device_architecture get_device_architecture(const vk::PhysicalDevice& device) {
  191. vk::PhysicalDeviceProperties props = device.getProperties();
  192. if (props.vendorID == VK_VENDOR_ID_AMD) {
  193. const std::vector<vk::ExtensionProperties> ext_props = device.enumerateDeviceExtensionProperties();
  194. bool amd_shader_core_properties = false;
  195. bool integer_dot_product = false;
  196. bool subgroup_size_control = false;
  197. for (const auto& properties : ext_props) {
  198. if (strcmp("VK_AMD_shader_core_properties", properties.extensionName) == 0) {
  199. amd_shader_core_properties = true;
  200. } else if (strcmp("VK_KHR_shader_integer_dot_product", properties.extensionName) == 0) {
  201. integer_dot_product = true;
  202. } else if (strcmp("VK_EXT_subgroup_size_control", properties.extensionName) == 0) {
  203. subgroup_size_control = true;
  204. }
  205. }
  206. if (!amd_shader_core_properties || !integer_dot_product || !subgroup_size_control) {
  207. return vk_device_architecture::OTHER;
  208. }
  209. vk::PhysicalDeviceProperties2 props2;
  210. vk::PhysicalDeviceShaderCorePropertiesAMD shader_core_props_amd;
  211. vk::PhysicalDeviceShaderIntegerDotProductPropertiesKHR integer_dot_props;
  212. vk::PhysicalDeviceSubgroupSizeControlPropertiesEXT subgroup_size_control_props;
  213. props2.pNext = &shader_core_props_amd;
  214. shader_core_props_amd.pNext = &integer_dot_props;
  215. integer_dot_props.pNext = &subgroup_size_control_props;
  216. device.getProperties2(&props2);
  217. if (subgroup_size_control_props.maxSubgroupSize == 64 && subgroup_size_control_props.minSubgroupSize == 64) {
  218. return vk_device_architecture::AMD_GCN;
  219. }
  220. if (subgroup_size_control_props.maxSubgroupSize == 64 && subgroup_size_control_props.minSubgroupSize == 32) {
  221. // RDNA
  222. if (shader_core_props_amd.wavefrontsPerSimd == 20) {
  223. return vk_device_architecture::AMD_RDNA1;
  224. }
  225. if (integer_dot_props.integerDotProduct4x8BitPackedMixedSignednessAccelerated) {
  226. return vk_device_architecture::AMD_RDNA3;
  227. }
  228. return vk_device_architecture::AMD_RDNA2;
  229. }
  230. } else if (props.vendorID == VK_VENDOR_ID_INTEL) {
  231. const std::vector<vk::ExtensionProperties> ext_props = device.enumerateDeviceExtensionProperties();
  232. bool subgroup_size_control = false;
  233. for (const auto& properties : ext_props) {
  234. if (strcmp("VK_EXT_subgroup_size_control", properties.extensionName) == 0) {
  235. subgroup_size_control = true;
  236. }
  237. }
  238. if (!subgroup_size_control) {
  239. return vk_device_architecture::OTHER;
  240. }
  241. vk::PhysicalDeviceProperties2 props2;
  242. vk::PhysicalDeviceSubgroupSizeControlPropertiesEXT subgroup_size_control_props;
  243. props2.pNext = &subgroup_size_control_props;
  244. device.getProperties2(&props2);
  245. if (subgroup_size_control_props.minSubgroupSize == 16) {
  246. // Xe2 architecture uses SIMD16 while previous Xe and Gen architecture uses SIMD8.
  247. // Minimum subgroup size matches the SIMD width so we distinguish architecture by checking this value.
  248. // https://www.intel.com/content/www/us/en/content-details/824434/2024-intel-tech-tour-xe2-and-lunar-lake-s-gpu.html
  249. // https://www.intel.com/content/www/us/en/docs/oneapi/optimization-guide-gpu/2025-0/intel-xe-gpu-architecture.html
  250. return vk_device_architecture::INTEL_XE2;
  251. }
  252. } else if (props.vendorID == VK_VENDOR_ID_NVIDIA) {
  253. const std::vector<vk::ExtensionProperties> ext_props = device.enumerateDeviceExtensionProperties();
  254. bool cooperative_matrix = false;
  255. // Detect "pre-turing" based on lack of coopmat support.
  256. for (const auto& properties : ext_props) {
  257. if (strcmp("VK_KHR_cooperative_matrix", properties.extensionName) == 0) {
  258. cooperative_matrix = true;
  259. break;
  260. }
  261. }
  262. if (!cooperative_matrix) {
  263. return vk_device_architecture::NVIDIA_PRE_TURING;
  264. }
  265. }
  266. return vk_device_architecture::OTHER;
  267. }
  268. enum vk_conv_shapes {
  269. CONV_SHAPE_128x128,
  270. CONV_SHAPE_64x32,
  271. CONV_SHAPE_32x256,
  272. CONV_SHAPE_COUNT,
  273. };
  274. enum dmmv_wg_sizes {
  275. DMMV_WG_SIZE_SUBGROUP,
  276. DMMV_WG_SIZE_LARGE,
  277. DMMV_WG_SIZE_COUNT,
  278. };
  279. enum FaCodePath {
  280. FA_SCALAR,
  281. FA_COOPMAT1,
  282. FA_COOPMAT2,
  283. };
  284. struct vk_fa_pipeline_state {
  285. vk_fa_pipeline_state(uint32_t HSK, uint32_t HSV, bool small_rows, FaCodePath path, bool aligned, bool f32acc)
  286. : HSK(HSK), HSV(HSV), small_rows(small_rows), path(path), aligned(aligned), f32acc(f32acc) {}
  287. uint32_t HSK, HSV;
  288. bool small_rows;
  289. FaCodePath path;
  290. bool aligned;
  291. bool f32acc;
  292. bool operator<(const vk_fa_pipeline_state &b) const {
  293. return std::tie(HSK, HSV, small_rows, path, aligned, f32acc) <
  294. std::tie(b.HSK, b.HSV, b.small_rows, b.path, b.aligned, b.f32acc);
  295. }
  296. };
  297. enum shader_reduction_mode {
  298. SHADER_REDUCTION_MODE_SHMEM,
  299. SHADER_REDUCTION_MODE_HYBRID,
  300. SHADER_REDUCTION_MODE_SUBGROUP,
  301. SHADER_REDUCTION_MODE_COUNT,
  302. };
  303. static constexpr uint32_t num_argsort_pipelines = 11;
  304. static constexpr uint32_t max_argsort_cols = 1 << (num_argsort_pipelines-1);
  305. struct vk_device_struct {
  306. std::recursive_mutex mutex;
  307. vk::PhysicalDevice physical_device;
  308. vk::PhysicalDeviceProperties properties;
  309. std::string name;
  310. uint64_t max_memory_allocation_size;
  311. uint64_t suballocation_block_size;
  312. bool fp16;
  313. bool bf16;
  314. bool pipeline_robustness;
  315. vk::Device device;
  316. uint32_t vendor_id;
  317. vk::DriverId driver_id;
  318. vk_device_architecture architecture;
  319. vk_queue compute_queue;
  320. vk_queue transfer_queue;
  321. bool single_queue;
  322. uint32_t subgroup_size;
  323. uint32_t shader_core_count;
  324. bool uma;
  325. bool prefer_host_memory;
  326. bool float_controls_rte_fp16;
  327. bool subgroup_arithmetic;
  328. bool subgroup_shuffle;
  329. bool subgroup_ballot;
  330. bool subgroup_clustered;
  331. bool multi_add;
  332. bool add_rms_fusion;
  333. uint32_t partials_binding_alignment;
  334. bool integer_dot_product;
  335. // 0: default, 1: force mmvq, -1: disable mmvq
  336. int32_t mmvq_mode;
  337. bool subgroup_size_control;
  338. uint32_t subgroup_min_size;
  339. uint32_t subgroup_max_size;
  340. bool subgroup_require_full_support;
  341. bool coopmat_support;
  342. bool coopmat_acc_f32_support {};
  343. bool coopmat_acc_f16_support {};
  344. bool coopmat_bf16_support {};
  345. bool coopmat_support_16x16x16_f16acc {};
  346. bool coopmat_support_16x16x16_f32acc {};
  347. bool coopmat1_fa_support {};
  348. uint32_t coopmat_m;
  349. uint32_t coopmat_n;
  350. uint32_t coopmat_k;
  351. bool coopmat_int_support;
  352. uint32_t coopmat_int_m;
  353. uint32_t coopmat_int_n;
  354. uint32_t coopmat_int_k;
  355. bool coopmat2;
  356. size_t idx;
  357. bool mul_mat_l[GGML_TYPE_COUNT];
  358. bool mul_mat_m[GGML_TYPE_COUNT];
  359. bool mul_mat_s[GGML_TYPE_COUNT];
  360. bool mul_mat_id_l[GGML_TYPE_COUNT];
  361. bool mul_mat_id_m[GGML_TYPE_COUNT];
  362. bool mul_mat_id_s[GGML_TYPE_COUNT];
  363. // set to true to indicate that some shaders need to be compiled after the dryrun
  364. bool need_compiles {};
  365. vk::DescriptorSetLayout dsl;
  366. vk_matmul_pipeline pipeline_matmul_f32 {};
  367. vk_matmul_pipeline pipeline_matmul_f32_f16 {};
  368. vk_matmul_pipeline pipeline_matmul_bf16 {};
  369. vk_matmul_pipeline2 pipeline_matmul_f16;
  370. vk_matmul_pipeline2 pipeline_matmul_f16_f32;
  371. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat[GGML_TYPE_COUNT];
  372. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_COUNT];
  373. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_COUNT];
  374. vk_matmul_pipeline pipeline_matmul_id_f32 {};
  375. vk_matmul_pipeline pipeline_matmul_id_bf16 {};
  376. vk_matmul_pipeline2 pipeline_matmul_id_f16;
  377. vk_matmul_pipeline2 pipeline_matmul_id_f16_f32;
  378. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat_id[GGML_TYPE_COUNT];
  379. vk_pipeline pipeline_matmul_split_k_reduce;
  380. vk_pipeline pipeline_quantize_q8_1;
  381. vk_pipeline pipeline_quantize_q8_1_x4;
  382. vk_pipeline pipeline_dequant[GGML_TYPE_COUNT];
  383. vk_pipeline pipeline_dequant_mul_mat_vec_f32_f32[DMMV_WG_SIZE_COUNT][GGML_TYPE_COUNT][mul_mat_vec_max_cols];
  384. vk_pipeline pipeline_dequant_mul_mat_vec_f16_f32[DMMV_WG_SIZE_COUNT][GGML_TYPE_COUNT][mul_mat_vec_max_cols];
  385. vk_pipeline pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_COUNT];
  386. vk_pipeline pipeline_dequant_mul_mat_vec_q8_1_f32[DMMV_WG_SIZE_COUNT][GGML_TYPE_COUNT][mul_mat_vec_max_cols];
  387. vk_pipeline pipeline_mul_mat_vec_p021_f16_f32[p021_max_gqa_ratio];
  388. vk_pipeline pipeline_mul_mat_vec_nc_f16_f32;
  389. vk_pipeline pipeline_get_rows[GGML_TYPE_COUNT];
  390. vk_pipeline pipeline_get_rows_f32[GGML_TYPE_COUNT];
  391. vk_pipeline pipeline_acc_f32;
  392. // [src0 0=fp32,1=fp16][src1 0=fp32,1=fp16][dst 0=fp32,1=fp16]
  393. vk_pipeline pipeline_add[2][2][2];
  394. vk_pipeline pipeline_add_norepeat[2][2][2];
  395. vk_pipeline pipeline_sub[2][2][2];
  396. vk_pipeline pipeline_sub_norepeat[2][2][2];
  397. vk_pipeline pipeline_mul[2][2][2];
  398. vk_pipeline pipeline_mul_norepeat[2][2][2];
  399. vk_pipeline pipeline_div[2][2][2];
  400. vk_pipeline pipeline_div_norepeat[2][2][2];
  401. vk_pipeline pipeline_add_rms[2][2][2];
  402. vk_pipeline pipeline_add_rms_norepeat[2][2][2];
  403. // indexed by num_additional_fused_ops == num_adds - 1
  404. vk_pipeline pipeline_multi_add[MAX_FUSED_ADDS];
  405. vk_pipeline pipeline_multi_add_rms[MAX_FUSED_ADDS];
  406. vk_pipeline pipeline_add_id_f32;
  407. vk_pipeline pipeline_concat_f32, pipeline_concat_f16, pipeline_concat_i32;
  408. vk_pipeline pipeline_upscale_nearest_f32, pipeline_upscale_bilinear_f32, pipeline_upscale_bilinear_ac_f32;
  409. vk_pipeline pipeline_scale_f32;
  410. vk_pipeline pipeline_sqr_f32;
  411. vk_pipeline pipeline_sqrt_f32;
  412. vk_pipeline pipeline_sin_f32;
  413. vk_pipeline pipeline_cos_f32;
  414. vk_pipeline pipeline_clamp_f32;
  415. vk_pipeline pipeline_pad_f32;
  416. vk_pipeline pipeline_roll_f32;
  417. vk_pipeline pipeline_repeat_f32, pipeline_repeat_back_f32;
  418. vk_pipeline pipeline_cpy_f32_f32, pipeline_cpy_f32_f16, pipeline_cpy_f16_f16, pipeline_cpy_f16_f32, pipeline_cpy_f32_bf16;
  419. vk_pipeline pipeline_contig_cpy_f32_f32, pipeline_contig_cpy_f32_f16, pipeline_contig_cpy_f16_f16, pipeline_contig_cpy_f16_f32, pipeline_contig_cpy_f32_bf16;
  420. vk_pipeline pipeline_cpy_f32_quant[GGML_TYPE_COUNT];
  421. vk_pipeline pipeline_cpy_quant_f32[GGML_TYPE_COUNT];
  422. vk_pipeline pipeline_set_rows[GGML_TYPE_COUNT];
  423. vk_pipeline pipeline_norm_f32;
  424. vk_pipeline pipeline_group_norm_f32;
  425. vk_pipeline pipeline_rms_norm_f32;
  426. vk_pipeline pipeline_rms_norm_mul_f32;
  427. vk_pipeline pipeline_rms_norm_partials_f32;
  428. vk_pipeline pipeline_rms_norm_mul_partials_f32;
  429. vk_pipeline pipeline_rms_norm_back_f32;
  430. vk_pipeline pipeline_l2_norm_f32;
  431. // [src/dst 0=fp32,1=fp16]
  432. vk_pipeline pipeline_exp[2];
  433. vk_pipeline pipeline_gelu[2];
  434. vk_pipeline pipeline_gelu_erf[2];
  435. vk_pipeline pipeline_gelu_quick[2];
  436. vk_pipeline pipeline_silu[2];
  437. vk_pipeline pipeline_relu[2];
  438. vk_pipeline pipeline_tanh[2];
  439. vk_pipeline pipeline_sigmoid[2];
  440. vk_pipeline pipeline_hardsigmoid[2];
  441. vk_pipeline pipeline_hardswish[2];
  442. vk_pipeline pipeline_geglu[2];
  443. vk_pipeline pipeline_reglu[2];
  444. vk_pipeline pipeline_swiglu[2];
  445. vk_pipeline pipeline_swiglu_oai[2];
  446. vk_pipeline pipeline_geglu_erf[2];
  447. vk_pipeline pipeline_geglu_quick[2];
  448. vk_pipeline pipeline_leaky_relu_f32;
  449. vk_pipeline pipeline_silu_back_f32;
  450. vk_pipeline pipeline_diag_mask_inf_f32;
  451. vk_pipeline pipeline_soft_max_f32, pipeline_soft_max_f32_f16;
  452. vk_pipeline pipeline_soft_max_f32_wg512, pipeline_soft_max_f32_f16_wg512;
  453. vk_pipeline pipeline_soft_max_back_f32;
  454. vk_pipeline pipeline_rope_norm_f32, pipeline_rope_norm_f16;
  455. vk_pipeline pipeline_rope_neox_f32, pipeline_rope_neox_f16;
  456. vk_pipeline pipeline_rope_multi_f32, pipeline_rope_multi_f16;
  457. vk_pipeline pipeline_rope_vision_f32, pipeline_rope_vision_f16;
  458. vk_pipeline pipeline_argsort_f32[num_argsort_pipelines];
  459. vk_pipeline pipeline_sum_rows_f32;
  460. vk_pipeline pipeline_argmax_f32;
  461. vk_pipeline pipeline_count_equal_i32;
  462. vk_pipeline pipeline_im2col_f32, pipeline_im2col_f32_f16;
  463. vk_pipeline pipeline_timestep_embedding_f32;
  464. vk_pipeline pipeline_conv_transpose_1d_f32;
  465. vk_pipeline pipeline_pool2d_f32;
  466. vk_pipeline pipeline_rwkv_wkv6_f32;
  467. vk_pipeline pipeline_rwkv_wkv7_f32;
  468. vk_pipeline pipeline_opt_step_adamw_f32;
  469. vk_pipeline pipeline_opt_step_sgd_f32;
  470. vk_pipeline pipeline_conv2d_f32[CONV_SHAPE_COUNT];
  471. vk_pipeline pipeline_conv2d_f16_f32[CONV_SHAPE_COUNT];
  472. vk_pipeline pipeline_conv2d_dw_whcn_f32, pipeline_conv2d_dw_whcn_f16_f32;
  473. vk_pipeline pipeline_conv2d_dw_cwhn_f32, pipeline_conv2d_dw_cwhn_f16_f32;
  474. std::map<vk_fa_pipeline_state, vk_pipeline> pipeline_flash_attn_f32_f16[GGML_TYPE_COUNT];
  475. vk_pipeline pipeline_flash_attn_split_k_reduce;
  476. std::vector<vk_pipeline_ref> all_pipelines;
  477. std::vector<std::tuple<void*, size_t, vk_buffer>> pinned_memory;
  478. vk::Fence fence;
  479. vk_buffer sync_staging;
  480. ggml_backend_buffer_type buffer_type;
  481. bool disable_fusion;
  482. bool disable_host_visible_vidmem;
  483. bool allow_sysmem_fallback;
  484. #ifdef GGML_VULKAN_MEMORY_DEBUG
  485. std::unique_ptr<vk_memory_logger> memory_logger;
  486. #endif
  487. // for GGML_VK_PERF_LOGGER
  488. std::unique_ptr<vk_perf_logger> perf_logger;
  489. vk::QueryPool query_pool;
  490. int32_t num_queries;
  491. ~vk_device_struct() {
  492. VK_LOG_DEBUG("destroy device " << name);
  493. device.destroyFence(fence);
  494. ggml_vk_destroy_buffer(sync_staging);
  495. compute_queue.cmd_pool.destroy(device);
  496. transfer_queue.cmd_pool.destroy(device);
  497. for (auto& pipeline : all_pipelines) {
  498. if (pipeline.expired()) {
  499. continue;
  500. }
  501. vk_pipeline pl = pipeline.lock();
  502. ggml_vk_destroy_pipeline(device, pl);
  503. }
  504. all_pipelines.clear();
  505. device.destroyDescriptorSetLayout(dsl);
  506. device.destroy();
  507. }
  508. };
  509. void vk_command_pool::init(vk_device& device, vk_queue *q_) {
  510. cmd_buffer_idx = 0;
  511. q = q_;
  512. vk::CommandPoolCreateInfo command_pool_create_info(vk::CommandPoolCreateFlags(VK_COMMAND_POOL_CREATE_TRANSIENT_BIT), q->queue_family_index);
  513. pool = device->device.createCommandPool(command_pool_create_info);
  514. }
  515. void vk_command_pool::destroy(vk::Device& device) {
  516. device.destroyCommandPool(pool);
  517. pool = nullptr;
  518. cmd_buffers.clear();
  519. }
  520. struct vk_buffer_struct {
  521. vk::Buffer buffer = VK_NULL_HANDLE;
  522. vk::DeviceMemory device_memory = VK_NULL_HANDLE;
  523. vk::MemoryPropertyFlags memory_property_flags;
  524. void * ptr;
  525. size_t size = 0;
  526. vk_device device;
  527. ~vk_buffer_struct() {
  528. if (size == 0) {
  529. return;
  530. }
  531. VK_LOG_DEBUG("~vk_buffer_struct(" << buffer << ", " << size << ")");
  532. device->device.freeMemory(device_memory);
  533. device->device.destroyBuffer(buffer);
  534. }
  535. };
  536. struct vk_subbuffer {
  537. vk_buffer buffer;
  538. uint64_t offset;
  539. uint64_t size;
  540. operator vk::DescriptorBufferInfo() const {
  541. return { buffer->buffer, offset, size };
  542. }
  543. };
  544. struct vk_semaphore {
  545. vk::Semaphore s;
  546. uint64_t value;
  547. };
  548. struct vk_submission {
  549. vk::CommandBuffer buffer;
  550. std::vector<vk_semaphore> wait_semaphores;
  551. std::vector<vk_semaphore> signal_semaphores;
  552. };
  553. typedef std::vector<vk_submission> vk_sequence;
  554. struct vk_mat_mat_push_constants {
  555. uint32_t M; uint32_t N; uint32_t K;
  556. uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  557. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  558. uint32_t k_split;
  559. uint32_t ne02; uint32_t ne12; uint32_t broadcast2; uint32_t broadcast3;
  560. uint32_t padded_N;
  561. };
  562. struct vk_mat_vec_push_constants {
  563. uint32_t ncols; uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  564. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  565. uint32_t ne02; uint32_t ne12; uint32_t broadcast2; uint32_t broadcast3;
  566. };
  567. struct vk_mat_mat_id_push_constants {
  568. uint32_t M; uint32_t N; uint32_t K;
  569. uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  570. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  571. uint32_t nei0; uint32_t nei1; uint32_t nbi1; uint32_t ne11;
  572. uint32_t padded_N;
  573. };
  574. struct vk_mat_vec_id_push_constants {
  575. uint32_t ncols; uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  576. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  577. uint32_t nei0; uint32_t ne11;
  578. };
  579. struct vk_flash_attn_push_constants {
  580. uint32_t N;
  581. uint32_t KV;
  582. uint32_t ne1;
  583. uint32_t ne2;
  584. uint32_t ne3;
  585. uint32_t neq2;
  586. uint32_t neq3;
  587. uint32_t nek2;
  588. uint32_t nek3;
  589. uint32_t nev2;
  590. uint32_t nev3;
  591. uint32_t nem1;
  592. uint32_t nem2;
  593. uint32_t nem3;
  594. uint32_t nb01;
  595. uint32_t nb02;
  596. uint32_t nb03;
  597. uint32_t nb11;
  598. uint32_t nb12;
  599. uint32_t nb13;
  600. uint32_t nb21;
  601. uint32_t nb22;
  602. uint32_t nb23;
  603. float scale;
  604. float max_bias;
  605. float logit_softcap;
  606. uint32_t mask_n_head_log2;
  607. float m0;
  608. float m1;
  609. uint32_t gqa_ratio;
  610. uint32_t split_kv;
  611. uint32_t k_num;
  612. };
  613. static_assert(sizeof(vk_flash_attn_push_constants) <= 128, "sizeof(vk_flash_attn_push_constants) must be <= 128");
  614. struct vk_op_push_constants {
  615. uint32_t KX;
  616. uint32_t KY;
  617. float param1;
  618. float param2;
  619. };
  620. struct vk_op_glu_push_constants {
  621. uint32_t N;
  622. uint32_t ne00;
  623. uint32_t ne20;
  624. uint32_t mode; // 0: default, 1: swapped, 2: split
  625. float alpha; // for swiglu_oai
  626. float limit;
  627. };
  628. struct vk_op_unary_push_constants {
  629. uint32_t ne;
  630. uint32_t ne00; uint32_t ne01; uint32_t ne02; uint32_t ne03; uint32_t nb00; uint32_t nb01; uint32_t nb02; uint32_t nb03;
  631. uint32_t ne10; uint32_t ne11; uint32_t ne12; uint32_t ne13; uint32_t nb10; uint32_t nb11; uint32_t nb12; uint32_t nb13;
  632. uint32_t misalign_offsets;
  633. float param1; float param2;
  634. uint32_t ne0_012mp; uint32_t ne0_012L;
  635. uint32_t ne0_01mp; uint32_t ne0_01L;
  636. uint32_t ne0_0mp; uint32_t ne0_0L;
  637. uint32_t ne1_012mp; uint32_t ne1_012L;
  638. uint32_t ne1_01mp; uint32_t ne1_01L;
  639. uint32_t ne1_0mp; uint32_t ne1_0L;
  640. };
  641. static_assert(sizeof(vk_op_unary_push_constants) <= 128, "sizeof(vk_op_unary_push_constants) must be <= 128");
  642. static vk_op_unary_push_constants vk_op_unary_push_constants_init(const ggml_tensor * src0, const ggml_tensor * dst, int64_t ne = 0) {
  643. GGML_ASSERT(ne != 0 || (ggml_nelements(src0) == ggml_nelements(dst)));
  644. ne = ne != 0 ? ne : ggml_nelements(dst);
  645. GGML_ASSERT(ne <= (int64_t)std::numeric_limits<uint32_t>::max());
  646. vk_op_unary_push_constants p{};
  647. p.ne = (uint32_t)ne;
  648. size_t src0_tsize = ggml_type_size(src0->type);
  649. p.ne00 = (uint32_t)src0->ne[0];
  650. p.ne01 = (uint32_t)src0->ne[1];
  651. p.ne02 = (uint32_t)src0->ne[2];
  652. p.ne03 = (uint32_t)src0->ne[3];
  653. p.nb00 = (uint32_t)(src0->nb[0] / src0_tsize);
  654. p.nb01 = (uint32_t)(src0->nb[1] / src0_tsize);
  655. p.nb02 = (uint32_t)(src0->nb[2] / src0_tsize);
  656. p.nb03 = (uint32_t)(src0->nb[3] / src0_tsize);
  657. size_t dst_tsize = ggml_type_size(dst->type);
  658. p.ne10 = (uint32_t)dst->ne[0];
  659. p.ne11 = (uint32_t)dst->ne[1];
  660. p.ne12 = (uint32_t)dst->ne[2];
  661. p.ne13 = (uint32_t)dst->ne[3];
  662. p.nb10 = (uint32_t)(dst->nb[0] / dst_tsize);
  663. p.nb11 = (uint32_t)(dst->nb[1] / dst_tsize);
  664. p.nb12 = (uint32_t)(dst->nb[2] / dst_tsize);
  665. p.nb13 = (uint32_t)(dst->nb[3] / dst_tsize);
  666. return p; // fastdiv values and offsets are initialized later in ggml_vk_op
  667. }
  668. // See https://gmplib.org/~tege/divcnst-pldi94.pdf figure 4.1.
  669. // Precompute mp (m' in the paper) and L such that division
  670. // can be computed using a multiply (high 32b of 64b result)
  671. // and a shift:
  672. //
  673. // n/d = (mulhi(n, mp) + n) >> L;
  674. static void init_fastdiv_values(uint32_t d, uint32_t &mp, uint32_t &L)
  675. {
  676. // compute L = ceil(log2(d));
  677. L = 0;
  678. while (L < 32 && (uint32_t{1} << L) < d) {
  679. L++;
  680. }
  681. mp = (uint32_t)((uint64_t{1} << 32) * ((uint64_t{1} << L) - d) / d + 1);
  682. }
  683. template <typename T> void init_pushconst_fastdiv(T &p) {
  684. GGML_UNUSED(p);
  685. static_assert(!std::is_const<T>::value, "unexpected type");
  686. }
  687. template <> void init_pushconst_fastdiv(vk_op_unary_push_constants &p) {
  688. // Compute magic values to divide by these six numbers.
  689. init_fastdiv_values(p.ne02*p.ne01*p.ne00, p.ne0_012mp, p.ne0_012L);
  690. init_fastdiv_values(p.ne01*p.ne00, p.ne0_01mp, p.ne0_01L);
  691. init_fastdiv_values(p.ne00, p.ne0_0mp, p.ne0_0L);
  692. init_fastdiv_values(p.ne12*p.ne11*p.ne10, p.ne1_012mp, p.ne1_012L);
  693. init_fastdiv_values(p.ne11*p.ne10, p.ne1_01mp, p.ne1_01L);
  694. init_fastdiv_values(p.ne10, p.ne1_0mp, p.ne1_0L);
  695. }
  696. struct vk_op_binary_push_constants {
  697. uint32_t ne;
  698. uint32_t ne00; uint32_t ne01; uint32_t ne02; uint32_t ne03; uint32_t nb00; uint32_t nb01; uint32_t nb02; uint32_t nb03;
  699. uint32_t ne10; uint32_t ne11; uint32_t ne12; uint32_t ne13; uint32_t nb10; uint32_t nb11; uint32_t nb12; uint32_t nb13;
  700. uint32_t ne20; uint32_t ne21; uint32_t ne22; uint32_t ne23; uint32_t nb20; uint32_t nb21; uint32_t nb22; uint32_t nb23;
  701. uint32_t misalign_offsets;
  702. float param1; float param2; int32_t param3;
  703. };
  704. struct vk_op_multi_add_push_constants {
  705. // shape for dst
  706. uint32_t ne20; uint32_t ne21; uint32_t ne22; uint32_t ne23;
  707. // strides for srcs+dst
  708. uint32_t nb[MAX_PARAMETER_COUNT][4];
  709. uint32_t rms_partials;
  710. };
  711. // update multi_add.comp if this changes
  712. static_assert(MAX_PARAMETER_COUNT == 12);
  713. static_assert(sizeof(vk_op_multi_add_push_constants) <= 256);
  714. struct vk_op_add_id_push_constants {
  715. uint32_t ne0;
  716. uint32_t ne1;
  717. uint32_t s01;
  718. uint32_t s02;
  719. uint32_t s11;
  720. uint32_t s21;
  721. };
  722. struct vk_op_diag_mask_push_constants {
  723. uint32_t ncols;
  724. uint32_t rows_per_channel;
  725. int32_t n_past;
  726. };
  727. struct vk_op_rope_push_constants {
  728. uint32_t ncols;
  729. uint32_t n_dims;
  730. float freq_scale;
  731. uint32_t p_delta_rows;
  732. float freq_base;
  733. float ext_factor;
  734. float attn_factor;
  735. float corr_dims[2];
  736. float theta_scale;
  737. uint32_t has_ff;
  738. uint32_t ne02;
  739. uint32_t s1;
  740. uint32_t s2;
  741. int32_t sections[4];
  742. uint32_t is_back;
  743. };
  744. struct vk_op_soft_max_push_constants {
  745. uint32_t KX;
  746. uint32_t KY;
  747. uint32_t ne00;
  748. uint32_t ne01;
  749. uint32_t ne02;
  750. uint32_t ne12;
  751. uint32_t ne13;
  752. uint32_t nb11;
  753. uint32_t nb12;
  754. uint32_t nb13;
  755. float scale;
  756. float max_bias;
  757. float m0;
  758. float m1;
  759. uint32_t n_head_log2;
  760. uint32_t nrows_x;
  761. uint32_t has_sinks;
  762. };
  763. struct vk_op_argsort_push_constants {
  764. uint32_t ncols;
  765. int32_t order;
  766. };
  767. struct vk_op_im2col_push_constants {
  768. uint32_t batch_offset; uint32_t offset_delta;
  769. uint32_t IC;
  770. uint32_t IW; uint32_t IH;
  771. uint32_t OW; uint32_t OH;
  772. uint32_t KW; uint32_t KH;
  773. uint32_t pelements;
  774. uint32_t CHW;
  775. int32_t s0; int32_t s1;
  776. int32_t p0; int32_t p1;
  777. int32_t d0; int32_t d1;
  778. };
  779. struct vk_op_timestep_embedding_push_constants {
  780. uint32_t nb1;
  781. uint32_t dim;
  782. uint32_t max_period;
  783. };
  784. struct vk_op_conv_transpose_1d_push_constants {
  785. uint32_t Cout;
  786. uint32_t Cin;
  787. uint32_t K;
  788. uint32_t L;
  789. uint32_t KL;
  790. uint32_t nb01;
  791. uint32_t nb02;
  792. uint32_t nb11;
  793. uint32_t nb1;
  794. int32_t s0;
  795. };
  796. struct vk_op_pool2d_push_constants {
  797. uint32_t IW; uint32_t IH;
  798. uint32_t OW; uint32_t OH;
  799. uint32_t OC;
  800. uint32_t pelements;
  801. uint32_t op;
  802. int32_t k0; int32_t k1;
  803. int32_t s0; int32_t s1;
  804. int32_t p0; int32_t p1;
  805. };
  806. struct vk_op_rwkv_wkv6_push_constants {
  807. uint32_t B;
  808. uint32_t T;
  809. uint32_t C;
  810. uint32_t H;
  811. };
  812. struct vk_op_rwkv_wkv7_push_constants {
  813. uint32_t B;
  814. uint32_t T;
  815. uint32_t C;
  816. uint32_t H;
  817. };
  818. struct vk_op_conv2d_push_constants {
  819. uint32_t Cout;
  820. uint32_t Cin;
  821. uint32_t N;
  822. uint32_t KW;
  823. uint32_t KH;
  824. uint32_t W;
  825. uint32_t H;
  826. uint32_t OW;
  827. uint32_t OH;
  828. uint32_t s0;
  829. uint32_t s1;
  830. uint32_t p0;
  831. uint32_t p1;
  832. uint32_t d0;
  833. uint32_t d1;
  834. uint32_t nb01;
  835. uint32_t nb02;
  836. uint32_t nb03;
  837. uint32_t nb11;
  838. uint32_t nb12;
  839. uint32_t nb13;
  840. uint32_t nb1;
  841. uint32_t nb2;
  842. uint32_t nb3;
  843. // init_fastdiv_values constants for dividing by KW, KW*KH, OW, OW*OH
  844. uint32_t KWmp; uint32_t KWL;
  845. uint32_t KWKHmp; uint32_t KWKHL;
  846. uint32_t OWmp; uint32_t OWL;
  847. uint32_t OWOHmp; uint32_t OWOHL;
  848. };
  849. template <> void init_pushconst_fastdiv(vk_op_conv2d_push_constants &p) {
  850. // Compute magic values to divide by KW, KW*KH, OW, OW*OH
  851. init_fastdiv_values(p.KW, p.KWmp, p.KWL);
  852. init_fastdiv_values(p.KW*p.KH, p.KWKHmp, p.KWKHL);
  853. init_fastdiv_values(p.OW, p.OWmp, p.OWL);
  854. init_fastdiv_values(p.OW*p.OH, p.OWOHmp, p.OWOHL);
  855. }
  856. struct vk_op_conv2d_dw_push_constants {
  857. uint32_t ne;
  858. uint32_t batches;
  859. uint32_t channels;
  860. uint32_t dst_w;
  861. uint32_t dst_h;
  862. uint32_t src_w;
  863. uint32_t src_h;
  864. uint32_t knl_w;
  865. uint32_t knl_h;
  866. int32_t stride_x;
  867. int32_t stride_y;
  868. int32_t pad_x;
  869. int32_t pad_y;
  870. int32_t dilation_x;
  871. int32_t dilation_y;
  872. };
  873. struct vk_op_upscale_push_constants {
  874. uint32_t ne; uint32_t a_offset; uint32_t d_offset;
  875. uint32_t ne00; uint32_t ne01;
  876. uint32_t nb00; uint32_t nb01; uint32_t nb02; uint32_t nb03;
  877. uint32_t ne10; uint32_t ne11; uint32_t ne12; uint32_t ne13;
  878. float sf0; float sf1; float sf2; float sf3;
  879. };
  880. struct vk_op_sum_rows_push_constants
  881. {
  882. uint32_t n_cols;
  883. uint32_t ne01, ne02;
  884. uint32_t nb01, nb02, nb03;
  885. uint32_t nb11, nb12, nb13;
  886. float weight;
  887. uint32_t misalign_offsets;
  888. uint32_t ne0_12mp, ne0_12L;
  889. uint32_t ne0_1mp, ne0_1L;
  890. };
  891. static vk_op_sum_rows_push_constants vk_op_sum_rows_push_constants_init(const ggml_tensor * src, const ggml_tensor * dst, int64_t n_cols) {
  892. uint32_t type_size = (uint32_t)ggml_type_size(src->type);
  893. vk_op_sum_rows_push_constants p = {};
  894. p.n_cols = (uint32_t)n_cols;
  895. p.ne01 = (uint32_t)src->ne[1];
  896. p.ne02 = (uint32_t)src->ne[2];
  897. p.nb01 = (uint32_t)src->nb[1] / type_size;
  898. p.nb02 = (uint32_t)src->nb[2] / type_size;
  899. p.nb03 = (uint32_t)src->nb[3] / type_size;
  900. p.nb11 = (uint32_t)dst->nb[1] / type_size;
  901. p.nb12 = (uint32_t)dst->nb[2] / type_size;
  902. p.nb13 = (uint32_t)dst->nb[3] / type_size;
  903. p.weight = 1.0f;
  904. return p;
  905. }
  906. template <> void init_pushconst_fastdiv(vk_op_sum_rows_push_constants &p) {
  907. init_fastdiv_values(p.ne01*p.ne02, p.ne0_12mp, p.ne0_12L);
  908. init_fastdiv_values(p.ne01, p.ne0_1mp, p.ne0_1L);
  909. }
  910. // Allow pre-recording command buffers
  911. struct vk_staging_memcpy {
  912. vk_staging_memcpy(void * _dst, const void * _src, size_t _n) : dst(_dst), src(_src), n(_n) {}
  913. void * dst;
  914. const void * src;
  915. size_t n;
  916. };
  917. struct vk_context_struct {
  918. vk_submission * s;
  919. std::vector<vk_sequence> seqs;
  920. int exit_tensor_idx;
  921. std::vector<vk_staging_memcpy> in_memcpys;
  922. std::vector<vk_staging_memcpy> out_memcpys;
  923. vk_command_pool * p {};
  924. };
  925. typedef std::shared_ptr<vk_context_struct> vk_context;
  926. typedef std::weak_ptr<vk_context_struct> vk_context_ref;
  927. struct ggml_vk_garbage_collector {
  928. std::vector<vk_semaphore> tl_semaphores;
  929. std::vector<vk_semaphore> semaphores;
  930. std::vector<vk::Event> events;
  931. std::vector<vk_buffer> temp_buffers;
  932. std::vector<vk_context> contexts;
  933. };
  934. #if defined(GGML_VULKAN_MEMORY_DEBUG) || defined(GGML_VULKAN_DEBUG)
  935. #define VK_LOG_MEMORY(msg) std::cerr << "ggml_vulkan memory: " << msg << std::endl
  936. static std::string format_size(size_t size) {
  937. const size_t kib = 1024;
  938. const size_t mib = kib * 1024;
  939. const size_t gib = mib * 1024;
  940. std::ostringstream oss;
  941. oss << std::fixed << std::setprecision(2);
  942. if (size >= gib) {
  943. oss << static_cast<double>(size) / gib << " GiB";
  944. } else if (size >= mib) {
  945. oss << static_cast<double>(size) / mib << " MiB";
  946. } else if (size >= kib) {
  947. oss << static_cast<double>(size) / kib << " KiB";
  948. } else {
  949. oss << size << " B";
  950. }
  951. return oss.str();
  952. }
  953. static std::mutex log_mutex;
  954. class vk_memory_logger {
  955. public:
  956. vk_memory_logger(): total_device(0), total_host(0) {}
  957. void log_allocation(vk_buffer_ref buf_ref, size_t size);
  958. void log_deallocation(vk_buffer_ref buf_ref);
  959. private:
  960. std::map<vk::Buffer, size_t> allocations; // Track allocations
  961. size_t total_device;
  962. size_t total_host;
  963. };
  964. #else
  965. #define VK_LOG_MEMORY(msg) ((void) 0)
  966. #endif // GGML_VULKAN_MEMORY_DEBUG
  967. class vk_perf_logger {
  968. public:
  969. void print_timings() {
  970. if (timings.empty()) {
  971. return;
  972. }
  973. uint64_t total_all_op_times = 0;
  974. std::cerr << "----------------\nVulkan Timings:" << std::endl;
  975. for (const auto & t : timings) {
  976. uint64_t total_op_times = 0;
  977. for (const auto & time : t.second) {
  978. total_op_times += time;
  979. }
  980. std::cerr << t.first << ": " << t.second.size() << " x " << (total_op_times / t.second.size() / 1000.0)
  981. << " us";
  982. // If we have as many flops entries as timing entries for the op, then compute and log the flops/S.
  983. auto it = flops.find(t.first);
  984. if (it != flops.end() && (it->second).size() == t.second.size()) {
  985. uint64_t total_op_flops = 0;
  986. for (const auto & elem : it->second) {
  987. total_op_flops += elem;
  988. }
  989. std::cerr << " ("
  990. << (double(total_op_flops) / (1000.0 * 1000.0 * 1000.0)) /
  991. (double(total_op_times) / (1000.0 * 1000.0 * 1000.0))
  992. << " GFLOPS/s)";
  993. }
  994. total_all_op_times += total_op_times;
  995. std::cerr << std::endl;
  996. }
  997. if (timings.size() > 0) {
  998. std::cerr << "Total time: " << total_all_op_times / 1000.0 << " us." << std::endl;
  999. }
  1000. timings.clear();
  1001. flops.clear();
  1002. }
  1003. void log_timing(const ggml_tensor * node, uint64_t time) {
  1004. if (node->op == GGML_OP_UNARY) {
  1005. timings[ggml_unary_op_name(ggml_get_unary_op(node))].push_back(time);
  1006. return;
  1007. }
  1008. if (node->op == GGML_OP_MUL_MAT || node->op == GGML_OP_MUL_MAT_ID) {
  1009. const uint64_t m = node->src[0]->ne[1];
  1010. const uint64_t n = node->ne[1];
  1011. const uint64_t k = node->src[1]->ne[0];
  1012. const uint64_t batch = node->src[1]->ne[2] * node->src[1]->ne[3];
  1013. std::string name = ggml_op_name(node->op);
  1014. if ((node->op == GGML_OP_MUL_MAT && n <= mul_mat_vec_max_cols) ||
  1015. (node->op == GGML_OP_MUL_MAT_ID && node->src[2]->ne[1] == 1)) {
  1016. name += "_VEC";
  1017. }
  1018. name += " ";
  1019. name += ggml_type_name(node->src[0]->type);
  1020. name += " m=" + std::to_string(m) + " n=" + std::to_string(n) + " k=" + std::to_string(k);
  1021. if (batch > 1) {
  1022. name += " batch=" + std::to_string(batch);
  1023. }
  1024. timings[name].push_back(time);
  1025. flops[name].push_back(m * n * (k + (k - 1)) * batch);
  1026. return;
  1027. }
  1028. if (node->op == GGML_OP_CONV_2D) {
  1029. std::string name = ggml_op_name(node->op);
  1030. ggml_tensor * knl = node->src[0];
  1031. uint64_t OW = node->ne[0];
  1032. uint64_t OH = node->ne[1];
  1033. uint64_t N = node->ne[3];
  1034. uint64_t Cout = node->ne[2];
  1035. uint64_t KW = knl->ne[0];
  1036. uint64_t KH = knl->ne[1];
  1037. uint64_t Cin = knl->ne[2];
  1038. // KxCRS @ CRSxNPQ = KxNPQ -> M=K, K=CRS, N=NPQ
  1039. uint64_t size_M = Cout;
  1040. uint64_t size_K = Cin * KW * KH;
  1041. uint64_t size_N = N * OW * OH;
  1042. uint64_t n_flops = size_M * size_N * (size_K + (size_K - 1));
  1043. name += " M=Cout=" + std::to_string(size_M) + ", K=Cin*KW*KH=" + std::to_string(size_K) +
  1044. ", N=N*OW*OH=" + std::to_string(size_N);
  1045. flops[name].push_back(n_flops);
  1046. timings[name].push_back(time);
  1047. return;
  1048. }
  1049. if (node->op == GGML_OP_RMS_NORM) {
  1050. std::string name = ggml_op_name(node->op);
  1051. name += "(" + std::to_string(node->ne[0]) + "," + std::to_string(node->ne[1]) + "," + std::to_string(node->ne[2]) + "," + std::to_string(node->ne[3]) + ")";
  1052. timings[name].push_back(time);
  1053. return;
  1054. }
  1055. timings[ggml_op_name(node->op)].push_back(time);
  1056. }
  1057. private:
  1058. std::map<std::string, std::vector<uint64_t>> timings;
  1059. std::map<std::string, std::vector<uint64_t>> flops;
  1060. };
  1061. struct ggml_backend_vk_context {
  1062. std::string name;
  1063. vk_device device;
  1064. size_t semaphore_idx, event_idx;
  1065. ggml_vk_garbage_collector gc;
  1066. size_t prealloc_size_x, prealloc_size_y, prealloc_size_split_k, prealloc_size_add_rms_partials, prealloc_size_add_rms_partials_offset;
  1067. vk_buffer prealloc_x, prealloc_y, prealloc_split_k, prealloc_add_rms_partials;
  1068. vk::Fence fence, almost_ready_fence;
  1069. bool almost_ready_fence_pending {};
  1070. // Set before op_add and unset after op_rms_norm to indicate that the add should
  1071. // write partial sums to accumulate the square of the vector components
  1072. bool do_add_rms_partials;
  1073. // Cache most recent tensor that was converted into prealloc_y, and what pipeline it used to convert.
  1074. vk_pipeline_struct * prealloc_y_last_pipeline_used {};
  1075. const ggml_tensor * prealloc_y_last_tensor_used {};
  1076. // Track which nodes have been used since the last sync, and whether they were written to
  1077. std::vector<const ggml_tensor *> unsynced_nodes_written;
  1078. std::vector<const ggml_tensor *> unsynced_nodes_read;
  1079. // Track which prealloc buffers have pending reads that need to be synchronized.
  1080. // These are checked before writing to the buffer (and call ggml_vk_sync_buffers if set),
  1081. // and set to true after the buffer contents are consumed.
  1082. bool prealloc_x_need_sync, prealloc_y_need_sync, prealloc_split_k_need_sync;
  1083. vk_buffer buffer_pool[MAX_VK_BUFFERS];
  1084. vk_context_ref compute_ctx;
  1085. vk_context_ref transfer_ctx;
  1086. std::vector<vk_context_ref> tensor_ctxs;
  1087. std::vector<vk::DescriptorPool> descriptor_pools;
  1088. std::vector<vk::DescriptorSet> descriptor_sets;
  1089. uint32_t descriptor_set_idx {};
  1090. uint32_t pipeline_descriptor_set_requirements {};
  1091. vk_command_pool compute_cmd_pool;
  1092. vk_command_pool transfer_cmd_pool;
  1093. // number of additional consecutive nodes that are being fused with the
  1094. // node currently being processed
  1095. int num_additional_fused_ops {};
  1096. };
  1097. static void * const vk_ptr_base = (void *)(uintptr_t) 0x1000; // NOLINT
  1098. static uint64_t vk_tensor_offset(const ggml_tensor * tensor) {
  1099. if (tensor->view_src) {
  1100. return (uint8_t *) tensor->view_src->data - (uint8_t *) vk_ptr_base;
  1101. }
  1102. return (uint8_t *) tensor->data - (uint8_t *) vk_ptr_base;
  1103. }
  1104. struct ggml_backend_vk_buffer_context {
  1105. vk_device_ref device;
  1106. vk_buffer dev_buffer;
  1107. std::string name;
  1108. ggml_backend_vk_buffer_context(vk_device_ref device, vk_buffer&& dev_buffer, std::string& name) :
  1109. device(device),
  1110. dev_buffer(dev_buffer),
  1111. name(name) {
  1112. }
  1113. ~ggml_backend_vk_buffer_context() {
  1114. ggml_vk_destroy_buffer(dev_buffer);
  1115. }
  1116. };
  1117. #ifdef GGML_VULKAN_MEMORY_DEBUG
  1118. void vk_memory_logger::log_allocation(vk_buffer_ref buf_ref, size_t size) {
  1119. std::lock_guard<std::mutex> guard(log_mutex);
  1120. vk_buffer buf = buf_ref.lock();
  1121. const bool device = bool(buf->memory_property_flags & vk::MemoryPropertyFlagBits::eDeviceLocal);
  1122. const std::string type = device ? "device" : "host";
  1123. allocations[buf->buffer] = size;
  1124. total_device += device ? size : 0;
  1125. total_host += device ? 0 : size;
  1126. VK_LOG_MEMORY(buf->device->name << ": +" << format_size(size) << " " << type << " at " << buf->buffer << ". Total device: " << format_size(total_device) << ", total host: " << format_size(total_host));
  1127. }
  1128. void vk_memory_logger::log_deallocation(vk_buffer_ref buf_ref) {
  1129. if (buf_ref.expired() || buf_ref.lock()->size == 0) {
  1130. return;
  1131. }
  1132. std::lock_guard<std::mutex> guard(log_mutex);
  1133. vk_buffer buf = buf_ref.lock();
  1134. const bool device = bool(buf->memory_property_flags & vk::MemoryPropertyFlagBits::eDeviceLocal);
  1135. std::string type = device ? "device" : "host";
  1136. auto it = allocations.find(buf->buffer);
  1137. total_device -= device ? it->second : 0;
  1138. total_host -= device ? 0 : it->second;
  1139. if (it != allocations.end()) {
  1140. VK_LOG_MEMORY(buf->device->name << ": -" << format_size(it->second) << " " << type << " at " << buf->buffer << ". Total device: " << format_size(total_device) << ", total host: " << format_size(total_host));
  1141. allocations.erase(it);
  1142. } else {
  1143. VK_LOG_MEMORY("ERROR " << buf->device->name << ": Attempted to deallocate unknown " << type << " memory at " << buf->buffer);
  1144. }
  1145. }
  1146. #endif // GGML_VULKAN_MEMORY_DEBUG
  1147. struct vk_instance_t {
  1148. vk::Instance instance;
  1149. bool debug_utils_support = false; // VK_EXT_debug_utils enabled
  1150. PFN_vkSetDebugUtilsObjectNameEXT pfn_vkSetDebugUtilsObjectNameEXT = {};
  1151. PFN_vkQueueBeginDebugUtilsLabelEXT pfn_vkQueueBeginDebugUtilsLabelEXT = {};
  1152. PFN_vkQueueEndDebugUtilsLabelEXT pfn_vkQueueEndDebugUtilsLabelEXT = {};
  1153. PFN_vkCmdBeginDebugUtilsLabelEXT pfn_vkCmdBeginDebugUtilsLabelEXT = {};
  1154. PFN_vkCmdEndDebugUtilsLabelEXT pfn_vkCmdEndDebugUtilsLabelEXT = {};
  1155. PFN_vkCmdInsertDebugUtilsLabelEXT pfn_vkCmdInsertDebugUtilsLabelEXT = {};
  1156. std::vector<size_t> device_indices;
  1157. std::vector<bool> device_supports_membudget;
  1158. vk_device devices[GGML_VK_MAX_DEVICES];
  1159. };
  1160. static bool vk_instance_initialized = false;
  1161. static vk_instance_t vk_instance;
  1162. static bool vk_perf_logger_enabled = false;
  1163. #ifdef GGML_VULKAN_CHECK_RESULTS
  1164. static size_t vk_skip_checks;
  1165. static size_t vk_output_tensor;
  1166. static void ggml_vk_print_tensor(const ggml_tensor * tensor, const char * name);
  1167. static void ggml_vk_check_results_0(ggml_backend_vk_context * ctx, ggml_cgraph * cgraph, int tensor_idx);
  1168. static void ggml_vk_check_results_1(ggml_backend_vk_context * ctx, ggml_cgraph * cgraph, int tensor_idx);
  1169. #endif
  1170. typedef void (*ggml_vk_func_t)(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst);
  1171. static void ggml_backend_vk_free(ggml_backend_t backend);
  1172. // Wait for ctx->fence to be signaled.
  1173. static void ggml_vk_wait_for_fence(ggml_backend_vk_context * ctx) {
  1174. // Use waitForFences while most of the graph executes. Hopefully the CPU can sleep
  1175. // during this wait.
  1176. if (ctx->almost_ready_fence_pending) {
  1177. VK_CHECK(ctx->device->device.waitForFences({ ctx->almost_ready_fence }, true, UINT64_MAX), "almost_ready_fence");
  1178. ctx->device->device.resetFences({ ctx->almost_ready_fence });
  1179. ctx->almost_ready_fence_pending = false;
  1180. }
  1181. // Spin (w/pause) waiting for the graph to finish executing.
  1182. vk::Result result;
  1183. while ((result = ctx->device->device.getFenceStatus(ctx->fence)) != vk::Result::eSuccess) {
  1184. if (result != vk::Result::eNotReady) {
  1185. fprintf(stderr, "ggml_vulkan: error %s at %s:%d\n", to_string(result).c_str(), __FILE__, __LINE__);
  1186. exit(1);
  1187. }
  1188. for (uint32_t i = 0; i < 100; ++i) {
  1189. YIELD();
  1190. YIELD();
  1191. YIELD();
  1192. YIELD();
  1193. YIELD();
  1194. YIELD();
  1195. YIELD();
  1196. YIELD();
  1197. YIELD();
  1198. YIELD();
  1199. }
  1200. }
  1201. ctx->device->device.resetFences({ ctx->fence });
  1202. }
  1203. // variables to track number of compiles in progress
  1204. static uint32_t compile_count = 0;
  1205. static std::mutex compile_count_mutex;
  1206. static std::condition_variable compile_count_cond;
  1207. static void ggml_vk_create_pipeline_func(vk_device& device, vk_pipeline& pipeline, size_t spv_size, const void* spv_data, const std::string entrypoint,
  1208. uint32_t parameter_count, std::array<uint32_t, 3> wg_denoms, std::vector<uint32_t> specialization_constants,
  1209. bool disable_robustness, bool require_full_subgroups, uint32_t required_subgroup_size) {
  1210. VK_LOG_DEBUG("ggml_vk_create_pipeline(" << device->name << ", " << pipeline->name << ", " << entrypoint << ", " << parameter_count <<
  1211. ", (" << wg_denoms[0] << "," << wg_denoms[1] << "," << wg_denoms[2] << "), specialization_constants, " <<
  1212. disable_robustness << ", " << require_full_subgroups << ", " << required_subgroup_size << ")");
  1213. GGML_ASSERT(parameter_count > 0);
  1214. GGML_ASSERT(parameter_count <= MAX_PARAMETER_COUNT);
  1215. GGML_ASSERT(wg_denoms[0] > 0 && wg_denoms[1] > 0 && wg_denoms[2] > 0); // NOLINT
  1216. vk::ShaderModuleCreateInfo shader_module_create_info({}, spv_size, reinterpret_cast<const uint32_t *>(spv_data));
  1217. pipeline->shader_module = device->device.createShaderModule(shader_module_create_info);
  1218. vk::PushConstantRange pcr(
  1219. vk::ShaderStageFlagBits::eCompute,
  1220. 0,
  1221. pipeline->push_constant_size
  1222. );
  1223. vk::PipelineLayoutCreateInfo pipeline_layout_create_info(vk::PipelineLayoutCreateFlags(), device->dsl, pcr);
  1224. pipeline->layout = device->device.createPipelineLayout(pipeline_layout_create_info);
  1225. std::vector<vk::SpecializationMapEntry> specialization_entries(specialization_constants.size());
  1226. for (size_t i = 0; i < specialization_constants.size(); i++) {
  1227. specialization_entries[i].constantID = i;
  1228. specialization_entries[i].offset = i * sizeof(uint32_t);
  1229. specialization_entries[i].size = sizeof(uint32_t);
  1230. }
  1231. vk::SpecializationInfo specialization_info(
  1232. specialization_entries.size(),
  1233. specialization_entries.data(),
  1234. specialization_constants.size() * sizeof(uint32_t),
  1235. specialization_constants.data()
  1236. );
  1237. vk::PipelineShaderStageCreateFlags pipeline_shader_stage_create_flags{};
  1238. if (device->subgroup_require_full_support && require_full_subgroups) {
  1239. pipeline_shader_stage_create_flags |= vk::PipelineShaderStageCreateFlagBits::eRequireFullSubgroupsEXT;
  1240. }
  1241. vk::PipelineShaderStageCreateInfo pipeline_shader_create_info(
  1242. pipeline_shader_stage_create_flags,
  1243. vk::ShaderStageFlagBits::eCompute,
  1244. pipeline->shader_module,
  1245. entrypoint.c_str(),
  1246. &specialization_info);
  1247. vk::PipelineShaderStageRequiredSubgroupSizeCreateInfoEXT pipeline_shader_stage_required_subgroup_size_create_info;
  1248. pipeline_shader_stage_required_subgroup_size_create_info.requiredSubgroupSize = required_subgroup_size;
  1249. if (device->subgroup_size_control && required_subgroup_size > 0) {
  1250. GGML_ASSERT(device->subgroup_min_size <= required_subgroup_size && required_subgroup_size <= device->subgroup_max_size);
  1251. pipeline_shader_create_info.setPNext(&pipeline_shader_stage_required_subgroup_size_create_info);
  1252. }
  1253. vk::ComputePipelineCreateInfo compute_pipeline_create_info(
  1254. vk::PipelineCreateFlags{},
  1255. pipeline_shader_create_info,
  1256. pipeline->layout);
  1257. vk::PipelineRobustnessCreateInfoEXT rci;
  1258. if (device->pipeline_robustness && disable_robustness) {
  1259. rci.storageBuffers = vk::PipelineRobustnessBufferBehaviorEXT::eDisabled;
  1260. rci.uniformBuffers = vk::PipelineRobustnessBufferBehaviorEXT::eDisabled;
  1261. compute_pipeline_create_info.setPNext(&rci);
  1262. }
  1263. try {
  1264. pipeline->pipeline = device->device.createComputePipeline(VK_NULL_HANDLE, compute_pipeline_create_info).value;
  1265. } catch (const vk::SystemError& e) {
  1266. std::cerr << "ggml_vulkan: Compute pipeline creation failed for " << pipeline->name << std::endl;
  1267. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  1268. throw e;
  1269. }
  1270. pipeline->compiled = true;
  1271. if (vk_instance.debug_utils_support) {
  1272. vk::DebugUtilsObjectNameInfoEXT duoni;
  1273. duoni.objectType = vk::ObjectType::ePipeline;
  1274. duoni.pObjectName = pipeline->name.c_str();
  1275. duoni.objectHandle = /*reinterpret_cast*/(uint64_t)(static_cast<VkPipeline>(pipeline->pipeline));
  1276. vk_instance.pfn_vkSetDebugUtilsObjectNameEXT(device->device, &static_cast<VkDebugUtilsObjectNameInfoEXT &>(duoni));
  1277. }
  1278. {
  1279. std::lock_guard<std::recursive_mutex> guard(device->mutex);
  1280. device->all_pipelines.push_back(pipeline);
  1281. }
  1282. {
  1283. std::lock_guard<std::mutex> guard(compile_count_mutex);
  1284. assert(compile_count > 0);
  1285. compile_count--;
  1286. }
  1287. compile_count_cond.notify_all();
  1288. }
  1289. static void ggml_vk_destroy_pipeline(vk::Device& device, vk_pipeline& pipeline) {
  1290. VK_LOG_DEBUG("ggml_pipeline_destroy_pipeline(" << pipeline->name << ")");
  1291. device.destroyPipelineLayout(pipeline->layout);
  1292. device.destroyShaderModule(pipeline->shader_module);
  1293. device.destroyPipeline(pipeline->pipeline);
  1294. }
  1295. static void ggml_pipeline_request_descriptor_sets(ggml_backend_vk_context *ctx, vk_pipeline& pipeline, uint32_t n) {
  1296. VK_LOG_DEBUG("ggml_pipeline_request_descriptor_sets(" << pipeline->name << ", " << n << ")");
  1297. ctx->pipeline_descriptor_set_requirements += n;
  1298. if (!pipeline->compiled) {
  1299. pipeline->needed = true;
  1300. ctx->device->need_compiles = true;
  1301. }
  1302. }
  1303. static void ggml_pipeline_allocate_descriptor_sets(ggml_backend_vk_context * ctx) {
  1304. if (ctx->descriptor_sets.size() >= ctx->pipeline_descriptor_set_requirements) {
  1305. // Enough descriptors are available
  1306. return;
  1307. }
  1308. vk_device& device = ctx->device;
  1309. uint32_t to_alloc = ctx->pipeline_descriptor_set_requirements - ctx->descriptor_sets.size();
  1310. uint32_t pool_remaining = VK_DEVICE_DESCRIPTOR_POOL_SIZE - ctx->descriptor_sets.size() % VK_DEVICE_DESCRIPTOR_POOL_SIZE;
  1311. uint32_t pool_idx = ctx->descriptor_sets.size() / VK_DEVICE_DESCRIPTOR_POOL_SIZE;
  1312. while (to_alloc > 0) {
  1313. const uint32_t alloc_count = std::min(pool_remaining, to_alloc);
  1314. to_alloc -= alloc_count;
  1315. pool_remaining = VK_DEVICE_DESCRIPTOR_POOL_SIZE;
  1316. if (pool_idx >= ctx->descriptor_pools.size()) {
  1317. vk::DescriptorPoolSize descriptor_pool_size(vk::DescriptorType::eStorageBuffer, MAX_PARAMETER_COUNT * VK_DEVICE_DESCRIPTOR_POOL_SIZE);
  1318. vk::DescriptorPoolCreateInfo descriptor_pool_create_info({}, VK_DEVICE_DESCRIPTOR_POOL_SIZE, descriptor_pool_size);
  1319. ctx->descriptor_pools.push_back(device->device.createDescriptorPool(descriptor_pool_create_info));
  1320. }
  1321. std::vector<vk::DescriptorSetLayout> layouts(alloc_count);
  1322. for (uint32_t i = 0; i < alloc_count; i++) {
  1323. layouts[i] = device->dsl;
  1324. }
  1325. vk::DescriptorSetAllocateInfo descriptor_set_alloc_info(ctx->descriptor_pools[pool_idx], alloc_count, layouts.data());
  1326. std::vector<vk::DescriptorSet> sets = device->device.allocateDescriptorSets(descriptor_set_alloc_info);
  1327. ctx->descriptor_sets.insert(ctx->descriptor_sets.end(), sets.begin(), sets.end());
  1328. pool_idx++;
  1329. }
  1330. }
  1331. static vk::CommandBuffer ggml_vk_create_cmd_buffer(vk_device& device, vk_command_pool& p) {
  1332. VK_LOG_DEBUG("ggml_vk_create_cmd_buffer()");
  1333. if (p.cmd_buffers.size() > p.cmd_buffer_idx) {
  1334. // Reuse command buffer
  1335. return p.cmd_buffers[p.cmd_buffer_idx++];
  1336. }
  1337. vk::CommandBufferAllocateInfo command_buffer_alloc_info(
  1338. p.pool,
  1339. vk::CommandBufferLevel::ePrimary,
  1340. 1);
  1341. const std::vector<vk::CommandBuffer> cmd_buffers = device->device.allocateCommandBuffers(command_buffer_alloc_info);
  1342. auto buf = cmd_buffers.front();
  1343. p.cmd_buffers.push_back(buf);
  1344. p.cmd_buffer_idx++;
  1345. return buf;
  1346. }
  1347. static void ggml_vk_submit(vk_context& ctx, vk::Fence fence) {
  1348. if (ctx->seqs.empty()) {
  1349. if (fence) {
  1350. std::lock_guard<std::mutex> guard(queue_mutex);
  1351. ctx->p->q->queue.submit({}, fence);
  1352. }
  1353. return;
  1354. }
  1355. VK_LOG_DEBUG("ggml_vk_submit(" << ctx << ", " << fence << ")");
  1356. std::vector<std::vector<uint64_t>> tl_wait_vals;
  1357. std::vector<std::vector<uint64_t>> tl_signal_vals;
  1358. std::vector<std::vector<vk::Semaphore>> tl_wait_semaphores;
  1359. std::vector<std::vector<vk::Semaphore>> tl_signal_semaphores;
  1360. std::vector<vk::TimelineSemaphoreSubmitInfo> tl_submit_infos;
  1361. std::vector<vk::SubmitInfo> submit_infos;
  1362. int idx = -1;
  1363. std::vector<std::vector<vk::PipelineStageFlags>> stage_flags;
  1364. size_t reserve = 0;
  1365. for (const auto& sequence : ctx->seqs) {
  1366. reserve += sequence.size();
  1367. }
  1368. // Pre-reserve vectors to prevent reallocation, which invalidates pointers
  1369. tl_wait_semaphores.reserve(reserve);
  1370. tl_wait_vals.reserve(reserve);
  1371. tl_signal_semaphores.reserve(reserve);
  1372. tl_signal_vals.reserve(reserve);
  1373. tl_submit_infos.reserve(reserve);
  1374. submit_infos.reserve(reserve);
  1375. stage_flags.reserve(reserve);
  1376. for (const auto& sequence : ctx->seqs) {
  1377. for (const auto& submission : sequence) {
  1378. stage_flags.push_back({});
  1379. idx++;
  1380. tl_wait_vals.push_back({});
  1381. tl_wait_semaphores.push_back({});
  1382. tl_signal_vals.push_back({});
  1383. tl_signal_semaphores.push_back({});
  1384. for (size_t i = 0; i < submission.wait_semaphores.size(); i++) {
  1385. stage_flags[idx].push_back(ctx->p->q->stage_flags);
  1386. tl_wait_vals[idx].push_back(submission.wait_semaphores[i].value);
  1387. tl_wait_semaphores[idx].push_back(submission.wait_semaphores[i].s);
  1388. }
  1389. for (size_t i = 0; i < submission.signal_semaphores.size(); i++) {
  1390. tl_signal_vals[idx].push_back(submission.signal_semaphores[i].value);
  1391. tl_signal_semaphores[idx].push_back(submission.signal_semaphores[i].s);
  1392. }
  1393. tl_submit_infos.push_back({
  1394. (uint32_t) submission.wait_semaphores.size(),
  1395. tl_wait_vals[idx].data(),
  1396. (uint32_t) submission.signal_semaphores.size(),
  1397. tl_signal_vals[idx].data(),
  1398. });
  1399. tl_submit_infos[idx].sType = vk::StructureType::eTimelineSemaphoreSubmitInfo;
  1400. tl_submit_infos[idx].pNext = nullptr;
  1401. vk::SubmitInfo si{
  1402. (uint32_t) submission.wait_semaphores.size(),
  1403. tl_wait_semaphores[idx].data(),
  1404. stage_flags[idx].data(),
  1405. 1,
  1406. &submission.buffer,
  1407. (uint32_t) submission.signal_semaphores.size(),
  1408. tl_signal_semaphores[idx].data(),
  1409. };
  1410. si.setPNext(&tl_submit_infos[idx]);
  1411. submit_infos.push_back(si);
  1412. }
  1413. }
  1414. std::lock_guard<std::mutex> guard(queue_mutex);
  1415. ctx->p->q->queue.submit(submit_infos, fence);
  1416. ctx->seqs.clear();
  1417. }
  1418. static uint32_t ggml_vk_find_queue_family_index(std::vector<vk::QueueFamilyProperties>& queue_family_props, const vk::QueueFlags& required, const vk::QueueFlags& avoid, int32_t compute_index, uint32_t min_num_queues) {
  1419. VK_LOG_DEBUG("ggml_vk_find_queue_family_index()");
  1420. const uint32_t qfsize = queue_family_props.size();
  1421. // Try with avoid preferences first
  1422. for (uint32_t i = 0; i < qfsize; i++) {
  1423. if (queue_family_props[i].queueCount >= min_num_queues && (compute_index < 0 || i != (uint32_t) compute_index) && queue_family_props[i].queueFlags & required && !(queue_family_props[i].queueFlags & avoid)) {
  1424. return i;
  1425. }
  1426. }
  1427. // Fall back to only required
  1428. for (size_t i = 0; i < qfsize; i++) {
  1429. if (queue_family_props[i].queueCount >= min_num_queues && (compute_index < 0 || i != (uint32_t) compute_index) && queue_family_props[i].queueFlags & required) {
  1430. return i;
  1431. }
  1432. }
  1433. // Fall back to reusing compute queue
  1434. for (size_t i = 0; i < qfsize; i++) {
  1435. if (queue_family_props[i].queueCount >= min_num_queues && queue_family_props[i].queueFlags & required) {
  1436. return i;
  1437. }
  1438. }
  1439. // Fall back to ignoring min_num_queries
  1440. for (size_t i = 0; i < qfsize; i++) {
  1441. if (queue_family_props[i].queueFlags & required) {
  1442. return i;
  1443. }
  1444. }
  1445. // All commands that are allowed on a queue that supports transfer operations are also allowed on a queue that supports either graphics or compute operations.
  1446. // Thus, if the capabilities of a queue family include VK_QUEUE_GRAPHICS_BIT or VK_QUEUE_COMPUTE_BIT, then reporting the VK_QUEUE_TRANSFER_BIT capability separately for that queue family is optional.
  1447. if (compute_index >= 0) {
  1448. return compute_index;
  1449. }
  1450. std::cerr << "ggml_vulkan: No suitable queue family index found." << std::endl;
  1451. for(auto &q_family : queue_family_props) {
  1452. std::cerr << "Queue number: " + std::to_string(q_family.queueCount) << " flags: " + to_string(q_family.queueFlags) << std::endl;
  1453. }
  1454. abort();
  1455. }
  1456. static void ggml_vk_create_queue(vk_device& device, vk_queue& q, uint32_t queue_family_index, uint32_t queue_index, vk::PipelineStageFlags&& stage_flags, bool transfer_only) {
  1457. VK_LOG_DEBUG("ggml_vk_create_queue()");
  1458. std::lock_guard<std::recursive_mutex> guard(device->mutex);
  1459. q.queue_family_index = queue_family_index;
  1460. q.transfer_only = transfer_only;
  1461. q.cmd_pool.init(device, &q);
  1462. q.queue = device->device.getQueue(queue_family_index, queue_index);
  1463. q.stage_flags = stage_flags;
  1464. }
  1465. static vk_context ggml_vk_create_context(ggml_backend_vk_context * ctx, vk_command_pool& p) {
  1466. vk_context result = std::make_shared<vk_context_struct>();
  1467. VK_LOG_DEBUG("ggml_vk_create_context(" << result << ")");
  1468. ctx->gc.contexts.emplace_back(result);
  1469. result->p = &p;
  1470. return result;
  1471. }
  1472. static vk_context ggml_vk_create_temporary_context(vk_command_pool& p) {
  1473. vk_context result = std::make_shared<vk_context_struct>();
  1474. VK_LOG_DEBUG("ggml_vk_create_temporary_context(" << result << ")");
  1475. result->p = &p;
  1476. return result;
  1477. }
  1478. static vk_semaphore * ggml_vk_create_binary_semaphore(ggml_backend_vk_context * ctx) {
  1479. VK_LOG_DEBUG("ggml_vk_create_timeline_semaphore()");
  1480. vk::SemaphoreTypeCreateInfo tci{ vk::SemaphoreType::eBinary, 0 };
  1481. vk::SemaphoreCreateInfo ci{};
  1482. ci.setPNext(&tci);
  1483. vk::Semaphore semaphore = ctx->device->device.createSemaphore(ci);
  1484. ctx->gc.semaphores.push_back({ semaphore, 0 });
  1485. return &ctx->gc.semaphores[ctx->gc.semaphores.size() - 1];
  1486. }
  1487. static vk_semaphore * ggml_vk_create_timeline_semaphore(ggml_backend_vk_context * ctx) {
  1488. VK_LOG_DEBUG("ggml_vk_create_timeline_semaphore()");
  1489. if (ctx->semaphore_idx >= ctx->gc.tl_semaphores.size()) {
  1490. vk::SemaphoreTypeCreateInfo tci{ vk::SemaphoreType::eTimeline, 0 };
  1491. vk::SemaphoreCreateInfo ci{};
  1492. ci.setPNext(&tci);
  1493. vk::Semaphore semaphore = ctx->device->device.createSemaphore(ci);
  1494. ctx->gc.tl_semaphores.push_back({ semaphore, 0 });
  1495. }
  1496. return &ctx->gc.tl_semaphores[ctx->semaphore_idx++];
  1497. }
  1498. static vk::Event ggml_vk_create_event(ggml_backend_vk_context * ctx) {
  1499. if (ctx->event_idx >= ctx->gc.events.size()) {
  1500. ctx->gc.events.push_back(ctx->device->device.createEvent({}));
  1501. }
  1502. return ctx->gc.events[ctx->event_idx++];
  1503. }
  1504. static void ggml_vk_command_pool_cleanup(vk_device& device, vk_command_pool& p) {
  1505. VK_LOG_DEBUG("ggml_vk_command_pool_cleanup()");
  1506. // Requires command buffers to be done
  1507. device->device.resetCommandPool(p.pool);
  1508. p.cmd_buffer_idx = 0;
  1509. }
  1510. static void ggml_vk_queue_command_pools_cleanup(vk_device& device) {
  1511. VK_LOG_DEBUG("ggml_vk_queue_command_pools_cleanup()");
  1512. // Arbitrary frequency to cleanup/reuse command buffers
  1513. static constexpr uint32_t cleanup_frequency = 10;
  1514. if (device->compute_queue.cmd_pool.cmd_buffer_idx >= cleanup_frequency) {
  1515. ggml_vk_command_pool_cleanup(device, device->compute_queue.cmd_pool);
  1516. }
  1517. if (device->transfer_queue.cmd_pool.cmd_buffer_idx >= cleanup_frequency) {
  1518. ggml_vk_command_pool_cleanup(device, device->transfer_queue.cmd_pool);
  1519. }
  1520. }
  1521. static uint32_t find_properties(const vk::PhysicalDeviceMemoryProperties* mem_props, vk::MemoryRequirements* mem_req, vk::MemoryPropertyFlags flags) {
  1522. for (uint32_t i = 0; i < mem_props->memoryTypeCount; ++i) {
  1523. vk::MemoryType memory_type = mem_props->memoryTypes[i];
  1524. if ((mem_req->memoryTypeBits & ((uint64_t)1 << i)) &&
  1525. (flags & memory_type.propertyFlags) == flags &&
  1526. mem_props->memoryHeaps[memory_type.heapIndex].size >= mem_req->size) {
  1527. return static_cast<int32_t>(i);
  1528. }
  1529. }
  1530. return UINT32_MAX;
  1531. }
  1532. static vk_buffer ggml_vk_create_buffer(vk_device& device, size_t size, const std::initializer_list<vk::MemoryPropertyFlags> & req_flags_list) {
  1533. VK_LOG_DEBUG("ggml_vk_create_buffer(" << device->name << ", " << size << ", " << to_string(req_flags_list.begin()[0]) << ", " << to_string(req_flags_list.begin()[req_flags_list.size()-1]) << ")");
  1534. if (size > device->max_memory_allocation_size) {
  1535. throw vk::OutOfDeviceMemoryError("Requested buffer size exceeds device memory allocation limit");
  1536. }
  1537. vk_buffer buf = std::make_shared<vk_buffer_struct>();
  1538. if (size == 0) {
  1539. buf->size = 0;
  1540. return buf;
  1541. }
  1542. vk::BufferCreateInfo buffer_create_info{
  1543. vk::BufferCreateFlags(),
  1544. size,
  1545. vk::BufferUsageFlagBits::eStorageBuffer | vk::BufferUsageFlagBits::eTransferSrc | vk::BufferUsageFlagBits::eTransferDst,
  1546. vk::SharingMode::eExclusive,
  1547. 0,
  1548. nullptr,
  1549. };
  1550. buf->buffer = device->device.createBuffer(buffer_create_info);
  1551. vk::MemoryRequirements mem_req = device->device.getBufferMemoryRequirements(buf->buffer);
  1552. vk::PhysicalDeviceMemoryProperties mem_props = device->physical_device.getMemoryProperties();
  1553. for (auto &req_flags : req_flags_list) {
  1554. uint32_t memory_type_index = find_properties(&mem_props, &mem_req, req_flags);
  1555. if (memory_type_index == UINT32_MAX) {
  1556. continue;
  1557. }
  1558. buf->memory_property_flags = req_flags;
  1559. try {
  1560. buf->device_memory = device->device.allocateMemory({ mem_req.size, memory_type_index });
  1561. break;
  1562. } catch (const vk::SystemError& e) {
  1563. // loop and retry
  1564. }
  1565. }
  1566. if (buf->device_memory == VK_NULL_HANDLE) {
  1567. device->device.destroyBuffer(buf->buffer);
  1568. throw vk::OutOfDeviceMemoryError("No suitable memory type found");
  1569. }
  1570. buf->ptr = nullptr;
  1571. if (buf->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  1572. buf->ptr = device->device.mapMemory(buf->device_memory, 0, VK_WHOLE_SIZE);
  1573. }
  1574. device->device.bindBufferMemory(buf->buffer, buf->device_memory, 0);
  1575. buf->device = device;
  1576. buf->size = size;
  1577. #ifdef GGML_VULKAN_MEMORY_DEBUG
  1578. device->memory_logger->log_allocation(buf, size);
  1579. #endif
  1580. return buf;
  1581. }
  1582. static vk_buffer ggml_vk_create_buffer_check(vk_device& device, size_t size, vk::MemoryPropertyFlags req_flags, vk::MemoryPropertyFlags fallback_flags = vk::MemoryPropertyFlags(0)) {
  1583. try {
  1584. return ggml_vk_create_buffer(device, size, {req_flags, fallback_flags});
  1585. } catch (const vk::SystemError& e) {
  1586. std::cerr << "ggml_vulkan: Memory allocation of size " << size << " failed." << std::endl;
  1587. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  1588. throw e;
  1589. }
  1590. }
  1591. static vk_buffer ggml_vk_create_buffer_device(vk_device& device, size_t size) {
  1592. vk_buffer buf;
  1593. try {
  1594. if (device->prefer_host_memory) {
  1595. buf = ggml_vk_create_buffer(device, size, {vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent,
  1596. vk::MemoryPropertyFlagBits::eDeviceLocal});
  1597. } else if (device->uma) {
  1598. // Fall back to host memory type
  1599. buf = ggml_vk_create_buffer(device, size, {vk::MemoryPropertyFlagBits::eDeviceLocal,
  1600. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent});
  1601. } else if (device->disable_host_visible_vidmem) {
  1602. if (device->allow_sysmem_fallback) {
  1603. buf = ggml_vk_create_buffer(device, size, {vk::MemoryPropertyFlagBits::eDeviceLocal,
  1604. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent});
  1605. } else {
  1606. buf = ggml_vk_create_buffer(device, size, {vk::MemoryPropertyFlagBits::eDeviceLocal});
  1607. }
  1608. } else {
  1609. // use rebar if available, otherwise fallback to device only visible memory
  1610. if (device->allow_sysmem_fallback) {
  1611. buf = ggml_vk_create_buffer(device, size, {vk::MemoryPropertyFlagBits::eDeviceLocal | vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent,
  1612. vk::MemoryPropertyFlagBits::eDeviceLocal,
  1613. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent});
  1614. } else {
  1615. buf = ggml_vk_create_buffer(device, size, {vk::MemoryPropertyFlagBits::eDeviceLocal | vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent,
  1616. vk::MemoryPropertyFlagBits::eDeviceLocal});
  1617. }
  1618. }
  1619. } catch (const vk::SystemError& e) {
  1620. std::cerr << "ggml_vulkan: Device memory allocation of size " << size << " failed." << std::endl;
  1621. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  1622. throw e;
  1623. }
  1624. return buf;
  1625. }
  1626. static void ggml_vk_destroy_buffer(vk_buffer& buf) {
  1627. if (buf == nullptr) {
  1628. return;
  1629. }
  1630. #ifdef GGML_VULKAN_MEMORY_DEBUG
  1631. if (buf->device != nullptr) {
  1632. buf->device->memory_logger->log_deallocation(buf);
  1633. }
  1634. #endif
  1635. buf.reset();
  1636. }
  1637. static vk_subbuffer ggml_vk_subbuffer(vk_buffer& buf) {
  1638. return { buf, 0, VK_WHOLE_SIZE };
  1639. }
  1640. static void ggml_vk_sync_buffers(ggml_backend_vk_context* ctx, vk_context& subctx) {
  1641. VK_LOG_DEBUG("ggml_vk_sync_buffers()");
  1642. const bool transfer_queue = subctx->p->q->transfer_only;
  1643. if (ctx) {
  1644. ctx->prealloc_x_need_sync = ctx->prealloc_y_need_sync = ctx->prealloc_split_k_need_sync = false;
  1645. }
  1646. subctx->s->buffer.pipelineBarrier(
  1647. subctx->p->q->stage_flags,
  1648. subctx->p->q->stage_flags,
  1649. {},
  1650. { {
  1651. { !transfer_queue ? (vk::AccessFlagBits::eShaderRead | vk::AccessFlagBits::eShaderWrite | vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) : (vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) },
  1652. { !transfer_queue ? (vk::AccessFlagBits::eShaderRead | vk::AccessFlagBits::eShaderWrite | vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) : (vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) }
  1653. } },
  1654. {},
  1655. {}
  1656. );
  1657. }
  1658. static void ggml_vk_wait_events(vk_context& ctx, std::vector<vk::Event>&& events) {
  1659. VK_LOG_DEBUG("ggml_vk_wait_events()");
  1660. if (events.empty()) {
  1661. return;
  1662. }
  1663. ctx->s->buffer.waitEvents(
  1664. events,
  1665. ctx->p->q->stage_flags,
  1666. ctx->p->q->stage_flags,
  1667. {},
  1668. {},
  1669. {}
  1670. );
  1671. }
  1672. // number of rows/cols for flash attention shader
  1673. static constexpr uint32_t flash_attention_num_small_rows = 32;
  1674. static constexpr uint32_t scalar_flash_attention_num_small_rows = 1;
  1675. static uint32_t get_fa_scalar_num_large_rows(uint32_t hsv) {
  1676. if (hsv >= 192) {
  1677. return 2;
  1678. } else {
  1679. return 8;
  1680. }
  1681. }
  1682. // The FA coopmat1 shader assumes 16x16x16 matrix multiply support.
  1683. // 128 threads split into four subgroups, each subgroup does 1/4
  1684. // of the Bc dimension.
  1685. static constexpr uint32_t coopmat1_flash_attention_num_large_rows = 16;
  1686. static constexpr uint32_t scalar_flash_attention_Bc = 64;
  1687. static constexpr uint32_t scalar_flash_attention_workgroup_size = 128;
  1688. static uint32_t get_fa_num_small_rows(FaCodePath path) {
  1689. if (path == FA_COOPMAT2) {
  1690. return flash_attention_num_small_rows;
  1691. } else {
  1692. return scalar_flash_attention_num_small_rows;
  1693. }
  1694. }
  1695. static std::array<uint32_t, 2> fa_rows_cols(FaCodePath path, uint32_t hsk, uint32_t hsv, uint32_t clamp, ggml_type type, bool small_rows) {
  1696. GGML_UNUSED(clamp);
  1697. GGML_UNUSED(hsv);
  1698. if (path == FA_SCALAR) {
  1699. if (small_rows) {
  1700. return {scalar_flash_attention_num_small_rows, 64};
  1701. } else {
  1702. if ((hsv | hsk) & 8) {
  1703. // HSV/HSK not being a multiple of 16 makes D_split smaller, which makes cols_per_iter
  1704. // larger, and Bc needs to be >= cols_per_thread. 64 is large enough, 32 is not.
  1705. return {get_fa_scalar_num_large_rows(hsv), 64};
  1706. } else {
  1707. return {get_fa_scalar_num_large_rows(hsv), 32};
  1708. }
  1709. }
  1710. }
  1711. if (path == FA_COOPMAT1) {
  1712. if (small_rows) {
  1713. return {scalar_flash_attention_num_small_rows, scalar_flash_attention_Bc};
  1714. } else {
  1715. return {coopmat1_flash_attention_num_large_rows, scalar_flash_attention_Bc};
  1716. }
  1717. }
  1718. // small rows, large cols
  1719. if (small_rows) {
  1720. return {get_fa_num_small_rows(FA_COOPMAT2), 32};
  1721. }
  1722. // small cols to reduce register count
  1723. if (ggml_is_quantized(type) || hsk >= 256 || hsv >= 256) {
  1724. if (hsk >= 512 || hsv >= 512) {
  1725. return {32, 32};
  1726. } else {
  1727. return {64, 32};
  1728. }
  1729. }
  1730. return {64, 64};
  1731. }
  1732. static uint32_t fa_align(FaCodePath path, uint32_t hsk, uint32_t hsv, ggml_type type, bool small_rows) {
  1733. return fa_rows_cols(path, hsk, hsv, 0, type, small_rows)[1];
  1734. }
  1735. static bool ggml_vk_matmul_shmem_support(const vk_device& device, const std::vector<uint32_t>& warptile, bool mul_mat_id, ggml_type src0_type) {
  1736. uint32_t lut_size = 0;
  1737. switch (src0_type) {
  1738. case GGML_TYPE_IQ1_S:
  1739. case GGML_TYPE_IQ1_M:
  1740. lut_size = 2*2048;
  1741. break;
  1742. case GGML_TYPE_IQ2_XXS:
  1743. lut_size = 8*256;
  1744. break;
  1745. case GGML_TYPE_IQ2_XS:
  1746. lut_size = 8*512;
  1747. break;
  1748. case GGML_TYPE_IQ2_S:
  1749. lut_size = 8*1024;
  1750. break;
  1751. case GGML_TYPE_IQ3_XXS:
  1752. lut_size = 4*256;
  1753. break;
  1754. case GGML_TYPE_IQ3_S:
  1755. lut_size = 4*512;
  1756. break;
  1757. case GGML_TYPE_IQ4_NL:
  1758. case GGML_TYPE_IQ4_XS:
  1759. case GGML_TYPE_MXFP4:
  1760. lut_size = 4*16;
  1761. break;
  1762. default:
  1763. break;
  1764. }
  1765. // Needs to be kept up to date on shader changes
  1766. const uint32_t bank_conflict_offset = device->coopmat_support ? 8 : 1;
  1767. const uint32_t type_size = device->fp16 ? sizeof(ggml_fp16_t) : sizeof(float);
  1768. const uint32_t warps = warptile[0] / warptile[10];
  1769. const uint32_t load_bufs = (warptile[1] + warptile[2]) * (warptile[3] + bank_conflict_offset) * type_size;
  1770. const uint32_t mmid_row_ids = mul_mat_id ? (warptile[2] * 2 * sizeof(uint16_t)) : 0;
  1771. const uint32_t coopmat_stage = device->coopmat_support ? warptile[7] * warptile[8] / warps * sizeof(float) : 0;
  1772. const uint32_t ballots_sh = mul_mat_id ? (warps * 4 * sizeof(uint32_t)) : 0;
  1773. const uint32_t total_size = load_bufs + mmid_row_ids + coopmat_stage + lut_size + ballots_sh;
  1774. const bool supported = total_size <= device->properties.limits.maxComputeSharedMemorySize;
  1775. VK_LOG_DEBUG("ggml_vk_matmul_shmem_support(warptile=(" << warptile[0] << "," << warptile[1] << "," << warptile[2] << "), "
  1776. "mul_mat_id=" << mul_mat_id << ", src0_type=" << ggml_type_name(src0_type) << ", supported=" << supported);
  1777. return supported;
  1778. }
  1779. struct GpuPipelineConfig {
  1780. // GPU architecture identifier.
  1781. // Example: vk_device_architecture::AMD_GCN
  1782. vk_device_architecture arch;
  1783. // Mapping of pipeline names to their specific subgroup sizes.
  1784. // Example: {"soft_max_f32", 64}
  1785. std::unordered_map<std::string, uint32_t> pipelines;
  1786. // Default subgroup size for this GPU.
  1787. // Defaults to 0 if not explicitly provided.
  1788. uint32_t default_subgroup_size = 0;
  1789. };
  1790. // Pipeline configuration for RDNA1 GPUs.
  1791. static const std::unordered_map<std::string, uint32_t> rdna1_pipelines = {
  1792. {"soft_max", 64}, {"im2col", 64},
  1793. {"argmax", 64}, {"mul_mat_vec", 64},
  1794. {"mul_mat_vec_f16", 32}, {"mul_mat_vec_f32_f16", 32}
  1795. };
  1796. // Pipeline configuration for RDNA2 GPUs.
  1797. static const std::unordered_map<std::string, uint32_t> rdna2_pipelines = {
  1798. {"soft_max", 64}, {"im2col", 64},
  1799. };
  1800. static constexpr uint32_t RDNA_DEFAULT_SUBGROUP_SIZE = 32;
  1801. // Define configurations for different GPUs.
  1802. static std::vector<GpuPipelineConfig> gpu_pipeline_configs = {
  1803. {
  1804. vk_device_architecture::AMD_RDNA1,
  1805. {
  1806. rdna1_pipelines,
  1807. },
  1808. RDNA_DEFAULT_SUBGROUP_SIZE
  1809. },
  1810. {
  1811. vk_device_architecture::AMD_RDNA2,
  1812. {
  1813. rdna2_pipelines,
  1814. },
  1815. RDNA_DEFAULT_SUBGROUP_SIZE
  1816. },
  1817. };
  1818. static uint32_t get_subgroup_size(const std::string &pipeline_name, const vk_device_architecture &arch) {
  1819. for (const auto &config : gpu_pipeline_configs) {
  1820. if (config.arch == arch) {
  1821. auto pipIt = config.pipelines.find(pipeline_name);
  1822. if (pipIt != config.pipelines.end()) {
  1823. return pipIt->second;
  1824. }
  1825. std::vector<std::pair<std::string, uint32_t>> sorted_pipelines(config.pipelines.begin(), config.pipelines.end());
  1826. std::sort(sorted_pipelines.begin(), sorted_pipelines.end(),
  1827. [](const auto &a, const auto &b) { return a.first.size() > b.first.size(); });
  1828. for (const auto &entry : sorted_pipelines) {
  1829. if (pipeline_name.find(entry.first) != std::string::npos) {
  1830. return entry.second;
  1831. }
  1832. }
  1833. return config.default_subgroup_size;
  1834. }
  1835. }
  1836. return 0; // If no matching configuration is found
  1837. }
  1838. static void ggml_vk_load_shaders(vk_device& device) {
  1839. VK_LOG_DEBUG("ggml_vk_load_shaders(" << device->name << ")");
  1840. // some shaders have a minimum subgroup size
  1841. const uint32_t subgroup_size_8 = std::max(device->subgroup_size, 8u);
  1842. const uint32_t subgroup_size_16 = std::max(device->subgroup_size, 16u);
  1843. const uint32_t subgroup_size_32 = std::max(device->subgroup_size, 32u);
  1844. const uint32_t mul_mat_subgroup_size = (device->vendor_id == VK_VENDOR_ID_INTEL && device->subgroup_size_control) ? device->subgroup_min_size : device->subgroup_size;
  1845. const uint32_t mul_mat_subgroup_size_8 = std::max(mul_mat_subgroup_size, 8u);
  1846. const uint32_t mul_mat_subgroup_size_16 = std::max(mul_mat_subgroup_size, 16u);
  1847. const uint32_t mul_mat_subgroup_size_32 = std::max(mul_mat_subgroup_size, 32u);
  1848. const bool subgroup_min_size_16 = (!device->subgroup_size_control && device->subgroup_size >= 16) ||
  1849. (device->subgroup_size_control && device->subgroup_max_size >= 16);
  1850. // mulmat
  1851. std::vector<uint32_t> l_warptile, m_warptile, s_warptile,
  1852. l_warptile_id, m_warptile_id, s_warptile_id,
  1853. l_warptile_mmq, m_warptile_mmq, s_warptile_mmq,
  1854. l_warptile_mmq_int, m_warptile_mmq_int, s_warptile_mmq_int,
  1855. l_warptile_mmq_k, m_warptile_mmq_k, s_warptile_mmq_k,
  1856. l_warptile_mmqid, m_warptile_mmqid, s_warptile_mmqid;
  1857. std::array<uint32_t, 3> l_wg_denoms, m_wg_denoms, s_wg_denoms,
  1858. l_mmq_wg_denoms, m_mmq_wg_denoms, s_mmq_wg_denoms,
  1859. l_mmq_wg_denoms_k, m_mmq_wg_denoms_k, s_mmq_wg_denoms_k,
  1860. l_mmqid_wg_denoms, m_mmqid_wg_denoms, s_mmqid_wg_denoms;
  1861. uint32_t l_align, m_align, s_align;
  1862. if (device->coopmat2) {
  1863. // spec constants and tile sizes for non-quant matmul/matmul_id
  1864. l_warptile = { 256, 128, 256, 64, 1 };
  1865. m_warptile = { 256, 128, 128, 64, 0 };
  1866. s_warptile = { 128, 64, 64, 64, 0 };
  1867. l_wg_denoms = {128, 256, 1 };
  1868. m_wg_denoms = {128, 128, 1 };
  1869. s_wg_denoms = { 64, 64, 1 };
  1870. // spec constants and tile sizes for quant matmul (non-Qi_K)
  1871. l_warptile_mmq = { 256, 128, 256, 64, 1 };
  1872. m_warptile_mmq = { 256, 128, 128, 64, 1 };
  1873. s_warptile_mmq = { 256, 32, 64, 128, 0 };
  1874. l_mmq_wg_denoms = { 128, 256, 1 };
  1875. m_mmq_wg_denoms = { 128, 128, 1 };
  1876. s_mmq_wg_denoms = { 32, 64, 1 };
  1877. // spec constants and tile sizes for quant matmul (Qi_K)
  1878. l_warptile_mmq_k = { 256, 128, 256, 64, 1 };
  1879. m_warptile_mmq_k = { 256, 128, 128, 64, 1 };
  1880. s_warptile_mmq_k = { 256, 32, 64, 128, 0 };
  1881. l_mmq_wg_denoms_k = { 128, 256, 1 };
  1882. m_mmq_wg_denoms_k = { 128, 128, 1 };
  1883. s_mmq_wg_denoms_k = { 32, 64, 1 };
  1884. // spec constants and tile sizes for quant matmul_id
  1885. l_warptile_mmqid = { 256, 128, 128, 16, 1, device->subgroup_size };
  1886. m_warptile_mmqid = { 256, 128, 64, 16, 0, device->subgroup_size };
  1887. s_warptile_mmqid = { 256, 128, 64, 16, 0, device->subgroup_size };
  1888. l_mmqid_wg_denoms = { 128, 128, 1 };
  1889. m_mmqid_wg_denoms = { 128, 64, 1 };
  1890. s_mmqid_wg_denoms = { 128, 64, 1 };
  1891. l_align = 128;
  1892. m_align = 64;
  1893. s_align = 32;
  1894. } else {
  1895. // Matrix cores require different warp group sizes
  1896. const uint32_t tm_l = device->coopmat_support ? device->coopmat_m : 4;
  1897. const uint32_t tm_m = device->coopmat_support ? device->coopmat_m : 4;
  1898. const uint32_t tm_s = device->coopmat_support ? device->coopmat_m : 2;
  1899. const uint32_t tn_l = device->coopmat_support ? device->coopmat_n : 4;
  1900. const uint32_t tn_m = device->coopmat_support ? device->coopmat_n : 2;
  1901. const uint32_t tn_s = device->coopmat_support ? device->coopmat_n : 2;
  1902. const uint32_t tk_l = device->coopmat_support ? device->coopmat_k : 1;
  1903. const uint32_t tk_m = device->coopmat_support ? device->coopmat_k : 1;
  1904. const uint32_t tk_s = device->coopmat_support ? device->coopmat_k : 1;
  1905. l_warptile = { 128, 128, 128, 16, subgroup_size_8 * 2, 64, 2, tm_l, tn_l, tk_l, subgroup_size_8 };
  1906. m_warptile = { 128, 64, 64, 16, subgroup_size_8, 32, 2, tm_m, tn_m, tk_m, subgroup_size_8 };
  1907. s_warptile = { subgroup_size_16, 32, 32, 16, 32, 32, 2, tm_s, tn_s, tk_s, subgroup_size_8 };
  1908. l_warptile_mmq = { 128, 128, 128, 32, subgroup_size_8 * 2, 64, 2, tm_l, tn_l, tk_l, subgroup_size_8 };
  1909. m_warptile_mmq = { 128, 64, 64, 32, subgroup_size_8, 32, 2, tm_m, tn_m, tk_m, subgroup_size_8 };
  1910. s_warptile_mmq = { subgroup_size_32, 32, 32, 32, 32, 32, 2, tm_s, tn_s, tk_s, subgroup_size_8 };
  1911. l_warptile_mmq_int = { 128, 128, 128, 32, subgroup_size_8 * 2, 64, 2, 4, 4, 1, subgroup_size_8 };
  1912. m_warptile_mmq_int = { 128, 64, 64, 32, subgroup_size_8, 32, 2, 2, 2, 1, subgroup_size_8 };
  1913. s_warptile_mmq_int = { subgroup_size_32, 32, 32, 32, 32, 32, 2, 2, 1, 1, subgroup_size_8 };
  1914. l_warptile_id = { 128, 128, 128, 16, mul_mat_subgroup_size_16 * 2, 64, 2, tm_l, tn_l, tk_l, mul_mat_subgroup_size_16 };
  1915. m_warptile_id = { 128, 64, 64, 16, mul_mat_subgroup_size_16, 32, 2, tm_m, tn_m, tk_m, mul_mat_subgroup_size_16 };
  1916. s_warptile_id = { mul_mat_subgroup_size_16, 32, 32, 16, 32, 32, 2, tm_s, tn_s, tk_s, mul_mat_subgroup_size_16 };
  1917. l_warptile_mmqid = { 128, 128, 128, 32, mul_mat_subgroup_size_8 * 2, 64, 2, tm_l, tn_l, tk_l, mul_mat_subgroup_size_8 };
  1918. m_warptile_mmqid = { 128, 64, 64, 32, mul_mat_subgroup_size_8, 32, 2, tm_m, tn_m, tk_m, mul_mat_subgroup_size_8 };
  1919. s_warptile_mmqid = { mul_mat_subgroup_size_32, 32, 32, 32, 32, 32, 2, tm_s, tn_s, tk_s, mul_mat_subgroup_size_8 };
  1920. // chip specific tuning
  1921. if ((device->architecture == AMD_GCN) && (device->driver_id != vk::DriverId::eAmdProprietary)) {
  1922. m_warptile_mmq = m_warptile_mmq_int = { 256, 64, 64, 32, 16, 16, 2, 2, 2, 1, 16 };
  1923. m_warptile_mmqid = { 256, 64, 64, 32, 16, 16, 2, 2, 2, 1, 16 };
  1924. }
  1925. l_mmq_wg_denoms = l_wg_denoms = {128, 128, 1 };
  1926. m_mmq_wg_denoms = m_wg_denoms = { 64, 64, 1 };
  1927. s_mmq_wg_denoms = s_wg_denoms = { 32, 32, 1 };
  1928. l_align = 128;
  1929. m_align = 64;
  1930. s_align = 32;
  1931. for (uint32_t i = 0; i < GGML_TYPE_COUNT; ++i) {
  1932. ggml_type t = (ggml_type)i;
  1933. // Disable medium and large matrix multiplication if not enough shared memory is available
  1934. // Check mmq warptiles as the largest configuration
  1935. // Throw an error if not enough for any matrix multiplication is available
  1936. if (!ggml_vk_matmul_shmem_support(device, s_warptile_mmq, false, t)) {
  1937. std::cerr << "ggml_vulkan: Error: Shared memory size too small for matrix multiplication." << std::endl;
  1938. throw std::runtime_error("Shared memory size too small for matrix multiplication.");
  1939. } else if (!ggml_vk_matmul_shmem_support(device, m_warptile_mmq, false, t)) {
  1940. device->mul_mat_m[i] = false;
  1941. device->mul_mat_l[i] = false;
  1942. } else if (!ggml_vk_matmul_shmem_support(device, l_warptile_mmq, false, t)) {
  1943. device->mul_mat_l[i] = false;
  1944. }
  1945. // Disable mul_mat_id if not enough shared memory is available
  1946. if (!ggml_vk_matmul_shmem_support(device, s_warptile_mmqid, true, t)) {
  1947. device->mul_mat_id_s[i] = false;
  1948. device->mul_mat_id_m[i] = false;
  1949. device->mul_mat_id_l[i] = false;
  1950. } else if (!ggml_vk_matmul_shmem_support(device, m_warptile_mmqid, true, t)) {
  1951. device->mul_mat_id_m[i] = false;
  1952. device->mul_mat_id_l[i] = false;
  1953. } else if (!ggml_vk_matmul_shmem_support(device, l_warptile_mmqid, true, t)) {
  1954. device->mul_mat_id_l[i] = false;
  1955. }
  1956. }
  1957. }
  1958. if (!device->pipeline_matmul_f32) {
  1959. device->pipeline_matmul_f32 = std::make_shared<vk_matmul_pipeline_struct>();
  1960. }
  1961. if (!device->pipeline_matmul_f32_f16) {
  1962. device->pipeline_matmul_f32_f16 = std::make_shared<vk_matmul_pipeline_struct>();
  1963. }
  1964. if (!device->pipeline_matmul_id_f32) {
  1965. device->pipeline_matmul_id_f32 = std::make_shared<vk_matmul_pipeline_struct>();
  1966. }
  1967. if (!device->pipeline_matmul_bf16) {
  1968. device->pipeline_matmul_bf16 = std::make_shared<vk_matmul_pipeline_struct>();
  1969. }
  1970. if (!device->pipeline_matmul_id_bf16) {
  1971. device->pipeline_matmul_id_bf16 = std::make_shared<vk_matmul_pipeline_struct>();
  1972. }
  1973. std::vector<std::future<void>> compiles;
  1974. auto const &ggml_vk_create_pipeline = [&](vk_device& device, vk_pipeline& pipeline, const std::string &name, size_t spv_size, const void* spv_data, const std::string &entrypoint,
  1975. uint32_t parameter_count, uint32_t push_constant_size, std::array<uint32_t, 3> wg_denoms, const std::vector<uint32_t>& specialization_constants,
  1976. uint32_t align, bool disable_robustness = false, bool require_full_subgroups = false, uint32_t required_subgroup_size = 0) {
  1977. if (!require_full_subgroups && required_subgroup_size == 0) {
  1978. required_subgroup_size = get_subgroup_size(name, device->architecture);
  1979. }
  1980. if (!pipeline) {
  1981. pipeline = std::make_shared<vk_pipeline_struct>();
  1982. }
  1983. if (!pipeline->initialized) {
  1984. pipeline->name = name;
  1985. pipeline->parameter_count = parameter_count;
  1986. pipeline->push_constant_size = push_constant_size;
  1987. pipeline->wg_denoms = wg_denoms;
  1988. pipeline->align = align;
  1989. pipeline->initialized = true;
  1990. }
  1991. if (!pipeline->needed || pipeline->compiled) {
  1992. return;
  1993. }
  1994. {
  1995. // wait until fewer than N compiles are in progress
  1996. uint32_t N = std::max(1u, std::thread::hardware_concurrency());
  1997. std::unique_lock<std::mutex> guard(compile_count_mutex);
  1998. while (compile_count >= N) {
  1999. compile_count_cond.wait(guard);
  2000. }
  2001. compile_count++;
  2002. }
  2003. compiles.push_back(std::async(ggml_vk_create_pipeline_func, std::ref(device), std::ref(pipeline), spv_size, spv_data, entrypoint,
  2004. parameter_count, wg_denoms, specialization_constants, disable_robustness, require_full_subgroups, required_subgroup_size));
  2005. };
  2006. auto const &fa_wg_denoms = [&](FaCodePath path, uint32_t hsk, uint32_t hsv, uint32_t clamp, ggml_type type, bool small_rows) -> std::array<uint32_t, 3> {
  2007. return {fa_rows_cols(path, hsk, hsv, clamp, type, small_rows)[0], 1, 1};
  2008. };
  2009. auto const &fa_spec_constants = [&](FaCodePath path, uint32_t hsk, uint32_t hsv, uint32_t clamp, ggml_type type, bool small_rows) -> std::vector<uint32_t> {
  2010. // For large number of rows, 128 invocations seems to work best.
  2011. // For small number of rows (e.g. N==1), 256 works better. But matrix granularity for 256 is 32, so we
  2012. // can't use 256 for D==80.
  2013. // For scalar, use 128 (arbitrary)
  2014. // The same D_split value is used for both HSK and HSV, so just base it on the union of the LSBs.
  2015. const uint32_t D = (hsk|hsv);
  2016. uint32_t wg_size = (path == FA_SCALAR || path == FA_COOPMAT1)
  2017. ? scalar_flash_attention_workgroup_size
  2018. : ((small_rows && (D % 32) == 0) ? 256 : 128);
  2019. auto rows_cols = fa_rows_cols(path, hsk, hsv, clamp, type, small_rows);
  2020. // D_split can't be larger than a subgroup because we use subgroupShuffle to reduce it.
  2021. // D_split can't be larger than the LSB of D divided by 4 due to vectorization in the shader.
  2022. const uint32_t D_lsb = D ^ (D & (D-1));
  2023. uint32_t D_split = std::min(std::min(device->subgroup_size, 8u), D_lsb / 4);
  2024. // mask dim1 is padded to 64, we rely on this to avoid clamping mask loads
  2025. GGML_ASSERT((GGML_KQ_MASK_PAD % rows_cols[0]) == 0);
  2026. return {wg_size, rows_cols[0], rows_cols[1], hsk, hsv, clamp, D_split};
  2027. };
  2028. #define CREATE_FA(TYPE, NAMELC, FAPATH, SUFFIX) \
  2029. for (auto &fa : device->pipeline_flash_attn_f32_f16[TYPE]) { \
  2030. uint32_t HSK = fa.first.HSK; \
  2031. uint32_t HSV = fa.first.HSV; \
  2032. bool small_rows = fa.first.small_rows; \
  2033. FaCodePath path = fa.first.path; \
  2034. bool aligned = fa.first.aligned; \
  2035. bool f32acc = fa.first.f32acc; \
  2036. if (path == FAPATH) { \
  2037. if (aligned) { \
  2038. if (f32acc) { \
  2039. ggml_vk_create_pipeline(device, fa.second, "flash_attn_f32_f16_aligned_f32acc" #NAMELC, flash_attn_f32_f16_ ## NAMELC ## SUFFIX ## _len, flash_attn_f32_f16_ ## NAMELC ## SUFFIX ## _data, "main", 6, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(FAPATH, HSK,HSV,0,TYPE,small_rows), fa_spec_constants(FAPATH, HSK,HSV,0,TYPE,small_rows), fa_align(FAPATH,HSK,HSV,TYPE,small_rows), true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  2040. } else { \
  2041. ggml_vk_create_pipeline(device, fa.second, "flash_attn_f32_f16_aligned_f16acc" #NAMELC, flash_attn_f32_f16_ ## NAMELC ## _f16acc ## SUFFIX ## _len, flash_attn_f32_f16_ ## NAMELC ## _f16acc ## SUFFIX ## _data, "main", 6, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(FAPATH, HSK,HSV,0,TYPE,small_rows), fa_spec_constants(FAPATH, HSK,HSV,0,TYPE,small_rows), fa_align(FAPATH,HSK,HSV,TYPE,small_rows), true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  2042. } \
  2043. } else { \
  2044. if (f32acc) { \
  2045. ggml_vk_create_pipeline(device, fa.second, "flash_attn_f32_f16_f32acc" #NAMELC, flash_attn_f32_f16_ ## NAMELC ## SUFFIX ## _len, flash_attn_f32_f16_ ## NAMELC ## SUFFIX ## _data, "main", 6, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(FAPATH, HSK,HSV,1,TYPE,small_rows), fa_spec_constants(FAPATH, HSK,HSV,1,TYPE,small_rows), 1, true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  2046. } else { \
  2047. ggml_vk_create_pipeline(device, fa.second, "flash_attn_f32_f16_f16acc" #NAMELC, flash_attn_f32_f16_ ## NAMELC ## _f16acc ## SUFFIX ## _len, flash_attn_f32_f16_ ## NAMELC ## _f16acc ## SUFFIX ## _data, "main", 6, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(FAPATH, HSK,HSV,1,TYPE,small_rows), fa_spec_constants(FAPATH, HSK,HSV,1,TYPE,small_rows), 1, true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  2048. } \
  2049. } \
  2050. } \
  2051. }
  2052. CREATE_FA(GGML_TYPE_F16, f16, FA_SCALAR, )
  2053. CREATE_FA(GGML_TYPE_Q4_0, q4_0, FA_SCALAR, )
  2054. CREATE_FA(GGML_TYPE_Q8_0, q8_0, FA_SCALAR, )
  2055. #if defined(VK_KHR_cooperative_matrix) && defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  2056. if (device->coopmat1_fa_support) {
  2057. CREATE_FA(GGML_TYPE_F16, f16, FA_COOPMAT1, _cm1)
  2058. CREATE_FA(GGML_TYPE_Q4_0, q4_0, FA_COOPMAT1, _cm1)
  2059. CREATE_FA(GGML_TYPE_Q8_0, q8_0, FA_COOPMAT1, _cm1)
  2060. }
  2061. #endif
  2062. #if defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  2063. if (device->coopmat2) {
  2064. CREATE_FA(GGML_TYPE_F16, f16, FA_COOPMAT2, _cm2)
  2065. CREATE_FA(GGML_TYPE_Q4_0, q4_0, FA_COOPMAT2, _cm2)
  2066. CREATE_FA(GGML_TYPE_Q4_1, q4_1, FA_COOPMAT2, _cm2)
  2067. CREATE_FA(GGML_TYPE_Q5_0, q5_0, FA_COOPMAT2, _cm2)
  2068. CREATE_FA(GGML_TYPE_Q5_1, q5_1, FA_COOPMAT2, _cm2)
  2069. CREATE_FA(GGML_TYPE_Q8_0, q8_0, FA_COOPMAT2, _cm2)
  2070. CREATE_FA(GGML_TYPE_IQ4_NL, iq4_nl, FA_COOPMAT2, _cm2)
  2071. }
  2072. #endif
  2073. #undef CREATE_FA
  2074. #if defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  2075. if (device->coopmat2) {
  2076. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  2077. #define CREATE_MM(PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  2078. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->l, #NAMELC #F16ACC "_l", NAMELC ## F16ACC ## _cm2_len, NAMELC ## F16ACC ## _cm2_data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, 1); \
  2079. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->m, #NAMELC #F16ACC "_m", NAMELC ## F16ACC ## _cm2_len, NAMELC ## F16ACC ## _cm2_data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, 1); \
  2080. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->s, #NAMELC #F16ACC "_s", NAMELC ## F16ACC ## _cm2_len, NAMELC ## F16ACC ## _cm2_data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, 1); \
  2081. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_l, #NAMELC #F16ACC "_aligned_l", NAMELC ## _aligned ## F16ACC ## _cm2_len, NAMELC ## _aligned ## F16ACC ## _cm2_data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, l_align); \
  2082. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_m, #NAMELC #F16ACC "_aligned_m", NAMELC ## _aligned ## F16ACC ## _cm2_len, NAMELC ## _aligned ## F16ACC ## _cm2_data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, m_align); \
  2083. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_s, #NAMELC #F16ACC "_aligned_s", NAMELC ## _aligned ## F16ACC ## _cm2_len, NAMELC ## _aligned ## F16ACC ## _cm2_data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, s_align); \
  2084. // Create 2 variants, {f16,f32} accumulator
  2085. #define CREATE_MM2(PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  2086. CREATE_MM(PIPELINE_NAME . f16acc, NAMELC, _f16acc, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  2087. CREATE_MM(PIPELINE_NAME . f32acc, NAMELC, , WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  2088. CREATE_MM2(pipeline_matmul_f16, matmul_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 3)
  2089. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  2090. if (device->coopmat_bf16_support) {
  2091. CREATE_MM(pipeline_matmul_bf16, matmul_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3)
  2092. }
  2093. #endif
  2094. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q4_0], matmul_q4_0_f16, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  2095. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q4_1], matmul_q4_1_f16, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  2096. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q5_0], matmul_q5_0_f16, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  2097. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q5_1], matmul_q5_1_f16, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  2098. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q8_0], matmul_q8_0_f16, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  2099. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q2_K], matmul_q2_k_f16, mmq_wg_denoms_k, warptile_mmq_k, vk_mat_mat_push_constants, 3)
  2100. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q3_K], matmul_q3_k_f16, mmq_wg_denoms_k, warptile_mmq_k, vk_mat_mat_push_constants, 3)
  2101. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q4_K], matmul_q4_k_f16, mmq_wg_denoms_k, warptile_mmq_k, vk_mat_mat_push_constants, 3)
  2102. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q5_K], matmul_q5_k_f16, mmq_wg_denoms_k, warptile_mmq_k, vk_mat_mat_push_constants, 3)
  2103. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q6_K], matmul_q6_k_f16, mmq_wg_denoms_k, warptile_mmq_k, vk_mat_mat_push_constants, 3)
  2104. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ1_S], matmul_iq1_s_f16, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  2105. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ1_M], matmul_iq1_m_f16, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  2106. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ2_XXS], matmul_iq2_xxs_f16, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  2107. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ2_XS], matmul_iq2_xs_f16, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  2108. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ2_S], matmul_iq2_s_f16, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  2109. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ3_XXS], matmul_iq3_xxs_f16, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  2110. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ3_S], matmul_iq3_s_f16, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  2111. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ4_XS], matmul_iq4_xs_f16, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  2112. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ4_NL], matmul_iq4_nl_f16, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  2113. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_MXFP4], matmul_mxfp4_f16, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  2114. GGML_ASSERT(device->subgroup_ballot);
  2115. CREATE_MM2(pipeline_matmul_id_f16, matmul_id_subgroup_f16, wg_denoms, warptile, vk_mat_mat_id_push_constants, 4)
  2116. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  2117. if (device->coopmat_bf16_support) {
  2118. CREATE_MM(pipeline_matmul_id_bf16, matmul_id_subgroup_bf16, , wg_denoms, warptile, vk_mat_mat_id_push_constants, 4)
  2119. }
  2120. #endif
  2121. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0], matmul_id_subgroup_q4_0_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2122. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1], matmul_id_subgroup_q4_1_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2123. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0], matmul_id_subgroup_q5_0_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2124. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1], matmul_id_subgroup_q5_1_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2125. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0], matmul_id_subgroup_q8_0_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2126. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K], matmul_id_subgroup_q2_k_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2127. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K], matmul_id_subgroup_q3_k_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2128. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K], matmul_id_subgroup_q4_k_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2129. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K], matmul_id_subgroup_q5_k_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2130. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K], matmul_id_subgroup_q6_k_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2131. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S], matmul_id_subgroup_iq1_s_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2132. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M], matmul_id_subgroup_iq1_m_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2133. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS], matmul_id_subgroup_iq2_xxs_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2134. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS], matmul_id_subgroup_iq2_xs_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2135. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S], matmul_id_subgroup_iq2_s_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2136. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS], matmul_id_subgroup_iq3_xxs_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2137. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S], matmul_id_subgroup_iq3_s_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2138. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS], matmul_id_subgroup_iq4_xs_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2139. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL], matmul_id_subgroup_iq4_nl_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2140. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_MXFP4], matmul_id_subgroup_mxfp4_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2141. #undef CREATE_MM
  2142. #undef CREATE_MM2
  2143. } else
  2144. #endif // defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  2145. #if defined(VK_KHR_cooperative_matrix) && defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  2146. if (device->coopmat_support) {
  2147. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  2148. #define CREATE_MM(TYPE, PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2149. if (device->mul_mat ## ID ## _l[TYPE]) \
  2150. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->l, #NAMELC #F16ACC "_l", NAMELC ## F16ACC ## _cm1_len, NAMELC ## F16ACC ## _cm1_data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, 1, false, true); \
  2151. if (device->mul_mat ## ID ## _m[TYPE]) \
  2152. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->m, #NAMELC #F16ACC "_m", NAMELC ## F16ACC ## _cm1_len, NAMELC ## F16ACC ## _cm1_data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, 1, false, true); \
  2153. if (device->mul_mat ## ID ## _s[TYPE]) \
  2154. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->s, #NAMELC #F16ACC "_s", NAMELC ## F16ACC ## _cm1_len, NAMELC ## F16ACC ## _cm1_data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, 1, false, true); \
  2155. if (device->mul_mat ## ID ## _l[TYPE]) \
  2156. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_l, #NAMELC #F16ACC "_aligned_l", NAMELC ## _aligned ## F16ACC ## _cm1_len, NAMELC ## _aligned ## F16ACC ## _cm1_data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, l_align, false, true); \
  2157. if (device->mul_mat ## ID ## _m[TYPE]) \
  2158. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_m, #NAMELC #F16ACC "_aligned_m", NAMELC ## _aligned ## F16ACC ## _cm1_len, NAMELC ## _aligned ## F16ACC ## _cm1_data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, m_align, false, true); \
  2159. if (device->mul_mat ## ID ## _s[TYPE]) \
  2160. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_s, #NAMELC #F16ACC "_aligned_s", NAMELC ## _aligned ## F16ACC ## _cm1_len, NAMELC ## _aligned ## F16ACC ## _cm1_data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, s_align, false, true); \
  2161. // Create 2 variants, {f16,f32} accumulator
  2162. #define CREATE_MM2(TYPE, PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2163. if (device->coopmat_acc_f16_support) { \
  2164. CREATE_MM(TYPE, PIPELINE_NAME . f16acc, NAMELC, _f16acc, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2165. } \
  2166. if (device->coopmat_acc_f32_support) { \
  2167. CREATE_MM(TYPE, PIPELINE_NAME . f32acc, NAMELC, , WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2168. } \
  2169. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32, matmul_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2170. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32_f16, matmul_f32_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2171. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16, matmul_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2172. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16_f32, matmul_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2173. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  2174. if (device->coopmat_bf16_support) {
  2175. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_bf16, matmul_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, )
  2176. }
  2177. #endif
  2178. if (device->coopmat_acc_f16_support) {
  2179. CREATE_MM2(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0], matmul_q4_0_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2180. CREATE_MM2(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1], matmul_q4_1_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2181. CREATE_MM2(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0], matmul_q5_0_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2182. CREATE_MM2(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1], matmul_q5_1_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2183. CREATE_MM2(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0], matmul_q8_0_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2184. CREATE_MM2(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K], matmul_q2_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2185. CREATE_MM2(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K], matmul_q3_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2186. CREATE_MM2(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K], matmul_q4_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2187. CREATE_MM2(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K], matmul_q5_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2188. CREATE_MM2(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K], matmul_q6_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2189. CREATE_MM2(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_S], matmul_iq1_s_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2190. CREATE_MM2(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_M], matmul_iq1_m_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2191. CREATE_MM2(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XXS], matmul_iq2_xxs_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2192. CREATE_MM2(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XS], matmul_iq2_xs_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2193. CREATE_MM2(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_S], matmul_iq2_s_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2194. CREATE_MM2(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_XXS], matmul_iq3_xxs_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2195. CREATE_MM2(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_S], matmul_iq3_s_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2196. CREATE_MM2(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_XS], matmul_iq4_xs_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2197. CREATE_MM2(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_NL], matmul_iq4_nl_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2198. CREATE_MM2(GGML_TYPE_MXFP4, pipeline_dequant_mul_mat_mat[GGML_TYPE_MXFP4], matmul_mxfp4_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2199. } else {
  2200. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0].f32acc, matmul_q4_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2201. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1].f32acc, matmul_q4_1_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2202. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0].f32acc, matmul_q5_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2203. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1].f32acc, matmul_q5_1_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2204. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0].f32acc, matmul_q8_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2205. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K].f32acc, matmul_q2_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2206. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K].f32acc, matmul_q3_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2207. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K].f32acc, matmul_q4_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2208. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K].f32acc, matmul_q5_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2209. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K].f32acc, matmul_q6_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2210. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_S].f32acc, matmul_iq1_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2211. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_M].f32acc, matmul_iq1_m_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2212. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XXS].f32acc, matmul_iq2_xxs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2213. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XS].f32acc, matmul_iq2_xs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2214. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_S].f32acc, matmul_iq2_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2215. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_XXS].f32acc, matmul_iq3_xxs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2216. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_S].f32acc, matmul_iq3_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2217. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_XS].f32acc, matmul_iq4_xs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2218. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_NL].f32acc, matmul_iq4_nl_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2219. CREATE_MM(GGML_TYPE_MXFP4, pipeline_dequant_mul_mat_mat[GGML_TYPE_MXFP4].f32acc, matmul_mxfp4_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2220. }
  2221. GGML_ASSERT(device->subgroup_ballot);
  2222. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_id_f32, matmul_id_subgroup_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2223. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16, matmul_id_subgroup_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2224. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16_f32, matmul_id_subgroup_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2225. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  2226. if (device->coopmat_bf16_support) {
  2227. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_id_bf16, matmul_id_subgroup_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2228. }
  2229. #endif
  2230. CREATE_MM2(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0], matmul_id_subgroup_q4_0_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2231. CREATE_MM2(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1], matmul_id_subgroup_q4_1_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2232. CREATE_MM2(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0], matmul_id_subgroup_q5_0_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2233. CREATE_MM2(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1], matmul_id_subgroup_q5_1_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2234. CREATE_MM2(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0], matmul_id_subgroup_q8_0_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2235. CREATE_MM2(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K], matmul_id_subgroup_q2_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2236. CREATE_MM2(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K], matmul_id_subgroup_q3_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2237. CREATE_MM2(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K], matmul_id_subgroup_q4_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2238. CREATE_MM2(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K], matmul_id_subgroup_q5_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2239. CREATE_MM2(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K], matmul_id_subgroup_q6_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2240. CREATE_MM2(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S], matmul_id_subgroup_iq1_s_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2241. CREATE_MM2(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M], matmul_id_subgroup_iq1_m_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2242. CREATE_MM2(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS], matmul_id_subgroup_iq2_xxs_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2243. CREATE_MM2(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS], matmul_id_subgroup_iq2_xs_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2244. CREATE_MM2(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S], matmul_id_subgroup_iq2_s_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2245. CREATE_MM2(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS], matmul_id_subgroup_iq3_xxs_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2246. CREATE_MM2(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S], matmul_id_subgroup_iq3_s_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2247. CREATE_MM2(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS], matmul_id_subgroup_iq4_xs_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2248. CREATE_MM2(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL], matmul_id_subgroup_iq4_nl_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2249. CREATE_MM2(GGML_TYPE_MXFP4, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_MXFP4], matmul_id_subgroup_mxfp4_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2250. #undef CREATE_MM2
  2251. #undef CREATE_MM
  2252. } else
  2253. #endif // defined(VK_KHR_cooperative_matrix) && defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  2254. if (device->fp16) {
  2255. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  2256. #define CREATE_MM(TYPE, PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID, REQSUBGROUPSIZE) \
  2257. if (device->mul_mat ## ID ## _l[TYPE]) \
  2258. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->l, #NAMELC #F16ACC "_l", NAMELC ## F16ACC ## _len, NAMELC ## F16ACC ## _data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, 1, false, REQSUBGROUPSIZE > 0, REQSUBGROUPSIZE); \
  2259. if (device->mul_mat ## ID ## _m[TYPE]) \
  2260. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->m, #NAMELC #F16ACC "_m", NAMELC ## F16ACC ## _len, NAMELC ## F16ACC ## _data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, 1, false, REQSUBGROUPSIZE > 0, REQSUBGROUPSIZE); \
  2261. if (device->mul_mat ## ID ## _s[TYPE]) \
  2262. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->s, #NAMELC #F16ACC "_s", NAMELC ## F16ACC ## _len, NAMELC ## F16ACC ## _data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, 1, false, REQSUBGROUPSIZE > 0, REQSUBGROUPSIZE); \
  2263. if (device->mul_mat ## ID ## _l[TYPE]) \
  2264. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_l, #NAMELC #F16ACC "_aligned_l", NAMELC ## _aligned ## F16ACC ## _len, NAMELC ## _aligned ## F16ACC ## _data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, l_align, false, REQSUBGROUPSIZE > 0, REQSUBGROUPSIZE); \
  2265. if (device->mul_mat ## ID ## _m[TYPE]) \
  2266. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_m, #NAMELC #F16ACC "_aligned_m", NAMELC ## _aligned ## F16ACC ## _len, NAMELC ## _aligned ## F16ACC ## _data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, m_align, false, REQSUBGROUPSIZE > 0, REQSUBGROUPSIZE); \
  2267. if (device->mul_mat ## ID ## _s[TYPE]) \
  2268. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_s, #NAMELC #F16ACC "_aligned_s", NAMELC ## _aligned ## F16ACC ## _len, NAMELC ## _aligned ## F16ACC ## _data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, s_align, false, REQSUBGROUPSIZE > 0, REQSUBGROUPSIZE); \
  2269. #define CREATE_MMQ(TYPE, PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2270. if (device->mul_mat ## ID ## _l[TYPE]) { \
  2271. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME .f16acc->l, #NAMELC "_f16acc_l", NAMELC ## _f16acc_len, NAMELC ## _f16acc_data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, 1); \
  2272. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME .f32acc->l, #NAMELC "_l", NAMELC ## _len, NAMELC ## _data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, 1); \
  2273. } \
  2274. if (device->mul_mat ## ID ## _m[TYPE]) { \
  2275. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME .f16acc->m, #NAMELC "_f16acc_m", NAMELC ## _f16acc_len, NAMELC ## _f16acc_data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, 1); \
  2276. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME .f32acc->m, #NAMELC "_m", NAMELC ## _len, NAMELC ## _data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, 1); \
  2277. } \
  2278. if (device->mul_mat ## ID ## _s[TYPE]) { \
  2279. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME .f16acc->s, #NAMELC "_f16acc_s", NAMELC ## _f16acc_len, NAMELC ## _f16acc_data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, 1); \
  2280. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME .f32acc->s, #NAMELC "_s", NAMELC ## _len, NAMELC ## _data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, 1); \
  2281. } \
  2282. // Create 2 variants, {f16,f32} accumulator
  2283. #define CREATE_MM2(TYPE, PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID, REQSUBGROUPSIZE) \
  2284. CREATE_MM(TYPE, PIPELINE_NAME . f16acc, NAMELC, _f16acc, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID, REQSUBGROUPSIZE) \
  2285. CREATE_MM(TYPE, PIPELINE_NAME . f32acc, NAMELC, , WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID, REQSUBGROUPSIZE) \
  2286. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32, matmul_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, , 0);
  2287. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32_f16, matmul_f32_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, , 0);
  2288. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16, matmul_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 3, , 0);
  2289. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16_f32, matmul_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 3, , 0);
  2290. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_bf16, matmul_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, , 0);
  2291. CREATE_MM2(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0], matmul_q4_0_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2292. CREATE_MM2(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1], matmul_q4_1_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2293. CREATE_MM2(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0], matmul_q5_0_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2294. CREATE_MM2(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1], matmul_q5_1_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2295. CREATE_MM2(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0], matmul_q8_0_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2296. CREATE_MM2(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K], matmul_q2_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2297. CREATE_MM2(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K], matmul_q3_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2298. CREATE_MM2(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K], matmul_q4_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2299. CREATE_MM2(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K], matmul_q5_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2300. CREATE_MM2(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K], matmul_q6_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2301. CREATE_MM2(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_S], matmul_iq1_s_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2302. CREATE_MM2(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_M], matmul_iq1_m_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2303. CREATE_MM2(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XXS], matmul_iq2_xxs_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2304. CREATE_MM2(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XS], matmul_iq2_xs_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2305. CREATE_MM2(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_S], matmul_iq2_s_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2306. CREATE_MM2(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_XXS], matmul_iq3_xxs_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2307. CREATE_MM2(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_S], matmul_iq3_s_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2308. CREATE_MM2(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_XS], matmul_iq4_xs_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2309. CREATE_MM2(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_NL], matmul_iq4_nl_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2310. CREATE_MM2(GGML_TYPE_MXFP4, pipeline_dequant_mul_mat_mat[GGML_TYPE_MXFP4], matmul_mxfp4_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2311. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  2312. if (device->integer_dot_product) {
  2313. CREATE_MMQ(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q4_0], matmul_q4_0_q8_1, mmq_wg_denoms, warptile_mmq_int, vk_mat_mat_push_constants, 3, );
  2314. CREATE_MMQ(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q4_1], matmul_q4_1_q8_1, mmq_wg_denoms, warptile_mmq_int, vk_mat_mat_push_constants, 3, );
  2315. CREATE_MMQ(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q5_0], matmul_q5_0_q8_1, mmq_wg_denoms, warptile_mmq_int, vk_mat_mat_push_constants, 3, );
  2316. CREATE_MMQ(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q5_1], matmul_q5_1_q8_1, mmq_wg_denoms, warptile_mmq_int, vk_mat_mat_push_constants, 3, );
  2317. CREATE_MMQ(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q8_0], matmul_q8_0_q8_1, mmq_wg_denoms, warptile_mmq_int, vk_mat_mat_push_constants, 3, );
  2318. }
  2319. #endif
  2320. if (device->subgroup_ballot && device->subgroup_require_full_support && subgroup_min_size_16) {
  2321. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_id_f32, matmul_id_subgroup_f32_f32, , wg_denoms, warptile_id, vk_mat_mat_push_constants, 4, _id, mul_mat_subgroup_size_16);
  2322. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16, matmul_id_subgroup_f16, wg_denoms, warptile_id, vk_mat_mat_push_constants, 4, _id, mul_mat_subgroup_size_16);
  2323. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16_f32, matmul_id_subgroup_f16_f32, wg_denoms, warptile_id, vk_mat_mat_push_constants, 4, _id, mul_mat_subgroup_size_16);
  2324. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_id_bf16, matmul_id_subgroup_bf16, , wg_denoms, warptile_id, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size_16);
  2325. CREATE_MM2(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0], matmul_id_subgroup_q4_0_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2326. CREATE_MM2(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1], matmul_id_subgroup_q4_1_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2327. CREATE_MM2(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0], matmul_id_subgroup_q5_0_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2328. CREATE_MM2(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1], matmul_id_subgroup_q5_1_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2329. CREATE_MM2(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0], matmul_id_subgroup_q8_0_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2330. CREATE_MM2(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K], matmul_id_subgroup_q2_k_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2331. CREATE_MM2(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K], matmul_id_subgroup_q3_k_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2332. CREATE_MM2(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K], matmul_id_subgroup_q4_k_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2333. CREATE_MM2(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K], matmul_id_subgroup_q5_k_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2334. CREATE_MM2(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K], matmul_id_subgroup_q6_k_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2335. CREATE_MM2(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S], matmul_id_subgroup_iq1_s_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2336. CREATE_MM2(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M], matmul_id_subgroup_iq1_m_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2337. CREATE_MM2(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS], matmul_id_subgroup_iq2_xxs_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2338. CREATE_MM2(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS], matmul_id_subgroup_iq2_xs_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2339. CREATE_MM2(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S], matmul_id_subgroup_iq2_s_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2340. CREATE_MM2(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS], matmul_id_subgroup_iq3_xxs_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2341. CREATE_MM2(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S], matmul_id_subgroup_iq3_s_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2342. CREATE_MM2(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS], matmul_id_subgroup_iq4_xs_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2343. CREATE_MM2(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL], matmul_id_subgroup_iq4_nl_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2344. CREATE_MM2(GGML_TYPE_MXFP4, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_MXFP4], matmul_id_subgroup_mxfp4_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2345. } else {
  2346. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_id_f32, matmul_id_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id, 0);
  2347. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16, matmul_id_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id, 0);
  2348. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16_f32, matmul_id_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id, 0);
  2349. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_id_bf16, matmul_id_bf16, , wg_denoms, warptile, vk_mat_mat_id_push_constants, 4, _id, 0);
  2350. CREATE_MM2(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0], matmul_id_q4_0_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2351. CREATE_MM2(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1], matmul_id_q4_1_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2352. CREATE_MM2(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0], matmul_id_q5_0_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2353. CREATE_MM2(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1], matmul_id_q5_1_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2354. CREATE_MM2(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0], matmul_id_q8_0_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2355. CREATE_MM2(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K], matmul_id_q2_k_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2356. CREATE_MM2(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K], matmul_id_q3_k_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2357. CREATE_MM2(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K], matmul_id_q4_k_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2358. CREATE_MM2(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K], matmul_id_q5_k_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2359. CREATE_MM2(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K], matmul_id_q6_k_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2360. CREATE_MM2(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S], matmul_id_iq1_s_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2361. CREATE_MM2(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M], matmul_id_iq1_m_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2362. CREATE_MM2(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS], matmul_id_iq2_xxs_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2363. CREATE_MM2(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS], matmul_id_iq2_xs_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2364. CREATE_MM2(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S], matmul_id_iq2_s_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2365. CREATE_MM2(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS], matmul_id_iq3_xxs_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2366. CREATE_MM2(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S], matmul_id_iq3_s_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2367. CREATE_MM2(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS], matmul_id_iq4_xs_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2368. CREATE_MM2(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL], matmul_id_iq4_nl_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2369. CREATE_MM2(GGML_TYPE_MXFP4, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_MXFP4], matmul_id_mxfp4_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2370. }
  2371. #undef CREATE_MM2
  2372. #undef CREATE_MMQ
  2373. #undef CREATE_MM
  2374. } else {
  2375. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  2376. #define CREATE_MM(TYPE, PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID, REQSUBGROUPSIZE) \
  2377. if (device->mul_mat ## ID ## _l[TYPE]) \
  2378. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->l, #NAMELC #F16ACC "_l", NAMELC ## F16ACC ## _fp32_len, NAMELC ## F16ACC ## _fp32_data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, 1, false, REQSUBGROUPSIZE > 0, REQSUBGROUPSIZE); \
  2379. if (device->mul_mat ## ID ## _m[TYPE]) \
  2380. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->m, #NAMELC #F16ACC "_m", NAMELC ## F16ACC ## _fp32_len, NAMELC ## F16ACC ## _fp32_data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, 1, false, REQSUBGROUPSIZE > 0, REQSUBGROUPSIZE); \
  2381. if (device->mul_mat ## ID ## _s[TYPE]) \
  2382. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->s, #NAMELC #F16ACC "_s", NAMELC ## F16ACC ## _fp32_len, NAMELC ## F16ACC ## _fp32_data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, 1, false, REQSUBGROUPSIZE > 0, REQSUBGROUPSIZE); \
  2383. if (device->mul_mat ## ID ## _l[TYPE]) \
  2384. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_l, #NAMELC #F16ACC "_aligned_l", NAMELC ## _aligned ## F16ACC ## _fp32_len, NAMELC ## _aligned ## F16ACC ## _fp32_data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, l_align, false, REQSUBGROUPSIZE > 0, REQSUBGROUPSIZE); \
  2385. if (device->mul_mat ## ID ## _m[TYPE]) \
  2386. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_m, #NAMELC #F16ACC "_aligned_m", NAMELC ## _aligned ## F16ACC ## _fp32_len, NAMELC ## _aligned ## F16ACC ## _fp32_data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, m_align, false, REQSUBGROUPSIZE > 0, REQSUBGROUPSIZE); \
  2387. if (device->mul_mat ## ID ## _s[TYPE]) \
  2388. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_s, #NAMELC #F16ACC "_aligned_s", NAMELC ## _aligned ## F16ACC ## _fp32_len, NAMELC ## _aligned ## F16ACC ## _fp32_data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, s_align, false, REQSUBGROUPSIZE > 0, REQSUBGROUPSIZE); \
  2389. #define CREATE_MMQ(TYPE, PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2390. if (device->mul_mat ## ID ## _l[TYPE]) \
  2391. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->l, #NAMELC "_l", NAMELC ## _fp32_len, NAMELC ## _fp32_data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, 1); \
  2392. if (device->mul_mat ## ID ## _m[TYPE]) \
  2393. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->m, #NAMELC "_m", NAMELC ## _fp32_len, NAMELC ## _fp32_data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, 1); \
  2394. if (device->mul_mat ## ID ## _s[TYPE]) \
  2395. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->s, #NAMELC "_s", NAMELC ## _fp32_len, NAMELC ## _fp32_data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, 1); \
  2396. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32, matmul_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, , 0);
  2397. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32_f16, matmul_f32_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, , 0);
  2398. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_f16.f32acc, matmul_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, , 0);
  2399. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_f16_f32.f32acc, matmul_f16_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, , 0);
  2400. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_bf16, matmul_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, , 0);
  2401. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0].f32acc, matmul_q4_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2402. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1].f32acc, matmul_q4_1_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2403. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0].f32acc, matmul_q5_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2404. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1].f32acc, matmul_q5_1_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2405. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0].f32acc, matmul_q8_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2406. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K].f32acc, matmul_q2_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2407. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K].f32acc, matmul_q3_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2408. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K].f32acc, matmul_q4_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2409. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K].f32acc, matmul_q5_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2410. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K].f32acc, matmul_q6_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2411. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_S].f32acc, matmul_iq1_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2412. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_M].f32acc, matmul_iq1_m_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2413. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XXS].f32acc, matmul_iq2_xxs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2414. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XS].f32acc, matmul_iq2_xs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2415. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_S].f32acc, matmul_iq2_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2416. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_XXS].f32acc, matmul_iq3_xxs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2417. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_S].f32acc, matmul_iq3_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2418. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_XS].f32acc, matmul_iq4_xs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2419. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_NL].f32acc, matmul_iq4_nl_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2420. CREATE_MM(GGML_TYPE_MXFP4, pipeline_dequant_mul_mat_mat[GGML_TYPE_MXFP4].f32acc, matmul_mxfp4_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2421. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  2422. if (device->integer_dot_product) {
  2423. CREATE_MMQ(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q4_0].f32acc, matmul_q4_0_q8_1, mmq_wg_denoms, warptile_mmq_int, vk_mat_mat_push_constants, 3, );
  2424. CREATE_MMQ(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q4_1].f32acc, matmul_q4_1_q8_1, mmq_wg_denoms, warptile_mmq_int, vk_mat_mat_push_constants, 3, );
  2425. CREATE_MMQ(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q5_0].f32acc, matmul_q5_0_q8_1, mmq_wg_denoms, warptile_mmq_int, vk_mat_mat_push_constants, 3, );
  2426. CREATE_MMQ(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q5_1].f32acc, matmul_q5_1_q8_1, mmq_wg_denoms, warptile_mmq_int, vk_mat_mat_push_constants, 3, );
  2427. CREATE_MMQ(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q8_0].f32acc, matmul_q8_0_q8_1, mmq_wg_denoms, warptile_mmq_int, vk_mat_mat_push_constants, 3, );
  2428. }
  2429. #endif
  2430. if (device->subgroup_ballot && device->subgroup_require_full_support && subgroup_min_size_16) {
  2431. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_id_f32, matmul_id_subgroup_f32_f32, , wg_denoms, warptile_id, vk_mat_mat_push_constants, 4, _id, mul_mat_subgroup_size_16);
  2432. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_id_f16.f32acc, matmul_id_subgroup_f16, , wg_denoms, warptile_id, vk_mat_mat_push_constants, 4, _id, mul_mat_subgroup_size_16);
  2433. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_id_f16_f32.f32acc, matmul_id_subgroup_f16_f32, , wg_denoms, warptile_id, vk_mat_mat_push_constants, 4, _id, mul_mat_subgroup_size_16);
  2434. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_id_bf16, matmul_id_subgroup_bf16, , wg_denoms, warptile_id, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size_16);
  2435. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0].f32acc, matmul_id_subgroup_q4_0_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2436. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1].f32acc, matmul_id_subgroup_q4_1_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2437. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0].f32acc, matmul_id_subgroup_q5_0_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2438. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1].f32acc, matmul_id_subgroup_q5_1_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2439. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0].f32acc, matmul_id_subgroup_q8_0_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2440. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K].f32acc, matmul_id_subgroup_q2_k_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2441. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K].f32acc, matmul_id_subgroup_q3_k_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2442. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K].f32acc, matmul_id_subgroup_q4_k_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2443. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K].f32acc, matmul_id_subgroup_q5_k_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2444. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K].f32acc, matmul_id_subgroup_q6_k_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2445. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S].f32acc, matmul_id_subgroup_iq1_s_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2446. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M].f32acc, matmul_id_subgroup_iq1_m_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2447. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS].f32acc, matmul_id_subgroup_iq2_xxs_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2448. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS].f32acc, matmul_id_subgroup_iq2_xs_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2449. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S].f32acc, matmul_id_subgroup_iq2_s_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2450. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS].f32acc, matmul_id_subgroup_iq3_xxs_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2451. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S].f32acc, matmul_id_subgroup_iq3_s_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2452. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS].f32acc, matmul_id_subgroup_iq4_xs_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2453. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL].f32acc, matmul_id_subgroup_iq4_nl_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2454. CREATE_MM(GGML_TYPE_MXFP4, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_MXFP4].f32acc, matmul_id_subgroup_mxfp4_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2455. } else {
  2456. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_id_f32, matmul_id_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id, 0);
  2457. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_id_f16.f32acc, matmul_id_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id, 0);
  2458. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_id_f16_f32.f32acc, matmul_id_f16_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id, 0);
  2459. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_id_bf16, matmul_id_bf16, , wg_denoms, warptile, vk_mat_mat_id_push_constants, 4, _id, 0);
  2460. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0].f32acc, matmul_id_q4_0_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2461. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1].f32acc, matmul_id_q4_1_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2462. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0].f32acc, matmul_id_q5_0_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2463. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1].f32acc, matmul_id_q5_1_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2464. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0].f32acc, matmul_id_q8_0_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2465. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K].f32acc, matmul_id_q2_k_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2466. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K].f32acc, matmul_id_q3_k_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2467. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K].f32acc, matmul_id_q4_k_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2468. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K].f32acc, matmul_id_q5_k_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2469. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K].f32acc, matmul_id_q6_k_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2470. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S].f32acc, matmul_id_iq1_s_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2471. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M].f32acc, matmul_id_iq1_m_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2472. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS].f32acc, matmul_id_iq2_xxs_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2473. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS].f32acc, matmul_id_iq2_xs_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2474. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S].f32acc, matmul_id_iq2_s_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2475. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS].f32acc, matmul_id_iq3_xxs_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2476. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S].f32acc, matmul_id_iq3_s_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2477. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS].f32acc, matmul_id_iq4_xs_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2478. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL].f32acc, matmul_id_iq4_nl_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2479. CREATE_MM(GGML_TYPE_MXFP4, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_MXFP4].f32acc, matmul_id_mxfp4_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2480. }
  2481. }
  2482. // reusing CREATE_MM from the fp32 path
  2483. if ((device->coopmat2 || device->coopmat_support)
  2484. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  2485. && !device->coopmat_bf16_support
  2486. #endif
  2487. ) {
  2488. // use scalar tile sizes
  2489. l_warptile = { 128, 128, 128, 16, subgroup_size_8 * 2, 64, 2, 4, 4, 1, subgroup_size_8 };
  2490. m_warptile = { 128, 64, 64, 16, subgroup_size_8, 32, 2, 4, 2, 1, subgroup_size_8 };
  2491. s_warptile = { subgroup_size_16, 32, 32, 16, 32, 32, 2, 2, 2, 1, subgroup_size_8 };
  2492. l_wg_denoms = {128, 128, 1 };
  2493. m_wg_denoms = { 64, 64, 1 };
  2494. s_wg_denoms = { 32, 32, 1 };
  2495. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_bf16, matmul_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, , 0);
  2496. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_id_bf16, matmul_id_bf16, , wg_denoms, warptile, vk_mat_mat_id_push_constants, 4, _id, 0);
  2497. }
  2498. #undef CREATE_MM
  2499. // mul mat vec
  2500. // the number of rows computed per shader depends on GPU model and quant
  2501. uint32_t rm_stdq = 1;
  2502. uint32_t rm_kq = 2;
  2503. if (device->vendor_id == VK_VENDOR_ID_AMD) {
  2504. if (device->architecture == AMD_GCN) {
  2505. rm_stdq = 2;
  2506. rm_kq = 4;
  2507. }
  2508. } else if (device->vendor_id == VK_VENDOR_ID_INTEL)
  2509. rm_stdq = 2;
  2510. uint32_t rm_iq = 2 * rm_kq;
  2511. const bool use_subgroups = device->subgroup_arithmetic && device->architecture != vk_device_architecture::AMD_GCN;
  2512. // Ensure a subgroup size >= 16 is available
  2513. const bool use_subgroups16 = use_subgroups &&
  2514. (!device->subgroup_size_control && device->subgroup_size >= 16 ||
  2515. device->subgroup_size_control && device->subgroup_min_size <= 16 && device->subgroup_max_size >= 16);
  2516. const uint32_t subgroup_size = (device->vendor_id == VK_VENDOR_ID_INTEL && device->subgroup_size_control && device->subgroup_min_size <= 16 && device->subgroup_max_size >= 16) ? 16 : device->subgroup_size;
  2517. const uint32_t subgroup_size16 = std::max(subgroup_size, 16u);
  2518. const uint32_t force_subgroup_size = use_subgroups ? subgroup_size : 0;
  2519. const uint32_t force_subgroup_size16 = use_subgroups16 ? subgroup_size16 : 0;
  2520. for (uint32_t w = 0; w < DMMV_WG_SIZE_COUNT; ++w) {
  2521. const uint32_t wg_size_subgroup = (w == DMMV_WG_SIZE_SUBGROUP) ? subgroup_size : (subgroup_size * 4);
  2522. const uint32_t wg_size_subgroup16 = (w == DMMV_WG_SIZE_SUBGROUP) ? subgroup_size16 : (subgroup_size16 * 4);
  2523. const shader_reduction_mode reduc = (use_subgroups && w == DMMV_WG_SIZE_SUBGROUP) ? SHADER_REDUCTION_MODE_SUBGROUP :
  2524. (use_subgroups && w == DMMV_WG_SIZE_LARGE) ? SHADER_REDUCTION_MODE_HYBRID :
  2525. SHADER_REDUCTION_MODE_SHMEM;
  2526. const shader_reduction_mode reduc16 = (use_subgroups16 && w == DMMV_WG_SIZE_SUBGROUP) ? SHADER_REDUCTION_MODE_SUBGROUP :
  2527. (use_subgroups16 && w == DMMV_WG_SIZE_LARGE) ? SHADER_REDUCTION_MODE_HYBRID :
  2528. SHADER_REDUCTION_MODE_SHMEM;
  2529. for (uint32_t i = 0; i < mul_mat_vec_max_cols; ++i) {
  2530. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_F32 ][i], "mul_mat_vec_f32_f32_f32", arr_dmmv_f32_f32_f32_len[reduc], arr_dmmv_f32_f32_f32_data[reduc], "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {wg_size_subgroup, 2, i+1}, 1, false, use_subgroups, force_subgroup_size);
  2531. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_F16 ][i], "mul_mat_vec_f16_f32_f32", arr_dmmv_f16_f32_f32_len[reduc], arr_dmmv_f16_f32_f32_data[reduc], "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {wg_size_subgroup, 2, i+1}, 1, false, use_subgroups, force_subgroup_size);
  2532. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_BF16][i], "mul_mat_vec_bf16_f32_f32", arr_dmmv_bf16_f32_f32_len[reduc], arr_dmmv_bf16_f32_f32_data[reduc], "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {wg_size_subgroup, 2, i+1}, 1, false, use_subgroups, force_subgroup_size);
  2533. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_Q4_0][i], "mul_mat_vec_q4_0_f32_f32", arr_dmmv_q4_0_f32_f32_len[reduc], arr_dmmv_q4_0_f32_f32_data[reduc], "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {wg_size_subgroup, 2*rm_stdq, i+1}, 1, true, use_subgroups, force_subgroup_size);
  2534. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_Q4_1][i], "mul_mat_vec_q4_1_f32_f32", arr_dmmv_q4_1_f32_f32_len[reduc], arr_dmmv_q4_1_f32_f32_data[reduc], "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {wg_size_subgroup, 2*rm_stdq, i+1}, 1, true, use_subgroups, force_subgroup_size);
  2535. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_Q5_0][i], "mul_mat_vec_q5_0_f32_f32", arr_dmmv_q5_0_f32_f32_len[reduc], arr_dmmv_q5_0_f32_f32_data[reduc], "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {wg_size_subgroup, 2*rm_stdq, i+1}, 1, true, use_subgroups, force_subgroup_size);
  2536. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_Q5_1][i], "mul_mat_vec_q5_1_f32_f32", arr_dmmv_q5_1_f32_f32_len[reduc], arr_dmmv_q5_1_f32_f32_data[reduc], "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {wg_size_subgroup, 2*rm_stdq, i+1}, 1, true, use_subgroups, force_subgroup_size);
  2537. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_Q8_0][i], "mul_mat_vec_q8_0_f32_f32", arr_dmmv_q8_0_f32_f32_len[reduc], arr_dmmv_q8_0_f32_f32_data[reduc], "main", 3, sizeof(vk_mat_vec_push_constants), {1*rm_stdq, 1, 1}, {wg_size_subgroup, 1*rm_stdq, i+1}, 1, true, use_subgroups, force_subgroup_size);
  2538. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_Q2_K][i], "mul_mat_vec_q2_k_f32_f32", arr_dmmv_q2_k_f32_f32_len[reduc16], arr_dmmv_q2_k_f32_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {wg_size_subgroup16, rm_kq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2539. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_Q3_K][i], "mul_mat_vec_q3_k_f32_f32", arr_dmmv_q3_k_f32_f32_len[reduc16], arr_dmmv_q3_k_f32_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {wg_size_subgroup16, rm_kq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2540. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_Q4_K][i], "mul_mat_vec_q4_k_f32_f32", arr_dmmv_q4_k_f32_f32_len[reduc16], arr_dmmv_q4_k_f32_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {wg_size_subgroup16, rm_kq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2541. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_Q5_K][i], "mul_mat_vec_q5_k_f32_f32", arr_dmmv_q5_k_f32_f32_len[reduc16], arr_dmmv_q5_k_f32_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {wg_size_subgroup16, rm_kq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2542. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_Q6_K][i], "mul_mat_vec_q6_k_f32_f32", arr_dmmv_q6_k_f32_f32_len[reduc16], arr_dmmv_q6_k_f32_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {wg_size_subgroup16, rm_kq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2543. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_IQ1_S][i], "mul_mat_vec_iq1_s_f32_f32", arr_dmmv_iq1_s_f32_f32_len[reduc16], arr_dmmv_iq1_s_f32_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2544. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_IQ1_M][i], "mul_mat_vec_iq1_m_f32_f32", arr_dmmv_iq1_m_f32_f32_len[reduc16], arr_dmmv_iq1_m_f32_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2545. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_IQ2_XXS][i], "mul_mat_vec_iq2_xxs_f32_f32", arr_dmmv_iq2_xxs_f32_f32_len[reduc16], arr_dmmv_iq2_xxs_f32_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2546. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_IQ2_XS][i], "mul_mat_vec_iq2_xs_f32_f32", arr_dmmv_iq2_xs_f32_f32_len[reduc16], arr_dmmv_iq2_xs_f32_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2547. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_IQ2_S][i], "mul_mat_vec_iq2_s_f32_f32", arr_dmmv_iq2_s_f32_f32_len[reduc16], arr_dmmv_iq2_s_f32_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2548. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_IQ3_XXS][i], "mul_mat_vec_iq3_xxs_f32_f32", arr_dmmv_iq3_xxs_f32_f32_len[reduc16], arr_dmmv_iq3_xxs_f32_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2549. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_IQ3_S][i], "mul_mat_vec_iq3_s_f32_f32", arr_dmmv_iq3_s_f32_f32_len[reduc16], arr_dmmv_iq3_s_f32_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2550. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_IQ4_XS][i], "mul_mat_vec_iq4_xs_f32_f32", arr_dmmv_iq4_xs_f32_f32_len[reduc16], arr_dmmv_iq4_xs_f32_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2551. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_IQ4_NL][i], "mul_mat_vec_iq4_nl_f32_f32", arr_dmmv_iq4_nl_f32_f32_len[reduc16], arr_dmmv_iq4_nl_f32_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2552. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_MXFP4][i], "mul_mat_vec_mxfp4_f32_f32", arr_dmmv_mxfp4_f32_f32_len[reduc16], arr_dmmv_mxfp4_f32_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2553. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_F32 ][i], "mul_mat_vec_f32_f16_f32", arr_dmmv_f32_f16_f32_len[reduc], arr_dmmv_f32_f16_f32_data[reduc], "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {wg_size_subgroup, 2, i+1}, 1, false, use_subgroups, force_subgroup_size);
  2554. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_F16 ][i], "mul_mat_vec_f16_f16_f32", arr_dmmv_f16_f16_f32_len[reduc], arr_dmmv_f16_f16_f32_data[reduc], "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {wg_size_subgroup, 2, i+1}, 1, false, use_subgroups, force_subgroup_size);
  2555. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_BF16][i], "mul_mat_vec_bf16_f16_f32", arr_dmmv_bf16_f16_f32_len[reduc], arr_dmmv_bf16_f16_f32_data[reduc], "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {wg_size_subgroup, 2, i+1}, 1, false, use_subgroups, force_subgroup_size);
  2556. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_Q4_0][i], "mul_mat_vec_q4_0_f16_f32", arr_dmmv_q4_0_f16_f32_len[reduc], arr_dmmv_q4_0_f16_f32_data[reduc], "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {wg_size_subgroup, 2*rm_stdq, i+1}, 1, true, use_subgroups, force_subgroup_size);
  2557. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_Q4_1][i], "mul_mat_vec_q4_1_f16_f32", arr_dmmv_q4_1_f16_f32_len[reduc], arr_dmmv_q4_1_f16_f32_data[reduc], "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {wg_size_subgroup, 2*rm_stdq, i+1}, 1, true, use_subgroups, force_subgroup_size);
  2558. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_Q5_0][i], "mul_mat_vec_q5_0_f16_f32", arr_dmmv_q5_0_f16_f32_len[reduc], arr_dmmv_q5_0_f16_f32_data[reduc], "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {wg_size_subgroup, 2*rm_stdq, i+1}, 1, true, use_subgroups, force_subgroup_size);
  2559. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_Q5_1][i], "mul_mat_vec_q5_1_f16_f32", arr_dmmv_q5_1_f16_f32_len[reduc], arr_dmmv_q5_1_f16_f32_data[reduc], "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {wg_size_subgroup, 2*rm_stdq, i+1}, 1, true, use_subgroups, force_subgroup_size);
  2560. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_Q8_0][i], "mul_mat_vec_q8_0_f16_f32", arr_dmmv_q8_0_f16_f32_len[reduc], arr_dmmv_q8_0_f16_f32_data[reduc], "main", 3, sizeof(vk_mat_vec_push_constants), {1*rm_stdq, 1, 1}, {wg_size_subgroup, 1*rm_stdq, i+1}, 1, true, use_subgroups, force_subgroup_size);
  2561. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_Q2_K][i], "mul_mat_vec_q2_k_f16_f32", arr_dmmv_q2_k_f16_f32_len[reduc16], arr_dmmv_q2_k_f16_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {wg_size_subgroup16, rm_kq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2562. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_Q3_K][i], "mul_mat_vec_q3_k_f16_f32", arr_dmmv_q3_k_f16_f32_len[reduc16], arr_dmmv_q3_k_f16_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {wg_size_subgroup16, rm_kq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2563. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_Q4_K][i], "mul_mat_vec_q4_k_f16_f32", arr_dmmv_q4_k_f16_f32_len[reduc16], arr_dmmv_q4_k_f16_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {wg_size_subgroup16, rm_kq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2564. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_Q5_K][i], "mul_mat_vec_q5_k_f16_f32", arr_dmmv_q5_k_f16_f32_len[reduc16], arr_dmmv_q5_k_f16_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {wg_size_subgroup16, rm_kq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2565. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_Q6_K][i], "mul_mat_vec_q6_k_f16_f32", arr_dmmv_q6_k_f16_f32_len[reduc16], arr_dmmv_q6_k_f16_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {wg_size_subgroup16, rm_kq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2566. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_IQ1_S][i], "mul_mat_vec_iq1_s_f16_f32", arr_dmmv_iq1_s_f16_f32_len[reduc16], arr_dmmv_iq1_s_f16_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2567. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_IQ1_M][i], "mul_mat_vec_iq1_m_f16_f32", arr_dmmv_iq1_m_f16_f32_len[reduc16], arr_dmmv_iq1_m_f16_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2568. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_IQ2_XXS][i], "mul_mat_vec_iq2_xxs_f16_f32", arr_dmmv_iq2_xxs_f16_f32_len[reduc16], arr_dmmv_iq2_xxs_f16_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2569. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_IQ2_XS][i], "mul_mat_vec_iq2_xs_f16_f32", arr_dmmv_iq2_xs_f16_f32_len[reduc16], arr_dmmv_iq2_xs_f16_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2570. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_IQ2_S][i], "mul_mat_vec_iq2_s_f16_f32", arr_dmmv_iq2_s_f16_f32_len[reduc16], arr_dmmv_iq2_s_f16_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2571. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_IQ3_XXS][i], "mul_mat_vec_iq3_xxs_f16_f32", arr_dmmv_iq3_xxs_f16_f32_len[reduc16], arr_dmmv_iq3_xxs_f16_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2572. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_IQ3_S][i], "mul_mat_vec_iq3_s_f16_f32", arr_dmmv_iq3_s_f16_f32_len[reduc16], arr_dmmv_iq3_s_f16_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2573. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_IQ4_XS][i], "mul_mat_vec_iq4_xs_f16_f32", arr_dmmv_iq4_xs_f16_f32_len[reduc16], arr_dmmv_iq4_xs_f16_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2574. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_IQ4_NL][i], "mul_mat_vec_iq4_nl_f16_f32", arr_dmmv_iq4_nl_f16_f32_len[reduc16], arr_dmmv_iq4_nl_f16_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2575. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_MXFP4][i], "mul_mat_vec_mxfp4_f16_f32", arr_dmmv_mxfp4_f16_f32_len[reduc16], arr_dmmv_mxfp4_f16_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2576. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  2577. if (device->integer_dot_product) {
  2578. const uint32_t subgroup_size_int = (device->vendor_id == VK_VENDOR_ID_INTEL && device->subgroup_size_control) ? device->subgroup_min_size : device->subgroup_size;
  2579. const uint32_t wg_size_subgroup_int = (w == DMMV_WG_SIZE_SUBGROUP) ? subgroup_size_int : (subgroup_size_int * 4);
  2580. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_q8_1_f32[w][GGML_TYPE_Q4_0][i], "mul_mat_vec_q4_0_q8_1_f32", arr_dmmv_q4_0_q8_1_f32_len[reduc], arr_dmmv_q4_0_q8_1_f32_data[reduc], "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {wg_size_subgroup_int, 2*rm_stdq, i+1}, 1, true, use_subgroups, subgroup_size_int);
  2581. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_q8_1_f32[w][GGML_TYPE_Q4_1][i], "mul_mat_vec_q4_1_q8_1_f32", arr_dmmv_q4_1_q8_1_f32_len[reduc], arr_dmmv_q4_1_q8_1_f32_data[reduc], "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {wg_size_subgroup_int, 2*rm_stdq, i+1}, 1, true, use_subgroups, subgroup_size_int);
  2582. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_q8_1_f32[w][GGML_TYPE_Q5_0][i], "mul_mat_vec_q5_0_q8_1_f32", arr_dmmv_q5_0_q8_1_f32_len[reduc], arr_dmmv_q5_0_q8_1_f32_data[reduc], "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {wg_size_subgroup_int, 2*rm_stdq, i+1}, 1, true, use_subgroups, subgroup_size_int);
  2583. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_q8_1_f32[w][GGML_TYPE_Q5_1][i], "mul_mat_vec_q5_1_q8_1_f32", arr_dmmv_q5_1_q8_1_f32_len[reduc], arr_dmmv_q5_1_q8_1_f32_data[reduc], "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {wg_size_subgroup_int, 2*rm_stdq, i+1}, 1, true, use_subgroups, subgroup_size_int);
  2584. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_q8_1_f32[w][GGML_TYPE_Q8_0][i], "mul_mat_vec_q8_0_q8_1_f32", arr_dmmv_q8_0_q8_1_f32_len[reduc], arr_dmmv_q8_0_q8_1_f32_data[reduc], "main", 3, sizeof(vk_mat_vec_push_constants), {1*rm_stdq, 1, 1}, {wg_size_subgroup_int, 1*rm_stdq, i+1}, 1, true, use_subgroups, subgroup_size_int);
  2585. }
  2586. #endif // GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT
  2587. }
  2588. }
  2589. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_F32 ], "mul_mat_vec_id_f32_f32", mul_mat_vec_id_f32_f32_len, mul_mat_vec_id_f32_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {2, 1, 1}, {device->subgroup_size, 2}, 1);
  2590. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_F16 ], "mul_mat_vec_id_f16_f32", mul_mat_vec_id_f16_f32_len, mul_mat_vec_id_f16_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {2, 1, 1}, {device->subgroup_size, 2}, 1);
  2591. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_BF16], "mul_mat_vec_id_bf16_f32", mul_mat_vec_id_bf16_f32_len, mul_mat_vec_id_bf16_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {2, 1, 1}, {device->subgroup_size, 2}, 1);
  2592. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_Q4_0], "mul_mat_vec_id_q4_0_f32", mul_mat_vec_id_q4_0_f32_len, mul_mat_vec_id_q4_0_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq}, 1, true);
  2593. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_Q4_1], "mul_mat_vec_id_q4_1_f32", mul_mat_vec_id_q4_1_f32_len, mul_mat_vec_id_q4_1_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq}, 1, true);
  2594. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_Q5_0], "mul_mat_vec_id_q5_0_f32", mul_mat_vec_id_q5_0_f32_len, mul_mat_vec_id_q5_0_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq}, 1, true);
  2595. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_Q5_1], "mul_mat_vec_id_q5_1_f32", mul_mat_vec_id_q5_1_f32_len, mul_mat_vec_id_q5_1_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq}, 1, true);
  2596. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_Q8_0], "mul_mat_vec_id_q8_0_f32", mul_mat_vec_id_q8_0_f32_len, mul_mat_vec_id_q8_0_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {1*rm_stdq, 1, 1}, {device->subgroup_size, 1*rm_stdq}, 1, true);
  2597. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_Q2_K], "mul_mat_vec_id_q2_k_f32", mul_mat_vec_id_q2_k_f32_len, mul_mat_vec_id_q2_k_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq}, 1, true);
  2598. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_Q3_K], "mul_mat_vec_id_q3_k_f32", mul_mat_vec_id_q3_k_f32_len, mul_mat_vec_id_q3_k_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq}, 1, true);
  2599. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_Q4_K], "mul_mat_vec_id_q4_k_f32", mul_mat_vec_id_q4_k_f32_len, mul_mat_vec_id_q4_k_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq}, 1, true);
  2600. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_Q5_K], "mul_mat_vec_id_q5_k_f32", mul_mat_vec_id_q5_k_f32_len, mul_mat_vec_id_q5_k_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq}, 1, true);
  2601. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_Q6_K], "mul_mat_vec_id_q6_k_f32", mul_mat_vec_id_q6_k_f32_len, mul_mat_vec_id_q6_k_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq}, 1, true);
  2602. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_IQ1_S], "mul_mat_vec_id_iq1_s_f32", mul_mat_vec_id_iq1_s_f32_len, mul_mat_vec_id_iq1_s_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  2603. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_IQ1_M], "mul_mat_vec_id_iq1_m_f32", mul_mat_vec_id_iq1_m_f32_len, mul_mat_vec_id_iq1_m_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  2604. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_IQ2_XXS], "mul_mat_vec_id_iq2_xxs_f32", mul_mat_vec_id_iq2_xxs_f32_len, mul_mat_vec_id_iq2_xxs_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  2605. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_IQ2_XS], "mul_mat_vec_id_iq2_xs_f32", mul_mat_vec_id_iq2_xs_f32_len, mul_mat_vec_id_iq2_xs_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  2606. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_IQ2_S], "mul_mat_vec_id_iq2_s_f32", mul_mat_vec_id_iq2_s_f32_len, mul_mat_vec_id_iq2_s_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  2607. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_IQ3_XXS], "mul_mat_vec_id_iq3_xxs_f32", mul_mat_vec_id_iq3_xxs_f32_len, mul_mat_vec_id_iq3_xxs_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  2608. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_IQ3_S], "mul_mat_vec_id_iq3_s_f32", mul_mat_vec_id_iq3_s_f32_len, mul_mat_vec_id_iq3_s_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  2609. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_IQ4_XS], "mul_mat_vec_id_iq4_xs_f32", mul_mat_vec_id_iq4_xs_f32_len, mul_mat_vec_id_iq4_xs_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  2610. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_IQ4_NL], "mul_mat_vec_id_iq4_nl_f32", mul_mat_vec_id_iq4_nl_f32_len, mul_mat_vec_id_iq4_nl_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  2611. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_MXFP4], "mul_mat_vec_id_mxfp4_f32", mul_mat_vec_id_mxfp4_f32_len, mul_mat_vec_id_mxfp4_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  2612. // dequant shaders
  2613. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_F32 ], "f32_to_f16", dequant_f32_len, dequant_f32_data, "main", 2, 5 * sizeof(uint32_t), {256 * 16, 1, 1}, {}, 1);
  2614. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_Q4_0], "dequant_q4_0", dequant_q4_0_len, dequant_q4_0_data, "main", 2, 5 * sizeof(uint32_t), {256 * 16, 1, 1}, {}, 1);
  2615. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_Q4_1], "dequant_q4_1", dequant_q4_1_len, dequant_q4_1_data, "main", 2, 5 * sizeof(uint32_t), {256 * 16, 1, 1}, {}, 1);
  2616. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_Q5_0], "dequant_q5_0", dequant_q5_0_len, dequant_q5_0_data, "main", 2, 5 * sizeof(uint32_t), {256 * 16, 1, 1}, {}, 1);
  2617. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_Q5_1], "dequant_q5_1", dequant_q5_1_len, dequant_q5_1_data, "main", 2, 5 * sizeof(uint32_t), {256 * 16, 1, 1}, {}, 1);
  2618. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_Q8_0], "dequant_q8_0", dequant_q8_0_len, dequant_q8_0_data, "main", 2, 5 * sizeof(uint32_t), {256 * 16, 1, 1}, {}, 1);
  2619. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_Q2_K], "dequant_q2_k", dequant_q2_k_len, dequant_q2_k_data, "main", 2, 5 * sizeof(uint32_t), {256 * 64, 1, 1}, {}, 1);
  2620. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_Q3_K], "dequant_q3_k", dequant_q3_k_len, dequant_q3_k_data, "main", 2, 5 * sizeof(uint32_t), {256 * 64, 1, 1}, {}, 1);
  2621. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_Q4_K], "dequant_q4_k", dequant_q4_k_len, dequant_q4_k_data, "main", 2, 5 * sizeof(uint32_t), {256 * 32, 1, 1}, {}, 1);
  2622. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_Q5_K], "dequant_q5_k", dequant_q5_k_len, dequant_q5_k_data, "main", 2, 5 * sizeof(uint32_t), {256 * 64, 1, 1}, {}, 1);
  2623. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_Q6_K], "dequant_q6_k", dequant_q6_k_len, dequant_q6_k_data, "main", 2, 5 * sizeof(uint32_t), {256 * 64, 1, 1}, {}, 1);
  2624. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_IQ1_S], "dequant_iq1_s", dequant_iq1_s_len, dequant_iq1_s_data, "main", 2, 5 * sizeof(uint32_t), {256 * 32, 1, 1}, {}, 1);
  2625. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_IQ1_M], "dequant_iq1_m", dequant_iq1_m_len, dequant_iq1_m_data, "main", 2, 5 * sizeof(uint32_t), {256 * 32, 1, 1}, {}, 1);
  2626. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_IQ2_XXS], "dequant_iq2_xxs", dequant_iq2_xxs_len, dequant_iq2_xxs_data, "main", 2, 5 * sizeof(uint32_t), {256 * 32, 1, 1}, {}, 1);
  2627. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_IQ2_XS], "dequant_iq2_xs", dequant_iq2_xs_len, dequant_iq2_xs_data, "main", 2, 5 * sizeof(uint32_t), {256 * 32, 1, 1}, {}, 1);
  2628. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_IQ2_S], "dequant_iq2_s", dequant_iq2_s_len, dequant_iq2_s_data, "main", 2, 5 * sizeof(uint32_t), {256 * 32, 1, 1}, {}, 1);
  2629. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_IQ3_XXS], "dequant_iq3_xxs", dequant_iq3_xxs_len, dequant_iq3_xxs_data, "main", 2, 5 * sizeof(uint32_t), {256 * 32, 1, 1}, {}, 1);
  2630. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_IQ3_S], "dequant_iq3_s", dequant_iq3_s_len, dequant_iq3_s_data, "main", 2, 5 * sizeof(uint32_t), {256 * 32, 1, 1}, {}, 1);
  2631. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_IQ4_XS], "dequant_iq4_xs", dequant_iq4_xs_len, dequant_iq4_xs_data, "main", 2, 5 * sizeof(uint32_t), {256 * 32, 1, 1}, {}, 1);
  2632. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_IQ4_NL], "dequant_iq4_nl", dequant_iq4_nl_len, dequant_iq4_nl_data, "main", 2, 5 * sizeof(uint32_t), {256 * 16, 1, 1}, {}, 1);
  2633. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_MXFP4], "dequant_mxfp4", dequant_mxfp4_len, dequant_mxfp4_data, "main", 2, 5 * sizeof(uint32_t), {256 * 16, 1, 1}, {}, 1);
  2634. // get_rows
  2635. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_F32 ], "get_rows_f32", get_rows_f32_len, get_rows_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), { 512, 1, 1}, {}, 1);
  2636. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_F16 ], "get_rows_f16", get_rows_f16_len, get_rows_f16_data, "main", 3, sizeof(vk_op_binary_push_constants), { 512, 1, 1}, {}, 1);
  2637. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_BF16], "get_rows_bf16", get_rows_bf16_len, get_rows_bf16_data, "main", 3, sizeof(vk_op_binary_push_constants), { 512, 1, 1}, {}, 1);
  2638. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_Q4_0], "get_rows_q4_0", get_rows_q4_0_len, get_rows_q4_0_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2639. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_Q4_1], "get_rows_q4_1", get_rows_q4_1_len, get_rows_q4_1_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2640. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_Q5_0], "get_rows_q5_0", get_rows_q5_0_len, get_rows_q5_0_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2641. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_Q5_1], "get_rows_q5_1", get_rows_q5_1_len, get_rows_q5_1_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2642. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_Q8_0], "get_rows_q8_0", get_rows_q8_0_len, get_rows_q8_0_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2643. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_IQ1_S], "get_rows_iq1_s", get_rows_iq1_s_len, get_rows_iq1_s_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2644. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_IQ1_M], "get_rows_iq1_m", get_rows_iq1_m_len, get_rows_iq1_m_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2645. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_IQ2_XXS], "get_rows_iq2_xxs", get_rows_iq2_xxs_len, get_rows_iq2_xxs_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2646. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_IQ2_XS], "get_rows_iq2_xs", get_rows_iq2_xs_len, get_rows_iq2_xs_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2647. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_IQ2_S], "get_rows_iq2_s", get_rows_iq2_s_len, get_rows_iq2_s_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2648. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_IQ3_XXS], "get_rows_iq3_xxs", get_rows_iq3_xxs_len, get_rows_iq3_xxs_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2649. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_IQ3_S], "get_rows_iq3_s", get_rows_iq3_s_len, get_rows_iq3_s_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2650. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_IQ4_XS], "get_rows_iq4_xs", get_rows_iq4_xs_len, get_rows_iq4_xs_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2651. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_IQ4_NL], "get_rows_iq4_nl", get_rows_iq4_nl_len, get_rows_iq4_nl_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2652. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_MXFP4], "get_rows_mxfp4", get_rows_mxfp4_len, get_rows_mxfp4_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2653. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_F32 ], "get_rows_f32_f32", get_rows_f32_f32_len, get_rows_f32_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), { 512, 1, 1}, {}, 1);
  2654. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_F16 ], "get_rows_f16_f32", get_rows_f16_f32_len, get_rows_f16_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), { 512, 1, 1}, {}, 1);
  2655. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_BF16], "get_rows_bf16_f32", get_rows_bf16_f32_len, get_rows_bf16_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), { 512, 1, 1}, {}, 1);
  2656. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_Q4_0], "get_rows_q4_0_f32", get_rows_q4_0_f32_len, get_rows_q4_0_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2657. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_Q4_1], "get_rows_q4_1_f32", get_rows_q4_1_f32_len, get_rows_q4_1_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2658. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_Q5_0], "get_rows_q5_0_f32", get_rows_q5_0_f32_len, get_rows_q5_0_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2659. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_Q5_1], "get_rows_q5_1_f32", get_rows_q5_1_f32_len, get_rows_q5_1_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2660. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_Q8_0], "get_rows_q8_0_f32", get_rows_q8_0_f32_len, get_rows_q8_0_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2661. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_IQ1_S], "get_rows_iq1_s_f32", get_rows_iq1_s_f32_len, get_rows_iq1_s_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2662. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_IQ1_M], "get_rows_iq1_m_f32", get_rows_iq1_m_f32_len, get_rows_iq1_m_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2663. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_IQ2_XXS], "get_rows_iq2_xxs_f32", get_rows_iq2_xxs_f32_len, get_rows_iq2_xxs_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2664. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_IQ2_XS], "get_rows_iq2_xs_f32", get_rows_iq2_xs_f32_len, get_rows_iq2_xs_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2665. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_IQ2_S], "get_rows_iq2_s_f32", get_rows_iq2_s_f32_len, get_rows_iq2_s_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2666. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_IQ3_XXS], "get_rows_iq3_xxs_f32", get_rows_iq3_xxs_f32_len, get_rows_iq3_xxs_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2667. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_IQ3_S], "get_rows_iq3_s_f32", get_rows_iq3_s_f32_len, get_rows_iq3_s_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2668. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_IQ4_XS], "get_rows_iq4_xs_f32", get_rows_iq4_xs_f32_len, get_rows_iq4_xs_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2669. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_IQ4_NL], "get_rows_iq4_nl_f32", get_rows_iq4_nl_f32_len, get_rows_iq4_nl_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2670. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_MXFP4], "get_rows_mxfp4_f32", get_rows_mxfp4_f32_len, get_rows_mxfp4_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2671. ggml_vk_create_pipeline(device, device->pipeline_matmul_split_k_reduce, "split_k_reduce", split_k_reduce_len, split_k_reduce_data, "main", 2, 2 * sizeof(uint32_t), {256 * 4, 1, 1}, {}, 1);
  2672. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_split_k_reduce, "fa_split_k_reduce", fa_split_k_reduce_len, fa_split_k_reduce_data, "main", 3, 5 * sizeof(uint32_t), {1, device->subgroup_size, 1}, {device->subgroup_size}, 1, true);
  2673. if (device->subgroup_clustered && device->subgroup_require_full_support) {
  2674. ggml_vk_create_pipeline(device, device->pipeline_quantize_q8_1, "quantize_q8_1", quantize_q8_1_subgroup_len, quantize_q8_1_subgroup_data, "main", 2, 1 * sizeof(uint32_t), {32 * device->subgroup_size / 8, 1, 1}, { device->subgroup_size }, 1, true, true);
  2675. ggml_vk_create_pipeline(device, device->pipeline_quantize_q8_1_x4, "quantize_q8_1_x4", quantize_q8_1_x4_subgroup_len, quantize_q8_1_x4_subgroup_data, "main", 2, 1 * sizeof(uint32_t), {32 * device->subgroup_size / 8, 1, 1}, { device->subgroup_size }, 1, true, true);
  2676. } else {
  2677. ggml_vk_create_pipeline(device, device->pipeline_quantize_q8_1, "quantize_q8_1", quantize_q8_1_len, quantize_q8_1_data, "main", 2, 1 * sizeof(uint32_t), {32 * device->subgroup_size / 8, 1, 1}, { device->subgroup_size }, 1);
  2678. ggml_vk_create_pipeline(device, device->pipeline_quantize_q8_1_x4, "quantize_q8_1_x4", quantize_q8_1_x4_len, quantize_q8_1_x4_data, "main", 2, 1 * sizeof(uint32_t), {32 * device->subgroup_size / 8, 1, 1}, { device->subgroup_size }, 1);
  2679. }
  2680. for (uint32_t i = 0; i < p021_max_gqa_ratio; ++i) {
  2681. if (device->subgroup_arithmetic && device->subgroup_require_full_support) {
  2682. ggml_vk_create_pipeline(device, device->pipeline_mul_mat_vec_p021_f16_f32[i], "mul_mat_vec_p021_f16_f32"+std::to_string(i+1), mul_mat_vec_p021_f16_f32_subgroup_add_len, mul_mat_vec_p021_f16_f32_subgroup_add_data, "main", 3, 6 * sizeof(uint32_t), {1, 1, 1}, {device->subgroup_size, i + 1}, 1, true, true);
  2683. } else {
  2684. ggml_vk_create_pipeline(device, device->pipeline_mul_mat_vec_p021_f16_f32[i], "mul_mat_vec_p021_f16_f32"+std::to_string(i+1), mul_mat_vec_p021_f16_f32_len, mul_mat_vec_p021_f16_f32_data, "main", 3, 6 * sizeof(uint32_t), {1, 1, 1}, {device->subgroup_size, i + 1}, 1, true);
  2685. }
  2686. }
  2687. ggml_vk_create_pipeline(device, device->pipeline_mul_mat_vec_nc_f16_f32, "mul_mat_vec_nc_f16_f32", mul_mat_vec_nc_f16_f32_len, mul_mat_vec_nc_f16_f32_data, "main", 3, 12 * sizeof(uint32_t), {1, 1, 1}, {}, 1);
  2688. ggml_vk_create_pipeline(device, device->pipeline_norm_f32, "norm_f32", norm_f32_len, norm_f32_data, "main", 2, sizeof(vk_op_push_constants), {1, 1, 1}, {}, 1);
  2689. ggml_vk_create_pipeline(device, device->pipeline_group_norm_f32, "group_norm_f32", group_norm_f32_len, group_norm_f32_data, "main", 2, sizeof(vk_op_push_constants), {1, 1, 1}, {}, 1);
  2690. ggml_vk_create_pipeline(device, device->pipeline_rms_norm_f32, "rms_norm_f32", rms_norm_f32_len, rms_norm_f32_data, "main", 4, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {0, 0}, 1, true);
  2691. ggml_vk_create_pipeline(device, device->pipeline_rms_norm_mul_f32, "rms_norm_mul_f32", rms_norm_f32_len, rms_norm_f32_data, "main", 4, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {0, 1}, 1, true);
  2692. ggml_vk_create_pipeline(device, device->pipeline_rms_norm_partials_f32, "rms_norm_partials_f32", rms_norm_partials_f32_len, rms_norm_partials_f32_data, "main", 4, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {0, 0}, 1, true);
  2693. ggml_vk_create_pipeline(device, device->pipeline_rms_norm_mul_partials_f32, "rms_norm_mul_partials_f32", rms_norm_partials_f32_len, rms_norm_partials_f32_data, "main", 4, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {0, 1}, 1, true);
  2694. ggml_vk_create_pipeline(device, device->pipeline_rms_norm_back_f32, "rms_norm_back_f32", rms_norm_back_f32_len, rms_norm_back_f32_data, "main", 3, sizeof(vk_op_push_constants), {1, 1, 1}, {}, 1);
  2695. ggml_vk_create_pipeline(device, device->pipeline_l2_norm_f32, "l2_norm_f32", l2_norm_f32_len, l2_norm_f32_data, "main", 2, sizeof(vk_op_push_constants), {1, 1, 1}, {}, 1);
  2696. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_f32, "cpy_f32_f32", cpy_f32_f32_len, cpy_f32_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2697. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_f16, "cpy_f32_f16", cpy_f32_f16_len, cpy_f32_f16_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2698. ggml_vk_create_pipeline(device, device->pipeline_cpy_f16_f16, "cpy_f16_f16", cpy_f16_f16_len, cpy_f16_f16_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2699. ggml_vk_create_pipeline(device, device->pipeline_cpy_f16_f32, "cpy_f16_f32", cpy_f16_f32_len, cpy_f16_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2700. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_bf16,"cpy_f32_bf16",cpy_f32_bf16_len,cpy_f32_bf16_data,"main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2701. ggml_vk_create_pipeline(device, device->pipeline_contig_cpy_f32_f32, "contig_cpy_f32_f32", contig_cpy_f32_f32_len, contig_cpy_f32_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2702. ggml_vk_create_pipeline(device, device->pipeline_contig_cpy_f32_f16, "contig_cpy_f32_f16", contig_cpy_f32_f16_len, contig_cpy_f32_f16_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2703. ggml_vk_create_pipeline(device, device->pipeline_contig_cpy_f16_f16, "contig_cpy_f16_f16", contig_cpy_f16_f16_len, contig_cpy_f16_f16_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2704. ggml_vk_create_pipeline(device, device->pipeline_contig_cpy_f16_f32, "contig_cpy_f16_f32", contig_cpy_f16_f32_len, contig_cpy_f16_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2705. ggml_vk_create_pipeline(device, device->pipeline_contig_cpy_f32_bf16,"contig_cpy_f32_bf16",contig_cpy_f32_bf16_len,contig_cpy_f32_bf16_data,"main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2706. if (device->float_controls_rte_fp16) {
  2707. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q4_0], "cpy_f32_q4_0", cpy_f32_q4_0_rte_len, cpy_f32_q4_0_rte_data, "main", 2, sizeof(vk_op_unary_push_constants), {32, 1, 1}, {}, 1);
  2708. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q4_1], "cpy_f32_q4_1", cpy_f32_q4_1_rte_len, cpy_f32_q4_1_rte_data, "main", 2, sizeof(vk_op_unary_push_constants), {32, 1, 1}, {}, 1);
  2709. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q5_0], "cpy_f32_q5_0", cpy_f32_q5_0_rte_len, cpy_f32_q5_0_rte_data, "main", 2, sizeof(vk_op_unary_push_constants), {32, 1, 1}, {}, 1);
  2710. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q5_1], "cpy_f32_q5_1", cpy_f32_q5_1_rte_len, cpy_f32_q5_1_rte_data, "main", 2, sizeof(vk_op_unary_push_constants), {32, 1, 1}, {}, 1);
  2711. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q8_0], "cpy_f32_q8_0", cpy_f32_q8_0_rte_len, cpy_f32_q8_0_rte_data, "main", 2, sizeof(vk_op_unary_push_constants), {32, 1, 1}, {}, 1);
  2712. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_IQ4_NL], "cpy_f32_iq4_nl", cpy_f32_iq4_nl_rte_len, cpy_f32_iq4_nl_rte_data, "main", 2, sizeof(vk_op_unary_push_constants), {32, 1, 1}, {}, 1);
  2713. } else {
  2714. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q4_0], "cpy_f32_q4_0", cpy_f32_q4_0_len, cpy_f32_q4_0_data, "main", 2, sizeof(vk_op_unary_push_constants), {32, 1, 1}, {}, 1);
  2715. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q4_1], "cpy_f32_q4_1", cpy_f32_q4_1_len, cpy_f32_q4_1_data, "main", 2, sizeof(vk_op_unary_push_constants), {32, 1, 1}, {}, 1);
  2716. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q5_0], "cpy_f32_q5_0", cpy_f32_q5_0_len, cpy_f32_q5_0_data, "main", 2, sizeof(vk_op_unary_push_constants), {32, 1, 1}, {}, 1);
  2717. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q5_1], "cpy_f32_q5_1", cpy_f32_q5_1_len, cpy_f32_q5_1_data, "main", 2, sizeof(vk_op_unary_push_constants), {32, 1, 1}, {}, 1);
  2718. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q8_0], "cpy_f32_q8_0", cpy_f32_q8_0_len, cpy_f32_q8_0_data, "main", 2, sizeof(vk_op_unary_push_constants), {32, 1, 1}, {}, 1);
  2719. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_IQ4_NL], "cpy_f32_iq4_nl", cpy_f32_iq4_nl_len, cpy_f32_iq4_nl_data, "main", 2, sizeof(vk_op_unary_push_constants), {32, 1, 1}, {}, 1);
  2720. }
  2721. if (device->float_controls_rte_fp16) {
  2722. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_F32], "set_rows_f32", set_rows_f32_rte_len, set_rows_f32_rte_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2723. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_F16], "set_rows_f16", set_rows_f16_rte_len, set_rows_f16_rte_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2724. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_BF16], "set_rows_bf16", set_rows_bf16_rte_len, set_rows_bf16_rte_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2725. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q4_0], "set_rows_q4_0", set_rows_q4_0_rte_len, set_rows_q4_0_rte_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2726. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q4_1], "set_rows_q4_1", set_rows_q4_1_rte_len, set_rows_q4_1_rte_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2727. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q5_0], "set_rows_q5_0", set_rows_q5_0_rte_len, set_rows_q5_0_rte_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2728. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q5_1], "set_rows_q5_1", set_rows_q5_1_rte_len, set_rows_q5_1_rte_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2729. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q8_0], "set_rows_q8_0", set_rows_q8_0_rte_len, set_rows_q8_0_rte_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2730. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_IQ4_NL], "set_rows_iq4_nl", set_rows_iq4_nl_rte_len, set_rows_iq4_nl_rte_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2731. } else {
  2732. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_F32], "set_rows_f32", set_rows_f32_len, set_rows_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2733. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_F16], "set_rows_f16", set_rows_f16_len, set_rows_f16_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2734. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_BF16], "set_rows_bf16", set_rows_bf16_len, set_rows_bf16_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2735. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q4_0], "set_rows_q4_0", set_rows_q4_0_len, set_rows_q4_0_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2736. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q4_1], "set_rows_q4_1", set_rows_q4_1_len, set_rows_q4_1_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2737. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q5_0], "set_rows_q5_0", set_rows_q5_0_len, set_rows_q5_0_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2738. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q5_1], "set_rows_q5_1", set_rows_q5_1_len, set_rows_q5_1_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2739. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q8_0], "set_rows_q8_0", set_rows_q8_0_len, set_rows_q8_0_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2740. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_IQ4_NL], "set_rows_iq4_nl", set_rows_iq4_nl_len, set_rows_iq4_nl_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2741. }
  2742. ggml_vk_create_pipeline(device, device->pipeline_cpy_quant_f32[GGML_TYPE_Q4_0], "cpy_q4_0_f32", cpy_q4_0_f32_len, cpy_q4_0_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q4_0), 1, 1}, {}, 1);
  2743. ggml_vk_create_pipeline(device, device->pipeline_cpy_quant_f32[GGML_TYPE_Q4_1], "cpy_q4_1_f32", cpy_q4_1_f32_len, cpy_q4_1_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q4_1), 1, 1}, {}, 1);
  2744. ggml_vk_create_pipeline(device, device->pipeline_cpy_quant_f32[GGML_TYPE_Q5_0], "cpy_q5_0_f32", cpy_q5_0_f32_len, cpy_q5_0_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q5_0), 1, 1}, {}, 1);
  2745. ggml_vk_create_pipeline(device, device->pipeline_cpy_quant_f32[GGML_TYPE_Q5_1], "cpy_q5_1_f32", cpy_q5_1_f32_len, cpy_q5_1_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q5_1), 1, 1}, {}, 1);
  2746. ggml_vk_create_pipeline(device, device->pipeline_cpy_quant_f32[GGML_TYPE_Q8_0], "cpy_q8_0_f32", cpy_q8_0_f32_len, cpy_q8_0_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q8_0), 1, 1}, {}, 1);
  2747. ggml_vk_create_pipeline(device, device->pipeline_cpy_quant_f32[GGML_TYPE_IQ4_NL], "cpy_iq4_nl_f32", cpy_iq4_nl_f32_len, cpy_iq4_nl_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_IQ4_NL), 1, 1}, {}, 1);
  2748. auto get_suffix = [](bool src0_f16, bool src1_f16, bool dst_f16) {
  2749. std::string s;
  2750. s += std::string(src0_f16 ? "_f16" : "_f32");
  2751. s += std::string(src1_f16 ? "_f16" : "_f32");
  2752. s += std::string(dst_f16 ? "_f16" : "_f32");
  2753. return s;
  2754. };
  2755. bool rte = device->float_controls_rte_fp16;
  2756. #define CREATE_BINARY(name, namemod, spec, bindings) \
  2757. for (int s0 : {0,1}) for (int s1 : {0,1}) for (int d : {0,1}) \
  2758. ggml_vk_create_pipeline(device, device->pipeline_ ## name ## namemod[s0][s1][d], \
  2759. #name + get_suffix(s0, s1, d) + #namemod, name ## _len[s0][s1][d][rte], name ## _data[s0][s1][d][rte], \
  2760. "main", (bindings), sizeof(vk_op_binary_push_constants), {512, 1, 1}, spec, 1);
  2761. CREATE_BINARY(add, , {0}, 4)
  2762. CREATE_BINARY(add, _norepeat, {1}, 4)
  2763. CREATE_BINARY(sub, , {0}, 3)
  2764. CREATE_BINARY(sub, _norepeat, {1}, 3)
  2765. CREATE_BINARY(mul, , {0}, 3)
  2766. CREATE_BINARY(mul, _norepeat, {1}, 3)
  2767. CREATE_BINARY(div, , {0}, 3)
  2768. CREATE_BINARY(div, _norepeat, {1}, 3)
  2769. CREATE_BINARY(add_rms, , {0}, 4)
  2770. CREATE_BINARY(add_rms, _norepeat, {1}, 4)
  2771. #undef CREATE_BINARY
  2772. if (device->multi_add) {
  2773. for (uint32_t i = 0; i < MAX_FUSED_ADDS; ++i) {
  2774. ggml_vk_create_pipeline(device, device->pipeline_multi_add[i], "multi_add_f32_" + std::to_string(i+1), multi_add_f32_len, multi_add_f32_data, "main", MAX_PARAMETER_COUNT, sizeof(vk_op_multi_add_push_constants), {512, 1, 1}, {i+2}, 1);
  2775. ggml_vk_create_pipeline(device, device->pipeline_multi_add_rms[i], "multi_add_rms_f32_" + std::to_string(i+1), multi_add_rms_f32_len, multi_add_rms_f32_data, "main", MAX_PARAMETER_COUNT, sizeof(vk_op_multi_add_push_constants), {512, 1, 1}, {i+2}, 1);
  2776. }
  2777. }
  2778. ggml_vk_create_pipeline(device, device->pipeline_add_id_f32, "add_id_f32", add_id_f32_len, add_id_f32_data, "main", 4, sizeof(vk_op_add_id_push_constants), {1, 1, 1}, {}, 1);
  2779. ggml_vk_create_pipeline(device, device->pipeline_acc_f32, "acc_f32", acc_f32_len, acc_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {}, 1);
  2780. ggml_vk_create_pipeline(device, device->pipeline_concat_f32, "concat_f32", concat_f32_len, concat_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {}, 1);
  2781. ggml_vk_create_pipeline(device, device->pipeline_concat_f16, "concat_f16", concat_f16_len, concat_f16_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {}, 1);
  2782. ggml_vk_create_pipeline(device, device->pipeline_concat_i32, "concat_i32", concat_i32_len, concat_i32_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {}, 1);
  2783. ggml_vk_create_pipeline(device, device->pipeline_upscale_nearest_f32, "upscale_f32", upscale_f32_len, upscale_f32_data, "main", 2, sizeof(vk_op_upscale_push_constants), {512, 1, 1}, {GGML_SCALE_MODE_NEAREST}, 1);
  2784. ggml_vk_create_pipeline(device, device->pipeline_upscale_bilinear_f32, "upscale_f32", upscale_f32_len, upscale_f32_data, "main", 2, sizeof(vk_op_upscale_push_constants), {512, 1, 1}, {GGML_SCALE_MODE_BILINEAR}, 1);
  2785. ggml_vk_create_pipeline(device, device->pipeline_upscale_bilinear_ac_f32, "upscale_f32", upscale_f32_len, upscale_f32_data, "main", 2, sizeof(vk_op_upscale_push_constants), {512, 1, 1}, {GGML_SCALE_MODE_BILINEAR | GGML_SCALE_FLAG_ALIGN_CORNERS}, 1);
  2786. ggml_vk_create_pipeline(device, device->pipeline_scale_f32, "scale_f32", scale_f32_len, scale_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2787. ggml_vk_create_pipeline(device, device->pipeline_sqr_f32, "sqr_f32", sqr_f32_len, sqr_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2788. ggml_vk_create_pipeline(device, device->pipeline_sqrt_f32, "sqrt_f32", sqrt_f32_len, sqrt_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2789. ggml_vk_create_pipeline(device, device->pipeline_sin_f32, "sin_f32", sin_f32_len, sin_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2790. ggml_vk_create_pipeline(device, device->pipeline_cos_f32, "cos_f32", cos_f32_len, cos_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2791. ggml_vk_create_pipeline(device, device->pipeline_clamp_f32, "clamp_f32", clamp_f32_len, clamp_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2792. ggml_vk_create_pipeline(device, device->pipeline_pad_f32, "pad_f32", pad_f32_len, pad_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2793. ggml_vk_create_pipeline(device, device->pipeline_roll_f32, "roll_f32", roll_f32_len, roll_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2794. ggml_vk_create_pipeline(device, device->pipeline_repeat_f32, "repeat_f32", repeat_f32_len, repeat_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2795. ggml_vk_create_pipeline(device, device->pipeline_repeat_back_f32, "repeat_back_f32", repeat_back_f32_len, repeat_back_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2796. #define CREATE_UNARY(name) \
  2797. ggml_vk_create_pipeline(device, device->pipeline_ ## name [0], #name "_f32", name ## _f32_len, name ## _f32_data, "main", 2, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1); \
  2798. ggml_vk_create_pipeline(device, device->pipeline_ ## name [1], #name "_f16", name ## _f16_len, name ## _f16_data, "main", 2, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
  2799. CREATE_UNARY(exp)
  2800. CREATE_UNARY(gelu)
  2801. CREATE_UNARY(gelu_erf)
  2802. CREATE_UNARY(gelu_quick)
  2803. CREATE_UNARY(silu)
  2804. CREATE_UNARY(relu)
  2805. CREATE_UNARY(tanh)
  2806. CREATE_UNARY(sigmoid)
  2807. CREATE_UNARY(hardsigmoid)
  2808. CREATE_UNARY(hardswish)
  2809. #undef CREATE_UNARY
  2810. #define CREATE_GLU(name) \
  2811. if (device->float_controls_rte_fp16) { \
  2812. ggml_vk_create_pipeline(device, device->pipeline_ ## name [0], #name "_f32_rte", name ## _f32_rte_len, name ## _f32_rte_data, "main", 3, sizeof(vk_op_glu_push_constants), {512, 1, 1}, {}, 1, true); \
  2813. ggml_vk_create_pipeline(device, device->pipeline_ ## name [1], #name "_f16_rte", name ## _f16_rte_len, name ## _f16_rte_data, "main", 3, sizeof(vk_op_glu_push_constants), {512, 1, 1}, {}, 1, true); \
  2814. } else { \
  2815. ggml_vk_create_pipeline(device, device->pipeline_ ## name [0], #name "_f32", name ## _f32_len, name ## _f32_data, "main", 3, sizeof(vk_op_glu_push_constants), {512, 1, 1}, {}, 1, true); \
  2816. ggml_vk_create_pipeline(device, device->pipeline_ ## name [1], #name "_f16", name ## _f16_len, name ## _f16_data, "main", 3, sizeof(vk_op_glu_push_constants), {512, 1, 1}, {}, 1, true); \
  2817. }
  2818. CREATE_GLU(geglu)
  2819. CREATE_GLU(reglu)
  2820. CREATE_GLU(swiglu)
  2821. CREATE_GLU(swiglu_oai)
  2822. CREATE_GLU(geglu_erf)
  2823. CREATE_GLU(geglu_quick)
  2824. #undef CREATE_GLU
  2825. ggml_vk_create_pipeline(device, device->pipeline_leaky_relu_f32, "leaky_relu_f32", leaky_relu_f32_len, leaky_relu_f32_data, "main", 2, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
  2826. ggml_vk_create_pipeline(device, device->pipeline_silu_back_f32, "silu_back_f32", silu_back_f32_len, silu_back_f32_data, "main", 3, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
  2827. ggml_vk_create_pipeline(device, device->pipeline_diag_mask_inf_f32, "diag_mask_inf_f32", diag_mask_inf_f32_len, diag_mask_inf_f32_data, "main", 2, sizeof(vk_op_diag_mask_push_constants), {1, 512, 1}, {}, 1, true);
  2828. ggml_vk_create_pipeline(device, device->pipeline_soft_max_f32, "soft_max_f32", soft_max_f32_len, soft_max_f32_data, "main", 4, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  2829. ggml_vk_create_pipeline(device, device->pipeline_soft_max_f32_wg512, "soft_max_f32_wg512", soft_max_f32_len, soft_max_f32_data, "main", 4, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, { 512 }, 1);
  2830. ggml_vk_create_pipeline(device, device->pipeline_soft_max_f32_f16, "soft_max_f32_f16", soft_max_f32_f16_len, soft_max_f32_f16_data, "main", 4, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  2831. ggml_vk_create_pipeline(device, device->pipeline_soft_max_f32_f16_wg512, "soft_max_f32_f16_wg512", soft_max_f32_f16_len, soft_max_f32_f16_data, "main", 4, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, { 512 }, 1);
  2832. ggml_vk_create_pipeline(device, device->pipeline_soft_max_back_f32, "soft_max_back_f32", soft_max_back_f32_len, soft_max_back_f32_data, "main", 3, sizeof(vk_op_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  2833. ggml_vk_create_pipeline(device, device->pipeline_rope_norm_f32, "rope_norm_f32", rope_norm_f32_len, rope_norm_f32_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2834. ggml_vk_create_pipeline(device, device->pipeline_rope_neox_f32, "rope_neox_f32", rope_neox_f32_len, rope_neox_f32_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2835. ggml_vk_create_pipeline(device, device->pipeline_rope_multi_f32, "rope_multi_f32", rope_multi_f32_len, rope_multi_f32_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2836. ggml_vk_create_pipeline(device, device->pipeline_rope_vision_f32, "rope_vision_f32", rope_vision_f32_len, rope_vision_f32_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2837. if (device->float_controls_rte_fp16) {
  2838. ggml_vk_create_pipeline(device, device->pipeline_rope_norm_f16, "rope_norm_f16", rope_norm_f16_rte_len, rope_norm_f16_rte_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2839. ggml_vk_create_pipeline(device, device->pipeline_rope_neox_f16, "rope_neox_f16", rope_neox_f16_rte_len, rope_neox_f16_rte_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2840. ggml_vk_create_pipeline(device, device->pipeline_rope_multi_f16, "rope_multi_f16", rope_multi_f16_rte_len, rope_multi_f16_rte_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2841. ggml_vk_create_pipeline(device, device->pipeline_rope_vision_f16, "rope_vision_f16", rope_vision_f16_rte_len, rope_vision_f16_rte_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2842. } else {
  2843. ggml_vk_create_pipeline(device, device->pipeline_rope_norm_f16, "rope_norm_f16", rope_norm_f16_len, rope_norm_f16_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2844. ggml_vk_create_pipeline(device, device->pipeline_rope_neox_f16, "rope_neox_f16", rope_neox_f16_len, rope_neox_f16_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2845. ggml_vk_create_pipeline(device, device->pipeline_rope_multi_f16, "rope_multi_f16", rope_multi_f16_len, rope_multi_f16_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2846. ggml_vk_create_pipeline(device, device->pipeline_rope_vision_f16, "rope_vision_f16", rope_vision_f16_len, rope_vision_f16_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2847. }
  2848. for (uint32_t i = 0; i < num_argsort_pipelines; ++i) {
  2849. ggml_vk_create_pipeline(device, device->pipeline_argsort_f32[i], "argsort_f32_"+std::to_string(i), argsort_f32_len, argsort_f32_data, "main", 2, sizeof(vk_op_argsort_push_constants), {1u<<i, 1, 1}, {1u<<i, i}, 1, true);
  2850. }
  2851. ggml_vk_create_pipeline(device, device->pipeline_argmax_f32, "argmax_f32", argmax_f32_len, argmax_f32_data, "main", 2, sizeof(vk_op_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  2852. ggml_vk_create_pipeline(device, device->pipeline_sum_rows_f32, "sum_rows_f32", sum_rows_f32_len, sum_rows_f32_data, "main", 2, sizeof(vk_op_sum_rows_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  2853. ggml_vk_create_pipeline(device, device->pipeline_count_equal_i32, "count_equal_i32", count_equal_i32_len, count_equal_i32_data, "main", 3, sizeof(vk_op_push_constants), {512, 1, 1}, { device->subgroup_size }, 1);
  2854. ggml_vk_create_pipeline(device, device->pipeline_im2col_f32, "im2col_f32", im2col_f32_len, im2col_f32_data, "main", 2, sizeof(vk_op_im2col_push_constants), {512, 1, 1}, { device->subgroup_size }, 1, true);
  2855. if (device->float_controls_rte_fp16) {
  2856. ggml_vk_create_pipeline(device, device->pipeline_im2col_f32_f16, "im2col_f32_f16", im2col_f32_f16_rte_len, im2col_f32_f16_rte_data, "main", 2, sizeof(vk_op_im2col_push_constants), {512, 1, 1}, { device->subgroup_size }, 1, true);
  2857. } else {
  2858. ggml_vk_create_pipeline(device, device->pipeline_im2col_f32_f16, "im2col_f32_f16", im2col_f32_f16_len, im2col_f32_f16_data, "main", 2, sizeof(vk_op_im2col_push_constants), {512, 1, 1}, { device->subgroup_size }, 1, true);
  2859. }
  2860. ggml_vk_create_pipeline(device, device->pipeline_timestep_embedding_f32, "timestep_embedding_f32", timestep_embedding_f32_len, timestep_embedding_f32_data, "main", 2, sizeof(vk_op_timestep_embedding_push_constants), {256, 1, 1}, {}, 1);
  2861. ggml_vk_create_pipeline(device, device->pipeline_conv_transpose_1d_f32, "conv_transpose_1d_f32", conv_transpose_1d_f32_len, conv_transpose_1d_f32_data, "main", 3, sizeof(vk_op_conv_transpose_1d_push_constants), {1, 1, 1}, {}, 1);
  2862. ggml_vk_create_pipeline(device, device->pipeline_pool2d_f32, "pool2d_f32", pool2d_f32_len, pool2d_f32_data, "main", 2, sizeof(vk_op_pool2d_push_constants), {512, 1, 1}, {}, 1);
  2863. ggml_vk_create_pipeline(device, device->pipeline_rwkv_wkv6_f32, "rwkv_wkv6_f32", rwkv_wkv6_f32_len, rwkv_wkv6_f32_data, "main", 7, sizeof(vk_op_rwkv_wkv6_push_constants), {1, 1, 1}, {device->subgroup_size}, 1);
  2864. ggml_vk_create_pipeline(device, device->pipeline_rwkv_wkv7_f32, "rwkv_wkv7_f32", rwkv_wkv7_f32_len, rwkv_wkv7_f32_data, "main", 8, sizeof(vk_op_rwkv_wkv7_push_constants), {1, 1, 1}, {device->subgroup_size}, 1);
  2865. ggml_vk_create_pipeline(device, device->pipeline_opt_step_adamw_f32, "opt_step_adamw_f32", opt_step_adamw_f32_len, opt_step_adamw_f32_data, "main", 5, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
  2866. ggml_vk_create_pipeline(device, device->pipeline_opt_step_sgd_f32, "opt_step_sgd_f32", opt_step_sgd_f32_len, opt_step_sgd_f32_data, "main", 3, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
  2867. // conv2d
  2868. for (uint32_t s = 0; s < CONV_SHAPE_COUNT; ++s) {
  2869. uint32_t conv2d_WG_SIZE = 256;
  2870. uint32_t conv2d_BS_K = 128;
  2871. uint32_t conv2d_BS_CRS = 16;
  2872. uint32_t use_collectives = 0; // Enables subgroup ops for preventing the re-calculation of indices.
  2873. uint32_t conv2d_BS_NPQ = 128;
  2874. uint32_t conv2d_TS_K = 8;
  2875. uint32_t conv2d_SHMEM_PAD = 4;
  2876. bool conv2d_UNROLL = true;
  2877. #if defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  2878. if (device->coopmat2) {
  2879. conv2d_SHMEM_PAD = 8; // 8 float16_t
  2880. }
  2881. #endif
  2882. if (device->vendor_id == VK_VENDOR_ID_INTEL) {
  2883. conv2d_SHMEM_PAD = 0;
  2884. conv2d_UNROLL = false;
  2885. } else if (device->vendor_id == VK_VENDOR_ID_AMD) {
  2886. conv2d_SHMEM_PAD = device->architecture == vk_device_architecture::AMD_GCN ? 1 : 4;
  2887. }
  2888. switch (s) {
  2889. default:
  2890. case CONV_SHAPE_128x128:
  2891. conv2d_BS_K = 128;
  2892. conv2d_BS_NPQ = 128;
  2893. conv2d_BS_CRS = 16;
  2894. if (device->vendor_id == VK_VENDOR_ID_AMD && device->architecture != vk_device_architecture::AMD_GCN) {
  2895. conv2d_UNROLL = false;
  2896. }
  2897. break;
  2898. case CONV_SHAPE_64x32:
  2899. conv2d_BS_K = 64;
  2900. conv2d_BS_NPQ = 32;
  2901. conv2d_BS_CRS = 32;
  2902. conv2d_TS_K = 4;
  2903. break;
  2904. case CONV_SHAPE_32x256:
  2905. conv2d_BS_K = 32;
  2906. conv2d_BS_NPQ = 256;
  2907. conv2d_BS_CRS = 16;
  2908. break;
  2909. }
  2910. // Use collectives on pre-Turing NVIDIA GPUs and GCN AMD cards, which had slower integer math.
  2911. bool allow_collectives_nv = device->vendor_id != VK_VENDOR_ID_NVIDIA ||
  2912. device->architecture == vk_device_architecture::NVIDIA_PRE_TURING;
  2913. bool allow_collectives_amd = device->vendor_id != VK_VENDOR_ID_AMD ||
  2914. device->architecture == vk_device_architecture::AMD_GCN;
  2915. if (device->subgroup_shuffle &&
  2916. device->vendor_id != VK_VENDOR_ID_INTEL && // Do not enable collectives on Intel, see PR 14316.
  2917. allow_collectives_nv &&
  2918. allow_collectives_amd) {
  2919. use_collectives = 1;
  2920. conv2d_BS_CRS = std::min(
  2921. device->subgroup_size,
  2922. conv2d_BS_CRS); // CRS block size should be capped at subgroup size for correctness when shuffle is used.
  2923. }
  2924. uint32_t conv2d_shmem_req =
  2925. (conv2d_BS_K * (conv2d_BS_CRS + conv2d_SHMEM_PAD) + conv2d_BS_CRS * (conv2d_BS_NPQ + conv2d_SHMEM_PAD)) * sizeof(float);
  2926. if (device->properties.limits.maxComputeSharedMemorySize < conv2d_shmem_req) {
  2927. conv2d_BS_CRS = 8;
  2928. if (use_collectives) {
  2929. conv2d_BS_CRS = std::min(device->subgroup_size, conv2d_BS_CRS);
  2930. }
  2931. }
  2932. std::array<uint32_t, 3> wg_denoms = { conv2d_BS_K, conv2d_BS_NPQ, 1 };
  2933. std::vector<uint32_t> spec_constants = { conv2d_WG_SIZE, conv2d_BS_K, conv2d_BS_CRS, conv2d_BS_NPQ, conv2d_TS_K, use_collectives, conv2d_SHMEM_PAD };
  2934. #if defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  2935. if (device->coopmat2) {
  2936. ggml_vk_create_pipeline(
  2937. device, device->pipeline_conv2d_f32[s], "conv2d_f32", conv2d_f32_cm2_len, conv2d_f32_cm2_data, "main", 3,
  2938. sizeof(vk_op_conv2d_push_constants), wg_denoms, spec_constants, 1, true, use_collectives);
  2939. ggml_vk_create_pipeline(
  2940. device, device->pipeline_conv2d_f16_f32[s], "conv2d_f16_f32", conv2d_f16_f32_cm2_len, conv2d_f16_f32_cm2_data, "main", 3,
  2941. sizeof(vk_op_conv2d_push_constants), wg_denoms, spec_constants, 1, true, use_collectives);
  2942. } else
  2943. #endif
  2944. if (conv2d_UNROLL) {
  2945. ggml_vk_create_pipeline(
  2946. device, device->pipeline_conv2d_f32[s], "conv2d_f32", conv2d_f32_unroll_len, conv2d_f32_unroll_data, "main", 3,
  2947. sizeof(vk_op_conv2d_push_constants), wg_denoms, spec_constants, 1, true, use_collectives);
  2948. ggml_vk_create_pipeline(
  2949. device, device->pipeline_conv2d_f16_f32[s], "conv2d_f16_f32", conv2d_f16_f32_unroll_len, conv2d_f16_f32_unroll_data, "main", 3,
  2950. sizeof(vk_op_conv2d_push_constants), wg_denoms, spec_constants, 1, true, use_collectives);
  2951. } else {
  2952. ggml_vk_create_pipeline(
  2953. device, device->pipeline_conv2d_f32[s], "conv2d_f32", conv2d_f32_len, conv2d_f32_data, "main", 3,
  2954. sizeof(vk_op_conv2d_push_constants), wg_denoms, spec_constants, 1, true, use_collectives);
  2955. ggml_vk_create_pipeline(
  2956. device, device->pipeline_conv2d_f16_f32[s], "conv2d_f16_f32", conv2d_f16_f32_len, conv2d_f16_f32_data, "main", 3,
  2957. sizeof(vk_op_conv2d_push_constants), wg_denoms, spec_constants, 1, true, use_collectives);
  2958. }
  2959. }
  2960. ggml_vk_create_pipeline(device, device->pipeline_conv2d_dw_whcn_f32, "conv2d_dw_whcn_f32", conv2d_dw_whcn_f32_len, conv2d_dw_whcn_f32_data, "main", 3, sizeof(vk_op_conv2d_dw_push_constants), {512, 1, 1}, {}, 1);
  2961. ggml_vk_create_pipeline(device, device->pipeline_conv2d_dw_cwhn_f32, "conv2d_dw_cwhn_f32", conv2d_dw_cwhn_f32_len, conv2d_dw_cwhn_f32_data, "main", 3, sizeof(vk_op_conv2d_dw_push_constants), {512, 1, 1}, {}, 1);
  2962. ggml_vk_create_pipeline(device, device->pipeline_conv2d_dw_whcn_f16_f32, "conv2d_dw_whcn_f16_f32", conv2d_dw_whcn_f16_f32_len, conv2d_dw_whcn_f16_f32_data, "main", 3, sizeof(vk_op_conv2d_dw_push_constants), {512, 1, 1}, {}, 1);
  2963. ggml_vk_create_pipeline(device, device->pipeline_conv2d_dw_cwhn_f16_f32, "conv2d_dw_cwhn_f16_f32", conv2d_dw_cwhn_f16_f32_len, conv2d_dw_cwhn_f16_f32_data, "main", 3, sizeof(vk_op_conv2d_dw_push_constants), {512, 1, 1}, {}, 1);
  2964. for (auto &c : compiles) {
  2965. c.wait();
  2966. }
  2967. device->need_compiles = false;
  2968. }
  2969. static bool ggml_vk_khr_cooperative_matrix_support(const vk::PhysicalDeviceProperties& props, const vk::PhysicalDeviceDriverProperties& driver_props, vk_device_architecture arch);
  2970. static vk_device ggml_vk_get_device(size_t idx) {
  2971. VK_LOG_DEBUG("ggml_vk_get_device(" << idx << ")");
  2972. if (vk_instance.devices[idx] == nullptr) {
  2973. VK_LOG_DEBUG("Initializing new vk_device");
  2974. vk_device device = std::make_shared<vk_device_struct>();
  2975. vk_instance.devices[idx] = device;
  2976. #ifdef GGML_VULKAN_MEMORY_DEBUG
  2977. device->memory_logger = std::unique_ptr<vk_memory_logger>(new vk_memory_logger());
  2978. #endif
  2979. if (vk_perf_logger_enabled) {
  2980. device->perf_logger = std::unique_ptr<vk_perf_logger>(new vk_perf_logger());
  2981. }
  2982. size_t dev_num = vk_instance.device_indices[idx];
  2983. std::vector<vk::PhysicalDevice> physical_devices = vk_instance.instance.enumeratePhysicalDevices();
  2984. if (dev_num >= physical_devices.size()) {
  2985. std::cerr << "ggml_vulkan: Device with index " << dev_num << " does not exist." << std::endl;
  2986. throw std::runtime_error("Device not found");
  2987. }
  2988. device->physical_device = physical_devices[dev_num];
  2989. const std::vector<vk::ExtensionProperties> ext_props = device->physical_device.enumerateDeviceExtensionProperties();
  2990. device->architecture = get_device_architecture(device->physical_device);
  2991. const char* GGML_VK_PREFER_HOST_MEMORY = getenv("GGML_VK_PREFER_HOST_MEMORY");
  2992. device->prefer_host_memory = GGML_VK_PREFER_HOST_MEMORY != nullptr;
  2993. const char* GGML_VK_DISABLE_HOST_VISIBLE_VIDMEM = getenv("GGML_VK_DISABLE_HOST_VISIBLE_VIDMEM");
  2994. device->disable_host_visible_vidmem = GGML_VK_DISABLE_HOST_VISIBLE_VIDMEM != nullptr;
  2995. const char* GGML_VK_ALLOW_SYSMEM_FALLBACK = getenv("GGML_VK_ALLOW_SYSMEM_FALLBACK");
  2996. device->allow_sysmem_fallback = GGML_VK_ALLOW_SYSMEM_FALLBACK != nullptr;
  2997. bool fp16_storage = false;
  2998. bool fp16_compute = false;
  2999. bool maintenance4_support = false;
  3000. bool sm_builtins = false;
  3001. bool amd_shader_core_properties2 = false;
  3002. bool pipeline_robustness = false;
  3003. bool coopmat2_support = false;
  3004. device->coopmat_support = false;
  3005. device->integer_dot_product = false;
  3006. bool bfloat16_support = false;
  3007. for (const auto& properties : ext_props) {
  3008. if (strcmp("VK_KHR_maintenance4", properties.extensionName) == 0) {
  3009. maintenance4_support = true;
  3010. } else if (strcmp("VK_KHR_16bit_storage", properties.extensionName) == 0) {
  3011. fp16_storage = true;
  3012. } else if (strcmp("VK_KHR_shader_float16_int8", properties.extensionName) == 0) {
  3013. fp16_compute = true;
  3014. } else if (strcmp("VK_NV_shader_sm_builtins", properties.extensionName) == 0) {
  3015. sm_builtins = true;
  3016. } else if (strcmp("VK_AMD_shader_core_properties2", properties.extensionName) == 0) {
  3017. amd_shader_core_properties2 = true;
  3018. } else if (strcmp("VK_EXT_pipeline_robustness", properties.extensionName) == 0) {
  3019. pipeline_robustness = true;
  3020. } else if (strcmp("VK_EXT_subgroup_size_control", properties.extensionName) == 0) {
  3021. device->subgroup_size_control = true;
  3022. #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  3023. } else if (strcmp("VK_KHR_cooperative_matrix", properties.extensionName) == 0 &&
  3024. !getenv("GGML_VK_DISABLE_COOPMAT")) {
  3025. device->coopmat_support = true;
  3026. device->coopmat_m = 0;
  3027. device->coopmat_n = 0;
  3028. device->coopmat_k = 0;
  3029. #endif
  3030. #if defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  3031. } else if (strcmp("VK_NV_cooperative_matrix2", properties.extensionName) == 0 &&
  3032. !getenv("GGML_VK_DISABLE_COOPMAT2")) {
  3033. coopmat2_support = true;
  3034. #endif
  3035. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  3036. } else if (strcmp("VK_KHR_shader_integer_dot_product", properties.extensionName) == 0 &&
  3037. !getenv("GGML_VK_DISABLE_INTEGER_DOT_PRODUCT")) {
  3038. device->integer_dot_product = true;
  3039. #endif
  3040. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  3041. } else if (strcmp("VK_KHR_shader_bfloat16", properties.extensionName) == 0 &&
  3042. !getenv("GGML_VK_DISABLE_BFLOAT16")) {
  3043. bfloat16_support = true;
  3044. #endif
  3045. }
  3046. }
  3047. vk::PhysicalDeviceProperties2 props2;
  3048. vk::PhysicalDeviceMaintenance3Properties props3;
  3049. vk::PhysicalDeviceMaintenance4Properties props4;
  3050. vk::PhysicalDeviceSubgroupProperties subgroup_props;
  3051. vk::PhysicalDeviceDriverProperties driver_props;
  3052. vk::PhysicalDeviceShaderSMBuiltinsPropertiesNV sm_props;
  3053. vk::PhysicalDeviceShaderCoreProperties2AMD amd_shader_core_properties2_props;
  3054. vk::PhysicalDeviceVulkan11Properties vk11_props;
  3055. vk::PhysicalDeviceVulkan12Properties vk12_props;
  3056. vk::PhysicalDeviceSubgroupSizeControlPropertiesEXT subgroup_size_control_props;
  3057. vk::PhysicalDeviceShaderIntegerDotProductPropertiesKHR shader_integer_dot_product_props;
  3058. props2.pNext = &props3;
  3059. props3.pNext = &subgroup_props;
  3060. subgroup_props.pNext = &driver_props;
  3061. driver_props.pNext = &vk11_props;
  3062. vk11_props.pNext = &vk12_props;
  3063. VkBaseOutStructure * last_struct = (VkBaseOutStructure *)&vk12_props;
  3064. if (maintenance4_support) {
  3065. last_struct->pNext = (VkBaseOutStructure *)&props4;
  3066. last_struct = (VkBaseOutStructure *)&props4;
  3067. }
  3068. if (sm_builtins) {
  3069. last_struct->pNext = (VkBaseOutStructure *)&sm_props;
  3070. last_struct = (VkBaseOutStructure *)&sm_props;
  3071. }
  3072. if (amd_shader_core_properties2) {
  3073. last_struct->pNext = (VkBaseOutStructure *)&amd_shader_core_properties2_props;
  3074. last_struct = (VkBaseOutStructure *)&amd_shader_core_properties2_props;
  3075. }
  3076. if (device->subgroup_size_control) {
  3077. last_struct->pNext = (VkBaseOutStructure *)&subgroup_size_control_props;
  3078. last_struct = (VkBaseOutStructure *)&subgroup_size_control_props;
  3079. }
  3080. #if defined(VK_NV_cooperative_matrix2)
  3081. vk::PhysicalDeviceCooperativeMatrix2PropertiesNV coopmat2_props;
  3082. if (coopmat2_support) {
  3083. last_struct->pNext = (VkBaseOutStructure *)&coopmat2_props;
  3084. last_struct = (VkBaseOutStructure *)&coopmat2_props;
  3085. }
  3086. #endif
  3087. if (device->integer_dot_product) {
  3088. last_struct->pNext = (VkBaseOutStructure *)&shader_integer_dot_product_props;
  3089. last_struct = (VkBaseOutStructure *)&shader_integer_dot_product_props;
  3090. }
  3091. device->physical_device.getProperties2(&props2);
  3092. device->properties = props2.properties;
  3093. device->vendor_id = device->properties.vendorID;
  3094. device->driver_id = driver_props.driverID;
  3095. const char* GGML_VK_FORCE_MAX_ALLOCATION_SIZE = getenv("GGML_VK_FORCE_MAX_ALLOCATION_SIZE");
  3096. if (GGML_VK_FORCE_MAX_ALLOCATION_SIZE != nullptr) {
  3097. device->max_memory_allocation_size = std::stoul(GGML_VK_FORCE_MAX_ALLOCATION_SIZE);
  3098. } else if (maintenance4_support) {
  3099. device->max_memory_allocation_size = std::min(props3.maxMemoryAllocationSize, props4.maxBufferSize);
  3100. } else {
  3101. device->max_memory_allocation_size = props3.maxMemoryAllocationSize;
  3102. }
  3103. const char* GGML_VK_SUBALLOCATION_BLOCK_SIZE = getenv("GGML_VK_SUBALLOCATION_BLOCK_SIZE");
  3104. if (GGML_VK_SUBALLOCATION_BLOCK_SIZE != nullptr) {
  3105. device->suballocation_block_size = std::stoul(GGML_VK_SUBALLOCATION_BLOCK_SIZE);
  3106. } else {
  3107. // Limit batching of allocations to 1GB by default to avoid fragmentation issues
  3108. device->suballocation_block_size = 1024*1024*1024;
  3109. }
  3110. device->suballocation_block_size = std::min(device->suballocation_block_size, device->max_memory_allocation_size);
  3111. device->subgroup_size = subgroup_props.subgroupSize;
  3112. device->uma = device->properties.deviceType == vk::PhysicalDeviceType::eIntegratedGpu;
  3113. if (sm_builtins) {
  3114. device->shader_core_count = sm_props.shaderSMCount;
  3115. } else if (amd_shader_core_properties2) {
  3116. device->shader_core_count = amd_shader_core_properties2_props.activeComputeUnitCount;
  3117. } else {
  3118. device->shader_core_count = 0;
  3119. }
  3120. device->float_controls_rte_fp16 = vk12_props.shaderRoundingModeRTEFloat16;
  3121. device->subgroup_arithmetic = (vk11_props.subgroupSupportedStages & vk::ShaderStageFlagBits::eCompute) &&
  3122. (vk11_props.subgroupSupportedOperations & vk::SubgroupFeatureFlagBits::eArithmetic);
  3123. device->subgroup_shuffle = (vk11_props.subgroupSupportedStages & vk::ShaderStageFlagBits::eCompute) &&
  3124. (vk11_props.subgroupSupportedOperations & vk::SubgroupFeatureFlagBits::eShuffle);
  3125. device->subgroup_clustered = (vk11_props.subgroupSupportedStages & vk::ShaderStageFlagBits::eCompute) &&
  3126. (vk11_props.subgroupSupportedOperations & vk::SubgroupFeatureFlagBits::eClustered);
  3127. device->subgroup_ballot = (vk11_props.subgroupSupportedStages & vk::ShaderStageFlagBits::eCompute) &&
  3128. (vk11_props.subgroupSupportedOperations & vk::SubgroupFeatureFlagBits::eBallot);
  3129. const bool force_disable_f16 = getenv("GGML_VK_DISABLE_F16") != nullptr;
  3130. device->fp16 = !force_disable_f16 && fp16_storage && fp16_compute;
  3131. if (!ggml_vk_khr_cooperative_matrix_support(device->properties, driver_props, device->architecture)) {
  3132. device->coopmat_support = false;
  3133. }
  3134. device->integer_dot_product = device->integer_dot_product && shader_integer_dot_product_props.integerDotProduct4x8BitPackedSignedAccelerated;
  3135. std::vector<vk::QueueFamilyProperties> queue_family_props = device->physical_device.getQueueFamilyProperties();
  3136. // Try to find a non-graphics compute queue and transfer-focused queues
  3137. const uint32_t compute_queue_family_index = ggml_vk_find_queue_family_index(queue_family_props, vk::QueueFlagBits::eCompute, vk::QueueFlagBits::eGraphics, -1, 1);
  3138. const uint32_t transfer_queue_family_index = ggml_vk_find_queue_family_index(queue_family_props, vk::QueueFlagBits::eTransfer, vk::QueueFlagBits::eCompute | vk::QueueFlagBits::eGraphics, compute_queue_family_index, 1);
  3139. const float priorities[] = { 1.0f, 1.0f };
  3140. device->single_queue = compute_queue_family_index == transfer_queue_family_index && queue_family_props[compute_queue_family_index].queueCount == 1;
  3141. std::vector<vk::DeviceQueueCreateInfo> device_queue_create_infos;
  3142. if (compute_queue_family_index != transfer_queue_family_index) {
  3143. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), compute_queue_family_index, 1, priorities});
  3144. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), transfer_queue_family_index, 1, priorities + 1});
  3145. } else if(!device->single_queue) {
  3146. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), compute_queue_family_index, 2, priorities});
  3147. } else {
  3148. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), compute_queue_family_index, 1, priorities});
  3149. }
  3150. vk::DeviceCreateInfo device_create_info;
  3151. std::vector<const char *> device_extensions;
  3152. vk::PhysicalDeviceFeatures device_features = device->physical_device.getFeatures();
  3153. VkPhysicalDeviceFeatures2 device_features2;
  3154. device_features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
  3155. device_features2.pNext = nullptr;
  3156. device_features2.features = (VkPhysicalDeviceFeatures)device_features;
  3157. VkPhysicalDeviceVulkan11Features vk11_features;
  3158. vk11_features.pNext = nullptr;
  3159. vk11_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES;
  3160. device_features2.pNext = &vk11_features;
  3161. VkPhysicalDeviceVulkan12Features vk12_features;
  3162. vk12_features.pNext = nullptr;
  3163. vk12_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES;
  3164. vk11_features.pNext = &vk12_features;
  3165. last_struct = (VkBaseOutStructure *)&vk12_features;
  3166. VkPhysicalDevicePipelineRobustnessFeaturesEXT pl_robustness_features;
  3167. pl_robustness_features.pNext = nullptr;
  3168. pl_robustness_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PIPELINE_ROBUSTNESS_FEATURES_EXT;
  3169. pl_robustness_features.pipelineRobustness = VK_FALSE;
  3170. if (pipeline_robustness) {
  3171. last_struct->pNext = (VkBaseOutStructure *)&pl_robustness_features;
  3172. last_struct = (VkBaseOutStructure *)&pl_robustness_features;
  3173. device_extensions.push_back("VK_EXT_pipeline_robustness");
  3174. }
  3175. VkPhysicalDeviceSubgroupSizeControlFeaturesEXT subgroup_size_control_features;
  3176. subgroup_size_control_features.pNext = nullptr;
  3177. subgroup_size_control_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_SIZE_CONTROL_FEATURES_EXT;
  3178. subgroup_size_control_features.computeFullSubgroups = false;
  3179. subgroup_size_control_features.subgroupSizeControl = false;
  3180. if (device->subgroup_size_control) {
  3181. last_struct->pNext = (VkBaseOutStructure *)&subgroup_size_control_features;
  3182. last_struct = (VkBaseOutStructure *)&subgroup_size_control_features;
  3183. }
  3184. #if defined(VK_KHR_cooperative_matrix)
  3185. VkPhysicalDeviceCooperativeMatrixFeaturesKHR coopmat_features;
  3186. coopmat_features.pNext = nullptr;
  3187. coopmat_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_FEATURES_KHR;
  3188. coopmat_features.cooperativeMatrix = VK_FALSE;
  3189. if (device->coopmat_support) {
  3190. last_struct->pNext = (VkBaseOutStructure *)&coopmat_features;
  3191. last_struct = (VkBaseOutStructure *)&coopmat_features;
  3192. }
  3193. #endif
  3194. #if defined(VK_NV_cooperative_matrix2)
  3195. VkPhysicalDeviceCooperativeMatrix2FeaturesNV coopmat2_features {};
  3196. coopmat2_features.pNext = nullptr;
  3197. coopmat2_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_2_FEATURES_NV;
  3198. if (coopmat2_support) {
  3199. last_struct->pNext = (VkBaseOutStructure *)&coopmat2_features;
  3200. last_struct = (VkBaseOutStructure *)&coopmat2_features;
  3201. device_extensions.push_back("VK_NV_cooperative_matrix2");
  3202. }
  3203. #endif
  3204. #if defined(VK_KHR_shader_bfloat16)
  3205. VkPhysicalDeviceShaderBfloat16FeaturesKHR bfloat16_features {};
  3206. bfloat16_features.pNext = nullptr;
  3207. bfloat16_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_BFLOAT16_FEATURES_KHR;
  3208. if (bfloat16_support) {
  3209. last_struct->pNext = (VkBaseOutStructure *)&bfloat16_features;
  3210. last_struct = (VkBaseOutStructure *)&bfloat16_features;
  3211. device_extensions.push_back("VK_KHR_shader_bfloat16");
  3212. }
  3213. #endif
  3214. VkPhysicalDeviceMaintenance4Features maint4_features {};
  3215. maint4_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_4_FEATURES;
  3216. if (maintenance4_support) {
  3217. last_struct->pNext = (VkBaseOutStructure *)&maint4_features;
  3218. last_struct = (VkBaseOutStructure *)&maint4_features;
  3219. device_extensions.push_back("VK_KHR_maintenance4");
  3220. }
  3221. VkPhysicalDeviceShaderIntegerDotProductFeaturesKHR shader_integer_dot_product_features {};
  3222. shader_integer_dot_product_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_INTEGER_DOT_PRODUCT_FEATURES_KHR;
  3223. if (device->integer_dot_product) {
  3224. last_struct->pNext = (VkBaseOutStructure *)&shader_integer_dot_product_features;
  3225. last_struct = (VkBaseOutStructure *)&shader_integer_dot_product_features;
  3226. device_extensions.push_back("VK_KHR_shader_integer_dot_product");
  3227. }
  3228. vkGetPhysicalDeviceFeatures2(device->physical_device, &device_features2);
  3229. device->fp16 = device->fp16 && vk12_features.shaderFloat16;
  3230. #if defined(VK_KHR_shader_bfloat16)
  3231. device->bf16 = bfloat16_support && bfloat16_features.shaderBFloat16Type;
  3232. #else
  3233. device->bf16 = false;
  3234. #endif
  3235. device->pipeline_robustness = pl_robustness_features.pipelineRobustness;
  3236. device->multi_add = vk12_props.shaderRoundingModeRTEFloat16 &&
  3237. device->properties.limits.maxPushConstantsSize >= sizeof(vk_op_multi_add_push_constants) &&
  3238. vk12_features.runtimeDescriptorArray &&
  3239. device->vendor_id != VK_VENDOR_ID_INTEL &&
  3240. getenv("GGML_VK_DISABLE_MULTI_ADD") == nullptr;
  3241. if (device->subgroup_size_control) {
  3242. device->subgroup_min_size = subgroup_size_control_props.minSubgroupSize;
  3243. device->subgroup_max_size = subgroup_size_control_props.maxSubgroupSize;
  3244. device_extensions.push_back("VK_EXT_subgroup_size_control");
  3245. }
  3246. device->subgroup_size_control = device->subgroup_size_control &&
  3247. (subgroup_size_control_props.requiredSubgroupSizeStages & vk::ShaderStageFlagBits::eCompute) &&
  3248. subgroup_size_control_features.subgroupSizeControl;
  3249. device->subgroup_require_full_support = subgroup_size_control_features.computeFullSubgroups;
  3250. #if defined(VK_KHR_cooperative_matrix)
  3251. device->coopmat_support = device->coopmat_support && coopmat_features.cooperativeMatrix;
  3252. // coopmat1 fa shader currently assumes 32 invocations per subgroup
  3253. device->coopmat1_fa_support = device->coopmat_support && device->subgroup_require_full_support &&
  3254. device->subgroup_size_control && device->subgroup_min_size <= 32 &&
  3255. device->subgroup_max_size >= 32;
  3256. #endif
  3257. if (coopmat2_support) {
  3258. #if defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  3259. if (coopmat2_features.cooperativeMatrixWorkgroupScope &&
  3260. coopmat2_features.cooperativeMatrixFlexibleDimensions &&
  3261. coopmat2_features.cooperativeMatrixReductions &&
  3262. coopmat2_features.cooperativeMatrixConversions &&
  3263. coopmat2_features.cooperativeMatrixPerElementOperations &&
  3264. coopmat2_features.cooperativeMatrixTensorAddressing &&
  3265. coopmat2_features.cooperativeMatrixBlockLoads &&
  3266. vk12_features.bufferDeviceAddress) {
  3267. std::vector<VkCooperativeMatrixFlexibleDimensionsPropertiesNV> flexible_dimensions;
  3268. uint32_t count = 0;
  3269. PFN_vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV
  3270. _vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV =
  3271. (PFN_vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV)
  3272. vk_instance.instance.getProcAddr("vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV");
  3273. _vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV(device->physical_device, &count, nullptr);
  3274. VkCooperativeMatrixFlexibleDimensionsPropertiesNV empty_prop {};
  3275. empty_prop.sType = VK_STRUCTURE_TYPE_COOPERATIVE_MATRIX_FLEXIBLE_DIMENSIONS_PROPERTIES_NV;
  3276. flexible_dimensions.resize(count, empty_prop);
  3277. _vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV(device->physical_device, &count, flexible_dimensions.data());
  3278. bool found_fp16_128 = false,
  3279. found_fp16_256 = false,
  3280. found_fp32_128 = false,
  3281. found_fp32_256 = false;
  3282. // need to support fp16*fp16 with fp16/fp32 accumulator, for workgroupsize 128
  3283. // with 32x16x16 and 256 with 32x32x16.
  3284. for (auto &prop : flexible_dimensions) {
  3285. if (prop.saturatingAccumulation == VK_FALSE &&
  3286. prop.scope == VK_SCOPE_WORKGROUP_KHR &&
  3287. prop.AType == VK_COMPONENT_TYPE_FLOAT16_KHR &&
  3288. prop.BType == VK_COMPONENT_TYPE_FLOAT16_KHR) {
  3289. if (prop.workgroupInvocations == 128 &&
  3290. prop.MGranularity <= 32 &&
  3291. prop.NGranularity <= 16 &&
  3292. prop.KGranularity <= 16) {
  3293. if (prop.CType == VK_COMPONENT_TYPE_FLOAT16_KHR &&
  3294. prop.ResultType == VK_COMPONENT_TYPE_FLOAT16_KHR) {
  3295. found_fp16_128 = true;
  3296. }
  3297. if (prop.CType == VK_COMPONENT_TYPE_FLOAT32_KHR &&
  3298. prop.ResultType == VK_COMPONENT_TYPE_FLOAT32_KHR) {
  3299. found_fp32_128 = true;
  3300. }
  3301. }
  3302. if (prop.workgroupInvocations == 256 &&
  3303. prop.MGranularity <= 32 &&
  3304. prop.NGranularity <= 32 &&
  3305. prop.KGranularity <= 16) {
  3306. if (prop.CType == VK_COMPONENT_TYPE_FLOAT16_KHR &&
  3307. prop.ResultType == VK_COMPONENT_TYPE_FLOAT16_KHR) {
  3308. found_fp16_256 = true;
  3309. }
  3310. if (prop.CType == VK_COMPONENT_TYPE_FLOAT32_KHR &&
  3311. prop.ResultType == VK_COMPONENT_TYPE_FLOAT32_KHR) {
  3312. found_fp32_256 = true;
  3313. }
  3314. }
  3315. }
  3316. }
  3317. if (found_fp16_128 && found_fp16_256 &&
  3318. found_fp32_128 && found_fp32_256 &&
  3319. coopmat2_props.cooperativeMatrixFlexibleDimensionsMaxDimension >= 512) {
  3320. device->coopmat2 = true;
  3321. }
  3322. }
  3323. #endif
  3324. }
  3325. if (!vk11_features.storageBuffer16BitAccess) {
  3326. std::cerr << "ggml_vulkan: device " << GGML_VK_NAME << idx << " does not support 16-bit storage." << std::endl;
  3327. throw std::runtime_error("Unsupported device");
  3328. }
  3329. device_extensions.push_back("VK_KHR_16bit_storage");
  3330. #ifdef GGML_VULKAN_VALIDATE
  3331. device_extensions.push_back("VK_KHR_shader_non_semantic_info");
  3332. #endif
  3333. if (device->fp16) {
  3334. device_extensions.push_back("VK_KHR_shader_float16_int8");
  3335. }
  3336. #if defined(VK_KHR_cooperative_matrix)
  3337. if (device->coopmat_support) {
  3338. // Query supported shapes
  3339. std::vector<VkCooperativeMatrixPropertiesKHR> cm_props;
  3340. PFN_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR pfn_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR =
  3341. (PFN_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR)vkGetInstanceProcAddr(vk_instance.instance, "vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR");
  3342. uint32_t cm_props_num;
  3343. pfn_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR(device->physical_device, &cm_props_num, nullptr);
  3344. cm_props.resize(cm_props_num);
  3345. for (auto& prop : cm_props) {
  3346. prop.sType = VK_STRUCTURE_TYPE_COOPERATIVE_MATRIX_PROPERTIES_KHR;
  3347. }
  3348. pfn_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR(device->physical_device, &cm_props_num, cm_props.data());
  3349. VK_LOG_DEBUG("ggml_vulkan: Cooperative Matrix Shapes: " << cm_props.size());
  3350. for (auto& prop : cm_props) {
  3351. VK_LOG_DEBUG("ggml_vulkan: M: " << prop.MSize << " N: " << prop.NSize << " K: " << prop.KSize << " A: " << vk::to_string((vk::ComponentTypeKHR)prop.AType) << " B: " << vk::to_string((vk::ComponentTypeKHR)prop.BType) << " C: " << vk::to_string((vk::ComponentTypeKHR)prop.CType) << " Result: " << vk::to_string((vk::ComponentTypeKHR)prop.ResultType) << " saturatingAccumulation: " << prop.saturatingAccumulation << " scope: " << vk::to_string((vk::ScopeKHR)prop.scope));
  3352. if ((vk::ComponentTypeKHR)prop.AType == vk::ComponentTypeKHR::eFloat16 &&
  3353. (vk::ComponentTypeKHR)prop.BType == vk::ComponentTypeKHR::eFloat16 &&
  3354. (vk::ScopeKHR)prop.scope == vk::ScopeKHR::eSubgroup
  3355. ) {
  3356. if ((vk::ComponentTypeKHR)prop.CType == vk::ComponentTypeKHR::eFloat32 &&
  3357. (vk::ComponentTypeKHR)prop.ResultType == vk::ComponentTypeKHR::eFloat32) {
  3358. // coopmat sizes not set yet
  3359. if (device->coopmat_m == 0) {
  3360. device->coopmat_acc_f32_support = true;
  3361. device->coopmat_m = prop.MSize;
  3362. device->coopmat_n = prop.NSize;
  3363. device->coopmat_k = prop.KSize;
  3364. } else if (device->coopmat_m == prop.MSize && device->coopmat_n == prop.NSize && device->coopmat_k == prop.KSize) {
  3365. // Only enable if shape is identical
  3366. device->coopmat_acc_f32_support = true;
  3367. }
  3368. if (prop.MSize == 16 && prop.NSize == 16 && prop.KSize == 16) {
  3369. device->coopmat_support_16x16x16_f32acc = true;
  3370. }
  3371. } else if ((vk::ComponentTypeKHR)prop.CType == vk::ComponentTypeKHR::eFloat16 &&
  3372. (vk::ComponentTypeKHR)prop.ResultType == vk::ComponentTypeKHR::eFloat16) {
  3373. // coopmat sizes not set yet
  3374. if (device->coopmat_m == 0) {
  3375. device->coopmat_acc_f16_support = true;
  3376. device->coopmat_m = prop.MSize;
  3377. device->coopmat_n = prop.NSize;
  3378. device->coopmat_k = prop.KSize;
  3379. } else if (device->coopmat_m == prop.MSize && device->coopmat_n == prop.NSize && device->coopmat_k == prop.KSize) {
  3380. // Only enable if shape is identical
  3381. device->coopmat_acc_f16_support = true;
  3382. }
  3383. if (prop.MSize == 16 && prop.NSize == 16 && prop.KSize == 16) {
  3384. device->coopmat_support_16x16x16_f16acc = true;
  3385. }
  3386. }
  3387. } else if ((vk::ComponentTypeKHR)prop.AType == vk::ComponentTypeKHR::eSint8 &&
  3388. (vk::ComponentTypeKHR)prop.BType == vk::ComponentTypeKHR::eSint8 &&
  3389. (vk::ComponentTypeKHR)prop.CType == vk::ComponentTypeKHR::eSint32 &&
  3390. (vk::ComponentTypeKHR)prop.ResultType == vk::ComponentTypeKHR::eSint32 &&
  3391. (vk::ScopeKHR)prop.scope == vk::ScopeKHR::eSubgroup &&
  3392. device->coopmat_int_m == 0
  3393. ) {
  3394. device->coopmat_int_support = true;
  3395. device->coopmat_int_m = prop.MSize;
  3396. device->coopmat_int_n = prop.NSize;
  3397. device->coopmat_int_k = prop.KSize;
  3398. }
  3399. #if defined(VK_KHR_shader_bfloat16) && defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  3400. if (prop.AType == VK_COMPONENT_TYPE_BFLOAT16_KHR &&
  3401. prop.BType == VK_COMPONENT_TYPE_BFLOAT16_KHR &&
  3402. prop.CType == VK_COMPONENT_TYPE_FLOAT32_KHR &&
  3403. prop.ResultType == VK_COMPONENT_TYPE_FLOAT32_KHR &&
  3404. (vk::ScopeKHR)prop.scope == vk::ScopeKHR::eSubgroup
  3405. ) {
  3406. // coopmat sizes not set yet
  3407. if (device->coopmat_m == 0) {
  3408. device->coopmat_bf16_support = true;
  3409. device->coopmat_m = prop.MSize;
  3410. device->coopmat_n = prop.NSize;
  3411. device->coopmat_k = prop.KSize;
  3412. } else if (device->coopmat_m == prop.MSize && device->coopmat_n == prop.NSize && device->coopmat_k == prop.KSize) {
  3413. // Only enable if shape is identical
  3414. device->coopmat_bf16_support = true;
  3415. }
  3416. }
  3417. #endif
  3418. }
  3419. if (device->coopmat_m == 0 || !device->coopmat_acc_f32_support) {
  3420. // No suitable matmul mode found
  3421. GGML_LOG_DEBUG("ggml_vulkan: WARNING: No suitable matrix core mode found. Disabling matrix cores.\n");
  3422. device->coopmat_support = false;
  3423. }
  3424. if (getenv("GGML_VK_DISABLE_BFLOAT16")) {
  3425. device->coopmat_bf16_support = false;
  3426. }
  3427. }
  3428. if (device->coopmat_support) {
  3429. device_extensions.push_back("VK_KHR_cooperative_matrix");
  3430. }
  3431. #if defined(VK_KHR_shader_bfloat16)
  3432. if (device->coopmat_bf16_support) {
  3433. device_extensions.push_back("VK_KHR_shader_bfloat16");
  3434. }
  3435. #endif
  3436. #endif
  3437. device->name = GGML_VK_NAME + std::to_string(idx);
  3438. device_create_info = {
  3439. vk::DeviceCreateFlags(),
  3440. device_queue_create_infos,
  3441. {},
  3442. device_extensions
  3443. };
  3444. device_create_info.setPNext(&device_features2);
  3445. device->device = device->physical_device.createDevice(device_create_info);
  3446. // Queues
  3447. ggml_vk_create_queue(device, device->compute_queue, compute_queue_family_index, 0, { vk::PipelineStageFlagBits::eComputeShader | vk::PipelineStageFlagBits::eTransfer }, false);
  3448. // Shaders
  3449. // Disable matmul tile sizes early if performance low or not supported
  3450. for (uint32_t i = 0; i < GGML_TYPE_COUNT; ++i) {
  3451. switch (device->vendor_id) {
  3452. #ifndef GGML_VULKAN_RUN_TESTS
  3453. case VK_VENDOR_ID_AMD:
  3454. case VK_VENDOR_ID_INTEL:
  3455. device->mul_mat_l[i] = false;
  3456. device->mul_mat_m[i] = true;
  3457. device->mul_mat_s[i] = true;
  3458. device->mul_mat_id_l[i] = false;
  3459. device->mul_mat_id_m[i] = true;
  3460. device->mul_mat_id_s[i] = true;
  3461. break;
  3462. case VK_VENDOR_ID_APPLE:
  3463. device->mul_mat_l[i] = false;
  3464. device->mul_mat_m[i] = true;
  3465. device->mul_mat_s[i] = false;
  3466. device->mul_mat_id_l[i] = false;
  3467. device->mul_mat_id_m[i] = true;
  3468. device->mul_mat_id_s[i] = false;
  3469. break;
  3470. #endif
  3471. default:
  3472. device->mul_mat_l[i] = true;
  3473. device->mul_mat_m[i] = true;
  3474. device->mul_mat_s[i] = true;
  3475. device->mul_mat_id_l[i] = true;
  3476. device->mul_mat_id_m[i] = true;
  3477. device->mul_mat_id_s[i] = true;
  3478. break;
  3479. }
  3480. }
  3481. std::vector<vk::DescriptorSetLayoutBinding> dsl_binding;
  3482. std::vector<vk::DescriptorBindingFlags> dsl_binding_flags;
  3483. for (uint32_t i = 0; i < MAX_PARAMETER_COUNT; i++) {
  3484. dsl_binding.push_back({i, vk::DescriptorType::eStorageBuffer, 1, vk::ShaderStageFlagBits::eCompute});
  3485. dsl_binding_flags.push_back({});
  3486. }
  3487. vk::DescriptorSetLayoutBindingFlagsCreateInfo dslbfci = { dsl_binding_flags };
  3488. vk::DescriptorSetLayoutCreateInfo descriptor_set_layout_create_info(
  3489. {},
  3490. dsl_binding);
  3491. descriptor_set_layout_create_info.setPNext(&dslbfci);
  3492. device->dsl = device->device.createDescriptorSetLayout(descriptor_set_layout_create_info);
  3493. ggml_vk_load_shaders(device);
  3494. if (!device->single_queue) {
  3495. const uint32_t transfer_queue_index = compute_queue_family_index == transfer_queue_family_index ? 1 : 0;
  3496. ggml_vk_create_queue(device, device->transfer_queue, transfer_queue_family_index, transfer_queue_index, { vk::PipelineStageFlagBits::eTransfer }, true);
  3497. } else {
  3498. // TODO: Use pointer or reference to avoid copy
  3499. device->transfer_queue.copyFrom(device->compute_queue);
  3500. device->transfer_queue.cmd_pool.init(device, &device->transfer_queue);
  3501. }
  3502. device->buffer_type = {
  3503. /* .iface = */ ggml_backend_vk_buffer_type_interface,
  3504. /* .device = */ ggml_backend_reg_dev_get(ggml_backend_vk_reg(), idx),
  3505. /* .context = */ new ggml_backend_vk_buffer_type_context{ device->name, device },
  3506. };
  3507. device->fence = device->device.createFence({});
  3508. device->idx = idx;
  3509. device->disable_fusion = getenv("GGML_VK_DISABLE_FUSION") != nullptr;
  3510. device->add_rms_fusion = !device->disable_fusion &&
  3511. device->subgroup_arithmetic &&
  3512. device->vendor_id != VK_VENDOR_ID_INTEL;
  3513. device->partials_binding_alignment =
  3514. std::max(4u, (uint32_t)device->properties.limits.minStorageBufferOffsetAlignment);
  3515. device->mmvq_mode = 0;
  3516. if (getenv("GGML_VK_DISABLE_MMVQ")) {
  3517. device->mmvq_mode = -1;
  3518. } else if (getenv("GGML_VK_FORCE_MMVQ")) {
  3519. device->mmvq_mode = 1;
  3520. }
  3521. return device;
  3522. }
  3523. return vk_instance.devices[idx];
  3524. }
  3525. static void ggml_vk_print_gpu_info(size_t idx) {
  3526. GGML_ASSERT(idx < vk_instance.device_indices.size());
  3527. size_t dev_num = vk_instance.device_indices[idx];
  3528. VK_LOG_DEBUG("ggml_vk_print_gpu_info(" << dev_num << ")");
  3529. GGML_ASSERT(vk_instance_initialized);
  3530. std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
  3531. if (dev_num >= devices.size()) {
  3532. std::cerr << "ggml_vulkan: Device with index " << dev_num << " does not exist." << std::endl;
  3533. throw std::runtime_error("Device not found");
  3534. }
  3535. vk::PhysicalDevice physical_device = devices[dev_num];
  3536. std::vector<vk::ExtensionProperties> ext_props = physical_device.enumerateDeviceExtensionProperties();
  3537. bool fp16_storage = false;
  3538. bool fp16_compute = false;
  3539. bool coopmat_support = false;
  3540. bool coopmat2_support = false;
  3541. bool integer_dot_product = false;
  3542. bool bfloat16_support = false;
  3543. for (auto properties : ext_props) {
  3544. if (strcmp("VK_KHR_16bit_storage", properties.extensionName) == 0) {
  3545. fp16_storage = true;
  3546. } else if (strcmp("VK_KHR_shader_float16_int8", properties.extensionName) == 0) {
  3547. fp16_compute = true;
  3548. #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  3549. } else if (strcmp("VK_KHR_cooperative_matrix", properties.extensionName) == 0 &&
  3550. !getenv("GGML_VK_DISABLE_COOPMAT")) {
  3551. coopmat_support = true;
  3552. #endif
  3553. #if defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  3554. } else if (strcmp("VK_NV_cooperative_matrix2", properties.extensionName) == 0 &&
  3555. !getenv("GGML_VK_DISABLE_COOPMAT2")) {
  3556. coopmat2_support = true;
  3557. #endif
  3558. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  3559. } else if (strcmp("VK_KHR_shader_integer_dot_product", properties.extensionName) == 0 &&
  3560. !getenv("GGML_VK_DISABLE_INTEGER_DOT_PRODUCT")) {
  3561. integer_dot_product = true;
  3562. #endif
  3563. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  3564. } else if (strcmp("VK_KHR_shader_bfloat16", properties.extensionName) == 0 &&
  3565. !getenv("GGML_VK_DISABLE_BFLOAT16")) {
  3566. bfloat16_support = true;
  3567. #endif
  3568. }
  3569. }
  3570. const vk_device_architecture device_architecture = get_device_architecture(physical_device);
  3571. const char* GGML_VK_DISABLE_F16 = getenv("GGML_VK_DISABLE_F16");
  3572. bool force_disable_f16 = GGML_VK_DISABLE_F16 != nullptr;
  3573. bool fp16 = !force_disable_f16 && fp16_storage && fp16_compute;
  3574. vk::PhysicalDeviceProperties2 props2;
  3575. vk::PhysicalDeviceMaintenance3Properties props3;
  3576. vk::PhysicalDeviceSubgroupProperties subgroup_props;
  3577. vk::PhysicalDeviceDriverProperties driver_props;
  3578. vk::PhysicalDeviceShaderIntegerDotProductPropertiesKHR shader_integer_dot_product_props;
  3579. props2.pNext = &props3;
  3580. props3.pNext = &subgroup_props;
  3581. subgroup_props.pNext = &driver_props;
  3582. // Pointer to the last chain element
  3583. VkBaseOutStructure * last_struct = (VkBaseOutStructure *)&driver_props;
  3584. if (integer_dot_product) {
  3585. last_struct->pNext = (VkBaseOutStructure *)&shader_integer_dot_product_props;
  3586. last_struct = (VkBaseOutStructure *)&shader_integer_dot_product_props;
  3587. }
  3588. physical_device.getProperties2(&props2);
  3589. VkPhysicalDeviceFeatures2 device_features2;
  3590. device_features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
  3591. device_features2.pNext = nullptr;
  3592. VkPhysicalDeviceVulkan11Features vk11_features;
  3593. vk11_features.pNext = nullptr;
  3594. vk11_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES;
  3595. device_features2.pNext = &vk11_features;
  3596. VkPhysicalDeviceVulkan12Features vk12_features;
  3597. vk12_features.pNext = nullptr;
  3598. vk12_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES;
  3599. vk11_features.pNext = &vk12_features;
  3600. // Pointer to the last chain element
  3601. last_struct = (VkBaseOutStructure *)&vk12_features;
  3602. #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  3603. VkPhysicalDeviceCooperativeMatrixFeaturesKHR coopmat_features;
  3604. coopmat_features.pNext = nullptr;
  3605. coopmat_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_FEATURES_KHR;
  3606. coopmat_features.cooperativeMatrix = VK_FALSE;
  3607. if (coopmat_support) {
  3608. last_struct->pNext = (VkBaseOutStructure *)&coopmat_features;
  3609. last_struct = (VkBaseOutStructure *)&coopmat_features;
  3610. }
  3611. #endif
  3612. VkPhysicalDeviceShaderIntegerDotProductFeaturesKHR shader_integer_dot_product_features {};
  3613. shader_integer_dot_product_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_INTEGER_DOT_PRODUCT_FEATURES_KHR;
  3614. if (integer_dot_product) {
  3615. last_struct->pNext = (VkBaseOutStructure *)&shader_integer_dot_product_features;
  3616. last_struct = (VkBaseOutStructure *)&shader_integer_dot_product_features;
  3617. }
  3618. #if defined(VK_KHR_shader_bfloat16)
  3619. VkPhysicalDeviceShaderBfloat16FeaturesKHR bfloat16_features {};
  3620. bfloat16_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_BFLOAT16_FEATURES_KHR;
  3621. if (bfloat16_support) {
  3622. last_struct->pNext = (VkBaseOutStructure *)&bfloat16_features;
  3623. last_struct = (VkBaseOutStructure *)&bfloat16_features;
  3624. }
  3625. #endif
  3626. vkGetPhysicalDeviceFeatures2(physical_device, &device_features2);
  3627. fp16 = fp16 && vk12_features.shaderFloat16;
  3628. #if defined(VK_KHR_shader_bfloat16)
  3629. bool bf16 = bfloat16_support && bfloat16_features.shaderBFloat16Type;
  3630. #else
  3631. bool bf16 = false;
  3632. #endif
  3633. uint32_t default_subgroup_size = get_subgroup_size("", device_architecture);
  3634. const size_t subgroup_size = (default_subgroup_size != 0) ? default_subgroup_size : subgroup_props.subgroupSize;
  3635. const bool uma = props2.properties.deviceType == vk::PhysicalDeviceType::eIntegratedGpu;
  3636. integer_dot_product = integer_dot_product
  3637. && shader_integer_dot_product_props.integerDotProduct4x8BitPackedSignedAccelerated
  3638. && shader_integer_dot_product_features.shaderIntegerDotProduct;
  3639. coopmat_support = coopmat_support
  3640. #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  3641. && coopmat_features.cooperativeMatrix
  3642. #endif
  3643. && ggml_vk_khr_cooperative_matrix_support(props2.properties, driver_props, device_architecture);
  3644. std::string matrix_cores = coopmat2_support ? "NV_coopmat2" : coopmat_support ? "KHR_coopmat" : "none";
  3645. std::string device_name = props2.properties.deviceName.data();
  3646. GGML_LOG_DEBUG("ggml_vulkan: %zu = %s (%s) | uma: %d | fp16: %d | bf16: %d | warp size: %zu | shared memory: %d | int dot: %d | matrix cores: %s\n",
  3647. idx, device_name.c_str(), driver_props.driverName.data(), uma, fp16, bf16, subgroup_size,
  3648. props2.properties.limits.maxComputeSharedMemorySize, integer_dot_product, matrix_cores.c_str());
  3649. if (props2.properties.deviceType == vk::PhysicalDeviceType::eCpu) {
  3650. GGML_LOG_DEBUG("ggml_vulkan: Warning: Device type is CPU. This is probably not the device you want.\n");
  3651. }
  3652. }
  3653. static bool ggml_vk_instance_validation_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions);
  3654. static bool ggml_vk_instance_portability_enumeration_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions);
  3655. static bool ggml_vk_instance_debug_utils_ext_available(const std::vector<vk::ExtensionProperties> & instance_extensions);
  3656. static void ggml_vk_instance_init() {
  3657. if (vk_instance_initialized) {
  3658. return;
  3659. }
  3660. VK_LOG_DEBUG("ggml_vk_instance_init()");
  3661. uint32_t api_version = vk::enumerateInstanceVersion();
  3662. if (api_version < VK_API_VERSION_1_2) {
  3663. std::cerr << "ggml_vulkan: Error: Vulkan 1.2 required." << std::endl;
  3664. GGML_ABORT("fatal error");
  3665. }
  3666. vk::ApplicationInfo app_info{ "ggml-vulkan", 1, nullptr, 0, api_version };
  3667. const std::vector<vk::ExtensionProperties> instance_extensions = vk::enumerateInstanceExtensionProperties();
  3668. const bool validation_ext = ggml_vk_instance_validation_ext_available(instance_extensions);
  3669. #ifdef __APPLE__
  3670. const bool portability_enumeration_ext = ggml_vk_instance_portability_enumeration_ext_available(instance_extensions);
  3671. #endif
  3672. const bool debug_utils_ext = ggml_vk_instance_debug_utils_ext_available(instance_extensions) && getenv("GGML_VK_DEBUG_MARKERS") != nullptr;
  3673. std::vector<const char*> layers;
  3674. if (validation_ext) {
  3675. layers.push_back("VK_LAYER_KHRONOS_validation");
  3676. }
  3677. std::vector<const char*> extensions;
  3678. if (validation_ext) {
  3679. extensions.push_back("VK_EXT_validation_features");
  3680. }
  3681. #ifdef __APPLE__
  3682. if (portability_enumeration_ext) {
  3683. extensions.push_back("VK_KHR_portability_enumeration");
  3684. }
  3685. #endif
  3686. if (debug_utils_ext) {
  3687. extensions.push_back("VK_EXT_debug_utils");
  3688. }
  3689. vk::InstanceCreateInfo instance_create_info(vk::InstanceCreateFlags{}, &app_info, layers, extensions);
  3690. #ifdef __APPLE__
  3691. if (portability_enumeration_ext) {
  3692. instance_create_info.flags |= vk::InstanceCreateFlagBits::eEnumeratePortabilityKHR;
  3693. }
  3694. #endif
  3695. std::vector<vk::ValidationFeatureEnableEXT> features_enable;
  3696. vk::ValidationFeaturesEXT validation_features;
  3697. if (validation_ext) {
  3698. features_enable = { vk::ValidationFeatureEnableEXT::eBestPractices };
  3699. validation_features = {
  3700. features_enable,
  3701. {},
  3702. };
  3703. validation_features.setPNext(nullptr);
  3704. instance_create_info.setPNext(&validation_features);
  3705. GGML_LOG_DEBUG("ggml_vulkan: Validation layers enabled\n");
  3706. }
  3707. vk_instance.instance = vk::createInstance(instance_create_info);
  3708. vk_instance_initialized = true;
  3709. if (debug_utils_ext) {
  3710. vk_instance.debug_utils_support = true;
  3711. vk_instance.pfn_vkSetDebugUtilsObjectNameEXT = (PFN_vkSetDebugUtilsObjectNameEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkSetDebugUtilsObjectNameEXT");
  3712. vk_instance.pfn_vkQueueBeginDebugUtilsLabelEXT = (PFN_vkQueueBeginDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkQueueBeginDebugUtilsLabelEXT");
  3713. vk_instance.pfn_vkQueueEndDebugUtilsLabelEXT = (PFN_vkQueueEndDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkQueueEndDebugUtilsLabelEXT");
  3714. vk_instance.pfn_vkCmdBeginDebugUtilsLabelEXT = (PFN_vkCmdBeginDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkCmdBeginDebugUtilsLabelEXT");
  3715. vk_instance.pfn_vkCmdEndDebugUtilsLabelEXT = (PFN_vkCmdEndDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkCmdEndDebugUtilsLabelEXT");
  3716. vk_instance.pfn_vkCmdInsertDebugUtilsLabelEXT = (PFN_vkCmdInsertDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkCmdInsertDebugUtilsLabelEXT");
  3717. }
  3718. vk_perf_logger_enabled = getenv("GGML_VK_PERF_LOGGER") != nullptr;
  3719. std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
  3720. // Emulate behavior of CUDA_VISIBLE_DEVICES for Vulkan
  3721. char * devices_env = getenv("GGML_VK_VISIBLE_DEVICES");
  3722. if (devices_env != nullptr) {
  3723. size_t num_available_devices = devices.size();
  3724. std::string devices(devices_env);
  3725. std::replace(devices.begin(), devices.end(), ',', ' ');
  3726. std::stringstream ss(devices);
  3727. size_t tmp;
  3728. while (ss >> tmp) {
  3729. if(tmp >= num_available_devices) {
  3730. std::cerr << "ggml_vulkan: Invalid device index " << tmp << " in GGML_VK_VISIBLE_DEVICES." << std::endl;
  3731. throw std::runtime_error("Invalid Vulkan device index");
  3732. }
  3733. vk_instance.device_indices.push_back(tmp);
  3734. }
  3735. } else {
  3736. // If no vulkan devices are found, return early
  3737. if (devices.empty()) {
  3738. GGML_LOG_INFO("ggml_vulkan: No devices found.\n");
  3739. return;
  3740. }
  3741. // Default to using all dedicated GPUs
  3742. for (size_t i = 0; i < devices.size(); i++) {
  3743. vk::PhysicalDeviceProperties2 new_props;
  3744. vk::PhysicalDeviceDriverProperties new_driver;
  3745. vk::PhysicalDeviceIDProperties new_id;
  3746. new_props.pNext = &new_driver;
  3747. new_driver.pNext = &new_id;
  3748. devices[i].getProperties2(&new_props);
  3749. if (new_props.properties.deviceType == vk::PhysicalDeviceType::eDiscreteGpu) {
  3750. // Check if there are two physical devices corresponding to the same GPU
  3751. auto old_device = std::find_if(
  3752. vk_instance.device_indices.begin(),
  3753. vk_instance.device_indices.end(),
  3754. [&devices, &new_id](const size_t k){
  3755. vk::PhysicalDeviceProperties2 old_props;
  3756. vk::PhysicalDeviceIDProperties old_id;
  3757. old_props.pNext = &old_id;
  3758. devices[k].getProperties2(&old_props);
  3759. return std::equal(std::begin(old_id.deviceUUID), std::end(old_id.deviceUUID), std::begin(new_id.deviceUUID));
  3760. }
  3761. );
  3762. if (old_device == vk_instance.device_indices.end()) {
  3763. vk_instance.device_indices.push_back(i);
  3764. } else {
  3765. // There can be two physical devices corresponding to the same GPU if there are 2 different drivers
  3766. // This can cause error when splitting layers aross the devices, need to keep only 1
  3767. VK_LOG_DEBUG("Device " << i << " and device " << *old_device << " have the same deviceUUID");
  3768. vk::PhysicalDeviceProperties2 old_props;
  3769. vk::PhysicalDeviceDriverProperties old_driver;
  3770. old_props.pNext = &old_driver;
  3771. devices[*old_device].getProperties2(&old_props);
  3772. std::map<vk::DriverId, int> driver_priorities {};
  3773. int old_priority = std::numeric_limits<int>::max();
  3774. int new_priority = std::numeric_limits<int>::max();
  3775. // Check https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VkDriverId.html for the list of driver id
  3776. // Smaller number -> higher priority
  3777. switch (old_props.properties.vendorID) {
  3778. case VK_VENDOR_ID_AMD:
  3779. driver_priorities[vk::DriverId::eMesaRadv] = 1;
  3780. driver_priorities[vk::DriverId::eAmdOpenSource] = 2;
  3781. driver_priorities[vk::DriverId::eAmdProprietary] = 3;
  3782. break;
  3783. case VK_VENDOR_ID_INTEL:
  3784. driver_priorities[vk::DriverId::eIntelOpenSourceMESA] = 1;
  3785. driver_priorities[vk::DriverId::eIntelProprietaryWindows] = 2;
  3786. break;
  3787. case VK_VENDOR_ID_NVIDIA:
  3788. driver_priorities[vk::DriverId::eNvidiaProprietary] = 1;
  3789. #if defined(VK_API_VERSION_1_3) && VK_HEADER_VERSION >= 235
  3790. driver_priorities[vk::DriverId::eMesaNvk] = 2;
  3791. #endif
  3792. break;
  3793. }
  3794. if (driver_priorities.count(old_driver.driverID)) {
  3795. old_priority = driver_priorities[old_driver.driverID];
  3796. }
  3797. if (driver_priorities.count(new_driver.driverID)) {
  3798. new_priority = driver_priorities[new_driver.driverID];
  3799. }
  3800. if (new_priority < old_priority) {
  3801. auto r = std::remove(vk_instance.device_indices.begin(), vk_instance.device_indices.end(), *old_device);
  3802. vk_instance.device_indices.erase(r, vk_instance.device_indices.end());
  3803. vk_instance.device_indices.push_back(i);
  3804. VK_LOG_DEBUG("Prioritize device " << i << " driver " << new_driver.driverName << " over device " << *old_device << " driver " << old_driver.driverName);
  3805. }
  3806. else {
  3807. VK_LOG_DEBUG("Prioritize device " << *old_device << " driver " << old_driver.driverName << " over device " << i << " driver " << new_driver.driverName << std::endl);
  3808. }
  3809. }
  3810. }
  3811. }
  3812. // If no dedicated GPUs found, fall back to the first non-CPU device.
  3813. // If only CPU devices are available, return without devices.
  3814. if (vk_instance.device_indices.empty()) {
  3815. for (size_t i = 0; i < devices.size(); i++) {
  3816. if (devices[i].getProperties().deviceType != vk::PhysicalDeviceType::eCpu) {
  3817. vk_instance.device_indices.push_back(i);
  3818. break;
  3819. }
  3820. }
  3821. }
  3822. if (vk_instance.device_indices.empty()) {
  3823. GGML_LOG_INFO("ggml_vulkan: No devices found.\n");
  3824. return;
  3825. }
  3826. }
  3827. GGML_LOG_DEBUG("ggml_vulkan: Found %zu Vulkan devices:\n", vk_instance.device_indices.size());
  3828. for (size_t i = 0; i < vk_instance.device_indices.size(); i++) {
  3829. vk::PhysicalDevice vkdev = devices[vk_instance.device_indices[i]];
  3830. std::vector<vk::ExtensionProperties> extensionprops = vkdev.enumerateDeviceExtensionProperties();
  3831. bool membudget_supported = false;
  3832. for (const auto & ext : extensionprops) {
  3833. if (strcmp(VK_EXT_MEMORY_BUDGET_EXTENSION_NAME, ext.extensionName) == 0) {
  3834. membudget_supported = true;
  3835. break;
  3836. }
  3837. }
  3838. vk_instance.device_supports_membudget.push_back(membudget_supported);
  3839. ggml_vk_print_gpu_info(i);
  3840. }
  3841. }
  3842. static void ggml_vk_init(ggml_backend_vk_context * ctx, size_t idx) {
  3843. VK_LOG_DEBUG("ggml_vk_init(" << ctx->name << ", " << idx << ")");
  3844. ggml_vk_instance_init();
  3845. GGML_ASSERT(idx < vk_instance.device_indices.size());
  3846. ctx->name = GGML_VK_NAME + std::to_string(idx);
  3847. ctx->device = ggml_vk_get_device(idx);
  3848. ctx->semaphore_idx = 0;
  3849. ctx->event_idx = 0;
  3850. ctx->prealloc_size_x = 0;
  3851. ctx->prealloc_size_y = 0;
  3852. ctx->prealloc_size_split_k = 0;
  3853. ctx->fence = ctx->device->device.createFence({});
  3854. ctx->almost_ready_fence = ctx->device->device.createFence({});
  3855. ctx->compute_cmd_pool.init(ctx->device, &ctx->device->compute_queue);
  3856. ctx->transfer_cmd_pool.init(ctx->device, &ctx->device->transfer_queue);
  3857. #ifdef GGML_VULKAN_CHECK_RESULTS
  3858. const char* skip_checks = getenv("GGML_VULKAN_SKIP_CHECKS");
  3859. vk_skip_checks = (skip_checks == NULL ? 0 : atoi(skip_checks));
  3860. const char* output_tensor = getenv("GGML_VULKAN_OUTPUT_TENSOR");
  3861. vk_output_tensor = (output_tensor == NULL ? 0 : atoi(output_tensor));
  3862. #endif
  3863. }
  3864. static vk_pipeline ggml_vk_get_to_fp16(ggml_backend_vk_context * ctx, ggml_type type) {
  3865. VK_LOG_DEBUG("ggml_vk_get_to_fp16()");
  3866. switch (type) {
  3867. case GGML_TYPE_F32:
  3868. case GGML_TYPE_Q4_0:
  3869. case GGML_TYPE_Q4_1:
  3870. case GGML_TYPE_Q5_0:
  3871. case GGML_TYPE_Q5_1:
  3872. case GGML_TYPE_Q8_0:
  3873. case GGML_TYPE_Q2_K:
  3874. case GGML_TYPE_Q3_K:
  3875. case GGML_TYPE_Q4_K:
  3876. case GGML_TYPE_Q5_K:
  3877. case GGML_TYPE_Q6_K:
  3878. case GGML_TYPE_IQ1_S:
  3879. case GGML_TYPE_IQ1_M:
  3880. case GGML_TYPE_IQ2_XXS:
  3881. case GGML_TYPE_IQ2_XS:
  3882. case GGML_TYPE_IQ2_S:
  3883. case GGML_TYPE_IQ3_XXS:
  3884. case GGML_TYPE_IQ3_S:
  3885. case GGML_TYPE_IQ4_XS:
  3886. case GGML_TYPE_IQ4_NL:
  3887. case GGML_TYPE_MXFP4:
  3888. break;
  3889. default:
  3890. return nullptr;
  3891. }
  3892. return ctx->device->pipeline_dequant[type];
  3893. }
  3894. static vk_matmul_pipeline ggml_vk_get_mul_mat_mat_pipeline(ggml_backend_vk_context * ctx, ggml_type src0_type, ggml_type src1_type, ggml_prec prec) {
  3895. VK_LOG_DEBUG("ggml_vk_get_mul_mat_mat_pipeline(" << ggml_type_name(src0_type) << ", " << ggml_type_name(src1_type) << ", " << prec << ")");
  3896. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  3897. return ctx->device->pipeline_matmul_f32;
  3898. }
  3899. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F16) {
  3900. return ctx->device->pipeline_matmul_f32_f16;
  3901. }
  3902. if (src0_type == GGML_TYPE_BF16 && src1_type == GGML_TYPE_BF16) {
  3903. return ctx->device->pipeline_matmul_bf16;
  3904. }
  3905. if (prec == GGML_PREC_DEFAULT && ctx->device->fp16 && !(ctx->device->coopmat_support && !ctx->device->coopmat_acc_f16_support)) {
  3906. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  3907. return ctx->device->pipeline_matmul_f16_f32.f16acc;
  3908. }
  3909. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  3910. return ctx->device->pipeline_matmul_f16.f16acc;
  3911. }
  3912. } else {
  3913. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  3914. return ctx->device->pipeline_matmul_f16_f32.f32acc;
  3915. }
  3916. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  3917. return ctx->device->pipeline_matmul_f16.f32acc;
  3918. }
  3919. }
  3920. // MMQ
  3921. if (src1_type == GGML_TYPE_Q8_1) {
  3922. vk_matmul_pipeline pipelines = (ctx->device->fp16 && prec == GGML_PREC_DEFAULT) ? ctx->device->pipeline_dequant_mul_mat_mat_q8_1[src0_type].f16acc : ctx->device->pipeline_dequant_mul_mat_mat_q8_1[src0_type].f32acc;
  3923. if (pipelines->s == nullptr && pipelines->m == nullptr && pipelines->l == nullptr) {
  3924. return nullptr;
  3925. }
  3926. return pipelines;
  3927. }
  3928. if (src1_type != GGML_TYPE_F32 && !ctx->device->coopmat2) {
  3929. return nullptr;
  3930. }
  3931. switch (src0_type) {
  3932. case GGML_TYPE_Q4_0:
  3933. case GGML_TYPE_Q4_1:
  3934. case GGML_TYPE_Q5_0:
  3935. case GGML_TYPE_Q5_1:
  3936. case GGML_TYPE_Q8_0:
  3937. case GGML_TYPE_Q2_K:
  3938. case GGML_TYPE_Q3_K:
  3939. case GGML_TYPE_Q4_K:
  3940. case GGML_TYPE_Q5_K:
  3941. case GGML_TYPE_Q6_K:
  3942. case GGML_TYPE_IQ1_S:
  3943. case GGML_TYPE_IQ1_M:
  3944. case GGML_TYPE_IQ2_XXS:
  3945. case GGML_TYPE_IQ2_XS:
  3946. case GGML_TYPE_IQ2_S:
  3947. case GGML_TYPE_IQ3_XXS:
  3948. case GGML_TYPE_IQ3_S:
  3949. case GGML_TYPE_IQ4_XS:
  3950. case GGML_TYPE_IQ4_NL:
  3951. case GGML_TYPE_MXFP4:
  3952. break;
  3953. default:
  3954. return nullptr;
  3955. }
  3956. if (ctx->device->coopmat2) {
  3957. assert(src1_type == GGML_TYPE_F16);
  3958. return prec == GGML_PREC_DEFAULT ? ctx->device->pipeline_dequant_mul_mat_mat_f16[src0_type].f16acc : ctx->device->pipeline_dequant_mul_mat_mat_f16[src0_type].f32acc;
  3959. }
  3960. if (ctx->device->coopmat_support) {
  3961. return (ctx->device->fp16 && ctx->device->coopmat_acc_f16_support && prec == GGML_PREC_DEFAULT) ? ctx->device->pipeline_dequant_mul_mat_mat[src0_type].f16acc : ctx->device->pipeline_dequant_mul_mat_mat[src0_type].f32acc;
  3962. }
  3963. return (ctx->device->fp16 && prec == GGML_PREC_DEFAULT) ? ctx->device->pipeline_dequant_mul_mat_mat[src0_type].f16acc : ctx->device->pipeline_dequant_mul_mat_mat[src0_type].f32acc;
  3964. }
  3965. static vk_pipeline ggml_vk_get_dequantize_mul_mat_vec(ggml_backend_vk_context * ctx, ggml_type a_type, ggml_type b_type, uint32_t num_cols, uint32_t m, uint32_t k) {
  3966. VK_LOG_DEBUG("ggml_vk_get_dequantize_mul_mat_vec()");
  3967. GGML_ASSERT(b_type == GGML_TYPE_F32 || b_type == GGML_TYPE_F16 || b_type == GGML_TYPE_Q8_1);
  3968. GGML_ASSERT(num_cols >= 1 && num_cols <= mul_mat_vec_max_cols);
  3969. if (b_type == GGML_TYPE_Q8_1) {
  3970. switch (a_type) {
  3971. case GGML_TYPE_Q4_0:
  3972. case GGML_TYPE_Q4_1:
  3973. case GGML_TYPE_Q5_0:
  3974. case GGML_TYPE_Q5_1:
  3975. case GGML_TYPE_Q8_0:
  3976. break;
  3977. default:
  3978. return nullptr;
  3979. }
  3980. }
  3981. switch (a_type) {
  3982. case GGML_TYPE_F32:
  3983. case GGML_TYPE_F16:
  3984. case GGML_TYPE_BF16:
  3985. case GGML_TYPE_Q4_0:
  3986. case GGML_TYPE_Q4_1:
  3987. case GGML_TYPE_Q5_0:
  3988. case GGML_TYPE_Q5_1:
  3989. case GGML_TYPE_Q8_0:
  3990. case GGML_TYPE_Q2_K:
  3991. case GGML_TYPE_Q3_K:
  3992. case GGML_TYPE_Q4_K:
  3993. case GGML_TYPE_Q5_K:
  3994. case GGML_TYPE_Q6_K:
  3995. case GGML_TYPE_IQ1_S:
  3996. case GGML_TYPE_IQ1_M:
  3997. case GGML_TYPE_IQ2_XXS:
  3998. case GGML_TYPE_IQ2_XS:
  3999. case GGML_TYPE_IQ2_S:
  4000. case GGML_TYPE_IQ3_XXS:
  4001. case GGML_TYPE_IQ3_S:
  4002. case GGML_TYPE_IQ4_XS:
  4003. case GGML_TYPE_IQ4_NL:
  4004. case GGML_TYPE_MXFP4:
  4005. break;
  4006. default:
  4007. return nullptr;
  4008. }
  4009. // heuristic to choose workgroup size
  4010. uint32_t dmmv_wg = DMMV_WG_SIZE_SUBGROUP;
  4011. if ((ctx->device->vendor_id == VK_VENDOR_ID_NVIDIA && ctx->device->architecture != vk_device_architecture::NVIDIA_PRE_TURING) || ctx->device->vendor_id == VK_VENDOR_ID_INTEL) {
  4012. // Prefer larger workgroups when M is small, to spread the work out more
  4013. // and keep more SMs busy.
  4014. // q6_k seems to prefer small workgroup size even for "medium" values of M.
  4015. if (a_type == GGML_TYPE_Q6_K) {
  4016. if (m < 4096 && k >= 1024) {
  4017. dmmv_wg = DMMV_WG_SIZE_LARGE;
  4018. }
  4019. } else {
  4020. if (m <= 8192 && k >= 1024) {
  4021. dmmv_wg = DMMV_WG_SIZE_LARGE;
  4022. }
  4023. }
  4024. }
  4025. if (b_type == GGML_TYPE_Q8_1) {
  4026. if (ctx->device->vendor_id == VK_VENDOR_ID_INTEL) {
  4027. dmmv_wg = DMMV_WG_SIZE_SUBGROUP;
  4028. }
  4029. return ctx->device->pipeline_dequant_mul_mat_vec_q8_1_f32[dmmv_wg][a_type][num_cols-1];
  4030. }
  4031. return b_type == GGML_TYPE_F32 ? ctx->device->pipeline_dequant_mul_mat_vec_f32_f32[dmmv_wg][a_type][num_cols-1] : ctx->device->pipeline_dequant_mul_mat_vec_f16_f32[dmmv_wg][a_type][num_cols-1];
  4032. }
  4033. static vk_matmul_pipeline ggml_vk_get_mul_mat_mat_id_pipeline(ggml_backend_vk_context * ctx, ggml_type src0_type, ggml_type src1_type, ggml_prec prec) {
  4034. VK_LOG_DEBUG("ggml_vk_get_mul_mat_mat_id_pipeline()");
  4035. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  4036. return ctx->device->pipeline_matmul_id_f32;
  4037. }
  4038. if (src0_type == GGML_TYPE_BF16 && src1_type == GGML_TYPE_BF16) {
  4039. return ctx->device->pipeline_matmul_id_bf16;
  4040. }
  4041. if (prec == GGML_PREC_DEFAULT && ctx->device->fp16 && !(ctx->device->coopmat_support && !ctx->device->coopmat_acc_f16_support)) {
  4042. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  4043. return ctx->device->pipeline_matmul_id_f16_f32.f16acc;
  4044. }
  4045. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  4046. return ctx->device->pipeline_matmul_id_f16.f16acc;
  4047. }
  4048. } else {
  4049. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  4050. return ctx->device->pipeline_matmul_id_f16_f32.f32acc;
  4051. }
  4052. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  4053. return ctx->device->pipeline_matmul_id_f16.f32acc;
  4054. }
  4055. }
  4056. GGML_ASSERT(src1_type == GGML_TYPE_F32 || (ctx->device->coopmat2 && src1_type == GGML_TYPE_F16));
  4057. switch (src0_type) {
  4058. case GGML_TYPE_Q4_0:
  4059. case GGML_TYPE_Q4_1:
  4060. case GGML_TYPE_Q5_0:
  4061. case GGML_TYPE_Q5_1:
  4062. case GGML_TYPE_Q8_0:
  4063. case GGML_TYPE_Q2_K:
  4064. case GGML_TYPE_Q3_K:
  4065. case GGML_TYPE_Q4_K:
  4066. case GGML_TYPE_Q5_K:
  4067. case GGML_TYPE_Q6_K:
  4068. case GGML_TYPE_IQ1_S:
  4069. case GGML_TYPE_IQ1_M:
  4070. case GGML_TYPE_IQ2_XXS:
  4071. case GGML_TYPE_IQ2_XS:
  4072. case GGML_TYPE_IQ2_S:
  4073. case GGML_TYPE_IQ3_XXS:
  4074. case GGML_TYPE_IQ3_S:
  4075. case GGML_TYPE_IQ4_XS:
  4076. case GGML_TYPE_IQ4_NL:
  4077. case GGML_TYPE_MXFP4:
  4078. break;
  4079. default:
  4080. return nullptr;
  4081. }
  4082. // XXX TODO 'prec' is not actually allowed in mul_mat_id.
  4083. bool prefer_fp16acc = ctx->device->fp16 /*&& prec == GGML_PREC_DEFAULT*/;
  4084. bool support_fp16acc = ctx->device->pipeline_dequant_mul_mat_mat_id[src0_type].f16acc != nullptr;
  4085. bool support_fp32acc = ctx->device->pipeline_dequant_mul_mat_mat_id[src0_type].f32acc != nullptr;
  4086. if (support_fp16acc && (prefer_fp16acc || !support_fp32acc)) {
  4087. return ctx->device->pipeline_dequant_mul_mat_mat_id[src0_type].f16acc;
  4088. } else {
  4089. GGML_ASSERT(support_fp32acc);
  4090. return ctx->device->pipeline_dequant_mul_mat_mat_id[src0_type].f32acc;
  4091. }
  4092. }
  4093. static vk_pipeline ggml_vk_get_dequantize_mul_mat_vec_id(ggml_backend_vk_context * ctx, ggml_type a_type, ggml_type b_type) {
  4094. VK_LOG_DEBUG("ggml_vk_get_dequantize_mul_mat_vec_id()");
  4095. GGML_ASSERT(b_type == GGML_TYPE_F32);
  4096. switch (a_type) {
  4097. case GGML_TYPE_F32:
  4098. case GGML_TYPE_F16:
  4099. case GGML_TYPE_BF16:
  4100. case GGML_TYPE_Q4_0:
  4101. case GGML_TYPE_Q4_1:
  4102. case GGML_TYPE_Q5_0:
  4103. case GGML_TYPE_Q5_1:
  4104. case GGML_TYPE_Q8_0:
  4105. case GGML_TYPE_Q2_K:
  4106. case GGML_TYPE_Q3_K:
  4107. case GGML_TYPE_Q4_K:
  4108. case GGML_TYPE_Q5_K:
  4109. case GGML_TYPE_Q6_K:
  4110. case GGML_TYPE_IQ1_S:
  4111. case GGML_TYPE_IQ1_M:
  4112. case GGML_TYPE_IQ2_XXS:
  4113. case GGML_TYPE_IQ2_XS:
  4114. case GGML_TYPE_IQ2_S:
  4115. case GGML_TYPE_IQ3_XXS:
  4116. case GGML_TYPE_IQ3_S:
  4117. case GGML_TYPE_IQ4_XS:
  4118. case GGML_TYPE_IQ4_NL:
  4119. case GGML_TYPE_MXFP4:
  4120. break;
  4121. default:
  4122. return nullptr;
  4123. }
  4124. return ctx->device->pipeline_dequant_mul_mat_vec_id_f32[a_type];
  4125. }
  4126. static vk_buffer ggml_vk_pool_malloc(ggml_backend_vk_context * ctx, size_t size) {
  4127. VK_LOG_DEBUG("ggml_vk_pool_malloc(" << size << ")");
  4128. VK_LOG_MEMORY("ggml_vk_pool_malloc");
  4129. int best_i = -1;
  4130. size_t best_size = std::numeric_limits<size_t>::max(); //smallest unused buffer that fits our needs
  4131. int worst_i = -1;
  4132. size_t worst_size = 0; //largest unused buffer seen so far
  4133. for (int i = 0; i < MAX_VK_BUFFERS; ++i) {
  4134. vk_buffer &b = ctx->buffer_pool[i];
  4135. if (b != nullptr && b->size >= size && b->size < best_size) {
  4136. best_i = i;
  4137. best_size = b->size;
  4138. }
  4139. if (b != nullptr && b->size > worst_size) {
  4140. worst_i = i;
  4141. worst_size = b->size;
  4142. }
  4143. }
  4144. if(best_i != -1) {
  4145. //found the smallest buffer that fits our needs
  4146. vk_buffer b = ctx->buffer_pool[best_i];
  4147. ctx->buffer_pool[best_i].reset();
  4148. return b;
  4149. }
  4150. if(worst_i != -1) {
  4151. //no buffer that fits our needs, resize largest one to save memory
  4152. vk_buffer& b = ctx->buffer_pool[worst_i];
  4153. ggml_vk_destroy_buffer(b);
  4154. }
  4155. return ggml_vk_create_buffer_device(ctx->device, size);
  4156. }
  4157. static void ggml_vk_pool_free(ggml_backend_vk_context * ctx, vk_buffer& buffer) {
  4158. VK_LOG_DEBUG("ggml_vk_pool_free(" << buffer->size << ")");
  4159. for (int i = 0; i < MAX_VK_BUFFERS; ++i) {
  4160. vk_buffer& b = ctx->buffer_pool[i];
  4161. if (b == nullptr) {
  4162. b = buffer;
  4163. return;
  4164. }
  4165. }
  4166. std::cerr << "ggml_vulkan: WARNING: vk buffer pool full, increase MAX_VK_BUFFERS" << std::endl;
  4167. ggml_vk_destroy_buffer(buffer);
  4168. }
  4169. // Returns an available temporary buffer that may only be used temporarily, it will be reused
  4170. static vk_buffer ggml_vk_create_buffer_temp(ggml_backend_vk_context * ctx, size_t size) {
  4171. // Try to find existing temp buffer with enough capacity
  4172. for (auto& buffer : ctx->gc.temp_buffers) {
  4173. if (buffer->size >= size) {
  4174. return buffer;
  4175. }
  4176. }
  4177. VK_LOG_MEMORY("ggml_vk_create_buffer_temp(" << size << ")");
  4178. // Otherwise create new buffer
  4179. vk_buffer buf = ggml_vk_pool_malloc(ctx, size);
  4180. ctx->gc.temp_buffers.push_back(buf);
  4181. return buf;
  4182. }
  4183. static void * ggml_vk_host_malloc(vk_device& device, size_t size) {
  4184. VK_LOG_MEMORY("ggml_vk_host_malloc(" << size << ")");
  4185. vk_buffer buf = ggml_vk_create_buffer(device, size,
  4186. {vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostCached,
  4187. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent});
  4188. if(!(buf->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible)) {
  4189. fprintf(stderr, "WARNING: failed to allocate %.2f MB of pinned memory\n",
  4190. size/1024.0/1024.0);
  4191. device->device.freeMemory(buf->device_memory);
  4192. device->device.destroyBuffer(buf->buffer);
  4193. return nullptr;
  4194. }
  4195. std::lock_guard<std::recursive_mutex> guard(device->mutex);
  4196. device->pinned_memory.push_back(std::make_tuple(buf->ptr, size, buf));
  4197. return buf->ptr;
  4198. }
  4199. static void ggml_vk_host_free(vk_device& device, void* ptr) {
  4200. if (ptr == nullptr) {
  4201. return;
  4202. }
  4203. VK_LOG_MEMORY("ggml_vk_host_free(" << ptr << ")");
  4204. std::lock_guard<std::recursive_mutex> guard(device->mutex);
  4205. vk_buffer buf;
  4206. size_t index;
  4207. for (size_t i = 0; i < device->pinned_memory.size(); i++) {
  4208. const uint8_t* addr = (const uint8_t*) std::get<0>(device->pinned_memory[i]);
  4209. const uint8_t* endr = addr + std::get<1>(device->pinned_memory[i]);
  4210. if (ptr >= addr && ptr < endr) {
  4211. buf = std::get<2>(device->pinned_memory[i]);
  4212. index = i;
  4213. break;
  4214. }
  4215. }
  4216. if (buf == nullptr) {
  4217. fprintf(stderr, "WARNING: failed to free pinned memory: memory not in map\n");
  4218. return;
  4219. }
  4220. ggml_vk_destroy_buffer(buf);
  4221. device->pinned_memory.erase(device->pinned_memory.begin() + index);
  4222. }
  4223. static void ggml_vk_host_get(vk_device& device, const void * ptr, vk_buffer& buf, size_t& buf_offset) {
  4224. std::lock_guard<std::recursive_mutex> guard(device->mutex);
  4225. buf = nullptr;
  4226. buf_offset = 0;
  4227. for (size_t i = 0; i < device->pinned_memory.size(); i++) {
  4228. const uint8_t* addr = (const uint8_t*) std::get<0>(device->pinned_memory[i]);
  4229. const uint8_t* endr = addr + std::get<1>(device->pinned_memory[i]);
  4230. if (ptr >= addr && ptr < endr) {
  4231. buf = std::get<2>(device->pinned_memory[i]);
  4232. buf_offset = ((const uint8_t *)ptr) - addr;
  4233. break;
  4234. }
  4235. }
  4236. }
  4237. static vk_submission ggml_vk_begin_submission(vk_device& device, vk_command_pool& p, bool one_time = true) {
  4238. vk_submission s;
  4239. s.buffer = ggml_vk_create_cmd_buffer(device, p);
  4240. if (one_time) {
  4241. s.buffer.begin({ vk::CommandBufferUsageFlagBits::eOneTimeSubmit });
  4242. } else {
  4243. s.buffer.begin({ vk::CommandBufferUsageFlags{} });
  4244. }
  4245. return s;
  4246. }
  4247. template <typename T> size_t push_constant_size(const T &t) {
  4248. static_assert(std::is_class<T>::value, "T must be a struct/class");
  4249. GGML_UNUSED(t);
  4250. return sizeof(T);
  4251. }
  4252. template <typename T> size_t push_constant_size(const std::vector<T> &t) {
  4253. GGML_UNUSED(t);
  4254. return sizeof(T) * t.size();
  4255. }
  4256. template <typename T, uint32_t N> size_t push_constant_size(const std::array<T, N> &t) {
  4257. GGML_UNUSED(t);
  4258. return sizeof(T) * N;
  4259. }
  4260. template <typename T> const T *push_constant_data(const T &t) {
  4261. static_assert(std::is_class<T>::value, "T must be a struct/class");
  4262. return &t;
  4263. }
  4264. template <typename T> const T *push_constant_data(const std::vector<T> &t) {
  4265. return t.data();
  4266. }
  4267. template <typename T, uint32_t N> const T *push_constant_data(const std::array<T, N> &t) {
  4268. return t.data();
  4269. }
  4270. template <typename T>
  4271. static void ggml_vk_dispatch_pipeline(ggml_backend_vk_context* ctx, vk_context& subctx, vk_pipeline& pipeline, std::initializer_list<vk::DescriptorBufferInfo> const& descriptor_buffer_infos, const T &push_constants, std::array<uint32_t, 3> elements) {
  4272. const uint32_t wg0 = CEIL_DIV(elements[0], pipeline->wg_denoms[0]);
  4273. const uint32_t wg1 = CEIL_DIV(elements[1], pipeline->wg_denoms[1]);
  4274. const uint32_t wg2 = CEIL_DIV(elements[2], pipeline->wg_denoms[2]);
  4275. VK_LOG_DEBUG("ggml_vk_dispatch_pipeline(" << pipeline->name << ", {";
  4276. for (auto& buffer : descriptor_buffer_infos) {
  4277. std::cerr << "(" << buffer.buffer << ", " << buffer.offset << ", " << buffer.range << "), ";
  4278. }
  4279. std::cerr << "}, (" << wg0 << "," << wg1 << "," << wg2 << "))");
  4280. GGML_ASSERT(ctx->descriptor_set_idx < ctx->descriptor_sets.size());
  4281. GGML_ASSERT(descriptor_buffer_infos.size() <= MAX_PARAMETER_COUNT);
  4282. GGML_ASSERT(pipeline->parameter_count == descriptor_buffer_infos.size());
  4283. vk::DescriptorSet& descriptor_set = ctx->descriptor_sets[ctx->descriptor_set_idx++];
  4284. vk::WriteDescriptorSet write_descriptor_set{ descriptor_set, 0, 0, pipeline->parameter_count, vk::DescriptorType::eStorageBuffer, nullptr, descriptor_buffer_infos.begin() };
  4285. ctx->device->device.updateDescriptorSets({ write_descriptor_set }, {});
  4286. subctx->s->buffer.pushConstants(pipeline->layout, vk::ShaderStageFlagBits::eCompute, 0, push_constant_size(push_constants), push_constant_data(push_constants));
  4287. subctx->s->buffer.bindPipeline(vk::PipelineBindPoint::eCompute, pipeline->pipeline);
  4288. subctx->s->buffer.bindDescriptorSets(vk::PipelineBindPoint::eCompute,
  4289. pipeline->layout,
  4290. 0,
  4291. { descriptor_set },
  4292. {});
  4293. subctx->s->buffer.dispatch(wg0, wg1, wg2);
  4294. }
  4295. static void ggml_vk_end_submission(vk_submission& s, std::vector<vk_semaphore> wait_semaphores, std::vector<vk_semaphore> signal_semaphores) {
  4296. s.buffer.end();
  4297. s.wait_semaphores = std::move(wait_semaphores);
  4298. s.signal_semaphores = std::move(signal_semaphores);
  4299. }
  4300. static void ggml_vk_ctx_end(vk_context& ctx) {
  4301. VK_LOG_DEBUG("ggml_vk_ctx_end(" << ctx << ", " << ctx->seqs.size() << ")");
  4302. if (ctx->s == nullptr) {
  4303. return;
  4304. }
  4305. ctx->s->buffer.end();
  4306. ctx->s = nullptr;
  4307. }
  4308. static void ggml_vk_ctx_begin(vk_device& device, vk_context& subctx) {
  4309. VK_LOG_DEBUG("ggml_vk_ctx_begin(" << device->name << ")");
  4310. if (subctx->s != nullptr) {
  4311. ggml_vk_ctx_end(subctx);
  4312. }
  4313. subctx->seqs.push_back({ ggml_vk_begin_submission(device, *subctx->p) });
  4314. subctx->s = subctx->seqs[subctx->seqs.size() - 1].data();
  4315. }
  4316. static size_t ggml_vk_align_size(size_t width, size_t align) {
  4317. VK_LOG_DEBUG("ggml_vk_align_size(" << width << ", " << align << ")");
  4318. return CEIL_DIV(width, align) * align;
  4319. }
  4320. static void deferred_memcpy(void * dst, const void * src, size_t size, std::vector<vk_staging_memcpy>* memcpys = nullptr) {
  4321. if (memcpys == nullptr) {
  4322. memcpy(dst, src, size);
  4323. } else {
  4324. memcpys->emplace_back(dst, src, size);
  4325. }
  4326. }
  4327. static void ggml_vk_ensure_sync_staging_buffer(vk_device& device, size_t size) {
  4328. if (device->sync_staging == nullptr || device->sync_staging->size < size) {
  4329. VK_LOG_MEMORY("ggml_vk_ensure_sync_staging_buffer(" << size << ")");
  4330. ggml_vk_destroy_buffer(device->sync_staging);
  4331. device->sync_staging = ggml_vk_create_buffer_check(device, size,
  4332. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostCached,
  4333. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
  4334. }
  4335. }
  4336. static void ggml_vk_buffer_write_nc_async(ggml_backend_vk_context * ctx, vk_context& subctx, vk_buffer& dst, size_t offset, const ggml_tensor * tensor, bool sync_staging = false) {
  4337. VK_LOG_DEBUG("ggml_vk_buffer_write_nc_async(" << tensor << ")");
  4338. GGML_ASSERT(!ggml_is_contiguous(tensor));
  4339. // Buffer is already mapped
  4340. if(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  4341. std::cerr << "ggml_vulkan: buffer_write_nc_async dst buffer is host_visible. Use synchronous write." << std::endl;
  4342. GGML_ABORT("fatal error");
  4343. }
  4344. // Check if src is pinned memory
  4345. vk_buffer buf = nullptr;
  4346. size_t buf_offset = 0;
  4347. ggml_vk_host_get(ctx->device, tensor->data, buf, buf_offset);
  4348. const uint64_t ne0 = tensor->ne[0];
  4349. const uint64_t ne1 = tensor->ne[1];
  4350. const uint64_t ne2 = tensor->ne[2];
  4351. const uint64_t ne3 = tensor->ne[3];
  4352. const uint64_t nb0 = tensor->nb[0];
  4353. const uint64_t nb1 = tensor->nb[1];
  4354. const uint64_t nb2 = tensor->nb[2];
  4355. const uint64_t nb3 = tensor->nb[3];
  4356. const ggml_type type = tensor->type;
  4357. const uint64_t ts = ggml_type_size(type);
  4358. const uint64_t bs = ggml_blck_size(type);
  4359. const uint64_t dstnb0 = ts;
  4360. const uint64_t dstnb1 = dstnb0*(ne0/bs);
  4361. const uint64_t dstnb2 = dstnb1*ne1;
  4362. const uint64_t dstnb3 = dstnb2*ne2;
  4363. const uint64_t ne = ggml_nelements(tensor);
  4364. if (buf != nullptr) {
  4365. // Memory is pinned, use as staging buffer
  4366. std::vector<vk::BufferCopy> slices;
  4367. for (uint64_t i3 = 0; i3 < ne3; i3++) {
  4368. for (uint64_t i2 = 0; i2 < ne2; i2++) {
  4369. // Find longest contiguous slice
  4370. if (ne1*nb1 == dstnb2) {
  4371. slices.push_back({ buf_offset + i3*nb3 + i2*nb2, offset + i3*dstnb3 + i2*dstnb2, dstnb2 });
  4372. } else {
  4373. for (uint64_t i1 = 0; i1 < ne1; i1++) {
  4374. if (ne0*nb0/bs == dstnb1) {
  4375. slices.push_back({ buf_offset + i3*nb3 + i2*nb2 + i1*nb1, offset + i3*dstnb3 + i2*dstnb2 + i1*dstnb1, dstnb1 });
  4376. } else {
  4377. const uint64_t s_off = buf_offset + i3*nb3 + i2*nb2 + i1*nb1;
  4378. const uint64_t d_off = offset + i3*dstnb3 + i2*dstnb2 + i1*dstnb1;
  4379. for (uint64_t i0 = 0; i0 < ne0; i0++) {
  4380. slices.push_back({ s_off + i1*nb0, d_off + i0*dstnb0, dstnb0 });
  4381. }
  4382. }
  4383. }
  4384. }
  4385. }
  4386. }
  4387. ggml_vk_sync_buffers(ctx, subctx);
  4388. subctx->s->buffer.copyBuffer(buf->buffer, dst->buffer, slices);
  4389. return;
  4390. }
  4391. if (!sync_staging) {
  4392. GGML_ABORT("Asynchronous write to non-pinned memory not supported");
  4393. }
  4394. // Staging buffer required
  4395. vk_buffer& staging = ctx->device->sync_staging;
  4396. const uint64_t copy_size = ts*ne/bs;
  4397. ggml_vk_ensure_sync_staging_buffer(ctx->device, copy_size);
  4398. VkBufferCopy buf_copy{ 0, offset, copy_size };
  4399. ggml_vk_sync_buffers(ctx, subctx);
  4400. vkCmdCopyBuffer(subctx->s->buffer, (VkBuffer)staging->buffer, (VkBuffer)dst->buffer, 1, &buf_copy);
  4401. for (uint64_t i3 = 0; i3 < ne3; i3++) {
  4402. for (uint64_t i2 = 0; i2 < ne2; i2++) {
  4403. // Find longest contiguous slice
  4404. if (ne1*nb1 == dstnb2) {
  4405. deferred_memcpy((uint8_t *)staging->ptr + i3*dstnb3 + i2*dstnb2, (const uint8_t *) tensor->data + buf_offset + i3*nb3 + i2*nb2, dstnb2, &subctx->in_memcpys);
  4406. } else {
  4407. for (uint64_t i1 = 0; i1 < ne1; i1++) {
  4408. if (ne0*nb0/bs == dstnb1) {
  4409. deferred_memcpy((uint8_t *)staging->ptr + i3*dstnb3 + i2*dstnb2 + i1*dstnb1, (const uint8_t *) tensor->data + buf_offset + i3*nb3 + i2*nb2 + i1*nb1, dstnb1, &subctx->in_memcpys);
  4410. } else {
  4411. const uint64_t s_off = buf_offset + i3*nb3 + i2*nb2 + i1*nb1;
  4412. const uint64_t d_off = i3*dstnb3 + i2*dstnb2 + i1*dstnb1;
  4413. for (uint64_t i0 = 0; i0 < ne0; i0++) {
  4414. deferred_memcpy((uint8_t *)staging->ptr + d_off + i0*dstnb0, (const uint8_t *) tensor->data + s_off + i0*nb0, dstnb0, &subctx->in_memcpys);
  4415. }
  4416. }
  4417. }
  4418. }
  4419. }
  4420. }
  4421. }
  4422. static void ggml_vk_buffer_write_2d_async(vk_context subctx, vk_buffer& dst, size_t offset, const void * src, size_t spitch, size_t width, size_t height, bool sync_staging = false) {
  4423. VK_LOG_DEBUG("ggml_vk_buffer_write_2d_async(" << width << ", " << height << ")");
  4424. // Buffer is already mapped
  4425. if(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  4426. std::cerr << "ggml_vulkan: buffer_write_async dst buffer is host_visible. Use synchronous write." << std::endl;
  4427. GGML_ABORT("fatal error");
  4428. }
  4429. // Check if src is pinned memory
  4430. vk_buffer buf = nullptr;
  4431. size_t buf_offset = 0;
  4432. ggml_vk_host_get(dst->device, src, buf, buf_offset);
  4433. if (buf != nullptr) {
  4434. // Memory is pinned, use as staging buffer
  4435. std::vector<vk::BufferCopy> slices(1);
  4436. if (width == spitch) {
  4437. // Only do single write if stride is equal
  4438. slices[0].srcOffset = buf_offset;
  4439. slices[0].dstOffset = offset;
  4440. slices[0].size = width * height;
  4441. } else {
  4442. slices.resize(height);
  4443. for (size_t i = 0; i < height; i++) {
  4444. slices[i].srcOffset = buf_offset + i * spitch;
  4445. slices[i].dstOffset = offset + i * width;
  4446. slices[i].size = width;
  4447. }
  4448. }
  4449. ggml_vk_sync_buffers(nullptr, subctx);
  4450. subctx->s->buffer.copyBuffer(buf->buffer, dst->buffer, slices);
  4451. return;
  4452. }
  4453. VK_LOG_DEBUG("STAGING");
  4454. if (!sync_staging) {
  4455. GGML_ABORT("Asynchronous write to non-pinned memory not supported");
  4456. }
  4457. // Staging buffer required
  4458. const size_t copy_size = width*height;
  4459. ggml_vk_ensure_sync_staging_buffer(dst->device, copy_size);
  4460. vk_buffer& staging_buffer = dst->device->sync_staging;
  4461. VkBufferCopy buf_copy = {
  4462. 0,
  4463. offset,
  4464. copy_size};
  4465. ggml_vk_sync_buffers(nullptr, subctx);
  4466. vkCmdCopyBuffer(subctx->s->buffer, (VkBuffer)staging_buffer->buffer, (VkBuffer)dst->buffer, 1, &buf_copy);
  4467. if (width == spitch) {
  4468. deferred_memcpy((uint8_t *)staging_buffer->ptr, src, width * height, &subctx->in_memcpys);
  4469. } else {
  4470. for (size_t i = 0; i < height; i++) {
  4471. deferred_memcpy((uint8_t *)staging_buffer->ptr + i * width, (const uint8_t *) src + i * spitch, width, &subctx->in_memcpys);
  4472. }
  4473. }
  4474. }
  4475. static void ggml_vk_buffer_write_async(vk_context subctx, vk_buffer& dst, size_t offset, const void * src, size_t size, bool sync_staging = false) {
  4476. VK_LOG_DEBUG("ggml_vk_buffer_write_async(" << size << ")");
  4477. return ggml_vk_buffer_write_2d_async(subctx, dst, offset, src, size, size, 1, sync_staging);
  4478. }
  4479. static void ggml_vk_buffer_write_2d(vk_buffer& dst, size_t offset, const void * src, size_t spitch, size_t width, size_t height) {
  4480. VK_LOG_DEBUG("ggml_vk_buffer_write_2d(" << width << ", " << height << ")");
  4481. // Buffer is already mapped
  4482. if(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  4483. GGML_ASSERT(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostCoherent);
  4484. for (size_t i = 0; i < height; i++) {
  4485. memcpy((uint8_t *)dst->ptr + offset + i * width, (const uint8_t *) src + i * spitch, width);
  4486. }
  4487. } else {
  4488. std::lock_guard<std::recursive_mutex> guard(dst->device->mutex);
  4489. vk_context subctx = ggml_vk_create_temporary_context(dst->device->transfer_queue.cmd_pool);
  4490. ggml_vk_ctx_begin(dst->device, subctx);
  4491. ggml_vk_buffer_write_2d_async(subctx, dst, offset, src, spitch, width, height, true);
  4492. ggml_vk_ctx_end(subctx);
  4493. for (auto& cpy : subctx->in_memcpys) {
  4494. memcpy(cpy.dst, cpy.src, cpy.n);
  4495. }
  4496. ggml_vk_submit(subctx, dst->device->fence);
  4497. VK_CHECK(dst->device->device.waitForFences({ dst->device->fence }, true, UINT64_MAX), "vk_buffer_write_2d waitForFences");
  4498. dst->device->device.resetFences({ dst->device->fence });
  4499. ggml_vk_queue_command_pools_cleanup(dst->device);
  4500. }
  4501. }
  4502. static void ggml_vk_buffer_write(vk_buffer& dst, size_t offset, const void * src, size_t size) {
  4503. VK_LOG_DEBUG("ggml_vk_buffer_write(" << size << ")");
  4504. ggml_vk_buffer_write_2d(dst, offset, src, 0, size, 1);
  4505. }
  4506. static void ggml_vk_buffer_read_2d_async(vk_context subctx, vk_buffer& src, size_t offset, void * dst, size_t spitch, size_t dpitch, size_t width, size_t height, bool sync_staging = false) {
  4507. VK_LOG_DEBUG("ggml_vk_buffer_read_2d_async(offset=" << offset << ", width=" << width << ", height=" << height << ")");
  4508. GGML_ASSERT(width > 0);
  4509. GGML_ASSERT(height > 0);
  4510. GGML_ASSERT(src != nullptr);
  4511. // TODO: staging_offset is not used
  4512. // Check if dst is pinned memory
  4513. vk_buffer buf = nullptr;
  4514. size_t buf_offset = 0;
  4515. ggml_vk_host_get(src->device, dst, buf, buf_offset);
  4516. std::vector<vk::BufferCopy> slices(1);
  4517. if (width == spitch && width == dpitch) {
  4518. // Only do single write if stride is equal
  4519. slices[0].srcOffset = offset;
  4520. slices[0].dstOffset = buf_offset;
  4521. slices[0].size = width * height;
  4522. } else {
  4523. slices.resize(height);
  4524. for (size_t i = 0; i < height; i++) {
  4525. slices[i].srcOffset = offset + i * spitch;
  4526. slices[i].dstOffset = buf_offset + i * dpitch;
  4527. slices[i].size = width;
  4528. }
  4529. }
  4530. if (buf != nullptr) {
  4531. // Memory is pinned, use as staging buffer
  4532. ggml_vk_sync_buffers(nullptr, subctx);
  4533. subctx->s->buffer.copyBuffer(src->buffer, buf->buffer, slices);
  4534. return;
  4535. }
  4536. VK_LOG_DEBUG("STAGING");
  4537. if (!sync_staging) {
  4538. GGML_ABORT("Asynchronous read from non-pinned memory not supported");
  4539. }
  4540. // Fall back to staging buffer
  4541. const size_t copy_size = dpitch * height;
  4542. ggml_vk_ensure_sync_staging_buffer(src->device, copy_size);
  4543. vk_buffer& staging_buffer = src->device->sync_staging;
  4544. ggml_vk_sync_buffers(nullptr, subctx);
  4545. subctx->s->buffer.copyBuffer(src->buffer, staging_buffer->buffer, slices);
  4546. deferred_memcpy(dst, staging_buffer->ptr, copy_size, &subctx->out_memcpys);
  4547. }
  4548. static void ggml_vk_buffer_read_async(vk_context subctx, vk_buffer& src, size_t offset, void * dst, size_t size, bool sync_staging = false) {
  4549. return ggml_vk_buffer_read_2d_async(subctx, src, offset, dst, size, size, size, 1, sync_staging);
  4550. }
  4551. static void ggml_vk_buffer_read(vk_buffer& src, size_t offset, void * dst, size_t size) {
  4552. VK_LOG_DEBUG("ggml_vk_buffer_read(" << src->buffer << ", " << offset << ", " << size << ")");
  4553. // If the device is not an UMA device the memory is host-accessible through rebar. While writing
  4554. // through PCIe is sufficient fast reading back data from PCIe is slower than going through
  4555. // the HW device to host copy path.
  4556. if(src->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible && src->device->uma) {
  4557. GGML_ASSERT(src->memory_property_flags & vk::MemoryPropertyFlagBits::eHostCoherent);
  4558. memcpy(dst, (uint8_t *) src->ptr + offset, size);
  4559. } else {
  4560. std::lock_guard<std::recursive_mutex> guard(src->device->mutex);
  4561. vk_context subctx = ggml_vk_create_temporary_context(src->device->transfer_queue.cmd_pool);
  4562. ggml_vk_ctx_begin(src->device, subctx);
  4563. ggml_vk_buffer_read_async(subctx, src, offset, dst, size, true);
  4564. ggml_vk_ctx_end(subctx);
  4565. ggml_vk_submit(subctx, src->device->fence);
  4566. VK_CHECK(src->device->device.waitForFences({ src->device->fence }, true, UINT64_MAX), "vk_buffer_read waitForFences");
  4567. src->device->device.resetFences({ src->device->fence });
  4568. ggml_vk_queue_command_pools_cleanup(src->device);
  4569. for (auto& cpy : subctx->out_memcpys) {
  4570. memcpy(cpy.dst, cpy.src, cpy.n);
  4571. }
  4572. }
  4573. }
  4574. static void ggml_vk_buffer_copy_async(vk_context& ctx, vk_buffer& dst, size_t dst_offset, vk_buffer& src, size_t src_offset, size_t size) {
  4575. VK_LOG_DEBUG("ggml_vk_buffer_copy_async(" << size << ")");
  4576. // Make sure both buffers are on same device
  4577. GGML_ASSERT(src->device == dst->device);
  4578. VkBufferCopy bc{ src_offset, dst_offset, size };
  4579. vkCmdCopyBuffer(ctx->s->buffer, (VkBuffer)src->buffer, (VkBuffer)dst->buffer, 1, &bc);
  4580. }
  4581. static void ggml_vk_buffer_copy(vk_buffer& dst, size_t dst_offset, vk_buffer& src, size_t src_offset, size_t size) {
  4582. if (src->device == dst->device) {
  4583. std::lock_guard<std::recursive_mutex> guard(src->device->mutex);
  4584. VK_LOG_DEBUG("ggml_vk_buffer_copy(SINGLE_DEVICE, " << size << ")");
  4585. // Copy within the device
  4586. vk_context subctx = ggml_vk_create_temporary_context(src->device->transfer_queue.cmd_pool);
  4587. ggml_vk_ctx_begin(src->device, subctx);
  4588. ggml_vk_buffer_copy_async(subctx, dst, dst_offset, src, src_offset, size);
  4589. ggml_vk_ctx_end(subctx);
  4590. ggml_vk_submit(subctx, src->device->fence);
  4591. VK_CHECK(src->device->device.waitForFences({ src->device->fence }, true, UINT64_MAX), "vk_buffer_copy waitForFences");
  4592. src->device->device.resetFences({ src->device->fence });
  4593. ggml_vk_queue_command_pools_cleanup(src->device);
  4594. } else {
  4595. VK_LOG_DEBUG("ggml_vk_buffer_copy(MULTI_DEVICE, " << size << ")");
  4596. // Copy device to device
  4597. ggml_vk_ensure_sync_staging_buffer(src->device, size);
  4598. ggml_vk_ensure_sync_staging_buffer(dst->device, size);
  4599. // Copy to src staging buffer
  4600. ggml_vk_buffer_copy(src->device->sync_staging, 0, src, src_offset, size);
  4601. // memcpy to dst staging buffer
  4602. memcpy(dst->device->sync_staging->ptr, src->device->sync_staging->ptr, size);
  4603. // Copy to dst buffer
  4604. ggml_vk_buffer_copy(dst, dst_offset, dst->device->sync_staging, 0, size);
  4605. }
  4606. }
  4607. static void ggml_vk_buffer_memset_async(vk_context& ctx, vk_buffer& dst, size_t offset, uint32_t c, size_t size) {
  4608. VK_LOG_DEBUG("ggml_vk_buffer_memset_async(" << offset << ", " << c << ", " << size << ")");
  4609. ctx->s->buffer.fillBuffer(dst->buffer, offset, size, c);
  4610. }
  4611. static void ggml_vk_buffer_memset(vk_buffer& dst, size_t offset, uint32_t c, size_t size) {
  4612. VK_LOG_DEBUG("ggml_vk_buffer_memset(" << offset << ", " << c << ", " << size << ")");
  4613. std::lock_guard<std::recursive_mutex> guard(dst->device->mutex);
  4614. vk_context subctx = ggml_vk_create_temporary_context(dst->device->transfer_queue.cmd_pool);
  4615. ggml_vk_ctx_begin(dst->device, subctx);
  4616. subctx->s->buffer.fillBuffer(dst->buffer, offset, size, c);
  4617. ggml_vk_ctx_end(subctx);
  4618. ggml_vk_submit(subctx, dst->device->fence);
  4619. VK_CHECK(dst->device->device.waitForFences({ dst->device->fence }, true, UINT64_MAX), "vk_memset waitForFences");
  4620. dst->device->device.resetFences({ dst->device->fence });
  4621. ggml_vk_queue_command_pools_cleanup(dst->device);
  4622. }
  4623. static uint32_t ggml_vk_guess_split_k(ggml_backend_vk_context * ctx, uint32_t m, uint32_t n, uint32_t k, const vk_pipeline& pipeline) {
  4624. VK_LOG_DEBUG("ggml_vk_guess_split_k(" << m << ", " << n << ", " << k << ")");
  4625. uint32_t split_k = 1;
  4626. if (ctx->device->shader_core_count != 0 && m >= pipeline->wg_denoms[0] && n >= pipeline->wg_denoms[1]) {
  4627. // If k is 'large' and the SMs will fill less than halfway, use split_k.
  4628. uint32_t m_tiles = CEIL_DIV(m, pipeline->wg_denoms[0]);
  4629. uint32_t n_tiles = CEIL_DIV(n, pipeline->wg_denoms[1]);
  4630. if (k >= 2048) {
  4631. if (m_tiles * n_tiles <= ctx->device->shader_core_count / 2) {
  4632. split_k = ctx->device->shader_core_count / (m_tiles * n_tiles);
  4633. } else if (m_tiles * n_tiles <= ctx->device->shader_core_count * 2 / 3) {
  4634. split_k = 3;
  4635. }
  4636. // Cap the split at 8x. Unless k is huge this is a lot of overhead.
  4637. split_k = std::min(split_k, 8u);
  4638. // ggml_vk_matmul will align the splits to be a multiple of 256.
  4639. // If this rounded up size would cause the last split to be empty,
  4640. // then reduce the split count.
  4641. while (true) {
  4642. if (split_k == 1) {
  4643. break;
  4644. }
  4645. uint32_t k_split = CEIL_DIV(k, split_k);
  4646. k_split = ROUNDUP_POW2(k_split, 256);
  4647. if (k_split * (split_k - 1) < k) {
  4648. break;
  4649. }
  4650. split_k--;
  4651. }
  4652. }
  4653. }
  4654. return split_k;
  4655. }
  4656. static vk_pipeline ggml_vk_guess_matmul_pipeline(ggml_backend_vk_context * ctx, vk_matmul_pipeline& mmp, uint32_t m, uint32_t n, bool aligned, ggml_type src0_type, ggml_type src1_type) {
  4657. VK_LOG_DEBUG("ggml_vk_guess_matmul_pipeline(" << m << ", " << n << ", " << aligned << ", " << ggml_type_name(src0_type) << ", " << ggml_type_name(src1_type) << ")");
  4658. if (ctx->device->coopmat2) {
  4659. const uint32_t shader_core_count = ctx->device->shader_core_count;
  4660. const uint32_t tiles_l = CEIL_DIV(m, mmp->a_l->wg_denoms[0]) * CEIL_DIV(n, mmp->a_l->wg_denoms[1]);
  4661. const uint32_t tiles_m = CEIL_DIV(m, mmp->a_m->wg_denoms[0]) * CEIL_DIV(n, mmp->a_m->wg_denoms[1]);
  4662. // Use large shader when the N dimension is greater than the medium shader's tile size
  4663. uint32_t crossover_large = mmp->m->wg_denoms[1];
  4664. // Prefer large over medium if either:
  4665. // - medium or large tiles would overfill the GPU
  4666. // - large tiles with a split_k==3 fits in the GPU and medium tiles with split_k==2 does not
  4667. // (medium with split_k==2 is probably better if it fits - more workgroups running and less split_k overhead)
  4668. bool prefer_large = tiles_m > shader_core_count || tiles_l > shader_core_count ||
  4669. // split_k==3 with large tiles likely better than medium tiles with no split_k.
  4670. (tiles_l <= shader_core_count / 3 && tiles_m > shader_core_count / 2);
  4671. if ((ctx->device->mul_mat_l[src0_type] && (n > crossover_large && prefer_large)) || (!ctx->device->mul_mat_m[src0_type] && !ctx->device->mul_mat_s[src0_type])) {
  4672. return aligned ? mmp->a_l : mmp->l;
  4673. }
  4674. // Use medium shader when the N dimension is greater than the small shader's tile size
  4675. uint32_t crossover_medium = mmp->s->wg_denoms[1];
  4676. if ((ctx->device->mul_mat_m[src0_type] && (n > crossover_medium)) || !ctx->device->mul_mat_s[src0_type]) {
  4677. return aligned ? mmp->a_m : mmp->m;
  4678. }
  4679. return aligned ? mmp->a_s : mmp->s;
  4680. }
  4681. if ((ctx->device->mul_mat_s[src0_type] && (m <= 32 || n <= 32)) || (!ctx->device->mul_mat_m[src0_type] && !ctx->device->mul_mat_l[src0_type])) {
  4682. return aligned ? mmp->a_s : mmp->s;
  4683. }
  4684. if ((ctx->device->mul_mat_m[src0_type] && (m <= 64 || n <= 64)) || !ctx->device->mul_mat_l[src0_type]) {
  4685. return aligned ? mmp->a_m : mmp->m;
  4686. }
  4687. return aligned ? mmp->a_l : mmp->l;
  4688. GGML_UNUSED(src1_type);
  4689. }
  4690. static uint32_t ggml_vk_guess_matmul_pipeline_align(ggml_backend_vk_context * ctx, vk_matmul_pipeline& mmp, int m, int n, ggml_type src0_type, ggml_type src1_type) {
  4691. VK_LOG_DEBUG("ggml_vk_guess_matmul_pipeline_align(" << m << ", " << n << ", " << ggml_type_name(src0_type) << ", " << ggml_type_name(src1_type) << ")");
  4692. return ggml_vk_guess_matmul_pipeline(ctx, mmp, m, n, true, src0_type, src1_type)->align;
  4693. }
  4694. static void ggml_vk_matmul(
  4695. ggml_backend_vk_context * ctx, vk_context& subctx, vk_pipeline& pipeline,
  4696. vk_subbuffer&& a, vk_subbuffer&& b, vk_subbuffer&& d, vk_subbuffer&& split_k_buffer,
  4697. uint32_t m, uint32_t n, uint32_t k, uint32_t stride_a, uint32_t stride_b, uint32_t stride_d,
  4698. uint32_t batch_stride_a, uint32_t batch_stride_b, uint32_t batch_stride_d,
  4699. uint32_t split_k, uint32_t batch, uint32_t ne02, uint32_t ne12, uint32_t broadcast2, uint32_t broadcast3,
  4700. uint32_t padded_n) {
  4701. VK_LOG_DEBUG("ggml_vk_matmul(a: (" << a.buffer->buffer << ", " << a.offset << ", " << a.size << "), b: (" << b.buffer->buffer << ", " << b.offset << ", " << b.size << "), d: (" << d.buffer->buffer << ", " << d.offset << ", " << d.size << "), split_k: (" << (split_k_buffer.buffer != nullptr ? split_k_buffer.buffer->buffer : VK_NULL_HANDLE) << ", " << split_k_buffer.offset << ", " << split_k_buffer.size << "), m: " << m << ", n: " << n << ", k: " << k << ", stride_a: " << stride_a << ", stride_b: " << stride_b << ", stride_d: " << stride_d << ", batch_stride_a: " << batch_stride_a << ", batch_stride_b: " << batch_stride_b << ", batch_stride_d: " << batch_stride_d << ", split_k: " << split_k << ", batch: " << batch << ", ne02: " << ne02 << ", ne12: " << ne12 << ", broadcast2: " << broadcast2 << ", broadcast3: " << broadcast3 << ", padded_n: " << padded_n << ")");
  4702. if (split_k == 1) {
  4703. const vk_mat_mat_push_constants pc = { m, n, k, stride_a, stride_b, stride_d, batch_stride_a, batch_stride_b, batch_stride_d, k, ne02, ne12, broadcast2, broadcast3, padded_n };
  4704. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { a, b, d }, pc, { m, n, batch });
  4705. return;
  4706. }
  4707. if (ctx->prealloc_split_k_need_sync) {
  4708. ggml_vk_sync_buffers(ctx, subctx);
  4709. }
  4710. GGML_ASSERT(batch_stride_d == m * n);
  4711. // Round the split size up to a multiple of 256 (k-quant alignment)
  4712. uint32_t k_split = CEIL_DIV(k, split_k);
  4713. k_split = ROUNDUP_POW2(k_split, 256);
  4714. const vk_mat_mat_push_constants pc1 = { m, n, k, stride_a, stride_b, stride_d, batch_stride_a, batch_stride_b, batch_stride_d, k_split, ne02, ne12, broadcast2, broadcast3, padded_n };
  4715. // Make sure enough workgroups get assigned for split k to work
  4716. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { a, b, split_k_buffer }, pc1, { (CEIL_DIV(m, pipeline->wg_denoms[0]) * pipeline->wg_denoms[0]) * split_k, n, batch });
  4717. ggml_vk_sync_buffers(ctx, subctx);
  4718. const std::array<uint32_t, 2> pc2 = { (uint32_t)(m * n * batch), split_k };
  4719. ggml_vk_dispatch_pipeline(ctx, subctx, ctx->device->pipeline_matmul_split_k_reduce, { split_k_buffer, d }, pc2, { m * n * batch, 1, 1 });
  4720. ctx->prealloc_split_k_need_sync = true;
  4721. }
  4722. static vk_pipeline ggml_vk_guess_matmul_id_pipeline(ggml_backend_vk_context * ctx, vk_matmul_pipeline& mmp, uint32_t m, uint32_t n, bool aligned, ggml_type src0_type) {
  4723. VK_LOG_DEBUG("ggml_vk_guess_matmul_id_pipeline(" << m << ", " << n << ", " << aligned << ", " << ggml_type_name(src0_type) << ")");
  4724. if (ctx->device->coopmat2) {
  4725. // Use large shader when the N dimension is greater than the medium shader's tile size
  4726. uint32_t crossover_large = mmp->m->wg_denoms[1];
  4727. if ((ctx->device->mul_mat_id_l[src0_type] && (n > crossover_large)) || (!ctx->device->mul_mat_id_m[src0_type] && !ctx->device->mul_mat_id_s[src0_type])) {
  4728. return aligned ? mmp->a_l : mmp->l;
  4729. }
  4730. // Use medium shader when the N dimension is greater than the small shader's tile size
  4731. uint32_t crossover_medium = mmp->s->wg_denoms[1];
  4732. if ((ctx->device->mul_mat_id_m[src0_type] && (n > crossover_medium)) || !ctx->device->mul_mat_id_s[src0_type]) {
  4733. return aligned ? mmp->a_m : mmp->m;
  4734. }
  4735. return aligned ? mmp->a_s : mmp->s;
  4736. }
  4737. if ((ctx->device->mul_mat_id_s[src0_type] && (m <= 32 || n <= 32)) || (!ctx->device->mul_mat_id_m[src0_type] && !ctx->device->mul_mat_id_l[src0_type])) {
  4738. return aligned ? mmp->a_s : mmp->s;
  4739. }
  4740. if ((ctx->device->mul_mat_id_m[src0_type] && (m <= 64 || n <= 64)) || !ctx->device->mul_mat_id_l[src0_type]) {
  4741. return aligned ? mmp->a_m : mmp->m;
  4742. }
  4743. return aligned ? mmp->a_l : mmp->l;
  4744. }
  4745. static uint32_t ggml_vk_guess_matmul_id_pipeline_align(ggml_backend_vk_context * ctx, vk_matmul_pipeline& mmp, int m, int n, ggml_type src0_type) {
  4746. VK_LOG_DEBUG("ggml_vk_guess_matmul_pipeline_align(" << m << ", " << n << ", " << ggml_type_name(src0_type) << ")");
  4747. return ggml_vk_guess_matmul_id_pipeline(ctx, mmp, m, n, true, src0_type)->align;
  4748. }
  4749. static void ggml_vk_matmul_id(
  4750. ggml_backend_vk_context * ctx, vk_context& subctx, vk_pipeline& pipeline,
  4751. vk_subbuffer&& a, vk_subbuffer&& b, vk_subbuffer&& d, vk_subbuffer&& ids,
  4752. uint32_t m, uint32_t n, uint32_t k, uint32_t stride_a, uint32_t stride_b, uint32_t stride_d,
  4753. uint32_t batch_stride_a, uint32_t batch_stride_b, uint32_t batch_stride_d,
  4754. uint32_t n_as, uint32_t nei0, uint32_t nei1, uint32_t nbi1, uint32_t ne11,
  4755. uint32_t padded_n) {
  4756. VK_LOG_DEBUG("ggml_vk_matmul_id(a: (" << a.buffer->buffer << ", " << a.offset << ", " << a.size << "), b: (" << b.buffer->buffer << ", " << b.offset << ", " << b.size << "), d: (" << d.buffer->buffer << ", " << d.offset << ", " << d.size << "), ids: (" << ids.buffer->buffer << ", " << ids.offset << ", " << ids.size << "), " <<
  4757. "m: " << m << ", n: " << n << ", k: " << k << ", stride_a: " << stride_a << ", stride_b: " << stride_b << ", stride_d: " << stride_d << ", " <<
  4758. "batch_stride_a: " << batch_stride_a << ", batch_stride_b: " << batch_stride_b << ", batch_stride_d: " << batch_stride_d << ", " <<
  4759. "n_as: " << n_as << ", nei0: " << nei0 << ", nei1: " << nei1 << ", nbi1: " << nbi1 << ", ne11: " << ne11 << ")");
  4760. const vk_mat_mat_id_push_constants pc = { m, n, k, stride_a, stride_b, stride_d, batch_stride_a, batch_stride_b, batch_stride_d,
  4761. nei0, nei1, nbi1, ne11, padded_n };
  4762. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { a, b, d, ids }, pc, { m, nei1, n_as });
  4763. }
  4764. static bool ggml_vk_dim01_contiguous(const ggml_tensor * tensor) {
  4765. return
  4766. tensor->nb[0] == ggml_type_size(tensor->type) &&
  4767. tensor->nb[1] == (tensor->nb[0]*tensor->ne[0])/ggml_blck_size(tensor->type) &&
  4768. (tensor->ne[3] == 1 || tensor->nb[3] == tensor->nb[2]*tensor->ne[2]);
  4769. }
  4770. static vk_pipeline ggml_vk_get_cpy_pipeline(ggml_backend_vk_context * ctx, const ggml_tensor * src, const ggml_tensor * dst, ggml_type to) {
  4771. // Choose "contiguous copy" shader if src/dst are contiguous
  4772. bool contig = ggml_is_contiguous(src) && (!dst || ggml_is_contiguous(dst));
  4773. if (src->type == GGML_TYPE_F32 && to == GGML_TYPE_F32) {
  4774. if (contig) {
  4775. return ctx->device->pipeline_contig_cpy_f32_f32;
  4776. } else {
  4777. return ctx->device->pipeline_cpy_f32_f32;
  4778. }
  4779. }
  4780. if (src->type == GGML_TYPE_F32 && to == GGML_TYPE_F16) {
  4781. if (contig) {
  4782. return ctx->device->pipeline_contig_cpy_f32_f16;
  4783. } else {
  4784. return ctx->device->pipeline_cpy_f32_f16;
  4785. }
  4786. }
  4787. if (src->type == GGML_TYPE_F16 && to == GGML_TYPE_F16) {
  4788. if (contig) {
  4789. return ctx->device->pipeline_contig_cpy_f16_f16;
  4790. } else {
  4791. return ctx->device->pipeline_cpy_f16_f16;
  4792. }
  4793. }
  4794. if (src->type == GGML_TYPE_F16 && to == GGML_TYPE_F32) {
  4795. if (contig) {
  4796. return ctx->device->pipeline_contig_cpy_f16_f32;
  4797. } else {
  4798. return ctx->device->pipeline_cpy_f16_f32;
  4799. }
  4800. }
  4801. if (src->type == GGML_TYPE_F32 && to == GGML_TYPE_BF16) {
  4802. if (contig) {
  4803. return ctx->device->pipeline_contig_cpy_f32_bf16;
  4804. } else {
  4805. return ctx->device->pipeline_cpy_f32_bf16;
  4806. }
  4807. }
  4808. if (src->type == GGML_TYPE_F32) {
  4809. switch (to) {
  4810. case GGML_TYPE_Q4_0:
  4811. case GGML_TYPE_Q4_1:
  4812. case GGML_TYPE_Q5_0:
  4813. case GGML_TYPE_Q5_1:
  4814. case GGML_TYPE_Q8_0:
  4815. case GGML_TYPE_IQ4_NL:
  4816. return ctx->device->pipeline_cpy_f32_quant[to];
  4817. default:
  4818. break;
  4819. }
  4820. }
  4821. if (to == GGML_TYPE_F32) {
  4822. switch (src->type) {
  4823. case GGML_TYPE_Q4_0:
  4824. case GGML_TYPE_Q4_1:
  4825. case GGML_TYPE_Q5_0:
  4826. case GGML_TYPE_Q5_1:
  4827. case GGML_TYPE_Q8_0:
  4828. case GGML_TYPE_IQ4_NL:
  4829. return ctx->device->pipeline_cpy_quant_f32[src->type];
  4830. default:
  4831. break;
  4832. }
  4833. }
  4834. if (src->type == to) {
  4835. // Copy two or four bytes at a time, depending on block size.
  4836. // For quantized types, we scale by block size/type size. But
  4837. // this path is also used for bf16->bf16 for example, where the
  4838. // type size must be exactly 2 or 4.
  4839. GGML_ASSERT(ggml_is_quantized(to) || ggml_type_size(src->type) == 2 || ggml_type_size(src->type) == 4);
  4840. if ((ggml_type_size(src->type) % 4) == 0) {
  4841. if (contig) {
  4842. return ctx->device->pipeline_contig_cpy_f32_f32;
  4843. } else {
  4844. return ctx->device->pipeline_cpy_f32_f32;
  4845. }
  4846. } else {
  4847. if (contig) {
  4848. return ctx->device->pipeline_contig_cpy_f16_f16;
  4849. } else {
  4850. return ctx->device->pipeline_cpy_f16_f16;
  4851. }
  4852. }
  4853. }
  4854. std::cerr << "Missing CPY op for types: " << ggml_type_name(src->type) << " " << ggml_type_name(to) << std::endl;
  4855. GGML_ABORT("fatal error");
  4856. }
  4857. static void ggml_vk_cpy_to_contiguous(ggml_backend_vk_context * ctx, vk_context& subctx, vk_pipeline pipeline, const ggml_tensor * tensor, vk_subbuffer&& in, vk_subbuffer&& out) {
  4858. VK_LOG_DEBUG("ggml_vk_cpy_to_contiguous((" << tensor << ", type=" << tensor->type << ", ne0=" << tensor->ne[0] << ", ne1=" << tensor->ne[1] << ", ne2=" << tensor->ne[2] << ", ne3=" << tensor->ne[3] << ", nb0=" << tensor->nb[0] << ", nb1=" << tensor->nb[1] << ", nb2=" << tensor->nb[2] << ", nb3=" << tensor->nb[3] << "), ";
  4859. std::cerr << "buffer in size=" << in.buffer->size << ", buffer out size=" << out.buffer->size << ")");
  4860. const int tensor_type_size = ggml_type_size(tensor->type);
  4861. const uint32_t ne = ggml_nelements(tensor);
  4862. std::array<uint32_t, 3> elements;
  4863. if (ne > 262144) {
  4864. elements = { 512, 512, CEIL_DIV(ne, 262144) };
  4865. } else if (ne > 512) {
  4866. elements = { 512, CEIL_DIV(ne, 512), 1 };
  4867. } else {
  4868. elements = { ne, 1, 1 };
  4869. }
  4870. vk_op_unary_push_constants pc = {
  4871. (uint32_t)ne,
  4872. (uint32_t)tensor->ne[0], (uint32_t)tensor->ne[1], (uint32_t)tensor->ne[2], (uint32_t)tensor->ne[3], (uint32_t)tensor->nb[0] / tensor_type_size, (uint32_t)tensor->nb[1] / tensor_type_size, (uint32_t)tensor->nb[2] / tensor_type_size, (uint32_t)tensor->nb[3] / tensor_type_size,
  4873. (uint32_t)tensor->ne[0], (uint32_t)tensor->ne[1], (uint32_t)tensor->ne[2], (uint32_t)tensor->ne[3], 1 , (uint32_t)tensor->ne[0] , (uint32_t)(tensor->ne[0] * tensor->ne[1]) , (uint32_t)(tensor->ne[0] * tensor->ne[1] * tensor->ne[2]),
  4874. 0,
  4875. 0.0f, 0.0f,
  4876. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  4877. };
  4878. init_pushconst_fastdiv(pc);
  4879. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { in, out }, pc, elements);
  4880. ggml_vk_sync_buffers(ctx, subctx);
  4881. }
  4882. static vk_pipeline ggml_vk_get_quantize_pipeline(ggml_backend_vk_context * ctx, ggml_type type, bool use_x4_blocks) {
  4883. switch(type) {
  4884. case GGML_TYPE_Q8_1:
  4885. return use_x4_blocks ? ctx->device->pipeline_quantize_q8_1_x4 : ctx->device->pipeline_quantize_q8_1;
  4886. default:
  4887. std::cerr << "Missing quantize pipeline for type: " << ggml_type_name(type) << std::endl;
  4888. GGML_ABORT("fatal error");
  4889. }
  4890. }
  4891. static void ggml_vk_quantize_q8_1(ggml_backend_vk_context * ctx, vk_context& subctx, vk_subbuffer&& in, vk_subbuffer&& out, uint32_t ne, bool use_x4_blocks = false) {
  4892. VK_LOG_DEBUG("ggml_vk_quantize_q8_1(" << "buffer in size=" << in.buffer->size << ", buffer out size=" << out.buffer->size << ", " << ne << ")");
  4893. vk_pipeline pipeline = use_x4_blocks ? ggml_vk_get_quantize_pipeline(ctx, GGML_TYPE_Q8_1, true) : ggml_vk_get_quantize_pipeline(ctx, GGML_TYPE_Q8_1, false);
  4894. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { in, out }, std::array<uint32_t, 1>{ne}, { ne, 1, 1 });
  4895. ggml_vk_sync_buffers(ctx, subctx);
  4896. }
  4897. static void ggml_vk_mul_mat_q_f16(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  4898. VK_LOG_DEBUG("ggml_vk_mul_mat_q_f16((" << src0 << ", name=" << src0->name << ", type=" << ggml_type_name(src0->type) << ", ne0=" << src0->ne[0] << ", ne1=" << src0->ne[1] << ", ne2=" << src0->ne[2] << ", ne3=" << src0->ne[3] << ", nb0=" << src0->nb[0] << ", nb1=" << src0->nb[1] << ", nb2=" << src0->nb[2] << ", nb3=" << src0->nb[3];
  4899. std::cerr << "), (" << src1 << ", name=" << src1->name << ", type=" << ggml_type_name(src1->type) << ", ne0=" << src1->ne[0] << ", ne1=" << src1->ne[1] << ", ne2=" << src1->ne[2] << ", ne3=" << src1->ne[3] << ", nb0=" << src1->nb[0] << ", nb1=" << src1->nb[1] << ", nb2=" << src1->nb[2] << ", nb3=" << src1->nb[3];
  4900. std::cerr << "), (" << dst << ", name=" << dst->name << ", type=" << ggml_type_name(dst->type) << ", ne0=" << dst->ne[0] << ", ne1=" << dst->ne[1] << ", ne2=" << dst->ne[2] << ", ne3=" << dst->ne[3] << ", nb0=" << dst->nb[0] << ", nb1=" << dst->nb[1] << ", nb2=" << dst->nb[2] << ", nb3=" << dst->nb[3];
  4901. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  4902. GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16); // NOLINT
  4903. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  4904. const uint64_t ne00 = src0->ne[0];
  4905. const uint64_t ne01 = src0->ne[1];
  4906. const uint64_t ne02 = src0->ne[2];
  4907. const uint64_t ne03 = src0->ne[3];
  4908. const uint64_t ne10 = src1->ne[0];
  4909. const uint64_t ne11 = src1->ne[1];
  4910. const uint64_t ne12 = src1->ne[2];
  4911. const uint64_t ne13 = src1->ne[3];
  4912. const uint64_t ne20 = dst->ne[0];
  4913. const uint64_t ne21 = dst->ne[1];
  4914. const uint64_t r2 = ne12 / ne02;
  4915. const uint64_t r3 = ne13 / ne03;
  4916. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  4917. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  4918. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  4919. vk_buffer d_Qx = nullptr;
  4920. size_t qx_buf_offset = 0;
  4921. vk_buffer d_Qy = nullptr;
  4922. size_t qy_buf_offset = 0;
  4923. bool src0_uma = false;
  4924. bool src1_uma = false;
  4925. if (ctx->device->uma) {
  4926. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  4927. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  4928. src0_uma = d_Qx != nullptr;
  4929. src1_uma = d_Qy != nullptr;
  4930. }
  4931. // Reformat and convert to fp16 if non-contiguous, or for coopmat2 for better perf
  4932. const bool x_non_contig = (ctx->device->coopmat2 && src0->type == GGML_TYPE_F32) ||
  4933. !ggml_vk_dim01_contiguous(src0);
  4934. const bool y_non_contig = (ctx->device->coopmat2 && src1->type == GGML_TYPE_F32) ||
  4935. (src0->type == GGML_TYPE_BF16 && src1->type != GGML_TYPE_BF16) ||
  4936. !ggml_vk_dim01_contiguous(src1);
  4937. // If src0 is BF16, try to use a BF16 x BF16 multiply
  4938. ggml_type f16_type = src0->type == GGML_TYPE_BF16 ? GGML_TYPE_BF16 : GGML_TYPE_F16;
  4939. const bool y_f32_kernel = src1->type == GGML_TYPE_F32 && !y_non_contig;
  4940. bool quantize_y = ctx->device->integer_dot_product && src1->type == GGML_TYPE_F32 && ggml_is_contiguous(src1) && (ne11 * ne10) % 4 == 0;
  4941. // Check for mmq first
  4942. vk_matmul_pipeline mmp = quantize_y ? ggml_vk_get_mul_mat_mat_pipeline(ctx, src0->type, GGML_TYPE_Q8_1, (ggml_prec)dst->op_params[0]) : nullptr;
  4943. if (mmp == nullptr) {
  4944. // Fall back to f16 dequant mul mat
  4945. mmp = ggml_vk_get_mul_mat_mat_pipeline(ctx, src0->type, y_non_contig ? f16_type : src1->type, (ggml_prec)dst->op_params[0]);
  4946. quantize_y = false;
  4947. }
  4948. const bool qx_needs_dequant = mmp == nullptr || x_non_contig;
  4949. const bool qy_needs_dequant = !quantize_y && ((src1->type != f16_type && !y_f32_kernel) || y_non_contig);
  4950. if (qx_needs_dequant) {
  4951. // Fall back to dequant + f16 mulmat
  4952. mmp = ggml_vk_get_mul_mat_mat_pipeline(ctx, f16_type, y_f32_kernel ? GGML_TYPE_F32 : f16_type, (ggml_prec)dst->op_params[0]);
  4953. }
  4954. // Not implemented
  4955. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  4956. const uint32_t kpad = quantize_y ? 0 : ggml_vk_align_size(ne10, ggml_vk_guess_matmul_pipeline_align(ctx, mmp, ne01, ne11, qx_needs_dequant ? f16_type : src0->type, quantize_y ? GGML_TYPE_Q8_1 : (y_f32_kernel ? GGML_TYPE_F32 : src1->type)));
  4957. const bool aligned = !quantize_y && ne10 == kpad && ne01 > 8 && ne11 > 8;
  4958. vk_pipeline pipeline = ggml_vk_guess_matmul_pipeline(ctx, mmp, ne01, ne11, aligned, qx_needs_dequant ? f16_type : src0->type, quantize_y ? GGML_TYPE_Q8_1 : (y_f32_kernel ? GGML_TYPE_F32 : src1->type));
  4959. // Reserve extra storage in the N dimension for the Y matrix, so we can avoid bounds-checking
  4960. uint32_t padded_n = qy_needs_dequant ? ROUNDUP_POW2(ne11, pipeline->wg_denoms[1]) : ne11;
  4961. const int x_ne = ne01 * ne00;
  4962. const int y_ne = padded_n * ne10;
  4963. const int d_ne = ne11 * ne01;
  4964. const uint32_t split_k = ggml_vk_guess_split_k(ctx, ne01, ne11, ne10, pipeline);
  4965. const uint64_t qx_sz = ggml_type_size(src0->type) * x_ne / ggml_blck_size(src0->type);
  4966. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  4967. const uint64_t x_sz = !qx_needs_dequant ? qx_sz : sizeof(ggml_fp16_t) * x_ne;
  4968. const uint64_t y_sz = quantize_y ? (y_ne * ggml_type_size(GGML_TYPE_Q8_1) / ggml_blck_size(GGML_TYPE_Q8_1)) : (y_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne);
  4969. const uint64_t d_sz = sizeof(float) * d_ne;
  4970. vk_pipeline to_fp16_vk_0 = nullptr;
  4971. vk_pipeline to_fp16_vk_1 = nullptr;
  4972. vk_pipeline to_q8_1 = nullptr;
  4973. if (x_non_contig) {
  4974. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, f16_type);
  4975. } else {
  4976. to_fp16_vk_0 = ggml_vk_get_to_fp16(ctx, src0->type);
  4977. }
  4978. if (y_non_contig) {
  4979. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, f16_type);
  4980. } else {
  4981. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  4982. }
  4983. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  4984. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  4985. if (quantize_y) {
  4986. to_q8_1 = ggml_vk_get_quantize_pipeline(ctx, GGML_TYPE_Q8_1, true);
  4987. }
  4988. if (dryrun) {
  4989. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  4990. uint64_t y_sz_upd = y_sz * ne12 * ne13;
  4991. if (quantize_y) {
  4992. y_sz_upd = CEIL_DIV(y_sz_upd, 144) * 144;
  4993. }
  4994. const uint64_t split_k_size = split_k > 1 ? d_sz * ne12 * ne13 * split_k : 0;
  4995. if (
  4996. (qx_needs_dequant && x_sz_upd > ctx->device->max_memory_allocation_size) ||
  4997. (qy_needs_dequant && y_sz_upd > ctx->device->max_memory_allocation_size) ||
  4998. (split_k > 1 && split_k_size > ctx->device->max_memory_allocation_size)) {
  4999. GGML_ABORT("Requested preallocation size is too large");
  5000. }
  5001. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  5002. ctx->prealloc_size_x = x_sz_upd;
  5003. }
  5004. if ((qy_needs_dequant || quantize_y) && ctx->prealloc_size_y < y_sz_upd) {
  5005. ctx->prealloc_size_y = y_sz_upd;
  5006. }
  5007. if (split_k > 1 && ctx->prealloc_size_split_k < split_k_size) {
  5008. ctx->prealloc_size_split_k = split_k_size;
  5009. }
  5010. // Request descriptor sets
  5011. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  5012. if (qx_needs_dequant) {
  5013. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_0, 1);
  5014. }
  5015. if (qy_needs_dequant) {
  5016. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_1, 1);
  5017. }
  5018. if (quantize_y) {
  5019. ggml_pipeline_request_descriptor_sets(ctx, to_q8_1, 1);
  5020. }
  5021. if (split_k > 1) {
  5022. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_matmul_split_k_reduce, 1);
  5023. }
  5024. return;
  5025. }
  5026. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  5027. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  5028. GGML_ASSERT(d_D != nullptr);
  5029. GGML_ASSERT(d_D->size >= d_buf_offset + d_sz * ne02 * ne03);
  5030. vk_buffer d_X;
  5031. uint64_t x_buf_offset = 0;
  5032. vk_buffer d_Y;
  5033. uint64_t y_buf_offset = 0;
  5034. if (!src0_uma) {
  5035. d_Qx = src0_buf_ctx->dev_buffer;
  5036. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  5037. GGML_ASSERT(d_Qx != nullptr);
  5038. }
  5039. if (!src1_uma) {
  5040. d_Qy = src1_buf_ctx->dev_buffer;
  5041. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  5042. GGML_ASSERT(d_Qy != nullptr);
  5043. }
  5044. if (qx_needs_dequant) {
  5045. d_X = ctx->prealloc_x;
  5046. GGML_ASSERT(d_X->size >= x_sz * ne02 * ne03);
  5047. } else {
  5048. d_X = d_Qx;
  5049. x_buf_offset = qx_buf_offset;
  5050. GGML_ASSERT(qx_sz == x_sz);
  5051. }
  5052. if (qy_needs_dequant) {
  5053. d_Y = ctx->prealloc_y;
  5054. GGML_ASSERT(d_Y->size >= y_sz * ne12 * ne13);
  5055. } else if (quantize_y) {
  5056. d_Y = ctx->prealloc_y;
  5057. GGML_ASSERT(d_Y->size >= CEIL_DIV(y_sz * ne12 * ne13, 144) * 144);
  5058. } else {
  5059. d_Y = d_Qy;
  5060. y_buf_offset = qy_buf_offset;
  5061. GGML_ASSERT(qy_sz == y_sz);
  5062. }
  5063. if (x_non_contig || qx_needs_dequant) {
  5064. if (ctx->prealloc_x_need_sync) {
  5065. ggml_vk_sync_buffers(ctx, subctx);
  5066. }
  5067. }
  5068. if (x_non_contig) {
  5069. ggml_vk_cpy_to_contiguous(ctx, subctx, to_fp16_vk_0, src0, { d_Qx, qx_buf_offset, VK_WHOLE_SIZE }, { d_X, 0, VK_WHOLE_SIZE });
  5070. } else if (qx_needs_dequant) {
  5071. const std::vector<uint32_t> pc = { (uint32_t)ne01, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)(ggml_nelements(src0)) };
  5072. ggml_vk_dispatch_pipeline(ctx, subctx, to_fp16_vk_0, { vk_subbuffer{ d_Qx, qx_buf_offset, qx_sz * ne02 * ne03 }, vk_subbuffer{ d_X, 0, x_sz * ne02 * ne03 } }, pc, { (uint32_t)(x_ne * ne02 * ne03), 1, 1});
  5073. ggml_vk_sync_buffers(ctx, subctx);
  5074. }
  5075. if (y_non_contig) {
  5076. if (ctx->prealloc_y_last_pipeline_used != to_fp16_vk_1.get() ||
  5077. ctx->prealloc_y_last_tensor_used != src1) {
  5078. if (ctx->prealloc_y_need_sync) {
  5079. ggml_vk_sync_buffers(ctx, subctx);
  5080. }
  5081. ggml_vk_cpy_to_contiguous(ctx, subctx, to_fp16_vk_1, src1, { d_Qy, qy_buf_offset, VK_WHOLE_SIZE }, { d_Y, 0, VK_WHOLE_SIZE });
  5082. ctx->prealloc_y_last_pipeline_used = to_fp16_vk_1.get();
  5083. ctx->prealloc_y_last_tensor_used = src1;
  5084. }
  5085. }
  5086. if (quantize_y) {
  5087. if (ctx->prealloc_y_last_pipeline_used != to_q8_1.get() ||
  5088. ctx->prealloc_y_last_tensor_used != src1) {
  5089. if (ctx->prealloc_y_need_sync) {
  5090. ggml_vk_sync_buffers(ctx, subctx);
  5091. }
  5092. ggml_vk_quantize_q8_1(ctx, subctx, { d_Qy, qy_buf_offset, VK_WHOLE_SIZE }, { d_Y, 0, VK_WHOLE_SIZE }, y_ne * ne12 * ne13, true);
  5093. ctx->prealloc_y_last_pipeline_used = to_q8_1.get();
  5094. ctx->prealloc_y_last_tensor_used = src1;
  5095. }
  5096. }
  5097. uint32_t stride_batch_x = ne00*ne01;
  5098. uint32_t stride_batch_y = ne10*ne11;
  5099. if (!ggml_vk_dim01_contiguous(src0) && !qx_needs_dequant) {
  5100. stride_batch_x = src0->nb[0] / ggml_type_size(src0->type);
  5101. }
  5102. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant && !quantize_y) {
  5103. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  5104. }
  5105. uint32_t y_sz_total = y_sz * ne12 * ne13;
  5106. if (quantize_y) {
  5107. y_sz_total = CEIL_DIV(y_sz_total, 144) * 144;
  5108. }
  5109. // compute
  5110. ggml_vk_matmul(
  5111. ctx, subctx, pipeline,
  5112. { d_X, x_buf_offset, x_sz * ne02 * ne03 }, { d_Y, y_buf_offset, y_sz_total },
  5113. { d_D, d_buf_offset, d_sz * ne12 * ne13 }, { ctx->prealloc_split_k, 0, d_sz * ne12 * ne13 * split_k },
  5114. ne01, ne11, ne10,
  5115. ne10, ne10, ne01, stride_batch_x, stride_batch_y, ne20*ne21,
  5116. split_k, ne12*ne13, ne02, ne12, r2, r3, padded_n
  5117. ); // NOLINT
  5118. if (x_non_contig || qx_needs_dequant) {
  5119. ctx->prealloc_x_need_sync = true;
  5120. }
  5121. if (y_non_contig || quantize_y) {
  5122. ctx->prealloc_y_need_sync = true;
  5123. }
  5124. }
  5125. // Device tuning
  5126. static bool ggml_vk_should_use_mmvq(const vk_device& device, uint32_t m, uint32_t n, uint32_t k, ggml_type src0_type) {
  5127. if (device->mmvq_mode == 1) {
  5128. return true;
  5129. } else if (device->mmvq_mode == -1) {
  5130. return false;
  5131. }
  5132. // MMVQ is generally good for batches
  5133. if (n > 1) {
  5134. return true;
  5135. }
  5136. switch (device->vendor_id) {
  5137. case VK_VENDOR_ID_NVIDIA:
  5138. switch (src0_type) {
  5139. case GGML_TYPE_Q8_0:
  5140. return device->architecture == vk_device_architecture::NVIDIA_PRE_TURING;
  5141. default:
  5142. return true;
  5143. }
  5144. case VK_VENDOR_ID_AMD:
  5145. switch (src0_type) {
  5146. case GGML_TYPE_Q8_0:
  5147. return device->architecture == vk_device_architecture::AMD_GCN;
  5148. default:
  5149. return true;
  5150. }
  5151. case VK_VENDOR_ID_INTEL:
  5152. switch (src0_type) {
  5153. // From tests on A770 Linux, may need more tuning
  5154. case GGML_TYPE_Q4_0:
  5155. case GGML_TYPE_Q5_1:
  5156. return false;
  5157. default:
  5158. return true;
  5159. }
  5160. default:
  5161. return true;
  5162. }
  5163. GGML_UNUSED(m);
  5164. GGML_UNUSED(k);
  5165. }
  5166. static void ggml_vk_mul_mat_vec_q_f16(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  5167. VK_LOG_DEBUG("ggml_vk_mul_mat_vec_q_f16((" << src0 << ", name=" << src0->name << ", type=" << src0->type << ", ne0=" << src0->ne[0] << ", ne1=" << src0->ne[1] << ", ne2=" << src0->ne[2] << ", ne3=" << src0->ne[3] << ", nb0=" << src0->nb[0] << ", nb1=" << src0->nb[1] << ", nb2=" << src0->nb[2] << ", nb3=" << src0->nb[3];
  5168. std::cerr << "), (" << src1 << ", name=" << src1->name << ", type=" << src1->type << ", ne0=" << src1->ne[0] << ", ne1=" << src1->ne[1] << ", ne2=" << src1->ne[2] << ", ne3=" << src1->ne[3] << ", nb0=" << src1->nb[0] << ", nb1=" << src1->nb[1] << ", nb2=" << src1->nb[2] << ", nb3=" << src1->nb[3];
  5169. std::cerr << "), (" << dst << ", name=" << dst->name << ", type=" << dst->type << ", ne0=" << dst->ne[0] << ", ne1=" << dst->ne[1] << ", ne2=" << dst->ne[2] << ", ne3=" << dst->ne[3] << ", nb0=" << dst->nb[0] << ", nb1=" << dst->nb[1] << ", nb2=" << dst->nb[2] << ", nb3=" << dst->nb[3];
  5170. std::cerr << "), " << (dryrun ? "dryrun" : "") << "),)");
  5171. GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16); // NOLINT
  5172. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  5173. const uint64_t ne00 = src0->ne[0];
  5174. const uint64_t ne01 = src0->ne[1];
  5175. const uint64_t ne02 = src0->ne[2];
  5176. const uint64_t ne03 = src0->ne[3];
  5177. const uint64_t ne10 = src1->ne[0];
  5178. const uint64_t ne11 = src1->ne[1];
  5179. const uint64_t ne12 = src1->ne[2];
  5180. const uint64_t ne13 = src1->ne[3];
  5181. const uint64_t ne20 = dst->ne[0];
  5182. const uint64_t ne21 = dst->ne[1];
  5183. const uint64_t ne22 = dst->ne[2];
  5184. const uint64_t ne23 = dst->ne[3];
  5185. const uint64_t r2 = ne12 / ne02;
  5186. const uint64_t r3 = ne13 / ne03;
  5187. // batch_n indicates that we need to compute a few vector results, and this assumes
  5188. // ne12 and ne13 are 1. It overloads the batch_strides to hold the row strides.
  5189. GGML_ASSERT(ne11 == 1 || ne12 * ne13 == 1);
  5190. bool batch_n = ne11 > 1;
  5191. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  5192. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  5193. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  5194. vk_buffer d_Qx = nullptr;
  5195. size_t qx_buf_offset = 0;
  5196. vk_buffer d_Qy = nullptr;
  5197. size_t qy_buf_offset = 0;
  5198. bool src0_uma = false;
  5199. bool src1_uma = false;
  5200. if (ctx->device->uma) {
  5201. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  5202. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  5203. src0_uma = d_Qx != nullptr;
  5204. src1_uma = d_Qy != nullptr;
  5205. }
  5206. const bool x_non_contig = !ggml_vk_dim01_contiguous(src0);
  5207. const bool y_non_contig = !ggml_vk_dim01_contiguous(src1);
  5208. const bool f16_f32_kernel = src1->type == GGML_TYPE_F32;
  5209. bool quantize_y = ctx->device->integer_dot_product && src1->type == GGML_TYPE_F32 && ggml_is_contiguous(src1) && (ne11 * ne10) % 4 == 0 && ggml_vk_should_use_mmvq(ctx->device, ne01, ne11, ne10, src0->type);
  5210. vk_pipeline to_fp16_vk_0 = nullptr;
  5211. vk_pipeline to_fp16_vk_1 = nullptr;
  5212. if (x_non_contig) {
  5213. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, src0->type);
  5214. }
  5215. if (y_non_contig) {
  5216. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, src1->type);
  5217. } else {
  5218. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  5219. }
  5220. // Check for mmq first
  5221. vk_pipeline dmmv = quantize_y ? ggml_vk_get_dequantize_mul_mat_vec(ctx, src0->type, GGML_TYPE_Q8_1, ne11, ne20, ne00) : nullptr;
  5222. vk_pipeline to_q8_1 = nullptr;
  5223. if (dmmv == nullptr) {
  5224. // Fall back to f16 dequant mul mat
  5225. dmmv = ggml_vk_get_dequantize_mul_mat_vec(ctx, src0->type, src1->type, ne11, ne20, ne00);
  5226. quantize_y = false;
  5227. }
  5228. if (quantize_y) {
  5229. to_q8_1 = ggml_vk_get_quantize_pipeline(ctx, GGML_TYPE_Q8_1, true);
  5230. }
  5231. const bool qx_needs_dequant = x_non_contig;
  5232. const bool qy_needs_dequant = !quantize_y && ((src1->type != GGML_TYPE_F16 && !f16_f32_kernel) || y_non_contig);
  5233. // Not implemented
  5234. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  5235. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  5236. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  5237. GGML_ASSERT(dmmv != nullptr);
  5238. const uint64_t x_ne = ne01 * ne00;
  5239. const uint64_t y_ne = ne11 * ne10;
  5240. const uint64_t d_ne = ne11 * ne01;
  5241. const uint64_t qx_sz = ggml_vk_align_size(ggml_type_size(src0->type) * x_ne / ggml_blck_size(src0->type), ctx->device->properties.limits.minStorageBufferOffsetAlignment);
  5242. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  5243. const uint64_t x_sz = x_non_contig ? ggml_vk_align_size(ggml_type_size(src0->type) * x_ne, ctx->device->properties.limits.minStorageBufferOffsetAlignment) : qx_sz;
  5244. const uint64_t y_sz = quantize_y ? (y_ne * ggml_type_size(GGML_TYPE_Q8_1) / ggml_blck_size(GGML_TYPE_Q8_1)) : (f16_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne);
  5245. const uint64_t d_sz = sizeof(float) * d_ne;
  5246. if (dryrun) {
  5247. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  5248. uint64_t y_sz_upd = y_sz * ne12 * ne13;
  5249. if (quantize_y) {
  5250. y_sz_upd = CEIL_DIV(y_sz_upd, 144) * 144;
  5251. }
  5252. if (
  5253. (qx_needs_dequant && x_sz_upd > ctx->device->max_memory_allocation_size) ||
  5254. (qy_needs_dequant && y_sz_upd > ctx->device->max_memory_allocation_size)) {
  5255. GGML_ABORT("Requested preallocation size is too large");
  5256. }
  5257. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  5258. ctx->prealloc_size_x = x_sz_upd;
  5259. }
  5260. if ((qy_needs_dequant || quantize_y) && ctx->prealloc_size_y < y_sz_upd) {
  5261. ctx->prealloc_size_y = y_sz_upd;
  5262. }
  5263. // Request descriptor sets
  5264. if (qx_needs_dequant) {
  5265. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_0, 1);
  5266. }
  5267. if (qy_needs_dequant) {
  5268. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_1, 1);
  5269. }
  5270. if (quantize_y) {
  5271. ggml_pipeline_request_descriptor_sets(ctx, to_q8_1, 1);
  5272. }
  5273. ggml_pipeline_request_descriptor_sets(ctx, dmmv, 1);
  5274. return;
  5275. }
  5276. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  5277. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  5278. GGML_ASSERT(d_D != nullptr);
  5279. vk_buffer d_X;
  5280. uint64_t x_buf_offset = 0;
  5281. vk_buffer d_Y;
  5282. uint64_t y_buf_offset = 0;
  5283. if(!src0_uma) {
  5284. d_Qx = src0_buf_ctx->dev_buffer;
  5285. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  5286. GGML_ASSERT(d_Qx != nullptr);
  5287. }
  5288. if(!src1_uma) {
  5289. d_Qy = src1_buf_ctx->dev_buffer;
  5290. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  5291. GGML_ASSERT(d_Qy != nullptr);
  5292. }
  5293. if (qx_needs_dequant) {
  5294. d_X = ctx->prealloc_x;
  5295. } else {
  5296. d_X = d_Qx;
  5297. x_buf_offset = qx_buf_offset;
  5298. GGML_ASSERT(qx_sz == x_sz);
  5299. }
  5300. if (qy_needs_dequant) {
  5301. d_Y = ctx->prealloc_y;
  5302. } else if (quantize_y) {
  5303. d_Y = ctx->prealloc_y;
  5304. GGML_ASSERT(d_Y->size >= CEIL_DIV(y_sz * ne12 * ne13, 144) * 144);
  5305. } else {
  5306. d_Y = d_Qy;
  5307. y_buf_offset = qy_buf_offset;
  5308. GGML_ASSERT(qy_sz == y_sz);
  5309. }
  5310. if (x_non_contig) {
  5311. if (ctx->prealloc_x_need_sync) {
  5312. ggml_vk_sync_buffers(ctx, subctx);
  5313. }
  5314. GGML_ASSERT(x_sz == ggml_vk_align_size(ggml_type_size(src0->type) * x_ne, ctx->device->properties.limits.minStorageBufferOffsetAlignment));
  5315. ggml_vk_cpy_to_contiguous(ctx, subctx, to_fp16_vk_0, src0, { d_Qx, qx_buf_offset, VK_WHOLE_SIZE }, { d_X, 0, VK_WHOLE_SIZE });
  5316. }
  5317. if (y_non_contig) {
  5318. GGML_ASSERT(y_sz == ggml_type_size(src1->type) * y_ne);
  5319. if (ctx->prealloc_y_last_pipeline_used != to_fp16_vk_1.get() ||
  5320. ctx->prealloc_y_last_tensor_used != src1) {
  5321. if (ctx->prealloc_y_need_sync) {
  5322. ggml_vk_sync_buffers(ctx, subctx);
  5323. }
  5324. ggml_vk_cpy_to_contiguous(ctx, subctx, to_fp16_vk_1, src1, { d_Qy, qy_buf_offset, VK_WHOLE_SIZE }, { d_Y, 0, VK_WHOLE_SIZE });
  5325. ctx->prealloc_y_last_pipeline_used = to_fp16_vk_1.get();
  5326. ctx->prealloc_y_last_tensor_used = src1;
  5327. }
  5328. }
  5329. if (quantize_y) {
  5330. if (ctx->prealloc_y_last_pipeline_used != to_q8_1.get() ||
  5331. ctx->prealloc_y_last_tensor_used != src1) {
  5332. if (ctx->prealloc_y_need_sync) {
  5333. ggml_vk_sync_buffers(ctx, subctx);
  5334. }
  5335. ggml_vk_quantize_q8_1(ctx, subctx, { d_Qy, qy_buf_offset, VK_WHOLE_SIZE }, { d_Y, 0, VK_WHOLE_SIZE }, y_ne * ne12 * ne13, true);
  5336. ctx->prealloc_y_last_pipeline_used = to_q8_1.get();
  5337. ctx->prealloc_y_last_tensor_used = src1;
  5338. }
  5339. }
  5340. // For batch_n, the A matrix is the same for each batch, and B/D use the row stride as the batch stride
  5341. uint32_t stride_batch_x = batch_n ? 0 : ne00*ne01;
  5342. uint32_t stride_batch_y = batch_n ? ne10 : (ne10*ne11);
  5343. uint32_t stride_batch_d = batch_n ? ne20 : (ne20*ne21);
  5344. if (!ggml_vk_dim01_contiguous(src0) && !qx_needs_dequant) {
  5345. stride_batch_x = src0->nb[0] / ggml_type_size(src0->type);
  5346. }
  5347. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant) {
  5348. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  5349. }
  5350. const uint32_t max_groups_x = ctx->device->properties.limits.maxComputeWorkGroupCount[0];
  5351. uint32_t groups_x = ne01;
  5352. uint32_t groups_z = 1;
  5353. if (ne01 > max_groups_x) {
  5354. groups_z = 64;
  5355. groups_x = CEIL_DIV(groups_x, groups_z);
  5356. }
  5357. // TODO: Clean up this whole sz * ne_2 * ne_3 thing, it hasn't been necessary for a long time
  5358. uint32_t y_sz_total = y_sz * ne12 * ne13;
  5359. if (quantize_y) {
  5360. y_sz_total = CEIL_DIV(y_sz_total, 144) * 144;
  5361. }
  5362. // compute
  5363. const vk_mat_vec_push_constants pc = {
  5364. (uint32_t)ne00, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne01,
  5365. stride_batch_x, stride_batch_y, stride_batch_d,
  5366. (uint32_t)ne02, (uint32_t)ne12, (uint32_t)r2, (uint32_t)r3,
  5367. };
  5368. ggml_vk_dispatch_pipeline(ctx, subctx, dmmv,
  5369. { vk_subbuffer{ d_X, x_buf_offset, x_sz * ne02 * ne03 }, vk_subbuffer{ d_Y, y_buf_offset, y_sz_total }, vk_subbuffer{ d_D, d_buf_offset, d_sz * ne22 * ne23} },
  5370. pc, { groups_x, (uint32_t)(ne12 * ne13), groups_z });
  5371. if (x_non_contig) {
  5372. ctx->prealloc_x_need_sync = true;
  5373. }
  5374. if (y_non_contig || quantize_y) {
  5375. ctx->prealloc_y_need_sync = true;
  5376. }
  5377. }
  5378. static void ggml_vk_mul_mat_vec_p021_f16_f32(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  5379. VK_LOG_DEBUG("ggml_vk_mul_mat_p021_f16_f32(" << src0 << ", name=" << src0->name << ", type=" << src0->type << ", ne0=" << src0->ne[0] << ", ne1=" << src0->ne[1] << ", ne2=" << src0->ne[2] << ", ne3=" << src0->ne[3] << ", nb0=" << src0->nb[0] << ", nb1=" << src0->nb[1] << ", nb2=" << src0->nb[2] << ", nb3=" << src0->nb[3];
  5380. std::cerr << "), (" << src1 << ", name=" << src1->name << ", type=" << src1->type << ", ne0=" << src1->ne[0] << ", ne1=" << src1->ne[1] << ", ne2=" << src1->ne[2] << ", ne3=" << src1->ne[3] << ", nb0=" << src1->nb[0] << ", nb1=" << src1->nb[1] << ", nb2=" << src1->nb[2] << ", nb3=" << src1->nb[3];
  5381. std::cerr << "), (" << dst << ", name=" << dst->name << ", type=" << dst->type << ", ne0=" << dst->ne[0] << ", ne1=" << dst->ne[1] << ", ne2=" << dst->ne[2] << ", ne3=" << dst->ne[3] << ", nb0=" << dst->nb[0] << ", nb1=" << dst->nb[1] << ", nb2=" << dst->nb[2] << ", nb3=" << dst->nb[3];
  5382. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  5383. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  5384. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // NOLINT
  5385. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // NOLINT
  5386. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  5387. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  5388. const uint64_t ne00 = src0->ne[0];
  5389. const uint64_t ne01 = src0->ne[1];
  5390. const uint64_t ne02 = src0->ne[2];
  5391. // const uint64_t ne03 = src0->ne[3];
  5392. const uint64_t ne10 = src1->ne[0];
  5393. const uint64_t ne11 = src1->ne[1];
  5394. const uint64_t ne12 = src1->ne[2];
  5395. // const uint64_t ne13 = src1->ne[3];
  5396. GGML_ASSERT(ne11 == 1);
  5397. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  5398. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  5399. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  5400. vk_buffer d_Qy = nullptr;
  5401. size_t qy_buf_offset = 0;
  5402. bool src1_uma = false;
  5403. if (ctx->device->uma) {
  5404. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  5405. src1_uma = d_Qy != nullptr;
  5406. }
  5407. const uint64_t x_ne = ne00 * ne01 * ne02;
  5408. const uint64_t y_ne = ne10 * ne11 * ne12;
  5409. const uint64_t d_ne = ne01 * ne11 * ne12;
  5410. const uint64_t qx_sz = ggml_vk_align_size(ggml_type_size(src0->type) * x_ne / ggml_blck_size(src0->type), ctx->device->properties.limits.minStorageBufferOffsetAlignment);
  5411. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  5412. const uint64_t d_sz = sizeof(float) * d_ne;
  5413. // With grouped query attention there are > 1 Q matrices per K, V matrix.
  5414. uint32_t gqa_ratio = (uint32_t)ne12 / (uint32_t)ne02;
  5415. if (gqa_ratio > 8 || gqa_ratio == 0 || ne12 != ne02 * gqa_ratio) {
  5416. gqa_ratio = 1;
  5417. }
  5418. if (dryrun) {
  5419. // Request descriptor sets
  5420. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_mul_mat_vec_p021_f16_f32[gqa_ratio - 1], 1);
  5421. return;
  5422. }
  5423. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  5424. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  5425. GGML_ASSERT(d_D != nullptr);
  5426. vk_buffer d_Qx = src0_buf_ctx->dev_buffer;
  5427. const uint64_t qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  5428. GGML_ASSERT(d_Qx != nullptr);
  5429. if (!src1_uma) {
  5430. d_Qy = src1_buf_ctx->dev_buffer;
  5431. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  5432. GGML_ASSERT(d_Qx != nullptr);
  5433. }
  5434. const uint64_t qy_buffer_offset = (qy_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  5435. const uint64_t qy_shader_offset = qy_buf_offset - qy_buffer_offset;
  5436. const uint64_t d_buffer_offset = (d_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  5437. const uint64_t d_shader_offset = d_buf_offset - d_buffer_offset;
  5438. // compute
  5439. const std::array<uint32_t, 6> pc = { (uint32_t)ne00, (uint32_t)ne01, (uint32_t)ne02, (uint32_t)ne12, (uint32_t)(qy_shader_offset / ggml_type_size(src1->type)), (uint32_t)(d_shader_offset / ggml_type_size(dst->type)) };
  5440. uint32_t workgroups_z = (uint32_t)ne12;
  5441. // When gqa_ratio > 1, each invocation does multiple rows and we can launch fewer workgroups
  5442. if (gqa_ratio > 1) {
  5443. workgroups_z /= gqa_ratio;
  5444. }
  5445. ggml_vk_dispatch_pipeline(ctx, subctx, ctx->device->pipeline_mul_mat_vec_p021_f16_f32[gqa_ratio - 1], { vk_subbuffer{ d_Qx, qx_buf_offset, qx_sz }, vk_subbuffer{ d_Qy, qy_buffer_offset, qy_sz + qy_shader_offset }, vk_subbuffer{ d_D, d_buffer_offset, d_sz + d_shader_offset } }, pc, { 1, (uint32_t)ne01, workgroups_z });
  5446. }
  5447. static void ggml_vk_mul_mat_vec_nc_f16_f32(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  5448. VK_LOG_DEBUG("ggml_vk_mul_mat_nc_f16_f32((" << src0 << ", name=" << src0->name << ", type=" << src0->type << ", ne0=" << src0->ne[0] << ", ne1=" << src0->ne[1] << ", ne2=" << src0->ne[2] << ", ne3=" << src0->ne[3] << ", nb0=" << src0->nb[0] << ", nb1=" << src0->nb[1] << ", nb2=" << src0->nb[2] << ", nb3=" << src0->nb[3];
  5449. std::cerr << "), (" << src1 << ", name=" << src1->name << ", type=" << src1->type << ", ne0=" << src1->ne[0] << ", ne1=" << src1->ne[1] << ", ne2=" << src1->ne[2] << ", ne3=" << src1->ne[3] << ", nb0=" << src1->nb[0] << ", nb1=" << src1->nb[1] << ", nb2=" << src1->nb[2] << ", nb3=" << src1->nb[3];
  5450. std::cerr << "), (" << dst << ", name=" << dst->name << ", type=" << dst->type << ", ne0=" << dst->ne[0] << ", ne1=" << dst->ne[1] << ", ne2=" << dst->ne[2] << ", ne3=" << dst->ne[3] << ", nb0=" << dst->nb[0] << ", nb1=" << dst->nb[1] << ", nb2=" << dst->nb[2] << ", nb3=" << dst->nb[3];
  5451. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  5452. GGML_ASSERT(!ggml_is_transposed(src0));
  5453. GGML_ASSERT(!ggml_is_transposed(src1));
  5454. GGML_ASSERT(!ggml_is_permuted(src0));
  5455. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  5456. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  5457. const uint64_t ne00 = src0->ne[0];
  5458. const uint64_t ne01 = src0->ne[1];
  5459. const uint64_t ne02 = src0->ne[2];
  5460. const uint64_t ne03 = src0->ne[3];
  5461. const uint64_t nb01 = src0->nb[1];
  5462. const uint64_t nb02 = src0->nb[2];
  5463. const uint64_t nb12 = src1->nb[2];
  5464. // const uint64_t ne10 = src1->ne[0];
  5465. const uint64_t ne11 = src1->ne[1];
  5466. const uint64_t ne12 = src1->ne[2];
  5467. // const uint64_t ne13 = src1->ne[3];
  5468. const uint32_t nb03 = (uint32_t)(src0->nb[3] / sizeof(ggml_fp16_t));
  5469. const uint32_t nb13 = (uint32_t)(src1->nb[3] / sizeof(float));
  5470. const uint32_t nb23 = (uint32_t)(dst->nb[3] / sizeof(float));
  5471. GGML_ASSERT(ne11 == 1);
  5472. GGML_ASSERT(src0->ne[3] == src1->ne[3]); // checked in supports_op
  5473. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  5474. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  5475. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  5476. vk_buffer d_Qy = nullptr;
  5477. size_t qy_buf_offset = 0;
  5478. bool src1_uma = false;
  5479. if (ctx->device->uma) {
  5480. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  5481. src1_uma = d_Qy != nullptr;
  5482. }
  5483. const uint64_t d_ne = ne01 * ne11 * ne12 * ne03;
  5484. const uint32_t row_stride_x = nb01 / sizeof(ggml_fp16_t);
  5485. const uint32_t channel_stride_x = nb02 / sizeof(ggml_fp16_t);
  5486. const uint32_t channel_stride_y = nb12 / sizeof(float);
  5487. const uint64_t qx_sz = ggml_nbytes(src0);
  5488. const uint64_t qy_sz = ggml_nbytes(src1);
  5489. const uint64_t d_sz = sizeof(float) * d_ne;
  5490. if (dryrun) {
  5491. // Request descriptor sets
  5492. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_mul_mat_vec_nc_f16_f32, 1);
  5493. return;
  5494. }
  5495. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  5496. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  5497. GGML_ASSERT(d_D != nullptr);
  5498. vk_buffer d_Qx = src0_buf_ctx->dev_buffer;
  5499. const uint64_t qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  5500. GGML_ASSERT(d_Qx != nullptr);
  5501. if (!src1_uma) {
  5502. d_Qy = src1_buf_ctx->dev_buffer;
  5503. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  5504. GGML_ASSERT(d_Qx != nullptr);
  5505. }
  5506. const uint64_t qy_buffer_offset = (qy_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  5507. const uint64_t qy_shader_offset = qy_buf_offset - qy_buffer_offset;
  5508. const uint64_t d_buffer_offset = (d_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  5509. const uint64_t d_shader_offset = d_buf_offset - d_buffer_offset;
  5510. // compute
  5511. const std::array<uint32_t, 12> pc = { (uint32_t)ne00, (uint32_t)ne01, row_stride_x, channel_stride_x, channel_stride_y, (uint32_t)(ne12 / ne02), (uint32_t)ne12, (uint32_t)(qy_shader_offset / ggml_type_size(src1->type)), (uint32_t)(d_shader_offset / ggml_type_size(dst->type)), nb03, nb13, nb23 };
  5512. ggml_vk_dispatch_pipeline(ctx, subctx, ctx->device->pipeline_mul_mat_vec_nc_f16_f32,
  5513. { vk_subbuffer{ d_Qx, qx_buf_offset, qx_sz }, vk_subbuffer{ d_Qy, qy_buffer_offset, qy_sz + qy_shader_offset }, vk_subbuffer{ d_D, d_buffer_offset, d_sz + d_shader_offset } }, pc, { (uint32_t)ne03, (uint32_t)ne01, (uint32_t)ne12 });
  5514. }
  5515. static void ggml_vk_mul_mat(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  5516. VK_LOG_DEBUG("ggml_vk_mul_mat(" << src0 << ", " << src1 << ", " << dst << ")");
  5517. if (src0->type == GGML_TYPE_F16 && ggml_is_permuted(src0) && ggml_is_permuted(src1) && dst->ne[1] == 1 &&
  5518. // detect 0213 permutation, and batch size of 1
  5519. src0->nb[0] <= src0->nb[2] &&
  5520. src0->nb[2] <= src0->nb[1] &&
  5521. src0->nb[1] <= src0->nb[3] &&
  5522. src1->nb[0] <= src1->nb[2] &&
  5523. src1->nb[2] <= src1->nb[1] &&
  5524. src1->nb[1] <= src1->nb[3] &&
  5525. src0->ne[3] == 1 &&
  5526. src1->ne[3] == 1) {
  5527. ggml_vk_mul_mat_vec_p021_f16_f32(ctx, subctx, src0, src1, dst, dryrun);
  5528. } else if (src0->type == GGML_TYPE_F16 && !ggml_is_contiguous(src0) && !ggml_is_transposed(src1) && dst->ne[1] == 1 &&
  5529. !ggml_is_permuted(src0) && !ggml_is_permuted(src1)) {
  5530. ggml_vk_mul_mat_vec_nc_f16_f32(ctx, subctx, src0, src1, dst, dryrun);
  5531. // mul_mat_vec supports batching ne12*ne13 when ne11==1, or treating ne11 as the batch size (up to four)
  5532. // when ne12 and ne13 are one.
  5533. } else if ((dst->ne[1] == 1 || (dst->ne[1] <= mul_mat_vec_max_cols && src1->ne[2] * src1->ne[3] == 1)) &&
  5534. (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16 || ggml_is_quantized(src0->type))) {
  5535. ggml_vk_mul_mat_vec_q_f16(ctx, subctx, src0, src1, dst, dryrun);
  5536. } else {
  5537. ggml_vk_mul_mat_q_f16(ctx, subctx, src0, src1, dst, dryrun);
  5538. }
  5539. }
  5540. static void ggml_vk_mul_mat_id_q_f16(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * ids, ggml_tensor * dst, bool dryrun = false) {
  5541. VK_LOG_DEBUG("ggml_vk_mul_mat_id_q_f16((" << src0 << ", name=" << src0->name << ", type=" << src0->type << ", ne0=" << src0->ne[0] << ", ne1=" << src0->ne[1] << ", ne2=" << src0->ne[2] << ", ne3=" << src0->ne[3] << ", nb0=" << src0->nb[0] << ", nb1=" << src0->nb[1] << ", nb2=" << src0->nb[2] << ", nb3=" << src0->nb[3];
  5542. std::cerr << "), (" << src1 << ", name=" << src1->name << ", type=" << src1->type << ", ne0=" << src1->ne[0] << ", ne1=" << src1->ne[1] << ", ne2=" << src1->ne[2] << ", ne3=" << src1->ne[3] << ", nb0=" << src1->nb[0] << ", nb1=" << src1->nb[1] << ", nb2=" << src1->nb[2] << ", nb3=" << src1->nb[3];
  5543. std::cerr << "), (" << ids << ", name=" << ids->name << ", type=" << ids->type << ", ne0=" << ids->ne[0] << ", ne1=" << ids->ne[1] << ", ne2=" << ids->ne[2] << ", ne3=" << ids->ne[3] << ", nb0=" << ids->nb[0] << ", nb1=" << ids->nb[1] << ", nb2=" << ids->nb[2] << ", nb3=" << ids->nb[3];
  5544. std::cerr << "), (" << dst << ", name=" << dst->name << ", type=" << dst->type << ", ne0=" << dst->ne[0] << ", ne1=" << dst->ne[1] << ", ne2=" << dst->ne[2] << ", ne3=" << dst->ne[3] << ", nb0=" << dst->nb[0] << ", nb1=" << dst->nb[1] << ", nb2=" << dst->nb[2] << ", nb3=" << dst->nb[3] << "),)");
  5545. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  5546. GGML_ASSERT(ids->type == GGML_TYPE_I32);
  5547. const uint64_t ne00 = src0->ne[0];
  5548. const uint64_t ne01 = src0->ne[1];
  5549. const uint64_t ne02 = src0->ne[2];
  5550. const uint64_t ne03 = src0->ne[3];
  5551. const uint64_t ne10 = src1->ne[0];
  5552. const uint64_t ne11 = src1->ne[1];
  5553. const uint64_t ne12 = src1->ne[2];
  5554. const uint64_t ne13 = src1->ne[3];
  5555. const uint64_t nei0 = ids->ne[0];
  5556. const uint64_t nei1 = ids->ne[1];
  5557. const uint32_t nbi1 = ids->nb[1];
  5558. const uint32_t nbi2 = ids->nb[2];
  5559. const uint64_t ne20 = dst->ne[0];
  5560. const uint64_t ne21 = dst->ne[1];
  5561. const uint64_t ne22 = dst->ne[2];
  5562. const uint64_t ne23 = dst->ne[3];
  5563. const uint64_t n_as = ne02;
  5564. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  5565. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  5566. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  5567. ggml_backend_vk_buffer_context * ids_buf_ctx = (ggml_backend_vk_buffer_context *)ids->buffer->context;
  5568. vk_buffer d_Qx = nullptr;
  5569. size_t qx_buf_offset = 0;
  5570. vk_buffer d_Qy = nullptr;
  5571. size_t qy_buf_offset = 0;
  5572. vk_buffer d_ids = nullptr;
  5573. size_t ids_buf_offset = 0;
  5574. bool src0_uma = false;
  5575. bool src1_uma = false;
  5576. bool ids_uma = false;
  5577. if (ctx->device->uma) {
  5578. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  5579. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  5580. ggml_vk_host_get(ctx->device, ids->data, d_ids, ids_buf_offset);
  5581. src0_uma = d_Qx != nullptr;
  5582. src1_uma = d_Qy != nullptr;
  5583. ids_uma = d_ids != nullptr;
  5584. }
  5585. // Reformat and convert to fp16 if non-contiguous, or for coopmat2 for better perf
  5586. const bool x_non_contig = (ctx->device->coopmat2 && src0->type == GGML_TYPE_F32) ||
  5587. !ggml_vk_dim01_contiguous(src0);
  5588. const bool y_non_contig = (ctx->device->coopmat2 && src1->type == GGML_TYPE_F32) ||
  5589. (src0->type == GGML_TYPE_BF16 && src1->type != GGML_TYPE_BF16) ||
  5590. !ggml_vk_dim01_contiguous(src1);
  5591. // If src0 is BF16, try to use a BF16 x BF16 multiply
  5592. ggml_type f16_type = src0->type == GGML_TYPE_BF16 ? GGML_TYPE_BF16 : GGML_TYPE_F16;
  5593. const bool y_f32_kernel = src1->type == GGML_TYPE_F32 && !y_non_contig;
  5594. vk_matmul_pipeline mmp = ggml_vk_get_mul_mat_mat_id_pipeline(ctx, src0->type, y_non_contig ? f16_type : src1->type, (ggml_prec)dst->op_params[0]);
  5595. const bool qx_needs_dequant = mmp == nullptr || x_non_contig;
  5596. const bool qy_needs_dequant = (src1->type != f16_type && !y_f32_kernel) || y_non_contig;
  5597. if (qx_needs_dequant) {
  5598. // Fall back to dequant + f16 mulmat
  5599. mmp = ggml_vk_get_mul_mat_mat_id_pipeline(ctx, f16_type, y_f32_kernel ? GGML_TYPE_F32 : f16_type, (ggml_prec)dst->op_params[0]);
  5600. }
  5601. // Not implemented
  5602. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  5603. const uint32_t kpad = ggml_vk_align_size(ne10, ggml_vk_guess_matmul_id_pipeline_align(ctx, mmp, ne01, nei1, qx_needs_dequant ? f16_type : src0->type));
  5604. const bool aligned = ne10 == kpad && ne01 > 8 && nei1 > 8;
  5605. vk_pipeline pipeline = ggml_vk_guess_matmul_id_pipeline(ctx, mmp, ne01, nei1, aligned, qx_needs_dequant ? f16_type : src0->type);
  5606. // Reserve extra storage in the N dimension for the Y matrix, so we can avoid bounds-checking
  5607. uint32_t padded_n = qy_needs_dequant ? ROUNDUP_POW2(ne11, pipeline->wg_denoms[1]) :ne11;
  5608. const uint64_t x_ne = ne01 * ne00;
  5609. const uint64_t y_ne = padded_n * ne10;
  5610. const uint64_t d_ne = ne21 * ne20;
  5611. const uint64_t qx_sz = ggml_type_size(src0->type) * x_ne / ggml_blck_size(src0->type);
  5612. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  5613. const uint64_t x_sz = !qx_needs_dequant ? qx_sz : sizeof(ggml_fp16_t) * x_ne;
  5614. const uint64_t y_sz = y_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne;
  5615. const uint64_t ids_sz = nbi2;
  5616. const uint64_t d_sz = sizeof(float) * d_ne;
  5617. vk_pipeline to_fp16_vk_0 = nullptr;
  5618. vk_pipeline to_fp16_vk_1 = nullptr;
  5619. if (x_non_contig) {
  5620. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, f16_type);
  5621. } else {
  5622. to_fp16_vk_0 = ggml_vk_get_to_fp16(ctx, src0->type);
  5623. }
  5624. if (y_non_contig) {
  5625. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, f16_type);
  5626. } else {
  5627. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  5628. }
  5629. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  5630. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  5631. if (dryrun) {
  5632. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  5633. const uint64_t y_sz_upd = y_sz * ne12 * ne13;
  5634. if (
  5635. (qx_needs_dequant && x_sz_upd > ctx->device->max_memory_allocation_size) ||
  5636. (qy_needs_dequant && y_sz_upd > ctx->device->max_memory_allocation_size)) {
  5637. GGML_ABORT("Requested preallocation size is too large");
  5638. }
  5639. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  5640. ctx->prealloc_size_x = x_sz_upd;
  5641. }
  5642. if (qy_needs_dequant && ctx->prealloc_size_y < y_sz_upd) {
  5643. ctx->prealloc_size_y = y_sz_upd;
  5644. }
  5645. // Request descriptor sets
  5646. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  5647. if (qx_needs_dequant) {
  5648. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_0, 1);
  5649. }
  5650. if (qy_needs_dequant) {
  5651. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_1, 1);
  5652. }
  5653. return;
  5654. }
  5655. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  5656. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  5657. GGML_ASSERT(d_D != nullptr);
  5658. vk_buffer d_X;
  5659. uint64_t x_buf_offset = 0;
  5660. vk_buffer d_Y;
  5661. uint64_t y_buf_offset = 0;
  5662. if (!src0_uma) {
  5663. d_Qx = src0_buf_ctx->dev_buffer;
  5664. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  5665. GGML_ASSERT(d_Qx != nullptr);
  5666. }
  5667. if (!src1_uma) {
  5668. d_Qy = src1_buf_ctx->dev_buffer;
  5669. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  5670. GGML_ASSERT(d_Qy != nullptr);
  5671. }
  5672. if (!ids_uma) {
  5673. d_ids = ids_buf_ctx->dev_buffer;
  5674. ids_buf_offset = vk_tensor_offset(ids) + ids->view_offs;
  5675. GGML_ASSERT(d_ids != nullptr);
  5676. }
  5677. if (qx_needs_dequant) {
  5678. d_X = ctx->prealloc_x;
  5679. GGML_ASSERT(d_X->size >= x_sz * ne02 * ne03);
  5680. } else {
  5681. d_X = d_Qx;
  5682. x_buf_offset = qx_buf_offset;
  5683. GGML_ASSERT(qx_sz == x_sz);
  5684. }
  5685. if (qy_needs_dequant) {
  5686. d_Y = ctx->prealloc_y;
  5687. GGML_ASSERT(d_Y->size >= y_sz * ne12 * ne13);
  5688. } else {
  5689. d_Y = d_Qy;
  5690. y_buf_offset = qy_buf_offset;
  5691. GGML_ASSERT(qy_sz == y_sz);
  5692. }
  5693. if (x_non_contig || qx_needs_dequant) {
  5694. if (ctx->prealloc_x_need_sync) {
  5695. ggml_vk_sync_buffers(ctx, subctx);
  5696. }
  5697. }
  5698. if (x_non_contig) {
  5699. ggml_vk_cpy_to_contiguous(ctx, subctx, to_fp16_vk_0, src0, { d_Qx, qx_buf_offset, VK_WHOLE_SIZE }, { d_X, 0, VK_WHOLE_SIZE });
  5700. } else if (qx_needs_dequant) {
  5701. const std::vector<uint32_t> pc = { (uint32_t)ne01, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)(ggml_nelements(src0)) };
  5702. ggml_vk_dispatch_pipeline(ctx, subctx, to_fp16_vk_0,
  5703. { vk_subbuffer{ d_Qx, qx_buf_offset, qx_sz * ne02 * ne03 }, vk_subbuffer{ d_X, 0, x_sz * ne02 * ne03 } }, pc, { (uint32_t)(x_ne * ne02 * ne03), 1, 1});
  5704. ggml_vk_sync_buffers(ctx, subctx);
  5705. }
  5706. if (y_non_contig) {
  5707. if (ctx->prealloc_y_last_pipeline_used != to_fp16_vk_1.get() ||
  5708. ctx->prealloc_y_last_tensor_used != src1) {
  5709. if (ctx->prealloc_y_need_sync) {
  5710. ggml_vk_sync_buffers(ctx, subctx);
  5711. }
  5712. ggml_vk_cpy_to_contiguous(ctx, subctx, to_fp16_vk_1, src1, { d_Qy, qy_buf_offset, VK_WHOLE_SIZE }, { d_Y, 0, VK_WHOLE_SIZE });
  5713. ctx->prealloc_y_last_pipeline_used = to_fp16_vk_1.get();
  5714. ctx->prealloc_y_last_tensor_used = src1;
  5715. }
  5716. }
  5717. uint32_t stride_batch_x = ne00*ne01;
  5718. uint32_t stride_batch_y = ne10*ne11;
  5719. if (!ggml_vk_dim01_contiguous(src0) && !qx_needs_dequant) {
  5720. stride_batch_x = src0->nb[0] / ggml_type_size(src0->type);
  5721. }
  5722. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant) {
  5723. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  5724. }
  5725. // compute
  5726. ggml_vk_matmul_id(
  5727. ctx, subctx, pipeline,
  5728. { d_X, x_buf_offset, x_sz * ne02 * ne03 }, { d_Y, y_buf_offset, y_sz * ne12 * ne13 },
  5729. { d_D, d_buf_offset, d_sz * ne22 * ne23 }, { d_ids, ids_buf_offset, ids_sz },
  5730. ne01, ne21, ne10, ne10, ne10, ne01,
  5731. stride_batch_x, stride_batch_y, ne20*ne21,
  5732. n_as, nei0, nei1, nbi1 / ggml_type_size(ids->type), ne11, padded_n
  5733. ); // NOLINT
  5734. if (x_non_contig || qx_needs_dequant) {
  5735. ctx->prealloc_x_need_sync = true;
  5736. }
  5737. if (y_non_contig) {
  5738. ctx->prealloc_y_need_sync = true;
  5739. }
  5740. }
  5741. static void ggml_vk_mul_mat_vec_id_q_f16(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * ids, ggml_tensor * dst, bool dryrun = false) {
  5742. VK_LOG_DEBUG("ggml_vk_mul_mat_vec_id_q_f16((" << src0 << ", name=" << src0->name << ", type=" << src0->type << ", ne0=" << src0->ne[0] << ", ne1=" << src0->ne[1] << ", ne2=" << src0->ne[2] << ", ne3=" << src0->ne[3] << ", nb0=" << src0->nb[0] << ", nb1=" << src0->nb[1] << ", nb2=" << src0->nb[2] << ", nb3=" << src0->nb[3];
  5743. std::cerr << "), (" << src1 << ", name=" << src1->name << ", type=" << src1->type << ", ne0=" << src1->ne[0] << ", ne1=" << src1->ne[1] << ", ne2=" << src1->ne[2] << ", ne3=" << src1->ne[3] << ", nb0=" << src1->nb[0] << ", nb1=" << src1->nb[1] << ", nb2=" << src1->nb[2] << ", nb3=" << src1->nb[3];
  5744. std::cerr << "), (" << ids << ", name=" << ids->name << ", type=" << ids->type << ", ne0=" << ids->ne[0] << ", ne1=" << ids->ne[1] << ", ne2=" << ids->ne[2] << ", ne3=" << ids->ne[3] << ", nb0=" << ids->nb[0] << ", nb1=" << ids->nb[1] << ", nb2=" << ids->nb[2] << ", nb3=" << ids->nb[3];
  5745. std::cerr << "), (" << dst << ", name=" << dst->name << ", type=" << dst->type << ", ne0=" << dst->ne[0] << ", ne1=" << dst->ne[1] << ", ne2=" << dst->ne[2] << ", ne3=" << dst->ne[3] << ", nb0=" << dst->nb[0] << ", nb1=" << dst->nb[1] << ", nb2=" << dst->nb[2] << ", nb3=" << dst->nb[3];
  5746. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  5747. GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16); // NOLINT
  5748. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  5749. GGML_ASSERT(ids->type == GGML_TYPE_I32);
  5750. const uint64_t ne00 = src0->ne[0];
  5751. const uint64_t ne01 = src0->ne[1];
  5752. const uint64_t ne02 = src0->ne[2];
  5753. const uint64_t ne03 = src0->ne[3];
  5754. const uint64_t ne10 = src1->ne[0];
  5755. const uint64_t ne11 = src1->ne[1];
  5756. const uint64_t ne12 = src1->ne[2];
  5757. const uint64_t ne13 = src1->ne[3];
  5758. const uint64_t nei0 = ids->ne[0];
  5759. const uint64_t nei1 = ids->ne[1];
  5760. const uint64_t nbi2 = ids->nb[2];
  5761. GGML_ASSERT(nei1 == 1);
  5762. const uint64_t ne20 = dst->ne[0];
  5763. const uint64_t ne21 = dst->ne[1];
  5764. const uint64_t ne22 = dst->ne[2];
  5765. const uint64_t ne23 = dst->ne[3];
  5766. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  5767. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  5768. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  5769. ggml_backend_vk_buffer_context * ids_buf_ctx = (ggml_backend_vk_buffer_context *)ids->buffer->context;
  5770. vk_buffer d_Qx = nullptr;
  5771. size_t qx_buf_offset = 0;
  5772. vk_buffer d_Qy = nullptr;
  5773. size_t qy_buf_offset = 0;
  5774. vk_buffer d_ids = nullptr;
  5775. size_t ids_buf_offset = 0;
  5776. bool src0_uma = false;
  5777. bool src1_uma = false;
  5778. bool ids_uma = false;
  5779. if (ctx->device->uma) {
  5780. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  5781. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  5782. ggml_vk_host_get(ctx->device, ids->data, d_ids, ids_buf_offset);
  5783. src0_uma = d_Qx != nullptr;
  5784. src1_uma = d_Qy != nullptr;
  5785. ids_uma = d_ids != nullptr;
  5786. }
  5787. const bool x_non_contig = !ggml_vk_dim01_contiguous(src0);
  5788. const bool y_non_contig = !ggml_vk_dim01_contiguous(src1);
  5789. const bool f16_f32_kernel = src1->type == GGML_TYPE_F32;
  5790. const bool qx_needs_dequant = x_non_contig;
  5791. const bool qy_needs_dequant = (src1->type != GGML_TYPE_F16 && !f16_f32_kernel) || y_non_contig;
  5792. // Not implemented
  5793. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  5794. const uint64_t x_ne = ne01 * ne00;
  5795. const uint64_t y_ne = ne11 * ne10;
  5796. const uint64_t d_ne = ne21 * ne20;
  5797. const uint64_t qx_sz = ggml_vk_align_size(ggml_type_size(src0->type) * x_ne / ggml_blck_size(src0->type), ctx->device->properties.limits.minStorageBufferOffsetAlignment);
  5798. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  5799. const uint64_t x_sz = x_non_contig ? ggml_vk_align_size(ggml_type_size(src0->type) * x_ne, ctx->device->properties.limits.minStorageBufferOffsetAlignment) : qx_sz;
  5800. const uint64_t y_sz = f16_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne;
  5801. const uint64_t ids_sz = nbi2;
  5802. const uint64_t d_sz = sizeof(float) * d_ne;
  5803. vk_pipeline to_fp16_vk_0 = nullptr;
  5804. vk_pipeline to_fp16_vk_1 = nullptr;
  5805. if (x_non_contig) {
  5806. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, src0->type);
  5807. }
  5808. if (y_non_contig) {
  5809. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, src1->type);
  5810. } else {
  5811. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  5812. }
  5813. vk_pipeline dmmv = ggml_vk_get_dequantize_mul_mat_vec_id(ctx, src0->type, src1->type);
  5814. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  5815. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  5816. GGML_ASSERT(dmmv != nullptr);
  5817. if (dryrun) {
  5818. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  5819. const uint64_t y_sz_upd = y_sz * ne12 * ne13;
  5820. if (
  5821. (qx_needs_dequant && x_sz_upd > ctx->device->max_memory_allocation_size) ||
  5822. (qy_needs_dequant && y_sz_upd > ctx->device->max_memory_allocation_size)) {
  5823. GGML_ABORT("Requested preallocation size is too large");
  5824. }
  5825. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  5826. ctx->prealloc_size_x = x_sz_upd;
  5827. }
  5828. if (qy_needs_dequant && ctx->prealloc_size_y < y_sz_upd) {
  5829. ctx->prealloc_size_y = y_sz_upd;
  5830. }
  5831. // Request descriptor sets
  5832. if (qx_needs_dequant) {
  5833. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_0, 1);
  5834. }
  5835. if (qy_needs_dequant) {
  5836. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_1, 1);
  5837. }
  5838. ggml_pipeline_request_descriptor_sets(ctx, dmmv, 1);
  5839. return;
  5840. }
  5841. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  5842. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  5843. GGML_ASSERT(d_D != nullptr);
  5844. vk_buffer d_X;
  5845. uint64_t x_buf_offset = 0;
  5846. vk_buffer d_Y;
  5847. uint64_t y_buf_offset = 0;
  5848. if(!src0_uma) {
  5849. d_Qx = src0_buf_ctx->dev_buffer;
  5850. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  5851. GGML_ASSERT(d_Qx != nullptr);
  5852. }
  5853. if(!src1_uma) {
  5854. d_Qy = src1_buf_ctx->dev_buffer;
  5855. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  5856. GGML_ASSERT(d_Qy != nullptr);
  5857. }
  5858. if(!ids_uma) {
  5859. d_ids = ids_buf_ctx->dev_buffer;
  5860. ids_buf_offset = vk_tensor_offset(ids) + ids->view_offs;
  5861. GGML_ASSERT(d_ids != nullptr);
  5862. }
  5863. if (qx_needs_dequant) {
  5864. d_X = ctx->prealloc_x;
  5865. } else {
  5866. d_X = d_Qx;
  5867. x_buf_offset = qx_buf_offset;
  5868. GGML_ASSERT(qx_sz == x_sz);
  5869. }
  5870. if (qy_needs_dequant) {
  5871. d_Y = ctx->prealloc_y;
  5872. } else {
  5873. d_Y = d_Qy;
  5874. y_buf_offset = qy_buf_offset;
  5875. GGML_ASSERT(qy_sz == y_sz);
  5876. }
  5877. if (x_non_contig) {
  5878. if (ctx->prealloc_x_need_sync) {
  5879. ggml_vk_sync_buffers(ctx, subctx);
  5880. }
  5881. }
  5882. if (x_non_contig) {
  5883. GGML_ASSERT(x_sz == ggml_vk_align_size(ggml_type_size(src0->type) * x_ne, ctx->device->properties.limits.minStorageBufferOffsetAlignment));
  5884. ggml_vk_cpy_to_contiguous(ctx, subctx, to_fp16_vk_0, src0, { d_Qx, qx_buf_offset, VK_WHOLE_SIZE }, { d_X, 0, VK_WHOLE_SIZE });
  5885. }
  5886. if (y_non_contig) {
  5887. GGML_ASSERT(y_sz == ggml_type_size(src1->type) * y_ne);
  5888. if (ctx->prealloc_y_last_pipeline_used != to_fp16_vk_1.get() ||
  5889. ctx->prealloc_y_last_tensor_used != src1) {
  5890. if (ctx->prealloc_y_need_sync) {
  5891. ggml_vk_sync_buffers(ctx, subctx);
  5892. }
  5893. ggml_vk_cpy_to_contiguous(ctx, subctx, to_fp16_vk_1, src1, { d_Qy, qy_buf_offset, VK_WHOLE_SIZE }, { d_Y, 0, VK_WHOLE_SIZE });
  5894. ctx->prealloc_y_last_pipeline_used = to_fp16_vk_1.get();
  5895. ctx->prealloc_y_last_tensor_used = src1;
  5896. }
  5897. }
  5898. uint32_t stride_batch_y = ne10*ne11;
  5899. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant) {
  5900. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  5901. }
  5902. const uint32_t max_groups_x = ctx->device->properties.limits.maxComputeWorkGroupCount[0];
  5903. uint32_t groups_x = ne01;
  5904. uint32_t groups_z = 1;
  5905. if (ne01 > max_groups_x) {
  5906. groups_z = 64;
  5907. groups_x = CEIL_DIV(groups_x, groups_z);
  5908. }
  5909. // compute
  5910. const vk_mat_vec_id_push_constants pc = {
  5911. (uint32_t)ne00, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne01,
  5912. (uint32_t)x_ne, stride_batch_y, (uint32_t)(ne20*ne21),
  5913. (uint32_t)nei0, (uint32_t)ne11,
  5914. };
  5915. ggml_vk_dispatch_pipeline(ctx, subctx, dmmv,
  5916. { vk_subbuffer{ d_X, x_buf_offset, x_sz * ne02 * ne03 },
  5917. vk_subbuffer{ d_Y, y_buf_offset, y_sz * ne12 * ne13 }, vk_subbuffer{ d_D, d_buf_offset, d_sz * ne22 * ne23}, vk_subbuffer{ d_ids, ids_buf_offset, ids_sz } },
  5918. pc, { groups_x, (uint32_t)nei0, groups_z });
  5919. if (x_non_contig) {
  5920. ctx->prealloc_x_need_sync = true;
  5921. }
  5922. if (y_non_contig) {
  5923. ctx->prealloc_y_need_sync = true;
  5924. }
  5925. }
  5926. static void ggml_vk_mul_mat_id(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, ggml_tensor * dst, bool dryrun = false) {
  5927. VK_LOG_DEBUG("ggml_vk_mul_mat_id(" << src0 << ", " << src1 << ", " << src2 << ", " << dst << ")");
  5928. if (src2->ne[1] == 1 && (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type))) {
  5929. ggml_vk_mul_mat_vec_id_q_f16(ctx, subctx, src0, src1, src2, dst, dryrun);
  5930. } else {
  5931. ggml_vk_mul_mat_id_q_f16(ctx, subctx, src0, src1, src2, dst, dryrun);
  5932. }
  5933. }
  5934. static bool ggml_vk_flash_attn_scalar_shmem_support(const vk_device& device, const uint32_t hsk, uint32_t hsv) {
  5935. // Needs to be kept up to date on shader changes
  5936. GGML_UNUSED(hsv);
  5937. const uint32_t wg_size = scalar_flash_attention_workgroup_size;
  5938. const uint32_t Br = get_fa_scalar_num_large_rows(hsv);
  5939. const uint32_t Bc = scalar_flash_attention_Bc;
  5940. const uint32_t tmpsh = wg_size * sizeof(float);
  5941. const uint32_t tmpshv4 = wg_size * 4 * sizeof(float);
  5942. const uint32_t masksh = Bc * Br * sizeof(float);
  5943. const uint32_t Qf = Br * (hsk / 4 + 2) * 4 * sizeof(float);
  5944. const uint32_t total_size = tmpsh + tmpshv4 + masksh + Qf;
  5945. const bool supported = total_size <= device->properties.limits.maxComputeSharedMemorySize;
  5946. VK_LOG_DEBUG("ggml_vk_flash_attn_coopmat_shmem_support(HSK=" << hsk << ", HSV=" << hsv << ", total_size=" << total_size << ", supported=" << supported);
  5947. return supported;
  5948. }
  5949. static bool ggml_vk_flash_attn_coopmat_shmem_support(const vk_device& device, const uint32_t hsk, uint32_t hsv, bool f32acc) {
  5950. // Needs to be kept up to date on shader changes
  5951. GGML_UNUSED(hsv);
  5952. const uint32_t wg_size = scalar_flash_attention_workgroup_size;
  5953. const uint32_t Br = coopmat1_flash_attention_num_large_rows;
  5954. const uint32_t Bc = scalar_flash_attention_Bc;
  5955. const uint32_t hsk_pad = ROUNDUP_POW2(hsk, 16);
  5956. const uint32_t acctype = f32acc ? 4 : 2;
  5957. const uint32_t f16vec4 = 8;
  5958. const uint32_t tmpsh = wg_size * sizeof(float);
  5959. const uint32_t tmpshv4 = wg_size * 4 * acctype;
  5960. const uint32_t qstride = hsk_pad / 4 + 2;
  5961. const uint32_t Qf = Br * qstride * f16vec4;
  5962. const uint32_t sfshstride = (hsk <= 128) ? (Br + 8) : Br;
  5963. const uint32_t sfsh = Bc * sfshstride * acctype;
  5964. const uint32_t kshstride = hsk_pad / 4 + 2;
  5965. const uint32_t ksh = Bc * kshstride * f16vec4;
  5966. const uint32_t slope = Br * sizeof(float);
  5967. const uint32_t total_size = tmpsh + tmpshv4 + Qf + sfsh + ksh + slope;
  5968. const bool supported = total_size <= device->properties.limits.maxComputeSharedMemorySize;
  5969. VK_LOG_DEBUG("ggml_vk_flash_attn_coopmat_shmem_support(HSK=" << hsk << ", HSV=" << hsv << ", f32acc=" << f32acc << ", total_size=" << total_size << ", supported=" << supported);
  5970. return supported;
  5971. }
  5972. static void ggml_vk_flash_attn(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * q, const ggml_tensor * k, const ggml_tensor * v, const ggml_tensor * mask, const ggml_tensor * sinks, ggml_tensor * dst, bool dryrun = false) {
  5973. VK_LOG_DEBUG("ggml_vk_flash_attn((" << q << ", name=" << q->name << ", type=" << q->type << ", ne0=" << q->ne[0] << ", ne1=" << q->ne[1] << ", ne2=" << q->ne[2] << ", ne3=" << q->ne[3] << ", nb0=" << q->nb[0] << ", nb1=" << q->nb[1] << ", nb2=" << q->nb[2] << ", nb3=" << q->nb[3];
  5974. std::cerr << "), (" << k << ", name=" << k->name << ", type=" << k->type << ", ne0=" << k->ne[0] << ", ne1=" << k->ne[1] << ", ne2=" << k->ne[2] << ", ne3=" << k->ne[3] << ", nb0=" << k->nb[0] << ", nb1=" << k->nb[1] << ", nb2=" << k->nb[2] << ", nb3=" << k->nb[3];
  5975. std::cerr << "), (" << v << ", name=" << v->name << ", type=" << v->type << ", ne0=" << v->ne[0] << ", ne1=" << v->ne[1] << ", ne2=" << v->ne[2] << ", ne3=" << v->ne[3] << ", nb0=" << v->nb[0] << ", nb1=" << v->nb[1] << ", nb2=" << v->nb[2] << ", nb3=" << v->nb[3];
  5976. std::cerr << "), (" << dst << ", name=" << dst->name << ", type=" << dst->type << ", ne0=" << dst->ne[0] << ", ne1=" << dst->ne[1] << ", ne2=" << dst->ne[2] << ", ne3=" << dst->ne[3] << ", nb0=" << dst->nb[0] << ", nb1=" << dst->nb[1] << ", nb2=" << dst->nb[2] << ", nb3=" << dst->nb[3];
  5977. if (sinks) {
  5978. std::cerr << "), (" << sinks << ", name=" << sinks->name << ", type=" << sinks->type << ", ne0=" << sinks->ne[0] << ", ne1=" << sinks->ne[1] << ", ne2=" << sinks->ne[2] << ", ne3=" << sinks->ne[3] << ", nb0=" << sinks->nb[0] << ", nb1=" << sinks->nb[1] << ", nb2=" << sinks->nb[2] << ", nb3=" << sinks->nb[3];
  5979. }
  5980. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  5981. GGML_TENSOR_LOCALS(int64_t, neq, q, ne)
  5982. GGML_TENSOR_LOCALS(size_t, nbq, q, nb)
  5983. GGML_TENSOR_LOCALS(int64_t, nek, k, ne)
  5984. GGML_TENSOR_LOCALS(size_t, nbk, k, nb)
  5985. GGML_TENSOR_LOCALS(int64_t, nev, v, ne)
  5986. GGML_TENSOR_LOCALS(size_t, nbv, v, nb)
  5987. GGML_TENSOR_LOCALS(int64_t, ne, dst, ne)
  5988. GGML_TENSOR_LOCALS(size_t, nb, dst, nb)
  5989. const uint32_t nem1 = mask ? mask->ne[1] : 0;
  5990. const uint32_t nem2 = mask ? mask->ne[2] : 0;
  5991. const uint32_t nem3 = mask ? mask->ne[3] : 0;
  5992. const uint32_t HSK = nek0;
  5993. const uint32_t HSV = nev0;
  5994. uint32_t N = neq1;
  5995. const uint32_t KV = nek1;
  5996. GGML_ASSERT(ne0 == HSV);
  5997. GGML_ASSERT(ne2 == N);
  5998. // input tensor rows must be contiguous
  5999. GGML_ASSERT(nbq0 == ggml_type_size(q->type));
  6000. GGML_ASSERT(nbk0 == ggml_type_size(k->type));
  6001. GGML_ASSERT(nbv0 == ggml_type_size(v->type));
  6002. GGML_ASSERT(neq0 == HSK);
  6003. GGML_ASSERT(neq1 == N);
  6004. GGML_ASSERT(nev1 == nek1);
  6005. // dst cannot be transposed or permuted
  6006. GGML_ASSERT(nb0 == sizeof(float));
  6007. GGML_ASSERT(nb0 <= nb1);
  6008. GGML_ASSERT(nb1 <= nb2);
  6009. GGML_ASSERT(nb2 <= nb3);
  6010. assert(dst->type == GGML_TYPE_F32);
  6011. assert(q->type == GGML_TYPE_F32);
  6012. assert(k->type == v->type);
  6013. FaCodePath path = ctx->device->coopmat2 ? FA_COOPMAT2 :
  6014. ctx->device->coopmat1_fa_support ? FA_COOPMAT1 : FA_SCALAR;
  6015. if (path == FA_COOPMAT1) {
  6016. const bool coopmat_shape_supported = (dst->op_params[3] == GGML_PREC_F32 && ctx->device->coopmat_support_16x16x16_f32acc) ||
  6017. (dst->op_params[3] != GGML_PREC_F32 && ctx->device->coopmat_support_16x16x16_f16acc);
  6018. const bool coopmat_shmem_supported = ggml_vk_flash_attn_coopmat_shmem_support(ctx->device, HSK, HSV, dst->op_params[3] == GGML_PREC_F32);
  6019. if (!coopmat_shape_supported || !coopmat_shmem_supported) {
  6020. path = FA_SCALAR;
  6021. }
  6022. }
  6023. uint32_t gqa_ratio = 1;
  6024. uint32_t qk_ratio = neq2 / nek2;
  6025. uint32_t workgroups_x = (uint32_t)neq1;
  6026. uint32_t workgroups_y = (uint32_t)neq2;
  6027. uint32_t workgroups_z = (uint32_t)neq3;
  6028. // For scalar/coopmat1 FA, we can use the "large" size to accommodate qga.
  6029. // For coopmat2 FA, we always use the small size (which is still pretty large for gqa).
  6030. uint32_t max_gqa;
  6031. switch (path) {
  6032. case FA_SCALAR:
  6033. case FA_COOPMAT1:
  6034. // We may switch from coopmat1 to scalar, so use the scalar limit for both
  6035. max_gqa = get_fa_scalar_num_large_rows(HSV);
  6036. break;
  6037. case FA_COOPMAT2:
  6038. max_gqa = get_fa_num_small_rows(FA_COOPMAT2);
  6039. break;
  6040. default:
  6041. GGML_ASSERT(0);
  6042. }
  6043. if (N == 1 && qk_ratio > 1 && qk_ratio <= max_gqa &&
  6044. qk_ratio * nek2 == neq2 && nek2 == nev2 && nem2 <= 1) {
  6045. // grouped query attention - make the N dimension equal to gqa_ratio, reduce
  6046. // workgroups proportionally in y dimension. The shader will detect gqa_ratio > 1
  6047. // and change addressing calculations to index Q's dimension 2.
  6048. gqa_ratio = qk_ratio;
  6049. N = gqa_ratio;
  6050. workgroups_y /= N;
  6051. }
  6052. bool small_rows = N <= get_fa_num_small_rows(path);
  6053. // coopmat1 does not actually support "small rows" (it needs 16 rows).
  6054. // So use scalar instead.
  6055. if (small_rows && path == FA_COOPMAT1) {
  6056. path = FA_SCALAR;
  6057. }
  6058. // scalar is faster than coopmat2 when N==1
  6059. if (N == 1 && path == FA_COOPMAT2) {
  6060. path = FA_SCALAR;
  6061. }
  6062. // with large hsk/hsv, scalar path may need to use small_rows to fit in shared memory
  6063. if (path == FA_SCALAR &&
  6064. !ggml_vk_flash_attn_scalar_shmem_support(ctx->device, HSK, HSV)) {
  6065. small_rows = true;
  6066. }
  6067. const uint32_t q_stride = (uint32_t)(nbq1 / ggml_type_size(q->type));
  6068. const uint32_t k_stride = (uint32_t)(nbk1 / ggml_type_size(k->type));
  6069. const uint32_t v_stride = (uint32_t)(nbv1 / ggml_type_size(v->type));
  6070. uint32_t alignment = fa_align(path, HSK, HSV, k->type, small_rows);
  6071. bool aligned = (KV % alignment) == 0 &&
  6072. // the "aligned" shader variant will forcibly align strides, for performance
  6073. (q_stride & 7) == 0 && (k_stride & 7) == 0 && (v_stride & 7) == 0;
  6074. // Need to use the coopmat2 variant that clamps loads when HSK/HSV aren't sufficiently aligned.
  6075. if (((HSK | HSV) % 16) != 0 && path == FA_COOPMAT2) {
  6076. aligned = false;
  6077. }
  6078. // mask dim1 is padded to 64, we rely on this to avoid clamping mask loads
  6079. GGML_ASSERT((nem1 % GGML_KQ_MASK_PAD) == 0);
  6080. bool f32acc = path == FA_SCALAR || dst->op_params[3] == GGML_PREC_F32;
  6081. vk_fa_pipeline_state fa_pipeline_state(HSK, HSV, small_rows, path, aligned, f32acc);
  6082. vk_pipeline pipeline = nullptr;
  6083. auto &pipelines = ctx->device->pipeline_flash_attn_f32_f16[k->type];
  6084. auto it = pipelines.find(fa_pipeline_state);
  6085. if (it != pipelines.end()) {
  6086. pipeline = it->second;
  6087. } else {
  6088. pipelines[fa_pipeline_state] = pipeline = std::make_shared<vk_pipeline_struct>();
  6089. }
  6090. assert(pipeline);
  6091. uint32_t split_kv = KV;
  6092. uint32_t split_k = 1;
  6093. // Use a placeholder core count if one isn't available. split_k is a big help for perf.
  6094. const uint32_t shader_core_count = ctx->device->shader_core_count ? ctx->device->shader_core_count : 16;
  6095. // Try to use split_k when KV is large enough to be worth the overhead
  6096. if (workgroups_x == 1 && shader_core_count > 0) {
  6097. // Try to run two workgroups per SM.
  6098. split_k = shader_core_count * 2 / (workgroups_y * workgroups_z);
  6099. if (split_k > 1) {
  6100. // Try to evenly split KV into split_k chunks, but it needs to be a multiple
  6101. // of "align", so recompute split_k based on that.
  6102. split_kv = ROUNDUP_POW2(std::max(1u, KV / split_k), alignment);
  6103. split_k = CEIL_DIV(KV, split_kv);
  6104. workgroups_x = split_k;
  6105. }
  6106. }
  6107. // Reserve space for split_k temporaries. For each split x batch, we need to store the O matrix (D x ne1)
  6108. // and the per-row m and L values (ne1 rows). We store all the matrices first, followed by the rows.
  6109. const uint64_t split_k_size = split_k > 1 ? (HSV * ne1 * sizeof(float) + ne1 * sizeof(float) * 2) * split_k * ne3 : 0;
  6110. if (split_k_size > ctx->device->max_memory_allocation_size) {
  6111. GGML_ABORT("Requested preallocation size is too large");
  6112. }
  6113. if (ctx->prealloc_size_split_k < split_k_size) {
  6114. ctx->prealloc_size_split_k = split_k_size;
  6115. }
  6116. if (dryrun) {
  6117. // Request descriptor sets
  6118. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  6119. if (split_k > 1) {
  6120. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_flash_attn_split_k_reduce, 1);
  6121. }
  6122. return;
  6123. }
  6124. float scale = 1.0f;
  6125. float max_bias = 0.0f;
  6126. float logit_softcap = 0.0f;
  6127. memcpy(&scale, (const float *) dst->op_params + 0, sizeof(float));
  6128. memcpy(&max_bias, (const float *) dst->op_params + 1, sizeof(float));
  6129. memcpy(&logit_softcap, (const float *) dst->op_params + 2, sizeof(float));
  6130. if (logit_softcap != 0) {
  6131. scale /= logit_softcap;
  6132. }
  6133. const uint32_t n_head_kv = neq2;
  6134. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head_kv));
  6135. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  6136. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  6137. vk_buffer d_Q = nullptr, d_K = nullptr, d_V = nullptr, d_D = nullptr, d_M = nullptr, d_S = nullptr;
  6138. size_t q_buf_offset = 0, k_buf_offset = 0, v_buf_offset = 0, d_buf_offset = 0, m_buf_offset = 0, s_buf_offset = 0;
  6139. bool Q_uma = false, K_uma = false, V_uma = false, D_uma = false, M_uma = false, S_uma = false;
  6140. if (ctx->device->uma) {
  6141. ggml_vk_host_get(ctx->device, q->data, d_Q, q_buf_offset);
  6142. ggml_vk_host_get(ctx->device, k->data, d_K, k_buf_offset);
  6143. ggml_vk_host_get(ctx->device, v->data, d_V, v_buf_offset);
  6144. ggml_vk_host_get(ctx->device, dst->data, d_D, d_buf_offset);
  6145. Q_uma = d_Q != nullptr;
  6146. K_uma = d_K != nullptr;
  6147. V_uma = d_V != nullptr;
  6148. D_uma = d_D != nullptr;
  6149. if (mask) {
  6150. ggml_vk_host_get(ctx->device, mask->data, d_M, m_buf_offset);
  6151. M_uma = d_M != nullptr;
  6152. }
  6153. if (sinks) {
  6154. ggml_vk_host_get(ctx->device, sinks->data, d_S, s_buf_offset);
  6155. S_uma = d_S != nullptr;
  6156. }
  6157. }
  6158. ggml_backend_vk_buffer_context * d_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  6159. ggml_backend_vk_buffer_context * q_buf_ctx = (ggml_backend_vk_buffer_context *)q->buffer->context;
  6160. ggml_backend_vk_buffer_context * k_buf_ctx = (ggml_backend_vk_buffer_context *)k->buffer->context;
  6161. ggml_backend_vk_buffer_context * v_buf_ctx = (ggml_backend_vk_buffer_context *)v->buffer->context;
  6162. if (!Q_uma) {
  6163. d_Q = q_buf_ctx->dev_buffer;
  6164. q_buf_offset = vk_tensor_offset(q) + q->view_offs;
  6165. }
  6166. if (!K_uma) {
  6167. d_K = k_buf_ctx->dev_buffer;
  6168. k_buf_offset = vk_tensor_offset(k) + k->view_offs;
  6169. }
  6170. if (!V_uma) {
  6171. d_V = v_buf_ctx->dev_buffer;
  6172. v_buf_offset = vk_tensor_offset(v) + v->view_offs;
  6173. }
  6174. if (!D_uma) {
  6175. d_D = d_buf_ctx->dev_buffer;
  6176. d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  6177. }
  6178. if (!M_uma) {
  6179. d_M = d_Q;
  6180. m_buf_offset = q_buf_offset;
  6181. if (mask) {
  6182. ggml_backend_vk_buffer_context * m_buf_ctx = (ggml_backend_vk_buffer_context*)mask->buffer->context;
  6183. d_M = m_buf_ctx->dev_buffer;
  6184. m_buf_offset = vk_tensor_offset(mask) + mask->view_offs;
  6185. }
  6186. }
  6187. if (!S_uma) {
  6188. d_S = d_Q;
  6189. s_buf_offset = q_buf_offset;
  6190. if (sinks) {
  6191. ggml_backend_vk_buffer_context * s_buf_ctx = (ggml_backend_vk_buffer_context*)sinks->buffer->context;
  6192. d_S = s_buf_ctx->dev_buffer;
  6193. s_buf_offset = vk_tensor_offset(sinks) + sinks->view_offs;
  6194. }
  6195. }
  6196. uint32_t mask_n_head_log2 = ((sinks != nullptr) << 24) | ((mask != nullptr) << 16) | n_head_log2;
  6197. const vk_flash_attn_push_constants pc = { N, KV,
  6198. (uint32_t)ne1, (uint32_t)ne2, (uint32_t)ne3,
  6199. (uint32_t)neq2, (uint32_t)neq3,
  6200. (uint32_t)nek2, (uint32_t)nek3,
  6201. (uint32_t)nev2, (uint32_t)nev3,
  6202. nem1, nem2, nem3,
  6203. q_stride, (uint32_t)nbq2, (uint32_t)nbq3,
  6204. k_stride, (uint32_t)nbk2, (uint32_t)nbk3,
  6205. v_stride, (uint32_t)nbv2, (uint32_t)nbv3,
  6206. scale, max_bias, logit_softcap,
  6207. mask_n_head_log2, m0, m1,
  6208. gqa_ratio, split_kv, split_k };
  6209. if (split_k > 1) {
  6210. if (ctx->prealloc_split_k_need_sync) {
  6211. ggml_vk_sync_buffers(ctx, subctx);
  6212. }
  6213. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline,
  6214. {
  6215. vk_subbuffer{d_Q, q_buf_offset, VK_WHOLE_SIZE},
  6216. vk_subbuffer{d_K, k_buf_offset, VK_WHOLE_SIZE},
  6217. vk_subbuffer{d_V, v_buf_offset, VK_WHOLE_SIZE},
  6218. vk_subbuffer{d_M, m_buf_offset, VK_WHOLE_SIZE},
  6219. vk_subbuffer{d_S, s_buf_offset, VK_WHOLE_SIZE},
  6220. vk_subbuffer{ctx->prealloc_split_k, 0, VK_WHOLE_SIZE},
  6221. },
  6222. // We only use split_k when group query attention is enabled, which means
  6223. // there's no more than one tile of rows (i.e. workgroups_x would have been
  6224. // one). We reuse workgroups_x to mean the number of splits, so we need to
  6225. // cancel out the divide by wg_denoms[0].
  6226. pc, { workgroups_x * pipeline->wg_denoms[0], workgroups_y, workgroups_z });
  6227. ggml_vk_sync_buffers(ctx, subctx);
  6228. const std::array<uint32_t, 5> pc2 = { HSV, (uint32_t)ne1, (uint32_t)ne3, split_k, (sinks != nullptr) };
  6229. ggml_vk_dispatch_pipeline(ctx, subctx, ctx->device->pipeline_flash_attn_split_k_reduce,
  6230. {
  6231. vk_subbuffer{ctx->prealloc_split_k, 0, VK_WHOLE_SIZE},
  6232. vk_subbuffer{d_S, s_buf_offset, VK_WHOLE_SIZE},
  6233. vk_subbuffer{d_D, d_buf_offset, VK_WHOLE_SIZE},
  6234. },
  6235. pc2, { (uint32_t)ne1, HSV, (uint32_t)ne3 });
  6236. ctx->prealloc_split_k_need_sync = true;
  6237. } else {
  6238. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline,
  6239. {
  6240. vk_subbuffer{d_Q, q_buf_offset, VK_WHOLE_SIZE},
  6241. vk_subbuffer{d_K, k_buf_offset, VK_WHOLE_SIZE},
  6242. vk_subbuffer{d_V, v_buf_offset, VK_WHOLE_SIZE},
  6243. vk_subbuffer{d_M, m_buf_offset, VK_WHOLE_SIZE},
  6244. vk_subbuffer{d_S, s_buf_offset, VK_WHOLE_SIZE},
  6245. vk_subbuffer{d_D, d_buf_offset, VK_WHOLE_SIZE},
  6246. },
  6247. pc, { workgroups_x, workgroups_y, workgroups_z });
  6248. }
  6249. }
  6250. static std::array<uint32_t, 3> ggml_vk_get_conv_elements(const ggml_tensor *dst) {
  6251. const ggml_tensor *src0 = dst->src[0];
  6252. const ggml_tensor *src1 = dst->src[1];
  6253. // src0 - kernel: [KW, KH, Cin, Cout]
  6254. // src1 - input: [W, H, Cin, N]
  6255. // dst - result: [OW, OH, Cout, N]
  6256. // Copied from ggml.c: int64_t ggml_calc_conv_output_size(int64_t ins, int64_t ks, int s, int p, int d)
  6257. auto calc_conv_output_size = [](int64_t ins, int64_t ks, int s, int p, int d) -> int64_t {
  6258. return (ins + 2 * p - d * (ks - 1) - 1) / s + 1;
  6259. };
  6260. // parallelize in {OW/BS_K, OH/BS_NPQ, 1}
  6261. int64_t W = src1->ne[0];
  6262. int64_t H = src1->ne[1];
  6263. int64_t KW = src0->ne[0];
  6264. int64_t KH = src0->ne[1];
  6265. int64_t Cout = src0->ne[3];
  6266. int64_t N = src1->ne[3];
  6267. int64_t OH = calc_conv_output_size(H, KH, dst->op_params[1], dst->op_params[3], dst->op_params[5]);
  6268. int64_t OW = calc_conv_output_size(W, KW, dst->op_params[0], dst->op_params[2], dst->op_params[4]);
  6269. int64_t NPQ = N * OW * OH;
  6270. // Tile output matrix to (K/NB_K, NPQ/NB_NPQ, 1) workgroups
  6271. std::array<uint32_t, 3> elements = { static_cast<uint32_t>(Cout), static_cast<uint32_t>(NPQ), 1 };
  6272. return elements;
  6273. }
  6274. static vk_pipeline ggml_vk_op_get_pipeline(ggml_backend_vk_context * ctx, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, const ggml_tensor * dst, ggml_op op) {
  6275. switch (op) {
  6276. case GGML_OP_GET_ROWS:
  6277. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  6278. if (dst->type == GGML_TYPE_F16) {
  6279. return ctx->device->pipeline_get_rows[src0->type];
  6280. }
  6281. if (dst->type == GGML_TYPE_F32) {
  6282. return ctx->device->pipeline_get_rows_f32[src0->type];
  6283. }
  6284. return nullptr;
  6285. case GGML_OP_ACC:
  6286. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6287. return ctx->device->pipeline_acc_f32;
  6288. }
  6289. return nullptr;
  6290. case GGML_OP_ADD:
  6291. case GGML_OP_SUB:
  6292. case GGML_OP_MUL:
  6293. case GGML_OP_DIV:
  6294. if ((src0->type != GGML_TYPE_F32 && src0->type != GGML_TYPE_F16) ||
  6295. (src1->type != GGML_TYPE_F32 && src1->type != GGML_TYPE_F16) ||
  6296. (dst->type != GGML_TYPE_F32 && dst->type != GGML_TYPE_F16)) {
  6297. return nullptr;
  6298. }
  6299. switch (op) {
  6300. case GGML_OP_ADD:
  6301. {
  6302. if (ctx->num_additional_fused_ops > 0) {
  6303. if (ctx->do_add_rms_partials) {
  6304. return ctx->device->pipeline_multi_add_rms[ctx->num_additional_fused_ops];
  6305. } else {
  6306. return ctx->device->pipeline_multi_add[ctx->num_additional_fused_ops];
  6307. }
  6308. }
  6309. if (ctx->do_add_rms_partials) {
  6310. auto pipelines = ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_add_rms_norepeat : ctx->device->pipeline_add_rms;
  6311. return pipelines[src0->type == GGML_TYPE_F16][src1->type == GGML_TYPE_F16][dst->type == GGML_TYPE_F16];
  6312. } else {
  6313. auto pipelines = ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_add_norepeat : ctx->device->pipeline_add;
  6314. return pipelines[src0->type == GGML_TYPE_F16][src1->type == GGML_TYPE_F16][dst->type == GGML_TYPE_F16];
  6315. }
  6316. }
  6317. case GGML_OP_SUB:
  6318. {
  6319. auto pipelines = ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_sub_norepeat : ctx->device->pipeline_sub;
  6320. return pipelines[src0->type == GGML_TYPE_F16][src1->type == GGML_TYPE_F16][dst->type == GGML_TYPE_F16];
  6321. }
  6322. case GGML_OP_MUL:
  6323. {
  6324. auto pipelines = ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_mul_norepeat : ctx->device->pipeline_mul;
  6325. return pipelines[src0->type == GGML_TYPE_F16][src1->type == GGML_TYPE_F16][dst->type == GGML_TYPE_F16];
  6326. }
  6327. case GGML_OP_DIV:
  6328. {
  6329. auto pipelines = ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_div_norepeat : ctx->device->pipeline_div;
  6330. return pipelines[src0->type == GGML_TYPE_F16][src1->type == GGML_TYPE_F16][dst->type == GGML_TYPE_F16];
  6331. }
  6332. default:
  6333. break;
  6334. }
  6335. return nullptr;
  6336. case GGML_OP_ADD_ID:
  6337. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && src2->type == GGML_TYPE_I32 && dst->type == GGML_TYPE_F32) {
  6338. return ctx->device->pipeline_add_id_f32;
  6339. }
  6340. return nullptr;
  6341. case GGML_OP_CONCAT:
  6342. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6343. return ctx->device->pipeline_concat_f32;
  6344. }
  6345. if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  6346. return ctx->device->pipeline_concat_f16;
  6347. }
  6348. if (src0->type == GGML_TYPE_I32 && src1->type == GGML_TYPE_I32 && dst->type == GGML_TYPE_I32) {
  6349. return ctx->device->pipeline_concat_i32;
  6350. }
  6351. return nullptr;
  6352. case GGML_OP_UPSCALE:
  6353. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6354. int mode = ggml_get_op_params_i32(dst, 0);
  6355. switch (mode) {
  6356. case GGML_SCALE_MODE_NEAREST:
  6357. return ctx->device->pipeline_upscale_nearest_f32;
  6358. case GGML_SCALE_MODE_BILINEAR:
  6359. return ctx->device->pipeline_upscale_bilinear_f32;
  6360. case GGML_SCALE_MODE_BILINEAR | GGML_SCALE_FLAG_ALIGN_CORNERS:
  6361. return ctx->device->pipeline_upscale_bilinear_ac_f32;
  6362. }
  6363. }
  6364. return nullptr;
  6365. case GGML_OP_SCALE:
  6366. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6367. return ctx->device->pipeline_scale_f32;
  6368. }
  6369. return nullptr;
  6370. case GGML_OP_SQR:
  6371. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6372. return ctx->device->pipeline_sqr_f32;
  6373. }
  6374. return nullptr;
  6375. case GGML_OP_SQRT:
  6376. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6377. return ctx->device->pipeline_sqrt_f32;
  6378. }
  6379. return nullptr;
  6380. case GGML_OP_SIN:
  6381. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6382. return ctx->device->pipeline_sin_f32;
  6383. }
  6384. return nullptr;
  6385. case GGML_OP_COS:
  6386. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6387. return ctx->device->pipeline_cos_f32;
  6388. }
  6389. return nullptr;
  6390. case GGML_OP_CLAMP:
  6391. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6392. return ctx->device->pipeline_clamp_f32;
  6393. }
  6394. return nullptr;
  6395. case GGML_OP_PAD:
  6396. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6397. return ctx->device->pipeline_pad_f32;
  6398. }
  6399. return nullptr;
  6400. case GGML_OP_ROLL:
  6401. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6402. return ctx->device->pipeline_roll_f32;
  6403. }
  6404. return nullptr;
  6405. case GGML_OP_REPEAT:
  6406. if (ggml_type_size(src0->type) == sizeof(float) && ggml_type_size(dst->type) == sizeof(float)) {
  6407. return ctx->device->pipeline_repeat_f32;
  6408. }
  6409. return nullptr;
  6410. case GGML_OP_REPEAT_BACK:
  6411. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6412. return ctx->device->pipeline_repeat_back_f32;
  6413. }
  6414. return nullptr;
  6415. case GGML_OP_CPY:
  6416. case GGML_OP_CONT:
  6417. case GGML_OP_DUP:
  6418. return ggml_vk_get_cpy_pipeline(ctx, src0, dst, dst->type);
  6419. case GGML_OP_SET_ROWS:
  6420. return ctx->device->pipeline_set_rows[dst->type];
  6421. case GGML_OP_SILU_BACK:
  6422. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6423. return ctx->device->pipeline_silu_back_f32;
  6424. }
  6425. return nullptr;
  6426. case GGML_OP_NORM:
  6427. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6428. return ctx->device->pipeline_norm_f32;
  6429. }
  6430. return nullptr;
  6431. case GGML_OP_GROUP_NORM:
  6432. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6433. return ctx->device->pipeline_group_norm_f32;
  6434. }
  6435. return nullptr;
  6436. case GGML_OP_RMS_NORM:
  6437. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6438. if (ctx->do_add_rms_partials) {
  6439. return ctx->num_additional_fused_ops > 0 ? ctx->device->pipeline_rms_norm_mul_partials_f32 : ctx->device->pipeline_rms_norm_partials_f32;
  6440. } else {
  6441. return ctx->num_additional_fused_ops > 0 ? ctx->device->pipeline_rms_norm_mul_f32 : ctx->device->pipeline_rms_norm_f32;
  6442. }
  6443. }
  6444. return nullptr;
  6445. case GGML_OP_RMS_NORM_BACK:
  6446. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6447. return ctx->device->pipeline_rms_norm_back_f32;
  6448. }
  6449. return nullptr;
  6450. case GGML_OP_L2_NORM:
  6451. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6452. return ctx->device->pipeline_l2_norm_f32;
  6453. }
  6454. return nullptr;
  6455. case GGML_OP_UNARY:
  6456. if ((src0->type != GGML_TYPE_F32 && src0->type != GGML_TYPE_F16) ||
  6457. (dst->type != GGML_TYPE_F32 && dst->type != GGML_TYPE_F16) ||
  6458. (src0->type != dst->type)) {
  6459. return nullptr;
  6460. }
  6461. switch (ggml_get_unary_op(dst)) {
  6462. case GGML_UNARY_OP_EXP:
  6463. return ctx->device->pipeline_exp[dst->type == GGML_TYPE_F16];
  6464. case GGML_UNARY_OP_SILU:
  6465. return ctx->device->pipeline_silu[dst->type == GGML_TYPE_F16];
  6466. case GGML_UNARY_OP_GELU:
  6467. return ctx->device->pipeline_gelu[dst->type == GGML_TYPE_F16];
  6468. case GGML_UNARY_OP_GELU_ERF:
  6469. return ctx->device->pipeline_gelu_erf[dst->type == GGML_TYPE_F16];
  6470. case GGML_UNARY_OP_GELU_QUICK:
  6471. return ctx->device->pipeline_gelu_quick[dst->type == GGML_TYPE_F16];
  6472. case GGML_UNARY_OP_RELU:
  6473. return ctx->device->pipeline_relu[dst->type == GGML_TYPE_F16];
  6474. case GGML_UNARY_OP_TANH:
  6475. return ctx->device->pipeline_tanh[dst->type == GGML_TYPE_F16];
  6476. case GGML_UNARY_OP_SIGMOID:
  6477. return ctx->device->pipeline_sigmoid[dst->type == GGML_TYPE_F16];
  6478. case GGML_UNARY_OP_HARDSIGMOID:
  6479. return ctx->device->pipeline_hardsigmoid[dst->type == GGML_TYPE_F16];
  6480. case GGML_UNARY_OP_HARDSWISH:
  6481. return ctx->device->pipeline_hardswish[dst->type == GGML_TYPE_F16];
  6482. default:
  6483. break;
  6484. }
  6485. return nullptr;
  6486. case GGML_OP_GLU:
  6487. if ((src0->type != GGML_TYPE_F32 && src0->type != GGML_TYPE_F16) ||
  6488. (dst->type != GGML_TYPE_F32 && dst->type != GGML_TYPE_F16) ||
  6489. (src0->type != dst->type)) {
  6490. return nullptr;
  6491. }
  6492. switch (ggml_get_glu_op(dst)) {
  6493. case GGML_GLU_OP_GEGLU:
  6494. return ctx->device->pipeline_geglu[dst->type == GGML_TYPE_F16];
  6495. case GGML_GLU_OP_REGLU:
  6496. return ctx->device->pipeline_reglu[dst->type == GGML_TYPE_F16];
  6497. case GGML_GLU_OP_SWIGLU:
  6498. return ctx->device->pipeline_swiglu[dst->type == GGML_TYPE_F16];
  6499. case GGML_GLU_OP_SWIGLU_OAI:
  6500. return ctx->device->pipeline_swiglu_oai[dst->type == GGML_TYPE_F16];
  6501. case GGML_GLU_OP_GEGLU_ERF:
  6502. return ctx->device->pipeline_geglu_erf[dst->type == GGML_TYPE_F16];
  6503. case GGML_GLU_OP_GEGLU_QUICK:
  6504. return ctx->device->pipeline_geglu_quick[dst->type == GGML_TYPE_F16];
  6505. default:
  6506. break;
  6507. }
  6508. return nullptr;
  6509. case GGML_OP_DIAG_MASK_INF:
  6510. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6511. return ctx->device->pipeline_diag_mask_inf_f32;
  6512. }
  6513. return nullptr;
  6514. case GGML_OP_SOFT_MAX:
  6515. GGML_ASSERT(!src1 || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16);
  6516. GGML_ASSERT(!src2 || src2->type == GGML_TYPE_F32);
  6517. if (src0->type == GGML_TYPE_F32 && (src1 == nullptr || src1->type == GGML_TYPE_F32) && dst->type == GGML_TYPE_F32) {
  6518. return src0->ne[0] > 1024 ? ctx->device->pipeline_soft_max_f32_wg512 : ctx->device->pipeline_soft_max_f32;
  6519. }
  6520. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  6521. return src0->ne[0] > 1024 ? ctx->device->pipeline_soft_max_f32_f16_wg512 : ctx->device->pipeline_soft_max_f32_f16;
  6522. }
  6523. return nullptr;
  6524. case GGML_OP_SOFT_MAX_BACK:
  6525. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6526. return ctx->device->pipeline_soft_max_back_f32;
  6527. }
  6528. return nullptr;
  6529. case GGML_OP_ROPE:
  6530. case GGML_OP_ROPE_BACK:
  6531. {
  6532. const int mode = ((const int32_t *) dst->op_params)[2];
  6533. const bool is_neox = mode & GGML_ROPE_TYPE_NEOX;
  6534. const bool is_mrope = mode & GGML_ROPE_TYPE_MROPE;
  6535. const bool is_vision = mode == GGML_ROPE_TYPE_VISION;
  6536. if (is_neox) {
  6537. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6538. return ctx->device->pipeline_rope_neox_f32;
  6539. }
  6540. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  6541. return ctx->device->pipeline_rope_neox_f16;
  6542. }
  6543. } else if (is_mrope && !is_vision) {
  6544. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6545. return ctx->device->pipeline_rope_multi_f32;
  6546. }
  6547. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  6548. return ctx->device->pipeline_rope_multi_f16;
  6549. }
  6550. } else if (is_vision) {
  6551. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6552. return ctx->device->pipeline_rope_vision_f32;
  6553. }
  6554. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  6555. return ctx->device->pipeline_rope_vision_f16;
  6556. }
  6557. } else {
  6558. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6559. return ctx->device->pipeline_rope_norm_f32;
  6560. }
  6561. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  6562. return ctx->device->pipeline_rope_norm_f16;
  6563. }
  6564. }
  6565. return nullptr;
  6566. }
  6567. case GGML_OP_ARGSORT:
  6568. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_I32) {
  6569. uint32_t idx = (uint32_t)ceilf(log2f(float(dst->ne[0])));
  6570. return ctx->device->pipeline_argsort_f32[idx];
  6571. }
  6572. return nullptr;
  6573. case GGML_OP_SUM:
  6574. case GGML_OP_SUM_ROWS:
  6575. case GGML_OP_MEAN:
  6576. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6577. return ctx->device->pipeline_sum_rows_f32;
  6578. }
  6579. return nullptr;
  6580. case GGML_OP_ARGMAX:
  6581. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_I32) {
  6582. return ctx->device->pipeline_argmax_f32;
  6583. }
  6584. return nullptr;
  6585. case GGML_OP_COUNT_EQUAL:
  6586. if (src0->type == GGML_TYPE_I32 && src1->type == GGML_TYPE_I32 && dst->type == GGML_TYPE_I64) {
  6587. return ctx->device->pipeline_count_equal_i32;
  6588. }
  6589. return nullptr;
  6590. case GGML_OP_IM2COL:
  6591. if (src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6592. return ctx->device->pipeline_im2col_f32;
  6593. }
  6594. if (src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F16) {
  6595. return ctx->device->pipeline_im2col_f32_f16;
  6596. }
  6597. return nullptr;
  6598. case GGML_OP_TIMESTEP_EMBEDDING:
  6599. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6600. return ctx->device->pipeline_timestep_embedding_f32;
  6601. }
  6602. return nullptr;
  6603. case GGML_OP_CONV_TRANSPOSE_1D:
  6604. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6605. return ctx->device->pipeline_conv_transpose_1d_f32;
  6606. }
  6607. return nullptr;
  6608. case GGML_OP_POOL_2D:
  6609. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6610. return ctx->device->pipeline_pool2d_f32;
  6611. }
  6612. return nullptr;
  6613. case GGML_OP_RWKV_WKV6:
  6614. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6615. return ctx->device->pipeline_rwkv_wkv6_f32;
  6616. }
  6617. return nullptr;
  6618. case GGML_OP_RWKV_WKV7:
  6619. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6620. return ctx->device->pipeline_rwkv_wkv7_f32;
  6621. }
  6622. return nullptr;
  6623. case GGML_OP_OPT_STEP_ADAMW:
  6624. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6625. return ctx->device->pipeline_opt_step_adamw_f32;
  6626. }
  6627. return nullptr;
  6628. case GGML_OP_OPT_STEP_SGD:
  6629. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6630. return ctx->device->pipeline_opt_step_sgd_f32;
  6631. }
  6632. return nullptr;
  6633. case GGML_OP_LEAKY_RELU:
  6634. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6635. return ctx->device->pipeline_leaky_relu_f32;
  6636. }
  6637. return nullptr;
  6638. case GGML_OP_CONV_2D:
  6639. if (src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32 &&
  6640. ggml_is_contiguous(src0) && ggml_is_contiguous(src1) && ggml_is_contiguous(dst)) {
  6641. auto elements = ggml_vk_get_conv_elements(dst);
  6642. vk_conv_shapes shape;
  6643. uint32_t tiles[CONV_SHAPE_COUNT];
  6644. for (uint32_t i = 0; i < CONV_SHAPE_COUNT; ++i) {
  6645. tiles[i] = CEIL_DIV(elements[0], ctx->device->pipeline_conv2d_f32[i]->wg_denoms[0]) * CEIL_DIV(elements[1], ctx->device->pipeline_conv2d_f32[i]->wg_denoms[1]);
  6646. }
  6647. // We can't query number of shader cores on Intel, use 32 as a placeholder
  6648. // so small convolutions will still choose a smaller tile.
  6649. const uint32_t shader_core_count = ctx->device->shader_core_count > 0 ? ctx->device->shader_core_count : 32;
  6650. if (elements[0] > 64 && tiles[CONV_SHAPE_128x128] >= shader_core_count * 2) {
  6651. shape = CONV_SHAPE_128x128;
  6652. } else if (elements[0] <= 32 && tiles[CONV_SHAPE_32x256] >= shader_core_count * 2) {
  6653. shape = CONV_SHAPE_32x256;
  6654. } else {
  6655. shape = CONV_SHAPE_64x32;
  6656. }
  6657. if (src0->type == GGML_TYPE_F32) {
  6658. return ctx->device->pipeline_conv2d_f32[shape];
  6659. } else if (src0->type == GGML_TYPE_F16) {
  6660. return ctx->device->pipeline_conv2d_f16_f32[shape];
  6661. }
  6662. }
  6663. return nullptr;
  6664. case GGML_OP_CONV_2D_DW:
  6665. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6666. if (ggml_is_contiguous(src1)) {
  6667. return ctx->device->pipeline_conv2d_dw_whcn_f32;
  6668. } else if (ggml_is_contiguous_channels(src1)) {
  6669. return ctx->device->pipeline_conv2d_dw_cwhn_f32;
  6670. }
  6671. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  6672. if (ggml_is_contiguous(src1)) {
  6673. return ctx->device->pipeline_conv2d_dw_whcn_f16_f32;
  6674. } else if (ggml_is_contiguous_channels(src1)) {
  6675. return ctx->device->pipeline_conv2d_dw_cwhn_f16_f32;
  6676. }
  6677. }
  6678. return nullptr;
  6679. default:
  6680. return nullptr;
  6681. }
  6682. GGML_UNUSED(src2);
  6683. }
  6684. static bool ggml_vk_op_supports_incontiguous(ggml_op op) {
  6685. switch (op) {
  6686. case GGML_OP_CPY:
  6687. case GGML_OP_GET_ROWS:
  6688. case GGML_OP_ADD:
  6689. case GGML_OP_SUB:
  6690. case GGML_OP_MUL:
  6691. case GGML_OP_DIV:
  6692. case GGML_OP_ADD_ID:
  6693. case GGML_OP_CONCAT:
  6694. case GGML_OP_UPSCALE:
  6695. case GGML_OP_SQR:
  6696. case GGML_OP_SQRT:
  6697. case GGML_OP_SIN:
  6698. case GGML_OP_COS:
  6699. case GGML_OP_CLAMP:
  6700. case GGML_OP_PAD:
  6701. case GGML_OP_REPEAT:
  6702. case GGML_OP_REPEAT_BACK:
  6703. case GGML_OP_ROPE:
  6704. case GGML_OP_RMS_NORM:
  6705. case GGML_OP_CONV_2D_DW:
  6706. case GGML_OP_IM2COL:
  6707. case GGML_OP_SET_ROWS:
  6708. case GGML_OP_SUM:
  6709. case GGML_OP_SUM_ROWS:
  6710. case GGML_OP_MEAN:
  6711. return true;
  6712. default:
  6713. return false;
  6714. }
  6715. }
  6716. static uint32_t get_misalign_bytes(ggml_backend_vk_context * ctx, const ggml_tensor * t)
  6717. {
  6718. return ((vk_tensor_offset(t) + t->view_offs) & (ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1));;
  6719. }
  6720. template <typename T> void init_pushconst_tensor_offsets(ggml_backend_vk_context * ctx, T &p, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, ggml_tensor * dst) {
  6721. GGML_UNUSED(p);
  6722. GGML_UNUSED(src0);
  6723. GGML_UNUSED(src1);
  6724. GGML_UNUSED(src2);
  6725. GGML_UNUSED(dst);
  6726. static_assert(!std::is_const<T>::value, "unexpected type");
  6727. GGML_ASSERT(!src0 || get_misalign_bytes(ctx, src0) == 0);
  6728. GGML_ASSERT(!src1 || get_misalign_bytes(ctx, src1) == 0);
  6729. GGML_ASSERT(!src2 || get_misalign_bytes(ctx, src2) == 0);
  6730. GGML_ASSERT(!dst || get_misalign_bytes(ctx, dst) == 0);
  6731. }
  6732. template <> void init_pushconst_tensor_offsets(ggml_backend_vk_context * ctx, vk_op_unary_push_constants &p, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, ggml_tensor * dst) {
  6733. const uint32_t a_offset = get_misalign_bytes(ctx, src0) / ggml_type_size(src0->type);
  6734. const uint32_t d_offset = get_misalign_bytes(ctx, dst) / ggml_type_size(dst->type);
  6735. p.misalign_offsets = (a_offset << 16) | d_offset;
  6736. GGML_UNUSED(src1);
  6737. GGML_UNUSED(src2);
  6738. }
  6739. template <> void init_pushconst_tensor_offsets(ggml_backend_vk_context * ctx, vk_op_sum_rows_push_constants &p, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, ggml_tensor * dst) {
  6740. const uint32_t a_offset = get_misalign_bytes(ctx, src0) / ggml_type_size(src0->type);
  6741. const uint32_t d_offset = get_misalign_bytes(ctx, dst) / ggml_type_size(dst->type);
  6742. p.misalign_offsets = (a_offset << 16) | d_offset;
  6743. GGML_UNUSED(src1);
  6744. GGML_UNUSED(src2);
  6745. }
  6746. template <> void init_pushconst_tensor_offsets(ggml_backend_vk_context * ctx, vk_op_binary_push_constants &p, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, ggml_tensor * dst) {
  6747. const uint32_t a_offset = get_misalign_bytes(ctx, src0) / ggml_type_size(src0->type);
  6748. const uint32_t b_offset = get_misalign_bytes(ctx, src1) / ggml_type_size(src1->type);
  6749. const uint32_t d_offset = get_misalign_bytes(ctx, dst) / ggml_type_size(dst->type);
  6750. GGML_ASSERT(dst->op != GGML_OP_GET_ROWS || (a_offset == 0 && b_offset == 0 && d_offset == 0));
  6751. p.misalign_offsets = (a_offset << 16) | (b_offset << 8) | d_offset;
  6752. GGML_UNUSED(src2);
  6753. }
  6754. template <> void init_pushconst_tensor_offsets(ggml_backend_vk_context * ctx, vk_op_upscale_push_constants &p, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, ggml_tensor * dst) {
  6755. const uint32_t a_offset = get_misalign_bytes(ctx, src0) / ggml_type_size(src0->type);
  6756. const uint32_t d_offset = get_misalign_bytes(ctx, dst) / ggml_type_size(dst->type);
  6757. p.a_offset = a_offset;
  6758. p.d_offset = d_offset;
  6759. GGML_UNUSED(src1);
  6760. GGML_UNUSED(src2);
  6761. }
  6762. template<typename PC>
  6763. static void ggml_vk_op_f32(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, ggml_tensor * dst, ggml_op op, PC&& pc, bool dryrun = false) {
  6764. VK_LOG_DEBUG("ggml_vk_op_f32((" << src0 << ", name=" << src0->name << ", type=" << src0->type << ", ne0=" << src0->ne[0] << ", ne1=" << src0->ne[1] << ", ne2=" << src0->ne[2] << ", ne3=" << src0->ne[3] << ", nb0=" << src0->nb[0] << ", nb1=" << src0->nb[1] << ", nb2=" << src0->nb[2] << ", nb3=" << src0->nb[3];
  6765. if (src1 != nullptr) {
  6766. std::cerr << "), (" << src1 << ", name=" << src1->name << ", type=" << src1->type << ", ne0=" << src1->ne[0] << ", ne1=" << src1->ne[1] << ", ne2=" << src1->ne[2] << ", ne3=" << src1->ne[3] << ", nb0=" << src1->nb[0] << ", nb1=" << src1->nb[1] << ", nb2=" << src1->nb[2] << ", nb3=" << src1->nb[3];
  6767. }
  6768. if (src2 != nullptr) {
  6769. std::cerr << "), (" << src2 << ", name=" << src2->name << ", type=" << src2->type << ", ne0=" << src2->ne[0] << ", ne1=" << src2->ne[1] << ", ne2=" << src2->ne[2] << ", ne3=" << src2->ne[3] << ", nb0=" << src2->nb[0] << ", nb1=" << src2->nb[1] << ", nb2=" << src2->nb[2] << ", nb3=" << src2->nb[3];
  6770. }
  6771. std::cerr << "), (" << dst << ", name=" << dst->name << ", type=" << dst->type << ", ne0=" << dst->ne[0] << ", ne1=" << dst->ne[1] << ", ne2=" << dst->ne[2] << ", ne3=" << dst->ne[3] << ", nb0=" << dst->nb[0] << ", nb1=" << dst->nb[1] << ", nb2=" << dst->nb[2] << ", nb3=" << dst->nb[3];
  6772. std::cerr << "), " << ggml_op_name(op) << ", " << (dryrun ? "dryrun" : "") << ")");
  6773. GGML_ASSERT(op == GGML_OP_GET_ROWS || op == GGML_OP_CPY || (!ggml_is_quantized(src0->type) && (src1 == nullptr || !ggml_is_quantized(src1->type)))); // NOLINT
  6774. GGML_ASSERT(ggml_vk_op_supports_incontiguous(op) || ggml_vk_dim01_contiguous(src0)); // NOLINT
  6775. GGML_ASSERT(dst->buffer != nullptr);
  6776. const uint64_t ne00 = src0->ne[0];
  6777. const uint64_t ne01 = src0->ne[1];
  6778. const uint64_t ne02 = src0->ne[2];
  6779. const uint64_t ne03 = src0->ne[3];
  6780. const uint64_t ne0 = ne00 * ne01;
  6781. const bool use_src1 = src1 != nullptr;
  6782. const uint64_t ne10 = use_src1 ? src1->ne[0] : 0;
  6783. const uint64_t ne11 = use_src1 ? src1->ne[1] : 0;
  6784. const uint64_t ne12 = use_src1 ? src1->ne[2] : 0;
  6785. const uint64_t ne13 = use_src1 ? src1->ne[3] : 0;
  6786. const uint64_t ne1 = ne10 * ne11;
  6787. // const uint64_t nb10 = use_src1 ? src1->nb[0] : 0;
  6788. const bool use_src2 = src2 != nullptr;
  6789. const uint64_t ne20 = use_src2 ? src2->ne[0] : 0;
  6790. const uint64_t ne21 = use_src2 ? src2->ne[1] : 0;
  6791. const uint64_t ne22 = use_src2 ? src2->ne[2] : 0;
  6792. const uint64_t ne23 = use_src2 ? src2->ne[3] : 0;
  6793. const uint64_t ne2 = ne20 * ne21;
  6794. const uint64_t ned0 = dst->ne[0];
  6795. const uint64_t ned1 = dst->ne[1];
  6796. const uint64_t ned2 = dst->ne[2];
  6797. const uint64_t ned3 = dst->ne[3];
  6798. const uint64_t ned = ned0 * ned1;
  6799. init_pushconst_fastdiv(pc);
  6800. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, src0, src1, src2, dst, op);
  6801. if (pipeline == nullptr) {
  6802. std::cerr << "ggml_vulkan: Error: Missing op: " << ggml_op_name(op) << " for " << ggml_type_name(src0->type);
  6803. if (src1 != nullptr) {
  6804. std::cerr << " and " << ggml_type_name(src1->type);
  6805. }
  6806. std::cerr << " to " << ggml_type_name(dst->type) << std::endl;
  6807. GGML_ABORT("fatal error");
  6808. }
  6809. if (dryrun) {
  6810. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  6811. return;
  6812. }
  6813. const bool op_supports_incontiguous = ggml_vk_op_supports_incontiguous(op);
  6814. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  6815. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  6816. ggml_backend_vk_buffer_context * src1_buf_ctx = use_src1 ? (ggml_backend_vk_buffer_context *)src1->buffer->context : nullptr;
  6817. ggml_backend_vk_buffer_context * src2_buf_ctx = use_src2 ? (ggml_backend_vk_buffer_context *)src2->buffer->context : nullptr;
  6818. vk_buffer d_X = nullptr;
  6819. size_t x_buf_offset = 0;
  6820. vk_buffer d_Y = nullptr;
  6821. size_t y_buf_offset = 0;
  6822. vk_buffer d_Z = nullptr;
  6823. size_t z_buf_offset = 0;
  6824. bool src0_uma = false;
  6825. bool src1_uma = false;
  6826. bool src2_uma = false;
  6827. if (ctx->device->uma) {
  6828. ggml_vk_host_get(ctx->device, src0->data, d_X, x_buf_offset);
  6829. src0_uma = d_X != nullptr;
  6830. if (use_src1) {
  6831. ggml_vk_host_get(ctx->device, src1->data, d_Y, y_buf_offset);
  6832. src1_uma = d_Y != nullptr;
  6833. }
  6834. if (use_src2) {
  6835. ggml_vk_host_get(ctx->device, src2->data, d_Z, z_buf_offset);
  6836. src2_uma = d_Z != nullptr;
  6837. }
  6838. }
  6839. uint64_t x_sz = ggml_type_size(src0->type)/ggml_blck_size(src0->type) * ne0;
  6840. uint64_t y_sz = use_src1 ? ggml_type_size(src1->type) * ne1 : 0;
  6841. uint64_t z_sz = use_src2 ? ggml_type_size(src2->type) * ne2 : 0;
  6842. uint64_t d_sz = ggml_type_size(dst->type) * ned;
  6843. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  6844. // Workaround for tiny tensor inputs on ROPE
  6845. if (op == GGML_OP_ROPE && use_src1 && y_sz > d_D->size) {
  6846. y_sz = VK_WHOLE_SIZE;
  6847. }
  6848. GGML_ASSERT(d_D != nullptr);
  6849. uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  6850. if(!src0_uma) {
  6851. d_X = src0_buf_ctx->dev_buffer;
  6852. x_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  6853. GGML_ASSERT(d_X != nullptr);
  6854. }
  6855. if (use_src1 && !src1_uma) {
  6856. d_Y = src1_buf_ctx->dev_buffer;
  6857. y_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  6858. GGML_ASSERT(d_Y != nullptr);
  6859. }
  6860. if (use_src2 && !src2_uma) {
  6861. d_Z = src2_buf_ctx->dev_buffer;
  6862. z_buf_offset = vk_tensor_offset(src2) + src2->view_offs;
  6863. GGML_ASSERT(d_Z != nullptr);
  6864. }
  6865. // Compute misalignment offset for descriptors and store it in in push constants, then align the descriptor offsets.
  6866. init_pushconst_tensor_offsets(ctx, pc, src0, src1, src2, dst);
  6867. x_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  6868. y_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  6869. z_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  6870. d_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  6871. if (op_supports_incontiguous) {
  6872. x_sz = ggml_nbytes(src0) + get_misalign_bytes(ctx, src0);
  6873. y_sz = use_src1 ? ggml_nbytes(src1) + get_misalign_bytes(ctx, src1) : 0;
  6874. z_sz = use_src2 ? ggml_nbytes(src2) + get_misalign_bytes(ctx, src2) : 0;
  6875. d_sz = ggml_nbytes(dst) + get_misalign_bytes(ctx, dst);
  6876. if (x_buf_offset + x_sz >= d_X->size) {
  6877. x_sz = VK_WHOLE_SIZE;
  6878. }
  6879. if (use_src1 && y_buf_offset + y_sz >= d_Y->size) {
  6880. y_sz = VK_WHOLE_SIZE;
  6881. }
  6882. if (use_src2 && z_buf_offset + z_sz >= d_Z->size) {
  6883. z_sz = VK_WHOLE_SIZE;
  6884. }
  6885. if (d_buf_offset + d_sz >= d_D->size) {
  6886. d_sz = VK_WHOLE_SIZE;
  6887. }
  6888. }
  6889. std::array<uint32_t, 3> elements;
  6890. // Single call if dimension 2 is contiguous
  6891. GGML_ASSERT(op_supports_incontiguous || (ggml_is_contiguous(src0) && (src1 == nullptr || ggml_is_contiguous(src1))));
  6892. switch (op) {
  6893. case GGML_OP_NORM:
  6894. case GGML_OP_RMS_NORM_BACK:
  6895. case GGML_OP_L2_NORM:
  6896. case GGML_OP_SOFT_MAX:
  6897. case GGML_OP_SOFT_MAX_BACK:
  6898. case GGML_OP_SUM_ROWS:
  6899. case GGML_OP_MEAN:
  6900. case GGML_OP_ARGMAX:
  6901. {
  6902. const uint32_t nr = ggml_nrows(src0);
  6903. if (nr > 262144) {
  6904. elements = { 512, 512, CEIL_DIV(nr, 262144) };
  6905. } else if (nr > 512) {
  6906. elements = { 512, CEIL_DIV(nr, 512), 1 };
  6907. } else {
  6908. elements = { nr, 1, 1 };
  6909. }
  6910. } break;
  6911. case GGML_OP_RMS_NORM:
  6912. if (ctx->do_add_rms_partials) {
  6913. // Run one element per thread, 128 threads per workgroup
  6914. elements = { (uint32_t)CEIL_DIV(ne00, 128), 1, 1 };
  6915. } else {
  6916. elements = { (uint32_t)ne01, (uint32_t)ne02, (uint32_t)ne03 };
  6917. }
  6918. break;
  6919. case GGML_OP_SUM:
  6920. // We use GGML_OP_SUM_ROWS with 1 row.
  6921. elements = { 1, 1, 1 };
  6922. break;
  6923. case GGML_OP_GROUP_NORM:
  6924. {
  6925. const uint32_t num_groups = dst->op_params[0];
  6926. elements = { num_groups * (uint32_t)src0->ne[3], 1, 1 };
  6927. } break;
  6928. case GGML_OP_DIAG_MASK_INF:
  6929. case GGML_OP_ROPE:
  6930. case GGML_OP_ROPE_BACK:
  6931. elements = { (uint32_t)ggml_nrows(src0), (uint32_t)ne00, 1 };
  6932. break;
  6933. case GGML_OP_GET_ROWS:
  6934. elements = { (uint32_t)ne00, (uint32_t)ne10, (uint32_t)(ne11 * ne12) };
  6935. elements[1] = std::min(elements[1], ctx->device->properties.limits.maxComputeWorkGroupCount[1]);
  6936. elements[2] = std::min(elements[2], ctx->device->properties.limits.maxComputeWorkGroupCount[2]);
  6937. break;
  6938. case GGML_OP_ARGSORT:
  6939. elements = { (uint32_t)ne00, (uint32_t)ggml_nrows(src0), 1 };
  6940. break;
  6941. case GGML_OP_IM2COL:
  6942. {
  6943. const bool is_2D = dst->op_params[6] == 1;
  6944. const uint32_t IC = src1->ne[is_2D ? 2 : 1];
  6945. const uint32_t KH = is_2D ? src0->ne[1] : 1;
  6946. const uint32_t KW = src0->ne[0];
  6947. const uint32_t OH = is_2D ? dst->ne[2] : 1;
  6948. const uint32_t OW = dst->ne[1];
  6949. const uint32_t batch = src1->ne[is_2D ? 3 : 2];
  6950. elements = { OW * KW * KH, OH, batch * IC };
  6951. } break;
  6952. case GGML_OP_TIMESTEP_EMBEDDING:
  6953. {
  6954. const uint32_t dim = dst->op_params[0];
  6955. uint32_t half_ceil = (dim + 1) / 2;
  6956. elements = { half_ceil, (uint32_t)src0->ne[0], 1 };
  6957. } break;
  6958. case GGML_OP_CONV_TRANSPOSE_1D:
  6959. {
  6960. elements = {uint32_t(src0->ne[1]), 1, 1}; // parallelize in {Cout, 1, 1}
  6961. } break;
  6962. case GGML_OP_POOL_2D:
  6963. {
  6964. const uint32_t N = dst->ne[3];
  6965. const uint32_t OC = dst->ne[2];
  6966. const uint32_t OH = dst->ne[1];
  6967. const uint32_t OW = dst->ne[0];
  6968. elements = { N * OC * OH * OW, 1, 1};
  6969. } break;
  6970. case GGML_OP_CONV_2D:
  6971. {
  6972. elements = ggml_vk_get_conv_elements(dst);
  6973. } break;
  6974. case GGML_OP_ADD:
  6975. case GGML_OP_SUB:
  6976. case GGML_OP_DIV:
  6977. case GGML_OP_MUL:
  6978. case GGML_OP_SCALE:
  6979. case GGML_OP_SQR:
  6980. case GGML_OP_SQRT:
  6981. case GGML_OP_SIN:
  6982. case GGML_OP_COS:
  6983. case GGML_OP_CLAMP:
  6984. case GGML_OP_PAD:
  6985. case GGML_OP_ROLL:
  6986. case GGML_OP_REPEAT:
  6987. case GGML_OP_REPEAT_BACK:
  6988. case GGML_OP_CPY:
  6989. case GGML_OP_CONCAT:
  6990. case GGML_OP_UPSCALE:
  6991. case GGML_OP_UNARY:
  6992. case GGML_OP_GLU:
  6993. case GGML_OP_CONV_2D_DW:
  6994. {
  6995. uint32_t ne = ggml_nelements(dst);
  6996. if (op == GGML_OP_CPY && ggml_is_quantized(src0->type) && ggml_is_quantized(dst->type)) {
  6997. // Convert from number of logical elements to 2- or 4-byte units.
  6998. ne /= ggml_blck_size(src0->type);
  6999. if ((ggml_type_size(src0->type) % 4) == 0) {
  7000. ne *= ggml_type_size(src0->type) / 4;
  7001. } else {
  7002. ne *= ggml_type_size(src0->type) / 2;
  7003. }
  7004. }
  7005. // copy_to_quant has block size of 32, and each thread does QUANT_K elements.
  7006. // Splitting into 512x512xZ wouldn't work well since each workgroup does 1024 elements.
  7007. // So divide by block size here before splitting into 512x512 groups.
  7008. if (op == GGML_OP_CPY && !ggml_is_quantized(src0->type) && ggml_is_quantized(dst->type)) {
  7009. ne = CEIL_DIV(ne, ggml_blck_size(dst->type));
  7010. }
  7011. if (ne > 262144) {
  7012. elements = { 512, 512, CEIL_DIV(ne, 262144) };
  7013. } else if (ne > 512) {
  7014. elements = { 512, CEIL_DIV(ne, 512), 1 };
  7015. } else {
  7016. elements = { ne, 1, 1 };
  7017. }
  7018. } break;
  7019. case GGML_OP_ADD_ID:
  7020. {
  7021. elements = { (uint32_t)ne01, (uint32_t)ne02, 1 };
  7022. } break;
  7023. case GGML_OP_SET_ROWS:
  7024. {
  7025. uint32_t ne = ggml_nelements(src0);
  7026. if (ggml_is_quantized(dst->type)) {
  7027. // quants run 32 threads each doing QUANT_K elements
  7028. ne = CEIL_DIV(ne, 32 * ggml_blck_size(dst->type));
  7029. } else {
  7030. // scalar types do one element per thread, running 512 threads
  7031. ne = CEIL_DIV(ne, 512);
  7032. }
  7033. if (ne > 262144) {
  7034. elements = { 512, 512, CEIL_DIV(ne, 262144) };
  7035. } else if (ne > 512) {
  7036. elements = { 512, CEIL_DIV(ne, 512), 1 };
  7037. } else {
  7038. elements = { ne, 1, 1 };
  7039. }
  7040. }
  7041. break;
  7042. default:
  7043. elements = { (uint32_t)ggml_nelements(src0), 1, 1 };
  7044. break;
  7045. }
  7046. if (!op_supports_incontiguous) {
  7047. if (x_sz != VK_WHOLE_SIZE) {
  7048. x_sz *= ne02 * ne03;
  7049. }
  7050. if (use_src1 && y_sz != VK_WHOLE_SIZE) {
  7051. y_sz *= ne12 * ne13;
  7052. }
  7053. if (use_src2 && z_sz != VK_WHOLE_SIZE) {
  7054. z_sz *= ne22 * ne23;
  7055. }
  7056. if (d_sz != VK_WHOLE_SIZE) {
  7057. d_sz *= ned2 * ned3;
  7058. }
  7059. }
  7060. if (op == GGML_OP_ADD || op == GGML_OP_RMS_NORM) {
  7061. vk_buffer d_A = ctx->do_add_rms_partials ? ctx->prealloc_add_rms_partials : d_X;
  7062. size_t a_buf_offset = ctx->do_add_rms_partials ? ctx->prealloc_size_add_rms_partials_offset : 0;
  7063. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline,
  7064. { vk_subbuffer{ d_X, x_buf_offset, x_sz },
  7065. vk_subbuffer{ d_Y, y_buf_offset, y_sz },
  7066. vk_subbuffer{ d_D, d_buf_offset, d_sz },
  7067. vk_subbuffer{ d_A, a_buf_offset, VK_WHOLE_SIZE },
  7068. }, pc, elements);
  7069. } else if (op == GGML_OP_GLU) {
  7070. // Empty src1 is possible in glu, but the shader needs a buffer
  7071. vk_subbuffer subbuf_y;
  7072. if (use_src1) {
  7073. subbuf_y = { d_Y, y_buf_offset, y_sz };
  7074. } else {
  7075. subbuf_y = { d_X, 0, x_sz };
  7076. }
  7077. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { vk_subbuffer{ d_X, x_buf_offset, x_sz }, subbuf_y, vk_subbuffer{ d_D, d_buf_offset, d_sz } }, pc, elements);
  7078. } else if (op == GGML_OP_SOFT_MAX) {
  7079. // Empty src1 and src2 is possible in soft_max, but the shader needs a buffer
  7080. vk_subbuffer subbuf_y;
  7081. if (use_src1) {
  7082. subbuf_y = { d_Y, y_buf_offset, y_sz };
  7083. } else {
  7084. subbuf_y = { d_X, 0, x_sz };
  7085. }
  7086. vk_subbuffer subbuf_z;
  7087. if (use_src2) {
  7088. subbuf_z = { d_Z, z_buf_offset, z_sz };
  7089. } else {
  7090. subbuf_z = { d_X, 0, x_sz };
  7091. }
  7092. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { vk_subbuffer{ d_X, x_buf_offset, x_sz }, subbuf_y, subbuf_z, vk_subbuffer{ d_D, d_buf_offset, d_sz } }, pc, elements);
  7093. } else if (op == GGML_OP_ROPE || op == GGML_OP_ROPE_BACK) {
  7094. // Empty src2 is possible in rope, but the shader needs a buffer
  7095. vk_subbuffer subbuf_z;
  7096. if (use_src2) {
  7097. subbuf_z = { d_Z, z_buf_offset, z_sz };
  7098. } else {
  7099. subbuf_z = { d_X, 0, x_sz };
  7100. }
  7101. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { vk_subbuffer{ d_X, x_buf_offset, x_sz }, vk_subbuffer{ d_Y, y_buf_offset, y_sz }, subbuf_z, vk_subbuffer{ d_D, d_buf_offset, d_sz } }, pc, elements);
  7102. } else if (op == GGML_OP_IM2COL) {
  7103. // im2col uses only src1 and dst buffers
  7104. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { vk_subbuffer{ d_Y, y_buf_offset, y_sz }, vk_subbuffer{ d_D, d_buf_offset, d_sz } }, pc, elements);
  7105. } else if (op == GGML_OP_COUNT_EQUAL) {
  7106. // count_equal assumes that destination buffer is initialized with zeroes
  7107. ggml_vk_buffer_memset_async(subctx, d_D, d_buf_offset, 0, d_sz);
  7108. ggml_vk_sync_buffers(ctx, subctx);
  7109. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { vk_subbuffer{ d_X, x_buf_offset, x_sz }, vk_subbuffer{ d_Y, y_buf_offset, y_sz }, vk_subbuffer{ d_D, d_buf_offset, d_sz } }, pc, elements);
  7110. } else if (op == GGML_OP_OPT_STEP_SGD) {
  7111. // OPT_STEP_SGD works on src0, it does not need dst
  7112. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { vk_subbuffer{ d_X, x_buf_offset, x_sz }, vk_subbuffer{ d_Y, y_buf_offset, y_sz }, vk_subbuffer{ d_Z, z_buf_offset, z_sz } }, pc, elements);
  7113. } else if (use_src2) {
  7114. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { vk_subbuffer{ d_X, x_buf_offset, x_sz }, vk_subbuffer{ d_Y, y_buf_offset, y_sz }, vk_subbuffer{ d_Z, z_buf_offset, z_sz }, vk_subbuffer{ d_D, d_buf_offset, d_sz } }, pc, elements);
  7115. } else if (use_src1) {
  7116. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { vk_subbuffer{ d_X, x_buf_offset, x_sz }, vk_subbuffer{ d_Y, y_buf_offset, y_sz }, vk_subbuffer{ d_D, d_buf_offset, d_sz } }, pc, elements);
  7117. } else {
  7118. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { vk_subbuffer{ d_X, x_buf_offset, x_sz }, vk_subbuffer{ d_D, d_buf_offset, d_sz } }, pc, elements);
  7119. }
  7120. }
  7121. static void ggml_vk_get_rows(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  7122. const uint32_t src0_type_size = ggml_type_size(src0->type);
  7123. const uint32_t src1_type_size = ggml_type_size(src1->type);
  7124. const uint32_t dst_type_size = ggml_type_size(dst->type);
  7125. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_GET_ROWS, {
  7126. (uint32_t)ggml_nelements(src0),
  7127. (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], (uint32_t)src0->ne[2],(uint32_t)src0->ne[3], (uint32_t)src0->nb[0] / src0_type_size, (uint32_t)src0->nb[1] / src0_type_size, (uint32_t)src0->nb[2] / src0_type_size, (uint32_t)src0->nb[3] / src0_type_size,
  7128. (uint32_t)src1->ne[0], (uint32_t)src1->ne[1], (uint32_t)src1->ne[2],(uint32_t)src1->ne[3], (uint32_t)src1->nb[0] / src1_type_size, (uint32_t)src1->nb[1] / src1_type_size, (uint32_t)src1->nb[2] / src1_type_size, (uint32_t)src1->nb[3] / src1_type_size,
  7129. (uint32_t) dst->ne[0], (uint32_t) dst->ne[1], (uint32_t) dst->ne[2],(uint32_t) dst->ne[3], (uint32_t) dst->nb[0] / dst_type_size, (uint32_t) dst->nb[1] / dst_type_size, (uint32_t) dst->nb[2] / dst_type_size, (uint32_t) dst->nb[3] / dst_type_size,
  7130. 0,
  7131. 0.0f, 0.0f, 0,
  7132. }, dryrun);
  7133. }
  7134. static void ggml_vk_acc(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  7135. const uint32_t src0_type_size = ggml_type_size(src0->type);
  7136. const uint32_t src1_type_size = ggml_type_size(src1->type);
  7137. const uint32_t dst_type_size = ggml_type_size(dst->type);
  7138. int nb1 = dst->op_params[0] / 4; // 4 bytes of float32
  7139. int nb2 = dst->op_params[1] / 4; // 4 bytes of float32
  7140. // int nb3 = dst->op_params[2] / 4; // 4 bytes of float32 - unused
  7141. int offset = dst->op_params[3] / 4; // offset in bytes
  7142. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_ACC, {
  7143. (uint32_t)ggml_nelements(src0),
  7144. (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], (uint32_t)src0->ne[2],(uint32_t)src0->ne[3], (uint32_t)src0->nb[0] / src0_type_size, (uint32_t)nb1, (uint32_t)nb2, (uint32_t)src0->nb[3] / src0_type_size,
  7145. (uint32_t)src1->ne[0], (uint32_t)src1->ne[1], (uint32_t)src1->ne[2],(uint32_t)src1->ne[3], (uint32_t)src1->nb[0] / src1_type_size, (uint32_t)src1->nb[1] / src1_type_size, (uint32_t)src1->nb[2] / src1_type_size, (uint32_t)src1->nb[3] / src1_type_size,
  7146. (uint32_t) dst->ne[0], (uint32_t) dst->ne[1], (uint32_t) dst->ne[2],(uint32_t) dst->ne[3], (uint32_t) dst->nb[0] / dst_type_size, (uint32_t)nb1, (uint32_t)nb2, (uint32_t) dst->nb[3] / dst_type_size,
  7147. 0,
  7148. 0.0f, 0.0f, offset,
  7149. }, dryrun);
  7150. }
  7151. static void ggml_vk_multi_add(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_cgraph * cgraph, int node_idx, bool dryrun = false) {
  7152. const ggml_tensor *first_node = cgraph->nodes[node_idx];
  7153. const ggml_tensor *dst = cgraph->nodes[node_idx + ctx->num_additional_fused_ops];
  7154. // Make a list of all the tensors used by the op.
  7155. // Last element of the list is the dest tensor.
  7156. const ggml_tensor *tensors[MAX_PARAMETER_COUNT];
  7157. uint32_t num_srcs = ctx->num_additional_fused_ops + 2;
  7158. uint32_t num_tensors = num_srcs + 1;
  7159. GGML_ASSERT(num_tensors + ctx->do_add_rms_partials <= MAX_PARAMETER_COUNT);
  7160. tensors[0] = first_node->src[0];
  7161. tensors[1] = first_node->src[1];
  7162. for (int32_t i = 0; i < ctx->num_additional_fused_ops; ++i) {
  7163. // check whether the previous result is src[0] or src[1]
  7164. if (cgraph->nodes[node_idx + i] == cgraph->nodes[node_idx + i + 1]->src[0]) {
  7165. tensors[i+2] = cgraph->nodes[node_idx + i + 1]->src[1];
  7166. } else {
  7167. tensors[i+2] = cgraph->nodes[node_idx + i + 1]->src[0];
  7168. }
  7169. }
  7170. tensors[num_srcs] = dst;
  7171. vk_op_multi_add_push_constants pc;
  7172. pc.ne20 = (uint32_t)dst->ne[0];
  7173. pc.ne21 = (uint32_t)dst->ne[1];
  7174. pc.ne22 = (uint32_t)dst->ne[2];
  7175. pc.ne23 = (uint32_t)dst->ne[3];
  7176. for (uint32_t i = 0; i < num_tensors; ++i) {
  7177. const ggml_tensor *t = tensors[i];
  7178. pc.nb[i][0] = (uint32_t)t->nb[0] / sizeof(float);
  7179. pc.nb[i][1] = (uint32_t)t->nb[1] / sizeof(float);
  7180. pc.nb[i][2] = (uint32_t)t->nb[2] / sizeof(float);
  7181. pc.nb[i][3] = (uint32_t)t->nb[3] / sizeof(float);
  7182. }
  7183. pc.rms_partials = ctx->do_add_rms_partials;
  7184. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, tensors[0], tensors[1], nullptr, dst, dst->op);
  7185. if (pipeline == nullptr) {
  7186. std::cerr << "ggml_vulkan: Error: Missing multi_add";
  7187. GGML_ABORT("fatal error");
  7188. }
  7189. if (dryrun) {
  7190. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  7191. return;
  7192. }
  7193. ggml_backend_vk_buffer_context * buf_ctx[MAX_PARAMETER_COUNT];
  7194. vk_buffer buf[MAX_PARAMETER_COUNT];
  7195. size_t offset[MAX_PARAMETER_COUNT];
  7196. bool uma[MAX_PARAMETER_COUNT];
  7197. for (uint32_t i = 0; i < num_tensors; ++i) {
  7198. buf_ctx[i] = (ggml_backend_vk_buffer_context *)tensors[i]->buffer->context;
  7199. buf[i] = nullptr;
  7200. offset[i] = 0;
  7201. uma[i] = false;
  7202. if (ctx->device->uma) {
  7203. ggml_vk_host_get(ctx->device, tensors[i]->data, buf[i], offset[i]);
  7204. uma[i] = buf[i] != nullptr;
  7205. }
  7206. if (!uma[i]) {
  7207. buf[i] = buf_ctx[i]->dev_buffer;
  7208. offset[i] = vk_tensor_offset(tensors[i]) + tensors[i]->view_offs;
  7209. }
  7210. GGML_ASSERT(buf[i] != nullptr);
  7211. }
  7212. // If any remaining descriptors are unused, just point them at src[0]
  7213. for (uint32_t i = num_tensors; i < MAX_PARAMETER_COUNT; ++i) {
  7214. buf[i] = buf[0];
  7215. offset[i] = 0;
  7216. }
  7217. if (ctx->do_add_rms_partials) {
  7218. buf[num_tensors] = ctx->prealloc_add_rms_partials;
  7219. offset[num_tensors] = ctx->prealloc_size_add_rms_partials_offset;
  7220. }
  7221. std::array<uint32_t, 3> elements;
  7222. uint32_t ne = ggml_nelements(dst);
  7223. if (ne > 262144) {
  7224. elements = { 512, 512, CEIL_DIV(ne, 262144) };
  7225. } else if (ne > 512) {
  7226. elements = { 512, CEIL_DIV(ne, 512), 1 };
  7227. } else {
  7228. elements = { ne, 1, 1 };
  7229. }
  7230. static_assert(MAX_PARAMETER_COUNT == 12);
  7231. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline,
  7232. {
  7233. vk_subbuffer{ buf[0], offset[0], VK_WHOLE_SIZE },
  7234. vk_subbuffer{ buf[1], offset[1], VK_WHOLE_SIZE },
  7235. vk_subbuffer{ buf[2], offset[2], VK_WHOLE_SIZE },
  7236. vk_subbuffer{ buf[3], offset[3], VK_WHOLE_SIZE },
  7237. vk_subbuffer{ buf[4], offset[4], VK_WHOLE_SIZE },
  7238. vk_subbuffer{ buf[5], offset[5], VK_WHOLE_SIZE },
  7239. vk_subbuffer{ buf[6], offset[6], VK_WHOLE_SIZE },
  7240. vk_subbuffer{ buf[7], offset[7], VK_WHOLE_SIZE },
  7241. vk_subbuffer{ buf[8], offset[8], VK_WHOLE_SIZE },
  7242. vk_subbuffer{ buf[9], offset[9], VK_WHOLE_SIZE },
  7243. vk_subbuffer{ buf[10], offset[10], VK_WHOLE_SIZE },
  7244. vk_subbuffer{ buf[11], offset[11], VK_WHOLE_SIZE },
  7245. }, pc, elements);
  7246. }
  7247. static void ggml_vk_add(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  7248. const uint32_t src0_type_size = ggml_type_size(src0->type);
  7249. const uint32_t src1_type_size = ggml_type_size(src1->type);
  7250. const uint32_t dst_type_size = ggml_type_size(dst->type);
  7251. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_ADD, {
  7252. (uint32_t)ggml_nelements(src0),
  7253. (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], (uint32_t)src0->ne[2],(uint32_t)src0->ne[3], (uint32_t)src0->nb[0] / src0_type_size, (uint32_t)src0->nb[1] / src0_type_size, (uint32_t)src0->nb[2] / src0_type_size, (uint32_t)src0->nb[3] / src0_type_size,
  7254. (uint32_t)src1->ne[0], (uint32_t)src1->ne[1], (uint32_t)src1->ne[2],(uint32_t)src1->ne[3], (uint32_t)src1->nb[0] / src1_type_size, (uint32_t)src1->nb[1] / src1_type_size, (uint32_t)src1->nb[2] / src1_type_size, (uint32_t)src1->nb[3] / src1_type_size,
  7255. (uint32_t) dst->ne[0], (uint32_t) dst->ne[1], (uint32_t) dst->ne[2],(uint32_t) dst->ne[3], (uint32_t) dst->nb[0] / dst_type_size, (uint32_t) dst->nb[1] / dst_type_size, (uint32_t) dst->nb[2] / dst_type_size, (uint32_t) dst->nb[3] / dst_type_size,
  7256. 0,
  7257. 0.0f, 0.0f, ctx->do_add_rms_partials,
  7258. }, dryrun);
  7259. }
  7260. static void ggml_vk_sub(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  7261. const uint32_t src0_type_size = ggml_type_size(src0->type);
  7262. const uint32_t src1_type_size = ggml_type_size(src1->type);
  7263. const uint32_t dst_type_size = ggml_type_size(dst->type);
  7264. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_SUB, {
  7265. (uint32_t)ggml_nelements(src0),
  7266. (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], (uint32_t)src0->ne[2],(uint32_t)src0->ne[3], (uint32_t)src0->nb[0] / src0_type_size, (uint32_t)src0->nb[1] / src0_type_size, (uint32_t)src0->nb[2] / src0_type_size, (uint32_t)src0->nb[3] / src0_type_size,
  7267. (uint32_t)src1->ne[0], (uint32_t)src1->ne[1], (uint32_t)src1->ne[2],(uint32_t)src1->ne[3], (uint32_t)src1->nb[0] / src1_type_size, (uint32_t)src1->nb[1] / src1_type_size, (uint32_t)src1->nb[2] / src1_type_size, (uint32_t)src1->nb[3] / src1_type_size,
  7268. (uint32_t) dst->ne[0], (uint32_t) dst->ne[1], (uint32_t) dst->ne[2],(uint32_t) dst->ne[3], (uint32_t) dst->nb[0] / dst_type_size, (uint32_t) dst->nb[1] / dst_type_size, (uint32_t) dst->nb[2] / dst_type_size, (uint32_t) dst->nb[3] / dst_type_size,
  7269. 0,
  7270. 0.0f, 0.0f, 0,
  7271. }, dryrun);
  7272. }
  7273. static void ggml_vk_mul(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  7274. const uint32_t src0_type_size = ggml_type_size(src0->type);
  7275. const uint32_t src1_type_size = ggml_type_size(src1->type);
  7276. const uint32_t dst_type_size = ggml_type_size(dst->type);
  7277. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_MUL, {
  7278. (uint32_t)ggml_nelements(src0),
  7279. (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], (uint32_t)src0->ne[2],(uint32_t)src0->ne[3], (uint32_t)src0->nb[0] / src0_type_size, (uint32_t)src0->nb[1] / src0_type_size, (uint32_t)src0->nb[2] / src0_type_size, (uint32_t)src0->nb[3] / src0_type_size,
  7280. (uint32_t)src1->ne[0], (uint32_t)src1->ne[1], (uint32_t)src1->ne[2],(uint32_t)src1->ne[3], (uint32_t)src1->nb[0] / src1_type_size, (uint32_t)src1->nb[1] / src1_type_size, (uint32_t)src1->nb[2] / src1_type_size, (uint32_t)src1->nb[3] / src1_type_size,
  7281. (uint32_t) dst->ne[0], (uint32_t) dst->ne[1], (uint32_t) dst->ne[2],(uint32_t) dst->ne[3], (uint32_t) dst->nb[0] / dst_type_size, (uint32_t) dst->nb[1] / dst_type_size, (uint32_t) dst->nb[2] / dst_type_size, (uint32_t) dst->nb[3] / dst_type_size,
  7282. 0,
  7283. 0.0f, 0.0f, 0,
  7284. }, dryrun);
  7285. }
  7286. static void ggml_vk_div(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  7287. const uint32_t src0_type_size = ggml_type_size(src0->type);
  7288. const uint32_t src1_type_size = ggml_type_size(src1->type);
  7289. const uint32_t dst_type_size = ggml_type_size(dst->type);
  7290. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_DIV, {
  7291. (uint32_t)ggml_nelements(src0),
  7292. (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], (uint32_t)src0->ne[2],(uint32_t)src0->ne[3], (uint32_t)src0->nb[0] / src0_type_size, (uint32_t)src0->nb[1] / src0_type_size, (uint32_t)src0->nb[2] / src0_type_size, (uint32_t)src0->nb[3] / src0_type_size,
  7293. (uint32_t)src1->ne[0], (uint32_t)src1->ne[1], (uint32_t)src1->ne[2],(uint32_t)src1->ne[3], (uint32_t)src1->nb[0] / src1_type_size, (uint32_t)src1->nb[1] / src1_type_size, (uint32_t)src1->nb[2] / src1_type_size, (uint32_t)src1->nb[3] / src1_type_size,
  7294. (uint32_t) dst->ne[0], (uint32_t) dst->ne[1], (uint32_t) dst->ne[2],(uint32_t) dst->ne[3], (uint32_t) dst->nb[0] / dst_type_size, (uint32_t) dst->nb[1] / dst_type_size, (uint32_t) dst->nb[2] / dst_type_size, (uint32_t) dst->nb[3] / dst_type_size,
  7295. 0,
  7296. 0.0f, 0.0f, 0,
  7297. }, dryrun);
  7298. }
  7299. static void ggml_vk_add_id(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, ggml_tensor * dst, bool dryrun = false) {
  7300. const uint32_t src0_type_size = ggml_type_size(src0->type);
  7301. const uint32_t src1_type_size = ggml_type_size(src1->type);
  7302. const uint32_t src2_type_size = ggml_type_size(src2->type);
  7303. ggml_vk_op_f32<vk_op_add_id_push_constants>(ctx, subctx, src0, src1, src2, dst, GGML_OP_ADD_ID, {
  7304. (uint32_t)dst->ne[0],
  7305. (uint32_t)dst->ne[1],
  7306. (uint32_t)src0->nb[1] / src0_type_size,
  7307. (uint32_t)src0->nb[2] / src0_type_size,
  7308. (uint32_t)src1->nb[1] / src1_type_size,
  7309. (uint32_t)src2->nb[1] / src2_type_size,
  7310. }, dryrun);
  7311. }
  7312. static void ggml_vk_op_f32_wkv(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, const vk_op_rwkv_wkv6_push_constants&& pc, int version, bool dryrun = false) {
  7313. GGML_ASSERT(version == 6 || version == 7);
  7314. int num_srcs = version == 6 ? 6 : 7;
  7315. for (int i = 0; i < num_srcs; i++) {
  7316. GGML_ASSERT(!ggml_is_quantized(dst->src[i]->type));
  7317. }
  7318. GGML_ASSERT(dst->buffer != nullptr);
  7319. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, dst->src[0], dst->src[1], dst->src[2], dst, dst->op);
  7320. GGML_ASSERT(pipeline != nullptr);
  7321. if (dryrun) {
  7322. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  7323. return;
  7324. }
  7325. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  7326. ggml_backend_vk_buffer_context * src_buf_ctxs[7] = { nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr };
  7327. for (int i = 0; i < num_srcs; i++) {
  7328. src_buf_ctxs[i] = (ggml_backend_vk_buffer_context *)dst->src[i]->buffer->context;
  7329. }
  7330. vk_buffer d_D = nullptr, d_srcs[7] = { nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr };
  7331. size_t dst_offset = 0, src_offsets[7] = { 0, 0, 0, 0, 0, 0, 0 };
  7332. bool dst_uma = false, srcs_uma[7] = { false, false, false, false, false, false, false };
  7333. if (ctx->device->uma) {
  7334. for (int i = 0; i < num_srcs; i++) {
  7335. ggml_vk_host_get(ctx->device, dst->src[i]->data, d_srcs[i], src_offsets[i]);
  7336. srcs_uma[i] = d_srcs[i] != nullptr;
  7337. }
  7338. ggml_vk_host_get(ctx->device, dst->data, d_D, dst_offset);
  7339. dst_uma = d_D != nullptr;
  7340. }
  7341. uint64_t src_sizes[7] = { 0, 0, 0, 0, 0, 0, 0 };
  7342. for (int i = 0; i < num_srcs; i++) {
  7343. src_sizes[i] = ggml_nbytes(dst->src[i]);
  7344. if (!srcs_uma[i]) {
  7345. d_srcs[i] = src_buf_ctxs[i]->dev_buffer;
  7346. src_offsets[i] = vk_tensor_offset(dst->src[i]) + dst->src[i]->view_offs;
  7347. }
  7348. }
  7349. const uint64_t dst_size = ggml_nbytes(dst);
  7350. if (!dst_uma) {
  7351. d_D = dst_buf_ctx->dev_buffer;
  7352. dst_offset = vk_tensor_offset(dst) + dst->view_offs;
  7353. }
  7354. std::array<uint32_t, 3> elements = {
  7355. (uint32_t)(pc.B * pc.H),
  7356. 1,
  7357. 1
  7358. };
  7359. if (version == 6) {
  7360. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, {
  7361. vk_subbuffer{ d_srcs[0], src_offsets[0], src_sizes[0] },
  7362. vk_subbuffer{ d_srcs[1], src_offsets[1], src_sizes[1] },
  7363. vk_subbuffer{ d_srcs[2], src_offsets[2], src_sizes[2] },
  7364. vk_subbuffer{ d_srcs[3], src_offsets[3], src_sizes[3] },
  7365. vk_subbuffer{ d_srcs[4], src_offsets[4], src_sizes[4] },
  7366. vk_subbuffer{ d_srcs[5], src_offsets[5], src_sizes[5] },
  7367. vk_subbuffer{ d_D, dst_offset, dst_size }
  7368. }, pc, elements);
  7369. } else if (version == 7) {
  7370. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, {
  7371. vk_subbuffer{ d_srcs[0], src_offsets[0], src_sizes[0] },
  7372. vk_subbuffer{ d_srcs[1], src_offsets[1], src_sizes[1] },
  7373. vk_subbuffer{ d_srcs[2], src_offsets[2], src_sizes[2] },
  7374. vk_subbuffer{ d_srcs[3], src_offsets[3], src_sizes[3] },
  7375. vk_subbuffer{ d_srcs[4], src_offsets[4], src_sizes[4] },
  7376. vk_subbuffer{ d_srcs[5], src_offsets[5], src_sizes[5] },
  7377. vk_subbuffer{ d_srcs[6], src_offsets[6], src_sizes[6] },
  7378. vk_subbuffer{ d_D, dst_offset, dst_size }
  7379. }, pc, elements);
  7380. } else {
  7381. // shouldn't happen
  7382. GGML_ASSERT(false);
  7383. }
  7384. }
  7385. static void ggml_vk_rwkv_wkv6(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, bool dryrun = false) {
  7386. const size_t seq_length = dst->src[0]->ne[2];
  7387. const size_t n_embed = dst->ne[0];
  7388. const size_t n_heads = dst->src[0]->ne[1];
  7389. const size_t n_seqs = dst->src[5]->ne[1];
  7390. ggml_vk_op_f32_wkv(
  7391. ctx, subctx, dst,
  7392. {
  7393. (uint32_t)n_seqs,
  7394. (uint32_t)seq_length,
  7395. (uint32_t)n_embed,
  7396. (uint32_t)n_heads,
  7397. },
  7398. 6,
  7399. dryrun
  7400. );
  7401. }
  7402. static void ggml_vk_rwkv_wkv7(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, bool dryrun = false) {
  7403. const size_t seq_length = dst->src[0]->ne[2];
  7404. const size_t n_embed = dst->ne[0];
  7405. const size_t n_heads = dst->src[0]->ne[1];
  7406. const size_t n_seqs = dst->src[6]->ne[1];
  7407. ggml_vk_op_f32_wkv(
  7408. ctx, subctx, dst,
  7409. {
  7410. (uint32_t)n_seqs,
  7411. (uint32_t)seq_length,
  7412. (uint32_t)n_embed,
  7413. (uint32_t)n_heads,
  7414. },
  7415. 7,
  7416. dryrun
  7417. );
  7418. }
  7419. static void ggml_vk_op_f32_opt_step_adamw(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, const vk_op_push_constants&& pc, bool dryrun = false) {
  7420. const ggml_tensor * x = dst->src[0];
  7421. const ggml_tensor * g = dst->src[1];
  7422. const ggml_tensor * gm = dst->src[2];
  7423. const ggml_tensor * gv = dst->src[3];
  7424. const ggml_tensor * p = dst->src[4];
  7425. GGML_ASSERT(x->type == GGML_TYPE_F32);
  7426. GGML_ASSERT(g->type == GGML_TYPE_F32);
  7427. GGML_ASSERT(gm->type == GGML_TYPE_F32);
  7428. GGML_ASSERT(gv->type == GGML_TYPE_F32);
  7429. GGML_ASSERT(p->type == GGML_TYPE_F32);
  7430. GGML_ASSERT(dst->buffer != nullptr);
  7431. GGML_ASSERT(ggml_is_contiguous(x));
  7432. GGML_ASSERT(ggml_is_contiguous(g));
  7433. GGML_ASSERT(ggml_is_contiguous(gm));
  7434. GGML_ASSERT(ggml_is_contiguous(gv));
  7435. GGML_ASSERT(ggml_is_contiguous(p));
  7436. GGML_ASSERT(ggml_are_same_shape(x, g));
  7437. GGML_ASSERT(ggml_are_same_shape(x, gm));
  7438. GGML_ASSERT(ggml_are_same_shape(x, gv));
  7439. GGML_ASSERT(ggml_nelements(p) == 7);
  7440. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, g, gm, gv, dst, GGML_OP_OPT_STEP_ADAMW);
  7441. GGML_ASSERT(pipeline != nullptr);
  7442. if (dryrun) {
  7443. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  7444. return;
  7445. }
  7446. ggml_backend_vk_buffer_context * x_buf_ctx = (ggml_backend_vk_buffer_context *)x->buffer->context;
  7447. ggml_backend_vk_buffer_context * g_buf_ctx = (ggml_backend_vk_buffer_context *)g->buffer->context;
  7448. ggml_backend_vk_buffer_context * gm_buf_ctx = (ggml_backend_vk_buffer_context *)gm->buffer->context;
  7449. ggml_backend_vk_buffer_context * gv_buf_ctx = (ggml_backend_vk_buffer_context *)gv->buffer->context;
  7450. ggml_backend_vk_buffer_context * p_buf_ctx = (ggml_backend_vk_buffer_context *)p->buffer->context;
  7451. vk_buffer d_X = nullptr, d_G = nullptr, d_GM = nullptr, d_GV = nullptr, d_P = nullptr;
  7452. size_t x_offset = 0, g_offset = 0, gm_offset = 0, gv_offset = 0, p_offset = 0;
  7453. bool X_uma = false, G_uma = false, GM_uma = false, GV_uma = false, P_uma = false;
  7454. if (ctx->device->uma) {
  7455. ggml_vk_host_get(ctx->device, x->data, d_X, x_offset);
  7456. ggml_vk_host_get(ctx->device, g->data, d_G, g_offset);
  7457. ggml_vk_host_get(ctx->device, gm->data, d_GM, gm_offset);
  7458. ggml_vk_host_get(ctx->device, gv->data, d_GV, gv_offset);
  7459. ggml_vk_host_get(ctx->device, p->data, d_P, p_offset);
  7460. X_uma = d_X != nullptr;
  7461. G_uma = d_G != nullptr;
  7462. GM_uma = d_GM != nullptr;
  7463. GV_uma = d_GV != nullptr;
  7464. P_uma = d_P != nullptr;
  7465. }
  7466. if (!X_uma) {
  7467. d_X = x_buf_ctx->dev_buffer;
  7468. x_offset = vk_tensor_offset(x) + x->view_offs;
  7469. }
  7470. if (!G_uma) {
  7471. d_G = g_buf_ctx->dev_buffer;
  7472. g_offset = vk_tensor_offset(g) + g->view_offs;
  7473. }
  7474. if (!GM_uma) {
  7475. d_GM = gm_buf_ctx->dev_buffer;
  7476. gm_offset = vk_tensor_offset(gm) + gm->view_offs;
  7477. }
  7478. if (!GV_uma) {
  7479. d_GV = gv_buf_ctx->dev_buffer;
  7480. gv_offset = vk_tensor_offset(gv) + gv->view_offs;
  7481. }
  7482. if (!P_uma) {
  7483. d_P = p_buf_ctx->dev_buffer;
  7484. p_offset = vk_tensor_offset(p) + p->view_offs;
  7485. }
  7486. const uint64_t x_size = ggml_nbytes(x);
  7487. const uint64_t g_size = ggml_nbytes(g);
  7488. const uint64_t gm_size = ggml_nbytes(gm);
  7489. const uint64_t gv_size = ggml_nbytes(gv);
  7490. const uint64_t p_size = ggml_nbytes(p);
  7491. std::array<uint32_t, 3> elements = { (uint32_t)ggml_nelements(x), 1, 1 };
  7492. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, {
  7493. vk_subbuffer{ d_X, x_offset, x_size },
  7494. vk_subbuffer{ d_G, g_offset, g_size },
  7495. vk_subbuffer{ d_GM, gm_offset, gm_size },
  7496. vk_subbuffer{ d_GV, gv_offset, gv_size },
  7497. vk_subbuffer{ d_P, p_offset, p_size },
  7498. }, pc, elements);
  7499. }
  7500. static void ggml_vk_opt_step_adamw(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, bool dryrun = false) {
  7501. const size_t n = ggml_nelements(dst->src[0]);
  7502. ggml_vk_op_f32_opt_step_adamw(
  7503. ctx, subctx, dst,
  7504. { (uint32_t)n, 0, 0.0f, 0.0f },
  7505. dryrun
  7506. );
  7507. }
  7508. static void ggml_vk_opt_step_sgd(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, ggml_tensor * dst, bool dryrun = false) {
  7509. const size_t n = ggml_nelements(dst->src[0]);
  7510. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, src1, src2, dst, GGML_OP_OPT_STEP_SGD, { (uint32_t)n, 0, 0.0f, 0.0f }, dryrun);
  7511. }
  7512. static void ggml_vk_concat(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  7513. int * op_params = (int *)dst->op_params;
  7514. const uint32_t src0_type_size = ggml_type_size(src0->type);
  7515. const uint32_t src1_type_size = ggml_type_size(src1->type);
  7516. const uint32_t dst_type_size = ggml_type_size(dst->type);
  7517. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_CONCAT, {
  7518. (uint32_t)ggml_nelements(dst),
  7519. (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], (uint32_t)src0->ne[2],(uint32_t)src0->ne[3], (uint32_t)src0->nb[0] / src0_type_size, (uint32_t)src0->nb[1] / src0_type_size, (uint32_t)src0->nb[2] / src0_type_size, (uint32_t)src0->nb[3] / src0_type_size,
  7520. (uint32_t)src1->ne[0], (uint32_t)src1->ne[1], (uint32_t)src1->ne[2],(uint32_t)src1->ne[3], (uint32_t)src1->nb[0] / src1_type_size, (uint32_t)src1->nb[1] / src1_type_size, (uint32_t)src1->nb[2] / src1_type_size, (uint32_t)src1->nb[3] / src1_type_size,
  7521. (uint32_t) dst->ne[0], (uint32_t) dst->ne[1], (uint32_t) dst->ne[2],(uint32_t) dst->ne[3], (uint32_t) dst->nb[0] / dst_type_size, (uint32_t) dst->nb[1] / dst_type_size, (uint32_t) dst->nb[2] / dst_type_size, (uint32_t) dst->nb[3] / dst_type_size,
  7522. 0,
  7523. 0.0f, 0.0f, op_params[0],
  7524. }, dryrun);
  7525. }
  7526. static void ggml_vk_upscale(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7527. const uint32_t src0_type_size = ggml_type_size(src0->type);
  7528. const uint32_t mode = (uint32_t)ggml_get_op_params_i32(dst, 0);
  7529. float sf0 = (float)dst->ne[0] / src0->ne[0];
  7530. float sf1 = (float)dst->ne[1] / src0->ne[1];
  7531. float sf2 = (float)dst->ne[2] / src0->ne[2];
  7532. float sf3 = (float)dst->ne[3] / src0->ne[3];
  7533. if (mode & GGML_SCALE_FLAG_ALIGN_CORNERS) {
  7534. sf0 = (float)(dst->ne[0] - 1) / (src0->ne[0] - 1);
  7535. sf1 = (float)(dst->ne[1] - 1) / (src0->ne[1] - 1);
  7536. }
  7537. ggml_vk_op_f32<vk_op_upscale_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_UPSCALE, {
  7538. (uint32_t)ggml_nelements(dst), 0, 0,
  7539. (uint32_t)src0->ne[0], (uint32_t)src0->ne[1],
  7540. (uint32_t)src0->nb[0] / src0_type_size, (uint32_t)src0->nb[1] / src0_type_size, (uint32_t)src0->nb[2] / src0_type_size, (uint32_t)src0->nb[3] / src0_type_size,
  7541. (uint32_t)dst->ne[0], (uint32_t)dst->ne[1], (uint32_t)dst->ne[2],(uint32_t)dst->ne[3],
  7542. sf0, sf1, sf2, sf3,
  7543. }, dryrun);
  7544. }
  7545. static void ggml_vk_scale(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7546. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst);
  7547. p.param1 = ggml_get_op_params_f32(dst, 0);
  7548. p.param2 = ggml_get_op_params_f32(dst, 1);
  7549. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SCALE, std::move(p), dryrun);
  7550. }
  7551. static void ggml_vk_sqr(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7552. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SQR, vk_op_unary_push_constants_init(src0, dst), dryrun);
  7553. }
  7554. static void ggml_vk_sqrt(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7555. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SQRT, vk_op_unary_push_constants_init(src0, dst), dryrun);
  7556. }
  7557. static void ggml_vk_sin(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7558. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SIN, vk_op_unary_push_constants_init(src0, dst), dryrun);
  7559. }
  7560. static void ggml_vk_cos(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7561. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_COS, vk_op_unary_push_constants_init(src0, dst), dryrun);
  7562. }
  7563. static void ggml_vk_clamp(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7564. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst);
  7565. p.param1 = ggml_get_op_params_f32(dst, 0);
  7566. p.param2 = ggml_get_op_params_f32(dst, 1);
  7567. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_CLAMP, std::move(p), dryrun);
  7568. }
  7569. static void ggml_vk_pad(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7570. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst, ggml_nelements(dst));
  7571. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_PAD, std::move(p), dryrun);
  7572. }
  7573. static void ggml_vk_roll(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7574. const int32_t s0 = ggml_get_op_params_i32(dst, 0);
  7575. const int32_t s1 = ggml_get_op_params_i32(dst, 1);
  7576. const int32_t s2 = ggml_get_op_params_i32(dst, 2);
  7577. const int32_t s3 = ggml_get_op_params_i32(dst, 3);
  7578. const uint32_t s01_packed = ((s0 + 0x8000) << 16) | (s1 + 0x8000);
  7579. const uint32_t s23_packed = ((s2 + 0x8000) << 16) | (s3 + 0x8000);
  7580. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst);
  7581. memcpy(&p.param1, &s01_packed, sizeof(float));
  7582. memcpy(&p.param2, &s23_packed, sizeof(float));
  7583. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_ROLL, std::move(p), dryrun);
  7584. }
  7585. static void ggml_vk_repeat(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7586. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst, ggml_nelements(dst));
  7587. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_REPEAT, std::move(p), dryrun);
  7588. }
  7589. static void ggml_vk_repeat_back(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7590. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst, ggml_nelements(dst));
  7591. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_REPEAT_BACK, std::move(p), dryrun);
  7592. }
  7593. static void ggml_vk_cpy(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7594. uint32_t ne = (uint32_t)ggml_nelements(src0);
  7595. if (ggml_is_quantized(src0->type) && ggml_is_quantized(dst->type)) {
  7596. // Convert from number of logical elements to 2- or 4-byte units.
  7597. ne /= ggml_blck_size(src0->type);
  7598. if ((ggml_type_size(src0->type) % 4) == 0) {
  7599. ne *= ggml_type_size(src0->type) / 4;
  7600. } else {
  7601. ne *= ggml_type_size(src0->type) / 2;
  7602. }
  7603. }
  7604. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst, ne);
  7605. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_CPY, std::move(p), dryrun);
  7606. }
  7607. static void ggml_vk_set_rows(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  7608. const uint32_t src0_type_size = ggml_type_size(src0->type);
  7609. const uint32_t src1_type_size = ggml_type_size(src1->type);
  7610. const uint32_t dst_type_size = ggml_type_size(dst->type);
  7611. // Skip empty skip_rows operations. For most ops the empty check at the start
  7612. // of ggml_vk_build_graph is sufficient, but set_rows can have a nonempty dst
  7613. // with empty srcs.
  7614. if (ggml_is_empty(src0) || ggml_is_empty(src1)) {
  7615. return;
  7616. }
  7617. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_SET_ROWS, {
  7618. (uint32_t)ggml_nelements(src0),
  7619. (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], (uint32_t)src0->ne[2],(uint32_t)src0->ne[3], (uint32_t)src0->nb[0] / src0_type_size, (uint32_t)src0->nb[1] / src0_type_size, (uint32_t)src0->nb[2] / src0_type_size, (uint32_t)src0->nb[3] / src0_type_size,
  7620. (uint32_t)src1->ne[0], (uint32_t)src1->ne[1], (uint32_t)src1->ne[2],(uint32_t)src1->ne[3], (uint32_t)src1->nb[0] / src1_type_size, (uint32_t)src1->nb[1] / src1_type_size, (uint32_t)src1->nb[2] / src1_type_size, (uint32_t)src1->nb[3] / src1_type_size,
  7621. (uint32_t) dst->ne[0], (uint32_t) dst->ne[1], (uint32_t) dst->ne[2],(uint32_t) dst->ne[3], (uint32_t) dst->nb[0] / dst_type_size, (uint32_t) dst->nb[1] / dst_type_size, (uint32_t) dst->nb[2] / dst_type_size, (uint32_t) dst->nb[3] / dst_type_size,
  7622. 0,
  7623. 0.0f, 0.0f, 0,
  7624. }, dryrun);
  7625. }
  7626. static void ggml_vk_silu_back(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  7627. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_SILU_BACK, { (uint32_t)ggml_nelements(src0), 0, 0.0f, 0.0f }, dryrun);
  7628. }
  7629. static void ggml_vk_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7630. float * op_params = (float *)dst->op_params;
  7631. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_NORM, { (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], op_params[0], 0.0f }, dryrun);
  7632. }
  7633. static void ggml_vk_group_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7634. const int * int_op_params = (const int *)dst->op_params;
  7635. const float * float_op_params = (const float *)dst->op_params;
  7636. const uint32_t num_groups = int_op_params[0];
  7637. const float eps = float_op_params[1];
  7638. const uint32_t group_size = src0->ne[0] * src0->ne[1] * ((src0->ne[2] + num_groups - 1) / num_groups);
  7639. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_GROUP_NORM, { group_size, 0, eps, 0.0f }, dryrun);
  7640. }
  7641. static uint32_t ggml_vk_rms_num_partials(ggml_backend_vk_context * ctx, const ggml_tensor *node) {
  7642. const uint32_t ne = (uint32_t)node->ne[0];
  7643. const uint32_t denom = ctx->device->pipeline_add_rms[0][0][0]->wg_denoms[0];
  7644. const uint32_t num_partials = CEIL_DIV(ne, denom);
  7645. return num_partials;
  7646. }
  7647. static uint32_t ggml_vk_rms_partials_size(ggml_backend_vk_context * ctx, const ggml_tensor *node) {
  7648. const uint32_t num_partials = ggml_vk_rms_num_partials(ctx, node);
  7649. const uint32_t num_bytes = ROUNDUP_POW2(num_partials * sizeof(uint32_t), ctx->device->partials_binding_alignment);
  7650. return num_bytes;
  7651. }
  7652. static void ggml_vk_rms_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, float * op_params, bool dryrun = false) {
  7653. const uint32_t src0_type_size = ggml_type_size(src0->type);
  7654. const uint32_t src1_type_size = ggml_type_size(src1->type);
  7655. const uint32_t dst_type_size = ggml_type_size(dst->type);
  7656. uint32_t param3 = ctx->do_add_rms_partials ? ggml_vk_rms_num_partials(ctx, dst) : 0;
  7657. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_RMS_NORM, {
  7658. (uint32_t)ggml_nelements(src0),
  7659. (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], (uint32_t)src0->ne[2],(uint32_t)src0->ne[3], (uint32_t)src0->nb[0] / src0_type_size, (uint32_t)src0->nb[1] / src0_type_size, (uint32_t)src0->nb[2] / src0_type_size, (uint32_t)src0->nb[3] / src0_type_size,
  7660. (uint32_t)src1->ne[0], (uint32_t)src1->ne[1], (uint32_t)src1->ne[2],(uint32_t)src1->ne[3], (uint32_t)src1->nb[0] / src1_type_size, (uint32_t)src1->nb[1] / src1_type_size, (uint32_t)src1->nb[2] / src1_type_size, (uint32_t)src1->nb[3] / src1_type_size,
  7661. (uint32_t) dst->ne[0], (uint32_t) dst->ne[1], (uint32_t) dst->ne[2],(uint32_t) dst->ne[3], (uint32_t) dst->nb[0] / dst_type_size, (uint32_t) dst->nb[1] / dst_type_size, (uint32_t) dst->nb[2] / dst_type_size, (uint32_t) dst->nb[3] / dst_type_size,
  7662. 0,
  7663. op_params[0], 0.0f, (int32_t)param3,
  7664. }, dryrun);
  7665. if (ctx->do_add_rms_partials) {
  7666. ctx->prealloc_size_add_rms_partials_offset += ggml_vk_rms_partials_size(ctx, src0);
  7667. ctx->do_add_rms_partials = false;
  7668. }
  7669. }
  7670. static void ggml_vk_rms_norm_back(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  7671. float * op_params = (float *)dst->op_params;
  7672. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_RMS_NORM_BACK, { (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], op_params[0], 0.0f }, dryrun);
  7673. }
  7674. static void ggml_vk_l2_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7675. float * op_params = (float *)dst->op_params;
  7676. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_L2_NORM, { (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], op_params[0], 0.0f }, dryrun);
  7677. }
  7678. static void ggml_vk_unary(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7679. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_UNARY, { (uint32_t)ggml_nelements(src0), 0, 0.0f, 0.0f }, dryrun);
  7680. }
  7681. static void ggml_vk_glu(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  7682. const float * op_params_f = (const float *)dst->op_params;
  7683. const bool swapped = (bool)dst->op_params[1];
  7684. const bool split = src1 != nullptr;
  7685. const float alpha = op_params_f[2];
  7686. const float limit = op_params_f[3];
  7687. GGML_ASSERT(ggml_is_contiguous(src0));
  7688. if (!split) {
  7689. GGML_ASSERT(src0->ne[0] / 2 == dst->ne[0]);
  7690. } else {
  7691. GGML_ASSERT(src0->ne[0] == src1->ne[0]);
  7692. GGML_ASSERT(src0->ne[0] == dst->ne[0]);
  7693. GGML_ASSERT(src0->type == src1->type);
  7694. }
  7695. const uint32_t mode = split ? 2 : (swapped ? 1 : 0);
  7696. ggml_vk_op_f32<vk_op_glu_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_GLU,
  7697. {
  7698. (uint32_t)ggml_nelements(dst),
  7699. (uint32_t)src0->ne[0],
  7700. (uint32_t)dst->ne[0],
  7701. mode,
  7702. alpha,
  7703. limit
  7704. }, dryrun);
  7705. }
  7706. static void ggml_vk_diag_mask_inf(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7707. int32_t * op_params = (int32_t *)dst->op_params;
  7708. ggml_vk_op_f32<vk_op_diag_mask_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_DIAG_MASK_INF, { (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], op_params[0] }, dryrun);
  7709. }
  7710. static void ggml_vk_soft_max(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, ggml_tensor * dst, bool dryrun = false) {
  7711. float * op_params = (float *)dst->op_params;
  7712. float scale = op_params[0];
  7713. float max_bias = op_params[1];
  7714. const uint32_t ncols = (uint32_t)src0->ne[0];
  7715. const uint32_t nrows_x = (uint32_t)ggml_nrows(src0);
  7716. const uint32_t nrows_y = (uint32_t)src0->ne[1];
  7717. const uint32_t ne12 = src1 ? (uint32_t)(src1->ne[2]) : 0u;
  7718. const uint32_t ne13 = src1 ? (uint32_t)(src1->ne[3]) : 0u;
  7719. const uint32_t nb11 = src1 ? (uint32_t)(src1->nb[1] / src1->nb[0]) : 0u;
  7720. const uint32_t nb12 = src1 ? (uint32_t)(src1->nb[2] / src1->nb[0]) : 0u;
  7721. const uint32_t nb13 = src1 ? (uint32_t)(src1->nb[3] / src1->nb[0]) : 0u;
  7722. const uint32_t n_head_kv = src0->ne[2];
  7723. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head_kv));
  7724. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  7725. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  7726. ggml_vk_op_f32<vk_op_soft_max_push_constants>(ctx, subctx, src0, src1, src2, dst, GGML_OP_SOFT_MAX, {
  7727. ncols,
  7728. src1 != nullptr ? nrows_y : (uint32_t)0,
  7729. (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], (uint32_t)src0->ne[2],
  7730. ne12, ne13,
  7731. nb11, nb12, nb13,
  7732. scale, max_bias,
  7733. m0, m1,
  7734. n_head_log2,
  7735. nrows_x,
  7736. src2 != nullptr
  7737. }, dryrun);
  7738. }
  7739. static void ggml_vk_soft_max_back(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  7740. float * op_params = (float *)dst->op_params;
  7741. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_SOFT_MAX_BACK, { (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], op_params[0], op_params[1] }, dryrun);
  7742. }
  7743. static void ggml_vk_rope(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, ggml_tensor * dst, bool backprop, bool dryrun = false) {
  7744. const int n_dims = ((int32_t *) dst->op_params)[1];
  7745. const int mode = ((int32_t *) dst->op_params)[2];
  7746. // const int n_ctx = ((int32_t *) dst->op_params)[3];
  7747. const int n_ctx_orig = ((int32_t *) dst->op_params)[4];
  7748. const float freq_base = ((float *) dst->op_params)[5];
  7749. const float freq_scale = ((float *) dst->op_params)[6];
  7750. const float ext_factor = ((float *) dst->op_params)[7];
  7751. const float attn_factor = ((float *) dst->op_params)[8];
  7752. const float beta_fast = ((float *) dst->op_params)[9];
  7753. const float beta_slow = ((float *) dst->op_params)[10];
  7754. int sections[4] {};
  7755. if (mode & GGML_ROPE_TYPE_MROPE) {
  7756. memcpy(sections, (int32_t *) dst->op_params + 11, sizeof(int)*4);
  7757. }
  7758. float corr_dims[2];
  7759. ggml_rope_yarn_corr_dims(n_dims, n_ctx_orig, freq_base, beta_fast, beta_slow, corr_dims);
  7760. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  7761. uint32_t s1 = src0->nb[1] / ggml_type_size(src0->type);
  7762. uint32_t s2 = src0->nb[2] / ggml_type_size(src0->type);
  7763. ggml_vk_op_f32<vk_op_rope_push_constants>(ctx, subctx, src0, src1, src2, dst, GGML_OP_ROPE, {
  7764. (uint32_t)src0->ne[0], (uint32_t)n_dims, freq_scale, (uint32_t)src0->ne[1],
  7765. freq_base, ext_factor, attn_factor, {corr_dims[0], corr_dims[1]}, theta_scale,
  7766. src2 != nullptr, (uint32_t)src0->ne[2], s1, s2,
  7767. { sections[0], sections[1], sections[2], sections[3] }, backprop
  7768. }, dryrun);
  7769. }
  7770. static void ggml_vk_argsort(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7771. int32_t * op_params = (int32_t *)dst->op_params;
  7772. uint32_t ncols = src0->ne[0];
  7773. ggml_vk_op_f32<vk_op_argsort_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_ARGSORT, {
  7774. ncols,
  7775. op_params[0],
  7776. }, dryrun);
  7777. }
  7778. static void ggml_vk_sum(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7779. vk_op_sum_rows_push_constants p = vk_op_sum_rows_push_constants_init(src0, dst, ggml_nelements(src0));
  7780. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SUM, p, dryrun);
  7781. }
  7782. static void ggml_vk_sum_rows(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7783. vk_op_sum_rows_push_constants p = vk_op_sum_rows_push_constants_init(src0, dst, src0->ne[0]);
  7784. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SUM_ROWS, p, dryrun);
  7785. }
  7786. static void ggml_vk_mean(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7787. vk_op_sum_rows_push_constants p = vk_op_sum_rows_push_constants_init(src0, dst, src0->ne[0]);
  7788. p.weight = 1.0f / (float)src0->ne[0];
  7789. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_MEAN, p, dryrun);
  7790. }
  7791. static void ggml_vk_argmax(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7792. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_ARGMAX, { (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], 0.0f, 0.0f }, dryrun);
  7793. }
  7794. static void ggml_vk_count_equal(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  7795. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_COUNT_EQUAL, { (uint32_t)ggml_nelements(src0), 0, 0.0f, 0.0f }, dryrun);
  7796. }
  7797. static void ggml_vk_im2col(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  7798. const int32_t s0 = dst->op_params[0];
  7799. const int32_t s1 = dst->op_params[1];
  7800. const int32_t p0 = dst->op_params[2];
  7801. const int32_t p1 = dst->op_params[3];
  7802. const int32_t d0 = dst->op_params[4];
  7803. const int32_t d1 = dst->op_params[5];
  7804. const bool is_2D = dst->op_params[6] == 1;
  7805. const uint32_t IC = src1->ne[is_2D ? 2 : 1];
  7806. const uint32_t IH = is_2D ? src1->ne[1] : 1;
  7807. const uint32_t IW = src1->ne[0];
  7808. const uint32_t KH = is_2D ? src0->ne[1] : 1;
  7809. const uint32_t KW = src0->ne[0];
  7810. const uint32_t OH = is_2D ? dst->ne[2] : 1;
  7811. const uint32_t OW = dst->ne[1];
  7812. const uint32_t offset_delta = src1->nb[is_2D ? 2 : 1] / 4; // nb is byte offset, src is type float32
  7813. const uint32_t batch_offset = src1->nb[is_2D ? 3 : 2] / 4; // nb is byte offset, src is type float32
  7814. const uint32_t pelements = OW * KW * KH;
  7815. ggml_vk_op_f32<vk_op_im2col_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_IM2COL, {
  7816. batch_offset, offset_delta,
  7817. IC, IW, IH, OW, OH, KW, KH,
  7818. pelements,
  7819. IC * KH * KW,
  7820. s0, s1, p0, p1, d0, d1,
  7821. }, dryrun);
  7822. }
  7823. static void ggml_vk_timestep_embedding(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7824. const uint32_t dim = dst->op_params[0];
  7825. const uint32_t max_period = dst->op_params[1];
  7826. const uint32_t nb1 = dst->nb[1] / ggml_type_size(dst->type);
  7827. ggml_vk_op_f32<vk_op_timestep_embedding_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_TIMESTEP_EMBEDDING, {
  7828. nb1, dim, max_period,
  7829. }, dryrun);
  7830. }
  7831. static void ggml_vk_conv_transpose_1d(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  7832. // src0: (K, Cout, Cin, 1) -- kernel
  7833. // src1: (L, Cin, 1, 1) -- input
  7834. // dst: (*, Cout, 1, 1)
  7835. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7836. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  7837. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7838. GGML_TENSOR_BINARY_OP_LOCALS
  7839. GGML_ASSERT(nb00 == sizeof(float));
  7840. GGML_ASSERT(nb10 == sizeof(float));
  7841. const int32_t s0 = dst->op_params[0];
  7842. vk_op_conv_transpose_1d_push_constants p{};
  7843. p.Cout = static_cast<uint32_t>(ne01);
  7844. p.Cin = static_cast<uint32_t>(ne02);
  7845. p.K = static_cast<uint32_t>(ne00);
  7846. p.L = static_cast<uint32_t>(ne10);
  7847. p.KL = static_cast<uint32_t>(ne0);
  7848. p.nb01 = static_cast<uint32_t>(nb01 / nb00);
  7849. p.nb02 = static_cast<uint32_t>(nb02 / nb00);
  7850. p.nb11 = static_cast<uint32_t>(nb11 / nb10);
  7851. p.nb1 = static_cast<uint32_t>(nb1 / nb0);
  7852. p.s0 = static_cast<uint32_t>(s0);
  7853. ggml_vk_op_f32(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_CONV_TRANSPOSE_1D, std::move(p), dryrun);
  7854. }
  7855. static void ggml_vk_pool_2d(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7856. uint32_t op = static_cast<uint32_t>(dst->op_params[0]);
  7857. const int32_t k1 = dst->op_params[1];
  7858. const int32_t k0 = dst->op_params[2];
  7859. const int32_t s1 = dst->op_params[3];
  7860. const int32_t s0 = dst->op_params[4];
  7861. const int32_t p1 = dst->op_params[5];
  7862. const int32_t p0 = dst->op_params[6];
  7863. const uint32_t IH = src0->ne[1];
  7864. const uint32_t IW = src0->ne[0];
  7865. const uint32_t N = dst->ne[3];
  7866. const uint32_t OC = dst->ne[2];
  7867. const uint32_t OH = dst->ne[1];
  7868. const uint32_t OW = dst->ne[0];
  7869. const uint32_t parallel_elements = N * OC * OH * OW;
  7870. ggml_vk_op_f32<vk_op_pool2d_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_POOL_2D, {
  7871. IW, IH, OW, OH, OC,
  7872. parallel_elements,
  7873. op,
  7874. k0, k1, s0, s1, p0, p1,
  7875. }, dryrun);
  7876. }
  7877. static void ggml_vk_conv_2d(ggml_backend_vk_context * ctx, vk_context & subctx, const ggml_tensor * src0,
  7878. const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  7879. GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16);
  7880. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  7881. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  7882. GGML_TENSOR_BINARY_OP_LOCALS
  7883. GGML_ASSERT(nb00 == sizeof(float) || nb00 == sizeof(ggml_fp16_t));
  7884. GGML_ASSERT(nb10 == sizeof(float));
  7885. GGML_ASSERT(nb0 == sizeof(float));
  7886. vk_op_conv2d_push_constants p{};
  7887. p.Cout = static_cast<uint32_t>(ne03);
  7888. p.Cin = static_cast<uint32_t>(ne02);
  7889. p.N = static_cast<uint32_t>(ne13);
  7890. p.KW = static_cast<uint32_t>(ne00);
  7891. p.KH = static_cast<uint32_t>(ne01);
  7892. p.W = static_cast<uint32_t>(ne10);
  7893. p.H = static_cast<uint32_t>(ne11);
  7894. p.OW = static_cast<uint32_t>(ne0);
  7895. p.OH = static_cast<uint32_t>(ne1);
  7896. p.s0 = static_cast<uint32_t>(dst->op_params[0]);
  7897. p.s1 = static_cast<uint32_t>(dst->op_params[1]);
  7898. p.p0 = static_cast<uint32_t>(dst->op_params[2]);
  7899. p.p1 = static_cast<uint32_t>(dst->op_params[3]);
  7900. p.d0 = static_cast<uint32_t>(dst->op_params[4]);
  7901. p.d1 = static_cast<uint32_t>(dst->op_params[5]);
  7902. p.nb01 = static_cast<uint32_t>(nb01 / nb00);
  7903. p.nb02 = static_cast<uint32_t>(nb02 / nb00);
  7904. p.nb03 = static_cast<uint32_t>(nb03 / nb00);
  7905. p.nb11 = static_cast<uint32_t>(nb11 / nb10);
  7906. p.nb12 = static_cast<uint32_t>(nb12 / nb10);
  7907. p.nb13 = static_cast<uint32_t>(nb13 / nb10);
  7908. p.nb1 = static_cast<uint32_t>(nb1 / nb0);
  7909. p.nb2 = static_cast<uint32_t>(nb2 / nb0);
  7910. p.nb3 = static_cast<uint32_t>(nb3 / nb0);
  7911. GGML_ASSERT(ne03 == ne2);
  7912. GGML_ASSERT(ne02 == ne12);
  7913. ggml_vk_op_f32(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_CONV_2D, std::move(p), dryrun);
  7914. }
  7915. static void ggml_vk_conv_2d_dw(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  7916. vk_op_conv2d_dw_push_constants p{};
  7917. p.ne = ggml_nelements(dst);
  7918. p.channels = dst->ne[2];
  7919. p.batches = dst->ne[3];
  7920. p.dst_w = dst->ne[0];
  7921. p.dst_h = dst->ne[1];
  7922. p.src_w = src1->ne[0];
  7923. p.src_h = src1->ne[1];
  7924. p.knl_w = src0->ne[0];
  7925. p.knl_h = src0->ne[1];
  7926. p.stride_x = dst->op_params[0];
  7927. p.stride_y = dst->op_params[1];
  7928. p.pad_x = dst->op_params[2];
  7929. p.pad_y = dst->op_params[3];
  7930. p.dilation_x = dst->op_params[4];
  7931. p.dilation_y = dst->op_params[5];
  7932. GGML_ASSERT(src0->ne[3] == p.channels);
  7933. GGML_ASSERT(src1->ne[3] == p.batches);
  7934. ggml_vk_op_f32(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_CONV_2D_DW, std::move(p), dryrun);
  7935. }
  7936. static void ggml_vk_leaky_relu(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7937. const float * op_params = (const float *)dst->op_params;
  7938. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_LEAKY_RELU, { (uint32_t)ggml_nelements(src0), 0, op_params[0], 0.0f }, dryrun);
  7939. }
  7940. #ifdef GGML_VULKAN_RUN_TESTS
  7941. static void ggml_vk_print_matrix_area(const void * data, ggml_type type, int ne0, int ne1, int i0, int i1, int i2) {
  7942. if (type != GGML_TYPE_F32 && type != GGML_TYPE_F16) {
  7943. return;
  7944. }
  7945. i0 = std::max(i0, 5);
  7946. i1 = std::max(i1, 5);
  7947. i2 = std::max(i2, 0);
  7948. fprintf(stderr, " ");
  7949. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  7950. fprintf(stderr, "%7d ", idx1);
  7951. }
  7952. fprintf(stderr, "\n");
  7953. for (int idx0 = i0 - 5; idx0 < i0 + 5; idx0++) {
  7954. fprintf(stderr, "%7d: ", idx0);
  7955. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  7956. if (idx0 >= 0 && idx0 < ne0 && idx1 >= 0 && idx1 < ne1) {
  7957. float val;
  7958. if (type == GGML_TYPE_F32) {
  7959. val = *((const float *) data + i2*ne1*ne0 + idx1*ne0 + idx0);
  7960. } else if (type == GGML_TYPE_F16) {
  7961. val = ggml_fp16_to_fp32(*((const ggml_fp16_t *) data + i2*ne1*ne0 + idx1*ne0 + idx0));
  7962. } else {
  7963. GGML_ABORT("fatal error");
  7964. }
  7965. fprintf(stderr, "% 7.2f ", val);
  7966. } else {
  7967. fprintf(stderr, " ");
  7968. }
  7969. }
  7970. fprintf(stderr, "\n");
  7971. }
  7972. }
  7973. template <typename X_TYPE, typename Y_TYPE>
  7974. static void ggml_vk_test_matmul(ggml_backend_vk_context * ctx, size_t m, size_t n, size_t k, size_t batch, size_t num_it, int split_k, int shader_size) {
  7975. VK_LOG_DEBUG("ggml_vk_test_matmul(" << m << ", " << n << ", " << k << ", " << batch << ", " << num_it << ", " << split_k << ", " << shader_size << ")");
  7976. const size_t x_ne = m * k * batch;
  7977. const size_t y_ne = k * n * batch;
  7978. const size_t d_ne = m * n * batch;
  7979. vk_pipeline p;
  7980. std::string shname;
  7981. if (shader_size == 0) {
  7982. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  7983. p = ctx->device->pipeline_matmul_f32->a_s;
  7984. shname = "F32_ALIGNED_S";
  7985. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  7986. p = ctx->device->pipeline_matmul_f32_f16->a_s;
  7987. shname = "F32_F16_ALIGNED_S";
  7988. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  7989. p = ctx->device->pipeline_matmul_f16_f32.f32acc->a_s;
  7990. shname = "F16_F32_ALIGNED_S";
  7991. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  7992. p = ctx->device->pipeline_matmul_f16.f32acc->a_s;
  7993. shname = "F16_ALIGNED_S";
  7994. } else {
  7995. GGML_ABORT("fatal error");
  7996. }
  7997. } else if (shader_size == 1) {
  7998. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  7999. p = ctx->device->pipeline_matmul_f32->a_m;
  8000. shname = "F32_ALIGNED_M";
  8001. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  8002. p = ctx->device->pipeline_matmul_f32_f16->a_m;
  8003. shname = "F32_F16_ALIGNED_M";
  8004. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  8005. p = ctx->device->pipeline_matmul_f16_f32.f32acc->a_m;
  8006. shname = "F16_F32_ALIGNED_M";
  8007. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  8008. p = ctx->device->pipeline_matmul_f16.f32acc->a_m;
  8009. shname = "F16_ALIGNED_M";
  8010. } else {
  8011. GGML_ABORT("fatal error");
  8012. }
  8013. } else if (shader_size == 2) {
  8014. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  8015. p = ctx->device->pipeline_matmul_f32->a_l;
  8016. shname = "F32_ALIGNED_L";
  8017. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  8018. p = ctx->device->pipeline_matmul_f32_f16->a_l;
  8019. shname = "F32_F16_ALIGNED_L";
  8020. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  8021. p = ctx->device->pipeline_matmul_f16_f32.f32acc->a_l;
  8022. shname = "F16_F32_ALIGNED_L";
  8023. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  8024. p = ctx->device->pipeline_matmul_f16.f32acc->a_l;
  8025. shname = "F16_ALIGNED_L";
  8026. } else {
  8027. GGML_ABORT("fatal error");
  8028. }
  8029. } else {
  8030. GGML_ASSERT(0);
  8031. }
  8032. const size_t kpad = ggml_vk_align_size(k, p->align);
  8033. if (k != kpad) {
  8034. if (shader_size == 0) {
  8035. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  8036. p = ctx->device->pipeline_matmul_f32->s;
  8037. shname = "F32_S";
  8038. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  8039. p = ctx->device->pipeline_matmul_f32_f16->s;
  8040. shname = "F32_F16_S";
  8041. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  8042. p = ctx->device->pipeline_matmul_f16_f32.f32acc->s;
  8043. shname = "F16_F32_S";
  8044. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  8045. p = ctx->device->pipeline_matmul_f16.f32acc->s;
  8046. shname = "F16_S";
  8047. }
  8048. } else if (shader_size == 1) {
  8049. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  8050. p = ctx->device->pipeline_matmul_f32->m;
  8051. shname = "F32_M";
  8052. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  8053. p = ctx->device->pipeline_matmul_f32_f16->m;
  8054. shname = "F32_F16_M";
  8055. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  8056. p = ctx->device->pipeline_matmul_f16_f32.f32acc->m;
  8057. shname = "F16_F32_M";
  8058. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  8059. p = ctx->device->pipeline_matmul_f16.f32acc->m;
  8060. shname = "F16_M";
  8061. }
  8062. } else if (shader_size == 2) {
  8063. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  8064. p = ctx->device->pipeline_matmul_f32->l;
  8065. shname = "F32_L";
  8066. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  8067. p = ctx->device->pipeline_matmul_f32_f16->l;
  8068. shname = "F32_F16_L";
  8069. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  8070. p = ctx->device->pipeline_matmul_f16_f32.f32acc->l;
  8071. shname = "F16_F32_L";
  8072. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  8073. p = ctx->device->pipeline_matmul_f16.f32acc->l;
  8074. shname = "F16_L";
  8075. }
  8076. }
  8077. }
  8078. ggml_pipeline_request_descriptor_sets(ctx, p, num_it);
  8079. if (split_k > 1) {
  8080. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_matmul_split_k_reduce, num_it);
  8081. if (ctx->prealloc_split_k == nullptr || ctx->prealloc_split_k->size < sizeof(float) * d_ne * split_k) {
  8082. // Resize buffer
  8083. if (ctx->prealloc_split_k != nullptr) {
  8084. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  8085. }
  8086. ctx->prealloc_split_k = ggml_vk_create_buffer_check(ctx->device, sizeof(float) * d_ne * split_k, {vk::MemoryPropertyFlagBits::eDeviceLocal});
  8087. }
  8088. }
  8089. if (ctx->device->need_compiles) {
  8090. ggml_vk_load_shaders(ctx->device);
  8091. }
  8092. ggml_pipeline_allocate_descriptor_sets(ctx);
  8093. vk_buffer d_X = ggml_vk_create_buffer_check(ctx->device, sizeof(X_TYPE) * x_ne, {vk::MemoryPropertyFlagBits::eDeviceLocal});
  8094. vk_buffer d_Y = ggml_vk_create_buffer_check(ctx->device, sizeof(Y_TYPE) * y_ne, {vk::MemoryPropertyFlagBits::eDeviceLocal});
  8095. vk_buffer d_D = ggml_vk_create_buffer_check(ctx->device, sizeof(float) * d_ne, {vk::MemoryPropertyFlagBits::eDeviceLocal});
  8096. X_TYPE* x = (X_TYPE *) malloc(sizeof(X_TYPE) * x_ne);
  8097. Y_TYPE* y = (Y_TYPE *) malloc(sizeof(Y_TYPE) * y_ne);
  8098. float* d = (float *) malloc(sizeof(float) * d_ne);
  8099. for (size_t i = 0; i < x_ne; i++) {
  8100. if (std::is_same<float, X_TYPE>()) {
  8101. x[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  8102. // x[i] = 1.0f;
  8103. // x[i] = i + 1;
  8104. // x[i] = (i % k == i / k) ? 1.0f : 0.0f;
  8105. } else if (std::is_same<ggml_fp16_t, X_TYPE>()) {
  8106. x[i] = ggml_fp32_to_fp16((rand() / (float)RAND_MAX) * 2.0f - 1.0f);
  8107. // x[i] = ggml_fp32_to_fp16(1.0f);
  8108. // x[i] = ggml_fp32_to_fp16(i + 1);
  8109. // x[i] = ggml_fp32_to_fp16((i % k == i / k) ? 1.0f : 0.0f);
  8110. } else {
  8111. GGML_ABORT("fatal error");
  8112. }
  8113. }
  8114. for (size_t i = 0; i < y_ne; i++) {
  8115. if (std::is_same<float, Y_TYPE>()) {
  8116. y[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  8117. // y[i] = (i % k == i / k) ? 1.0f : 0.0f;
  8118. // y[i] = i + 1;
  8119. } else if (std::is_same<ggml_fp16_t, Y_TYPE>()) {
  8120. y[i] = ggml_fp32_to_fp16((rand() / (float)RAND_MAX) * 2.0f - 1.0f);
  8121. // y[i] = ggml_fp32_to_fp16((i % k == i / k) ? 1.0f : 0.0f);
  8122. // y[i] = ggml_fp32_to_fp16(i + 1);
  8123. } else {
  8124. GGML_ABORT("fatal error");
  8125. }
  8126. }
  8127. ggml_vk_buffer_write(d_X, 0, x, sizeof(X_TYPE) * k * m * batch);
  8128. ggml_vk_buffer_write(d_Y, 0, y, sizeof(Y_TYPE) * k * n * batch);
  8129. vk_context subctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  8130. ggml_vk_ctx_begin(ctx->device, subctx);
  8131. for (size_t i = 0; i < num_it; i++) {
  8132. ggml_vk_matmul(
  8133. ctx, subctx, p, ggml_vk_subbuffer(d_X), ggml_vk_subbuffer(d_Y), ggml_vk_subbuffer(d_D), ggml_vk_subbuffer(ctx->prealloc_split_k),
  8134. m, n, k,
  8135. k, k, m, k*m, k*n, m*n,
  8136. split_k, batch, batch, batch, 1, 1, n
  8137. );
  8138. }
  8139. ggml_vk_ctx_end(subctx);
  8140. auto begin = std::chrono::high_resolution_clock::now();
  8141. ggml_vk_submit(subctx, ctx->fence);
  8142. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_matmul waitForFences");
  8143. ctx->device->device.resetFences({ ctx->fence });
  8144. ggml_vk_queue_command_pools_cleanup(ctx->device);
  8145. auto end = std::chrono::high_resolution_clock::now();
  8146. double time = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  8147. // copy dst to host
  8148. ggml_vk_buffer_read(d_D, 0, d, sizeof(float) * d_ne);
  8149. float * d_chk = (float *) malloc(sizeof(float) * d_ne);
  8150. ggml_init_params iparams = {
  8151. /*.mem_size =*/ 1024*1024*1024,
  8152. /*.mem_buffer =*/ NULL,
  8153. /*.no_alloc =*/ true,
  8154. };
  8155. ggml_context * ggml_ctx = ggml_init(iparams);
  8156. ggml_type src0_type;
  8157. ggml_type src1_type;
  8158. if (std::is_same<float, X_TYPE>()) {
  8159. src0_type = GGML_TYPE_F32;
  8160. } else if (std::is_same<ggml_fp16_t, X_TYPE>()) {
  8161. src0_type = GGML_TYPE_F16;
  8162. } else {
  8163. GGML_ABORT("fatal error");
  8164. }
  8165. if (std::is_same<float, Y_TYPE>()) {
  8166. src1_type = GGML_TYPE_F32;
  8167. } else if (std::is_same<ggml_fp16_t, Y_TYPE>()) {
  8168. src1_type = GGML_TYPE_F16;
  8169. } else {
  8170. GGML_ABORT("fatal error");
  8171. }
  8172. ggml_tensor * src0_ggml = ggml_new_tensor_3d(ggml_ctx, src0_type, k, m, batch);
  8173. ggml_tensor * src1_ggml = ggml_new_tensor_3d(ggml_ctx, src1_type, k, n, batch);
  8174. ggml_tensor * tensor_ggml = ggml_mul_mat(ggml_ctx, src0_ggml, src1_ggml);
  8175. src0_ggml->data = x;
  8176. src1_ggml->data = y;
  8177. tensor_ggml->data = d_chk;
  8178. ggml_cgraph * cgraph = ggml_new_graph(ggml_ctx);
  8179. ggml_build_forward_expand(cgraph, tensor_ggml);
  8180. ggml_graph_compute_with_ctx(ggml_ctx, cgraph, 1);
  8181. ggml_free(ggml_ctx);
  8182. double avg_err = 0.0;
  8183. int first_err_n = -1;
  8184. int first_err_m = -1;
  8185. int first_err_b = -1;
  8186. for (size_t i = 0; i < m*n*batch; i++) {
  8187. double err = std::fabs(d[i] - d_chk[i]);
  8188. avg_err += err;
  8189. if ((err > 0.05f || std::isnan(err)) && first_err_n == -1) {
  8190. first_err_b = i / (m * n);
  8191. first_err_n = (i % (m * n)) / m;
  8192. first_err_m = (i % (m * n)) % m;
  8193. }
  8194. }
  8195. avg_err /= m * n;
  8196. double tflops = 2.0*m*n*k*batch*num_it / (time / 1000.0) / (1000.0*1000.0*1000.0*1000.0);
  8197. std::cerr << "TEST " << shname << " m=" << m << " n=" << n << " k=" << k << " batch=" << batch << " split_k=" << split_k << " matmul " << time / num_it << "ms " << tflops << " TFLOPS avg_err=" << avg_err << std::endl;
  8198. if (avg_err > 0.1 || std::isnan(avg_err)) {
  8199. std::cerr << "m = " << first_err_m << " n = " << first_err_n << " b = " << first_err_b << std::endl;
  8200. std::cerr << "Actual result: " << std::endl << std::endl;
  8201. ggml_vk_print_matrix_area(d, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  8202. std::cerr << "Expected result: " << std::endl << std::endl;
  8203. ggml_vk_print_matrix_area(d_chk, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  8204. if (split_k > 1) {
  8205. float * split_k_buf = (float *) malloc(sizeof(float) * d_ne * split_k);
  8206. ggml_vk_buffer_read(ctx->prealloc_split_k, 0, split_k_buf, sizeof(float) * d_ne * split_k);
  8207. std::cerr << "d_buf0: " << std::endl << std::endl;
  8208. ggml_vk_print_matrix_area(split_k_buf, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  8209. std::cerr << "d_buf1: " << std::endl << std::endl;
  8210. ggml_vk_print_matrix_area(split_k_buf + d_ne, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  8211. std::cerr << "d_buf2: " << std::endl << std::endl;
  8212. ggml_vk_print_matrix_area(split_k_buf + 2 * d_ne, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  8213. std::cerr << "d_buf3: " << std::endl << std::endl;
  8214. ggml_vk_print_matrix_area(split_k_buf + 3 * d_ne, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  8215. free(split_k_buf);
  8216. }
  8217. }
  8218. free(d_chk);
  8219. ggml_vk_command_pool_cleanup(ctx->device, ctx->compute_cmd_pool);
  8220. ggml_vk_command_pool_cleanup(ctx->device, ctx->transfer_cmd_pool);
  8221. ggml_vk_destroy_buffer(d_X);
  8222. ggml_vk_destroy_buffer(d_Y);
  8223. ggml_vk_destroy_buffer(d_D);
  8224. free(x);
  8225. free(y);
  8226. free(d);
  8227. }
  8228. static void ggml_vk_print_tensor_area(const ggml_tensor * tensor, int i0, int i1, int i2, int i3) {
  8229. if (tensor->type != GGML_TYPE_F32 && tensor->type != GGML_TYPE_F16) {
  8230. return;
  8231. }
  8232. i0 = std::max(i0, 5);
  8233. i1 = std::max(i1, 5);
  8234. i2 = std::max(i2, 0);
  8235. i3 = std::max(i3, 0);
  8236. fprintf(stderr, " ");
  8237. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  8238. fprintf(stderr, "%7d ", idx1);
  8239. }
  8240. fprintf(stderr, "\n");
  8241. for (int idx0 = i0 - 5; idx0 < i0 + 5; idx0++) {
  8242. fprintf(stderr, "%7d: ", idx0);
  8243. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  8244. if (idx0 >= 0 && idx0 < tensor->ne[0] && idx1 >= 0 && idx1 < tensor->ne[1] && i2 >= 0 && i2 < tensor->ne[2] && i3 >= 0 && i3 < tensor->ne[3]) {
  8245. float val;
  8246. if (tensor->type == GGML_TYPE_F32) {
  8247. val = *(float *) ((char *) tensor->data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]);
  8248. } else if (tensor->type == GGML_TYPE_F16) {
  8249. val = ggml_fp16_to_fp32(*(ggml_fp16_t *) ((char *) tensor->data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]));
  8250. } else {
  8251. GGML_ABORT("fatal error");
  8252. }
  8253. fprintf(stderr, "% 7.2f ", val);
  8254. } else {
  8255. fprintf(stderr, " ");
  8256. }
  8257. }
  8258. fprintf(stderr, "\n");
  8259. }
  8260. }
  8261. static void ggml_vk_quantize_data(const float * from, void * to, size_t ne, ggml_type quant) {
  8262. ggml_quantize_chunk(quant, from, to, 0, 1, ne, nullptr);
  8263. }
  8264. static void ggml_vk_dequantize_data(const void * from, float * to, size_t ne, ggml_type quant) {
  8265. if (quant == GGML_TYPE_F32) {
  8266. memcpy(to, from, sizeof(float) * ne);
  8267. return;
  8268. }
  8269. const auto * tt = ggml_get_type_traits(quant);
  8270. ggml_to_float_t dequant_fn = tt->to_float;
  8271. dequant_fn(from, to, ne);
  8272. }
  8273. static void ggml_vk_test_dequant(ggml_backend_vk_context * ctx, size_t ne, ggml_type quant) {
  8274. VK_LOG_DEBUG("ggml_vk_test_dequant(" << ne << ")");
  8275. const size_t x_sz = sizeof(float) * ne;
  8276. const size_t x_sz_f16 = sizeof(ggml_fp16_t) * ne;
  8277. const size_t qx_sz = ne * ggml_type_size(quant)/ggml_blck_size(quant);
  8278. float * x = (float *) malloc(x_sz);
  8279. void * qx = malloc(qx_sz);
  8280. vk_buffer qx_buf = ggml_vk_create_buffer_check(ctx->device, qx_sz, {vk::MemoryPropertyFlagBits::eDeviceLocal});
  8281. vk_buffer x_buf = ggml_vk_create_buffer_check(ctx->device, x_sz_f16, {vk::MemoryPropertyFlagBits::eDeviceLocal});
  8282. float * x_ref = (float *) malloc(x_sz);
  8283. ggml_fp16_t * x_chk = (ggml_fp16_t *) malloc(x_sz_f16);
  8284. for (size_t i = 0; i < ne; i++) {
  8285. x[i] = rand() / (float)RAND_MAX;
  8286. }
  8287. vk_pipeline p = ggml_vk_get_to_fp16(ctx, quant);
  8288. ggml_vk_quantize_data(x, qx, ne, quant);
  8289. ggml_vk_dequantize_data(qx, x_ref, ne, quant);
  8290. ggml_pipeline_request_descriptor_sets(ctx, p, 1);
  8291. if (ctx->device->need_compiles) {
  8292. ggml_vk_load_shaders(ctx->device);
  8293. }
  8294. ggml_pipeline_allocate_descriptor_sets(ctx);
  8295. ggml_vk_buffer_write(qx_buf, 0, qx, qx_sz);
  8296. vk_context subctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  8297. ggml_vk_ctx_begin(ctx->device, subctx);
  8298. const std::vector<uint32_t> pc = { 1, (uint32_t)ne, (uint32_t)ne, (uint32_t)ne, (uint32_t)ne };
  8299. ggml_vk_dispatch_pipeline(ctx, subctx, p, { vk_subbuffer{ qx_buf, 0, qx_sz }, vk_subbuffer{ x_buf, 0, x_sz_f16 } }, pc, { (uint32_t)ne, 1, 1});
  8300. ggml_vk_ctx_end(subctx);
  8301. auto begin = std::chrono::high_resolution_clock::now();
  8302. ggml_vk_submit(subctx, ctx->fence);
  8303. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_dequant waitForFences");
  8304. ctx->device->device.resetFences({ ctx->fence });
  8305. ggml_vk_queue_command_pools_cleanup(ctx->device);
  8306. auto end = std::chrono::high_resolution_clock::now();
  8307. double ms_dequant = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  8308. ggml_vk_buffer_read(x_buf, 0, x_chk, x_sz_f16);
  8309. int first_err = -1;
  8310. double avg_err = 0.0;
  8311. for (size_t i = 0; i < ne; i++) {
  8312. double error = std::fabs(x_ref[i] - ggml_fp16_to_fp32(x_chk[i]));
  8313. avg_err += error;
  8314. if (first_err < 0 && error > 0.05) {
  8315. first_err = i;
  8316. }
  8317. }
  8318. avg_err /= ne;
  8319. std::cerr << "TEST DEQUANT " << ggml_type_name(quant) << " time=" << ms_dequant << "ms avg_err=" << avg_err << std::endl;
  8320. if (avg_err > 0.1) {
  8321. std::cerr << "first_error = " << first_err << std::endl;
  8322. std::cerr << "Actual result: " << std::endl << std::endl;
  8323. for (int i = std::max(0, first_err - 5); i < std::min((int)ne, first_err + 5); i++) {
  8324. std::cerr << ggml_fp16_to_fp32(x_chk[i]) << ", ";
  8325. }
  8326. std::cerr << std::endl << "Expected result: " << std::endl << std::endl;
  8327. for (int i = std::max(0, first_err - 5); i < std::min((int)ne, first_err + 5); i++) {
  8328. std::cerr << x_ref[i] << ", ";
  8329. }
  8330. std::cerr << std::endl;
  8331. }
  8332. ggml_vk_destroy_buffer(x_buf);
  8333. ggml_vk_destroy_buffer(qx_buf);
  8334. free(x);
  8335. free(qx);
  8336. free(x_ref);
  8337. free(x_chk);
  8338. }
  8339. // This does not work without ggml q8_1 quantization support
  8340. //
  8341. // typedef uint16_t ggml_half;
  8342. // typedef uint32_t ggml_half2;
  8343. //
  8344. // #define QK8_1 32
  8345. // typedef struct {
  8346. // union {
  8347. // struct {
  8348. // ggml_half d; // delta
  8349. // ggml_half s; // d * sum(qs[i])
  8350. // } GGML_COMMON_AGGR_S;
  8351. // ggml_half2 ds;
  8352. // } GGML_COMMON_AGGR_U;
  8353. // int8_t qs[QK8_1]; // quants
  8354. // } block_q8_1;
  8355. //
  8356. // static void ggml_vk_test_quantize(ggml_backend_vk_context * ctx, size_t ne, ggml_type quant) {
  8357. // VK_LOG_DEBUG("ggml_vk_test_quantize(" << ne << ")");
  8358. // GGML_ASSERT(quant == GGML_TYPE_Q8_1);
  8359. //
  8360. // const size_t x_sz = sizeof(float) * ne;
  8361. // const size_t qx_sz = ne * ggml_type_size(quant)/ggml_blck_size(quant);
  8362. // float * x = (float *) malloc(x_sz);
  8363. // block_q8_1 * qx = (block_q8_1 *)malloc(qx_sz);
  8364. // block_q8_1 * qx_res = (block_q8_1 *)malloc(qx_sz);
  8365. // vk_buffer x_buf = ggml_vk_create_buffer_check(ctx->device, x_sz, {vk::MemoryPropertyFlagBits::eDeviceLocal});
  8366. // vk_buffer qx_buf = ggml_vk_create_buffer_check(ctx->device, qx_sz, {vk::MemoryPropertyFlagBits::eDeviceLocal});
  8367. //
  8368. // for (size_t i = 0; i < ne; i++) {
  8369. // x[i] = rand() / (float)RAND_MAX;
  8370. // }
  8371. //
  8372. // vk_pipeline p = ggml_vk_get_quantize_pipeline(ctx, quant);
  8373. //
  8374. // ggml_pipeline_request_descriptor_sets(ctx, p, 1);
  8375. //
  8376. // if (ctx->device->need_compiles) {
  8377. // ggml_vk_load_shaders(ctx->device);
  8378. // }
  8379. //
  8380. // ggml_pipeline_allocate_descriptor_sets(ctx);
  8381. //
  8382. // ggml_vk_buffer_write(x_buf, 0, x, x_sz);
  8383. //
  8384. // vk_context subctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  8385. // ggml_vk_ctx_begin(ctx->device, subctx);
  8386. // ggml_vk_quantize_q8_1(ctx, subctx, ggml_vk_subbuffer(x_buf), ggml_vk_subbuffer(qx_buf), ne);
  8387. // ggml_vk_ctx_end(subctx);
  8388. //
  8389. // auto begin = std::chrono::high_resolution_clock::now();
  8390. //
  8391. // ggml_vk_submit(subctx, ctx->fence);
  8392. // VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_quantize waitForFences");
  8393. // ctx->device->device.resetFences({ ctx->fence });
  8394. // ggml_vk_queue_command_pools_cleanup(ctx->device);
  8395. //
  8396. // auto end = std::chrono::high_resolution_clock::now();
  8397. //
  8398. // double ms_quant = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  8399. // ggml_vk_buffer_read(qx_buf, 0, qx, qx_sz);
  8400. //
  8401. // ggml_vk_quantize_data(x, qx_res, ne, quant);
  8402. //
  8403. // int first_err = -1;
  8404. //
  8405. // for (size_t i = 0; i < ne / 32; i++) {
  8406. // double error = std::fabs(ggml_fp16_to_fp32(qx_res[i].GGML_COMMON_AGGR_U.GGML_COMMON_AGGR_S.d) - ggml_fp16_to_fp32(qx[i].GGML_COMMON_AGGR_U.GGML_COMMON_AGGR_S.d));
  8407. //
  8408. // if (first_err < 0 && error > 0.1) {
  8409. // first_err = i;
  8410. // }
  8411. //
  8412. // error = std::fabs(ggml_fp16_to_fp32(qx_res[i].GGML_COMMON_AGGR_U.GGML_COMMON_AGGR_S.s) - ggml_fp16_to_fp32(qx[i].GGML_COMMON_AGGR_U.GGML_COMMON_AGGR_S.s));
  8413. //
  8414. // if (first_err < 0 && error > 0.1) {
  8415. // first_err = i;
  8416. // }
  8417. //
  8418. // for (size_t j = 0; j < 32; j++) {
  8419. // uint64_t error = std::abs(qx_res[i].qs[j] - qx[i].qs[j]);
  8420. //
  8421. // if (first_err < 0 && error > 1) {
  8422. // first_err = i;
  8423. // }
  8424. // }
  8425. // }
  8426. //
  8427. // std::cerr << "TEST QUANTIZE " << ggml_type_name(quant) << " time=" << ms_quant << "ms " << (first_err == -1 ? "CORRECT" : "INCORRECT") << std::endl;
  8428. //
  8429. // if (first_err != -1) {
  8430. // std::cerr << "first_error = " << first_err << std::endl;
  8431. // std::cerr << "Actual result: " << std::endl << std::endl;
  8432. // std::cout << "d=" << ggml_fp16_to_fp32(qx[first_err].GGML_COMMON_AGGR_U.GGML_COMMON_AGGR_S.d) << " s=" << ggml_fp16_to_fp32(qx[first_err].GGML_COMMON_AGGR_U.GGML_COMMON_AGGR_S.s) << " ";
  8433. // for (size_t j = 0; j < 32; j++) {
  8434. // std::cout << " qs" << j << "=" << (uint32_t)qx[first_err].qs[j] << " ";
  8435. // }
  8436. // std::cerr << std::endl << std::endl << "Expected result: " << std::endl << std::endl;
  8437. // std::cout << "d=" << ggml_fp16_to_fp32(qx_res[first_err].GGML_COMMON_AGGR_U.GGML_COMMON_AGGR_S.d) << " s=" << ggml_fp16_to_fp32(qx_res[first_err].GGML_COMMON_AGGR_U.GGML_COMMON_AGGR_S.s) << " ";
  8438. // for (size_t j = 0; j < 32; j++) {
  8439. // std::cout << " qs" << j << "=" << (uint32_t)qx_res[first_err].qs[j] << " ";
  8440. // }
  8441. // std::cerr << std::endl;
  8442. // }
  8443. //
  8444. // ggml_vk_destroy_buffer(x_buf);
  8445. // ggml_vk_destroy_buffer(qx_buf);
  8446. //
  8447. // free(x);
  8448. // free(qx);
  8449. // free(qx_res);
  8450. // }
  8451. static void ggml_vk_test_dequant_matmul(ggml_backend_vk_context * ctx, size_t m, size_t n, size_t k, size_t batch, size_t num_it, size_t split_k, size_t shader_size, ggml_type quant, bool mmq = false) {
  8452. VK_LOG_DEBUG("ggml_vk_test_dequant_matmul(" << m << ", " << n << ", " << k << ", " << batch << ", " << num_it << ", " << split_k << ", " << ggml_type_name(quant) << ")");
  8453. const size_t x_ne = m * k * batch;
  8454. const size_t y_ne = k * n * batch;
  8455. const size_t d_ne = m * n * batch;
  8456. vk_matmul_pipeline2 * pipelines;
  8457. if (mmq) {
  8458. pipelines = ctx->device->pipeline_dequant_mul_mat_mat_q8_1;
  8459. } else {
  8460. pipelines = ctx->device->pipeline_dequant_mul_mat_mat;
  8461. }
  8462. const bool fp16acc = ctx->device->fp16;
  8463. vk_pipeline p;
  8464. std::string shname;
  8465. if (shader_size == 0) {
  8466. p = fp16acc ? pipelines[quant].f16acc->a_s : pipelines[quant].f32acc->a_s;
  8467. shname = std::string(ggml_type_name(quant)) + "_ALIGNED_S";
  8468. } else if (shader_size == 1) {
  8469. p = fp16acc ? pipelines[quant].f16acc->a_m : pipelines[quant].f32acc->a_m;
  8470. shname = std::string(ggml_type_name(quant)) + "_ALIGNED_M";
  8471. } else if (shader_size == 2) {
  8472. p = fp16acc ? pipelines[quant].f16acc->a_l : pipelines[quant].f32acc->a_l;
  8473. shname = std::string(ggml_type_name(quant)) + "_ALIGNED_L";
  8474. } else {
  8475. GGML_ASSERT(0);
  8476. }
  8477. const size_t kpad = mmq ? 0 : ggml_vk_align_size(k, p->align);
  8478. if (mmq || k != kpad) {
  8479. if (shader_size == 0) {
  8480. p = fp16acc ? pipelines[quant].f16acc->s : pipelines[quant].f32acc->s;
  8481. shname = std::string(ggml_type_name(quant)) + "_S";
  8482. } else if (shader_size == 1) {
  8483. p = fp16acc ? pipelines[quant].f16acc->m : pipelines[quant].f32acc->m;
  8484. shname = std::string(ggml_type_name(quant)) + "_M";
  8485. } else if (shader_size == 2) {
  8486. p = fp16acc ? pipelines[quant].f16acc->l : pipelines[quant].f32acc->l;
  8487. shname = std::string(ggml_type_name(quant)) + "_L";
  8488. } else {
  8489. GGML_ASSERT(0);
  8490. }
  8491. }
  8492. if (p == nullptr) {
  8493. std::cerr << "error: no pipeline for ggml_vk_test_dequant_matmul " << ggml_type_name(quant) << std::endl;
  8494. return;
  8495. }
  8496. const size_t x_sz = sizeof(float) * x_ne;
  8497. const size_t y_sz = sizeof(float) * y_ne;
  8498. const size_t qx_sz = x_ne * ggml_type_size(quant)/ggml_blck_size(quant);
  8499. const size_t qy_sz = mmq ? y_ne * ggml_type_size(GGML_TYPE_Q8_1)/ggml_blck_size(GGML_TYPE_Q8_1) : y_sz;
  8500. const size_t d_sz = sizeof(float) * d_ne;
  8501. float * x = (float *) malloc(x_sz);
  8502. float * y = (float *) malloc(y_sz);
  8503. void * qx = malloc(qx_sz);
  8504. vk_buffer qx_buf = ggml_vk_create_buffer_check(ctx->device, qx_sz, {vk::MemoryPropertyFlagBits::eDeviceLocal});
  8505. vk_buffer y_buf = ggml_vk_create_buffer_check(ctx->device, y_sz, {vk::MemoryPropertyFlagBits::eDeviceLocal});
  8506. vk_buffer qy_buf = ggml_vk_create_buffer_check(ctx->device, qy_sz, {vk::MemoryPropertyFlagBits::eDeviceLocal});
  8507. vk_buffer d_buf = ggml_vk_create_buffer_check(ctx->device, d_sz, {vk::MemoryPropertyFlagBits::eDeviceLocal});
  8508. float * d = (float *) malloc(d_sz);
  8509. float * d_chk = (float *) malloc(d_sz);
  8510. for (size_t i = 0; i < x_ne; i++) {
  8511. x[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  8512. // x[i] = (i % k == i / k) ? 1.0f : 0.0f;
  8513. // x[i] = i % k;
  8514. }
  8515. ggml_vk_quantize_data(x, qx, x_ne, quant);
  8516. for (size_t i = 0; i < y_ne; i++) {
  8517. y[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  8518. // y[i] = (i % k == i / k) ? 1.0f : 0.0f;
  8519. // y[i] = i % k;
  8520. }
  8521. ggml_pipeline_request_descriptor_sets(ctx, p, num_it);
  8522. if (split_k > 1) {
  8523. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_matmul_split_k_reduce, num_it);
  8524. if (ctx->prealloc_split_k == nullptr || ctx->prealloc_split_k->size < sizeof(float) * d_ne * split_k) {
  8525. // Resize buffer
  8526. if (ctx->prealloc_split_k != nullptr) {
  8527. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  8528. }
  8529. ctx->prealloc_split_k = ggml_vk_create_buffer_check(ctx->device, sizeof(float) * d_ne * split_k, {vk::MemoryPropertyFlagBits::eDeviceLocal});
  8530. }
  8531. }
  8532. if (mmq) {
  8533. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_quantize_q8_1, num_it);
  8534. }
  8535. if (ctx->device->need_compiles) {
  8536. ggml_vk_load_shaders(ctx->device);
  8537. }
  8538. ggml_pipeline_allocate_descriptor_sets(ctx);
  8539. ggml_vk_buffer_write(qx_buf, 0, qx, qx_sz);
  8540. ggml_vk_buffer_write(y_buf, 0, y, y_sz);
  8541. vk_context subctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  8542. ggml_vk_ctx_begin(ctx->device, subctx);
  8543. if (mmq) {
  8544. for (size_t i = 0; i < num_it; i++) {
  8545. ggml_vk_quantize_q8_1(ctx, subctx, { y_buf, 0, y_sz }, { qy_buf, 0, qy_sz }, y_ne);
  8546. ggml_vk_matmul(
  8547. ctx, subctx, p, { qx_buf, 0, qx_sz }, { qy_buf, 0, qy_sz }, { d_buf, 0, d_sz }, { ctx->prealloc_split_k, 0, ctx->prealloc_size_split_k },
  8548. m, n, k,
  8549. k, k, m, k*m, k*n, m*n,
  8550. split_k, batch, batch, batch, 1, 1, n
  8551. );
  8552. }
  8553. } else {
  8554. for (size_t i = 0; i < num_it; i++) {
  8555. ggml_vk_matmul(
  8556. ctx, subctx, p, { qx_buf, 0, qx_sz }, { y_buf, 0, y_sz }, { d_buf, 0, d_sz }, { ctx->prealloc_split_k, 0, ctx->prealloc_size_split_k },
  8557. m, n, k,
  8558. k, k, m, k*m, k*n, m*n,
  8559. split_k, batch, batch, batch, 1, 1, n
  8560. );
  8561. }
  8562. }
  8563. ggml_vk_ctx_end(subctx);
  8564. auto begin = std::chrono::high_resolution_clock::now();
  8565. ggml_vk_submit(subctx, ctx->fence);
  8566. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_dequant waitForFences");
  8567. ctx->device->device.resetFences({ ctx->fence });
  8568. ggml_vk_queue_command_pools_cleanup(ctx->device);
  8569. auto end = std::chrono::high_resolution_clock::now();
  8570. double time_ms = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  8571. ggml_vk_buffer_read(d_buf, 0, d, d_sz);
  8572. ggml_init_params iparams = {
  8573. /*.mem_size =*/ 1024*1024*1024,
  8574. /*.mem_buffer =*/ NULL,
  8575. /*.no_alloc =*/ true,
  8576. };
  8577. ggml_context * ggml_ctx = ggml_init(iparams);
  8578. ggml_tensor * src0_ggml = ggml_new_tensor_3d(ggml_ctx, quant, k, m, batch);
  8579. ggml_tensor * src1_ggml = ggml_new_tensor_3d(ggml_ctx, GGML_TYPE_F32, k, n, batch);
  8580. ggml_tensor * tensor_ggml = ggml_mul_mat(ggml_ctx, src0_ggml, src1_ggml);
  8581. src0_ggml->data = qx;
  8582. src1_ggml->data = y;
  8583. tensor_ggml->data = d_chk;
  8584. ggml_cgraph * cgraph = ggml_new_graph(ggml_ctx);
  8585. ggml_build_forward_expand(cgraph, tensor_ggml);
  8586. ggml_graph_compute_with_ctx(ggml_ctx, cgraph, 1);
  8587. ggml_free(ggml_ctx);
  8588. double avg_err = 0.0;
  8589. int first_err_n = -1;
  8590. int first_err_m = -1;
  8591. int first_err_b = -1;
  8592. for (size_t i = 0; i < m*n*batch; i++) {
  8593. double err = std::fabs(d[i] - d_chk[i]);
  8594. avg_err += err;
  8595. if ((err > 0.05f || std::isnan(err)) && first_err_n == -1) {
  8596. first_err_b = i / (m * n);
  8597. first_err_n = (i % (m * n)) / m;
  8598. first_err_m = (i % (m * n)) % m;
  8599. }
  8600. }
  8601. avg_err /= m * n;
  8602. double tflops = 2.0*m*n*k*batch*num_it / (time_ms / 1000.0) / (1000.0*1000.0*1000.0*1000.0);
  8603. std::cerr << "TEST dequant matmul " << shname;
  8604. if (mmq) {
  8605. std::cerr << " mmq";
  8606. }
  8607. std::cerr << " m=" << m << " n=" << n << " k=" << k << " batch=" << batch << " split_k=" << split_k << " matmul " << time_ms / num_it << "ms " << tflops << " TFLOPS avg_err=" << avg_err << std::endl;
  8608. if (avg_err > 0.01 || std::isnan(avg_err)) {
  8609. std::cerr << "m = " << first_err_m << " n = " << first_err_n << " b = " << first_err_b << std::endl;
  8610. std::cerr << "Actual result: " << std::endl << std::endl;
  8611. ggml_vk_print_matrix_area(d, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  8612. std::cerr << std::endl;
  8613. std::cerr << "Expected result: " << std::endl << std::endl;
  8614. ggml_vk_print_matrix_area(d_chk, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  8615. std::cerr << "src0: " << std::endl << std::endl;
  8616. ggml_vk_print_matrix_area(x, GGML_TYPE_F32, k, m, first_err_m, first_err_n, first_err_b);
  8617. std::cerr << std::endl;
  8618. std::cerr << "src1: " << std::endl << std::endl;
  8619. ggml_vk_print_matrix_area(y, GGML_TYPE_F32, k, n, first_err_m, first_err_n, first_err_b);
  8620. if (split_k > 1) {
  8621. float * split_k_buf = (float *) malloc(sizeof(float) * d_ne * split_k);
  8622. ggml_vk_buffer_read(ctx->prealloc_split_k, 0, split_k_buf, sizeof(float) * d_ne * split_k);
  8623. std::cerr << "d_buf0: " << std::endl << std::endl;
  8624. ggml_vk_print_matrix_area(split_k_buf, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  8625. std::cerr << "d_buf1: " << std::endl << std::endl;
  8626. ggml_vk_print_matrix_area(split_k_buf + d_ne, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  8627. std::cerr << "d_buf2: " << std::endl << std::endl;
  8628. ggml_vk_print_matrix_area(split_k_buf + 2 * d_ne, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  8629. std::cerr << "d_buf3: " << std::endl << std::endl;
  8630. ggml_vk_print_matrix_area(split_k_buf + 3 * d_ne, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  8631. free(split_k_buf);
  8632. }
  8633. }
  8634. ggml_vk_destroy_buffer(qx_buf);
  8635. ggml_vk_destroy_buffer(y_buf);
  8636. ggml_vk_destroy_buffer(qy_buf);
  8637. ggml_vk_destroy_buffer(d_buf);
  8638. free(x);
  8639. free(qx);
  8640. free(y);
  8641. free(d);
  8642. free(d_chk);
  8643. }
  8644. #endif
  8645. static void ggml_vk_preallocate_buffers(ggml_backend_vk_context * ctx) {
  8646. #if defined(GGML_VULKAN_RUN_TESTS)
  8647. const std::vector<size_t> vals {
  8648. 512, 512, 128,
  8649. 128, 512, 512,
  8650. 4096, 512, 4096,
  8651. 11008, 512, 4096,
  8652. 4096, 512, 11008,
  8653. 32000, 512, 4096,
  8654. 8, 8, 8,
  8655. 100, 46, 576,
  8656. 623, 111, 128,
  8657. 100, 46, 558,
  8658. 512, 1, 256,
  8659. 128, 110, 622,
  8660. 511, 511, 127,
  8661. 511, 511, 7,
  8662. 511, 511, 17,
  8663. 49, 49, 128,
  8664. 128, 49, 49,
  8665. 4096, 49, 4096,
  8666. };
  8667. const size_t num_it = 100;
  8668. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 0, GGML_TYPE_Q4_0);
  8669. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 1, GGML_TYPE_Q4_0);
  8670. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 2, GGML_TYPE_Q4_0);
  8671. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 0, GGML_TYPE_Q4_0, true);
  8672. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 1, GGML_TYPE_Q4_0, true);
  8673. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 2, GGML_TYPE_Q4_0, true);
  8674. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 0, GGML_TYPE_Q8_0);
  8675. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 1, GGML_TYPE_Q8_0);
  8676. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 2, GGML_TYPE_Q8_0);
  8677. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 0, GGML_TYPE_Q8_0, true);
  8678. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 1, GGML_TYPE_Q8_0, true);
  8679. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 2, GGML_TYPE_Q8_0, true);
  8680. abort();
  8681. for (size_t i = 0; i < vals.size(); i += 3) {
  8682. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 0);
  8683. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 1);
  8684. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 2);
  8685. std::cerr << '\n';
  8686. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 0);
  8687. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 1);
  8688. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 2);
  8689. std::cerr << '\n';
  8690. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 0);
  8691. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 1);
  8692. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 2);
  8693. std::cerr << '\n' << std::endl;
  8694. if (vals[i + 2] % 32 == 0) {
  8695. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 0, GGML_TYPE_Q4_0);
  8696. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 1, GGML_TYPE_Q4_0);
  8697. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 2, GGML_TYPE_Q4_0);
  8698. std::cerr << '\n';
  8699. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 0, GGML_TYPE_Q4_0);
  8700. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 1, GGML_TYPE_Q4_0);
  8701. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 2, GGML_TYPE_Q4_0);
  8702. std::cerr << '\n';
  8703. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 0, GGML_TYPE_Q4_0);
  8704. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 1, GGML_TYPE_Q4_0);
  8705. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 2, GGML_TYPE_Q4_0);
  8706. std::cerr << '\n' << std::endl;
  8707. }
  8708. if (vals[i + 2] % 256 == 0) {
  8709. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 0, GGML_TYPE_Q4_K);
  8710. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 1, GGML_TYPE_Q4_K);
  8711. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 2, GGML_TYPE_Q4_K);
  8712. std::cerr << '\n';
  8713. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 0, GGML_TYPE_Q4_K);
  8714. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 1, GGML_TYPE_Q4_K);
  8715. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 2, GGML_TYPE_Q4_K);
  8716. std::cerr << '\n';
  8717. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 0, GGML_TYPE_Q4_K);
  8718. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 1, GGML_TYPE_Q4_K);
  8719. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 2, GGML_TYPE_Q4_K);
  8720. std::cerr << '\n' << std::endl;
  8721. }
  8722. }
  8723. GGML_ABORT("fatal error");
  8724. #endif
  8725. if (ctx->prealloc_x == nullptr || (ctx->prealloc_size_x > 0 && ctx->prealloc_x->size < ctx->prealloc_size_x)) {
  8726. VK_LOG_MEMORY("ggml_vk_preallocate_buffers(x_size: " << ctx->prealloc_size_x << ")");
  8727. // Resize buffer
  8728. if (ctx->prealloc_x != nullptr) {
  8729. ggml_vk_destroy_buffer(ctx->prealloc_x);
  8730. }
  8731. ctx->prealloc_x = ggml_vk_create_buffer_device(ctx->device, ctx->prealloc_size_x);
  8732. }
  8733. if (ctx->prealloc_y == nullptr || (ctx->prealloc_size_y > 0 && ctx->prealloc_y->size < ctx->prealloc_size_y)) {
  8734. VK_LOG_MEMORY("ggml_vk_preallocate_buffers(y_size: " << ctx->prealloc_size_y << ")");
  8735. // Resize buffer
  8736. if (ctx->prealloc_y != nullptr) {
  8737. ggml_vk_destroy_buffer(ctx->prealloc_y);
  8738. }
  8739. ctx->prealloc_y = ggml_vk_create_buffer_device(ctx->device, ctx->prealloc_size_y);
  8740. }
  8741. if (ctx->prealloc_split_k == nullptr || (ctx->prealloc_size_split_k > 0 && ctx->prealloc_split_k->size < ctx->prealloc_size_split_k)) {
  8742. VK_LOG_MEMORY("ggml_vk_preallocate_buffers(split_k_size: " << ctx->prealloc_size_split_k << ")");
  8743. // Resize buffer
  8744. if (ctx->prealloc_split_k != nullptr) {
  8745. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  8746. }
  8747. ctx->prealloc_split_k = ggml_vk_create_buffer_device(ctx->device, ctx->prealloc_size_split_k);
  8748. }
  8749. if (ctx->prealloc_add_rms_partials == nullptr || (ctx->prealloc_size_add_rms_partials > 0 && ctx->prealloc_add_rms_partials->size < ctx->prealloc_size_add_rms_partials)) {
  8750. VK_LOG_MEMORY("ggml_vk_preallocate_buffers(add_partials_size: " << ctx->prealloc_add_rms_partials << ")");
  8751. // Resize buffer
  8752. if (ctx->prealloc_add_rms_partials != nullptr) {
  8753. ggml_vk_destroy_buffer(ctx->prealloc_add_rms_partials);
  8754. }
  8755. ctx->prealloc_add_rms_partials = ggml_vk_create_buffer_device(ctx->device, ctx->prealloc_size_add_rms_partials);
  8756. }
  8757. }
  8758. static bool ggml_vk_compute_forward(ggml_backend_vk_context* ctx, ggml_cgraph * cgraph, ggml_tensor* tensor, int tensor_idx, bool use_fence, bool almost_ready);
  8759. // Returns true if node has enqueued work into the queue, false otherwise
  8760. // If submit is true the current all operations queued so far are being submitted to Vulkan to overlap cmdlist creation and GPU execution.
  8761. static bool ggml_vk_build_graph(ggml_backend_vk_context * ctx, ggml_cgraph * cgraph, int node_idx, ggml_tensor *node_begin, int node_idx_begin, bool dryrun, bool last_node, bool almost_ready, bool submit){
  8762. ggml_tensor * node = cgraph->nodes[node_idx];
  8763. if (ggml_is_empty(node) || !node->buffer) {
  8764. return false;
  8765. }
  8766. VK_LOG_DEBUG("ggml_vk_build_graph(" << node << ", " << ggml_op_name(node->op) << ")");
  8767. ctx->semaphore_idx = 0;
  8768. const ggml_tensor * src0 = node->src[0];
  8769. const ggml_tensor * src1 = node->src[1];
  8770. const ggml_tensor * src2 = node->src[2];
  8771. const ggml_tensor * src3 = node->src[3];
  8772. switch (node->op) {
  8773. // Return on empty ops to avoid generating a compute_ctx and setting exit_tensor
  8774. case GGML_OP_RESHAPE:
  8775. case GGML_OP_VIEW:
  8776. case GGML_OP_PERMUTE:
  8777. case GGML_OP_TRANSPOSE:
  8778. case GGML_OP_NONE:
  8779. return false;
  8780. case GGML_OP_UNARY:
  8781. switch (ggml_get_unary_op(node)) {
  8782. case GGML_UNARY_OP_EXP:
  8783. case GGML_UNARY_OP_SILU:
  8784. case GGML_UNARY_OP_GELU:
  8785. case GGML_UNARY_OP_GELU_ERF:
  8786. case GGML_UNARY_OP_GELU_QUICK:
  8787. case GGML_UNARY_OP_RELU:
  8788. case GGML_UNARY_OP_TANH:
  8789. case GGML_UNARY_OP_SIGMOID:
  8790. case GGML_UNARY_OP_HARDSIGMOID:
  8791. case GGML_UNARY_OP_HARDSWISH:
  8792. break;
  8793. default:
  8794. return false;
  8795. }
  8796. break;
  8797. case GGML_OP_GLU:
  8798. switch (ggml_get_glu_op(node)) {
  8799. case GGML_GLU_OP_GEGLU:
  8800. case GGML_GLU_OP_REGLU:
  8801. case GGML_GLU_OP_SWIGLU:
  8802. case GGML_GLU_OP_SWIGLU_OAI:
  8803. case GGML_GLU_OP_GEGLU_ERF:
  8804. case GGML_GLU_OP_GEGLU_QUICK:
  8805. break;
  8806. default:
  8807. return false;
  8808. }
  8809. break;
  8810. case GGML_OP_ADD:
  8811. {
  8812. int next_node_idx = node_idx + 1 + ctx->num_additional_fused_ops;
  8813. if (next_node_idx < cgraph->n_nodes &&
  8814. cgraph->nodes[next_node_idx]->op == GGML_OP_RMS_NORM &&
  8815. cgraph->nodes[next_node_idx]->src[0] == cgraph->nodes[next_node_idx - 1] &&
  8816. ggml_nrows(cgraph->nodes[next_node_idx]) == 1 &&
  8817. ctx->device->add_rms_fusion) {
  8818. if (dryrun) {
  8819. ctx->prealloc_size_add_rms_partials += ggml_vk_rms_partials_size(ctx, cgraph->nodes[node_idx]);
  8820. }
  8821. ctx->do_add_rms_partials = true;
  8822. }
  8823. } break;
  8824. case GGML_OP_REPEAT:
  8825. case GGML_OP_REPEAT_BACK:
  8826. case GGML_OP_GET_ROWS:
  8827. case GGML_OP_ADD_ID:
  8828. case GGML_OP_ACC:
  8829. case GGML_OP_SUB:
  8830. case GGML_OP_MUL:
  8831. case GGML_OP_DIV:
  8832. case GGML_OP_CONCAT:
  8833. case GGML_OP_UPSCALE:
  8834. case GGML_OP_SCALE:
  8835. case GGML_OP_SQR:
  8836. case GGML_OP_SQRT:
  8837. case GGML_OP_SIN:
  8838. case GGML_OP_COS:
  8839. case GGML_OP_CLAMP:
  8840. case GGML_OP_PAD:
  8841. case GGML_OP_ROLL:
  8842. case GGML_OP_CPY:
  8843. case GGML_OP_SET_ROWS:
  8844. case GGML_OP_CONT:
  8845. case GGML_OP_DUP:
  8846. case GGML_OP_SILU_BACK:
  8847. case GGML_OP_NORM:
  8848. case GGML_OP_GROUP_NORM:
  8849. case GGML_OP_RMS_NORM:
  8850. case GGML_OP_RMS_NORM_BACK:
  8851. case GGML_OP_L2_NORM:
  8852. case GGML_OP_DIAG_MASK_INF:
  8853. case GGML_OP_SOFT_MAX:
  8854. case GGML_OP_SOFT_MAX_BACK:
  8855. case GGML_OP_ROPE:
  8856. case GGML_OP_ROPE_BACK:
  8857. case GGML_OP_MUL_MAT:
  8858. case GGML_OP_MUL_MAT_ID:
  8859. case GGML_OP_ARGSORT:
  8860. case GGML_OP_SUM:
  8861. case GGML_OP_SUM_ROWS:
  8862. case GGML_OP_MEAN:
  8863. case GGML_OP_ARGMAX:
  8864. case GGML_OP_COUNT_EQUAL:
  8865. case GGML_OP_IM2COL:
  8866. case GGML_OP_TIMESTEP_EMBEDDING:
  8867. case GGML_OP_CONV_TRANSPOSE_1D:
  8868. case GGML_OP_POOL_2D:
  8869. case GGML_OP_CONV_2D:
  8870. case GGML_OP_CONV_2D_DW:
  8871. case GGML_OP_RWKV_WKV6:
  8872. case GGML_OP_RWKV_WKV7:
  8873. case GGML_OP_LEAKY_RELU:
  8874. case GGML_OP_FLASH_ATTN_EXT:
  8875. case GGML_OP_OPT_STEP_ADAMW:
  8876. case GGML_OP_OPT_STEP_SGD:
  8877. break;
  8878. default:
  8879. std::cerr << "ggml_vulkan: Error: Missing op: " << ggml_op_name(node->op) << std::endl;
  8880. GGML_ABORT("fatal error");
  8881. }
  8882. vk_context compute_ctx;
  8883. if (!dryrun) {
  8884. if (ctx->compute_ctx.expired()) {
  8885. compute_ctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  8886. ctx->compute_ctx = compute_ctx;
  8887. ggml_vk_ctx_begin(ctx->device, compute_ctx);
  8888. } else {
  8889. compute_ctx = ctx->compute_ctx.lock();
  8890. }
  8891. } else {
  8892. switch (node->op) {
  8893. case GGML_OP_REPEAT:
  8894. case GGML_OP_REPEAT_BACK:
  8895. case GGML_OP_ACC:
  8896. case GGML_OP_GET_ROWS:
  8897. case GGML_OP_ADD:
  8898. case GGML_OP_SUB:
  8899. case GGML_OP_MUL:
  8900. case GGML_OP_DIV:
  8901. case GGML_OP_CONCAT:
  8902. case GGML_OP_UPSCALE:
  8903. case GGML_OP_SCALE:
  8904. case GGML_OP_SQR:
  8905. case GGML_OP_SQRT:
  8906. case GGML_OP_SIN:
  8907. case GGML_OP_COS:
  8908. case GGML_OP_CLAMP:
  8909. case GGML_OP_PAD:
  8910. case GGML_OP_CPY:
  8911. case GGML_OP_SET_ROWS:
  8912. case GGML_OP_CONT:
  8913. case GGML_OP_DUP:
  8914. case GGML_OP_SILU_BACK:
  8915. case GGML_OP_NORM:
  8916. case GGML_OP_GROUP_NORM:
  8917. case GGML_OP_RMS_NORM:
  8918. case GGML_OP_RMS_NORM_BACK:
  8919. case GGML_OP_L2_NORM:
  8920. case GGML_OP_UNARY:
  8921. case GGML_OP_GLU:
  8922. case GGML_OP_DIAG_MASK_INF:
  8923. case GGML_OP_SOFT_MAX:
  8924. case GGML_OP_SOFT_MAX_BACK:
  8925. case GGML_OP_ROPE:
  8926. case GGML_OP_ROPE_BACK:
  8927. case GGML_OP_ARGSORT:
  8928. case GGML_OP_SUM:
  8929. case GGML_OP_SUM_ROWS:
  8930. case GGML_OP_MEAN:
  8931. case GGML_OP_ARGMAX:
  8932. case GGML_OP_COUNT_EQUAL:
  8933. case GGML_OP_IM2COL:
  8934. case GGML_OP_TIMESTEP_EMBEDDING:
  8935. case GGML_OP_CONV_TRANSPOSE_1D:
  8936. case GGML_OP_POOL_2D:
  8937. case GGML_OP_CONV_2D:
  8938. case GGML_OP_CONV_2D_DW:
  8939. case GGML_OP_LEAKY_RELU:
  8940. case GGML_OP_OPT_STEP_SGD:
  8941. {
  8942. // These operations all go through ggml_vk_op_f32, so short-circuit and
  8943. // do the only thing needed for the dryrun.
  8944. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, src0, src1, src2, node, node->op);
  8945. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  8946. if (node->op == GGML_OP_RMS_NORM) {
  8947. ctx->do_add_rms_partials = false;
  8948. }
  8949. return false;
  8950. }
  8951. default:
  8952. break;
  8953. }
  8954. }
  8955. if (!dryrun) {
  8956. // This logic detects dependencies between modes in the graph and calls ggml_vk_sync_buffers
  8957. // to synchronize them. This handles most "normal" synchronization when computing the graph, and when
  8958. // there is no auxiliary memory use, it shouldn't be necessary to call ggml_vk_sync_buffers
  8959. // outside of this logic. When a node uses one of the prealloc buffers for something like
  8960. // dequantization or split_k, additional synchronization is needed between those passes.
  8961. bool need_sync = false;
  8962. // Check whether "node" requires synchronization. The node requires synchronization if it
  8963. // overlaps in memory with another unsynchronized node and at least one of them is a write.
  8964. // Destination nodes are checked against both the written/read lists. Source nodes are only
  8965. // checked against the written list. Two nodes overlap in memory if they come from the same
  8966. // buffer and the tensor or view ranges overlap.
  8967. auto const &overlaps_unsynced = [&](const ggml_tensor *node, const std::vector<const ggml_tensor *> &unsynced_nodes) -> bool {
  8968. if (unsynced_nodes.size() == 0) {
  8969. return false;
  8970. }
  8971. auto n_base = vk_tensor_offset(node) + node->view_offs;
  8972. auto n_size = ggml_nbytes(node);
  8973. ggml_backend_vk_buffer_context * a_buf_ctx = (ggml_backend_vk_buffer_context *)node->buffer->context;
  8974. vk_buffer a_buf = a_buf_ctx->dev_buffer;
  8975. for (auto &other : unsynced_nodes) {
  8976. ggml_backend_vk_buffer_context * o_buf_ctx = (ggml_backend_vk_buffer_context *)other->buffer->context;
  8977. vk_buffer o_buf = o_buf_ctx->dev_buffer;
  8978. if (a_buf == o_buf) {
  8979. auto o_base = vk_tensor_offset(other) + other->view_offs;
  8980. auto o_size = ggml_nbytes(other);
  8981. if ((o_base <= n_base && n_base < o_base + o_size) ||
  8982. (n_base <= o_base && o_base < n_base + n_size)) {
  8983. return true;
  8984. }
  8985. }
  8986. }
  8987. return false;
  8988. };
  8989. // For all fused ops, check if the destination node or any of the source
  8990. // nodes require synchronization.
  8991. for (int32_t i = 0; i < ctx->num_additional_fused_ops + 1 && !need_sync; ++i) {
  8992. const ggml_tensor *cur_node = cgraph->nodes[node_idx + i];
  8993. if (overlaps_unsynced(cur_node, ctx->unsynced_nodes_read) || overlaps_unsynced(cur_node, ctx->unsynced_nodes_written)) {
  8994. need_sync = true;
  8995. break;
  8996. }
  8997. for (uint32_t j = 0; j < GGML_MAX_SRC; ++j) {
  8998. if (!cur_node->src[j]) {
  8999. continue;
  9000. }
  9001. if (overlaps_unsynced(cur_node->src[j], ctx->unsynced_nodes_written)) {
  9002. need_sync = true;
  9003. break;
  9004. }
  9005. }
  9006. }
  9007. if (need_sync) {
  9008. ctx->unsynced_nodes_written.clear();
  9009. ctx->unsynced_nodes_read.clear();
  9010. ggml_vk_sync_buffers(ctx, compute_ctx);
  9011. }
  9012. // Add the last fused node and all fused source nodes to the unsynchronized list.
  9013. const ggml_tensor * last_node = cgraph->nodes[node_idx + ctx->num_additional_fused_ops];
  9014. ctx->unsynced_nodes_written.push_back(last_node);
  9015. for (int32_t i = 0; i < ctx->num_additional_fused_ops + 1; ++i) {
  9016. const ggml_tensor *cur_node = cgraph->nodes[node_idx + i];
  9017. for (uint32_t j = 0; j < GGML_MAX_SRC; ++j) {
  9018. if (!cur_node->src[j]) {
  9019. continue;
  9020. }
  9021. ctx->unsynced_nodes_read.push_back(cur_node->src[j]);
  9022. }
  9023. }
  9024. }
  9025. switch (node->op) {
  9026. case GGML_OP_REPEAT:
  9027. ggml_vk_repeat(ctx, compute_ctx, src0, node, dryrun);
  9028. break;
  9029. case GGML_OP_REPEAT_BACK:
  9030. ggml_vk_repeat_back(ctx, compute_ctx, src0, node, dryrun);
  9031. break;
  9032. case GGML_OP_ACC:
  9033. ggml_vk_acc(ctx, compute_ctx, src0, src1, node, dryrun);
  9034. break;
  9035. case GGML_OP_GET_ROWS:
  9036. ggml_vk_get_rows(ctx, compute_ctx, src0, src1, node, dryrun);
  9037. break;
  9038. case GGML_OP_ADD:
  9039. if (ctx->num_additional_fused_ops) {
  9040. ggml_vk_multi_add(ctx, compute_ctx, cgraph, node_idx, dryrun);
  9041. } else {
  9042. ggml_vk_add(ctx, compute_ctx, src0, src1, node, dryrun);
  9043. }
  9044. break;
  9045. case GGML_OP_SUB:
  9046. ggml_vk_sub(ctx, compute_ctx, src0, src1, node, dryrun);
  9047. break;
  9048. case GGML_OP_MUL:
  9049. ggml_vk_mul(ctx, compute_ctx, src0, src1, node, dryrun);
  9050. break;
  9051. case GGML_OP_DIV:
  9052. ggml_vk_div(ctx, compute_ctx, src0, src1, node, dryrun);
  9053. break;
  9054. case GGML_OP_ADD_ID:
  9055. ggml_vk_add_id(ctx, compute_ctx, src0, src1, src2, node, dryrun);
  9056. break;
  9057. case GGML_OP_CONCAT:
  9058. ggml_vk_concat(ctx, compute_ctx, src0, src1, node, dryrun);
  9059. break;
  9060. case GGML_OP_UPSCALE:
  9061. ggml_vk_upscale(ctx, compute_ctx, src0, node, dryrun);
  9062. break;
  9063. case GGML_OP_SCALE:
  9064. ggml_vk_scale(ctx, compute_ctx, src0, node, dryrun);
  9065. break;
  9066. case GGML_OP_SQR:
  9067. ggml_vk_sqr(ctx, compute_ctx, src0, node, dryrun);
  9068. break;
  9069. case GGML_OP_SQRT:
  9070. ggml_vk_sqrt(ctx, compute_ctx, src0, node, dryrun);
  9071. break;
  9072. case GGML_OP_SIN:
  9073. ggml_vk_sin(ctx, compute_ctx, src0, node, dryrun);
  9074. break;
  9075. case GGML_OP_COS:
  9076. ggml_vk_cos(ctx, compute_ctx, src0, node, dryrun);
  9077. break;
  9078. case GGML_OP_CLAMP:
  9079. ggml_vk_clamp(ctx, compute_ctx, src0, node, dryrun);
  9080. break;
  9081. case GGML_OP_PAD:
  9082. ggml_vk_pad(ctx, compute_ctx, src0, node, dryrun);
  9083. break;
  9084. case GGML_OP_ROLL:
  9085. ggml_vk_roll(ctx, compute_ctx, src0, node, dryrun);
  9086. break;
  9087. case GGML_OP_CPY:
  9088. case GGML_OP_CONT:
  9089. case GGML_OP_DUP:
  9090. ggml_vk_cpy(ctx, compute_ctx, src0, node, dryrun);
  9091. break;
  9092. case GGML_OP_SET_ROWS:
  9093. ggml_vk_set_rows(ctx, compute_ctx, src0, src1, node, dryrun);
  9094. break;
  9095. case GGML_OP_SILU_BACK:
  9096. ggml_vk_silu_back(ctx, compute_ctx, src0, src1, node, dryrun);
  9097. break;
  9098. case GGML_OP_NORM:
  9099. ggml_vk_norm(ctx, compute_ctx, src0, node, dryrun);
  9100. break;
  9101. case GGML_OP_GROUP_NORM:
  9102. ggml_vk_group_norm(ctx, compute_ctx, src0, node, dryrun);
  9103. break;
  9104. case GGML_OP_RMS_NORM:
  9105. if (ctx->num_additional_fused_ops > 0) {
  9106. // fused rms_norm + mul
  9107. ggml_tensor *mul = cgraph->nodes[node_idx + 1];
  9108. ggml_tensor *other_src = mul->src[0] == node ? mul->src[1] : mul->src[0];
  9109. ggml_vk_rms_norm(ctx, compute_ctx, src0, other_src, mul, (float *)node->op_params, dryrun);
  9110. } else {
  9111. ggml_vk_rms_norm(ctx, compute_ctx, src0, src0, node, (float *)node->op_params, dryrun);
  9112. }
  9113. break;
  9114. case GGML_OP_RMS_NORM_BACK:
  9115. ggml_vk_rms_norm_back(ctx, compute_ctx, src0, src1, node, dryrun);
  9116. break;
  9117. case GGML_OP_L2_NORM:
  9118. ggml_vk_l2_norm(ctx, compute_ctx, src0, node, dryrun);
  9119. break;
  9120. case GGML_OP_UNARY:
  9121. switch (ggml_get_unary_op(node)) {
  9122. case GGML_UNARY_OP_EXP:
  9123. case GGML_UNARY_OP_SILU:
  9124. case GGML_UNARY_OP_GELU:
  9125. case GGML_UNARY_OP_GELU_ERF:
  9126. case GGML_UNARY_OP_GELU_QUICK:
  9127. case GGML_UNARY_OP_RELU:
  9128. case GGML_UNARY_OP_TANH:
  9129. case GGML_UNARY_OP_SIGMOID:
  9130. case GGML_UNARY_OP_HARDSIGMOID:
  9131. case GGML_UNARY_OP_HARDSWISH:
  9132. ggml_vk_unary(ctx, compute_ctx, src0, node, dryrun);
  9133. break;
  9134. default:
  9135. return false;
  9136. }
  9137. break;
  9138. case GGML_OP_GLU:
  9139. switch (ggml_get_glu_op(node)) {
  9140. case GGML_GLU_OP_GEGLU:
  9141. case GGML_GLU_OP_REGLU:
  9142. case GGML_GLU_OP_SWIGLU:
  9143. case GGML_GLU_OP_SWIGLU_OAI:
  9144. case GGML_GLU_OP_GEGLU_ERF:
  9145. case GGML_GLU_OP_GEGLU_QUICK:
  9146. ggml_vk_glu(ctx, compute_ctx, src0, src1, node, dryrun);
  9147. break;
  9148. default:
  9149. return false;
  9150. }
  9151. break;
  9152. case GGML_OP_DIAG_MASK_INF:
  9153. ggml_vk_diag_mask_inf(ctx, compute_ctx, src0, node, dryrun);
  9154. break;
  9155. case GGML_OP_SOFT_MAX:
  9156. ggml_vk_soft_max(ctx, compute_ctx, src0, src1, src2, node, dryrun);
  9157. break;
  9158. case GGML_OP_SOFT_MAX_BACK:
  9159. ggml_vk_soft_max_back(ctx, compute_ctx, src0, src1, node, dryrun);
  9160. break;
  9161. case GGML_OP_ROPE:
  9162. ggml_vk_rope(ctx, compute_ctx, src0, src1, src2, node, false, dryrun);
  9163. break;
  9164. case GGML_OP_ROPE_BACK:
  9165. ggml_vk_rope(ctx, compute_ctx, src0, src1, src2, node, true, dryrun);
  9166. break;
  9167. case GGML_OP_ARGSORT:
  9168. ggml_vk_argsort(ctx, compute_ctx, src0, node, dryrun);
  9169. break;
  9170. case GGML_OP_SUM:
  9171. ggml_vk_sum(ctx, compute_ctx, src0, node, dryrun);
  9172. break;
  9173. case GGML_OP_SUM_ROWS:
  9174. ggml_vk_sum_rows(ctx, compute_ctx, src0, node, dryrun);
  9175. break;
  9176. case GGML_OP_MEAN:
  9177. ggml_vk_mean(ctx, compute_ctx, src0, node, dryrun);
  9178. break;
  9179. case GGML_OP_ARGMAX:
  9180. ggml_vk_argmax(ctx, compute_ctx, src0, node, dryrun);
  9181. break;
  9182. case GGML_OP_COUNT_EQUAL:
  9183. ggml_vk_count_equal(ctx, compute_ctx, src0, src1, node, dryrun);
  9184. break;
  9185. case GGML_OP_IM2COL:
  9186. ggml_vk_im2col(ctx, compute_ctx, src0, src1, node, dryrun);
  9187. break;
  9188. case GGML_OP_TIMESTEP_EMBEDDING:
  9189. ggml_vk_timestep_embedding(ctx, compute_ctx, src0, node, dryrun);
  9190. break;
  9191. case GGML_OP_CONV_TRANSPOSE_1D:
  9192. ggml_vk_conv_transpose_1d(ctx, compute_ctx, src0, src1, node, dryrun);
  9193. break;
  9194. case GGML_OP_POOL_2D:
  9195. ggml_vk_pool_2d(ctx, compute_ctx, src0, node, dryrun);
  9196. break;
  9197. case GGML_OP_CONV_2D:
  9198. ggml_vk_conv_2d(ctx, compute_ctx, src0, src1, node, dryrun);
  9199. break;
  9200. case GGML_OP_CONV_2D_DW:
  9201. ggml_vk_conv_2d_dw(ctx, compute_ctx, src0, src1, node, dryrun);
  9202. break;
  9203. case GGML_OP_LEAKY_RELU:
  9204. ggml_vk_leaky_relu(ctx, compute_ctx, src0, node, dryrun);
  9205. break;
  9206. case GGML_OP_MUL_MAT:
  9207. ggml_vk_mul_mat(ctx, compute_ctx, src0, src1, node, dryrun);
  9208. break;
  9209. case GGML_OP_MUL_MAT_ID:
  9210. ggml_vk_mul_mat_id(ctx, compute_ctx, src0, src1, src2, node, dryrun);
  9211. break;
  9212. case GGML_OP_FLASH_ATTN_EXT:
  9213. ggml_vk_flash_attn(ctx, compute_ctx, src0, src1, src2, src3, node->src[4], node, dryrun);
  9214. break;
  9215. case GGML_OP_RWKV_WKV6:
  9216. ggml_vk_rwkv_wkv6(ctx, compute_ctx, node, dryrun);
  9217. break;
  9218. case GGML_OP_RWKV_WKV7:
  9219. ggml_vk_rwkv_wkv7(ctx, compute_ctx, node, dryrun);
  9220. break;
  9221. case GGML_OP_OPT_STEP_ADAMW:
  9222. ggml_vk_opt_step_adamw(ctx, compute_ctx, node, dryrun);
  9223. break;
  9224. case GGML_OP_OPT_STEP_SGD:
  9225. ggml_vk_opt_step_sgd(ctx, compute_ctx, src0, src1, src2, node, dryrun);
  9226. break;
  9227. default:
  9228. return false;
  9229. }
  9230. if (dryrun) {
  9231. return false;
  9232. }
  9233. ctx->tensor_ctxs[node_idx] = compute_ctx;
  9234. #if defined(GGML_VULKAN_CHECK_RESULTS)
  9235. // Force context reset on each node so that each tensor ends up in its own context
  9236. // and can be run and compared to its CPU equivalent separately
  9237. last_node = true;
  9238. #endif
  9239. if (submit || last_node) {
  9240. ggml_vk_ctx_end(compute_ctx);
  9241. // TODO probably it'd be better to pass a exit_node flag to ggml_vk_compute_forward
  9242. if (last_node) {
  9243. compute_ctx->exit_tensor_idx = node_idx_begin;
  9244. }
  9245. else {
  9246. compute_ctx->exit_tensor_idx = -1;
  9247. }
  9248. ctx->compute_ctx.reset();
  9249. bool ok = ggml_vk_compute_forward(ctx, cgraph, node_begin, node_idx_begin, false, almost_ready);
  9250. if (!ok) {
  9251. if (node->op == GGML_OP_UNARY) {
  9252. std::cerr << __func__ << ": error: op not supported UNARY " << node->name << " (" << ggml_unary_op_name(static_cast<ggml_unary_op>(node->op_params[0])) << ")" << std::endl;
  9253. } else if (node->op == GGML_OP_GLU) {
  9254. std::cerr << __func__ << ": error: op not supported GLU " << node->name << " (" << ggml_glu_op_name(static_cast<ggml_glu_op>(node->op_params[0])) << ")" << std::endl;
  9255. } else {
  9256. std::cerr << __func__ << ": error: op not supported " << node->name << " (" << ggml_op_name(node->op) << ")" << std::endl;
  9257. }
  9258. }
  9259. }
  9260. return true;
  9261. }
  9262. static bool ggml_vk_compute_forward(ggml_backend_vk_context * ctx, ggml_cgraph * cgraph, ggml_tensor * tensor, int tensor_idx, bool use_fence = true, bool almost_ready = false) {
  9263. GGML_UNUSED(cgraph);
  9264. ggml_backend_buffer * buf = nullptr;
  9265. switch (tensor->op) {
  9266. case GGML_OP_ADD:
  9267. case GGML_OP_ACC:
  9268. case GGML_OP_GET_ROWS:
  9269. case GGML_OP_SUB:
  9270. case GGML_OP_MUL:
  9271. case GGML_OP_DIV:
  9272. case GGML_OP_ADD_ID:
  9273. case GGML_OP_CONCAT:
  9274. case GGML_OP_UPSCALE:
  9275. case GGML_OP_SCALE:
  9276. case GGML_OP_SQR:
  9277. case GGML_OP_SQRT:
  9278. case GGML_OP_SIN:
  9279. case GGML_OP_COS:
  9280. case GGML_OP_CLAMP:
  9281. case GGML_OP_PAD:
  9282. case GGML_OP_ROLL:
  9283. case GGML_OP_CPY:
  9284. case GGML_OP_SET_ROWS:
  9285. case GGML_OP_CONT:
  9286. case GGML_OP_DUP:
  9287. case GGML_OP_SILU_BACK:
  9288. case GGML_OP_NORM:
  9289. case GGML_OP_GROUP_NORM:
  9290. case GGML_OP_RMS_NORM:
  9291. case GGML_OP_RMS_NORM_BACK:
  9292. case GGML_OP_L2_NORM:
  9293. case GGML_OP_DIAG_MASK_INF:
  9294. case GGML_OP_SOFT_MAX:
  9295. case GGML_OP_SOFT_MAX_BACK:
  9296. case GGML_OP_ROPE:
  9297. case GGML_OP_ROPE_BACK:
  9298. case GGML_OP_RESHAPE:
  9299. case GGML_OP_VIEW:
  9300. case GGML_OP_PERMUTE:
  9301. case GGML_OP_TRANSPOSE:
  9302. case GGML_OP_NONE:
  9303. case GGML_OP_ARGSORT:
  9304. case GGML_OP_SUM:
  9305. case GGML_OP_SUM_ROWS:
  9306. case GGML_OP_MEAN:
  9307. case GGML_OP_ARGMAX:
  9308. case GGML_OP_COUNT_EQUAL:
  9309. case GGML_OP_IM2COL:
  9310. case GGML_OP_TIMESTEP_EMBEDDING:
  9311. case GGML_OP_CONV_TRANSPOSE_1D:
  9312. case GGML_OP_POOL_2D:
  9313. case GGML_OP_CONV_2D:
  9314. case GGML_OP_CONV_2D_DW:
  9315. case GGML_OP_RWKV_WKV6:
  9316. case GGML_OP_RWKV_WKV7:
  9317. case GGML_OP_LEAKY_RELU:
  9318. case GGML_OP_REPEAT:
  9319. case GGML_OP_REPEAT_BACK:
  9320. case GGML_OP_OPT_STEP_ADAMW:
  9321. case GGML_OP_OPT_STEP_SGD:
  9322. buf = tensor->buffer;
  9323. break;
  9324. case GGML_OP_UNARY:
  9325. switch (ggml_get_unary_op(tensor)) {
  9326. case GGML_UNARY_OP_EXP:
  9327. case GGML_UNARY_OP_SILU:
  9328. case GGML_UNARY_OP_GELU:
  9329. case GGML_UNARY_OP_GELU_ERF:
  9330. case GGML_UNARY_OP_GELU_QUICK:
  9331. case GGML_UNARY_OP_RELU:
  9332. case GGML_UNARY_OP_TANH:
  9333. case GGML_UNARY_OP_SIGMOID:
  9334. case GGML_UNARY_OP_HARDSIGMOID:
  9335. case GGML_UNARY_OP_HARDSWISH:
  9336. buf = tensor->buffer;
  9337. break;
  9338. default:
  9339. return false;
  9340. }
  9341. break;
  9342. case GGML_OP_GLU:
  9343. switch (ggml_get_glu_op(tensor)) {
  9344. case GGML_GLU_OP_GEGLU:
  9345. case GGML_GLU_OP_REGLU:
  9346. case GGML_GLU_OP_SWIGLU:
  9347. case GGML_GLU_OP_SWIGLU_OAI:
  9348. case GGML_GLU_OP_GEGLU_ERF:
  9349. case GGML_GLU_OP_GEGLU_QUICK:
  9350. buf = tensor->buffer;
  9351. break;
  9352. default:
  9353. return false;
  9354. }
  9355. break;
  9356. case GGML_OP_MUL_MAT:
  9357. case GGML_OP_MUL_MAT_ID:
  9358. case GGML_OP_FLASH_ATTN_EXT:
  9359. buf = tensor->buffer;
  9360. break;
  9361. default:
  9362. return false;
  9363. }
  9364. if (buf == nullptr) {
  9365. return false;
  9366. }
  9367. VK_LOG_DEBUG("ggml_vk_compute_forward(" << tensor << ", name=" << tensor->name << ", op=" << ggml_op_name(tensor->op) << ", type=" << tensor->type << ", ne0=" << tensor->ne[0] << ", ne1=" << tensor->ne[1] << ", ne2=" << tensor->ne[2] << ", ne3=" << tensor->ne[3] << ", nb0=" << tensor->nb[0] << ", nb1=" << tensor->nb[1] << ", nb2=" << tensor->nb[2] << ", nb3=" << tensor->nb[3] << ", view_src=" << tensor->view_src << ", view_offs=" << tensor->view_offs << ")");
  9368. vk_context subctx = ctx->tensor_ctxs[tensor_idx].lock();
  9369. // always wait for the GPU work to be done for the last submit
  9370. if (tensor_idx == subctx->exit_tensor_idx) {
  9371. use_fence = true;
  9372. }
  9373. // Only run if ctx hasn't been submitted yet
  9374. if (!subctx->seqs.empty()) {
  9375. #ifdef GGML_VULKAN_CHECK_RESULTS
  9376. ggml_vk_check_results_0(ctx, cgraph, tensor_idx);
  9377. use_fence = true;
  9378. #endif
  9379. // Do staging buffer copies
  9380. for (auto& cpy : subctx->in_memcpys) {
  9381. memcpy(cpy.dst, cpy.src, cpy.n);
  9382. }
  9383. if (almost_ready && !ctx->almost_ready_fence_pending && !use_fence) {
  9384. ggml_vk_submit(subctx, ctx->almost_ready_fence);
  9385. ctx->almost_ready_fence_pending = true;
  9386. } else {
  9387. ggml_vk_submit(subctx, use_fence ? ctx->fence : vk::Fence{});
  9388. }
  9389. if (use_fence) {
  9390. ggml_vk_wait_for_fence(ctx);
  9391. }
  9392. #ifdef GGML_VULKAN_CHECK_RESULTS
  9393. ggml_vk_check_results_1(ctx, cgraph, tensor_idx);
  9394. #endif
  9395. }
  9396. if (tensor_idx == subctx->exit_tensor_idx) {
  9397. // Do staging buffer copies
  9398. for (auto& cpy : subctx->out_memcpys) {
  9399. memcpy(cpy.dst, cpy.src, cpy.n);
  9400. }
  9401. subctx->in_memcpys.clear();
  9402. subctx->out_memcpys.clear();
  9403. }
  9404. return true;
  9405. }
  9406. // Clean up after graph processing is done
  9407. static void ggml_vk_graph_cleanup(ggml_backend_vk_context * ctx) {
  9408. VK_LOG_DEBUG("ggml_vk_graph_cleanup()");
  9409. for (auto& buffer : ctx->gc.temp_buffers) {
  9410. ggml_vk_pool_free(ctx, buffer);
  9411. }
  9412. ctx->gc.temp_buffers.clear();
  9413. ctx->prealloc_y_last_pipeline_used = {};
  9414. ctx->unsynced_nodes_written.clear();
  9415. ctx->unsynced_nodes_read.clear();
  9416. ctx->prealloc_x_need_sync = ctx->prealloc_y_need_sync = ctx->prealloc_split_k_need_sync = false;
  9417. ggml_vk_command_pool_cleanup(ctx->device, ctx->compute_cmd_pool);
  9418. ggml_vk_command_pool_cleanup(ctx->device, ctx->transfer_cmd_pool);
  9419. for (size_t i = 0; i < ctx->gc.semaphores.size(); i++) {
  9420. ctx->device->device.destroySemaphore({ ctx->gc.semaphores[i].s });
  9421. }
  9422. ctx->gc.semaphores.clear();
  9423. for (size_t i = 0; i < ctx->gc.tl_semaphores.size(); i++) {
  9424. ctx->device->device.destroySemaphore({ ctx->gc.tl_semaphores[i].s });
  9425. }
  9426. ctx->gc.tl_semaphores.clear();
  9427. ctx->semaphore_idx = 0;
  9428. ctx->event_idx = 0;
  9429. for (auto& event : ctx->gc.events) {
  9430. ctx->device->device.resetEvent(event);
  9431. }
  9432. ctx->tensor_ctxs.clear();
  9433. ctx->gc.contexts.clear();
  9434. ctx->pipeline_descriptor_set_requirements = 0;
  9435. ctx->descriptor_set_idx = 0;
  9436. }
  9437. // Clean up on backend free
  9438. static void ggml_vk_cleanup(ggml_backend_vk_context * ctx) {
  9439. VK_LOG_DEBUG("ggml_vk_cleanup(" << ctx->name << ")");
  9440. ggml_vk_graph_cleanup(ctx);
  9441. ggml_vk_destroy_buffer(ctx->prealloc_x);
  9442. ggml_vk_destroy_buffer(ctx->prealloc_y);
  9443. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  9444. ctx->prealloc_y_last_pipeline_used = nullptr;
  9445. for (auto& buffer : ctx->buffer_pool) {
  9446. ggml_vk_destroy_buffer(buffer);
  9447. }
  9448. ctx->prealloc_size_x = 0;
  9449. ctx->prealloc_size_y = 0;
  9450. ctx->prealloc_size_split_k = 0;
  9451. for (auto& event : ctx->gc.events) {
  9452. ctx->device->device.destroyEvent(event);
  9453. }
  9454. ctx->gc.events.clear();
  9455. ctx->device->device.destroyFence(ctx->fence);
  9456. ctx->device->device.destroyFence(ctx->almost_ready_fence);
  9457. for (auto& pool : ctx->descriptor_pools) {
  9458. ctx->device->device.destroyDescriptorPool(pool);
  9459. }
  9460. ctx->descriptor_pools.clear();
  9461. ctx->descriptor_sets.clear();
  9462. ctx->compute_cmd_pool.destroy(ctx->device->device);
  9463. ctx->transfer_cmd_pool.destroy(ctx->device->device);
  9464. }
  9465. static int ggml_vk_get_device_count() {
  9466. ggml_vk_instance_init();
  9467. return vk_instance.device_indices.size();
  9468. }
  9469. static void ggml_vk_get_device_description(int device, char * description, size_t description_size) {
  9470. ggml_vk_instance_init();
  9471. std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
  9472. vk::PhysicalDeviceProperties props;
  9473. devices[device].getProperties(&props);
  9474. snprintf(description, description_size, "%s", props.deviceName.data());
  9475. }
  9476. // backend interface
  9477. #define UNUSED GGML_UNUSED
  9478. // device backend
  9479. static bool ggml_backend_buffer_is_vk(ggml_backend_buffer_t buffer) {
  9480. return buffer->buft->iface.get_name == ggml_backend_vk_buffer_type_name;
  9481. }
  9482. static void ggml_backend_vk_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  9483. VK_LOG_MEMORY("ggml_backend_vk_buffer_free_buffer()");
  9484. ggml_backend_vk_buffer_context * ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  9485. ggml_vk_destroy_buffer(ctx->dev_buffer);
  9486. delete ctx;
  9487. }
  9488. static void * ggml_backend_vk_buffer_get_base(ggml_backend_buffer_t buffer) {
  9489. return vk_ptr_base;
  9490. UNUSED(buffer);
  9491. }
  9492. static enum ggml_status ggml_backend_vk_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
  9493. VK_LOG_DEBUG("ggml_backend_vk_buffer_init_tensor(" << buffer << " (" << buffer->context << "), " << tensor << ")");
  9494. if (tensor->view_src != nullptr) {
  9495. GGML_ASSERT(tensor->view_src->buffer->buft == buffer->buft);
  9496. }
  9497. return GGML_STATUS_SUCCESS;
  9498. }
  9499. static void ggml_backend_vk_buffer_memset_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor, uint8_t value, size_t offset, size_t size) {
  9500. VK_LOG_DEBUG("ggml_backend_vk_buffer_memset_tensor(" << buffer << ", " << tensor << ", " << value << ", " << offset << ", " << size << ")");
  9501. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  9502. vk_buffer buf = buf_ctx->dev_buffer;
  9503. uint32_t val32 = (uint32_t)value * 0x01010101;
  9504. ggml_vk_buffer_memset(buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, val32, size);
  9505. }
  9506. static void ggml_backend_vk_buffer_set_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
  9507. VK_LOG_DEBUG("ggml_backend_vk_buffer_set_tensor(" << buffer << ", " << tensor << ", " << data << ", " << offset << ", " << size << ")");
  9508. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  9509. vk_buffer buf = buf_ctx->dev_buffer;
  9510. ggml_vk_buffer_write(buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  9511. }
  9512. static void ggml_backend_vk_buffer_get_tensor(ggml_backend_buffer_t buffer, const ggml_tensor * tensor, void * data, size_t offset, size_t size) {
  9513. VK_LOG_DEBUG("ggml_backend_vk_buffer_get_tensor(" << buffer << ", " << tensor << ", " << data << ", " << offset << ", " << size << ")");
  9514. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  9515. vk_buffer buf = buf_ctx->dev_buffer;
  9516. ggml_vk_buffer_read(buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  9517. }
  9518. static bool ggml_backend_vk_buffer_cpy_tensor(ggml_backend_buffer_t buffer, const ggml_tensor * src, ggml_tensor * dst) {
  9519. if (ggml_backend_buffer_is_vk(src->buffer)) {
  9520. ggml_backend_vk_buffer_context * src_buf_ctx = (ggml_backend_vk_buffer_context *)src->buffer->context;
  9521. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  9522. vk_buffer src_buf = src_buf_ctx->dev_buffer;
  9523. vk_buffer dst_buf = dst_buf_ctx->dev_buffer;
  9524. ggml_vk_buffer_copy(dst_buf, vk_tensor_offset(dst) + dst->view_offs, src_buf, vk_tensor_offset(src) + src->view_offs, ggml_nbytes(src));
  9525. return true;
  9526. }
  9527. return false;
  9528. UNUSED(buffer);
  9529. }
  9530. static void ggml_backend_vk_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  9531. ggml_backend_vk_buffer_context * ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  9532. ggml_vk_buffer_memset(ctx->dev_buffer, 0, value, buffer->size);
  9533. }
  9534. static ggml_backend_buffer_i ggml_backend_vk_buffer_interface = {
  9535. /* .free_buffer = */ ggml_backend_vk_buffer_free_buffer,
  9536. /* .get_base = */ ggml_backend_vk_buffer_get_base,
  9537. /* .init_tensor = */ ggml_backend_vk_buffer_init_tensor,
  9538. /* .memset_tensor = */ ggml_backend_vk_buffer_memset_tensor,
  9539. /* .set_tensor = */ ggml_backend_vk_buffer_set_tensor,
  9540. /* .get_tensor = */ ggml_backend_vk_buffer_get_tensor,
  9541. /* .cpy_tensor = */ ggml_backend_vk_buffer_cpy_tensor,
  9542. /* .clear = */ ggml_backend_vk_buffer_clear,
  9543. /* .reset = */ NULL,
  9544. };
  9545. // vk buffer type
  9546. static const char * ggml_backend_vk_buffer_type_name(ggml_backend_buffer_type_t buft) {
  9547. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *)buft->context;
  9548. return ctx->name.c_str();
  9549. }
  9550. static ggml_backend_buffer_t ggml_backend_vk_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  9551. VK_LOG_MEMORY("ggml_backend_vk_buffer_type_alloc_buffer(" << size << ")");
  9552. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *) buft->context;
  9553. vk_buffer dev_buffer = nullptr;
  9554. try {
  9555. dev_buffer = ggml_vk_create_buffer_device(ctx->device, size);
  9556. } catch (const vk::SystemError& e) {
  9557. return nullptr;
  9558. }
  9559. ggml_backend_vk_buffer_context * bufctx = new ggml_backend_vk_buffer_context(ctx->device, std::move(dev_buffer), ctx->name);
  9560. return ggml_backend_buffer_init(buft, ggml_backend_vk_buffer_interface, bufctx, size);
  9561. }
  9562. static size_t ggml_backend_vk_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  9563. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *) buft->context;
  9564. return ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  9565. }
  9566. static size_t ggml_backend_vk_buffer_type_get_max_size(ggml_backend_buffer_type_t buft) {
  9567. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *) buft->context;
  9568. return ctx->device->suballocation_block_size;
  9569. }
  9570. static size_t ggml_backend_vk_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  9571. return ggml_nbytes(tensor);
  9572. UNUSED(buft);
  9573. }
  9574. ggml_backend_buffer_type_t ggml_backend_vk_buffer_type(size_t dev_num) {
  9575. ggml_vk_instance_init();
  9576. VK_LOG_DEBUG("ggml_backend_vk_buffer_type(" << dev_num << ")");
  9577. vk_device dev = ggml_vk_get_device(dev_num);
  9578. return &dev->buffer_type;
  9579. }
  9580. // host buffer type
  9581. static const char * ggml_backend_vk_host_buffer_type_name(ggml_backend_buffer_type_t buft) {
  9582. return GGML_VK_NAME "_Host";
  9583. UNUSED(buft);
  9584. }
  9585. static const char * ggml_backend_vk_host_buffer_name(ggml_backend_buffer_t buffer) {
  9586. return GGML_VK_NAME "_Host";
  9587. UNUSED(buffer);
  9588. }
  9589. static void ggml_backend_vk_host_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  9590. VK_LOG_MEMORY("ggml_backend_vk_host_buffer_free_buffer()");
  9591. ggml_vk_host_free(vk_instance.devices[0], buffer->context);
  9592. }
  9593. static ggml_backend_buffer_t ggml_backend_vk_host_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  9594. VK_LOG_MEMORY("ggml_backend_vk_host_buffer_type_alloc_buffer(" << size << ")");
  9595. size += 32; // Behave like the CPU buffer type
  9596. void * ptr = nullptr;
  9597. try {
  9598. ptr = ggml_vk_host_malloc(vk_instance.devices[0], size);
  9599. } catch (vk::SystemError& e) {
  9600. GGML_LOG_WARN("ggml_vulkan: Failed to allocate pinned memory (%s)\n", e.what());
  9601. // fallback to cpu buffer
  9602. return ggml_backend_buft_alloc_buffer(ggml_backend_cpu_buffer_type(), size);
  9603. }
  9604. ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size);
  9605. buffer->buft = buft;
  9606. buffer->iface.free_buffer = ggml_backend_vk_host_buffer_free_buffer;
  9607. return buffer;
  9608. UNUSED(buft);
  9609. }
  9610. static size_t ggml_backend_vk_host_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  9611. return vk_instance.devices[0]->properties.limits.minMemoryMapAlignment;
  9612. UNUSED(buft);
  9613. }
  9614. static size_t ggml_backend_vk_host_buffer_type_get_max_size(ggml_backend_buffer_type_t buft) {
  9615. return vk_instance.devices[0]->suballocation_block_size;
  9616. UNUSED(buft);
  9617. }
  9618. // Should be changed to return device-specific host buffer type
  9619. // but that probably requires changes in llama.cpp
  9620. ggml_backend_buffer_type_t ggml_backend_vk_host_buffer_type() {
  9621. static struct ggml_backend_buffer_type ggml_backend_vk_buffer_type_host = {
  9622. /* .iface = */ {
  9623. /* .get_name = */ ggml_backend_vk_host_buffer_type_name,
  9624. /* .alloc_buffer = */ ggml_backend_vk_host_buffer_type_alloc_buffer,
  9625. /* .get_alignment = */ ggml_backend_vk_host_buffer_type_get_alignment,
  9626. /* .get_max_size = */ ggml_backend_vk_host_buffer_type_get_max_size,
  9627. /* .get_alloc_size = */ ggml_backend_cpu_buffer_type()->iface.get_alloc_size,
  9628. /* .is_host = */ ggml_backend_cpu_buffer_type()->iface.is_host,
  9629. },
  9630. /* .device = */ ggml_backend_reg_dev_get(ggml_backend_vk_reg(), 0),
  9631. /* .context = */ nullptr,
  9632. };
  9633. // Make sure device 0 is initialized
  9634. ggml_vk_instance_init();
  9635. ggml_vk_get_device(0);
  9636. return &ggml_backend_vk_buffer_type_host;
  9637. }
  9638. // backend
  9639. static const char * ggml_backend_vk_name(ggml_backend_t backend) {
  9640. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  9641. return ctx->name.c_str();
  9642. }
  9643. static void ggml_backend_vk_free(ggml_backend_t backend) {
  9644. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  9645. VK_LOG_DEBUG("ggml_backend_vk_free(" << ctx->name << ")");
  9646. ggml_vk_cleanup(ctx);
  9647. delete ctx;
  9648. delete backend;
  9649. }
  9650. static ggml_backend_buffer_type_t ggml_backend_vk_get_default_buffer_type(ggml_backend_t backend) {
  9651. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  9652. return &ctx->device->buffer_type;
  9653. }
  9654. static void ggml_backend_vk_set_tensor_async(ggml_backend_t backend, ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
  9655. VK_LOG_DEBUG("ggml_backend_vk_set_tensor_async(" << size << ")");
  9656. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  9657. GGML_ASSERT((tensor->buffer->buft == ggml_backend_vk_get_default_buffer_type(backend) || tensor->buffer->buft == ggml_backend_vk_host_buffer_type()) && "unsupported buffer type");
  9658. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  9659. vk_context transfer_ctx;
  9660. if (ctx->transfer_ctx.expired()) {
  9661. // Initialize new transfer context
  9662. transfer_ctx = ggml_vk_create_context(ctx, ctx->transfer_cmd_pool);
  9663. ctx->transfer_ctx = transfer_ctx;
  9664. ggml_vk_ctx_begin(ctx->device, transfer_ctx);
  9665. } else {
  9666. transfer_ctx = ctx->transfer_ctx.lock();
  9667. }
  9668. vk_buffer buf = buf_ctx->dev_buffer;
  9669. ggml_vk_buffer_write_async(transfer_ctx, buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  9670. }
  9671. static void ggml_backend_vk_get_tensor_async(ggml_backend_t backend, const ggml_tensor * tensor, void * data, size_t offset, size_t size) {
  9672. VK_LOG_DEBUG("ggml_backend_vk_get_tensor_async(" << size << ")");
  9673. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  9674. GGML_ASSERT((tensor->buffer->buft == ggml_backend_vk_get_default_buffer_type(backend) || tensor->buffer->buft == ggml_backend_vk_host_buffer_type()) && "unsupported buffer type");
  9675. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  9676. vk_context transfer_ctx;
  9677. if (ctx->transfer_ctx.expired()) {
  9678. // Initialize new transfer context
  9679. transfer_ctx = ggml_vk_create_context(ctx, ctx->transfer_cmd_pool);
  9680. ctx->transfer_ctx = transfer_ctx;
  9681. ggml_vk_ctx_begin(ctx->device, transfer_ctx);
  9682. } else {
  9683. transfer_ctx = ctx->transfer_ctx.lock();
  9684. }
  9685. vk_buffer buf = buf_ctx->dev_buffer;
  9686. ggml_vk_buffer_read_async(transfer_ctx, buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  9687. }
  9688. static bool ggml_backend_vk_cpy_tensor_async(ggml_backend_t backend, const ggml_tensor * src, ggml_tensor * dst) {
  9689. VK_LOG_DEBUG("ggml_backend_vk_cpy_tensor_async()");
  9690. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  9691. if ((dst->buffer->buft == ggml_backend_vk_get_default_buffer_type(backend) || dst->buffer->buft == ggml_backend_vk_host_buffer_type()) && ggml_backend_buffer_is_vk(src->buffer)) {
  9692. ggml_backend_vk_buffer_context * src_buf_ctx = (ggml_backend_vk_buffer_context *)src->buffer->context;
  9693. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  9694. vk_context transfer_ctx;
  9695. if (ctx->transfer_ctx.expired()) {
  9696. // Initialize new transfer context
  9697. transfer_ctx = ggml_vk_create_context(ctx, ctx->transfer_cmd_pool);
  9698. ctx->transfer_ctx = transfer_ctx;
  9699. ggml_vk_ctx_begin(ctx->device, transfer_ctx);
  9700. } else {
  9701. transfer_ctx = ctx->transfer_ctx.lock();
  9702. }
  9703. vk_buffer src_buf = src_buf_ctx->dev_buffer;
  9704. vk_buffer dst_buf = dst_buf_ctx->dev_buffer;
  9705. ggml_vk_buffer_copy_async(transfer_ctx, dst_buf, vk_tensor_offset(dst) + dst->view_offs, src_buf, vk_tensor_offset(src) + src->view_offs, ggml_nbytes(src));
  9706. return true;
  9707. }
  9708. return false;
  9709. }
  9710. static void ggml_backend_vk_synchronize(ggml_backend_t backend) {
  9711. VK_LOG_DEBUG("ggml_backend_vk_synchronize()");
  9712. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  9713. if(ctx->transfer_ctx.expired()) {
  9714. return;
  9715. }
  9716. vk_context transfer_ctx = ctx->transfer_ctx.lock();
  9717. ggml_vk_ctx_end(transfer_ctx);
  9718. for (auto& cpy : transfer_ctx->in_memcpys) {
  9719. memcpy(cpy.dst, cpy.src, cpy.n);
  9720. }
  9721. ggml_vk_submit(transfer_ctx, ctx->fence);
  9722. ggml_vk_wait_for_fence(ctx);
  9723. for (auto& cpy : transfer_ctx->out_memcpys) {
  9724. memcpy(cpy.dst, cpy.src, cpy.n);
  9725. }
  9726. ctx->transfer_ctx.reset();
  9727. }
  9728. static bool ggml_vk_is_empty(ggml_tensor * node) {
  9729. return ggml_is_empty(node) || node->op == GGML_OP_NONE || node->op == GGML_OP_RESHAPE || node->op == GGML_OP_TRANSPOSE || node->op == GGML_OP_VIEW || node->op == GGML_OP_PERMUTE;
  9730. }
  9731. static bool ggml_vk_can_fuse(const struct ggml_cgraph * cgraph, int node_idx, std::initializer_list<enum ggml_op> ops) {
  9732. if (!ggml_can_fuse(cgraph, node_idx, ops)) {
  9733. return false;
  9734. }
  9735. if (ops.size() == 2 && ops.begin()[0] == GGML_OP_RMS_NORM && ops.begin()[1] == GGML_OP_MUL) {
  9736. // additional constraints specific to this fusion
  9737. const ggml_tensor *rms_norm = cgraph->nodes[node_idx];
  9738. const ggml_tensor *mul = cgraph->nodes[node_idx + 1];
  9739. GGML_ASSERT(rms_norm->src[0]->type == GGML_TYPE_F32);
  9740. GGML_ASSERT(rms_norm->type == GGML_TYPE_F32);
  9741. // rms_norm only supports f32
  9742. if (mul->src[0]->type != GGML_TYPE_F32 ||
  9743. mul->src[1]->type != GGML_TYPE_F32 ||
  9744. mul->type != GGML_TYPE_F32) {
  9745. return false;
  9746. }
  9747. // if rms_norm is the B operand, then we don't handle broadcast
  9748. if (rms_norm == mul->src[1] &&
  9749. !ggml_are_same_shape(mul->src[0], rms_norm)) {
  9750. return false;
  9751. }
  9752. // rms_norm shader assumes contiguous rows
  9753. if (!ggml_is_contiguous_rows(mul->src[0]) || !ggml_is_contiguous_rows(mul->src[1])) {
  9754. return false;
  9755. }
  9756. }
  9757. return true;
  9758. }
  9759. static uint32_t ggml_vk_fuse_multi_add(ggml_backend_vk_context * ctx, const struct ggml_cgraph * cgraph, int node_idx) {
  9760. const ggml_tensor *first_node = cgraph->nodes[node_idx];
  9761. if (first_node->op != GGML_OP_ADD) {
  9762. return 0;
  9763. }
  9764. if (!ctx->device->multi_add) {
  9765. return 0;
  9766. }
  9767. int32_t num_adds = 1;
  9768. while (node_idx + num_adds < cgraph->n_nodes &&
  9769. cgraph->nodes[node_idx + num_adds]->op == GGML_OP_ADD &&
  9770. num_adds < MAX_FUSED_ADDS) {
  9771. num_adds++;
  9772. }
  9773. // The shader currently requires same shapes (but different strides are allowed),
  9774. // everything f32, and no misalignment
  9775. for (int32_t i = 0; i < num_adds; ++i) {
  9776. const ggml_tensor *next_node = cgraph->nodes[node_idx + i];
  9777. if (!ggml_are_same_shape(first_node, next_node->src[0]) ||
  9778. !ggml_are_same_shape(first_node, next_node->src[1]) ||
  9779. next_node->type != GGML_TYPE_F32 ||
  9780. next_node->src[0]->type != GGML_TYPE_F32 ||
  9781. next_node->src[1]->type != GGML_TYPE_F32 ||
  9782. get_misalign_bytes(ctx, next_node) ||
  9783. get_misalign_bytes(ctx, next_node->src[0]) ||
  9784. get_misalign_bytes(ctx, next_node->src[1])) {
  9785. num_adds = i;
  9786. }
  9787. }
  9788. // Verify we can fuse these
  9789. ggml_op adds[MAX_FUSED_ADDS];
  9790. for (int32_t i = 0; i < num_adds; ++i) {
  9791. adds[i] = GGML_OP_ADD;
  9792. }
  9793. // decrease num_adds if they can't all be fused
  9794. while (num_adds > 1 && !ggml_can_fuse(cgraph, node_idx, adds, num_adds)) {
  9795. num_adds--;
  9796. }
  9797. // a single add is not "fused", so just return zero
  9798. if (num_adds == 1) {
  9799. return 0;
  9800. }
  9801. return num_adds;
  9802. }
  9803. static ggml_status ggml_backend_vk_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
  9804. VK_LOG_DEBUG("ggml_backend_vk_graph_compute(" << cgraph->n_nodes << " nodes)");
  9805. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  9806. if (vk_instance.debug_utils_support) {
  9807. vk::DebugUtilsLabelEXT dul = {};
  9808. dul.pLabelName = "ggml_backend_vk_graph_compute";
  9809. dul.color = std::array<float,4>{1.0f, 1.0f, 1.0f, 1.0f};
  9810. vk_instance.pfn_vkQueueBeginDebugUtilsLabelEXT(ctx->device->compute_queue.queue, reinterpret_cast<VkDebugUtilsLabelEXT*>(&dul));
  9811. }
  9812. ctx->prealloc_size_add_rms_partials = 0;
  9813. ctx->prealloc_size_add_rms_partials_offset = 0;
  9814. ctx->do_add_rms_partials = false;
  9815. uint64_t total_mat_mul_bytes = 0;
  9816. for (int i = 0; i < cgraph->n_nodes; i++) {
  9817. if (!ctx->device->disable_fusion) {
  9818. uint32_t num_adds = ggml_vk_fuse_multi_add(ctx, cgraph, i);
  9819. if (num_adds) {
  9820. ctx->num_additional_fused_ops = num_adds - 1;
  9821. } else if (ggml_vk_can_fuse(cgraph, i, { GGML_OP_RMS_NORM, GGML_OP_MUL })) {
  9822. ctx->num_additional_fused_ops = 1;
  9823. }
  9824. }
  9825. ggml_vk_build_graph(ctx, cgraph, i, nullptr, 0, true, false, false, false);
  9826. if (cgraph->nodes[i]->op == GGML_OP_MUL_MAT || cgraph->nodes[i]->op == GGML_OP_MUL_MAT_ID) {
  9827. total_mat_mul_bytes += ggml_nbytes(cgraph->nodes[i]->src[0]);
  9828. } else if (cgraph->nodes[i]->op == GGML_OP_CONV_2D) {
  9829. // Return CRSxNPQxsizeof(*) to account as many bytes as mul_mat has in im2col->mul_mat mode.
  9830. auto CRS_size =
  9831. cgraph->nodes[i]->src[0]->ne[0] * cgraph->nodes[i]->src[0]->ne[1] * cgraph->nodes[i]->src[0]->ne[2];
  9832. auto NPQ_size = cgraph->nodes[i]->ne[0] * cgraph->nodes[i]->ne[1] * cgraph->nodes[i]->ne[3];
  9833. total_mat_mul_bytes += NPQ_size * CRS_size * ggml_type_size(cgraph->nodes[i]->type);
  9834. }
  9835. i += ctx->num_additional_fused_ops;
  9836. ctx->num_additional_fused_ops = 0;
  9837. }
  9838. if (ctx->device->need_compiles) {
  9839. ggml_vk_load_shaders(ctx->device);
  9840. }
  9841. ggml_vk_preallocate_buffers(ctx);
  9842. ggml_pipeline_allocate_descriptor_sets(ctx);
  9843. int last_node = cgraph->n_nodes - 1;
  9844. // If the last op in the cgraph isn't backend GPU, the command buffer doesn't get closed properly
  9845. while (last_node > 0 && ggml_vk_is_empty(cgraph->nodes[last_node])) {
  9846. last_node -= 1;
  9847. }
  9848. // Reserve tensor context space for all nodes
  9849. ctx->tensor_ctxs.resize(cgraph->n_nodes);
  9850. bool first_node_in_batch = true; // true if next node will be first node in a batch
  9851. int submit_node_idx = 0; // index to first node in a batch
  9852. vk_context compute_ctx;
  9853. if (vk_perf_logger_enabled) {
  9854. // allocate/resize the query pool
  9855. if (ctx->device->num_queries < cgraph->n_nodes + 1) {
  9856. if (ctx->device->query_pool) {
  9857. ctx->device->device.destroyQueryPool(ctx->device->query_pool);
  9858. }
  9859. vk::QueryPoolCreateInfo query_create_info;
  9860. query_create_info.queryType = vk::QueryType::eTimestamp;
  9861. query_create_info.queryCount = cgraph->n_nodes + 100;
  9862. ctx->device->query_pool = ctx->device->device.createQueryPool(query_create_info);
  9863. ctx->device->num_queries = query_create_info.queryCount;
  9864. }
  9865. ctx->device->device.resetQueryPool(ctx->device->query_pool, 0, cgraph->n_nodes+1);
  9866. GGML_ASSERT(ctx->compute_ctx.expired());
  9867. compute_ctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  9868. ctx->compute_ctx = compute_ctx;
  9869. ggml_vk_ctx_begin(ctx->device, compute_ctx);
  9870. compute_ctx->s->buffer.writeTimestamp(vk::PipelineStageFlagBits::eAllCommands, ctx->device->query_pool, 0);
  9871. }
  9872. ctx->prealloc_y_last_pipeline_used = nullptr;
  9873. ctx->prealloc_y_last_tensor_used = nullptr;
  9874. if (ctx->prealloc_size_add_rms_partials) {
  9875. if (ctx->compute_ctx.expired()) {
  9876. compute_ctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  9877. ctx->compute_ctx = compute_ctx;
  9878. ggml_vk_ctx_begin(ctx->device, compute_ctx);
  9879. } else {
  9880. compute_ctx = ctx->compute_ctx.lock();
  9881. }
  9882. // initialize partial sums to zero.
  9883. ggml_vk_buffer_memset_async(compute_ctx, ctx->prealloc_add_rms_partials, 0, 0, ctx->prealloc_size_add_rms_partials);
  9884. ggml_vk_sync_buffers(ctx, compute_ctx);
  9885. }
  9886. // Submit after enough work has accumulated, to overlap CPU cmdbuffer generation with GPU execution.
  9887. // Estimate the amount of matmul work by looking at the weight matrix size, and submit every 100MB
  9888. // (and scaled down based on model size, so smaller models submit earlier).
  9889. // Also submit at least every 100 nodes, in case there are workloads without as much matmul.
  9890. int nodes_per_submit = 100;
  9891. int submitted_nodes = 0;
  9892. int submit_count = 0;
  9893. uint64_t mul_mat_bytes = 0;
  9894. uint64_t mul_mat_bytes_per_submit = std::min(uint64_t(100*1000*1000), total_mat_mul_bytes / 40u);
  9895. for (int i = 0; i < cgraph->n_nodes; i++) {
  9896. if (first_node_in_batch) {
  9897. submit_node_idx = i;
  9898. }
  9899. if (cgraph->nodes[i]->op == GGML_OP_MUL_MAT || cgraph->nodes[i]->op == GGML_OP_MUL_MAT_ID) {
  9900. mul_mat_bytes += ggml_nbytes(cgraph->nodes[i]->src[0]);
  9901. }
  9902. if (!ctx->device->disable_fusion) {
  9903. uint32_t num_adds = ggml_vk_fuse_multi_add(ctx, cgraph, i);
  9904. if (num_adds) {
  9905. ctx->num_additional_fused_ops = num_adds - 1;
  9906. } else if (ggml_vk_can_fuse(cgraph, i, { GGML_OP_RMS_NORM, GGML_OP_MUL })) {
  9907. ctx->num_additional_fused_ops = 1;
  9908. }
  9909. }
  9910. // Signal the almost_ready fence when the graph is mostly complete (< 20% remaining)
  9911. bool almost_ready = (cgraph->n_nodes - i) < cgraph->n_nodes / 5;
  9912. bool submit = (submitted_nodes >= nodes_per_submit) ||
  9913. (mul_mat_bytes >= mul_mat_bytes_per_submit) ||
  9914. (i + ctx->num_additional_fused_ops == last_node) ||
  9915. (almost_ready && !ctx->almost_ready_fence_pending);
  9916. bool enqueued = ggml_vk_build_graph(ctx, cgraph, i, cgraph->nodes[submit_node_idx], submit_node_idx, false, i + ctx->num_additional_fused_ops == last_node, almost_ready, submit);
  9917. if (vk_perf_logger_enabled) {
  9918. if (ctx->compute_ctx.expired()) {
  9919. compute_ctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  9920. ctx->compute_ctx = compute_ctx;
  9921. ggml_vk_ctx_begin(ctx->device, compute_ctx);
  9922. } else {
  9923. compute_ctx = ctx->compute_ctx.lock();
  9924. }
  9925. // If there are fused ops, just write out timestamps for all nodes to keep the accounting simple
  9926. for (int j = 0; j < ctx->num_additional_fused_ops + 1; ++j) {
  9927. compute_ctx->s->buffer.writeTimestamp(vk::PipelineStageFlagBits::eAllCommands, ctx->device->query_pool, i+j+1);
  9928. }
  9929. }
  9930. if (enqueued) {
  9931. ++submitted_nodes;
  9932. #ifndef GGML_VULKAN_CHECK_RESULTS
  9933. if (first_node_in_batch) {
  9934. first_node_in_batch = false;
  9935. }
  9936. #endif
  9937. }
  9938. if (submit && enqueued) {
  9939. first_node_in_batch = true;
  9940. submitted_nodes = 0;
  9941. mul_mat_bytes = 0;
  9942. if (submit_count < 3) {
  9943. mul_mat_bytes_per_submit *= 2;
  9944. }
  9945. submit_count++;
  9946. }
  9947. i += ctx->num_additional_fused_ops;
  9948. ctx->num_additional_fused_ops = 0;
  9949. }
  9950. if (vk_perf_logger_enabled) {
  9951. // End the command buffer and submit/wait
  9952. GGML_ASSERT(!ctx->compute_ctx.expired());
  9953. compute_ctx = ctx->compute_ctx.lock();
  9954. ggml_vk_ctx_end(compute_ctx);
  9955. ggml_vk_submit(compute_ctx, ctx->device->fence);
  9956. VK_CHECK(ctx->device->device.waitForFences({ ctx->device->fence }, true, UINT64_MAX), "GGML_VULKAN_PERF waitForFences");
  9957. ctx->device->device.resetFences({ ctx->device->fence });
  9958. // Get the results and pass them to the logger
  9959. std::vector<uint64_t> timestamps(cgraph->n_nodes + 1);
  9960. VK_CHECK(ctx->device->device.getQueryPoolResults(ctx->device->query_pool, 0, cgraph->n_nodes + 1, (cgraph->n_nodes + 1)*sizeof(uint64_t), timestamps.data(), sizeof(uint64_t), vk::QueryResultFlagBits::e64 | vk::QueryResultFlagBits::eWait), "get timestamp results");
  9961. for (int i = 0; i < cgraph->n_nodes; i++) {
  9962. if (!ggml_vk_is_empty(cgraph->nodes[i])) {
  9963. ctx->device->perf_logger->log_timing(cgraph->nodes[i], uint64_t((timestamps[i+1] - timestamps[i]) * ctx->device->properties.limits.timestampPeriod));
  9964. }
  9965. }
  9966. ctx->device->perf_logger->print_timings();
  9967. }
  9968. ggml_vk_graph_cleanup(ctx);
  9969. return GGML_STATUS_SUCCESS;
  9970. UNUSED(backend);
  9971. }
  9972. // TODO: enable async and synchronize
  9973. static ggml_backend_i ggml_backend_vk_interface = {
  9974. /* .get_name = */ ggml_backend_vk_name,
  9975. /* .free = */ ggml_backend_vk_free,
  9976. /* .set_tensor_async = */ NULL, // ggml_backend_vk_set_tensor_async,
  9977. /* .get_tensor_async = */ NULL, // ggml_backend_vk_get_tensor_async,
  9978. /* .cpy_tensor_async = */ NULL, // ggml_backend_vk_cpy_tensor_async,
  9979. /* .synchronize = */ NULL, // ggml_backend_vk_synchronize,
  9980. /* .graph_plan_create = */ NULL,
  9981. /* .graph_plan_free = */ NULL,
  9982. /* .graph_plan_update = */ NULL,
  9983. /* .graph_plan_compute = */ NULL,
  9984. /* .graph_compute = */ ggml_backend_vk_graph_compute,
  9985. /* .event_record = */ NULL,
  9986. /* .event_wait = */ NULL,
  9987. };
  9988. static ggml_guid_t ggml_backend_vk_guid() {
  9989. static ggml_guid guid = { 0xb8, 0xf7, 0x4f, 0x86, 0x40, 0x3c, 0xe1, 0x02, 0x91, 0xc8, 0xdd, 0xe9, 0x02, 0x3f, 0xc0, 0x2b };
  9990. return &guid;
  9991. }
  9992. ggml_backend_t ggml_backend_vk_init(size_t dev_num) {
  9993. VK_LOG_DEBUG("ggml_backend_vk_init(" << dev_num << ")");
  9994. ggml_backend_vk_context * ctx = new ggml_backend_vk_context;
  9995. ggml_vk_init(ctx, dev_num);
  9996. ggml_backend_t vk_backend = new ggml_backend {
  9997. /* .guid = */ ggml_backend_vk_guid(),
  9998. /* .iface = */ ggml_backend_vk_interface,
  9999. /* .device = */ ggml_backend_reg_dev_get(ggml_backend_vk_reg(), dev_num),
  10000. /* .context = */ ctx,
  10001. };
  10002. return vk_backend;
  10003. }
  10004. bool ggml_backend_is_vk(ggml_backend_t backend) {
  10005. return backend != NULL && ggml_guid_matches(backend->guid, ggml_backend_vk_guid());
  10006. }
  10007. int ggml_backend_vk_get_device_count() {
  10008. return ggml_vk_get_device_count();
  10009. }
  10010. void ggml_backend_vk_get_device_description(int device, char * description, size_t description_size) {
  10011. GGML_ASSERT(device < (int) vk_instance.device_indices.size());
  10012. int dev_idx = vk_instance.device_indices[device];
  10013. ggml_vk_get_device_description(dev_idx, description, description_size);
  10014. }
  10015. void ggml_backend_vk_get_device_memory(int device, size_t * free, size_t * total) {
  10016. GGML_ASSERT(device < (int) vk_instance.device_indices.size());
  10017. GGML_ASSERT(device < (int) vk_instance.device_supports_membudget.size());
  10018. vk::PhysicalDevice vkdev = vk_instance.instance.enumeratePhysicalDevices()[vk_instance.device_indices[device]];
  10019. vk::PhysicalDeviceMemoryBudgetPropertiesEXT budgetprops;
  10020. vk::PhysicalDeviceMemoryProperties2 memprops = {};
  10021. bool membudget_supported = vk_instance.device_supports_membudget[device];
  10022. if (membudget_supported) {
  10023. memprops.pNext = &budgetprops;
  10024. }
  10025. vkdev.getMemoryProperties2(&memprops);
  10026. for (uint32_t i = 0; i < memprops.memoryProperties.memoryHeapCount; ++i) {
  10027. const vk::MemoryHeap & heap = memprops.memoryProperties.memoryHeaps[i];
  10028. if (heap.flags & vk::MemoryHeapFlagBits::eDeviceLocal) {
  10029. *total = heap.size;
  10030. if (membudget_supported && i < budgetprops.heapUsage.size()) {
  10031. *free = budgetprops.heapBudget[i] - budgetprops.heapUsage[i];
  10032. } else {
  10033. *free = heap.size;
  10034. }
  10035. break;
  10036. }
  10037. }
  10038. }
  10039. //////////////////////////
  10040. struct ggml_backend_vk_device_context {
  10041. size_t device;
  10042. std::string name;
  10043. std::string description;
  10044. };
  10045. static const char * ggml_backend_vk_device_get_name(ggml_backend_dev_t dev) {
  10046. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  10047. return ctx->name.c_str();
  10048. }
  10049. static const char * ggml_backend_vk_device_get_description(ggml_backend_dev_t dev) {
  10050. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  10051. return ctx->description.c_str();
  10052. }
  10053. static void ggml_backend_vk_device_get_memory(ggml_backend_dev_t device, size_t * free, size_t * total) {
  10054. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)device->context;
  10055. ggml_backend_vk_get_device_memory(ctx->device, free, total);
  10056. }
  10057. static ggml_backend_buffer_type_t ggml_backend_vk_device_get_buffer_type(ggml_backend_dev_t dev) {
  10058. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  10059. return ggml_backend_vk_buffer_type(ctx->device);
  10060. }
  10061. static ggml_backend_buffer_type_t ggml_backend_vk_device_get_host_buffer_type(ggml_backend_dev_t dev) {
  10062. UNUSED(dev);
  10063. return ggml_backend_vk_host_buffer_type();
  10064. }
  10065. static enum ggml_backend_dev_type ggml_backend_vk_device_get_type(ggml_backend_dev_t dev) {
  10066. UNUSED(dev);
  10067. return GGML_BACKEND_DEVICE_TYPE_GPU;
  10068. }
  10069. static void ggml_backend_vk_device_get_props(ggml_backend_dev_t dev, struct ggml_backend_dev_props * props) {
  10070. props->name = ggml_backend_vk_device_get_name(dev);
  10071. props->description = ggml_backend_vk_device_get_description(dev);
  10072. props->type = ggml_backend_vk_device_get_type(dev);
  10073. ggml_backend_vk_device_get_memory(dev, &props->memory_free, &props->memory_total);
  10074. props->caps = {
  10075. /* .async = */ false,
  10076. /* .host_buffer = */ true,
  10077. /* .buffer_from_host_ptr = */ false,
  10078. /* .events = */ false,
  10079. };
  10080. }
  10081. static ggml_backend_t ggml_backend_vk_device_init(ggml_backend_dev_t dev, const char * params) {
  10082. UNUSED(params);
  10083. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  10084. return ggml_backend_vk_init(ctx->device);
  10085. }
  10086. static bool ggml_backend_vk_device_supports_op(ggml_backend_dev_t dev, const ggml_tensor * op) {
  10087. switch (op->op) {
  10088. case GGML_OP_UNARY:
  10089. switch (ggml_get_unary_op(op)) {
  10090. case GGML_UNARY_OP_EXP:
  10091. case GGML_UNARY_OP_GELU:
  10092. case GGML_UNARY_OP_GELU_ERF:
  10093. case GGML_UNARY_OP_GELU_QUICK:
  10094. case GGML_UNARY_OP_SILU:
  10095. case GGML_UNARY_OP_RELU:
  10096. case GGML_UNARY_OP_TANH:
  10097. case GGML_UNARY_OP_SIGMOID:
  10098. case GGML_UNARY_OP_HARDSIGMOID:
  10099. case GGML_UNARY_OP_HARDSWISH:
  10100. return ggml_is_contiguous(op->src[0]) &&
  10101. (op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16) &&
  10102. (op->type == GGML_TYPE_F32 || op->type == GGML_TYPE_F16) &&
  10103. (op->src[0]->type == op->type);
  10104. default:
  10105. return false;
  10106. }
  10107. case GGML_OP_GLU:
  10108. switch (ggml_get_glu_op(op)) {
  10109. case GGML_GLU_OP_GEGLU:
  10110. case GGML_GLU_OP_REGLU:
  10111. case GGML_GLU_OP_SWIGLU:
  10112. case GGML_GLU_OP_SWIGLU_OAI:
  10113. case GGML_GLU_OP_GEGLU_ERF:
  10114. case GGML_GLU_OP_GEGLU_QUICK:
  10115. return ggml_is_contiguous(op->src[0]) &&
  10116. (op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16) &&
  10117. (op->type == GGML_TYPE_F32 || op->type == GGML_TYPE_F16) &&
  10118. (op->src[0]->type == op->type);
  10119. default:
  10120. return false;
  10121. }
  10122. case GGML_OP_MUL_MAT:
  10123. case GGML_OP_MUL_MAT_ID:
  10124. {
  10125. ggml_type src0_type = op->src[0]->type;
  10126. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  10127. const vk_device& device = ggml_vk_get_device(ctx->device);
  10128. if (op->op == GGML_OP_MUL_MAT_ID) {
  10129. if (!device->mul_mat_id_s[src0_type] && !device->mul_mat_id_m[src0_type] && !device->mul_mat_id_l[src0_type]) {
  10130. // If there's not enough shared memory for row_ids and the result tile, fallback to CPU
  10131. return false;
  10132. }
  10133. }
  10134. switch (src0_type) {
  10135. case GGML_TYPE_F32:
  10136. case GGML_TYPE_F16:
  10137. case GGML_TYPE_BF16:
  10138. case GGML_TYPE_Q4_0:
  10139. case GGML_TYPE_Q4_1:
  10140. case GGML_TYPE_Q5_0:
  10141. case GGML_TYPE_Q5_1:
  10142. case GGML_TYPE_Q8_0:
  10143. case GGML_TYPE_Q2_K:
  10144. case GGML_TYPE_Q3_K:
  10145. case GGML_TYPE_Q4_K:
  10146. case GGML_TYPE_Q5_K:
  10147. case GGML_TYPE_Q6_K:
  10148. case GGML_TYPE_IQ1_S:
  10149. case GGML_TYPE_IQ1_M:
  10150. case GGML_TYPE_IQ2_XXS:
  10151. case GGML_TYPE_IQ2_XS:
  10152. case GGML_TYPE_IQ2_S:
  10153. case GGML_TYPE_IQ3_XXS:
  10154. case GGML_TYPE_IQ3_S:
  10155. case GGML_TYPE_IQ4_XS:
  10156. case GGML_TYPE_IQ4_NL:
  10157. case GGML_TYPE_MXFP4:
  10158. break;
  10159. default:
  10160. return false;
  10161. }
  10162. struct ggml_tensor * a;
  10163. struct ggml_tensor * b;
  10164. if (op->op == GGML_OP_MUL_MAT) {
  10165. a = op->src[0];
  10166. b = op->src[1];
  10167. } else {
  10168. a = op->src[2];
  10169. b = op->src[1];
  10170. }
  10171. if (a->ne[3] != b->ne[3]) {
  10172. return false;
  10173. }
  10174. if (!(ggml_vk_dim01_contiguous(op->src[0]) || op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16 || op->src[0]->type == GGML_TYPE_BF16) ||
  10175. !(ggml_vk_dim01_contiguous(op->src[1]) || op->src[1]->type == GGML_TYPE_F32 || op->src[1]->type == GGML_TYPE_F16)) {
  10176. return false;
  10177. }
  10178. if (op->src[0]->type == GGML_TYPE_BF16 && op->src[1]->type == GGML_TYPE_F16) {
  10179. // We currently don't have a bf16 x f16 shader, or an fp16->bf16 copy shader.
  10180. // So don't support this combination for now.
  10181. return false;
  10182. }
  10183. return true;
  10184. }
  10185. case GGML_OP_FLASH_ATTN_EXT:
  10186. {
  10187. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  10188. auto device = ggml_vk_get_device(ctx->device);
  10189. bool coopmat2 = device->coopmat2;
  10190. uint32_t HSK = op->src[1]->ne[0];
  10191. uint32_t HSV = op->src[2]->ne[0];
  10192. if ((HSK % 8) != 0 || (HSV % 8) != 0) {
  10193. return false;
  10194. }
  10195. if (op->src[4] && op->src[4]->type != GGML_TYPE_F32) {
  10196. return false;
  10197. }
  10198. if (op->src[0]->type != GGML_TYPE_F32) {
  10199. return false;
  10200. }
  10201. if (op->type != GGML_TYPE_F32) {
  10202. return false;
  10203. }
  10204. if (op->src[3] && op->src[3]->type != GGML_TYPE_F16) {
  10205. return false;
  10206. }
  10207. // It's straightforward to support different K/V dequant, but would
  10208. // significantly increase the number of pipelines
  10209. if (op->src[1]->type != op->src[2]->type) {
  10210. return false;
  10211. }
  10212. switch (op->src[1]->type) {
  10213. case GGML_TYPE_F16:
  10214. case GGML_TYPE_Q4_0:
  10215. case GGML_TYPE_Q8_0:
  10216. // supported in scalar and coopmat2 paths
  10217. break;
  10218. case GGML_TYPE_Q4_1:
  10219. case GGML_TYPE_Q5_0:
  10220. case GGML_TYPE_Q5_1:
  10221. // K dequants currently disabled because D dimension is rounded up to 256 and runs inefficiently
  10222. //case GGML_TYPE_Q2_K:
  10223. //case GGML_TYPE_Q3_K:
  10224. //case GGML_TYPE_Q4_K:
  10225. //case GGML_TYPE_Q5_K:
  10226. //case GGML_TYPE_Q6_K:
  10227. //case GGML_TYPE_IQ1_S:
  10228. //case GGML_TYPE_IQ1_M:
  10229. //case GGML_TYPE_IQ2_XXS:
  10230. //case GGML_TYPE_IQ2_XS:
  10231. //case GGML_TYPE_IQ2_S:
  10232. //case GGML_TYPE_IQ3_XXS:
  10233. //case GGML_TYPE_IQ3_S:
  10234. //case GGML_TYPE_IQ4_XS:
  10235. case GGML_TYPE_IQ4_NL:
  10236. // currently supported only in coopmat2 path
  10237. if (!coopmat2) {
  10238. return false;
  10239. }
  10240. break;
  10241. default:
  10242. return false;
  10243. }
  10244. if (!coopmat2 && !device->subgroup_shuffle) {
  10245. // scalar FA uses subgroupShuffle
  10246. return false;
  10247. }
  10248. return true;
  10249. }
  10250. case GGML_OP_GET_ROWS:
  10251. {
  10252. switch (op->src[0]->type) {
  10253. case GGML_TYPE_F32:
  10254. case GGML_TYPE_F16:
  10255. case GGML_TYPE_BF16:
  10256. case GGML_TYPE_Q4_0:
  10257. case GGML_TYPE_Q4_1:
  10258. case GGML_TYPE_Q5_0:
  10259. case GGML_TYPE_Q5_1:
  10260. case GGML_TYPE_Q8_0:
  10261. case GGML_TYPE_IQ1_S:
  10262. case GGML_TYPE_IQ1_M:
  10263. case GGML_TYPE_IQ2_XXS:
  10264. case GGML_TYPE_IQ2_XS:
  10265. case GGML_TYPE_IQ2_S:
  10266. case GGML_TYPE_IQ3_XXS:
  10267. case GGML_TYPE_IQ3_S:
  10268. case GGML_TYPE_IQ4_XS:
  10269. case GGML_TYPE_IQ4_NL:
  10270. case GGML_TYPE_MXFP4:
  10271. return true;
  10272. default:
  10273. return false;
  10274. }
  10275. }
  10276. case GGML_OP_SET_ROWS:
  10277. {
  10278. switch (op->type) {
  10279. case GGML_TYPE_F32:
  10280. case GGML_TYPE_F16:
  10281. case GGML_TYPE_BF16:
  10282. case GGML_TYPE_Q4_0:
  10283. case GGML_TYPE_Q4_1:
  10284. case GGML_TYPE_Q5_0:
  10285. case GGML_TYPE_Q5_1:
  10286. case GGML_TYPE_Q8_0:
  10287. case GGML_TYPE_IQ4_NL:
  10288. return true;
  10289. default:
  10290. return false;
  10291. }
  10292. }
  10293. case GGML_OP_CONT:
  10294. case GGML_OP_CPY:
  10295. case GGML_OP_DUP:
  10296. {
  10297. ggml_type src0_type = op->src[0]->type;
  10298. ggml_type src1_type = op->src[1] != nullptr ? op->src[1]->type : src0_type;
  10299. if (src0_type == GGML_TYPE_F32) {
  10300. switch (src1_type) {
  10301. case GGML_TYPE_F32:
  10302. case GGML_TYPE_F16:
  10303. case GGML_TYPE_BF16:
  10304. case GGML_TYPE_Q4_0:
  10305. case GGML_TYPE_Q4_1:
  10306. case GGML_TYPE_Q5_0:
  10307. case GGML_TYPE_Q5_1:
  10308. case GGML_TYPE_Q8_0:
  10309. case GGML_TYPE_IQ4_NL:
  10310. return true;
  10311. default:
  10312. break;
  10313. }
  10314. }
  10315. if (src1_type == GGML_TYPE_F32) {
  10316. switch (src0_type) {
  10317. case GGML_TYPE_F16:
  10318. case GGML_TYPE_Q4_0:
  10319. case GGML_TYPE_Q4_1:
  10320. case GGML_TYPE_Q5_0:
  10321. case GGML_TYPE_Q5_1:
  10322. case GGML_TYPE_Q8_0:
  10323. case GGML_TYPE_IQ4_NL:
  10324. return true;
  10325. default:
  10326. break;
  10327. }
  10328. }
  10329. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  10330. return true;
  10331. }
  10332. // We can handle copying from a type to the same type if it's
  10333. // contiguous (memcpy). We use f16 or f32 shaders to do the copy,
  10334. // so the type/block size must be a multiple of 4.
  10335. if (src0_type == src1_type &&
  10336. ggml_is_contiguous(op->src[0]) && ggml_is_contiguous(op) &&
  10337. (ggml_type_size(src0_type) % 2) == 0) {
  10338. return true;
  10339. }
  10340. return false;
  10341. }
  10342. case GGML_OP_REPEAT:
  10343. return ggml_type_size(op->type) == sizeof(float) && ggml_type_size(op->src[0]->type) == sizeof(float);
  10344. case GGML_OP_REPEAT_BACK:
  10345. return op->type == GGML_TYPE_F32 && op->src[0]->type == GGML_TYPE_F32;
  10346. case GGML_OP_ROPE:
  10347. case GGML_OP_ROPE_BACK:
  10348. case GGML_OP_NONE:
  10349. case GGML_OP_RESHAPE:
  10350. case GGML_OP_VIEW:
  10351. case GGML_OP_PERMUTE:
  10352. case GGML_OP_TRANSPOSE:
  10353. case GGML_OP_RMS_NORM:
  10354. return true;
  10355. case GGML_OP_NORM:
  10356. case GGML_OP_GROUP_NORM:
  10357. case GGML_OP_L2_NORM:
  10358. return ggml_is_contiguous(op->src[0]);
  10359. case GGML_OP_ADD:
  10360. case GGML_OP_SUB:
  10361. case GGML_OP_MUL:
  10362. case GGML_OP_DIV:
  10363. return (op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16) &&
  10364. (op->src[1]->type == GGML_TYPE_F32 || op->src[1]->type == GGML_TYPE_F16) &&
  10365. (op->type == GGML_TYPE_F32 || op->type == GGML_TYPE_F16);
  10366. case GGML_OP_ADD_ID:
  10367. return op->src[0]->type == GGML_TYPE_F32 && op->src[1]->type == GGML_TYPE_F32 && op->src[2]->type == GGML_TYPE_I32 &&
  10368. op->type == GGML_TYPE_F32;
  10369. case GGML_OP_SILU_BACK:
  10370. case GGML_OP_RMS_NORM_BACK:
  10371. case GGML_OP_SQR:
  10372. case GGML_OP_SQRT:
  10373. case GGML_OP_SIN:
  10374. case GGML_OP_COS:
  10375. case GGML_OP_CLAMP:
  10376. case GGML_OP_LEAKY_RELU:
  10377. case GGML_OP_OPT_STEP_ADAMW:
  10378. case GGML_OP_OPT_STEP_SGD:
  10379. return op->src[0]->type == GGML_TYPE_F32;
  10380. case GGML_OP_ARGSORT:
  10381. return op->ne[0] <= max_argsort_cols;
  10382. case GGML_OP_UPSCALE:
  10383. case GGML_OP_ACC:
  10384. case GGML_OP_CONCAT:
  10385. case GGML_OP_SCALE:
  10386. case GGML_OP_PAD:
  10387. case GGML_OP_ROLL:
  10388. case GGML_OP_DIAG_MASK_INF:
  10389. case GGML_OP_SOFT_MAX:
  10390. case GGML_OP_SOFT_MAX_BACK:
  10391. return true;
  10392. case GGML_OP_SUM:
  10393. case GGML_OP_SUM_ROWS:
  10394. case GGML_OP_MEAN:
  10395. return op->src[0]->type == GGML_TYPE_F32 && ggml_is_contiguous_rows(op->src[0]);
  10396. case GGML_OP_ARGMAX:
  10397. case GGML_OP_COUNT_EQUAL:
  10398. case GGML_OP_IM2COL:
  10399. case GGML_OP_TIMESTEP_EMBEDDING:
  10400. case GGML_OP_CONV_2D_DW:
  10401. case GGML_OP_POOL_2D:
  10402. case GGML_OP_RWKV_WKV6:
  10403. case GGML_OP_RWKV_WKV7:
  10404. return true;
  10405. case GGML_OP_CONV_TRANSPOSE_1D:
  10406. return op->src[0]->type == GGML_TYPE_F32 && op->src[1]->type == GGML_TYPE_F32;
  10407. case GGML_OP_CONV_2D:
  10408. {
  10409. // Op is disabled for Apple because it segfaults at pipeline create time on MoltenVK
  10410. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  10411. const vk_device& device = ggml_vk_get_device(ctx->device);
  10412. // Channel-contiguous format is not supported yet.
  10413. return ((op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16) &&
  10414. op->src[1]->type == GGML_TYPE_F32 &&
  10415. op->type == GGML_TYPE_F32 &&
  10416. ggml_is_contiguous(op->src[0]) &&
  10417. ggml_is_contiguous(op->src[1]) &&
  10418. ggml_is_contiguous(op));
  10419. }
  10420. default:
  10421. return false;
  10422. }
  10423. UNUSED(dev);
  10424. }
  10425. static bool ggml_backend_vk_device_supports_buft(ggml_backend_dev_t dev, ggml_backend_buffer_type_t buft) {
  10426. if (buft->iface.get_name != ggml_backend_vk_buffer_type_name) {
  10427. return false;
  10428. }
  10429. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  10430. ggml_backend_vk_buffer_type_context * buft_ctx = (ggml_backend_vk_buffer_type_context *)buft->context;
  10431. return buft_ctx->device->idx == ctx->device;
  10432. }
  10433. static bool ggml_backend_vk_device_offload_op(ggml_backend_dev_t dev, const ggml_tensor * op) {
  10434. const int min_batch_size = 32;
  10435. return (op->ne[1] >= min_batch_size && op->op != GGML_OP_GET_ROWS) ||
  10436. (op->ne[2] >= min_batch_size && op->op == GGML_OP_MUL_MAT_ID);
  10437. UNUSED(dev);
  10438. }
  10439. static const struct ggml_backend_device_i ggml_backend_vk_device_i = {
  10440. /* .get_name = */ ggml_backend_vk_device_get_name,
  10441. /* .get_description = */ ggml_backend_vk_device_get_description,
  10442. /* .get_memory = */ ggml_backend_vk_device_get_memory,
  10443. /* .get_type = */ ggml_backend_vk_device_get_type,
  10444. /* .get_props = */ ggml_backend_vk_device_get_props,
  10445. /* .init_backend = */ ggml_backend_vk_device_init,
  10446. /* .get_buffer_type = */ ggml_backend_vk_device_get_buffer_type,
  10447. /* .get_host_buffer_type = */ ggml_backend_vk_device_get_host_buffer_type,
  10448. /* .buffer_from_host_ptr = */ NULL,
  10449. /* .supports_op = */ ggml_backend_vk_device_supports_op,
  10450. /* .supports_buft = */ ggml_backend_vk_device_supports_buft,
  10451. /* .offload_op = */ ggml_backend_vk_device_offload_op,
  10452. /* .event_new = */ NULL,
  10453. /* .event_free = */ NULL,
  10454. /* .event_synchronize = */ NULL,
  10455. };
  10456. static const char * ggml_backend_vk_reg_get_name(ggml_backend_reg_t reg) {
  10457. UNUSED(reg);
  10458. return GGML_VK_NAME;
  10459. }
  10460. static size_t ggml_backend_vk_reg_get_device_count(ggml_backend_reg_t reg) {
  10461. UNUSED(reg);
  10462. return ggml_backend_vk_get_device_count();
  10463. }
  10464. static ggml_backend_dev_t ggml_backend_vk_reg_get_device(ggml_backend_reg_t reg, size_t device) {
  10465. static std::vector<ggml_backend_dev_t> devices;
  10466. static bool initialized = false;
  10467. {
  10468. static std::mutex mutex;
  10469. std::lock_guard<std::mutex> lock(mutex);
  10470. if (!initialized) {
  10471. for (int i = 0; i < ggml_backend_vk_get_device_count(); i++) {
  10472. ggml_backend_vk_device_context * ctx = new ggml_backend_vk_device_context;
  10473. char desc[256];
  10474. ggml_backend_vk_get_device_description(i, desc, sizeof(desc));
  10475. ctx->device = i;
  10476. ctx->name = GGML_VK_NAME + std::to_string(i);
  10477. ctx->description = desc;
  10478. devices.push_back(new ggml_backend_device {
  10479. /* .iface = */ ggml_backend_vk_device_i,
  10480. /* .reg = */ reg,
  10481. /* .context = */ ctx,
  10482. });
  10483. }
  10484. initialized = true;
  10485. }
  10486. }
  10487. GGML_ASSERT(device < devices.size());
  10488. return devices[device];
  10489. }
  10490. static const struct ggml_backend_reg_i ggml_backend_vk_reg_i = {
  10491. /* .get_name = */ ggml_backend_vk_reg_get_name,
  10492. /* .get_device_count = */ ggml_backend_vk_reg_get_device_count,
  10493. /* .get_device = */ ggml_backend_vk_reg_get_device,
  10494. /* .get_proc_address = */ NULL,
  10495. };
  10496. ggml_backend_reg_t ggml_backend_vk_reg() {
  10497. static ggml_backend_reg reg = {
  10498. /* .api_version = */ GGML_BACKEND_API_VERSION,
  10499. /* .iface = */ ggml_backend_vk_reg_i,
  10500. /* .context = */ nullptr,
  10501. };
  10502. try {
  10503. ggml_vk_instance_init();
  10504. return &reg;
  10505. } catch (const vk::SystemError& e) {
  10506. VK_LOG_DEBUG("ggml_backend_vk_reg() -> Error: System error: " << e.what());
  10507. return nullptr;
  10508. }
  10509. }
  10510. // Extension availability
  10511. static bool ggml_vk_instance_validation_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions) {
  10512. #ifdef GGML_VULKAN_VALIDATE
  10513. bool portability_enumeration_ext = false;
  10514. // Check for portability enumeration extension for MoltenVK support
  10515. for (const auto& properties : instance_extensions) {
  10516. if (strcmp("VK_KHR_portability_enumeration", properties.extensionName) == 0) {
  10517. return true;
  10518. }
  10519. }
  10520. if (!portability_enumeration_ext) {
  10521. std::cerr << "ggml_vulkan: WARNING: Instance extension VK_KHR_portability_enumeration not found." << std::endl;
  10522. }
  10523. #endif
  10524. return false;
  10525. UNUSED(instance_extensions);
  10526. }
  10527. static bool ggml_vk_instance_portability_enumeration_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions) {
  10528. #ifdef __APPLE__
  10529. // Check for portability enumeration extension for MoltenVK support
  10530. for (const auto& properties : instance_extensions) {
  10531. if (strcmp("VK_KHR_portability_enumeration", properties.extensionName) == 0) {
  10532. return true;
  10533. }
  10534. }
  10535. std::cerr << "ggml_vulkan: WARNING: Instance extension VK_KHR_portability_enumeration not found." << std::endl;
  10536. #endif
  10537. return false;
  10538. UNUSED(instance_extensions);
  10539. }
  10540. // Extension availability
  10541. static bool ggml_vk_instance_debug_utils_ext_available(
  10542. const std::vector<vk::ExtensionProperties> & instance_extensions) {
  10543. // Check for portability enumeration extension for MoltenVK support
  10544. for (const auto & properties : instance_extensions) {
  10545. if (strcmp("VK_EXT_debug_utils", properties.extensionName) == 0) {
  10546. return true;
  10547. }
  10548. }
  10549. std::cerr << "ggml_vulkan: WARNING: Instance extension VK_EXT_debug_utils not found." << std::endl;
  10550. return false;
  10551. UNUSED(instance_extensions);
  10552. }
  10553. static bool ggml_vk_khr_cooperative_matrix_support(const vk::PhysicalDeviceProperties& props, const vk::PhysicalDeviceDriverProperties& driver_props, vk_device_architecture arch) {
  10554. switch (props.vendorID) {
  10555. case VK_VENDOR_ID_INTEL:
  10556. // Only allowing Xe2 GPU at the moment since Xe2 GPU can gain significant performance boost,
  10557. // while some older hardware (ex. Arc A770) has performance regressions
  10558. return arch == vk_device_architecture::INTEL_XE2;
  10559. case VK_VENDOR_ID_AMD:
  10560. if (driver_props.driverID == vk::DriverId::eAmdProprietary || driver_props.driverID == vk::DriverId::eAmdOpenSource) {
  10561. // Workaround for AMD proprietary driver reporting support on all GPUs
  10562. return arch == vk_device_architecture::AMD_RDNA3;
  10563. }
  10564. return true;
  10565. default:
  10566. return true;
  10567. }
  10568. }
  10569. // checks
  10570. #ifdef GGML_VULKAN_CHECK_RESULTS
  10571. static void ggml_vk_print_graph_origin(const ggml_tensor * tensor, std::vector<const ggml_tensor *>& done, int level = 0) {
  10572. if (std::find(done.begin(), done.end(), tensor) != done.end() || level > 10) {
  10573. return;
  10574. }
  10575. for (int j = 0; j < level; j++) {
  10576. std::cerr << " ";
  10577. }
  10578. std::cerr << ggml_op_name(tensor->op) << " gpu=" << (tensor->extra != nullptr) << std::endl;
  10579. done.push_back(tensor);
  10580. for (int i = 0; i < GGML_MAX_SRC; i++) {
  10581. if (tensor->src[i] != nullptr) {
  10582. ggml_vk_print_graph_origin(tensor->src[i], done, level + 1);
  10583. }
  10584. }
  10585. }
  10586. static void ggml_vk_print_tensor_area(const ggml_tensor * tensor, const void * data, int i0, int i1, int i2, int i3) {
  10587. if (tensor->type != GGML_TYPE_F32 && tensor->type != GGML_TYPE_F16 && tensor->type != GGML_TYPE_I32) {
  10588. return;
  10589. }
  10590. i0 = std::max(i0, 5);
  10591. i1 = std::max(i1, 5);
  10592. i2 = std::max(i2, 0);
  10593. i3 = std::max(i3, 0);
  10594. fprintf(stderr, " ");
  10595. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  10596. fprintf(stderr, "%7d ", idx1);
  10597. }
  10598. fprintf(stderr, "\n");
  10599. for (int idx0 = i0 - 5; idx0 < i0 + 5; idx0++) {
  10600. fprintf(stderr, "%7d: ", idx0);
  10601. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  10602. if (idx0 >= 0 && idx0 < tensor->ne[0] && idx1 >= 0 && idx1 < tensor->ne[1] && i2 >= 0 && i2 < tensor->ne[2] && i3 >= 0 && i3 < tensor->ne[3]) {
  10603. float val;
  10604. if (tensor->type == GGML_TYPE_F32) {
  10605. val = *(const float *) ((const char *) data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]);
  10606. } else if (tensor->type == GGML_TYPE_F16) {
  10607. val = ggml_fp16_to_fp32(*(const ggml_fp16_t *) ((const char *) data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]));
  10608. } else if (tensor->type == GGML_TYPE_I32) {
  10609. val = *(const int32_t *) ((const char *) data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]);
  10610. } else {
  10611. GGML_ABORT("fatal error");
  10612. }
  10613. fprintf(stderr, "% 7.2f ", val);
  10614. } else {
  10615. fprintf(stderr, " ");
  10616. }
  10617. }
  10618. fprintf(stderr, "\n");
  10619. }
  10620. }
  10621. static void ggml_vk_print_tensor(const ggml_tensor * tensor, const char * name) {
  10622. void * tensor_data = tensor->data;
  10623. const bool is_gpu = tensor->buffer != nullptr && ggml_backend_buffer_is_vk(tensor->buffer);
  10624. if (is_gpu) {
  10625. const size_t tensor_size = ggml_nbytes(tensor);
  10626. tensor_data = malloc(tensor_size);
  10627. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  10628. vk_buffer buffer_gpu = buf_ctx->dev_buffer;
  10629. ggml_vk_buffer_read(buffer_gpu, vk_tensor_offset(tensor) + tensor->view_offs, tensor_data, tensor_size);
  10630. }
  10631. std::cerr << "TENSOR CHECK " << name << " (" << tensor->name << "): " << ggml_op_name(tensor->op) << std::endl;
  10632. std::cerr << "tensor=" << tensor << " tensor->type: " << ggml_type_name(tensor->type) << " ne0=" << tensor->ne[0] << " nb0=" << tensor->nb[0] << " ne1=" << tensor->ne[1] << " nb1=" << tensor->nb[1] << " ne2=" << tensor->ne[2] << " nb2=" << tensor->nb[2] << " ne3=" << tensor->ne[3] << " nb3=" << tensor->nb[3] << std::endl;
  10633. if (tensor->src[0] != nullptr) {
  10634. std::cerr << "tensor->src[0]=" << tensor->src[0] << " name=" << tensor->src[0]->name << " op=" << ggml_op_name(tensor->src[0]->op) << " type=" << ggml_type_name(tensor->src[0]->type) << " ne0=" << tensor->src[0]->ne[0] << " nb0=" << tensor->src[0]->nb[0] << " ne1=" << tensor->src[0]->ne[1] << " nb1=" << tensor->src[0]->nb[1] << " ne2=" << tensor->src[0]->ne[2] << " nb2=" << tensor->src[0]->nb[2] << " ne3=" << tensor->src[0]->ne[3] << " nb3=" << tensor->src[0]->nb[3] << std::endl;
  10635. }
  10636. if (tensor->src[1] != nullptr) {
  10637. std::cerr << "tensor->src[1]=" << tensor->src[1] << " name=" << tensor->src[1]->name << " op=" << ggml_op_name(tensor->src[1]->op) << " type=" << ggml_type_name(tensor->src[1]->type) << " ne0=" << tensor->src[1]->ne[0] << " nb0=" << tensor->src[1]->nb[0] << " ne1=" << tensor->src[1]->ne[1] << " nb1=" << tensor->src[1]->nb[1] << " ne2=" << tensor->src[1]->ne[2] << " nb2=" << tensor->src[1]->nb[2] << " ne3=" << tensor->src[1]->ne[3] << " nb3=" << tensor->src[1]->nb[3] << std::endl;
  10638. }
  10639. std::cerr << std::endl << "Result:" << std::endl;
  10640. ggml_vk_print_tensor_area(tensor, tensor_data, 5, 5, 0, 0);
  10641. std::cerr << std::endl;
  10642. std::vector<const ggml_tensor *> done;
  10643. ggml_vk_print_graph_origin(tensor, done);
  10644. if (is_gpu) {
  10645. free(tensor_data);
  10646. }
  10647. }
  10648. void * comp_result;
  10649. size_t comp_size;
  10650. size_t comp_nb[GGML_MAX_DIMS];
  10651. size_t check_counter = 0;
  10652. static void ggml_vk_check_results_0(ggml_backend_vk_context * ctx, ggml_cgraph * cgraph, int tensor_idx) {
  10653. ggml_tensor * tensor = cgraph->nodes[tensor_idx];
  10654. if (tensor->op == GGML_OP_TRANSPOSE || tensor->op == GGML_OP_SET_ROWS) {
  10655. return;
  10656. }
  10657. bool fused_rms_norm_mul = false;
  10658. int rms_norm_idx = -1;
  10659. if (ctx->num_additional_fused_ops == 1 &&
  10660. tensor->op == GGML_OP_RMS_NORM &&
  10661. cgraph->nodes[tensor_idx + 1]->op == GGML_OP_MUL) {
  10662. fused_rms_norm_mul = true;
  10663. tensor = cgraph->nodes[tensor_idx + 1];
  10664. }
  10665. check_counter++;
  10666. if (!(vk_output_tensor > 0 && vk_output_tensor == check_counter) && check_counter <= vk_skip_checks) {
  10667. return;
  10668. }
  10669. VK_LOG_DEBUG("ggml_vk_check_results_0(" << tensor->name << ")");
  10670. ggml_tensor * src0 = tensor->src[0];
  10671. ggml_tensor * src1 = tensor->src[1];
  10672. struct ggml_init_params iparams = {
  10673. /*.mem_size =*/ 2ul*1024ul*1024ul*1024ul,
  10674. /*.mem_buffer =*/ NULL,
  10675. /*.no_alloc =*/ false,
  10676. };
  10677. struct ggml_context * ggml_ctx = ggml_init(iparams);
  10678. std::array<struct ggml_tensor *, 6> src_clone = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
  10679. std::array<size_t, 6> src_size = {0, 0, 0, 0, 0, 0};
  10680. std::array<void *, 6> src_buffer = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
  10681. const char * srci_name[6] = {"src0", "src1", "src2", "src3", "src4", "src5"};
  10682. struct ggml_tensor * tensor_clone = nullptr;
  10683. for (int i = 0; i < 6; i++) {
  10684. ggml_tensor * srci = tensor->src[i];
  10685. if (fused_rms_norm_mul) {
  10686. rms_norm_idx = tensor->src[0]->op == GGML_OP_RMS_NORM ? 0 : 1;
  10687. ggml_tensor *rms_norm = tensor->src[rms_norm_idx];
  10688. switch (i) {
  10689. case 0: srci = rms_norm->src[0]; break;
  10690. case 1: srci = tensor->src[1 - rms_norm_idx]; break;
  10691. default: continue;
  10692. }
  10693. }
  10694. if (srci == nullptr) {
  10695. continue;
  10696. }
  10697. ggml_tensor * srci_clone = ggml_dup_tensor(ggml_ctx, srci);
  10698. size_t srci_size = ggml_nbytes(srci);
  10699. src_clone[i] = srci_clone;
  10700. src_size[i] = ggml_nbytes(srci);
  10701. src_buffer[i] = malloc(srci_size);
  10702. srci_clone->data = src_buffer[i];
  10703. if (ggml_backend_buffer_is_host(srci->buffer)) {
  10704. memcpy(srci_clone->data, srci->data, srci_size);
  10705. memcpy(srci_clone->nb, srci->nb, sizeof(size_t) * GGML_MAX_DIMS);
  10706. } else if (ggml_backend_buffer_is_vk(srci->buffer)) {
  10707. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)srci->buffer->context;
  10708. vk_buffer& buffer_gpu = buf_ctx->dev_buffer;
  10709. uint64_t offset = vk_tensor_offset(srci) + srci->view_offs;
  10710. if (!ggml_is_contiguous(srci) && ggml_vk_dim01_contiguous(srci)) {
  10711. for (int i3 = 0; i3 < srci->ne[3]; i3++) {
  10712. for (int i2 = 0; i2 < srci->ne[2]; i2++) {
  10713. const int idx = i3*srci->ne[2] + i2;
  10714. ggml_vk_buffer_read(buffer_gpu, offset + idx * srci->nb[2], ((char *)srci_clone->data + idx * srci_clone->nb[2]), srci->ne[1] * srci->nb[1]);
  10715. }
  10716. }
  10717. srci_clone->nb[0] = srci->nb[0];
  10718. srci_clone->nb[1] = srci->nb[1];
  10719. for (int i = 2; i < GGML_MAX_DIMS; i++) {
  10720. srci_clone->nb[i] = srci_clone->nb[i - 1]*srci_clone->ne[i - 1];
  10721. }
  10722. } else {
  10723. if (offset + srci_size >= buffer_gpu->size) {
  10724. srci_size = buffer_gpu->size - offset;
  10725. }
  10726. ggml_vk_buffer_read(buffer_gpu, offset, srci_clone->data, srci_size);
  10727. memcpy(srci_clone->nb, srci->nb, sizeof(size_t) * GGML_MAX_DIMS);
  10728. }
  10729. } else {
  10730. GGML_ABORT("fatal error");
  10731. }
  10732. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  10733. ggml_vk_print_tensor(srci, srci_name[i]);
  10734. }
  10735. }
  10736. if (tensor->op == GGML_OP_FLASH_ATTN_EXT) {
  10737. const float * params = (const float *)tensor->op_params;
  10738. tensor_clone = ggml_flash_attn_ext(ggml_ctx, src_clone[0], src_clone[1], src_clone[2], src_clone[3], params[0], params[1], params[2]);
  10739. if (src_clone[4]) {
  10740. ggml_flash_attn_ext_add_sinks(tensor_clone, src_clone[4]);
  10741. }
  10742. } else if (tensor->op == GGML_OP_MUL_MAT) {
  10743. tensor_clone = ggml_mul_mat(ggml_ctx, src_clone[0], src_clone[1]);
  10744. } else if (tensor->op == GGML_OP_MUL_MAT_ID) {
  10745. tensor_clone = ggml_mul_mat_id(ggml_ctx, src_clone[0], src_clone[1], src_clone[2]);
  10746. } else if (tensor->op == GGML_OP_SUB) {
  10747. tensor_clone = ggml_sub(ggml_ctx, src_clone[0], src_clone[1]);
  10748. } else if (tensor->op == GGML_OP_MUL) {
  10749. if (fused_rms_norm_mul) {
  10750. tensor_clone = ggml_rms_norm(ggml_ctx, src_clone[0], *(float *)tensor->src[rms_norm_idx]->op_params);
  10751. tensor_clone = ggml_mul(ggml_ctx, tensor_clone, src_clone[1 - rms_norm_idx]);
  10752. } else {
  10753. tensor_clone = ggml_mul(ggml_ctx, src_clone[0], src_clone[1]);
  10754. }
  10755. } else if (tensor->op == GGML_OP_DIV) {
  10756. tensor_clone = ggml_div(ggml_ctx, src_clone[0], src_clone[1]);
  10757. } else if (tensor->op == GGML_OP_CONCAT) {
  10758. tensor_clone = ggml_concat(ggml_ctx, src_clone[0], src_clone[1], *(int *)tensor->op_params);
  10759. } else if (tensor->op == GGML_OP_UPSCALE) {
  10760. tensor_clone = ggml_interpolate(ggml_ctx, src_clone[0], tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3], (ggml_scale_mode) tensor->op_params[0]);
  10761. } else if (tensor->op == GGML_OP_SCALE) {
  10762. const float * params = (const float *)tensor->op_params;
  10763. tensor_clone = ggml_scale_bias(ggml_ctx, src_clone[0], params[0], params[1]);
  10764. } else if (tensor->op == GGML_OP_SQR) {
  10765. tensor_clone = ggml_sqr(ggml_ctx, src_clone[0]);
  10766. } else if (tensor->op == GGML_OP_SQRT) {
  10767. tensor_clone = ggml_sqrt(ggml_ctx, src_clone[0]);
  10768. } else if (tensor->op == GGML_OP_SIN) {
  10769. tensor_clone = ggml_sin(ggml_ctx, src_clone[0]);
  10770. } else if (tensor->op == GGML_OP_COS) {
  10771. tensor_clone = ggml_cos(ggml_ctx, src_clone[0]);
  10772. } else if (tensor->op == GGML_OP_CLAMP) {
  10773. const float * params = (const float *)tensor->op_params;
  10774. tensor_clone = ggml_clamp(ggml_ctx, src_clone[0], params[0], params[1]);
  10775. } else if (tensor->op == GGML_OP_PAD) {
  10776. tensor_clone = ggml_pad(ggml_ctx, src_clone[0], tensor->ne[0] - src_clone[0]->ne[0], tensor->ne[1] - src_clone[0]->ne[1], tensor->ne[2] - src_clone[0]->ne[2], tensor->ne[3] - src_clone[0]->ne[3]);
  10777. } else if (tensor->op == GGML_OP_REPEAT) {
  10778. tensor_clone = ggml_repeat(ggml_ctx, src_clone[0], tensor);
  10779. } else if (tensor->op == GGML_OP_REPEAT_BACK) {
  10780. tensor_clone = ggml_repeat_back(ggml_ctx, src_clone[0], tensor);
  10781. } else if (tensor->op == GGML_OP_ADD) {
  10782. tensor_clone = ggml_add(ggml_ctx, src_clone[0], src_clone[1]);
  10783. } else if (tensor->op == GGML_OP_ACC) {
  10784. tensor_clone = ggml_acc(ggml_ctx, src_clone[0], src_clone[1], tensor->op_params[0], tensor->op_params[1], tensor->op_params[2], tensor->op_params[3]);
  10785. } else if (tensor->op == GGML_OP_NORM) {
  10786. tensor_clone = ggml_norm(ggml_ctx, src_clone[0], *(float *)tensor->op_params);
  10787. } else if (tensor->op == GGML_OP_GROUP_NORM) {
  10788. const float * float_params = (const float *)tensor->op_params;
  10789. tensor_clone = ggml_group_norm(ggml_ctx, src_clone[0], tensor->op_params[0], float_params[1]);
  10790. } else if (tensor->op == GGML_OP_RMS_NORM) {
  10791. tensor_clone = ggml_rms_norm(ggml_ctx, src_clone[0], *(float *)tensor->op_params);
  10792. } else if (tensor->op == GGML_OP_RMS_NORM_BACK) {
  10793. const float eps = ((float *) tensor->op_params)[0];
  10794. tensor_clone = ggml_rms_norm_back(ggml_ctx, src_clone[0], src_clone[1], eps);
  10795. } else if (tensor->op == GGML_OP_SILU_BACK) {
  10796. tensor_clone = ggml_silu_back(ggml_ctx, src_clone[0], src_clone[1]);
  10797. } else if (tensor->op == GGML_OP_L2_NORM) {
  10798. const float eps = ((float *) tensor->op_params)[0];
  10799. tensor_clone = ggml_l2_norm(ggml_ctx, src_clone[0], eps);
  10800. } else if (tensor->op == GGML_OP_SOFT_MAX) {
  10801. if (src1 != nullptr) {
  10802. const float * params = (const float *)tensor->op_params;
  10803. tensor_clone = ggml_soft_max_ext(ggml_ctx, src_clone[0], src_clone[1], params[0], params[1]);
  10804. } else {
  10805. tensor_clone = ggml_soft_max(ggml_ctx, src_clone[0]);
  10806. }
  10807. } else if (tensor->op == GGML_OP_SOFT_MAX_BACK) {
  10808. tensor_clone = ggml_soft_max_ext_back(ggml_ctx, src_clone[0], src_clone[1], ((float *)tensor->op_params)[0], ((float *)tensor->op_params)[1]);
  10809. } else if (tensor->op == GGML_OP_DIAG_MASK_INF) {
  10810. tensor_clone = ggml_diag_mask_inf(ggml_ctx, src_clone[0], tensor->op_params[0]);
  10811. } else if (tensor->op == GGML_OP_ROPE || tensor->op == GGML_OP_ROPE_BACK) {
  10812. const int n_dims = ((int32_t *) tensor->op_params)[1];
  10813. const int mode = ((int32_t *) tensor->op_params)[2];
  10814. //const int n_ctx_ggml = ((int32_t *) tensor->op_params)[3];
  10815. const int n_ctx_orig_ggml = ((int32_t *) tensor->op_params)[4];
  10816. const float freq_base = ((float *) tensor->op_params)[5];
  10817. const float freq_scale = ((float *) tensor->op_params)[6];
  10818. const float ext_factor = ((float *) tensor->op_params)[7];
  10819. const float attn_factor = ((float *) tensor->op_params)[8];
  10820. const float beta_fast = ((float *) tensor->op_params)[9];
  10821. const float beta_slow = ((float *) tensor->op_params)[10];
  10822. if (mode & GGML_ROPE_TYPE_MROPE) {
  10823. int32_t *sections = ((int32_t *) tensor->op_params) + 11;
  10824. if (tensor->op == GGML_OP_ROPE) {
  10825. tensor_clone = ggml_rope_multi(ggml_ctx, src_clone[0], src_clone[1], src_clone[2], n_dims, sections, mode, n_ctx_orig_ggml, freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow);
  10826. } else {
  10827. tensor_clone = ggml_rope_multi_back(ggml_ctx, src_clone[0], src_clone[1], src_clone[2], n_dims, sections, mode, n_ctx_orig_ggml, freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow);
  10828. }
  10829. } else {
  10830. if (tensor->op == GGML_OP_ROPE) {
  10831. tensor_clone = ggml_rope_ext(ggml_ctx, src_clone[0], src_clone[1], src_clone[2], n_dims, mode, n_ctx_orig_ggml, freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow);
  10832. } else {
  10833. tensor_clone = ggml_rope_ext_back(ggml_ctx, src_clone[0], src_clone[1], src_clone[2], n_dims, mode, n_ctx_orig_ggml, freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow);
  10834. }
  10835. }
  10836. } else if (tensor->op == GGML_OP_UNARY) {
  10837. switch (ggml_get_unary_op(tensor)) {
  10838. case GGML_UNARY_OP_EXP:
  10839. tensor_clone = ggml_exp(ggml_ctx, src_clone[0]);
  10840. break;
  10841. case GGML_UNARY_OP_SILU:
  10842. tensor_clone = ggml_silu(ggml_ctx, src_clone[0]);
  10843. break;
  10844. case GGML_UNARY_OP_GELU:
  10845. tensor_clone = ggml_gelu(ggml_ctx, src_clone[0]);
  10846. break;
  10847. case GGML_UNARY_OP_GELU_ERF:
  10848. tensor_clone = ggml_gelu_erf(ggml_ctx, src_clone[0]);
  10849. break;
  10850. case GGML_UNARY_OP_GELU_QUICK:
  10851. tensor_clone = ggml_gelu_quick(ggml_ctx, src_clone[0]);
  10852. break;
  10853. case GGML_UNARY_OP_RELU:
  10854. tensor_clone = ggml_relu(ggml_ctx, src_clone[0]);
  10855. break;
  10856. case GGML_UNARY_OP_TANH:
  10857. tensor_clone = ggml_tanh(ggml_ctx, src_clone[0]);
  10858. break;
  10859. case GGML_UNARY_OP_SIGMOID:
  10860. tensor_clone = ggml_sigmoid(ggml_ctx, src_clone[0]);
  10861. break;
  10862. case GGML_UNARY_OP_HARDSIGMOID:
  10863. tensor_clone = ggml_hardsigmoid(ggml_ctx, src_clone[0]);
  10864. break;
  10865. case GGML_UNARY_OP_HARDSWISH:
  10866. tensor_clone = ggml_hardswish(ggml_ctx, src_clone[0]);
  10867. break;
  10868. default:
  10869. std::cerr << "Missing vk_check_results OP: " << ggml_op_name(tensor->op) << std::endl;
  10870. GGML_ABORT("fatal error");
  10871. }
  10872. } else if (tensor->op == GGML_OP_GLU) {
  10873. if (src_clone[1] == nullptr) {
  10874. tensor_clone = ggml_glu(ggml_ctx, src_clone[0], (ggml_glu_op) tensor->op_params[0], tensor->op_params[1]);
  10875. } else {
  10876. tensor_clone = ggml_glu_split(ggml_ctx, src_clone[0], src_clone[1], (ggml_glu_op) tensor->op_params[0]);
  10877. }
  10878. ggml_set_op_params_i32(tensor_clone, 2, ggml_get_op_params_i32(tensor, 2));
  10879. ggml_set_op_params_i32(tensor_clone, 3, ggml_get_op_params_i32(tensor, 3));
  10880. } else if (tensor->op == GGML_OP_CPY || tensor->op == GGML_OP_DUP) {
  10881. if (src1 == nullptr) {
  10882. tensor_clone = ggml_dup(ggml_ctx, src_clone[0]);
  10883. tensor_clone->type = tensor->type;
  10884. } else {
  10885. tensor_clone = ggml_cpy(ggml_ctx, src_clone[0], src_clone[1]);
  10886. }
  10887. } else if (tensor->op == GGML_OP_CONT) {
  10888. tensor_clone = ggml_cont_4d(ggml_ctx, src_clone[0], tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3]);
  10889. } else if (tensor->op == GGML_OP_RESHAPE) {
  10890. tensor_clone = ggml_reshape_4d(ggml_ctx, src_clone[0], tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3]);
  10891. } else if (tensor->op == GGML_OP_VIEW) {
  10892. tensor_clone = ggml_view_4d(ggml_ctx, src_clone[0], tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3], tensor->nb[1], tensor->nb[2], tensor->nb[3], ((int32_t *) tensor->op_params)[0]);
  10893. } else if (tensor->op == GGML_OP_PERMUTE) {
  10894. int32_t * params = (int32_t *)tensor->op_params;
  10895. tensor_clone = ggml_permute(ggml_ctx, src_clone[0], params[0], params[1], params[2], params[3]);
  10896. } else if (tensor->op == GGML_OP_TRANSPOSE) {
  10897. tensor_clone = ggml_transpose(ggml_ctx, src_clone[0]);
  10898. } else if (tensor->op == GGML_OP_GET_ROWS) {
  10899. tensor_clone = ggml_get_rows(ggml_ctx, src_clone[0], src_clone[1]);
  10900. } else if (tensor->op == GGML_OP_ARGSORT) {
  10901. tensor_clone = ggml_argsort(ggml_ctx, src_clone[0], (ggml_sort_order) *(int *)tensor->op_params);
  10902. } else if (tensor->op == GGML_OP_SUM) {
  10903. tensor_clone = ggml_sum(ggml_ctx, src_clone[0]);
  10904. } else if (tensor->op == GGML_OP_SUM_ROWS) {
  10905. tensor_clone = ggml_sum_rows(ggml_ctx, src_clone[0]);
  10906. } else if (tensor->op == GGML_OP_MEAN) {
  10907. tensor_clone = ggml_mean(ggml_ctx, src_clone[0]);
  10908. } else if (tensor->op == GGML_OP_ARGMAX) {
  10909. tensor_clone = ggml_argmax(ggml_ctx, src_clone[0]);
  10910. } else if (tensor->op == GGML_OP_COUNT_EQUAL) {
  10911. tensor_clone = ggml_count_equal(ggml_ctx, src_clone[0], src_clone[1]);
  10912. } else if (tensor->op == GGML_OP_IM2COL) {
  10913. const int32_t s0 = tensor->op_params[0];
  10914. const int32_t s1 = tensor->op_params[1];
  10915. const int32_t p0 = tensor->op_params[2];
  10916. const int32_t p1 = tensor->op_params[3];
  10917. const int32_t d0 = tensor->op_params[4];
  10918. const int32_t d1 = tensor->op_params[5];
  10919. const bool is_2D = tensor->op_params[6] == 1;
  10920. tensor_clone = ggml_im2col(ggml_ctx, src_clone[0], src_clone[1], s0, s1, p0, p1, d0, d1, is_2D, tensor->type);
  10921. } else if (tensor->op == GGML_OP_TIMESTEP_EMBEDDING) {
  10922. const int32_t dim = tensor->op_params[0];
  10923. const int32_t max_period = tensor->op_params[1];
  10924. tensor_clone = ggml_timestep_embedding(ggml_ctx, src_clone[0], dim, max_period);
  10925. } else if (tensor->op == GGML_OP_CONV_TRANSPOSE_1D){
  10926. const int32_t s0 = tensor->op_params[0];
  10927. const int32_t p0 = tensor->op_params[1];
  10928. const int32_t d0 = tensor->op_params[2];
  10929. tensor_clone = ggml_conv_transpose_1d(ggml_ctx, src_clone[0], src_clone[1], s0, p0, d0);
  10930. } else if (tensor->op == GGML_OP_POOL_2D) {
  10931. enum ggml_op_pool op = static_cast<ggml_op_pool>(tensor->op_params[0]);
  10932. const int32_t k0 = tensor->op_params[1];
  10933. const int32_t k1 = tensor->op_params[2];
  10934. const int32_t s0 = tensor->op_params[3];
  10935. const int32_t s1 = tensor->op_params[4];
  10936. const int32_t p0 = tensor->op_params[5];
  10937. const int32_t p1 = tensor->op_params[6];
  10938. tensor_clone = ggml_pool_2d(ggml_ctx, src_clone[0], op, k0, k1, s0, s1, p0, p1);
  10939. } else if (tensor->op == GGML_OP_CONV_2D) {
  10940. const int32_t s0 = tensor->op_params[0];
  10941. const int32_t s1 = tensor->op_params[1];
  10942. const int32_t p0 = tensor->op_params[2];
  10943. const int32_t p1 = tensor->op_params[3];
  10944. const int32_t d0 = tensor->op_params[4];
  10945. const int32_t d1 = tensor->op_params[5];
  10946. tensor_clone = ggml_conv_2d(ggml_ctx, src_clone[0], src_clone[1], s0, s1, p0, p1, d0, d1);
  10947. } else if (tensor->op == GGML_OP_LEAKY_RELU) {
  10948. const float * op_params = (const float *)tensor->op_params;
  10949. tensor_clone = ggml_leaky_relu(ggml_ctx, src_clone[0], op_params[0], false);
  10950. } else if (tensor->op == GGML_OP_RWKV_WKV6) {
  10951. tensor_clone = ggml_rwkv_wkv6(ggml_ctx, src_clone[0], src_clone[1],
  10952. src_clone[2], src_clone[3], src_clone[4], src_clone[5]);
  10953. } else if (tensor->op == GGML_OP_RWKV_WKV7) {
  10954. tensor_clone = ggml_rwkv_wkv7(ggml_ctx, src_clone[0], src_clone[1], src_clone[2], src_clone[3],
  10955. src_clone[4], src_clone[5], src_clone[6]);
  10956. } else if (tensor->op == GGML_OP_OPT_STEP_ADAMW) {
  10957. src_clone[0]->flags = src0->flags;
  10958. tensor_clone = ggml_opt_step_adamw(ggml_ctx, src_clone[0], src_clone[1],
  10959. src_clone[2], src_clone[3], src_clone[4]);
  10960. } else if (tensor->op == GGML_OP_OPT_STEP_SGD) {
  10961. src_clone[0]->flags = src0->flags;
  10962. tensor_clone = ggml_opt_step_sgd(ggml_ctx, src_clone[0], src_clone[1],
  10963. src_clone[2]);
  10964. } else if (tensor->op == GGML_OP_ADD_ID) {
  10965. tensor_clone = ggml_add_id(ggml_ctx, src_clone[0], src_clone[1], src_clone[2]);
  10966. }
  10967. else {
  10968. std::cerr << "Missing vk_check_results OP: " << ggml_op_name(tensor->op) << std::endl;
  10969. GGML_ABORT("fatal error");
  10970. }
  10971. ggml_cgraph * cgraph_cpu = ggml_new_graph(ggml_ctx);
  10972. ggml_build_forward_expand(cgraph_cpu, tensor_clone);
  10973. ggml_graph_compute_with_ctx(ggml_ctx, cgraph_cpu, 8);
  10974. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  10975. ggml_vk_print_tensor(tensor_clone, "tensor_clone");
  10976. }
  10977. comp_size = ggml_nbytes(tensor_clone);
  10978. comp_result = malloc(comp_size);
  10979. memcpy(comp_result, tensor_clone->data, comp_size);
  10980. memcpy(comp_nb, tensor_clone->nb, sizeof(size_t) * GGML_MAX_DIMS);
  10981. for (int i = 0; i < 6; i++) {
  10982. if (src_buffer[i] != nullptr) {
  10983. free(src_buffer[i]);
  10984. }
  10985. }
  10986. ggml_free(ggml_ctx);
  10987. VK_LOG_DEBUG("END ggml_vk_check_results_0(" << tensor->name << ")");
  10988. }
  10989. static void ggml_vk_check_results_1(ggml_backend_vk_context * ctx, ggml_cgraph * cgraph, int tensor_idx) {
  10990. ggml_tensor * tensor = cgraph->nodes[tensor_idx];
  10991. if (tensor->op == GGML_OP_TRANSPOSE || tensor->op == GGML_OP_SET_ROWS) {
  10992. return;
  10993. }
  10994. if (ctx->num_additional_fused_ops == 1 &&
  10995. tensor->op == GGML_OP_RMS_NORM &&
  10996. cgraph->nodes[tensor_idx + 1]->op == GGML_OP_MUL) {
  10997. tensor = cgraph->nodes[tensor_idx + 1];
  10998. }
  10999. if (!(vk_output_tensor > 0 && vk_output_tensor == check_counter) && check_counter <= vk_skip_checks) {
  11000. return;
  11001. }
  11002. VK_LOG_DEBUG("ggml_vk_check_results_1(" << tensor->name << ")");
  11003. ggml_tensor * src0 = tensor->src[0];
  11004. ggml_tensor * src1 = tensor->src[1];
  11005. ggml_tensor * src2 = tensor->src[2];
  11006. ggml_tensor * src3 = tensor->src[3];
  11007. void * tensor_data = tensor->data;
  11008. if (ggml_backend_buffer_is_vk(tensor->buffer)) {
  11009. size_t tensor_size = ggml_nbytes(tensor);
  11010. tensor_data = malloc(tensor_size);
  11011. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  11012. vk_buffer& buffer_gpu = buf_ctx->dev_buffer;
  11013. uint64_t offset = vk_tensor_offset(tensor) + tensor->view_offs;
  11014. if (offset + tensor_size >= buffer_gpu->size) {
  11015. tensor_size = buffer_gpu->size - offset;
  11016. }
  11017. ggml_vk_buffer_read(buffer_gpu, offset, tensor_data, tensor_size);
  11018. }
  11019. float first_error_result = -1.0f;
  11020. float first_error_correct = -1.0f;
  11021. std::array<int, 4> first_error = { -1, -1, -1, -1 };
  11022. double avg_err = 0.0;
  11023. size_t counter = 0;
  11024. for (int i3 = 0; i3 < tensor->ne[3]; i3++) {
  11025. for (int i2 = 0; i2 < tensor->ne[2]; i2++) {
  11026. for (int i1 = 0; i1 < tensor->ne[1]; i1++) {
  11027. for (int i0 = 0; i0 < tensor->ne[0]; i0++) {
  11028. const bool buffer_size_fit = i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0] < comp_size;
  11029. float correct = 0.0f;
  11030. float result = 0.0f;
  11031. if (buffer_size_fit) {
  11032. if (tensor->type == GGML_TYPE_F32) {
  11033. correct = *(float *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]);
  11034. result = *(float *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]);
  11035. } else if (tensor->type == GGML_TYPE_F16) {
  11036. correct = ggml_fp16_to_fp32(*(ggml_fp16_t *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]));
  11037. result = ggml_fp16_to_fp32(*(ggml_fp16_t *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]));
  11038. } else if (tensor->type == GGML_TYPE_BF16) {
  11039. correct = ggml_bf16_to_fp32(*(ggml_bf16_t *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]));
  11040. result = ggml_bf16_to_fp32(*(ggml_bf16_t *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]));
  11041. } else if (tensor->type == GGML_TYPE_I32) {
  11042. correct = *(int32_t *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]);
  11043. result = *(int32_t *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]);
  11044. } else if (tensor->type == GGML_TYPE_I64) {
  11045. correct = *(int64_t *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]);
  11046. result = *(int64_t *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]);
  11047. } else {
  11048. std::cerr << "Results check not implemented for type " << ggml_type_name(tensor->type) << std::endl;
  11049. }
  11050. } else {
  11051. std::cerr << "Missing debug code for type " << ggml_type_name(tensor->type) << std::endl;
  11052. GGML_ABORT("fatal error");
  11053. }
  11054. if ((std::isnan(correct) != std::isnan(result)) || (std::isinf(correct) != std::isinf(result)) || !buffer_size_fit) {
  11055. std::cerr << "ERROR: Invalid value in " << ggml_op_name(tensor->op) << " i3=" << i3 << " i2=" << i2 << " i1=" << i1 << " i0=" << i0 << " result=" << result << " correct=" << correct << " avg_err=" << (avg_err / counter) << std::endl;
  11056. std::cerr << "tensor=" << tensor << " tensor->name=" << tensor->name << " tensor->type: " << ggml_type_name(tensor->type) << " ne0=" << tensor->ne[0] << " nb0=" << tensor->nb[0] << " ne1=" << tensor->ne[1] << " nb1=" << tensor->nb[1] << " ne2=" << tensor->ne[2] << " nb2=" << tensor->nb[2] << " ne3=" << tensor->ne[3] << " nb3=" << tensor->nb[3] << " offset=" << tensor->view_offs << std::endl;
  11057. if (src0 != nullptr) {
  11058. std::cerr << "src0=" << src0 << " src0->name=" << src0->name << " op=" << ggml_op_name(src0->op) << " type=" << ggml_type_name(src0->type) << " ne0=" << src0->ne[0] << " nb0=" << src0->nb[0] << " ne1=" << src0->ne[1] << " nb1=" << src0->nb[1] << " ne2=" << src0->ne[2] << " nb2=" << src0->nb[2] << " ne3=" << src0->ne[3] << " nb3=" << src0->nb[3] << " offset=" << src0->view_offs << std::endl;
  11059. }
  11060. if (src1 != nullptr) {
  11061. std::cerr << "src1=" << src1 << " src1->name=" << src1->name << " op=" << ggml_op_name(src1->op) << " type=" << ggml_type_name(src1->type) << " ne0=" << src1->ne[0] << " nb0=" << src1->nb[0] << " ne1=" << src1->ne[1] << " nb1=" << src1->nb[1] << " ne2=" << src1->ne[2] << " nb2=" << src1->nb[2] << " ne3=" << src1->ne[3] << " nb3=" << src1->nb[3] << " offset=" << src1->view_offs << std::endl;
  11062. }
  11063. if (src2 != nullptr) {
  11064. std::cerr << "src2=" << src2 << " src2->name=" << src2->name << " op=" << ggml_op_name(src2->op) << " type=" << ggml_type_name(src2->type) << " ne0=" << src2->ne[0] << " nb0=" << src2->nb[0] << " ne1=" << src2->ne[1] << " nb1=" << src2->nb[1] << " ne2=" << src2->ne[2] << " nb2=" << src2->nb[2] << " ne3=" << src2->ne[3] << " nb3=" << src2->nb[3] << " offset=" << src2->view_offs << std::endl;
  11065. }
  11066. if (src3 != nullptr) {
  11067. std::cerr << "src3=" << src3 << " src3->name=" << src3->name << " op=" << ggml_op_name(src3->op) << " type=" << ggml_type_name(src3->type) << " ne0=" << src3->ne[0] << " nb0=" << src3->nb[0] << " ne1=" << src3->ne[1] << " nb1=" << src3->nb[1] << " ne2=" << src3->ne[2] << " nb2=" << src3->nb[2] << " ne3=" << src3->ne[3] << " nb3=" << src3->nb[3] << " offset=" << src3->view_offs << std::endl;
  11068. }
  11069. std::cerr << "First error: result=" << first_error_result << " correct=" << first_error_correct << " i3=" << first_error[3] << " i2=" << first_error[2] << " i1=" << first_error[1] << " i0=" << first_error[0] << std::endl;
  11070. std::cerr << std::endl << "Result:" << std::endl;
  11071. ggml_vk_print_tensor_area(tensor, tensor_data, i0, i1, i2, i3);
  11072. std::cerr << std::endl << "Correct:" << std::endl;
  11073. ggml_vk_print_tensor_area(tensor, comp_result, i0, i1, i2, i3);
  11074. std::cerr << std::endl;
  11075. std::vector<const ggml_tensor *> done;
  11076. ggml_vk_print_graph_origin(tensor, done);
  11077. GGML_ABORT("fatal error");
  11078. }
  11079. const double denom = std::fabs(correct) > 1.0f ? (std::fabs(correct) > 1e-8 ? std::fabs(correct) : 1e-8) : 1.0f;
  11080. if (first_error[0] == -1 && std::fabs(correct - result) / denom > 0.5) {
  11081. first_error[0] = i0;
  11082. first_error[1] = i1;
  11083. first_error[2] = i2;
  11084. first_error[3] = i3;
  11085. first_error_result = result;
  11086. first_error_correct = correct;
  11087. }
  11088. // Special case, value is infinite, avoid NaN result in avg_err
  11089. // NaN also appears in results, if both are nan error is 0
  11090. if (!std::isinf(correct) && !std::isinf(result) && !std::isnan(correct) && !std::isnan(result)) {
  11091. avg_err += std::fabs(correct - result) / denom;
  11092. }
  11093. counter++;
  11094. }
  11095. }
  11096. }
  11097. }
  11098. avg_err /= counter;
  11099. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  11100. std::cerr << "TENSOR CHECK: avg_err=" << avg_err << " in " << ggml_op_name(tensor->op) << " (check " << check_counter << ")" << std::endl;
  11101. std::cerr << "tensor=" << tensor << " tensor->name=" << tensor->name << " tensor->type: " << ggml_type_name(tensor->type) << " ne0=" << tensor->ne[0] << " nb0=" << tensor->nb[0] << " ne1=" << tensor->ne[1] << " nb1=" << tensor->nb[1] << " ne2=" << tensor->ne[2] << " nb2=" << tensor->nb[2] << " ne3=" << tensor->ne[3] << " nb3=" << tensor->nb[3] << " offset=" << tensor->view_offs << std::endl;
  11102. if (src0 != nullptr) {
  11103. std::cerr << "src0=" << src0 << " op=" << ggml_op_name(src0->op) << " type=" << ggml_type_name(src0->type) << " ne0=" << src0->ne[0] << " nb0=" << src0->nb[0] << " ne1=" << src0->ne[1] << " nb1=" << src0->nb[1] << " ne2=" << src0->ne[2] << " nb2=" << src0->nb[2] << " ne3=" << src0->ne[3] << " nb3=" << src0->nb[3] << " offset=" << src0->view_offs << std::endl;
  11104. }
  11105. if (src1 != nullptr) {
  11106. std::cerr << "src1=" << src1 << " op=" << ggml_op_name(src1->op) << " type=" << ggml_type_name(src1->type) << " ne0=" << src1->ne[0] << " nb0=" << src1->nb[0] << " ne1=" << src1->ne[1] << " nb1=" << src1->nb[1] << " ne2=" << src1->ne[2] << " nb2=" << src1->nb[2] << " ne3=" << src1->ne[3] << " nb3=" << src1->nb[3] << " offset=" << src1->view_offs << std::endl;
  11107. }
  11108. if (src2 != nullptr) {
  11109. std::cerr << "src2=" << src2 << " op=" << ggml_op_name(src2->op) << " type=" << ggml_type_name(src2->type) << " ne0=" << src2->ne[0] << " nb0=" << src2->nb[0] << " ne1=" << src2->ne[1] << " nb1=" << src2->nb[1] << " ne2=" << src2->ne[2] << " nb2=" << src2->nb[2] << " ne3=" << src2->ne[3] << " nb3=" << src2->nb[3] << " offset=" << src2->view_offs << std::endl;
  11110. }
  11111. if (src3 != nullptr) {
  11112. std::cerr << "src3=" << src3 << " op=" << ggml_op_name(src3->op) << " type=" << ggml_type_name(src3->type) << " ne0=" << src3->ne[0] << " nb0=" << src3->nb[0] << " ne1=" << src3->ne[1] << " nb1=" << src3->nb[1] << " ne2=" << src3->ne[2] << " nb2=" << src3->nb[2] << " ne3=" << src3->ne[3] << " nb3=" << src3->nb[3] << " offset=" << src3->view_offs << std::endl;
  11113. }
  11114. std::cerr << "First error: result=" << first_error_result << " correct=" << first_error_correct << " i3=" << first_error[3] << " i2=" << first_error[2] << " i1=" << first_error[1] << " i0=" << first_error[0] << std::endl;
  11115. std::cerr << std::endl << "Result:" << std::endl;
  11116. ggml_vk_print_tensor_area(tensor, tensor_data, 5, 5, 0, 0);
  11117. std::cerr << std::endl << "Correct:" << std::endl;
  11118. ggml_vk_print_tensor_area(tensor, comp_result, 5, 5, 0, 0);
  11119. std::cerr << std::endl;
  11120. std::vector<const ggml_tensor *> done;
  11121. ggml_vk_print_graph_origin(tensor, done);
  11122. }
  11123. if (avg_err > 0.5 || std::isnan(avg_err)) {
  11124. std::cerr << "ERROR: avg_err=" << avg_err << " in " << ggml_op_name(tensor->op) << " (check " << check_counter << ")" << std::endl;
  11125. std::cerr << "tensor=" << tensor << " tensor->name=" << tensor->name << " tensor->type: " << ggml_type_name(tensor->type) << " ne0=" << tensor->ne[0] << " nb0=" << tensor->nb[0] << " ne1=" << tensor->ne[1] << " nb1=" << tensor->nb[1] << " ne2=" << tensor->ne[2] << " nb2=" << tensor->nb[2] << " ne3=" << tensor->ne[3] << " nb3=" << tensor->nb[3] << " offset=" << tensor->view_offs << std::endl;
  11126. if (src0 != nullptr) {
  11127. std::cerr << "src0=" << src0 << " op=" << ggml_op_name(src0->op) << " type=" << ggml_type_name(src0->type) << " ne0=" << src0->ne[0] << " nb0=" << src0->nb[0] << " ne1=" << src0->ne[1] << " nb1=" << src0->nb[1] << " ne2=" << src0->ne[2] << " nb2=" << src0->nb[2] << " ne3=" << src0->ne[3] << " nb3=" << src0->nb[3] << " offset=" << src0->view_offs << std::endl;
  11128. }
  11129. if (src1 != nullptr) {
  11130. std::cerr << "src1=" << src1 << " op=" << ggml_op_name(src1->op) << " type=" << ggml_type_name(src1->type) << " ne0=" << src1->ne[0] << " nb0=" << src1->nb[0] << " ne1=" << src1->ne[1] << " nb1=" << src1->nb[1] << " ne2=" << src1->ne[2] << " nb2=" << src1->nb[2] << " ne3=" << src1->ne[3] << " nb3=" << src1->nb[3] << " offset=" << src1->view_offs << std::endl;
  11131. }
  11132. if (src2 != nullptr) {
  11133. std::cerr << "src2=" << src2 << " op=" << ggml_op_name(src2->op) << " type=" << ggml_type_name(src2->type) << " ne0=" << src2->ne[0] << " nb0=" << src2->nb[0] << " ne1=" << src2->ne[1] << " nb1=" << src2->nb[1] << " ne2=" << src2->ne[2] << " nb2=" << src2->nb[2] << " ne3=" << src2->ne[3] << " nb3=" << src2->nb[3] << " offset=" << src2->view_offs << std::endl;
  11134. }
  11135. if (src3 != nullptr) {
  11136. std::cerr << "src3=" << src3 << " op=" << ggml_op_name(src3->op) << " type=" << ggml_type_name(src3->type) << " ne0=" << src3->ne[0] << " nb0=" << src3->nb[0] << " ne1=" << src3->ne[1] << " nb1=" << src3->nb[1] << " ne2=" << src3->ne[2] << " nb2=" << src3->nb[2] << " ne3=" << src3->ne[3] << " nb3=" << src3->nb[3] << " offset=" << src3->view_offs << std::endl;
  11137. }
  11138. std::cerr << "First error: result=" << first_error_result << " correct=" << first_error_correct << " i3=" << first_error[3] << " i2=" << first_error[2] << " i1=" << first_error[1] << " i0=" << first_error[0] << std::endl;
  11139. std::cerr << std::endl << "Result:" << std::endl;
  11140. ggml_vk_print_tensor_area(tensor, tensor_data, first_error[0], first_error[1], first_error[2], first_error[3]);
  11141. std::cerr << std::endl << "Correct:" << std::endl;
  11142. ggml_vk_print_tensor_area(tensor, comp_result, first_error[0], first_error[1], first_error[2], first_error[3]);
  11143. std::cerr << std::endl;
  11144. std::vector<const ggml_tensor *> done;
  11145. ggml_vk_print_graph_origin(tensor, done);
  11146. GGML_ABORT("fatal error");
  11147. } else {
  11148. std::cerr << check_counter << " " << tensor->name << " op=" << ggml_op_name(tensor->op) << " avg_err=" << avg_err << std::endl;
  11149. }
  11150. free(comp_result);
  11151. comp_result = nullptr;
  11152. comp_size = 0;
  11153. if (ggml_backend_buffer_is_vk(tensor->buffer)) {
  11154. free(tensor_data);
  11155. }
  11156. VK_LOG_DEBUG("END ggml_vk_check_results_1(" << tensor->name << ")");
  11157. }
  11158. #endif
  11159. GGML_BACKEND_DL_IMPL(ggml_backend_vk_reg)