ggml-vulkan.cpp 569 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412
  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 8
  88. struct vk_pipeline_struct {
  89. std::string name;
  90. vk::ShaderModule shader_module;
  91. vk::PipelineLayout layout;
  92. vk::Pipeline pipeline;
  93. uint32_t push_constant_size;
  94. uint32_t parameter_count;
  95. std::array<uint32_t, 3> wg_denoms;
  96. uint32_t align;
  97. // set to true to request the pipeline is compiled after the dryrun
  98. bool needed {};
  99. // set to true when the shader has been compiled
  100. bool compiled {};
  101. };
  102. typedef std::shared_ptr<vk_pipeline_struct> vk_pipeline;
  103. typedef std::weak_ptr<vk_pipeline_struct> vk_pipeline_ref;
  104. static void ggml_vk_destroy_pipeline(vk::Device& device, vk_pipeline& pipeline);
  105. struct vk_matmul_pipeline_struct {
  106. vk_pipeline l, m, s;
  107. vk_pipeline a_l, a_m, a_s;
  108. };
  109. typedef std::shared_ptr<vk_matmul_pipeline_struct> vk_matmul_pipeline;
  110. struct vk_matmul_pipeline2 {
  111. vk_matmul_pipeline2() {
  112. f16acc = std::make_shared<vk_matmul_pipeline_struct>();
  113. f32acc = std::make_shared<vk_matmul_pipeline_struct>();
  114. }
  115. vk_matmul_pipeline f32acc;
  116. vk_matmul_pipeline f16acc;
  117. };
  118. struct vk_device_struct;
  119. typedef std::shared_ptr<vk_device_struct> vk_device;
  120. typedef std::weak_ptr<vk_device_struct> vk_device_ref;
  121. struct vk_buffer_struct;
  122. typedef std::shared_ptr<vk_buffer_struct> vk_buffer;
  123. typedef std::weak_ptr<vk_buffer_struct> vk_buffer_ref;
  124. struct ggml_backend_vk_buffer_type_context {
  125. std::string name;
  126. vk_device device;
  127. };
  128. struct vk_queue;
  129. // Stores command pool/buffers. There's an instance of this
  130. // for each (context,queue) pair and for each (device,queue) pair.
  131. struct vk_command_pool {
  132. void init(vk_device& device, vk_queue *q_);
  133. void destroy(vk::Device& device);
  134. vk::CommandPool pool;
  135. uint32_t cmd_buffer_idx;
  136. std::vector<vk::CommandBuffer> cmd_buffers;
  137. vk_queue *q;
  138. };
  139. // Prevent simultaneous submissions to the same queue.
  140. // This could be per vk_queue if we stopped having two vk_queue structures
  141. // sharing the same vk::Queue.
  142. static std::mutex queue_mutex;
  143. struct vk_queue {
  144. uint32_t queue_family_index;
  145. vk::Queue queue;
  146. vk_command_pool cmd_pool;
  147. vk::PipelineStageFlags stage_flags;
  148. bool transfer_only;
  149. // copy everything except the cmd_pool
  150. void copyFrom(vk_queue &other) {
  151. queue_family_index = other.queue_family_index;
  152. queue = other.queue;
  153. stage_flags = other.stage_flags;
  154. transfer_only = other.transfer_only;
  155. }
  156. };
  157. static const char * ggml_backend_vk_buffer_type_name(ggml_backend_buffer_type_t buft);
  158. static ggml_backend_buffer_t ggml_backend_vk_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size);
  159. static size_t ggml_backend_vk_buffer_type_get_alignment(ggml_backend_buffer_type_t buft);
  160. static size_t ggml_backend_vk_buffer_type_get_max_size(ggml_backend_buffer_type_t buft);
  161. static size_t ggml_backend_vk_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor);
  162. static ggml_backend_buffer_type_i ggml_backend_vk_buffer_type_interface = {
  163. /* .get_name = */ ggml_backend_vk_buffer_type_name,
  164. /* .alloc_buffer = */ ggml_backend_vk_buffer_type_alloc_buffer,
  165. /* .get_alignment = */ ggml_backend_vk_buffer_type_get_alignment,
  166. /* .get_max_size = */ ggml_backend_vk_buffer_type_get_max_size,
  167. /* .get_alloc_size = */ ggml_backend_vk_buffer_type_get_alloc_size,
  168. /* .is_host = */ NULL,
  169. };
  170. #ifdef GGML_VULKAN_MEMORY_DEBUG
  171. class vk_memory_logger;
  172. #endif
  173. class vk_perf_logger;
  174. static void ggml_vk_destroy_buffer(vk_buffer& buf);
  175. static constexpr uint32_t mul_mat_vec_max_cols = 8;
  176. static constexpr uint32_t p021_max_gqa_ratio = 8;
  177. enum vk_device_architecture {
  178. OTHER,
  179. AMD_GCN,
  180. AMD_RDNA1,
  181. AMD_RDNA2,
  182. AMD_RDNA3,
  183. INTEL_XE2,
  184. };
  185. // HSK x HSV
  186. enum FaHeadSizes {
  187. FA_HEAD_SIZE_64,
  188. FA_HEAD_SIZE_80,
  189. FA_HEAD_SIZE_96,
  190. FA_HEAD_SIZE_112,
  191. FA_HEAD_SIZE_128,
  192. FA_HEAD_SIZE_192,
  193. FA_HEAD_SIZE_192_128,
  194. FA_HEAD_SIZE_256,
  195. FA_HEAD_SIZE_576_512,
  196. FA_HEAD_SIZE_UNSUPPORTED,
  197. FA_HEAD_SIZE_COUNT = FA_HEAD_SIZE_UNSUPPORTED,
  198. };
  199. static vk_device_architecture get_device_architecture(const vk::PhysicalDevice& device) {
  200. vk::PhysicalDeviceProperties props = device.getProperties();
  201. if (props.vendorID == VK_VENDOR_ID_AMD) {
  202. const std::vector<vk::ExtensionProperties> ext_props = device.enumerateDeviceExtensionProperties();
  203. bool amd_shader_core_properties = false;
  204. bool integer_dot_product = false;
  205. bool subgroup_size_control = false;
  206. for (const auto& properties : ext_props) {
  207. if (strcmp("VK_AMD_shader_core_properties", properties.extensionName) == 0) {
  208. amd_shader_core_properties = true;
  209. } else if (strcmp("VK_KHR_shader_integer_dot_product", properties.extensionName) == 0) {
  210. integer_dot_product = true;
  211. } else if (strcmp("VK_EXT_subgroup_size_control", properties.extensionName) == 0) {
  212. subgroup_size_control = true;
  213. }
  214. }
  215. if (!amd_shader_core_properties || !integer_dot_product || !subgroup_size_control) {
  216. return vk_device_architecture::OTHER;
  217. }
  218. vk::PhysicalDeviceProperties2 props2;
  219. vk::PhysicalDeviceShaderCorePropertiesAMD shader_core_props_amd;
  220. vk::PhysicalDeviceShaderIntegerDotProductPropertiesKHR integer_dot_props;
  221. vk::PhysicalDeviceSubgroupSizeControlPropertiesEXT subgroup_size_control_props;
  222. props2.pNext = &shader_core_props_amd;
  223. shader_core_props_amd.pNext = &integer_dot_props;
  224. integer_dot_props.pNext = &subgroup_size_control_props;
  225. device.getProperties2(&props2);
  226. if (subgroup_size_control_props.maxSubgroupSize == 64 && subgroup_size_control_props.minSubgroupSize == 64) {
  227. return vk_device_architecture::AMD_GCN;
  228. }
  229. if (subgroup_size_control_props.maxSubgroupSize == 64 && subgroup_size_control_props.minSubgroupSize == 32) {
  230. // RDNA
  231. if (shader_core_props_amd.wavefrontsPerSimd == 20) {
  232. return vk_device_architecture::AMD_RDNA1;
  233. }
  234. if (integer_dot_props.integerDotProduct4x8BitPackedMixedSignednessAccelerated) {
  235. return vk_device_architecture::AMD_RDNA3;
  236. }
  237. return vk_device_architecture::AMD_RDNA2;
  238. }
  239. } else if (props.vendorID == VK_VENDOR_ID_INTEL) {
  240. const std::vector<vk::ExtensionProperties> ext_props = device.enumerateDeviceExtensionProperties();
  241. bool subgroup_size_control = false;
  242. for (const auto& properties : ext_props) {
  243. if (strcmp("VK_EXT_subgroup_size_control", properties.extensionName) == 0) {
  244. subgroup_size_control = true;
  245. }
  246. }
  247. if (!subgroup_size_control) {
  248. return vk_device_architecture::OTHER;
  249. }
  250. vk::PhysicalDeviceProperties2 props2;
  251. vk::PhysicalDeviceSubgroupSizeControlPropertiesEXT subgroup_size_control_props;
  252. props2.pNext = &subgroup_size_control_props;
  253. device.getProperties2(&props2);
  254. if (subgroup_size_control_props.minSubgroupSize == 16) {
  255. // Xe2 architecture uses SIMD16 while previous Xe and Gen architecture uses SIMD8.
  256. // Minimum subgroup size matches the SIMD width so we distinguish architecture by checking this value.
  257. // https://www.intel.com/content/www/us/en/content-details/824434/2024-intel-tech-tour-xe2-and-lunar-lake-s-gpu.html
  258. // https://www.intel.com/content/www/us/en/docs/oneapi/optimization-guide-gpu/2025-0/intel-xe-gpu-architecture.html
  259. return vk_device_architecture::INTEL_XE2;
  260. }
  261. }
  262. return vk_device_architecture::OTHER;
  263. }
  264. struct vk_device_struct {
  265. std::recursive_mutex mutex;
  266. vk::PhysicalDevice physical_device;
  267. vk::PhysicalDeviceProperties properties;
  268. std::string name;
  269. uint64_t max_memory_allocation_size;
  270. uint64_t suballocation_block_size;
  271. bool fp16;
  272. bool pipeline_robustness;
  273. vk::Device device;
  274. uint32_t vendor_id;
  275. vk::DriverId driver_id;
  276. vk_device_architecture architecture;
  277. vk_queue compute_queue;
  278. vk_queue transfer_queue;
  279. bool single_queue;
  280. uint32_t subgroup_size;
  281. uint32_t shader_core_count;
  282. bool uma;
  283. bool prefer_host_memory;
  284. bool float_controls_rte_fp16;
  285. bool subgroup_add;
  286. bool subgroup_shuffle;
  287. bool integer_dot_product;
  288. bool subgroup_size_control;
  289. uint32_t subgroup_min_size;
  290. uint32_t subgroup_max_size;
  291. bool subgroup_require_full_support;
  292. bool coopmat_support;
  293. bool coopmat_acc_f32_support {};
  294. bool coopmat_acc_f16_support {};
  295. bool coopmat_bf16_support {};
  296. bool coopmat_support_16x16x16_f16acc {};
  297. bool coopmat_support_16x16x16_f32acc {};
  298. bool coopmat1_fa_support {};
  299. uint32_t coopmat_m;
  300. uint32_t coopmat_n;
  301. uint32_t coopmat_k;
  302. bool coopmat_int_support;
  303. uint32_t coopmat_int_m;
  304. uint32_t coopmat_int_n;
  305. uint32_t coopmat_int_k;
  306. bool coopmat2;
  307. size_t idx;
  308. bool mul_mat_l[GGML_TYPE_COUNT];
  309. bool mul_mat_m[GGML_TYPE_COUNT];
  310. bool mul_mat_s[GGML_TYPE_COUNT];
  311. bool mul_mat_id_l[GGML_TYPE_COUNT];
  312. bool mul_mat_id_m[GGML_TYPE_COUNT];
  313. bool mul_mat_id_s[GGML_TYPE_COUNT];
  314. // set to true to indicate that some shaders need to be compiled after the dryrun
  315. bool need_compiles {};
  316. vk::DescriptorSetLayout dsl;
  317. vk_matmul_pipeline pipeline_matmul_f32 {};
  318. vk_matmul_pipeline pipeline_matmul_f32_f16 {};
  319. vk_matmul_pipeline pipeline_matmul_bf16 {};
  320. vk_matmul_pipeline2 pipeline_matmul_f16;
  321. vk_matmul_pipeline2 pipeline_matmul_f16_f32;
  322. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat[GGML_TYPE_COUNT];
  323. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_COUNT];
  324. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_COUNT];
  325. vk_matmul_pipeline pipeline_matmul_id_f32 {};
  326. vk_matmul_pipeline pipeline_matmul_id_bf16 {};
  327. vk_matmul_pipeline2 pipeline_matmul_id_f16;
  328. vk_matmul_pipeline2 pipeline_matmul_id_f16_f32;
  329. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat_id[GGML_TYPE_COUNT];
  330. vk_pipeline pipeline_matmul_split_k_reduce;
  331. vk_pipeline pipeline_quantize_q8_1;
  332. vk_pipeline pipeline_dequant[GGML_TYPE_COUNT];
  333. vk_pipeline pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_COUNT][mul_mat_vec_max_cols];
  334. vk_pipeline pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_COUNT][mul_mat_vec_max_cols];
  335. vk_pipeline pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_COUNT];
  336. vk_pipeline pipeline_mul_mat_vec_p021_f16_f32[p021_max_gqa_ratio];
  337. vk_pipeline pipeline_mul_mat_vec_nc_f16_f32;
  338. vk_pipeline pipeline_get_rows[GGML_TYPE_COUNT];
  339. vk_pipeline pipeline_get_rows_f32[GGML_TYPE_COUNT];
  340. vk_pipeline pipeline_acc_f32;
  341. // [src0 0=fp32,1=fp16][src1 0=fp32,1=fp16][dst 0=fp32,1=fp16]
  342. vk_pipeline pipeline_add[2][2][2];
  343. vk_pipeline pipeline_add_norepeat[2][2][2];
  344. vk_pipeline pipeline_sub[2][2][2];
  345. vk_pipeline pipeline_sub_norepeat[2][2][2];
  346. vk_pipeline pipeline_mul[2][2][2];
  347. vk_pipeline pipeline_mul_norepeat[2][2][2];
  348. vk_pipeline pipeline_div[2][2][2];
  349. vk_pipeline pipeline_div_norepeat[2][2][2];
  350. vk_pipeline pipeline_concat_f32, pipeline_concat_f16, pipeline_concat_i32;
  351. vk_pipeline pipeline_upscale_nearest_f32, pipeline_upscale_bilinear_f32, pipeline_upscale_bilinear_ac_f32;
  352. vk_pipeline pipeline_scale_f32;
  353. vk_pipeline pipeline_sqr_f32;
  354. vk_pipeline pipeline_sin_f32;
  355. vk_pipeline pipeline_cos_f32;
  356. vk_pipeline pipeline_clamp_f32;
  357. vk_pipeline pipeline_pad_f32;
  358. vk_pipeline pipeline_roll_f32;
  359. vk_pipeline pipeline_repeat_f32, pipeline_repeat_back_f32;
  360. vk_pipeline pipeline_cpy_f32_f32, pipeline_cpy_f32_f16, pipeline_cpy_f16_f16, pipeline_cpy_f16_f32, pipeline_cpy_f32_bf16;
  361. 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;
  362. vk_pipeline pipeline_cpy_f32_quant[GGML_TYPE_COUNT];
  363. vk_pipeline pipeline_cpy_quant_f32[GGML_TYPE_COUNT];
  364. vk_pipeline pipeline_set_rows[GGML_TYPE_COUNT];
  365. vk_pipeline pipeline_norm_f32;
  366. vk_pipeline pipeline_group_norm_f32;
  367. vk_pipeline pipeline_rms_norm_f32;
  368. vk_pipeline pipeline_rms_norm_mul_f32;
  369. vk_pipeline pipeline_rms_norm_back_f32;
  370. vk_pipeline pipeline_l2_norm_f32;
  371. // [src/dst 0=fp32,1=fp16]
  372. vk_pipeline pipeline_gelu[2];
  373. vk_pipeline pipeline_gelu_erf[2];
  374. vk_pipeline pipeline_gelu_quick[2];
  375. vk_pipeline pipeline_silu[2];
  376. vk_pipeline pipeline_relu[2];
  377. vk_pipeline pipeline_tanh[2];
  378. vk_pipeline pipeline_sigmoid[2];
  379. vk_pipeline pipeline_geglu[2];
  380. vk_pipeline pipeline_reglu[2];
  381. vk_pipeline pipeline_swiglu[2];
  382. vk_pipeline pipeline_geglu_erf[2];
  383. vk_pipeline pipeline_geglu_quick[2];
  384. vk_pipeline pipeline_leaky_relu_f32;
  385. vk_pipeline pipeline_silu_back_f32;
  386. vk_pipeline pipeline_diag_mask_inf_f32;
  387. vk_pipeline pipeline_soft_max_f32, pipeline_soft_max_f32_f16;
  388. vk_pipeline pipeline_soft_max_f32_wg512, pipeline_soft_max_f32_f16_wg512;
  389. vk_pipeline pipeline_soft_max_back_f32;
  390. vk_pipeline pipeline_rope_norm_f32, pipeline_rope_norm_f16;
  391. vk_pipeline pipeline_rope_neox_f32, pipeline_rope_neox_f16;
  392. vk_pipeline pipeline_rope_multi_f32, pipeline_rope_multi_f16;
  393. vk_pipeline pipeline_rope_vision_f32, pipeline_rope_vision_f16;
  394. vk_pipeline pipeline_argsort_f32;
  395. vk_pipeline pipeline_sum_rows_f32;
  396. vk_pipeline pipeline_argmax_f32;
  397. vk_pipeline pipeline_count_equal_i32;
  398. vk_pipeline pipeline_im2col_f32, pipeline_im2col_f32_f16;
  399. vk_pipeline pipeline_timestep_embedding_f32;
  400. vk_pipeline pipeline_conv_transpose_1d_f32;
  401. vk_pipeline pipeline_pool2d_f32;
  402. vk_pipeline pipeline_rwkv_wkv6_f32;
  403. vk_pipeline pipeline_rwkv_wkv7_f32;
  404. vk_pipeline pipeline_opt_step_adamw_f32;
  405. vk_pipeline pipeline_conv2d_dw_whcn_f32;
  406. vk_pipeline pipeline_conv2d_dw_cwhn_f32;
  407. // [2][2][2] is for {f16acc,f32acc}x{large,small_rows}x{unaligned, aligned}
  408. vk_pipeline pipeline_flash_attn_f32_f16_cm2[GGML_TYPE_COUNT][FA_HEAD_SIZE_COUNT][2][2][2];
  409. vk_pipeline pipeline_flash_attn_f32_f16_cm1[GGML_TYPE_COUNT][FA_HEAD_SIZE_COUNT][2][2][2];
  410. vk_pipeline pipeline_flash_attn_f32_f16[GGML_TYPE_COUNT][FA_HEAD_SIZE_COUNT][2][2][2];
  411. vk_pipeline pipeline_flash_attn_split_k_reduce;
  412. std::unordered_map<std::string, vk_pipeline_ref> pipelines;
  413. std::vector<std::tuple<void*, size_t, vk_buffer>> pinned_memory;
  414. vk::Fence fence;
  415. vk_buffer sync_staging;
  416. ggml_backend_buffer_type buffer_type;
  417. bool disable_fusion;
  418. #ifdef GGML_VULKAN_MEMORY_DEBUG
  419. std::unique_ptr<vk_memory_logger> memory_logger;
  420. #endif
  421. // for GGML_VK_PERF_LOGGER
  422. std::unique_ptr<vk_perf_logger> perf_logger;
  423. vk::QueryPool query_pool;
  424. int32_t num_queries;
  425. ~vk_device_struct() {
  426. VK_LOG_DEBUG("destroy device " << name);
  427. device.destroyFence(fence);
  428. ggml_vk_destroy_buffer(sync_staging);
  429. compute_queue.cmd_pool.destroy(device);
  430. transfer_queue.cmd_pool.destroy(device);
  431. for (auto& pipeline : pipelines) {
  432. if (pipeline.second.expired()) {
  433. continue;
  434. }
  435. vk_pipeline pl = pipeline.second.lock();
  436. ggml_vk_destroy_pipeline(device, pl);
  437. }
  438. pipelines.clear();
  439. device.destroyDescriptorSetLayout(dsl);
  440. device.destroy();
  441. }
  442. };
  443. void vk_command_pool::init(vk_device& device, vk_queue *q_) {
  444. cmd_buffer_idx = 0;
  445. q = q_;
  446. vk::CommandPoolCreateInfo command_pool_create_info(vk::CommandPoolCreateFlags(VK_COMMAND_POOL_CREATE_TRANSIENT_BIT), q->queue_family_index);
  447. pool = device->device.createCommandPool(command_pool_create_info);
  448. }
  449. void vk_command_pool::destroy(vk::Device& device) {
  450. device.destroyCommandPool(pool);
  451. pool = nullptr;
  452. cmd_buffers.clear();
  453. }
  454. struct vk_buffer_struct {
  455. vk::Buffer buffer = VK_NULL_HANDLE;
  456. vk::DeviceMemory device_memory = VK_NULL_HANDLE;
  457. vk::MemoryPropertyFlags memory_property_flags;
  458. void * ptr;
  459. size_t size = 0;
  460. vk_device device;
  461. ~vk_buffer_struct() {
  462. if (size == 0) {
  463. return;
  464. }
  465. VK_LOG_DEBUG("~vk_buffer_struct(" << buffer << ", " << size << ")");
  466. device->device.freeMemory(device_memory);
  467. device->device.destroyBuffer(buffer);
  468. }
  469. };
  470. struct vk_subbuffer {
  471. vk_buffer buffer;
  472. uint64_t offset;
  473. uint64_t size;
  474. operator vk::DescriptorBufferInfo() const {
  475. return { buffer->buffer, offset, size };
  476. }
  477. };
  478. struct vk_semaphore {
  479. vk::Semaphore s;
  480. uint64_t value;
  481. };
  482. struct vk_submission {
  483. vk::CommandBuffer buffer;
  484. std::vector<vk_semaphore> wait_semaphores;
  485. std::vector<vk_semaphore> signal_semaphores;
  486. };
  487. typedef std::vector<vk_submission> vk_sequence;
  488. struct vk_mat_mat_push_constants {
  489. uint32_t M; uint32_t N; uint32_t K;
  490. uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  491. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  492. uint32_t k_split;
  493. uint32_t ne02; uint32_t ne12; uint32_t broadcast2; uint32_t broadcast3;
  494. uint32_t padded_N;
  495. };
  496. struct vk_mat_vec_push_constants {
  497. uint32_t ncols; uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  498. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  499. uint32_t ne02; uint32_t ne12; uint32_t broadcast2; uint32_t broadcast3;
  500. };
  501. struct vk_mat_mat_id_push_constants {
  502. uint32_t M; uint32_t N; uint32_t K;
  503. uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  504. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  505. uint32_t nei0; uint32_t nei1; uint32_t nbi1; uint32_t ne11;
  506. uint32_t padded_N;
  507. };
  508. struct vk_mat_vec_id_push_constants {
  509. uint32_t ncols; uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  510. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  511. uint32_t nei0; uint32_t ne11;
  512. };
  513. struct vk_flash_attn_push_constants {
  514. uint32_t N;
  515. uint32_t KV;
  516. uint32_t ne1;
  517. uint32_t ne2;
  518. uint32_t ne3;
  519. uint32_t neq2;
  520. uint32_t neq3;
  521. uint32_t nek2;
  522. uint32_t nek3;
  523. uint32_t nev2;
  524. uint32_t nev3;
  525. uint32_t nem1;
  526. uint32_t nem2;
  527. uint32_t nem3;
  528. uint32_t nb01;
  529. uint32_t nb02;
  530. uint32_t nb03;
  531. uint32_t nb11;
  532. uint32_t nb12;
  533. uint32_t nb13;
  534. uint32_t nb21;
  535. uint32_t nb22;
  536. uint32_t nb23;
  537. float scale;
  538. float max_bias;
  539. float logit_softcap;
  540. uint32_t mask_n_head_log2;
  541. float m0;
  542. float m1;
  543. uint32_t gqa_ratio;
  544. uint32_t split_kv;
  545. uint32_t k_num;
  546. };
  547. static_assert(sizeof(vk_flash_attn_push_constants) <= 128, "sizeof(vk_flash_attn_push_constants) must be <= 128");
  548. struct vk_op_push_constants {
  549. uint32_t KX;
  550. uint32_t KY;
  551. float param1;
  552. float param2;
  553. };
  554. struct vk_op_glu_push_constants {
  555. uint32_t N;
  556. uint32_t ne00;
  557. uint32_t ne20;
  558. uint32_t mode; // 0: default, 1: swapped, 2: split
  559. };
  560. struct vk_op_unary_push_constants {
  561. uint32_t ne;
  562. uint32_t ne00; uint32_t ne01; uint32_t ne02; uint32_t ne03; uint32_t nb00; uint32_t nb01; uint32_t nb02; uint32_t nb03;
  563. uint32_t ne10; uint32_t ne11; uint32_t ne12; uint32_t ne13; uint32_t nb10; uint32_t nb11; uint32_t nb12; uint32_t nb13;
  564. uint32_t misalign_offsets;
  565. float param1; float param2;
  566. uint32_t ne0_012mp; uint32_t ne0_012L;
  567. uint32_t ne0_01mp; uint32_t ne0_01L;
  568. uint32_t ne0_0mp; uint32_t ne0_0L;
  569. uint32_t ne1_012mp; uint32_t ne1_012L;
  570. uint32_t ne1_01mp; uint32_t ne1_01L;
  571. uint32_t ne1_0mp; uint32_t ne1_0L;
  572. };
  573. static_assert(sizeof(vk_op_unary_push_constants) <= 128, "sizeof(vk_op_unary_push_constants) must be <= 128");
  574. static vk_op_unary_push_constants vk_op_unary_push_constants_init(const ggml_tensor * src0, const ggml_tensor * dst, int64_t ne = 0) {
  575. GGML_ASSERT(ne != 0 || (ggml_nelements(src0) == ggml_nelements(dst)));
  576. ne = ne != 0 ? ne : ggml_nelements(dst);
  577. GGML_ASSERT(ne <= (int64_t)std::numeric_limits<uint32_t>::max());
  578. vk_op_unary_push_constants p{};
  579. p.ne = (uint32_t)ne;
  580. size_t src0_tsize = ggml_type_size(src0->type);
  581. p.ne00 = (uint32_t)src0->ne[0];
  582. p.ne01 = (uint32_t)src0->ne[1];
  583. p.ne02 = (uint32_t)src0->ne[2];
  584. p.ne03 = (uint32_t)src0->ne[3];
  585. p.nb00 = (uint32_t)(src0->nb[0] / src0_tsize);
  586. p.nb01 = (uint32_t)(src0->nb[1] / src0_tsize);
  587. p.nb02 = (uint32_t)(src0->nb[2] / src0_tsize);
  588. p.nb03 = (uint32_t)(src0->nb[3] / src0_tsize);
  589. size_t dst_tsize = ggml_type_size(dst->type);
  590. p.ne10 = (uint32_t)dst->ne[0];
  591. p.ne11 = (uint32_t)dst->ne[1];
  592. p.ne12 = (uint32_t)dst->ne[2];
  593. p.ne13 = (uint32_t)dst->ne[3];
  594. p.nb10 = (uint32_t)(dst->nb[0] / dst_tsize);
  595. p.nb11 = (uint32_t)(dst->nb[1] / dst_tsize);
  596. p.nb12 = (uint32_t)(dst->nb[2] / dst_tsize);
  597. p.nb13 = (uint32_t)(dst->nb[3] / dst_tsize);
  598. return p; // fastdiv values and offsets are initialized later in ggml_vk_op
  599. }
  600. // See https://gmplib.org/~tege/divcnst-pldi94.pdf figure 4.1.
  601. // Precompute mp (m' in the paper) and L such that division
  602. // can be computed using a multiply (high 32b of 64b result)
  603. // and a shift:
  604. //
  605. // n/d = (mulhi(n, mp) + n) >> L;
  606. static void init_fastdiv_values(uint32_t d, uint32_t &mp, uint32_t &L)
  607. {
  608. // compute L = ceil(log2(d));
  609. L = 0;
  610. while (L < 32 && (uint32_t{1} << L) < d) {
  611. L++;
  612. }
  613. mp = (uint32_t)((uint64_t{1} << 32) * ((uint64_t{1} << L) - d) / d + 1);
  614. }
  615. template <typename T> void init_pushconst_fastdiv(T &p) {
  616. GGML_UNUSED(p);
  617. static_assert(!std::is_const<T>::value, "unexpected type");
  618. }
  619. template <> void init_pushconst_fastdiv(vk_op_unary_push_constants &p) {
  620. // Compute magic values to divide by these six numbers.
  621. init_fastdiv_values(p.ne02*p.ne01*p.ne00, p.ne0_012mp, p.ne0_012L);
  622. init_fastdiv_values(p.ne01*p.ne00, p.ne0_01mp, p.ne0_01L);
  623. init_fastdiv_values(p.ne00, p.ne0_0mp, p.ne0_0L);
  624. init_fastdiv_values(p.ne12*p.ne11*p.ne10, p.ne1_012mp, p.ne1_012L);
  625. init_fastdiv_values(p.ne11*p.ne10, p.ne1_01mp, p.ne1_01L);
  626. init_fastdiv_values(p.ne10, p.ne1_0mp, p.ne1_0L);
  627. }
  628. struct vk_op_binary_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 ne20; uint32_t ne21; uint32_t ne22; uint32_t ne23; uint32_t nb20; uint32_t nb21; uint32_t nb22; uint32_t nb23;
  633. uint32_t misalign_offsets;
  634. float param1; float param2; int32_t param3;
  635. };
  636. struct vk_op_diag_mask_push_constants {
  637. uint32_t ncols;
  638. uint32_t rows_per_channel;
  639. int32_t n_past;
  640. };
  641. struct vk_op_rope_push_constants {
  642. uint32_t ncols;
  643. uint32_t n_dims;
  644. float freq_scale;
  645. uint32_t p_delta_rows;
  646. float freq_base;
  647. float ext_factor;
  648. float attn_factor;
  649. float corr_dims[2];
  650. float theta_scale;
  651. uint32_t has_ff;
  652. uint32_t ne02;
  653. uint32_t s1;
  654. uint32_t s2;
  655. int32_t sections[4];
  656. uint32_t is_back;
  657. };
  658. struct vk_op_soft_max_push_constants {
  659. uint32_t KX;
  660. uint32_t KY;
  661. uint32_t ne00;
  662. uint32_t ne01;
  663. uint32_t ne02;
  664. uint32_t ne12;
  665. uint32_t ne13;
  666. uint32_t nb11;
  667. uint32_t nb12;
  668. uint32_t nb13;
  669. float scale;
  670. float max_bias;
  671. float m0;
  672. float m1;
  673. uint32_t n_head_log2;
  674. uint32_t nrows_x;
  675. };
  676. struct vk_op_argsort_push_constants {
  677. uint32_t ncols;
  678. uint32_t ncols_pad;
  679. int32_t order;
  680. };
  681. struct vk_op_im2col_push_constants {
  682. uint32_t batch_offset; uint32_t offset_delta;
  683. uint32_t IC;
  684. uint32_t IW; uint32_t IH;
  685. uint32_t OW; uint32_t OH;
  686. uint32_t KW; uint32_t KH;
  687. uint32_t pelements;
  688. uint32_t CHW;
  689. int32_t s0; int32_t s1;
  690. int32_t p0; int32_t p1;
  691. int32_t d0; int32_t d1;
  692. };
  693. struct vk_op_timestep_embedding_push_constants {
  694. uint32_t nb1;
  695. uint32_t dim;
  696. uint32_t max_period;
  697. };
  698. struct vk_op_conv_transpose_1d_push_constants {
  699. uint32_t Cout;
  700. uint32_t Cin;
  701. uint32_t K;
  702. uint32_t L;
  703. uint32_t KL;
  704. uint32_t nb01;
  705. uint32_t nb02;
  706. uint32_t nb11;
  707. uint32_t nb1;
  708. int32_t s0;
  709. };
  710. struct vk_op_pool2d_push_constants {
  711. uint32_t IW; uint32_t IH;
  712. uint32_t OW; uint32_t OH;
  713. uint32_t OC;
  714. uint32_t pelements;
  715. uint32_t op;
  716. int32_t k0; int32_t k1;
  717. int32_t s0; int32_t s1;
  718. int32_t p0; int32_t p1;
  719. };
  720. struct vk_op_rwkv_wkv6_push_constants {
  721. uint32_t B;
  722. uint32_t T;
  723. uint32_t C;
  724. uint32_t H;
  725. };
  726. struct vk_op_rwkv_wkv7_push_constants {
  727. uint32_t B;
  728. uint32_t T;
  729. uint32_t C;
  730. uint32_t H;
  731. };
  732. struct vk_op_conv2d_dw_push_constants {
  733. uint32_t ne;
  734. uint32_t batches;
  735. uint32_t channels;
  736. uint32_t dst_w;
  737. uint32_t dst_h;
  738. uint32_t src_w;
  739. uint32_t src_h;
  740. uint32_t knl_w;
  741. uint32_t knl_h;
  742. int32_t stride_x;
  743. int32_t stride_y;
  744. int32_t pad_x;
  745. int32_t pad_y;
  746. int32_t dilation_x;
  747. int32_t dilation_y;
  748. };
  749. struct vk_op_upscale_push_constants {
  750. uint32_t ne; uint32_t a_offset; uint32_t d_offset;
  751. uint32_t ne00; uint32_t ne01;
  752. uint32_t nb00; uint32_t nb01; uint32_t nb02; uint32_t nb03;
  753. uint32_t ne10; uint32_t ne11; uint32_t ne12; uint32_t ne13;
  754. float sf0; float sf1; float sf2; float sf3;
  755. };
  756. // Allow pre-recording command buffers
  757. struct vk_staging_memcpy {
  758. vk_staging_memcpy(void * _dst, const void * _src, size_t _n) : dst(_dst), src(_src), n(_n) {}
  759. void * dst;
  760. const void * src;
  761. size_t n;
  762. };
  763. struct vk_context_struct {
  764. vk_submission * s;
  765. std::vector<vk_sequence> seqs;
  766. int exit_tensor_idx;
  767. std::vector<vk_staging_memcpy> in_memcpys;
  768. std::vector<vk_staging_memcpy> out_memcpys;
  769. vk_command_pool * p {};
  770. };
  771. typedef std::shared_ptr<vk_context_struct> vk_context;
  772. typedef std::weak_ptr<vk_context_struct> vk_context_ref;
  773. struct ggml_vk_garbage_collector {
  774. std::vector<vk_semaphore> tl_semaphores;
  775. std::vector<vk_semaphore> semaphores;
  776. std::vector<vk::Event> events;
  777. std::vector<vk_buffer> temp_buffers;
  778. std::vector<vk_context> contexts;
  779. };
  780. #if defined(GGML_VULKAN_MEMORY_DEBUG) || defined(GGML_VULKAN_DEBUG)
  781. #define VK_LOG_MEMORY(msg) std::cerr << "ggml_vulkan memory: " << msg << std::endl
  782. static std::string format_size(size_t size) {
  783. const size_t kib = 1024;
  784. const size_t mib = kib * 1024;
  785. const size_t gib = mib * 1024;
  786. std::ostringstream oss;
  787. oss << std::fixed << std::setprecision(2);
  788. if (size >= gib) {
  789. oss << static_cast<double>(size) / gib << " GiB";
  790. } else if (size >= mib) {
  791. oss << static_cast<double>(size) / mib << " MiB";
  792. } else if (size >= kib) {
  793. oss << static_cast<double>(size) / kib << " KiB";
  794. } else {
  795. oss << size << " B";
  796. }
  797. return oss.str();
  798. }
  799. static std::mutex log_mutex;
  800. class vk_memory_logger {
  801. public:
  802. vk_memory_logger(): total_device(0), total_host(0) {}
  803. void log_allocation(vk_buffer_ref buf_ref, size_t size);
  804. void log_deallocation(vk_buffer_ref buf_ref);
  805. private:
  806. std::map<vk::Buffer, size_t> allocations; // Track allocations
  807. size_t total_device;
  808. size_t total_host;
  809. };
  810. #else
  811. #define VK_LOG_MEMORY(msg) ((void) 0)
  812. #endif // GGML_VULKAN_MEMORY_DEBUG
  813. class vk_perf_logger {
  814. public:
  815. void print_timings() {
  816. std::cerr << "----------------\nVulkan Timings:" << std::endl;
  817. for (const auto& t : timings) {
  818. uint64_t total = 0;
  819. for (const auto& time : t.second) {
  820. total += time;
  821. }
  822. std::cerr << t.first << ": " << t.second.size() << " x " << (total / t.second.size() / 1000.0) << " us" << std::endl;
  823. }
  824. timings.clear();
  825. }
  826. void log_timing(const ggml_tensor * node, uint64_t time) {
  827. if (node->op == GGML_OP_UNARY) {
  828. timings[ggml_unary_op_name(ggml_get_unary_op(node))].push_back(time);
  829. return;
  830. }
  831. if (node->op == GGML_OP_MUL_MAT || node->op == GGML_OP_MUL_MAT_ID) {
  832. const uint64_t m = node->src[0]->ne[1];
  833. const uint64_t n = node->src[1]->ne[1];
  834. const uint64_t k = node->src[1]->ne[0];
  835. std::string name = ggml_op_name(node->op);
  836. if (n == 1) {
  837. name += "_VEC m=" + std::to_string(m) + " k=" + std::to_string(k);
  838. } else {
  839. name += " m=" + std::to_string(m) + " n=" + std::to_string(n) + " k=" + std::to_string(k);
  840. }
  841. timings[name].push_back(time);
  842. return;
  843. }
  844. timings[ggml_op_name(node->op)].push_back(time);
  845. }
  846. private:
  847. std::map<std::string, std::vector<uint64_t>> timings;
  848. };
  849. struct ggml_backend_vk_context {
  850. std::string name;
  851. vk_device device;
  852. size_t semaphore_idx, event_idx;
  853. ggml_vk_garbage_collector gc;
  854. size_t prealloc_size_x, prealloc_size_y, prealloc_size_split_k;
  855. vk_buffer prealloc_x, prealloc_y, prealloc_split_k;
  856. vk::Fence fence, almost_ready_fence;
  857. bool almost_ready_fence_pending {};
  858. vk_buffer buffer_pool[MAX_VK_BUFFERS];
  859. vk_context_ref compute_ctx;
  860. vk_context_ref transfer_ctx;
  861. std::vector<vk_context_ref> tensor_ctxs;
  862. std::vector<vk::DescriptorPool> descriptor_pools;
  863. std::vector<vk::DescriptorSet> descriptor_sets;
  864. uint32_t descriptor_set_idx {};
  865. uint32_t pipeline_descriptor_set_requirements {};
  866. vk_command_pool compute_cmd_pool;
  867. vk_command_pool transfer_cmd_pool;
  868. // number of additional consecutive nodes that are being fused with the
  869. // node currently being processed
  870. int num_additional_fused_ops {};
  871. };
  872. static void * const vk_ptr_base = (void *)(uintptr_t) 0x1000; // NOLINT
  873. static uint64_t vk_tensor_offset(const ggml_tensor * tensor) {
  874. if (tensor->view_src) {
  875. return (uint8_t *) tensor->view_src->data - (uint8_t *) vk_ptr_base;
  876. }
  877. return (uint8_t *) tensor->data - (uint8_t *) vk_ptr_base;
  878. }
  879. struct ggml_backend_vk_buffer_context {
  880. vk_device_ref device;
  881. vk_buffer dev_buffer;
  882. std::string name;
  883. ggml_backend_vk_buffer_context(vk_device_ref device, vk_buffer&& dev_buffer, std::string& name) :
  884. device(device),
  885. dev_buffer(dev_buffer),
  886. name(name) {
  887. }
  888. ~ggml_backend_vk_buffer_context() {
  889. ggml_vk_destroy_buffer(dev_buffer);
  890. }
  891. };
  892. #ifdef GGML_VULKAN_MEMORY_DEBUG
  893. void vk_memory_logger::log_allocation(vk_buffer_ref buf_ref, size_t size) {
  894. std::lock_guard<std::mutex> guard(log_mutex);
  895. vk_buffer buf = buf_ref.lock();
  896. const bool device = bool(buf->memory_property_flags & vk::MemoryPropertyFlagBits::eDeviceLocal);
  897. const std::string type = device ? "device" : "host";
  898. allocations[buf->buffer] = size;
  899. total_device += device ? size : 0;
  900. total_host += device ? 0 : size;
  901. 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));
  902. }
  903. void vk_memory_logger::log_deallocation(vk_buffer_ref buf_ref) {
  904. if (buf_ref.expired() || buf_ref.lock()->size == 0) {
  905. return;
  906. }
  907. std::lock_guard<std::mutex> guard(log_mutex);
  908. vk_buffer buf = buf_ref.lock();
  909. const bool device = bool(buf->memory_property_flags & vk::MemoryPropertyFlagBits::eDeviceLocal);
  910. std::string type = device ? "device" : "host";
  911. auto it = allocations.find(buf->buffer);
  912. total_device -= device ? it->second : 0;
  913. total_host -= device ? 0 : it->second;
  914. if (it != allocations.end()) {
  915. 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));
  916. allocations.erase(it);
  917. } else {
  918. VK_LOG_MEMORY("ERROR " << buf->device->name << ": Attempted to deallocate unknown " << type << " memory at " << buf->buffer);
  919. }
  920. }
  921. #endif // GGML_VULKAN_MEMORY_DEBUG
  922. struct vk_instance_t {
  923. vk::Instance instance;
  924. bool debug_utils_support = false; // VK_EXT_debug_utils enabled
  925. PFN_vkSetDebugUtilsObjectNameEXT pfn_vkSetDebugUtilsObjectNameEXT = {};
  926. PFN_vkQueueBeginDebugUtilsLabelEXT pfn_vkQueueBeginDebugUtilsLabelEXT = {};
  927. PFN_vkQueueEndDebugUtilsLabelEXT pfn_vkQueueEndDebugUtilsLabelEXT = {};
  928. PFN_vkCmdBeginDebugUtilsLabelEXT pfn_vkCmdBeginDebugUtilsLabelEXT = {};
  929. PFN_vkCmdEndDebugUtilsLabelEXT pfn_vkCmdEndDebugUtilsLabelEXT = {};
  930. PFN_vkCmdInsertDebugUtilsLabelEXT pfn_vkCmdInsertDebugUtilsLabelEXT = {};
  931. std::vector<size_t> device_indices;
  932. vk_device devices[GGML_VK_MAX_DEVICES];
  933. };
  934. static bool vk_instance_initialized = false;
  935. static vk_instance_t vk_instance;
  936. static bool vk_perf_logger_enabled = false;
  937. #ifdef GGML_VULKAN_CHECK_RESULTS
  938. static size_t vk_skip_checks;
  939. static size_t vk_output_tensor;
  940. static void ggml_vk_print_tensor(const ggml_tensor * tensor, const char * name);
  941. static void ggml_vk_check_results_0(ggml_backend_vk_context * ctx, ggml_cgraph * cgraph, int tensor_idx);
  942. static void ggml_vk_check_results_1(ggml_backend_vk_context * ctx, ggml_cgraph * cgraph, int tensor_idx);
  943. #endif
  944. 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);
  945. static void ggml_backend_vk_free(ggml_backend_t backend);
  946. // Wait for ctx->fence to be signaled.
  947. static void ggml_vk_wait_for_fence(ggml_backend_vk_context * ctx) {
  948. // Use waitForFences while most of the graph executes. Hopefully the CPU can sleep
  949. // during this wait.
  950. if (ctx->almost_ready_fence_pending) {
  951. VK_CHECK(ctx->device->device.waitForFences({ ctx->almost_ready_fence }, true, UINT64_MAX), "almost_ready_fence");
  952. ctx->device->device.resetFences({ ctx->almost_ready_fence });
  953. ctx->almost_ready_fence_pending = false;
  954. }
  955. // Spin (w/pause) waiting for the graph to finish executing.
  956. vk::Result result;
  957. while ((result = ctx->device->device.getFenceStatus(ctx->fence)) != vk::Result::eSuccess) {
  958. if (result != vk::Result::eNotReady) {
  959. fprintf(stderr, "ggml_vulkan: error %s at %s:%d\n", to_string(result).c_str(), __FILE__, __LINE__);
  960. exit(1);
  961. }
  962. for (uint32_t i = 0; i < 100; ++i) {
  963. YIELD();
  964. YIELD();
  965. YIELD();
  966. YIELD();
  967. YIELD();
  968. YIELD();
  969. YIELD();
  970. YIELD();
  971. YIELD();
  972. YIELD();
  973. }
  974. }
  975. ctx->device->device.resetFences({ ctx->fence });
  976. }
  977. // variables to track number of compiles in progress
  978. static uint32_t compile_count = 0;
  979. static std::mutex compile_count_mutex;
  980. static std::condition_variable compile_count_cond;
  981. 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,
  982. uint32_t parameter_count, std::array<uint32_t, 3> wg_denoms, std::vector<uint32_t> specialization_constants,
  983. bool disable_robustness, bool require_full_subgroups, uint32_t required_subgroup_size) {
  984. VK_LOG_DEBUG("ggml_vk_create_pipeline(" << device->name << ", " << pipeline->name << ", " << entrypoint << ", " << parameter_count <<
  985. ", (" << wg_denoms[0] << "," << wg_denoms[1] << "," << wg_denoms[2] << "), specialization_constants, " <<
  986. disable_robustness << ", " << require_full_subgroups << ", " << required_subgroup_size << ")");
  987. GGML_ASSERT(parameter_count > 0);
  988. GGML_ASSERT(parameter_count <= MAX_PARAMETER_COUNT);
  989. GGML_ASSERT(wg_denoms[0] > 0 && wg_denoms[1] > 0 && wg_denoms[2] > 0); // NOLINT
  990. vk::ShaderModuleCreateInfo shader_module_create_info({}, spv_size, reinterpret_cast<const uint32_t *>(spv_data));
  991. pipeline->shader_module = device->device.createShaderModule(shader_module_create_info);
  992. vk::PushConstantRange pcr(
  993. vk::ShaderStageFlagBits::eCompute,
  994. 0,
  995. pipeline->push_constant_size
  996. );
  997. vk::PipelineLayoutCreateInfo pipeline_layout_create_info(vk::PipelineLayoutCreateFlags(), device->dsl, pcr);
  998. pipeline->layout = device->device.createPipelineLayout(pipeline_layout_create_info);
  999. std::vector<vk::SpecializationMapEntry> specialization_entries(specialization_constants.size());
  1000. for (size_t i = 0; i < specialization_constants.size(); i++) {
  1001. specialization_entries[i].constantID = i;
  1002. specialization_entries[i].offset = i * sizeof(uint32_t);
  1003. specialization_entries[i].size = sizeof(uint32_t);
  1004. }
  1005. vk::SpecializationInfo specialization_info(
  1006. specialization_entries.size(),
  1007. specialization_entries.data(),
  1008. specialization_constants.size() * sizeof(uint32_t),
  1009. specialization_constants.data()
  1010. );
  1011. vk::PipelineShaderStageCreateFlags pipeline_shader_stage_create_flags{};
  1012. if (device->subgroup_require_full_support && require_full_subgroups) {
  1013. pipeline_shader_stage_create_flags |= vk::PipelineShaderStageCreateFlagBits::eRequireFullSubgroupsEXT;
  1014. }
  1015. vk::PipelineShaderStageCreateInfo pipeline_shader_create_info(
  1016. pipeline_shader_stage_create_flags,
  1017. vk::ShaderStageFlagBits::eCompute,
  1018. pipeline->shader_module,
  1019. entrypoint.c_str(),
  1020. &specialization_info);
  1021. vk::PipelineShaderStageRequiredSubgroupSizeCreateInfoEXT pipeline_shader_stage_required_subgroup_size_create_info;
  1022. pipeline_shader_stage_required_subgroup_size_create_info.requiredSubgroupSize = required_subgroup_size;
  1023. if (device->subgroup_size_control && required_subgroup_size > 0) {
  1024. GGML_ASSERT(device->subgroup_min_size <= required_subgroup_size && required_subgroup_size <= device->subgroup_max_size);
  1025. pipeline_shader_create_info.setPNext(&pipeline_shader_stage_required_subgroup_size_create_info);
  1026. }
  1027. vk::ComputePipelineCreateInfo compute_pipeline_create_info(
  1028. vk::PipelineCreateFlags{},
  1029. pipeline_shader_create_info,
  1030. pipeline->layout);
  1031. vk::PipelineRobustnessCreateInfoEXT rci;
  1032. if (device->pipeline_robustness && disable_robustness) {
  1033. rci.storageBuffers = vk::PipelineRobustnessBufferBehaviorEXT::eDisabled;
  1034. rci.uniformBuffers = vk::PipelineRobustnessBufferBehaviorEXT::eDisabled;
  1035. compute_pipeline_create_info.setPNext(&rci);
  1036. }
  1037. try {
  1038. pipeline->pipeline = device->device.createComputePipeline(VK_NULL_HANDLE, compute_pipeline_create_info).value;
  1039. } catch (const vk::SystemError& e) {
  1040. std::cerr << "ggml_vulkan: Compute pipeline creation failed for " << pipeline->name << std::endl;
  1041. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  1042. throw e;
  1043. }
  1044. pipeline->compiled = true;
  1045. if (vk_instance.debug_utils_support) {
  1046. vk::DebugUtilsObjectNameInfoEXT duoni;
  1047. duoni.objectType = vk::ObjectType::ePipeline;
  1048. duoni.pObjectName = pipeline->name.c_str();
  1049. duoni.objectHandle = reinterpret_cast<uint64_t>(static_cast<VkPipeline_T*>(pipeline->pipeline));
  1050. vk_instance.pfn_vkSetDebugUtilsObjectNameEXT(device->device, &static_cast<VkDebugUtilsObjectNameInfoEXT &>(duoni));
  1051. }
  1052. {
  1053. std::lock_guard<std::recursive_mutex> guard(device->mutex);
  1054. device->pipelines.insert({ pipeline->name, pipeline });
  1055. }
  1056. {
  1057. std::lock_guard<std::mutex> guard(compile_count_mutex);
  1058. assert(compile_count > 0);
  1059. compile_count--;
  1060. }
  1061. compile_count_cond.notify_all();
  1062. }
  1063. static void ggml_vk_destroy_pipeline(vk::Device& device, vk_pipeline& pipeline) {
  1064. VK_LOG_DEBUG("ggml_pipeline_destroy_pipeline(" << pipeline->name << ")");
  1065. device.destroyPipelineLayout(pipeline->layout);
  1066. device.destroyShaderModule(pipeline->shader_module);
  1067. device.destroyPipeline(pipeline->pipeline);
  1068. }
  1069. static void ggml_pipeline_request_descriptor_sets(ggml_backend_vk_context *ctx, vk_pipeline& pipeline, uint32_t n) {
  1070. VK_LOG_DEBUG("ggml_pipeline_request_descriptor_sets(" << pipeline->name << ", " << n << ")");
  1071. ctx->pipeline_descriptor_set_requirements += n;
  1072. if (!pipeline->compiled) {
  1073. pipeline->needed = true;
  1074. ctx->device->need_compiles = true;
  1075. }
  1076. }
  1077. static void ggml_pipeline_allocate_descriptor_sets(ggml_backend_vk_context * ctx) {
  1078. if (ctx->descriptor_sets.size() >= ctx->pipeline_descriptor_set_requirements) {
  1079. // Enough descriptors are available
  1080. return;
  1081. }
  1082. vk_device& device = ctx->device;
  1083. uint32_t to_alloc = ctx->pipeline_descriptor_set_requirements - ctx->descriptor_sets.size();
  1084. uint32_t pool_remaining = VK_DEVICE_DESCRIPTOR_POOL_SIZE - ctx->descriptor_sets.size() % VK_DEVICE_DESCRIPTOR_POOL_SIZE;
  1085. uint32_t pool_idx = ctx->descriptor_sets.size() / VK_DEVICE_DESCRIPTOR_POOL_SIZE;
  1086. while (to_alloc > 0) {
  1087. const uint32_t alloc_count = std::min(pool_remaining, to_alloc);
  1088. to_alloc -= alloc_count;
  1089. pool_remaining = VK_DEVICE_DESCRIPTOR_POOL_SIZE;
  1090. if (pool_idx >= ctx->descriptor_pools.size()) {
  1091. vk::DescriptorPoolSize descriptor_pool_size(vk::DescriptorType::eStorageBuffer, MAX_PARAMETER_COUNT * VK_DEVICE_DESCRIPTOR_POOL_SIZE);
  1092. vk::DescriptorPoolCreateInfo descriptor_pool_create_info({}, VK_DEVICE_DESCRIPTOR_POOL_SIZE, descriptor_pool_size);
  1093. ctx->descriptor_pools.push_back(device->device.createDescriptorPool(descriptor_pool_create_info));
  1094. }
  1095. std::vector<vk::DescriptorSetLayout> layouts(alloc_count);
  1096. for (uint32_t i = 0; i < alloc_count; i++) {
  1097. layouts[i] = device->dsl;
  1098. }
  1099. vk::DescriptorSetAllocateInfo descriptor_set_alloc_info(ctx->descriptor_pools[pool_idx], alloc_count, layouts.data());
  1100. std::vector<vk::DescriptorSet> sets = device->device.allocateDescriptorSets(descriptor_set_alloc_info);
  1101. ctx->descriptor_sets.insert(ctx->descriptor_sets.end(), sets.begin(), sets.end());
  1102. pool_idx++;
  1103. }
  1104. }
  1105. static vk::CommandBuffer ggml_vk_create_cmd_buffer(vk_device& device, vk_command_pool& p) {
  1106. VK_LOG_DEBUG("ggml_vk_create_cmd_buffer()");
  1107. if (p.cmd_buffers.size() > p.cmd_buffer_idx) {
  1108. // Reuse command buffer
  1109. return p.cmd_buffers[p.cmd_buffer_idx++];
  1110. }
  1111. vk::CommandBufferAllocateInfo command_buffer_alloc_info(
  1112. p.pool,
  1113. vk::CommandBufferLevel::ePrimary,
  1114. 1);
  1115. const std::vector<vk::CommandBuffer> cmd_buffers = device->device.allocateCommandBuffers(command_buffer_alloc_info);
  1116. auto buf = cmd_buffers.front();
  1117. p.cmd_buffers.push_back(buf);
  1118. p.cmd_buffer_idx++;
  1119. return buf;
  1120. }
  1121. static void ggml_vk_submit(vk_context& ctx, vk::Fence fence) {
  1122. if (ctx->seqs.empty()) {
  1123. if (fence) {
  1124. std::lock_guard<std::mutex> guard(queue_mutex);
  1125. ctx->p->q->queue.submit({}, fence);
  1126. }
  1127. return;
  1128. }
  1129. VK_LOG_DEBUG("ggml_vk_submit(" << ctx << ", " << fence << ")");
  1130. std::vector<std::vector<uint64_t>> tl_wait_vals;
  1131. std::vector<std::vector<uint64_t>> tl_signal_vals;
  1132. std::vector<std::vector<vk::Semaphore>> tl_wait_semaphores;
  1133. std::vector<std::vector<vk::Semaphore>> tl_signal_semaphores;
  1134. std::vector<vk::TimelineSemaphoreSubmitInfo> tl_submit_infos;
  1135. std::vector<vk::SubmitInfo> submit_infos;
  1136. int idx = -1;
  1137. std::vector<std::vector<vk::PipelineStageFlags>> stage_flags;
  1138. size_t reserve = 0;
  1139. for (const auto& sequence : ctx->seqs) {
  1140. reserve += sequence.size();
  1141. }
  1142. // Pre-reserve vectors to prevent reallocation, which invalidates pointers
  1143. tl_wait_semaphores.reserve(reserve);
  1144. tl_wait_vals.reserve(reserve);
  1145. tl_signal_semaphores.reserve(reserve);
  1146. tl_signal_vals.reserve(reserve);
  1147. tl_submit_infos.reserve(reserve);
  1148. submit_infos.reserve(reserve);
  1149. stage_flags.reserve(reserve);
  1150. for (const auto& sequence : ctx->seqs) {
  1151. for (const auto& submission : sequence) {
  1152. stage_flags.push_back({});
  1153. idx++;
  1154. tl_wait_vals.push_back({});
  1155. tl_wait_semaphores.push_back({});
  1156. tl_signal_vals.push_back({});
  1157. tl_signal_semaphores.push_back({});
  1158. for (size_t i = 0; i < submission.wait_semaphores.size(); i++) {
  1159. stage_flags[idx].push_back(ctx->p->q->stage_flags);
  1160. tl_wait_vals[idx].push_back(submission.wait_semaphores[i].value);
  1161. tl_wait_semaphores[idx].push_back(submission.wait_semaphores[i].s);
  1162. }
  1163. for (size_t i = 0; i < submission.signal_semaphores.size(); i++) {
  1164. tl_signal_vals[idx].push_back(submission.signal_semaphores[i].value);
  1165. tl_signal_semaphores[idx].push_back(submission.signal_semaphores[i].s);
  1166. }
  1167. tl_submit_infos.push_back({
  1168. (uint32_t) submission.wait_semaphores.size(),
  1169. tl_wait_vals[idx].data(),
  1170. (uint32_t) submission.signal_semaphores.size(),
  1171. tl_signal_vals[idx].data(),
  1172. });
  1173. tl_submit_infos[idx].sType = vk::StructureType::eTimelineSemaphoreSubmitInfo;
  1174. tl_submit_infos[idx].pNext = nullptr;
  1175. vk::SubmitInfo si{
  1176. (uint32_t) submission.wait_semaphores.size(),
  1177. tl_wait_semaphores[idx].data(),
  1178. stage_flags[idx].data(),
  1179. 1,
  1180. &submission.buffer,
  1181. (uint32_t) submission.signal_semaphores.size(),
  1182. tl_signal_semaphores[idx].data(),
  1183. };
  1184. si.setPNext(&tl_submit_infos[idx]);
  1185. submit_infos.push_back(si);
  1186. }
  1187. }
  1188. std::lock_guard<std::mutex> guard(queue_mutex);
  1189. ctx->p->q->queue.submit(submit_infos, fence);
  1190. ctx->seqs.clear();
  1191. }
  1192. 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) {
  1193. VK_LOG_DEBUG("ggml_vk_find_queue_family_index()");
  1194. const uint32_t qfsize = queue_family_props.size();
  1195. // Try with avoid preferences first
  1196. for (uint32_t i = 0; i < qfsize; i++) {
  1197. 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)) {
  1198. return i;
  1199. }
  1200. }
  1201. // Fall back to only required
  1202. for (size_t i = 0; i < qfsize; i++) {
  1203. if (queue_family_props[i].queueCount >= min_num_queues && (compute_index < 0 || i != (uint32_t) compute_index) && queue_family_props[i].queueFlags & required) {
  1204. return i;
  1205. }
  1206. }
  1207. // Fall back to reusing compute queue
  1208. for (size_t i = 0; i < qfsize; i++) {
  1209. if (queue_family_props[i].queueCount >= min_num_queues && queue_family_props[i].queueFlags & required) {
  1210. return i;
  1211. }
  1212. }
  1213. // Fall back to ignoring min_num_queries
  1214. for (size_t i = 0; i < qfsize; i++) {
  1215. if (queue_family_props[i].queueFlags & required) {
  1216. return i;
  1217. }
  1218. }
  1219. // 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.
  1220. // 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.
  1221. if (compute_index >= 0) {
  1222. return compute_index;
  1223. }
  1224. std::cerr << "ggml_vulkan: No suitable queue family index found." << std::endl;
  1225. for(auto &q_family : queue_family_props) {
  1226. std::cerr << "Queue number: " + std::to_string(q_family.queueCount) << " flags: " + to_string(q_family.queueFlags) << std::endl;
  1227. }
  1228. abort();
  1229. }
  1230. 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) {
  1231. VK_LOG_DEBUG("ggml_vk_create_queue()");
  1232. std::lock_guard<std::recursive_mutex> guard(device->mutex);
  1233. q.queue_family_index = queue_family_index;
  1234. q.transfer_only = transfer_only;
  1235. q.cmd_pool.init(device, &q);
  1236. q.queue = device->device.getQueue(queue_family_index, queue_index);
  1237. q.stage_flags = stage_flags;
  1238. }
  1239. static vk_context ggml_vk_create_context(ggml_backend_vk_context * ctx, vk_command_pool& p) {
  1240. vk_context result = std::make_shared<vk_context_struct>();
  1241. VK_LOG_DEBUG("ggml_vk_create_context(" << result << ")");
  1242. ctx->gc.contexts.emplace_back(result);
  1243. result->p = &p;
  1244. return result;
  1245. }
  1246. static vk_context ggml_vk_create_temporary_context(vk_command_pool& p) {
  1247. vk_context result = std::make_shared<vk_context_struct>();
  1248. VK_LOG_DEBUG("ggml_vk_create_temporary_context(" << result << ")");
  1249. result->p = &p;
  1250. return result;
  1251. }
  1252. static vk_semaphore * ggml_vk_create_binary_semaphore(ggml_backend_vk_context * ctx) {
  1253. VK_LOG_DEBUG("ggml_vk_create_timeline_semaphore()");
  1254. vk::SemaphoreTypeCreateInfo tci{ vk::SemaphoreType::eBinary, 0 };
  1255. vk::SemaphoreCreateInfo ci{};
  1256. ci.setPNext(&tci);
  1257. vk::Semaphore semaphore = ctx->device->device.createSemaphore(ci);
  1258. ctx->gc.semaphores.push_back({ semaphore, 0 });
  1259. return &ctx->gc.semaphores[ctx->gc.semaphores.size() - 1];
  1260. }
  1261. static vk_semaphore * ggml_vk_create_timeline_semaphore(ggml_backend_vk_context * ctx) {
  1262. VK_LOG_DEBUG("ggml_vk_create_timeline_semaphore()");
  1263. if (ctx->semaphore_idx >= ctx->gc.tl_semaphores.size()) {
  1264. vk::SemaphoreTypeCreateInfo tci{ vk::SemaphoreType::eTimeline, 0 };
  1265. vk::SemaphoreCreateInfo ci{};
  1266. ci.setPNext(&tci);
  1267. vk::Semaphore semaphore = ctx->device->device.createSemaphore(ci);
  1268. ctx->gc.tl_semaphores.push_back({ semaphore, 0 });
  1269. }
  1270. return &ctx->gc.tl_semaphores[ctx->semaphore_idx++];
  1271. }
  1272. static vk::Event ggml_vk_create_event(ggml_backend_vk_context * ctx) {
  1273. if (ctx->event_idx >= ctx->gc.events.size()) {
  1274. ctx->gc.events.push_back(ctx->device->device.createEvent({}));
  1275. }
  1276. return ctx->gc.events[ctx->event_idx++];
  1277. }
  1278. static void ggml_vk_command_pool_cleanup(vk_device& device, vk_command_pool& p) {
  1279. VK_LOG_DEBUG("ggml_vk_command_pool_cleanup()");
  1280. // Requires command buffers to be done
  1281. device->device.resetCommandPool(p.pool);
  1282. p.cmd_buffer_idx = 0;
  1283. }
  1284. static void ggml_vk_queue_command_pools_cleanup(vk_device& device) {
  1285. VK_LOG_DEBUG("ggml_vk_queue_command_pools_cleanup()");
  1286. // Arbitrary frequency to cleanup/reuse command buffers
  1287. static constexpr uint32_t cleanup_frequency = 10;
  1288. if (device->compute_queue.cmd_pool.cmd_buffer_idx >= cleanup_frequency) {
  1289. ggml_vk_command_pool_cleanup(device, device->compute_queue.cmd_pool);
  1290. }
  1291. if (device->transfer_queue.cmd_pool.cmd_buffer_idx >= cleanup_frequency) {
  1292. ggml_vk_command_pool_cleanup(device, device->transfer_queue.cmd_pool);
  1293. }
  1294. }
  1295. static uint32_t find_properties(const vk::PhysicalDeviceMemoryProperties* mem_props, vk::MemoryRequirements* mem_req, vk::MemoryPropertyFlags flags) {
  1296. for (uint32_t i = 0; i < mem_props->memoryTypeCount; ++i) {
  1297. vk::MemoryType memory_type = mem_props->memoryTypes[i];
  1298. if ((mem_req->memoryTypeBits & ((uint64_t)1 << i)) &&
  1299. (flags & memory_type.propertyFlags) == flags &&
  1300. mem_props->memoryHeaps[memory_type.heapIndex].size >= mem_req->size) {
  1301. return static_cast<int32_t>(i);
  1302. }
  1303. }
  1304. return UINT32_MAX;
  1305. }
  1306. static vk_buffer ggml_vk_create_buffer(vk_device& device, size_t size, vk::MemoryPropertyFlags req_flags, vk::MemoryPropertyFlags fallback_flags = vk::MemoryPropertyFlags(0)) {
  1307. VK_LOG_DEBUG("ggml_vk_create_buffer(" << device->name << ", " << size << ", " << to_string(req_flags) << ", " << to_string(fallback_flags) << ")");
  1308. if (size > device->max_memory_allocation_size) {
  1309. throw vk::OutOfDeviceMemoryError("Requested buffer size exceeds device memory allocation limit");
  1310. }
  1311. vk_buffer buf = std::make_shared<vk_buffer_struct>();
  1312. if (size == 0) {
  1313. buf->size = 0;
  1314. return buf;
  1315. }
  1316. vk::BufferCreateInfo buffer_create_info{
  1317. vk::BufferCreateFlags(),
  1318. size,
  1319. vk::BufferUsageFlagBits::eStorageBuffer | vk::BufferUsageFlagBits::eTransferSrc | vk::BufferUsageFlagBits::eTransferDst,
  1320. vk::SharingMode::eExclusive,
  1321. 0,
  1322. nullptr,
  1323. };
  1324. buf->buffer = device->device.createBuffer(buffer_create_info);
  1325. vk::MemoryRequirements mem_req = device->device.getBufferMemoryRequirements(buf->buffer);
  1326. vk::PhysicalDeviceMemoryProperties mem_props = device->physical_device.getMemoryProperties();
  1327. uint32_t memory_type_index = UINT32_MAX;
  1328. memory_type_index = find_properties(&mem_props, &mem_req, req_flags);
  1329. buf->memory_property_flags = req_flags;
  1330. if (memory_type_index == UINT32_MAX && fallback_flags) {
  1331. memory_type_index = find_properties(&mem_props, &mem_req, fallback_flags);
  1332. buf->memory_property_flags = fallback_flags;
  1333. }
  1334. if (memory_type_index == UINT32_MAX) {
  1335. device->device.destroyBuffer(buf->buffer);
  1336. throw vk::OutOfDeviceMemoryError("No suitable memory type found");
  1337. }
  1338. try {
  1339. buf->device_memory = device->device.allocateMemory({ mem_req.size, memory_type_index });
  1340. } catch (const vk::SystemError& e) {
  1341. if (buf->memory_property_flags != fallback_flags) {
  1342. // Try again with fallback flags
  1343. memory_type_index = find_properties(&mem_props, &mem_req, fallback_flags);
  1344. buf->memory_property_flags = fallback_flags;
  1345. try {
  1346. buf->device_memory = device->device.allocateMemory({ mem_req.size, memory_type_index });
  1347. }
  1348. catch (const vk::SystemError& e) {
  1349. device->device.destroyBuffer(buf->buffer);
  1350. throw e;
  1351. }
  1352. } else {
  1353. // Out of Host/Device memory, clean up buffer
  1354. device->device.destroyBuffer(buf->buffer);
  1355. throw e;
  1356. }
  1357. }
  1358. buf->ptr = nullptr;
  1359. if (buf->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  1360. buf->ptr = device->device.mapMemory(buf->device_memory, 0, VK_WHOLE_SIZE);
  1361. }
  1362. device->device.bindBufferMemory(buf->buffer, buf->device_memory, 0);
  1363. buf->device = device;
  1364. buf->size = size;
  1365. #ifdef GGML_VULKAN_MEMORY_DEBUG
  1366. device->memory_logger->log_allocation(buf, size);
  1367. #endif
  1368. return buf;
  1369. }
  1370. 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)) {
  1371. try {
  1372. return ggml_vk_create_buffer(device, size, req_flags, fallback_flags);
  1373. } catch (const vk::SystemError& e) {
  1374. std::cerr << "ggml_vulkan: Memory allocation of size " << size << " failed." << std::endl;
  1375. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  1376. throw e;
  1377. }
  1378. }
  1379. static vk_buffer ggml_vk_create_buffer_device(vk_device& device, size_t size) {
  1380. vk_buffer buf;
  1381. try {
  1382. if (device->prefer_host_memory) {
  1383. buf = ggml_vk_create_buffer(device, size, vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent, vk::MemoryPropertyFlagBits::eDeviceLocal);
  1384. } else if (device->uma) {
  1385. // Fall back to host memory type
  1386. buf = ggml_vk_create_buffer(device, size, vk::MemoryPropertyFlagBits::eDeviceLocal, vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
  1387. } else {
  1388. // use rebar if available, otherwise fallback to device only visible memory
  1389. buf = ggml_vk_create_buffer(device, size, vk::MemoryPropertyFlagBits::eDeviceLocal | vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent, vk::MemoryPropertyFlagBits::eDeviceLocal);
  1390. }
  1391. } catch (const vk::SystemError& e) {
  1392. std::cerr << "ggml_vulkan: Device memory allocation of size " << size << " failed." << std::endl;
  1393. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  1394. throw e;
  1395. }
  1396. return buf;
  1397. }
  1398. static void ggml_vk_destroy_buffer(vk_buffer& buf) {
  1399. if (buf == nullptr) {
  1400. return;
  1401. }
  1402. #ifdef GGML_VULKAN_MEMORY_DEBUG
  1403. if (buf->device != nullptr) {
  1404. buf->device->memory_logger->log_deallocation(buf);
  1405. }
  1406. #endif
  1407. buf.reset();
  1408. }
  1409. static vk_subbuffer ggml_vk_subbuffer(vk_buffer& buf) {
  1410. return { buf, 0, VK_WHOLE_SIZE };
  1411. }
  1412. static void ggml_vk_sync_buffers(vk_context& ctx) {
  1413. VK_LOG_DEBUG("ggml_vk_sync_buffers()");
  1414. const bool transfer_queue = ctx->p->q->transfer_only;
  1415. ctx->s->buffer.pipelineBarrier(
  1416. ctx->p->q->stage_flags,
  1417. ctx->p->q->stage_flags,
  1418. {},
  1419. { {
  1420. { !transfer_queue ? (vk::AccessFlagBits::eShaderRead | vk::AccessFlagBits::eShaderWrite | vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) : (vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) },
  1421. { !transfer_queue ? (vk::AccessFlagBits::eShaderRead | vk::AccessFlagBits::eShaderWrite | vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) : (vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) }
  1422. } },
  1423. {},
  1424. {}
  1425. );
  1426. }
  1427. static void ggml_vk_wait_events(vk_context& ctx, std::vector<vk::Event>&& events) {
  1428. VK_LOG_DEBUG("ggml_vk_wait_events()");
  1429. if (events.empty()) {
  1430. return;
  1431. }
  1432. ctx->s->buffer.waitEvents(
  1433. events,
  1434. ctx->p->q->stage_flags,
  1435. ctx->p->q->stage_flags,
  1436. {},
  1437. {},
  1438. {}
  1439. );
  1440. }
  1441. enum FaCodePath {
  1442. FA_SCALAR,
  1443. FA_COOPMAT1,
  1444. FA_COOPMAT2,
  1445. };
  1446. static FaHeadSizes fa_get_head_sizes(uint32_t hsk, uint32_t hsv) {
  1447. if (hsk != 192 && hsk != 576 && hsk != hsv) {
  1448. return FA_HEAD_SIZE_UNSUPPORTED;
  1449. }
  1450. switch (hsk) {
  1451. case 64: return FA_HEAD_SIZE_64;
  1452. case 80: return FA_HEAD_SIZE_80;
  1453. case 96: return FA_HEAD_SIZE_96;
  1454. case 112: return FA_HEAD_SIZE_112;
  1455. case 128: return FA_HEAD_SIZE_128;
  1456. case 192:
  1457. if (hsv == 192) {
  1458. return FA_HEAD_SIZE_192;
  1459. } else if (hsv == 128) {
  1460. return FA_HEAD_SIZE_192_128;
  1461. } else {
  1462. return FA_HEAD_SIZE_UNSUPPORTED;
  1463. }
  1464. case 256: return FA_HEAD_SIZE_256;
  1465. case 576:
  1466. if (hsv == 512) {
  1467. return FA_HEAD_SIZE_576_512;
  1468. } else {
  1469. return FA_HEAD_SIZE_UNSUPPORTED;
  1470. }
  1471. default: return FA_HEAD_SIZE_UNSUPPORTED;
  1472. }
  1473. }
  1474. // number of rows/cols for flash attention shader
  1475. static constexpr uint32_t flash_attention_num_small_rows = 32;
  1476. static constexpr uint32_t scalar_flash_attention_num_small_rows = 1;
  1477. static uint32_t get_fa_scalar_num_large_rows(uint32_t hsv) {
  1478. if (hsv >= 512) {
  1479. return 2;
  1480. } else {
  1481. return 8;
  1482. }
  1483. }
  1484. // The FA coopmat1 shader assumes 16x16x16 matrix multiply support.
  1485. // 128 threads split into four subgroups, each subgroup does 1/4
  1486. // of the Bc dimension.
  1487. static constexpr uint32_t coopmat1_flash_attention_num_large_rows = 16;
  1488. static constexpr uint32_t scalar_flash_attention_Bc = 64;
  1489. static constexpr uint32_t scalar_flash_attention_workgroup_size = 128;
  1490. static uint32_t get_fa_num_small_rows(FaCodePath path) {
  1491. if (path == FA_COOPMAT2) {
  1492. return flash_attention_num_small_rows;
  1493. } else {
  1494. return scalar_flash_attention_num_small_rows;
  1495. }
  1496. }
  1497. 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) {
  1498. GGML_UNUSED(clamp);
  1499. GGML_UNUSED(hsv);
  1500. if (path == FA_SCALAR) {
  1501. if (small_rows) {
  1502. return {scalar_flash_attention_num_small_rows, 64};
  1503. } else {
  1504. return {get_fa_scalar_num_large_rows(hsv), 32};
  1505. }
  1506. }
  1507. if (path == FA_COOPMAT1) {
  1508. if (small_rows) {
  1509. return {scalar_flash_attention_num_small_rows, scalar_flash_attention_Bc};
  1510. } else {
  1511. return {coopmat1_flash_attention_num_large_rows, scalar_flash_attention_Bc};
  1512. }
  1513. }
  1514. // small rows, large cols
  1515. if (small_rows) {
  1516. return {get_fa_num_small_rows(FA_COOPMAT2), 32};
  1517. }
  1518. // small cols to reduce register count
  1519. if (ggml_is_quantized(type) || hsk >= 256) {
  1520. if (hsk >= 512) {
  1521. return {32, 32};
  1522. } else {
  1523. return {64, 32};
  1524. }
  1525. }
  1526. return {64, 64};
  1527. }
  1528. 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) {
  1529. uint32_t lut_size = 0;
  1530. switch (src0_type) {
  1531. case GGML_TYPE_IQ1_S:
  1532. case GGML_TYPE_IQ1_M:
  1533. lut_size = 2*2048;
  1534. break;
  1535. case GGML_TYPE_IQ2_XXS:
  1536. lut_size = 8*256;
  1537. break;
  1538. case GGML_TYPE_IQ2_XS:
  1539. lut_size = 8*512;
  1540. break;
  1541. case GGML_TYPE_IQ2_S:
  1542. lut_size = 8*1024;
  1543. break;
  1544. case GGML_TYPE_IQ3_XXS:
  1545. lut_size = 4*256;
  1546. break;
  1547. case GGML_TYPE_IQ3_S:
  1548. lut_size = 4*512;
  1549. break;
  1550. case GGML_TYPE_IQ4_NL:
  1551. case GGML_TYPE_IQ4_XS:
  1552. lut_size = 4*16;
  1553. break;
  1554. default:
  1555. break;
  1556. }
  1557. // Needs to be kept up to date on shader changes
  1558. const uint32_t bank_conflict_offset = device->coopmat_support ? 8 : 1;
  1559. const uint32_t type_size = device->fp16 ? sizeof(ggml_fp16_t) : sizeof(float);
  1560. const uint32_t warps = warptile[0] / warptile[10];
  1561. const uint32_t load_bufs = (warptile[1] + warptile[2]) * (warptile[3] + bank_conflict_offset) * type_size;
  1562. const uint32_t mmid_row_ids = mul_mat_id ? (4096 * sizeof(uint32_t) + 4/*_ne1*/) : 0;
  1563. const uint32_t coopmat_stage = device->coopmat_support ? warptile[7] * warptile[8] / warps * sizeof(float) : 0;
  1564. const uint32_t total_size = load_bufs + mmid_row_ids + coopmat_stage + lut_size;
  1565. const bool supported = total_size <= device->properties.limits.maxComputeSharedMemorySize;
  1566. VK_LOG_DEBUG("ggml_vk_matmul_shmem_support(warptile=(" << warptile[0] << "," << warptile[1] << "," << warptile[2] << "), "
  1567. "mul_mat_id=" << mul_mat_id << ", src0_type=" << ggml_type_name(src0_type) << ", supported=" << supported);
  1568. return supported;
  1569. }
  1570. struct GpuPipelineConfig {
  1571. // GPU architecture identifier.
  1572. // Example: vk_device_architecture::AMD_GCN
  1573. vk_device_architecture arch;
  1574. // Mapping of pipeline names to their specific subgroup sizes.
  1575. // Example: {"soft_max_f32", 64}
  1576. std::unordered_map<std::string, uint32_t> pipelines;
  1577. // Default subgroup size for this GPU.
  1578. // Defaults to 0 if not explicitly provided.
  1579. uint32_t default_subgroup_size = 0;
  1580. };
  1581. // Pipeline configuration for RDNA1 GPUs.
  1582. static const std::unordered_map<std::string, uint32_t> rdna1_pipelines = {
  1583. {"soft_max", 64}, {"im2col", 64},
  1584. {"argmax", 64}, {"mul_mat_vec", 64},
  1585. {"mul_mat_vec_f16", 32}, {"mul_mat_vec_f32_f16", 32}
  1586. };
  1587. // Pipeline configuration for RDNA2 GPUs.
  1588. static const std::unordered_map<std::string, uint32_t> rdna2_pipelines = {
  1589. {"soft_max", 64}, {"im2col", 64},
  1590. };
  1591. static constexpr uint32_t RDNA_DEFAULT_SUBGROUP_SIZE = 32;
  1592. // Define configurations for different GPUs.
  1593. static std::vector<GpuPipelineConfig> gpu_pipeline_configs = {
  1594. {
  1595. vk_device_architecture::AMD_RDNA1,
  1596. {
  1597. rdna1_pipelines,
  1598. },
  1599. RDNA_DEFAULT_SUBGROUP_SIZE
  1600. },
  1601. {
  1602. vk_device_architecture::AMD_RDNA2,
  1603. {
  1604. rdna2_pipelines,
  1605. },
  1606. RDNA_DEFAULT_SUBGROUP_SIZE
  1607. },
  1608. };
  1609. static uint32_t get_subgroup_size(const std::string &pipeline_name, const vk_device_architecture &arch) {
  1610. for (const auto &config : gpu_pipeline_configs) {
  1611. if (config.arch == arch) {
  1612. auto pipIt = config.pipelines.find(pipeline_name);
  1613. if (pipIt != config.pipelines.end()) {
  1614. return pipIt->second;
  1615. }
  1616. std::vector<std::pair<std::string, uint32_t>> sorted_pipelines(config.pipelines.begin(), config.pipelines.end());
  1617. std::sort(sorted_pipelines.begin(), sorted_pipelines.end(),
  1618. [](const auto &a, const auto &b) { return a.first.size() > b.first.size(); });
  1619. for (const auto &entry : sorted_pipelines) {
  1620. if (pipeline_name.find(entry.first) != std::string::npos) {
  1621. return entry.second;
  1622. }
  1623. }
  1624. return config.default_subgroup_size;
  1625. }
  1626. }
  1627. return 0; // If no matching configuration is found
  1628. }
  1629. static void ggml_vk_load_shaders(vk_device& device) {
  1630. VK_LOG_DEBUG("ggml_vk_load_shaders(" << device->name << ")");
  1631. // some shaders have a minimum subgroup size
  1632. const uint32_t subgroup_size_8 = std::max(device->subgroup_size, 8u);
  1633. const uint32_t subgroup_size_16 = std::max(device->subgroup_size, 16u);
  1634. const uint32_t subgroup_size_32 = std::max(device->subgroup_size, 32u);
  1635. // mulmat
  1636. std::vector<uint32_t> l_warptile, m_warptile, s_warptile,
  1637. l_warptile_mmq, m_warptile_mmq, s_warptile_mmq,
  1638. l_warptile_mmq_int, m_warptile_mmq_int, s_warptile_mmq_int,
  1639. l_warptile_mmq_k, m_warptile_mmq_k, s_warptile_mmq_k,
  1640. l_warptile_mmqid, m_warptile_mmqid, s_warptile_mmqid;
  1641. std::array<uint32_t, 3> l_wg_denoms, m_wg_denoms, s_wg_denoms,
  1642. l_mmq_wg_denoms, m_mmq_wg_denoms, s_mmq_wg_denoms,
  1643. l_mmq_wg_denoms_k, m_mmq_wg_denoms_k, s_mmq_wg_denoms_k,
  1644. l_mmqid_wg_denoms, m_mmqid_wg_denoms, s_mmqid_wg_denoms;
  1645. uint32_t l_align, m_align, s_align;
  1646. if (device->coopmat2) {
  1647. // spec constants and tile sizes for non-quant matmul/matmul_id
  1648. l_warptile = { 256, 128, 256, 64, 1 };
  1649. m_warptile = { 256, 128, 128, 64, 0 };
  1650. s_warptile = { 128, 64, 64, 64, 0 };
  1651. l_wg_denoms = {128, 256, 1 };
  1652. m_wg_denoms = {128, 128, 1 };
  1653. s_wg_denoms = { 64, 64, 1 };
  1654. // spec constants and tile sizes for quant matmul (non-Qi_K)
  1655. l_warptile_mmq = { 256, 128, 256, 64, 1 };
  1656. m_warptile_mmq = { 256, 128, 128, 64, 1 };
  1657. s_warptile_mmq = { 256, 32, 64, 128, 0 };
  1658. l_mmq_wg_denoms = { 128, 256, 1 };
  1659. m_mmq_wg_denoms = { 128, 128, 1 };
  1660. s_mmq_wg_denoms = { 32, 64, 1 };
  1661. // spec constants and tile sizes for quant matmul (Qi_K)
  1662. l_warptile_mmq_k = { 256, 64, 128, 64, 1 };
  1663. m_warptile_mmq_k = { 256, 32, 64, 64, 0 };
  1664. s_warptile_mmq_k = { 256, 32, 32, 128, 0 };
  1665. l_mmq_wg_denoms_k = { 64, 128, 1 };
  1666. m_mmq_wg_denoms_k = { 32, 64, 1 };
  1667. s_mmq_wg_denoms_k = { 32, 32, 1 };
  1668. // spec constants and tile sizes for quant matmul_id
  1669. l_warptile_mmqid = { 256, 128, 128, 16, 0 };
  1670. m_warptile_mmqid = { 256, 128, 64, 16, 0 };
  1671. s_warptile_mmqid = { 256, 128, 64, 16, 0 };
  1672. l_mmqid_wg_denoms = { 128, 128, 1 };
  1673. m_mmqid_wg_denoms = { 128, 64, 1 };
  1674. s_mmqid_wg_denoms = { 128, 64, 1 };
  1675. l_align = 128;
  1676. m_align = 64;
  1677. s_align = 32;
  1678. } else {
  1679. // Matrix cores require different warp group sizes
  1680. const uint32_t tm_l = device->coopmat_support ? device->coopmat_m : 4;
  1681. const uint32_t tm_m = device->coopmat_support ? device->coopmat_m : 4;
  1682. const uint32_t tm_s = device->coopmat_support ? device->coopmat_m : 2;
  1683. const uint32_t tn_l = device->coopmat_support ? device->coopmat_n : 4;
  1684. const uint32_t tn_m = device->coopmat_support ? device->coopmat_n : 2;
  1685. const uint32_t tn_s = device->coopmat_support ? device->coopmat_n : 2;
  1686. const uint32_t tk_l = device->coopmat_support ? device->coopmat_k : 1;
  1687. const uint32_t tk_m = device->coopmat_support ? device->coopmat_k : 1;
  1688. const uint32_t tk_s = device->coopmat_support ? device->coopmat_k : 1;
  1689. l_warptile = { 128, 128, 128, 16, subgroup_size_8 * 2, 64, 2, tm_l, tn_l, tk_l, subgroup_size_8 };
  1690. m_warptile = { 128, 64, 64, 16, subgroup_size_8, 32, 2, tm_m, tn_m, tk_m, subgroup_size_8 };
  1691. s_warptile = { subgroup_size_16, 32, 32, 16, 32, 32, 2, tm_s, tn_s, tk_s, subgroup_size_8 };
  1692. l_warptile_mmq = { 128, 128, 128, 32, subgroup_size_8 * 2, 64, 2, tm_l, tn_l, tk_l, subgroup_size_8 };
  1693. m_warptile_mmq = { 128, 64, 64, 32, subgroup_size_8, 32, 2, tm_m, tn_m, tk_m, subgroup_size_8 };
  1694. s_warptile_mmq = { subgroup_size_32, 32, 32, 32, 32, 32, 2, tm_s, tn_s, tk_s, subgroup_size_8 };
  1695. l_warptile_mmq_int = { 128, 128, 128, 32, subgroup_size_8 * 2, 64, 2, 4, 4, 1, subgroup_size_8 };
  1696. m_warptile_mmq_int = { 128, 64, 64, 32, subgroup_size_8, 32, 2, 2, 2, 1, subgroup_size_8 };
  1697. s_warptile_mmq_int = { subgroup_size_32, 32, 32, 32, 32, 32, 2, 2, 1, 1, subgroup_size_8 };
  1698. // chip specific tuning
  1699. if ((device->architecture == AMD_GCN) && (device->driver_id != vk::DriverId::eAmdProprietary)) {
  1700. m_warptile_mmq = m_warptile_mmq_int = { 256, 64, 64, 32, 16, 16, 2, 2, 2, 1, 16 };
  1701. }
  1702. l_mmq_wg_denoms = l_wg_denoms = {128, 128, 1 };
  1703. m_mmq_wg_denoms = m_wg_denoms = { 64, 64, 1 };
  1704. s_mmq_wg_denoms = s_wg_denoms = { 32, 32, 1 };
  1705. l_align = 128;
  1706. m_align = 64;
  1707. s_align = 32;
  1708. for (uint32_t i = 0; i < GGML_TYPE_COUNT; ++i) {
  1709. ggml_type t = (ggml_type)i;
  1710. // Disable medium and large matrix multiplication if not enough shared memory is available
  1711. // Check mmq warptiles as the largest configuration
  1712. // Throw an error if not enough for any matrix multiplication is available
  1713. if (!ggml_vk_matmul_shmem_support(device, s_warptile_mmq, false, t)) {
  1714. std::cerr << "ggml_vulkan: Error: Shared memory size too small for matrix multiplication." << std::endl;
  1715. throw std::runtime_error("Shared memory size too small for matrix multiplication.");
  1716. } else if (!ggml_vk_matmul_shmem_support(device, m_warptile_mmq, false, t)) {
  1717. device->mul_mat_m[i] = false;
  1718. device->mul_mat_l[i] = false;
  1719. } else if (!ggml_vk_matmul_shmem_support(device, l_warptile_mmq, false, t)) {
  1720. device->mul_mat_l[i] = false;
  1721. }
  1722. // Disable mul_mat_id if not enough shared memory is available
  1723. if (!ggml_vk_matmul_shmem_support(device, s_warptile_mmq, true, t)) {
  1724. device->mul_mat_id_s[i] = false;
  1725. device->mul_mat_id_m[i] = false;
  1726. device->mul_mat_id_l[i] = false;
  1727. } else if (!ggml_vk_matmul_shmem_support(device, m_warptile_mmq, true, t)) {
  1728. device->mul_mat_id_m[i] = false;
  1729. device->mul_mat_id_l[i] = false;
  1730. } else if (!ggml_vk_matmul_shmem_support(device, l_warptile_mmq, true, t)) {
  1731. device->mul_mat_id_l[i] = false;
  1732. }
  1733. }
  1734. }
  1735. if (!device->pipeline_matmul_f32) {
  1736. device->pipeline_matmul_f32 = std::make_shared<vk_matmul_pipeline_struct>();
  1737. }
  1738. if (!device->pipeline_matmul_f32_f16) {
  1739. device->pipeline_matmul_f32_f16 = std::make_shared<vk_matmul_pipeline_struct>();
  1740. }
  1741. if (!device->pipeline_matmul_id_f32) {
  1742. device->pipeline_matmul_id_f32 = std::make_shared<vk_matmul_pipeline_struct>();
  1743. }
  1744. if (!device->pipeline_matmul_bf16) {
  1745. device->pipeline_matmul_bf16 = std::make_shared<vk_matmul_pipeline_struct>();
  1746. }
  1747. if (!device->pipeline_matmul_id_bf16) {
  1748. device->pipeline_matmul_id_bf16 = std::make_shared<vk_matmul_pipeline_struct>();
  1749. }
  1750. std::vector<std::future<void>> compiles;
  1751. 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,
  1752. uint32_t parameter_count, uint32_t push_constant_size, std::array<uint32_t, 3> wg_denoms, const std::vector<uint32_t>& specialization_constants,
  1753. uint32_t align, bool disable_robustness = false, bool require_full_subgroups = false, uint32_t required_subgroup_size = 0) {
  1754. if (!require_full_subgroups && required_subgroup_size == 0) {
  1755. required_subgroup_size = get_subgroup_size(name, device->architecture);
  1756. }
  1757. if (!pipeline) {
  1758. pipeline = std::make_shared<vk_pipeline_struct>();
  1759. pipeline->name = name;
  1760. pipeline->parameter_count = parameter_count;
  1761. pipeline->push_constant_size = push_constant_size;
  1762. pipeline->wg_denoms = wg_denoms;
  1763. pipeline->align = align;
  1764. }
  1765. if (!pipeline->needed || pipeline->compiled) {
  1766. return;
  1767. }
  1768. {
  1769. // wait until fewer than N compiles are in progress
  1770. uint32_t N = std::max(1u, std::thread::hardware_concurrency());
  1771. std::unique_lock<std::mutex> guard(compile_count_mutex);
  1772. while (compile_count >= N) {
  1773. compile_count_cond.wait(guard);
  1774. }
  1775. compile_count++;
  1776. }
  1777. compiles.push_back(std::async(ggml_vk_create_pipeline_func, std::ref(device), std::ref(pipeline), spv_size, spv_data, entrypoint,
  1778. parameter_count, wg_denoms, specialization_constants, disable_robustness, require_full_subgroups, required_subgroup_size));
  1779. };
  1780. 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> {
  1781. return {fa_rows_cols(path, hsk, hsv, clamp, type, small_rows)[0], 1, 1};
  1782. };
  1783. 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> {
  1784. // For large number of rows, 128 invocations seems to work best.
  1785. // For small number of rows (e.g. N==1), 256 works better. But matrix granularity for 256 is 32, so we
  1786. // can't use 256 for D==80.
  1787. // For scalar, use 128 (arbitrary)
  1788. // The same D_split value is used for both HSK and HSV, so just base it on the union of the LSBs.
  1789. const uint32_t D = (hsk|hsv);
  1790. uint32_t wg_size = (path == FA_SCALAR || path == FA_COOPMAT1)
  1791. ? scalar_flash_attention_workgroup_size
  1792. : ((small_rows && (D % 32) == 0) ? 256 : 128);
  1793. auto rows_cols = fa_rows_cols(path, hsk, hsv, clamp, type, small_rows);
  1794. // D_split can't be larger than a subgroup because we use subgroupShuffle to reduce it.
  1795. // D_split can't be larger than the LSB of D divided by 4 due to vectorization in the shader.
  1796. const uint32_t D_lsb = D ^ (D & (D-1));
  1797. uint32_t D_split = std::min(std::min(device->subgroup_size, 8u), D_lsb / 4);
  1798. // mask dim1 is padded to 64, we rely on this to avoid clamping mask loads
  1799. GGML_ASSERT((GGML_KQ_MASK_PAD % rows_cols[0]) == 0);
  1800. return {wg_size, rows_cols[0], rows_cols[1], hsk, hsv, clamp, D_split};
  1801. };
  1802. #define CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, HSK, HSV, HEAD_SIZES) \
  1803. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16 ## SUFFIX[TYPE][FA_HEAD_SIZE_##HEAD_SIZES][0][0][0], "flash_attn_f32_f16_" #HEAD_SIZES "_f16acc" #NAMELC #SUFFIX, flash_attn_f32_f16_ ## NAMELC ## _f16acc ## SUFFIX ## _len, flash_attn_f32_f16_ ## NAMELC ## _f16acc ## SUFFIX ## _data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(FAPATH, HSK,HSV,1,TYPE,false), fa_spec_constants(FAPATH, HSK,HSV,1,TYPE,false), 1, true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  1804. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16 ## SUFFIX[TYPE][FA_HEAD_SIZE_##HEAD_SIZES][0][0][1], "flash_attn_f32_f16_" #HEAD_SIZES "_aligned_f16acc" #NAMELC #SUFFIX, flash_attn_f32_f16_ ## NAMELC ## _f16acc ## SUFFIX ## _len, flash_attn_f32_f16_ ## NAMELC ## _f16acc ## SUFFIX ## _data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(FAPATH, HSK,HSV,0,TYPE,false), fa_spec_constants(FAPATH, HSK,HSV,0,TYPE,false), fa_rows_cols(FAPATH,HSK,HSV,0,TYPE,false)[1], true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  1805. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16 ## SUFFIX[TYPE][FA_HEAD_SIZE_##HEAD_SIZES][1][0][0], "flash_attn_f32_f16_" #HEAD_SIZES "_f32acc" #NAMELC #SUFFIX, flash_attn_f32_f16_ ## NAMELC ## SUFFIX ## _len, flash_attn_f32_f16_ ## NAMELC ## SUFFIX ## _data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(FAPATH, HSK,HSV,1,TYPE,false), fa_spec_constants(FAPATH, HSK,HSV,1,TYPE,false), 1, true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  1806. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16 ## SUFFIX[TYPE][FA_HEAD_SIZE_##HEAD_SIZES][1][0][1], "flash_attn_f32_f16_" #HEAD_SIZES "_aligned_f32acc" #NAMELC #SUFFIX, flash_attn_f32_f16_ ## NAMELC ## SUFFIX ## _len, flash_attn_f32_f16_ ## NAMELC ## SUFFIX ## _data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(FAPATH, HSK,HSV,0,TYPE,false), fa_spec_constants(FAPATH, HSK,HSV,0,TYPE,false), fa_rows_cols(FAPATH,HSK,HSV,0,TYPE,false)[1], true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  1807. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16 ## SUFFIX[TYPE][FA_HEAD_SIZE_##HEAD_SIZES][0][1][0], "flash_attn_f32_f16_" #HEAD_SIZES "_f16acc_smallrows" #NAMELC #SUFFIX, flash_attn_f32_f16_ ## NAMELC ## _f16acc ## SUFFIX ## _len, flash_attn_f32_f16_ ## NAMELC ## _f16acc ## SUFFIX ## _data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(FAPATH, HSK,HSV,1,TYPE,true), fa_spec_constants(FAPATH, HSK,HSV,1,TYPE,true), 1, true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  1808. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16 ## SUFFIX[TYPE][FA_HEAD_SIZE_##HEAD_SIZES][0][1][1], "flash_attn_f32_f16_" #HEAD_SIZES "_aligned_f16acc_smallrows" #NAMELC #SUFFIX, flash_attn_f32_f16_ ## NAMELC ## _f16acc ## SUFFIX ## _len, flash_attn_f32_f16_ ## NAMELC ## _f16acc ## SUFFIX ## _data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(FAPATH, HSK,HSV,0,TYPE,true), fa_spec_constants(FAPATH, HSK,HSV,0,TYPE,true), fa_rows_cols(FAPATH,HSK,HSV,0,TYPE,true)[1], true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  1809. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16 ## SUFFIX[TYPE][FA_HEAD_SIZE_##HEAD_SIZES][1][1][0], "flash_attn_f32_f16_" #HEAD_SIZES "_f32acc_smallrows" #NAMELC #SUFFIX, flash_attn_f32_f16_ ## NAMELC ## SUFFIX ## _len, flash_attn_f32_f16_ ## NAMELC ## SUFFIX ## _data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(FAPATH, HSK,HSV,1,TYPE,true), fa_spec_constants(FAPATH, HSK,HSV,1,TYPE,true), 1, true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  1810. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16 ## SUFFIX[TYPE][FA_HEAD_SIZE_##HEAD_SIZES][1][1][1], "flash_attn_f32_f16_" #HEAD_SIZES "_aligned_f32acc_smallrows" #NAMELC #SUFFIX, flash_attn_f32_f16_ ## NAMELC ## SUFFIX ## _len, flash_attn_f32_f16_ ## NAMELC ## SUFFIX ## _data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(FAPATH, HSK,HSV,0,TYPE,true), fa_spec_constants(FAPATH, HSK,HSV,0,TYPE,true), fa_rows_cols(FAPATH,HSK,HSV,0,TYPE,true)[1], true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  1811. #define CREATE_FA(TYPE, NAMELC, FAPATH, SUFFIX) \
  1812. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 64, 64, 64) \
  1813. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 80, 80, 80) \
  1814. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 96, 96, 96) \
  1815. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 112, 112, 112) \
  1816. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 128, 128, 128) \
  1817. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 192, 192, 192) \
  1818. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 192, 128, 192_128) \
  1819. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 256, 256, 256) \
  1820. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 576, 512, 576_512)
  1821. CREATE_FA(GGML_TYPE_F16, f16, FA_SCALAR, )
  1822. CREATE_FA(GGML_TYPE_Q4_0, q4_0, FA_SCALAR, )
  1823. CREATE_FA(GGML_TYPE_Q8_0, q8_0, FA_SCALAR, )
  1824. #if defined(VK_KHR_cooperative_matrix) && defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  1825. if (device->coopmat1_fa_support) {
  1826. CREATE_FA(GGML_TYPE_F16, f16, FA_COOPMAT1, _cm1)
  1827. CREATE_FA(GGML_TYPE_Q4_0, q4_0, FA_COOPMAT1, _cm1)
  1828. CREATE_FA(GGML_TYPE_Q8_0, q8_0, FA_COOPMAT1, _cm1)
  1829. }
  1830. #endif
  1831. #if defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  1832. if (device->coopmat2) {
  1833. CREATE_FA(GGML_TYPE_F16, f16, FA_COOPMAT2, _cm2)
  1834. CREATE_FA(GGML_TYPE_Q4_0, q4_0, FA_COOPMAT2, _cm2)
  1835. CREATE_FA(GGML_TYPE_Q4_1, q4_1, FA_COOPMAT2, _cm2)
  1836. CREATE_FA(GGML_TYPE_Q5_0, q5_0, FA_COOPMAT2, _cm2)
  1837. CREATE_FA(GGML_TYPE_Q5_1, q5_1, FA_COOPMAT2, _cm2)
  1838. CREATE_FA(GGML_TYPE_Q8_0, q8_0, FA_COOPMAT2, _cm2)
  1839. CREATE_FA(GGML_TYPE_IQ4_NL, iq4_nl, FA_COOPMAT2, _cm2)
  1840. }
  1841. #endif
  1842. #undef CREATE_FA2
  1843. #undef CREATE_FA
  1844. #if defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  1845. if (device->coopmat2) {
  1846. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  1847. #define CREATE_MM(PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  1848. 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); \
  1849. 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); \
  1850. 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); \
  1851. 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); \
  1852. 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); \
  1853. 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); \
  1854. // Create 2 variants, {f16,f32} accumulator
  1855. #define CREATE_MM2(PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  1856. CREATE_MM(PIPELINE_NAME . f16acc, NAMELC, _f16acc, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  1857. CREATE_MM(PIPELINE_NAME . f32acc, NAMELC, , WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  1858. CREATE_MM2(pipeline_matmul_f16, matmul_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 3)
  1859. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  1860. if (device->coopmat_bf16_support) {
  1861. CREATE_MM(pipeline_matmul_bf16, matmul_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3)
  1862. }
  1863. #endif
  1864. 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)
  1865. 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)
  1866. 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)
  1867. 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)
  1868. 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)
  1869. 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)
  1870. 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)
  1871. 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)
  1872. 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)
  1873. 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)
  1874. 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)
  1875. 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)
  1876. 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)
  1877. 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)
  1878. 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)
  1879. 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)
  1880. 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)
  1881. 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)
  1882. 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)
  1883. CREATE_MM2(pipeline_matmul_id_f16, matmul_id_f16, wg_denoms, warptile, vk_mat_mat_id_push_constants, 4)
  1884. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  1885. if (device->coopmat_bf16_support) {
  1886. CREATE_MM(pipeline_matmul_id_bf16, matmul_id_bf16, , wg_denoms, warptile, vk_mat_mat_id_push_constants, 4)
  1887. }
  1888. #endif
  1889. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0].f16acc, matmul_id_q4_0_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1890. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1].f16acc, matmul_id_q4_1_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1891. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0].f16acc, matmul_id_q5_0_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1892. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1].f16acc, matmul_id_q5_1_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1893. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0].f16acc, matmul_id_q8_0_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1894. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K].f16acc, matmul_id_q2_k_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1895. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K].f16acc, matmul_id_q3_k_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1896. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K].f16acc, matmul_id_q4_k_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1897. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K].f16acc, matmul_id_q5_k_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1898. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K].f16acc, matmul_id_q6_k_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1899. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S].f16acc, matmul_id_iq1_s_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1900. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M].f16acc, matmul_id_iq1_m_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1901. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS].f16acc, matmul_id_iq2_xxs_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1902. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS].f16acc, matmul_id_iq2_xs_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1903. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S].f16acc, matmul_id_iq2_s_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1904. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS].f16acc, matmul_id_iq3_xxs_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1905. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S].f16acc, matmul_id_iq3_s_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1906. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS].f16acc, matmul_id_iq4_xs_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1907. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL].f16acc, matmul_id_iq4_nl_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1908. #undef CREATE_MM
  1909. #undef CREATE_MM2
  1910. } else
  1911. #endif // defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  1912. #if defined(VK_KHR_cooperative_matrix) && defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  1913. if (device->coopmat_support) {
  1914. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  1915. #define CREATE_MM(TYPE, PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1916. if (device->mul_mat ## ID ## _l[TYPE]) \
  1917. 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); \
  1918. if (device->mul_mat ## ID ## _m[TYPE]) \
  1919. 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); \
  1920. if (device->mul_mat ## ID ## _s[TYPE]) \
  1921. 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); \
  1922. if (device->mul_mat ## ID ## _l[TYPE]) \
  1923. 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); \
  1924. if (device->mul_mat ## ID ## _m[TYPE]) \
  1925. 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); \
  1926. if (device->mul_mat ## ID ## _s[TYPE]) \
  1927. 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); \
  1928. // Create 2 variants, {f16,f32} accumulator
  1929. #define CREATE_MM2(TYPE, PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1930. if (device->coopmat_acc_f16_support) { \
  1931. CREATE_MM(TYPE, PIPELINE_NAME . f16acc, NAMELC, _f16acc, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1932. } \
  1933. if (device->coopmat_acc_f32_support) { \
  1934. CREATE_MM(TYPE, PIPELINE_NAME . f32acc, NAMELC, , WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1935. } \
  1936. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32, matmul_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1937. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32_f16, matmul_f32_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1938. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16, matmul_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1939. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16_f32, matmul_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1940. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  1941. if (device->coopmat_bf16_support) {
  1942. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_bf16, matmul_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, )
  1943. }
  1944. #endif
  1945. if (device->coopmat_acc_f16_support) {
  1946. 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, );
  1947. 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, );
  1948. 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, );
  1949. 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, );
  1950. 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, );
  1951. 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, );
  1952. 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, );
  1953. 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, );
  1954. 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, );
  1955. 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, );
  1956. 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, );
  1957. 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, );
  1958. 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, );
  1959. 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, );
  1960. 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, );
  1961. 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, );
  1962. 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, );
  1963. 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, );
  1964. 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, );
  1965. } else {
  1966. 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, );
  1967. 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, );
  1968. 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, );
  1969. 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, );
  1970. 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, );
  1971. 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, );
  1972. 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, );
  1973. 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, );
  1974. 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, );
  1975. 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, );
  1976. 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, );
  1977. 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, );
  1978. 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, );
  1979. 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, );
  1980. 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, );
  1981. 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, );
  1982. 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, );
  1983. 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, );
  1984. 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, );
  1985. }
  1986. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_id_f32, matmul_id_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1987. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16, matmul_id_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1988. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16_f32, matmul_id_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1989. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  1990. if (device->coopmat_bf16_support) {
  1991. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_id_bf16, matmul_id_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1992. }
  1993. #endif
  1994. if (device->coopmat_acc_f16_support) {
  1995. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0].f16acc, matmul_id_q4_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1996. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1].f16acc, matmul_id_q4_1_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1997. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0].f16acc, matmul_id_q5_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1998. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1].f16acc, matmul_id_q5_1_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1999. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0].f16acc, matmul_id_q8_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2000. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K].f16acc, matmul_id_q2_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2001. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K].f16acc, matmul_id_q3_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2002. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K].f16acc, matmul_id_q4_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2003. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K].f16acc, matmul_id_q5_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2004. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K].f16acc, matmul_id_q6_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2005. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S].f16acc, matmul_id_iq1_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2006. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M].f16acc, matmul_id_iq1_m_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2007. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS].f16acc, matmul_id_iq2_xxs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2008. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS].f16acc, matmul_id_iq2_xs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2009. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S].f16acc, matmul_id_iq2_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2010. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS].f16acc, matmul_id_iq3_xxs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2011. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S].f16acc, matmul_id_iq3_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2012. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS].f16acc, matmul_id_iq4_xs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2013. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL].f16acc, matmul_id_iq4_nl_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2014. } else {
  2015. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0].f16acc, matmul_id_q4_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2016. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1].f16acc, matmul_id_q4_1_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2017. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0].f16acc, matmul_id_q5_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2018. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1].f16acc, matmul_id_q5_1_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2019. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0].f16acc, matmul_id_q8_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2020. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K].f16acc, matmul_id_q2_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2021. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K].f16acc, matmul_id_q3_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2022. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K].f16acc, matmul_id_q4_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2023. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K].f16acc, matmul_id_q5_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2024. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K].f16acc, matmul_id_q6_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2025. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S].f16acc, matmul_id_iq1_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2026. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M].f16acc, matmul_id_iq1_m_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2027. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS].f16acc, matmul_id_iq2_xxs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2028. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS].f16acc, matmul_id_iq2_xs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2029. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S].f16acc, matmul_id_iq2_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2030. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS].f16acc, matmul_id_iq3_xxs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2031. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S].f16acc, matmul_id_iq3_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2032. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS].f16acc, matmul_id_iq4_xs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2033. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL].f16acc, matmul_id_iq4_nl_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2034. }
  2035. #undef CREATE_MM2
  2036. #undef CREATE_MM
  2037. } else
  2038. #endif // defined(VK_KHR_cooperative_matrix) && defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  2039. if (device->fp16) {
  2040. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  2041. #define CREATE_MM(TYPE, PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2042. if (device->mul_mat ## ID ## _l[TYPE]) \
  2043. 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); \
  2044. if (device->mul_mat ## ID ## _m[TYPE]) \
  2045. 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); \
  2046. if (device->mul_mat ## ID ## _s[TYPE]) \
  2047. 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); \
  2048. if (device->mul_mat ## ID ## _l[TYPE]) \
  2049. 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); \
  2050. if (device->mul_mat ## ID ## _m[TYPE]) \
  2051. 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); \
  2052. if (device->mul_mat ## ID ## _s[TYPE]) \
  2053. 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); \
  2054. #define CREATE_MMQ(TYPE, PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2055. if (device->mul_mat ## ID ## _l[TYPE]) { \
  2056. 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); \
  2057. 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); \
  2058. } \
  2059. if (device->mul_mat ## ID ## _m[TYPE]) { \
  2060. 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); \
  2061. 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); \
  2062. } \
  2063. if (device->mul_mat ## ID ## _s[TYPE]) { \
  2064. 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); \
  2065. 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); \
  2066. } \
  2067. // Create 2 variants, {f16,f32} accumulator
  2068. #define CREATE_MM2(TYPE, PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2069. CREATE_MM(TYPE, PIPELINE_NAME . f16acc, NAMELC, _f16acc, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2070. CREATE_MM(TYPE, PIPELINE_NAME . f32acc, NAMELC, , WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2071. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32, matmul_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2072. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32_f16, matmul_f32_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2073. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16, matmul_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2074. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16_f32, matmul_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2075. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_bf16, matmul_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2076. 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, );
  2077. 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, );
  2078. 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, );
  2079. 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, );
  2080. 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, );
  2081. 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, );
  2082. 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, );
  2083. 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, );
  2084. 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, );
  2085. 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, );
  2086. 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, );
  2087. 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, );
  2088. 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, );
  2089. 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, );
  2090. 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, );
  2091. 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, );
  2092. 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, );
  2093. 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, );
  2094. 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, );
  2095. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  2096. if (device->integer_dot_product) {
  2097. 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, );
  2098. 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, );
  2099. 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, );
  2100. 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, );
  2101. 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, );
  2102. }
  2103. #endif
  2104. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_id_f32, matmul_id_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2105. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16, matmul_id_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2106. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16_f32, matmul_id_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2107. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_id_bf16, matmul_id_bf16, , wg_denoms, warptile, vk_mat_mat_id_push_constants, 4, _id);
  2108. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0].f16acc, matmul_id_q4_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2109. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1].f16acc, matmul_id_q4_1_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2110. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0].f16acc, matmul_id_q5_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2111. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1].f16acc, matmul_id_q5_1_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2112. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0].f16acc, matmul_id_q8_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2113. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K].f16acc, matmul_id_q2_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2114. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K].f16acc, matmul_id_q3_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2115. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K].f16acc, matmul_id_q4_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2116. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K].f16acc, matmul_id_q5_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2117. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K].f16acc, matmul_id_q6_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2118. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S].f16acc, matmul_id_iq1_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2119. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M].f16acc, matmul_id_iq1_m_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2120. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS].f16acc, matmul_id_iq2_xxs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2121. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS].f16acc, matmul_id_iq2_xs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2122. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S].f16acc, matmul_id_iq2_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2123. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS].f16acc, matmul_id_iq3_xxs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2124. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S].f16acc, matmul_id_iq3_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2125. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS].f16acc, matmul_id_iq4_xs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2126. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL].f16acc, matmul_id_iq4_nl_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2127. #undef CREATE_MM2
  2128. #undef CREATE_MMQ
  2129. #undef CREATE_MM
  2130. } else {
  2131. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  2132. #define CREATE_MM(TYPE, PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2133. if (device->mul_mat ## ID ## _l[TYPE]) \
  2134. 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); \
  2135. if (device->mul_mat ## ID ## _m[TYPE]) \
  2136. 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); \
  2137. if (device->mul_mat ## ID ## _s[TYPE]) \
  2138. 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); \
  2139. if (device->mul_mat ## ID ## _l[TYPE]) \
  2140. 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); \
  2141. if (device->mul_mat ## ID ## _m[TYPE]) \
  2142. 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); \
  2143. if (device->mul_mat ## ID ## _s[TYPE]) \
  2144. 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); \
  2145. #define CREATE_MMQ(TYPE, PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2146. if (device->mul_mat ## ID ## _l[TYPE]) \
  2147. 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); \
  2148. if (device->mul_mat ## ID ## _m[TYPE]) \
  2149. 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); \
  2150. if (device->mul_mat ## ID ## _s[TYPE]) \
  2151. 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); \
  2152. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32, matmul_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2153. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32_f16, matmul_f32_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2154. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_f16.f32acc, matmul_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2155. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_f16_f32.f32acc, matmul_f16_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2156. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_bf16, matmul_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2157. 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, );
  2158. 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, );
  2159. 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, );
  2160. 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, );
  2161. 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, );
  2162. 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, );
  2163. 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, );
  2164. 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, );
  2165. 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, );
  2166. 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, );
  2167. 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, );
  2168. 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, );
  2169. 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, );
  2170. 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, );
  2171. 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, );
  2172. 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, );
  2173. 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, );
  2174. 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, );
  2175. 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, );
  2176. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  2177. if (device->integer_dot_product) {
  2178. 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, );
  2179. 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, );
  2180. 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, );
  2181. 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, );
  2182. 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, );
  2183. }
  2184. #endif
  2185. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_id_f32, matmul_id_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2186. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_id_f16.f32acc, matmul_id_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2187. 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);
  2188. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_id_bf16, matmul_id_bf16, , wg_denoms, warptile, vk_mat_mat_id_push_constants, 4, _id);
  2189. 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_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2190. 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_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2191. 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_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2192. 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_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2193. 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_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2194. 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_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2195. 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_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2196. 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_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2197. 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_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2198. 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_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2199. 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_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2200. 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_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2201. 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_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2202. 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_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2203. 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_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2204. 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_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2205. 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_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2206. 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_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2207. 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_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2208. }
  2209. // reusing CREATE_MM from the fp32 path
  2210. if ((device->coopmat2 || device->coopmat_support)
  2211. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  2212. && !device->coopmat_bf16_support
  2213. #endif
  2214. ) {
  2215. // use scalar tile sizes
  2216. l_warptile = { 128, 128, 128, 16, subgroup_size_8 * 2, 64, 2, 4, 4, 1, subgroup_size_8 };
  2217. m_warptile = { 128, 64, 64, 16, subgroup_size_8, 32, 2, 4, 2, 1, subgroup_size_8 };
  2218. s_warptile = { subgroup_size_16, 32, 32, 16, 32, 32, 2, 2, 2, 1, subgroup_size_8 };
  2219. l_wg_denoms = {128, 128, 1 };
  2220. m_wg_denoms = { 64, 64, 1 };
  2221. s_wg_denoms = { 32, 32, 1 };
  2222. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_bf16, matmul_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2223. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_id_bf16, matmul_id_bf16, , wg_denoms, warptile, vk_mat_mat_id_push_constants, 4, _id);
  2224. }
  2225. #undef CREATE_MM
  2226. // mul mat vec
  2227. // the number of rows computed per shader depends on GPU model and quant
  2228. uint32_t rm_stdq = 1;
  2229. uint32_t rm_kq = 2;
  2230. if (device->vendor_id == VK_VENDOR_ID_AMD) {
  2231. if (device->architecture == AMD_GCN) {
  2232. rm_stdq = 2;
  2233. rm_kq = 4;
  2234. }
  2235. } else if (device->vendor_id == VK_VENDOR_ID_INTEL)
  2236. rm_stdq = 2;
  2237. uint32_t rm_iq = 2 * rm_kq;
  2238. for (uint32_t i = 0; i < mul_mat_vec_max_cols; ++i) {
  2239. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_F32 ][i], "mul_mat_vec_f32_f32_f32_"+std::to_string(i+1), mul_mat_vec_f32_f32_f32_len, mul_mat_vec_f32_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {device->subgroup_size, 2, i+1}, 1);
  2240. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_F16 ][i], "mul_mat_vec_f16_f32_f32_"+std::to_string(i+1), mul_mat_vec_f16_f32_f32_len, mul_mat_vec_f16_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {device->subgroup_size, 2, i+1}, 1);
  2241. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_BF16][i], "mul_mat_vec_bf16_f32_f32_"+std::to_string(i+1), mul_mat_vec_bf16_f32_f32_len, mul_mat_vec_bf16_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {device->subgroup_size, 2, i+1}, 1);
  2242. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q4_0][i], "mul_mat_vec_q4_0_f32_f32_"+std::to_string(i+1), mul_mat_vec_q4_0_f32_f32_len, mul_mat_vec_q4_0_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  2243. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q4_1][i], "mul_mat_vec_q4_1_f32_f32_"+std::to_string(i+1), mul_mat_vec_q4_1_f32_f32_len, mul_mat_vec_q4_1_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  2244. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q5_0][i], "mul_mat_vec_q5_0_f32_f32_"+std::to_string(i+1), mul_mat_vec_q5_0_f32_f32_len, mul_mat_vec_q5_0_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  2245. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q5_1][i], "mul_mat_vec_q5_1_f32_f32_"+std::to_string(i+1), mul_mat_vec_q5_1_f32_f32_len, mul_mat_vec_q5_1_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  2246. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q8_0][i], "mul_mat_vec_q8_0_f32_f32_"+std::to_string(i+1), mul_mat_vec_q8_0_f32_f32_len, mul_mat_vec_q8_0_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1*rm_stdq, 1, 1}, {device->subgroup_size, 1*rm_stdq, i+1}, 1, true);
  2247. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q2_K][i], "mul_mat_vec_q2_k_f32_f32_"+std::to_string(i+1), mul_mat_vec_q2_k_f32_f32_len, mul_mat_vec_q2_k_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  2248. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q3_K][i], "mul_mat_vec_q3_k_f32_f32_"+std::to_string(i+1), mul_mat_vec_q3_k_f32_f32_len, mul_mat_vec_q3_k_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  2249. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q4_K][i], "mul_mat_vec_q4_k_f32_f32_"+std::to_string(i+1), mul_mat_vec_q4_k_f32_f32_len, mul_mat_vec_q4_k_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  2250. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q5_K][i], "mul_mat_vec_q5_k_f32_f32_"+std::to_string(i+1), mul_mat_vec_q5_k_f32_f32_len, mul_mat_vec_q5_k_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  2251. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q6_K][i], "mul_mat_vec_q6_k_f32_f32_"+std::to_string(i+1), mul_mat_vec_q6_k_f32_f32_len, mul_mat_vec_q6_k_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  2252. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ1_S][i], "mul_mat_vec_iq1_s_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq1_s_f32_f32_len, mul_mat_vec_iq1_s_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2253. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ1_M][i], "mul_mat_vec_iq1_m_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq1_m_f32_f32_len, mul_mat_vec_iq1_m_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2254. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ2_XXS][i], "mul_mat_vec_iq2_xxs_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq2_xxs_f32_f32_len, mul_mat_vec_iq2_xxs_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2255. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ2_XS][i], "mul_mat_vec_iq2_xs_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq2_xs_f32_f32_len, mul_mat_vec_iq2_xs_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2256. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ2_S][i], "mul_mat_vec_iq2_s_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq2_s_f32_f32_len, mul_mat_vec_iq2_s_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2257. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ3_XXS][i], "mul_mat_vec_iq3_xxs_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq3_xxs_f32_f32_len, mul_mat_vec_iq3_xxs_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2258. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ3_S][i], "mul_mat_vec_iq3_s_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq3_s_f32_f32_len, mul_mat_vec_iq3_s_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2259. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ4_XS][i], "mul_mat_vec_iq4_xs_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq4_xs_f32_f32_len, mul_mat_vec_iq4_xs_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2260. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ4_NL][i], "mul_mat_vec_iq4_nl_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq4_nl_f32_f32_len, mul_mat_vec_iq4_nl_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2261. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_F32 ][i], "mul_mat_vec_f32_f16_f32_"+std::to_string(i+1), mul_mat_vec_f32_f16_f32_len, mul_mat_vec_f32_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {device->subgroup_size, 2, i+1}, 1);
  2262. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_F16 ][i], "mul_mat_vec_f16_f16_f32_"+std::to_string(i+1), mul_mat_vec_f16_f16_f32_len, mul_mat_vec_f16_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {device->subgroup_size, 2, i+1}, 1);
  2263. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_BF16][i], "mul_mat_vec_bf16_f16_f32_"+std::to_string(i+1), mul_mat_vec_bf16_f16_f32_len, mul_mat_vec_bf16_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {device->subgroup_size, 2, i+1}, 1);
  2264. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q4_0][i], "mul_mat_vec_q4_0_f16_f32_"+std::to_string(i+1), mul_mat_vec_q4_0_f16_f32_len, mul_mat_vec_q4_0_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  2265. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q4_1][i], "mul_mat_vec_q4_1_f16_f32_"+std::to_string(i+1), mul_mat_vec_q4_1_f16_f32_len, mul_mat_vec_q4_1_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  2266. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q5_0][i], "mul_mat_vec_q5_0_f16_f32_"+std::to_string(i+1), mul_mat_vec_q5_0_f16_f32_len, mul_mat_vec_q5_0_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  2267. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q5_1][i], "mul_mat_vec_q5_1_f16_f32_"+std::to_string(i+1), mul_mat_vec_q5_1_f16_f32_len, mul_mat_vec_q5_1_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  2268. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q8_0][i], "mul_mat_vec_q8_0_f16_f32_"+std::to_string(i+1), mul_mat_vec_q8_0_f16_f32_len, mul_mat_vec_q8_0_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1*rm_stdq, 1, 1}, {device->subgroup_size, 1*rm_stdq, i+1}, 1, true);
  2269. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q2_K][i], "mul_mat_vec_q2_k_f16_f32_"+std::to_string(i+1), mul_mat_vec_q2_k_f16_f32_len, mul_mat_vec_q2_k_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  2270. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q3_K][i], "mul_mat_vec_q3_k_f16_f32_"+std::to_string(i+1), mul_mat_vec_q3_k_f16_f32_len, mul_mat_vec_q3_k_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  2271. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q4_K][i], "mul_mat_vec_q4_k_f16_f32_"+std::to_string(i+1), mul_mat_vec_q4_k_f16_f32_len, mul_mat_vec_q4_k_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  2272. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q5_K][i], "mul_mat_vec_q5_k_f16_f32_"+std::to_string(i+1), mul_mat_vec_q5_k_f16_f32_len, mul_mat_vec_q5_k_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  2273. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q6_K][i], "mul_mat_vec_q6_k_f16_f32_"+std::to_string(i+1), mul_mat_vec_q6_k_f16_f32_len, mul_mat_vec_q6_k_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  2274. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ1_S][i], "mul_mat_vec_iq1_s_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq1_s_f16_f32_len, mul_mat_vec_iq1_s_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2275. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ1_M][i], "mul_mat_vec_iq1_m_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq1_m_f16_f32_len, mul_mat_vec_iq1_m_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2276. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ2_XXS][i], "mul_mat_vec_iq2_xxs_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq2_xxs_f16_f32_len, mul_mat_vec_iq2_xxs_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2277. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ2_XS][i], "mul_mat_vec_iq2_xs_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq2_xs_f16_f32_len, mul_mat_vec_iq2_xs_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2278. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ2_S][i], "mul_mat_vec_iq2_s_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq2_s_f16_f32_len, mul_mat_vec_iq2_s_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2279. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ3_XXS][i], "mul_mat_vec_iq3_xxs_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq3_xxs_f16_f32_len, mul_mat_vec_iq3_xxs_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2280. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ3_S][i], "mul_mat_vec_iq3_s_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq3_s_f16_f32_len, mul_mat_vec_iq3_s_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2281. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ4_XS][i], "mul_mat_vec_iq4_xs_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq4_xs_f16_f32_len, mul_mat_vec_iq4_xs_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2282. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ4_NL][i], "mul_mat_vec_iq4_nl_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq4_nl_f16_f32_len, mul_mat_vec_iq4_nl_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2283. }
  2284. 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);
  2285. 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);
  2286. 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);
  2287. 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);
  2288. 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);
  2289. 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);
  2290. 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);
  2291. 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);
  2292. 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);
  2293. 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);
  2294. 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);
  2295. 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);
  2296. 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);
  2297. 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);
  2298. 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);
  2299. 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);
  2300. 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);
  2301. 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);
  2302. 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);
  2303. 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);
  2304. 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);
  2305. 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);
  2306. // dequant shaders
  2307. 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);
  2308. 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);
  2309. 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);
  2310. 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);
  2311. 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);
  2312. 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);
  2313. 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);
  2314. 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);
  2315. 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);
  2316. 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);
  2317. 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);
  2318. 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);
  2319. 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);
  2320. 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);
  2321. 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);
  2322. 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);
  2323. 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);
  2324. 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);
  2325. 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);
  2326. 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);
  2327. // get_rows
  2328. 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);
  2329. 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);
  2330. 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);
  2331. 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);
  2332. 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);
  2333. 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);
  2334. 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);
  2335. 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);
  2336. 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);
  2337. 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);
  2338. 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);
  2339. 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);
  2340. 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);
  2341. 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);
  2342. 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);
  2343. 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);
  2344. 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);
  2345. 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);
  2346. 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);
  2347. 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);
  2348. 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);
  2349. 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);
  2350. 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);
  2351. 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);
  2352. 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);
  2353. 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);
  2354. 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);
  2355. 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);
  2356. 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);
  2357. 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);
  2358. 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);
  2359. 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);
  2360. 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);
  2361. 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);
  2362. 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);
  2363. 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", 2, 4 * sizeof(uint32_t), {1, device->subgroup_size, 1}, {device->subgroup_size}, 1, true);
  2364. 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);
  2365. for (uint32_t i = 0; i < p021_max_gqa_ratio; ++i) {
  2366. if (device->subgroup_add && device->subgroup_require_full_support) {
  2367. 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);
  2368. } else {
  2369. 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);
  2370. }
  2371. }
  2372. 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, 9 * sizeof(uint32_t), {1, 1, 1}, {}, 1);
  2373. 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);
  2374. 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);
  2375. ggml_vk_create_pipeline(device, device->pipeline_rms_norm_f32, "rms_norm_f32", rms_norm_f32_len, rms_norm_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {0, 0}, 1);
  2376. ggml_vk_create_pipeline(device, device->pipeline_rms_norm_mul_f32, "rms_norm_mul_f32", rms_norm_f32_len, rms_norm_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {0, 1}, 1);
  2377. 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);
  2378. 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);
  2379. 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);
  2380. 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);
  2381. 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);
  2382. 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);
  2383. 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);
  2384. 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);
  2385. 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);
  2386. 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);
  2387. 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);
  2388. 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);
  2389. if (device->float_controls_rte_fp16) {
  2390. 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);
  2391. 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);
  2392. 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);
  2393. 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);
  2394. 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);
  2395. 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);
  2396. } else {
  2397. 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);
  2398. 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);
  2399. 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);
  2400. 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);
  2401. 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);
  2402. 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);
  2403. }
  2404. if (device->float_controls_rte_fp16) {
  2405. 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);
  2406. 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);
  2407. 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);
  2408. 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);
  2409. 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);
  2410. 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);
  2411. 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);
  2412. 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);
  2413. 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);
  2414. } else {
  2415. 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);
  2416. 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);
  2417. 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);
  2418. 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);
  2419. 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);
  2420. 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);
  2421. 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);
  2422. 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);
  2423. 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);
  2424. }
  2425. 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);
  2426. 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);
  2427. 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);
  2428. 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);
  2429. 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);
  2430. 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);
  2431. auto get_suffix = [](bool src0_f16, bool src1_f16, bool dst_f16) {
  2432. std::string s;
  2433. s += std::string(src0_f16 ? "_f16" : "_f32");
  2434. s += std::string(src1_f16 ? "_f16" : "_f32");
  2435. s += std::string(dst_f16 ? "_f16" : "_f32");
  2436. return s;
  2437. };
  2438. #define CREATE_BINARY(name, namemod, spec) \
  2439. for (int s0 : {0,1}) for (int s1 : {0,1}) for (int d : {0,1}) \
  2440. ggml_vk_create_pipeline(device, device->pipeline_ ## name ## namemod[s0][s1][d], \
  2441. #name + get_suffix(s0, s1, d) + #namemod, name ## _len[s0][s1][d], name ## _data[s0][s1][d], \
  2442. "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, spec, 1);
  2443. CREATE_BINARY(add, , {0})
  2444. CREATE_BINARY(add, _norepeat, {1})
  2445. CREATE_BINARY(sub, , {0})
  2446. CREATE_BINARY(sub, _norepeat, {1})
  2447. CREATE_BINARY(mul, , {0})
  2448. CREATE_BINARY(mul, _norepeat, {1})
  2449. CREATE_BINARY(div, , {0})
  2450. CREATE_BINARY(div, _norepeat, {1})
  2451. #undef CREATE_BINARY
  2452. 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);
  2453. 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);
  2454. 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);
  2455. 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);
  2456. 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);
  2457. 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);
  2458. 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);
  2459. 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);
  2460. 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);
  2461. 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);
  2462. 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);
  2463. 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);
  2464. 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);
  2465. 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);
  2466. 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);
  2467. 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);
  2468. #define CREATE_UNARY(name) \
  2469. 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); \
  2470. 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);
  2471. CREATE_UNARY(gelu)
  2472. CREATE_UNARY(gelu_erf)
  2473. CREATE_UNARY(gelu_quick)
  2474. CREATE_UNARY(silu)
  2475. CREATE_UNARY(relu)
  2476. CREATE_UNARY(tanh)
  2477. CREATE_UNARY(sigmoid)
  2478. #undef CREATE_UNARY
  2479. #define CREATE_GLU(name) \
  2480. 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); \
  2481. 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);
  2482. CREATE_GLU(geglu)
  2483. CREATE_GLU(reglu)
  2484. CREATE_GLU(swiglu)
  2485. CREATE_GLU(geglu_erf)
  2486. CREATE_GLU(geglu_quick)
  2487. #undef CREATE_GLU
  2488. 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);
  2489. 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);
  2490. 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);
  2491. ggml_vk_create_pipeline(device, device->pipeline_soft_max_f32, "soft_max_f32", soft_max_f32_len, soft_max_f32_data, "main", 3, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  2492. ggml_vk_create_pipeline(device, device->pipeline_soft_max_f32_wg512, "soft_max_f32_wg512", soft_max_f32_len, soft_max_f32_data, "main", 3, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, { 512 }, 1);
  2493. 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", 3, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  2494. 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", 3, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, { 512 }, 1);
  2495. 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);
  2496. 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);
  2497. 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);
  2498. 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);
  2499. 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);
  2500. if (device->float_controls_rte_fp16) {
  2501. 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);
  2502. 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);
  2503. 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);
  2504. 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);
  2505. } else {
  2506. 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);
  2507. 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);
  2508. 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);
  2509. 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);
  2510. }
  2511. ggml_vk_create_pipeline(device, device->pipeline_argsort_f32, "argsort_f32", argsort_f32_len, argsort_f32_data, "main", 2, sizeof(vk_op_argsort_push_constants), {1024, 1, 1}, {}, 1);
  2512. 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);
  2513. 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_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  2514. 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);
  2515. 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);
  2516. if (device->float_controls_rte_fp16) {
  2517. 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);
  2518. } else {
  2519. 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);
  2520. }
  2521. 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);
  2522. 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);
  2523. 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);
  2524. 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);
  2525. 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);
  2526. 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);
  2527. 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);
  2528. 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);
  2529. for (auto &c : compiles) {
  2530. c.wait();
  2531. }
  2532. device->need_compiles = false;
  2533. }
  2534. static bool ggml_vk_khr_cooperative_matrix_support(const vk::PhysicalDeviceProperties& props, const vk::PhysicalDeviceDriverProperties& driver_props, vk_device_architecture arch);
  2535. static vk_device ggml_vk_get_device(size_t idx) {
  2536. VK_LOG_DEBUG("ggml_vk_get_device(" << idx << ")");
  2537. if (vk_instance.devices[idx] == nullptr) {
  2538. VK_LOG_DEBUG("Initializing new vk_device");
  2539. vk_device device = std::make_shared<vk_device_struct>();
  2540. vk_instance.devices[idx] = device;
  2541. #ifdef GGML_VULKAN_MEMORY_DEBUG
  2542. device->memory_logger = std::unique_ptr<vk_memory_logger>(new vk_memory_logger());
  2543. #endif
  2544. if (vk_perf_logger_enabled) {
  2545. device->perf_logger = std::unique_ptr<vk_perf_logger>(new vk_perf_logger());
  2546. }
  2547. size_t dev_num = vk_instance.device_indices[idx];
  2548. std::vector<vk::PhysicalDevice> physical_devices = vk_instance.instance.enumeratePhysicalDevices();
  2549. if (dev_num >= physical_devices.size()) {
  2550. std::cerr << "ggml_vulkan: Device with index " << dev_num << " does not exist." << std::endl;
  2551. throw std::runtime_error("Device not found");
  2552. }
  2553. device->physical_device = physical_devices[dev_num];
  2554. const std::vector<vk::ExtensionProperties> ext_props = device->physical_device.enumerateDeviceExtensionProperties();
  2555. device->architecture = get_device_architecture(device->physical_device);
  2556. const char* GGML_VK_PREFER_HOST_MEMORY = getenv("GGML_VK_PREFER_HOST_MEMORY");
  2557. device->prefer_host_memory = GGML_VK_PREFER_HOST_MEMORY != nullptr;
  2558. bool fp16_storage = false;
  2559. bool fp16_compute = false;
  2560. bool maintenance4_support = false;
  2561. bool sm_builtins = false;
  2562. bool amd_shader_core_properties2 = false;
  2563. bool pipeline_robustness = false;
  2564. bool coopmat2_support = false;
  2565. device->coopmat_support = false;
  2566. device->integer_dot_product = false;
  2567. bool bfloat16_support = false;
  2568. for (const auto& properties : ext_props) {
  2569. if (strcmp("VK_KHR_maintenance4", properties.extensionName) == 0) {
  2570. maintenance4_support = true;
  2571. } else if (strcmp("VK_KHR_16bit_storage", properties.extensionName) == 0) {
  2572. fp16_storage = true;
  2573. } else if (strcmp("VK_KHR_shader_float16_int8", properties.extensionName) == 0) {
  2574. fp16_compute = true;
  2575. } else if (strcmp("VK_NV_shader_sm_builtins", properties.extensionName) == 0) {
  2576. sm_builtins = true;
  2577. } else if (strcmp("VK_AMD_shader_core_properties2", properties.extensionName) == 0) {
  2578. amd_shader_core_properties2 = true;
  2579. } else if (strcmp("VK_EXT_pipeline_robustness", properties.extensionName) == 0) {
  2580. pipeline_robustness = true;
  2581. } else if (strcmp("VK_EXT_subgroup_size_control", properties.extensionName) == 0) {
  2582. device->subgroup_size_control = true;
  2583. #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  2584. } else if (strcmp("VK_KHR_cooperative_matrix", properties.extensionName) == 0 &&
  2585. !getenv("GGML_VK_DISABLE_COOPMAT")) {
  2586. device->coopmat_support = true;
  2587. device->coopmat_m = 0;
  2588. device->coopmat_n = 0;
  2589. device->coopmat_k = 0;
  2590. #endif
  2591. #if defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  2592. } else if (strcmp("VK_NV_cooperative_matrix2", properties.extensionName) == 0 &&
  2593. !getenv("GGML_VK_DISABLE_COOPMAT2")) {
  2594. coopmat2_support = true;
  2595. #endif
  2596. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  2597. } else if (strcmp("VK_KHR_shader_integer_dot_product", properties.extensionName) == 0 &&
  2598. !getenv("GGML_VK_DISABLE_INTEGER_DOT_PRODUCT")) {
  2599. device->integer_dot_product = true;
  2600. #endif
  2601. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  2602. } else if (strcmp("VK_KHR_shader_bfloat16", properties.extensionName) == 0 &&
  2603. !getenv("GGML_VK_DISABLE_BFLOAT16")) {
  2604. bfloat16_support = true;
  2605. #endif
  2606. }
  2607. }
  2608. vk::PhysicalDeviceProperties2 props2;
  2609. vk::PhysicalDeviceMaintenance3Properties props3;
  2610. vk::PhysicalDeviceMaintenance4Properties props4;
  2611. vk::PhysicalDeviceSubgroupProperties subgroup_props;
  2612. vk::PhysicalDeviceDriverProperties driver_props;
  2613. vk::PhysicalDeviceShaderSMBuiltinsPropertiesNV sm_props;
  2614. vk::PhysicalDeviceShaderCoreProperties2AMD amd_shader_core_properties2_props;
  2615. vk::PhysicalDeviceVulkan11Properties vk11_props;
  2616. vk::PhysicalDeviceVulkan12Properties vk12_props;
  2617. vk::PhysicalDeviceSubgroupSizeControlPropertiesEXT subgroup_size_control_props;
  2618. vk::PhysicalDeviceShaderIntegerDotProductPropertiesKHR shader_integer_dot_product_props;
  2619. props2.pNext = &props3;
  2620. props3.pNext = &subgroup_props;
  2621. subgroup_props.pNext = &driver_props;
  2622. driver_props.pNext = &vk11_props;
  2623. vk11_props.pNext = &vk12_props;
  2624. VkBaseOutStructure * last_struct = (VkBaseOutStructure *)&vk12_props;
  2625. if (maintenance4_support) {
  2626. last_struct->pNext = (VkBaseOutStructure *)&props4;
  2627. last_struct = (VkBaseOutStructure *)&props4;
  2628. }
  2629. if (sm_builtins) {
  2630. last_struct->pNext = (VkBaseOutStructure *)&sm_props;
  2631. last_struct = (VkBaseOutStructure *)&sm_props;
  2632. }
  2633. if (amd_shader_core_properties2) {
  2634. last_struct->pNext = (VkBaseOutStructure *)&amd_shader_core_properties2_props;
  2635. last_struct = (VkBaseOutStructure *)&amd_shader_core_properties2_props;
  2636. }
  2637. if (device->subgroup_size_control) {
  2638. last_struct->pNext = (VkBaseOutStructure *)&subgroup_size_control_props;
  2639. last_struct = (VkBaseOutStructure *)&subgroup_size_control_props;
  2640. }
  2641. #if defined(VK_NV_cooperative_matrix2)
  2642. vk::PhysicalDeviceCooperativeMatrix2PropertiesNV coopmat2_props;
  2643. if (coopmat2_support) {
  2644. last_struct->pNext = (VkBaseOutStructure *)&coopmat2_props;
  2645. last_struct = (VkBaseOutStructure *)&coopmat2_props;
  2646. }
  2647. #endif
  2648. if (device->integer_dot_product) {
  2649. last_struct->pNext = (VkBaseOutStructure *)&shader_integer_dot_product_props;
  2650. last_struct = (VkBaseOutStructure *)&shader_integer_dot_product_props;
  2651. }
  2652. device->physical_device.getProperties2(&props2);
  2653. device->properties = props2.properties;
  2654. device->vendor_id = device->properties.vendorID;
  2655. device->driver_id = driver_props.driverID;
  2656. const char* GGML_VK_FORCE_MAX_ALLOCATION_SIZE = getenv("GGML_VK_FORCE_MAX_ALLOCATION_SIZE");
  2657. if (GGML_VK_FORCE_MAX_ALLOCATION_SIZE != nullptr) {
  2658. device->max_memory_allocation_size = std::stoul(GGML_VK_FORCE_MAX_ALLOCATION_SIZE);
  2659. } else if (maintenance4_support) {
  2660. device->max_memory_allocation_size = std::min(props3.maxMemoryAllocationSize, props4.maxBufferSize);
  2661. } else {
  2662. device->max_memory_allocation_size = props3.maxMemoryAllocationSize;
  2663. }
  2664. const char* GGML_VK_SUBALLOCATION_BLOCK_SIZE = getenv("GGML_VK_SUBALLOCATION_BLOCK_SIZE");
  2665. if (GGML_VK_SUBALLOCATION_BLOCK_SIZE != nullptr) {
  2666. device->suballocation_block_size = std::stoul(GGML_VK_SUBALLOCATION_BLOCK_SIZE);
  2667. } else {
  2668. // Limit batching of allocations to 1GB by default to avoid fragmentation issues
  2669. device->suballocation_block_size = 1024*1024*1024;
  2670. }
  2671. device->suballocation_block_size = std::min(device->suballocation_block_size, device->max_memory_allocation_size);
  2672. device->subgroup_size = subgroup_props.subgroupSize;
  2673. device->uma = device->properties.deviceType == vk::PhysicalDeviceType::eIntegratedGpu;
  2674. if (sm_builtins) {
  2675. device->shader_core_count = sm_props.shaderSMCount;
  2676. } else if (amd_shader_core_properties2) {
  2677. device->shader_core_count = amd_shader_core_properties2_props.activeComputeUnitCount;
  2678. } else {
  2679. device->shader_core_count = 0;
  2680. }
  2681. device->float_controls_rte_fp16 = vk12_props.shaderRoundingModeRTEFloat16;
  2682. device->subgroup_add = (vk11_props.subgroupSupportedStages & vk::ShaderStageFlagBits::eCompute) &&
  2683. (vk11_props.subgroupSupportedOperations & vk::SubgroupFeatureFlagBits::eArithmetic);
  2684. device->subgroup_shuffle = (vk11_props.subgroupSupportedStages & vk::ShaderStageFlagBits::eCompute) &&
  2685. (vk11_props.subgroupSupportedOperations & vk::SubgroupFeatureFlagBits::eShuffle);
  2686. const bool force_disable_f16 = getenv("GGML_VK_DISABLE_F16") != nullptr;
  2687. device->fp16 = !force_disable_f16 && fp16_storage && fp16_compute;
  2688. if (!ggml_vk_khr_cooperative_matrix_support(device->properties, driver_props, device->architecture)) {
  2689. device->coopmat_support = false;
  2690. }
  2691. device->integer_dot_product = device->integer_dot_product && shader_integer_dot_product_props.integerDotProduct4x8BitPackedSignedAccelerated;
  2692. std::vector<vk::QueueFamilyProperties> queue_family_props = device->physical_device.getQueueFamilyProperties();
  2693. // Try to find a non-graphics compute queue and transfer-focused queues
  2694. const uint32_t compute_queue_family_index = ggml_vk_find_queue_family_index(queue_family_props, vk::QueueFlagBits::eCompute, vk::QueueFlagBits::eGraphics, -1, 1);
  2695. 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);
  2696. const float priorities[] = { 1.0f, 1.0f };
  2697. device->single_queue = compute_queue_family_index == transfer_queue_family_index && queue_family_props[compute_queue_family_index].queueCount == 1;
  2698. std::vector<vk::DeviceQueueCreateInfo> device_queue_create_infos;
  2699. if (compute_queue_family_index != transfer_queue_family_index) {
  2700. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), compute_queue_family_index, 1, priorities});
  2701. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), transfer_queue_family_index, 1, priorities + 1});
  2702. } else if(!device->single_queue) {
  2703. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), compute_queue_family_index, 2, priorities});
  2704. } else {
  2705. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), compute_queue_family_index, 1, priorities});
  2706. }
  2707. vk::DeviceCreateInfo device_create_info;
  2708. std::vector<const char *> device_extensions;
  2709. vk::PhysicalDeviceFeatures device_features = device->physical_device.getFeatures();
  2710. VkPhysicalDeviceFeatures2 device_features2;
  2711. device_features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
  2712. device_features2.pNext = nullptr;
  2713. device_features2.features = (VkPhysicalDeviceFeatures)device_features;
  2714. VkPhysicalDeviceVulkan11Features vk11_features;
  2715. vk11_features.pNext = nullptr;
  2716. vk11_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES;
  2717. device_features2.pNext = &vk11_features;
  2718. VkPhysicalDeviceVulkan12Features vk12_features;
  2719. vk12_features.pNext = nullptr;
  2720. vk12_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES;
  2721. vk11_features.pNext = &vk12_features;
  2722. last_struct = (VkBaseOutStructure *)&vk12_features;
  2723. VkPhysicalDevicePipelineRobustnessFeaturesEXT pl_robustness_features;
  2724. pl_robustness_features.pNext = nullptr;
  2725. pl_robustness_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PIPELINE_ROBUSTNESS_FEATURES_EXT;
  2726. pl_robustness_features.pipelineRobustness = VK_FALSE;
  2727. if (pipeline_robustness) {
  2728. last_struct->pNext = (VkBaseOutStructure *)&pl_robustness_features;
  2729. last_struct = (VkBaseOutStructure *)&pl_robustness_features;
  2730. device_extensions.push_back("VK_EXT_pipeline_robustness");
  2731. }
  2732. VkPhysicalDeviceSubgroupSizeControlFeaturesEXT subgroup_size_control_features;
  2733. subgroup_size_control_features.pNext = nullptr;
  2734. subgroup_size_control_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_SIZE_CONTROL_FEATURES_EXT;
  2735. subgroup_size_control_features.computeFullSubgroups = false;
  2736. subgroup_size_control_features.subgroupSizeControl = false;
  2737. if (device->subgroup_size_control) {
  2738. last_struct->pNext = (VkBaseOutStructure *)&subgroup_size_control_features;
  2739. last_struct = (VkBaseOutStructure *)&subgroup_size_control_features;
  2740. }
  2741. #if defined(VK_KHR_cooperative_matrix)
  2742. VkPhysicalDeviceCooperativeMatrixFeaturesKHR coopmat_features;
  2743. coopmat_features.pNext = nullptr;
  2744. coopmat_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_FEATURES_KHR;
  2745. coopmat_features.cooperativeMatrix = VK_FALSE;
  2746. if (device->coopmat_support) {
  2747. last_struct->pNext = (VkBaseOutStructure *)&coopmat_features;
  2748. last_struct = (VkBaseOutStructure *)&coopmat_features;
  2749. }
  2750. #endif
  2751. #if defined(VK_NV_cooperative_matrix2)
  2752. VkPhysicalDeviceCooperativeMatrix2FeaturesNV coopmat2_features {};
  2753. coopmat2_features.pNext = nullptr;
  2754. coopmat2_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_2_FEATURES_NV;
  2755. if (coopmat2_support) {
  2756. last_struct->pNext = (VkBaseOutStructure *)&coopmat2_features;
  2757. last_struct = (VkBaseOutStructure *)&coopmat2_features;
  2758. device_extensions.push_back("VK_NV_cooperative_matrix2");
  2759. }
  2760. #endif
  2761. #if defined(VK_KHR_shader_bfloat16)
  2762. VkPhysicalDeviceShaderBfloat16FeaturesKHR bfloat16_features {};
  2763. bfloat16_features.pNext = nullptr;
  2764. bfloat16_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_BFLOAT16_FEATURES_KHR;
  2765. if (bfloat16_support) {
  2766. last_struct->pNext = (VkBaseOutStructure *)&bfloat16_features;
  2767. last_struct = (VkBaseOutStructure *)&bfloat16_features;
  2768. device_extensions.push_back("VK_KHR_shader_bfloat16");
  2769. }
  2770. #endif
  2771. VkPhysicalDeviceMaintenance4Features maint4_features {};
  2772. maint4_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_4_FEATURES;
  2773. if (maintenance4_support) {
  2774. last_struct->pNext = (VkBaseOutStructure *)&maint4_features;
  2775. last_struct = (VkBaseOutStructure *)&maint4_features;
  2776. device_extensions.push_back("VK_KHR_maintenance4");
  2777. }
  2778. VkPhysicalDeviceShaderIntegerDotProductFeaturesKHR shader_integer_dot_product_features {};
  2779. shader_integer_dot_product_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_INTEGER_DOT_PRODUCT_FEATURES_KHR;
  2780. if (device->integer_dot_product) {
  2781. last_struct->pNext = (VkBaseOutStructure *)&shader_integer_dot_product_features;
  2782. last_struct = (VkBaseOutStructure *)&shader_integer_dot_product_features;
  2783. device_extensions.push_back("VK_KHR_shader_integer_dot_product");
  2784. }
  2785. vkGetPhysicalDeviceFeatures2(device->physical_device, &device_features2);
  2786. device->fp16 = device->fp16 && vk12_features.shaderFloat16;
  2787. device->pipeline_robustness = pl_robustness_features.pipelineRobustness;
  2788. if (device->subgroup_size_control) {
  2789. device->subgroup_min_size = subgroup_size_control_props.minSubgroupSize;
  2790. device->subgroup_max_size = subgroup_size_control_props.maxSubgroupSize;
  2791. device_extensions.push_back("VK_EXT_subgroup_size_control");
  2792. }
  2793. device->subgroup_size_control = device->subgroup_size_control &&
  2794. (subgroup_size_control_props.requiredSubgroupSizeStages & vk::ShaderStageFlagBits::eCompute) &&
  2795. subgroup_size_control_features.subgroupSizeControl;
  2796. if (device->subgroup_size_control) {
  2797. device->subgroup_require_full_support = subgroup_size_control_features.computeFullSubgroups;
  2798. }
  2799. #if defined(VK_KHR_cooperative_matrix)
  2800. device->coopmat_support = device->coopmat_support && coopmat_features.cooperativeMatrix;
  2801. // coopmat1 fa shader currently assumes 32 invocations per subgroup
  2802. device->coopmat1_fa_support = device->coopmat_support && device->subgroup_require_full_support &&
  2803. device->subgroup_size_control && device->subgroup_min_size <= 32 &&
  2804. device->subgroup_max_size >= 32;
  2805. #endif
  2806. if (coopmat2_support) {
  2807. #if defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  2808. if (coopmat2_features.cooperativeMatrixWorkgroupScope &&
  2809. coopmat2_features.cooperativeMatrixFlexibleDimensions &&
  2810. coopmat2_features.cooperativeMatrixReductions &&
  2811. coopmat2_features.cooperativeMatrixConversions &&
  2812. coopmat2_features.cooperativeMatrixPerElementOperations &&
  2813. coopmat2_features.cooperativeMatrixTensorAddressing &&
  2814. coopmat2_features.cooperativeMatrixBlockLoads &&
  2815. vk12_features.bufferDeviceAddress) {
  2816. std::vector<VkCooperativeMatrixFlexibleDimensionsPropertiesNV> flexible_dimensions;
  2817. uint32_t count = 0;
  2818. PFN_vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV
  2819. _vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV =
  2820. (PFN_vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV)
  2821. vk_instance.instance.getProcAddr("vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV");
  2822. _vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV(device->physical_device, &count, nullptr);
  2823. VkCooperativeMatrixFlexibleDimensionsPropertiesNV empty_prop {};
  2824. empty_prop.sType = VK_STRUCTURE_TYPE_COOPERATIVE_MATRIX_FLEXIBLE_DIMENSIONS_PROPERTIES_NV;
  2825. flexible_dimensions.resize(count, empty_prop);
  2826. _vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV(device->physical_device, &count, flexible_dimensions.data());
  2827. bool found_fp16_128 = false,
  2828. found_fp16_256 = false,
  2829. found_fp32_128 = false,
  2830. found_fp32_256 = false;
  2831. // need to support fp16*fp16 with fp16/fp32 accumulator, for workgroupsize 128
  2832. // with 32x16x16 and 256 with 32x32x16.
  2833. for (auto &prop : flexible_dimensions) {
  2834. if (prop.saturatingAccumulation == VK_FALSE &&
  2835. prop.scope == VK_SCOPE_WORKGROUP_KHR &&
  2836. prop.AType == VK_COMPONENT_TYPE_FLOAT16_KHR &&
  2837. prop.BType == VK_COMPONENT_TYPE_FLOAT16_KHR) {
  2838. if (prop.workgroupInvocations == 128 &&
  2839. prop.MGranularity <= 32 &&
  2840. prop.NGranularity <= 16 &&
  2841. prop.KGranularity <= 16) {
  2842. if (prop.CType == VK_COMPONENT_TYPE_FLOAT16_KHR &&
  2843. prop.ResultType == VK_COMPONENT_TYPE_FLOAT16_KHR) {
  2844. found_fp16_128 = true;
  2845. }
  2846. if (prop.CType == VK_COMPONENT_TYPE_FLOAT32_KHR &&
  2847. prop.ResultType == VK_COMPONENT_TYPE_FLOAT32_KHR) {
  2848. found_fp32_128 = true;
  2849. }
  2850. }
  2851. if (prop.workgroupInvocations == 256 &&
  2852. prop.MGranularity <= 32 &&
  2853. prop.NGranularity <= 32 &&
  2854. prop.KGranularity <= 16) {
  2855. if (prop.CType == VK_COMPONENT_TYPE_FLOAT16_KHR &&
  2856. prop.ResultType == VK_COMPONENT_TYPE_FLOAT16_KHR) {
  2857. found_fp16_256 = true;
  2858. }
  2859. if (prop.CType == VK_COMPONENT_TYPE_FLOAT32_KHR &&
  2860. prop.ResultType == VK_COMPONENT_TYPE_FLOAT32_KHR) {
  2861. found_fp32_256 = true;
  2862. }
  2863. }
  2864. }
  2865. }
  2866. if (found_fp16_128 && found_fp16_256 &&
  2867. found_fp32_128 && found_fp32_256 &&
  2868. coopmat2_props.cooperativeMatrixFlexibleDimensionsMaxDimension >= 512) {
  2869. device->coopmat2 = true;
  2870. }
  2871. }
  2872. #endif
  2873. }
  2874. if (!vk11_features.storageBuffer16BitAccess) {
  2875. std::cerr << "ggml_vulkan: device " << GGML_VK_NAME << idx << " does not support 16-bit storage." << std::endl;
  2876. throw std::runtime_error("Unsupported device");
  2877. }
  2878. device_extensions.push_back("VK_KHR_16bit_storage");
  2879. #ifdef GGML_VULKAN_VALIDATE
  2880. device_extensions.push_back("VK_KHR_shader_non_semantic_info");
  2881. #endif
  2882. if (device->fp16) {
  2883. device_extensions.push_back("VK_KHR_shader_float16_int8");
  2884. }
  2885. #if defined(VK_KHR_cooperative_matrix)
  2886. if (device->coopmat_support) {
  2887. // Query supported shapes
  2888. std::vector<VkCooperativeMatrixPropertiesKHR> cm_props;
  2889. PFN_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR pfn_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR =
  2890. (PFN_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR)vkGetInstanceProcAddr(vk_instance.instance, "vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR");
  2891. uint32_t cm_props_num;
  2892. pfn_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR(device->physical_device, &cm_props_num, nullptr);
  2893. cm_props.resize(cm_props_num);
  2894. for (auto& prop : cm_props) {
  2895. prop.sType = VK_STRUCTURE_TYPE_COOPERATIVE_MATRIX_PROPERTIES_KHR;
  2896. }
  2897. pfn_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR(device->physical_device, &cm_props_num, cm_props.data());
  2898. VK_LOG_DEBUG("ggml_vulkan: Cooperative Matrix Shapes: " << cm_props.size());
  2899. for (auto& prop : cm_props) {
  2900. 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));
  2901. if ((vk::ComponentTypeKHR)prop.AType == vk::ComponentTypeKHR::eFloat16 &&
  2902. (vk::ComponentTypeKHR)prop.BType == vk::ComponentTypeKHR::eFloat16 &&
  2903. (vk::ScopeKHR)prop.scope == vk::ScopeKHR::eSubgroup
  2904. ) {
  2905. if ((vk::ComponentTypeKHR)prop.CType == vk::ComponentTypeKHR::eFloat32 &&
  2906. (vk::ComponentTypeKHR)prop.ResultType == vk::ComponentTypeKHR::eFloat32) {
  2907. // coopmat sizes not set yet
  2908. if (device->coopmat_m == 0) {
  2909. device->coopmat_acc_f32_support = true;
  2910. device->coopmat_m = prop.MSize;
  2911. device->coopmat_n = prop.NSize;
  2912. device->coopmat_k = prop.KSize;
  2913. } else if (device->coopmat_m == prop.MSize && device->coopmat_n == prop.NSize && device->coopmat_k == prop.KSize) {
  2914. // Only enable if shape is identical
  2915. device->coopmat_acc_f32_support = true;
  2916. }
  2917. if (prop.MSize == 16 && prop.NSize == 16 && prop.KSize == 16) {
  2918. device->coopmat_support_16x16x16_f32acc = true;
  2919. }
  2920. } else if ((vk::ComponentTypeKHR)prop.CType == vk::ComponentTypeKHR::eFloat16 &&
  2921. (vk::ComponentTypeKHR)prop.ResultType == vk::ComponentTypeKHR::eFloat16) {
  2922. // coopmat sizes not set yet
  2923. if (device->coopmat_m == 0) {
  2924. device->coopmat_acc_f16_support = true;
  2925. device->coopmat_m = prop.MSize;
  2926. device->coopmat_n = prop.NSize;
  2927. device->coopmat_k = prop.KSize;
  2928. } else if (device->coopmat_m == prop.MSize && device->coopmat_n == prop.NSize && device->coopmat_k == prop.KSize) {
  2929. // Only enable if shape is identical
  2930. device->coopmat_acc_f16_support = true;
  2931. }
  2932. if (prop.MSize == 16 && prop.NSize == 16 && prop.KSize == 16) {
  2933. device->coopmat_support_16x16x16_f16acc = true;
  2934. }
  2935. }
  2936. } else if ((vk::ComponentTypeKHR)prop.AType == vk::ComponentTypeKHR::eSint8 &&
  2937. (vk::ComponentTypeKHR)prop.BType == vk::ComponentTypeKHR::eSint8 &&
  2938. (vk::ComponentTypeKHR)prop.CType == vk::ComponentTypeKHR::eSint32 &&
  2939. (vk::ComponentTypeKHR)prop.ResultType == vk::ComponentTypeKHR::eSint32 &&
  2940. (vk::ScopeKHR)prop.scope == vk::ScopeKHR::eSubgroup &&
  2941. device->coopmat_int_m == 0
  2942. ) {
  2943. device->coopmat_int_support = true;
  2944. device->coopmat_int_m = prop.MSize;
  2945. device->coopmat_int_n = prop.NSize;
  2946. device->coopmat_int_k = prop.KSize;
  2947. }
  2948. #if defined(VK_KHR_shader_bfloat16) && defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  2949. if (prop.AType == VK_COMPONENT_TYPE_BFLOAT16_KHR &&
  2950. prop.BType == VK_COMPONENT_TYPE_BFLOAT16_KHR &&
  2951. prop.CType == VK_COMPONENT_TYPE_FLOAT32_KHR &&
  2952. prop.ResultType == VK_COMPONENT_TYPE_FLOAT32_KHR &&
  2953. (vk::ScopeKHR)prop.scope == vk::ScopeKHR::eSubgroup
  2954. ) {
  2955. // coopmat sizes not set yet
  2956. if (device->coopmat_m == 0) {
  2957. device->coopmat_bf16_support = true;
  2958. device->coopmat_m = prop.MSize;
  2959. device->coopmat_n = prop.NSize;
  2960. device->coopmat_k = prop.KSize;
  2961. } else if (device->coopmat_m == prop.MSize && device->coopmat_n == prop.NSize && device->coopmat_k == prop.KSize) {
  2962. // Only enable if shape is identical
  2963. device->coopmat_bf16_support = true;
  2964. }
  2965. }
  2966. #endif
  2967. }
  2968. if (device->coopmat_m == 0 || !device->coopmat_acc_f32_support) {
  2969. // No suitable matmul mode found
  2970. GGML_LOG_DEBUG("ggml_vulkan: WARNING: No suitable matrix core mode found. Disabling matrix cores.\n");
  2971. device->coopmat_support = false;
  2972. }
  2973. if (getenv("GGML_VK_DISABLE_BFLOAT16")) {
  2974. device->coopmat_bf16_support = false;
  2975. }
  2976. }
  2977. if (device->coopmat_support) {
  2978. device_extensions.push_back("VK_KHR_cooperative_matrix");
  2979. }
  2980. #if defined(VK_KHR_shader_bfloat16)
  2981. if (device->coopmat_bf16_support) {
  2982. device_extensions.push_back("VK_KHR_shader_bfloat16");
  2983. }
  2984. #endif
  2985. #endif
  2986. device->name = GGML_VK_NAME + std::to_string(idx);
  2987. device_create_info = {
  2988. vk::DeviceCreateFlags(),
  2989. device_queue_create_infos,
  2990. {},
  2991. device_extensions
  2992. };
  2993. device_create_info.setPNext(&device_features2);
  2994. device->device = device->physical_device.createDevice(device_create_info);
  2995. // Queues
  2996. ggml_vk_create_queue(device, device->compute_queue, compute_queue_family_index, 0, { vk::PipelineStageFlagBits::eComputeShader | vk::PipelineStageFlagBits::eTransfer }, false);
  2997. // Shaders
  2998. // Disable matmul tile sizes early if performance low or not supported
  2999. for (uint32_t i = 0; i < GGML_TYPE_COUNT; ++i) {
  3000. switch (device->vendor_id) {
  3001. #ifndef GGML_VULKAN_RUN_TESTS
  3002. case VK_VENDOR_ID_AMD:
  3003. case VK_VENDOR_ID_INTEL:
  3004. device->mul_mat_l[i] = false;
  3005. device->mul_mat_m[i] = true;
  3006. device->mul_mat_s[i] = true;
  3007. device->mul_mat_id_l[i] = false;
  3008. device->mul_mat_id_m[i] = true;
  3009. device->mul_mat_id_s[i] = true;
  3010. break;
  3011. case VK_VENDOR_ID_APPLE:
  3012. device->mul_mat_l[i] = false;
  3013. device->mul_mat_m[i] = true;
  3014. device->mul_mat_s[i] = false;
  3015. device->mul_mat_id_l[i] = false;
  3016. device->mul_mat_id_m[i] = true;
  3017. device->mul_mat_id_s[i] = false;
  3018. break;
  3019. #endif
  3020. default:
  3021. device->mul_mat_l[i] = true;
  3022. device->mul_mat_m[i] = true;
  3023. device->mul_mat_s[i] = true;
  3024. device->mul_mat_id_l[i] = true;
  3025. device->mul_mat_id_m[i] = true;
  3026. device->mul_mat_id_s[i] = true;
  3027. break;
  3028. }
  3029. }
  3030. std::vector<vk::DescriptorSetLayoutBinding> dsl_binding;
  3031. std::vector<vk::DescriptorBindingFlags> dsl_binding_flags;
  3032. for (uint32_t i = 0; i < MAX_PARAMETER_COUNT; i++) {
  3033. dsl_binding.push_back({i, vk::DescriptorType::eStorageBuffer, 1, vk::ShaderStageFlagBits::eCompute});
  3034. dsl_binding_flags.push_back({});
  3035. }
  3036. vk::DescriptorSetLayoutBindingFlagsCreateInfo dslbfci = { dsl_binding_flags };
  3037. vk::DescriptorSetLayoutCreateInfo descriptor_set_layout_create_info(
  3038. {},
  3039. dsl_binding);
  3040. descriptor_set_layout_create_info.setPNext(&dslbfci);
  3041. device->dsl = device->device.createDescriptorSetLayout(descriptor_set_layout_create_info);
  3042. ggml_vk_load_shaders(device);
  3043. if (!device->single_queue) {
  3044. const uint32_t transfer_queue_index = compute_queue_family_index == transfer_queue_family_index ? 1 : 0;
  3045. ggml_vk_create_queue(device, device->transfer_queue, transfer_queue_family_index, transfer_queue_index, { vk::PipelineStageFlagBits::eTransfer }, true);
  3046. } else {
  3047. // TODO: Use pointer or reference to avoid copy
  3048. device->transfer_queue.copyFrom(device->compute_queue);
  3049. device->transfer_queue.cmd_pool.init(device, &device->transfer_queue);
  3050. }
  3051. device->buffer_type = {
  3052. /* .iface = */ ggml_backend_vk_buffer_type_interface,
  3053. /* .device = */ ggml_backend_reg_dev_get(ggml_backend_vk_reg(), idx),
  3054. /* .context = */ new ggml_backend_vk_buffer_type_context{ device->name, device },
  3055. };
  3056. device->fence = device->device.createFence({});
  3057. device->idx = idx;
  3058. device->disable_fusion = getenv("GGML_VK_DISABLE_FUSION") != nullptr;
  3059. return device;
  3060. }
  3061. return vk_instance.devices[idx];
  3062. }
  3063. static void ggml_vk_print_gpu_info(size_t idx) {
  3064. GGML_ASSERT(idx < vk_instance.device_indices.size());
  3065. size_t dev_num = vk_instance.device_indices[idx];
  3066. VK_LOG_DEBUG("ggml_vk_print_gpu_info(" << dev_num << ")");
  3067. GGML_ASSERT(vk_instance_initialized);
  3068. std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
  3069. if (dev_num >= devices.size()) {
  3070. std::cerr << "ggml_vulkan: Device with index " << dev_num << " does not exist." << std::endl;
  3071. throw std::runtime_error("Device not found");
  3072. }
  3073. vk::PhysicalDevice physical_device = devices[dev_num];
  3074. std::vector<vk::ExtensionProperties> ext_props = physical_device.enumerateDeviceExtensionProperties();
  3075. bool fp16_storage = false;
  3076. bool fp16_compute = false;
  3077. bool coopmat_support = false;
  3078. bool coopmat2_support = false;
  3079. bool integer_dot_product = false;
  3080. for (auto properties : ext_props) {
  3081. if (strcmp("VK_KHR_16bit_storage", properties.extensionName) == 0) {
  3082. fp16_storage = true;
  3083. } else if (strcmp("VK_KHR_shader_float16_int8", properties.extensionName) == 0) {
  3084. fp16_compute = true;
  3085. #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  3086. } else if (strcmp("VK_KHR_cooperative_matrix", properties.extensionName) == 0 &&
  3087. !getenv("GGML_VK_DISABLE_COOPMAT")) {
  3088. coopmat_support = true;
  3089. #endif
  3090. #if defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  3091. } else if (strcmp("VK_NV_cooperative_matrix2", properties.extensionName) == 0 &&
  3092. !getenv("GGML_VK_DISABLE_COOPMAT2")) {
  3093. coopmat2_support = true;
  3094. #endif
  3095. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  3096. } else if (strcmp("VK_KHR_shader_integer_dot_product", properties.extensionName) == 0 &&
  3097. !getenv("GGML_VK_DISABLE_INTEGER_DOT_PRODUCT")) {
  3098. integer_dot_product = true;
  3099. #endif
  3100. }
  3101. }
  3102. const vk_device_architecture device_architecture = get_device_architecture(physical_device);
  3103. const char* GGML_VK_DISABLE_F16 = getenv("GGML_VK_DISABLE_F16");
  3104. bool force_disable_f16 = GGML_VK_DISABLE_F16 != nullptr;
  3105. bool fp16 = !force_disable_f16 && fp16_storage && fp16_compute;
  3106. vk::PhysicalDeviceProperties2 props2;
  3107. vk::PhysicalDeviceMaintenance3Properties props3;
  3108. vk::PhysicalDeviceSubgroupProperties subgroup_props;
  3109. vk::PhysicalDeviceDriverProperties driver_props;
  3110. vk::PhysicalDeviceShaderIntegerDotProductPropertiesKHR shader_integer_dot_product_props;
  3111. props2.pNext = &props3;
  3112. props3.pNext = &subgroup_props;
  3113. subgroup_props.pNext = &driver_props;
  3114. // Pointer to the last chain element
  3115. VkBaseOutStructure * last_struct = (VkBaseOutStructure *)&driver_props;
  3116. if (integer_dot_product) {
  3117. last_struct->pNext = (VkBaseOutStructure *)&shader_integer_dot_product_props;
  3118. last_struct = (VkBaseOutStructure *)&shader_integer_dot_product_props;
  3119. }
  3120. physical_device.getProperties2(&props2);
  3121. VkPhysicalDeviceFeatures2 device_features2;
  3122. device_features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
  3123. device_features2.pNext = nullptr;
  3124. VkPhysicalDeviceVulkan11Features vk11_features;
  3125. vk11_features.pNext = nullptr;
  3126. vk11_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES;
  3127. device_features2.pNext = &vk11_features;
  3128. VkPhysicalDeviceVulkan12Features vk12_features;
  3129. vk12_features.pNext = nullptr;
  3130. vk12_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES;
  3131. vk11_features.pNext = &vk12_features;
  3132. // Pointer to the last chain element
  3133. last_struct = (VkBaseOutStructure *)&vk12_features;
  3134. #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  3135. VkPhysicalDeviceCooperativeMatrixFeaturesKHR coopmat_features;
  3136. coopmat_features.pNext = nullptr;
  3137. coopmat_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_FEATURES_KHR;
  3138. coopmat_features.cooperativeMatrix = VK_FALSE;
  3139. if (coopmat_support) {
  3140. last_struct->pNext = (VkBaseOutStructure *)&coopmat_features;
  3141. last_struct = (VkBaseOutStructure *)&coopmat_features;
  3142. }
  3143. #endif
  3144. VkPhysicalDeviceShaderIntegerDotProductFeaturesKHR shader_integer_dot_product_features {};
  3145. shader_integer_dot_product_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_INTEGER_DOT_PRODUCT_FEATURES_KHR;
  3146. if (integer_dot_product) {
  3147. last_struct->pNext = (VkBaseOutStructure *)&shader_integer_dot_product_features;
  3148. last_struct = (VkBaseOutStructure *)&shader_integer_dot_product_features;
  3149. }
  3150. vkGetPhysicalDeviceFeatures2(physical_device, &device_features2);
  3151. fp16 = fp16 && vk12_features.shaderFloat16;
  3152. uint32_t default_subgroup_size = get_subgroup_size("", device_architecture);
  3153. const size_t subgroup_size = (default_subgroup_size != 0) ? default_subgroup_size : subgroup_props.subgroupSize;
  3154. const bool uma = props2.properties.deviceType == vk::PhysicalDeviceType::eIntegratedGpu;
  3155. integer_dot_product = integer_dot_product
  3156. && shader_integer_dot_product_props.integerDotProduct4x8BitPackedSignedAccelerated
  3157. && shader_integer_dot_product_features.shaderIntegerDotProduct;
  3158. coopmat_support = coopmat_support
  3159. #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  3160. && coopmat_features.cooperativeMatrix
  3161. #endif
  3162. && ggml_vk_khr_cooperative_matrix_support(props2.properties, driver_props, device_architecture);
  3163. std::string matrix_cores = coopmat2_support ? "NV_coopmat2" : coopmat_support ? "KHR_coopmat" : "none";
  3164. std::string device_name = props2.properties.deviceName.data();
  3165. GGML_LOG_DEBUG("ggml_vulkan: %zu = %s (%s) | uma: %d | fp16: %d | warp size: %zu | shared memory: %d | int dot: %d | matrix cores: %s\n",
  3166. idx, device_name.c_str(), driver_props.driverName.data(), uma, fp16, subgroup_size,
  3167. props2.properties.limits.maxComputeSharedMemorySize, integer_dot_product, matrix_cores.c_str());
  3168. if (props2.properties.deviceType == vk::PhysicalDeviceType::eCpu) {
  3169. GGML_LOG_DEBUG("ggml_vulkan: Warning: Device type is CPU. This is probably not the device you want.\n");
  3170. }
  3171. }
  3172. static bool ggml_vk_instance_validation_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions);
  3173. static bool ggml_vk_instance_portability_enumeration_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions);
  3174. static bool ggml_vk_instance_debug_utils_ext_available(const std::vector<vk::ExtensionProperties> & instance_extensions);
  3175. static void ggml_vk_instance_init() {
  3176. if (vk_instance_initialized) {
  3177. return;
  3178. }
  3179. VK_LOG_DEBUG("ggml_vk_instance_init()");
  3180. uint32_t api_version = vk::enumerateInstanceVersion();
  3181. if (api_version < VK_API_VERSION_1_2) {
  3182. std::cerr << "ggml_vulkan: Error: Vulkan 1.2 required." << std::endl;
  3183. GGML_ABORT("fatal error");
  3184. }
  3185. vk::ApplicationInfo app_info{ "ggml-vulkan", 1, nullptr, 0, api_version };
  3186. const std::vector<vk::ExtensionProperties> instance_extensions = vk::enumerateInstanceExtensionProperties();
  3187. const bool validation_ext = ggml_vk_instance_validation_ext_available(instance_extensions);
  3188. #ifdef __APPLE__
  3189. const bool portability_enumeration_ext = ggml_vk_instance_portability_enumeration_ext_available(instance_extensions);
  3190. #endif
  3191. const bool debug_utils_ext = ggml_vk_instance_debug_utils_ext_available(instance_extensions) && getenv("GGML_VK_DEBUG_MARKERS") != nullptr;
  3192. std::vector<const char*> layers;
  3193. if (validation_ext) {
  3194. layers.push_back("VK_LAYER_KHRONOS_validation");
  3195. }
  3196. std::vector<const char*> extensions;
  3197. if (validation_ext) {
  3198. extensions.push_back("VK_EXT_validation_features");
  3199. }
  3200. #ifdef __APPLE__
  3201. if (portability_enumeration_ext) {
  3202. extensions.push_back("VK_KHR_portability_enumeration");
  3203. }
  3204. #endif
  3205. if (debug_utils_ext) {
  3206. extensions.push_back("VK_EXT_debug_utils");
  3207. }
  3208. vk::InstanceCreateInfo instance_create_info(vk::InstanceCreateFlags{}, &app_info, layers, extensions);
  3209. #ifdef __APPLE__
  3210. if (portability_enumeration_ext) {
  3211. instance_create_info.flags |= vk::InstanceCreateFlagBits::eEnumeratePortabilityKHR;
  3212. }
  3213. #endif
  3214. std::vector<vk::ValidationFeatureEnableEXT> features_enable;
  3215. vk::ValidationFeaturesEXT validation_features;
  3216. if (validation_ext) {
  3217. features_enable = { vk::ValidationFeatureEnableEXT::eBestPractices };
  3218. validation_features = {
  3219. features_enable,
  3220. {},
  3221. };
  3222. validation_features.setPNext(nullptr);
  3223. instance_create_info.setPNext(&validation_features);
  3224. GGML_LOG_DEBUG("ggml_vulkan: Validation layers enabled\n");
  3225. }
  3226. vk_instance.instance = vk::createInstance(instance_create_info);
  3227. vk_instance_initialized = true;
  3228. if (debug_utils_ext) {
  3229. vk_instance.debug_utils_support = true;
  3230. vk_instance.pfn_vkSetDebugUtilsObjectNameEXT = (PFN_vkSetDebugUtilsObjectNameEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkSetDebugUtilsObjectNameEXT");
  3231. vk_instance.pfn_vkQueueBeginDebugUtilsLabelEXT = (PFN_vkQueueBeginDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkQueueBeginDebugUtilsLabelEXT");
  3232. vk_instance.pfn_vkQueueEndDebugUtilsLabelEXT = (PFN_vkQueueEndDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkQueueEndDebugUtilsLabelEXT");
  3233. vk_instance.pfn_vkCmdBeginDebugUtilsLabelEXT = (PFN_vkCmdBeginDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkCmdBeginDebugUtilsLabelEXT");
  3234. vk_instance.pfn_vkCmdEndDebugUtilsLabelEXT = (PFN_vkCmdEndDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkCmdEndDebugUtilsLabelEXT");
  3235. vk_instance.pfn_vkCmdInsertDebugUtilsLabelEXT = (PFN_vkCmdInsertDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkCmdInsertDebugUtilsLabelEXT");
  3236. }
  3237. vk_perf_logger_enabled = getenv("GGML_VK_PERF_LOGGER") != nullptr;
  3238. // Emulate behavior of CUDA_VISIBLE_DEVICES for Vulkan
  3239. char * devices_env = getenv("GGML_VK_VISIBLE_DEVICES");
  3240. if (devices_env != nullptr) {
  3241. size_t num_available_devices = vk_instance.instance.enumeratePhysicalDevices().size();
  3242. std::string devices(devices_env);
  3243. std::replace(devices.begin(), devices.end(), ',', ' ');
  3244. std::stringstream ss(devices);
  3245. size_t tmp;
  3246. while (ss >> tmp) {
  3247. if(tmp >= num_available_devices) {
  3248. std::cerr << "ggml_vulkan: Invalid device index " << tmp << " in GGML_VK_VISIBLE_DEVICES." << std::endl;
  3249. throw std::runtime_error("Invalid Vulkan device index");
  3250. }
  3251. vk_instance.device_indices.push_back(tmp);
  3252. }
  3253. } else {
  3254. std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
  3255. // If no vulkan devices are found, return early
  3256. if (devices.empty()) {
  3257. GGML_LOG_INFO("ggml_vulkan: No devices found.\n");
  3258. return;
  3259. }
  3260. // Default to using all dedicated GPUs
  3261. for (size_t i = 0; i < devices.size(); i++) {
  3262. vk::PhysicalDeviceProperties2 new_props;
  3263. vk::PhysicalDeviceDriverProperties new_driver;
  3264. vk::PhysicalDeviceIDProperties new_id;
  3265. new_props.pNext = &new_driver;
  3266. new_driver.pNext = &new_id;
  3267. devices[i].getProperties2(&new_props);
  3268. if (new_props.properties.deviceType == vk::PhysicalDeviceType::eDiscreteGpu) {
  3269. // Check if there are two physical devices corresponding to the same GPU
  3270. auto old_device = std::find_if(
  3271. vk_instance.device_indices.begin(),
  3272. vk_instance.device_indices.end(),
  3273. [&devices, &new_id](const size_t k){
  3274. vk::PhysicalDeviceProperties2 old_props;
  3275. vk::PhysicalDeviceIDProperties old_id;
  3276. old_props.pNext = &old_id;
  3277. devices[k].getProperties2(&old_props);
  3278. return std::equal(std::begin(old_id.deviceUUID), std::end(old_id.deviceUUID), std::begin(new_id.deviceUUID));
  3279. }
  3280. );
  3281. if (old_device == vk_instance.device_indices.end()) {
  3282. vk_instance.device_indices.push_back(i);
  3283. } else {
  3284. // There can be two physical devices corresponding to the same GPU if there are 2 different drivers
  3285. // This can cause error when splitting layers aross the devices, need to keep only 1
  3286. VK_LOG_DEBUG("Device " << i << " and device " << *old_device << " have the same deviceUUID");
  3287. vk::PhysicalDeviceProperties2 old_props;
  3288. vk::PhysicalDeviceDriverProperties old_driver;
  3289. old_props.pNext = &old_driver;
  3290. devices[*old_device].getProperties2(&old_props);
  3291. std::map<vk::DriverId, int> driver_priorities {};
  3292. int old_priority = std::numeric_limits<int>::max();
  3293. int new_priority = std::numeric_limits<int>::max();
  3294. // Check https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VkDriverId.html for the list of driver id
  3295. // Smaller number -> higher priority
  3296. switch (old_props.properties.vendorID) {
  3297. case VK_VENDOR_ID_AMD:
  3298. driver_priorities[vk::DriverId::eMesaRadv] = 1;
  3299. driver_priorities[vk::DriverId::eAmdOpenSource] = 2;
  3300. driver_priorities[vk::DriverId::eAmdProprietary] = 3;
  3301. break;
  3302. case VK_VENDOR_ID_INTEL:
  3303. driver_priorities[vk::DriverId::eIntelOpenSourceMESA] = 1;
  3304. driver_priorities[vk::DriverId::eIntelProprietaryWindows] = 2;
  3305. break;
  3306. case VK_VENDOR_ID_NVIDIA:
  3307. driver_priorities[vk::DriverId::eNvidiaProprietary] = 1;
  3308. #if defined(VK_API_VERSION_1_3) && VK_HEADER_VERSION >= 235
  3309. driver_priorities[vk::DriverId::eMesaNvk] = 2;
  3310. #endif
  3311. break;
  3312. }
  3313. if (driver_priorities.count(old_driver.driverID)) {
  3314. old_priority = driver_priorities[old_driver.driverID];
  3315. }
  3316. if (driver_priorities.count(new_driver.driverID)) {
  3317. new_priority = driver_priorities[new_driver.driverID];
  3318. }
  3319. if (new_priority < old_priority) {
  3320. auto r = std::remove(vk_instance.device_indices.begin(), vk_instance.device_indices.end(), *old_device);
  3321. vk_instance.device_indices.erase(r, vk_instance.device_indices.end());
  3322. vk_instance.device_indices.push_back(i);
  3323. VK_LOG_DEBUG("Prioritize device " << i << " driver " << new_driver.driverName << " over device " << *old_device << " driver " << old_driver.driverName);
  3324. }
  3325. else {
  3326. VK_LOG_DEBUG("Prioritize device " << *old_device << " driver " << old_driver.driverName << " over device " << i << " driver " << new_driver.driverName << std::endl);
  3327. }
  3328. }
  3329. }
  3330. }
  3331. // If no dedicated GPUs found, fall back to the first non-CPU device.
  3332. // If only CPU devices are available, return without devices.
  3333. if (vk_instance.device_indices.empty()) {
  3334. for (size_t i = 0; i < devices.size(); i++) {
  3335. if (devices[i].getProperties().deviceType != vk::PhysicalDeviceType::eCpu) {
  3336. vk_instance.device_indices.push_back(i);
  3337. break;
  3338. }
  3339. }
  3340. }
  3341. if (vk_instance.device_indices.empty()) {
  3342. GGML_LOG_INFO("ggml_vulkan: No devices found.\n");
  3343. return;
  3344. }
  3345. }
  3346. GGML_LOG_DEBUG("ggml_vulkan: Found %zu Vulkan devices:\n", vk_instance.device_indices.size());
  3347. for (size_t i = 0; i < vk_instance.device_indices.size(); i++) {
  3348. ggml_vk_print_gpu_info(i);
  3349. }
  3350. }
  3351. static void ggml_vk_init(ggml_backend_vk_context * ctx, size_t idx) {
  3352. VK_LOG_DEBUG("ggml_vk_init(" << ctx->name << ", " << idx << ")");
  3353. ggml_vk_instance_init();
  3354. GGML_ASSERT(idx < vk_instance.device_indices.size());
  3355. ctx->name = GGML_VK_NAME + std::to_string(idx);
  3356. ctx->device = ggml_vk_get_device(idx);
  3357. ctx->semaphore_idx = 0;
  3358. ctx->event_idx = 0;
  3359. ctx->prealloc_size_x = 0;
  3360. ctx->prealloc_size_y = 0;
  3361. ctx->prealloc_size_split_k = 0;
  3362. ctx->fence = ctx->device->device.createFence({});
  3363. ctx->almost_ready_fence = ctx->device->device.createFence({});
  3364. ctx->compute_cmd_pool.init(ctx->device, &ctx->device->compute_queue);
  3365. ctx->transfer_cmd_pool.init(ctx->device, &ctx->device->transfer_queue);
  3366. #ifdef GGML_VULKAN_CHECK_RESULTS
  3367. const char* skip_checks = getenv("GGML_VULKAN_SKIP_CHECKS");
  3368. vk_skip_checks = (skip_checks == NULL ? 0 : atoi(skip_checks));
  3369. const char* output_tensor = getenv("GGML_VULKAN_OUTPUT_TENSOR");
  3370. vk_output_tensor = (output_tensor == NULL ? 0 : atoi(output_tensor));
  3371. #endif
  3372. }
  3373. static vk_pipeline ggml_vk_get_to_fp16(ggml_backend_vk_context * ctx, ggml_type type) {
  3374. VK_LOG_DEBUG("ggml_vk_get_to_fp16()");
  3375. switch (type) {
  3376. case GGML_TYPE_F32:
  3377. case GGML_TYPE_Q4_0:
  3378. case GGML_TYPE_Q4_1:
  3379. case GGML_TYPE_Q5_0:
  3380. case GGML_TYPE_Q5_1:
  3381. case GGML_TYPE_Q8_0:
  3382. case GGML_TYPE_Q2_K:
  3383. case GGML_TYPE_Q3_K:
  3384. case GGML_TYPE_Q4_K:
  3385. case GGML_TYPE_Q5_K:
  3386. case GGML_TYPE_Q6_K:
  3387. case GGML_TYPE_IQ1_S:
  3388. case GGML_TYPE_IQ1_M:
  3389. case GGML_TYPE_IQ2_XXS:
  3390. case GGML_TYPE_IQ2_XS:
  3391. case GGML_TYPE_IQ2_S:
  3392. case GGML_TYPE_IQ3_XXS:
  3393. case GGML_TYPE_IQ3_S:
  3394. case GGML_TYPE_IQ4_XS:
  3395. case GGML_TYPE_IQ4_NL:
  3396. break;
  3397. default:
  3398. return nullptr;
  3399. }
  3400. return ctx->device->pipeline_dequant[type];
  3401. }
  3402. 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) {
  3403. VK_LOG_DEBUG("ggml_vk_get_mul_mat_mat_pipeline(" << ggml_type_name(src0_type) << ", " << ggml_type_name(src1_type) << ", " << prec << ")");
  3404. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  3405. return ctx->device->pipeline_matmul_f32;
  3406. }
  3407. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F16) {
  3408. return ctx->device->pipeline_matmul_f32_f16;
  3409. }
  3410. if (src0_type == GGML_TYPE_BF16 && src1_type == GGML_TYPE_BF16) {
  3411. return ctx->device->pipeline_matmul_bf16;
  3412. }
  3413. if (prec == GGML_PREC_DEFAULT && ctx->device->fp16 && !(ctx->device->coopmat_support && !ctx->device->coopmat_acc_f16_support)) {
  3414. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  3415. return ctx->device->pipeline_matmul_f16_f32.f16acc;
  3416. }
  3417. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  3418. return ctx->device->pipeline_matmul_f16.f16acc;
  3419. }
  3420. } else {
  3421. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  3422. return ctx->device->pipeline_matmul_f16_f32.f32acc;
  3423. }
  3424. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  3425. return ctx->device->pipeline_matmul_f16.f32acc;
  3426. }
  3427. }
  3428. // MMQ
  3429. if (src1_type == GGML_TYPE_Q8_1) {
  3430. 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;
  3431. if (pipelines->s == nullptr && pipelines->m == nullptr && pipelines->l == nullptr) {
  3432. return nullptr;
  3433. }
  3434. return pipelines;
  3435. }
  3436. if (src1_type != GGML_TYPE_F32 && !ctx->device->coopmat2) {
  3437. return nullptr;
  3438. }
  3439. switch (src0_type) {
  3440. case GGML_TYPE_Q4_0:
  3441. case GGML_TYPE_Q4_1:
  3442. case GGML_TYPE_Q5_0:
  3443. case GGML_TYPE_Q5_1:
  3444. case GGML_TYPE_Q8_0:
  3445. case GGML_TYPE_Q2_K:
  3446. case GGML_TYPE_Q3_K:
  3447. case GGML_TYPE_Q4_K:
  3448. case GGML_TYPE_Q5_K:
  3449. case GGML_TYPE_Q6_K:
  3450. case GGML_TYPE_IQ1_S:
  3451. case GGML_TYPE_IQ1_M:
  3452. case GGML_TYPE_IQ2_XXS:
  3453. case GGML_TYPE_IQ2_XS:
  3454. case GGML_TYPE_IQ2_S:
  3455. case GGML_TYPE_IQ3_XXS:
  3456. case GGML_TYPE_IQ3_S:
  3457. case GGML_TYPE_IQ4_XS:
  3458. case GGML_TYPE_IQ4_NL:
  3459. break;
  3460. default:
  3461. return nullptr;
  3462. }
  3463. if (ctx->device->coopmat2) {
  3464. assert(src1_type == GGML_TYPE_F16);
  3465. 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;
  3466. }
  3467. if (ctx->device->coopmat_support) {
  3468. 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;
  3469. }
  3470. 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;
  3471. }
  3472. 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) {
  3473. VK_LOG_DEBUG("ggml_vk_get_dequantize_mul_mat_vec()");
  3474. GGML_ASSERT(b_type == GGML_TYPE_F32 || b_type == GGML_TYPE_F16);
  3475. GGML_ASSERT(num_cols >= 1 && num_cols <= mul_mat_vec_max_cols);
  3476. switch (a_type) {
  3477. case GGML_TYPE_F32:
  3478. case GGML_TYPE_F16:
  3479. case GGML_TYPE_BF16:
  3480. case GGML_TYPE_Q4_0:
  3481. case GGML_TYPE_Q4_1:
  3482. case GGML_TYPE_Q5_0:
  3483. case GGML_TYPE_Q5_1:
  3484. case GGML_TYPE_Q8_0:
  3485. case GGML_TYPE_Q2_K:
  3486. case GGML_TYPE_Q3_K:
  3487. case GGML_TYPE_Q4_K:
  3488. case GGML_TYPE_Q5_K:
  3489. case GGML_TYPE_Q6_K:
  3490. case GGML_TYPE_IQ1_S:
  3491. case GGML_TYPE_IQ1_M:
  3492. case GGML_TYPE_IQ2_XXS:
  3493. case GGML_TYPE_IQ2_XS:
  3494. case GGML_TYPE_IQ2_S:
  3495. case GGML_TYPE_IQ3_XXS:
  3496. case GGML_TYPE_IQ3_S:
  3497. case GGML_TYPE_IQ4_XS:
  3498. case GGML_TYPE_IQ4_NL:
  3499. break;
  3500. default:
  3501. return nullptr;
  3502. }
  3503. return b_type == GGML_TYPE_F32 ? ctx->device->pipeline_dequant_mul_mat_vec_f32_f32[a_type][num_cols-1] : ctx->device->pipeline_dequant_mul_mat_vec_f16_f32[a_type][num_cols-1];
  3504. }
  3505. 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) {
  3506. VK_LOG_DEBUG("ggml_vk_get_mul_mat_mat_id_pipeline()");
  3507. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  3508. return ctx->device->pipeline_matmul_id_f32;
  3509. }
  3510. if (src0_type == GGML_TYPE_BF16 && src1_type == GGML_TYPE_BF16) {
  3511. return ctx->device->pipeline_matmul_id_bf16;
  3512. }
  3513. if (prec == GGML_PREC_DEFAULT && ctx->device->fp16 && !(ctx->device->coopmat_support && !ctx->device->coopmat_acc_f16_support)) {
  3514. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  3515. return ctx->device->pipeline_matmul_id_f16_f32.f16acc;
  3516. }
  3517. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  3518. return ctx->device->pipeline_matmul_id_f16.f16acc;
  3519. }
  3520. } else {
  3521. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  3522. return ctx->device->pipeline_matmul_id_f16_f32.f32acc;
  3523. }
  3524. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  3525. return ctx->device->pipeline_matmul_id_f16.f32acc;
  3526. }
  3527. }
  3528. GGML_ASSERT(src1_type == GGML_TYPE_F32 || (ctx->device->coopmat2 && src1_type == GGML_TYPE_F16));
  3529. switch (src0_type) {
  3530. case GGML_TYPE_Q4_0:
  3531. case GGML_TYPE_Q4_1:
  3532. case GGML_TYPE_Q5_0:
  3533. case GGML_TYPE_Q5_1:
  3534. case GGML_TYPE_Q8_0:
  3535. case GGML_TYPE_Q2_K:
  3536. case GGML_TYPE_Q3_K:
  3537. case GGML_TYPE_Q4_K:
  3538. case GGML_TYPE_Q5_K:
  3539. case GGML_TYPE_Q6_K:
  3540. case GGML_TYPE_IQ1_S:
  3541. case GGML_TYPE_IQ1_M:
  3542. case GGML_TYPE_IQ2_XXS:
  3543. case GGML_TYPE_IQ2_XS:
  3544. case GGML_TYPE_IQ2_S:
  3545. case GGML_TYPE_IQ3_XXS:
  3546. case GGML_TYPE_IQ3_S:
  3547. case GGML_TYPE_IQ4_XS:
  3548. case GGML_TYPE_IQ4_NL:
  3549. break;
  3550. default:
  3551. return nullptr;
  3552. }
  3553. return ctx->device->fp16 ? ctx->device->pipeline_dequant_mul_mat_mat_id[src0_type].f16acc : ctx->device->pipeline_dequant_mul_mat_mat_id[src0_type].f32acc;
  3554. }
  3555. static vk_pipeline ggml_vk_get_dequantize_mul_mat_vec_id(ggml_backend_vk_context * ctx, ggml_type a_type, ggml_type b_type) {
  3556. VK_LOG_DEBUG("ggml_vk_get_dequantize_mul_mat_vec()");
  3557. GGML_ASSERT(b_type == GGML_TYPE_F32);
  3558. switch (a_type) {
  3559. case GGML_TYPE_F32:
  3560. case GGML_TYPE_F16:
  3561. case GGML_TYPE_BF16:
  3562. case GGML_TYPE_Q4_0:
  3563. case GGML_TYPE_Q4_1:
  3564. case GGML_TYPE_Q5_0:
  3565. case GGML_TYPE_Q5_1:
  3566. case GGML_TYPE_Q8_0:
  3567. case GGML_TYPE_Q2_K:
  3568. case GGML_TYPE_Q3_K:
  3569. case GGML_TYPE_Q4_K:
  3570. case GGML_TYPE_Q5_K:
  3571. case GGML_TYPE_Q6_K:
  3572. case GGML_TYPE_IQ1_S:
  3573. case GGML_TYPE_IQ1_M:
  3574. case GGML_TYPE_IQ2_XXS:
  3575. case GGML_TYPE_IQ2_XS:
  3576. case GGML_TYPE_IQ2_S:
  3577. case GGML_TYPE_IQ3_XXS:
  3578. case GGML_TYPE_IQ3_S:
  3579. case GGML_TYPE_IQ4_XS:
  3580. case GGML_TYPE_IQ4_NL:
  3581. break;
  3582. default:
  3583. return nullptr;
  3584. }
  3585. return ctx->device->pipeline_dequant_mul_mat_vec_id_f32[a_type];
  3586. }
  3587. static vk_buffer ggml_vk_pool_malloc(ggml_backend_vk_context * ctx, size_t size) {
  3588. VK_LOG_DEBUG("ggml_vk_pool_malloc(" << size << ")");
  3589. VK_LOG_MEMORY("ggml_vk_pool_malloc");
  3590. int best_i = -1;
  3591. size_t best_size = std::numeric_limits<size_t>::max(); //smallest unused buffer that fits our needs
  3592. int worst_i = -1;
  3593. size_t worst_size = 0; //largest unused buffer seen so far
  3594. for (int i = 0; i < MAX_VK_BUFFERS; ++i) {
  3595. vk_buffer &b = ctx->buffer_pool[i];
  3596. if (b != nullptr && b->size >= size && b->size < best_size) {
  3597. best_i = i;
  3598. best_size = b->size;
  3599. }
  3600. if (b != nullptr && b->size > worst_size) {
  3601. worst_i = i;
  3602. worst_size = b->size;
  3603. }
  3604. }
  3605. if(best_i != -1) {
  3606. //found the smallest buffer that fits our needs
  3607. vk_buffer b = ctx->buffer_pool[best_i];
  3608. ctx->buffer_pool[best_i].reset();
  3609. return b;
  3610. }
  3611. if(worst_i != -1) {
  3612. //no buffer that fits our needs, resize largest one to save memory
  3613. vk_buffer& b = ctx->buffer_pool[worst_i];
  3614. ggml_vk_destroy_buffer(b);
  3615. }
  3616. return ggml_vk_create_buffer_device(ctx->device, size);
  3617. }
  3618. static void ggml_vk_pool_free(ggml_backend_vk_context * ctx, vk_buffer& buffer) {
  3619. VK_LOG_DEBUG("ggml_vk_pool_free(" << buffer->size << ")");
  3620. for (int i = 0; i < MAX_VK_BUFFERS; ++i) {
  3621. vk_buffer& b = ctx->buffer_pool[i];
  3622. if (b == nullptr) {
  3623. b = buffer;
  3624. return;
  3625. }
  3626. }
  3627. std::cerr << "ggml_vulkan: WARNING: vk buffer pool full, increase MAX_VK_BUFFERS" << std::endl;
  3628. ggml_vk_destroy_buffer(buffer);
  3629. }
  3630. // Returns an available temporary buffer that may only be used temporarily, it will be reused
  3631. static vk_buffer ggml_vk_create_buffer_temp(ggml_backend_vk_context * ctx, size_t size) {
  3632. // Try to find existing temp buffer with enough capacity
  3633. for (auto& buffer : ctx->gc.temp_buffers) {
  3634. if (buffer->size >= size) {
  3635. return buffer;
  3636. }
  3637. }
  3638. VK_LOG_MEMORY("ggml_vk_create_buffer_temp(" << size << ")");
  3639. // Otherwise create new buffer
  3640. vk_buffer buf = ggml_vk_pool_malloc(ctx, size);
  3641. ctx->gc.temp_buffers.push_back(buf);
  3642. return buf;
  3643. }
  3644. static void * ggml_vk_host_malloc(vk_device& device, size_t size) {
  3645. VK_LOG_MEMORY("ggml_vk_host_malloc(" << size << ")");
  3646. vk_buffer buf = ggml_vk_create_buffer(device, size,
  3647. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostCached,
  3648. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
  3649. if(!(buf->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible)) {
  3650. fprintf(stderr, "WARNING: failed to allocate %.2f MB of pinned memory\n",
  3651. size/1024.0/1024.0);
  3652. device->device.freeMemory(buf->device_memory);
  3653. device->device.destroyBuffer(buf->buffer);
  3654. return nullptr;
  3655. }
  3656. std::lock_guard<std::recursive_mutex> guard(device->mutex);
  3657. device->pinned_memory.push_back(std::make_tuple(buf->ptr, size, buf));
  3658. return buf->ptr;
  3659. }
  3660. static void ggml_vk_host_free(vk_device& device, void* ptr) {
  3661. if (ptr == nullptr) {
  3662. return;
  3663. }
  3664. VK_LOG_MEMORY("ggml_vk_host_free(" << ptr << ")");
  3665. std::lock_guard<std::recursive_mutex> guard(device->mutex);
  3666. vk_buffer buf;
  3667. size_t index;
  3668. for (size_t i = 0; i < device->pinned_memory.size(); i++) {
  3669. const uint8_t* addr = (const uint8_t*) std::get<0>(device->pinned_memory[i]);
  3670. const uint8_t* endr = addr + std::get<1>(device->pinned_memory[i]);
  3671. if (ptr >= addr && ptr < endr) {
  3672. buf = std::get<2>(device->pinned_memory[i]);
  3673. index = i;
  3674. break;
  3675. }
  3676. }
  3677. if (buf == nullptr) {
  3678. fprintf(stderr, "WARNING: failed to free pinned memory: memory not in map\n");
  3679. return;
  3680. }
  3681. ggml_vk_destroy_buffer(buf);
  3682. device->pinned_memory.erase(device->pinned_memory.begin() + index);
  3683. }
  3684. static void ggml_vk_host_get(vk_device& device, const void * ptr, vk_buffer& buf, size_t& buf_offset) {
  3685. std::lock_guard<std::recursive_mutex> guard(device->mutex);
  3686. buf = nullptr;
  3687. buf_offset = 0;
  3688. for (size_t i = 0; i < device->pinned_memory.size(); i++) {
  3689. const uint8_t* addr = (const uint8_t*) std::get<0>(device->pinned_memory[i]);
  3690. const uint8_t* endr = addr + std::get<1>(device->pinned_memory[i]);
  3691. if (ptr >= addr && ptr < endr) {
  3692. buf = std::get<2>(device->pinned_memory[i]);
  3693. buf_offset = ((const uint8_t *)ptr) - addr;
  3694. break;
  3695. }
  3696. }
  3697. }
  3698. static vk_submission ggml_vk_begin_submission(vk_device& device, vk_command_pool& p, bool one_time = true) {
  3699. vk_submission s;
  3700. s.buffer = ggml_vk_create_cmd_buffer(device, p);
  3701. if (one_time) {
  3702. s.buffer.begin({ vk::CommandBufferUsageFlagBits::eOneTimeSubmit });
  3703. } else {
  3704. s.buffer.begin({ vk::CommandBufferUsageFlags{} });
  3705. }
  3706. return s;
  3707. }
  3708. template <typename T> size_t push_constant_size(const T &t) {
  3709. static_assert(std::is_class<T>::value, "T must be a struct/class");
  3710. GGML_UNUSED(t);
  3711. return sizeof(T);
  3712. }
  3713. template <typename T> size_t push_constant_size(const std::vector<T> &t) {
  3714. GGML_UNUSED(t);
  3715. return sizeof(T) * t.size();
  3716. }
  3717. template <typename T, uint32_t N> size_t push_constant_size(const std::array<T, N> &t) {
  3718. GGML_UNUSED(t);
  3719. return sizeof(T) * N;
  3720. }
  3721. template <typename T> const T *push_constant_data(const T &t) {
  3722. static_assert(std::is_class<T>::value, "T must be a struct/class");
  3723. return &t;
  3724. }
  3725. template <typename T> const T *push_constant_data(const std::vector<T> &t) {
  3726. return t.data();
  3727. }
  3728. template <typename T, uint32_t N> const T *push_constant_data(const std::array<T, N> &t) {
  3729. return t.data();
  3730. }
  3731. template <typename T>
  3732. 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) {
  3733. const uint32_t wg0 = CEIL_DIV(elements[0], pipeline->wg_denoms[0]);
  3734. const uint32_t wg1 = CEIL_DIV(elements[1], pipeline->wg_denoms[1]);
  3735. const uint32_t wg2 = CEIL_DIV(elements[2], pipeline->wg_denoms[2]);
  3736. VK_LOG_DEBUG("ggml_vk_dispatch_pipeline(" << pipeline->name << ", {";
  3737. for (auto& buffer : descriptor_buffer_infos) {
  3738. std::cerr << "(" << buffer.buffer << ", " << buffer.offset << ", " << buffer.range << "), ";
  3739. }
  3740. std::cerr << "}, (" << wg0 << "," << wg1 << "," << wg2 << "))");
  3741. GGML_ASSERT(ctx->descriptor_set_idx < ctx->descriptor_sets.size());
  3742. GGML_ASSERT(descriptor_buffer_infos.size() <= MAX_PARAMETER_COUNT);
  3743. vk::DescriptorSet& descriptor_set = ctx->descriptor_sets[ctx->descriptor_set_idx++];
  3744. vk::WriteDescriptorSet write_descriptor_set{ descriptor_set, 0, 0, pipeline->parameter_count, vk::DescriptorType::eStorageBuffer, nullptr, descriptor_buffer_infos.begin() };
  3745. ctx->device->device.updateDescriptorSets({ write_descriptor_set }, {});
  3746. subctx->s->buffer.pushConstants(pipeline->layout, vk::ShaderStageFlagBits::eCompute, 0, push_constant_size(push_constants), push_constant_data(push_constants));
  3747. subctx->s->buffer.bindPipeline(vk::PipelineBindPoint::eCompute, pipeline->pipeline);
  3748. subctx->s->buffer.bindDescriptorSets(vk::PipelineBindPoint::eCompute,
  3749. pipeline->layout,
  3750. 0,
  3751. { descriptor_set },
  3752. {});
  3753. subctx->s->buffer.dispatch(wg0, wg1, wg2);
  3754. }
  3755. static void ggml_vk_end_submission(vk_submission& s, std::vector<vk_semaphore> wait_semaphores, std::vector<vk_semaphore> signal_semaphores) {
  3756. s.buffer.end();
  3757. s.wait_semaphores = std::move(wait_semaphores);
  3758. s.signal_semaphores = std::move(signal_semaphores);
  3759. }
  3760. static void ggml_vk_ctx_end(vk_context& ctx) {
  3761. VK_LOG_DEBUG("ggml_vk_ctx_end(" << ctx << ", " << ctx->seqs.size() << ")");
  3762. if (ctx->s == nullptr) {
  3763. return;
  3764. }
  3765. ctx->s->buffer.end();
  3766. ctx->s = nullptr;
  3767. }
  3768. static void ggml_vk_ctx_begin(vk_device& device, vk_context& subctx) {
  3769. VK_LOG_DEBUG("ggml_vk_ctx_begin(" << device->name << ")");
  3770. if (subctx->s != nullptr) {
  3771. ggml_vk_ctx_end(subctx);
  3772. }
  3773. subctx->seqs.push_back({ ggml_vk_begin_submission(device, *subctx->p) });
  3774. subctx->s = subctx->seqs[subctx->seqs.size() - 1].data();
  3775. }
  3776. static size_t ggml_vk_align_size(size_t width, size_t align) {
  3777. VK_LOG_DEBUG("ggml_vk_align_size(" << width << ", " << align << ")");
  3778. return CEIL_DIV(width, align) * align;
  3779. }
  3780. static void deferred_memcpy(void * dst, const void * src, size_t size, std::vector<vk_staging_memcpy>* memcpys = nullptr) {
  3781. if (memcpys == nullptr) {
  3782. memcpy(dst, src, size);
  3783. } else {
  3784. memcpys->emplace_back(dst, src, size);
  3785. }
  3786. }
  3787. static void ggml_vk_ensure_sync_staging_buffer(vk_device& device, size_t size) {
  3788. if (device->sync_staging == nullptr || device->sync_staging->size < size) {
  3789. VK_LOG_MEMORY("ggml_vk_ensure_sync_staging_buffer(" << size << ")");
  3790. ggml_vk_destroy_buffer(device->sync_staging);
  3791. device->sync_staging = ggml_vk_create_buffer_check(device, size,
  3792. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostCached,
  3793. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
  3794. }
  3795. }
  3796. 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) {
  3797. VK_LOG_DEBUG("ggml_vk_buffer_write_nc_async(" << tensor << ")");
  3798. GGML_ASSERT(!ggml_is_contiguous(tensor));
  3799. // Buffer is already mapped
  3800. if(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  3801. std::cerr << "ggml_vulkan: buffer_write_nc_async dst buffer is host_visible. Use synchronous write." << std::endl;
  3802. GGML_ABORT("fatal error");
  3803. }
  3804. // Check if src is pinned memory
  3805. vk_buffer buf = nullptr;
  3806. size_t buf_offset = 0;
  3807. ggml_vk_host_get(ctx->device, tensor->data, buf, buf_offset);
  3808. const uint64_t ne0 = tensor->ne[0];
  3809. const uint64_t ne1 = tensor->ne[1];
  3810. const uint64_t ne2 = tensor->ne[2];
  3811. const uint64_t ne3 = tensor->ne[3];
  3812. const uint64_t nb0 = tensor->nb[0];
  3813. const uint64_t nb1 = tensor->nb[1];
  3814. const uint64_t nb2 = tensor->nb[2];
  3815. const uint64_t nb3 = tensor->nb[3];
  3816. const ggml_type type = tensor->type;
  3817. const uint64_t ts = ggml_type_size(type);
  3818. const uint64_t bs = ggml_blck_size(type);
  3819. const uint64_t dstnb0 = ts;
  3820. const uint64_t dstnb1 = dstnb0*(ne0/bs);
  3821. const uint64_t dstnb2 = dstnb1*ne1;
  3822. const uint64_t dstnb3 = dstnb2*ne2;
  3823. const uint64_t ne = ggml_nelements(tensor);
  3824. if (buf != nullptr) {
  3825. // Memory is pinned, use as staging buffer
  3826. std::vector<vk::BufferCopy> slices;
  3827. for (uint64_t i3 = 0; i3 < ne3; i3++) {
  3828. for (uint64_t i2 = 0; i2 < ne2; i2++) {
  3829. // Find longest contiguous slice
  3830. if (ne1*nb1 == dstnb2) {
  3831. slices.push_back({ buf_offset + i3*nb3 + i2*nb2, offset + i3*dstnb3 + i2*dstnb2, dstnb2 });
  3832. } else {
  3833. for (uint64_t i1 = 0; i1 < ne1; i1++) {
  3834. if (ne0*nb0/bs == dstnb1) {
  3835. slices.push_back({ buf_offset + i3*nb3 + i2*nb2 + i1*nb1, offset + i3*dstnb3 + i2*dstnb2 + i1*dstnb1, dstnb1 });
  3836. } else {
  3837. const uint64_t s_off = buf_offset + i3*nb3 + i2*nb2 + i1*nb1;
  3838. const uint64_t d_off = offset + i3*dstnb3 + i2*dstnb2 + i1*dstnb1;
  3839. for (uint64_t i0 = 0; i0 < ne0; i0++) {
  3840. slices.push_back({ s_off + i1*nb0, d_off + i0*dstnb0, dstnb0 });
  3841. }
  3842. }
  3843. }
  3844. }
  3845. }
  3846. }
  3847. ggml_vk_sync_buffers(subctx);
  3848. subctx->s->buffer.copyBuffer(buf->buffer, dst->buffer, slices);
  3849. return;
  3850. }
  3851. if (!sync_staging) {
  3852. GGML_ABORT("Asynchronous write to non-pinned memory not supported");
  3853. }
  3854. // Staging buffer required
  3855. vk_buffer& staging = ctx->device->sync_staging;
  3856. const uint64_t copy_size = ts*ne/bs;
  3857. ggml_vk_ensure_sync_staging_buffer(ctx->device, copy_size);
  3858. VkBufferCopy buf_copy{ 0, offset, copy_size };
  3859. ggml_vk_sync_buffers(subctx);
  3860. vkCmdCopyBuffer(subctx->s->buffer, (VkBuffer)staging->buffer, (VkBuffer)dst->buffer, 1, &buf_copy);
  3861. for (uint64_t i3 = 0; i3 < ne3; i3++) {
  3862. for (uint64_t i2 = 0; i2 < ne2; i2++) {
  3863. // Find longest contiguous slice
  3864. if (ne1*nb1 == dstnb2) {
  3865. 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);
  3866. } else {
  3867. for (uint64_t i1 = 0; i1 < ne1; i1++) {
  3868. if (ne0*nb0/bs == dstnb1) {
  3869. 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);
  3870. } else {
  3871. const uint64_t s_off = buf_offset + i3*nb3 + i2*nb2 + i1*nb1;
  3872. const uint64_t d_off = i3*dstnb3 + i2*dstnb2 + i1*dstnb1;
  3873. for (uint64_t i0 = 0; i0 < ne0; i0++) {
  3874. deferred_memcpy((uint8_t *)staging->ptr + d_off + i0*dstnb0, (const uint8_t *) tensor->data + s_off + i0*nb0, dstnb0, &subctx->in_memcpys);
  3875. }
  3876. }
  3877. }
  3878. }
  3879. }
  3880. }
  3881. }
  3882. 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) {
  3883. VK_LOG_DEBUG("ggml_vk_buffer_write_2d_async(" << width << ", " << height << ")");
  3884. // Buffer is already mapped
  3885. if(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  3886. std::cerr << "ggml_vulkan: buffer_write_async dst buffer is host_visible. Use synchronous write." << std::endl;
  3887. GGML_ABORT("fatal error");
  3888. }
  3889. // Check if src is pinned memory
  3890. vk_buffer buf = nullptr;
  3891. size_t buf_offset = 0;
  3892. ggml_vk_host_get(dst->device, src, buf, buf_offset);
  3893. if (buf != nullptr) {
  3894. // Memory is pinned, use as staging buffer
  3895. std::vector<vk::BufferCopy> slices(1);
  3896. if (width == spitch) {
  3897. // Only do single write if stride is equal
  3898. slices[0].srcOffset = buf_offset;
  3899. slices[0].dstOffset = offset;
  3900. slices[0].size = width * height;
  3901. } else {
  3902. slices.resize(height);
  3903. for (size_t i = 0; i < height; i++) {
  3904. slices[i].srcOffset = buf_offset + i * spitch;
  3905. slices[i].dstOffset = offset + i * width;
  3906. slices[i].size = width;
  3907. }
  3908. }
  3909. ggml_vk_sync_buffers(subctx);
  3910. subctx->s->buffer.copyBuffer(buf->buffer, dst->buffer, slices);
  3911. return;
  3912. }
  3913. VK_LOG_DEBUG("STAGING");
  3914. if (!sync_staging) {
  3915. GGML_ABORT("Asynchronous write to non-pinned memory not supported");
  3916. }
  3917. // Staging buffer required
  3918. const size_t copy_size = width*height;
  3919. ggml_vk_ensure_sync_staging_buffer(dst->device, copy_size);
  3920. vk_buffer& staging_buffer = dst->device->sync_staging;
  3921. VkBufferCopy buf_copy = {
  3922. 0,
  3923. offset,
  3924. copy_size};
  3925. ggml_vk_sync_buffers(subctx);
  3926. vkCmdCopyBuffer(subctx->s->buffer, (VkBuffer)staging_buffer->buffer, (VkBuffer)dst->buffer, 1, &buf_copy);
  3927. if (width == spitch) {
  3928. deferred_memcpy((uint8_t *)staging_buffer->ptr, src, width * height, &subctx->in_memcpys);
  3929. } else {
  3930. for (size_t i = 0; i < height; i++) {
  3931. deferred_memcpy((uint8_t *)staging_buffer->ptr + i * width, (const uint8_t *) src + i * spitch, width, &subctx->in_memcpys);
  3932. }
  3933. }
  3934. }
  3935. 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) {
  3936. VK_LOG_DEBUG("ggml_vk_buffer_write_async(" << size << ")");
  3937. return ggml_vk_buffer_write_2d_async(subctx, dst, offset, src, size, size, 1, sync_staging);
  3938. }
  3939. 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) {
  3940. VK_LOG_DEBUG("ggml_vk_buffer_write_2d(" << width << ", " << height << ")");
  3941. // Buffer is already mapped
  3942. if(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  3943. GGML_ASSERT(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostCoherent);
  3944. for (size_t i = 0; i < height; i++) {
  3945. memcpy((uint8_t *)dst->ptr + offset + i * width, (const uint8_t *) src + i * spitch, width);
  3946. }
  3947. } else {
  3948. std::lock_guard<std::recursive_mutex> guard(dst->device->mutex);
  3949. vk_context subctx = ggml_vk_create_temporary_context(dst->device->transfer_queue.cmd_pool);
  3950. ggml_vk_ctx_begin(dst->device, subctx);
  3951. ggml_vk_buffer_write_2d_async(subctx, dst, offset, src, spitch, width, height, true);
  3952. ggml_vk_ctx_end(subctx);
  3953. for (auto& cpy : subctx->in_memcpys) {
  3954. memcpy(cpy.dst, cpy.src, cpy.n);
  3955. }
  3956. ggml_vk_submit(subctx, dst->device->fence);
  3957. VK_CHECK(dst->device->device.waitForFences({ dst->device->fence }, true, UINT64_MAX), "vk_buffer_write_2d waitForFences");
  3958. dst->device->device.resetFences({ dst->device->fence });
  3959. ggml_vk_queue_command_pools_cleanup(dst->device);
  3960. }
  3961. }
  3962. static void ggml_vk_buffer_write(vk_buffer& dst, size_t offset, const void * src, size_t size) {
  3963. VK_LOG_DEBUG("ggml_vk_buffer_write(" << size << ")");
  3964. ggml_vk_buffer_write_2d(dst, offset, src, 0, size, 1);
  3965. }
  3966. 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) {
  3967. VK_LOG_DEBUG("ggml_vk_buffer_read_2d_async(offset=" << offset << ", width=" << width << ", height=" << height << ")");
  3968. GGML_ASSERT(width > 0);
  3969. GGML_ASSERT(height > 0);
  3970. GGML_ASSERT(src != nullptr);
  3971. // TODO: staging_offset is not used
  3972. // Check if dst is pinned memory
  3973. vk_buffer buf = nullptr;
  3974. size_t buf_offset = 0;
  3975. ggml_vk_host_get(src->device, dst, buf, buf_offset);
  3976. std::vector<vk::BufferCopy> slices(1);
  3977. if (width == spitch && width == dpitch) {
  3978. // Only do single write if stride is equal
  3979. slices[0].srcOffset = offset;
  3980. slices[0].dstOffset = buf_offset;
  3981. slices[0].size = width * height;
  3982. } else {
  3983. slices.resize(height);
  3984. for (size_t i = 0; i < height; i++) {
  3985. slices[i].srcOffset = offset + i * spitch;
  3986. slices[i].dstOffset = buf_offset + i * dpitch;
  3987. slices[i].size = width;
  3988. }
  3989. }
  3990. if (buf != nullptr) {
  3991. // Memory is pinned, use as staging buffer
  3992. ggml_vk_sync_buffers(subctx);
  3993. subctx->s->buffer.copyBuffer(src->buffer, buf->buffer, slices);
  3994. return;
  3995. }
  3996. VK_LOG_DEBUG("STAGING");
  3997. if (!sync_staging) {
  3998. GGML_ABORT("Asynchronous read from non-pinned memory not supported");
  3999. }
  4000. // Fall back to staging buffer
  4001. const size_t copy_size = dpitch * height;
  4002. ggml_vk_ensure_sync_staging_buffer(src->device, copy_size);
  4003. vk_buffer& staging_buffer = src->device->sync_staging;
  4004. ggml_vk_sync_buffers(subctx);
  4005. subctx->s->buffer.copyBuffer(src->buffer, staging_buffer->buffer, slices);
  4006. deferred_memcpy(dst, staging_buffer->ptr, copy_size, &subctx->out_memcpys);
  4007. }
  4008. 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) {
  4009. return ggml_vk_buffer_read_2d_async(subctx, src, offset, dst, size, size, size, 1, sync_staging);
  4010. }
  4011. static void ggml_vk_buffer_read(vk_buffer& src, size_t offset, void * dst, size_t size) {
  4012. VK_LOG_DEBUG("ggml_vk_buffer_read(" << src->buffer << ", " << offset << ", " << size << ")");
  4013. // If the device is not an UMA device the memory is host-accessible through rebar. While writing
  4014. // through PCIe is sufficient fast reading back data from PCIe is slower than going through
  4015. // the HW device to host copy path.
  4016. if(src->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible && src->device->uma) {
  4017. GGML_ASSERT(src->memory_property_flags & vk::MemoryPropertyFlagBits::eHostCoherent);
  4018. memcpy(dst, (uint8_t *) src->ptr + offset, size);
  4019. } else {
  4020. std::lock_guard<std::recursive_mutex> guard(src->device->mutex);
  4021. vk_context subctx = ggml_vk_create_temporary_context(src->device->transfer_queue.cmd_pool);
  4022. ggml_vk_ctx_begin(src->device, subctx);
  4023. ggml_vk_buffer_read_async(subctx, src, offset, dst, size, true);
  4024. ggml_vk_ctx_end(subctx);
  4025. ggml_vk_submit(subctx, src->device->fence);
  4026. VK_CHECK(src->device->device.waitForFences({ src->device->fence }, true, UINT64_MAX), "vk_buffer_read waitForFences");
  4027. src->device->device.resetFences({ src->device->fence });
  4028. ggml_vk_queue_command_pools_cleanup(src->device);
  4029. for (auto& cpy : subctx->out_memcpys) {
  4030. memcpy(cpy.dst, cpy.src, cpy.n);
  4031. }
  4032. }
  4033. }
  4034. 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) {
  4035. VK_LOG_DEBUG("ggml_vk_buffer_copy_async(" << size << ")");
  4036. // Make sure both buffers are on same device
  4037. GGML_ASSERT(src->device == dst->device);
  4038. VkBufferCopy bc{ src_offset, dst_offset, size };
  4039. vkCmdCopyBuffer(ctx->s->buffer, (VkBuffer)src->buffer, (VkBuffer)dst->buffer, 1, &bc);
  4040. }
  4041. static void ggml_vk_buffer_copy(vk_buffer& dst, size_t dst_offset, vk_buffer& src, size_t src_offset, size_t size) {
  4042. if (src->device == dst->device) {
  4043. std::lock_guard<std::recursive_mutex> guard(src->device->mutex);
  4044. VK_LOG_DEBUG("ggml_vk_buffer_copy(SINGLE_DEVICE, " << size << ")");
  4045. // Copy within the device
  4046. vk_context subctx = ggml_vk_create_temporary_context(src->device->transfer_queue.cmd_pool);
  4047. ggml_vk_ctx_begin(src->device, subctx);
  4048. ggml_vk_buffer_copy_async(subctx, dst, dst_offset, src, src_offset, size);
  4049. ggml_vk_ctx_end(subctx);
  4050. ggml_vk_submit(subctx, src->device->fence);
  4051. VK_CHECK(src->device->device.waitForFences({ src->device->fence }, true, UINT64_MAX), "vk_buffer_copy waitForFences");
  4052. src->device->device.resetFences({ src->device->fence });
  4053. ggml_vk_queue_command_pools_cleanup(src->device);
  4054. } else {
  4055. VK_LOG_DEBUG("ggml_vk_buffer_copy(MULTI_DEVICE, " << size << ")");
  4056. // Copy device to device
  4057. ggml_vk_ensure_sync_staging_buffer(src->device, size);
  4058. ggml_vk_ensure_sync_staging_buffer(dst->device, size);
  4059. // Copy to src staging buffer
  4060. ggml_vk_buffer_copy(src->device->sync_staging, 0, src, src_offset, size);
  4061. // memcpy to dst staging buffer
  4062. memcpy(dst->device->sync_staging->ptr, src->device->sync_staging->ptr, size);
  4063. // Copy to dst buffer
  4064. ggml_vk_buffer_copy(dst, dst_offset, dst->device->sync_staging, 0, size);
  4065. }
  4066. }
  4067. static void ggml_vk_buffer_memset_async(vk_context& ctx, vk_buffer& dst, size_t offset, uint32_t c, size_t size) {
  4068. VK_LOG_DEBUG("ggml_vk_buffer_memset_async(" << offset << ", " << c << ", " << size << ")");
  4069. ctx->s->buffer.fillBuffer(dst->buffer, offset, size, c);
  4070. }
  4071. static void ggml_vk_buffer_memset(vk_buffer& dst, size_t offset, uint32_t c, size_t size) {
  4072. VK_LOG_DEBUG("ggml_vk_buffer_memset(" << offset << ", " << c << ", " << size << ")");
  4073. std::lock_guard<std::recursive_mutex> guard(dst->device->mutex);
  4074. vk_context subctx = ggml_vk_create_temporary_context(dst->device->transfer_queue.cmd_pool);
  4075. ggml_vk_ctx_begin(dst->device, subctx);
  4076. subctx->s->buffer.fillBuffer(dst->buffer, offset, size, c);
  4077. ggml_vk_ctx_end(subctx);
  4078. ggml_vk_submit(subctx, dst->device->fence);
  4079. VK_CHECK(dst->device->device.waitForFences({ dst->device->fence }, true, UINT64_MAX), "vk_memset waitForFences");
  4080. dst->device->device.resetFences({ dst->device->fence });
  4081. ggml_vk_queue_command_pools_cleanup(dst->device);
  4082. }
  4083. static uint32_t ggml_vk_guess_split_k(ggml_backend_vk_context * ctx, int m, int n, int k, const vk_pipeline& pipeline) {
  4084. VK_LOG_DEBUG("ggml_vk_guess_split_k(" << m << ", " << n << ", " << k << ")");
  4085. uint32_t split_k = 1;
  4086. if (ctx->device->shader_core_count != 0 && m >= (int)pipeline->wg_denoms[0] && n >= (int)pipeline->wg_denoms[1]) {
  4087. // If k is 'large' and the SMs will fill less than halfway, use split_k.
  4088. uint32_t m_tiles = CEIL_DIV(m, pipeline->wg_denoms[0]);
  4089. uint32_t n_tiles = CEIL_DIV(n, pipeline->wg_denoms[1]);
  4090. if (k >= 2048 && m_tiles * n_tiles < ctx->device->shader_core_count / 2) {
  4091. split_k = ctx->device->shader_core_count / (m_tiles * n_tiles);
  4092. // Clamp to 2 or 4
  4093. split_k = std::min(split_k, 4u);
  4094. if (split_k == 3) {
  4095. split_k = 2;
  4096. }
  4097. if (ctx->device->coopmat2) {
  4098. // coopmat2 shader expects splits to be aligned to 256
  4099. while (split_k > 1 && ((k / split_k) % 256) != 0) {
  4100. split_k /= 2;
  4101. }
  4102. }
  4103. }
  4104. }
  4105. return split_k;
  4106. }
  4107. 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) {
  4108. VK_LOG_DEBUG("ggml_vk_guess_matmul_pipeline(" << m << ", " << n << ", " << aligned << ", " << ggml_type_name(src0_type) << ", " << ggml_type_name(src1_type) << ")");
  4109. if (ctx->device->coopmat2) {
  4110. // Use large shader when the N dimension is greater than the medium shader's tile size
  4111. uint32_t crossover_large = mmp->m->wg_denoms[1];
  4112. if ((ctx->device->mul_mat_l[src0_type] && (n > crossover_large)) || (!ctx->device->mul_mat_m[src0_type] && !ctx->device->mul_mat_s[src0_type])) {
  4113. return aligned ? mmp->a_l : mmp->l;
  4114. }
  4115. // Use medium shader when the N dimension is greater than the small shader's tile size
  4116. uint32_t crossover_medium = mmp->s->wg_denoms[1];
  4117. if ((ctx->device->mul_mat_m[src0_type] && (n > crossover_medium)) || !ctx->device->mul_mat_s[src0_type]) {
  4118. return aligned ? mmp->a_m : mmp->m;
  4119. }
  4120. return aligned ? mmp->a_s : mmp->s;
  4121. }
  4122. 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])) {
  4123. return aligned ? mmp->a_s : mmp->s;
  4124. }
  4125. if ((ctx->device->mul_mat_m[src0_type] && (m <= 64 || n <= 64)) || !ctx->device->mul_mat_l[src0_type]) {
  4126. return aligned ? mmp->a_m : mmp->m;
  4127. }
  4128. return aligned ? mmp->a_l : mmp->l;
  4129. GGML_UNUSED(src1_type);
  4130. }
  4131. 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) {
  4132. VK_LOG_DEBUG("ggml_vk_guess_matmul_pipeline_align(" << m << ", " << n << ", " << ggml_type_name(src0_type) << ", " << ggml_type_name(src1_type) << ")");
  4133. return ggml_vk_guess_matmul_pipeline(ctx, mmp, m, n, true, src0_type, src1_type)->align;
  4134. }
  4135. static void ggml_vk_matmul(
  4136. ggml_backend_vk_context * ctx, vk_context& subctx, vk_pipeline& pipeline,
  4137. vk_subbuffer&& a, vk_subbuffer&& b, vk_subbuffer&& d, vk_subbuffer&& split_k_buffer,
  4138. uint32_t m, uint32_t n, uint32_t k, uint32_t stride_a, uint32_t stride_b, uint32_t stride_d,
  4139. uint32_t batch_stride_a, uint32_t batch_stride_b, uint32_t batch_stride_d,
  4140. uint32_t split_k, uint32_t batch, uint32_t ne02, uint32_t ne12, uint32_t broadcast2, uint32_t broadcast3,
  4141. uint32_t padded_n) {
  4142. 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 << ")");
  4143. ggml_vk_sync_buffers(subctx);
  4144. if (split_k == 1) {
  4145. 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 };
  4146. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { a, b, d }, pc, { m, n, batch });
  4147. return;
  4148. }
  4149. GGML_ASSERT(batch_stride_d == m * n);
  4150. 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, CEIL_DIV(k, split_k), ne02, ne12, broadcast2, broadcast3, padded_n };
  4151. // Make sure enough workgroups get assigned for split k to work
  4152. 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 });
  4153. ggml_vk_sync_buffers(subctx);
  4154. const std::array<uint32_t, 2> pc2 = { (uint32_t)(m * n * batch), split_k };
  4155. ggml_vk_dispatch_pipeline(ctx, subctx, ctx->device->pipeline_matmul_split_k_reduce, { split_k_buffer, d }, pc2, { m * n * batch, 1, 1 });
  4156. }
  4157. 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) {
  4158. VK_LOG_DEBUG("ggml_vk_guess_matmul_id_pipeline(" << m << ", " << n << ", " << aligned << ", " << ggml_type_name(src0_type) << ")");
  4159. if (ctx->device->coopmat2) {
  4160. // Use large shader when the N dimension is greater than the medium shader's tile size
  4161. uint32_t crossover_large = mmp->m->wg_denoms[1];
  4162. 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])) {
  4163. return aligned ? mmp->a_l : mmp->l;
  4164. }
  4165. // Use medium shader when the N dimension is greater than the small shader's tile size
  4166. uint32_t crossover_medium = mmp->s->wg_denoms[1];
  4167. if ((ctx->device->mul_mat_id_m[src0_type] && (n > crossover_medium)) || !ctx->device->mul_mat_id_s[src0_type]) {
  4168. return aligned ? mmp->a_m : mmp->m;
  4169. }
  4170. return aligned ? mmp->a_s : mmp->s;
  4171. }
  4172. 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])) {
  4173. return aligned ? mmp->a_s : mmp->s;
  4174. }
  4175. if ((ctx->device->mul_mat_id_m[src0_type] && (m <= 64 || n <= 64)) || !ctx->device->mul_mat_id_l[src0_type]) {
  4176. return aligned ? mmp->a_m : mmp->m;
  4177. }
  4178. return aligned ? mmp->a_l : mmp->l;
  4179. }
  4180. 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) {
  4181. VK_LOG_DEBUG("ggml_vk_guess_matmul_pipeline_align(" << m << ", " << n << ", " << ggml_type_name(src0_type) << ")");
  4182. return ggml_vk_guess_matmul_id_pipeline(ctx, mmp, m, n, true, src0_type)->align;
  4183. }
  4184. static void ggml_vk_matmul_id(
  4185. ggml_backend_vk_context * ctx, vk_context& subctx, vk_pipeline& pipeline,
  4186. vk_subbuffer&& a, vk_subbuffer&& b, vk_subbuffer&& d, vk_subbuffer&& ids,
  4187. uint32_t m, uint32_t n, uint32_t k, uint32_t stride_a, uint32_t stride_b, uint32_t stride_d,
  4188. uint32_t batch_stride_a, uint32_t batch_stride_b, uint32_t batch_stride_d,
  4189. uint32_t n_as, uint32_t nei0, uint32_t nei1, uint32_t nbi1, uint32_t ne11,
  4190. uint32_t padded_n) {
  4191. 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 << "), " <<
  4192. "m: " << m << ", n: " << n << ", k: " << k << ", stride_a: " << stride_a << ", stride_b: " << stride_b << ", stride_d: " << stride_d << ", " <<
  4193. "batch_stride_a: " << batch_stride_a << ", batch_stride_b: " << batch_stride_b << ", batch_stride_d: " << batch_stride_d << ", " <<
  4194. "n_as: " << n_as << ", nei0: " << nei0 << ", nei1: " << nei1 << ", nbi1: " << nbi1 << ", ne11: " << ne11 << ")");
  4195. ggml_vk_sync_buffers(subctx);
  4196. 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,
  4197. nei0, nei1, nbi1, ne11, padded_n };
  4198. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { a, b, d, ids }, pc, { m, nei1, n_as });
  4199. }
  4200. static bool ggml_vk_dim01_contiguous(const ggml_tensor * tensor) {
  4201. return
  4202. tensor->nb[0] == ggml_type_size(tensor->type) &&
  4203. tensor->nb[1] == (tensor->nb[0]*tensor->ne[0])/ggml_blck_size(tensor->type) &&
  4204. tensor->nb[3] == tensor->nb[2]*tensor->ne[2];
  4205. }
  4206. static vk_pipeline ggml_vk_get_cpy_pipeline(ggml_backend_vk_context * ctx, const ggml_tensor * src, const ggml_tensor * dst, ggml_type to) {
  4207. // Choose "contiguous copy" shader if src/dst are contiguous
  4208. bool contig = ggml_is_contiguous(src) && (!dst || ggml_is_contiguous(dst));
  4209. if (src->type == GGML_TYPE_F32 && to == GGML_TYPE_F32) {
  4210. if (contig) {
  4211. return ctx->device->pipeline_contig_cpy_f32_f32;
  4212. } else {
  4213. return ctx->device->pipeline_cpy_f32_f32;
  4214. }
  4215. }
  4216. if (src->type == GGML_TYPE_F32 && to == GGML_TYPE_F16) {
  4217. if (contig) {
  4218. return ctx->device->pipeline_contig_cpy_f32_f16;
  4219. } else {
  4220. return ctx->device->pipeline_cpy_f32_f16;
  4221. }
  4222. }
  4223. if (src->type == GGML_TYPE_F16 && to == GGML_TYPE_F16) {
  4224. if (contig) {
  4225. return ctx->device->pipeline_contig_cpy_f16_f16;
  4226. } else {
  4227. return ctx->device->pipeline_cpy_f16_f16;
  4228. }
  4229. }
  4230. if (src->type == GGML_TYPE_F16 && to == GGML_TYPE_F32) {
  4231. if (contig) {
  4232. return ctx->device->pipeline_contig_cpy_f16_f32;
  4233. } else {
  4234. return ctx->device->pipeline_cpy_f16_f32;
  4235. }
  4236. }
  4237. if (src->type == GGML_TYPE_F32 && to == GGML_TYPE_BF16) {
  4238. if (contig) {
  4239. return ctx->device->pipeline_contig_cpy_f32_bf16;
  4240. } else {
  4241. return ctx->device->pipeline_cpy_f32_bf16;
  4242. }
  4243. }
  4244. if (src->type == GGML_TYPE_F32) {
  4245. switch (to) {
  4246. case GGML_TYPE_Q4_0:
  4247. case GGML_TYPE_Q4_1:
  4248. case GGML_TYPE_Q5_0:
  4249. case GGML_TYPE_Q5_1:
  4250. case GGML_TYPE_Q8_0:
  4251. case GGML_TYPE_IQ4_NL:
  4252. return ctx->device->pipeline_cpy_f32_quant[to];
  4253. default:
  4254. break;
  4255. }
  4256. }
  4257. if (to == GGML_TYPE_F32) {
  4258. switch (src->type) {
  4259. case GGML_TYPE_Q4_0:
  4260. case GGML_TYPE_Q4_1:
  4261. case GGML_TYPE_Q5_0:
  4262. case GGML_TYPE_Q5_1:
  4263. case GGML_TYPE_Q8_0:
  4264. case GGML_TYPE_IQ4_NL:
  4265. return ctx->device->pipeline_cpy_quant_f32[src->type];
  4266. default:
  4267. break;
  4268. }
  4269. }
  4270. if (src->type == to) {
  4271. // Copy two or four bytes at a time, depending on block size.
  4272. // For quantized types, we scale by block size/type size. But
  4273. // this path is also used for bf16->bf16 for example, where the
  4274. // type size must be exactly 2 or 4.
  4275. GGML_ASSERT(ggml_is_quantized(to) || ggml_type_size(src->type) == 2 || ggml_type_size(src->type) == 4);
  4276. if ((ggml_type_size(src->type) % 4) == 0) {
  4277. if (contig) {
  4278. return ctx->device->pipeline_contig_cpy_f32_f32;
  4279. } else {
  4280. return ctx->device->pipeline_cpy_f32_f32;
  4281. }
  4282. } else {
  4283. if (contig) {
  4284. return ctx->device->pipeline_contig_cpy_f16_f16;
  4285. } else {
  4286. return ctx->device->pipeline_cpy_f16_f16;
  4287. }
  4288. }
  4289. }
  4290. std::cerr << "Missing CPY op for types: " << ggml_type_name(src->type) << " " << ggml_type_name(to) << std::endl;
  4291. GGML_ABORT("fatal error");
  4292. }
  4293. 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) {
  4294. 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] << "), ";
  4295. std::cerr << "buffer in size=" << in.buffer->size << ", buffer out size=" << out.buffer->size << ")");
  4296. const int tensor_type_size = ggml_type_size(tensor->type);
  4297. const uint32_t ne = ggml_nelements(tensor);
  4298. std::array<uint32_t, 3> elements;
  4299. if (ne > 262144) {
  4300. elements = { 512, 512, CEIL_DIV(ne, 262144) };
  4301. } else if (ne > 512) {
  4302. elements = { 512, CEIL_DIV(ne, 512), 1 };
  4303. } else {
  4304. elements = { ne, 1, 1 };
  4305. }
  4306. vk_op_unary_push_constants pc = {
  4307. (uint32_t)ne,
  4308. (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,
  4309. (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]),
  4310. 0,
  4311. 0.0f, 0.0f,
  4312. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  4313. };
  4314. init_pushconst_fastdiv(pc);
  4315. ggml_vk_sync_buffers(subctx);
  4316. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { in, out }, pc, elements);
  4317. }
  4318. static vk_pipeline ggml_vk_get_quantize_pipeline(ggml_backend_vk_context * ctx, ggml_type type) {
  4319. switch(type) {
  4320. case GGML_TYPE_Q8_1:
  4321. return ctx->device->pipeline_quantize_q8_1;
  4322. default:
  4323. std::cerr << "Missing quantize pipeline for type: " << ggml_type_name(type) << std::endl;
  4324. GGML_ABORT("fatal error");
  4325. }
  4326. }
  4327. static void ggml_vk_quantize_q8_1(ggml_backend_vk_context * ctx, vk_context& subctx, vk_subbuffer&& in, vk_subbuffer&& out, uint32_t ne) {
  4328. VK_LOG_DEBUG("ggml_vk_quantize_q8_1(" << "buffer in size=" << in.buffer->size << ", buffer out size=" << out.buffer->size << ", " << ne << ")");
  4329. vk_pipeline pipeline = ggml_vk_get_quantize_pipeline(ctx, GGML_TYPE_Q8_1);
  4330. ggml_vk_sync_buffers(subctx);
  4331. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { in, out }, std::array<uint32_t, 1>{ne}, { ne, 1, 1 });
  4332. }
  4333. 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) {
  4334. VK_LOG_DEBUG("ggml_vk_mul_mat_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];
  4335. 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];
  4336. 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];
  4337. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  4338. GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16); // NOLINT
  4339. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  4340. const uint64_t ne00 = src0->ne[0];
  4341. const uint64_t ne01 = src0->ne[1];
  4342. const uint64_t ne02 = src0->ne[2];
  4343. const uint64_t ne03 = src0->ne[3];
  4344. const uint64_t ne10 = src1->ne[0];
  4345. const uint64_t ne11 = src1->ne[1];
  4346. const uint64_t ne12 = src1->ne[2];
  4347. const uint64_t ne13 = src1->ne[3];
  4348. const uint64_t ne20 = dst->ne[0];
  4349. const uint64_t ne21 = dst->ne[1];
  4350. const uint64_t r2 = ne12 / ne02;
  4351. const uint64_t r3 = ne13 / ne03;
  4352. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  4353. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  4354. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  4355. vk_buffer d_Qx = nullptr;
  4356. size_t qx_buf_offset = 0;
  4357. vk_buffer d_Qy = nullptr;
  4358. size_t qy_buf_offset = 0;
  4359. bool src0_uma = false;
  4360. bool src1_uma = false;
  4361. if (ctx->device->uma) {
  4362. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  4363. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  4364. src0_uma = d_Qx != nullptr;
  4365. src1_uma = d_Qy != nullptr;
  4366. }
  4367. // Reformat and convert to fp16 if non-contiguous, or for coopmat2 for better perf
  4368. const bool x_non_contig = (ctx->device->coopmat2 && src0->type == GGML_TYPE_F32) ||
  4369. !ggml_vk_dim01_contiguous(src0);
  4370. const bool y_non_contig = (ctx->device->coopmat2 && src1->type == GGML_TYPE_F32) ||
  4371. (src0->type == GGML_TYPE_BF16 && src1->type != GGML_TYPE_BF16) ||
  4372. !ggml_vk_dim01_contiguous(src1);
  4373. // If src0 is BF16, try to use a BF16 x BF16 multiply
  4374. ggml_type f16_type = src0->type == GGML_TYPE_BF16 ? GGML_TYPE_BF16 : GGML_TYPE_F16;
  4375. const bool y_f32_kernel = src1->type == GGML_TYPE_F32 && !y_non_contig;
  4376. bool quantize_y = ctx->device->integer_dot_product && src1->type == GGML_TYPE_F32 && ggml_is_contiguous(src1) && (ne11 * ne10) % 4 == 0;
  4377. // Check for mmq first
  4378. 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;
  4379. if (mmp == nullptr) {
  4380. // Fall back to f16 dequant mul mat
  4381. mmp = ggml_vk_get_mul_mat_mat_pipeline(ctx, src0->type, y_non_contig ? f16_type : src1->type, (ggml_prec)dst->op_params[0]);
  4382. quantize_y = false;
  4383. }
  4384. const bool qx_needs_dequant = mmp == nullptr || x_non_contig;
  4385. const bool qy_needs_dequant = !quantize_y && ((src1->type != f16_type && !y_f32_kernel) || y_non_contig);
  4386. if (qx_needs_dequant) {
  4387. // Fall back to dequant + f16 mulmat
  4388. 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]);
  4389. }
  4390. // Not implemented
  4391. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  4392. 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)));
  4393. const bool aligned = !quantize_y && ne10 == kpad && ne01 > 8 && ne11 > 8;
  4394. 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));
  4395. // Reserve extra storage in the N dimension for the Y matrix, so we can avoid bounds-checking
  4396. uint32_t padded_n = qy_needs_dequant ? ROUNDUP_POW2(ne11, pipeline->wg_denoms[1]) : ne11;
  4397. const int x_ne = ne01 * ne00;
  4398. const int y_ne = padded_n * ne10;
  4399. const int d_ne = ne11 * ne01;
  4400. const uint32_t split_k = ggml_vk_guess_split_k(ctx, ne01, ne11, ne10, pipeline);
  4401. const uint64_t qx_sz = ggml_type_size(src0->type) * x_ne / ggml_blck_size(src0->type);
  4402. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  4403. const uint64_t x_sz = !qx_needs_dequant ? qx_sz : sizeof(ggml_fp16_t) * x_ne;
  4404. 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);
  4405. const uint64_t d_sz = sizeof(float) * d_ne;
  4406. vk_pipeline to_fp16_vk_0 = nullptr;
  4407. vk_pipeline to_fp16_vk_1 = nullptr;
  4408. vk_pipeline to_q8_1 = nullptr;
  4409. if (x_non_contig) {
  4410. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, f16_type);
  4411. } else {
  4412. to_fp16_vk_0 = ggml_vk_get_to_fp16(ctx, src0->type);
  4413. }
  4414. if (y_non_contig) {
  4415. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, f16_type);
  4416. } else {
  4417. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  4418. }
  4419. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  4420. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  4421. if (quantize_y) {
  4422. to_q8_1 = ggml_vk_get_quantize_pipeline(ctx, GGML_TYPE_Q8_1);
  4423. }
  4424. if (dryrun) {
  4425. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  4426. const uint64_t y_sz_upd = y_sz * ne12 * ne13;
  4427. const uint64_t split_k_size = split_k > 1 ? d_sz * ne12 * ne13 * split_k : 0;
  4428. if (
  4429. (qx_needs_dequant && x_sz_upd > ctx->device->max_memory_allocation_size) ||
  4430. (qy_needs_dequant && y_sz_upd > ctx->device->max_memory_allocation_size) ||
  4431. (split_k > 1 && split_k_size > ctx->device->max_memory_allocation_size)) {
  4432. GGML_ABORT("Requested preallocation size is too large");
  4433. }
  4434. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  4435. ctx->prealloc_size_x = x_sz_upd;
  4436. }
  4437. if ((qy_needs_dequant || quantize_y) && ctx->prealloc_size_y < y_sz_upd) {
  4438. ctx->prealloc_size_y = y_sz_upd;
  4439. }
  4440. if (split_k > 1 && ctx->prealloc_size_split_k < split_k_size) {
  4441. ctx->prealloc_size_split_k = split_k_size;
  4442. }
  4443. // Request descriptor sets
  4444. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  4445. if (qx_needs_dequant) {
  4446. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_0, 1);
  4447. }
  4448. if (qy_needs_dequant) {
  4449. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_1, 1);
  4450. }
  4451. if (quantize_y) {
  4452. ggml_pipeline_request_descriptor_sets(ctx, to_q8_1, 1);
  4453. }
  4454. if (split_k > 1) {
  4455. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_matmul_split_k_reduce, 1);
  4456. }
  4457. return;
  4458. }
  4459. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  4460. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  4461. GGML_ASSERT(d_D != nullptr);
  4462. GGML_ASSERT(d_D->size >= d_buf_offset + d_sz * ne02 * ne03);
  4463. vk_buffer d_X;
  4464. uint64_t x_buf_offset = 0;
  4465. vk_buffer d_Y;
  4466. uint64_t y_buf_offset = 0;
  4467. if (!src0_uma) {
  4468. d_Qx = src0_buf_ctx->dev_buffer;
  4469. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  4470. GGML_ASSERT(d_Qx != nullptr);
  4471. }
  4472. if (!src1_uma) {
  4473. d_Qy = src1_buf_ctx->dev_buffer;
  4474. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  4475. GGML_ASSERT(d_Qy != nullptr);
  4476. }
  4477. if (qx_needs_dequant) {
  4478. d_X = ctx->prealloc_x;
  4479. GGML_ASSERT(d_X->size >= x_sz * ne02 * ne03);
  4480. } else {
  4481. d_X = d_Qx;
  4482. x_buf_offset = qx_buf_offset;
  4483. GGML_ASSERT(qx_sz == x_sz);
  4484. }
  4485. if (qy_needs_dequant) {
  4486. d_Y = ctx->prealloc_y;
  4487. GGML_ASSERT(d_Y->size >= y_sz * ne12 * ne13);
  4488. } else if (quantize_y) {
  4489. d_Y = ctx->prealloc_y;
  4490. GGML_ASSERT(d_Y->size >= y_ne * ggml_type_size(GGML_TYPE_Q8_1) / ggml_blck_size(GGML_TYPE_Q8_1));
  4491. } else {
  4492. d_Y = d_Qy;
  4493. y_buf_offset = qy_buf_offset;
  4494. GGML_ASSERT(qy_sz == y_sz);
  4495. }
  4496. if (x_non_contig) {
  4497. 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 });
  4498. } else if (qx_needs_dequant) {
  4499. const std::vector<uint32_t> pc = { (uint32_t)ne01, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)(ggml_nelements(src0)) };
  4500. ggml_vk_sync_buffers(subctx);
  4501. 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});
  4502. }
  4503. if (y_non_contig) {
  4504. 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 });
  4505. }
  4506. if (quantize_y) {
  4507. 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);
  4508. }
  4509. uint32_t stride_batch_x = ne00*ne01;
  4510. uint32_t stride_batch_y = ne10*ne11;
  4511. if (!ggml_vk_dim01_contiguous(src0) && !qx_needs_dequant) {
  4512. stride_batch_x = src0->nb[0] / ggml_type_size(src0->type);
  4513. }
  4514. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant && !quantize_y) {
  4515. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  4516. }
  4517. // compute
  4518. ggml_vk_matmul(
  4519. ctx, subctx, pipeline,
  4520. { d_X, x_buf_offset, x_sz * ne02 * ne03 }, { d_Y, y_buf_offset, y_sz * ne12 * ne13 },
  4521. { d_D, d_buf_offset, d_sz * ne12 * ne13 }, { ctx->prealloc_split_k, 0, d_sz * ne12 * ne13 * split_k },
  4522. ne01, ne11, ne10,
  4523. ne10, ne10, ne01, stride_batch_x, stride_batch_y, ne20*ne21,
  4524. split_k, ne12*ne13, ne02, ne12, r2, r3, padded_n
  4525. ); // NOLINT
  4526. }
  4527. 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) {
  4528. 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];
  4529. 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];
  4530. 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];
  4531. std::cerr << "), " << (dryrun ? "dryrun" : "") << "),)");
  4532. GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16); // NOLINT
  4533. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  4534. const uint64_t ne00 = src0->ne[0];
  4535. const uint64_t ne01 = src0->ne[1];
  4536. const uint64_t ne02 = src0->ne[2];
  4537. const uint64_t ne03 = src0->ne[3];
  4538. const uint64_t ne10 = src1->ne[0];
  4539. const uint64_t ne11 = src1->ne[1];
  4540. const uint64_t ne12 = src1->ne[2];
  4541. const uint64_t ne13 = src1->ne[3];
  4542. const uint64_t ne20 = dst->ne[0];
  4543. const uint64_t ne21 = dst->ne[1];
  4544. const uint64_t ne22 = dst->ne[2];
  4545. const uint64_t ne23 = dst->ne[3];
  4546. const uint64_t r2 = ne12 / ne02;
  4547. const uint64_t r3 = ne13 / ne03;
  4548. // batch_n indicates that we need to compute a few vector results, and this assumes
  4549. // ne12 and ne13 are 1. It overloads the batch_strides to hold the row strides.
  4550. GGML_ASSERT(ne11 == 1 || ne12 * ne13 == 1);
  4551. bool batch_n = ne11 > 1;
  4552. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  4553. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  4554. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  4555. vk_buffer d_Qx = nullptr;
  4556. size_t qx_buf_offset = 0;
  4557. vk_buffer d_Qy = nullptr;
  4558. size_t qy_buf_offset = 0;
  4559. bool src0_uma = false;
  4560. bool src1_uma = false;
  4561. if (ctx->device->uma) {
  4562. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  4563. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  4564. src0_uma = d_Qx != nullptr;
  4565. src1_uma = d_Qy != nullptr;
  4566. }
  4567. const bool x_non_contig = !ggml_vk_dim01_contiguous(src0);
  4568. const bool y_non_contig = !ggml_vk_dim01_contiguous(src1);
  4569. const bool f16_f32_kernel = src1->type == GGML_TYPE_F32;
  4570. const bool qx_needs_dequant = x_non_contig;
  4571. const bool qy_needs_dequant = (src1->type != GGML_TYPE_F16 && !f16_f32_kernel) || y_non_contig;
  4572. // Not implemented
  4573. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  4574. const uint64_t x_ne = ne01 * ne00;
  4575. const uint64_t y_ne = ne11 * ne10;
  4576. const uint64_t d_ne = ne11 * ne01;
  4577. 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);
  4578. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  4579. 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;
  4580. const uint64_t y_sz = f16_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne;
  4581. const uint64_t d_sz = sizeof(float) * d_ne;
  4582. vk_pipeline to_fp16_vk_0 = nullptr;
  4583. vk_pipeline to_fp16_vk_1 = nullptr;
  4584. if (x_non_contig) {
  4585. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, src0->type);
  4586. }
  4587. if (y_non_contig) {
  4588. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, src1->type);
  4589. } else {
  4590. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  4591. }
  4592. vk_pipeline dmmv = ggml_vk_get_dequantize_mul_mat_vec(ctx, src0->type, src1->type, ne11);
  4593. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  4594. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  4595. GGML_ASSERT(dmmv != nullptr);
  4596. if (dryrun) {
  4597. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  4598. const uint64_t y_sz_upd = y_sz * ne12 * ne13;
  4599. if (
  4600. (qx_needs_dequant && x_sz_upd > ctx->device->max_memory_allocation_size) ||
  4601. (qy_needs_dequant && y_sz_upd > ctx->device->max_memory_allocation_size)) {
  4602. GGML_ABORT("Requested preallocation size is too large");
  4603. }
  4604. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  4605. ctx->prealloc_size_x = x_sz_upd;
  4606. }
  4607. if (qy_needs_dequant && ctx->prealloc_size_y < y_sz_upd) {
  4608. ctx->prealloc_size_y = y_sz_upd;
  4609. }
  4610. // Request descriptor sets
  4611. if (qx_needs_dequant) {
  4612. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_0, 1);
  4613. }
  4614. if (qy_needs_dequant) {
  4615. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_1, 1);
  4616. }
  4617. ggml_pipeline_request_descriptor_sets(ctx, dmmv, 1);
  4618. return;
  4619. }
  4620. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  4621. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  4622. GGML_ASSERT(d_D != nullptr);
  4623. vk_buffer d_X;
  4624. uint64_t x_buf_offset = 0;
  4625. vk_buffer d_Y;
  4626. uint64_t y_buf_offset = 0;
  4627. if(!src0_uma) {
  4628. d_Qx = src0_buf_ctx->dev_buffer;
  4629. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  4630. GGML_ASSERT(d_Qx != nullptr);
  4631. }
  4632. if(!src1_uma) {
  4633. d_Qy = src1_buf_ctx->dev_buffer;
  4634. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  4635. GGML_ASSERT(d_Qy != nullptr);
  4636. }
  4637. if (qx_needs_dequant) {
  4638. d_X = ctx->prealloc_x;
  4639. } else {
  4640. d_X = d_Qx;
  4641. x_buf_offset = qx_buf_offset;
  4642. GGML_ASSERT(qx_sz == x_sz);
  4643. }
  4644. if (qy_needs_dequant) {
  4645. d_Y = ctx->prealloc_y;
  4646. } else {
  4647. d_Y = d_Qy;
  4648. y_buf_offset = qy_buf_offset;
  4649. GGML_ASSERT(qy_sz == y_sz);
  4650. }
  4651. if (x_non_contig) {
  4652. GGML_ASSERT(x_sz == ggml_vk_align_size(ggml_type_size(src0->type) * x_ne, ctx->device->properties.limits.minStorageBufferOffsetAlignment));
  4653. 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 });
  4654. }
  4655. if (y_non_contig) {
  4656. GGML_ASSERT(y_sz == ggml_type_size(src1->type) * y_ne);
  4657. 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 });
  4658. }
  4659. // For batch_n, the A matrix is the same for each batch, and B/D use the row stride as the batch stride
  4660. uint32_t stride_batch_x = batch_n ? 0 : ne00*ne01;
  4661. uint32_t stride_batch_y = batch_n ? ne10 : (ne10*ne11);
  4662. uint32_t stride_batch_d = batch_n ? ne20 : (ne20*ne21);
  4663. if (!ggml_vk_dim01_contiguous(src0) && !qx_needs_dequant) {
  4664. stride_batch_x = src0->nb[0] / ggml_type_size(src0->type);
  4665. }
  4666. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant) {
  4667. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  4668. }
  4669. const uint32_t max_groups_x = ctx->device->properties.limits.maxComputeWorkGroupCount[0];
  4670. uint32_t groups_x = ne01;
  4671. uint32_t groups_z = 1;
  4672. if (ne01 > max_groups_x) {
  4673. groups_z = 64;
  4674. groups_x = CEIL_DIV(groups_x, groups_z);
  4675. }
  4676. // compute
  4677. const vk_mat_vec_push_constants pc = {
  4678. (uint32_t)ne00, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne01,
  4679. stride_batch_x, stride_batch_y, stride_batch_d,
  4680. (uint32_t)ne02, (uint32_t)ne12, (uint32_t)r2, (uint32_t)r3,
  4681. };
  4682. ggml_vk_sync_buffers(subctx);
  4683. ggml_vk_dispatch_pipeline(ctx, subctx, dmmv,
  4684. { vk_subbuffer{ d_X, x_buf_offset, x_sz * ne02 * ne03 }, vk_subbuffer{ d_Y, y_buf_offset, y_sz * ne12 * ne13 }, vk_subbuffer{ d_D, d_buf_offset, d_sz * ne22 * ne23} },
  4685. pc, { groups_x, (uint32_t)(ne12 * ne13), groups_z });
  4686. }
  4687. 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) {
  4688. 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];
  4689. 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];
  4690. 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];
  4691. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  4692. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  4693. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // NOLINT
  4694. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // NOLINT
  4695. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  4696. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  4697. const uint64_t ne00 = src0->ne[0];
  4698. const uint64_t ne01 = src0->ne[1];
  4699. const uint64_t ne02 = src0->ne[2];
  4700. // const uint64_t ne03 = src0->ne[3];
  4701. const uint64_t ne10 = src1->ne[0];
  4702. const uint64_t ne11 = src1->ne[1];
  4703. const uint64_t ne12 = src1->ne[2];
  4704. // const uint64_t ne13 = src1->ne[3];
  4705. GGML_ASSERT(ne11 == 1);
  4706. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  4707. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  4708. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  4709. vk_buffer d_Qy = nullptr;
  4710. size_t qy_buf_offset = 0;
  4711. bool src1_uma = false;
  4712. if (ctx->device->uma) {
  4713. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  4714. src1_uma = d_Qy != nullptr;
  4715. }
  4716. const uint64_t x_ne = ne00 * ne01 * ne02;
  4717. const uint64_t y_ne = ne10 * ne11 * ne12;
  4718. const uint64_t d_ne = ne01 * ne11 * ne12;
  4719. 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);
  4720. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  4721. const uint64_t d_sz = sizeof(float) * d_ne;
  4722. // With grouped query attention there are > 1 Q matrices per K, V matrix.
  4723. uint32_t gqa_ratio = (uint32_t)ne12 / (uint32_t)ne02;
  4724. if (gqa_ratio > 8 || gqa_ratio == 0 || ne12 != ne02 * gqa_ratio) {
  4725. gqa_ratio = 1;
  4726. }
  4727. if (dryrun) {
  4728. // Request descriptor sets
  4729. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_mul_mat_vec_p021_f16_f32[gqa_ratio - 1], 1);
  4730. return;
  4731. }
  4732. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  4733. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  4734. GGML_ASSERT(d_D != nullptr);
  4735. vk_buffer d_Qx = src0_buf_ctx->dev_buffer;
  4736. const uint64_t qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  4737. GGML_ASSERT(d_Qx != nullptr);
  4738. if (!src1_uma) {
  4739. d_Qy = src1_buf_ctx->dev_buffer;
  4740. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  4741. GGML_ASSERT(d_Qx != nullptr);
  4742. }
  4743. const uint64_t qy_buffer_offset = (qy_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  4744. const uint64_t qy_shader_offset = qy_buf_offset - qy_buffer_offset;
  4745. const uint64_t d_buffer_offset = (d_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  4746. const uint64_t d_shader_offset = d_buf_offset - d_buffer_offset;
  4747. // compute
  4748. 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)) };
  4749. uint32_t workgroups_z = (uint32_t)ne12;
  4750. // When gqa_ratio > 1, each invocation does multiple rows and we can launch fewer workgroups
  4751. if (gqa_ratio > 1) {
  4752. workgroups_z /= gqa_ratio;
  4753. }
  4754. ggml_vk_sync_buffers(subctx);
  4755. 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 });
  4756. }
  4757. 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) {
  4758. 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];
  4759. 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];
  4760. 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];
  4761. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  4762. GGML_ASSERT(!ggml_is_transposed(src0));
  4763. GGML_ASSERT(!ggml_is_transposed(src1));
  4764. GGML_ASSERT(!ggml_is_permuted(src0));
  4765. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  4766. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  4767. const uint64_t ne00 = src0->ne[0];
  4768. const uint64_t ne01 = src0->ne[1];
  4769. const uint64_t ne02 = src0->ne[2];
  4770. // const uint64_t ne03 = src0->ne[3];
  4771. const uint64_t nb01 = src0->nb[1];
  4772. const uint64_t nb02 = src0->nb[2];
  4773. const uint64_t nb12 = src1->nb[2];
  4774. // const uint64_t ne10 = src1->ne[0];
  4775. const uint64_t ne11 = src1->ne[1];
  4776. const uint64_t ne12 = src1->ne[2];
  4777. // const uint64_t ne13 = src1->ne[3];
  4778. GGML_ASSERT(ne11 == 1);
  4779. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  4780. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  4781. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  4782. vk_buffer d_Qy = nullptr;
  4783. size_t qy_buf_offset = 0;
  4784. bool src1_uma = false;
  4785. if (ctx->device->uma) {
  4786. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  4787. src1_uma = d_Qy != nullptr;
  4788. }
  4789. const uint64_t d_ne = ne01 * ne11 * ne12;
  4790. const uint32_t row_stride_x = nb01 / sizeof(ggml_fp16_t);
  4791. const uint32_t channel_stride_x = nb02 / sizeof(ggml_fp16_t);
  4792. const uint32_t channel_stride_y = nb12 / sizeof(float);
  4793. const uint64_t qx_sz = ggml_nbytes(src0);
  4794. const uint64_t qy_sz = ggml_nbytes(src1);
  4795. const uint64_t d_sz = sizeof(float) * d_ne;
  4796. if (dryrun) {
  4797. // Request descriptor sets
  4798. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_mul_mat_vec_nc_f16_f32, 1);
  4799. return;
  4800. }
  4801. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  4802. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  4803. GGML_ASSERT(d_D != nullptr);
  4804. vk_buffer d_Qx = src0_buf_ctx->dev_buffer;
  4805. const uint64_t qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  4806. GGML_ASSERT(d_Qx != nullptr);
  4807. if (!src1_uma) {
  4808. d_Qy = src1_buf_ctx->dev_buffer;
  4809. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  4810. GGML_ASSERT(d_Qx != nullptr);
  4811. }
  4812. const uint64_t qy_buffer_offset = (qy_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  4813. const uint64_t qy_shader_offset = qy_buf_offset - qy_buffer_offset;
  4814. const uint64_t d_buffer_offset = (d_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  4815. const uint64_t d_shader_offset = d_buf_offset - d_buffer_offset;
  4816. // compute
  4817. const std::array<uint32_t, 9> 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)) };
  4818. ggml_vk_sync_buffers(subctx);
  4819. ggml_vk_dispatch_pipeline(ctx, subctx, ctx->device->pipeline_mul_mat_vec_nc_f16_f32,
  4820. { 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, (uint32_t)ne12 });
  4821. }
  4822. 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) {
  4823. VK_LOG_DEBUG("ggml_vk_mul_mat(" << src0 << ", " << src1 << ", " << dst << ")");
  4824. if (src0->type == GGML_TYPE_F16 && ggml_is_permuted(src0) && ggml_is_permuted(src1) && dst->ne[1] == 1 &&
  4825. // detect 0213 permutation, and batch size of 1
  4826. src0->nb[0] <= src0->nb[2] &&
  4827. src0->nb[2] <= src0->nb[1] &&
  4828. src0->nb[1] <= src0->nb[3] &&
  4829. src1->nb[0] <= src1->nb[2] &&
  4830. src1->nb[2] <= src1->nb[1] &&
  4831. src1->nb[1] <= src1->nb[3] &&
  4832. src0->ne[3] == 1 &&
  4833. src1->ne[3] == 1) {
  4834. ggml_vk_mul_mat_vec_p021_f16_f32(ctx, subctx, src0, src1, dst, dryrun);
  4835. } else if (src0->type == GGML_TYPE_F16 && !ggml_is_contiguous(src0) && !ggml_is_transposed(src1) && dst->ne[1] == 1 &&
  4836. !ggml_is_permuted(src0) && !ggml_is_permuted(src1)) {
  4837. ggml_vk_mul_mat_vec_nc_f16_f32(ctx, subctx, src0, src1, dst, dryrun);
  4838. // mul_mat_vec supports batching ne12*ne13 when ne11==1, or treating ne11 as the batch size (up to four)
  4839. // when ne12 and ne13 are one.
  4840. } else if ((dst->ne[1] == 1 || (dst->ne[1] <= mul_mat_vec_max_cols && src1->ne[2] * src1->ne[3] == 1)) &&
  4841. (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16 || ggml_is_quantized(src0->type))) {
  4842. ggml_vk_mul_mat_vec_q_f16(ctx, subctx, src0, src1, dst, dryrun);
  4843. } else {
  4844. ggml_vk_mul_mat_q_f16(ctx, subctx, src0, src1, dst, dryrun);
  4845. }
  4846. }
  4847. 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) {
  4848. 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];
  4849. 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];
  4850. 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];
  4851. 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] << "),)");
  4852. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  4853. GGML_ASSERT(ids->type == GGML_TYPE_I32);
  4854. const uint64_t ne00 = src0->ne[0];
  4855. const uint64_t ne01 = src0->ne[1];
  4856. const uint64_t ne02 = src0->ne[2];
  4857. const uint64_t ne03 = src0->ne[3];
  4858. const uint64_t ne10 = src1->ne[0];
  4859. const uint64_t ne11 = src1->ne[1];
  4860. const uint64_t ne12 = src1->ne[2];
  4861. const uint64_t ne13 = src1->ne[3];
  4862. const uint64_t nei0 = ids->ne[0];
  4863. const uint64_t nei1 = ids->ne[1];
  4864. GGML_ASSERT(nei0 * nei1 <= 4096);
  4865. const uint32_t nbi1 = ids->nb[1];
  4866. const uint32_t nbi2 = ids->nb[2];
  4867. const uint64_t ne20 = dst->ne[0];
  4868. const uint64_t ne21 = dst->ne[1];
  4869. const uint64_t ne22 = dst->ne[2];
  4870. const uint64_t ne23 = dst->ne[3];
  4871. const uint64_t n_as = ne02;
  4872. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  4873. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  4874. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  4875. ggml_backend_vk_buffer_context * ids_buf_ctx = (ggml_backend_vk_buffer_context *)ids->buffer->context;
  4876. vk_buffer d_Qx = nullptr;
  4877. size_t qx_buf_offset = 0;
  4878. vk_buffer d_Qy = nullptr;
  4879. size_t qy_buf_offset = 0;
  4880. vk_buffer d_ids = nullptr;
  4881. size_t ids_buf_offset = 0;
  4882. bool src0_uma = false;
  4883. bool src1_uma = false;
  4884. bool ids_uma = false;
  4885. if (ctx->device->uma) {
  4886. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  4887. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  4888. ggml_vk_host_get(ctx->device, ids->data, d_ids, ids_buf_offset);
  4889. src0_uma = d_Qx != nullptr;
  4890. src1_uma = d_Qy != nullptr;
  4891. ids_uma = d_ids != nullptr;
  4892. }
  4893. // Reformat and convert to fp16 if non-contiguous, or for coopmat2 for better perf
  4894. const bool x_non_contig = (ctx->device->coopmat2 && src0->type == GGML_TYPE_F32) ||
  4895. !ggml_vk_dim01_contiguous(src0);
  4896. const bool y_non_contig = (ctx->device->coopmat2 && src1->type == GGML_TYPE_F32) ||
  4897. (src0->type == GGML_TYPE_BF16 && src1->type != GGML_TYPE_BF16) ||
  4898. !ggml_vk_dim01_contiguous(src1);
  4899. // If src0 is BF16, try to use a BF16 x BF16 multiply
  4900. ggml_type f16_type = src0->type == GGML_TYPE_BF16 ? GGML_TYPE_BF16 : GGML_TYPE_F16;
  4901. const bool y_f32_kernel = src1->type == GGML_TYPE_F32 && !y_non_contig;
  4902. 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]);
  4903. const bool qx_needs_dequant = mmp == nullptr || x_non_contig;
  4904. const bool qy_needs_dequant = (src1->type != f16_type && !y_f32_kernel) || y_non_contig;
  4905. if (qx_needs_dequant) {
  4906. // Fall back to dequant + f16 mulmat
  4907. 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]);
  4908. }
  4909. // Not implemented
  4910. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  4911. 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));
  4912. const bool aligned = ne10 == kpad && ne01 > 8 && nei1 > 8;
  4913. vk_pipeline pipeline = ggml_vk_guess_matmul_id_pipeline(ctx, mmp, ne01, nei1, aligned, qx_needs_dequant ? f16_type : src0->type);
  4914. // Reserve extra storage in the N dimension for the Y matrix, so we can avoid bounds-checking
  4915. uint32_t padded_n = qy_needs_dequant ? ROUNDUP_POW2(ne11, pipeline->wg_denoms[1]) :ne11;
  4916. const uint64_t x_ne = ne01 * ne00;
  4917. const uint64_t y_ne = padded_n * ne10;
  4918. const uint64_t d_ne = ne21 * ne20;
  4919. const uint64_t qx_sz = ggml_type_size(src0->type) * x_ne / ggml_blck_size(src0->type);
  4920. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  4921. const uint64_t x_sz = !qx_needs_dequant ? qx_sz : sizeof(ggml_fp16_t) * x_ne;
  4922. const uint64_t y_sz = y_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne;
  4923. const uint64_t ids_sz = nbi2;
  4924. const uint64_t d_sz = sizeof(float) * d_ne;
  4925. vk_pipeline to_fp16_vk_0 = nullptr;
  4926. vk_pipeline to_fp16_vk_1 = nullptr;
  4927. if (x_non_contig) {
  4928. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, f16_type);
  4929. } else {
  4930. to_fp16_vk_0 = ggml_vk_get_to_fp16(ctx, src0->type);
  4931. }
  4932. if (y_non_contig) {
  4933. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, f16_type);
  4934. } else {
  4935. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  4936. }
  4937. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  4938. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  4939. if (dryrun) {
  4940. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  4941. const uint64_t y_sz_upd = y_sz * ne12 * ne13;
  4942. if (
  4943. (qx_needs_dequant && x_sz_upd > ctx->device->max_memory_allocation_size) ||
  4944. (qy_needs_dequant && y_sz_upd > ctx->device->max_memory_allocation_size)) {
  4945. GGML_ABORT("Requested preallocation size is too large");
  4946. }
  4947. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  4948. ctx->prealloc_size_x = x_sz_upd;
  4949. }
  4950. if (qy_needs_dequant && ctx->prealloc_size_y < y_sz_upd) {
  4951. ctx->prealloc_size_y = y_sz_upd;
  4952. }
  4953. // Request descriptor sets
  4954. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  4955. if (qx_needs_dequant) {
  4956. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_0, 1);
  4957. }
  4958. if (qy_needs_dequant) {
  4959. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_1, 1);
  4960. }
  4961. return;
  4962. }
  4963. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  4964. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  4965. GGML_ASSERT(d_D != nullptr);
  4966. vk_buffer d_X;
  4967. uint64_t x_buf_offset = 0;
  4968. vk_buffer d_Y;
  4969. uint64_t y_buf_offset = 0;
  4970. if (!src0_uma) {
  4971. d_Qx = src0_buf_ctx->dev_buffer;
  4972. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  4973. GGML_ASSERT(d_Qx != nullptr);
  4974. }
  4975. if (!src1_uma) {
  4976. d_Qy = src1_buf_ctx->dev_buffer;
  4977. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  4978. GGML_ASSERT(d_Qy != nullptr);
  4979. }
  4980. if (!ids_uma) {
  4981. d_ids = ids_buf_ctx->dev_buffer;
  4982. ids_buf_offset = vk_tensor_offset(ids) + ids->view_offs;
  4983. GGML_ASSERT(d_ids != nullptr);
  4984. }
  4985. if (qx_needs_dequant) {
  4986. d_X = ctx->prealloc_x;
  4987. GGML_ASSERT(d_X->size >= x_sz * ne02 * ne03);
  4988. } else {
  4989. d_X = d_Qx;
  4990. x_buf_offset = qx_buf_offset;
  4991. GGML_ASSERT(qx_sz == x_sz);
  4992. }
  4993. if (qy_needs_dequant) {
  4994. d_Y = ctx->prealloc_y;
  4995. GGML_ASSERT(d_Y->size >= y_sz * ne12 * ne13);
  4996. } else {
  4997. d_Y = d_Qy;
  4998. y_buf_offset = qy_buf_offset;
  4999. GGML_ASSERT(qy_sz == y_sz);
  5000. }
  5001. if (x_non_contig) {
  5002. 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 });
  5003. } else if (qx_needs_dequant) {
  5004. const std::vector<uint32_t> pc = { (uint32_t)ne01, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)(ggml_nelements(src0)) };
  5005. ggml_vk_sync_buffers(subctx);
  5006. ggml_vk_dispatch_pipeline(ctx, subctx, to_fp16_vk_0,
  5007. { 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});
  5008. }
  5009. if (y_non_contig) {
  5010. 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 });
  5011. }
  5012. uint32_t stride_batch_x = ne00*ne01;
  5013. uint32_t stride_batch_y = ne10*ne11;
  5014. if (!ggml_vk_dim01_contiguous(src0) && !qx_needs_dequant) {
  5015. stride_batch_x = src0->nb[0] / ggml_type_size(src0->type);
  5016. }
  5017. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant) {
  5018. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  5019. }
  5020. // compute
  5021. ggml_vk_matmul_id(
  5022. ctx, subctx, pipeline,
  5023. { d_X, x_buf_offset, x_sz * ne02 * ne03 }, { d_Y, y_buf_offset, y_sz * ne12 * ne13 },
  5024. { d_D, d_buf_offset, d_sz * ne22 * ne23 }, { d_ids, ids_buf_offset, ids_sz },
  5025. ne01, ne21, ne10, ne10, ne10, ne01,
  5026. stride_batch_x, stride_batch_y, ne20*ne21,
  5027. n_as, nei0, nei1, nbi1 / ggml_type_size(ids->type), ne11, padded_n
  5028. ); // NOLINT
  5029. }
  5030. 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) {
  5031. 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];
  5032. 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];
  5033. 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];
  5034. 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];
  5035. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  5036. GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16); // NOLINT
  5037. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  5038. GGML_ASSERT(ids->type == GGML_TYPE_I32);
  5039. const uint64_t ne00 = src0->ne[0];
  5040. const uint64_t ne01 = src0->ne[1];
  5041. const uint64_t ne02 = src0->ne[2];
  5042. const uint64_t ne03 = src0->ne[3];
  5043. const uint64_t ne10 = src1->ne[0];
  5044. const uint64_t ne11 = src1->ne[1];
  5045. const uint64_t ne12 = src1->ne[2];
  5046. const uint64_t ne13 = src1->ne[3];
  5047. const uint64_t nei0 = ids->ne[0];
  5048. const uint64_t nei1 = ids->ne[1];
  5049. const uint64_t nbi2 = ids->nb[2];
  5050. GGML_ASSERT(nei1 == 1);
  5051. const uint64_t ne20 = dst->ne[0];
  5052. const uint64_t ne21 = dst->ne[1];
  5053. const uint64_t ne22 = dst->ne[2];
  5054. const uint64_t ne23 = dst->ne[3];
  5055. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  5056. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  5057. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  5058. ggml_backend_vk_buffer_context * ids_buf_ctx = (ggml_backend_vk_buffer_context *)ids->buffer->context;
  5059. vk_buffer d_Qx = nullptr;
  5060. size_t qx_buf_offset = 0;
  5061. vk_buffer d_Qy = nullptr;
  5062. size_t qy_buf_offset = 0;
  5063. vk_buffer d_ids = nullptr;
  5064. size_t ids_buf_offset = 0;
  5065. bool src0_uma = false;
  5066. bool src1_uma = false;
  5067. bool ids_uma = false;
  5068. if (ctx->device->uma) {
  5069. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  5070. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  5071. ggml_vk_host_get(ctx->device, ids->data, d_ids, ids_buf_offset);
  5072. src0_uma = d_Qx != nullptr;
  5073. src1_uma = d_Qy != nullptr;
  5074. ids_uma = d_ids != nullptr;
  5075. }
  5076. const bool x_non_contig = !ggml_vk_dim01_contiguous(src0);
  5077. const bool y_non_contig = !ggml_vk_dim01_contiguous(src1);
  5078. const bool f16_f32_kernel = src1->type == GGML_TYPE_F32;
  5079. const bool qx_needs_dequant = x_non_contig;
  5080. const bool qy_needs_dequant = (src1->type != GGML_TYPE_F16 && !f16_f32_kernel) || y_non_contig;
  5081. // Not implemented
  5082. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  5083. const uint64_t x_ne = ne01 * ne00;
  5084. const uint64_t y_ne = ne11 * ne10;
  5085. const uint64_t d_ne = ne21 * ne20;
  5086. 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);
  5087. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  5088. 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;
  5089. const uint64_t y_sz = f16_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne;
  5090. const uint64_t ids_sz = nbi2;
  5091. const uint64_t d_sz = sizeof(float) * d_ne;
  5092. vk_pipeline to_fp16_vk_0 = nullptr;
  5093. vk_pipeline to_fp16_vk_1 = nullptr;
  5094. if (x_non_contig) {
  5095. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, src0->type);
  5096. }
  5097. if (y_non_contig) {
  5098. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, src1->type);
  5099. } else {
  5100. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  5101. }
  5102. vk_pipeline dmmv = ggml_vk_get_dequantize_mul_mat_vec_id(ctx, src0->type, src1->type);
  5103. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  5104. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  5105. GGML_ASSERT(dmmv != nullptr);
  5106. if (dryrun) {
  5107. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  5108. const uint64_t y_sz_upd = y_sz * ne12 * ne13;
  5109. if (
  5110. (qx_needs_dequant && x_sz_upd > ctx->device->max_memory_allocation_size) ||
  5111. (qy_needs_dequant && y_sz_upd > ctx->device->max_memory_allocation_size)) {
  5112. GGML_ABORT("Requested preallocation size is too large");
  5113. }
  5114. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  5115. ctx->prealloc_size_x = x_sz_upd;
  5116. }
  5117. if (qy_needs_dequant && ctx->prealloc_size_y < y_sz_upd) {
  5118. ctx->prealloc_size_y = y_sz_upd;
  5119. }
  5120. // Request descriptor sets
  5121. if (qx_needs_dequant) {
  5122. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_0, 1);
  5123. }
  5124. if (qy_needs_dequant) {
  5125. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_1, 1);
  5126. }
  5127. ggml_pipeline_request_descriptor_sets(ctx, dmmv, 1);
  5128. return;
  5129. }
  5130. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  5131. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  5132. GGML_ASSERT(d_D != nullptr);
  5133. vk_buffer d_X;
  5134. uint64_t x_buf_offset = 0;
  5135. vk_buffer d_Y;
  5136. uint64_t y_buf_offset = 0;
  5137. if(!src0_uma) {
  5138. d_Qx = src0_buf_ctx->dev_buffer;
  5139. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  5140. GGML_ASSERT(d_Qx != nullptr);
  5141. }
  5142. if(!src1_uma) {
  5143. d_Qy = src1_buf_ctx->dev_buffer;
  5144. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  5145. GGML_ASSERT(d_Qy != nullptr);
  5146. }
  5147. if(!ids_uma) {
  5148. d_ids = ids_buf_ctx->dev_buffer;
  5149. ids_buf_offset = vk_tensor_offset(ids) + ids->view_offs;
  5150. GGML_ASSERT(d_ids != nullptr);
  5151. }
  5152. if (qx_needs_dequant) {
  5153. d_X = ctx->prealloc_x;
  5154. } else {
  5155. d_X = d_Qx;
  5156. x_buf_offset = qx_buf_offset;
  5157. GGML_ASSERT(qx_sz == x_sz);
  5158. }
  5159. if (qy_needs_dequant) {
  5160. d_Y = ctx->prealloc_y;
  5161. } else {
  5162. d_Y = d_Qy;
  5163. y_buf_offset = qy_buf_offset;
  5164. GGML_ASSERT(qy_sz == y_sz);
  5165. }
  5166. if (x_non_contig) {
  5167. GGML_ASSERT(x_sz == ggml_vk_align_size(ggml_type_size(src0->type) * x_ne, ctx->device->properties.limits.minStorageBufferOffsetAlignment));
  5168. 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 });
  5169. }
  5170. if (y_non_contig) {
  5171. GGML_ASSERT(y_sz == ggml_type_size(src1->type) * y_ne);
  5172. 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 });
  5173. }
  5174. uint32_t stride_batch_y = ne10*ne11;
  5175. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant) {
  5176. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  5177. }
  5178. const uint32_t max_groups_x = ctx->device->properties.limits.maxComputeWorkGroupCount[0];
  5179. uint32_t groups_x = ne01;
  5180. uint32_t groups_z = 1;
  5181. if (ne01 > max_groups_x) {
  5182. groups_z = 64;
  5183. groups_x = CEIL_DIV(groups_x, groups_z);
  5184. }
  5185. // compute
  5186. const vk_mat_vec_id_push_constants pc = {
  5187. (uint32_t)ne00, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne01,
  5188. (uint32_t)x_ne, stride_batch_y, (uint32_t)(ne20*ne21),
  5189. (uint32_t)nei0, (uint32_t)ne11,
  5190. };
  5191. ggml_vk_sync_buffers(subctx);
  5192. ggml_vk_dispatch_pipeline(ctx, subctx, dmmv,
  5193. { vk_subbuffer{ d_X, x_buf_offset, x_sz * ne02 * ne03 },
  5194. 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 } },
  5195. pc, { groups_x, (uint32_t)nei0, groups_z });
  5196. }
  5197. 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) {
  5198. VK_LOG_DEBUG("ggml_vk_mul_mat_id(" << src0 << ", " << src1 << ", " << src2 << ", " << dst << ")");
  5199. if (src2->ne[1] == 1 && (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type))) {
  5200. ggml_vk_mul_mat_vec_id_q_f16(ctx, subctx, src0, src1, src2, dst, dryrun);
  5201. } else {
  5202. // Split based on number of ids, to fit in shared memory
  5203. const uint32_t nei0 = (uint32_t)src2->ne[0];
  5204. const uint32_t nei1 = (uint32_t)src2->ne[1];
  5205. GGML_ASSERT(nei0 <= 4096);
  5206. const uint32_t split_size = std::min(nei1, 4096u / nei0);
  5207. ggml_tensor src1_copy = *src1;
  5208. ggml_tensor src2_copy = *src2;
  5209. ggml_tensor dst_copy = *dst;
  5210. for (uint32_t token_start = 0; token_start < nei1; token_start += split_size) {
  5211. const uint32_t n_tokens = std::min(split_size, nei1 - token_start);
  5212. src1_copy.view_offs = src1->view_offs + token_start * src1_copy.nb[2];
  5213. src2_copy.view_offs = src2->view_offs + token_start * src2_copy.nb[1];
  5214. dst_copy.view_offs = dst->view_offs + token_start * dst_copy.nb[2];
  5215. src1_copy.ne[2] = n_tokens;
  5216. src2_copy.ne[1] = n_tokens;
  5217. dst_copy.ne[2] = n_tokens;
  5218. ggml_vk_mul_mat_id_q_f16(ctx, subctx, src0, &src1_copy, &src2_copy, &dst_copy, dryrun);
  5219. }
  5220. }
  5221. }
  5222. static bool ggml_vk_flash_attn_scalar_shmem_support(const vk_device& device, const uint32_t hsk, uint32_t hsv) {
  5223. // Needs to be kept up to date on shader changes
  5224. GGML_UNUSED(hsv);
  5225. const uint32_t wg_size = scalar_flash_attention_workgroup_size;
  5226. const uint32_t Br = get_fa_scalar_num_large_rows(hsv);
  5227. const uint32_t Bc = scalar_flash_attention_Bc;
  5228. const uint32_t tmpsh = wg_size * sizeof(float);
  5229. const uint32_t tmpshv4 = wg_size * 4 * sizeof(float);
  5230. const uint32_t masksh = Bc * Br * sizeof(float);
  5231. const uint32_t Qf = Br * (hsk / 4 + 2) * 4 * sizeof(float);
  5232. const uint32_t total_size = tmpsh + tmpshv4 + masksh + Qf;
  5233. const bool supported = total_size <= device->properties.limits.maxComputeSharedMemorySize;
  5234. VK_LOG_DEBUG("ggml_vk_flash_attn_coopmat_shmem_support(HSK=" << hsk << ", HSV=" << hsv << ", total_size=" << total_size << ", supported=" << supported);
  5235. return supported;
  5236. }
  5237. static bool ggml_vk_flash_attn_coopmat_shmem_support(const vk_device& device, const uint32_t hsk, uint32_t hsv, bool f32acc) {
  5238. // Needs to be kept up to date on shader changes
  5239. GGML_UNUSED(hsv);
  5240. const uint32_t wg_size = scalar_flash_attention_workgroup_size;
  5241. const uint32_t Br = coopmat1_flash_attention_num_large_rows;
  5242. const uint32_t Bc = scalar_flash_attention_Bc;
  5243. const uint32_t acctype = f32acc ? 4 : 2;
  5244. const uint32_t f16vec4 = 8;
  5245. const uint32_t tmpsh = wg_size * sizeof(float);
  5246. const uint32_t tmpshv4 = wg_size * 4 * acctype;
  5247. const uint32_t Qf = Br * (hsk / 4 + 2) * f16vec4;
  5248. const uint32_t sfshstride = (hsk <= 128) ? (Br + 8) : Br;
  5249. const uint32_t sfsh = Bc * sfshstride * acctype;
  5250. const uint32_t kshstride = hsk / 4 + 2;
  5251. const uint32_t ksh = Bc * kshstride * f16vec4;
  5252. const uint32_t slope = Br * sizeof(float);
  5253. const uint32_t total_size = tmpsh + tmpshv4 + Qf + sfsh + ksh + slope;
  5254. const bool supported = total_size <= device->properties.limits.maxComputeSharedMemorySize;
  5255. VK_LOG_DEBUG("ggml_vk_flash_attn_coopmat_shmem_support(HSK=" << hsk << ", HSV=" << hsv << ", f32acc=" << f32acc << ", total_size=" << total_size << ", supported=" << supported);
  5256. return supported;
  5257. }
  5258. 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, ggml_tensor * dst, bool dryrun = false) {
  5259. 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];
  5260. 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];
  5261. 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];
  5262. 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];
  5263. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  5264. GGML_TENSOR_LOCALS(int64_t, neq, q, ne)
  5265. GGML_TENSOR_LOCALS(size_t, nbq, q, nb)
  5266. GGML_TENSOR_LOCALS(int64_t, nek, k, ne)
  5267. GGML_TENSOR_LOCALS(size_t, nbk, k, nb)
  5268. GGML_TENSOR_LOCALS(int64_t, nev, v, ne)
  5269. GGML_TENSOR_LOCALS(size_t, nbv, v, nb)
  5270. GGML_TENSOR_LOCALS(int64_t, ne, dst, ne)
  5271. GGML_TENSOR_LOCALS(size_t, nb, dst, nb)
  5272. const uint32_t nem1 = mask ? mask->ne[1] : 0;
  5273. const uint32_t nem2 = mask ? mask->ne[2] : 0;
  5274. const uint32_t nem3 = mask ? mask->ne[3] : 0;
  5275. const uint32_t HSK = nek0;
  5276. const uint32_t HSV = nev0;
  5277. uint32_t N = neq1;
  5278. const uint32_t KV = nek1;
  5279. GGML_ASSERT(ne0 == HSV);
  5280. GGML_ASSERT(ne2 == N);
  5281. // input tensor rows must be contiguous
  5282. GGML_ASSERT(nbq0 == ggml_type_size(q->type));
  5283. GGML_ASSERT(nbk0 == ggml_type_size(k->type));
  5284. GGML_ASSERT(nbv0 == ggml_type_size(v->type));
  5285. GGML_ASSERT(neq0 == HSK);
  5286. GGML_ASSERT(neq1 == N);
  5287. GGML_ASSERT(nev1 == nek1);
  5288. // dst cannot be transposed or permuted
  5289. GGML_ASSERT(nb0 == sizeof(float));
  5290. GGML_ASSERT(nb0 <= nb1);
  5291. GGML_ASSERT(nb1 <= nb2);
  5292. GGML_ASSERT(nb2 <= nb3);
  5293. assert(dst->type == GGML_TYPE_F32);
  5294. assert(q->type == GGML_TYPE_F32);
  5295. assert(k->type == v->type);
  5296. FaCodePath path = ctx->device->coopmat2 ? FA_COOPMAT2 :
  5297. ctx->device->coopmat1_fa_support ? FA_COOPMAT1 : FA_SCALAR;
  5298. if (path == FA_COOPMAT1) {
  5299. const bool coopmat_shape_supported = (dst->op_params[3] == GGML_PREC_F32 && ctx->device->coopmat_support_16x16x16_f32acc) ||
  5300. (dst->op_params[3] != GGML_PREC_F32 && ctx->device->coopmat_support_16x16x16_f16acc);
  5301. const bool coopmat_shmem_supported = ggml_vk_flash_attn_coopmat_shmem_support(ctx->device, HSK, HSV, dst->op_params[3] == GGML_PREC_F32);
  5302. if (!coopmat_shape_supported || !coopmat_shmem_supported) {
  5303. path = FA_SCALAR;
  5304. }
  5305. }
  5306. uint32_t gqa_ratio = 1;
  5307. uint32_t qk_ratio = neq2 / nek2;
  5308. uint32_t workgroups_x = (uint32_t)neq1;
  5309. uint32_t workgroups_y = (uint32_t)neq2;
  5310. uint32_t workgroups_z = (uint32_t)neq3;
  5311. // For scalar/coopmat1 FA, we can use the "large" size to accommodate qga.
  5312. // For coopmat2 FA, we always use the small size (which is still pretty large for gqa).
  5313. uint32_t max_gqa;
  5314. switch (path) {
  5315. case FA_SCALAR:
  5316. case FA_COOPMAT1:
  5317. // We may switch from coopmat1 to scalar, so use the scalar limit for both
  5318. max_gqa = get_fa_scalar_num_large_rows(HSV);
  5319. break;
  5320. case FA_COOPMAT2:
  5321. max_gqa = get_fa_num_small_rows(FA_COOPMAT2);
  5322. break;
  5323. default:
  5324. GGML_ASSERT(0);
  5325. }
  5326. if (N == 1 && qk_ratio > 1 && qk_ratio <= max_gqa &&
  5327. qk_ratio * nek2 == neq2 && nek2 == nev2 && nem2 <= 1) {
  5328. // grouped query attention - make the N dimension equal to gqa_ratio, reduce
  5329. // workgroups proportionally in y dimension. The shader will detect gqa_ratio > 1
  5330. // and change addressing calculations to index Q's dimension 2.
  5331. gqa_ratio = qk_ratio;
  5332. N = gqa_ratio;
  5333. workgroups_y /= N;
  5334. }
  5335. vk_pipeline *pipelines;
  5336. bool small_rows = N <= get_fa_num_small_rows(path);
  5337. // coopmat1 does not actually support "small rows" (it needs 16 rows).
  5338. // So use scalar instead.
  5339. if (small_rows && path == FA_COOPMAT1) {
  5340. path = FA_SCALAR;
  5341. }
  5342. // scalar is faster than coopmat2 when N==1
  5343. if (N == 1 && path == FA_COOPMAT2) {
  5344. path = FA_SCALAR;
  5345. }
  5346. // with large hsk/hsv, scalar path may need to use small_rows to fit in shared memory
  5347. if (path == FA_SCALAR &&
  5348. !ggml_vk_flash_attn_scalar_shmem_support(ctx->device, HSK, HSV)) {
  5349. small_rows = true;
  5350. }
  5351. bool f32acc = path == FA_SCALAR || dst->op_params[3] == GGML_PREC_F32;
  5352. FaHeadSizes head_sizes = fa_get_head_sizes(k->ne[0], v->ne[0]);
  5353. switch (path) {
  5354. case FA_SCALAR:
  5355. pipelines = &ctx->device->pipeline_flash_attn_f32_f16[k->type][head_sizes][f32acc][small_rows][0];
  5356. break;
  5357. case FA_COOPMAT1:
  5358. pipelines = &ctx->device->pipeline_flash_attn_f32_f16_cm1[k->type][head_sizes][f32acc][small_rows][0];
  5359. break;
  5360. case FA_COOPMAT2:
  5361. pipelines = &ctx->device->pipeline_flash_attn_f32_f16_cm2[k->type][head_sizes][f32acc][small_rows][0];
  5362. break;
  5363. default:
  5364. GGML_ASSERT(0);
  5365. }
  5366. assert(pipelines);
  5367. const uint32_t q_stride = (uint32_t)(nbq1 / ggml_type_size(q->type));
  5368. const uint32_t k_stride = (uint32_t)(nbk1 / ggml_type_size(k->type));
  5369. const uint32_t v_stride = (uint32_t)(nbv1 / ggml_type_size(v->type));
  5370. bool aligned = (KV % pipelines[1]->align) == 0 &&
  5371. // the "aligned" shader variant will forcibly align strides, for performance
  5372. (q_stride & 7) == 0 && (k_stride & 7) == 0 && (v_stride & 7) == 0;
  5373. // mask dim1 is padded to 64, we rely on this to avoid clamping mask loads
  5374. GGML_ASSERT((nem1 % GGML_KQ_MASK_PAD) == 0);
  5375. vk_pipeline pipeline = pipelines[aligned];
  5376. assert(pipeline);
  5377. uint32_t split_kv = KV;
  5378. uint32_t split_k = 1;
  5379. // Use a placeholder core count if one isn't available. split_k is a big help for perf.
  5380. const uint32_t shader_core_count = ctx->device->shader_core_count ? ctx->device->shader_core_count : 16;
  5381. // Try to use split_k when KV is large enough to be worth the overhead
  5382. if (workgroups_x == 1 && shader_core_count > 0) {
  5383. // Try to run two workgroups per SM.
  5384. split_k = shader_core_count * 2 / (workgroups_y * workgroups_z);
  5385. if (split_k > 1) {
  5386. // Try to evenly split KV into split_k chunks, but it needs to be a multiple
  5387. // of "align", so recompute split_k based on that.
  5388. split_kv = ROUNDUP_POW2(std::max(1u, KV / split_k), pipelines[1]->align);
  5389. split_k = CEIL_DIV(KV, split_kv);
  5390. workgroups_x = split_k;
  5391. }
  5392. }
  5393. // Reserve space for split_k temporaries. For each split x batch, we need to store the O matrix (D x ne1)
  5394. // and the per-row m and L values (ne1 rows). We store all the matrices first, followed by the rows.
  5395. const uint64_t split_k_size = split_k > 1 ? (HSV * ne1 * sizeof(float) + ne1 * sizeof(float) * 2) * split_k * ne3 : 0;
  5396. if (split_k_size > ctx->device->max_memory_allocation_size) {
  5397. GGML_ABORT("Requested preallocation size is too large");
  5398. }
  5399. if (ctx->prealloc_size_split_k < split_k_size) {
  5400. ctx->prealloc_size_split_k = split_k_size;
  5401. }
  5402. if (dryrun) {
  5403. // Request descriptor sets
  5404. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  5405. if (split_k > 1) {
  5406. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_flash_attn_split_k_reduce, 1);
  5407. }
  5408. return;
  5409. }
  5410. float scale = 1.0f;
  5411. float max_bias = 0.0f;
  5412. float logit_softcap = 0.0f;
  5413. memcpy(&scale, (const float *) dst->op_params + 0, sizeof(float));
  5414. memcpy(&max_bias, (const float *) dst->op_params + 1, sizeof(float));
  5415. memcpy(&logit_softcap, (const float *) dst->op_params + 2, sizeof(float));
  5416. if (logit_softcap != 0) {
  5417. scale /= logit_softcap;
  5418. }
  5419. const uint32_t n_head_kv = neq2;
  5420. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head_kv));
  5421. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  5422. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  5423. vk_buffer d_Q = nullptr, d_K = nullptr, d_V = nullptr, d_D = nullptr, d_M = nullptr;
  5424. size_t q_buf_offset = 0, k_buf_offset = 0, v_buf_offset = 0, d_buf_offset = 0, m_buf_offset = 0;
  5425. bool Q_uma = false, K_uma = false, V_uma = false, D_uma = false, M_uma = false;
  5426. if (ctx->device->uma) {
  5427. ggml_vk_host_get(ctx->device, q->data, d_Q, q_buf_offset);
  5428. ggml_vk_host_get(ctx->device, k->data, d_K, k_buf_offset);
  5429. ggml_vk_host_get(ctx->device, v->data, d_V, v_buf_offset);
  5430. ggml_vk_host_get(ctx->device, dst->data, d_D, d_buf_offset);
  5431. Q_uma = d_Q != nullptr;
  5432. K_uma = d_K != nullptr;
  5433. V_uma = d_V != nullptr;
  5434. D_uma = d_D != nullptr;
  5435. if (mask) {
  5436. ggml_vk_host_get(ctx->device, mask->data, d_M, m_buf_offset);
  5437. M_uma = d_M != nullptr;
  5438. }
  5439. }
  5440. ggml_backend_vk_buffer_context * d_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  5441. ggml_backend_vk_buffer_context * q_buf_ctx = (ggml_backend_vk_buffer_context *)q->buffer->context;
  5442. ggml_backend_vk_buffer_context * k_buf_ctx = (ggml_backend_vk_buffer_context *)k->buffer->context;
  5443. ggml_backend_vk_buffer_context * v_buf_ctx = (ggml_backend_vk_buffer_context *)v->buffer->context;
  5444. if (!Q_uma) {
  5445. d_Q = q_buf_ctx->dev_buffer;
  5446. q_buf_offset = vk_tensor_offset(q) + q->view_offs;
  5447. }
  5448. if (!K_uma) {
  5449. d_K = k_buf_ctx->dev_buffer;
  5450. k_buf_offset = vk_tensor_offset(k) + k->view_offs;
  5451. }
  5452. if (!V_uma) {
  5453. d_V = v_buf_ctx->dev_buffer;
  5454. v_buf_offset = vk_tensor_offset(v) + v->view_offs;
  5455. }
  5456. if (!D_uma) {
  5457. d_D = d_buf_ctx->dev_buffer;
  5458. d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  5459. }
  5460. if (!M_uma) {
  5461. d_M = d_Q;
  5462. m_buf_offset = q_buf_offset;
  5463. if (mask) {
  5464. ggml_backend_vk_buffer_context * m_buf_ctx = (ggml_backend_vk_buffer_context*)mask->buffer->context;
  5465. d_M = m_buf_ctx->dev_buffer;
  5466. m_buf_offset = vk_tensor_offset(mask) + mask->view_offs;
  5467. }
  5468. }
  5469. uint32_t mask_n_head_log2 = ((mask != nullptr) << 16) | n_head_log2;
  5470. const vk_flash_attn_push_constants pc = { N, KV,
  5471. (uint32_t)ne1, (uint32_t)ne2, (uint32_t)ne3,
  5472. (uint32_t)neq2, (uint32_t)neq3,
  5473. (uint32_t)nek2, (uint32_t)nek3,
  5474. (uint32_t)nev2, (uint32_t)nev3,
  5475. nem1, nem2, nem3,
  5476. q_stride, (uint32_t)nbq2, (uint32_t)nbq3,
  5477. k_stride, (uint32_t)nbk2, (uint32_t)nbk3,
  5478. v_stride, (uint32_t)nbv2, (uint32_t)nbv3,
  5479. scale, max_bias, logit_softcap,
  5480. mask_n_head_log2, m0, m1,
  5481. gqa_ratio, split_kv, split_k };
  5482. ggml_vk_sync_buffers(subctx);
  5483. if (split_k > 1) {
  5484. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline,
  5485. {
  5486. vk_subbuffer{d_Q, q_buf_offset, VK_WHOLE_SIZE},
  5487. vk_subbuffer{d_K, k_buf_offset, VK_WHOLE_SIZE},
  5488. vk_subbuffer{d_V, v_buf_offset, VK_WHOLE_SIZE},
  5489. vk_subbuffer{d_M, m_buf_offset, VK_WHOLE_SIZE},
  5490. vk_subbuffer{ctx->prealloc_split_k, 0, VK_WHOLE_SIZE},
  5491. },
  5492. // We only use split_k when group query attention is enabled, which means
  5493. // there's no more than one tile of rows (i.e. workgroups_x would have been
  5494. // one). We reuse workgroups_x to mean the number of splits, so we need to
  5495. // cancel out the divide by wg_denoms[0].
  5496. pc, { workgroups_x * pipeline->wg_denoms[0], workgroups_y, workgroups_z });
  5497. ggml_vk_sync_buffers(subctx);
  5498. const std::array<uint32_t, 4> pc2 = { HSV, (uint32_t)ne1, (uint32_t)ne3, split_k };
  5499. ggml_vk_dispatch_pipeline(ctx, subctx, ctx->device->pipeline_flash_attn_split_k_reduce,
  5500. {
  5501. vk_subbuffer{ctx->prealloc_split_k, 0, VK_WHOLE_SIZE},
  5502. vk_subbuffer{d_D, d_buf_offset, VK_WHOLE_SIZE},
  5503. },
  5504. pc2, { (uint32_t)ne1, HSV, (uint32_t)ne3 });
  5505. } else {
  5506. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline,
  5507. {
  5508. vk_subbuffer{d_Q, q_buf_offset, VK_WHOLE_SIZE},
  5509. vk_subbuffer{d_K, k_buf_offset, VK_WHOLE_SIZE},
  5510. vk_subbuffer{d_V, v_buf_offset, VK_WHOLE_SIZE},
  5511. vk_subbuffer{d_M, m_buf_offset, VK_WHOLE_SIZE},
  5512. vk_subbuffer{d_D, d_buf_offset, VK_WHOLE_SIZE},
  5513. },
  5514. pc, { workgroups_x, workgroups_y, workgroups_z });
  5515. }
  5516. }
  5517. 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, ggml_tensor * dst, ggml_op op) {
  5518. switch (op) {
  5519. case GGML_OP_GET_ROWS:
  5520. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  5521. if (dst->type == GGML_TYPE_F16) {
  5522. return ctx->device->pipeline_get_rows[src0->type];
  5523. }
  5524. if (dst->type == GGML_TYPE_F32) {
  5525. return ctx->device->pipeline_get_rows_f32[src0->type];
  5526. }
  5527. return nullptr;
  5528. case GGML_OP_ACC:
  5529. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5530. return ctx->device->pipeline_acc_f32;
  5531. }
  5532. return nullptr;
  5533. case GGML_OP_ADD:
  5534. case GGML_OP_SUB:
  5535. case GGML_OP_MUL:
  5536. case GGML_OP_DIV:
  5537. if ((src0->type != GGML_TYPE_F32 && src0->type != GGML_TYPE_F16) ||
  5538. (src1->type != GGML_TYPE_F32 && src1->type != GGML_TYPE_F16) ||
  5539. (dst->type != GGML_TYPE_F32 && dst->type != GGML_TYPE_F16)) {
  5540. return nullptr;
  5541. }
  5542. switch (op) {
  5543. case GGML_OP_ADD:
  5544. {
  5545. auto pipelines = ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_add_norepeat : ctx->device->pipeline_add;
  5546. return pipelines[src0->type == GGML_TYPE_F16][src1->type == GGML_TYPE_F16][dst->type == GGML_TYPE_F16];
  5547. }
  5548. case GGML_OP_SUB:
  5549. {
  5550. auto pipelines = ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_sub_norepeat : ctx->device->pipeline_sub;
  5551. return pipelines[src0->type == GGML_TYPE_F16][src1->type == GGML_TYPE_F16][dst->type == GGML_TYPE_F16];
  5552. }
  5553. case GGML_OP_MUL:
  5554. {
  5555. auto pipelines = ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_mul_norepeat : ctx->device->pipeline_mul;
  5556. return pipelines[src0->type == GGML_TYPE_F16][src1->type == GGML_TYPE_F16][dst->type == GGML_TYPE_F16];
  5557. }
  5558. case GGML_OP_DIV:
  5559. {
  5560. auto pipelines = ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_div_norepeat : ctx->device->pipeline_div;
  5561. return pipelines[src0->type == GGML_TYPE_F16][src1->type == GGML_TYPE_F16][dst->type == GGML_TYPE_F16];
  5562. }
  5563. default:
  5564. break;
  5565. }
  5566. return nullptr;
  5567. case GGML_OP_CONCAT:
  5568. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5569. return ctx->device->pipeline_concat_f32;
  5570. }
  5571. if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  5572. return ctx->device->pipeline_concat_f16;
  5573. }
  5574. if (src0->type == GGML_TYPE_I32 && src1->type == GGML_TYPE_I32 && dst->type == GGML_TYPE_I32) {
  5575. return ctx->device->pipeline_concat_i32;
  5576. }
  5577. return nullptr;
  5578. case GGML_OP_UPSCALE:
  5579. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5580. int mode = ggml_get_op_params_i32(dst, 0);
  5581. switch (mode) {
  5582. case GGML_SCALE_MODE_NEAREST:
  5583. return ctx->device->pipeline_upscale_nearest_f32;
  5584. case GGML_SCALE_MODE_BILINEAR:
  5585. return ctx->device->pipeline_upscale_bilinear_f32;
  5586. case GGML_SCALE_MODE_BILINEAR | GGML_SCALE_FLAG_ALIGN_CORNERS:
  5587. return ctx->device->pipeline_upscale_bilinear_ac_f32;
  5588. }
  5589. }
  5590. return nullptr;
  5591. case GGML_OP_SCALE:
  5592. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5593. return ctx->device->pipeline_scale_f32;
  5594. }
  5595. return nullptr;
  5596. case GGML_OP_SQR:
  5597. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5598. return ctx->device->pipeline_sqr_f32;
  5599. }
  5600. return nullptr;
  5601. case GGML_OP_SIN:
  5602. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5603. return ctx->device->pipeline_sin_f32;
  5604. }
  5605. return nullptr;
  5606. case GGML_OP_COS:
  5607. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5608. return ctx->device->pipeline_cos_f32;
  5609. }
  5610. return nullptr;
  5611. case GGML_OP_CLAMP:
  5612. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5613. return ctx->device->pipeline_clamp_f32;
  5614. }
  5615. return nullptr;
  5616. case GGML_OP_PAD:
  5617. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5618. return ctx->device->pipeline_pad_f32;
  5619. }
  5620. return nullptr;
  5621. case GGML_OP_ROLL:
  5622. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5623. return ctx->device->pipeline_roll_f32;
  5624. }
  5625. return nullptr;
  5626. case GGML_OP_REPEAT:
  5627. if (ggml_type_size(src0->type) == sizeof(float) && ggml_type_size(dst->type) == sizeof(float)) {
  5628. return ctx->device->pipeline_repeat_f32;
  5629. }
  5630. return nullptr;
  5631. case GGML_OP_REPEAT_BACK:
  5632. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5633. return ctx->device->pipeline_repeat_back_f32;
  5634. }
  5635. return nullptr;
  5636. case GGML_OP_CPY:
  5637. case GGML_OP_CONT:
  5638. case GGML_OP_DUP:
  5639. return ggml_vk_get_cpy_pipeline(ctx, src0, dst, dst->type);
  5640. case GGML_OP_SET_ROWS:
  5641. return ctx->device->pipeline_set_rows[dst->type];
  5642. case GGML_OP_SILU_BACK:
  5643. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5644. return ctx->device->pipeline_silu_back_f32;
  5645. }
  5646. return nullptr;
  5647. case GGML_OP_NORM:
  5648. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5649. return ctx->device->pipeline_norm_f32;
  5650. }
  5651. return nullptr;
  5652. case GGML_OP_GROUP_NORM:
  5653. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5654. return ctx->device->pipeline_group_norm_f32;
  5655. }
  5656. return nullptr;
  5657. case GGML_OP_RMS_NORM:
  5658. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5659. return ctx->num_additional_fused_ops > 0 ? ctx->device->pipeline_rms_norm_mul_f32 : ctx->device->pipeline_rms_norm_f32;
  5660. }
  5661. return nullptr;
  5662. case GGML_OP_RMS_NORM_BACK:
  5663. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5664. return ctx->device->pipeline_rms_norm_back_f32;
  5665. }
  5666. return nullptr;
  5667. case GGML_OP_L2_NORM:
  5668. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5669. return ctx->device->pipeline_l2_norm_f32;
  5670. }
  5671. return nullptr;
  5672. case GGML_OP_UNARY:
  5673. if ((src0->type != GGML_TYPE_F32 && src0->type != GGML_TYPE_F16) ||
  5674. (dst->type != GGML_TYPE_F32 && dst->type != GGML_TYPE_F16) ||
  5675. (src0->type != dst->type)) {
  5676. return nullptr;
  5677. }
  5678. switch (ggml_get_unary_op(dst)) {
  5679. case GGML_UNARY_OP_SILU:
  5680. return ctx->device->pipeline_silu[dst->type == GGML_TYPE_F16];
  5681. case GGML_UNARY_OP_GELU:
  5682. return ctx->device->pipeline_gelu[dst->type == GGML_TYPE_F16];
  5683. case GGML_UNARY_OP_GELU_ERF:
  5684. return ctx->device->pipeline_gelu_erf[dst->type == GGML_TYPE_F16];
  5685. case GGML_UNARY_OP_GELU_QUICK:
  5686. return ctx->device->pipeline_gelu_quick[dst->type == GGML_TYPE_F16];
  5687. case GGML_UNARY_OP_RELU:
  5688. return ctx->device->pipeline_relu[dst->type == GGML_TYPE_F16];
  5689. case GGML_UNARY_OP_TANH:
  5690. return ctx->device->pipeline_tanh[dst->type == GGML_TYPE_F16];
  5691. case GGML_UNARY_OP_SIGMOID:
  5692. return ctx->device->pipeline_sigmoid[dst->type == GGML_TYPE_F16];
  5693. default:
  5694. break;
  5695. }
  5696. return nullptr;
  5697. case GGML_OP_GLU:
  5698. if ((src0->type != GGML_TYPE_F32 && src0->type != GGML_TYPE_F16) ||
  5699. (dst->type != GGML_TYPE_F32 && dst->type != GGML_TYPE_F16) ||
  5700. (src0->type != dst->type)) {
  5701. return nullptr;
  5702. }
  5703. switch (ggml_get_glu_op(dst)) {
  5704. case GGML_GLU_OP_GEGLU:
  5705. return ctx->device->pipeline_geglu[dst->type == GGML_TYPE_F16];
  5706. case GGML_GLU_OP_REGLU:
  5707. return ctx->device->pipeline_reglu[dst->type == GGML_TYPE_F16];
  5708. case GGML_GLU_OP_SWIGLU:
  5709. return ctx->device->pipeline_swiglu[dst->type == GGML_TYPE_F16];
  5710. case GGML_GLU_OP_GEGLU_ERF:
  5711. return ctx->device->pipeline_geglu_erf[dst->type == GGML_TYPE_F16];
  5712. case GGML_GLU_OP_GEGLU_QUICK:
  5713. return ctx->device->pipeline_geglu_quick[dst->type == GGML_TYPE_F16];
  5714. default:
  5715. break;
  5716. }
  5717. return nullptr;
  5718. case GGML_OP_DIAG_MASK_INF:
  5719. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5720. return ctx->device->pipeline_diag_mask_inf_f32;
  5721. }
  5722. return nullptr;
  5723. case GGML_OP_SOFT_MAX:
  5724. GGML_ASSERT(!src1 || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16);
  5725. if (src0->type == GGML_TYPE_F32 && (src1 == nullptr || src1->type == GGML_TYPE_F32) && dst->type == GGML_TYPE_F32) {
  5726. return src0->ne[0] > 1024 ? ctx->device->pipeline_soft_max_f32_wg512 : ctx->device->pipeline_soft_max_f32;
  5727. }
  5728. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  5729. return src0->ne[0] > 1024 ? ctx->device->pipeline_soft_max_f32_f16_wg512 : ctx->device->pipeline_soft_max_f32_f16;
  5730. }
  5731. return nullptr;
  5732. case GGML_OP_SOFT_MAX_BACK:
  5733. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5734. return ctx->device->pipeline_soft_max_back_f32;
  5735. }
  5736. return nullptr;
  5737. case GGML_OP_ROPE:
  5738. case GGML_OP_ROPE_BACK:
  5739. {
  5740. const int mode = ((const int32_t *) dst->op_params)[2];
  5741. const bool is_neox = mode & GGML_ROPE_TYPE_NEOX;
  5742. const bool is_mrope = mode & GGML_ROPE_TYPE_MROPE;
  5743. const bool is_vision = mode == GGML_ROPE_TYPE_VISION;
  5744. if (is_neox) {
  5745. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5746. return ctx->device->pipeline_rope_neox_f32;
  5747. }
  5748. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  5749. return ctx->device->pipeline_rope_neox_f16;
  5750. }
  5751. } else if (is_mrope && !is_vision) {
  5752. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5753. return ctx->device->pipeline_rope_multi_f32;
  5754. }
  5755. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  5756. return ctx->device->pipeline_rope_multi_f16;
  5757. }
  5758. } else if (is_vision) {
  5759. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5760. return ctx->device->pipeline_rope_vision_f32;
  5761. }
  5762. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  5763. return ctx->device->pipeline_rope_vision_f16;
  5764. }
  5765. } else {
  5766. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5767. return ctx->device->pipeline_rope_norm_f32;
  5768. }
  5769. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  5770. return ctx->device->pipeline_rope_norm_f16;
  5771. }
  5772. }
  5773. return nullptr;
  5774. }
  5775. case GGML_OP_ARGSORT:
  5776. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_I32) {
  5777. return ctx->device->pipeline_argsort_f32;
  5778. }
  5779. return nullptr;
  5780. case GGML_OP_SUM:
  5781. case GGML_OP_SUM_ROWS:
  5782. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5783. return ctx->device->pipeline_sum_rows_f32;
  5784. }
  5785. return nullptr;
  5786. case GGML_OP_ARGMAX:
  5787. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_I32) {
  5788. return ctx->device->pipeline_argmax_f32;
  5789. }
  5790. return nullptr;
  5791. case GGML_OP_COUNT_EQUAL:
  5792. if (src0->type == GGML_TYPE_I32 && src1->type == GGML_TYPE_I32 && dst->type == GGML_TYPE_I64) {
  5793. return ctx->device->pipeline_count_equal_i32;
  5794. }
  5795. return nullptr;
  5796. case GGML_OP_IM2COL:
  5797. if (src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5798. return ctx->device->pipeline_im2col_f32;
  5799. }
  5800. if (src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F16) {
  5801. return ctx->device->pipeline_im2col_f32_f16;
  5802. }
  5803. return nullptr;
  5804. case GGML_OP_TIMESTEP_EMBEDDING:
  5805. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5806. return ctx->device->pipeline_timestep_embedding_f32;
  5807. }
  5808. return nullptr;
  5809. case GGML_OP_CONV_TRANSPOSE_1D:
  5810. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5811. return ctx->device->pipeline_conv_transpose_1d_f32;
  5812. }
  5813. return nullptr;
  5814. case GGML_OP_POOL_2D:
  5815. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5816. return ctx->device->pipeline_pool2d_f32;
  5817. }
  5818. return nullptr;
  5819. case GGML_OP_RWKV_WKV6:
  5820. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5821. return ctx->device->pipeline_rwkv_wkv6_f32;
  5822. }
  5823. return nullptr;
  5824. case GGML_OP_RWKV_WKV7:
  5825. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5826. return ctx->device->pipeline_rwkv_wkv7_f32;
  5827. }
  5828. return nullptr;
  5829. case GGML_OP_OPT_STEP_ADAMW:
  5830. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5831. return ctx->device->pipeline_opt_step_adamw_f32;
  5832. }
  5833. return nullptr;
  5834. case GGML_OP_LEAKY_RELU:
  5835. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5836. return ctx->device->pipeline_leaky_relu_f32;
  5837. }
  5838. return nullptr;
  5839. case GGML_OP_CONV_2D_DW:
  5840. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5841. if (ggml_is_contiguous(src1)) {
  5842. return ctx->device->pipeline_conv2d_dw_whcn_f32;
  5843. } else if (ggml_is_contiguous_channels(src1)) {
  5844. return ctx->device->pipeline_conv2d_dw_cwhn_f32;
  5845. }
  5846. }
  5847. return nullptr;
  5848. default:
  5849. return nullptr;
  5850. }
  5851. GGML_UNUSED(src2);
  5852. }
  5853. static bool ggml_vk_op_supports_incontiguous(ggml_op op) {
  5854. switch (op) {
  5855. case GGML_OP_CPY:
  5856. case GGML_OP_GET_ROWS:
  5857. case GGML_OP_ADD:
  5858. case GGML_OP_SUB:
  5859. case GGML_OP_MUL:
  5860. case GGML_OP_DIV:
  5861. case GGML_OP_CONCAT:
  5862. case GGML_OP_UPSCALE:
  5863. case GGML_OP_SQR:
  5864. case GGML_OP_SIN:
  5865. case GGML_OP_COS:
  5866. case GGML_OP_CLAMP:
  5867. case GGML_OP_PAD:
  5868. case GGML_OP_REPEAT:
  5869. case GGML_OP_REPEAT_BACK:
  5870. case GGML_OP_ROPE:
  5871. case GGML_OP_RMS_NORM:
  5872. case GGML_OP_CONV_2D_DW:
  5873. case GGML_OP_IM2COL:
  5874. case GGML_OP_SET_ROWS:
  5875. return true;
  5876. default:
  5877. return false;
  5878. }
  5879. }
  5880. static uint32_t get_misalign_bytes(ggml_backend_vk_context * ctx, const ggml_tensor * t)
  5881. {
  5882. return ((vk_tensor_offset(t) + t->view_offs) & (ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1));;
  5883. }
  5884. 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) {
  5885. GGML_UNUSED(p);
  5886. GGML_UNUSED(src0);
  5887. GGML_UNUSED(src1);
  5888. GGML_UNUSED(src2);
  5889. GGML_UNUSED(dst);
  5890. static_assert(!std::is_const<T>::value, "unexpected type");
  5891. GGML_ASSERT(!src0 || get_misalign_bytes(ctx, src0) == 0);
  5892. GGML_ASSERT(!src1 || get_misalign_bytes(ctx, src1) == 0);
  5893. GGML_ASSERT(!src2 || get_misalign_bytes(ctx, src2) == 0);
  5894. GGML_ASSERT(!dst || get_misalign_bytes(ctx, dst) == 0);
  5895. }
  5896. 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) {
  5897. const uint32_t a_offset = get_misalign_bytes(ctx, src0) / ggml_type_size(src0->type);
  5898. const uint32_t d_offset = get_misalign_bytes(ctx, dst) / ggml_type_size(dst->type);
  5899. p.misalign_offsets = (a_offset << 16) | d_offset;
  5900. GGML_UNUSED(src1);
  5901. GGML_UNUSED(src2);
  5902. }
  5903. 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) {
  5904. const uint32_t a_offset = get_misalign_bytes(ctx, src0) / ggml_type_size(src0->type);
  5905. const uint32_t b_offset = get_misalign_bytes(ctx, src1) / ggml_type_size(src1->type);
  5906. const uint32_t d_offset = get_misalign_bytes(ctx, dst) / ggml_type_size(dst->type);
  5907. GGML_ASSERT(dst->op != GGML_OP_GET_ROWS || (a_offset == 0 && b_offset == 0 && d_offset == 0));
  5908. p.misalign_offsets = (a_offset << 16) | (b_offset << 8) | d_offset;
  5909. GGML_UNUSED(src2);
  5910. }
  5911. 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) {
  5912. const uint32_t a_offset = get_misalign_bytes(ctx, src0) / ggml_type_size(src0->type);
  5913. const uint32_t d_offset = get_misalign_bytes(ctx, dst) / ggml_type_size(dst->type);
  5914. p.a_offset = a_offset;
  5915. p.d_offset = d_offset;
  5916. GGML_UNUSED(src1);
  5917. GGML_UNUSED(src2);
  5918. }
  5919. template<typename PC>
  5920. 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) {
  5921. 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];
  5922. if (src1 != nullptr) {
  5923. 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];
  5924. }
  5925. if (src2 != nullptr) {
  5926. 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];
  5927. }
  5928. 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];
  5929. std::cerr << "), " << ggml_op_name(op) << ", " << (dryrun ? "dryrun" : "") << ")");
  5930. GGML_ASSERT(op == GGML_OP_GET_ROWS || op == GGML_OP_CPY || (!ggml_is_quantized(src0->type) && (src1 == nullptr || !ggml_is_quantized(src1->type)))); // NOLINT
  5931. GGML_ASSERT(ggml_vk_op_supports_incontiguous(op) || ggml_vk_dim01_contiguous(src0)); // NOLINT
  5932. GGML_ASSERT(dst->buffer != nullptr);
  5933. const uint64_t ne00 = src0->ne[0];
  5934. const uint64_t ne01 = src0->ne[1];
  5935. const uint64_t ne02 = src0->ne[2];
  5936. const uint64_t ne03 = src0->ne[3];
  5937. const uint64_t ne0 = ne00 * ne01;
  5938. const bool use_src1 = src1 != nullptr;
  5939. const uint64_t ne10 = use_src1 ? src1->ne[0] : 0;
  5940. const uint64_t ne11 = use_src1 ? src1->ne[1] : 0;
  5941. const uint64_t ne12 = use_src1 ? src1->ne[2] : 0;
  5942. const uint64_t ne13 = use_src1 ? src1->ne[3] : 0;
  5943. const uint64_t ne1 = ne10 * ne11;
  5944. // const uint64_t nb10 = use_src1 ? src1->nb[0] : 0;
  5945. const bool use_src2 = src2 != nullptr;
  5946. const uint64_t ne20 = use_src2 ? src2->ne[0] : 0;
  5947. const uint64_t ne21 = use_src2 ? src2->ne[1] : 0;
  5948. const uint64_t ne22 = use_src2 ? src2->ne[2] : 0;
  5949. const uint64_t ne23 = use_src2 ? src2->ne[3] : 0;
  5950. const uint64_t ne2 = ne20 * ne21;
  5951. const uint64_t ned0 = dst->ne[0];
  5952. const uint64_t ned1 = dst->ne[1];
  5953. const uint64_t ned2 = dst->ne[2];
  5954. const uint64_t ned3 = dst->ne[3];
  5955. const uint64_t ned = ned0 * ned1;
  5956. init_pushconst_fastdiv(pc);
  5957. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, src0, src1, src2, dst, op);
  5958. if (pipeline == nullptr) {
  5959. std::cerr << "ggml_vulkan: Error: Missing op: " << ggml_op_name(op) << " for " << ggml_type_name(src0->type);
  5960. if (src1 != nullptr) {
  5961. std::cerr << " and " << ggml_type_name(src1->type);
  5962. }
  5963. std::cerr << " to " << ggml_type_name(dst->type) << std::endl;
  5964. GGML_ABORT("fatal error");
  5965. }
  5966. if (dryrun) {
  5967. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  5968. return;
  5969. }
  5970. const bool op_supports_incontiguous = ggml_vk_op_supports_incontiguous(op);
  5971. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  5972. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  5973. ggml_backend_vk_buffer_context * src1_buf_ctx = use_src1 ? (ggml_backend_vk_buffer_context *)src1->buffer->context : nullptr;
  5974. ggml_backend_vk_buffer_context * src2_buf_ctx = use_src2 ? (ggml_backend_vk_buffer_context *)src2->buffer->context : nullptr;
  5975. vk_buffer d_X = nullptr;
  5976. size_t x_buf_offset = 0;
  5977. vk_buffer d_Y = nullptr;
  5978. size_t y_buf_offset = 0;
  5979. vk_buffer d_Z = nullptr;
  5980. size_t z_buf_offset = 0;
  5981. bool src0_uma = false;
  5982. bool src1_uma = false;
  5983. bool src2_uma = false;
  5984. if (ctx->device->uma) {
  5985. ggml_vk_host_get(ctx->device, src0->data, d_X, x_buf_offset);
  5986. src0_uma = d_X != nullptr;
  5987. if (use_src1) {
  5988. ggml_vk_host_get(ctx->device, src1->data, d_Y, y_buf_offset);
  5989. src1_uma = d_Y != nullptr;
  5990. }
  5991. if (use_src2) {
  5992. ggml_vk_host_get(ctx->device, src2->data, d_Z, z_buf_offset);
  5993. src2_uma = d_Z != nullptr;
  5994. }
  5995. }
  5996. uint64_t x_sz = ggml_type_size(src0->type)/ggml_blck_size(src0->type) * ne0;
  5997. uint64_t y_sz = use_src1 ? ggml_type_size(src1->type) * ne1 : 0;
  5998. uint64_t z_sz = use_src2 ? ggml_type_size(src2->type) * ne2 : 0;
  5999. uint64_t d_sz = ggml_type_size(dst->type) * ned;
  6000. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  6001. // Workaround for tiny tensor inputs on ROPE
  6002. if (op == GGML_OP_ROPE && use_src1 && y_sz > d_D->size) {
  6003. y_sz = VK_WHOLE_SIZE;
  6004. }
  6005. GGML_ASSERT(d_D != nullptr);
  6006. uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  6007. if(!src0_uma) {
  6008. d_X = src0_buf_ctx->dev_buffer;
  6009. x_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  6010. GGML_ASSERT(d_X != nullptr);
  6011. }
  6012. if (use_src1 && !src1_uma) {
  6013. d_Y = src1_buf_ctx->dev_buffer;
  6014. y_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  6015. GGML_ASSERT(d_Y != nullptr);
  6016. }
  6017. if (use_src2 && !src2_uma) {
  6018. d_Z = src2_buf_ctx->dev_buffer;
  6019. z_buf_offset = vk_tensor_offset(src2) + src2->view_offs;
  6020. GGML_ASSERT(d_Z != nullptr);
  6021. }
  6022. // Compute misalignment offset for descriptors and store it in in push constants, then align the descriptor offsets.
  6023. init_pushconst_tensor_offsets(ctx, pc, src0, src1, src2, dst);
  6024. x_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  6025. y_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  6026. z_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  6027. d_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  6028. if (op_supports_incontiguous) {
  6029. x_sz = ggml_nbytes(src0);
  6030. y_sz = use_src1 ? ggml_nbytes(src1) : 0;
  6031. z_sz = use_src2 ? ggml_nbytes(src2) : 0;
  6032. d_sz = ggml_nbytes(dst);
  6033. if (x_buf_offset + x_sz >= d_X->size) {
  6034. x_sz = VK_WHOLE_SIZE;
  6035. }
  6036. if (use_src1 && y_buf_offset + y_sz >= d_Y->size) {
  6037. y_sz = VK_WHOLE_SIZE;
  6038. }
  6039. if (use_src2 && z_buf_offset + z_sz >= d_Z->size) {
  6040. z_sz = VK_WHOLE_SIZE;
  6041. }
  6042. if (d_buf_offset + d_sz >= d_D->size) {
  6043. d_sz = VK_WHOLE_SIZE;
  6044. }
  6045. }
  6046. std::array<uint32_t, 3> elements;
  6047. // Single call if dimension 2 is contiguous
  6048. GGML_ASSERT(op_supports_incontiguous || (ggml_is_contiguous(src0) && (src1 == nullptr || ggml_is_contiguous(src1))));
  6049. switch (op) {
  6050. case GGML_OP_NORM:
  6051. case GGML_OP_RMS_NORM_BACK:
  6052. case GGML_OP_L2_NORM:
  6053. case GGML_OP_SOFT_MAX:
  6054. case GGML_OP_SOFT_MAX_BACK:
  6055. case GGML_OP_SUM_ROWS:
  6056. case GGML_OP_ARGMAX:
  6057. {
  6058. const uint32_t nr = ggml_nrows(src0);
  6059. if (nr > 262144) {
  6060. elements = { 512, 512, CEIL_DIV(nr, 262144) };
  6061. } else if (nr > 512) {
  6062. elements = { 512, CEIL_DIV(nr, 512), 1 };
  6063. } else {
  6064. elements = { nr, 1, 1 };
  6065. }
  6066. } break;
  6067. case GGML_OP_RMS_NORM:
  6068. elements = { (uint32_t)ne01, (uint32_t)ne02, (uint32_t)ne03 };
  6069. break;
  6070. case GGML_OP_SUM:
  6071. // We use GGML_OP_SUM_ROWS with 1 row.
  6072. elements = { 1, 1, 1 };
  6073. break;
  6074. case GGML_OP_GROUP_NORM:
  6075. {
  6076. const uint32_t num_groups = dst->op_params[0];
  6077. elements = { num_groups * (uint32_t)src0->ne[3], 1, 1 };
  6078. } break;
  6079. case GGML_OP_DIAG_MASK_INF:
  6080. case GGML_OP_ROPE:
  6081. case GGML_OP_ROPE_BACK:
  6082. elements = { (uint32_t)ggml_nrows(src0), (uint32_t)ne00, 1 };
  6083. break;
  6084. case GGML_OP_GET_ROWS:
  6085. elements = { (uint32_t)ne00, (uint32_t)ne10, (uint32_t)(ne11 * ne12) };
  6086. break;
  6087. case GGML_OP_ARGSORT:
  6088. elements = { (uint32_t)ne00, (uint32_t)ggml_nrows(src0), 1 };
  6089. break;
  6090. case GGML_OP_IM2COL:
  6091. {
  6092. const bool is_2D = dst->op_params[6] == 1;
  6093. const uint32_t IC = src1->ne[is_2D ? 2 : 1];
  6094. const uint32_t KH = is_2D ? src0->ne[1] : 1;
  6095. const uint32_t KW = src0->ne[0];
  6096. const uint32_t OH = is_2D ? dst->ne[2] : 1;
  6097. const uint32_t OW = dst->ne[1];
  6098. const uint32_t batch = src1->ne[is_2D ? 3 : 2];
  6099. elements = { OW * KW * KH, OH, batch * IC };
  6100. } break;
  6101. case GGML_OP_TIMESTEP_EMBEDDING:
  6102. {
  6103. const uint32_t dim = dst->op_params[0];
  6104. uint32_t half_ceil = (dim + 1) / 2;
  6105. elements = { half_ceil, (uint32_t)src0->ne[0], 1 };
  6106. } break;
  6107. case GGML_OP_CONV_TRANSPOSE_1D:
  6108. {
  6109. elements = {uint32_t(src0->ne[1]), 1, 1}; // parallelize in {Cout, 1, 1}
  6110. } break;
  6111. case GGML_OP_POOL_2D:
  6112. {
  6113. const uint32_t N = dst->ne[3];
  6114. const uint32_t OC = dst->ne[2];
  6115. const uint32_t OH = dst->ne[1];
  6116. const uint32_t OW = dst->ne[0];
  6117. elements = { N * OC * OH * OW, 1, 1};
  6118. } break;
  6119. case GGML_OP_ADD:
  6120. case GGML_OP_SUB:
  6121. case GGML_OP_DIV:
  6122. case GGML_OP_MUL:
  6123. case GGML_OP_SCALE:
  6124. case GGML_OP_SQR:
  6125. case GGML_OP_SIN:
  6126. case GGML_OP_COS:
  6127. case GGML_OP_CLAMP:
  6128. case GGML_OP_PAD:
  6129. case GGML_OP_ROLL:
  6130. case GGML_OP_REPEAT:
  6131. case GGML_OP_REPEAT_BACK:
  6132. case GGML_OP_CPY:
  6133. case GGML_OP_CONCAT:
  6134. case GGML_OP_UPSCALE:
  6135. case GGML_OP_UNARY:
  6136. case GGML_OP_GLU:
  6137. case GGML_OP_CONV_2D_DW:
  6138. {
  6139. uint32_t ne = ggml_nelements(dst);
  6140. if (op == GGML_OP_CPY && ggml_is_quantized(src0->type) && ggml_is_quantized(dst->type)) {
  6141. // Convert from number of logical elements to 2- or 4-byte units.
  6142. ne /= ggml_blck_size(src0->type);
  6143. if ((ggml_type_size(src0->type) % 4) == 0) {
  6144. ne *= ggml_type_size(src0->type) / 4;
  6145. } else {
  6146. ne *= ggml_type_size(src0->type) / 2;
  6147. }
  6148. }
  6149. // copy_to_quant has block size of 32, and each thread does QUANT_K elements.
  6150. // Splitting into 512x512xZ wouldn't work well since each workgroup does 1024 elements.
  6151. // So divide by block size here before splitting into 512x512 groups.
  6152. if (op == GGML_OP_CPY && !ggml_is_quantized(src0->type) && ggml_is_quantized(dst->type)) {
  6153. ne = CEIL_DIV(ne, ggml_blck_size(dst->type));
  6154. }
  6155. if (ne > 262144) {
  6156. elements = { 512, 512, CEIL_DIV(ne, 262144) };
  6157. } else if (ne > 512) {
  6158. elements = { 512, CEIL_DIV(ne, 512), 1 };
  6159. } else {
  6160. elements = { ne, 1, 1 };
  6161. }
  6162. } break;
  6163. case GGML_OP_SET_ROWS:
  6164. {
  6165. uint32_t ne = ggml_nelements(src0);
  6166. if (ggml_is_quantized(dst->type)) {
  6167. // quants run 32 threads each doing QUANT_K elements
  6168. ne = CEIL_DIV(ne, 32 * ggml_blck_size(dst->type));
  6169. } else {
  6170. // scalar types do one element per thread, running 512 threads
  6171. ne = CEIL_DIV(ne, 512);
  6172. }
  6173. if (ne > 262144) {
  6174. elements = { 512, 512, CEIL_DIV(ne, 262144) };
  6175. } else if (ne > 512) {
  6176. elements = { 512, CEIL_DIV(ne, 512), 1 };
  6177. } else {
  6178. elements = { ne, 1, 1 };
  6179. }
  6180. }
  6181. break;
  6182. default:
  6183. elements = { (uint32_t)ggml_nelements(src0), 1, 1 };
  6184. break;
  6185. }
  6186. if (!op_supports_incontiguous) {
  6187. if (x_sz != VK_WHOLE_SIZE) {
  6188. x_sz *= ne02 * ne03;
  6189. }
  6190. if (use_src1 && y_sz != VK_WHOLE_SIZE) {
  6191. y_sz *= ne12 * ne13;
  6192. }
  6193. if (use_src2 && z_sz != VK_WHOLE_SIZE) {
  6194. z_sz *= ne22 * ne23;
  6195. }
  6196. if (d_sz != VK_WHOLE_SIZE) {
  6197. d_sz *= ned2 * ned3;
  6198. }
  6199. }
  6200. if (op == GGML_OP_SOFT_MAX || op == GGML_OP_GLU) {
  6201. // Empty src1 is possible in soft_max, but the shader needs a buffer
  6202. vk_subbuffer subbuf_y;
  6203. if (use_src1) {
  6204. subbuf_y = { d_Y, y_buf_offset, y_sz };
  6205. } else {
  6206. subbuf_y = { d_X, 0, x_sz };
  6207. }
  6208. ggml_vk_sync_buffers(subctx);
  6209. 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);
  6210. } else if (op == GGML_OP_ROPE || op == GGML_OP_ROPE_BACK) {
  6211. // Empty src2 is possible in rope, but the shader needs a buffer
  6212. vk_subbuffer subbuf_z;
  6213. if (use_src2) {
  6214. subbuf_z = { d_Z, z_buf_offset, z_sz };
  6215. } else {
  6216. subbuf_z = { d_X, 0, x_sz };
  6217. }
  6218. ggml_vk_sync_buffers(subctx);
  6219. 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);
  6220. } else if (op == GGML_OP_IM2COL) {
  6221. // im2col uses only src1 and dst buffers
  6222. ggml_vk_sync_buffers(subctx);
  6223. 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);
  6224. } else if (op == GGML_OP_COUNT_EQUAL) {
  6225. ggml_vk_sync_buffers(subctx);
  6226. // count_equal assumes that destination buffer is initialized with zeroes
  6227. ggml_vk_buffer_memset_async(subctx, d_D, d_buf_offset, 0, d_sz);
  6228. ggml_vk_sync_buffers(subctx);
  6229. 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);
  6230. } else if (use_src2) {
  6231. ggml_vk_sync_buffers(subctx);
  6232. 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);
  6233. } else if (use_src1) {
  6234. ggml_vk_sync_buffers(subctx);
  6235. 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);
  6236. } else {
  6237. ggml_vk_sync_buffers(subctx);
  6238. 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);
  6239. }
  6240. }
  6241. 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) {
  6242. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6243. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6244. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6245. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_GET_ROWS, {
  6246. (uint32_t)ggml_nelements(src0),
  6247. (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,
  6248. (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,
  6249. (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,
  6250. 0,
  6251. 0.0f, 0.0f, 0,
  6252. }, dryrun);
  6253. }
  6254. 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) {
  6255. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6256. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6257. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6258. int nb1 = dst->op_params[0] / 4; // 4 bytes of float32
  6259. int nb2 = dst->op_params[1] / 4; // 4 bytes of float32
  6260. // int nb3 = dst->op_params[2] / 4; // 4 bytes of float32 - unused
  6261. int offset = dst->op_params[3] / 4; // offset in bytes
  6262. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_ACC, {
  6263. (uint32_t)ggml_nelements(src0),
  6264. (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,
  6265. (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,
  6266. (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,
  6267. 0,
  6268. 0.0f, 0.0f, offset,
  6269. }, dryrun);
  6270. }
  6271. 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) {
  6272. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6273. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6274. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6275. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_ADD, {
  6276. (uint32_t)ggml_nelements(src0),
  6277. (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,
  6278. (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,
  6279. (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,
  6280. 0,
  6281. 0.0f, 0.0f, 0,
  6282. }, dryrun);
  6283. }
  6284. 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) {
  6285. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6286. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6287. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6288. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_SUB, {
  6289. (uint32_t)ggml_nelements(src0),
  6290. (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,
  6291. (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,
  6292. (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,
  6293. 0,
  6294. 0.0f, 0.0f, 0,
  6295. }, dryrun);
  6296. }
  6297. 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) {
  6298. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6299. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6300. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6301. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_MUL, {
  6302. (uint32_t)ggml_nelements(src0),
  6303. (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,
  6304. (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,
  6305. (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,
  6306. 0,
  6307. 0.0f, 0.0f, 0,
  6308. }, dryrun);
  6309. }
  6310. 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) {
  6311. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6312. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6313. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6314. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_DIV, {
  6315. (uint32_t)ggml_nelements(src0),
  6316. (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,
  6317. (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,
  6318. (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,
  6319. 0,
  6320. 0.0f, 0.0f, 0,
  6321. }, dryrun);
  6322. }
  6323. 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) {
  6324. GGML_ASSERT(version == 6 || version == 7);
  6325. int num_srcs = version == 6 ? 6 : 7;
  6326. for (int i = 0; i < num_srcs; i++) {
  6327. GGML_ASSERT(!ggml_is_quantized(dst->src[i]->type));
  6328. }
  6329. GGML_ASSERT(dst->buffer != nullptr);
  6330. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, dst->src[0], dst->src[1], dst->src[2], dst, dst->op);
  6331. GGML_ASSERT(pipeline != nullptr);
  6332. if (dryrun) {
  6333. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  6334. return;
  6335. }
  6336. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  6337. ggml_backend_vk_buffer_context * src_buf_ctxs[7] = { nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr };
  6338. for (int i = 0; i < num_srcs; i++) {
  6339. src_buf_ctxs[i] = (ggml_backend_vk_buffer_context *)dst->src[i]->buffer->context;
  6340. }
  6341. ggml_vk_sync_buffers(subctx);
  6342. vk_buffer d_D = nullptr, d_srcs[7] = { nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr };
  6343. size_t dst_offset = 0, src_offsets[7] = { 0, 0, 0, 0, 0, 0, 0 };
  6344. bool dst_uma = false, srcs_uma[7] = { false, false, false, false, false, false, false };
  6345. if (ctx->device->uma) {
  6346. for (int i = 0; i < num_srcs; i++) {
  6347. ggml_vk_host_get(ctx->device, dst->src[i]->data, d_srcs[i], src_offsets[i]);
  6348. srcs_uma[i] = d_srcs[i] != nullptr;
  6349. }
  6350. ggml_vk_host_get(ctx->device, dst->data, d_D, dst_offset);
  6351. dst_uma = d_D != nullptr;
  6352. }
  6353. uint64_t src_sizes[7] = { 0, 0, 0, 0, 0, 0, 0 };
  6354. for (int i = 0; i < num_srcs; i++) {
  6355. src_sizes[i] = ggml_nbytes(dst->src[i]);
  6356. if (!srcs_uma[i]) {
  6357. d_srcs[i] = src_buf_ctxs[i]->dev_buffer;
  6358. src_offsets[i] = vk_tensor_offset(dst->src[i]) + dst->src[i]->view_offs;
  6359. }
  6360. }
  6361. const uint64_t dst_size = ggml_nbytes(dst);
  6362. if (!dst_uma) {
  6363. d_D = dst_buf_ctx->dev_buffer;
  6364. dst_offset = vk_tensor_offset(dst) + dst->view_offs;
  6365. }
  6366. std::array<uint32_t, 3> elements = {
  6367. (uint32_t)(pc.B * pc.H),
  6368. 1,
  6369. 1
  6370. };
  6371. if (version == 6) {
  6372. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, {
  6373. vk_subbuffer{ d_srcs[0], src_offsets[0], src_sizes[0] },
  6374. vk_subbuffer{ d_srcs[1], src_offsets[1], src_sizes[1] },
  6375. vk_subbuffer{ d_srcs[2], src_offsets[2], src_sizes[2] },
  6376. vk_subbuffer{ d_srcs[3], src_offsets[3], src_sizes[3] },
  6377. vk_subbuffer{ d_srcs[4], src_offsets[4], src_sizes[4] },
  6378. vk_subbuffer{ d_srcs[5], src_offsets[5], src_sizes[5] },
  6379. vk_subbuffer{ d_D, dst_offset, dst_size }
  6380. }, pc, elements);
  6381. } else if (version == 7) {
  6382. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, {
  6383. vk_subbuffer{ d_srcs[0], src_offsets[0], src_sizes[0] },
  6384. vk_subbuffer{ d_srcs[1], src_offsets[1], src_sizes[1] },
  6385. vk_subbuffer{ d_srcs[2], src_offsets[2], src_sizes[2] },
  6386. vk_subbuffer{ d_srcs[3], src_offsets[3], src_sizes[3] },
  6387. vk_subbuffer{ d_srcs[4], src_offsets[4], src_sizes[4] },
  6388. vk_subbuffer{ d_srcs[5], src_offsets[5], src_sizes[5] },
  6389. vk_subbuffer{ d_srcs[6], src_offsets[6], src_sizes[6] },
  6390. vk_subbuffer{ d_D, dst_offset, dst_size }
  6391. }, pc, elements);
  6392. } else {
  6393. // shouldn't happen
  6394. GGML_ASSERT(false);
  6395. }
  6396. }
  6397. static void ggml_vk_rwkv_wkv6(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, bool dryrun = false) {
  6398. const size_t seq_length = dst->src[0]->ne[2];
  6399. const size_t n_embed = dst->ne[0];
  6400. const size_t n_heads = dst->src[0]->ne[1];
  6401. const size_t n_seqs = dst->src[5]->ne[1];
  6402. ggml_vk_op_f32_wkv(
  6403. ctx, subctx, dst,
  6404. {
  6405. (uint32_t)n_seqs,
  6406. (uint32_t)seq_length,
  6407. (uint32_t)n_embed,
  6408. (uint32_t)n_heads,
  6409. },
  6410. 6,
  6411. dryrun
  6412. );
  6413. }
  6414. static void ggml_vk_rwkv_wkv7(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, bool dryrun = false) {
  6415. const size_t seq_length = dst->src[0]->ne[2];
  6416. const size_t n_embed = dst->ne[0];
  6417. const size_t n_heads = dst->src[0]->ne[1];
  6418. const size_t n_seqs = dst->src[6]->ne[1];
  6419. ggml_vk_op_f32_wkv(
  6420. ctx, subctx, dst,
  6421. {
  6422. (uint32_t)n_seqs,
  6423. (uint32_t)seq_length,
  6424. (uint32_t)n_embed,
  6425. (uint32_t)n_heads,
  6426. },
  6427. 7,
  6428. dryrun
  6429. );
  6430. }
  6431. 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) {
  6432. const ggml_tensor * x = dst->src[0];
  6433. const ggml_tensor * g = dst->src[1];
  6434. const ggml_tensor * gm = dst->src[2];
  6435. const ggml_tensor * gv = dst->src[3];
  6436. const ggml_tensor * p = dst->src[4];
  6437. GGML_ASSERT(x->type == GGML_TYPE_F32);
  6438. GGML_ASSERT(g->type == GGML_TYPE_F32);
  6439. GGML_ASSERT(gm->type == GGML_TYPE_F32);
  6440. GGML_ASSERT(gv->type == GGML_TYPE_F32);
  6441. GGML_ASSERT(p->type == GGML_TYPE_F32);
  6442. GGML_ASSERT(dst->buffer != nullptr);
  6443. GGML_ASSERT(ggml_is_contiguous(x));
  6444. GGML_ASSERT(ggml_is_contiguous(g));
  6445. GGML_ASSERT(ggml_is_contiguous(gm));
  6446. GGML_ASSERT(ggml_is_contiguous(gv));
  6447. GGML_ASSERT(ggml_is_contiguous(p));
  6448. GGML_ASSERT(ggml_are_same_shape(x, g));
  6449. GGML_ASSERT(ggml_are_same_shape(x, gm));
  6450. GGML_ASSERT(ggml_are_same_shape(x, gv));
  6451. GGML_ASSERT(ggml_nelements(p) == 7);
  6452. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, g, gm, gv, dst, GGML_OP_OPT_STEP_ADAMW);
  6453. GGML_ASSERT(pipeline != nullptr);
  6454. if (dryrun) {
  6455. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  6456. return;
  6457. }
  6458. ggml_backend_vk_buffer_context * x_buf_ctx = (ggml_backend_vk_buffer_context *)x->buffer->context;
  6459. ggml_backend_vk_buffer_context * g_buf_ctx = (ggml_backend_vk_buffer_context *)g->buffer->context;
  6460. ggml_backend_vk_buffer_context * gm_buf_ctx = (ggml_backend_vk_buffer_context *)gm->buffer->context;
  6461. ggml_backend_vk_buffer_context * gv_buf_ctx = (ggml_backend_vk_buffer_context *)gv->buffer->context;
  6462. ggml_backend_vk_buffer_context * p_buf_ctx = (ggml_backend_vk_buffer_context *)p->buffer->context;
  6463. ggml_vk_sync_buffers(subctx);
  6464. vk_buffer d_X = nullptr, d_G = nullptr, d_GM = nullptr, d_GV = nullptr, d_P = nullptr;
  6465. size_t x_offset = 0, g_offset = 0, gm_offset = 0, gv_offset = 0, p_offset = 0;
  6466. bool X_uma = false, G_uma = false, GM_uma = false, GV_uma = false, P_uma = false;
  6467. if (ctx->device->uma) {
  6468. ggml_vk_host_get(ctx->device, x->data, d_X, x_offset);
  6469. ggml_vk_host_get(ctx->device, g->data, d_G, g_offset);
  6470. ggml_vk_host_get(ctx->device, gm->data, d_GM, gm_offset);
  6471. ggml_vk_host_get(ctx->device, gv->data, d_GV, gv_offset);
  6472. ggml_vk_host_get(ctx->device, p->data, d_P, p_offset);
  6473. X_uma = d_X != nullptr;
  6474. G_uma = d_G != nullptr;
  6475. GM_uma = d_GM != nullptr;
  6476. GV_uma = d_GV != nullptr;
  6477. P_uma = d_P != nullptr;
  6478. }
  6479. if (!X_uma) {
  6480. d_X = x_buf_ctx->dev_buffer;
  6481. x_offset = vk_tensor_offset(x) + x->view_offs;
  6482. }
  6483. if (!G_uma) {
  6484. d_G = g_buf_ctx->dev_buffer;
  6485. g_offset = vk_tensor_offset(g) + g->view_offs;
  6486. }
  6487. if (!GM_uma) {
  6488. d_GM = gm_buf_ctx->dev_buffer;
  6489. gm_offset = vk_tensor_offset(gm) + gm->view_offs;
  6490. }
  6491. if (!GV_uma) {
  6492. d_GV = gv_buf_ctx->dev_buffer;
  6493. gv_offset = vk_tensor_offset(gv) + gv->view_offs;
  6494. }
  6495. if (!P_uma) {
  6496. d_P = p_buf_ctx->dev_buffer;
  6497. p_offset = vk_tensor_offset(p) + p->view_offs;
  6498. }
  6499. const uint64_t x_size = ggml_nbytes(x);
  6500. const uint64_t g_size = ggml_nbytes(g);
  6501. const uint64_t gm_size = ggml_nbytes(gm);
  6502. const uint64_t gv_size = ggml_nbytes(gv);
  6503. const uint64_t p_size = ggml_nbytes(p);
  6504. std::array<uint32_t, 3> elements = { (uint32_t)ggml_nelements(x), 1, 1 };
  6505. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, {
  6506. vk_subbuffer{ d_X, x_offset, x_size },
  6507. vk_subbuffer{ d_G, g_offset, g_size },
  6508. vk_subbuffer{ d_GM, gm_offset, gm_size },
  6509. vk_subbuffer{ d_GV, gv_offset, gv_size },
  6510. vk_subbuffer{ d_P, p_offset, p_size },
  6511. }, pc, elements);
  6512. }
  6513. static void ggml_vk_opt_step_adamw(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, bool dryrun = false) {
  6514. const size_t n = ggml_nelements(dst->src[0]);
  6515. ggml_vk_op_f32_opt_step_adamw(
  6516. ctx, subctx, dst,
  6517. { (uint32_t)n, 0, 0.0f, 0.0f },
  6518. dryrun
  6519. );
  6520. }
  6521. 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) {
  6522. int * op_params = (int *)dst->op_params;
  6523. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6524. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6525. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6526. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_CONCAT, {
  6527. (uint32_t)ggml_nelements(dst),
  6528. (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,
  6529. (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,
  6530. (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,
  6531. 0,
  6532. 0.0f, 0.0f, op_params[0],
  6533. }, dryrun);
  6534. }
  6535. static void ggml_vk_upscale(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6536. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6537. const uint32_t mode = (uint32_t)ggml_get_op_params_i32(dst, 0);
  6538. float sf0 = (float)dst->ne[0] / src0->ne[0];
  6539. float sf1 = (float)dst->ne[1] / src0->ne[1];
  6540. float sf2 = (float)dst->ne[2] / src0->ne[2];
  6541. float sf3 = (float)dst->ne[3] / src0->ne[3];
  6542. if (mode & GGML_SCALE_FLAG_ALIGN_CORNERS) {
  6543. sf0 = (float)(dst->ne[0] - 1) / (src0->ne[0] - 1);
  6544. sf1 = (float)(dst->ne[1] - 1) / (src0->ne[1] - 1);
  6545. }
  6546. ggml_vk_op_f32<vk_op_upscale_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_UPSCALE, {
  6547. (uint32_t)ggml_nelements(dst), 0, 0,
  6548. (uint32_t)src0->ne[0], (uint32_t)src0->ne[1],
  6549. (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,
  6550. (uint32_t)dst->ne[0], (uint32_t)dst->ne[1], (uint32_t)dst->ne[2],(uint32_t)dst->ne[3],
  6551. sf0, sf1, sf2, sf3,
  6552. }, dryrun);
  6553. }
  6554. static void ggml_vk_scale(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6555. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst);
  6556. p.param1 = ggml_get_op_params_f32(dst, 0);
  6557. p.param2 = ggml_get_op_params_f32(dst, 1);
  6558. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SCALE, std::move(p), dryrun);
  6559. }
  6560. static void ggml_vk_sqr(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6561. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SQR, vk_op_unary_push_constants_init(src0, dst), dryrun);
  6562. }
  6563. static void ggml_vk_sin(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6564. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SIN, vk_op_unary_push_constants_init(src0, dst), dryrun);
  6565. }
  6566. static void ggml_vk_cos(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6567. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_COS, vk_op_unary_push_constants_init(src0, dst), dryrun);
  6568. }
  6569. static void ggml_vk_clamp(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6570. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst);
  6571. p.param1 = ggml_get_op_params_f32(dst, 0);
  6572. p.param2 = ggml_get_op_params_f32(dst, 1);
  6573. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_CLAMP, std::move(p), dryrun);
  6574. }
  6575. static void ggml_vk_pad(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6576. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst, ggml_nelements(dst));
  6577. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_PAD, std::move(p), dryrun);
  6578. }
  6579. static void ggml_vk_roll(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6580. const int32_t s0 = ggml_get_op_params_i32(dst, 0);
  6581. const int32_t s1 = ggml_get_op_params_i32(dst, 1);
  6582. const int32_t s2 = ggml_get_op_params_i32(dst, 2);
  6583. const int32_t s3 = ggml_get_op_params_i32(dst, 3);
  6584. const uint32_t s01_packed = ((s0 + 0x8000) << 16) | (s1 + 0x8000);
  6585. const uint32_t s23_packed = ((s2 + 0x8000) << 16) | (s3 + 0x8000);
  6586. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst);
  6587. memcpy(&p.param1, &s01_packed, sizeof(float));
  6588. memcpy(&p.param2, &s23_packed, sizeof(float));
  6589. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_ROLL, std::move(p), dryrun);
  6590. }
  6591. static void ggml_vk_repeat(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6592. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst, ggml_nelements(dst));
  6593. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_REPEAT, std::move(p), dryrun);
  6594. }
  6595. static void ggml_vk_repeat_back(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6596. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst, ggml_nelements(dst));
  6597. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_REPEAT_BACK, std::move(p), dryrun);
  6598. }
  6599. static void ggml_vk_cpy(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6600. uint32_t ne = (uint32_t)ggml_nelements(src0);
  6601. if (ggml_is_quantized(src0->type) && ggml_is_quantized(dst->type)) {
  6602. // Convert from number of logical elements to 2- or 4-byte units.
  6603. ne /= ggml_blck_size(src0->type);
  6604. if ((ggml_type_size(src0->type) % 4) == 0) {
  6605. ne *= ggml_type_size(src0->type) / 4;
  6606. } else {
  6607. ne *= ggml_type_size(src0->type) / 2;
  6608. }
  6609. }
  6610. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst, ne);
  6611. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_CPY, std::move(p), dryrun);
  6612. }
  6613. 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) {
  6614. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6615. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6616. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6617. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_SET_ROWS, {
  6618. (uint32_t)ggml_nelements(src0),
  6619. (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,
  6620. (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,
  6621. (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,
  6622. 0,
  6623. 0.0f, 0.0f, 0,
  6624. }, dryrun);
  6625. }
  6626. 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) {
  6627. 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);
  6628. }
  6629. static void ggml_vk_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6630. float * op_params = (float *)dst->op_params;
  6631. 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);
  6632. }
  6633. static void ggml_vk_group_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6634. const int * int_op_params = (const int *)dst->op_params;
  6635. const float * float_op_params = (const float *)dst->op_params;
  6636. const uint32_t num_groups = int_op_params[0];
  6637. const float eps = float_op_params[1];
  6638. const uint32_t group_size = src0->ne[0] * src0->ne[1] * ((src0->ne[2] + num_groups - 1) / num_groups);
  6639. 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);
  6640. }
  6641. 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) {
  6642. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6643. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6644. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6645. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_RMS_NORM, {
  6646. (uint32_t)ggml_nelements(src0),
  6647. (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,
  6648. (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,
  6649. (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,
  6650. 0,
  6651. op_params[0], 0.0f, 0,
  6652. }, dryrun);
  6653. }
  6654. 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) {
  6655. float * op_params = (float *)dst->op_params;
  6656. 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);
  6657. }
  6658. static void ggml_vk_l2_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6659. float * op_params = (float *)dst->op_params;
  6660. 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);
  6661. }
  6662. static void ggml_vk_unary(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6663. 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);
  6664. }
  6665. 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) {
  6666. const bool swapped = (bool)dst->op_params[1];
  6667. const bool split = src1 != nullptr;
  6668. GGML_ASSERT(ggml_is_contiguous(src0));
  6669. if (!split) {
  6670. GGML_ASSERT(src0->ne[0] / 2 == dst->ne[0]);
  6671. } else {
  6672. GGML_ASSERT(src0->ne[0] == src1->ne[0]);
  6673. GGML_ASSERT(src0->ne[0] == dst->ne[0]);
  6674. GGML_ASSERT(src0->type == src1->type);
  6675. }
  6676. const uint32_t mode = split ? 2 : (swapped ? 1 : 0);
  6677. ggml_vk_op_f32<vk_op_glu_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_GLU, { (uint32_t)ggml_nelements(dst), (uint32_t)src0->ne[0], (uint32_t)dst->ne[0], mode }, dryrun);
  6678. }
  6679. 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) {
  6680. int32_t * op_params = (int32_t *)dst->op_params;
  6681. 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);
  6682. }
  6683. static void ggml_vk_soft_max(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  6684. float * op_params = (float *)dst->op_params;
  6685. float scale = op_params[0];
  6686. float max_bias = op_params[1];
  6687. const uint32_t ncols = (uint32_t)src0->ne[0];
  6688. const uint32_t nrows_x = (uint32_t)ggml_nrows(src0);
  6689. const uint32_t nrows_y = (uint32_t)src0->ne[1];
  6690. const uint32_t ne12 = src1 ? (uint32_t)(src1->ne[2]) : 0u;
  6691. const uint32_t ne13 = src1 ? (uint32_t)(src1->ne[3]) : 0u;
  6692. const uint32_t nb11 = src1 ? (uint32_t)(src1->nb[1] / src1->nb[0]) : 0u;
  6693. const uint32_t nb12 = src1 ? (uint32_t)(src1->nb[2] / src1->nb[0]) : 0u;
  6694. const uint32_t nb13 = src1 ? (uint32_t)(src1->nb[3] / src1->nb[0]) : 0u;
  6695. const uint32_t n_head_kv = src0->ne[2];
  6696. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head_kv));
  6697. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  6698. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  6699. ggml_vk_op_f32<vk_op_soft_max_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_SOFT_MAX, {
  6700. ncols,
  6701. src1 != nullptr ? nrows_y : (uint32_t)0,
  6702. (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], (uint32_t)src0->ne[2],
  6703. ne12, ne13,
  6704. nb11, nb12, nb13,
  6705. scale, max_bias,
  6706. m0, m1,
  6707. n_head_log2,
  6708. nrows_x,
  6709. }, dryrun);
  6710. }
  6711. 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) {
  6712. float * op_params = (float *)dst->op_params;
  6713. 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);
  6714. }
  6715. 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) {
  6716. const int n_dims = ((int32_t *) dst->op_params)[1];
  6717. const int mode = ((int32_t *) dst->op_params)[2];
  6718. // const int n_ctx = ((int32_t *) dst->op_params)[3];
  6719. const int n_ctx_orig = ((int32_t *) dst->op_params)[4];
  6720. const float freq_base = ((float *) dst->op_params)[5];
  6721. const float freq_scale = ((float *) dst->op_params)[6];
  6722. const float ext_factor = ((float *) dst->op_params)[7];
  6723. const float attn_factor = ((float *) dst->op_params)[8];
  6724. const float beta_fast = ((float *) dst->op_params)[9];
  6725. const float beta_slow = ((float *) dst->op_params)[10];
  6726. int sections[4] {};
  6727. if (mode & GGML_ROPE_TYPE_MROPE) {
  6728. memcpy(sections, (int32_t *) dst->op_params + 11, sizeof(int)*4);
  6729. }
  6730. float corr_dims[2];
  6731. ggml_rope_yarn_corr_dims(n_dims, n_ctx_orig, freq_base, beta_fast, beta_slow, corr_dims);
  6732. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  6733. uint32_t s1 = src0->nb[1] / ggml_type_size(src0->type);
  6734. uint32_t s2 = src0->nb[2] / ggml_type_size(src0->type);
  6735. ggml_vk_op_f32<vk_op_rope_push_constants>(ctx, subctx, src0, src1, src2, dst, GGML_OP_ROPE, {
  6736. (uint32_t)src0->ne[0], (uint32_t)n_dims, freq_scale, (uint32_t)src0->ne[1],
  6737. freq_base, ext_factor, attn_factor, {corr_dims[0], corr_dims[1]}, theta_scale,
  6738. src2 != nullptr, (uint32_t)src0->ne[2], s1, s2,
  6739. sections[0], sections[1], sections[2], sections[3], backprop
  6740. }, dryrun);
  6741. }
  6742. static void ggml_vk_argsort(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6743. int32_t * op_params = (int32_t *)dst->op_params;
  6744. uint32_t ncols = src0->ne[0];
  6745. uint32_t ncols_pad = 1;
  6746. while (ncols_pad < ncols) {
  6747. ncols_pad *= 2;
  6748. }
  6749. GGML_ASSERT(ncols_pad <= 1024);
  6750. ggml_vk_op_f32<vk_op_argsort_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_ARGSORT, {
  6751. ncols,
  6752. ncols_pad,
  6753. op_params[0],
  6754. }, dryrun);
  6755. }
  6756. static void ggml_vk_sum(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6757. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SUM, { (uint32_t)ggml_nelements(src0), 0, 0.0f, 0.0f }, dryrun);
  6758. }
  6759. static void ggml_vk_sum_rows(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6760. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SUM_ROWS, { (uint32_t)src0->ne[0], 0, 0.0f, 0.0f }, dryrun);
  6761. }
  6762. static void ggml_vk_argmax(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6763. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_ARGMAX, { (uint32_t)src0->ne[0], 0, 0.0f, 0.0f }, dryrun);
  6764. }
  6765. 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) {
  6766. 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);
  6767. }
  6768. 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) {
  6769. const int32_t s0 = dst->op_params[0];
  6770. const int32_t s1 = dst->op_params[1];
  6771. const int32_t p0 = dst->op_params[2];
  6772. const int32_t p1 = dst->op_params[3];
  6773. const int32_t d0 = dst->op_params[4];
  6774. const int32_t d1 = dst->op_params[5];
  6775. const bool is_2D = dst->op_params[6] == 1;
  6776. const uint32_t IC = src1->ne[is_2D ? 2 : 1];
  6777. const uint32_t IH = is_2D ? src1->ne[1] : 1;
  6778. const uint32_t IW = src1->ne[0];
  6779. const uint32_t KH = is_2D ? src0->ne[1] : 1;
  6780. const uint32_t KW = src0->ne[0];
  6781. const uint32_t OH = is_2D ? dst->ne[2] : 1;
  6782. const uint32_t OW = dst->ne[1];
  6783. const uint32_t offset_delta = src1->nb[is_2D ? 2 : 1] / 4; // nb is byte offset, src is type float32
  6784. const uint32_t batch_offset = src1->nb[is_2D ? 3 : 2] / 4; // nb is byte offset, src is type float32
  6785. const uint32_t pelements = OW * KW * KH;
  6786. ggml_vk_op_f32<vk_op_im2col_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_IM2COL, {
  6787. batch_offset, offset_delta,
  6788. IC, IW, IH, OW, OH, KW, KH,
  6789. pelements,
  6790. IC * KH * KW,
  6791. s0, s1, p0, p1, d0, d1,
  6792. }, dryrun);
  6793. }
  6794. static void ggml_vk_timestep_embedding(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6795. const uint32_t dim = dst->op_params[0];
  6796. const uint32_t max_period = dst->op_params[1];
  6797. const uint32_t nb1 = dst->nb[1] / ggml_type_size(dst->type);
  6798. ggml_vk_op_f32<vk_op_timestep_embedding_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_TIMESTEP_EMBEDDING, {
  6799. nb1, dim, max_period,
  6800. }, dryrun);
  6801. }
  6802. 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) {
  6803. // src0: (K, Cout, Cin, 1) -- kernel
  6804. // src1: (L, Cin, 1, 1) -- input
  6805. // dst: (*, Cout, 1, 1)
  6806. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6807. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6808. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6809. GGML_TENSOR_BINARY_OP_LOCALS
  6810. GGML_ASSERT(nb00 == sizeof(float));
  6811. GGML_ASSERT(nb10 == sizeof(float));
  6812. const int32_t s0 = dst->op_params[0];
  6813. vk_op_conv_transpose_1d_push_constants p{};
  6814. p.Cout = static_cast<uint32_t>(ne01);
  6815. p.Cin = static_cast<uint32_t>(ne02);
  6816. p.K = static_cast<uint32_t>(ne00);
  6817. p.L = static_cast<uint32_t>(ne10);
  6818. p.KL = static_cast<uint32_t>(ne0);
  6819. p.nb01 = static_cast<uint32_t>(nb01 / nb00);
  6820. p.nb02 = static_cast<uint32_t>(nb02 / nb00);
  6821. p.nb11 = static_cast<uint32_t>(nb11 / nb10);
  6822. p.nb1 = static_cast<uint32_t>(nb1 / nb0);
  6823. p.s0 = static_cast<uint32_t>(s0);
  6824. ggml_vk_op_f32(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_CONV_TRANSPOSE_1D, std::move(p), dryrun);
  6825. }
  6826. static void ggml_vk_pool_2d(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6827. uint32_t op = static_cast<uint32_t>(dst->op_params[0]);
  6828. const int32_t k1 = dst->op_params[1];
  6829. const int32_t k0 = dst->op_params[2];
  6830. const int32_t s1 = dst->op_params[3];
  6831. const int32_t s0 = dst->op_params[4];
  6832. const int32_t p1 = dst->op_params[5];
  6833. const int32_t p0 = dst->op_params[6];
  6834. const uint32_t IH = src0->ne[1];
  6835. const uint32_t IW = src0->ne[0];
  6836. const uint32_t N = dst->ne[3];
  6837. const uint32_t OC = dst->ne[2];
  6838. const uint32_t OH = dst->ne[1];
  6839. const uint32_t OW = dst->ne[0];
  6840. const uint32_t parallel_elements = N * OC * OH * OW;
  6841. ggml_vk_op_f32<vk_op_pool2d_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_POOL_2D, {
  6842. IW, IH, OW, OH, OC,
  6843. parallel_elements,
  6844. op,
  6845. k0, k1, s0, s1, p0, p1,
  6846. }, dryrun);
  6847. }
  6848. 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) {
  6849. vk_op_conv2d_dw_push_constants p{};
  6850. p.ne = ggml_nelements(dst);
  6851. p.channels = dst->ne[2];
  6852. p.batches = dst->ne[3];
  6853. p.dst_w = dst->ne[0];
  6854. p.dst_h = dst->ne[1];
  6855. p.src_w = src1->ne[0];
  6856. p.src_h = src1->ne[1];
  6857. p.knl_w = src0->ne[0];
  6858. p.knl_h = src0->ne[1];
  6859. p.stride_x = dst->op_params[0];
  6860. p.stride_y = dst->op_params[1];
  6861. p.pad_x = dst->op_params[2];
  6862. p.pad_y = dst->op_params[3];
  6863. p.dilation_x = dst->op_params[4];
  6864. p.dilation_y = dst->op_params[5];
  6865. GGML_ASSERT(src0->ne[3] == p.channels);
  6866. GGML_ASSERT(src1->ne[3] == p.batches);
  6867. ggml_vk_op_f32(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_CONV_2D_DW, std::move(p), dryrun);
  6868. }
  6869. static void ggml_vk_leaky_relu(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6870. const float * op_params = (const float *)dst->op_params;
  6871. 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);
  6872. }
  6873. #ifdef GGML_VULKAN_RUN_TESTS
  6874. static void ggml_vk_print_matrix_area(const void * data, ggml_type type, int ne0, int ne1, int i0, int i1, int i2) {
  6875. if (type != GGML_TYPE_F32 && type != GGML_TYPE_F16) {
  6876. return;
  6877. }
  6878. i0 = std::max(i0, 5);
  6879. i1 = std::max(i1, 5);
  6880. i2 = std::max(i2, 0);
  6881. fprintf(stderr, " ");
  6882. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  6883. fprintf(stderr, "%7d ", idx1);
  6884. }
  6885. fprintf(stderr, "\n");
  6886. for (int idx0 = i0 - 5; idx0 < i0 + 5; idx0++) {
  6887. fprintf(stderr, "%7d: ", idx0);
  6888. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  6889. if (idx0 >= 0 && idx0 < ne0 && idx1 >= 0 && idx1 < ne1) {
  6890. float val;
  6891. if (type == GGML_TYPE_F32) {
  6892. val = *((const float *) data + i2*ne1*ne0 + idx1*ne0 + idx0);
  6893. } else if (type == GGML_TYPE_F16) {
  6894. val = ggml_fp16_to_fp32(*((const ggml_fp16_t *) data + i2*ne1*ne0 + idx1*ne0 + idx0));
  6895. } else {
  6896. GGML_ABORT("fatal error");
  6897. }
  6898. fprintf(stderr, "% 7.2f ", val);
  6899. } else {
  6900. fprintf(stderr, " ");
  6901. }
  6902. }
  6903. fprintf(stderr, "\n");
  6904. }
  6905. }
  6906. template <typename X_TYPE, typename Y_TYPE>
  6907. 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) {
  6908. VK_LOG_DEBUG("ggml_vk_test_matmul(" << m << ", " << n << ", " << k << ", " << batch << ", " << num_it << ", " << split_k << ", " << shader_size << ")");
  6909. const size_t x_ne = m * k * batch;
  6910. const size_t y_ne = k * n * batch;
  6911. const size_t d_ne = m * n * batch;
  6912. vk_pipeline p;
  6913. std::string shname;
  6914. if (shader_size == 0) {
  6915. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6916. p = ctx->device->pipeline_matmul_f32->a_s;
  6917. shname = "F32_ALIGNED_S";
  6918. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6919. p = ctx->device->pipeline_matmul_f32_f16->a_s;
  6920. shname = "F32_F16_ALIGNED_S";
  6921. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6922. p = ctx->device->pipeline_matmul_f16_f32.f32acc->a_s;
  6923. shname = "F16_F32_ALIGNED_S";
  6924. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6925. p = ctx->device->pipeline_matmul_f16.f32acc->a_s;
  6926. shname = "F16_ALIGNED_S";
  6927. } else {
  6928. GGML_ABORT("fatal error");
  6929. }
  6930. } else if (shader_size == 1) {
  6931. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6932. p = ctx->device->pipeline_matmul_f32->a_m;
  6933. shname = "F32_ALIGNED_M";
  6934. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6935. p = ctx->device->pipeline_matmul_f32_f16->a_m;
  6936. shname = "F32_F16_ALIGNED_M";
  6937. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6938. p = ctx->device->pipeline_matmul_f16_f32.f32acc->a_m;
  6939. shname = "F16_F32_ALIGNED_M";
  6940. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6941. p = ctx->device->pipeline_matmul_f16.f32acc->a_m;
  6942. shname = "F16_ALIGNED_M";
  6943. } else {
  6944. GGML_ABORT("fatal error");
  6945. }
  6946. } else if (shader_size == 2) {
  6947. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6948. p = ctx->device->pipeline_matmul_f32->a_l;
  6949. shname = "F32_ALIGNED_L";
  6950. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6951. p = ctx->device->pipeline_matmul_f32_f16->a_l;
  6952. shname = "F32_F16_ALIGNED_L";
  6953. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6954. p = ctx->device->pipeline_matmul_f16_f32.f32acc->a_l;
  6955. shname = "F16_F32_ALIGNED_L";
  6956. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6957. p = ctx->device->pipeline_matmul_f16.f32acc->a_l;
  6958. shname = "F16_ALIGNED_L";
  6959. } else {
  6960. GGML_ABORT("fatal error");
  6961. }
  6962. } else {
  6963. GGML_ASSERT(0);
  6964. }
  6965. const size_t kpad = ggml_vk_align_size(k, p->align);
  6966. if (k != kpad) {
  6967. if (shader_size == 0) {
  6968. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6969. p = ctx->device->pipeline_matmul_f32->s;
  6970. shname = "F32_S";
  6971. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6972. p = ctx->device->pipeline_matmul_f32_f16->s;
  6973. shname = "F32_F16_S";
  6974. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6975. p = ctx->device->pipeline_matmul_f16_f32.f32acc->s;
  6976. shname = "F16_F32_S";
  6977. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6978. p = ctx->device->pipeline_matmul_f16.f32acc->s;
  6979. shname = "F16_S";
  6980. }
  6981. } else if (shader_size == 1) {
  6982. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6983. p = ctx->device->pipeline_matmul_f32->m;
  6984. shname = "F32_M";
  6985. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6986. p = ctx->device->pipeline_matmul_f32_f16->m;
  6987. shname = "F32_F16_M";
  6988. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6989. p = ctx->device->pipeline_matmul_f16_f32.f32acc->m;
  6990. shname = "F16_F32_M";
  6991. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6992. p = ctx->device->pipeline_matmul_f16.f32acc->m;
  6993. shname = "F16_M";
  6994. }
  6995. } else if (shader_size == 2) {
  6996. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6997. p = ctx->device->pipeline_matmul_f32->l;
  6998. shname = "F32_L";
  6999. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  7000. p = ctx->device->pipeline_matmul_f32_f16->l;
  7001. shname = "F32_F16_L";
  7002. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  7003. p = ctx->device->pipeline_matmul_f16_f32.f32acc->l;
  7004. shname = "F16_F32_L";
  7005. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  7006. p = ctx->device->pipeline_matmul_f16.f32acc->l;
  7007. shname = "F16_L";
  7008. }
  7009. }
  7010. }
  7011. ggml_pipeline_request_descriptor_sets(ctx, p, num_it);
  7012. if (split_k > 1) {
  7013. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_matmul_split_k_reduce, num_it);
  7014. if (ctx->prealloc_split_k == nullptr || ctx->prealloc_split_k->size < sizeof(float) * d_ne * split_k) {
  7015. // Resize buffer
  7016. if (ctx->prealloc_split_k != nullptr) {
  7017. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  7018. }
  7019. ctx->prealloc_split_k = ggml_vk_create_buffer_check(ctx->device, sizeof(float) * d_ne * split_k, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7020. }
  7021. }
  7022. if (ctx->device->need_compiles) {
  7023. ggml_vk_load_shaders(ctx->device);
  7024. }
  7025. ggml_pipeline_allocate_descriptor_sets(ctx);
  7026. vk_buffer d_X = ggml_vk_create_buffer_check(ctx->device, sizeof(X_TYPE) * x_ne, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7027. vk_buffer d_Y = ggml_vk_create_buffer_check(ctx->device, sizeof(Y_TYPE) * y_ne, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7028. vk_buffer d_D = ggml_vk_create_buffer_check(ctx->device, sizeof(float) * d_ne, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7029. X_TYPE* x = (X_TYPE *) malloc(sizeof(X_TYPE) * x_ne);
  7030. Y_TYPE* y = (Y_TYPE *) malloc(sizeof(Y_TYPE) * y_ne);
  7031. float* d = (float *) malloc(sizeof(float) * d_ne);
  7032. for (size_t i = 0; i < x_ne; i++) {
  7033. if (std::is_same<float, X_TYPE>()) {
  7034. x[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  7035. // x[i] = 1.0f;
  7036. // x[i] = i + 1;
  7037. // x[i] = (i % k == i / k) ? 1.0f : 0.0f;
  7038. } else if (std::is_same<ggml_fp16_t, X_TYPE>()) {
  7039. x[i] = ggml_fp32_to_fp16((rand() / (float)RAND_MAX) * 2.0f - 1.0f);
  7040. // x[i] = ggml_fp32_to_fp16(1.0f);
  7041. // x[i] = ggml_fp32_to_fp16(i + 1);
  7042. // x[i] = ggml_fp32_to_fp16((i % k == i / k) ? 1.0f : 0.0f);
  7043. } else {
  7044. GGML_ABORT("fatal error");
  7045. }
  7046. }
  7047. for (size_t i = 0; i < y_ne; i++) {
  7048. if (std::is_same<float, Y_TYPE>()) {
  7049. y[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  7050. // y[i] = (i % k == i / k) ? 1.0f : 0.0f;
  7051. // y[i] = i + 1;
  7052. } else if (std::is_same<ggml_fp16_t, Y_TYPE>()) {
  7053. y[i] = ggml_fp32_to_fp16((rand() / (float)RAND_MAX) * 2.0f - 1.0f);
  7054. // y[i] = ggml_fp32_to_fp16((i % k == i / k) ? 1.0f : 0.0f);
  7055. // y[i] = ggml_fp32_to_fp16(i + 1);
  7056. } else {
  7057. GGML_ABORT("fatal error");
  7058. }
  7059. }
  7060. ggml_vk_buffer_write(d_X, 0, x, sizeof(X_TYPE) * k * m * batch);
  7061. ggml_vk_buffer_write(d_Y, 0, y, sizeof(Y_TYPE) * k * n * batch);
  7062. vk_context subctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  7063. ggml_vk_ctx_begin(ctx->device, subctx);
  7064. for (size_t i = 0; i < num_it; i++) {
  7065. ggml_vk_matmul(
  7066. 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),
  7067. m, n, k,
  7068. k, k, m, k*m, k*n, m*n,
  7069. split_k, batch, batch, batch, 1, 1, n
  7070. );
  7071. }
  7072. ggml_vk_ctx_end(subctx);
  7073. auto begin = std::chrono::high_resolution_clock::now();
  7074. ggml_vk_submit(subctx, ctx->fence);
  7075. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_matmul waitForFences");
  7076. ctx->device->device.resetFences({ ctx->fence });
  7077. ggml_vk_queue_command_pools_cleanup(ctx->device);
  7078. auto end = std::chrono::high_resolution_clock::now();
  7079. double time = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  7080. // copy dst to host
  7081. ggml_vk_buffer_read(d_D, 0, d, sizeof(float) * d_ne);
  7082. float * d_chk = (float *) malloc(sizeof(float) * d_ne);
  7083. ggml_init_params iparams = {
  7084. /*.mem_size =*/ 1024*1024*1024,
  7085. /*.mem_buffer =*/ NULL,
  7086. /*.no_alloc =*/ true,
  7087. };
  7088. ggml_context * ggml_ctx = ggml_init(iparams);
  7089. ggml_type src0_type;
  7090. ggml_type src1_type;
  7091. if (std::is_same<float, X_TYPE>()) {
  7092. src0_type = GGML_TYPE_F32;
  7093. } else if (std::is_same<ggml_fp16_t, X_TYPE>()) {
  7094. src0_type = GGML_TYPE_F16;
  7095. } else {
  7096. GGML_ABORT("fatal error");
  7097. }
  7098. if (std::is_same<float, Y_TYPE>()) {
  7099. src1_type = GGML_TYPE_F32;
  7100. } else if (std::is_same<ggml_fp16_t, Y_TYPE>()) {
  7101. src1_type = GGML_TYPE_F16;
  7102. } else {
  7103. GGML_ABORT("fatal error");
  7104. }
  7105. ggml_tensor * src0_ggml = ggml_new_tensor_3d(ggml_ctx, src0_type, k, m, batch);
  7106. ggml_tensor * src1_ggml = ggml_new_tensor_3d(ggml_ctx, src1_type, k, n, batch);
  7107. ggml_tensor * tensor_ggml = ggml_mul_mat(ggml_ctx, src0_ggml, src1_ggml);
  7108. src0_ggml->data = x;
  7109. src1_ggml->data = y;
  7110. tensor_ggml->data = d_chk;
  7111. ggml_cgraph * cgraph = ggml_new_graph(ggml_ctx);
  7112. ggml_build_forward_expand(cgraph, tensor_ggml);
  7113. ggml_graph_compute_with_ctx(ggml_ctx, cgraph, 1);
  7114. ggml_free(ggml_ctx);
  7115. double avg_err = 0.0;
  7116. int first_err_n = -1;
  7117. int first_err_m = -1;
  7118. int first_err_b = -1;
  7119. for (size_t i = 0; i < m*n*batch; i++) {
  7120. double err = std::fabs(d[i] - d_chk[i]);
  7121. avg_err += err;
  7122. if ((err > 0.05f || std::isnan(err)) && first_err_n == -1) {
  7123. first_err_b = i / (m * n);
  7124. first_err_n = (i % (m * n)) / m;
  7125. first_err_m = (i % (m * n)) % m;
  7126. }
  7127. }
  7128. avg_err /= m * n;
  7129. double tflops = 2.0*m*n*k*batch*num_it / (time / 1000.0) / (1000.0*1000.0*1000.0*1000.0);
  7130. 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;
  7131. if (avg_err > 0.1 || std::isnan(avg_err)) {
  7132. std::cerr << "m = " << first_err_m << " n = " << first_err_n << " b = " << first_err_b << std::endl;
  7133. std::cerr << "Actual result: " << std::endl << std::endl;
  7134. ggml_vk_print_matrix_area(d, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  7135. std::cerr << "Expected result: " << std::endl << std::endl;
  7136. ggml_vk_print_matrix_area(d_chk, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  7137. if (split_k > 1) {
  7138. float * split_k_buf = (float *) malloc(sizeof(float) * d_ne * split_k);
  7139. ggml_vk_buffer_read(ctx->prealloc_split_k, 0, split_k_buf, sizeof(float) * d_ne * split_k);
  7140. std::cerr << "d_buf0: " << std::endl << std::endl;
  7141. ggml_vk_print_matrix_area(split_k_buf, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  7142. std::cerr << "d_buf1: " << std::endl << std::endl;
  7143. ggml_vk_print_matrix_area(split_k_buf + d_ne, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  7144. std::cerr << "d_buf2: " << std::endl << std::endl;
  7145. 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);
  7146. std::cerr << "d_buf3: " << std::endl << std::endl;
  7147. 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);
  7148. free(split_k_buf);
  7149. }
  7150. }
  7151. free(d_chk);
  7152. ggml_vk_command_pool_cleanup(ctx->device, ctx->compute_cmd_pool);
  7153. ggml_vk_command_pool_cleanup(ctx->device, ctx->transfer_cmd_pool);
  7154. ggml_vk_destroy_buffer(d_X);
  7155. ggml_vk_destroy_buffer(d_Y);
  7156. ggml_vk_destroy_buffer(d_D);
  7157. free(x);
  7158. free(y);
  7159. free(d);
  7160. }
  7161. static void ggml_vk_print_tensor_area(const ggml_tensor * tensor, int i0, int i1, int i2, int i3) {
  7162. if (tensor->type != GGML_TYPE_F32 && tensor->type != GGML_TYPE_F16) {
  7163. return;
  7164. }
  7165. i0 = std::max(i0, 5);
  7166. i1 = std::max(i1, 5);
  7167. i2 = std::max(i2, 0);
  7168. i3 = std::max(i3, 0);
  7169. fprintf(stderr, " ");
  7170. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  7171. fprintf(stderr, "%7d ", idx1);
  7172. }
  7173. fprintf(stderr, "\n");
  7174. for (int idx0 = i0 - 5; idx0 < i0 + 5; idx0++) {
  7175. fprintf(stderr, "%7d: ", idx0);
  7176. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  7177. 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]) {
  7178. float val;
  7179. if (tensor->type == GGML_TYPE_F32) {
  7180. val = *(float *) ((char *) tensor->data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]);
  7181. } else if (tensor->type == GGML_TYPE_F16) {
  7182. 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]));
  7183. } else {
  7184. GGML_ABORT("fatal error");
  7185. }
  7186. fprintf(stderr, "% 7.2f ", val);
  7187. } else {
  7188. fprintf(stderr, " ");
  7189. }
  7190. }
  7191. fprintf(stderr, "\n");
  7192. }
  7193. }
  7194. static void ggml_vk_quantize_data(const float * from, void * to, size_t ne, ggml_type quant) {
  7195. ggml_quantize_chunk(quant, from, to, 0, 1, ne, nullptr);
  7196. }
  7197. static void ggml_vk_dequantize_data(const void * from, float * to, size_t ne, ggml_type quant) {
  7198. if (quant == GGML_TYPE_F32) {
  7199. memcpy(to, from, sizeof(float) * ne);
  7200. return;
  7201. }
  7202. const auto * tt = ggml_get_type_traits(quant);
  7203. ggml_to_float_t dequant_fn = tt->to_float;
  7204. dequant_fn(from, to, ne);
  7205. }
  7206. static void ggml_vk_test_dequant(ggml_backend_vk_context * ctx, size_t ne, ggml_type quant) {
  7207. VK_LOG_DEBUG("ggml_vk_test_dequant(" << ne << ")");
  7208. const size_t x_sz = sizeof(float) * ne;
  7209. const size_t x_sz_f16 = sizeof(ggml_fp16_t) * ne;
  7210. const size_t qx_sz = ne * ggml_type_size(quant)/ggml_blck_size(quant);
  7211. float * x = (float *) malloc(x_sz);
  7212. void * qx = malloc(qx_sz);
  7213. vk_buffer qx_buf = ggml_vk_create_buffer_check(ctx->device, qx_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7214. vk_buffer x_buf = ggml_vk_create_buffer_check(ctx->device, x_sz_f16, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7215. float * x_ref = (float *) malloc(x_sz);
  7216. ggml_fp16_t * x_chk = (ggml_fp16_t *) malloc(x_sz_f16);
  7217. for (size_t i = 0; i < ne; i++) {
  7218. x[i] = rand() / (float)RAND_MAX;
  7219. }
  7220. vk_pipeline p = ggml_vk_get_to_fp16(ctx, quant);
  7221. ggml_vk_quantize_data(x, qx, ne, quant);
  7222. ggml_vk_dequantize_data(qx, x_ref, ne, quant);
  7223. ggml_pipeline_request_descriptor_sets(ctx, p, 1);
  7224. if (ctx->device->need_compiles) {
  7225. ggml_vk_load_shaders(ctx->device);
  7226. }
  7227. ggml_pipeline_allocate_descriptor_sets(ctx);
  7228. ggml_vk_buffer_write(qx_buf, 0, qx, qx_sz);
  7229. vk_context subctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  7230. ggml_vk_ctx_begin(ctx->device, subctx);
  7231. const std::vector<uint32_t> pc = { 1, (uint32_t)ne, (uint32_t)ne, (uint32_t)ne, (uint32_t)ne };
  7232. 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});
  7233. ggml_vk_ctx_end(subctx);
  7234. auto begin = std::chrono::high_resolution_clock::now();
  7235. ggml_vk_submit(subctx, ctx->fence);
  7236. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_dequant waitForFences");
  7237. ctx->device->device.resetFences({ ctx->fence });
  7238. ggml_vk_queue_command_pools_cleanup(ctx->device);
  7239. auto end = std::chrono::high_resolution_clock::now();
  7240. double ms_dequant = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  7241. ggml_vk_buffer_read(x_buf, 0, x_chk, x_sz_f16);
  7242. int first_err = -1;
  7243. double avg_err = 0.0;
  7244. for (size_t i = 0; i < ne; i++) {
  7245. double error = std::fabs(x_ref[i] - ggml_fp16_to_fp32(x_chk[i]));
  7246. avg_err += error;
  7247. if (first_err < 0 && error > 0.05) {
  7248. first_err = i;
  7249. }
  7250. }
  7251. avg_err /= ne;
  7252. std::cerr << "TEST DEQUANT " << ggml_type_name(quant) << " time=" << ms_dequant << "ms avg_err=" << avg_err << std::endl;
  7253. if (avg_err > 0.1) {
  7254. std::cerr << "first_error = " << first_err << std::endl;
  7255. std::cerr << "Actual result: " << std::endl << std::endl;
  7256. for (int i = std::max(0, first_err - 5); i < std::min((int)ne, first_err + 5); i++) {
  7257. std::cerr << ggml_fp16_to_fp32(x_chk[i]) << ", ";
  7258. }
  7259. std::cerr << std::endl << "Expected result: " << std::endl << std::endl;
  7260. for (int i = std::max(0, first_err - 5); i < std::min((int)ne, first_err + 5); i++) {
  7261. std::cerr << x_ref[i] << ", ";
  7262. }
  7263. std::cerr << std::endl;
  7264. }
  7265. ggml_vk_destroy_buffer(x_buf);
  7266. ggml_vk_destroy_buffer(qx_buf);
  7267. free(x);
  7268. free(qx);
  7269. free(x_ref);
  7270. free(x_chk);
  7271. }
  7272. // This does not work without ggml q8_1 quantization support
  7273. //
  7274. // typedef uint16_t ggml_half;
  7275. // typedef uint32_t ggml_half2;
  7276. //
  7277. // #define QK8_1 32
  7278. // typedef struct {
  7279. // union {
  7280. // struct {
  7281. // ggml_half d; // delta
  7282. // ggml_half s; // d * sum(qs[i])
  7283. // } GGML_COMMON_AGGR_S;
  7284. // ggml_half2 ds;
  7285. // } GGML_COMMON_AGGR_U;
  7286. // int8_t qs[QK8_1]; // quants
  7287. // } block_q8_1;
  7288. //
  7289. // static void ggml_vk_test_quantize(ggml_backend_vk_context * ctx, size_t ne, ggml_type quant) {
  7290. // VK_LOG_DEBUG("ggml_vk_test_quantize(" << ne << ")");
  7291. // GGML_ASSERT(quant == GGML_TYPE_Q8_1);
  7292. //
  7293. // const size_t x_sz = sizeof(float) * ne;
  7294. // const size_t qx_sz = ne * ggml_type_size(quant)/ggml_blck_size(quant);
  7295. // float * x = (float *) malloc(x_sz);
  7296. // block_q8_1 * qx = (block_q8_1 *)malloc(qx_sz);
  7297. // block_q8_1 * qx_res = (block_q8_1 *)malloc(qx_sz);
  7298. // vk_buffer x_buf = ggml_vk_create_buffer_check(ctx->device, x_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7299. // vk_buffer qx_buf = ggml_vk_create_buffer_check(ctx->device, qx_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7300. //
  7301. // for (size_t i = 0; i < ne; i++) {
  7302. // x[i] = rand() / (float)RAND_MAX;
  7303. // }
  7304. //
  7305. // vk_pipeline p = ggml_vk_get_quantize_pipeline(ctx, quant);
  7306. //
  7307. // ggml_pipeline_request_descriptor_sets(ctx, p, 1);
  7308. //
  7309. // if (ctx->device->need_compiles) {
  7310. // ggml_vk_load_shaders(ctx->device);
  7311. // }
  7312. //
  7313. // ggml_pipeline_allocate_descriptor_sets(ctx);
  7314. //
  7315. // ggml_vk_buffer_write(x_buf, 0, x, x_sz);
  7316. //
  7317. // vk_context subctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  7318. // ggml_vk_ctx_begin(ctx->device, subctx);
  7319. // ggml_vk_quantize_q8_1(ctx, subctx, ggml_vk_subbuffer(x_buf), ggml_vk_subbuffer(qx_buf), ne);
  7320. // ggml_vk_ctx_end(subctx);
  7321. //
  7322. // auto begin = std::chrono::high_resolution_clock::now();
  7323. //
  7324. // ggml_vk_submit(subctx, ctx->fence);
  7325. // VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_quantize waitForFences");
  7326. // ctx->device->device.resetFences({ ctx->fence });
  7327. // ggml_vk_queue_command_pools_cleanup(ctx->device);
  7328. //
  7329. // auto end = std::chrono::high_resolution_clock::now();
  7330. //
  7331. // double ms_quant = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  7332. // ggml_vk_buffer_read(qx_buf, 0, qx, qx_sz);
  7333. //
  7334. // ggml_vk_quantize_data(x, qx_res, ne, quant);
  7335. //
  7336. // int first_err = -1;
  7337. //
  7338. // for (size_t i = 0; i < ne / 32; i++) {
  7339. // 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));
  7340. //
  7341. // if (first_err < 0 && error > 0.1) {
  7342. // first_err = i;
  7343. // }
  7344. //
  7345. // 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));
  7346. //
  7347. // if (first_err < 0 && error > 0.1) {
  7348. // first_err = i;
  7349. // }
  7350. //
  7351. // for (size_t j = 0; j < 32; j++) {
  7352. // uint64_t error = std::abs(qx_res[i].qs[j] - qx[i].qs[j]);
  7353. //
  7354. // if (first_err < 0 && error > 1) {
  7355. // first_err = i;
  7356. // }
  7357. // }
  7358. // }
  7359. //
  7360. // std::cerr << "TEST QUANTIZE " << ggml_type_name(quant) << " time=" << ms_quant << "ms " << (first_err == -1 ? "CORRECT" : "INCORRECT") << std::endl;
  7361. //
  7362. // if (first_err != -1) {
  7363. // std::cerr << "first_error = " << first_err << std::endl;
  7364. // std::cerr << "Actual result: " << std::endl << std::endl;
  7365. // 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) << " ";
  7366. // for (size_t j = 0; j < 32; j++) {
  7367. // std::cout << " qs" << j << "=" << (uint32_t)qx[first_err].qs[j] << " ";
  7368. // }
  7369. // std::cerr << std::endl << std::endl << "Expected result: " << std::endl << std::endl;
  7370. // 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) << " ";
  7371. // for (size_t j = 0; j < 32; j++) {
  7372. // std::cout << " qs" << j << "=" << (uint32_t)qx_res[first_err].qs[j] << " ";
  7373. // }
  7374. // std::cerr << std::endl;
  7375. // }
  7376. //
  7377. // ggml_vk_destroy_buffer(x_buf);
  7378. // ggml_vk_destroy_buffer(qx_buf);
  7379. //
  7380. // free(x);
  7381. // free(qx);
  7382. // free(qx_res);
  7383. // }
  7384. 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) {
  7385. VK_LOG_DEBUG("ggml_vk_test_dequant_matmul(" << m << ", " << n << ", " << k << ", " << batch << ", " << num_it << ", " << split_k << ", " << ggml_type_name(quant) << ")");
  7386. const size_t x_ne = m * k * batch;
  7387. const size_t y_ne = k * n * batch;
  7388. const size_t d_ne = m * n * batch;
  7389. vk_matmul_pipeline2 * pipelines;
  7390. if (mmq) {
  7391. pipelines = ctx->device->pipeline_dequant_mul_mat_mat_q8_1;
  7392. } else {
  7393. pipelines = ctx->device->pipeline_dequant_mul_mat_mat;
  7394. }
  7395. const bool fp16acc = ctx->device->fp16;
  7396. vk_pipeline p;
  7397. std::string shname;
  7398. if (shader_size == 0) {
  7399. p = fp16acc ? pipelines[quant].f16acc->a_s : pipelines[quant].f32acc->a_s;
  7400. shname = std::string(ggml_type_name(quant)) + "_ALIGNED_S";
  7401. } else if (shader_size == 1) {
  7402. p = fp16acc ? pipelines[quant].f16acc->a_m : pipelines[quant].f32acc->a_m;
  7403. shname = std::string(ggml_type_name(quant)) + "_ALIGNED_M";
  7404. } else if (shader_size == 2) {
  7405. p = fp16acc ? pipelines[quant].f16acc->a_l : pipelines[quant].f32acc->a_l;
  7406. shname = std::string(ggml_type_name(quant)) + "_ALIGNED_L";
  7407. } else {
  7408. GGML_ASSERT(0);
  7409. }
  7410. const size_t kpad = mmq ? 0 : ggml_vk_align_size(k, p->align);
  7411. if (mmq || k != kpad) {
  7412. if (shader_size == 0) {
  7413. p = fp16acc ? pipelines[quant].f16acc->s : pipelines[quant].f32acc->s;
  7414. shname = std::string(ggml_type_name(quant)) + "_S";
  7415. } else if (shader_size == 1) {
  7416. p = fp16acc ? pipelines[quant].f16acc->m : pipelines[quant].f32acc->m;
  7417. shname = std::string(ggml_type_name(quant)) + "_M";
  7418. } else if (shader_size == 2) {
  7419. p = fp16acc ? pipelines[quant].f16acc->l : pipelines[quant].f32acc->l;
  7420. shname = std::string(ggml_type_name(quant)) + "_L";
  7421. } else {
  7422. GGML_ASSERT(0);
  7423. }
  7424. }
  7425. if (p == nullptr) {
  7426. std::cerr << "error: no pipeline for ggml_vk_test_dequant_matmul " << ggml_type_name(quant) << std::endl;
  7427. return;
  7428. }
  7429. const size_t x_sz = sizeof(float) * x_ne;
  7430. const size_t y_sz = sizeof(float) * y_ne;
  7431. const size_t qx_sz = x_ne * ggml_type_size(quant)/ggml_blck_size(quant);
  7432. const size_t qy_sz = mmq ? y_ne * ggml_type_size(GGML_TYPE_Q8_1)/ggml_blck_size(GGML_TYPE_Q8_1) : y_sz;
  7433. const size_t d_sz = sizeof(float) * d_ne;
  7434. float * x = (float *) malloc(x_sz);
  7435. float * y = (float *) malloc(y_sz);
  7436. void * qx = malloc(qx_sz);
  7437. vk_buffer qx_buf = ggml_vk_create_buffer_check(ctx->device, qx_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7438. vk_buffer y_buf = ggml_vk_create_buffer_check(ctx->device, y_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7439. vk_buffer qy_buf = ggml_vk_create_buffer_check(ctx->device, qy_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7440. vk_buffer d_buf = ggml_vk_create_buffer_check(ctx->device, d_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7441. float * d = (float *) malloc(d_sz);
  7442. float * d_chk = (float *) malloc(d_sz);
  7443. for (size_t i = 0; i < x_ne; i++) {
  7444. x[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  7445. // x[i] = (i % k == i / k) ? 1.0f : 0.0f;
  7446. // x[i] = i % k;
  7447. }
  7448. ggml_vk_quantize_data(x, qx, x_ne, quant);
  7449. for (size_t i = 0; i < y_ne; i++) {
  7450. y[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  7451. // y[i] = (i % k == i / k) ? 1.0f : 0.0f;
  7452. // y[i] = i % k;
  7453. }
  7454. ggml_pipeline_request_descriptor_sets(ctx, p, num_it);
  7455. if (split_k > 1) {
  7456. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_matmul_split_k_reduce, num_it);
  7457. if (ctx->prealloc_split_k == nullptr || ctx->prealloc_split_k->size < sizeof(float) * d_ne * split_k) {
  7458. // Resize buffer
  7459. if (ctx->prealloc_split_k != nullptr) {
  7460. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  7461. }
  7462. ctx->prealloc_split_k = ggml_vk_create_buffer_check(ctx->device, sizeof(float) * d_ne * split_k, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7463. }
  7464. }
  7465. if (mmq) {
  7466. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_quantize_q8_1, num_it);
  7467. }
  7468. if (ctx->device->need_compiles) {
  7469. ggml_vk_load_shaders(ctx->device);
  7470. }
  7471. ggml_pipeline_allocate_descriptor_sets(ctx);
  7472. ggml_vk_buffer_write(qx_buf, 0, qx, qx_sz);
  7473. ggml_vk_buffer_write(y_buf, 0, y, y_sz);
  7474. vk_context subctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  7475. ggml_vk_ctx_begin(ctx->device, subctx);
  7476. if (mmq) {
  7477. for (size_t i = 0; i < num_it; i++) {
  7478. ggml_vk_quantize_q8_1(ctx, subctx, { y_buf, 0, y_sz }, { qy_buf, 0, qy_sz }, y_ne);
  7479. ggml_vk_matmul(
  7480. 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 },
  7481. m, n, k,
  7482. k, k, m, k*m, k*n, m*n,
  7483. split_k, batch, batch, batch, 1, 1, n
  7484. );
  7485. }
  7486. } else {
  7487. for (size_t i = 0; i < num_it; i++) {
  7488. ggml_vk_matmul(
  7489. 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 },
  7490. m, n, k,
  7491. k, k, m, k*m, k*n, m*n,
  7492. split_k, batch, batch, batch, 1, 1, n
  7493. );
  7494. }
  7495. }
  7496. ggml_vk_ctx_end(subctx);
  7497. auto begin = std::chrono::high_resolution_clock::now();
  7498. ggml_vk_submit(subctx, ctx->fence);
  7499. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_dequant waitForFences");
  7500. ctx->device->device.resetFences({ ctx->fence });
  7501. ggml_vk_queue_command_pools_cleanup(ctx->device);
  7502. auto end = std::chrono::high_resolution_clock::now();
  7503. double time_ms = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  7504. ggml_vk_buffer_read(d_buf, 0, d, d_sz);
  7505. ggml_init_params iparams = {
  7506. /*.mem_size =*/ 1024*1024*1024,
  7507. /*.mem_buffer =*/ NULL,
  7508. /*.no_alloc =*/ true,
  7509. };
  7510. ggml_context * ggml_ctx = ggml_init(iparams);
  7511. ggml_tensor * src0_ggml = ggml_new_tensor_3d(ggml_ctx, quant, k, m, batch);
  7512. ggml_tensor * src1_ggml = ggml_new_tensor_3d(ggml_ctx, GGML_TYPE_F32, k, n, batch);
  7513. ggml_tensor * tensor_ggml = ggml_mul_mat(ggml_ctx, src0_ggml, src1_ggml);
  7514. src0_ggml->data = qx;
  7515. src1_ggml->data = y;
  7516. tensor_ggml->data = d_chk;
  7517. ggml_cgraph * cgraph = ggml_new_graph(ggml_ctx);
  7518. ggml_build_forward_expand(cgraph, tensor_ggml);
  7519. ggml_graph_compute_with_ctx(ggml_ctx, cgraph, 1);
  7520. ggml_free(ggml_ctx);
  7521. double avg_err = 0.0;
  7522. int first_err_n = -1;
  7523. int first_err_m = -1;
  7524. int first_err_b = -1;
  7525. for (size_t i = 0; i < m*n*batch; i++) {
  7526. double err = std::fabs(d[i] - d_chk[i]);
  7527. avg_err += err;
  7528. if ((err > 0.05f || std::isnan(err)) && first_err_n == -1) {
  7529. first_err_b = i / (m * n);
  7530. first_err_n = (i % (m * n)) / m;
  7531. first_err_m = (i % (m * n)) % m;
  7532. }
  7533. }
  7534. avg_err /= m * n;
  7535. double tflops = 2.0*m*n*k*batch*num_it / (time_ms / 1000.0) / (1000.0*1000.0*1000.0*1000.0);
  7536. std::cerr << "TEST dequant matmul " << shname;
  7537. if (mmq) {
  7538. std::cerr << " mmq";
  7539. }
  7540. 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;
  7541. if (avg_err > 0.01 || std::isnan(avg_err)) {
  7542. std::cerr << "m = " << first_err_m << " n = " << first_err_n << " b = " << first_err_b << std::endl;
  7543. std::cerr << "Actual result: " << std::endl << std::endl;
  7544. ggml_vk_print_matrix_area(d, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  7545. std::cerr << std::endl;
  7546. std::cerr << "Expected result: " << std::endl << std::endl;
  7547. ggml_vk_print_matrix_area(d_chk, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  7548. std::cerr << "src0: " << std::endl << std::endl;
  7549. ggml_vk_print_matrix_area(x, GGML_TYPE_F32, k, m, first_err_m, first_err_n, first_err_b);
  7550. std::cerr << std::endl;
  7551. std::cerr << "src1: " << std::endl << std::endl;
  7552. ggml_vk_print_matrix_area(y, GGML_TYPE_F32, k, n, first_err_m, first_err_n, first_err_b);
  7553. if (split_k > 1) {
  7554. float * split_k_buf = (float *) malloc(sizeof(float) * d_ne * split_k);
  7555. ggml_vk_buffer_read(ctx->prealloc_split_k, 0, split_k_buf, sizeof(float) * d_ne * split_k);
  7556. std::cerr << "d_buf0: " << std::endl << std::endl;
  7557. ggml_vk_print_matrix_area(split_k_buf, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  7558. std::cerr << "d_buf1: " << std::endl << std::endl;
  7559. ggml_vk_print_matrix_area(split_k_buf + d_ne, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  7560. std::cerr << "d_buf2: " << std::endl << std::endl;
  7561. 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);
  7562. std::cerr << "d_buf3: " << std::endl << std::endl;
  7563. 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);
  7564. free(split_k_buf);
  7565. }
  7566. }
  7567. ggml_vk_destroy_buffer(qx_buf);
  7568. ggml_vk_destroy_buffer(y_buf);
  7569. ggml_vk_destroy_buffer(qy_buf);
  7570. ggml_vk_destroy_buffer(d_buf);
  7571. free(x);
  7572. free(qx);
  7573. free(y);
  7574. free(d);
  7575. free(d_chk);
  7576. }
  7577. #endif
  7578. static void ggml_vk_preallocate_buffers(ggml_backend_vk_context * ctx) {
  7579. #if defined(GGML_VULKAN_RUN_TESTS)
  7580. const std::vector<size_t> vals {
  7581. 512, 512, 128,
  7582. 128, 512, 512,
  7583. 4096, 512, 4096,
  7584. 11008, 512, 4096,
  7585. 4096, 512, 11008,
  7586. 32000, 512, 4096,
  7587. 8, 8, 8,
  7588. 100, 46, 576,
  7589. 623, 111, 128,
  7590. 100, 46, 558,
  7591. 512, 1, 256,
  7592. 128, 110, 622,
  7593. 511, 511, 127,
  7594. 511, 511, 7,
  7595. 511, 511, 17,
  7596. 49, 49, 128,
  7597. 128, 49, 49,
  7598. 4096, 49, 4096,
  7599. };
  7600. const size_t num_it = 100;
  7601. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 0, GGML_TYPE_Q4_0);
  7602. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 1, GGML_TYPE_Q4_0);
  7603. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 2, GGML_TYPE_Q4_0);
  7604. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 0, GGML_TYPE_Q4_0, true);
  7605. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 1, GGML_TYPE_Q4_0, true);
  7606. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 2, GGML_TYPE_Q4_0, true);
  7607. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 0, GGML_TYPE_Q8_0);
  7608. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 1, GGML_TYPE_Q8_0);
  7609. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 2, GGML_TYPE_Q8_0);
  7610. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 0, GGML_TYPE_Q8_0, true);
  7611. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 1, GGML_TYPE_Q8_0, true);
  7612. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 2, GGML_TYPE_Q8_0, true);
  7613. abort();
  7614. for (size_t i = 0; i < vals.size(); i += 3) {
  7615. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 0);
  7616. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 1);
  7617. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 2);
  7618. std::cerr << '\n';
  7619. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 0);
  7620. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 1);
  7621. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 2);
  7622. std::cerr << '\n';
  7623. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 0);
  7624. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 1);
  7625. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 2);
  7626. std::cerr << '\n' << std::endl;
  7627. if (vals[i + 2] % 32 == 0) {
  7628. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 0, GGML_TYPE_Q4_0);
  7629. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 1, GGML_TYPE_Q4_0);
  7630. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 2, GGML_TYPE_Q4_0);
  7631. std::cerr << '\n';
  7632. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 0, GGML_TYPE_Q4_0);
  7633. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 1, GGML_TYPE_Q4_0);
  7634. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 2, GGML_TYPE_Q4_0);
  7635. std::cerr << '\n';
  7636. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 0, GGML_TYPE_Q4_0);
  7637. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 1, GGML_TYPE_Q4_0);
  7638. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 2, GGML_TYPE_Q4_0);
  7639. std::cerr << '\n' << std::endl;
  7640. }
  7641. if (vals[i + 2] % 256 == 0) {
  7642. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 0, GGML_TYPE_Q4_K);
  7643. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 1, GGML_TYPE_Q4_K);
  7644. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 2, GGML_TYPE_Q4_K);
  7645. std::cerr << '\n';
  7646. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 0, GGML_TYPE_Q4_K);
  7647. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 1, GGML_TYPE_Q4_K);
  7648. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 2, GGML_TYPE_Q4_K);
  7649. std::cerr << '\n';
  7650. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 0, GGML_TYPE_Q4_K);
  7651. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 1, GGML_TYPE_Q4_K);
  7652. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 2, GGML_TYPE_Q4_K);
  7653. std::cerr << '\n' << std::endl;
  7654. }
  7655. }
  7656. GGML_ABORT("fatal error");
  7657. #endif
  7658. if (ctx->prealloc_x == nullptr || (ctx->prealloc_size_x > 0 && ctx->prealloc_x->size < ctx->prealloc_size_x)) {
  7659. VK_LOG_MEMORY("ggml_vk_preallocate_buffers(x_size: " << ctx->prealloc_size_x << ")");
  7660. // Resize buffer
  7661. if (ctx->prealloc_x != nullptr) {
  7662. ggml_vk_destroy_buffer(ctx->prealloc_x);
  7663. }
  7664. ctx->prealloc_x = ggml_vk_create_buffer_device(ctx->device, ctx->prealloc_size_x);
  7665. }
  7666. if (ctx->prealloc_y == nullptr || (ctx->prealloc_size_y > 0 && ctx->prealloc_y->size < ctx->prealloc_size_y)) {
  7667. VK_LOG_MEMORY("ggml_vk_preallocate_buffers(y_size: " << ctx->prealloc_size_y << ")");
  7668. // Resize buffer
  7669. if (ctx->prealloc_y != nullptr) {
  7670. ggml_vk_destroy_buffer(ctx->prealloc_y);
  7671. }
  7672. ctx->prealloc_y = ggml_vk_create_buffer_device(ctx->device, ctx->prealloc_size_y);
  7673. }
  7674. if (ctx->prealloc_split_k == nullptr || (ctx->prealloc_size_split_k > 0 && ctx->prealloc_split_k->size < ctx->prealloc_size_split_k)) {
  7675. VK_LOG_MEMORY("ggml_vk_preallocate_buffers(split_k_size: " << ctx->prealloc_size_split_k << ")");
  7676. // Resize buffer
  7677. if (ctx->prealloc_split_k != nullptr) {
  7678. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  7679. }
  7680. ctx->prealloc_split_k = ggml_vk_create_buffer_device(ctx->device, ctx->prealloc_size_split_k);
  7681. }
  7682. }
  7683. 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);
  7684. // Returns true if node has enqueued work into the queue, false otherwise
  7685. // If submit is true the current all operations queued so far are being submitted to Vulkan to overlap cmdlist creation and GPU execution.
  7686. 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){
  7687. ggml_tensor * node = cgraph->nodes[node_idx];
  7688. if (ggml_is_empty(node) || !node->buffer) {
  7689. return false;
  7690. }
  7691. VK_LOG_DEBUG("ggml_vk_build_graph(" << node << ", " << ggml_op_name(node->op) << ")");
  7692. ctx->semaphore_idx = 0;
  7693. const ggml_tensor * src0 = node->src[0];
  7694. const ggml_tensor * src1 = node->src[1];
  7695. const ggml_tensor * src2 = node->src[2];
  7696. const ggml_tensor * src3 = node->src[3];
  7697. switch (node->op) {
  7698. // Return on empty ops to avoid generating a compute_ctx and setting exit_tensor
  7699. case GGML_OP_RESHAPE:
  7700. case GGML_OP_VIEW:
  7701. case GGML_OP_PERMUTE:
  7702. case GGML_OP_TRANSPOSE:
  7703. case GGML_OP_NONE:
  7704. return false;
  7705. case GGML_OP_UNARY:
  7706. switch (ggml_get_unary_op(node)) {
  7707. case GGML_UNARY_OP_SILU:
  7708. case GGML_UNARY_OP_GELU:
  7709. case GGML_UNARY_OP_GELU_ERF:
  7710. case GGML_UNARY_OP_GELU_QUICK:
  7711. case GGML_UNARY_OP_RELU:
  7712. case GGML_UNARY_OP_TANH:
  7713. case GGML_UNARY_OP_SIGMOID:
  7714. break;
  7715. default:
  7716. return false;
  7717. }
  7718. break;
  7719. case GGML_OP_GLU:
  7720. switch (ggml_get_glu_op(node)) {
  7721. case GGML_GLU_OP_GEGLU:
  7722. case GGML_GLU_OP_REGLU:
  7723. case GGML_GLU_OP_SWIGLU:
  7724. case GGML_GLU_OP_GEGLU_ERF:
  7725. case GGML_GLU_OP_GEGLU_QUICK:
  7726. break;
  7727. default:
  7728. return false;
  7729. }
  7730. break;
  7731. case GGML_OP_REPEAT:
  7732. case GGML_OP_REPEAT_BACK:
  7733. case GGML_OP_GET_ROWS:
  7734. case GGML_OP_ADD:
  7735. case GGML_OP_ACC:
  7736. case GGML_OP_SUB:
  7737. case GGML_OP_MUL:
  7738. case GGML_OP_DIV:
  7739. case GGML_OP_CONCAT:
  7740. case GGML_OP_UPSCALE:
  7741. case GGML_OP_SCALE:
  7742. case GGML_OP_SQR:
  7743. case GGML_OP_SIN:
  7744. case GGML_OP_COS:
  7745. case GGML_OP_CLAMP:
  7746. case GGML_OP_PAD:
  7747. case GGML_OP_ROLL:
  7748. case GGML_OP_CPY:
  7749. case GGML_OP_SET_ROWS:
  7750. case GGML_OP_CONT:
  7751. case GGML_OP_DUP:
  7752. case GGML_OP_SILU_BACK:
  7753. case GGML_OP_NORM:
  7754. case GGML_OP_GROUP_NORM:
  7755. case GGML_OP_RMS_NORM:
  7756. case GGML_OP_RMS_NORM_BACK:
  7757. case GGML_OP_L2_NORM:
  7758. case GGML_OP_DIAG_MASK_INF:
  7759. case GGML_OP_SOFT_MAX:
  7760. case GGML_OP_SOFT_MAX_BACK:
  7761. case GGML_OP_ROPE:
  7762. case GGML_OP_ROPE_BACK:
  7763. case GGML_OP_MUL_MAT:
  7764. case GGML_OP_MUL_MAT_ID:
  7765. case GGML_OP_ARGSORT:
  7766. case GGML_OP_SUM:
  7767. case GGML_OP_SUM_ROWS:
  7768. case GGML_OP_ARGMAX:
  7769. case GGML_OP_COUNT_EQUAL:
  7770. case GGML_OP_IM2COL:
  7771. case GGML_OP_TIMESTEP_EMBEDDING:
  7772. case GGML_OP_CONV_TRANSPOSE_1D:
  7773. case GGML_OP_POOL_2D:
  7774. case GGML_OP_CONV_2D_DW:
  7775. case GGML_OP_RWKV_WKV6:
  7776. case GGML_OP_RWKV_WKV7:
  7777. case GGML_OP_LEAKY_RELU:
  7778. case GGML_OP_FLASH_ATTN_EXT:
  7779. case GGML_OP_OPT_STEP_ADAMW:
  7780. break;
  7781. default:
  7782. std::cerr << "ggml_vulkan: Error: Missing op: " << ggml_op_name(node->op) << std::endl;
  7783. GGML_ABORT("fatal error");
  7784. return false;
  7785. }
  7786. vk_context compute_ctx;
  7787. if (!dryrun) {
  7788. if (ctx->compute_ctx.expired()) {
  7789. compute_ctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  7790. ctx->compute_ctx = compute_ctx;
  7791. ggml_vk_ctx_begin(ctx->device, compute_ctx);
  7792. } else {
  7793. compute_ctx = ctx->compute_ctx.lock();
  7794. }
  7795. } else {
  7796. switch (node->op) {
  7797. case GGML_OP_REPEAT:
  7798. case GGML_OP_REPEAT_BACK:
  7799. case GGML_OP_ACC:
  7800. case GGML_OP_GET_ROWS:
  7801. case GGML_OP_ADD:
  7802. case GGML_OP_SUB:
  7803. case GGML_OP_MUL:
  7804. case GGML_OP_DIV:
  7805. case GGML_OP_CONCAT:
  7806. case GGML_OP_UPSCALE:
  7807. case GGML_OP_SCALE:
  7808. case GGML_OP_SQR:
  7809. case GGML_OP_SIN:
  7810. case GGML_OP_COS:
  7811. case GGML_OP_CLAMP:
  7812. case GGML_OP_PAD:
  7813. case GGML_OP_CPY:
  7814. case GGML_OP_SET_ROWS:
  7815. case GGML_OP_CONT:
  7816. case GGML_OP_DUP:
  7817. case GGML_OP_SILU_BACK:
  7818. case GGML_OP_NORM:
  7819. case GGML_OP_GROUP_NORM:
  7820. case GGML_OP_RMS_NORM:
  7821. case GGML_OP_RMS_NORM_BACK:
  7822. case GGML_OP_L2_NORM:
  7823. case GGML_OP_UNARY:
  7824. case GGML_OP_GLU:
  7825. case GGML_OP_DIAG_MASK_INF:
  7826. case GGML_OP_SOFT_MAX:
  7827. case GGML_OP_SOFT_MAX_BACK:
  7828. case GGML_OP_ROPE:
  7829. case GGML_OP_ROPE_BACK:
  7830. case GGML_OP_ARGSORT:
  7831. case GGML_OP_SUM:
  7832. case GGML_OP_SUM_ROWS:
  7833. case GGML_OP_ARGMAX:
  7834. case GGML_OP_COUNT_EQUAL:
  7835. case GGML_OP_IM2COL:
  7836. case GGML_OP_TIMESTEP_EMBEDDING:
  7837. case GGML_OP_CONV_TRANSPOSE_1D:
  7838. case GGML_OP_POOL_2D:
  7839. case GGML_OP_CONV_2D_DW:
  7840. case GGML_OP_LEAKY_RELU:
  7841. {
  7842. // These operations all go through ggml_vk_op_f32, so short-circuit and
  7843. // do the only thing needed for the dryrun.
  7844. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, src0, src1, src2, node, node->op);
  7845. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  7846. return false;
  7847. }
  7848. default:
  7849. break;
  7850. }
  7851. }
  7852. switch (node->op) {
  7853. case GGML_OP_REPEAT:
  7854. ggml_vk_repeat(ctx, compute_ctx, src0, node, dryrun);
  7855. break;
  7856. case GGML_OP_REPEAT_BACK:
  7857. ggml_vk_repeat_back(ctx, compute_ctx, src0, node, dryrun);
  7858. break;
  7859. case GGML_OP_ACC:
  7860. ggml_vk_acc(ctx, compute_ctx, src0, src1, node, dryrun);
  7861. break;
  7862. case GGML_OP_GET_ROWS:
  7863. ggml_vk_get_rows(ctx, compute_ctx, src0, src1, node, dryrun);
  7864. break;
  7865. case GGML_OP_ADD:
  7866. ggml_vk_add(ctx, compute_ctx, src0, src1, node, dryrun);
  7867. break;
  7868. case GGML_OP_SUB:
  7869. ggml_vk_sub(ctx, compute_ctx, src0, src1, node, dryrun);
  7870. break;
  7871. case GGML_OP_MUL:
  7872. ggml_vk_mul(ctx, compute_ctx, src0, src1, node, dryrun);
  7873. break;
  7874. case GGML_OP_DIV:
  7875. ggml_vk_div(ctx, compute_ctx, src0, src1, node, dryrun);
  7876. break;
  7877. case GGML_OP_CONCAT:
  7878. ggml_vk_concat(ctx, compute_ctx, src0, src1, node, dryrun);
  7879. break;
  7880. case GGML_OP_UPSCALE:
  7881. ggml_vk_upscale(ctx, compute_ctx, src0, node, dryrun);
  7882. break;
  7883. case GGML_OP_SCALE:
  7884. ggml_vk_scale(ctx, compute_ctx, src0, node, dryrun);
  7885. break;
  7886. case GGML_OP_SQR:
  7887. ggml_vk_sqr(ctx, compute_ctx, src0, node, dryrun);
  7888. break;
  7889. case GGML_OP_SIN:
  7890. ggml_vk_sin(ctx, compute_ctx, src0, node, dryrun);
  7891. break;
  7892. case GGML_OP_COS:
  7893. ggml_vk_cos(ctx, compute_ctx, src0, node, dryrun);
  7894. break;
  7895. case GGML_OP_CLAMP:
  7896. ggml_vk_clamp(ctx, compute_ctx, src0, node, dryrun);
  7897. break;
  7898. case GGML_OP_PAD:
  7899. ggml_vk_pad(ctx, compute_ctx, src0, node, dryrun);
  7900. break;
  7901. case GGML_OP_ROLL:
  7902. ggml_vk_roll(ctx, compute_ctx, src0, node, dryrun);
  7903. break;
  7904. case GGML_OP_CPY:
  7905. case GGML_OP_CONT:
  7906. case GGML_OP_DUP:
  7907. ggml_vk_cpy(ctx, compute_ctx, src0, node, dryrun);
  7908. break;
  7909. case GGML_OP_SET_ROWS:
  7910. ggml_vk_set_rows(ctx, compute_ctx, src0, src1, node, dryrun);
  7911. break;
  7912. case GGML_OP_SILU_BACK:
  7913. ggml_vk_silu_back(ctx, compute_ctx, src0, src1, node, dryrun);
  7914. break;
  7915. case GGML_OP_NORM:
  7916. ggml_vk_norm(ctx, compute_ctx, src0, node, dryrun);
  7917. break;
  7918. case GGML_OP_GROUP_NORM:
  7919. ggml_vk_group_norm(ctx, compute_ctx, src0, node, dryrun);
  7920. break;
  7921. case GGML_OP_RMS_NORM:
  7922. if (ctx->num_additional_fused_ops > 0) {
  7923. // fused rms_norm + mul
  7924. ggml_tensor *mul = cgraph->nodes[node_idx + 1];
  7925. ggml_tensor *other_src = mul->src[0] == node ? mul->src[1] : mul->src[0];
  7926. ggml_vk_rms_norm(ctx, compute_ctx, src0, other_src, mul, (float *)node->op_params, dryrun);
  7927. } else {
  7928. ggml_vk_rms_norm(ctx, compute_ctx, src0, src0, node, (float *)node->op_params, dryrun);
  7929. }
  7930. break;
  7931. case GGML_OP_RMS_NORM_BACK:
  7932. ggml_vk_rms_norm_back(ctx, compute_ctx, src0, src1, node, dryrun);
  7933. break;
  7934. case GGML_OP_L2_NORM:
  7935. ggml_vk_l2_norm(ctx, compute_ctx, src0, node, dryrun);
  7936. break;
  7937. case GGML_OP_UNARY:
  7938. switch (ggml_get_unary_op(node)) {
  7939. case GGML_UNARY_OP_SILU:
  7940. case GGML_UNARY_OP_GELU:
  7941. case GGML_UNARY_OP_GELU_ERF:
  7942. case GGML_UNARY_OP_GELU_QUICK:
  7943. case GGML_UNARY_OP_RELU:
  7944. case GGML_UNARY_OP_TANH:
  7945. case GGML_UNARY_OP_SIGMOID:
  7946. ggml_vk_unary(ctx, compute_ctx, src0, node, dryrun);
  7947. break;
  7948. default:
  7949. return false;
  7950. }
  7951. break;
  7952. case GGML_OP_GLU:
  7953. switch (ggml_get_glu_op(node)) {
  7954. case GGML_GLU_OP_GEGLU:
  7955. case GGML_GLU_OP_REGLU:
  7956. case GGML_GLU_OP_SWIGLU:
  7957. case GGML_GLU_OP_GEGLU_ERF:
  7958. case GGML_GLU_OP_GEGLU_QUICK:
  7959. ggml_vk_glu(ctx, compute_ctx, src0, src1, node, dryrun);
  7960. break;
  7961. default:
  7962. return false;
  7963. }
  7964. break;
  7965. case GGML_OP_DIAG_MASK_INF:
  7966. ggml_vk_diag_mask_inf(ctx, compute_ctx, src0, node, dryrun);
  7967. break;
  7968. case GGML_OP_SOFT_MAX:
  7969. ggml_vk_soft_max(ctx, compute_ctx, src0, src1, node, dryrun);
  7970. break;
  7971. case GGML_OP_SOFT_MAX_BACK:
  7972. ggml_vk_soft_max_back(ctx, compute_ctx, src0, src1, node, dryrun);
  7973. break;
  7974. case GGML_OP_ROPE:
  7975. ggml_vk_rope(ctx, compute_ctx, src0, src1, src2, node, false, dryrun);
  7976. break;
  7977. case GGML_OP_ROPE_BACK:
  7978. ggml_vk_rope(ctx, compute_ctx, src0, src1, src2, node, true, dryrun);
  7979. break;
  7980. case GGML_OP_ARGSORT:
  7981. ggml_vk_argsort(ctx, compute_ctx, src0, node, dryrun);
  7982. break;
  7983. case GGML_OP_SUM:
  7984. ggml_vk_sum(ctx, compute_ctx, src0, node, dryrun);
  7985. break;
  7986. case GGML_OP_SUM_ROWS:
  7987. ggml_vk_sum_rows(ctx, compute_ctx, src0, node, dryrun);
  7988. break;
  7989. case GGML_OP_ARGMAX:
  7990. ggml_vk_argmax(ctx, compute_ctx, src0, node, dryrun);
  7991. break;
  7992. case GGML_OP_COUNT_EQUAL:
  7993. ggml_vk_count_equal(ctx, compute_ctx, src0, src1, node, dryrun);
  7994. break;
  7995. case GGML_OP_IM2COL:
  7996. ggml_vk_im2col(ctx, compute_ctx, src0, src1, node, dryrun);
  7997. break;
  7998. case GGML_OP_TIMESTEP_EMBEDDING:
  7999. ggml_vk_timestep_embedding(ctx, compute_ctx, src0, node, dryrun);
  8000. break;
  8001. case GGML_OP_CONV_TRANSPOSE_1D:
  8002. ggml_vk_conv_transpose_1d(ctx, compute_ctx, src0, src1, node, dryrun);
  8003. break;
  8004. case GGML_OP_POOL_2D:
  8005. ggml_vk_pool_2d(ctx, compute_ctx, src0, node, dryrun);
  8006. break;
  8007. case GGML_OP_CONV_2D_DW:
  8008. ggml_vk_conv_2d_dw(ctx, compute_ctx, src0, src1, node, dryrun);
  8009. break;
  8010. case GGML_OP_LEAKY_RELU:
  8011. ggml_vk_leaky_relu(ctx, compute_ctx, src0, node, dryrun);
  8012. break;
  8013. case GGML_OP_MUL_MAT:
  8014. ggml_vk_mul_mat(ctx, compute_ctx, src0, src1, node, dryrun);
  8015. break;
  8016. case GGML_OP_MUL_MAT_ID:
  8017. ggml_vk_mul_mat_id(ctx, compute_ctx, src0, src1, src2, node, dryrun);
  8018. break;
  8019. case GGML_OP_FLASH_ATTN_EXT:
  8020. ggml_vk_flash_attn(ctx, compute_ctx, src0, src1, src2, src3, node, dryrun);
  8021. break;
  8022. case GGML_OP_RWKV_WKV6:
  8023. ggml_vk_rwkv_wkv6(ctx, compute_ctx, node, dryrun);
  8024. break;
  8025. case GGML_OP_RWKV_WKV7:
  8026. ggml_vk_rwkv_wkv7(ctx, compute_ctx, node, dryrun);
  8027. break;
  8028. case GGML_OP_OPT_STEP_ADAMW:
  8029. ggml_vk_opt_step_adamw(ctx, compute_ctx, node, dryrun);
  8030. break;
  8031. default:
  8032. return false;
  8033. }
  8034. if (dryrun) {
  8035. return false;
  8036. }
  8037. ctx->tensor_ctxs[node_idx] = compute_ctx;
  8038. #if defined(GGML_VULKAN_CHECK_RESULTS)
  8039. // Force context reset on each node so that each tensor ends up in its own context
  8040. // and can be run and compared to its CPU equivalent separately
  8041. last_node = true;
  8042. #endif
  8043. if (submit || last_node) {
  8044. ggml_vk_ctx_end(compute_ctx);
  8045. // TODO probably it'd be better to pass a exit_node flag to ggml_vk_compute_forward
  8046. if (last_node) {
  8047. compute_ctx->exit_tensor_idx = node_idx_begin;
  8048. }
  8049. else {
  8050. compute_ctx->exit_tensor_idx = -1;
  8051. }
  8052. ctx->compute_ctx.reset();
  8053. bool ok = ggml_vk_compute_forward(ctx, cgraph, node_begin, node_idx_begin, false, almost_ready);
  8054. if (!ok) {
  8055. if (node->op == GGML_OP_UNARY) {
  8056. 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;
  8057. } else if (node->op == GGML_OP_GLU) {
  8058. 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;
  8059. } else {
  8060. std::cerr << __func__ << ": error: op not supported " << node->name << " (" << ggml_op_name(node->op) << ")" << std::endl;
  8061. }
  8062. }
  8063. }
  8064. return true;
  8065. }
  8066. 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) {
  8067. GGML_UNUSED(cgraph);
  8068. ggml_backend_buffer * buf = nullptr;
  8069. switch (tensor->op) {
  8070. case GGML_OP_ADD:
  8071. case GGML_OP_ACC:
  8072. case GGML_OP_GET_ROWS:
  8073. case GGML_OP_SUB:
  8074. case GGML_OP_MUL:
  8075. case GGML_OP_DIV:
  8076. case GGML_OP_CONCAT:
  8077. case GGML_OP_UPSCALE:
  8078. case GGML_OP_SCALE:
  8079. case GGML_OP_SQR:
  8080. case GGML_OP_SIN:
  8081. case GGML_OP_COS:
  8082. case GGML_OP_CLAMP:
  8083. case GGML_OP_PAD:
  8084. case GGML_OP_ROLL:
  8085. case GGML_OP_CPY:
  8086. case GGML_OP_SET_ROWS:
  8087. case GGML_OP_CONT:
  8088. case GGML_OP_DUP:
  8089. case GGML_OP_SILU_BACK:
  8090. case GGML_OP_NORM:
  8091. case GGML_OP_GROUP_NORM:
  8092. case GGML_OP_RMS_NORM:
  8093. case GGML_OP_RMS_NORM_BACK:
  8094. case GGML_OP_L2_NORM:
  8095. case GGML_OP_DIAG_MASK_INF:
  8096. case GGML_OP_SOFT_MAX:
  8097. case GGML_OP_SOFT_MAX_BACK:
  8098. case GGML_OP_ROPE:
  8099. case GGML_OP_ROPE_BACK:
  8100. case GGML_OP_RESHAPE:
  8101. case GGML_OP_VIEW:
  8102. case GGML_OP_PERMUTE:
  8103. case GGML_OP_TRANSPOSE:
  8104. case GGML_OP_NONE:
  8105. case GGML_OP_ARGSORT:
  8106. case GGML_OP_SUM:
  8107. case GGML_OP_SUM_ROWS:
  8108. case GGML_OP_ARGMAX:
  8109. case GGML_OP_COUNT_EQUAL:
  8110. case GGML_OP_IM2COL:
  8111. case GGML_OP_TIMESTEP_EMBEDDING:
  8112. case GGML_OP_CONV_TRANSPOSE_1D:
  8113. case GGML_OP_POOL_2D:
  8114. case GGML_OP_CONV_2D_DW:
  8115. case GGML_OP_RWKV_WKV6:
  8116. case GGML_OP_RWKV_WKV7:
  8117. case GGML_OP_LEAKY_RELU:
  8118. case GGML_OP_REPEAT:
  8119. case GGML_OP_REPEAT_BACK:
  8120. case GGML_OP_OPT_STEP_ADAMW:
  8121. buf = tensor->buffer;
  8122. break;
  8123. case GGML_OP_UNARY:
  8124. switch (ggml_get_unary_op(tensor)) {
  8125. case GGML_UNARY_OP_SILU:
  8126. case GGML_UNARY_OP_GELU:
  8127. case GGML_UNARY_OP_GELU_ERF:
  8128. case GGML_UNARY_OP_GELU_QUICK:
  8129. case GGML_UNARY_OP_RELU:
  8130. case GGML_UNARY_OP_TANH:
  8131. case GGML_UNARY_OP_SIGMOID:
  8132. buf = tensor->buffer;
  8133. break;
  8134. default:
  8135. return false;
  8136. }
  8137. break;
  8138. case GGML_OP_GLU:
  8139. switch (ggml_get_glu_op(tensor)) {
  8140. case GGML_GLU_OP_GEGLU:
  8141. case GGML_GLU_OP_REGLU:
  8142. case GGML_GLU_OP_SWIGLU:
  8143. case GGML_GLU_OP_GEGLU_ERF:
  8144. case GGML_GLU_OP_GEGLU_QUICK:
  8145. buf = tensor->buffer;
  8146. break;
  8147. default:
  8148. return false;
  8149. }
  8150. break;
  8151. case GGML_OP_MUL_MAT:
  8152. case GGML_OP_MUL_MAT_ID:
  8153. case GGML_OP_FLASH_ATTN_EXT:
  8154. buf = tensor->buffer;
  8155. break;
  8156. default:
  8157. return false;
  8158. }
  8159. if (buf == nullptr) {
  8160. return false;
  8161. }
  8162. 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 << ")");
  8163. vk_context subctx = ctx->tensor_ctxs[tensor_idx].lock();
  8164. // always wait for the GPU work to be done for the last submit
  8165. if (tensor_idx == subctx->exit_tensor_idx) {
  8166. use_fence = true;
  8167. }
  8168. // Only run if ctx hasn't been submitted yet
  8169. if (!subctx->seqs.empty()) {
  8170. #ifdef GGML_VULKAN_CHECK_RESULTS
  8171. ggml_vk_check_results_0(ctx, cgraph, tensor_idx);
  8172. use_fence = true;
  8173. #endif
  8174. // Do staging buffer copies
  8175. for (auto& cpy : subctx->in_memcpys) {
  8176. memcpy(cpy.dst, cpy.src, cpy.n);
  8177. }
  8178. if (almost_ready && !ctx->almost_ready_fence_pending && !use_fence) {
  8179. ggml_vk_submit(subctx, ctx->almost_ready_fence);
  8180. ctx->almost_ready_fence_pending = true;
  8181. } else {
  8182. ggml_vk_submit(subctx, use_fence ? ctx->fence : vk::Fence{});
  8183. }
  8184. if (use_fence) {
  8185. ggml_vk_wait_for_fence(ctx);
  8186. }
  8187. #ifdef GGML_VULKAN_CHECK_RESULTS
  8188. ggml_vk_check_results_1(ctx, cgraph, tensor_idx);
  8189. #endif
  8190. }
  8191. if (tensor_idx == subctx->exit_tensor_idx) {
  8192. // Do staging buffer copies
  8193. for (auto& cpy : subctx->out_memcpys) {
  8194. memcpy(cpy.dst, cpy.src, cpy.n);
  8195. }
  8196. subctx->in_memcpys.clear();
  8197. subctx->out_memcpys.clear();
  8198. }
  8199. return true;
  8200. }
  8201. // Clean up after graph processing is done
  8202. static void ggml_vk_graph_cleanup(ggml_backend_vk_context * ctx) {
  8203. VK_LOG_DEBUG("ggml_vk_graph_cleanup()");
  8204. for (auto& buffer : ctx->gc.temp_buffers) {
  8205. ggml_vk_pool_free(ctx, buffer);
  8206. }
  8207. ctx->gc.temp_buffers.clear();
  8208. ggml_vk_command_pool_cleanup(ctx->device, ctx->compute_cmd_pool);
  8209. ggml_vk_command_pool_cleanup(ctx->device, ctx->transfer_cmd_pool);
  8210. for (size_t i = 0; i < ctx->gc.semaphores.size(); i++) {
  8211. ctx->device->device.destroySemaphore({ ctx->gc.semaphores[i].s });
  8212. }
  8213. ctx->gc.semaphores.clear();
  8214. for (size_t i = 0; i < ctx->gc.tl_semaphores.size(); i++) {
  8215. ctx->device->device.destroySemaphore({ ctx->gc.tl_semaphores[i].s });
  8216. }
  8217. ctx->gc.tl_semaphores.clear();
  8218. ctx->semaphore_idx = 0;
  8219. ctx->event_idx = 0;
  8220. for (auto& event : ctx->gc.events) {
  8221. ctx->device->device.resetEvent(event);
  8222. }
  8223. ctx->tensor_ctxs.clear();
  8224. ctx->gc.contexts.clear();
  8225. ctx->pipeline_descriptor_set_requirements = 0;
  8226. ctx->descriptor_set_idx = 0;
  8227. }
  8228. // Clean up on backend free
  8229. static void ggml_vk_cleanup(ggml_backend_vk_context * ctx) {
  8230. VK_LOG_DEBUG("ggml_vk_cleanup(" << ctx->name << ")");
  8231. ggml_vk_graph_cleanup(ctx);
  8232. ggml_vk_destroy_buffer(ctx->prealloc_x);
  8233. ggml_vk_destroy_buffer(ctx->prealloc_y);
  8234. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  8235. for (auto& buffer : ctx->buffer_pool) {
  8236. ggml_vk_destroy_buffer(buffer);
  8237. }
  8238. ctx->prealloc_size_x = 0;
  8239. ctx->prealloc_size_y = 0;
  8240. ctx->prealloc_size_split_k = 0;
  8241. for (auto& event : ctx->gc.events) {
  8242. ctx->device->device.destroyEvent(event);
  8243. }
  8244. ctx->gc.events.clear();
  8245. ctx->device->device.destroyFence(ctx->fence);
  8246. ctx->device->device.destroyFence(ctx->almost_ready_fence);
  8247. for (auto& pool : ctx->descriptor_pools) {
  8248. ctx->device->device.destroyDescriptorPool(pool);
  8249. }
  8250. ctx->descriptor_pools.clear();
  8251. ctx->descriptor_sets.clear();
  8252. ctx->compute_cmd_pool.destroy(ctx->device->device);
  8253. ctx->transfer_cmd_pool.destroy(ctx->device->device);
  8254. }
  8255. static int ggml_vk_get_device_count() {
  8256. ggml_vk_instance_init();
  8257. return vk_instance.device_indices.size();
  8258. }
  8259. static void ggml_vk_get_device_description(int device, char * description, size_t description_size) {
  8260. ggml_vk_instance_init();
  8261. std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
  8262. vk::PhysicalDeviceProperties props;
  8263. devices[device].getProperties(&props);
  8264. snprintf(description, description_size, "%s", props.deviceName.data());
  8265. }
  8266. // backend interface
  8267. #define UNUSED GGML_UNUSED
  8268. // device backend
  8269. static bool ggml_backend_buffer_is_vk(ggml_backend_buffer_t buffer) {
  8270. return buffer->buft->iface.get_name == ggml_backend_vk_buffer_type_name;
  8271. }
  8272. static void ggml_backend_vk_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  8273. VK_LOG_MEMORY("ggml_backend_vk_buffer_free_buffer()");
  8274. ggml_backend_vk_buffer_context * ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  8275. ggml_vk_destroy_buffer(ctx->dev_buffer);
  8276. delete ctx;
  8277. }
  8278. static void * ggml_backend_vk_buffer_get_base(ggml_backend_buffer_t buffer) {
  8279. return vk_ptr_base;
  8280. UNUSED(buffer);
  8281. }
  8282. static enum ggml_status ggml_backend_vk_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
  8283. VK_LOG_DEBUG("ggml_backend_vk_buffer_init_tensor(" << buffer << " (" << buffer->context << "), " << tensor << ")");
  8284. if (tensor->view_src != nullptr) {
  8285. GGML_ASSERT(tensor->view_src->buffer->buft == buffer->buft);
  8286. }
  8287. return GGML_STATUS_SUCCESS;
  8288. }
  8289. 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) {
  8290. VK_LOG_DEBUG("ggml_backend_vk_buffer_memset_tensor(" << buffer << ", " << tensor << ", " << value << ", " << offset << ", " << size << ")");
  8291. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  8292. vk_buffer buf = buf_ctx->dev_buffer;
  8293. uint32_t val32 = (uint32_t)value * 0x01010101;
  8294. ggml_vk_buffer_memset(buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, val32, size);
  8295. }
  8296. 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) {
  8297. VK_LOG_DEBUG("ggml_backend_vk_buffer_set_tensor(" << buffer << ", " << tensor << ", " << data << ", " << offset << ", " << size << ")");
  8298. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  8299. vk_buffer buf = buf_ctx->dev_buffer;
  8300. ggml_vk_buffer_write(buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  8301. }
  8302. 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) {
  8303. VK_LOG_DEBUG("ggml_backend_vk_buffer_get_tensor(" << buffer << ", " << tensor << ", " << data << ", " << offset << ", " << size << ")");
  8304. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  8305. vk_buffer buf = buf_ctx->dev_buffer;
  8306. ggml_vk_buffer_read(buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  8307. }
  8308. static bool ggml_backend_vk_buffer_cpy_tensor(ggml_backend_buffer_t buffer, const ggml_tensor * src, ggml_tensor * dst) {
  8309. if (ggml_backend_buffer_is_vk(src->buffer)) {
  8310. ggml_backend_vk_buffer_context * src_buf_ctx = (ggml_backend_vk_buffer_context *)src->buffer->context;
  8311. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  8312. vk_buffer src_buf = src_buf_ctx->dev_buffer;
  8313. vk_buffer dst_buf = dst_buf_ctx->dev_buffer;
  8314. 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));
  8315. return true;
  8316. }
  8317. return false;
  8318. UNUSED(buffer);
  8319. }
  8320. static void ggml_backend_vk_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  8321. ggml_backend_vk_buffer_context * ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  8322. ggml_vk_buffer_memset(ctx->dev_buffer, 0, value, buffer->size);
  8323. }
  8324. static ggml_backend_buffer_i ggml_backend_vk_buffer_interface = {
  8325. /* .free_buffer = */ ggml_backend_vk_buffer_free_buffer,
  8326. /* .get_base = */ ggml_backend_vk_buffer_get_base,
  8327. /* .init_tensor = */ ggml_backend_vk_buffer_init_tensor,
  8328. /* .memset_tensor = */ ggml_backend_vk_buffer_memset_tensor,
  8329. /* .set_tensor = */ ggml_backend_vk_buffer_set_tensor,
  8330. /* .get_tensor = */ ggml_backend_vk_buffer_get_tensor,
  8331. /* .cpy_tensor = */ ggml_backend_vk_buffer_cpy_tensor,
  8332. /* .clear = */ ggml_backend_vk_buffer_clear,
  8333. /* .reset = */ NULL,
  8334. };
  8335. // vk buffer type
  8336. static const char * ggml_backend_vk_buffer_type_name(ggml_backend_buffer_type_t buft) {
  8337. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *)buft->context;
  8338. return ctx->name.c_str();
  8339. }
  8340. static ggml_backend_buffer_t ggml_backend_vk_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  8341. VK_LOG_MEMORY("ggml_backend_vk_buffer_type_alloc_buffer(" << size << ")");
  8342. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *) buft->context;
  8343. vk_buffer dev_buffer = nullptr;
  8344. try {
  8345. dev_buffer = ggml_vk_create_buffer_device(ctx->device, size);
  8346. } catch (const vk::SystemError& e) {
  8347. return nullptr;
  8348. }
  8349. ggml_backend_vk_buffer_context * bufctx = new ggml_backend_vk_buffer_context(ctx->device, std::move(dev_buffer), ctx->name);
  8350. return ggml_backend_buffer_init(buft, ggml_backend_vk_buffer_interface, bufctx, size);
  8351. }
  8352. static size_t ggml_backend_vk_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  8353. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *) buft->context;
  8354. return ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  8355. }
  8356. static size_t ggml_backend_vk_buffer_type_get_max_size(ggml_backend_buffer_type_t buft) {
  8357. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *) buft->context;
  8358. return ctx->device->suballocation_block_size;
  8359. }
  8360. static size_t ggml_backend_vk_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  8361. return ggml_nbytes(tensor);
  8362. UNUSED(buft);
  8363. }
  8364. ggml_backend_buffer_type_t ggml_backend_vk_buffer_type(size_t dev_num) {
  8365. ggml_vk_instance_init();
  8366. VK_LOG_DEBUG("ggml_backend_vk_buffer_type(" << dev_num << ")");
  8367. vk_device dev = ggml_vk_get_device(dev_num);
  8368. return &dev->buffer_type;
  8369. }
  8370. // host buffer type
  8371. static const char * ggml_backend_vk_host_buffer_type_name(ggml_backend_buffer_type_t buft) {
  8372. return GGML_VK_NAME "_Host";
  8373. UNUSED(buft);
  8374. }
  8375. static const char * ggml_backend_vk_host_buffer_name(ggml_backend_buffer_t buffer) {
  8376. return GGML_VK_NAME "_Host";
  8377. UNUSED(buffer);
  8378. }
  8379. static void ggml_backend_vk_host_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  8380. VK_LOG_MEMORY("ggml_backend_vk_host_buffer_free_buffer()");
  8381. ggml_vk_host_free(vk_instance.devices[0], buffer->context);
  8382. }
  8383. static ggml_backend_buffer_t ggml_backend_vk_host_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  8384. VK_LOG_MEMORY("ggml_backend_vk_host_buffer_type_alloc_buffer(" << size << ")");
  8385. size += 32; // Behave like the CPU buffer type
  8386. void * ptr = nullptr;
  8387. try {
  8388. ptr = ggml_vk_host_malloc(vk_instance.devices[0], size);
  8389. } catch (vk::SystemError& e) {
  8390. GGML_LOG_WARN("ggml_vulkan: Failed to allocate pinned memory (%s)\n", e.what());
  8391. // fallback to cpu buffer
  8392. return ggml_backend_buft_alloc_buffer(ggml_backend_cpu_buffer_type(), size);
  8393. }
  8394. ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size);
  8395. buffer->buft = buft;
  8396. buffer->iface.free_buffer = ggml_backend_vk_host_buffer_free_buffer;
  8397. return buffer;
  8398. UNUSED(buft);
  8399. }
  8400. static size_t ggml_backend_vk_host_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  8401. return vk_instance.devices[0]->properties.limits.minMemoryMapAlignment;
  8402. UNUSED(buft);
  8403. }
  8404. static size_t ggml_backend_vk_host_buffer_type_get_max_size(ggml_backend_buffer_type_t buft) {
  8405. return vk_instance.devices[0]->suballocation_block_size;
  8406. UNUSED(buft);
  8407. }
  8408. // Should be changed to return device-specific host buffer type
  8409. // but that probably requires changes in llama.cpp
  8410. ggml_backend_buffer_type_t ggml_backend_vk_host_buffer_type() {
  8411. static struct ggml_backend_buffer_type ggml_backend_vk_buffer_type_host = {
  8412. /* .iface = */ {
  8413. /* .get_name = */ ggml_backend_vk_host_buffer_type_name,
  8414. /* .alloc_buffer = */ ggml_backend_vk_host_buffer_type_alloc_buffer,
  8415. /* .get_alignment = */ ggml_backend_vk_host_buffer_type_get_alignment,
  8416. /* .get_max_size = */ ggml_backend_vk_host_buffer_type_get_max_size,
  8417. /* .get_alloc_size = */ ggml_backend_cpu_buffer_type()->iface.get_alloc_size,
  8418. /* .is_host = */ ggml_backend_cpu_buffer_type()->iface.is_host,
  8419. },
  8420. /* .device = */ ggml_backend_reg_dev_get(ggml_backend_vk_reg(), 0),
  8421. /* .context = */ nullptr,
  8422. };
  8423. // Make sure device 0 is initialized
  8424. ggml_vk_instance_init();
  8425. ggml_vk_get_device(0);
  8426. return &ggml_backend_vk_buffer_type_host;
  8427. }
  8428. // backend
  8429. static const char * ggml_backend_vk_name(ggml_backend_t backend) {
  8430. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  8431. return ctx->name.c_str();
  8432. }
  8433. static void ggml_backend_vk_free(ggml_backend_t backend) {
  8434. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  8435. VK_LOG_DEBUG("ggml_backend_vk_free(" << ctx->name << ")");
  8436. ggml_vk_cleanup(ctx);
  8437. delete ctx;
  8438. delete backend;
  8439. }
  8440. static ggml_backend_buffer_type_t ggml_backend_vk_get_default_buffer_type(ggml_backend_t backend) {
  8441. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  8442. return &ctx->device->buffer_type;
  8443. }
  8444. static void ggml_backend_vk_set_tensor_async(ggml_backend_t backend, ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
  8445. VK_LOG_DEBUG("ggml_backend_vk_set_tensor_async(" << size << ")");
  8446. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  8447. 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");
  8448. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  8449. vk_context transfer_ctx;
  8450. if (ctx->transfer_ctx.expired()) {
  8451. // Initialize new transfer context
  8452. transfer_ctx = ggml_vk_create_context(ctx, ctx->transfer_cmd_pool);
  8453. ctx->transfer_ctx = transfer_ctx;
  8454. ggml_vk_ctx_begin(ctx->device, transfer_ctx);
  8455. } else {
  8456. transfer_ctx = ctx->transfer_ctx.lock();
  8457. }
  8458. vk_buffer buf = buf_ctx->dev_buffer;
  8459. ggml_vk_buffer_write_async(transfer_ctx, buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  8460. }
  8461. static void ggml_backend_vk_get_tensor_async(ggml_backend_t backend, const ggml_tensor * tensor, void * data, size_t offset, size_t size) {
  8462. VK_LOG_DEBUG("ggml_backend_vk_get_tensor_async(" << size << ")");
  8463. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  8464. 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");
  8465. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  8466. vk_context transfer_ctx;
  8467. if (ctx->transfer_ctx.expired()) {
  8468. // Initialize new transfer context
  8469. transfer_ctx = ggml_vk_create_context(ctx, ctx->transfer_cmd_pool);
  8470. ctx->transfer_ctx = transfer_ctx;
  8471. ggml_vk_ctx_begin(ctx->device, transfer_ctx);
  8472. } else {
  8473. transfer_ctx = ctx->transfer_ctx.lock();
  8474. }
  8475. vk_buffer buf = buf_ctx->dev_buffer;
  8476. ggml_vk_buffer_read_async(transfer_ctx, buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  8477. }
  8478. static bool ggml_backend_vk_cpy_tensor_async(ggml_backend_t backend, const ggml_tensor * src, ggml_tensor * dst) {
  8479. VK_LOG_DEBUG("ggml_backend_vk_cpy_tensor_async()");
  8480. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  8481. 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)) {
  8482. ggml_backend_vk_buffer_context * src_buf_ctx = (ggml_backend_vk_buffer_context *)src->buffer->context;
  8483. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  8484. vk_context transfer_ctx;
  8485. if (ctx->transfer_ctx.expired()) {
  8486. // Initialize new transfer context
  8487. transfer_ctx = ggml_vk_create_context(ctx, ctx->transfer_cmd_pool);
  8488. ctx->transfer_ctx = transfer_ctx;
  8489. ggml_vk_ctx_begin(ctx->device, transfer_ctx);
  8490. } else {
  8491. transfer_ctx = ctx->transfer_ctx.lock();
  8492. }
  8493. vk_buffer src_buf = src_buf_ctx->dev_buffer;
  8494. vk_buffer dst_buf = dst_buf_ctx->dev_buffer;
  8495. 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));
  8496. return true;
  8497. }
  8498. return false;
  8499. }
  8500. static void ggml_backend_vk_synchronize(ggml_backend_t backend) {
  8501. VK_LOG_DEBUG("ggml_backend_vk_synchronize()");
  8502. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  8503. if(ctx->transfer_ctx.expired()) {
  8504. return;
  8505. }
  8506. vk_context transfer_ctx = ctx->transfer_ctx.lock();
  8507. ggml_vk_ctx_end(transfer_ctx);
  8508. for (auto& cpy : transfer_ctx->in_memcpys) {
  8509. memcpy(cpy.dst, cpy.src, cpy.n);
  8510. }
  8511. ggml_vk_submit(transfer_ctx, ctx->fence);
  8512. ggml_vk_wait_for_fence(ctx);
  8513. for (auto& cpy : transfer_ctx->out_memcpys) {
  8514. memcpy(cpy.dst, cpy.src, cpy.n);
  8515. }
  8516. ctx->transfer_ctx.reset();
  8517. }
  8518. static bool ggml_vk_is_empty(ggml_tensor * node) {
  8519. 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;
  8520. }
  8521. static bool ggml_vk_can_fuse(const struct ggml_cgraph * cgraph, int node_idx, std::initializer_list<enum ggml_op> ops) {
  8522. if (!ggml_can_fuse(cgraph, node_idx, ops)) {
  8523. return false;
  8524. }
  8525. if (ops.size() == 2 && ops.begin()[0] == GGML_OP_RMS_NORM && ops.begin()[1] == GGML_OP_MUL) {
  8526. // additional constraints specific to this fusion
  8527. const ggml_tensor *rms_norm = cgraph->nodes[node_idx];
  8528. const ggml_tensor *mul = cgraph->nodes[node_idx + 1];
  8529. GGML_ASSERT(rms_norm->src[0]->type == GGML_TYPE_F32);
  8530. GGML_ASSERT(rms_norm->type == GGML_TYPE_F32);
  8531. // rms_norm only supports f32
  8532. if (mul->src[0]->type != GGML_TYPE_F32 ||
  8533. mul->src[1]->type != GGML_TYPE_F32 ||
  8534. mul->type != GGML_TYPE_F32) {
  8535. return false;
  8536. }
  8537. // if rms_norm is the B operand, then we don't handle broadcast
  8538. if (rms_norm == mul->src[1] &&
  8539. mul->src[0]->ne[1] != rms_norm->ne[1]) {
  8540. return false;
  8541. }
  8542. // rms_norm shader assumes contiguous rows
  8543. if (!ggml_is_contiguous_rows(mul->src[0]) || !ggml_is_contiguous_rows(mul->src[1])) {
  8544. return false;
  8545. }
  8546. }
  8547. return true;
  8548. }
  8549. static ggml_status ggml_backend_vk_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
  8550. VK_LOG_DEBUG("ggml_backend_vk_graph_compute(" << cgraph->n_nodes << " nodes)");
  8551. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  8552. if (vk_instance.debug_utils_support) {
  8553. vk::DebugUtilsLabelEXT dul = {};
  8554. dul.pLabelName = "ggml_backend_vk_graph_compute";
  8555. dul.color = std::array<float,4>{1.0f, 1.0f, 1.0f, 1.0f};
  8556. vk_instance.pfn_vkQueueBeginDebugUtilsLabelEXT(ctx->device->compute_queue.queue, reinterpret_cast<VkDebugUtilsLabelEXT*>(&dul));
  8557. }
  8558. uint64_t total_mat_mul_bytes = 0;
  8559. for (int i = 0; i < cgraph->n_nodes; i++) {
  8560. if (!ctx->device->disable_fusion && ggml_vk_can_fuse(cgraph, i, { GGML_OP_RMS_NORM, GGML_OP_MUL })) {
  8561. ctx->num_additional_fused_ops = 1;
  8562. }
  8563. ggml_vk_build_graph(ctx, cgraph, i, nullptr, 0, true, false, false, false);
  8564. if (cgraph->nodes[i]->op == GGML_OP_MUL_MAT || cgraph->nodes[i]->op == GGML_OP_MUL_MAT_ID) {
  8565. total_mat_mul_bytes += ggml_nbytes(cgraph->nodes[i]->src[0]);
  8566. }
  8567. i += ctx->num_additional_fused_ops;
  8568. ctx->num_additional_fused_ops = 0;
  8569. }
  8570. if (ctx->device->need_compiles) {
  8571. ggml_vk_load_shaders(ctx->device);
  8572. }
  8573. ggml_vk_preallocate_buffers(ctx);
  8574. ggml_pipeline_allocate_descriptor_sets(ctx);
  8575. int last_node = cgraph->n_nodes - 1;
  8576. // If the last op in the cgraph isn't backend GPU, the command buffer doesn't get closed properly
  8577. while (last_node > 0 && ggml_vk_is_empty(cgraph->nodes[last_node])) {
  8578. last_node -= 1;
  8579. }
  8580. // Reserve tensor context space for all nodes
  8581. ctx->tensor_ctxs.resize(cgraph->n_nodes);
  8582. bool first_node_in_batch = true; // true if next node will be first node in a batch
  8583. int submit_node_idx = 0; // index to first node in a batch
  8584. vk_context compute_ctx;
  8585. if (vk_perf_logger_enabled) {
  8586. // allocate/resize the query pool
  8587. if (ctx->device->num_queries < cgraph->n_nodes + 1) {
  8588. if (ctx->device->query_pool) {
  8589. ctx->device->device.destroyQueryPool(ctx->device->query_pool);
  8590. }
  8591. vk::QueryPoolCreateInfo query_create_info;
  8592. query_create_info.queryType = vk::QueryType::eTimestamp;
  8593. query_create_info.queryCount = cgraph->n_nodes + 100;
  8594. ctx->device->query_pool = ctx->device->device.createQueryPool(query_create_info);
  8595. ctx->device->num_queries = query_create_info.queryCount;
  8596. }
  8597. ctx->device->device.resetQueryPool(ctx->device->query_pool, 0, cgraph->n_nodes+1);
  8598. GGML_ASSERT(ctx->compute_ctx.expired());
  8599. compute_ctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  8600. ctx->compute_ctx = compute_ctx;
  8601. ggml_vk_ctx_begin(ctx->device, compute_ctx);
  8602. compute_ctx->s->buffer.writeTimestamp(vk::PipelineStageFlagBits::eAllCommands, ctx->device->query_pool, 0);
  8603. }
  8604. // Submit after enough work has accumulated, to overlap CPU cmdbuffer generation with GPU execution.
  8605. // Estimate the amount of matmul work by looking at the weight matrix size, and submit every 100MB
  8606. // (and scaled down based on model size, so smaller models submit earlier).
  8607. // Also submit at least every 100 nodes, in case there are workloads without as much matmul.
  8608. int nodes_per_submit = 100;
  8609. int submitted_nodes = 0;
  8610. int submit_count = 0;
  8611. uint64_t mul_mat_bytes = 0;
  8612. uint64_t mul_mat_bytes_per_submit = std::min(uint64_t(100*1000*1000), total_mat_mul_bytes / 40u);
  8613. for (int i = 0; i < cgraph->n_nodes; i++) {
  8614. if (first_node_in_batch) {
  8615. submit_node_idx = i;
  8616. }
  8617. if (cgraph->nodes[i]->op == GGML_OP_MUL_MAT || cgraph->nodes[i]->op == GGML_OP_MUL_MAT_ID) {
  8618. mul_mat_bytes += ggml_nbytes(cgraph->nodes[i]->src[0]);
  8619. }
  8620. if (!ctx->device->disable_fusion && ggml_vk_can_fuse(cgraph, i, { GGML_OP_RMS_NORM, GGML_OP_MUL })) {
  8621. ctx->num_additional_fused_ops = 1;
  8622. }
  8623. // Signal the almost_ready fence when the graph is mostly complete (< 20% remaining)
  8624. bool almost_ready = (cgraph->n_nodes - i) < cgraph->n_nodes / 5;
  8625. bool submit = (submitted_nodes >= nodes_per_submit) ||
  8626. (mul_mat_bytes >= mul_mat_bytes_per_submit) ||
  8627. (i + ctx->num_additional_fused_ops == last_node) ||
  8628. (almost_ready && !ctx->almost_ready_fence_pending);
  8629. 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);
  8630. if (vk_perf_logger_enabled) {
  8631. if (ctx->compute_ctx.expired()) {
  8632. compute_ctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  8633. ctx->compute_ctx = compute_ctx;
  8634. ggml_vk_ctx_begin(ctx->device, compute_ctx);
  8635. } else {
  8636. compute_ctx = ctx->compute_ctx.lock();
  8637. }
  8638. // If there are fused ops, just write out timestamps for all nodes to keep the accounting simple
  8639. for (int j = 0; j < ctx->num_additional_fused_ops + 1; ++j) {
  8640. compute_ctx->s->buffer.writeTimestamp(vk::PipelineStageFlagBits::eAllCommands, ctx->device->query_pool, i+j+1);
  8641. }
  8642. }
  8643. if (enqueued) {
  8644. ++submitted_nodes;
  8645. #ifndef GGML_VULKAN_CHECK_RESULTS
  8646. if (first_node_in_batch) {
  8647. first_node_in_batch = false;
  8648. }
  8649. #endif
  8650. }
  8651. if (submit && enqueued) {
  8652. first_node_in_batch = true;
  8653. submitted_nodes = 0;
  8654. mul_mat_bytes = 0;
  8655. if (submit_count < 3) {
  8656. mul_mat_bytes_per_submit *= 2;
  8657. }
  8658. submit_count++;
  8659. }
  8660. i += ctx->num_additional_fused_ops;
  8661. ctx->num_additional_fused_ops = 0;
  8662. }
  8663. if (vk_perf_logger_enabled) {
  8664. // End the command buffer and submit/wait
  8665. GGML_ASSERT(!ctx->compute_ctx.expired());
  8666. compute_ctx = ctx->compute_ctx.lock();
  8667. ggml_vk_ctx_end(compute_ctx);
  8668. ggml_vk_submit(compute_ctx, ctx->device->fence);
  8669. VK_CHECK(ctx->device->device.waitForFences({ ctx->device->fence }, true, UINT64_MAX), "GGML_VULKAN_PERF waitForFences");
  8670. ctx->device->device.resetFences({ ctx->device->fence });
  8671. // Get the results and pass them to the logger
  8672. std::vector<uint64_t> timestamps(cgraph->n_nodes + 1);
  8673. 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");
  8674. for (int i = 0; i < cgraph->n_nodes; i++) {
  8675. if (!ggml_vk_is_empty(cgraph->nodes[i])) {
  8676. ctx->device->perf_logger->log_timing(cgraph->nodes[i], uint64_t((timestamps[i+1] - timestamps[i]) * ctx->device->properties.limits.timestampPeriod));
  8677. }
  8678. }
  8679. ctx->device->perf_logger->print_timings();
  8680. }
  8681. ggml_vk_graph_cleanup(ctx);
  8682. return GGML_STATUS_SUCCESS;
  8683. UNUSED(backend);
  8684. }
  8685. // TODO: enable async and synchronize
  8686. static ggml_backend_i ggml_backend_vk_interface = {
  8687. /* .get_name = */ ggml_backend_vk_name,
  8688. /* .free = */ ggml_backend_vk_free,
  8689. /* .set_tensor_async = */ NULL, // ggml_backend_vk_set_tensor_async,
  8690. /* .get_tensor_async = */ NULL, // ggml_backend_vk_get_tensor_async,
  8691. /* .cpy_tensor_async = */ NULL, // ggml_backend_vk_cpy_tensor_async,
  8692. /* .synchronize = */ NULL, // ggml_backend_vk_synchronize,
  8693. /* .graph_plan_create = */ NULL,
  8694. /* .graph_plan_free = */ NULL,
  8695. /* .graph_plan_update = */ NULL,
  8696. /* .graph_plan_compute = */ NULL,
  8697. /* .graph_compute = */ ggml_backend_vk_graph_compute,
  8698. /* .event_record = */ NULL,
  8699. /* .event_wait = */ NULL,
  8700. };
  8701. static ggml_guid_t ggml_backend_vk_guid() {
  8702. static ggml_guid guid = { 0xb8, 0xf7, 0x4f, 0x86, 0x40, 0x3c, 0xe1, 0x02, 0x91, 0xc8, 0xdd, 0xe9, 0x02, 0x3f, 0xc0, 0x2b };
  8703. return &guid;
  8704. }
  8705. ggml_backend_t ggml_backend_vk_init(size_t dev_num) {
  8706. VK_LOG_DEBUG("ggml_backend_vk_init(" << dev_num << ")");
  8707. ggml_backend_vk_context * ctx = new ggml_backend_vk_context;
  8708. ggml_vk_init(ctx, dev_num);
  8709. ggml_backend_t vk_backend = new ggml_backend {
  8710. /* .guid = */ ggml_backend_vk_guid(),
  8711. /* .interface = */ ggml_backend_vk_interface,
  8712. /* .device = */ ggml_backend_reg_dev_get(ggml_backend_vk_reg(), dev_num),
  8713. /* .context = */ ctx,
  8714. };
  8715. return vk_backend;
  8716. }
  8717. bool ggml_backend_is_vk(ggml_backend_t backend) {
  8718. return backend != NULL && ggml_guid_matches(backend->guid, ggml_backend_vk_guid());
  8719. }
  8720. int ggml_backend_vk_get_device_count() {
  8721. return ggml_vk_get_device_count();
  8722. }
  8723. void ggml_backend_vk_get_device_description(int device, char * description, size_t description_size) {
  8724. GGML_ASSERT(device < (int) vk_instance.device_indices.size());
  8725. int dev_idx = vk_instance.device_indices[device];
  8726. ggml_vk_get_device_description(dev_idx, description, description_size);
  8727. }
  8728. void ggml_backend_vk_get_device_memory(int device, size_t * free, size_t * total) {
  8729. GGML_ASSERT(device < (int) vk_instance.device_indices.size());
  8730. vk::PhysicalDevice vkdev = vk_instance.instance.enumeratePhysicalDevices()[vk_instance.device_indices[device]];
  8731. vk::PhysicalDeviceMemoryProperties memprops = vkdev.getMemoryProperties();
  8732. for (const vk::MemoryHeap& heap : memprops.memoryHeaps) {
  8733. if (heap.flags & vk::MemoryHeapFlagBits::eDeviceLocal) {
  8734. *total = heap.size;
  8735. *free = heap.size;
  8736. break;
  8737. }
  8738. }
  8739. }
  8740. //////////////////////////
  8741. struct ggml_backend_vk_device_context {
  8742. size_t device;
  8743. std::string name;
  8744. std::string description;
  8745. };
  8746. static const char * ggml_backend_vk_device_get_name(ggml_backend_dev_t dev) {
  8747. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  8748. return ctx->name.c_str();
  8749. }
  8750. static const char * ggml_backend_vk_device_get_description(ggml_backend_dev_t dev) {
  8751. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  8752. return ctx->description.c_str();
  8753. }
  8754. static void ggml_backend_vk_device_get_memory(ggml_backend_dev_t device, size_t * free, size_t * total) {
  8755. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)device->context;
  8756. ggml_backend_vk_get_device_memory(ctx->device, free, total);
  8757. }
  8758. static ggml_backend_buffer_type_t ggml_backend_vk_device_get_buffer_type(ggml_backend_dev_t dev) {
  8759. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  8760. return ggml_backend_vk_buffer_type(ctx->device);
  8761. }
  8762. static ggml_backend_buffer_type_t ggml_backend_vk_device_get_host_buffer_type(ggml_backend_dev_t dev) {
  8763. UNUSED(dev);
  8764. return ggml_backend_vk_host_buffer_type();
  8765. }
  8766. static enum ggml_backend_dev_type ggml_backend_vk_device_get_type(ggml_backend_dev_t dev) {
  8767. UNUSED(dev);
  8768. return GGML_BACKEND_DEVICE_TYPE_GPU;
  8769. }
  8770. static void ggml_backend_vk_device_get_props(ggml_backend_dev_t dev, struct ggml_backend_dev_props * props) {
  8771. props->name = ggml_backend_vk_device_get_name(dev);
  8772. props->description = ggml_backend_vk_device_get_description(dev);
  8773. props->type = ggml_backend_vk_device_get_type(dev);
  8774. ggml_backend_vk_device_get_memory(dev, &props->memory_free, &props->memory_total);
  8775. props->caps = {
  8776. /* .async = */ false,
  8777. /* .host_buffer = */ true,
  8778. /* .buffer_from_host_ptr = */ false,
  8779. /* .events = */ false,
  8780. };
  8781. }
  8782. static ggml_backend_t ggml_backend_vk_device_init(ggml_backend_dev_t dev, const char * params) {
  8783. UNUSED(params);
  8784. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  8785. return ggml_backend_vk_init(ctx->device);
  8786. }
  8787. static bool ggml_backend_vk_device_supports_op(ggml_backend_dev_t dev, const ggml_tensor * op) {
  8788. switch (op->op) {
  8789. case GGML_OP_UNARY:
  8790. switch (ggml_get_unary_op(op)) {
  8791. case GGML_UNARY_OP_GELU:
  8792. case GGML_UNARY_OP_GELU_ERF:
  8793. case GGML_UNARY_OP_GELU_QUICK:
  8794. case GGML_UNARY_OP_SILU:
  8795. case GGML_UNARY_OP_RELU:
  8796. case GGML_UNARY_OP_TANH:
  8797. case GGML_UNARY_OP_SIGMOID:
  8798. return ggml_is_contiguous(op->src[0]) &&
  8799. (op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16) &&
  8800. (op->type == GGML_TYPE_F32 || op->type == GGML_TYPE_F16) &&
  8801. (op->src[0]->type == op->type);
  8802. default:
  8803. return false;
  8804. }
  8805. break;
  8806. case GGML_OP_GLU:
  8807. switch (ggml_get_glu_op(op)) {
  8808. case GGML_GLU_OP_GEGLU:
  8809. case GGML_GLU_OP_REGLU:
  8810. case GGML_GLU_OP_SWIGLU:
  8811. case GGML_GLU_OP_GEGLU_ERF:
  8812. case GGML_GLU_OP_GEGLU_QUICK:
  8813. return ggml_is_contiguous(op->src[0]) &&
  8814. (op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16) &&
  8815. (op->type == GGML_TYPE_F32 || op->type == GGML_TYPE_F16) &&
  8816. (op->src[0]->type == op->type);
  8817. default:
  8818. return false;
  8819. }
  8820. break;
  8821. case GGML_OP_MUL_MAT:
  8822. case GGML_OP_MUL_MAT_ID:
  8823. {
  8824. ggml_type src0_type = op->src[0]->type;
  8825. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  8826. const vk_device& device = ggml_vk_get_device(ctx->device);
  8827. if (op->op == GGML_OP_MUL_MAT_ID) {
  8828. if (!device->mul_mat_id_s[src0_type] && !device->mul_mat_id_m[src0_type] && !device->mul_mat_id_l[src0_type]) {
  8829. // If there's not enough shared memory for row_ids and the result tile, fallback to CPU
  8830. return false;
  8831. }
  8832. // Check against size of shared memory variable
  8833. if (op->src[2]->ne[0] > 4096) {
  8834. return false;
  8835. }
  8836. }
  8837. switch (src0_type) {
  8838. case GGML_TYPE_F32:
  8839. case GGML_TYPE_F16:
  8840. case GGML_TYPE_BF16:
  8841. case GGML_TYPE_Q4_0:
  8842. case GGML_TYPE_Q4_1:
  8843. case GGML_TYPE_Q5_0:
  8844. case GGML_TYPE_Q5_1:
  8845. case GGML_TYPE_Q8_0:
  8846. case GGML_TYPE_Q2_K:
  8847. case GGML_TYPE_Q3_K:
  8848. case GGML_TYPE_Q4_K:
  8849. case GGML_TYPE_Q5_K:
  8850. case GGML_TYPE_Q6_K:
  8851. case GGML_TYPE_IQ1_S:
  8852. case GGML_TYPE_IQ1_M:
  8853. case GGML_TYPE_IQ2_XXS:
  8854. case GGML_TYPE_IQ2_XS:
  8855. case GGML_TYPE_IQ2_S:
  8856. case GGML_TYPE_IQ3_XXS:
  8857. case GGML_TYPE_IQ3_S:
  8858. case GGML_TYPE_IQ4_XS:
  8859. case GGML_TYPE_IQ4_NL:
  8860. break;
  8861. default:
  8862. return false;
  8863. }
  8864. struct ggml_tensor * a;
  8865. struct ggml_tensor * b;
  8866. if (op->op == GGML_OP_MUL_MAT) {
  8867. a = op->src[0];
  8868. b = op->src[1];
  8869. } else {
  8870. a = op->src[2];
  8871. b = op->src[1];
  8872. }
  8873. if (a->ne[3] != b->ne[3]) {
  8874. return false;
  8875. }
  8876. 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) ||
  8877. !(ggml_vk_dim01_contiguous(op->src[1]) || op->src[1]->type == GGML_TYPE_F32 || op->src[1]->type == GGML_TYPE_F16)) {
  8878. return false;
  8879. }
  8880. if (op->src[0]->type == GGML_TYPE_BF16 && op->src[1]->type == GGML_TYPE_F16) {
  8881. // We currently don't have a bf16 x f16 shader, or an fp16->bf16 copy shader.
  8882. // So don't support this combination for now.
  8883. return false;
  8884. }
  8885. return true;
  8886. } break;
  8887. case GGML_OP_FLASH_ATTN_EXT:
  8888. {
  8889. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  8890. auto device = ggml_vk_get_device(ctx->device);
  8891. bool coopmat2 = device->coopmat2;
  8892. FaHeadSizes head_sizes = fa_get_head_sizes(op->src[1]->ne[0], op->src[2]->ne[0]);
  8893. if (head_sizes == FA_HEAD_SIZE_UNSUPPORTED) {
  8894. return false;
  8895. }
  8896. if (op->src[0]->type != GGML_TYPE_F32) {
  8897. return false;
  8898. }
  8899. if (op->type != GGML_TYPE_F32) {
  8900. return false;
  8901. }
  8902. if (op->src[3] && op->src[3]->type != GGML_TYPE_F16) {
  8903. return false;
  8904. }
  8905. // It's straightforward to support different K/V dequant, but would
  8906. // significantly increase the number of pipelines
  8907. if (op->src[1]->type != op->src[2]->type) {
  8908. return false;
  8909. }
  8910. switch (op->src[1]->type) {
  8911. case GGML_TYPE_F16:
  8912. case GGML_TYPE_Q4_0:
  8913. case GGML_TYPE_Q8_0:
  8914. // supported in scalar and coopmat2 paths
  8915. break;
  8916. case GGML_TYPE_Q4_1:
  8917. case GGML_TYPE_Q5_0:
  8918. case GGML_TYPE_Q5_1:
  8919. // K dequants currently disabled because D dimension is rounded up to 256 and runs inefficiently
  8920. //case GGML_TYPE_Q2_K:
  8921. //case GGML_TYPE_Q3_K:
  8922. //case GGML_TYPE_Q4_K:
  8923. //case GGML_TYPE_Q5_K:
  8924. //case GGML_TYPE_Q6_K:
  8925. //case GGML_TYPE_IQ1_S:
  8926. //case GGML_TYPE_IQ1_M:
  8927. //case GGML_TYPE_IQ2_XXS:
  8928. //case GGML_TYPE_IQ2_XS:
  8929. //case GGML_TYPE_IQ2_S:
  8930. //case GGML_TYPE_IQ3_XXS:
  8931. //case GGML_TYPE_IQ3_S:
  8932. //case GGML_TYPE_IQ4_XS:
  8933. case GGML_TYPE_IQ4_NL:
  8934. // currently supported only in coopmat2 path
  8935. if (!coopmat2) {
  8936. return false;
  8937. }
  8938. break;
  8939. default:
  8940. return false;
  8941. }
  8942. if (!coopmat2 && !device->subgroup_shuffle) {
  8943. // scalar FA uses subgroupShuffle
  8944. return false;
  8945. }
  8946. return true;
  8947. }
  8948. case GGML_OP_GET_ROWS:
  8949. {
  8950. switch (op->src[0]->type) {
  8951. case GGML_TYPE_F32:
  8952. case GGML_TYPE_F16:
  8953. case GGML_TYPE_BF16:
  8954. case GGML_TYPE_Q4_0:
  8955. case GGML_TYPE_Q4_1:
  8956. case GGML_TYPE_Q5_0:
  8957. case GGML_TYPE_Q5_1:
  8958. case GGML_TYPE_Q8_0:
  8959. case GGML_TYPE_IQ1_S:
  8960. case GGML_TYPE_IQ1_M:
  8961. case GGML_TYPE_IQ2_XXS:
  8962. case GGML_TYPE_IQ2_XS:
  8963. case GGML_TYPE_IQ2_S:
  8964. case GGML_TYPE_IQ3_XXS:
  8965. case GGML_TYPE_IQ3_S:
  8966. case GGML_TYPE_IQ4_XS:
  8967. case GGML_TYPE_IQ4_NL:
  8968. return true;
  8969. default:
  8970. return false;
  8971. }
  8972. } break;
  8973. case GGML_OP_SET_ROWS:
  8974. {
  8975. switch (op->type) {
  8976. case GGML_TYPE_F32:
  8977. case GGML_TYPE_F16:
  8978. case GGML_TYPE_BF16:
  8979. case GGML_TYPE_Q4_0:
  8980. case GGML_TYPE_Q4_1:
  8981. case GGML_TYPE_Q5_0:
  8982. case GGML_TYPE_Q5_1:
  8983. case GGML_TYPE_Q8_0:
  8984. case GGML_TYPE_IQ4_NL:
  8985. return true;
  8986. default:
  8987. return false;
  8988. }
  8989. } break;
  8990. case GGML_OP_CONT:
  8991. case GGML_OP_CPY:
  8992. case GGML_OP_DUP:
  8993. {
  8994. ggml_type src0_type = op->src[0]->type;
  8995. ggml_type src1_type = op->src[1] != nullptr ? op->src[1]->type : src0_type;
  8996. if (src0_type == GGML_TYPE_F32) {
  8997. switch (src1_type) {
  8998. case GGML_TYPE_F32:
  8999. case GGML_TYPE_F16:
  9000. case GGML_TYPE_BF16:
  9001. case GGML_TYPE_Q4_0:
  9002. case GGML_TYPE_Q4_1:
  9003. case GGML_TYPE_Q5_0:
  9004. case GGML_TYPE_Q5_1:
  9005. case GGML_TYPE_Q8_0:
  9006. case GGML_TYPE_IQ4_NL:
  9007. return true;
  9008. default:
  9009. break;
  9010. }
  9011. }
  9012. if (src1_type == GGML_TYPE_F32) {
  9013. switch (src0_type) {
  9014. case GGML_TYPE_F16:
  9015. case GGML_TYPE_Q4_0:
  9016. case GGML_TYPE_Q4_1:
  9017. case GGML_TYPE_Q5_0:
  9018. case GGML_TYPE_Q5_1:
  9019. case GGML_TYPE_Q8_0:
  9020. case GGML_TYPE_IQ4_NL:
  9021. return true;
  9022. default:
  9023. break;
  9024. }
  9025. }
  9026. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  9027. return true;
  9028. }
  9029. // We can handle copying from a type to the same type if it's
  9030. // contiguous (memcpy). We use f16 or f32 shaders to do the copy,
  9031. // so the type/block size must be a multiple of 4.
  9032. if (src0_type == src1_type &&
  9033. ggml_is_contiguous(op->src[0]) && ggml_is_contiguous(op) &&
  9034. (ggml_type_size(src0_type) % 2) == 0) {
  9035. return true;
  9036. }
  9037. return false;
  9038. } break;
  9039. case GGML_OP_REPEAT:
  9040. return ggml_type_size(op->type) == sizeof(float) && ggml_type_size(op->src[0]->type) == sizeof(float);
  9041. case GGML_OP_REPEAT_BACK:
  9042. return op->type == GGML_TYPE_F32 && op->src[0]->type == GGML_TYPE_F32;
  9043. case GGML_OP_ROPE:
  9044. case GGML_OP_ROPE_BACK:
  9045. case GGML_OP_NONE:
  9046. case GGML_OP_RESHAPE:
  9047. case GGML_OP_VIEW:
  9048. case GGML_OP_PERMUTE:
  9049. case GGML_OP_TRANSPOSE:
  9050. case GGML_OP_RMS_NORM:
  9051. return true;
  9052. case GGML_OP_NORM:
  9053. case GGML_OP_GROUP_NORM:
  9054. case GGML_OP_L2_NORM:
  9055. return ggml_is_contiguous(op->src[0]);
  9056. case GGML_OP_ADD:
  9057. case GGML_OP_SUB:
  9058. case GGML_OP_MUL:
  9059. case GGML_OP_DIV:
  9060. return (op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16) &&
  9061. (op->src[1]->type == GGML_TYPE_F32 || op->src[1]->type == GGML_TYPE_F16) &&
  9062. (op->type == GGML_TYPE_F32 || op->type == GGML_TYPE_F16);
  9063. case GGML_OP_SILU_BACK:
  9064. case GGML_OP_RMS_NORM_BACK:
  9065. case GGML_OP_SQR:
  9066. case GGML_OP_SIN:
  9067. case GGML_OP_COS:
  9068. case GGML_OP_CLAMP:
  9069. return op->src[0]->type == GGML_TYPE_F32;
  9070. case GGML_OP_UPSCALE:
  9071. case GGML_OP_ACC:
  9072. case GGML_OP_CONCAT:
  9073. case GGML_OP_SCALE:
  9074. case GGML_OP_PAD:
  9075. case GGML_OP_ROLL:
  9076. case GGML_OP_DIAG_MASK_INF:
  9077. case GGML_OP_SOFT_MAX:
  9078. case GGML_OP_SOFT_MAX_BACK:
  9079. case GGML_OP_ARGSORT:
  9080. case GGML_OP_SUM:
  9081. case GGML_OP_SUM_ROWS:
  9082. case GGML_OP_ARGMAX:
  9083. case GGML_OP_COUNT_EQUAL:
  9084. case GGML_OP_IM2COL:
  9085. case GGML_OP_TIMESTEP_EMBEDDING:
  9086. case GGML_OP_CONV_2D_DW:
  9087. case GGML_OP_POOL_2D:
  9088. case GGML_OP_RWKV_WKV6:
  9089. case GGML_OP_RWKV_WKV7:
  9090. case GGML_OP_LEAKY_RELU:
  9091. case GGML_OP_OPT_STEP_ADAMW:
  9092. return true;
  9093. case GGML_OP_CONV_TRANSPOSE_1D:
  9094. return op->src[0]->type == GGML_TYPE_F32 && op->src[1]->type == GGML_TYPE_F32;
  9095. default:
  9096. return false;
  9097. }
  9098. UNUSED(dev);
  9099. }
  9100. static bool ggml_backend_vk_device_supports_buft(ggml_backend_dev_t dev, ggml_backend_buffer_type_t buft) {
  9101. if (buft->iface.get_name != ggml_backend_vk_buffer_type_name) {
  9102. return false;
  9103. }
  9104. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  9105. ggml_backend_vk_buffer_type_context * buft_ctx = (ggml_backend_vk_buffer_type_context *)buft->context;
  9106. return buft_ctx->device->idx == ctx->device;
  9107. }
  9108. static bool ggml_backend_vk_device_offload_op(ggml_backend_dev_t dev, const ggml_tensor * op) {
  9109. const int min_batch_size = 32;
  9110. return (op->ne[1] >= min_batch_size && op->op != GGML_OP_GET_ROWS) ||
  9111. (op->ne[2] >= min_batch_size && op->op == GGML_OP_MUL_MAT_ID);
  9112. UNUSED(dev);
  9113. }
  9114. static const struct ggml_backend_device_i ggml_backend_vk_device_i = {
  9115. /* .get_name = */ ggml_backend_vk_device_get_name,
  9116. /* .get_description = */ ggml_backend_vk_device_get_description,
  9117. /* .get_memory = */ ggml_backend_vk_device_get_memory,
  9118. /* .get_type = */ ggml_backend_vk_device_get_type,
  9119. /* .get_props = */ ggml_backend_vk_device_get_props,
  9120. /* .init_backend = */ ggml_backend_vk_device_init,
  9121. /* .get_buffer_type = */ ggml_backend_vk_device_get_buffer_type,
  9122. /* .get_host_buffer_type = */ ggml_backend_vk_device_get_host_buffer_type,
  9123. /* .buffer_from_host_ptr = */ NULL,
  9124. /* .supports_op = */ ggml_backend_vk_device_supports_op,
  9125. /* .supports_buft = */ ggml_backend_vk_device_supports_buft,
  9126. /* .offload_op = */ ggml_backend_vk_device_offload_op,
  9127. /* .event_new = */ NULL,
  9128. /* .event_free = */ NULL,
  9129. /* .event_synchronize = */ NULL,
  9130. };
  9131. static const char * ggml_backend_vk_reg_get_name(ggml_backend_reg_t reg) {
  9132. UNUSED(reg);
  9133. return GGML_VK_NAME;
  9134. }
  9135. static size_t ggml_backend_vk_reg_get_device_count(ggml_backend_reg_t reg) {
  9136. UNUSED(reg);
  9137. return ggml_backend_vk_get_device_count();
  9138. }
  9139. static ggml_backend_dev_t ggml_backend_vk_reg_get_device(ggml_backend_reg_t reg, size_t device) {
  9140. static std::vector<ggml_backend_dev_t> devices;
  9141. static bool initialized = false;
  9142. {
  9143. static std::mutex mutex;
  9144. std::lock_guard<std::mutex> lock(mutex);
  9145. if (!initialized) {
  9146. for (int i = 0; i < ggml_backend_vk_get_device_count(); i++) {
  9147. ggml_backend_vk_device_context * ctx = new ggml_backend_vk_device_context;
  9148. char desc[256];
  9149. ggml_backend_vk_get_device_description(i, desc, sizeof(desc));
  9150. ctx->device = i;
  9151. ctx->name = GGML_VK_NAME + std::to_string(i);
  9152. ctx->description = desc;
  9153. devices.push_back(new ggml_backend_device {
  9154. /* .iface = */ ggml_backend_vk_device_i,
  9155. /* .reg = */ reg,
  9156. /* .context = */ ctx,
  9157. });
  9158. }
  9159. initialized = true;
  9160. }
  9161. }
  9162. GGML_ASSERT(device < devices.size());
  9163. return devices[device];
  9164. }
  9165. static const struct ggml_backend_reg_i ggml_backend_vk_reg_i = {
  9166. /* .get_name = */ ggml_backend_vk_reg_get_name,
  9167. /* .get_device_count = */ ggml_backend_vk_reg_get_device_count,
  9168. /* .get_device = */ ggml_backend_vk_reg_get_device,
  9169. /* .get_proc_address = */ NULL,
  9170. };
  9171. ggml_backend_reg_t ggml_backend_vk_reg() {
  9172. static ggml_backend_reg reg = {
  9173. /* .api_version = */ GGML_BACKEND_API_VERSION,
  9174. /* .iface = */ ggml_backend_vk_reg_i,
  9175. /* .context = */ nullptr,
  9176. };
  9177. try {
  9178. ggml_vk_instance_init();
  9179. return &reg;
  9180. } catch (const vk::SystemError& e) {
  9181. VK_LOG_DEBUG("ggml_backend_vk_reg() -> Error: System error: " << e.what());
  9182. return nullptr;
  9183. }
  9184. }
  9185. // Extension availability
  9186. static bool ggml_vk_instance_validation_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions) {
  9187. #ifdef GGML_VULKAN_VALIDATE
  9188. bool portability_enumeration_ext = false;
  9189. // Check for portability enumeration extension for MoltenVK support
  9190. for (const auto& properties : instance_extensions) {
  9191. if (strcmp("VK_KHR_portability_enumeration", properties.extensionName) == 0) {
  9192. return true;
  9193. }
  9194. }
  9195. if (!portability_enumeration_ext) {
  9196. std::cerr << "ggml_vulkan: WARNING: Instance extension VK_KHR_portability_enumeration not found." << std::endl;
  9197. }
  9198. #endif
  9199. return false;
  9200. UNUSED(instance_extensions);
  9201. }
  9202. static bool ggml_vk_instance_portability_enumeration_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions) {
  9203. #ifdef __APPLE__
  9204. bool portability_enumeration_ext = false;
  9205. // Check for portability enumeration extension for MoltenVK support
  9206. for (const auto& properties : instance_extensions) {
  9207. if (strcmp("VK_KHR_portability_enumeration", properties.extensionName) == 0) {
  9208. return true;
  9209. }
  9210. }
  9211. if (!portability_enumeration_ext) {
  9212. std::cerr << "ggml_vulkan: WARNING: Instance extension VK_KHR_portability_enumeration not found." << std::endl;
  9213. }
  9214. #endif
  9215. return false;
  9216. UNUSED(instance_extensions);
  9217. }
  9218. // Extension availability
  9219. static bool ggml_vk_instance_debug_utils_ext_available(
  9220. const std::vector<vk::ExtensionProperties> & instance_extensions) {
  9221. // Check for portability enumeration extension for MoltenVK support
  9222. for (const auto & properties : instance_extensions) {
  9223. if (strcmp("VK_EXT_debug_utils", properties.extensionName) == 0) {
  9224. return true;
  9225. }
  9226. }
  9227. std::cerr << "ggml_vulkan: WARNING: Instance extension VK_EXT_debug_utils not found." << std::endl;
  9228. return false;
  9229. UNUSED(instance_extensions);
  9230. }
  9231. static bool ggml_vk_khr_cooperative_matrix_support(const vk::PhysicalDeviceProperties& props, const vk::PhysicalDeviceDriverProperties& driver_props, vk_device_architecture arch) {
  9232. switch (props.vendorID) {
  9233. case VK_VENDOR_ID_INTEL:
  9234. // Only allowing Xe2 GPU at the moment since Xe2 GPU can gain significant performance boost,
  9235. // while some older hardware (ex. Arc A770) has performance regressions
  9236. return arch == vk_device_architecture::INTEL_XE2;
  9237. case VK_VENDOR_ID_AMD:
  9238. if (driver_props.driverID == vk::DriverId::eAmdProprietary || driver_props.driverID == vk::DriverId::eAmdOpenSource) {
  9239. // Workaround for AMD proprietary driver reporting support on all GPUs
  9240. return arch == vk_device_architecture::AMD_RDNA3;
  9241. }
  9242. return true;
  9243. default:
  9244. return true;
  9245. }
  9246. }
  9247. // checks
  9248. #ifdef GGML_VULKAN_CHECK_RESULTS
  9249. static void ggml_vk_print_graph_origin(const ggml_tensor * tensor, std::vector<const ggml_tensor *>& done, int level = 0) {
  9250. if (std::find(done.begin(), done.end(), tensor) != done.end() || level > 10) {
  9251. return;
  9252. }
  9253. for (int j = 0; j < level; j++) {
  9254. std::cerr << " ";
  9255. }
  9256. std::cerr << ggml_op_name(tensor->op) << " gpu=" << (tensor->extra != nullptr) << std::endl;
  9257. done.push_back(tensor);
  9258. for (int i = 0; i < GGML_MAX_SRC; i++) {
  9259. if (tensor->src[i] != nullptr) {
  9260. ggml_vk_print_graph_origin(tensor->src[i], done, level + 1);
  9261. }
  9262. }
  9263. }
  9264. static void ggml_vk_print_tensor_area(const ggml_tensor * tensor, const void * data, int i0, int i1, int i2, int i3) {
  9265. if (tensor->type != GGML_TYPE_F32 && tensor->type != GGML_TYPE_F16 && tensor->type != GGML_TYPE_I32) {
  9266. return;
  9267. }
  9268. i0 = std::max(i0, 5);
  9269. i1 = std::max(i1, 5);
  9270. i2 = std::max(i2, 0);
  9271. i3 = std::max(i3, 0);
  9272. fprintf(stderr, " ");
  9273. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  9274. fprintf(stderr, "%7d ", idx1);
  9275. }
  9276. fprintf(stderr, "\n");
  9277. for (int idx0 = i0 - 5; idx0 < i0 + 5; idx0++) {
  9278. fprintf(stderr, "%7d: ", idx0);
  9279. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  9280. 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]) {
  9281. float val;
  9282. if (tensor->type == GGML_TYPE_F32) {
  9283. val = *(const float *) ((const char *) data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]);
  9284. } else if (tensor->type == GGML_TYPE_F16) {
  9285. 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]));
  9286. } else if (tensor->type == GGML_TYPE_I32) {
  9287. val = *(const int32_t *) ((const char *) data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]);
  9288. } else {
  9289. GGML_ABORT("fatal error");
  9290. }
  9291. fprintf(stderr, "% 7.2f ", val);
  9292. } else {
  9293. fprintf(stderr, " ");
  9294. }
  9295. }
  9296. fprintf(stderr, "\n");
  9297. }
  9298. }
  9299. static void ggml_vk_print_tensor(const ggml_tensor * tensor, const char * name) {
  9300. void * tensor_data = tensor->data;
  9301. const bool is_gpu = tensor->buffer != nullptr && ggml_backend_buffer_is_vk(tensor->buffer);
  9302. if (is_gpu) {
  9303. const size_t tensor_size = ggml_nbytes(tensor);
  9304. tensor_data = malloc(tensor_size);
  9305. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  9306. vk_buffer buffer_gpu = buf_ctx->dev_buffer;
  9307. ggml_vk_buffer_read(buffer_gpu, vk_tensor_offset(tensor) + tensor->view_offs, tensor_data, tensor_size);
  9308. }
  9309. std::cerr << "TENSOR CHECK " << name << " (" << tensor->name << "): " << ggml_op_name(tensor->op) << std::endl;
  9310. 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;
  9311. if (tensor->src[0] != nullptr) {
  9312. 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;
  9313. }
  9314. if (tensor->src[1] != nullptr) {
  9315. 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;
  9316. }
  9317. std::cerr << std::endl << "Result:" << std::endl;
  9318. ggml_vk_print_tensor_area(tensor, tensor_data, 5, 5, 0, 0);
  9319. std::cerr << std::endl;
  9320. std::vector<const ggml_tensor *> done;
  9321. ggml_vk_print_graph_origin(tensor, done);
  9322. if (is_gpu) {
  9323. free(tensor_data);
  9324. }
  9325. }
  9326. void * comp_result;
  9327. size_t comp_size;
  9328. size_t comp_nb[GGML_MAX_DIMS];
  9329. size_t check_counter = 0;
  9330. static void ggml_vk_check_results_0(ggml_backend_vk_context * ctx, ggml_cgraph * cgraph, int tensor_idx) {
  9331. ggml_tensor * tensor = cgraph->nodes[tensor_idx];
  9332. if (tensor->op == GGML_OP_TRANSPOSE) {
  9333. return;
  9334. }
  9335. bool fused_rms_norm_mul = false;
  9336. int rms_norm_idx = -1;
  9337. if (ctx->num_additional_fused_ops == 1 &&
  9338. tensor->op == GGML_OP_RMS_NORM &&
  9339. cgraph->nodes[tensor_idx + 1]->op == GGML_OP_MUL) {
  9340. fused_rms_norm_mul = true;
  9341. tensor = cgraph->nodes[tensor_idx + 1];
  9342. }
  9343. check_counter++;
  9344. if (!(vk_output_tensor > 0 && vk_output_tensor == check_counter) && check_counter <= vk_skip_checks) {
  9345. return;
  9346. }
  9347. VK_LOG_DEBUG("ggml_vk_check_results_0(" << tensor->name << ")");
  9348. ggml_tensor * src0 = tensor->src[0];
  9349. ggml_tensor * src1 = tensor->src[1];
  9350. struct ggml_init_params iparams = {
  9351. /*.mem_size =*/ 2ul*1024ul*1024ul*1024ul,
  9352. /*.mem_buffer =*/ NULL,
  9353. /*.no_alloc =*/ false,
  9354. };
  9355. struct ggml_context * ggml_ctx = ggml_init(iparams);
  9356. std::array<struct ggml_tensor *, 6> src_clone = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
  9357. std::array<size_t, 6> src_size = {0, 0, 0, 0, 0, 0};
  9358. std::array<void *, 6> src_buffer = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
  9359. const char * srci_name[6] = {"src0", "src1", "src2", "src3", "src4", "src5"};
  9360. struct ggml_tensor * tensor_clone = nullptr;
  9361. for (int i = 0; i < 6; i++) {
  9362. ggml_tensor * srci = tensor->src[i];
  9363. if (fused_rms_norm_mul) {
  9364. rms_norm_idx = tensor->src[0]->op == GGML_OP_RMS_NORM ? 0 : 1;
  9365. ggml_tensor *rms_norm = tensor->src[rms_norm_idx];
  9366. switch (i) {
  9367. case 0: srci = rms_norm->src[0]; break;
  9368. case 1: srci = tensor->src[1 - rms_norm_idx]; break;
  9369. default: continue;
  9370. }
  9371. }
  9372. if (srci == nullptr) {
  9373. continue;
  9374. }
  9375. ggml_tensor * srci_clone = ggml_dup_tensor(ggml_ctx, srci);
  9376. size_t srci_size = ggml_nbytes(srci);
  9377. src_clone[i] = srci_clone;
  9378. src_size[i] = ggml_nbytes(srci);
  9379. src_buffer[i] = malloc(srci_size);
  9380. srci_clone->data = src_buffer[i];
  9381. if (ggml_backend_buffer_is_host(srci->buffer)) {
  9382. memcpy(srci_clone->data, srci->data, srci_size);
  9383. memcpy(srci_clone->nb, srci->nb, sizeof(size_t) * GGML_MAX_DIMS);
  9384. } else if (ggml_backend_buffer_is_vk(srci->buffer)) {
  9385. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)srci->buffer->context;
  9386. vk_buffer& buffer_gpu = buf_ctx->dev_buffer;
  9387. uint64_t offset = vk_tensor_offset(srci) + srci->view_offs;
  9388. if (!ggml_is_contiguous(srci) && ggml_vk_dim01_contiguous(srci)) {
  9389. for (int i3 = 0; i3 < srci->ne[3]; i3++) {
  9390. for (int i2 = 0; i2 < srci->ne[2]; i2++) {
  9391. const int idx = i3*srci->ne[2] + i2;
  9392. 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]);
  9393. }
  9394. }
  9395. srci_clone->nb[0] = srci->nb[0];
  9396. srci_clone->nb[1] = srci->nb[1];
  9397. for (int i = 2; i < GGML_MAX_DIMS; i++) {
  9398. srci_clone->nb[i] = srci_clone->nb[i - 1]*srci_clone->ne[i - 1];
  9399. }
  9400. } else {
  9401. if (offset + srci_size >= buffer_gpu->size) {
  9402. srci_size = buffer_gpu->size - offset;
  9403. }
  9404. ggml_vk_buffer_read(buffer_gpu, offset, srci_clone->data, srci_size);
  9405. memcpy(srci_clone->nb, srci->nb, sizeof(size_t) * GGML_MAX_DIMS);
  9406. }
  9407. } else {
  9408. GGML_ABORT("fatal error");
  9409. }
  9410. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  9411. ggml_vk_print_tensor(srci, srci_name[i]);
  9412. }
  9413. }
  9414. if (tensor->op == GGML_OP_FLASH_ATTN_EXT) {
  9415. const float * params = (const float *)tensor->op_params;
  9416. 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]);
  9417. } else if (tensor->op == GGML_OP_MUL_MAT) {
  9418. tensor_clone = ggml_mul_mat(ggml_ctx, src_clone[0], src_clone[1]);
  9419. } else if (tensor->op == GGML_OP_MUL_MAT_ID) {
  9420. tensor_clone = ggml_mul_mat_id(ggml_ctx, src_clone[0], src_clone[1], src_clone[2]);
  9421. } else if (tensor->op == GGML_OP_SUB) {
  9422. tensor_clone = ggml_sub(ggml_ctx, src_clone[0], src_clone[1]);
  9423. } else if (tensor->op == GGML_OP_MUL) {
  9424. if (fused_rms_norm_mul) {
  9425. tensor_clone = ggml_rms_norm(ggml_ctx, src_clone[0], *(float *)tensor->src[rms_norm_idx]->op_params);
  9426. tensor_clone = ggml_mul(ggml_ctx, tensor_clone, src_clone[1 - rms_norm_idx]);
  9427. } else {
  9428. tensor_clone = ggml_mul(ggml_ctx, src_clone[0], src_clone[1]);
  9429. }
  9430. } else if (tensor->op == GGML_OP_DIV) {
  9431. tensor_clone = ggml_div(ggml_ctx, src_clone[0], src_clone[1]);
  9432. } else if (tensor->op == GGML_OP_CONCAT) {
  9433. tensor_clone = ggml_concat(ggml_ctx, src_clone[0], src_clone[1], *(int *)tensor->op_params);
  9434. } else if (tensor->op == GGML_OP_UPSCALE) {
  9435. tensor_clone = ggml_upscale_ext(ggml_ctx, src_clone[0], tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3], (ggml_scale_mode) tensor->op_params[0]);
  9436. } else if (tensor->op == GGML_OP_SCALE) {
  9437. const float * params = (const float *)tensor->op_params;
  9438. tensor_clone = ggml_scale(ggml_ctx, src_clone[0], params[0]);
  9439. } else if (tensor->op == GGML_OP_SQR) {
  9440. tensor_clone = ggml_sqr(ggml_ctx, src_clone[0]);
  9441. } else if (tensor->op == GGML_OP_SIN) {
  9442. tensor_clone = ggml_sin(ggml_ctx, src_clone[0]);
  9443. } else if (tensor->op == GGML_OP_COS) {
  9444. tensor_clone = ggml_cos(ggml_ctx, src_clone[0]);
  9445. } else if (tensor->op == GGML_OP_CLAMP) {
  9446. const float * params = (const float *)tensor->op_params;
  9447. tensor_clone = ggml_clamp(ggml_ctx, src_clone[0], params[0], params[1]);
  9448. } else if (tensor->op == GGML_OP_PAD) {
  9449. 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]);
  9450. } else if (tensor->op == GGML_OP_REPEAT) {
  9451. tensor_clone = ggml_repeat(ggml_ctx, src_clone[0], tensor);
  9452. } else if (tensor->op == GGML_OP_REPEAT_BACK) {
  9453. tensor_clone = ggml_repeat_back(ggml_ctx, src_clone[0], tensor);
  9454. } else if (tensor->op == GGML_OP_ADD) {
  9455. tensor_clone = ggml_add(ggml_ctx, src_clone[0], src_clone[1]);
  9456. } else if (tensor->op == GGML_OP_ACC) {
  9457. 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]);
  9458. } else if (tensor->op == GGML_OP_NORM) {
  9459. tensor_clone = ggml_norm(ggml_ctx, src_clone[0], *(float *)tensor->op_params);
  9460. } else if (tensor->op == GGML_OP_GROUP_NORM) {
  9461. const float * float_params = (const float *)tensor->op_params;
  9462. tensor_clone = ggml_group_norm(ggml_ctx, src_clone[0], tensor->op_params[0], float_params[1]);
  9463. } else if (tensor->op == GGML_OP_RMS_NORM) {
  9464. tensor_clone = ggml_rms_norm(ggml_ctx, src_clone[0], *(float *)tensor->op_params);
  9465. } else if (tensor->op == GGML_OP_RMS_NORM_BACK) {
  9466. const float eps = ((float *) tensor->op_params)[0];
  9467. tensor_clone = ggml_rms_norm_back(ggml_ctx, src_clone[0], src_clone[1], eps);
  9468. } else if (tensor->op == GGML_OP_SILU_BACK) {
  9469. tensor_clone = ggml_silu_back(ggml_ctx, src_clone[0], src_clone[1]);
  9470. } else if (tensor->op == GGML_OP_L2_NORM) {
  9471. const float eps = ((float *) tensor->op_params)[0];
  9472. tensor_clone = ggml_l2_norm(ggml_ctx, src_clone[0], eps);
  9473. } else if (tensor->op == GGML_OP_SOFT_MAX) {
  9474. if (src1 != nullptr) {
  9475. const float * params = (const float *)tensor->op_params;
  9476. tensor_clone = ggml_soft_max_ext(ggml_ctx, src_clone[0], src_clone[1], params[0], params[1]);
  9477. } else {
  9478. tensor_clone = ggml_soft_max(ggml_ctx, src_clone[0]);
  9479. }
  9480. } else if (tensor->op == GGML_OP_SOFT_MAX_BACK) {
  9481. 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]);
  9482. } else if (tensor->op == GGML_OP_DIAG_MASK_INF) {
  9483. tensor_clone = ggml_diag_mask_inf(ggml_ctx, src_clone[0], tensor->op_params[0]);
  9484. } else if (tensor->op == GGML_OP_ROPE || tensor->op == GGML_OP_ROPE_BACK) {
  9485. const int n_dims = ((int32_t *) tensor->op_params)[1];
  9486. const int mode = ((int32_t *) tensor->op_params)[2];
  9487. //const int n_ctx_ggml = ((int32_t *) tensor->op_params)[3];
  9488. const int n_ctx_orig_ggml = ((int32_t *) tensor->op_params)[4];
  9489. const float freq_base = ((float *) tensor->op_params)[5];
  9490. const float freq_scale = ((float *) tensor->op_params)[6];
  9491. const float ext_factor = ((float *) tensor->op_params)[7];
  9492. const float attn_factor = ((float *) tensor->op_params)[8];
  9493. const float beta_fast = ((float *) tensor->op_params)[9];
  9494. const float beta_slow = ((float *) tensor->op_params)[10];
  9495. if (mode & GGML_ROPE_TYPE_MROPE) {
  9496. int32_t *sections = ((int32_t *) tensor->op_params) + 11;
  9497. if (tensor->op == GGML_OP_ROPE) {
  9498. 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);
  9499. } else {
  9500. 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);
  9501. }
  9502. } else {
  9503. if (tensor->op == GGML_OP_ROPE) {
  9504. 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);
  9505. } else {
  9506. 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);
  9507. }
  9508. }
  9509. } else if (tensor->op == GGML_OP_UNARY) {
  9510. switch (ggml_get_unary_op(tensor)) {
  9511. case GGML_UNARY_OP_SILU:
  9512. tensor_clone = ggml_silu(ggml_ctx, src_clone[0]);
  9513. break;
  9514. case GGML_UNARY_OP_GELU:
  9515. tensor_clone = ggml_gelu(ggml_ctx, src_clone[0]);
  9516. break;
  9517. case GGML_UNARY_OP_GELU_ERF:
  9518. tensor_clone = ggml_gelu_erf(ggml_ctx, src_clone[0]);
  9519. break;
  9520. case GGML_UNARY_OP_GELU_QUICK:
  9521. tensor_clone = ggml_gelu_quick(ggml_ctx, src_clone[0]);
  9522. break;
  9523. case GGML_UNARY_OP_RELU:
  9524. tensor_clone = ggml_relu(ggml_ctx, src_clone[0]);
  9525. break;
  9526. case GGML_UNARY_OP_TANH:
  9527. tensor_clone = ggml_tanh(ggml_ctx, src_clone[0]);
  9528. break;
  9529. case GGML_UNARY_OP_SIGMOID:
  9530. tensor_clone = ggml_sigmoid(ggml_ctx, src_clone[0]);
  9531. break;
  9532. default:
  9533. std::cerr << "Missing vk_check_results OP: " << ggml_op_name(tensor->op) << std::endl;
  9534. GGML_ABORT("fatal error");
  9535. }
  9536. } else if (tensor->op == GGML_OP_GLU) {
  9537. if (src_clone[1] == nullptr) {
  9538. tensor_clone = ggml_glu(ggml_ctx, src_clone[0], (ggml_glu_op) tensor->op_params[0], tensor->op_params[1]);
  9539. } else {
  9540. tensor_clone = ggml_glu_split(ggml_ctx, src_clone[0], src_clone[1], (ggml_glu_op) tensor->op_params[0]);
  9541. }
  9542. } else if (tensor->op == GGML_OP_CPY || tensor->op == GGML_OP_DUP) {
  9543. if (src1 == nullptr) {
  9544. tensor_clone = ggml_dup(ggml_ctx, src_clone[0]);
  9545. tensor_clone->type = tensor->type;
  9546. } else {
  9547. tensor_clone = ggml_cpy(ggml_ctx, src_clone[0], src_clone[1]);
  9548. }
  9549. } else if (tensor->op == GGML_OP_SET_ROWS) {
  9550. tensor_clone = ggml_set_rows(ggml_ctx, src_clone[0], src_clone[1]);
  9551. } else if (tensor->op == GGML_OP_CONT) {
  9552. tensor_clone = ggml_cont_4d(ggml_ctx, src_clone[0], tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3]);
  9553. } else if (tensor->op == GGML_OP_RESHAPE) {
  9554. tensor_clone = ggml_reshape_4d(ggml_ctx, src_clone[0], tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3]);
  9555. } else if (tensor->op == GGML_OP_VIEW) {
  9556. 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]);
  9557. } else if (tensor->op == GGML_OP_PERMUTE) {
  9558. int32_t * params = (int32_t *)tensor->op_params;
  9559. tensor_clone = ggml_permute(ggml_ctx, src_clone[0], params[0], params[1], params[2], params[3]);
  9560. } else if (tensor->op == GGML_OP_TRANSPOSE) {
  9561. tensor_clone = ggml_transpose(ggml_ctx, src_clone[0]);
  9562. } else if (tensor->op == GGML_OP_GET_ROWS) {
  9563. tensor_clone = ggml_get_rows(ggml_ctx, src_clone[0], src_clone[1]);
  9564. } else if (tensor->op == GGML_OP_ARGSORT) {
  9565. tensor_clone = ggml_argsort(ggml_ctx, src_clone[0], (ggml_sort_order) *(int *)tensor->op_params);
  9566. } else if (tensor->op == GGML_OP_SUM) {
  9567. tensor_clone = ggml_sum(ggml_ctx, src_clone[0]);
  9568. } else if (tensor->op == GGML_OP_SUM_ROWS) {
  9569. tensor_clone = ggml_sum_rows(ggml_ctx, src_clone[0]);
  9570. } else if (tensor->op == GGML_OP_ARGMAX) {
  9571. tensor_clone = ggml_argmax(ggml_ctx, src_clone[0]);
  9572. } else if (tensor->op == GGML_OP_COUNT_EQUAL) {
  9573. tensor_clone = ggml_count_equal(ggml_ctx, src_clone[0], src_clone[1]);
  9574. } else if (tensor->op == GGML_OP_IM2COL) {
  9575. const int32_t s0 = tensor->op_params[0];
  9576. const int32_t s1 = tensor->op_params[1];
  9577. const int32_t p0 = tensor->op_params[2];
  9578. const int32_t p1 = tensor->op_params[3];
  9579. const int32_t d0 = tensor->op_params[4];
  9580. const int32_t d1 = tensor->op_params[5];
  9581. const bool is_2D = tensor->op_params[6] == 1;
  9582. tensor_clone = ggml_im2col(ggml_ctx, src_clone[0], src_clone[1], s0, s1, p0, p1, d0, d1, is_2D, tensor->type);
  9583. } else if (tensor->op == GGML_OP_TIMESTEP_EMBEDDING) {
  9584. const int32_t dim = tensor->op_params[0];
  9585. const int32_t max_period = tensor->op_params[1];
  9586. tensor_clone = ggml_timestep_embedding(ggml_ctx, src_clone[0], dim, max_period);
  9587. } else if (tensor->op == GGML_OP_CONV_TRANSPOSE_1D){
  9588. const int32_t s0 = tensor->op_params[0];
  9589. const int32_t p0 = tensor->op_params[1];
  9590. const int32_t d0 = tensor->op_params[2];
  9591. tensor_clone = ggml_conv_transpose_1d(ggml_ctx, src_clone[0], src_clone[1], s0, p0, d0);
  9592. } else if (tensor->op == GGML_OP_POOL_2D) {
  9593. enum ggml_op_pool op = static_cast<ggml_op_pool>(tensor->op_params[0]);
  9594. const int32_t k0 = tensor->op_params[1];
  9595. const int32_t k1 = tensor->op_params[2];
  9596. const int32_t s0 = tensor->op_params[3];
  9597. const int32_t s1 = tensor->op_params[4];
  9598. const int32_t p0 = tensor->op_params[5];
  9599. const int32_t p1 = tensor->op_params[6];
  9600. tensor_clone = ggml_pool_2d(ggml_ctx, src_clone[0], op, k0, k1, s0, s1, p0, p1);
  9601. } else if (tensor->op == GGML_OP_LEAKY_RELU) {
  9602. const float * op_params = (const float *)tensor->op_params;
  9603. tensor_clone = ggml_leaky_relu(ggml_ctx, src_clone[0], op_params[0], false);
  9604. } else if (tensor->op == GGML_OP_RWKV_WKV6) {
  9605. tensor_clone = ggml_rwkv_wkv6(ggml_ctx, src_clone[0], src_clone[1],
  9606. src_clone[2], src_clone[3], src_clone[4], src_clone[5]);
  9607. } else if (tensor->op == GGML_OP_RWKV_WKV7) {
  9608. tensor_clone = ggml_rwkv_wkv7(ggml_ctx, src_clone[0], src_clone[1], src_clone[2], src_clone[3],
  9609. src_clone[4], src_clone[5], src_clone[6]);
  9610. } else if (tensor->op == GGML_OP_OPT_STEP_ADAMW) {
  9611. src_clone[0]->flags = src0->flags;
  9612. tensor_clone = ggml_opt_step_adamw(ggml_ctx, src_clone[0], src_clone[1],
  9613. src_clone[2], src_clone[3], src_clone[4]);
  9614. }
  9615. else {
  9616. std::cerr << "Missing vk_check_results OP: " << ggml_op_name(tensor->op) << std::endl;
  9617. GGML_ABORT("fatal error");
  9618. }
  9619. ggml_cgraph * cgraph_cpu = ggml_new_graph(ggml_ctx);
  9620. ggml_build_forward_expand(cgraph_cpu, tensor_clone);
  9621. ggml_graph_compute_with_ctx(ggml_ctx, cgraph_cpu, 8);
  9622. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  9623. ggml_vk_print_tensor(tensor_clone, "tensor_clone");
  9624. }
  9625. comp_size = ggml_nbytes(tensor_clone);
  9626. comp_result = malloc(comp_size);
  9627. memcpy(comp_result, tensor_clone->data, comp_size);
  9628. memcpy(comp_nb, tensor_clone->nb, sizeof(size_t) * GGML_MAX_DIMS);
  9629. for (int i = 0; i < 6; i++) {
  9630. if (src_buffer[i] != nullptr) {
  9631. free(src_buffer[i]);
  9632. }
  9633. }
  9634. ggml_free(ggml_ctx);
  9635. VK_LOG_DEBUG("END ggml_vk_check_results_0(" << tensor->name << ")");
  9636. }
  9637. static void ggml_vk_check_results_1(ggml_backend_vk_context * ctx, ggml_cgraph * cgraph, int tensor_idx) {
  9638. ggml_tensor * tensor = cgraph->nodes[tensor_idx];
  9639. if (tensor->op == GGML_OP_TRANSPOSE) {
  9640. return;
  9641. }
  9642. bool fused_rms_norm_mul = false;
  9643. if (ctx->num_additional_fused_ops == 1 &&
  9644. tensor->op == GGML_OP_RMS_NORM &&
  9645. cgraph->nodes[tensor_idx + 1]->op == GGML_OP_MUL) {
  9646. fused_rms_norm_mul = true;
  9647. tensor = cgraph->nodes[tensor_idx + 1];
  9648. }
  9649. if (!(vk_output_tensor > 0 && vk_output_tensor == check_counter) && check_counter <= vk_skip_checks) {
  9650. return;
  9651. }
  9652. VK_LOG_DEBUG("ggml_vk_check_results_1(" << tensor->name << ")");
  9653. ggml_tensor * src0 = tensor->src[0];
  9654. ggml_tensor * src1 = tensor->src[1];
  9655. ggml_tensor * src2 = tensor->src[2];
  9656. ggml_tensor * src3 = tensor->src[3];
  9657. void * tensor_data = tensor->data;
  9658. if (ggml_backend_buffer_is_vk(tensor->buffer)) {
  9659. size_t tensor_size = ggml_nbytes(tensor);
  9660. tensor_data = malloc(tensor_size);
  9661. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  9662. vk_buffer& buffer_gpu = buf_ctx->dev_buffer;
  9663. uint64_t offset = vk_tensor_offset(tensor) + tensor->view_offs;
  9664. if (offset + tensor_size >= buffer_gpu->size) {
  9665. tensor_size = buffer_gpu->size - offset;
  9666. }
  9667. ggml_vk_buffer_read(buffer_gpu, offset, tensor_data, tensor_size);
  9668. }
  9669. float first_error_result = -1.0f;
  9670. float first_error_correct = -1.0f;
  9671. std::array<int, 4> first_error = { -1, -1, -1, -1 };
  9672. double avg_err = 0.0;
  9673. size_t counter = 0;
  9674. for (int i3 = 0; i3 < tensor->ne[3]; i3++) {
  9675. for (int i2 = 0; i2 < tensor->ne[2]; i2++) {
  9676. for (int i1 = 0; i1 < tensor->ne[1]; i1++) {
  9677. for (int i0 = 0; i0 < tensor->ne[0]; i0++) {
  9678. const bool buffer_size_fit = i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0] < comp_size;
  9679. float correct = 0.0f;
  9680. float result = 0.0f;
  9681. if (buffer_size_fit) {
  9682. if (tensor->type == GGML_TYPE_F32) {
  9683. correct = *(float *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]);
  9684. result = *(float *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]);
  9685. } else if (tensor->type == GGML_TYPE_F16) {
  9686. 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]));
  9687. 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]));
  9688. } else if (tensor->type == GGML_TYPE_I32) {
  9689. correct = *(int32_t *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]);
  9690. result = *(int32_t *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]);
  9691. } else if (tensor->type == GGML_TYPE_I64) {
  9692. correct = *(int64_t *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]);
  9693. result = *(int64_t *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]);
  9694. } else {
  9695. std::cerr << "Results check not implemented for type " << ggml_type_name(tensor->type) << std::endl;
  9696. }
  9697. } else {
  9698. std::cerr << "Missing debug code for type " << ggml_type_name(tensor->type) << std::endl;
  9699. GGML_ABORT("fatal error");
  9700. }
  9701. if ((std::isnan(correct) != std::isnan(result)) || (std::isinf(correct) != std::isinf(result)) || !buffer_size_fit) {
  9702. 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;
  9703. 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;
  9704. if (src0 != nullptr) {
  9705. 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;
  9706. }
  9707. if (src1 != nullptr) {
  9708. 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;
  9709. }
  9710. if (src2 != nullptr) {
  9711. 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;
  9712. }
  9713. if (src3 != nullptr) {
  9714. 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;
  9715. }
  9716. 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;
  9717. std::cerr << std::endl << "Result:" << std::endl;
  9718. ggml_vk_print_tensor_area(tensor, tensor_data, i0, i1, i2, i3);
  9719. std::cerr << std::endl << "Correct:" << std::endl;
  9720. ggml_vk_print_tensor_area(tensor, comp_result, i0, i1, i2, i3);
  9721. std::cerr << std::endl;
  9722. std::vector<const ggml_tensor *> done;
  9723. ggml_vk_print_graph_origin(tensor, done);
  9724. GGML_ABORT("fatal error");
  9725. }
  9726. const double denom = std::fabs(correct) > 1.0f ? (std::fabs(correct) > 1e-8 ? std::fabs(correct) : 1e-8) : 1.0f;
  9727. if (first_error[0] == -1 && std::fabs(correct - result) / denom > 0.5) {
  9728. first_error[0] = i0;
  9729. first_error[1] = i1;
  9730. first_error[2] = i2;
  9731. first_error[3] = i3;
  9732. first_error_result = result;
  9733. first_error_correct = correct;
  9734. }
  9735. // Special case, value is infinite, avoid NaN result in avg_err
  9736. // NaN also appears in results, if both are nan error is 0
  9737. if (!std::isinf(correct) && !std::isinf(result) && !std::isnan(correct) && !std::isnan(result)) {
  9738. avg_err += std::fabs(correct - result) / denom;
  9739. }
  9740. counter++;
  9741. }
  9742. }
  9743. }
  9744. }
  9745. avg_err /= counter;
  9746. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  9747. std::cerr << "TENSOR CHECK: avg_err=" << avg_err << " in " << ggml_op_name(tensor->op) << " (check " << check_counter << ")" << std::endl;
  9748. 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;
  9749. if (src0 != nullptr) {
  9750. 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;
  9751. }
  9752. if (src1 != nullptr) {
  9753. 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;
  9754. }
  9755. if (src2 != nullptr) {
  9756. 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;
  9757. }
  9758. if (src3 != nullptr) {
  9759. 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;
  9760. }
  9761. 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;
  9762. std::cerr << std::endl << "Result:" << std::endl;
  9763. ggml_vk_print_tensor_area(tensor, tensor_data, 5, 5, 0, 0);
  9764. std::cerr << std::endl << "Correct:" << std::endl;
  9765. ggml_vk_print_tensor_area(tensor, comp_result, 5, 5, 0, 0);
  9766. std::cerr << std::endl;
  9767. std::vector<const ggml_tensor *> done;
  9768. ggml_vk_print_graph_origin(tensor, done);
  9769. }
  9770. if (avg_err > 0.5 || std::isnan(avg_err)) {
  9771. std::cerr << "ERROR: avg_err=" << avg_err << " in " << ggml_op_name(tensor->op) << " (check " << check_counter << ")" << std::endl;
  9772. 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;
  9773. if (src0 != nullptr) {
  9774. 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;
  9775. }
  9776. if (src1 != nullptr) {
  9777. 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;
  9778. }
  9779. if (src2 != nullptr) {
  9780. 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;
  9781. }
  9782. if (src3 != nullptr) {
  9783. 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;
  9784. }
  9785. 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;
  9786. std::cerr << std::endl << "Result:" << std::endl;
  9787. ggml_vk_print_tensor_area(tensor, tensor_data, first_error[0], first_error[1], first_error[2], first_error[3]);
  9788. std::cerr << std::endl << "Correct:" << std::endl;
  9789. ggml_vk_print_tensor_area(tensor, comp_result, first_error[0], first_error[1], first_error[2], first_error[3]);
  9790. std::cerr << std::endl;
  9791. std::vector<const ggml_tensor *> done;
  9792. ggml_vk_print_graph_origin(tensor, done);
  9793. GGML_ABORT("fatal error");
  9794. } else {
  9795. std::cerr << check_counter << " " << tensor->name << " op=" << ggml_op_name(tensor->op) << " avg_err=" << avg_err << std::endl;
  9796. }
  9797. free(comp_result);
  9798. comp_result = nullptr;
  9799. comp_size = 0;
  9800. if (ggml_backend_buffer_is_vk(tensor->buffer)) {
  9801. free(tensor_data);
  9802. }
  9803. VK_LOG_DEBUG("END ggml_vk_check_results_1(" << tensor->name << ")");
  9804. }
  9805. #endif
  9806. GGML_BACKEND_DL_IMPL(ggml_backend_vk_reg)