ggml-vulkan.cpp 387 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197
  1. #include "ggml-vulkan.h"
  2. #ifdef GGML_VULKAN_RUN_TESTS
  3. #include <chrono>
  4. #endif
  5. #include <vulkan/vulkan.hpp>
  6. #include <algorithm>
  7. #include <cmath>
  8. #include <iostream>
  9. #include <limits>
  10. #include <tuple>
  11. #include <vector>
  12. #include <sstream>
  13. #include <utility>
  14. #include <memory>
  15. #include "ggml.h"
  16. #include "ggml-backend-impl.h"
  17. #include "ggml-vulkan-shaders.hpp"
  18. #define VK_API_VERSION VK_API_VERSION_1_2
  19. #define CEIL_DIV(M, N) (((M) + (N)-1) / (N))
  20. #define VK_VENDOR_ID_AMD 0x1002
  21. #define VK_VENDOR_ID_APPLE 0x106b
  22. #define VK_VENDOR_ID_INTEL 0x8086
  23. #define VK_VENDOR_ID_NVIDIA 0x10de
  24. #define VK_DEVICE_DESCRIPTOR_POOL_MODE_UNKNOWN 0
  25. #define VK_DEVICE_DESCRIPTOR_POOL_MODE_MULTI 1
  26. #define VK_DEVICE_DESCRIPTOR_POOL_MODE_SINGLE 2
  27. #define VK_NUM_TYPES 16
  28. #define GGML_VK_MAX_NODES 8192
  29. #define MAX_VK_BUFFERS 256
  30. #ifndef K_QUANTS_PER_ITERATION
  31. #define K_QUANTS_PER_ITERATION 1
  32. #else
  33. static_assert(K_QUANTS_PER_ITERATION == 1 || K_QUANTS_PER_ITERATION == 2, "K_QUANTS_PER_ITERATION must be 1 or 2");
  34. #endif
  35. #define VK_CHECK(err, msg) \
  36. do { \
  37. vk::Result err_ = (err); \
  38. if (err_ != vk::Result::eSuccess) { \
  39. fprintf(stderr, "ggml_vulkan: %s error %s at %s:%d\n", \
  40. #err, to_string(err_).c_str(), __FILE__, __LINE__); \
  41. exit(1); \
  42. } \
  43. } while (0)
  44. struct ggml_backend_vk_context;
  45. struct vk_queue {
  46. uint32_t queue_family_index;
  47. vk::Queue queue;
  48. vk::CommandPool pool;
  49. uint32_t cmd_buffer_idx;
  50. std::vector<vk::CommandBuffer> cmd_buffers;
  51. vk::PipelineStageFlags stage_flags;
  52. };
  53. struct vk_pipeline_struct {
  54. std::string name;
  55. vk::ShaderModule shader_module;
  56. vk::DescriptorSetLayout dsl;
  57. std::vector<vk::DescriptorPool> descriptor_pools;
  58. std::vector<vk::DescriptorSet> descriptor_sets;
  59. uint32_t descriptor_set_idx;
  60. vk::PipelineLayout layout;
  61. vk::Pipeline pipeline;
  62. uint32_t push_constant_size;
  63. uint32_t parameter_count;
  64. std::array<uint32_t, 3> wg_denoms;
  65. uint32_t align;
  66. };
  67. typedef std::shared_ptr<vk_pipeline_struct> vk_pipeline;
  68. typedef std::weak_ptr<vk_pipeline_struct> vk_pipeline_ref;
  69. static void ggml_vk_destroy_pipeline(vk::Device& device, vk_pipeline& pipeline);
  70. struct vk_matmul_pipeline_struct {
  71. vk_pipeline l, m, s;
  72. vk_pipeline a_l, a_m, a_s;
  73. };
  74. typedef std::shared_ptr<vk_matmul_pipeline_struct> vk_matmul_pipeline;
  75. struct vk_device {
  76. vk::PhysicalDevice physical_device;
  77. vk::PhysicalDeviceProperties properties;
  78. std::string name;
  79. uint64_t max_memory_allocation_size;
  80. bool fp16;
  81. vk::Device device;
  82. uint32_t vendor_id;
  83. vk_queue compute_queue;
  84. vk_queue transfer_queue;
  85. bool single_queue;
  86. uint32_t descriptor_set_mode;
  87. uint32_t subgroup_size;
  88. bool uma;
  89. bool initialized;
  90. size_t idx;
  91. vk_matmul_pipeline pipeline_matmul_f32;
  92. vk_matmul_pipeline pipeline_matmul_f32_f16;
  93. vk_matmul_pipeline pipeline_matmul_f16;
  94. vk_matmul_pipeline pipeline_matmul_f16_f32;
  95. vk_pipeline pipeline_matmul_split_k_reduce;
  96. vk_matmul_pipeline pipeline_dequant_mul_mat_mat[VK_NUM_TYPES];
  97. vk_matmul_pipeline pipeline_matmul_id_f32;
  98. vk_matmul_pipeline pipeline_matmul_id_f16;
  99. vk_matmul_pipeline pipeline_matmul_id_f16_f32;
  100. vk_matmul_pipeline pipeline_dequant_mul_mat_mat_id[VK_NUM_TYPES];
  101. vk_pipeline pipeline_dequant[VK_NUM_TYPES];
  102. vk_pipeline pipeline_dequant_mul_mat_vec_f32_f32[VK_NUM_TYPES];
  103. vk_pipeline pipeline_dequant_mul_mat_vec_f16_f32[VK_NUM_TYPES];
  104. vk_pipeline pipeline_dequant_mul_mat_vec_id_f32[VK_NUM_TYPES];
  105. vk_pipeline pipeline_mul_mat_vec_p021_f16_f32;
  106. vk_pipeline pipeline_mul_mat_vec_nc_f16_f32;
  107. vk_pipeline pipeline_get_rows[VK_NUM_TYPES];
  108. vk_pipeline pipeline_get_rows_f32[VK_NUM_TYPES];
  109. vk_pipeline pipeline_mul_f32;
  110. vk_pipeline pipeline_div_f32;
  111. vk_pipeline pipeline_add_f32;
  112. vk_pipeline pipeline_scale_f32;
  113. vk_pipeline pipeline_sqr_f32;
  114. vk_pipeline pipeline_clamp_f32;
  115. vk_pipeline pipeline_cpy_f32_f32, pipeline_cpy_f32_f16, pipeline_cpy_f16_f16;
  116. vk_pipeline pipeline_norm_f32;
  117. vk_pipeline pipeline_rms_norm_f32;
  118. vk_pipeline pipeline_gelu_f32;
  119. vk_pipeline pipeline_silu_f32;
  120. vk_pipeline pipeline_relu_f32;
  121. vk_pipeline pipeline_diag_mask_inf_f32;
  122. vk_pipeline pipeline_soft_max_f32, pipeline_soft_max_f32_f16;
  123. vk_pipeline pipeline_rope_norm_f32, pipeline_rope_norm_f16;
  124. vk_pipeline pipeline_rope_neox_f32, pipeline_rope_neox_f16;
  125. vk_pipeline pipeline_argsort_f32;
  126. vk_pipeline pipeline_sum_rows_f32;
  127. std::vector<vk_pipeline_ref> pipelines;
  128. ~vk_device() {
  129. #ifdef GGML_VULKAN_DEBUG
  130. std::cerr << "destroy device " << name << std::endl;
  131. #endif
  132. device.destroyCommandPool(compute_queue.pool);
  133. if (!single_queue) {
  134. device.destroyCommandPool(transfer_queue.pool);
  135. }
  136. for (auto& pipeline : pipelines) {
  137. if (pipeline.expired()) {
  138. continue;
  139. }
  140. vk_pipeline pl = pipeline.lock();
  141. ggml_vk_destroy_pipeline(device, pl);
  142. }
  143. pipelines.clear();
  144. device.destroy();
  145. }
  146. };
  147. struct vk_buffer_struct {
  148. vk::Buffer buffer;
  149. vk::DeviceMemory device_memory;
  150. vk::MemoryPropertyFlags memory_property_flags;
  151. void * ptr;
  152. size_t size = 0;
  153. ggml_backend_vk_context * ctx;
  154. std::shared_ptr<vk_device> device;
  155. ~vk_buffer_struct() {
  156. if (size == 0) {
  157. return;
  158. }
  159. #ifdef GGML_VULKAN_DEBUG
  160. std::cerr << "~vk_buffer_struct(" << buffer << ", " << size << ")" << std::endl;
  161. #endif
  162. device->device.freeMemory(device_memory);
  163. device->device.destroyBuffer(buffer);
  164. }
  165. };
  166. typedef std::shared_ptr<vk_buffer_struct> vk_buffer;
  167. typedef std::weak_ptr<vk_buffer_struct> vk_buffer_ref;
  168. struct vk_subbuffer {
  169. vk_buffer buffer;
  170. uint64_t offset;
  171. uint64_t size;
  172. };
  173. struct vk_semaphore {
  174. vk::Semaphore s;
  175. uint64_t value;
  176. };
  177. struct vk_submission {
  178. vk::CommandBuffer buffer;
  179. std::vector<vk_semaphore> wait_semaphores;
  180. std::vector<vk_semaphore> signal_semaphores;
  181. };
  182. typedef std::vector<vk_submission> vk_sequence;
  183. struct vk_mat_mat_push_constants {
  184. uint32_t M; uint32_t N; uint32_t K;
  185. uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  186. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  187. uint32_t k_split;
  188. uint32_t ne02; uint32_t ne12; uint32_t broadcast2; uint32_t broadcast3;
  189. };
  190. struct vk_mat_vec_push_constants {
  191. uint32_t ncols; uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  192. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  193. uint32_t ne02; uint32_t ne12; uint32_t broadcast2; uint32_t broadcast3;
  194. };
  195. struct vk_mat_mat_id_push_constants {
  196. uint32_t M; uint32_t N; uint32_t K;
  197. uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  198. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  199. uint32_t nei0; uint32_t nei1; uint32_t nbi1; uint32_t ne11;
  200. };
  201. struct vk_mat_vec_id_push_constants {
  202. uint32_t ncols; uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  203. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  204. uint32_t nei0; uint32_t ne11;
  205. };
  206. struct vk_op_push_constants {
  207. uint32_t KX;
  208. uint32_t KY;
  209. float param1;
  210. float param2;
  211. };
  212. struct vk_op_unary_push_constants {
  213. uint32_t ne;
  214. uint32_t ne00; uint32_t ne01; uint32_t ne02; uint32_t ne03; uint32_t nb00; uint32_t nb01; uint32_t nb02; uint32_t nb03;
  215. uint32_t ne10; uint32_t ne11; uint32_t ne12; uint32_t ne13; uint32_t nb10; uint32_t nb11; uint32_t nb12; uint32_t nb13;
  216. uint32_t d_offset;
  217. float param1; float param2;
  218. };
  219. struct vk_op_binary_push_constants {
  220. uint32_t ne;
  221. uint32_t ne00; uint32_t ne01; uint32_t ne02; uint32_t ne03; uint32_t nb00; uint32_t nb01; uint32_t nb02; uint32_t nb03;
  222. uint32_t ne10; uint32_t ne11; uint32_t ne12; uint32_t ne13; uint32_t nb10; uint32_t nb11; uint32_t nb12; uint32_t nb13;
  223. uint32_t ne20; uint32_t ne21; uint32_t ne22; uint32_t ne23; uint32_t nb20; uint32_t nb21; uint32_t nb22; uint32_t nb23;
  224. uint32_t d_offset;
  225. float param1; float param2;
  226. };
  227. struct vk_op_diag_mask_push_constants {
  228. uint32_t ncols;
  229. uint32_t rows_per_channel;
  230. int32_t n_past;
  231. };
  232. struct vk_op_rope_push_constants {
  233. uint32_t ncols;
  234. uint32_t n_dims;
  235. float freq_scale;
  236. uint32_t p_delta_rows;
  237. float freq_base;
  238. float ext_factor;
  239. float attn_factor;
  240. float corr_dims[2];
  241. float theta_scale;
  242. uint32_t has_ff;
  243. };
  244. struct vk_op_soft_max_push_constants {
  245. uint32_t KX;
  246. uint32_t KY;
  247. float scale;
  248. float max_bias;
  249. float m0;
  250. float m1;
  251. uint32_t n_head_log2;
  252. };
  253. struct vk_op_argsort_push_constants {
  254. uint32_t ncols;
  255. uint32_t ncols_pad;
  256. int32_t order;
  257. };
  258. // Allow pre-recording command buffers
  259. struct vk_staging_memcpy {
  260. vk_staging_memcpy(void * _dst, const void * _src, size_t _n) : dst(_dst), src(_src), n(_n) {}
  261. void * dst;
  262. const void * src;
  263. size_t n;
  264. };
  265. struct vk_context {
  266. size_t idx;
  267. vk_submission * s;
  268. std::vector<vk_sequence> seqs;
  269. ggml_tensor * exit_tensor;
  270. std::vector<vk_staging_memcpy> in_memcpys;
  271. std::vector<vk_staging_memcpy> out_memcpys;
  272. vk_queue * q;
  273. };
  274. struct ggml_tensor_extra_gpu {
  275. size_t ctx_idx;
  276. vk_buffer_ref buffer_gpu;
  277. uint64_t offset;
  278. void reset() {
  279. ctx_idx = 0;
  280. buffer_gpu.reset();
  281. offset = 0;
  282. }
  283. };
  284. struct ggml_vk_garbage_collector {
  285. std::vector<vk_semaphore> tl_semaphores;
  286. std::vector<vk_semaphore> semaphores;
  287. std::vector<vk::Event> events;
  288. std::vector<vk_buffer> temp_buffers;
  289. std::vector<vk_context> contexts;
  290. };
  291. struct ggml_backend_vk_context {
  292. std::string name;
  293. std::shared_ptr<vk_device> device;
  294. size_t semaphore_idx, event_idx;
  295. ggml_vk_garbage_collector gc;
  296. std::vector<std::tuple<void*, size_t, vk_buffer>> pinned_memory;
  297. size_t prealloc_size_x, prealloc_size_y, prealloc_size_split_k;
  298. vk_buffer prealloc_x, prealloc_y, prealloc_split_k;
  299. vk::Fence fence;
  300. vk_buffer staging;
  301. size_t staging_size;
  302. size_t staging_offset;
  303. vk_buffer sync_staging;
  304. vk_buffer buffer_pool[MAX_VK_BUFFERS];
  305. vk_context * compute_ctx;
  306. vk_context * transfer_ctx;
  307. bool initialized;
  308. size_t idx;
  309. };
  310. struct vk_instance_t {
  311. vk::Instance instance;
  312. std::vector<size_t> device_indices;
  313. ggml_backend_t backends[GGML_VK_MAX_DEVICES];
  314. ggml_backend_vk_context contexts[GGML_VK_MAX_DEVICES];
  315. ggml_backend_buffer_type buffer_types[GGML_VK_MAX_DEVICES];
  316. bool initialized[GGML_VK_MAX_DEVICES];
  317. };
  318. static std::shared_ptr<vk_device> ggml_vk_get_device(size_t idx) {
  319. #ifdef GGML_VULKAN_DEBUG
  320. std::cerr << "ggml_vk_get_device(" << idx << ")" << std::endl;
  321. #endif
  322. static std::weak_ptr<vk_device> devices[GGML_VK_MAX_DEVICES];
  323. if (devices[idx].expired()) {
  324. #ifdef GGML_VULKAN_DEBUG
  325. std::cerr << "Initializing new vk_device" << std::endl;
  326. #endif
  327. std::shared_ptr<vk_device> device = std::make_shared<vk_device>();
  328. device->initialized = false;
  329. devices[idx] = device;
  330. return device;
  331. }
  332. return devices[idx].lock();
  333. }
  334. #ifdef GGML_VULKAN_CHECK_RESULTS
  335. static size_t vk_skip_checks;
  336. static size_t vk_output_tensor;
  337. static void ggml_vk_print_tensor(ggml_backend * ctx, const ggml_tensor * tensor, const char * name);
  338. static void ggml_vk_check_results_0(ggml_backend_vk_context * ctx, ggml_compute_params * params, ggml_tensor * tensor);
  339. static void ggml_vk_check_results_1(ggml_backend_vk_context * ctx, ggml_compute_params * params, ggml_tensor * tensor);
  340. #endif
  341. 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);
  342. static bool vk_instance_initialized = false;
  343. static vk_instance_t vk_instance;
  344. GGML_CALL static void ggml_backend_vk_free(ggml_backend_t backend);
  345. static void ggml_vk_create_pipeline(ggml_backend_vk_context * ctx, vk_pipeline& pipeline, const std::string& name, size_t spv_size, const void* spv_data, const std::string& entrypoint, uint32_t parameter_count, uint32_t push_constant_size, std::array<uint32_t, 3> wg_denoms, std::vector<uint32_t>&& specialization_constants, uint32_t align) {
  346. #ifdef GGML_VULKAN_DEBUG
  347. std::cerr << "ggml_vk_create_pipeline(" << name << ", " << entrypoint << ", " << parameter_count << ", " << push_constant_size << ", (" << wg_denoms[0] << "," << wg_denoms[1] << "," << wg_denoms[2] << "), specialization_constants, " << align << ")" << std::endl;
  348. #endif
  349. GGML_ASSERT(parameter_count > 0);
  350. GGML_ASSERT(wg_denoms[0] > 0 && wg_denoms[1] > 0 && wg_denoms[2] > 0); // NOLINT
  351. pipeline = std::make_shared<vk_pipeline_struct>();
  352. pipeline->name = name;
  353. pipeline->parameter_count = parameter_count;
  354. pipeline->push_constant_size = push_constant_size;
  355. pipeline->wg_denoms = wg_denoms;
  356. pipeline->align = align;
  357. vk::ShaderModuleCreateInfo shader_module_create_info({}, spv_size, reinterpret_cast<const uint32_t *>(spv_data));
  358. pipeline->shader_module = ctx->device->device.createShaderModule(shader_module_create_info);
  359. std::vector<vk::DescriptorSetLayoutBinding> dsl_binding;
  360. std::vector<vk::DescriptorBindingFlags> dsl_binding_flags;
  361. for (uint32_t i = 0; i < parameter_count; i++) {
  362. dsl_binding.push_back({i, vk::DescriptorType::eStorageBuffer, 1, vk::ShaderStageFlagBits::eCompute});
  363. dsl_binding_flags.push_back({});
  364. }
  365. vk::DescriptorSetLayoutBindingFlagsCreateInfo dslbfci = { dsl_binding_flags };
  366. vk::PushConstantRange pcr(
  367. vk::ShaderStageFlagBits::eCompute,
  368. 0,
  369. pipeline->push_constant_size
  370. );
  371. vk::DescriptorSetLayoutCreateInfo descriptor_set_layout_create_info(
  372. {},
  373. dsl_binding);
  374. descriptor_set_layout_create_info.setPNext(&dslbfci);
  375. pipeline->dsl = ctx->device->device.createDescriptorSetLayout(descriptor_set_layout_create_info);
  376. // Check if device supports multiple descriptors per pool
  377. if (ctx->device->descriptor_set_mode == VK_DEVICE_DESCRIPTOR_POOL_MODE_UNKNOWN) {
  378. const uint32_t alloc_count = 2;
  379. // Try allocating multiple sets from one pool
  380. // This fails on AMD for some reason, so add a fall back to allocating one pool per set
  381. vk::DescriptorPoolSize descriptor_pool_size(vk::DescriptorType::eStorageBuffer, pipeline->parameter_count);
  382. vk::DescriptorPoolCreateInfo descriptor_pool_create_info({}, alloc_count, descriptor_pool_size);
  383. vk::DescriptorPool pool = ctx->device->device.createDescriptorPool(descriptor_pool_create_info);
  384. std::vector<vk::DescriptorSetLayout> layouts(alloc_count);
  385. for (uint32_t i = 0; i < alloc_count; i++) {
  386. layouts[i] = pipeline->dsl;
  387. }
  388. try {
  389. vk::DescriptorSetAllocateInfo descriptor_set_alloc_info(pool, alloc_count, layouts.data());
  390. std::vector<vk::DescriptorSet> sets = ctx->device->device.allocateDescriptorSets(descriptor_set_alloc_info);
  391. } catch(vk::OutOfPoolMemoryError const&) {
  392. ctx->device->descriptor_set_mode = VK_DEVICE_DESCRIPTOR_POOL_MODE_SINGLE;
  393. }
  394. ctx->device->device.destroyDescriptorPool(pool);
  395. }
  396. if (ctx->device->descriptor_set_mode == VK_DEVICE_DESCRIPTOR_POOL_MODE_MULTI) {
  397. vk::DescriptorPoolSize descriptor_pool_size(vk::DescriptorType::eStorageBuffer, pipeline->parameter_count);
  398. vk::DescriptorPoolCreateInfo descriptor_pool_create_info({}, 128, descriptor_pool_size);
  399. pipeline->descriptor_pools.push_back(ctx->device->device.createDescriptorPool(descriptor_pool_create_info));
  400. }
  401. pipeline->descriptor_set_idx = 0;
  402. vk::PipelineLayoutCreateInfo pipeline_layout_create_info(vk::PipelineLayoutCreateFlags(), pipeline->dsl, pcr);
  403. pipeline->layout = ctx->device->device.createPipelineLayout(pipeline_layout_create_info);
  404. std::vector<vk::SpecializationMapEntry> specialization_entries(specialization_constants.size());
  405. for (size_t i = 0; i < specialization_constants.size(); i++) {
  406. specialization_entries[i].constantID = i;
  407. specialization_entries[i].offset = i * sizeof(uint32_t);
  408. specialization_entries[i].size = sizeof(uint32_t);
  409. }
  410. vk::SpecializationInfo specialization_info(
  411. specialization_entries.size(),
  412. specialization_entries.data(),
  413. specialization_constants.size() * sizeof(uint32_t),
  414. specialization_constants.data()
  415. );
  416. vk::PipelineShaderStageCreateInfo pipeline_shader_create_info(
  417. vk::PipelineShaderStageCreateFlags(),
  418. vk::ShaderStageFlagBits::eCompute,
  419. pipeline->shader_module,
  420. entrypoint.c_str(),
  421. &specialization_info);
  422. vk::ComputePipelineCreateInfo compute_pipeline_create_info(
  423. vk::PipelineCreateFlags(),
  424. pipeline_shader_create_info,
  425. pipeline->layout);
  426. pipeline->pipeline = ctx->device->device.createComputePipeline(VK_NULL_HANDLE, compute_pipeline_create_info).value;
  427. ctx->device->pipelines.push_back(pipeline);
  428. }
  429. static void ggml_vk_destroy_pipeline(vk::Device& device, vk_pipeline& pipeline) {
  430. #ifdef GGML_VULKAN_DEBUG
  431. std::cerr << "ggml_pipeline_destroy_pipeline(" << pipeline->name << ")" << std::endl;
  432. #endif
  433. for (auto& pool : pipeline->descriptor_pools) {
  434. device.destroyDescriptorPool(pool);
  435. }
  436. pipeline->descriptor_pools.clear();
  437. pipeline->descriptor_sets.clear();
  438. pipeline->descriptor_set_idx = 0;
  439. device.destroyDescriptorSetLayout(pipeline->dsl);
  440. device.destroyPipelineLayout(pipeline->layout);
  441. device.destroyShaderModule(pipeline->shader_module);
  442. device.destroyPipeline(pipeline->pipeline);
  443. }
  444. static void ggml_pipeline_allocate_descriptor_sets(ggml_backend_vk_context * ctx, vk_pipeline& pipeline, uint32_t n) {
  445. #ifdef GGML_VULKAN_DEBUG
  446. std::cerr << "ggml_pipeline_allocate_descriptor_sets(" << pipeline->name << ", " << n << ")" << std::endl;
  447. #endif
  448. if (pipeline->descriptor_sets.size() >= pipeline->descriptor_set_idx + n) {
  449. // Enough descriptors are available
  450. return;
  451. }
  452. if (ctx->device->descriptor_set_mode == VK_DEVICE_DESCRIPTOR_POOL_MODE_MULTI) {
  453. const uint32_t alloc_count = pipeline->descriptor_set_idx + n - pipeline->descriptor_sets.size();
  454. std::vector<vk::DescriptorSetLayout> layouts(alloc_count);
  455. for (uint32_t i = 0; i < alloc_count; i++) {
  456. layouts[i] = pipeline->dsl;
  457. }
  458. vk::DescriptorSetAllocateInfo descriptor_set_alloc_info(pipeline->descriptor_pools[0], alloc_count, layouts.data());
  459. std::vector<vk::DescriptorSet> sets = ctx->device->device.allocateDescriptorSets(descriptor_set_alloc_info);
  460. pipeline->descriptor_sets.insert(pipeline->descriptor_sets.end(), sets.begin(), sets.end());
  461. } else {
  462. for (uint32_t i = pipeline->descriptor_sets.size(); i < pipeline->descriptor_set_idx + n; i++) {
  463. vk::DescriptorPoolSize descriptor_pool_size(vk::DescriptorType::eStorageBuffer, pipeline->parameter_count);
  464. vk::DescriptorPoolCreateInfo descriptor_pool_create_info({}, 1, descriptor_pool_size);
  465. pipeline->descriptor_pools.push_back(ctx->device->device.createDescriptorPool(descriptor_pool_create_info));
  466. vk::DescriptorSetAllocateInfo descriptor_set_alloc_info(pipeline->descriptor_pools[i], 1, &pipeline->dsl);
  467. std::vector<vk::DescriptorSet> sets = ctx->device->device.allocateDescriptorSets(descriptor_set_alloc_info);
  468. pipeline->descriptor_sets.push_back(sets[0]);
  469. }
  470. }
  471. }
  472. static void ggml_pipeline_cleanup(vk_pipeline& pipeline) {
  473. #ifdef GGML_VULKAN_DEBUG
  474. std::cerr << "ggml_pipeline_cleanup(" << pipeline->name << ")" << std::endl;
  475. #endif
  476. pipeline->descriptor_set_idx = 0;
  477. }
  478. static vk::CommandBuffer ggml_vk_create_cmd_buffer(ggml_backend_vk_context * ctx, vk_queue& q) {
  479. #ifdef GGML_VULKAN_DEBUG
  480. std::cerr << "ggml_vk_create_cmd_buffer()" << std::endl;
  481. #endif
  482. if (q.cmd_buffers.size() > q.cmd_buffer_idx) {
  483. // Reuse command buffer
  484. return q.cmd_buffers[q.cmd_buffer_idx++];
  485. }
  486. vk::CommandBufferAllocateInfo command_buffer_alloc_info(
  487. q.pool,
  488. vk::CommandBufferLevel::ePrimary,
  489. 1);
  490. const std::vector<vk::CommandBuffer> cmd_buffers = ctx->device->device.allocateCommandBuffers(command_buffer_alloc_info);
  491. auto buf = cmd_buffers.front();
  492. q.cmd_buffers.push_back(buf);
  493. q.cmd_buffer_idx++;
  494. return buf;
  495. }
  496. static vk_submission ggml_vk_create_submission(ggml_backend_vk_context * ctx, vk_queue& q, std::vector<vk_semaphore> wait_semaphores, std::vector<vk_semaphore> signal_semaphores) {
  497. #ifdef GGML_VULKAN_DEBUG
  498. std::cerr << "ggml_vk_create_submission()" << std::endl;
  499. #endif
  500. vk_submission s;
  501. s.buffer = ggml_vk_create_cmd_buffer(ctx, q);
  502. s.wait_semaphores = std::move(wait_semaphores);
  503. s.signal_semaphores = std::move(signal_semaphores);
  504. return s;
  505. }
  506. static void ggml_vk_submit(vk_context * ctx, vk::Fence fence) {
  507. #ifdef GGML_VULKAN_DEBUG
  508. std::cerr << "ggml_vk_submit(" << ctx->seqs.size() << ", " << fence << ")" << std::endl;
  509. #endif
  510. if (ctx->seqs.empty()) {
  511. return;
  512. }
  513. std::vector<std::vector<uint64_t>> tl_wait_vals;
  514. std::vector<std::vector<uint64_t>> tl_signal_vals;
  515. std::vector<std::vector<vk::Semaphore>> tl_wait_semaphores;
  516. std::vector<std::vector<vk::Semaphore>> tl_signal_semaphores;
  517. std::vector<vk::TimelineSemaphoreSubmitInfo> tl_submit_infos;
  518. std::vector<vk::SubmitInfo> submit_infos;
  519. int idx = -1;
  520. std::vector<std::vector<vk::PipelineStageFlags>> stage_flags;
  521. size_t reserve = 0;
  522. for (const auto& sequence : ctx->seqs) {
  523. reserve += sequence.size();
  524. }
  525. // Pre-reserve vectors to prevent reallocation, which invalidates pointers
  526. tl_wait_semaphores.reserve(reserve);
  527. tl_wait_vals.reserve(reserve);
  528. tl_signal_semaphores.reserve(reserve);
  529. tl_signal_vals.reserve(reserve);
  530. tl_submit_infos.reserve(reserve);
  531. submit_infos.reserve(reserve);
  532. stage_flags.reserve(reserve);
  533. for (const auto& sequence : ctx->seqs) {
  534. for (const auto& submission : sequence) {
  535. stage_flags.push_back({});
  536. idx++;
  537. tl_wait_vals.push_back({});
  538. tl_wait_semaphores.push_back({});
  539. tl_signal_vals.push_back({});
  540. tl_signal_semaphores.push_back({});
  541. for (size_t i = 0; i < submission.wait_semaphores.size(); i++) {
  542. stage_flags[idx].push_back(ctx->q->stage_flags);
  543. tl_wait_vals[idx].push_back(submission.wait_semaphores[i].value);
  544. tl_wait_semaphores[idx].push_back(submission.wait_semaphores[i].s);
  545. }
  546. for (size_t i = 0; i < submission.signal_semaphores.size(); i++) {
  547. tl_signal_vals[idx].push_back(submission.signal_semaphores[i].value);
  548. tl_signal_semaphores[idx].push_back(submission.signal_semaphores[i].s);
  549. }
  550. tl_submit_infos.push_back({
  551. (uint32_t) submission.wait_semaphores.size(),
  552. tl_wait_vals[idx].data(),
  553. (uint32_t) submission.signal_semaphores.size(),
  554. tl_signal_vals[idx].data(),
  555. });
  556. tl_submit_infos[idx].sType = vk::StructureType::eTimelineSemaphoreSubmitInfo;
  557. tl_submit_infos[idx].pNext = nullptr;
  558. vk::SubmitInfo si{
  559. (uint32_t) submission.wait_semaphores.size(),
  560. tl_wait_semaphores[idx].data(),
  561. stage_flags[idx].data(),
  562. 1,
  563. &submission.buffer,
  564. (uint32_t) submission.signal_semaphores.size(),
  565. tl_signal_semaphores[idx].data(),
  566. };
  567. si.setPNext(&tl_submit_infos[idx]);
  568. submit_infos.push_back(si);
  569. }
  570. }
  571. ctx->q->queue.submit(submit_infos, fence);
  572. ctx->seqs.clear();
  573. }
  574. 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) {
  575. #ifdef GGML_VULKAN_DEBUG
  576. std::cerr << "ggml_vk_find_queue_family_index()" << std::endl;
  577. #endif
  578. const uint32_t qfsize = queue_family_props.size();
  579. // Try with avoid preferences first
  580. for (uint32_t i = 0; i < qfsize; i++) {
  581. 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)) {
  582. return i;
  583. }
  584. }
  585. // Fall back to only required
  586. for (size_t i = 0; i < qfsize; i++) {
  587. if (queue_family_props[i].queueCount >= min_num_queues && (compute_index < 0 || i != (uint32_t) compute_index) && queue_family_props[i].queueFlags & required) {
  588. return i;
  589. }
  590. }
  591. // Fall back to reusing compute queue
  592. for (size_t i = 0; i < qfsize; i++) {
  593. if (queue_family_props[i].queueCount >= min_num_queues && queue_family_props[i].queueFlags & required) {
  594. return i;
  595. }
  596. }
  597. // Fall back to ignoring min_num_queries
  598. for (size_t i = 0; i < qfsize; i++) {
  599. if (queue_family_props[i].queueFlags & required) {
  600. return i;
  601. }
  602. }
  603. // 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.
  604. // 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.
  605. if (compute_index >= 0) {
  606. return compute_index;
  607. }
  608. std::cerr << "ggml_vulkan: No suitable queue family index found." << std::endl;
  609. for(auto &q_family : queue_family_props) {
  610. std::cerr << "Queue number: " + std::to_string(q_family.queueCount) << " flags: " + to_string(q_family.queueFlags) << std::endl;
  611. }
  612. abort();
  613. }
  614. static void ggml_vk_create_queue(ggml_backend_vk_context * ctx, vk_queue& q, uint32_t queue_family_index, uint32_t queue_index, vk::PipelineStageFlags&& stage_flags) {
  615. #ifdef GGML_VULKAN_DEBUG
  616. std::cerr << "ggml_vk_create_queue()" << std::endl;
  617. #endif
  618. q.queue_family_index = queue_family_index;
  619. vk::CommandPoolCreateInfo command_pool_create_info_compute(vk::CommandPoolCreateFlags(VK_COMMAND_POOL_CREATE_TRANSIENT_BIT), queue_family_index);
  620. q.pool = ctx->device->device.createCommandPool(command_pool_create_info_compute);
  621. q.cmd_buffer_idx = 0;
  622. q.queue = ctx->device->device.getQueue(queue_family_index, queue_index);
  623. q.stage_flags = stage_flags;
  624. }
  625. static vk_context * ggml_vk_create_context(ggml_backend_vk_context * ctx, vk_queue& q) {
  626. #ifdef GGML_VULKAN_DEBUG
  627. std::cerr << "ggml_vk_create_context()" << std::endl;
  628. #endif
  629. ctx->gc.contexts.emplace_back();
  630. vk_context * result = &ctx->gc.contexts[ctx->gc.contexts.size() - 1];
  631. memset((void *) result, 0, sizeof(vk_context));
  632. result->idx = ctx->gc.contexts.size() - 1;
  633. result->q = &q;
  634. return result;
  635. }
  636. static vk_semaphore * ggml_vk_create_binary_semaphore(ggml_backend_vk_context * ctx) {
  637. #ifdef GGML_VULKAN_DEBUG
  638. std::cerr << "ggml_vk_create_timeline_semaphore()" << std::endl;
  639. #endif
  640. vk::SemaphoreTypeCreateInfo tci{ vk::SemaphoreType::eBinary, 0 };
  641. vk::SemaphoreCreateInfo ci{};
  642. ci.setPNext(&tci);
  643. vk::Semaphore semaphore = ctx->device->device.createSemaphore(ci);
  644. ctx->gc.semaphores.push_back({ semaphore, 0 });
  645. return &ctx->gc.semaphores[ctx->gc.semaphores.size() - 1];
  646. }
  647. static vk_semaphore * ggml_vk_create_timeline_semaphore(ggml_backend_vk_context * ctx) {
  648. #ifdef GGML_VULKAN_DEBUG
  649. std::cerr << "ggml_vk_create_timeline_semaphore()" << std::endl;
  650. #endif
  651. if (ctx->semaphore_idx >= ctx->gc.tl_semaphores.size()) {
  652. vk::SemaphoreTypeCreateInfo tci{ vk::SemaphoreType::eTimeline, 0 };
  653. vk::SemaphoreCreateInfo ci{};
  654. ci.setPNext(&tci);
  655. vk::Semaphore semaphore = ctx->device->device.createSemaphore(ci);
  656. ctx->gc.tl_semaphores.push_back({ semaphore, 0 });
  657. }
  658. return &ctx->gc.tl_semaphores[ctx->semaphore_idx++];
  659. }
  660. static vk::Event ggml_vk_create_event(ggml_backend_vk_context * ctx) {
  661. if (ctx->event_idx >= ctx->gc.events.size()) {
  662. ctx->gc.events.push_back(ctx->device->device.createEvent({}));
  663. }
  664. return ctx->gc.events[ctx->event_idx++];
  665. }
  666. static void ggml_vk_queue_cleanup(ggml_backend_vk_context * ctx, vk_queue& q) {
  667. #ifdef GGML_VULKAN_DEBUG
  668. std::cerr << "ggml_vk_queue_cleanup()" << std::endl;
  669. #endif
  670. // Requires command buffers to be done
  671. ctx->device->device.resetCommandPool(q.pool);
  672. q.cmd_buffer_idx = 0;
  673. }
  674. static uint32_t find_properties(const vk::PhysicalDeviceMemoryProperties* mem_props, vk::MemoryRequirements* mem_req, vk::MemoryPropertyFlags flags) {
  675. for (uint32_t i = 0; i < mem_props->memoryTypeCount; ++i) {
  676. vk::MemoryType memory_type = mem_props->memoryTypes[i];
  677. if ((mem_req->memoryTypeBits & ((uint64_t)1 << i)) &&
  678. (flags & memory_type.propertyFlags) == flags &&
  679. mem_props->memoryHeaps[memory_type.heapIndex].size >= mem_req->size) {
  680. return static_cast<int32_t>(i);
  681. }
  682. }
  683. return UINT32_MAX;
  684. }
  685. static vk_buffer ggml_vk_create_buffer(ggml_backend_vk_context * ctx, size_t size, vk::MemoryPropertyFlags req_flags, vk::MemoryPropertyFlags fallback_flags = vk::MemoryPropertyFlags(0)) {
  686. #ifdef GGML_VULKAN_DEBUG
  687. std::cerr << "ggml_vk_create_buffer(device " << ctx->idx << ", " << size << ", " << to_string(req_flags) << ", " << to_string(fallback_flags) << ")" << std::endl;
  688. #endif
  689. vk_buffer buf = std::make_shared<vk_buffer_struct>();
  690. if (size == 0) {
  691. buf->size = 0;
  692. return buf;
  693. }
  694. buf->size = size;
  695. vk::BufferCreateInfo buffer_create_info{
  696. vk::BufferCreateFlags(),
  697. size,
  698. vk::BufferUsageFlagBits::eStorageBuffer | vk::BufferUsageFlagBits::eTransferSrc | vk::BufferUsageFlagBits::eTransferDst,
  699. vk::SharingMode::eExclusive,
  700. 0,
  701. nullptr,
  702. };
  703. buf->buffer = ctx->device->device.createBuffer(buffer_create_info);
  704. vk::MemoryRequirements mem_req = ctx->device->device.getBufferMemoryRequirements(buf->buffer);
  705. vk::PhysicalDeviceMemoryProperties mem_props = ctx->device->physical_device.getMemoryProperties();
  706. uint32_t memory_type_index = UINT32_MAX;
  707. memory_type_index = find_properties(&mem_props, &mem_req, req_flags);
  708. buf->memory_property_flags = req_flags;
  709. if (memory_type_index == UINT32_MAX && fallback_flags) {
  710. memory_type_index = find_properties(&mem_props, &mem_req, fallback_flags);
  711. buf->memory_property_flags = fallback_flags;
  712. }
  713. if (memory_type_index == UINT32_MAX) {
  714. ctx->device->device.destroyBuffer(buf->buffer);
  715. buf->size = 0;
  716. throw vk::OutOfDeviceMemoryError("No suitable memory type found");
  717. }
  718. try {
  719. buf->device_memory = ctx->device->device.allocateMemory({ mem_req.size, memory_type_index });
  720. } catch (const vk::SystemError& e) {
  721. // Out of Host/Device memory, clean up buffer
  722. ctx->device->device.destroyBuffer(buf->buffer);
  723. buf->size = 0;
  724. throw e;
  725. }
  726. buf->ptr = nullptr;
  727. if (buf->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  728. buf->ptr = ctx->device->device.mapMemory(buf->device_memory, 0, VK_WHOLE_SIZE);
  729. }
  730. ctx->device->device.bindBufferMemory(buf->buffer, buf->device_memory, 0);
  731. buf->ctx = ctx;
  732. buf->device = ctx->device;
  733. #ifdef GGML_VULKAN_DEBUG
  734. std::cerr << "Created buffer " << buf->buffer << std::endl;
  735. #endif
  736. return buf;
  737. }
  738. static vk_buffer ggml_vk_create_buffer_check(ggml_backend_vk_context * ctx, size_t size, vk::MemoryPropertyFlags req_flags, vk::MemoryPropertyFlags fallback_flags = vk::MemoryPropertyFlags(0)) {
  739. try {
  740. return ggml_vk_create_buffer(ctx, size, req_flags, fallback_flags);
  741. } catch (const vk::SystemError& e) {
  742. std::cerr << "ggml_vulkan: Memory allocation of size " << size << " failed." << std::endl;
  743. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  744. throw e;
  745. }
  746. }
  747. static vk_buffer ggml_vk_create_buffer_device(ggml_backend_vk_context * ctx, size_t size) {
  748. vk_buffer buf;
  749. try {
  750. if (ctx->device->uma) {
  751. // Fall back to host memory type
  752. buf = ggml_vk_create_buffer(ctx, size, vk::MemoryPropertyFlagBits::eDeviceLocal, vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
  753. } else {
  754. buf = ggml_vk_create_buffer(ctx, size, vk::MemoryPropertyFlagBits::eDeviceLocal);
  755. }
  756. } catch (const vk::SystemError& e) {
  757. std::cerr << "ggml_vulkan: Device memory allocation of size " << size << " failed." << std::endl;
  758. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  759. throw e;
  760. }
  761. return buf;
  762. }
  763. static void ggml_vk_destroy_buffer(vk_buffer& buf) {
  764. buf.reset();
  765. }
  766. static vk_subbuffer ggml_vk_subbuffer(vk_buffer& buf) {
  767. return { buf, 0, VK_WHOLE_SIZE };
  768. }
  769. static void ggml_vk_sync_buffers(vk_context * ctx) {
  770. #ifdef GGML_VULKAN_DEBUG
  771. std::cerr << "ggml_vk_sync_buffers()" << std::endl;
  772. #endif
  773. const std::vector<vk::MemoryBarrier> mem_barriers{ { { vk::AccessFlagBits::eMemoryRead | vk::AccessFlagBits::eMemoryWrite }, { vk::AccessFlagBits::eMemoryRead | vk::AccessFlagBits::eMemoryWrite } } };
  774. ctx->s->buffer.pipelineBarrier(
  775. ctx->q->stage_flags,
  776. ctx->q->stage_flags,
  777. {},
  778. mem_barriers,
  779. {},
  780. {}
  781. );
  782. }
  783. static void ggml_vk_wait_events(vk_context * ctx, std::vector<vk::Event>&& events) {
  784. #ifdef GGML_VULKAN_DEBUG
  785. std::cerr << "ggml_vk_wait_events()" << std::endl;
  786. #endif
  787. if (events.empty()) {
  788. return;
  789. }
  790. ctx->s->buffer.waitEvents(
  791. events,
  792. ctx->q->stage_flags,
  793. ctx->q->stage_flags,
  794. {},
  795. {},
  796. {}
  797. );
  798. }
  799. static bool ggml_vk_build_shader(ggml_type type) {
  800. switch(type) {
  801. case GGML_TYPE_F16:
  802. case GGML_TYPE_Q4_0:
  803. case GGML_TYPE_Q4_1:
  804. case GGML_TYPE_Q5_0:
  805. case GGML_TYPE_Q5_1:
  806. case GGML_TYPE_Q8_0:
  807. case GGML_TYPE_Q2_K:
  808. case GGML_TYPE_Q3_K:
  809. case GGML_TYPE_Q4_K:
  810. case GGML_TYPE_Q5_K:
  811. case GGML_TYPE_Q6_K:
  812. return true;
  813. default:
  814. return false;
  815. }
  816. }
  817. static void ggml_vk_load_shaders(ggml_backend_vk_context * ctx) {
  818. #ifdef GGML_VULKAN_DEBUG
  819. std::cerr << "ggml_vk_load_shaders(" << ctx->name << ")" << std::endl;
  820. #endif
  821. const std::shared_ptr<vk_device> device = ctx->device;
  822. // mulmat
  823. std::initializer_list<uint32_t> warptile_l = { 128, 128, 128, 16, device->subgroup_size * 2, 64, 2, 4, 4, device->subgroup_size };
  824. std::initializer_list<uint32_t> warptile_m = { 128, 64, 64, 16, device->subgroup_size, 32, 2, 4, 2, device->subgroup_size };
  825. std::initializer_list<uint32_t> warptile_s = { device->subgroup_size, 32, 32, 16, 32, 32, 2, 2, 2, device->subgroup_size };
  826. std::initializer_list<uint32_t> warptile_mmq_l = { 128, 128, 128, 32, device->subgroup_size * 2, 64, 2, 4, 4, device->subgroup_size };
  827. std::initializer_list<uint32_t> warptile_mmq_m = { 128, 64, 64, 32, device->subgroup_size, 32, 2, 4, 2, device->subgroup_size };
  828. std::initializer_list<uint32_t> warptile_mmq_s = { device->subgroup_size, 32, 32, 32, 32, 32, 2, 2, 2, device->subgroup_size };
  829. std::array<uint32_t, 3> l_wg_denoms = {128, 128, 1 };
  830. std::array<uint32_t, 3> m_wg_denoms = { 64, 64, 1 };
  831. std::array<uint32_t, 3> s_wg_denoms = { 32, 32, 1 };
  832. uint32_t l_align = 128;
  833. uint32_t m_align = 64;
  834. uint32_t s_align = 32;
  835. ctx->device->pipeline_matmul_f32 = std::make_shared<vk_matmul_pipeline_struct>();
  836. ctx->device->pipeline_matmul_f32_f16 = std::make_shared<vk_matmul_pipeline_struct>();
  837. ctx->device->pipeline_matmul_f16_f32 = std::make_shared<vk_matmul_pipeline_struct>();
  838. ctx->device->pipeline_matmul_f16 = std::make_shared<vk_matmul_pipeline_struct>();
  839. ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0] = std::make_shared<vk_matmul_pipeline_struct>();
  840. ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1] = std::make_shared<vk_matmul_pipeline_struct>();
  841. ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0] = std::make_shared<vk_matmul_pipeline_struct>();
  842. ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1] = std::make_shared<vk_matmul_pipeline_struct>();
  843. ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0] = std::make_shared<vk_matmul_pipeline_struct>();
  844. ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K] = std::make_shared<vk_matmul_pipeline_struct>();
  845. ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K] = std::make_shared<vk_matmul_pipeline_struct>();
  846. ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K] = std::make_shared<vk_matmul_pipeline_struct>();
  847. ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K] = std::make_shared<vk_matmul_pipeline_struct>();
  848. ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K] = std::make_shared<vk_matmul_pipeline_struct>();
  849. ctx->device->pipeline_matmul_id_f32 = std::make_shared<vk_matmul_pipeline_struct>();
  850. ctx->device->pipeline_matmul_id_f16_f32 = std::make_shared<vk_matmul_pipeline_struct>();
  851. ctx->device->pipeline_matmul_id_f16 = std::make_shared<vk_matmul_pipeline_struct>();
  852. ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0] = std::make_shared<vk_matmul_pipeline_struct>();
  853. ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1] = std::make_shared<vk_matmul_pipeline_struct>();
  854. ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0] = std::make_shared<vk_matmul_pipeline_struct>();
  855. ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1] = std::make_shared<vk_matmul_pipeline_struct>();
  856. ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0] = std::make_shared<vk_matmul_pipeline_struct>();
  857. ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K] = std::make_shared<vk_matmul_pipeline_struct>();
  858. ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K] = std::make_shared<vk_matmul_pipeline_struct>();
  859. ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K] = std::make_shared<vk_matmul_pipeline_struct>();
  860. ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K] = std::make_shared<vk_matmul_pipeline_struct>();
  861. ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K] = std::make_shared<vk_matmul_pipeline_struct>();
  862. if (device->fp16) {
  863. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f32->l, "matmul_f32_l", matmul_f32_len, matmul_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_l, 1);
  864. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f32->m, "matmul_f32_m", matmul_f32_len, matmul_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_m, 1);
  865. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f32->s, "matmul_f32_s", matmul_f32_len, matmul_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_s, 1);
  866. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f32->a_l, "matmul_f32_aligned_l", matmul_f32_aligned_len, matmul_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_l, l_align);
  867. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f32->a_m, "matmul_f32_aligned_m", matmul_f32_aligned_len, matmul_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_m, m_align);
  868. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f32->a_s, "matmul_f32_aligned_s", matmul_f32_aligned_len, matmul_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_s, s_align);
  869. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f32_f16->l, "matmul_f32_f16_l", matmul_f32_f16_len, matmul_f32_f16_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_l, 1);
  870. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f32_f16->m, "matmul_f32_f16_m", matmul_f32_f16_len, matmul_f32_f16_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_m, 1);
  871. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f32_f16->s, "matmul_f32_f16_s", matmul_f32_f16_len, matmul_f32_f16_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_s, 1);
  872. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f32_f16->a_l, "matmul_f32_f16_aligned_l", matmul_f32_f16_aligned_len, matmul_f32_f16_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_l, l_align);
  873. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f32_f16->a_m, "matmul_f32_f16_aligned_m", matmul_f32_f16_aligned_len, matmul_f32_f16_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_m, m_align);
  874. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f32_f16->a_s, "matmul_f32_f16_aligned_s", matmul_f32_f16_aligned_len, matmul_f32_f16_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_s, s_align);
  875. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f16->l, "matmul_f16_l", matmul_f16_len, matmul_f16_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_l, 1);
  876. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f16->m, "matmul_f16_m", matmul_f16_len, matmul_f16_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_m, 1);
  877. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f16->s, "matmul_f16_s", matmul_f16_len, matmul_f16_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_s, 1);
  878. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f16->a_l, "matmul_f16_aligned_l", matmul_f16_aligned_len, matmul_f16_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_l, l_align);
  879. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f16->a_m, "matmul_f16_aligned_m", matmul_f16_aligned_len, matmul_f16_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_m, m_align);
  880. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f16->a_s, "matmul_f16_aligned_s", matmul_f16_aligned_len, matmul_f16_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_s, s_align);
  881. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f16_f32->l, "matmul_f16_f32_l", matmul_f16_f32_len, matmul_f16_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_l, 1);
  882. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f16_f32->m, "matmul_f16_f32_m", matmul_f16_f32_len, matmul_f16_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_m, 1);
  883. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f16_f32->s, "matmul_f16_f32_s", matmul_f16_f32_len, matmul_f16_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_s, 1);
  884. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f16_f32->a_l, "matmul_f16_f32_aligned_l", matmul_f16_f32_aligned_len, matmul_f16_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_l, l_align);
  885. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f16_f32->a_m, "matmul_f16_f32_aligned_m", matmul_f16_f32_aligned_len, matmul_f16_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_m, m_align);
  886. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f16_f32->a_s, "matmul_f16_f32_aligned_s", matmul_f16_f32_aligned_len, matmul_f16_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_s, s_align);
  887. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0]->l, "matmul_q4_0_f32_l", matmul_q4_0_f32_len, matmul_q4_0_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  888. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0]->m, "matmul_q4_0_f32_m", matmul_q4_0_f32_len, matmul_q4_0_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  889. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0]->s, "matmul_q4_0_f32_s", matmul_q4_0_f32_len, matmul_q4_0_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  890. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0]->a_l, "matmul_q4_0_f32_aligned_l", matmul_q4_0_f32_aligned_len, matmul_q4_0_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  891. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0]->a_m, "matmul_q4_0_f32_aligned_m", matmul_q4_0_f32_aligned_len, matmul_q4_0_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  892. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0]->a_s, "matmul_q4_0_f32_aligned_s", matmul_q4_0_f32_aligned_len, matmul_q4_0_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  893. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1]->l, "matmul_q4_1_f32_l", matmul_q4_1_f32_len, matmul_q4_1_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  894. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1]->m, "matmul_q4_1_f32_m", matmul_q4_1_f32_len, matmul_q4_1_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  895. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1]->s, "matmul_q4_1_f32_s", matmul_q4_1_f32_len, matmul_q4_1_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  896. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1]->a_l, "matmul_q4_1_f32_aligned_l", matmul_q4_1_f32_aligned_len, matmul_q4_1_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  897. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1]->a_m, "matmul_q4_1_f32_aligned_m", matmul_q4_1_f32_aligned_len, matmul_q4_1_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  898. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1]->a_s, "matmul_q4_1_f32_aligned_s", matmul_q4_1_f32_aligned_len, matmul_q4_1_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  899. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0]->l, "matmul_q5_0_f32_l", matmul_q5_0_f32_len, matmul_q5_0_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  900. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0]->m, "matmul_q5_0_f32_m", matmul_q5_0_f32_len, matmul_q5_0_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  901. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0]->s, "matmul_q5_0_f32_s", matmul_q5_0_f32_len, matmul_q5_0_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  902. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0]->a_l, "matmul_q5_0_f32_aligned_l", matmul_q5_0_f32_aligned_len, matmul_q5_0_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  903. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0]->a_m, "matmul_q5_0_f32_aligned_m", matmul_q5_0_f32_aligned_len, matmul_q5_0_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  904. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0]->a_s, "matmul_q5_0_f32_aligned_s", matmul_q5_0_f32_aligned_len, matmul_q5_0_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  905. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1]->l, "matmul_q5_1_f32_l", matmul_q5_1_f32_len, matmul_q5_1_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  906. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1]->m, "matmul_q5_1_f32_m", matmul_q5_1_f32_len, matmul_q5_1_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  907. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1]->s, "matmul_q5_1_f32_s", matmul_q5_1_f32_len, matmul_q5_1_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  908. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1]->a_l, "matmul_q5_1_f32_aligned_l", matmul_q5_1_f32_aligned_len, matmul_q5_1_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  909. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1]->a_m, "matmul_q5_1_f32_aligned_m", matmul_q5_1_f32_aligned_len, matmul_q5_1_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  910. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1]->a_s, "matmul_q5_1_f32_aligned_s", matmul_q5_1_f32_aligned_len, matmul_q5_1_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  911. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0]->l, "matmul_q8_0_f32_l", matmul_q8_0_f32_len, matmul_q8_0_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  912. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0]->m, "matmul_q8_0_f32_m", matmul_q8_0_f32_len, matmul_q8_0_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  913. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0]->s, "matmul_q8_0_f32_s", matmul_q8_0_f32_len, matmul_q8_0_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  914. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0]->a_l, "matmul_q8_0_f32_aligned_l", matmul_q8_0_f32_aligned_len, matmul_q8_0_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  915. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0]->a_m, "matmul_q8_0_f32_aligned_m", matmul_q8_0_f32_aligned_len, matmul_q8_0_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  916. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0]->a_s, "matmul_q8_0_f32_aligned_s", matmul_q8_0_f32_aligned_len, matmul_q8_0_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  917. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K]->l, "matmul_q2_k_f32_l", matmul_q2_k_f32_len, matmul_q2_k_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  918. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K]->m, "matmul_q2_k_f32_m", matmul_q2_k_f32_len, matmul_q2_k_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  919. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K]->s, "matmul_q2_k_f32_s", matmul_q2_k_f32_len, matmul_q2_k_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  920. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K]->a_l, "matmul_q2_k_f32_aligned_l", matmul_q2_k_f32_aligned_len, matmul_q2_k_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  921. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K]->a_m, "matmul_q2_k_f32_aligned_m", matmul_q2_k_f32_aligned_len, matmul_q2_k_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  922. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K]->a_s, "matmul_q2_k_f32_aligned_s", matmul_q2_k_f32_aligned_len, matmul_q2_k_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  923. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K]->l, "matmul_q3_k_f32_l", matmul_q3_k_f32_len, matmul_q3_k_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  924. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K]->m, "matmul_q3_k_f32_m", matmul_q3_k_f32_len, matmul_q3_k_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  925. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K]->s, "matmul_q3_k_f32_s", matmul_q3_k_f32_len, matmul_q3_k_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  926. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K]->a_l, "matmul_q3_k_f32_aligned_l", matmul_q3_k_f32_aligned_len, matmul_q3_k_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  927. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K]->a_m, "matmul_q3_k_f32_aligned_m", matmul_q3_k_f32_aligned_len, matmul_q3_k_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  928. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K]->a_s, "matmul_q3_k_f32_aligned_s", matmul_q3_k_f32_aligned_len, matmul_q3_k_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  929. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K]->l, "matmul_q4_k_f32_l", matmul_q4_k_f32_len, matmul_q4_k_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  930. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K]->m, "matmul_q4_k_f32_m", matmul_q4_k_f32_len, matmul_q4_k_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  931. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K]->s, "matmul_q4_k_f32_s", matmul_q4_k_f32_len, matmul_q4_k_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  932. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K]->a_l, "matmul_q4_k_f32_aligned_l", matmul_q4_k_f32_aligned_len, matmul_q4_k_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  933. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K]->a_m, "matmul_q4_k_f32_aligned_m", matmul_q4_k_f32_aligned_len, matmul_q4_k_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  934. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K]->a_s, "matmul_q4_k_f32_aligned_s", matmul_q4_k_f32_aligned_len, matmul_q4_k_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  935. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K]->l, "matmul_q5_k_f32_l", matmul_q5_k_f32_len, matmul_q5_k_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  936. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K]->m, "matmul_q5_k_f32_m", matmul_q5_k_f32_len, matmul_q5_k_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  937. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K]->s, "matmul_q5_k_f32_s", matmul_q5_k_f32_len, matmul_q5_k_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  938. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K]->a_l, "matmul_q5_k_f32_aligned_l", matmul_q5_k_f32_aligned_len, matmul_q5_k_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  939. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K]->a_m, "matmul_q5_k_f32_aligned_m", matmul_q5_k_f32_aligned_len, matmul_q5_k_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  940. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K]->a_s, "matmul_q5_k_f32_aligned_s", matmul_q5_k_f32_aligned_len, matmul_q5_k_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  941. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K]->l, "matmul_q6_k_f32_l", matmul_q6_k_f32_len, matmul_q6_k_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  942. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K]->m, "matmul_q6_k_f32_m", matmul_q6_k_f32_len, matmul_q6_k_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  943. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K]->s, "matmul_q6_k_f32_s", matmul_q6_k_f32_len, matmul_q6_k_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  944. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K]->a_l, "matmul_q6_k_f32_aligned_l", matmul_q6_k_f32_aligned_len, matmul_q6_k_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  945. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K]->a_m, "matmul_q6_k_f32_aligned_m", matmul_q6_k_f32_aligned_len, matmul_q6_k_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  946. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K]->a_s, "matmul_q6_k_f32_aligned_s", matmul_q6_k_f32_aligned_len, matmul_q6_k_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  947. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f32->l, "matmul_id_f32_l", matmul_id_f32_len, matmul_id_f32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_l, 1);
  948. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f32->m, "matmul_id_f32_m", matmul_id_f32_len, matmul_id_f32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_m, 1);
  949. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f32->s, "matmul_id_f32_s", matmul_id_f32_len, matmul_id_f32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_s, 1);
  950. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f32->a_l, "matmul_id_f32_aligned_l", matmul_id_f32_aligned_len, matmul_id_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_l, l_align);
  951. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f32->a_m, "matmul_id_f32_aligned_m", matmul_id_f32_aligned_len, matmul_id_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_m, m_align);
  952. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f32->a_s, "matmul_id_f32_aligned_s", matmul_id_f32_aligned_len, matmul_id_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_s, s_align);
  953. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f16->l, "matmul_id_f16_l", matmul_id_f16_len, matmul_id_f16_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_l, 1);
  954. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f16->m, "matmul_id_f16_m", matmul_id_f16_len, matmul_id_f16_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_m, 1);
  955. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f16->s, "matmul_id_f16_s", matmul_id_f16_len, matmul_id_f16_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_s, 1);
  956. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f16->a_l, "matmul_id_f16_aligned_l", matmul_id_f16_aligned_len, matmul_id_f16_aligned_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_l, l_align);
  957. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f16->a_m, "matmul_id_f16_aligned_m", matmul_id_f16_aligned_len, matmul_id_f16_aligned_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_m, m_align);
  958. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f16->a_s, "matmul_id_f16_aligned_s", matmul_id_f16_aligned_len, matmul_id_f16_aligned_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_s, s_align);
  959. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f16_f32->l, "matmul_id_f16_f32_l", matmul_id_f16_f32_len, matmul_id_f16_f32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_l, 1);
  960. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f16_f32->m, "matmul_id_f16_f32_m", matmul_id_f16_f32_len, matmul_id_f16_f32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_m, 1);
  961. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f16_f32->s, "matmul_id_f16_f32_s", matmul_id_f16_f32_len, matmul_id_f16_f32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_s, 1);
  962. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f16_f32->a_l, "matmul_id_f16_f32_aligned_l", matmul_id_f16_f32_aligned_len, matmul_id_f16_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_l, l_align);
  963. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f16_f32->a_m, "matmul_id_f16_f32_aligned_m", matmul_id_f16_f32_aligned_len, matmul_id_f16_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_m, m_align);
  964. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f16_f32->a_s, "matmul_id_f16_f32_aligned_s", matmul_id_f16_f32_aligned_len, matmul_id_f16_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_s, s_align);
  965. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0]->l, "matmul_id_q4_0_f32_l", matmul_id_q4_0_f32_len, matmul_id_q4_0_f32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  966. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0]->m, "matmul_id_q4_0_f32_m", matmul_id_q4_0_f32_len, matmul_id_q4_0_f32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  967. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0]->s, "matmul_id_q4_0_f32_s", matmul_id_q4_0_f32_len, matmul_id_q4_0_f32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  968. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0]->a_l, "matmul_id_q4_0_f32_aligned_l", matmul_id_q4_0_f32_aligned_len, matmul_id_q4_0_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  969. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0]->a_m, "matmul_id_q4_0_f32_aligned_m", matmul_id_q4_0_f32_aligned_len, matmul_id_q4_0_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  970. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0]->a_s, "matmul_id_q4_0_f32_aligned_s", matmul_id_q4_0_f32_aligned_len, matmul_id_q4_0_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  971. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1]->l, "matmul_id_q4_1_f32_l", matmul_id_q4_1_f32_len, matmul_id_q4_1_f32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  972. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1]->m, "matmul_id_q4_1_f32_m", matmul_id_q4_1_f32_len, matmul_id_q4_1_f32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  973. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1]->s, "matmul_id_q4_1_f32_s", matmul_id_q4_1_f32_len, matmul_id_q4_1_f32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  974. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1]->a_l, "matmul_id_q4_1_f32_aligned_l", matmul_id_q4_1_f32_aligned_len, matmul_id_q4_1_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  975. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1]->a_m, "matmul_id_q4_1_f32_aligned_m", matmul_id_q4_1_f32_aligned_len, matmul_id_q4_1_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  976. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1]->a_s, "matmul_id_q4_1_f32_aligned_s", matmul_id_q4_1_f32_aligned_len, matmul_id_q4_1_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  977. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0]->l, "matmul_id_q5_0_f32_l", matmul_id_q5_0_f32_len, matmul_id_q5_0_f32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  978. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0]->m, "matmul_id_q5_0_f32_m", matmul_id_q5_0_f32_len, matmul_id_q5_0_f32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  979. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0]->s, "matmul_id_q5_0_f32_s", matmul_id_q5_0_f32_len, matmul_id_q5_0_f32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  980. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0]->a_l, "matmul_id_q5_0_f32_aligned_l", matmul_id_q5_0_f32_aligned_len, matmul_id_q5_0_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  981. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0]->a_m, "matmul_id_q5_0_f32_aligned_m", matmul_id_q5_0_f32_aligned_len, matmul_id_q5_0_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  982. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0]->a_s, "matmul_id_q5_0_f32_aligned_s", matmul_id_q5_0_f32_aligned_len, matmul_id_q5_0_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  983. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1]->l, "matmul_id_q5_1_f32_l", matmul_id_q5_1_f32_len, matmul_id_q5_1_f32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  984. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1]->m, "matmul_id_q5_1_f32_m", matmul_id_q5_1_f32_len, matmul_id_q5_1_f32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  985. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1]->s, "matmul_id_q5_1_f32_s", matmul_id_q5_1_f32_len, matmul_id_q5_1_f32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  986. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1]->a_l, "matmul_id_q5_1_f32_aligned_l", matmul_id_q5_1_f32_aligned_len, matmul_id_q5_1_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  987. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1]->a_m, "matmul_id_q5_1_f32_aligned_m", matmul_id_q5_1_f32_aligned_len, matmul_id_q5_1_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  988. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1]->a_s, "matmul_id_q5_1_f32_aligned_s", matmul_id_q5_1_f32_aligned_len, matmul_id_q5_1_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  989. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0]->l, "matmul_id_q8_0_f32_l", matmul_id_q8_0_f32_len, matmul_id_q8_0_f32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  990. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0]->m, "matmul_id_q8_0_f32_m", matmul_id_q8_0_f32_len, matmul_id_q8_0_f32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  991. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0]->s, "matmul_id_q8_0_f32_s", matmul_id_q8_0_f32_len, matmul_id_q8_0_f32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  992. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0]->a_l, "matmul_id_q8_0_f32_aligned_l", matmul_id_q8_0_f32_aligned_len, matmul_id_q8_0_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  993. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0]->a_m, "matmul_id_q8_0_f32_aligned_m", matmul_id_q8_0_f32_aligned_len, matmul_id_q8_0_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  994. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0]->a_s, "matmul_id_q8_0_f32_aligned_s", matmul_id_q8_0_f32_aligned_len, matmul_id_q8_0_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  995. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K]->l, "matmul_id_q2_k_f32_l", matmul_id_q2_k_f32_len, matmul_id_q2_k_f32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  996. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K]->m, "matmul_id_q2_k_f32_m", matmul_id_q2_k_f32_len, matmul_id_q2_k_f32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  997. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K]->s, "matmul_id_q2_k_f32_s", matmul_id_q2_k_f32_len, matmul_id_q2_k_f32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  998. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K]->a_l, "matmul_id_q2_k_f32_aligned_l", matmul_id_q2_k_f32_aligned_len, matmul_id_q2_k_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  999. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K]->a_m, "matmul_id_q2_k_f32_aligned_m", matmul_id_q2_k_f32_aligned_len, matmul_id_q2_k_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1000. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K]->a_s, "matmul_id_q2_k_f32_aligned_s", matmul_id_q2_k_f32_aligned_len, matmul_id_q2_k_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1001. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K]->l, "matmul_id_q3_k_f32_l", matmul_id_q3_k_f32_len, matmul_id_q3_k_f32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1002. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K]->m, "matmul_id_q3_k_f32_m", matmul_id_q3_k_f32_len, matmul_id_q3_k_f32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1003. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K]->s, "matmul_id_q3_k_f32_s", matmul_id_q3_k_f32_len, matmul_id_q3_k_f32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1004. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K]->a_l, "matmul_id_q3_k_f32_aligned_l", matmul_id_q3_k_f32_aligned_len, matmul_id_q3_k_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1005. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K]->a_m, "matmul_id_q3_k_f32_aligned_m", matmul_id_q3_k_f32_aligned_len, matmul_id_q3_k_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1006. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K]->a_s, "matmul_id_q3_k_f32_aligned_s", matmul_id_q3_k_f32_aligned_len, matmul_id_q3_k_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1007. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K]->l, "matmul_id_q4_k_f32_l", matmul_id_q4_k_f32_len, matmul_id_q4_k_f32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1008. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K]->m, "matmul_id_q4_k_f32_m", matmul_id_q4_k_f32_len, matmul_id_q4_k_f32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1009. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K]->s, "matmul_id_q4_k_f32_s", matmul_id_q4_k_f32_len, matmul_id_q4_k_f32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1010. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K]->a_l, "matmul_id_q4_k_f32_aligned_l", matmul_id_q4_k_f32_aligned_len, matmul_id_q4_k_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1011. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K]->a_m, "matmul_id_q4_k_f32_aligned_m", matmul_id_q4_k_f32_aligned_len, matmul_id_q4_k_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1012. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K]->a_s, "matmul_id_q4_k_f32_aligned_s", matmul_id_q4_k_f32_aligned_len, matmul_id_q4_k_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1013. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K]->l, "matmul_id_q5_k_f32_l", matmul_id_q5_k_f32_len, matmul_id_q5_k_f32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1014. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K]->m, "matmul_id_q5_k_f32_m", matmul_id_q5_k_f32_len, matmul_id_q5_k_f32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1015. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K]->s, "matmul_id_q5_k_f32_s", matmul_id_q5_k_f32_len, matmul_id_q5_k_f32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1016. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K]->a_l, "matmul_id_q5_k_f32_aligned_l", matmul_id_q5_k_f32_aligned_len, matmul_id_q5_k_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1017. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K]->a_m, "matmul_id_q5_k_f32_aligned_m", matmul_id_q5_k_f32_aligned_len, matmul_id_q5_k_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1018. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K]->a_s, "matmul_id_q5_k_f32_aligned_s", matmul_id_q5_k_f32_aligned_len, matmul_id_q5_k_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1019. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K]->l, "matmul_id_q6_k_f32_l", matmul_id_q6_k_f32_len, matmul_id_q6_k_f32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1020. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K]->m, "matmul_id_q6_k_f32_m", matmul_id_q6_k_f32_len, matmul_id_q6_k_f32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1021. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K]->s, "matmul_id_q6_k_f32_s", matmul_id_q6_k_f32_len, matmul_id_q6_k_f32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1022. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K]->a_l, "matmul_id_q6_k_f32_aligned_l", matmul_id_q6_k_f32_aligned_len, matmul_id_q6_k_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1023. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K]->a_m, "matmul_id_q6_k_f32_aligned_m", matmul_id_q6_k_f32_aligned_len, matmul_id_q6_k_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1024. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K]->a_s, "matmul_id_q6_k_f32_aligned_s", matmul_id_q6_k_f32_aligned_len, matmul_id_q6_k_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1025. } else {
  1026. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f32->l, "matmul_f32_l", matmul_f32_fp32_len, matmul_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_l, 1);
  1027. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f32->m, "matmul_f32_m", matmul_f32_fp32_len, matmul_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_m, 1);
  1028. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f32->s, "matmul_f32_s", matmul_f32_fp32_len, matmul_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_s, 1);
  1029. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f32->a_l, "matmul_f32_aligned_l", matmul_f32_aligned_fp32_len, matmul_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_l, l_align);
  1030. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f32->a_m, "matmul_f32_aligned_m", matmul_f32_aligned_fp32_len, matmul_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_m, m_align);
  1031. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f32->a_s, "matmul_f32_aligned_s", matmul_f32_aligned_fp32_len, matmul_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_s, s_align);
  1032. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f32_f16->l, "matmul_f32_f16_l", matmul_f32_f16_fp32_len, matmul_f32_f16_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_l, 1);
  1033. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f32_f16->m, "matmul_f32_f16_m", matmul_f32_f16_fp32_len, matmul_f32_f16_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_m, 1);
  1034. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f32_f16->s, "matmul_f32_f16_s", matmul_f32_f16_fp32_len, matmul_f32_f16_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_s, 1);
  1035. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f32_f16->a_l, "matmul_f32_f16_aligned_l", matmul_f32_f16_aligned_fp32_len, matmul_f32_f16_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_l, l_align);
  1036. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f32_f16->a_m, "matmul_f32_f16_aligned_m", matmul_f32_f16_aligned_fp32_len, matmul_f32_f16_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_m, m_align);
  1037. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f32_f16->a_s, "matmul_f32_f16_aligned_s", matmul_f32_f16_aligned_fp32_len, matmul_f32_f16_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_s, s_align);
  1038. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f16->l, "matmul_f16_l", matmul_f16_fp32_len, matmul_f16_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_l, 1);
  1039. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f16->m, "matmul_f16_m", matmul_f16_fp32_len, matmul_f16_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_m, 1);
  1040. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f16->s, "matmul_f16_s", matmul_f16_fp32_len, matmul_f16_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_s, 1);
  1041. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f16->a_l, "matmul_f16_aligned_l", matmul_f16_aligned_fp32_len, matmul_f16_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_l, l_align);
  1042. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f16->a_m, "matmul_f16_aligned_m", matmul_f16_aligned_fp32_len, matmul_f16_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_m, m_align);
  1043. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f16->a_s, "matmul_f16_aligned_s", matmul_f16_aligned_fp32_len, matmul_f16_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_s, s_align);
  1044. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f16_f32->l, "matmul_f16_f32_l", matmul_f16_f32_fp32_len, matmul_f16_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_l, 1);
  1045. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f16_f32->m, "matmul_f16_f32_m", matmul_f16_f32_fp32_len, matmul_f16_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_m, 1);
  1046. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f16_f32->s, "matmul_f16_f32_s", matmul_f16_f32_fp32_len, matmul_f16_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_s, 1);
  1047. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f16_f32->a_l, "matmul_f16_f32_aligned_l", matmul_f16_f32_aligned_fp32_len, matmul_f16_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_l, l_align);
  1048. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f16_f32->a_m, "matmul_f16_f32_aligned_m", matmul_f16_f32_aligned_fp32_len, matmul_f16_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_m, m_align);
  1049. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f16_f32->a_s, "matmul_f16_f32_aligned_s", matmul_f16_f32_aligned_fp32_len, matmul_f16_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_s, s_align);
  1050. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0]->l, "matmul_q4_0_f32_l", matmul_q4_0_f32_fp32_len, matmul_q4_0_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1051. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0]->m, "matmul_q4_0_f32_m", matmul_q4_0_f32_fp32_len, matmul_q4_0_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1052. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0]->s, "matmul_q4_0_f32_s", matmul_q4_0_f32_fp32_len, matmul_q4_0_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1053. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0]->a_l, "matmul_q4_0_f32_aligned_l", matmul_q4_0_f32_aligned_fp32_len, matmul_q4_0_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1054. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0]->a_m, "matmul_q4_0_f32_aligned_m", matmul_q4_0_f32_aligned_fp32_len, matmul_q4_0_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1055. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0]->a_s, "matmul_q4_0_f32_aligned_s", matmul_q4_0_f32_aligned_fp32_len, matmul_q4_0_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1056. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1]->l, "matmul_q4_1_f32_l", matmul_q4_1_f32_fp32_len, matmul_q4_1_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1057. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1]->m, "matmul_q4_1_f32_m", matmul_q4_1_f32_fp32_len, matmul_q4_1_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1058. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1]->s, "matmul_q4_1_f32_s", matmul_q4_1_f32_fp32_len, matmul_q4_1_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1059. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1]->a_l, "matmul_q4_1_f32_aligned_l", matmul_q4_1_f32_aligned_fp32_len, matmul_q4_1_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1060. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1]->a_m, "matmul_q4_1_f32_aligned_m", matmul_q4_1_f32_aligned_fp32_len, matmul_q4_1_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1061. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1]->a_s, "matmul_q4_1_f32_aligned_s", matmul_q4_1_f32_aligned_fp32_len, matmul_q4_1_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1062. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0]->l, "matmul_q5_0_f32_l", matmul_q5_0_f32_fp32_len, matmul_q5_0_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1063. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0]->m, "matmul_q5_0_f32_m", matmul_q5_0_f32_fp32_len, matmul_q5_0_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1064. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0]->s, "matmul_q5_0_f32_s", matmul_q5_0_f32_fp32_len, matmul_q5_0_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1065. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0]->a_l, "matmul_q5_0_f32_aligned_l", matmul_q5_0_f32_aligned_fp32_len, matmul_q5_0_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1066. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0]->a_m, "matmul_q5_0_f32_aligned_m", matmul_q5_0_f32_aligned_fp32_len, matmul_q5_0_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1067. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0]->a_s, "matmul_q5_0_f32_aligned_s", matmul_q5_0_f32_aligned_fp32_len, matmul_q5_0_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1068. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1]->l, "matmul_q5_1_f32_l", matmul_q5_1_f32_fp32_len, matmul_q5_1_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1069. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1]->m, "matmul_q5_1_f32_m", matmul_q5_1_f32_fp32_len, matmul_q5_1_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1070. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1]->s, "matmul_q5_1_f32_s", matmul_q5_1_f32_fp32_len, matmul_q5_1_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1071. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1]->a_l, "matmul_q5_1_f32_aligned_l", matmul_q5_1_f32_aligned_fp32_len, matmul_q5_1_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1072. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1]->a_m, "matmul_q5_1_f32_aligned_m", matmul_q5_1_f32_aligned_fp32_len, matmul_q5_1_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1073. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1]->a_s, "matmul_q5_1_f32_aligned_s", matmul_q5_1_f32_aligned_fp32_len, matmul_q5_1_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1074. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0]->l, "matmul_q8_0_f32_l", matmul_q8_0_f32_fp32_len, matmul_q8_0_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1075. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0]->m, "matmul_q8_0_f32_m", matmul_q8_0_f32_fp32_len, matmul_q8_0_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1076. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0]->s, "matmul_q8_0_f32_s", matmul_q8_0_f32_fp32_len, matmul_q8_0_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1077. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0]->a_l, "matmul_q8_0_f32_aligned_l", matmul_q8_0_f32_aligned_fp32_len, matmul_q8_0_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1078. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0]->a_m, "matmul_q8_0_f32_aligned_m", matmul_q8_0_f32_aligned_fp32_len, matmul_q8_0_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1079. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0]->a_s, "matmul_q8_0_f32_aligned_s", matmul_q8_0_f32_aligned_fp32_len, matmul_q8_0_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1080. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K]->l, "matmul_q2_k_f32_l", matmul_q2_k_f32_fp32_len, matmul_q2_k_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1081. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K]->m, "matmul_q2_k_f32_m", matmul_q2_k_f32_fp32_len, matmul_q2_k_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1082. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K]->s, "matmul_q2_k_f32_s", matmul_q2_k_f32_fp32_len, matmul_q2_k_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1083. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K]->a_l, "matmul_q2_k_f32_aligned_l", matmul_q2_k_f32_aligned_fp32_len, matmul_q2_k_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1084. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K]->a_m, "matmul_q2_k_f32_aligned_m", matmul_q2_k_f32_aligned_fp32_len, matmul_q2_k_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1085. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K]->a_s, "matmul_q2_k_f32_aligned_s", matmul_q2_k_f32_aligned_fp32_len, matmul_q2_k_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1086. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K]->l, "matmul_q3_k_f32_l", matmul_q3_k_f32_fp32_len, matmul_q3_k_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1087. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K]->m, "matmul_q3_k_f32_m", matmul_q3_k_f32_fp32_len, matmul_q3_k_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1088. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K]->s, "matmul_q3_k_f32_s", matmul_q3_k_f32_fp32_len, matmul_q3_k_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1089. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K]->a_l, "matmul_q3_k_f32_aligned_l", matmul_q3_k_f32_aligned_fp32_len, matmul_q3_k_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1090. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K]->a_m, "matmul_q3_k_f32_aligned_m", matmul_q3_k_f32_aligned_fp32_len, matmul_q3_k_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1091. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K]->a_s, "matmul_q3_k_f32_aligned_s", matmul_q3_k_f32_aligned_fp32_len, matmul_q3_k_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1092. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K]->l, "matmul_q4_k_f32_l", matmul_q4_k_f32_fp32_len, matmul_q4_k_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1093. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K]->m, "matmul_q4_k_f32_m", matmul_q4_k_f32_fp32_len, matmul_q4_k_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1094. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K]->s, "matmul_q4_k_f32_s", matmul_q4_k_f32_fp32_len, matmul_q4_k_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1095. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K]->a_l, "matmul_q4_k_f32_aligned_l", matmul_q4_k_f32_aligned_fp32_len, matmul_q4_k_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1096. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K]->a_m, "matmul_q4_k_f32_aligned_m", matmul_q4_k_f32_aligned_fp32_len, matmul_q4_k_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1097. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K]->a_s, "matmul_q4_k_f32_aligned_s", matmul_q4_k_f32_aligned_fp32_len, matmul_q4_k_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1098. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K]->l, "matmul_q5_k_f32_l", matmul_q5_k_f32_fp32_len, matmul_q5_k_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1099. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K]->m, "matmul_q5_k_f32_m", matmul_q5_k_f32_fp32_len, matmul_q5_k_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1100. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K]->s, "matmul_q5_k_f32_s", matmul_q5_k_f32_fp32_len, matmul_q5_k_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1101. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K]->a_l, "matmul_q5_k_f32_aligned_l", matmul_q5_k_f32_aligned_fp32_len, matmul_q5_k_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1102. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K]->a_m, "matmul_q5_k_f32_aligned_m", matmul_q5_k_f32_aligned_fp32_len, matmul_q5_k_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1103. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K]->a_s, "matmul_q5_k_f32_aligned_s", matmul_q5_k_f32_aligned_fp32_len, matmul_q5_k_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1104. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K]->l, "matmul_q6_k_f32_l", matmul_q6_k_f32_fp32_len, matmul_q6_k_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1105. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K]->m, "matmul_q6_k_f32_m", matmul_q6_k_f32_fp32_len, matmul_q6_k_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1106. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K]->s, "matmul_q6_k_f32_s", matmul_q6_k_f32_fp32_len, matmul_q6_k_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1107. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K]->a_l, "matmul_q6_k_f32_aligned_l", matmul_q6_k_f32_aligned_fp32_len, matmul_q6_k_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1108. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K]->a_m, "matmul_q6_k_f32_aligned_m", matmul_q6_k_f32_aligned_fp32_len, matmul_q6_k_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1109. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K]->a_s, "matmul_q6_k_f32_aligned_s", matmul_q6_k_f32_aligned_fp32_len, matmul_q6_k_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1110. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f32->l, "matmul_id_f32_l", matmul_id_f32_fp32_len, matmul_id_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_l, 1);
  1111. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f32->m, "matmul_id_f32_m", matmul_id_f32_fp32_len, matmul_id_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_m, 1);
  1112. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f32->s, "matmul_id_f32_s", matmul_id_f32_fp32_len, matmul_id_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_s, 1);
  1113. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f32->a_l, "matmul_id_f32_aligned_l", matmul_id_f32_aligned_fp32_len, matmul_id_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_l, l_align);
  1114. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f32->a_m, "matmul_id_f32_aligned_m", matmul_id_f32_aligned_fp32_len, matmul_id_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_m, m_align);
  1115. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f32->a_s, "matmul_id_f32_aligned_s", matmul_id_f32_aligned_fp32_len, matmul_id_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_s, s_align);
  1116. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f16->l, "matmul_id_f16_l", matmul_id_f16_fp32_len, matmul_id_f16_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_l, 1);
  1117. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f16->m, "matmul_id_f16_m", matmul_id_f16_fp32_len, matmul_id_f16_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_m, 1);
  1118. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f16->s, "matmul_id_f16_s", matmul_id_f16_fp32_len, matmul_id_f16_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_s, 1);
  1119. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f16->a_l, "matmul_id_f16_aligned_l", matmul_id_f16_aligned_fp32_len, matmul_id_f16_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_l, l_align);
  1120. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f16->a_m, "matmul_id_f16_aligned_m", matmul_id_f16_aligned_fp32_len, matmul_id_f16_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_m, m_align);
  1121. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f16->a_s, "matmul_id_f16_aligned_s", matmul_id_f16_aligned_fp32_len, matmul_id_f16_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_s, s_align);
  1122. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f16_f32->l, "matmul_id_f16_f32_l", matmul_id_f16_f32_fp32_len, matmul_id_f16_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_l, 1);
  1123. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f16_f32->m, "matmul_id_f16_f32_m", matmul_id_f16_f32_fp32_len, matmul_id_f16_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_m, 1);
  1124. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f16_f32->s, "matmul_id_f16_f32_s", matmul_id_f16_f32_fp32_len, matmul_id_f16_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_s, 1);
  1125. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f16_f32->a_l, "matmul_id_f16_f32_aligned_l", matmul_id_f16_f32_aligned_fp32_len, matmul_id_f16_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_l, l_align);
  1126. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f16_f32->a_m, "matmul_id_f16_f32_aligned_m", matmul_id_f16_f32_aligned_fp32_len, matmul_id_f16_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_m, m_align);
  1127. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f16_f32->a_s, "matmul_id_f16_f32_aligned_s", matmul_id_f16_f32_aligned_fp32_len, matmul_id_f16_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_s, s_align);
  1128. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0]->l, "matmul_id_q4_0_f32_l", matmul_id_q4_0_f32_fp32_len, matmul_id_q4_0_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1129. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0]->m, "matmul_id_q4_0_f32_m", matmul_id_q4_0_f32_fp32_len, matmul_id_q4_0_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1130. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0]->s, "matmul_id_q4_0_f32_s", matmul_id_q4_0_f32_fp32_len, matmul_id_q4_0_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1131. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0]->a_l, "matmul_id_q4_0_f32_aligned_l", matmul_id_q4_0_f32_aligned_fp32_len, matmul_id_q4_0_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1132. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0]->a_m, "matmul_id_q4_0_f32_aligned_m", matmul_id_q4_0_f32_aligned_fp32_len, matmul_id_q4_0_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1133. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0]->a_s, "matmul_id_q4_0_f32_aligned_s", matmul_id_q4_0_f32_aligned_fp32_len, matmul_id_q4_0_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1134. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1]->l, "matmul_id_q4_1_f32_l", matmul_id_q4_1_f32_fp32_len, matmul_id_q4_1_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1135. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1]->m, "matmul_id_q4_1_f32_m", matmul_id_q4_1_f32_fp32_len, matmul_id_q4_1_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1136. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1]->s, "matmul_id_q4_1_f32_s", matmul_id_q4_1_f32_fp32_len, matmul_id_q4_1_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1137. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1]->a_l, "matmul_id_q4_1_f32_aligned_l", matmul_id_q4_1_f32_aligned_fp32_len, matmul_id_q4_1_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1138. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1]->a_m, "matmul_id_q4_1_f32_aligned_m", matmul_id_q4_1_f32_aligned_fp32_len, matmul_id_q4_1_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1139. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1]->a_s, "matmul_id_q4_1_f32_aligned_s", matmul_id_q4_1_f32_aligned_fp32_len, matmul_id_q4_1_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1140. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0]->l, "matmul_id_q5_0_f32_l", matmul_id_q5_0_f32_fp32_len, matmul_id_q5_0_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1141. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0]->m, "matmul_id_q5_0_f32_m", matmul_id_q5_0_f32_fp32_len, matmul_id_q5_0_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1142. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0]->s, "matmul_id_q5_0_f32_s", matmul_id_q5_0_f32_fp32_len, matmul_id_q5_0_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1143. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0]->a_l, "matmul_id_q5_0_f32_aligned_l", matmul_id_q5_0_f32_aligned_fp32_len, matmul_id_q5_0_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1144. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0]->a_m, "matmul_id_q5_0_f32_aligned_m", matmul_id_q5_0_f32_aligned_fp32_len, matmul_id_q5_0_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1145. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0]->a_s, "matmul_id_q5_0_f32_aligned_s", matmul_id_q5_0_f32_aligned_fp32_len, matmul_id_q5_0_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1146. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1]->l, "matmul_id_q5_1_f32_l", matmul_id_q5_1_f32_fp32_len, matmul_id_q5_1_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1147. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1]->m, "matmul_id_q5_1_f32_m", matmul_id_q5_1_f32_fp32_len, matmul_id_q5_1_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1148. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1]->s, "matmul_id_q5_1_f32_s", matmul_id_q5_1_f32_fp32_len, matmul_id_q5_1_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1149. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1]->a_l, "matmul_id_q5_1_f32_aligned_l", matmul_id_q5_1_f32_aligned_fp32_len, matmul_id_q5_1_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1150. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1]->a_m, "matmul_id_q5_1_f32_aligned_m", matmul_id_q5_1_f32_aligned_fp32_len, matmul_id_q5_1_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1151. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1]->a_s, "matmul_id_q5_1_f32_aligned_s", matmul_id_q5_1_f32_aligned_fp32_len, matmul_id_q5_1_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1152. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0]->l, "matmul_id_q8_0_f32_l", matmul_id_q8_0_f32_fp32_len, matmul_id_q8_0_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1153. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0]->m, "matmul_id_q8_0_f32_m", matmul_id_q8_0_f32_fp32_len, matmul_id_q8_0_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1154. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0]->s, "matmul_id_q8_0_f32_s", matmul_id_q8_0_f32_fp32_len, matmul_id_q8_0_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1155. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0]->a_l, "matmul_id_q8_0_f32_aligned_l", matmul_id_q8_0_f32_aligned_fp32_len, matmul_id_q8_0_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1156. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0]->a_m, "matmul_id_q8_0_f32_aligned_m", matmul_id_q8_0_f32_aligned_fp32_len, matmul_id_q8_0_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1157. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0]->a_s, "matmul_id_q8_0_f32_aligned_s", matmul_id_q8_0_f32_aligned_fp32_len, matmul_id_q8_0_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1158. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K]->l, "matmul_id_q2_k_f32_l", matmul_id_q2_k_f32_fp32_len, matmul_id_q2_k_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1159. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K]->m, "matmul_id_q2_k_f32_m", matmul_id_q2_k_f32_fp32_len, matmul_id_q2_k_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1160. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K]->s, "matmul_id_q2_k_f32_s", matmul_id_q2_k_f32_fp32_len, matmul_id_q2_k_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1161. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K]->a_l, "matmul_id_q2_k_f32_aligned_l", matmul_id_q2_k_f32_aligned_fp32_len, matmul_id_q2_k_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1162. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K]->a_m, "matmul_id_q2_k_f32_aligned_m", matmul_id_q2_k_f32_aligned_fp32_len, matmul_id_q2_k_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1163. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K]->a_s, "matmul_id_q2_k_f32_aligned_s", matmul_id_q2_k_f32_aligned_fp32_len, matmul_id_q2_k_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1164. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K]->l, "matmul_id_q3_k_f32_l", matmul_id_q3_k_f32_fp32_len, matmul_id_q3_k_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1165. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K]->m, "matmul_id_q3_k_f32_m", matmul_id_q3_k_f32_fp32_len, matmul_id_q3_k_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1166. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K]->s, "matmul_id_q3_k_f32_s", matmul_id_q3_k_f32_fp32_len, matmul_id_q3_k_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1167. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K]->a_l, "matmul_id_q3_k_f32_aligned_l", matmul_id_q3_k_f32_aligned_fp32_len, matmul_id_q3_k_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1168. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K]->a_m, "matmul_id_q3_k_f32_aligned_m", matmul_id_q3_k_f32_aligned_fp32_len, matmul_id_q3_k_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1169. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K]->a_s, "matmul_id_q3_k_f32_aligned_s", matmul_id_q3_k_f32_aligned_fp32_len, matmul_id_q3_k_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1170. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K]->l, "matmul_id_q4_k_f32_l", matmul_id_q4_k_f32_fp32_len, matmul_id_q4_k_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1171. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K]->m, "matmul_id_q4_k_f32_m", matmul_id_q4_k_f32_fp32_len, matmul_id_q4_k_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1172. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K]->s, "matmul_id_q4_k_f32_s", matmul_id_q4_k_f32_fp32_len, matmul_id_q4_k_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1173. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K]->a_l, "matmul_id_q4_k_f32_aligned_l", matmul_id_q4_k_f32_aligned_fp32_len, matmul_id_q4_k_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1174. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K]->a_m, "matmul_id_q4_k_f32_aligned_m", matmul_id_q4_k_f32_aligned_fp32_len, matmul_id_q4_k_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1175. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K]->a_s, "matmul_id_q4_k_f32_aligned_s", matmul_id_q4_k_f32_aligned_fp32_len, matmul_id_q4_k_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1176. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K]->l, "matmul_id_q5_k_f32_l", matmul_id_q5_k_f32_fp32_len, matmul_id_q5_k_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1177. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K]->m, "matmul_id_q5_k_f32_m", matmul_id_q5_k_f32_fp32_len, matmul_id_q5_k_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1178. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K]->s, "matmul_id_q5_k_f32_s", matmul_id_q5_k_f32_fp32_len, matmul_id_q5_k_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1179. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K]->a_l, "matmul_id_q5_k_f32_aligned_l", matmul_id_q5_k_f32_aligned_fp32_len, matmul_id_q5_k_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1180. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K]->a_m, "matmul_id_q5_k_f32_aligned_m", matmul_id_q5_k_f32_aligned_fp32_len, matmul_id_q5_k_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1181. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K]->a_s, "matmul_id_q5_k_f32_aligned_s", matmul_id_q5_k_f32_aligned_fp32_len, matmul_id_q5_k_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1182. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K]->l, "matmul_id_q6_k_f32_l", matmul_id_q6_k_f32_fp32_len, matmul_id_q6_k_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1183. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K]->m, "matmul_id_q6_k_f32_m", matmul_id_q6_k_f32_fp32_len, matmul_id_q6_k_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1184. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K]->s, "matmul_id_q6_k_f32_s", matmul_id_q6_k_f32_fp32_len, matmul_id_q6_k_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1185. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K]->a_l, "matmul_id_q6_k_f32_aligned_l", matmul_id_q6_k_f32_aligned_fp32_len, matmul_id_q6_k_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1186. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K]->a_m, "matmul_id_q6_k_f32_aligned_m", matmul_id_q6_k_f32_aligned_fp32_len, matmul_id_q6_k_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1187. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K]->a_s, "matmul_id_q6_k_f32_aligned_s", matmul_id_q6_k_f32_aligned_fp32_len, matmul_id_q6_k_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_id_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1188. }
  1189. // mul mat vec
  1190. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_F32 ], "mul_mat_vec_f32_f32_f32", mul_mat_vec_f32_f32_f32_len, mul_mat_vec_f32_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1191. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_F16 ], "mul_mat_vec_f16_f32_f32", mul_mat_vec_f16_f32_f32_len, mul_mat_vec_f16_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1192. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q4_0], "mul_mat_vec_q4_0_f32_f32", mul_mat_vec_q4_0_f32_f32_len, mul_mat_vec_q4_0_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1193. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q4_1], "mul_mat_vec_q4_1_f32_f32", mul_mat_vec_q4_1_f32_f32_len, mul_mat_vec_q4_1_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1194. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q5_0], "mul_mat_vec_q5_0_f32_f32", mul_mat_vec_q5_0_f32_f32_len, mul_mat_vec_q5_0_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1195. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q5_1], "mul_mat_vec_q5_1_f32_f32", mul_mat_vec_q5_1_f32_f32_len, mul_mat_vec_q5_1_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1196. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q8_0], "mul_mat_vec_q8_0_f32_f32", 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, 1, 1}, { device->subgroup_size }, 1);
  1197. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q2_K], "mul_mat_vec_q2_K_f32_f32", mul_mat_vec_q2_K_f32_f32_len, mul_mat_vec_q2_K_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1198. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q3_K], "mul_mat_vec_q3_K_f32_f32", mul_mat_vec_q3_K_f32_f32_len, mul_mat_vec_q3_K_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1199. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q4_K], "mul_mat_vec_q4_K_f32_f32", mul_mat_vec_q4_K_f32_f32_len, mul_mat_vec_q4_K_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1200. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q5_K], "mul_mat_vec_q5_K_f32_f32", mul_mat_vec_q5_K_f32_f32_len, mul_mat_vec_q5_K_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1201. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q6_K], "mul_mat_vec_q6_K_f32_f32", mul_mat_vec_q6_K_f32_f32_len, mul_mat_vec_q6_K_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1202. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_F32 ], "mul_mat_vec_f32_f16_f32", mul_mat_vec_f32_f16_f32_len, mul_mat_vec_f32_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1203. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_F16 ], "mul_mat_vec_f16_f16_f32", mul_mat_vec_f16_f16_f32_len, mul_mat_vec_f16_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1204. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q4_0], "mul_mat_vec_q4_0_f16_f32", mul_mat_vec_q4_0_f16_f32_len, mul_mat_vec_q4_0_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1205. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q4_1], "mul_mat_vec_q4_1_f16_f32", mul_mat_vec_q4_1_f16_f32_len, mul_mat_vec_q4_1_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1206. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q5_0], "mul_mat_vec_q5_0_f16_f32", mul_mat_vec_q5_0_f16_f32_len, mul_mat_vec_q5_0_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1207. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q5_1], "mul_mat_vec_q5_1_f16_f32", mul_mat_vec_q5_1_f16_f32_len, mul_mat_vec_q5_1_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1208. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q8_0], "mul_mat_vec_q8_0_f16_f32", 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, 1, 1}, { device->subgroup_size }, 1);
  1209. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q2_K], "mul_mat_vec_q2_K_f16_f32", mul_mat_vec_q2_K_f16_f32_len, mul_mat_vec_q2_K_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1210. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q3_K], "mul_mat_vec_q3_K_f16_f32", mul_mat_vec_q3_K_f16_f32_len, mul_mat_vec_q3_K_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1211. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q4_K], "mul_mat_vec_q4_K_f16_f32", mul_mat_vec_q4_K_f16_f32_len, mul_mat_vec_q4_K_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1212. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q5_K], "mul_mat_vec_q5_K_f16_f32", mul_mat_vec_q5_K_f16_f32_len, mul_mat_vec_q5_K_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1213. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q6_K], "mul_mat_vec_q6_K_f16_f32", mul_mat_vec_q6_K_f16_f32_len, mul_mat_vec_q6_K_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1214. ggml_vk_create_pipeline(ctx, ctx->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), {1, 1, 1}, { device->subgroup_size }, 1);
  1215. ggml_vk_create_pipeline(ctx, ctx->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), {1, 1, 1}, { device->subgroup_size }, 1);
  1216. ggml_vk_create_pipeline(ctx, ctx->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), {1, 1, 1}, { device->subgroup_size }, 1);
  1217. ggml_vk_create_pipeline(ctx, ctx->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), {1, 1, 1}, { device->subgroup_size }, 1);
  1218. ggml_vk_create_pipeline(ctx, ctx->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), {1, 1, 1}, { device->subgroup_size }, 1);
  1219. ggml_vk_create_pipeline(ctx, ctx->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), {1, 1, 1}, { device->subgroup_size }, 1);
  1220. ggml_vk_create_pipeline(ctx, ctx->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, 1, 1}, { device->subgroup_size }, 1);
  1221. ggml_vk_create_pipeline(ctx, ctx->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), {1, 1, 1}, { device->subgroup_size }, 1);
  1222. ggml_vk_create_pipeline(ctx, ctx->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), {1, 1, 1}, { device->subgroup_size }, 1);
  1223. ggml_vk_create_pipeline(ctx, ctx->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), {1, 1, 1}, { device->subgroup_size }, 1);
  1224. ggml_vk_create_pipeline(ctx, ctx->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), {1, 1, 1}, { device->subgroup_size }, 1);
  1225. ggml_vk_create_pipeline(ctx, ctx->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), {1, 1, 1}, { device->subgroup_size }, 1);
  1226. // dequant shaders
  1227. ggml_vk_create_pipeline(ctx, ctx->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);
  1228. ggml_vk_create_pipeline(ctx, ctx->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);
  1229. ggml_vk_create_pipeline(ctx, ctx->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);
  1230. ggml_vk_create_pipeline(ctx, ctx->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);
  1231. ggml_vk_create_pipeline(ctx, ctx->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);
  1232. ggml_vk_create_pipeline(ctx, ctx->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);
  1233. ggml_vk_create_pipeline(ctx, ctx->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);
  1234. ggml_vk_create_pipeline(ctx, ctx->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);
  1235. ggml_vk_create_pipeline(ctx, ctx->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);
  1236. ggml_vk_create_pipeline(ctx, ctx->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);
  1237. ggml_vk_create_pipeline(ctx, ctx->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);
  1238. // get_rows
  1239. ggml_vk_create_pipeline(ctx, ctx->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);
  1240. ggml_vk_create_pipeline(ctx, ctx->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);
  1241. ggml_vk_create_pipeline(ctx, ctx->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);
  1242. ggml_vk_create_pipeline(ctx, ctx->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);
  1243. ggml_vk_create_pipeline(ctx, ctx->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);
  1244. ggml_vk_create_pipeline(ctx, ctx->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);
  1245. ggml_vk_create_pipeline(ctx, ctx->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);
  1246. ggml_vk_create_pipeline(ctx, ctx->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);
  1247. ggml_vk_create_pipeline(ctx, ctx->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);
  1248. ggml_vk_create_pipeline(ctx, ctx->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);
  1249. ggml_vk_create_pipeline(ctx, ctx->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);
  1250. ggml_vk_create_pipeline(ctx, ctx->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);
  1251. ggml_vk_create_pipeline(ctx, ctx->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);
  1252. ggml_vk_create_pipeline(ctx, ctx->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);
  1253. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_split_k_reduce, "split_k_reduce", split_k_reduce_len, split_k_reduce_data, "main", 2, 2 * sizeof(uint32_t), {256, 1, 1}, {}, 1);
  1254. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_mul_mat_vec_p021_f16_f32, "mul_mat_vec_p021_f16_f32", mul_mat_vec_p021_f16_f32_len, mul_mat_vec_p021_f16_f32_data, "main", 3, 6 * sizeof(uint32_t), {1, 1, 1}, {}, 1);
  1255. ggml_vk_create_pipeline(ctx, ctx->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, 7 * sizeof(uint32_t), {1, 1, 1}, {}, 1);
  1256. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_norm_f32, "norm_f32", norm_f32_len, norm_f32_data, "main", 2, sizeof(vk_op_push_constants), {1, 1, 1}, {}, 1);
  1257. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_rms_norm_f32, "rms_norm_f32", rms_norm_f32_len, rms_norm_f32_data, "main", 2, sizeof(vk_op_push_constants), {1, 1, 1}, {}, 1);
  1258. ggml_vk_create_pipeline(ctx, ctx->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);
  1259. ggml_vk_create_pipeline(ctx, ctx->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);
  1260. ggml_vk_create_pipeline(ctx, ctx->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);
  1261. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_add_f32, "add_f32", add_f32_len, add_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {}, 1);
  1262. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_mul_f32, "mul_f32", mul_f32_len, mul_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {}, 1);
  1263. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_div_f32, "div_f32", div_f32_len, div_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {}, 1);
  1264. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_scale_f32, "scale_f32", scale_f32_len, scale_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  1265. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_sqr_f32, "sqr_f32", sqr_f32_len, sqr_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  1266. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_clamp_f32, "clamp_f32", clamp_f32_len, clamp_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  1267. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_gelu_f32, "gelu_f32", gelu_f32_len, gelu_f32_data, "main", 2, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
  1268. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_silu_f32, "silu_f32", silu_f32_len, silu_f32_data, "main", 2, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
  1269. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_relu_f32, "relu_f32", relu_f32_len, relu_f32_data, "main", 2, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
  1270. ggml_vk_create_pipeline(ctx, ctx->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), {512, 1, 1}, {}, 1);
  1271. ggml_vk_create_pipeline(ctx, ctx->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}, {}, 1);
  1272. ggml_vk_create_pipeline(ctx, ctx->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}, {}, 1);
  1273. ggml_vk_create_pipeline(ctx, ctx->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);
  1274. ggml_vk_create_pipeline(ctx, ctx->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);
  1275. ggml_vk_create_pipeline(ctx, ctx->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);
  1276. ggml_vk_create_pipeline(ctx, ctx->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);
  1277. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_argsort_f32, "argsort_f32", argsort_f32_len, argsort_f32_data, "main", 2, sizeof(vk_op_argsort_push_constants), {1024, 1, 1}, {}, 1);
  1278. ggml_vk_create_pipeline(ctx, ctx->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);
  1279. }
  1280. static void ggml_vk_print_gpu_info(size_t idx) {
  1281. GGML_ASSERT(idx < vk_instance.device_indices.size());
  1282. size_t dev_num = vk_instance.device_indices[idx];
  1283. #ifdef GGML_VULKAN_DEBUG
  1284. std::cerr << "ggml_vk_print_gpu_info(" << dev_num << ")" << std::endl;
  1285. #endif
  1286. GGML_ASSERT(vk_instance.initialized);
  1287. std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
  1288. if (dev_num >= devices.size()) {
  1289. std::cerr << "ggml_vulkan: Device with index " << dev_num << " does not exist." << std::endl;
  1290. throw std::runtime_error("Device not found");
  1291. }
  1292. vk::PhysicalDevice physical_device = devices[dev_num];
  1293. std::vector<vk::ExtensionProperties> ext_props = physical_device.enumerateDeviceExtensionProperties();
  1294. vk::PhysicalDeviceProperties2 props2;
  1295. vk::PhysicalDeviceMaintenance3Properties props3;
  1296. vk::PhysicalDeviceSubgroupProperties subgroup_props;
  1297. props2.pNext = &props3;
  1298. props3.pNext = &subgroup_props;
  1299. physical_device.getProperties2(&props2);
  1300. const size_t subgroup_size = subgroup_props.subgroupSize;
  1301. const bool uma = props2.properties.deviceType == vk::PhysicalDeviceType::eIntegratedGpu;
  1302. bool fp16_storage = false;
  1303. bool fp16_compute = false;
  1304. for (auto properties : ext_props) {
  1305. if (strcmp("VK_KHR_16bit_storage", properties.extensionName) == 0) {
  1306. fp16_storage = true;
  1307. } else if (strcmp("VK_KHR_shader_float16_int8", properties.extensionName) == 0) {
  1308. fp16_compute = true;
  1309. }
  1310. }
  1311. const char* GGML_VK_DISABLE_F16 = getenv("GGML_VK_DISABLE_F16");
  1312. bool force_disable_f16 = GGML_VK_DISABLE_F16 != nullptr;
  1313. bool fp16 = !force_disable_f16 && fp16_storage && fp16_compute;
  1314. vk::PhysicalDeviceFeatures device_features = physical_device.getFeatures();
  1315. VkPhysicalDeviceFeatures2 device_features2;
  1316. device_features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
  1317. device_features2.pNext = nullptr;
  1318. device_features2.features = (VkPhysicalDeviceFeatures)device_features;
  1319. VkPhysicalDeviceVulkan11Features vk11_features;
  1320. vk11_features.pNext = nullptr;
  1321. vk11_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES;
  1322. device_features2.pNext = &vk11_features;
  1323. VkPhysicalDeviceVulkan12Features vk12_features;
  1324. vk12_features.pNext = nullptr;
  1325. vk12_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES;
  1326. vk11_features.pNext = &vk12_features;
  1327. vkGetPhysicalDeviceFeatures2(physical_device, &device_features2);
  1328. fp16 = fp16 && vk12_features.shaderFloat16;
  1329. std::string device_name = props2.properties.deviceName.data();
  1330. std::cerr << GGML_VK_NAME << idx << ": " << device_name << " | uma: " << uma << " | fp16: " << fp16 << " | warp size: " << subgroup_size << std::endl;
  1331. if (props2.properties.deviceType == vk::PhysicalDeviceType::eCpu) {
  1332. std::cerr << "ggml_vulkan: Warning: Device type is CPU. This is probably not the device you want." << std::endl;
  1333. }
  1334. }
  1335. static bool ggml_vk_instance_validation_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions);
  1336. static bool ggml_vk_instance_portability_enumeration_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions);
  1337. void ggml_vk_instance_init() {
  1338. if (vk_instance_initialized) {
  1339. return;
  1340. }
  1341. #ifdef GGML_VULKAN_DEBUG
  1342. std::cerr << "ggml_vk_instance_init()" << std::endl;
  1343. #endif
  1344. vk::ApplicationInfo app_info{ "ggml-vulkan", 1, nullptr, 0, VK_API_VERSION };
  1345. const std::vector<vk::ExtensionProperties> instance_extensions = vk::enumerateInstanceExtensionProperties();
  1346. const bool validation_ext = ggml_vk_instance_validation_ext_available(instance_extensions);
  1347. #ifdef __APPLE__
  1348. const bool portability_enumeration_ext = ggml_vk_instance_portability_enumeration_ext_available(instance_extensions);
  1349. #endif
  1350. std::vector<const char*> layers;
  1351. if (validation_ext) {
  1352. layers.push_back("VK_LAYER_KHRONOS_validation");
  1353. }
  1354. std::vector<const char*> extensions;
  1355. if (validation_ext) {
  1356. extensions.push_back("VK_EXT_validation_features");
  1357. }
  1358. #ifdef __APPLE__
  1359. if (portability_enumeration_ext) {
  1360. extensions.push_back("VK_KHR_portability_enumeration");
  1361. }
  1362. #endif
  1363. vk::InstanceCreateInfo instance_create_info(vk::InstanceCreateFlags{}, &app_info, layers, extensions);
  1364. #ifdef __APPLE__
  1365. if (portability_enumeration_ext) {
  1366. instance_create_info.flags |= vk::InstanceCreateFlagBits::eEnumeratePortabilityKHR;
  1367. }
  1368. #endif
  1369. std::vector<vk::ValidationFeatureEnableEXT> features_enable;
  1370. vk::ValidationFeaturesEXT validation_features;
  1371. if (validation_ext) {
  1372. features_enable = { vk::ValidationFeatureEnableEXT::eBestPractices };
  1373. validation_features = {
  1374. features_enable,
  1375. {},
  1376. };
  1377. validation_features.setPNext(nullptr);
  1378. instance_create_info.setPNext(&validation_features);
  1379. std::cerr << "ggml_vulkan: Validation layers enabled" << std::endl;
  1380. }
  1381. vk_instance.instance = vk::createInstance(instance_create_info);
  1382. memset(vk_instance.initialized, 0, sizeof(bool) * GGML_VK_MAX_DEVICES);
  1383. size_t num_available_devices = vk_instance.instance.enumeratePhysicalDevices().size();
  1384. // Emulate behavior of CUDA_VISIBLE_DEVICES for Vulkan
  1385. char * devices_env = getenv("GGML_VK_VISIBLE_DEVICES");
  1386. if (devices_env != nullptr) {
  1387. std::string devices(devices_env);
  1388. std::replace(devices.begin(), devices.end(), ',', ' ');
  1389. std::stringstream ss(devices);
  1390. size_t tmp;
  1391. while (ss >> tmp) {
  1392. if(tmp >= num_available_devices) {
  1393. std::cerr << "ggml_vulkan: Invalid device index " << tmp << " in GGML_VK_VISIBLE_DEVICES." << std::endl;
  1394. throw std::runtime_error("Invalid Vulkan device index");
  1395. }
  1396. vk_instance.device_indices.push_back(tmp);
  1397. }
  1398. } else {
  1399. std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
  1400. // Make sure at least one device exists
  1401. if (devices.empty()) {
  1402. std::cerr << "ggml_vulkan: Error: No devices found." << std::endl;
  1403. GGML_ASSERT(false);
  1404. }
  1405. // Default to using all dedicated GPUs
  1406. for (size_t i = 0; i < devices.size(); i++) {
  1407. vk::PhysicalDeviceProperties props = devices[i].getProperties();
  1408. if (props.deviceType == vk::PhysicalDeviceType::eDiscreteGpu) {
  1409. vk_instance.device_indices.push_back(i);
  1410. }
  1411. }
  1412. // If no dedicated GPUs found, fall back to GPU 0
  1413. if (vk_instance.device_indices.empty()) {
  1414. vk_instance.device_indices.push_back(0);
  1415. }
  1416. }
  1417. std::cerr << "ggml_vulkan: Found " << vk_instance.device_indices.size() << " Vulkan devices:" << std::endl;
  1418. for (size_t i = 0; i < vk_instance.device_indices.size(); i++) {
  1419. ggml_vk_print_gpu_info(i);
  1420. }
  1421. vk_instance_initialized = true;
  1422. }
  1423. static void ggml_vk_init(ggml_backend_vk_context * ctx, size_t idx) {
  1424. GGML_ASSERT(idx < vk_instance.device_indices.size());
  1425. size_t dev_num = vk_instance.device_indices[idx];
  1426. #ifdef GGML_VULKAN_DEBUG
  1427. std::cerr << "ggml_vk_init(" << ctx->name << ", " << dev_num << ")" << std::endl;
  1428. #endif
  1429. ggml_vk_instance_init();
  1430. std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
  1431. if (dev_num >= devices.size()) {
  1432. std::cerr << "ggml_vulkan: Device with index " << dev_num << " does not exist." << std::endl;
  1433. throw std::runtime_error("Device not found");
  1434. }
  1435. ctx->device = ggml_vk_get_device(idx);
  1436. if (!ctx->device->initialized) {
  1437. ctx->device->physical_device = devices[dev_num];
  1438. const std::vector<vk::ExtensionProperties> ext_props = ctx->device->physical_device.enumerateDeviceExtensionProperties();
  1439. bool maintenance4_support = false;
  1440. // Check if maintenance4 is supported
  1441. for (const auto& properties : ext_props) {
  1442. if (strcmp("VK_KHR_maintenance4", properties.extensionName) == 0) {
  1443. maintenance4_support = true;
  1444. }
  1445. }
  1446. vk::PhysicalDeviceProperties2 props2;
  1447. vk::PhysicalDeviceMaintenance3Properties props3;
  1448. vk::PhysicalDeviceMaintenance4Properties props4;
  1449. vk::PhysicalDeviceSubgroupProperties subgroup_props;
  1450. props2.pNext = &props3;
  1451. props3.pNext = &subgroup_props;
  1452. if (maintenance4_support) {
  1453. subgroup_props.pNext = &props4;
  1454. }
  1455. ctx->device->physical_device.getProperties2(&props2);
  1456. ctx->device->properties = props2.properties;
  1457. const char* GGML_VK_FORCE_MAX_ALLOCATION_SIZE = getenv("GGML_VK_FORCE_MAX_ALLOCATION_SIZE");
  1458. if (GGML_VK_FORCE_MAX_ALLOCATION_SIZE != nullptr) {
  1459. ctx->device->max_memory_allocation_size = std::stoi(GGML_VK_FORCE_MAX_ALLOCATION_SIZE);
  1460. } else if (maintenance4_support) {
  1461. ctx->device->max_memory_allocation_size = std::min(props3.maxMemoryAllocationSize, props4.maxBufferSize);
  1462. } else {
  1463. ctx->device->max_memory_allocation_size = props3.maxMemoryAllocationSize;
  1464. }
  1465. ctx->device->vendor_id = ctx->device->properties.vendorID;
  1466. ctx->device->subgroup_size = subgroup_props.subgroupSize;
  1467. ctx->device->uma = ctx->device->properties.deviceType == vk::PhysicalDeviceType::eIntegratedGpu;
  1468. bool fp16_storage = false;
  1469. bool fp16_compute = false;
  1470. for (const auto& properties : ext_props) {
  1471. if (strcmp("VK_KHR_16bit_storage", properties.extensionName) == 0) {
  1472. fp16_storage = true;
  1473. } else if (strcmp("VK_KHR_shader_float16_int8", properties.extensionName) == 0) {
  1474. fp16_compute = true;
  1475. }
  1476. }
  1477. const char* GGML_VK_DISABLE_F16 = getenv("GGML_VK_DISABLE_F16");
  1478. const bool force_disable_f16 = GGML_VK_DISABLE_F16 != nullptr;
  1479. ctx->device->fp16 = !force_disable_f16 && fp16_storage && fp16_compute;
  1480. std::vector<vk::QueueFamilyProperties> queue_family_props = ctx->device->physical_device.getQueueFamilyProperties();
  1481. // Try to find a non-graphics compute queue and transfer-focused queues
  1482. const uint32_t compute_queue_family_index = ggml_vk_find_queue_family_index(queue_family_props, vk::QueueFlagBits::eCompute, vk::QueueFlagBits::eGraphics, -1, 1);
  1483. 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);
  1484. const float priorities[] = { 1.0f, 1.0f };
  1485. ctx->device->single_queue = compute_queue_family_index == transfer_queue_family_index && queue_family_props[compute_queue_family_index].queueCount == 1;
  1486. std::vector<vk::DeviceQueueCreateInfo> device_queue_create_infos;
  1487. if (compute_queue_family_index != transfer_queue_family_index) {
  1488. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), compute_queue_family_index, 1, priorities});
  1489. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), transfer_queue_family_index, 1, priorities + 1});
  1490. } else if(!ctx->device->single_queue) {
  1491. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), compute_queue_family_index, 2, priorities});
  1492. } else {
  1493. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), compute_queue_family_index, 1, priorities});
  1494. }
  1495. vk::DeviceCreateInfo device_create_info;
  1496. std::vector<const char *> device_extensions;
  1497. vk::PhysicalDeviceFeatures device_features = ctx->device->physical_device.getFeatures();
  1498. VkPhysicalDeviceFeatures2 device_features2;
  1499. device_features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
  1500. device_features2.pNext = nullptr;
  1501. device_features2.features = (VkPhysicalDeviceFeatures)device_features;
  1502. VkPhysicalDeviceVulkan11Features vk11_features;
  1503. vk11_features.pNext = nullptr;
  1504. vk11_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES;
  1505. device_features2.pNext = &vk11_features;
  1506. VkPhysicalDeviceVulkan12Features vk12_features;
  1507. vk12_features.pNext = nullptr;
  1508. vk12_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES;
  1509. vk11_features.pNext = &vk12_features;
  1510. vkGetPhysicalDeviceFeatures2(ctx->device->physical_device, &device_features2);
  1511. ctx->device->fp16 = ctx->device->fp16 && vk12_features.shaderFloat16;
  1512. if (!vk11_features.storageBuffer16BitAccess) {
  1513. std::cerr << "ggml_vulkan: device " << GGML_VK_NAME << idx << " does not support 16-bit storage." << std::endl;
  1514. throw std::runtime_error("Unsupported device");
  1515. }
  1516. device_extensions.push_back("VK_KHR_16bit_storage");
  1517. #ifdef GGML_VULKAN_VALIDATE
  1518. device_extensions.push_back("VK_KHR_shader_non_semantic_info");
  1519. #endif
  1520. if (ctx->device->fp16) {
  1521. device_extensions.push_back("VK_KHR_shader_float16_int8");
  1522. }
  1523. ctx->device->name = ctx->device->properties.deviceName.data();
  1524. device_create_info = {
  1525. vk::DeviceCreateFlags(),
  1526. device_queue_create_infos,
  1527. {},
  1528. device_extensions
  1529. };
  1530. device_create_info.setPNext(&device_features2);
  1531. ctx->device->device = ctx->device->physical_device.createDevice(device_create_info);
  1532. ctx->device->descriptor_set_mode = VK_DEVICE_DESCRIPTOR_POOL_MODE_UNKNOWN;
  1533. // Queues
  1534. ggml_vk_create_queue(ctx, ctx->device->compute_queue, compute_queue_family_index, 0, { vk::PipelineStageFlagBits::eComputeShader | vk::PipelineStageFlagBits::eTransfer });
  1535. // Shaders
  1536. ggml_vk_load_shaders(ctx);
  1537. if (!ctx->device->single_queue) {
  1538. const uint32_t transfer_queue_index = compute_queue_family_index == transfer_queue_family_index ? 1 : 0;
  1539. ggml_vk_create_queue(ctx, ctx->device->transfer_queue, transfer_queue_family_index, transfer_queue_index, { vk::PipelineStageFlagBits::eTransfer });
  1540. } else {
  1541. // TODO: Use pointer or reference to avoid copy
  1542. ctx->device->transfer_queue = ctx->device->compute_queue;
  1543. }
  1544. ctx->device->idx = dev_num;
  1545. ctx->device->initialized = true;
  1546. } else if (ctx->device->idx != dev_num) {
  1547. std::cerr << "ggml_vulkan: Device " << ctx->device->name << " already initialized with index " << ctx->device->idx << ", but trying to reinitialize with index " << dev_num << std::endl;
  1548. throw std::runtime_error("Device already initialized");
  1549. }
  1550. ctx->fence = ctx->device->device.createFence({});
  1551. ctx->compute_ctx = nullptr;
  1552. ctx->transfer_ctx = nullptr;
  1553. ctx->initialized = true;
  1554. ctx->idx = idx;
  1555. #ifdef GGML_VULKAN_CHECK_RESULTS
  1556. const char* skip_checks = getenv("GGML_VULKAN_SKIP_CHECKS");
  1557. vk_skip_checks = (skip_checks == NULL ? 0 : atoi(skip_checks));
  1558. const char* output_tensor = getenv("GGML_VULKAN_OUTPUT_TENSOR");
  1559. vk_output_tensor = (output_tensor == NULL ? 0 : atoi(output_tensor));
  1560. #endif
  1561. }
  1562. static vk_pipeline ggml_vk_get_to_fp16(ggml_backend_vk_context * ctx, ggml_type type) {
  1563. #ifdef GGML_VULKAN_DEBUG
  1564. std::cerr << "ggml_vk_get_to_fp16()" << std::endl;
  1565. #endif
  1566. switch (type) {
  1567. case GGML_TYPE_F32:
  1568. case GGML_TYPE_Q4_0:
  1569. case GGML_TYPE_Q4_1:
  1570. case GGML_TYPE_Q5_0:
  1571. case GGML_TYPE_Q5_1:
  1572. case GGML_TYPE_Q8_0:
  1573. case GGML_TYPE_Q2_K:
  1574. case GGML_TYPE_Q3_K:
  1575. case GGML_TYPE_Q4_K:
  1576. case GGML_TYPE_Q5_K:
  1577. case GGML_TYPE_Q6_K:
  1578. break;
  1579. default:
  1580. return nullptr;
  1581. }
  1582. return ctx->device->pipeline_dequant[type];
  1583. }
  1584. static vk_matmul_pipeline ggml_vk_get_mul_mat_mat_pipeline(ggml_backend_vk_context * ctx, ggml_type src0_type, ggml_type src1_type) {
  1585. #ifdef GGML_VULKAN_DEBUG
  1586. std::cerr << "ggml_vk_get_mul_mat_mat_pipeline()" << std::endl;
  1587. #endif
  1588. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  1589. return ctx->device->pipeline_matmul_f32;
  1590. }
  1591. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F16) {
  1592. return ctx->device->pipeline_matmul_f32_f16;
  1593. }
  1594. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  1595. return ctx->device->pipeline_matmul_f16_f32;
  1596. }
  1597. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  1598. return ctx->device->pipeline_matmul_f16;
  1599. }
  1600. GGML_ASSERT(src1_type == GGML_TYPE_F32);
  1601. switch (src0_type) {
  1602. case GGML_TYPE_Q4_0:
  1603. case GGML_TYPE_Q4_1:
  1604. case GGML_TYPE_Q5_0:
  1605. case GGML_TYPE_Q5_1:
  1606. case GGML_TYPE_Q8_0:
  1607. case GGML_TYPE_Q2_K:
  1608. case GGML_TYPE_Q3_K:
  1609. case GGML_TYPE_Q4_K:
  1610. case GGML_TYPE_Q5_K:
  1611. case GGML_TYPE_Q6_K:
  1612. break;
  1613. default:
  1614. return nullptr;
  1615. }
  1616. return ctx->device->pipeline_dequant_mul_mat_mat[src0_type];
  1617. }
  1618. static vk_pipeline ggml_vk_get_dequantize_mul_mat_vec(ggml_backend_vk_context * ctx, ggml_type a_type, ggml_type b_type) {
  1619. #ifdef GGML_VULKAN_DEBUG
  1620. std::cerr << "ggml_vk_get_dequantize_mul_mat_vec()" << std::endl;
  1621. #endif
  1622. GGML_ASSERT(b_type == GGML_TYPE_F32 || b_type == GGML_TYPE_F16);
  1623. switch (a_type) {
  1624. case GGML_TYPE_F32:
  1625. case GGML_TYPE_F16:
  1626. case GGML_TYPE_Q4_0:
  1627. case GGML_TYPE_Q4_1:
  1628. case GGML_TYPE_Q5_0:
  1629. case GGML_TYPE_Q5_1:
  1630. case GGML_TYPE_Q8_0:
  1631. case GGML_TYPE_Q2_K:
  1632. case GGML_TYPE_Q3_K:
  1633. case GGML_TYPE_Q4_K:
  1634. case GGML_TYPE_Q5_K:
  1635. case GGML_TYPE_Q6_K:
  1636. break;
  1637. default:
  1638. return nullptr;
  1639. }
  1640. return b_type == GGML_TYPE_F32 ? ctx->device->pipeline_dequant_mul_mat_vec_f32_f32[a_type] : ctx->device->pipeline_dequant_mul_mat_vec_f16_f32[a_type];
  1641. }
  1642. 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) {
  1643. #ifdef GGML_VULKAN_DEBUG
  1644. std::cerr << "ggml_vk_get_mul_mat_mat_id_pipeline()" << std::endl;
  1645. #endif
  1646. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  1647. return ctx->device->pipeline_matmul_id_f32;
  1648. }
  1649. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  1650. return ctx->device->pipeline_matmul_id_f16_f32;
  1651. }
  1652. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  1653. return ctx->device->pipeline_matmul_id_f16;
  1654. }
  1655. GGML_ASSERT(src1_type == GGML_TYPE_F32);
  1656. switch (src0_type) {
  1657. case GGML_TYPE_Q4_0:
  1658. case GGML_TYPE_Q4_1:
  1659. case GGML_TYPE_Q5_0:
  1660. case GGML_TYPE_Q5_1:
  1661. case GGML_TYPE_Q8_0:
  1662. case GGML_TYPE_Q2_K:
  1663. case GGML_TYPE_Q3_K:
  1664. case GGML_TYPE_Q4_K:
  1665. case GGML_TYPE_Q5_K:
  1666. case GGML_TYPE_Q6_K:
  1667. break;
  1668. default:
  1669. return nullptr;
  1670. }
  1671. return ctx->device->pipeline_dequant_mul_mat_mat_id[src0_type];
  1672. }
  1673. static vk_pipeline ggml_vk_get_dequantize_mul_mat_vec_id(ggml_backend_vk_context * ctx, ggml_type a_type, ggml_type b_type) {
  1674. #ifdef GGML_VULKAN_DEBUG
  1675. std::cerr << "ggml_vk_get_dequantize_mul_mat_vec()" << std::endl;
  1676. #endif
  1677. GGML_ASSERT(b_type == GGML_TYPE_F32);
  1678. switch (a_type) {
  1679. case GGML_TYPE_F32:
  1680. case GGML_TYPE_F16:
  1681. case GGML_TYPE_Q4_0:
  1682. case GGML_TYPE_Q4_1:
  1683. case GGML_TYPE_Q5_0:
  1684. case GGML_TYPE_Q5_1:
  1685. case GGML_TYPE_Q8_0:
  1686. case GGML_TYPE_Q2_K:
  1687. case GGML_TYPE_Q3_K:
  1688. case GGML_TYPE_Q4_K:
  1689. case GGML_TYPE_Q5_K:
  1690. case GGML_TYPE_Q6_K:
  1691. break;
  1692. default:
  1693. return nullptr;
  1694. }
  1695. return ctx->device->pipeline_dequant_mul_mat_vec_id_f32[a_type];
  1696. }
  1697. static vk_buffer ggml_vk_pool_malloc(ggml_backend_vk_context * ctx, size_t size) {
  1698. #ifdef GGML_VULKAN_DEBUG
  1699. std::cerr << "ggml_vk_pool_malloc(" << size << ")" << std::endl;
  1700. #endif
  1701. int best_i = -1;
  1702. size_t best_size = std::numeric_limits<size_t>::max(); //smallest unused buffer that fits our needs
  1703. int worst_i = -1;
  1704. size_t worst_size = 0; //largest unused buffer seen so far
  1705. for (int i = 0; i < MAX_VK_BUFFERS; ++i) {
  1706. vk_buffer &b = ctx->buffer_pool[i];
  1707. if (b != nullptr && b->size >= size && b->size < best_size) {
  1708. best_i = i;
  1709. best_size = b->size;
  1710. }
  1711. if (b != nullptr && b->size > worst_size) {
  1712. worst_i = i;
  1713. worst_size = b->size;
  1714. }
  1715. }
  1716. if(best_i != -1) {
  1717. //found the smallest buffer that fits our needs
  1718. vk_buffer b = ctx->buffer_pool[best_i];
  1719. ctx->buffer_pool[best_i].reset();
  1720. return b;
  1721. }
  1722. if(worst_i != -1) {
  1723. //no buffer that fits our needs, resize largest one to save memory
  1724. vk_buffer& b = ctx->buffer_pool[worst_i];
  1725. ggml_vk_destroy_buffer(b);
  1726. }
  1727. return ggml_vk_create_buffer_check(ctx, size, vk::MemoryPropertyFlagBits::eDeviceLocal);
  1728. }
  1729. static void ggml_vk_pool_free(ggml_backend_vk_context * ctx, vk_buffer& buffer) {
  1730. #ifdef GGML_VULKAN_DEBUG
  1731. std::cerr << "ggml_vk_pool_free(" << buffer->size << ")" << std::endl;
  1732. #endif
  1733. for (int i = 0; i < MAX_VK_BUFFERS; ++i) {
  1734. vk_buffer& b = ctx->buffer_pool[i];
  1735. if (b == nullptr) {
  1736. b = buffer;
  1737. return;
  1738. }
  1739. }
  1740. std::cerr << "ggml_vulkan: WARNING: vk buffer pool full, increase MAX_VK_BUFFERS" << std::endl;
  1741. ggml_vk_destroy_buffer(buffer);
  1742. }
  1743. // Returns an available temporary buffer that may only be used temporarily, it will be reused
  1744. static vk_buffer ggml_vk_create_buffer_temp(ggml_backend_vk_context * ctx, size_t size) {
  1745. // Try to find existing temp buffer with enough capacity
  1746. for (auto& buffer : ctx->gc.temp_buffers) {
  1747. if (buffer->size >= size) {
  1748. return buffer;
  1749. }
  1750. }
  1751. // Otherwise create new buffer
  1752. vk_buffer buf = ggml_vk_pool_malloc(ctx, size);
  1753. ctx->gc.temp_buffers.push_back(buf);
  1754. return buf;
  1755. }
  1756. static void * ggml_vk_host_malloc(ggml_backend_vk_context * ctx, size_t size) {
  1757. #ifdef GGML_VULKAN_DEBUG
  1758. std::cerr << "ggml_vk_host_malloc(" << size << ")" << std::endl;
  1759. #endif
  1760. vk_buffer buf = ggml_vk_create_buffer(ctx, size,
  1761. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostCached,
  1762. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
  1763. if(!(buf->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible)) {
  1764. fprintf(stderr, "WARNING: failed to allocate %.2f MB of pinned memory\n",
  1765. size/1024.0/1024.0);
  1766. ctx->device->device.freeMemory(buf->device_memory);
  1767. ctx->device->device.destroyBuffer(buf->buffer);
  1768. return nullptr;
  1769. }
  1770. ctx->pinned_memory.push_back(std::make_tuple(buf->ptr, size, buf));
  1771. return buf->ptr;
  1772. }
  1773. static void ggml_vk_host_free(ggml_backend_vk_context * ctx, void* ptr) {
  1774. if (ptr == nullptr) {
  1775. return;
  1776. }
  1777. #ifdef GGML_VULKAN_DEBUG
  1778. std::cerr << "ggml_vk_host_free(" << ptr << ")" << std::endl;
  1779. #endif
  1780. vk_buffer buf;
  1781. size_t index;
  1782. for (size_t i = 0; i < ctx->pinned_memory.size(); i++) {
  1783. const uint8_t* addr = (const uint8_t*) std::get<0>(ctx->pinned_memory[i]);
  1784. const uint8_t* endr = addr + std::get<1>(ctx->pinned_memory[i]);
  1785. if (ptr >= addr && ptr < endr) {
  1786. buf = std::get<2>(ctx->pinned_memory[i]);
  1787. index = i;
  1788. break;
  1789. }
  1790. }
  1791. if (buf == nullptr) {
  1792. fprintf(stderr, "WARNING: failed to free pinned memory: memory not in map\n");
  1793. return;
  1794. }
  1795. ggml_vk_destroy_buffer(buf);
  1796. ctx->pinned_memory.erase(ctx->pinned_memory.begin() + index);
  1797. }
  1798. static void ggml_vk_host_get(ggml_backend_vk_context * ctx, const void * ptr, vk_buffer& buf, size_t& buf_offset) {
  1799. buf = nullptr;
  1800. buf_offset = 0;
  1801. for (size_t i = 0; i < ctx->pinned_memory.size(); i++) {
  1802. const uint8_t* addr = (const uint8_t*) std::get<0>(ctx->pinned_memory[i]);
  1803. const uint8_t* endr = addr + std::get<1>(ctx->pinned_memory[i]);
  1804. if (ptr >= addr && ptr < endr) {
  1805. buf = std::get<2>(ctx->pinned_memory[i]);
  1806. buf_offset = ((const uint8_t *)ptr) - addr;
  1807. break;
  1808. }
  1809. }
  1810. }
  1811. static vk_submission ggml_vk_begin_submission(ggml_backend_vk_context * ctx, vk_queue& q, bool one_time = true) {
  1812. vk_submission s;
  1813. s.buffer = ggml_vk_create_cmd_buffer(ctx, q);
  1814. if (one_time) {
  1815. s.buffer.begin({ vk::CommandBufferUsageFlagBits::eOneTimeSubmit });
  1816. } else {
  1817. s.buffer.begin({ vk::CommandBufferUsageFlags{} });
  1818. }
  1819. return s;
  1820. }
  1821. static void ggml_vk_dispatch_pipeline(ggml_backend_vk_context * ctx, vk_context * subctx, vk_pipeline& pipeline, std::vector<vk_subbuffer>&& buffers, size_t push_constant_size, const void* push_constants, std::array<uint32_t, 3> elements) {
  1822. const uint32_t wg0 = CEIL_DIV(elements[0], pipeline->wg_denoms[0]);
  1823. const uint32_t wg1 = CEIL_DIV(elements[1], pipeline->wg_denoms[1]);
  1824. const uint32_t wg2 = CEIL_DIV(elements[2], pipeline->wg_denoms[2]);
  1825. #ifdef GGML_VULKAN_DEBUG
  1826. std::cerr << "ggml_vk_dispatch_pipeline(" << pipeline->name << ", {";
  1827. for (auto& buffer : buffers) {
  1828. std::cerr << "(" << buffer.buffer << ", " << buffer.offset << ", " << buffer.size << "), ";
  1829. }
  1830. std::cerr << "}, (" << wg0 << "," << wg1 << "," << wg2 << "))" << std::endl;
  1831. #endif
  1832. std::vector<vk::DescriptorBufferInfo> descriptor_buffer_infos;
  1833. std::vector<vk::WriteDescriptorSet> write_descriptor_sets;
  1834. GGML_ASSERT(pipeline->descriptor_set_idx < pipeline->descriptor_sets.size());
  1835. GGML_ASSERT(buffers.size() == pipeline->parameter_count);
  1836. vk::DescriptorSet& descriptor_set = pipeline->descriptor_sets[pipeline->descriptor_set_idx++];
  1837. for (uint32_t i = 0; i < pipeline->parameter_count; i++) {
  1838. descriptor_buffer_infos.push_back({buffers[i].buffer->buffer, buffers[i].offset, buffers[i].size});
  1839. }
  1840. for (uint32_t i = 0; i < pipeline->parameter_count; i++) {
  1841. write_descriptor_sets.push_back({descriptor_set, i, 0, 1, vk::DescriptorType::eStorageBuffer, nullptr, &descriptor_buffer_infos[i]});
  1842. }
  1843. ctx->device->device.updateDescriptorSets(write_descriptor_sets, {});
  1844. subctx->s->buffer.pushConstants(pipeline->layout, vk::ShaderStageFlagBits::eCompute, 0, push_constant_size, push_constants);
  1845. subctx->s->buffer.bindPipeline(vk::PipelineBindPoint::eCompute, pipeline->pipeline);
  1846. subctx->s->buffer.bindDescriptorSets(vk::PipelineBindPoint::eCompute,
  1847. pipeline->layout,
  1848. 0,
  1849. { descriptor_set },
  1850. {});
  1851. subctx->s->buffer.dispatch(wg0, wg1, wg2);
  1852. }
  1853. static void ggml_vk_end_submission(vk_submission& s, std::vector<vk_semaphore> wait_semaphores, std::vector<vk_semaphore> signal_semaphores) {
  1854. s.buffer.end();
  1855. s.wait_semaphores = std::move(wait_semaphores);
  1856. s.signal_semaphores = std::move(signal_semaphores);
  1857. }
  1858. static void ggml_vk_ctx_end(vk_context * ctx) {
  1859. #ifdef GGML_VULKAN_DEBUG
  1860. std::cerr << "ggml_vk_ctx_end(" << ctx << ", " << ctx->seqs.size() << ")" << std::endl;
  1861. #endif
  1862. if (ctx->s == nullptr) {
  1863. return;
  1864. }
  1865. ctx->s->buffer.end();
  1866. ctx->s = nullptr;
  1867. }
  1868. static void ggml_vk_ctx_begin(ggml_backend_vk_context * ctx, vk_context * subctx) {
  1869. #ifdef GGML_VULKAN_DEBUG
  1870. std::cerr << "ggml_vk_ctx_begin(" << ctx << ")" << std::endl;
  1871. #endif
  1872. if (subctx->s != nullptr) {
  1873. ggml_vk_ctx_end(subctx);
  1874. }
  1875. subctx->seqs.push_back({ ggml_vk_begin_submission(ctx, *subctx->q) });
  1876. subctx->s = subctx->seqs[subctx->seqs.size() - 1].data();
  1877. }
  1878. static size_t ggml_vk_align_size(size_t width, size_t align) {
  1879. #ifdef GGML_VULKAN_DEBUG
  1880. std::cerr << "ggml_vk_align_size(" << width << ", " << align << ")" << std::endl;
  1881. #endif
  1882. return CEIL_DIV(width, align) * align;
  1883. }
  1884. static void deferred_memcpy(void * dst, const void * src, size_t size, std::vector<vk_staging_memcpy>* memcpys = nullptr) {
  1885. if (memcpys == nullptr) {
  1886. memcpy(dst, src, size);
  1887. } else {
  1888. memcpys->emplace_back(dst, src, size);
  1889. }
  1890. }
  1891. static void ggml_vk_ensure_sync_staging_buffer(ggml_backend_vk_context * ctx, size_t size) {
  1892. if (ctx->sync_staging == nullptr || ctx->sync_staging->size < size) {
  1893. ggml_vk_destroy_buffer(ctx->sync_staging);
  1894. ctx->sync_staging = ggml_vk_create_buffer_check(ctx, size,
  1895. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostCached,
  1896. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
  1897. }
  1898. }
  1899. 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) {
  1900. #ifdef GGML_VULKAN_DEBUG
  1901. std::cerr << "ggml_vk_buffer_write_nc_async(" << tensor << ")" << std::endl;
  1902. #endif
  1903. GGML_ASSERT(!ggml_is_contiguous(tensor));
  1904. // Buffer is already mapped
  1905. if(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  1906. std::cerr << "ggml_vulkan: buffer_write_nc_async dst buffer is host_visible. Use synchronous write." << std::endl;
  1907. GGML_ASSERT(false);
  1908. }
  1909. // Check if src is pinned memory
  1910. vk_buffer buf;
  1911. size_t buf_offset;
  1912. ggml_vk_host_get(ctx, tensor->data, buf, buf_offset);
  1913. const uint64_t ne0 = tensor->ne[0];
  1914. const uint64_t ne1 = tensor->ne[1];
  1915. const uint64_t ne2 = tensor->ne[2];
  1916. const uint64_t ne3 = tensor->ne[3];
  1917. const uint64_t nb0 = tensor->nb[0];
  1918. const uint64_t nb1 = tensor->nb[1];
  1919. const uint64_t nb2 = tensor->nb[2];
  1920. const uint64_t nb3 = tensor->nb[3];
  1921. const ggml_type type = tensor->type;
  1922. const uint64_t ts = ggml_type_size(type);
  1923. const uint64_t bs = ggml_blck_size(type);
  1924. const uint64_t dstnb0 = ts;
  1925. const uint64_t dstnb1 = dstnb0*(ne0/bs);
  1926. const uint64_t dstnb2 = dstnb1*ne1;
  1927. const uint64_t dstnb3 = dstnb2*ne2;
  1928. const uint64_t ne = ggml_nelements(tensor);
  1929. if (buf != nullptr) {
  1930. // Memory is pinned, use as staging buffer
  1931. std::vector<vk::BufferCopy> slices;
  1932. for (uint64_t i3 = 0; i3 < ne3; i3++) {
  1933. for (uint64_t i2 = 0; i2 < ne2; i2++) {
  1934. // Find longest contiguous slice
  1935. if (ne1*nb1 == dstnb2) {
  1936. slices.push_back({ buf_offset + i3*nb3 + i2*nb2, offset + i3*dstnb3 + i2*dstnb2, dstnb2 });
  1937. } else {
  1938. for (uint64_t i1 = 0; i1 < ne1; i1++) {
  1939. if (ne0*nb0/bs == dstnb1) {
  1940. slices.push_back({ buf_offset + i3*nb3 + i2*nb2 + i1*nb1, offset + i3*dstnb3 + i2*dstnb2 + i1*dstnb1, dstnb1 });
  1941. } else {
  1942. const uint64_t s_off = buf_offset + i3*nb3 + i2*nb2 + i1*nb1;
  1943. const uint64_t d_off = offset + i3*dstnb3 + i2*dstnb2 + i1*dstnb1;
  1944. for (uint64_t i0 = 0; i0 < ne0; i0++) {
  1945. slices.push_back({ s_off + i1*nb0, d_off + i0*dstnb0, dstnb0 });
  1946. }
  1947. }
  1948. }
  1949. }
  1950. }
  1951. }
  1952. ggml_vk_sync_buffers(subctx);
  1953. subctx->s->buffer.copyBuffer(buf->buffer, dst->buffer, slices);
  1954. return;
  1955. }
  1956. // Staging buffer required
  1957. vk_buffer staging = ctx->staging;
  1958. size_t staging_offset = ctx->staging_offset;
  1959. const size_t copy_size = ts*ne/bs;
  1960. if (ctx->staging->size < ctx->staging_offset + copy_size) {
  1961. if (sync_staging) {
  1962. // Create temporary larger buffer
  1963. ggml_vk_ensure_sync_staging_buffer(ctx, copy_size);
  1964. staging = ctx->sync_staging;
  1965. staging_offset = 0;
  1966. } else {
  1967. GGML_ASSERT(false);
  1968. }
  1969. }
  1970. VkBufferCopy buf_copy{ staging_offset, offset, copy_size };
  1971. ggml_vk_sync_buffers(subctx);
  1972. vkCmdCopyBuffer(subctx->s->buffer, staging->buffer, dst->buffer, 1, &buf_copy);
  1973. for (uint64_t i3 = 0; i3 < ne3; i3++) {
  1974. for (uint64_t i2 = 0; i2 < ne2; i2++) {
  1975. // Find longest contiguous slice
  1976. if (ne1*nb1 == dstnb2) {
  1977. deferred_memcpy((uint8_t *)staging->ptr + staging_offset + i3*dstnb3 + i2*dstnb2, (const uint8_t *) tensor->data + buf_offset + i3*nb3 + i2*nb2, dstnb2, &subctx->in_memcpys);
  1978. } else {
  1979. for (uint64_t i1 = 0; i1 < ne1; i1++) {
  1980. if (ne0*nb0/bs == dstnb1) {
  1981. deferred_memcpy((uint8_t *)staging->ptr + staging_offset + i3*dstnb3 + i2*dstnb2 + i1*dstnb1, (const uint8_t *) tensor->data + buf_offset + i3*nb3 + i2*nb2 + i1*nb1, dstnb1, &subctx->in_memcpys);
  1982. } else {
  1983. const uint64_t s_off = buf_offset + i3*nb3 + i2*nb2 + i1*nb1;
  1984. const uint64_t d_off = staging_offset + i3*dstnb3 + i2*dstnb2 + i1*dstnb1;
  1985. for (uint64_t i0 = 0; i0 < ne0; i0++) {
  1986. deferred_memcpy((uint8_t *)staging->ptr + d_off + i0*dstnb0, (const uint8_t *) tensor->data + s_off + i0*nb0, dstnb0, &subctx->in_memcpys);
  1987. }
  1988. }
  1989. }
  1990. }
  1991. }
  1992. }
  1993. }
  1994. static void ggml_vk_buffer_write_2d_async(ggml_backend_vk_context * ctx, 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) {
  1995. #ifdef GGML_VULKAN_DEBUG
  1996. std::cerr << "ggml_vk_buffer_write_2d_async(" << width << ", " << height << ")" << std::endl;
  1997. #endif
  1998. // Make sure ctx owns the buffer
  1999. GGML_ASSERT(dst->ctx == ctx);
  2000. // Buffer is already mapped
  2001. if(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  2002. std::cerr << "ggml_vulkan: buffer_write_async dst buffer is host_visible. Use synchronous write." << std::endl;
  2003. GGML_ASSERT(false);
  2004. }
  2005. // Check if src is pinned memory
  2006. vk_buffer buf = nullptr;
  2007. size_t buf_offset;
  2008. ggml_vk_host_get(ctx, src, buf, buf_offset);
  2009. if (buf != nullptr) {
  2010. // Memory is pinned, use as staging buffer
  2011. std::vector<vk::BufferCopy> slices(1);
  2012. if (width == spitch) {
  2013. // Only do single write if stride is equal
  2014. slices[0].srcOffset = buf_offset;
  2015. slices[0].dstOffset = offset;
  2016. slices[0].size = width * height;
  2017. } else {
  2018. slices.resize(height);
  2019. for (size_t i = 0; i < height; i++) {
  2020. slices[i].srcOffset = buf_offset + i * spitch;
  2021. slices[i].dstOffset = offset + i * width;
  2022. slices[i].size = width;
  2023. }
  2024. }
  2025. ggml_vk_sync_buffers(subctx);
  2026. subctx->s->buffer.copyBuffer(buf->buffer, dst->buffer, slices);
  2027. return;
  2028. }
  2029. #ifdef GGML_VULKAN_DEBUG
  2030. std::cerr << "STAGING" << std::endl;
  2031. #endif
  2032. // Staging buffer required
  2033. vk_buffer staging = ctx->staging;
  2034. size_t staging_offset = ctx->staging_offset;
  2035. const size_t copy_size = width*height;
  2036. if (ctx->staging == nullptr || ctx->staging->size < ctx->staging_offset + copy_size) {
  2037. if (sync_staging) {
  2038. ggml_vk_ensure_sync_staging_buffer(ctx, copy_size);
  2039. staging = ctx->sync_staging;
  2040. staging_offset = 0;
  2041. } else {
  2042. GGML_ASSERT(false);
  2043. }
  2044. }
  2045. VkBufferCopy buf_copy = {
  2046. staging_offset,
  2047. offset,
  2048. copy_size};
  2049. ggml_vk_sync_buffers(subctx);
  2050. vkCmdCopyBuffer(subctx->s->buffer, staging->buffer, dst->buffer, 1, &buf_copy);
  2051. if (width == spitch) {
  2052. deferred_memcpy((uint8_t *)staging->ptr + staging_offset, src, width * height, &subctx->in_memcpys);
  2053. } else {
  2054. for (size_t i = 0; i < height; i++) {
  2055. deferred_memcpy((uint8_t *)staging->ptr + staging_offset + i * width, (const uint8_t *) src + i * spitch, width, &subctx->in_memcpys);
  2056. }
  2057. }
  2058. }
  2059. static void ggml_vk_buffer_write_async(ggml_backend_vk_context * ctx, vk_context * subctx, vk_buffer& dst, size_t offset, const void * src, size_t size, bool sync_staging = false) {
  2060. #ifdef GGML_VULKAN_DEBUG
  2061. std::cerr << "ggml_vk_buffer_write_async(" << size << ")" << std::endl;
  2062. #endif
  2063. return ggml_vk_buffer_write_2d_async(ctx, subctx, dst, offset, src, size, size, 1, sync_staging);
  2064. }
  2065. static void ggml_vk_buffer_write_2d(ggml_backend_vk_context * ctx, vk_buffer& dst, size_t offset, const void * src, size_t spitch, size_t width, size_t height) {
  2066. #ifdef GGML_VULKAN_DEBUG
  2067. std::cerr << "ggml_vk_buffer_write_2d(" << width << ", " << height << ")" << std::endl;
  2068. #endif
  2069. // Buffer is already mapped
  2070. if(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  2071. GGML_ASSERT(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostCoherent);
  2072. for (size_t i = 0; i < height; i++) {
  2073. memcpy((uint8_t *)dst->ptr + offset + i * width, (const uint8_t *) src + i * spitch, width);
  2074. }
  2075. } else {
  2076. vk_context * subctx = ggml_vk_create_context(ctx, ctx->device->transfer_queue);
  2077. ggml_vk_ctx_begin(ctx, subctx);
  2078. ggml_vk_buffer_write_2d_async(ctx, subctx, dst, offset, src, spitch, width, height, true);
  2079. ggml_vk_ctx_end(subctx);
  2080. for (auto& cpy : subctx->in_memcpys) {
  2081. memcpy(cpy.dst, cpy.src, cpy.n);
  2082. }
  2083. ggml_vk_submit(subctx, ctx->fence);
  2084. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "vk_buffer_write_2d waitForFences");
  2085. ctx->device->device.resetFences({ ctx->fence });
  2086. }
  2087. }
  2088. static void ggml_vk_buffer_write(ggml_backend_vk_context * ctx, vk_buffer& dst, size_t offset, const void * src, size_t size) {
  2089. #ifdef GGML_VULKAN_DEBUG
  2090. std::cerr << "ggml_vk_buffer_write(" << size << ")" << std::endl;
  2091. #endif
  2092. ggml_vk_buffer_write_2d(ctx, dst, offset, src, 0, size, 1);
  2093. }
  2094. static void ggml_vk_buffer_read_2d_async(ggml_backend_vk_context * ctx, 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) {
  2095. #ifdef GGML_VULKAN_DEBUG
  2096. std::cerr << "ggml_vk_buffer_read_2d_async(offset=" << offset << ", width=" << width << ", height=" << height << ")" << std::endl;
  2097. #endif
  2098. GGML_ASSERT(width > 0);
  2099. GGML_ASSERT(height > 0);
  2100. GGML_ASSERT(src != nullptr);
  2101. // Make sure ctx owns the buffer
  2102. GGML_ASSERT(src->ctx == ctx);
  2103. // Check if dst is pinned memory
  2104. vk_buffer buf = nullptr;
  2105. size_t buf_offset;
  2106. ggml_vk_host_get(ctx, dst, buf, buf_offset);
  2107. std::vector<vk::BufferCopy> slices(1);
  2108. if (width == spitch && width == dpitch) {
  2109. // Only do single write if stride is equal
  2110. slices[0].srcOffset = offset;
  2111. slices[0].dstOffset = buf_offset;
  2112. slices[0].size = width * height;
  2113. } else {
  2114. slices.resize(height);
  2115. for (size_t i = 0; i < height; i++) {
  2116. slices[i].srcOffset = offset + i * spitch;
  2117. slices[i].dstOffset = buf_offset + i * dpitch;
  2118. slices[i].size = width;
  2119. }
  2120. }
  2121. if (buf != nullptr) {
  2122. // Memory is pinned, use as staging buffer
  2123. ggml_vk_sync_buffers(subctx);
  2124. subctx->s->buffer.copyBuffer(src->buffer, buf->buffer, slices);
  2125. return;
  2126. }
  2127. #ifdef GGML_VULKAN_DEBUG
  2128. std::cerr << "STAGING" << std::endl;
  2129. #endif
  2130. // Fall back to staging buffer
  2131. vk_buffer staging = ctx->staging;
  2132. const size_t copy_size = dpitch * height;
  2133. if (ctx->staging == nullptr || ctx->staging->size < ctx->staging_offset + copy_size) {
  2134. if (sync_staging) {
  2135. // Create temporary larger buffer
  2136. ggml_vk_ensure_sync_staging_buffer(ctx, copy_size);
  2137. staging = ctx->sync_staging;
  2138. } else {
  2139. GGML_ASSERT(false);
  2140. }
  2141. }
  2142. ggml_vk_sync_buffers(subctx);
  2143. subctx->s->buffer.copyBuffer(src->buffer, staging->buffer, slices);
  2144. deferred_memcpy(dst, staging->ptr, copy_size, &subctx->out_memcpys);
  2145. }
  2146. static void ggml_vk_buffer_read_async(ggml_backend_vk_context * ctx, vk_context * subctx, vk_buffer& src, size_t offset, void * dst, size_t size, bool sync_staging = false) {
  2147. return ggml_vk_buffer_read_2d_async(ctx, subctx, src, offset, dst, size, size, size, 1, sync_staging);
  2148. }
  2149. static void ggml_vk_buffer_read(ggml_backend_vk_context * ctx, vk_buffer& src, size_t offset, void * dst, size_t size) {
  2150. #ifdef GGML_VULKAN_DEBUG
  2151. std::cerr << "ggml_vk_buffer_read(" << offset << ", " << size << ")" << std::endl;
  2152. #endif
  2153. if(src->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  2154. GGML_ASSERT(src->memory_property_flags & vk::MemoryPropertyFlagBits::eHostCoherent);
  2155. memcpy(dst, (uint8_t *) src->ptr + offset, size);
  2156. } else {
  2157. vk_context * subctx = ggml_vk_create_context(ctx, ctx->device->transfer_queue);
  2158. ggml_vk_ctx_begin(ctx, subctx);
  2159. ggml_vk_buffer_read_async(ctx, subctx, src, offset, dst, size, true);
  2160. ggml_vk_ctx_end(subctx);
  2161. ggml_vk_submit(subctx, ctx->fence);
  2162. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "vk_buffer_read waitForFences");
  2163. ctx->device->device.resetFences({ ctx->fence });
  2164. for (auto& cpy : subctx->out_memcpys) {
  2165. memcpy(cpy.dst, cpy.src, cpy.n);
  2166. }
  2167. }
  2168. }
  2169. 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) {
  2170. #ifdef GGML_VULKAN_DEBUG
  2171. std::cerr << "ggml_vk_buffer_copy_async(" << size << ")" << std::endl;
  2172. #endif
  2173. // Make sure both buffers are on same ctx
  2174. GGML_ASSERT(src->ctx == dst->ctx);
  2175. VkBufferCopy bc{ src_offset, dst_offset, size };
  2176. vkCmdCopyBuffer(ctx->s->buffer, src->buffer, dst->buffer, 1, &bc);
  2177. }
  2178. static void ggml_vk_buffer_copy(vk_buffer& dst, size_t dst_offset, vk_buffer& src, size_t src_offset, size_t size) {
  2179. if (src->ctx == dst->ctx) {
  2180. #ifdef GGML_VULKAN_DEBUG
  2181. std::cerr << "ggml_vk_buffer_copy(SINGLE_DEVICE, " << size << ")" << std::endl;
  2182. #endif
  2183. // Copy within the device
  2184. ggml_backend_vk_context * ctx = src->ctx;
  2185. vk_context * subctx = ggml_vk_create_context(ctx, ctx->device->transfer_queue);
  2186. ggml_vk_ctx_begin(ctx, subctx);
  2187. ggml_vk_buffer_copy_async(subctx, dst, dst_offset, src, src_offset, size);
  2188. ggml_vk_ctx_end(subctx);
  2189. ggml_vk_submit(subctx, ctx->fence);
  2190. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "vk_buffer_copy waitForFences");
  2191. ctx->device->device.resetFences({ ctx->fence });
  2192. } else {
  2193. #ifdef GGML_VULKAN_DEBUG
  2194. std::cerr << "ggml_vk_buffer_copy(MULTI_DEVICE, " << size << ")" << std::endl;
  2195. #endif
  2196. // Copy device to device
  2197. ggml_backend_vk_context * src_ctx = src->ctx;
  2198. ggml_backend_vk_context * dst_ctx = dst->ctx;
  2199. ggml_vk_ensure_sync_staging_buffer(src_ctx, size);
  2200. ggml_vk_ensure_sync_staging_buffer(dst_ctx, size);
  2201. // Copy to src staging buffer
  2202. ggml_vk_buffer_copy(src_ctx->sync_staging, 0, src, src_offset, size);
  2203. // memcpy to dst staging buffer
  2204. memcpy(dst_ctx->sync_staging->ptr, src_ctx->sync_staging->ptr, size);
  2205. // Copy to dst buffer
  2206. ggml_vk_buffer_copy(dst, dst_offset, dst_ctx->sync_staging, 0, size);
  2207. }
  2208. }
  2209. static void ggml_vk_buffer_memset(ggml_backend_vk_context * ctx, vk_buffer& dst, size_t offset, uint32_t c, size_t size) {
  2210. #ifdef GGML_VULKAN_DEBUG
  2211. std::cerr << "ggml_vk_buffer_memset(" << offset << ", " << c << ", " << size << ")" << std::endl;
  2212. #endif
  2213. // Make sure ctx owns the buffer
  2214. GGML_ASSERT(dst->ctx == ctx);
  2215. vk_context * subctx = ggml_vk_create_context(ctx, ctx->device->transfer_queue);
  2216. ggml_vk_ctx_begin(ctx, subctx);
  2217. subctx->s->buffer.fillBuffer(dst->buffer, offset, size, c);
  2218. ggml_vk_ctx_end(subctx);
  2219. ggml_vk_submit(subctx, ctx->fence);
  2220. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "vk_memset waitForFences");
  2221. ctx->device->device.resetFences({ ctx->fence });
  2222. }
  2223. static void ggml_vk_h2d_tensor_2d(ggml_backend_vk_context * ctx, vk_context * subctx, vk_buffer& dst, size_t offset, const ggml_tensor * src, uint64_t i3, uint64_t i2, uint64_t i1) {
  2224. #ifdef GGML_VULKAN_DEBUG
  2225. std::cerr << "ggml_vk_h2d_tensor_2d(dst=" << dst << ", offset=" << offset << ", src=" << src << ", i3=" << i3 << ", i2=" << i2 << ", i1=" << i1 << ")" << std::endl;
  2226. #endif
  2227. const uint64_t ne0 = src->ne[0];
  2228. const uint64_t ne1 = src->ne[1];
  2229. const uint64_t nb0 = src->nb[0];
  2230. const uint64_t nb1 = src->nb[1];
  2231. const uint64_t nb2 = src->nb[2];
  2232. const uint64_t nb3 = src->nb[3];
  2233. const enum ggml_type type = src->type;
  2234. const size_t ts = ggml_type_size(type);
  2235. const size_t bs = ggml_blck_size(type);
  2236. const size_t row_length = ts*ne0/bs;
  2237. const void * x = (const void *) ((const char *) src->data + i2*nb2 + i3*nb3);
  2238. if (nb0 == ts && nb1 == row_length) {
  2239. return ggml_vk_buffer_write_async(ctx, subctx, dst, offset, x, i1*nb1);
  2240. }
  2241. if (nb0 == ts && (i1 == ne1 || !ggml_is_permuted(src))) {
  2242. return ggml_vk_buffer_write_2d_async(ctx, subctx, dst, offset, x, nb1, row_length, i1);
  2243. }
  2244. GGML_ASSERT(i3 == 0);
  2245. GGML_ASSERT(i2 == 0);
  2246. GGML_ASSERT(i1 == (uint64_t) ggml_nrows(src));
  2247. return ggml_vk_buffer_write_nc_async(ctx, subctx, dst, offset, src);
  2248. }
  2249. static void ggml_vk_d2h_tensor_2d(ggml_backend_vk_context * ctx, vk_context * subctx, vk_buffer& src, size_t offset, const ggml_tensor * dst) {
  2250. #ifdef GGML_VULKAN_DEBUG
  2251. std::cerr << "ggml_vk_d2h_tensor_2d()" << std::endl;
  2252. #endif
  2253. const uint64_t ne0 = dst->ne[0];
  2254. const uint64_t ne1 = dst->ne[1];
  2255. const uint64_t ne2 = dst->ne[2];
  2256. const uint64_t ne3 = dst->ne[3];
  2257. const uint64_t nb0 = dst->nb[0];
  2258. const uint64_t nb1 = dst->nb[1];
  2259. // const uint64_t nb2 = dst->nb[2];
  2260. // const uint64_t nb3 = dst->nb[3];
  2261. const enum ggml_type type = dst->type;
  2262. const size_t ts = ggml_type_size(type);
  2263. const size_t bs = ggml_blck_size(type);
  2264. const size_t row_length = ts*ne0/bs;
  2265. if (ggml_is_contiguous(dst)) {
  2266. return ggml_vk_buffer_read_async(ctx, subctx, src, offset, dst->data, ne1*nb1*ne2*ne3);
  2267. }
  2268. if (nb0 == ts) {
  2269. return ggml_vk_buffer_read_2d_async(ctx, subctx, src, offset, dst->data, nb1, nb1, row_length, ne1*ne2*ne3);
  2270. }
  2271. GGML_ASSERT(false);
  2272. }
  2273. static uint32_t ggml_vk_guess_split_k(int m, int n, int k) {
  2274. #ifdef GGML_VULKAN_DEBUG
  2275. std::cerr << "ggml_vk_guess_split_k(" << m << ", " << n << ", " << k << ")" << std::endl;
  2276. #endif
  2277. // if (k > 128 && (m < 128 || n < 128) && m > 2 && n > 2) {
  2278. // return 4;
  2279. // }
  2280. return 1;
  2281. GGML_UNUSED(m); GGML_UNUSED(n); GGML_UNUSED(k);
  2282. }
  2283. static vk_pipeline ggml_vk_guess_matmul_pipeline_amd(ggml_backend_vk_context * ctx, vk_matmul_pipeline& mmp, int m, int n, bool aligned) {
  2284. if (m <= 32 || n <= 32) {
  2285. return aligned ? mmp->a_s : mmp->s;
  2286. }
  2287. return aligned ? mmp->a_m : mmp->m;
  2288. GGML_UNUSED(ctx);
  2289. }
  2290. static vk_pipeline ggml_vk_guess_matmul_pipeline_apple(ggml_backend_vk_context * ctx, vk_matmul_pipeline& mmp, bool aligned) {
  2291. return aligned ? mmp->a_m : mmp->m;
  2292. GGML_UNUSED(ctx);
  2293. }
  2294. static vk_pipeline ggml_vk_guess_matmul_pipeline_intel(ggml_backend_vk_context * ctx, vk_matmul_pipeline& mmp, bool aligned) {
  2295. return aligned ? mmp->a_s : mmp->s;
  2296. GGML_UNUSED(ctx);
  2297. }
  2298. static vk_pipeline ggml_vk_guess_matmul_pipeline(ggml_backend_vk_context * ctx, vk_matmul_pipeline& mmp, int m, int n, bool aligned) {
  2299. #ifdef GGML_VULKAN_DEBUG
  2300. std::cerr << "ggml_vk_guess_matmul_pipeline(" << m << ", " << n << ", " << aligned << ")" << std::endl;
  2301. #endif
  2302. switch (ctx->device->vendor_id) {
  2303. case VK_VENDOR_ID_AMD:
  2304. return ggml_vk_guess_matmul_pipeline_amd(ctx, mmp, m, n, aligned);
  2305. case VK_VENDOR_ID_APPLE:
  2306. return ggml_vk_guess_matmul_pipeline_apple(ctx, mmp, aligned);
  2307. case VK_VENDOR_ID_INTEL:
  2308. return ggml_vk_guess_matmul_pipeline_intel(ctx, mmp, aligned);
  2309. default:
  2310. break;
  2311. }
  2312. if (m <= 32 || n <= 32) {
  2313. return aligned ? mmp->a_s : mmp->s;
  2314. }
  2315. if (m <= 64 || n <= 64) {
  2316. return aligned ? mmp->a_m : mmp->m;
  2317. }
  2318. return aligned ? mmp->a_l : mmp->l;
  2319. }
  2320. static uint32_t ggml_vk_guess_matmul_pipeline_align(ggml_backend_vk_context * ctx, vk_matmul_pipeline& mmp, int m, int n) {
  2321. #ifdef GGML_VULKAN_DEBUG
  2322. std::cerr << "ggml_vk_guess_matmul_pipeline_align(" << m << ", " << n << ")" << std::endl;
  2323. #endif
  2324. return ggml_vk_guess_matmul_pipeline(ctx, mmp, m, n, true)->align;
  2325. }
  2326. static void ggml_vk_matmul(
  2327. ggml_backend_vk_context * ctx, vk_context * subctx, vk_pipeline& pipeline,
  2328. vk_subbuffer&& a, vk_subbuffer&& b, vk_subbuffer&& d, vk_subbuffer&& split_k_buffer,
  2329. uint32_t m, uint32_t n, uint32_t k, uint32_t stride_a, uint32_t stride_b, uint32_t stride_d,
  2330. uint32_t batch_stride_a, uint32_t batch_stride_b, uint32_t batch_stride_d,
  2331. uint32_t split_k, uint32_t batch, uint32_t ne02, uint32_t ne12, uint32_t broadcast2, uint32_t broadcast3) {
  2332. #ifdef GGML_VULKAN_DEBUG
  2333. std::cerr << "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 << ")" << std::endl;
  2334. #endif
  2335. ggml_vk_sync_buffers(subctx);
  2336. if (split_k == 1) {
  2337. 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 };
  2338. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { a, b, d }, sizeof(vk_mat_mat_push_constants), &pc, { m, n, batch });
  2339. return;
  2340. }
  2341. GGML_ASSERT(batch_stride_d == m * n);
  2342. 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 };
  2343. // Make sure enough workgroups get assigned for split k to work
  2344. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { a, b, split_k_buffer }, sizeof(vk_mat_mat_push_constants), &pc1, { (CEIL_DIV(m, pipeline->wg_denoms[0]) * pipeline->wg_denoms[0]) * split_k, n, batch });
  2345. ggml_vk_sync_buffers(subctx);
  2346. const std::array<uint32_t, 2> pc2 = { (uint32_t)(m * n * batch), split_k };
  2347. ggml_vk_dispatch_pipeline(ctx, subctx, ctx->device->pipeline_matmul_split_k_reduce, { split_k_buffer, d }, pc2.size() * sizeof(uint32_t), pc2.data(), { m * n * batch, 1, 1 });
  2348. }
  2349. static void ggml_vk_matmul_id(
  2350. ggml_backend_vk_context * ctx, vk_context * subctx, vk_pipeline& pipeline,
  2351. vk_subbuffer&& a, vk_subbuffer&& b, vk_subbuffer&& d, vk_subbuffer&& ids,
  2352. uint32_t m, uint32_t n, uint32_t k, uint32_t stride_a, uint32_t stride_b, uint32_t stride_d,
  2353. uint32_t batch_stride_a, uint32_t batch_stride_b, uint32_t batch_stride_d,
  2354. uint32_t n_as, uint32_t nei0, uint32_t nei1, uint32_t nbi1, uint32_t ne11) {
  2355. #ifdef GGML_VULKAN_DEBUG
  2356. std::cerr << "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 << "), " <<
  2357. "m: " << m << ", n: " << n << ", k: " << k << ", stride_a: " << stride_a << ", stride_b: " << stride_b << ", stride_d: " << stride_d << ", " <<
  2358. "batch_stride_a: " << batch_stride_a << ", batch_stride_b: " << batch_stride_b << ", batch_stride_d: " << batch_stride_d << ", " <<
  2359. "n_as: " << n_as << ", nei0: " << nei0 << ", nei1: " << nei1 << ", nbi1: " << nbi1 << ", ne11: " << ne11 << ")" << std::endl;
  2360. #endif
  2361. ggml_vk_sync_buffers(subctx);
  2362. 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,
  2363. nei0, nei1, nbi1, ne11 };
  2364. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { a, b, d, ids }, sizeof(vk_mat_mat_id_push_constants), &pc, { m, nei1, n_as });
  2365. }
  2366. static bool ggml_vk_dim01_contiguous(const ggml_tensor * tensor) {
  2367. return
  2368. tensor->nb[0] == ggml_type_size(tensor->type) &&
  2369. tensor->nb[1] == (tensor->nb[0]*tensor->ne[0])/ggml_blck_size(tensor->type) &&
  2370. tensor->nb[3] == tensor->nb[2]*tensor->ne[2];
  2371. }
  2372. static vk_pipeline ggml_vk_get_cpy_pipeline(ggml_backend_vk_context * ctx, ggml_type from, ggml_type to) {
  2373. if (from == GGML_TYPE_F32 && to == GGML_TYPE_F32) {
  2374. return ctx->device->pipeline_cpy_f32_f32;
  2375. }
  2376. if (from == GGML_TYPE_F32 && to == GGML_TYPE_F16) {
  2377. return ctx->device->pipeline_cpy_f32_f16;
  2378. }
  2379. if (from == GGML_TYPE_F16 && to == GGML_TYPE_F16) {
  2380. return ctx->device->pipeline_cpy_f16_f16;
  2381. }
  2382. std::cerr << "Missing CPY op for types: " << ggml_type_name(from) << " " << ggml_type_name(to) << std::endl;
  2383. GGML_ASSERT(false);
  2384. }
  2385. 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) {
  2386. #ifdef GGML_VULKAN_DEBUG
  2387. std::cerr << "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] << "), ";
  2388. std::cerr << "buffer in size=" << in.buffer->size << ", buffer out size=" << out.buffer->size << ")" << std::endl;
  2389. #endif
  2390. const int tensor_type_size = ggml_type_size(tensor->type);
  2391. const uint32_t ne = ggml_nelements(tensor);
  2392. const vk_op_unary_push_constants pc = {
  2393. (uint32_t)ne,
  2394. (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,
  2395. (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]),
  2396. 0,
  2397. 0.0f, 0.0f,
  2398. };
  2399. ggml_vk_sync_buffers(subctx);
  2400. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { in, out }, sizeof(vk_op_unary_push_constants), &pc, { ne, 1, 1 });
  2401. }
  2402. 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) {
  2403. #ifdef GGML_VULKAN_DEBUG
  2404. std::cerr << "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];
  2405. 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];
  2406. 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] << "),)" << std::endl;
  2407. #endif
  2408. GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16); // NOLINT
  2409. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  2410. const uint64_t ne00 = src0->ne[0];
  2411. const uint64_t ne01 = src0->ne[1];
  2412. const uint64_t ne02 = src0->ne[2];
  2413. const uint64_t ne03 = src0->ne[3];
  2414. const uint64_t ne10 = src1->ne[0];
  2415. const uint64_t ne11 = src1->ne[1];
  2416. const uint64_t ne12 = src1->ne[2];
  2417. const uint64_t ne13 = src1->ne[3];
  2418. const uint64_t ne20 = dst->ne[0];
  2419. const uint64_t ne21 = dst->ne[1];
  2420. const uint64_t r2 = ne12 / ne02;
  2421. const uint64_t r3 = ne13 / ne03;
  2422. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) dst->extra;
  2423. ggml_tensor_extra_gpu * extra_src0 = (ggml_tensor_extra_gpu *) src0->extra;
  2424. ggml_tensor_extra_gpu * extra_src1 = (ggml_tensor_extra_gpu *) src1->extra;
  2425. vk_buffer d_Qx;
  2426. size_t qx_buf_offset = 0;
  2427. vk_buffer d_Qy;
  2428. size_t qy_buf_offset = 0;
  2429. bool src0_uma = false;
  2430. bool src1_uma = false;
  2431. if (ctx->device->uma) {
  2432. ggml_vk_host_get(ctx, src0->data, d_Qx, qx_buf_offset);
  2433. ggml_vk_host_get(ctx, src1->data, d_Qy, qy_buf_offset);
  2434. src0_uma = d_Qx != nullptr;
  2435. src1_uma = d_Qy != nullptr;
  2436. }
  2437. const bool x_non_contig = !ggml_vk_dim01_contiguous(src0);
  2438. const bool y_non_contig = !ggml_vk_dim01_contiguous(src1);
  2439. const bool y_f32_kernel = src1->type == GGML_TYPE_F32 && !y_non_contig;
  2440. vk_matmul_pipeline mmp = ggml_vk_get_mul_mat_mat_pipeline(ctx, src0->type, y_non_contig ? GGML_TYPE_F16 : src1->type);
  2441. const bool qx_needs_dequant = mmp == nullptr || x_non_contig;
  2442. const bool qy_needs_dequant = (src1->type != GGML_TYPE_F16 && !y_f32_kernel) || y_non_contig;
  2443. if (mmp == nullptr) {
  2444. // Fall back to dequant + f16 mulmat
  2445. mmp = ggml_vk_get_mul_mat_mat_pipeline(ctx, GGML_TYPE_F16, y_f32_kernel ? GGML_TYPE_F32 : GGML_TYPE_F16);
  2446. }
  2447. // Not implemented
  2448. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  2449. const int x_ne = ne01 * ne00;
  2450. const int y_ne = ne11 * ne10;
  2451. const int d_ne = ne11 * ne01;
  2452. const uint32_t kpad = ggml_vk_align_size(ne10, ggml_vk_guess_matmul_pipeline_align(ctx, mmp, ne01, ne11));
  2453. const bool aligned = ne10 == kpad && ne01 > 8 && ne11 > 8;
  2454. const uint32_t split_k = ggml_vk_guess_split_k(ne01, ne11, ne10);
  2455. vk_pipeline pipeline = ggml_vk_guess_matmul_pipeline(ctx, mmp, ne01, ne11, aligned);
  2456. const uint64_t qx_sz = ggml_type_size(src0->type) * x_ne / ggml_blck_size(src0->type);
  2457. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  2458. const uint64_t x_sz = !qx_needs_dequant ? qx_sz : sizeof(ggml_fp16_t) * x_ne;
  2459. const uint64_t y_sz = y_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne;
  2460. const uint64_t d_sz = sizeof(float) * d_ne;
  2461. vk_buffer d_D = extra->buffer_gpu.lock();
  2462. const uint64_t d_buf_offset = extra->offset + dst->view_offs;
  2463. GGML_ASSERT(d_D != nullptr);
  2464. GGML_ASSERT(d_D->size >= d_buf_offset + d_sz * ne02 * ne03);
  2465. vk_buffer d_X;
  2466. uint64_t x_buf_offset = 0;
  2467. vk_buffer d_Y;
  2468. uint64_t y_buf_offset = 0;
  2469. if (!src0_uma) {
  2470. d_Qx = extra_src0->buffer_gpu.lock();
  2471. qx_buf_offset = extra_src0->offset + src0->view_offs;
  2472. GGML_ASSERT(d_Qx != nullptr);
  2473. }
  2474. if (!src1_uma) {
  2475. d_Qy = extra_src1->buffer_gpu.lock();
  2476. qy_buf_offset = extra_src1->offset + src1->view_offs;
  2477. GGML_ASSERT(d_Qy != nullptr);
  2478. }
  2479. if (qx_needs_dequant) {
  2480. d_X = ctx->prealloc_x;
  2481. GGML_ASSERT(d_X->size >= x_sz * ne02 * ne03);
  2482. } else {
  2483. d_X = d_Qx;
  2484. x_buf_offset = qx_buf_offset;
  2485. GGML_ASSERT(qx_sz == x_sz);
  2486. }
  2487. if (qy_needs_dequant) {
  2488. d_Y = ctx->prealloc_y;
  2489. GGML_ASSERT(d_Y->size >= y_sz * ne02 * ne03);
  2490. } else {
  2491. d_Y = d_Qy;
  2492. y_buf_offset = qy_buf_offset;
  2493. GGML_ASSERT(qy_sz == y_sz);
  2494. }
  2495. vk_pipeline to_fp16_vk_0 = nullptr;
  2496. vk_pipeline to_fp16_vk_1 = nullptr;
  2497. if (x_non_contig) {
  2498. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0->type, GGML_TYPE_F16);
  2499. } else {
  2500. to_fp16_vk_0 = ggml_vk_get_to_fp16(ctx, src0->type);
  2501. }
  2502. if (y_non_contig) {
  2503. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1->type, GGML_TYPE_F16);
  2504. } else {
  2505. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  2506. }
  2507. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  2508. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  2509. // Allocate descriptor sets
  2510. ggml_pipeline_allocate_descriptor_sets(ctx, pipeline, 1);
  2511. if (qx_needs_dequant) {
  2512. ggml_pipeline_allocate_descriptor_sets(ctx, to_fp16_vk_0, 1);
  2513. }
  2514. if (qy_needs_dequant) {
  2515. ggml_pipeline_allocate_descriptor_sets(ctx, to_fp16_vk_1, 1);
  2516. }
  2517. if (split_k > 1) {
  2518. ggml_pipeline_allocate_descriptor_sets(ctx, ctx->device->pipeline_matmul_split_k_reduce, 1);
  2519. }
  2520. if (x_non_contig) {
  2521. 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 });
  2522. } else if (qx_needs_dequant) {
  2523. const std::vector<uint32_t> pc = { (uint32_t)ne01, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)(ggml_nelements(src0)) };
  2524. ggml_vk_sync_buffers(subctx);
  2525. ggml_vk_dispatch_pipeline(ctx, subctx, to_fp16_vk_0, { { d_Qx, qx_buf_offset, qx_sz * ne02 * ne03 }, { d_X, 0, x_sz * ne02 * ne03 } }, pc.size() * sizeof(uint32_t), pc.data(), { (uint32_t)(x_ne * ne02 * ne03), 1, 1});
  2526. }
  2527. if (y_non_contig) {
  2528. 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 });
  2529. }
  2530. uint32_t stride_batch_x = ne00*ne01;
  2531. uint32_t stride_batch_y = ne10*ne11;
  2532. if (!ggml_vk_dim01_contiguous(src0) && !qx_needs_dequant) {
  2533. stride_batch_x = src0->nb[0] / ggml_type_size(src0->type);
  2534. }
  2535. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant) {
  2536. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  2537. }
  2538. // compute
  2539. ggml_vk_matmul(
  2540. ctx, subctx, pipeline,
  2541. { d_X, x_buf_offset, x_sz * ne02 * ne03 }, { d_Y, y_buf_offset, y_sz * ne12 * ne13 },
  2542. { d_D, d_buf_offset, d_sz * ne12 * ne13 }, { ctx->prealloc_split_k, 0, d_sz * ne12 * ne13 * split_k },
  2543. ne01, ne11, ne10,
  2544. ne10, ne10, ne01, stride_batch_x, stride_batch_y, ne20*ne21,
  2545. split_k, ne12*ne13, ne02, ne12, r2, r3
  2546. ); // NOLINT
  2547. }
  2548. 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) {
  2549. #ifdef GGML_VULKAN_DEBUG
  2550. std::cerr << "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];
  2551. 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];
  2552. 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] << "),)" << std::endl;
  2553. #endif
  2554. GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16); // NOLINT
  2555. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  2556. const uint64_t ne00 = src0->ne[0];
  2557. const uint64_t ne01 = src0->ne[1];
  2558. const uint64_t ne02 = src0->ne[2];
  2559. const uint64_t ne03 = src0->ne[3];
  2560. const uint64_t ne10 = src1->ne[0];
  2561. const uint64_t ne11 = src1->ne[1];
  2562. const uint64_t ne12 = src1->ne[2];
  2563. const uint64_t ne13 = src1->ne[3];
  2564. GGML_ASSERT(ne11 == 1);
  2565. const uint64_t ne20 = dst->ne[0];
  2566. const uint64_t ne21 = dst->ne[1];
  2567. const uint64_t ne22 = dst->ne[2];
  2568. const uint64_t ne23 = dst->ne[3];
  2569. const uint64_t r2 = ne12 / ne02;
  2570. const uint64_t r3 = ne13 / ne03;
  2571. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) dst->extra;
  2572. ggml_tensor_extra_gpu * extra_src0 = (ggml_tensor_extra_gpu *) src0->extra;
  2573. ggml_tensor_extra_gpu * extra_src1 = (ggml_tensor_extra_gpu *) src1->extra;
  2574. vk_buffer d_Qx;
  2575. size_t qx_buf_offset = 0;
  2576. vk_buffer d_Qy;
  2577. size_t qy_buf_offset = 0;
  2578. bool src0_uma = false;
  2579. bool src1_uma = false;
  2580. if (ctx->device->uma) {
  2581. ggml_vk_host_get(ctx, src0->data, d_Qx, qx_buf_offset);
  2582. ggml_vk_host_get(ctx, src1->data, d_Qy, qy_buf_offset);
  2583. src0_uma = d_Qx != nullptr;
  2584. src1_uma = d_Qy != nullptr;
  2585. }
  2586. const bool x_non_contig = !ggml_vk_dim01_contiguous(src0);
  2587. const bool y_non_contig = !ggml_vk_dim01_contiguous(src1);
  2588. const bool f16_f32_kernel = src1->type == GGML_TYPE_F32;
  2589. const bool qx_needs_dequant = x_non_contig;
  2590. const bool qy_needs_dequant = (src1->type != GGML_TYPE_F16 && !f16_f32_kernel) || y_non_contig;
  2591. // Not implemented
  2592. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  2593. const uint64_t x_ne = ne01 * ne00;
  2594. const uint64_t y_ne = ne11 * ne10;
  2595. const uint64_t d_ne = ne11 * ne01;
  2596. 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);
  2597. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  2598. 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;
  2599. const uint64_t y_sz = f16_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne;
  2600. const uint64_t d_sz = sizeof(float) * d_ne;
  2601. vk_buffer d_D = extra->buffer_gpu.lock();
  2602. const uint64_t d_buf_offset = extra->offset + dst->view_offs;
  2603. GGML_ASSERT(d_D != nullptr);
  2604. vk_buffer d_X;
  2605. uint64_t x_buf_offset = 0;
  2606. vk_buffer d_Y;
  2607. uint64_t y_buf_offset = 0;
  2608. if(!src0_uma) {
  2609. d_Qx = extra_src0->buffer_gpu.lock();
  2610. qx_buf_offset = extra_src0->offset + src0->view_offs;
  2611. GGML_ASSERT(d_Qx != nullptr);
  2612. }
  2613. if(!src1_uma) {
  2614. d_Qy = extra_src1->buffer_gpu.lock();
  2615. qy_buf_offset = extra_src1->offset + src1->view_offs;
  2616. GGML_ASSERT(d_Qy != nullptr);
  2617. }
  2618. if (qx_needs_dequant) {
  2619. d_X = ctx->prealloc_x;
  2620. } else {
  2621. d_X = d_Qx;
  2622. x_buf_offset = qx_buf_offset;
  2623. GGML_ASSERT(qx_sz == x_sz);
  2624. }
  2625. if (qy_needs_dequant) {
  2626. d_Y = ctx->prealloc_y;
  2627. } else {
  2628. d_Y = d_Qy;
  2629. y_buf_offset = qy_buf_offset;
  2630. GGML_ASSERT(qy_sz == y_sz);
  2631. }
  2632. vk_pipeline to_fp16_vk_0 = nullptr;
  2633. vk_pipeline to_fp16_vk_1 = nullptr;
  2634. if (x_non_contig) {
  2635. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0->type, src0->type);
  2636. }
  2637. if (y_non_contig) {
  2638. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1->type, src1->type);
  2639. } else {
  2640. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  2641. }
  2642. vk_pipeline dmmv = ggml_vk_get_dequantize_mul_mat_vec(ctx, src0->type, src1->type);
  2643. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  2644. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  2645. GGML_ASSERT(dmmv != nullptr);
  2646. // Allocate descriptor sets
  2647. if (qx_needs_dequant) {
  2648. ggml_pipeline_allocate_descriptor_sets(ctx, to_fp16_vk_0, 1);
  2649. }
  2650. if (qy_needs_dequant) {
  2651. ggml_pipeline_allocate_descriptor_sets(ctx, to_fp16_vk_1, y_non_contig ? 1 : ne12 * ne13);
  2652. }
  2653. ggml_pipeline_allocate_descriptor_sets(ctx, dmmv, ne12 * ne13);
  2654. if (x_non_contig) {
  2655. GGML_ASSERT(x_sz == ggml_vk_align_size(ggml_type_size(src0->type) * x_ne, ctx->device->properties.limits.minStorageBufferOffsetAlignment));
  2656. 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 });
  2657. }
  2658. if (y_non_contig) {
  2659. GGML_ASSERT(y_sz == ggml_type_size(src1->type) * y_ne);
  2660. 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 });
  2661. }
  2662. uint32_t stride_batch_x = ne00*ne01;
  2663. uint32_t stride_batch_y = ne10*ne11;
  2664. if (!ggml_vk_dim01_contiguous(src0) && !qx_needs_dequant) {
  2665. stride_batch_x = src0->nb[0] / ggml_type_size(src0->type);
  2666. }
  2667. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant) {
  2668. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  2669. }
  2670. // compute
  2671. const vk_mat_vec_push_constants pc = {
  2672. (uint32_t)ne00, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne01,
  2673. stride_batch_x, stride_batch_y, (uint32_t)(ne20*ne21),
  2674. (uint32_t)ne02, (uint32_t)ne12, (uint32_t)r2, (uint32_t)r3,
  2675. };
  2676. ggml_vk_sync_buffers(subctx);
  2677. ggml_vk_dispatch_pipeline(ctx, subctx, dmmv, { { d_X, x_buf_offset, x_sz * ne02 * ne03 }, { d_Y, y_buf_offset, y_sz * ne12 * ne13 }, { d_D, d_buf_offset, d_sz * ne22 * ne23} }, sizeof(vk_mat_vec_push_constants), &pc, { (uint32_t)ne01, (uint32_t)(ne12 * ne13), 1});
  2678. }
  2679. 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) {
  2680. #ifdef GGML_VULKAN_DEBUG
  2681. std::cerr << "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];
  2682. 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];
  2683. 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] << "),)" << std::endl;
  2684. #endif
  2685. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  2686. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // NOLINT
  2687. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // NOLINT
  2688. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  2689. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  2690. const uint64_t ne00 = src0->ne[0];
  2691. const uint64_t ne01 = src0->ne[1];
  2692. const uint64_t ne02 = src0->ne[2];
  2693. // const uint64_t ne03 = src0->ne[3];
  2694. const uint64_t ne10 = src1->ne[0];
  2695. const uint64_t ne11 = src1->ne[1];
  2696. const uint64_t ne12 = src1->ne[2];
  2697. // const uint64_t ne13 = src1->ne[3];
  2698. GGML_ASSERT(ne11 == 1);
  2699. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) dst->extra;
  2700. ggml_tensor_extra_gpu * extra_src0 = (ggml_tensor_extra_gpu *) src0->extra;
  2701. ggml_tensor_extra_gpu * extra_src1 = (ggml_tensor_extra_gpu *) src1->extra;
  2702. vk_buffer d_Qy;
  2703. size_t qy_buf_offset = 0;
  2704. bool src1_uma = false;
  2705. if (ctx->device->uma) {
  2706. ggml_vk_host_get(ctx, src1->data, d_Qy, qy_buf_offset);
  2707. src1_uma = d_Qy != nullptr;
  2708. }
  2709. const uint64_t x_ne = ne00 * ne01 * ne02;
  2710. const uint64_t y_ne = ne10 * ne11 * ne12;
  2711. const uint64_t d_ne = ne01 * ne11 * ne12;
  2712. 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);
  2713. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  2714. const uint64_t d_sz = sizeof(float) * d_ne;
  2715. vk_buffer d_D = extra->buffer_gpu.lock();
  2716. const uint64_t d_buf_offset = extra->offset + dst->view_offs;
  2717. GGML_ASSERT(d_D != nullptr);
  2718. vk_buffer d_Qx = extra_src0->buffer_gpu.lock();
  2719. const uint64_t qx_buf_offset = extra_src0->offset + src0->view_offs;
  2720. GGML_ASSERT(d_Qx != nullptr);
  2721. if (!src1_uma) {
  2722. d_Qy = extra_src1->buffer_gpu.lock();
  2723. qy_buf_offset = extra_src1->offset + src1->view_offs;
  2724. GGML_ASSERT(d_Qx != nullptr);
  2725. }
  2726. // Allocate descriptor sets
  2727. ggml_pipeline_allocate_descriptor_sets(ctx, ctx->device->pipeline_mul_mat_vec_p021_f16_f32, 1);
  2728. const uint64_t qy_buffer_offset = (qy_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  2729. const uint64_t qy_shader_offset = qy_buf_offset - qy_buffer_offset;
  2730. const uint64_t d_buffer_offset = (d_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  2731. const uint64_t d_shader_offset = d_buf_offset - d_buffer_offset;
  2732. // compute
  2733. 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)) };
  2734. ggml_vk_sync_buffers(subctx);
  2735. ggml_vk_dispatch_pipeline(ctx, subctx, ctx->device->pipeline_mul_mat_vec_p021_f16_f32, { { d_Qx, qx_buf_offset, qx_sz }, { d_Qy, qy_buffer_offset, qy_sz + qy_shader_offset }, { d_D, d_buffer_offset, d_sz + d_shader_offset } }, 6 * sizeof(uint32_t), &pc, { 1, (uint32_t)ne01, (uint32_t)ne12 });
  2736. }
  2737. 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) {
  2738. #ifdef GGML_VULKAN_DEBUG
  2739. std::cerr << "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];
  2740. 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];
  2741. 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] << "),)" << std::endl;
  2742. #endif
  2743. GGML_ASSERT(!ggml_is_transposed(src0));
  2744. GGML_ASSERT(!ggml_is_transposed(src1));
  2745. GGML_ASSERT(!ggml_is_permuted(src0));
  2746. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  2747. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  2748. const uint64_t ne00 = src0->ne[0];
  2749. const uint64_t ne01 = src0->ne[1];
  2750. const uint64_t ne02 = src0->ne[2];
  2751. // const uint64_t ne03 = src0->ne[3];
  2752. const uint64_t nb01 = src0->nb[1];
  2753. const uint64_t nb02 = src0->nb[2];
  2754. // const uint64_t ne10 = src1->ne[0];
  2755. const uint64_t ne11 = src1->ne[1];
  2756. const uint64_t ne12 = src1->ne[2];
  2757. // const uint64_t ne13 = src1->ne[3];
  2758. GGML_ASSERT(ne11 == 1);
  2759. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) dst->extra;
  2760. ggml_tensor_extra_gpu * extra_src0 = (ggml_tensor_extra_gpu *) src0->extra;
  2761. ggml_tensor_extra_gpu * extra_src1 = (ggml_tensor_extra_gpu *) src1->extra;
  2762. vk_buffer d_Qy = nullptr;
  2763. size_t qy_buf_offset = 0;
  2764. bool src1_uma = false;
  2765. if (ctx->device->uma) {
  2766. ggml_vk_host_get(ctx, src1->data, d_Qy, qy_buf_offset);
  2767. src1_uma = d_Qy != nullptr;
  2768. }
  2769. const uint64_t d_ne = ne01 * ne11 * ne12;
  2770. const uint32_t row_stride_x = nb01 / sizeof(ggml_fp16_t);
  2771. const uint32_t channel_stride_x = nb02 / sizeof(ggml_fp16_t);
  2772. const uint64_t qx_sz = ggml_nbytes(src0);
  2773. const uint64_t qy_sz = ggml_nbytes(src1);
  2774. const uint64_t d_sz = sizeof(float) * d_ne;
  2775. vk_buffer d_D = extra->buffer_gpu.lock();
  2776. const uint64_t d_buf_offset = extra->offset + dst->view_offs;
  2777. GGML_ASSERT(d_D != nullptr);
  2778. vk_buffer d_Qx = extra_src0->buffer_gpu.lock();
  2779. const uint64_t qx_buf_offset = extra_src0->offset + src0->view_offs;
  2780. GGML_ASSERT(d_Qx != nullptr);
  2781. if (!src1_uma) {
  2782. d_Qy = extra_src1->buffer_gpu.lock();
  2783. qy_buf_offset = extra_src1->offset + src1->view_offs;
  2784. GGML_ASSERT(d_Qx != nullptr);
  2785. }
  2786. // Allocate descriptor sets
  2787. ggml_pipeline_allocate_descriptor_sets(ctx, ctx->device->pipeline_mul_mat_vec_nc_f16_f32, 1);
  2788. const uint64_t qy_buffer_offset = (qy_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  2789. const uint64_t qy_shader_offset = qy_buf_offset - qy_buffer_offset;
  2790. const uint64_t d_buffer_offset = (d_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  2791. const uint64_t d_shader_offset = d_buf_offset - d_buffer_offset;
  2792. // compute
  2793. const std::array<uint32_t, 7> pc = { (uint32_t)ne00, (uint32_t)ne01, row_stride_x, channel_stride_x, (uint32_t)(ne12 / ne02), (uint32_t)(qy_shader_offset / ggml_type_size(src1->type)), (uint32_t)(d_shader_offset / ggml_type_size(dst->type)) };
  2794. ggml_vk_sync_buffers(subctx);
  2795. ggml_vk_dispatch_pipeline(ctx, subctx, ctx->device->pipeline_mul_mat_vec_nc_f16_f32, { { d_Qx, qx_buf_offset, qx_sz }, { d_Qy, qy_buffer_offset, qy_sz + qy_shader_offset }, { d_D, d_buffer_offset, d_sz + d_shader_offset } }, 7 * sizeof(uint32_t), &pc, { 1, (uint32_t)ne01, (uint32_t)ne12 });
  2796. }
  2797. 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) {
  2798. #ifdef GGML_VULKAN_DEBUG
  2799. std::cerr << "ggml_vk_mul_mat(" << src0 << ", " << src1 << ", " << dst << ")" << std::endl;
  2800. #endif
  2801. if (src0->type == GGML_TYPE_F16 && ggml_is_permuted(src0) && ggml_is_permuted(src1) && dst->ne[1] == 1) {
  2802. ggml_vk_mul_mat_vec_p021_f16_f32(ctx, subctx, src0, src1, dst);
  2803. } else if (src0->type == GGML_TYPE_F16 && !ggml_is_contiguous(src0) && !ggml_is_transposed(src1) && dst->ne[1] == 1) {
  2804. ggml_vk_mul_mat_vec_nc_f16_f32(ctx, subctx, src0, src1, dst);
  2805. } else if (dst->ne[1] == 1 && (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type))) {
  2806. ggml_vk_mul_mat_vec_q_f16(ctx, subctx, src0, src1, dst);
  2807. } else {
  2808. ggml_vk_mul_mat_q_f16(ctx, subctx, src0, src1, dst);
  2809. }
  2810. }
  2811. 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) {
  2812. #ifdef GGML_VULKAN_DEBUG
  2813. std::cerr << "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];
  2814. 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];
  2815. 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];
  2816. 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] << "),)" << std::endl;
  2817. #endif
  2818. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  2819. GGML_ASSERT(ids->type == GGML_TYPE_I32);
  2820. const uint64_t ne00 = src0->ne[0];
  2821. const uint64_t ne01 = src0->ne[1];
  2822. const uint64_t ne02 = src0->ne[2];
  2823. const uint64_t ne03 = src0->ne[3];
  2824. const uint64_t ne10 = src1->ne[0];
  2825. const uint64_t ne11 = src1->ne[1];
  2826. const uint64_t ne12 = src1->ne[2];
  2827. const uint64_t ne13 = src1->ne[3];
  2828. const uint64_t nei0 = ids->ne[0];
  2829. const uint64_t nei1 = ids->ne[1];
  2830. GGML_ASSERT(nei0 * nei1 <= 2048);
  2831. const uint32_t nbi1 = ids->nb[1];
  2832. const uint32_t nbi2 = ids->nb[2];
  2833. const uint64_t ne20 = dst->ne[0];
  2834. const uint64_t ne21 = dst->ne[1];
  2835. const uint64_t ne22 = dst->ne[2];
  2836. const uint64_t ne23 = dst->ne[3];
  2837. const uint64_t n_as = ne02;
  2838. GGML_ASSERT(n_as <= 8);
  2839. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) dst->extra;
  2840. ggml_tensor_extra_gpu * extra_src0 = (ggml_tensor_extra_gpu *) src0->extra;
  2841. ggml_tensor_extra_gpu * extra_src1 = (ggml_tensor_extra_gpu *) src1->extra;
  2842. ggml_tensor_extra_gpu * extra_ids = (ggml_tensor_extra_gpu *) ids->extra;
  2843. vk_buffer d_Qx;
  2844. size_t qx_buf_offset = 0;
  2845. vk_buffer d_Qy;
  2846. size_t qy_buf_offset = 0;
  2847. vk_buffer d_ids;
  2848. size_t ids_buf_offset = 0;
  2849. bool src0_uma = false;
  2850. bool src1_uma = false;
  2851. bool ids_uma = false;
  2852. if (ctx->device->uma) {
  2853. ggml_vk_host_get(ctx, src0->data, d_Qx, qx_buf_offset);
  2854. ggml_vk_host_get(ctx, src1->data, d_Qy, qy_buf_offset);
  2855. ggml_vk_host_get(ctx, ids->data, d_ids, ids_buf_offset);
  2856. src0_uma = d_Qx != nullptr;
  2857. src1_uma = d_Qy != nullptr;
  2858. ids_uma = d_ids != nullptr;
  2859. }
  2860. const bool x_non_contig = !ggml_vk_dim01_contiguous(src0);
  2861. const bool y_non_contig = !ggml_vk_dim01_contiguous(src1);
  2862. const bool y_f32_kernel = src1->type == GGML_TYPE_F32 && !y_non_contig;
  2863. vk_matmul_pipeline mmp = ggml_vk_get_mul_mat_mat_id_pipeline(ctx, src0->type, y_non_contig ? GGML_TYPE_F16 : src1->type);
  2864. const bool qx_needs_dequant = mmp == nullptr || x_non_contig;
  2865. const bool qy_needs_dequant = (src1->type != GGML_TYPE_F16 && !y_f32_kernel) || y_non_contig;
  2866. if (mmp == nullptr) {
  2867. GGML_ASSERT(false);
  2868. }
  2869. // Not implemented
  2870. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  2871. const uint64_t x_ne = ne01 * ne00;
  2872. const uint64_t y_ne = ne11 * ne10;
  2873. const uint64_t d_ne = ne21 * ne20;
  2874. const uint32_t kpad = ggml_vk_align_size(ne10, ggml_vk_guess_matmul_pipeline_align(ctx, mmp, ne01, nei1));
  2875. const bool aligned = ne10 == kpad && ne01 > 8 && nei1 > 8;
  2876. vk_pipeline pipeline = ggml_vk_guess_matmul_pipeline(ctx, mmp, ne01, nei1, aligned);
  2877. const uint64_t qx_sz = ggml_type_size(src0->type) * x_ne / ggml_blck_size(src0->type);
  2878. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  2879. const uint64_t x_sz = !qx_needs_dequant ? qx_sz : sizeof(ggml_fp16_t) * x_ne;
  2880. const uint64_t y_sz = y_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne;
  2881. const uint64_t ids_sz = nbi2;
  2882. const uint64_t d_sz = sizeof(float) * d_ne;
  2883. vk_buffer d_D = extra->buffer_gpu.lock();
  2884. const uint64_t d_buf_offset = extra->offset + dst->view_offs;
  2885. GGML_ASSERT(d_D != nullptr);
  2886. vk_buffer d_X;
  2887. uint64_t x_buf_offset = 0;
  2888. vk_buffer d_Y;
  2889. uint64_t y_buf_offset = 0;
  2890. if (!src0_uma) {
  2891. d_Qx = extra_src0->buffer_gpu.lock();
  2892. qx_buf_offset = extra_src0->offset + src0->view_offs;
  2893. GGML_ASSERT(d_Qx != nullptr);
  2894. }
  2895. if (!src1_uma) {
  2896. d_Qy = extra_src1->buffer_gpu.lock();
  2897. qy_buf_offset = extra_src1->offset + src1->view_offs;
  2898. GGML_ASSERT(d_Qy != nullptr);
  2899. }
  2900. if (!ids_uma) {
  2901. d_ids = extra_ids->buffer_gpu.lock();
  2902. ids_buf_offset = extra_ids->offset + ids->view_offs;
  2903. GGML_ASSERT(d_ids != nullptr);
  2904. }
  2905. if (qx_needs_dequant) {
  2906. d_X = ctx->prealloc_x;
  2907. GGML_ASSERT(d_X->size >= x_sz * ne02 * ne03);
  2908. } else {
  2909. d_X = d_Qx;
  2910. x_buf_offset = qx_buf_offset;
  2911. GGML_ASSERT(qx_sz == x_sz);
  2912. }
  2913. if (qy_needs_dequant) {
  2914. d_Y = ctx->prealloc_y;
  2915. GGML_ASSERT(d_Y->size >= y_sz * ne02 * ne03);
  2916. } else {
  2917. d_Y = d_Qy;
  2918. y_buf_offset = qy_buf_offset;
  2919. GGML_ASSERT(qy_sz == y_sz);
  2920. }
  2921. vk_pipeline to_fp16_vk_0 = nullptr;
  2922. vk_pipeline to_fp16_vk_1 = nullptr;
  2923. if (x_non_contig) {
  2924. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0->type, GGML_TYPE_F16);
  2925. } else {
  2926. to_fp16_vk_0 = ggml_vk_get_to_fp16(ctx, src0->type);
  2927. }
  2928. if (y_non_contig) {
  2929. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1->type, GGML_TYPE_F16);
  2930. } else {
  2931. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  2932. }
  2933. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  2934. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  2935. // Allocate descriptor sets
  2936. ggml_pipeline_allocate_descriptor_sets(ctx, pipeline, 1);
  2937. if (qx_needs_dequant) {
  2938. ggml_pipeline_allocate_descriptor_sets(ctx, to_fp16_vk_0, 1);
  2939. }
  2940. if (qy_needs_dequant) {
  2941. ggml_pipeline_allocate_descriptor_sets(ctx, to_fp16_vk_1, 1);
  2942. }
  2943. if (x_non_contig) {
  2944. 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 });
  2945. } else if (qx_needs_dequant) {
  2946. const std::vector<uint32_t> pc = { (uint32_t)ne01, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)(ggml_nelements(src0)) };
  2947. ggml_vk_sync_buffers(subctx);
  2948. ggml_vk_dispatch_pipeline(ctx, subctx, to_fp16_vk_0, { { d_Qx, qx_buf_offset, qx_sz * ne02 * ne03 }, { d_X, 0, x_sz * ne02 * ne03 } }, pc.size() * sizeof(uint32_t), pc.data(), { (uint32_t)(x_ne * ne02 * ne03), 1, 1});
  2949. }
  2950. if (y_non_contig) {
  2951. 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 });
  2952. }
  2953. uint32_t stride_batch_x = ne00*ne01;
  2954. uint32_t stride_batch_y = ne10*ne11;
  2955. if (!ggml_vk_dim01_contiguous(src0) && !qx_needs_dequant) {
  2956. stride_batch_x = src0->nb[0] / ggml_type_size(src0->type);
  2957. }
  2958. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant) {
  2959. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  2960. }
  2961. // compute
  2962. ggml_vk_matmul_id(
  2963. ctx, subctx, pipeline,
  2964. { d_X, x_buf_offset, x_sz * ne02 * ne03 }, { d_Y, y_buf_offset, y_sz * ne12 * ne13 },
  2965. { d_D, d_buf_offset, d_sz * ne22 * ne23 }, { d_ids, ids_buf_offset, ids_sz },
  2966. ne01, ne21, ne10, ne10, ne10, ne01,
  2967. stride_batch_x, stride_batch_y, ne20*ne21,
  2968. n_as, nei0, nei1, nbi1 / ggml_type_size(ids->type), ne11
  2969. ); // NOLINT
  2970. }
  2971. 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) {
  2972. #ifdef GGML_VULKAN_DEBUG
  2973. std::cerr << "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];
  2974. 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];
  2975. 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];
  2976. 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] << "),)" << std::endl;
  2977. #endif
  2978. GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16); // NOLINT
  2979. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  2980. GGML_ASSERT(ids->type == GGML_TYPE_I32);
  2981. const uint64_t ne00 = src0->ne[0];
  2982. const uint64_t ne01 = src0->ne[1];
  2983. const uint64_t ne02 = src0->ne[2];
  2984. const uint64_t ne03 = src0->ne[3];
  2985. const uint64_t ne10 = src1->ne[0];
  2986. const uint64_t ne11 = src1->ne[1];
  2987. const uint64_t ne12 = src1->ne[2];
  2988. const uint64_t ne13 = src1->ne[3];
  2989. const uint64_t nei0 = ids->ne[0];
  2990. const uint64_t nei1 = ids->ne[1];
  2991. const uint64_t nbi2 = ids->nb[2];
  2992. GGML_ASSERT(nei1 == 1);
  2993. const uint64_t ne20 = dst->ne[0];
  2994. const uint64_t ne21 = dst->ne[1];
  2995. const uint64_t ne22 = dst->ne[2];
  2996. const uint64_t ne23 = dst->ne[3];
  2997. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) dst->extra;
  2998. ggml_tensor_extra_gpu * extra_src0 = (ggml_tensor_extra_gpu *) src0->extra;
  2999. ggml_tensor_extra_gpu * extra_src1 = (ggml_tensor_extra_gpu *) src1->extra;
  3000. ggml_tensor_extra_gpu * extra_ids = (ggml_tensor_extra_gpu *) ids->extra;
  3001. vk_buffer d_Qx;
  3002. size_t qx_buf_offset = 0;
  3003. vk_buffer d_Qy;
  3004. size_t qy_buf_offset = 0;
  3005. vk_buffer d_ids;
  3006. size_t ids_buf_offset = 0;
  3007. bool src0_uma = false;
  3008. bool src1_uma = false;
  3009. bool ids_uma = false;
  3010. if (ctx->device->uma) {
  3011. ggml_vk_host_get(ctx, src0->data, d_Qx, qx_buf_offset);
  3012. ggml_vk_host_get(ctx, src1->data, d_Qy, qy_buf_offset);
  3013. ggml_vk_host_get(ctx, ids->data, d_ids, ids_buf_offset);
  3014. src0_uma = d_Qx != nullptr;
  3015. src1_uma = d_Qy != nullptr;
  3016. ids_uma = d_ids != nullptr;
  3017. }
  3018. const bool x_non_contig = !ggml_vk_dim01_contiguous(src0);
  3019. const bool y_non_contig = !ggml_vk_dim01_contiguous(src1);
  3020. const bool f16_f32_kernel = src1->type == GGML_TYPE_F32;
  3021. const bool qx_needs_dequant = x_non_contig;
  3022. const bool qy_needs_dequant = (src1->type != GGML_TYPE_F16 && !f16_f32_kernel) || y_non_contig;
  3023. // Not implemented
  3024. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  3025. const uint64_t x_ne = ne01 * ne00;
  3026. const uint64_t y_ne = ne11 * ne10;
  3027. const uint64_t d_ne = ne21 * ne20;
  3028. 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);
  3029. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  3030. 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;
  3031. const uint64_t y_sz = f16_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne;
  3032. const uint64_t ids_sz = nbi2;
  3033. const uint64_t d_sz = sizeof(float) * d_ne;
  3034. vk_buffer d_D = extra->buffer_gpu.lock();
  3035. const uint64_t d_buf_offset = extra->offset + dst->view_offs;
  3036. GGML_ASSERT(d_D != nullptr);
  3037. vk_buffer d_X;
  3038. uint64_t x_buf_offset = 0;
  3039. vk_buffer d_Y;
  3040. uint64_t y_buf_offset = 0;
  3041. if(!src0_uma) {
  3042. d_Qx = extra_src0->buffer_gpu.lock();
  3043. qx_buf_offset = extra_src0->offset + src0->view_offs;
  3044. GGML_ASSERT(d_Qx != nullptr);
  3045. }
  3046. if(!src1_uma) {
  3047. d_Qy = extra_src1->buffer_gpu.lock();
  3048. qy_buf_offset = extra_src1->offset + src1->view_offs;
  3049. GGML_ASSERT(d_Qy != nullptr);
  3050. }
  3051. if(!ids_uma) {
  3052. d_ids = extra_ids->buffer_gpu.lock();
  3053. ids_buf_offset = extra_ids->offset + ids->view_offs;
  3054. GGML_ASSERT(d_ids != nullptr);
  3055. }
  3056. if (qx_needs_dequant) {
  3057. d_X = ctx->prealloc_x;
  3058. } else {
  3059. d_X = d_Qx;
  3060. x_buf_offset = qx_buf_offset;
  3061. GGML_ASSERT(qx_sz == x_sz);
  3062. }
  3063. if (qy_needs_dequant) {
  3064. d_Y = ctx->prealloc_y;
  3065. } else {
  3066. d_Y = d_Qy;
  3067. y_buf_offset = qy_buf_offset;
  3068. GGML_ASSERT(qy_sz == y_sz);
  3069. }
  3070. vk_pipeline to_fp16_vk_0 = nullptr;
  3071. vk_pipeline to_fp16_vk_1 = nullptr;
  3072. if (x_non_contig) {
  3073. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0->type, src0->type);
  3074. }
  3075. if (y_non_contig) {
  3076. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1->type, src1->type);
  3077. } else {
  3078. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  3079. }
  3080. vk_pipeline dmmv = ggml_vk_get_dequantize_mul_mat_vec_id(ctx, src0->type, src1->type);
  3081. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  3082. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  3083. GGML_ASSERT(dmmv != nullptr);
  3084. // Allocate descriptor sets
  3085. if (qx_needs_dequant) {
  3086. ggml_pipeline_allocate_descriptor_sets(ctx, to_fp16_vk_0, 1);
  3087. }
  3088. if (qy_needs_dequant) {
  3089. ggml_pipeline_allocate_descriptor_sets(ctx, to_fp16_vk_1, y_non_contig ? 1 : ne12 * ne13);
  3090. }
  3091. ggml_pipeline_allocate_descriptor_sets(ctx, dmmv, ne12 * ne13);
  3092. if (x_non_contig) {
  3093. GGML_ASSERT(x_sz == ggml_vk_align_size(ggml_type_size(src0->type) * x_ne, ctx->device->properties.limits.minStorageBufferOffsetAlignment));
  3094. 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 });
  3095. }
  3096. if (y_non_contig) {
  3097. GGML_ASSERT(y_sz == ggml_type_size(src1->type) * y_ne);
  3098. 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 });
  3099. }
  3100. uint32_t stride_batch_y = ne10*ne11;
  3101. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant) {
  3102. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  3103. }
  3104. // compute
  3105. const vk_mat_vec_id_push_constants pc = {
  3106. (uint32_t)ne00, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne01,
  3107. (uint32_t)x_ne, stride_batch_y, (uint32_t)(ne20*ne21),
  3108. (uint32_t)nei0, (uint32_t)ne11,
  3109. };
  3110. ggml_vk_sync_buffers(subctx);
  3111. ggml_vk_dispatch_pipeline(ctx, subctx, dmmv,
  3112. { { d_X, x_buf_offset, x_sz * ne02 * ne03 }, { d_Y, y_buf_offset, y_sz * ne12 * ne13 }, { d_D, d_buf_offset, d_sz * ne22 * ne23}, { d_ids, ids_buf_offset, ids_sz } },
  3113. sizeof(vk_mat_vec_id_push_constants), &pc, { (uint32_t)ne01, (uint32_t)nei0, 1 });
  3114. }
  3115. 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) {
  3116. #ifdef GGML_VULKAN_DEBUG
  3117. std::cerr << "ggml_vk_mul_mat_id(" << src0 << ", " << src1 << ", " << src2 << ", " << dst << ")" << std::endl;
  3118. #endif
  3119. if (src2->ne[1] == 1 && (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type))) {
  3120. ggml_vk_mul_mat_vec_id_q_f16(ctx, subctx, src0, src1, src2, dst);
  3121. } else {
  3122. ggml_vk_mul_mat_id_q_f16(ctx, subctx, src0, src1, src2, dst);
  3123. }
  3124. }
  3125. static void ggml_vk_op_repeat(ggml_backend_vk_context * ctx, vk_context * subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3126. // guaranteed to be an integer due to the check in ggml_can_repeat
  3127. const uint64_t ne0 = dst->ne[0];
  3128. const uint64_t ne1 = dst->ne[1];
  3129. const uint64_t ne2 = dst->ne[2];
  3130. const uint64_t ne3 = dst->ne[3];
  3131. const uint64_t ne00 = src0->ne[0];
  3132. const uint64_t ne01 = src0->ne[1];
  3133. const uint64_t ne02 = src0->ne[2];
  3134. const uint64_t ne03 = src0->ne[3];
  3135. const uint64_t nb0 = dst->nb[0];
  3136. const uint64_t nb1 = dst->nb[1];
  3137. const uint64_t nb2 = dst->nb[2];
  3138. const uint64_t nb3 = dst->nb[3];
  3139. const uint64_t nb00 = src0->nb[0];
  3140. const uint64_t nb01 = src0->nb[1];
  3141. const uint64_t nb02 = src0->nb[2];
  3142. const uint64_t nb03 = src0->nb[3];
  3143. const uint64_t nr0 = ne0/ne00;
  3144. const uint64_t nr1 = ne1/ne01;
  3145. const uint64_t nr2 = ne2/ne02;
  3146. const uint64_t nr3 = ne3/ne03;
  3147. // TODO: support for transposed / permuted tensors
  3148. GGML_ASSERT(nb0 == sizeof(float));
  3149. GGML_ASSERT(nb00 == sizeof(float));
  3150. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) dst->extra;
  3151. ggml_tensor_extra_gpu * extra_src0 = (ggml_tensor_extra_gpu *) src0->extra;
  3152. const vk_buffer src_buf = extra_src0->buffer_gpu.lock();
  3153. const uint64_t src_offset = extra_src0->offset + src0->view_offs;
  3154. vk_buffer dst_buf = extra->buffer_gpu.lock();
  3155. const uint64_t dst_offset = extra->offset + dst->view_offs;
  3156. std::vector<vk::BufferCopy> copies;
  3157. for (uint64_t i3 = 0; i3 < nr3; i3++) {
  3158. for (uint64_t k3 = 0; k3 < ne03; k3++) {
  3159. for (uint64_t i2 = 0; i2 < nr2; i2++) {
  3160. for (uint64_t k2 = 0; k2 < ne02; k2++) {
  3161. for (uint64_t i1 = 0; i1 < nr1; i1++) {
  3162. for (uint64_t k1 = 0; k1 < ne01; k1++) {
  3163. for (uint64_t i0 = 0; i0 < nr0; i0++) {
  3164. copies.push_back({
  3165. src_offset + (i3*ne03 + k3)*nb3 + (i2*ne02 + k2)*nb2 + (i1*ne01 + k1)*nb1 + (i0*ne00)*nb0,
  3166. dst_offset + ( k3)*nb03 + ( k2)*nb02 + ( k1)*nb01,
  3167. ne00*nb0,
  3168. });
  3169. }
  3170. }
  3171. }
  3172. }
  3173. }
  3174. }
  3175. }
  3176. ggml_vk_sync_buffers(subctx);
  3177. subctx->s->buffer.copyBuffer(src_buf->buffer, dst_buf->buffer, copies);
  3178. GGML_UNUSED(ctx);
  3179. GGML_UNUSED(src1);
  3180. }
  3181. 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) {
  3182. switch (op) {
  3183. case GGML_OP_ADD:
  3184. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  3185. return ctx->device->pipeline_add_f32;
  3186. }
  3187. return nullptr;
  3188. case GGML_OP_GET_ROWS:
  3189. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  3190. if (dst->type == GGML_TYPE_F16) {
  3191. return ctx->device->pipeline_get_rows[src0->type];
  3192. }
  3193. if (dst->type == GGML_TYPE_F32) {
  3194. return ctx->device->pipeline_get_rows_f32[src0->type];
  3195. }
  3196. return nullptr;
  3197. case GGML_OP_MUL:
  3198. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  3199. return ctx->device->pipeline_mul_f32;
  3200. }
  3201. return nullptr;
  3202. case GGML_OP_DIV:
  3203. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  3204. return ctx->device->pipeline_div_f32;
  3205. }
  3206. return nullptr;
  3207. case GGML_OP_SCALE:
  3208. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  3209. return ctx->device->pipeline_scale_f32;
  3210. }
  3211. return nullptr;
  3212. case GGML_OP_SQR:
  3213. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  3214. return ctx->device->pipeline_sqr_f32;
  3215. }
  3216. return nullptr;
  3217. case GGML_OP_CLAMP:
  3218. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  3219. return ctx->device->pipeline_clamp_f32;
  3220. }
  3221. return nullptr;
  3222. case GGML_OP_CPY:
  3223. case GGML_OP_CONT:
  3224. case GGML_OP_DUP:
  3225. return ggml_vk_get_cpy_pipeline(ctx, src0->type, dst->type);
  3226. case GGML_OP_NORM:
  3227. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  3228. return ctx->device->pipeline_norm_f32;
  3229. }
  3230. return nullptr;
  3231. case GGML_OP_RMS_NORM:
  3232. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  3233. return ctx->device->pipeline_rms_norm_f32;
  3234. }
  3235. return nullptr;
  3236. case GGML_OP_UNARY:
  3237. switch (ggml_get_unary_op(dst)) {
  3238. case GGML_UNARY_OP_SILU:
  3239. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  3240. return ctx->device->pipeline_silu_f32;
  3241. }
  3242. break;
  3243. case GGML_UNARY_OP_GELU:
  3244. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  3245. return ctx->device->pipeline_gelu_f32;
  3246. }
  3247. break;
  3248. case GGML_UNARY_OP_RELU:
  3249. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  3250. return ctx->device->pipeline_relu_f32;
  3251. }
  3252. break;
  3253. default:
  3254. break;
  3255. }
  3256. return nullptr;
  3257. case GGML_OP_DIAG_MASK_INF:
  3258. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  3259. return ctx->device->pipeline_diag_mask_inf_f32;
  3260. }
  3261. return nullptr;
  3262. case GGML_OP_SOFT_MAX:
  3263. GGML_ASSERT(!src1 || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16);
  3264. if (src0->type == GGML_TYPE_F32 && (src1 == nullptr || src1->type == GGML_TYPE_F32) && dst->type == GGML_TYPE_F32) {
  3265. return ctx->device->pipeline_soft_max_f32;
  3266. }
  3267. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  3268. return ctx->device->pipeline_soft_max_f32_f16;
  3269. }
  3270. return nullptr;
  3271. case GGML_OP_ROPE:
  3272. {
  3273. const int mode = ((const int32_t *) dst->op_params)[2];
  3274. const bool is_neox = mode & 2;
  3275. if (is_neox) {
  3276. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  3277. return ctx->device->pipeline_rope_neox_f32;
  3278. }
  3279. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  3280. return ctx->device->pipeline_rope_neox_f16;
  3281. }
  3282. } else {
  3283. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  3284. return ctx->device->pipeline_rope_norm_f32;
  3285. }
  3286. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  3287. return ctx->device->pipeline_rope_norm_f16;
  3288. }
  3289. }
  3290. return nullptr;
  3291. }
  3292. case GGML_OP_ARGSORT:
  3293. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_I32) {
  3294. return ctx->device->pipeline_argsort_f32;
  3295. }
  3296. return nullptr;
  3297. case GGML_OP_SUM_ROWS:
  3298. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  3299. return ctx->device->pipeline_sum_rows_f32;
  3300. }
  3301. return nullptr;
  3302. default:
  3303. return nullptr;
  3304. }
  3305. GGML_UNUSED(src2);
  3306. }
  3307. static ggml_vk_func_t ggml_vk_op_get_func(ggml_op op) {
  3308. switch(op) {
  3309. case GGML_OP_REPEAT:
  3310. return ggml_vk_op_repeat;
  3311. default:
  3312. return nullptr;
  3313. }
  3314. }
  3315. static bool ggml_vk_op_supports_incontiguous(ggml_op op) {
  3316. switch (op) {
  3317. case GGML_OP_CPY:
  3318. case GGML_OP_GET_ROWS:
  3319. case GGML_OP_ADD:
  3320. case GGML_OP_MUL:
  3321. case GGML_OP_DIV:
  3322. case GGML_OP_SCALE:
  3323. case GGML_OP_SQR:
  3324. case GGML_OP_CLAMP:
  3325. return true;
  3326. default:
  3327. return false;
  3328. }
  3329. }
  3330. template<typename PC>
  3331. 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, const PC&& pc) {
  3332. #ifdef GGML_VULKAN_DEBUG
  3333. std::cerr << "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];
  3334. if (src1 != nullptr) {
  3335. 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];
  3336. }
  3337. if (src2 != nullptr) {
  3338. 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];
  3339. }
  3340. 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] << "), " << ggml_op_name(op) << ")" << std::endl;
  3341. #endif
  3342. GGML_ASSERT(op == GGML_OP_GET_ROWS || (!ggml_is_quantized(src0->type) && (src1 == nullptr || !ggml_is_quantized(src1->type)))); // NOLINT
  3343. GGML_ASSERT(ggml_vk_op_supports_incontiguous(op) || ggml_vk_dim01_contiguous(src0)); // NOLINT
  3344. GGML_ASSERT(dst->extra != nullptr);
  3345. const uint64_t ne00 = src0->ne[0];
  3346. const uint64_t ne01 = src0->ne[1];
  3347. const uint64_t ne02 = src0->ne[2];
  3348. const uint64_t ne03 = src0->ne[3];
  3349. const uint64_t ne0 = ne00 * ne01;
  3350. const bool use_src1 = src1 != nullptr;
  3351. const uint64_t ne10 = use_src1 ? src1->ne[0] : 0;
  3352. const uint64_t ne11 = use_src1 ? src1->ne[1] : 0;
  3353. const uint64_t ne12 = use_src1 ? src1->ne[2] : 0;
  3354. const uint64_t ne13 = use_src1 ? src1->ne[3] : 0;
  3355. const uint64_t ne1 = ne10 * ne11;
  3356. // const uint64_t nb10 = use_src1 ? src1->nb[0] : 0;
  3357. const bool use_src2 = src2 != nullptr;
  3358. const uint64_t ne20 = use_src2 ? src2->ne[0] : 0;
  3359. const uint64_t ne21 = use_src2 ? src2->ne[1] : 0;
  3360. const uint64_t ne22 = use_src2 ? src2->ne[2] : 0;
  3361. const uint64_t ne23 = use_src2 ? src2->ne[3] : 0;
  3362. const uint64_t ne2 = ne20 * ne21;
  3363. const uint64_t ned0 = dst->ne[0];
  3364. const uint64_t ned1 = dst->ne[1];
  3365. const uint64_t ned2 = dst->ne[2];
  3366. const uint64_t ned3 = dst->ne[3];
  3367. const uint64_t ned = ned0 * ned1;
  3368. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, src0, src1, src2, dst, op);
  3369. ggml_vk_func_t op_func;
  3370. if (pipeline == nullptr) {
  3371. op_func = ggml_vk_op_get_func(op);
  3372. if (op_func == nullptr) {
  3373. std::cerr << "ggml_vulkan: Error: Missing op: " << ggml_op_name(op) << " for " << ggml_type_name(src0->type);
  3374. if (src1 != nullptr) {
  3375. std::cerr << " and " << ggml_type_name(src1->type);
  3376. }
  3377. std::cerr << " to " << ggml_type_name(dst->type) << std::endl;
  3378. GGML_ASSERT(false);
  3379. }
  3380. op_func(ctx, subctx, src0, src1, dst);
  3381. return;
  3382. }
  3383. const bool op_supports_incontiguous = ggml_vk_op_supports_incontiguous(op);
  3384. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) dst->extra;
  3385. ggml_tensor_extra_gpu * extra_src0 = (ggml_tensor_extra_gpu *) src0->extra;
  3386. ggml_tensor_extra_gpu * extra_src1 = use_src1 ? (ggml_tensor_extra_gpu *) src1->extra : nullptr;
  3387. ggml_tensor_extra_gpu * extra_src2 = use_src2 ? (ggml_tensor_extra_gpu *) src2->extra : nullptr;
  3388. vk_buffer d_X = nullptr;
  3389. size_t x_buf_offset = 0;
  3390. vk_buffer d_Y = nullptr;
  3391. size_t y_buf_offset = 0;
  3392. vk_buffer d_Z = nullptr;
  3393. size_t z_buf_offset = 0;
  3394. bool src0_uma = false;
  3395. bool src1_uma = false;
  3396. bool src2_uma = false;
  3397. if (ctx->device->uma) {
  3398. ggml_vk_host_get(ctx, src0->data, d_X, x_buf_offset);
  3399. src0_uma = d_X != nullptr;
  3400. if (use_src1) {
  3401. ggml_vk_host_get(ctx, src1->data, d_Y, y_buf_offset);
  3402. src1_uma = d_Y != nullptr;
  3403. }
  3404. if (use_src2) {
  3405. ggml_vk_host_get(ctx, src2->data, d_Z, z_buf_offset);
  3406. src2_uma = d_Z != nullptr;
  3407. }
  3408. }
  3409. uint64_t x_sz = ggml_type_size(src0->type)/ggml_blck_size(src0->type) * ne0;
  3410. uint64_t y_sz = use_src1 ? ggml_type_size(src1->type) * ne1 : 0;
  3411. uint64_t z_sz = use_src2 ? ggml_type_size(src2->type) * ne2 : 0;
  3412. uint64_t d_sz = ggml_type_size(dst->type) * ned;
  3413. vk_buffer d_D = extra->buffer_gpu.lock();
  3414. // Workaround for tiny tensor inputs on ROPE
  3415. if (use_src1 && y_sz > d_D->size) {
  3416. y_sz = VK_WHOLE_SIZE;
  3417. }
  3418. GGML_ASSERT(d_D != nullptr);
  3419. uint64_t d_buf_offset = ((extra->offset + dst->view_offs) / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  3420. GGML_ASSERT(d_buf_offset == extra->offset || op == GGML_OP_CPY); // NOLINT
  3421. if(!src0_uma) {
  3422. d_X = extra_src0->buffer_gpu.lock();
  3423. x_buf_offset = extra_src0->offset + src0->view_offs;
  3424. GGML_ASSERT(d_X != nullptr);
  3425. }
  3426. if (use_src1 && !src1_uma) {
  3427. d_Y = extra_src1->buffer_gpu.lock();
  3428. y_buf_offset = extra_src1->offset + src1->view_offs;
  3429. GGML_ASSERT(d_Y != nullptr);
  3430. }
  3431. if (use_src2 && !src2_uma) {
  3432. d_Z = extra_src2->buffer_gpu.lock();
  3433. z_buf_offset = extra_src2->offset + src2->view_offs;
  3434. GGML_ASSERT(d_Z != nullptr);
  3435. }
  3436. if (op_supports_incontiguous) {
  3437. x_sz = ggml_nbytes(src0);
  3438. y_sz = use_src1 ? ggml_nbytes(src1) : 0;
  3439. z_sz = use_src2 ? ggml_nbytes(src2) : 0;
  3440. d_sz = ggml_nbytes(dst);
  3441. if (x_buf_offset + x_sz >= d_X->size) {
  3442. x_sz = VK_WHOLE_SIZE;
  3443. }
  3444. if (use_src1 && y_buf_offset + y_sz >= d_Y->size) {
  3445. y_sz = VK_WHOLE_SIZE;
  3446. }
  3447. if (use_src2 && z_buf_offset + z_sz >= d_Z->size) {
  3448. z_sz = VK_WHOLE_SIZE;
  3449. }
  3450. if (d_buf_offset + d_sz >= d_D->size) {
  3451. d_sz = VK_WHOLE_SIZE;
  3452. }
  3453. }
  3454. std::array<uint32_t, 3> elements;
  3455. // Single call if dimension 2 is contiguous
  3456. if (op_supports_incontiguous || (ggml_is_contiguous(src0) && (src1 == nullptr || ggml_is_contiguous(src1)))) {
  3457. ggml_pipeline_allocate_descriptor_sets(ctx, pipeline, 1);
  3458. switch (dst->op) {
  3459. case GGML_OP_NORM:
  3460. case GGML_OP_RMS_NORM:
  3461. case GGML_OP_SOFT_MAX:
  3462. case GGML_OP_SUM_ROWS:
  3463. elements = { (uint32_t)ggml_nrows(src0), 1, 1 };
  3464. break;
  3465. case GGML_OP_DIAG_MASK_INF:
  3466. case GGML_OP_ROPE:
  3467. elements = { (uint32_t)ggml_nrows(src0), (uint32_t)ne00, 1 };
  3468. break;
  3469. case GGML_OP_GET_ROWS:
  3470. elements = { (uint32_t)ne00, (uint32_t)ne10, (uint32_t)(ne11 * ne12) };
  3471. break;
  3472. case GGML_OP_ARGSORT:
  3473. elements = { (uint32_t)ne00, (uint32_t)ggml_nrows(src0), 1 };
  3474. break;
  3475. default:
  3476. elements = { (uint32_t)ggml_nelements(src0), 1, 1 };
  3477. break;
  3478. }
  3479. if (!op_supports_incontiguous) {
  3480. if (x_sz != VK_WHOLE_SIZE) {
  3481. x_sz *= ne02 * ne03;
  3482. }
  3483. if (use_src1 && y_sz != VK_WHOLE_SIZE) {
  3484. y_sz *= ne12 * ne13;
  3485. }
  3486. if (use_src2 && z_sz != VK_WHOLE_SIZE) {
  3487. z_sz *= ne22 * ne23;
  3488. }
  3489. if (d_sz != VK_WHOLE_SIZE) {
  3490. d_sz *= ned2 * ned3;
  3491. }
  3492. }
  3493. if (op == GGML_OP_SOFT_MAX) {
  3494. // Empty src1 is possible in soft_max, but the shader needs a buffer
  3495. vk_subbuffer subbuf_y;
  3496. if (use_src1) {
  3497. subbuf_y = { d_Y, y_buf_offset, y_sz };
  3498. } else {
  3499. subbuf_y = { d_X, 0, d_X->size };
  3500. }
  3501. ggml_vk_sync_buffers(subctx);
  3502. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { { d_X, x_buf_offset, x_sz }, subbuf_y, { d_D, d_buf_offset, d_sz } }, sizeof(PC), &pc, elements);
  3503. } else if (op == GGML_OP_ROPE) {
  3504. // Empty src2 is possible in rope, but the shader needs a buffer
  3505. vk_subbuffer subbuf_z;
  3506. if (use_src2) {
  3507. subbuf_z = { d_Z, z_buf_offset, z_sz };
  3508. } else {
  3509. subbuf_z = { d_X, 0, d_X->size };
  3510. }
  3511. ggml_vk_sync_buffers(subctx);
  3512. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { { d_X, x_buf_offset, x_sz }, { d_Y, y_buf_offset, y_sz }, subbuf_z, { d_D, d_buf_offset, d_sz } }, sizeof(PC), &pc, elements);
  3513. } else if (use_src2) {
  3514. ggml_vk_sync_buffers(subctx);
  3515. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { { d_X, x_buf_offset, x_sz }, { d_Y, y_buf_offset, y_sz }, { d_Z, z_buf_offset, z_sz }, { d_D, d_buf_offset, d_sz } }, sizeof(PC), &pc, elements);
  3516. } else if (use_src1) {
  3517. ggml_vk_sync_buffers(subctx);
  3518. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { { d_X, x_buf_offset, x_sz }, { d_Y, y_buf_offset, y_sz }, { d_D, d_buf_offset, d_sz } }, sizeof(PC), &pc, elements);
  3519. } else {
  3520. ggml_vk_sync_buffers(subctx);
  3521. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { { d_X, x_buf_offset, x_sz }, { d_D, d_buf_offset, d_sz } }, sizeof(PC), &pc, elements);
  3522. }
  3523. } else {
  3524. GGML_ASSERT(op != GGML_OP_SOFT_MAX);
  3525. GGML_ASSERT(op != GGML_OP_ARGSORT);
  3526. GGML_ASSERT(!use_src2);
  3527. ggml_pipeline_allocate_descriptor_sets(ctx, pipeline, ne02 * ne03);
  3528. switch (dst->op) {
  3529. case GGML_OP_NORM:
  3530. case GGML_OP_RMS_NORM:
  3531. elements = { (uint32_t)ne01, 1, 1 };
  3532. break;
  3533. case GGML_OP_DIAG_MASK_INF:
  3534. case GGML_OP_ROPE:
  3535. elements = { (uint32_t)ne01, (uint32_t)ne00, 1 };
  3536. break;
  3537. case GGML_OP_GET_ROWS:
  3538. elements = { (uint32_t)ne00, (uint32_t)ne10, (uint32_t)(ne11 * ne12) };
  3539. break;
  3540. default:
  3541. elements = { (uint32_t)ne0, 1, 1 };
  3542. break;
  3543. }
  3544. for (uint64_t i03 = 0; i03 < ne03; i03++) {
  3545. for (uint64_t i02 = 0; i02 < ne02; i02++) {
  3546. const uint32_t it_idx0 = (i03 * ne02 + i02);
  3547. const uint32_t it_idx1 = use_src1 ? ((i03 % ne13) * ne12 + (i02 % ne12)) : 0;
  3548. const uint32_t x_offset = x_sz * it_idx0;
  3549. const uint32_t y_offset = y_sz * it_idx1;
  3550. const uint32_t d_offset = d_sz * it_idx0;
  3551. if (use_src1) {
  3552. ggml_vk_sync_buffers(subctx);
  3553. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { { d_X, x_buf_offset + x_offset, x_sz }, { d_Y, y_buf_offset + y_offset, y_sz }, { d_D, d_buf_offset + d_offset, d_sz } }, sizeof(PC), &pc, elements);
  3554. } else {
  3555. ggml_vk_sync_buffers(subctx);
  3556. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { { d_X, x_buf_offset + x_offset, x_sz }, { d_D, d_buf_offset + d_offset, d_sz } }, sizeof(PC), &pc, elements);
  3557. }
  3558. }
  3559. }
  3560. }
  3561. }
  3562. static void ggml_vk_repeat(ggml_backend_vk_context * ctx, vk_context * subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3563. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_REPEAT, { (uint32_t)ggml_nelements(src0), (uint32_t)ggml_nelements(src1), 0.0f, 0.0f });
  3564. }
  3565. 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) {
  3566. const uint32_t src0_type_size = ggml_type_size(src0->type);
  3567. const uint32_t src1_type_size = ggml_type_size(src1->type);
  3568. const uint32_t dst_type_size = ggml_type_size(dst->type);
  3569. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_GET_ROWS, {
  3570. (uint32_t)ggml_nelements(src0),
  3571. (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,
  3572. (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,
  3573. (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,
  3574. 0,
  3575. 0.0f, 0.0f,
  3576. });
  3577. }
  3578. static void ggml_vk_add(ggml_backend_vk_context * ctx, vk_context * subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3579. const uint32_t src0_type_size = ggml_type_size(src0->type);
  3580. const uint32_t src1_type_size = ggml_type_size(src1->type);
  3581. const uint32_t dst_type_size = ggml_type_size(dst->type);
  3582. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_ADD, {
  3583. (uint32_t)ggml_nelements(src0),
  3584. (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,
  3585. (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,
  3586. (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,
  3587. 0,
  3588. 0.0f, 0.0f,
  3589. });
  3590. }
  3591. static void ggml_vk_mul(ggml_backend_vk_context * ctx, vk_context * subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3592. const uint32_t src0_type_size = ggml_type_size(src0->type);
  3593. const uint32_t src1_type_size = ggml_type_size(src1->type);
  3594. const uint32_t dst_type_size = ggml_type_size(dst->type);
  3595. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_MUL, {
  3596. (uint32_t)ggml_nelements(src0),
  3597. (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,
  3598. (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,
  3599. (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,
  3600. 0,
  3601. 0.0f, 0.0f,
  3602. });
  3603. }
  3604. static void ggml_vk_div(ggml_backend_vk_context * ctx, vk_context * subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3605. const uint32_t src0_type_size = ggml_type_size(src0->type);
  3606. const uint32_t src1_type_size = ggml_type_size(src1->type);
  3607. const uint32_t dst_type_size = ggml_type_size(dst->type);
  3608. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_DIV, {
  3609. (uint32_t)ggml_nelements(src0),
  3610. (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,
  3611. (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,
  3612. (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,
  3613. 0,
  3614. 0.0f, 0.0f,
  3615. });
  3616. }
  3617. static void ggml_vk_scale(ggml_backend_vk_context * ctx, vk_context * subctx, const ggml_tensor * src0, ggml_tensor * dst) {
  3618. float * op_params = (float *)dst->op_params;
  3619. const uint32_t src0_type_size = ggml_type_size(src0->type);
  3620. const uint32_t dst_type_size = ggml_type_size(dst->type);
  3621. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SCALE, {
  3622. (uint32_t)ggml_nelements(src0),
  3623. (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,
  3624. (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,
  3625. 0,
  3626. op_params[0], 0.0f
  3627. });
  3628. }
  3629. static void ggml_vk_sqr(ggml_backend_vk_context * ctx, vk_context * subctx, const ggml_tensor * src0, ggml_tensor * dst) {
  3630. const uint32_t src0_type_size = ggml_type_size(src0->type);
  3631. const uint32_t dst_type_size = ggml_type_size(dst->type);
  3632. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SQR, {
  3633. (uint32_t)ggml_nelements(src0),
  3634. (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,
  3635. (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,
  3636. 0,
  3637. 0.0f, 0.0f,
  3638. });
  3639. }
  3640. static void ggml_vk_clamp(ggml_backend_vk_context * ctx, vk_context * subctx, const ggml_tensor * src0, ggml_tensor * dst) {
  3641. float * op_params = (float *)dst->op_params;
  3642. const uint32_t src0_type_size = ggml_type_size(src0->type);
  3643. const uint32_t dst_type_size = ggml_type_size(dst->type);
  3644. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_CLAMP, {
  3645. (uint32_t)ggml_nelements(src0),
  3646. (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,
  3647. (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,
  3648. 0,
  3649. op_params[0], op_params[1],
  3650. });
  3651. }
  3652. static void ggml_vk_cpy(ggml_backend_vk_context * ctx, vk_context * subctx, const ggml_tensor * src0, ggml_tensor * dst) {
  3653. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) dst->extra;
  3654. const uint32_t src0_type_size = ggml_type_size(src0->type);
  3655. const uint32_t dst_type_size = ggml_type_size(dst->type);
  3656. const uint32_t d_offset = ((extra->offset + dst->view_offs) % ctx->device->properties.limits.minStorageBufferOffsetAlignment) / dst_type_size;
  3657. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_CPY, {
  3658. (uint32_t)ggml_nelements(src0),
  3659. (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,
  3660. (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,
  3661. d_offset,
  3662. 0.0f, 0.0f,
  3663. });
  3664. }
  3665. static void ggml_vk_norm(ggml_backend_vk_context * ctx, vk_context * subctx, const ggml_tensor * src0, ggml_tensor * dst) {
  3666. float * op_params = (float *)dst->op_params;
  3667. 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 });
  3668. }
  3669. static void ggml_vk_rms_norm(ggml_backend_vk_context * ctx, vk_context * subctx, const ggml_tensor * src0, ggml_tensor * dst) {
  3670. float * op_params = (float *)dst->op_params;
  3671. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_RMS_NORM, { (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], op_params[0], 0.0f });
  3672. }
  3673. static void ggml_vk_unary(ggml_backend_vk_context * ctx, vk_context * subctx, const ggml_tensor * src0, ggml_tensor * dst) {
  3674. 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 });
  3675. }
  3676. static void ggml_vk_diag_mask_inf(ggml_backend_vk_context * ctx, vk_context * subctx, const ggml_tensor * src0, ggml_tensor * dst) {
  3677. int32_t * op_params = (int32_t *)dst->op_params;
  3678. 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] });
  3679. }
  3680. 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) {
  3681. float * op_params = (float *)dst->op_params;
  3682. float scale = op_params[0];
  3683. float max_bias = op_params[1];
  3684. const uint32_t ncols = (uint32_t)src0->ne[0];
  3685. const uint32_t nrows_x = (uint32_t)ggml_nrows(src0);
  3686. const uint32_t nrows_y = (uint32_t)src0->ne[1];
  3687. const uint32_t n_head_kv = nrows_x/nrows_y;
  3688. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head_kv));
  3689. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  3690. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  3691. ggml_vk_op_f32<vk_op_soft_max_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_SOFT_MAX, {
  3692. ncols,
  3693. src1 != nullptr ? nrows_y : (uint32_t)0,
  3694. scale, max_bias,
  3695. m0, m1,
  3696. n_head_log2,
  3697. });
  3698. }
  3699. 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) {
  3700. const int n_dims = ((int32_t *) dst->op_params)[1];
  3701. // const int mode = ((int32_t *) dst->op_params)[2];
  3702. // const int n_ctx = ((int32_t *) dst->op_params)[3];
  3703. const int n_ctx_orig = ((int32_t *) dst->op_params)[4];
  3704. const float freq_base = ((float *) dst->op_params)[5];
  3705. const float freq_scale = ((float *) dst->op_params)[6];
  3706. const float ext_factor = ((float *) dst->op_params)[7];
  3707. const float attn_factor = ((float *) dst->op_params)[8];
  3708. const float beta_fast = ((float *) dst->op_params)[9];
  3709. const float beta_slow = ((float *) dst->op_params)[10];
  3710. float corr_dims[2];
  3711. ggml_rope_yarn_corr_dims(n_dims, n_ctx_orig, freq_base, beta_fast, beta_slow, corr_dims);
  3712. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  3713. ggml_vk_op_f32<vk_op_rope_push_constants>(ctx, subctx, src0, src1, src2, dst, GGML_OP_ROPE, {
  3714. (uint32_t)src0->ne[0], (uint32_t)n_dims, freq_scale, (uint32_t)src0->ne[1],
  3715. freq_base, ext_factor, attn_factor, {corr_dims[0], corr_dims[1]}, theta_scale,
  3716. src2 != nullptr,
  3717. });
  3718. }
  3719. static void ggml_vk_argsort(ggml_backend_vk_context * ctx, vk_context * subctx, const ggml_tensor * src0, ggml_tensor * dst) {
  3720. int32_t * op_params = (int32_t *)dst->op_params;
  3721. uint32_t ncols = src0->ne[0];
  3722. uint32_t ncols_pad = 1;
  3723. while (ncols_pad < ncols) {
  3724. ncols_pad *= 2;
  3725. }
  3726. GGML_ASSERT(ncols_pad <= 1024);
  3727. ggml_vk_op_f32<vk_op_argsort_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_ARGSORT, {
  3728. ncols,
  3729. ncols_pad,
  3730. op_params[0],
  3731. });
  3732. }
  3733. static void ggml_vk_sum_rows(ggml_backend_vk_context * ctx, vk_context * subctx, const ggml_tensor * src0, ggml_tensor * dst) {
  3734. 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 });
  3735. }
  3736. #ifdef GGML_VULKAN_RUN_TESTS
  3737. static void ggml_vk_print_matrix_area(const void * data, ggml_type type, int ne0, int ne1, int i0, int i1, int i2) {
  3738. if (type != GGML_TYPE_F32 && type != GGML_TYPE_F16) {
  3739. return;
  3740. }
  3741. i0 = std::max(i0, 5);
  3742. i1 = std::max(i1, 5);
  3743. i2 = std::max(i2, 0);
  3744. fprintf(stderr, " ");
  3745. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  3746. fprintf(stderr, "%7d ", idx1);
  3747. }
  3748. fprintf(stderr, "\n");
  3749. for (int idx0 = i0 - 5; idx0 < i0 + 5; idx0++) {
  3750. fprintf(stderr, "%7d: ", idx0);
  3751. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  3752. if (idx0 >= 0 && idx0 < ne0 && idx1 >= 0 && idx1 < ne1) {
  3753. float val;
  3754. if (type == GGML_TYPE_F32) {
  3755. val = *((const float *) data + i2*ne1*ne0 + idx1*ne0 + idx0);
  3756. } else if (type == GGML_TYPE_F16) {
  3757. val = ggml_fp16_to_fp32(*((const ggml_fp16_t *) data + i2*ne1*ne0 + idx1*ne0 + idx0));
  3758. } else {
  3759. GGML_ASSERT(false);
  3760. }
  3761. fprintf(stderr, "% 7.2f ", val);
  3762. } else {
  3763. fprintf(stderr, " ");
  3764. }
  3765. }
  3766. fprintf(stderr, "\n");
  3767. }
  3768. }
  3769. template <typename X_TYPE, typename Y_TYPE>
  3770. 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) {
  3771. #ifdef GGML_VULKAN_DEBUG
  3772. std::cerr << "ggml_vk_test_matmul(" << m << ", " << n << ", " << k << ", " << batch << ", " << num_it << ", " << split_k << ", " << shader_size << ")" << std::endl;
  3773. #endif
  3774. const size_t x_ne = m * k * batch;
  3775. const size_t y_ne = k * n * batch;
  3776. const size_t d_ne = m * n * batch;
  3777. vk_pipeline p;
  3778. std::string shname;
  3779. if (shader_size == 0) {
  3780. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  3781. p = ctx->device->pipeline_matmul_f32->a_s;
  3782. shname = "F32_ALIGNED_S";
  3783. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  3784. p = ctx->device->pipeline_matmul_f32_f16->a_s;
  3785. shname = "F32_F16_ALIGNED_S";
  3786. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  3787. p = ctx->device->pipeline_matmul_f16_f32->a_s;
  3788. shname = "F16_F32_ALIGNED_S";
  3789. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  3790. p = ctx->device->pipeline_matmul_f16->a_s;
  3791. shname = "F16_ALIGNED_S";
  3792. } else {
  3793. GGML_ASSERT(false);
  3794. }
  3795. } else if (shader_size == 1) {
  3796. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  3797. p = ctx->device->pipeline_matmul_f32->a_m;
  3798. shname = "F32_ALIGNED_M";
  3799. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  3800. p = ctx->device->pipeline_matmul_f32_f16->a_m;
  3801. shname = "F32_F16_ALIGNED_M";
  3802. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  3803. p = ctx->device->pipeline_matmul_f16_f32->a_m;
  3804. shname = "F16_F32_ALIGNED_M";
  3805. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  3806. p = ctx->device->pipeline_matmul_f16->a_m;
  3807. shname = "F16_ALIGNED_M";
  3808. } else {
  3809. GGML_ASSERT(false);
  3810. }
  3811. } else if (shader_size == 2) {
  3812. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  3813. p = ctx->device->pipeline_matmul_f32->a_l;
  3814. shname = "F32_ALIGNED_L";
  3815. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  3816. p = ctx->device->pipeline_matmul_f32_f16->a_l;
  3817. shname = "F32_F16_ALIGNED_L";
  3818. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  3819. p = ctx->device->pipeline_matmul_f16_f32->a_l;
  3820. shname = "F16_F32_ALIGNED_L";
  3821. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  3822. p = ctx->device->pipeline_matmul_f16->a_l;
  3823. shname = "F16_ALIGNED_L";
  3824. } else {
  3825. GGML_ASSERT(false);
  3826. }
  3827. } else {
  3828. GGML_ASSERT(0);
  3829. }
  3830. const size_t kpad = ggml_vk_align_size(k, p->align);
  3831. if (k != kpad) {
  3832. if (shader_size == 0) {
  3833. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  3834. p = ctx->device->pipeline_matmul_f32->s;
  3835. shname = "F32_S";
  3836. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  3837. p = ctx->device->pipeline_matmul_f32_f16->s;
  3838. shname = "F32_F16_S";
  3839. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  3840. p = ctx->device->pipeline_matmul_f16_f32->s;
  3841. shname = "F16_F32_S";
  3842. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  3843. p = ctx->device->pipeline_matmul_f16->s;
  3844. shname = "F16_S";
  3845. }
  3846. } else if (shader_size == 1) {
  3847. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  3848. p = ctx->device->pipeline_matmul_f32->m;
  3849. shname = "F32_M";
  3850. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  3851. p = ctx->device->pipeline_matmul_f32_f16->m;
  3852. shname = "F32_F16_M";
  3853. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  3854. p = ctx->device->pipeline_matmul_f16_f32->m;
  3855. shname = "F16_F32_M";
  3856. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  3857. p = ctx->device->pipeline_matmul_f16->m;
  3858. shname = "F16_M";
  3859. }
  3860. } else if (shader_size == 2) {
  3861. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  3862. p = ctx->device->pipeline_matmul_f32->l;
  3863. shname = "F32_L";
  3864. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  3865. p = ctx->device->pipeline_matmul_f32_f16->l;
  3866. shname = "F32_F16_L";
  3867. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  3868. p = ctx->device->pipeline_matmul_f16_f32->l;
  3869. shname = "F16_F32_L";
  3870. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  3871. p = ctx->device->pipeline_matmul_f16->l;
  3872. shname = "F16_L";
  3873. }
  3874. }
  3875. }
  3876. ggml_pipeline_allocate_descriptor_sets(ctx, p, num_it);
  3877. if (split_k > 1) {
  3878. ggml_pipeline_allocate_descriptor_sets(ctx, ctx->device->pipeline_matmul_split_k_reduce, num_it);
  3879. if (ctx->prealloc_split_k == nullptr || ctx->prealloc_split_k->size < sizeof(float) * d_ne * split_k) {
  3880. // Resize buffer
  3881. if (ctx->prealloc_split_k != nullptr) {
  3882. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  3883. }
  3884. ctx->prealloc_split_k = ggml_vk_create_buffer_check(ctx, sizeof(float) * d_ne * split_k, vk::MemoryPropertyFlagBits::eDeviceLocal);
  3885. }
  3886. }
  3887. vk_buffer d_X = ggml_vk_create_buffer_check(ctx, sizeof(X_TYPE) * x_ne, vk::MemoryPropertyFlagBits::eDeviceLocal);
  3888. vk_buffer d_Y = ggml_vk_create_buffer_check(ctx, sizeof(Y_TYPE) * y_ne, vk::MemoryPropertyFlagBits::eDeviceLocal);
  3889. vk_buffer d_D = ggml_vk_create_buffer_check(ctx, sizeof(float) * d_ne, vk::MemoryPropertyFlagBits::eDeviceLocal);
  3890. X_TYPE* x = (X_TYPE *) malloc(sizeof(X_TYPE) * x_ne);
  3891. Y_TYPE* y = (Y_TYPE *) malloc(sizeof(Y_TYPE) * y_ne);
  3892. float* d = (float *) malloc(sizeof(float) * d_ne);
  3893. for (size_t i = 0; i < x_ne; i++) {
  3894. if (std::is_same<float, X_TYPE>()) {
  3895. x[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  3896. } else if (std::is_same<ggml_fp16_t, X_TYPE>()) {
  3897. x[i] = ggml_fp32_to_fp16((rand() / (float)RAND_MAX) * 2.0f - 1.0f);
  3898. } else {
  3899. GGML_ASSERT(false);
  3900. }
  3901. }
  3902. for (size_t i = 0; i < y_ne; i++) {
  3903. if (std::is_same<float, Y_TYPE>()) {
  3904. // y[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  3905. y[i] = (i % k == i / k) ? 1.0f : 0.0f;
  3906. } else if (std::is_same<ggml_fp16_t, Y_TYPE>()) {
  3907. // y[i] = ggml_fp32_to_fp16((rand() / (float)RAND_MAX) * 2.0f - 1.0f);
  3908. y[i] = ggml_fp32_to_fp16((i % k == i / k) ? 1.0f : 0.0f);
  3909. } else {
  3910. GGML_ASSERT(false);
  3911. }
  3912. }
  3913. ggml_vk_buffer_write(ctx, d_X, 0, x, sizeof(X_TYPE) * k * m * batch);
  3914. ggml_vk_buffer_write(ctx, d_Y, 0, y, sizeof(Y_TYPE) * k * n * batch);
  3915. vk_context * subctx = ggml_vk_create_context(ctx, ctx->device->compute_queue);
  3916. for (size_t i = 0; i < num_it; i++) {
  3917. ggml_vk_ctx_begin(ctx, subctx);
  3918. ggml_vk_matmul(
  3919. 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),
  3920. m, n, k,
  3921. k, k, m, k*m, k*n, m*n,
  3922. split_k, batch, batch, batch, 1, 1
  3923. );
  3924. ggml_vk_ctx_end(subctx);
  3925. }
  3926. auto begin = std::chrono::high_resolution_clock::now();
  3927. ggml_vk_submit(subctx, ctx->fence);
  3928. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_matmul waitForFences");
  3929. ctx->device->device.resetFences({ ctx->fence });
  3930. auto end = std::chrono::high_resolution_clock::now();
  3931. double time = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  3932. // copy dst to host
  3933. ggml_vk_buffer_read(ctx, d_D, 0, d, sizeof(float) * d_ne);
  3934. float * d_chk = (float *) malloc(sizeof(float) * d_ne);
  3935. ggml_init_params iparams = {
  3936. /*.mem_size =*/ 1024*1024*1024,
  3937. /*.mem_buffer =*/ NULL,
  3938. /*.no_alloc =*/ true,
  3939. };
  3940. ggml_context * ggml_ctx = ggml_init(iparams);
  3941. ggml_type src0_type;
  3942. ggml_type src1_type;
  3943. if (std::is_same<float, X_TYPE>()) {
  3944. src0_type = GGML_TYPE_F32;
  3945. } else if (std::is_same<ggml_fp16_t, X_TYPE>()) {
  3946. src0_type = GGML_TYPE_F16;
  3947. } else {
  3948. GGML_ASSERT(false);
  3949. }
  3950. if (std::is_same<float, Y_TYPE>()) {
  3951. src1_type = GGML_TYPE_F32;
  3952. } else if (std::is_same<ggml_fp16_t, Y_TYPE>()) {
  3953. src1_type = GGML_TYPE_F16;
  3954. } else {
  3955. GGML_ASSERT(false);
  3956. }
  3957. ggml_tensor * src0_ggml = ggml_new_tensor_3d(ggml_ctx, src0_type, k, m, batch);
  3958. ggml_tensor * src1_ggml = ggml_new_tensor_3d(ggml_ctx, src1_type, k, n, batch);
  3959. ggml_tensor * tensor_ggml = ggml_mul_mat(ggml_ctx, src0_ggml, src1_ggml);
  3960. src0_ggml->data = x;
  3961. src1_ggml->data = y;
  3962. tensor_ggml->data = d_chk;
  3963. ggml_cgraph * cgraph = ggml_new_graph(ggml_ctx);
  3964. ggml_build_forward_expand(cgraph, tensor_ggml);
  3965. ggml_graph_compute_with_ctx(ggml_ctx, cgraph, 1);
  3966. ggml_free(ggml_ctx);
  3967. double avg_err = 0.0;
  3968. int first_err_n = -1;
  3969. int first_err_m = -1;
  3970. int first_err_b = -1;
  3971. for (size_t i = 0; i < m*n*batch; i++) {
  3972. double err = std::fabs(d[i] - d_chk[i]);
  3973. avg_err += err;
  3974. if (err > 0.05f && first_err_n == -1) {
  3975. first_err_b = i / (m * n);
  3976. first_err_n = (i % (m * n)) / m;
  3977. first_err_m = (i % (m * n)) % m;
  3978. }
  3979. }
  3980. avg_err /= m * n;
  3981. std::cerr << "TEST " << shname << " m=" << m << " n=" << n << " k=" << k << " batch=" << batch << " split_k=" << split_k << " matmul " << time / num_it << "ms avg_err=" << avg_err << std::endl;
  3982. if (avg_err > 0.1) {
  3983. std::cerr << "m = " << first_err_m << " n = " << first_err_n << " b = " << first_err_b << std::endl;
  3984. std::cerr << "Actual result: " << std::endl << std::endl;
  3985. ggml_vk_print_matrix_area(d, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  3986. std::cerr << std::endl;
  3987. ggml_vk_print_matrix_area(d, GGML_TYPE_F32, m, n, first_err_m, first_err_n + 15, first_err_b);
  3988. std::cerr << "Expected result: " << std::endl << std::endl;
  3989. ggml_vk_print_matrix_area(d_chk, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  3990. if (split_k > 1) {
  3991. float * split_k_buf = (float *) malloc(sizeof(float) * d_ne * split_k);
  3992. ggml_vk_buffer_read(ctx, ctx->prealloc_split_k, 0, split_k_buf, sizeof(float) * d_ne * split_k);
  3993. std::cerr << "d_buf0: " << std::endl << std::endl;
  3994. ggml_vk_print_matrix_area(split_k_buf, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  3995. std::cerr << "d_buf1: " << std::endl << std::endl;
  3996. ggml_vk_print_matrix_area(split_k_buf + d_ne, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  3997. std::cerr << "d_buf2: " << std::endl << std::endl;
  3998. 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);
  3999. std::cerr << "d_buf3: " << std::endl << std::endl;
  4000. 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);
  4001. free(split_k_buf);
  4002. }
  4003. }
  4004. free(d_chk);
  4005. ggml_vk_queue_cleanup(ctx, ctx->device->transfer_queue);
  4006. ggml_vk_queue_cleanup(ctx, ctx->device->compute_queue);
  4007. ggml_vk_destroy_buffer(d_X);
  4008. ggml_vk_destroy_buffer(d_Y);
  4009. ggml_vk_destroy_buffer(d_D);
  4010. ggml_pipeline_cleanup(p);
  4011. ggml_pipeline_cleanup(ctx->device->pipeline_matmul_split_k_reduce);
  4012. free(x);
  4013. free(y);
  4014. free(d);
  4015. }
  4016. static void ggml_vk_print_tensor_area(const ggml_tensor * tensor, int i0, int i1, int i2, int i3) {
  4017. if (tensor->type != GGML_TYPE_F32 && tensor->type != GGML_TYPE_F16) {
  4018. return;
  4019. }
  4020. i0 = std::max(i0, 5);
  4021. i1 = std::max(i1, 5);
  4022. i2 = std::max(i2, 0);
  4023. i3 = std::max(i3, 0);
  4024. fprintf(stderr, " ");
  4025. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  4026. fprintf(stderr, "%7d ", idx1);
  4027. }
  4028. fprintf(stderr, "\n");
  4029. for (int idx0 = i0 - 5; idx0 < i0 + 5; idx0++) {
  4030. fprintf(stderr, "%7d: ", idx0);
  4031. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  4032. 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]) {
  4033. float val;
  4034. if (tensor->type == GGML_TYPE_F32) {
  4035. val = *(float *) ((char *) tensor->data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]);
  4036. } else if (tensor->type == GGML_TYPE_F16) {
  4037. 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]));
  4038. } else {
  4039. GGML_ASSERT(false);
  4040. }
  4041. fprintf(stderr, "% 7.2f ", val);
  4042. } else {
  4043. fprintf(stderr, " ");
  4044. }
  4045. }
  4046. fprintf(stderr, "\n");
  4047. }
  4048. }
  4049. static void ggml_vk_test_h2d_nc(ggml_backend_vk_context * ctx, size_t ne0, size_t ne1, size_t ne2, size_t ne3) {
  4050. const size_t ne = ne0 * ne1 * ne2 * ne3;
  4051. ggml_init_params iparams = {
  4052. /*.mem_size =*/ 1024*1024*1024,
  4053. /*.mem_buffer =*/ NULL,
  4054. /*.no_alloc =*/ true,
  4055. };
  4056. ggml_context * ggml_ctx = ggml_init(iparams);
  4057. ggml_tensor * tensor = ggml_new_tensor_4d(ggml_ctx, GGML_TYPE_F32, ne0, ne2, ne1, ne3); // NOLINT
  4058. ggml_tensor * result_tensor = ggml_new_tensor_4d(ggml_ctx, GGML_TYPE_F32, ne0, ne1, ne2, ne3);
  4059. float * data = (float *) ggml_vk_host_malloc(ctx, ggml_nbytes(tensor));
  4060. tensor->data = data;
  4061. float * result_data = (float *) malloc(ggml_nbytes(tensor));
  4062. result_tensor->data = result_data;
  4063. // Permute
  4064. {
  4065. size_t tmp = tensor->nb[2];
  4066. tensor->nb[2] = tensor->nb[1];
  4067. tensor->nb[1] = tmp;
  4068. tensor->ne[2] = ne2;
  4069. tensor->ne[1] = ne1;
  4070. }
  4071. for (size_t i = 0; i < ne; i++) {
  4072. data[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  4073. }
  4074. vk_context * subctx = ggml_vk_create_context(ctx, ctx->device->compute_queue);
  4075. ggml_vk_ctx_begin(ctx, subctx);
  4076. vk_buffer buffer = ggml_vk_create_buffer_check(ctx, ggml_nbytes(tensor), vk::MemoryPropertyFlagBits::eDeviceLocal);
  4077. ggml_vk_h2d_tensor_2d(ctx, subctx, buffer, 0, tensor, 0, 0, ggml_nrows(tensor));
  4078. ggml_vk_ctx_end(subctx);
  4079. ggml_vk_submit(subctx, ctx->fence);
  4080. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_h2d_nc waitForFences");
  4081. ctx->device->device.resetFences({ ctx->fence });
  4082. ggml_vk_buffer_read(ctx, buffer, 0, result_data, ggml_nbytes(tensor));
  4083. double avg_err = 0.0;
  4084. int first_err_i0 = -1;
  4085. int first_err_i1 = -1;
  4086. int first_err_i2 = -1;
  4087. int first_err_i3 = -1;
  4088. for (size_t i3 = 0; i3 < ne3; i3++) {
  4089. for (size_t i2 = 0; i2 < ne2; i2++) {
  4090. for (size_t i1 = 0; i1 < ne1; i1++) {
  4091. for (size_t i0 = 0; i0 < ne0; i0++) {
  4092. float correct = *(float *) ((char *) data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]);
  4093. float result = *(float *) ((char *) result_data + i3*ne2*ne1*ne0*sizeof(float) + i2*ne1*ne0*sizeof(float) + i1*ne0*sizeof(float) + i0*sizeof(float));
  4094. double err = std::fabs(result - correct);
  4095. avg_err += err;
  4096. if (err > 0.05f && first_err_i0 == -1) {
  4097. first_err_i0 = i0;
  4098. first_err_i1 = i1;
  4099. first_err_i2 = i2;
  4100. first_err_i3 = i3;
  4101. }
  4102. }
  4103. }
  4104. }
  4105. }
  4106. avg_err /= ne;
  4107. std::cerr << "TEST nc copy ne0=" << ne0 << " ne1=" << ne1 << " ne2=" << ne2 << " ne3=" << ne3 << " avg_err=" << avg_err << std::endl;
  4108. if (avg_err > 0.1) {
  4109. std::cerr << "i0 = " << first_err_i0 << " i1 = " << first_err_i1 << " i2 = " << first_err_i2 << " i3 = " << first_err_i3 << std::endl;
  4110. std::cerr << "Actual result: " << std::endl << std::endl;
  4111. ggml_vk_print_tensor_area(result_tensor, first_err_i0, first_err_i1, first_err_i2, first_err_i3);
  4112. std::cerr << "Expected result: " << std::endl << std::endl;
  4113. ggml_vk_print_tensor_area(tensor, first_err_i0, first_err_i1, first_err_i2, first_err_i3);
  4114. }
  4115. ggml_free(ggml_ctx);
  4116. ggml_vk_destroy_buffer(buffer);
  4117. ggml_vk_host_free(ctx, data);
  4118. free(result_data);
  4119. }
  4120. static void ggml_vk_test_transfer(ggml_backend_vk_context * ctx, size_t ne, bool pinned) {
  4121. #ifdef GGML_VULKAN_DEBUG
  4122. std::cerr << "ggml_vk_test_transfer(" << ne << ")" << std::endl;
  4123. #endif
  4124. // Check transfers are correct
  4125. vk_buffer buffer = ggml_vk_create_buffer_check(ctx, sizeof(float) * ne, vk::MemoryPropertyFlagBits::eDeviceLocal);
  4126. float * x;
  4127. float * y;
  4128. if (pinned) {
  4129. x = (float *) ggml_vk_host_malloc(ctx, sizeof(float) * ne);
  4130. y = (float *) ggml_vk_host_malloc(ctx, sizeof(float) * ne);
  4131. } else {
  4132. x = (float *) malloc(sizeof(float) * ne);
  4133. y = (float *) malloc(sizeof(float) * ne);
  4134. }
  4135. for (size_t i = 0; i < ne; i++) {
  4136. x[i] = rand() / (float)RAND_MAX;
  4137. }
  4138. vk_context * subctx = ggml_vk_create_context(ctx, ctx->device->compute_queue);
  4139. ggml_vk_ctx_begin(ctx, subctx);
  4140. auto begin = std::chrono::high_resolution_clock::now();
  4141. ggml_vk_buffer_write_async(ctx, subctx, buffer, 0, x, sizeof(float) * ne);
  4142. for (auto& cpy : subctx->in_memcpys) {
  4143. memcpy(cpy.dst, cpy.src, cpy.n);
  4144. }
  4145. subctx->in_memcpys.clear();
  4146. ggml_vk_ctx_end(subctx);
  4147. ggml_vk_submit(subctx, ctx->fence);
  4148. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_transfer waitForFences");
  4149. ctx->device->device.resetFences({ ctx->fence });
  4150. auto end = std::chrono::high_resolution_clock::now();
  4151. double ms_to_gpu = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  4152. ggml_vk_ctx_begin(ctx, subctx);
  4153. begin = std::chrono::high_resolution_clock::now();
  4154. ggml_vk_buffer_read_async(ctx, subctx, buffer, 0, y, sizeof(float) * ne);
  4155. ggml_vk_ctx_end(subctx);
  4156. ggml_vk_submit(subctx, ctx->fence);
  4157. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_transfer waitForFences");
  4158. ctx->device->device.resetFences({ ctx->fence });
  4159. for (auto& cpy : subctx->out_memcpys) {
  4160. memcpy(cpy.dst, cpy.src, cpy.n);
  4161. }
  4162. subctx->out_memcpys.clear();
  4163. end = std::chrono::high_resolution_clock::now();
  4164. double ms_from_gpu = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  4165. double avg_err = 0.0;
  4166. for (size_t i = 0; i < ne; i++) {
  4167. avg_err += std::fabs(x[i] - y[i]);
  4168. }
  4169. double kb = ne * sizeof(float) / 1024.0;
  4170. std::cerr << "TEST TRANSFER " << kb << " KB to_gpu " << ms_to_gpu << "ms (" << kb / ms_to_gpu * 1000.0 / 1024.0 << " MB/s) from_gpu " << ms_from_gpu << "ms (" << kb / ms_from_gpu * 1000.0 / 1024.0 << " MB/s) avg_err=" << avg_err / ne << std::endl;
  4171. ggml_vk_destroy_buffer(buffer);
  4172. if (pinned) {
  4173. ggml_vk_host_free(ctx, x);
  4174. ggml_vk_host_free(ctx, y);
  4175. } else {
  4176. free(x);
  4177. free(y);
  4178. }
  4179. }
  4180. static void ggml_vk_quantize_data(const float * from, void * to, size_t ne, ggml_type quant) {
  4181. ggml_quantize_chunk(quant, from, to, 0, 1, ne, nullptr);
  4182. }
  4183. static void ggml_vk_test_dequant(ggml_backend_vk_context * ctx, size_t ne, ggml_type quant) {
  4184. #ifdef GGML_VULKAN_DEBUG
  4185. std::cerr << "ggml_vk_test_dequant(" << ne << ")" << std::endl;
  4186. #endif
  4187. const size_t x_sz = sizeof(float) * ne;
  4188. const size_t x_sz_f16 = sizeof(ggml_fp16_t) * ne;
  4189. const size_t qx_sz = ne * ggml_type_size(quant)/ggml_blck_size(quant);
  4190. float * x = (float *) malloc(x_sz);
  4191. void * qx = malloc(qx_sz);
  4192. vk_buffer qx_buf = ggml_vk_create_buffer_check(ctx, qx_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  4193. vk_buffer x_buf = ggml_vk_create_buffer_check(ctx, x_sz_f16, vk::MemoryPropertyFlagBits::eDeviceLocal);
  4194. ggml_fp16_t * x_chk = (ggml_fp16_t *) malloc(x_sz_f16);
  4195. for (size_t i = 0; i < ne; i++) {
  4196. x[i] = rand() / (float)RAND_MAX;
  4197. }
  4198. vk_pipeline p = ctx->device->pipeline_dequant[quant];
  4199. ggml_vk_quantize_data(x, qx, ne, quant);
  4200. ggml_pipeline_allocate_descriptor_sets(ctx, p, 1);
  4201. ggml_vk_buffer_write(ctx, qx_buf, 0, qx, qx_sz);
  4202. vk_context * subctx = ggml_vk_create_context(ctx, ctx->device->compute_queue);
  4203. ggml_vk_ctx_begin(ctx, subctx);
  4204. const std::vector<uint32_t> pc = { 1, (uint32_t)ne, (uint32_t)ne, (uint32_t)ne, (uint32_t)ne };
  4205. ggml_vk_dispatch_pipeline(ctx, subctx, p, { { qx_buf, 0, qx_sz }, { x_buf, 0, x_sz_f16 } }, pc.size() * sizeof(int), pc.data(), { (uint32_t)ne, 1, 1});
  4206. ggml_vk_ctx_end(subctx);
  4207. auto begin = std::chrono::high_resolution_clock::now();
  4208. ggml_vk_submit(subctx, ctx->fence);
  4209. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_dequant waitForFences");
  4210. ctx->device->device.resetFences({ ctx->fence });
  4211. auto end = std::chrono::high_resolution_clock::now();
  4212. double ms_dequant = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  4213. ggml_vk_buffer_read(ctx, x_buf, 0, x_chk, x_sz_f16);
  4214. int first_err = -1;
  4215. double avg_err = 0.0;
  4216. for (size_t i = 0; i < ne; i++) {
  4217. double error = std::fabs(x[i] - ggml_fp16_to_fp32(x_chk[i]));
  4218. avg_err += error;
  4219. if (first_err < 0 && error > 0.05) {
  4220. first_err = i;
  4221. }
  4222. }
  4223. avg_err /= ne;
  4224. std::cerr << "TEST DEQUANT " << ggml_type_name(quant) << " time=" << ms_dequant << "ms avg_err=" << avg_err << std::endl;
  4225. if (avg_err > 0.1) {
  4226. std::cerr << "first_error = " << first_err << std::endl;
  4227. std::cerr << "Actual result: " << std::endl << std::endl;
  4228. for (int i = std::max(0, first_err - 5); i < std::min((int)ne, first_err + 5); i++) {
  4229. std::cerr << ggml_fp16_to_fp32(x_chk[i]) << ", ";
  4230. }
  4231. std::cerr << std::endl << "Expected result: " << std::endl << std::endl;
  4232. for (int i = std::max(0, first_err - 5); i < std::min((int)ne, first_err + 5); i++) {
  4233. std::cerr << x[i] << ", ";
  4234. }
  4235. std::cerr << std::endl;
  4236. }
  4237. ggml_vk_destroy_buffer(x_buf);
  4238. ggml_vk_destroy_buffer(qx_buf);
  4239. free(x);
  4240. free(qx);
  4241. free(x_chk);
  4242. }
  4243. 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) {
  4244. #ifdef GGML_VULKAN_DEBUG
  4245. std::cerr << "ggml_vk_test_dequant_matmul(" << m << ", " << n << ", " << k << ", " << batch << ", " << num_it << ", " << split_k << ", " << ggml_type_name(quant) << ")" << std::endl;
  4246. #endif
  4247. const size_t x_ne = m * k * batch;
  4248. const size_t y_ne = k * n * batch;
  4249. const size_t d_ne = m * n * batch;
  4250. vk_pipeline p;
  4251. std::string shname;
  4252. if (shader_size == 0) {
  4253. p = ctx->device->pipeline_dequant_mul_mat_mat[quant]->a_s;
  4254. shname = std::string(ggml_type_name(quant)) + "_ALIGNED_S";
  4255. } else if (shader_size == 1) {
  4256. p = ctx->device->pipeline_dequant_mul_mat_mat[quant]->a_m;
  4257. shname = std::string(ggml_type_name(quant)) + "_ALIGNED_M";
  4258. } else if (shader_size == 2) {
  4259. p = ctx->device->pipeline_dequant_mul_mat_mat[quant]->a_l;
  4260. shname = std::string(ggml_type_name(quant)) + "_ALIGNED_L";
  4261. } else {
  4262. GGML_ASSERT(0);
  4263. }
  4264. const size_t kpad = ggml_vk_align_size(k, p->align);
  4265. if (k != kpad) {
  4266. if (shader_size == 0) {
  4267. p = ctx->device->pipeline_dequant_mul_mat_mat[quant]->s;
  4268. shname = std::string(ggml_type_name(quant)) + "_S";
  4269. } else if (shader_size == 1) {
  4270. p = ctx->device->pipeline_dequant_mul_mat_mat[quant]->m;
  4271. shname = std::string(ggml_type_name(quant)) + "_M";
  4272. } else if (shader_size == 2) {
  4273. p = ctx->device->pipeline_dequant_mul_mat_mat[quant]->l;
  4274. shname = std::string(ggml_type_name(quant)) + "_L";
  4275. } else {
  4276. GGML_ASSERT(0);
  4277. }
  4278. }
  4279. const size_t x_sz = sizeof(float) * x_ne;
  4280. const size_t y_sz = sizeof(float) * y_ne;
  4281. const size_t qx_sz = x_ne * ggml_type_size(quant)/ggml_blck_size(quant);
  4282. const size_t d_sz = sizeof(float) * d_ne;
  4283. float * x = (float *) malloc(x_sz);
  4284. float * y = (float *) malloc(y_sz);
  4285. void * qx = malloc(qx_sz);
  4286. vk_buffer qx_buf = ggml_vk_create_buffer_check(ctx, qx_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  4287. vk_buffer y_buf = ggml_vk_create_buffer_check(ctx, y_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  4288. vk_buffer d_buf = ggml_vk_create_buffer_check(ctx, d_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  4289. float * d = (float *) malloc(d_sz);
  4290. float * d_chk = (float *) malloc(d_sz);
  4291. for (size_t i = 0; i < x_ne; i++) {
  4292. x[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  4293. }
  4294. ggml_vk_quantize_data(x, qx, x_ne, quant);
  4295. for (size_t i = 0; i < y_ne; i++) {
  4296. // y[i] = rand() / (float)RAND_MAX;
  4297. y[i] = (i % k == i / k) ? 1.0f : 0.0f;
  4298. }
  4299. ggml_pipeline_allocate_descriptor_sets(ctx, p, num_it);
  4300. if (split_k > 1) {
  4301. ggml_pipeline_allocate_descriptor_sets(ctx, ctx->device->pipeline_matmul_split_k_reduce, num_it);
  4302. if (ctx->prealloc_split_k == nullptr || ctx->prealloc_split_k->size < sizeof(float) * d_ne * split_k) {
  4303. // Resize buffer
  4304. if (ctx->prealloc_split_k != nullptr) {
  4305. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  4306. }
  4307. ctx->prealloc_split_k = ggml_vk_create_buffer_check(ctx, sizeof(float) * d_ne * split_k, vk::MemoryPropertyFlagBits::eDeviceLocal);
  4308. }
  4309. }
  4310. ggml_vk_buffer_write(ctx, qx_buf, 0, qx, qx_sz);
  4311. ggml_vk_buffer_write(ctx, y_buf, 0, y, y_sz);
  4312. vk_context * subctx = ggml_vk_create_context(ctx, ctx->device->compute_queue);
  4313. for (size_t i = 0; i < num_it; i++) {
  4314. ggml_vk_ctx_begin(ctx, subctx);
  4315. ggml_vk_matmul(
  4316. ctx, subctx, p, ggml_vk_subbuffer(qx_buf), ggml_vk_subbuffer(y_buf), ggml_vk_subbuffer(d_buf), ggml_vk_subbuffer(ctx->prealloc_split_k),
  4317. m, n, k,
  4318. k, k, m, k*m, k*n, m*n,
  4319. split_k, batch, batch, batch, 1, 1
  4320. );
  4321. ggml_vk_ctx_end(subctx);
  4322. }
  4323. auto begin = std::chrono::high_resolution_clock::now();
  4324. ggml_vk_submit(subctx, ctx->fence);
  4325. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_dequant waitForFences");
  4326. ctx->device->device.resetFences({ ctx->fence });
  4327. auto end = std::chrono::high_resolution_clock::now();
  4328. double time_ms = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  4329. ggml_vk_buffer_read(ctx, d_buf, 0, d, d_sz);
  4330. ggml_init_params iparams = {
  4331. /*.mem_size =*/ 1024*1024*1024,
  4332. /*.mem_buffer =*/ NULL,
  4333. /*.no_alloc =*/ true,
  4334. };
  4335. ggml_context * ggml_ctx = ggml_init(iparams);
  4336. ggml_tensor * src0_ggml = ggml_new_tensor_3d(ggml_ctx, quant, k, m, batch);
  4337. ggml_tensor * src1_ggml = ggml_new_tensor_3d(ggml_ctx, GGML_TYPE_F32, k, n, batch);
  4338. ggml_tensor * tensor_ggml = ggml_mul_mat(ggml_ctx, src0_ggml, src1_ggml);
  4339. src0_ggml->data = qx;
  4340. src1_ggml->data = y;
  4341. tensor_ggml->data = d_chk;
  4342. ggml_cgraph * cgraph = ggml_new_graph(ggml_ctx);
  4343. ggml_build_forward_expand(cgraph, tensor_ggml);
  4344. ggml_graph_compute_with_ctx(ggml_ctx, cgraph, 1);
  4345. ggml_free(ggml_ctx);
  4346. double avg_err = 0.0;
  4347. int first_err_n = -1;
  4348. int first_err_m = -1;
  4349. int first_err_b = -1;
  4350. for (size_t i = 0; i < m*n*batch; i++) {
  4351. double err = std::fabs(d[i] - d_chk[i]);
  4352. avg_err += err;
  4353. if ((err > 0.05f || std::isnan(err)) && first_err_n == -1) {
  4354. first_err_b = i / (m * n);
  4355. first_err_n = (i % (m * n)) / m;
  4356. first_err_m = (i % (m * n)) % m;
  4357. }
  4358. }
  4359. avg_err /= m * n;
  4360. std::cerr << "TEST MMQ " << shname << " m=" << m << " n=" << n << " k=" << k << " batch=" << batch << " split_k=" << split_k << " matmul " << time_ms / num_it << "ms avg_err=" << avg_err << std::endl;
  4361. if (avg_err > 0.01 || std::isnan(avg_err)) {
  4362. std::cerr << "m = " << first_err_m << " n = " << first_err_n << " b = " << first_err_b << std::endl;
  4363. std::cerr << "Actual result: " << std::endl << std::endl;
  4364. ggml_vk_print_matrix_area(d, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  4365. std::cerr << std::endl;
  4366. std::cerr << "Expected result: " << std::endl << std::endl;
  4367. ggml_vk_print_matrix_area(d_chk, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  4368. if (split_k > 1) {
  4369. float * split_k_buf = (float *) malloc(sizeof(float) * d_ne * split_k);
  4370. ggml_vk_buffer_read(ctx, ctx->prealloc_split_k, 0, split_k_buf, sizeof(float) * d_ne * split_k);
  4371. std::cerr << "d_buf0: " << std::endl << std::endl;
  4372. ggml_vk_print_matrix_area(split_k_buf, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  4373. std::cerr << "d_buf1: " << std::endl << std::endl;
  4374. ggml_vk_print_matrix_area(split_k_buf + d_ne, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  4375. std::cerr << "d_buf2: " << std::endl << std::endl;
  4376. 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);
  4377. std::cerr << "d_buf3: " << std::endl << std::endl;
  4378. 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);
  4379. free(split_k_buf);
  4380. }
  4381. }
  4382. ggml_vk_destroy_buffer(qx_buf);
  4383. ggml_vk_destroy_buffer(y_buf);
  4384. ggml_vk_destroy_buffer(d_buf);
  4385. free(x);
  4386. free(qx);
  4387. free(y);
  4388. free(d);
  4389. free(d_chk);
  4390. }
  4391. #endif
  4392. static ggml_tensor_extra_gpu * ggml_vk_tensor_create_extra(ggml_tensor * tensor) {
  4393. #ifdef GGML_VULKAN_DEBUG
  4394. std::cerr << "ggml_vk_create_extra(" << tensor << " (" << tensor->name << ", " << ggml_op_name(tensor->op) << "))" << std::endl;
  4395. #endif
  4396. ggml_tensor_extra_gpu * extra = new ggml_tensor_extra_gpu;
  4397. extra->reset();
  4398. tensor->extra = extra;
  4399. return extra;
  4400. }
  4401. static void ggml_vk_preallocate_buffers_graph(ggml_backend_vk_context * ctx, ggml_tensor * node){
  4402. #ifdef GGML_VULKAN_DEBUG
  4403. std::cerr << "ggml_vk_preallocate_buffers_graph(" << node << ")" << std::endl;
  4404. #endif
  4405. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) node->extra;
  4406. if (extra == nullptr) {
  4407. return;
  4408. }
  4409. ggml_tensor * src0 = node->src[0];
  4410. ggml_tensor * src1 = node->src[1];
  4411. const bool use_src0 = src0 != nullptr;
  4412. const int64_t ne00 = use_src0 ? src0->ne[0] : 0;
  4413. const int64_t ne01 = use_src0 ? src0->ne[1] : 0;
  4414. const int64_t ne02 = use_src0 ? src0->ne[2] : 0;
  4415. const int64_t ne03 = use_src0 ? src0->ne[3] : 0;
  4416. const bool use_src1 = src1 != nullptr && node->op != GGML_OP_CPY && node->op != GGML_OP_CONT && node->op != GGML_OP_DUP;
  4417. const int64_t ne10 = use_src1 ? src1->ne[0] : 0;
  4418. const int64_t ne11 = use_src1 ? src1->ne[1] : 0;
  4419. const int64_t ne12 = use_src1 ? src1->ne[2] : 0;
  4420. const int64_t ne13 = use_src1 ? src1->ne[3] : 0;
  4421. const int64_t ne20 = node->ne[0];
  4422. const int64_t ne21 = node->ne[1];
  4423. const int64_t ne22 = node->ne[2];
  4424. const int64_t ne23 = node->ne[3];
  4425. const ggml_type src0_type = (use_src0 && src0->type == GGML_TYPE_F32) ? src0->type : GGML_TYPE_F16;
  4426. const ggml_type src1_type = (use_src1 && src1->type == GGML_TYPE_F32) ? src1->type : GGML_TYPE_F16;
  4427. const bool x_non_contig = use_src0 && !ggml_vk_dim01_contiguous(src0);
  4428. const bool y_non_contig = use_src1 && !ggml_vk_dim01_contiguous(src1);
  4429. const bool y_f32_kernel = use_src1 && src1->type == GGML_TYPE_F32 && !y_non_contig;
  4430. bool mmp = (use_src0 && use_src1 && src1_type == GGML_TYPE_F32) ? ggml_vk_get_mul_mat_mat_pipeline(ctx, src0_type, y_non_contig ? GGML_TYPE_F16 : src1->type) != nullptr : false;
  4431. const bool qx_needs_dequant = use_src0 && (mmp || x_non_contig);
  4432. const bool qy_needs_dequant = use_src1 && ((src1->type != GGML_TYPE_F16 && !y_f32_kernel) || y_non_contig);
  4433. int split_k;
  4434. if (node->op == GGML_OP_MUL_MAT || node->op == GGML_OP_MUL_MAT_ID) {
  4435. split_k = ggml_vk_guess_split_k(ne01, ne11, ne10);
  4436. } else {
  4437. split_k = 1;
  4438. }
  4439. const uint32_t x_ne = ne00 * ne01;
  4440. const uint32_t y_ne = ne10 * ne11;
  4441. const uint32_t d_ne = ne20 * ne21;
  4442. const uint64_t x_sz = (use_src0 && qx_needs_dequant) ? ggml_vk_align_size(sizeof(src0_type) * x_ne, ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ne02 * ne03 : 0;
  4443. const uint64_t y_sz = (use_src1 && qy_needs_dequant) ? ggml_vk_align_size(sizeof(src1_type) * y_ne, ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ne12 * ne13 : 0;
  4444. uint64_t d_sz = ggml_vk_align_size(ggml_type_size(node->type) * d_ne, ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ne22 * ne23;
  4445. const uint64_t split_k_size = split_k > 1 ? d_sz * 4 : 0;
  4446. if (extra->buffer_gpu.expired()) {
  4447. // Workaround for CPU backend BLAS matmul calls
  4448. extra->buffer_gpu = ggml_vk_create_buffer_temp(ctx, d_sz);
  4449. }
  4450. switch (node->op) {
  4451. case GGML_OP_REPEAT:
  4452. case GGML_OP_GET_ROWS:
  4453. case GGML_OP_RESHAPE:
  4454. case GGML_OP_VIEW:
  4455. case GGML_OP_PERMUTE:
  4456. case GGML_OP_TRANSPOSE:
  4457. case GGML_OP_ADD:
  4458. case GGML_OP_SCALE:
  4459. case GGML_OP_SQR:
  4460. case GGML_OP_CLAMP:
  4461. case GGML_OP_CPY:
  4462. case GGML_OP_CONT:
  4463. case GGML_OP_DUP:
  4464. case GGML_OP_MUL:
  4465. case GGML_OP_DIV:
  4466. case GGML_OP_NORM:
  4467. case GGML_OP_RMS_NORM:
  4468. case GGML_OP_DIAG_MASK_INF:
  4469. case GGML_OP_SOFT_MAX:
  4470. case GGML_OP_ROPE:
  4471. case GGML_OP_ARGSORT:
  4472. case GGML_OP_SUM_ROWS:
  4473. break;
  4474. case GGML_OP_UNARY:
  4475. switch (ggml_get_unary_op(node)) {
  4476. case GGML_UNARY_OP_SILU:
  4477. case GGML_UNARY_OP_GELU:
  4478. case GGML_UNARY_OP_RELU:
  4479. break;
  4480. default:
  4481. return;
  4482. }
  4483. break;
  4484. case GGML_OP_MUL_MAT:
  4485. case GGML_OP_MUL_MAT_ID:
  4486. if (ctx->prealloc_size_x < x_sz) {
  4487. ctx->prealloc_size_x = x_sz;
  4488. }
  4489. if (ctx->prealloc_size_y < y_sz) {
  4490. ctx->prealloc_size_y = y_sz;
  4491. }
  4492. if (ctx->prealloc_size_split_k < split_k_size) {
  4493. ctx->prealloc_size_split_k = split_k_size;
  4494. }
  4495. if (ctx->staging_size < x_sz + y_sz) {
  4496. ctx->staging_size = x_sz + y_sz;
  4497. }
  4498. break;
  4499. default:
  4500. return;
  4501. }
  4502. }
  4503. static void ggml_vk_preallocate_buffers(ggml_backend_vk_context * ctx) {
  4504. #ifdef GGML_VULKAN_DEBUG
  4505. std::cerr << "ggml_vk_preallocate_buffers(x_size: " << ctx->prealloc_size_x << " y_size: " << ctx->prealloc_size_y << " split_k_size: " << ctx->prealloc_size_split_k << ")" << std::endl;
  4506. #endif
  4507. #if defined(GGML_VULKAN_RUN_TESTS)
  4508. ctx->staging = ggml_vk_create_buffer_check(ctx, 100ul * 1024ul * 1024ul,
  4509. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostCached,
  4510. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
  4511. ggml_vk_test_transfer(ctx, 8192 * 1000, false);
  4512. ggml_vk_test_transfer(ctx, 8192 * 1000, true);
  4513. ggml_vk_test_dequant(ctx, 7680, GGML_TYPE_F32);
  4514. ggml_vk_test_dequant(ctx, 7680, GGML_TYPE_Q4_0);
  4515. ggml_vk_test_dequant(ctx, 7680, GGML_TYPE_Q4_1);
  4516. ggml_vk_test_dequant(ctx, 7680, GGML_TYPE_Q5_0);
  4517. ggml_vk_test_dequant(ctx, 7680, GGML_TYPE_Q5_1);
  4518. ggml_vk_test_dequant(ctx, 7680, GGML_TYPE_Q8_0);
  4519. ggml_vk_test_dequant(ctx, 7680, GGML_TYPE_Q2_K);
  4520. ggml_vk_test_dequant(ctx, 7680, GGML_TYPE_Q3_K);
  4521. ggml_vk_test_dequant(ctx, 7680, GGML_TYPE_Q4_K);
  4522. ggml_vk_test_dequant(ctx, 7680, GGML_TYPE_Q5_K);
  4523. ggml_vk_test_dequant(ctx, 7680, GGML_TYPE_Q6_K);
  4524. ggml_vk_test_matmul<ggml_fp16_t, ggml_fp16_t>(ctx, 512, 512, 100, 32, 100, 1, 2);
  4525. ggml_vk_test_matmul<float, float>(ctx, 128, 512, 512, 2, 100, 1, 0);
  4526. ggml_vk_test_matmul<float, float>(ctx, 128, 512, 512, 2, 100, 1, 1);
  4527. ggml_vk_test_matmul<float, float>(ctx, 128, 512, 512, 2, 100, 1, 2);
  4528. ggml_vk_test_matmul<float, float>(ctx, 128, 512, 512, 2, 100, 4, 0);
  4529. ggml_vk_test_matmul<float, float>(ctx, 128, 512, 512, 2, 100, 4, 1);
  4530. ggml_vk_test_matmul<float, float>(ctx, 128, 512, 512, 2, 100, 4, 2);
  4531. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 0, GGML_TYPE_Q4_0);
  4532. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 1, GGML_TYPE_Q4_0);
  4533. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 2, GGML_TYPE_Q4_0);
  4534. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 0, GGML_TYPE_Q4_0);
  4535. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 1, GGML_TYPE_Q4_0);
  4536. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 2, GGML_TYPE_Q4_0);
  4537. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 0, GGML_TYPE_Q4_1);
  4538. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 1, GGML_TYPE_Q4_1);
  4539. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 2, GGML_TYPE_Q4_1);
  4540. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 0, GGML_TYPE_Q4_1);
  4541. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 1, GGML_TYPE_Q4_1);
  4542. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 2, GGML_TYPE_Q4_1);
  4543. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 0, GGML_TYPE_Q5_0);
  4544. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 1, GGML_TYPE_Q5_0);
  4545. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 2, GGML_TYPE_Q5_0);
  4546. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 0, GGML_TYPE_Q5_0);
  4547. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 1, GGML_TYPE_Q5_0);
  4548. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 2, GGML_TYPE_Q5_0);
  4549. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 0, GGML_TYPE_Q5_1);
  4550. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 1, GGML_TYPE_Q5_1);
  4551. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 2, GGML_TYPE_Q5_1);
  4552. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 0, GGML_TYPE_Q5_1);
  4553. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 1, GGML_TYPE_Q5_1);
  4554. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 2, GGML_TYPE_Q5_1);
  4555. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 0, GGML_TYPE_Q8_0);
  4556. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 1, GGML_TYPE_Q8_0);
  4557. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 2, GGML_TYPE_Q8_0);
  4558. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 0, GGML_TYPE_Q8_0);
  4559. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 1, GGML_TYPE_Q8_0);
  4560. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 2, GGML_TYPE_Q8_0);
  4561. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 0, GGML_TYPE_Q2_K);
  4562. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 1, GGML_TYPE_Q2_K);
  4563. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 2, GGML_TYPE_Q2_K);
  4564. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 0, GGML_TYPE_Q2_K);
  4565. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 1, GGML_TYPE_Q2_K);
  4566. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 2, GGML_TYPE_Q2_K);
  4567. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 0, GGML_TYPE_Q3_K);
  4568. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 1, GGML_TYPE_Q3_K);
  4569. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 2, GGML_TYPE_Q3_K);
  4570. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 0, GGML_TYPE_Q3_K);
  4571. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 1, GGML_TYPE_Q3_K);
  4572. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 2, GGML_TYPE_Q3_K);
  4573. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 0, GGML_TYPE_Q4_K);
  4574. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 1, GGML_TYPE_Q4_K);
  4575. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 2, GGML_TYPE_Q4_K);
  4576. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 0, GGML_TYPE_Q4_K);
  4577. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 1, GGML_TYPE_Q4_K);
  4578. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 2, GGML_TYPE_Q4_K);
  4579. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 0, GGML_TYPE_Q5_K);
  4580. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 1, GGML_TYPE_Q5_K);
  4581. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 2, GGML_TYPE_Q5_K);
  4582. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 0, GGML_TYPE_Q5_K);
  4583. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 1, GGML_TYPE_Q5_K);
  4584. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 2, GGML_TYPE_Q5_K);
  4585. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 0, GGML_TYPE_Q6_K);
  4586. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 1, GGML_TYPE_Q6_K);
  4587. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 2, GGML_TYPE_Q6_K);
  4588. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 0, GGML_TYPE_Q6_K);
  4589. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 1, GGML_TYPE_Q6_K);
  4590. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 2, GGML_TYPE_Q6_K);
  4591. std::cerr << std::endl;
  4592. const std::vector<size_t> vals {
  4593. 8, 8, 8,
  4594. 100, 46, 576,
  4595. 623, 111, 128,
  4596. 100, 46, 558,
  4597. 512, 1, 256,
  4598. 128, 110, 622,
  4599. 511, 511, 127,
  4600. 511, 511, 7,
  4601. 511, 511, 17,
  4602. 49, 49, 128,
  4603. 128, 49, 49,
  4604. 4096, 49, 4096,
  4605. 11008, 49, 4096,
  4606. 4096, 49, 11008,
  4607. 32000, 49, 4096,
  4608. 512, 512, 128,
  4609. 128, 512, 512,
  4610. 4096, 512, 4096,
  4611. 11008, 512, 4096,
  4612. 4096, 512, 11008,
  4613. 32000, 512, 4096,
  4614. };
  4615. const size_t num_it = 1;
  4616. for (size_t i = 0; i < vals.size(); i += 3) {
  4617. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 0);
  4618. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 1);
  4619. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 2);
  4620. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 0);
  4621. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 1);
  4622. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 2);
  4623. std::cerr << std::endl;
  4624. }
  4625. GGML_ASSERT(false);
  4626. #endif
  4627. if (ctx->prealloc_x == nullptr || (ctx->prealloc_size_x > 0 && ctx->prealloc_x->size < ctx->prealloc_size_x)) {
  4628. // Resize buffer
  4629. if (ctx->prealloc_x != nullptr) {
  4630. ggml_vk_destroy_buffer(ctx->prealloc_x);
  4631. }
  4632. ctx->prealloc_x = ggml_vk_create_buffer_device(ctx, ctx->prealloc_size_x);
  4633. }
  4634. if (ctx->prealloc_y == nullptr || (ctx->prealloc_size_y > 0 && ctx->prealloc_y->size < ctx->prealloc_size_y)) {
  4635. // Resize buffer
  4636. if (ctx->prealloc_y != nullptr) {
  4637. ggml_vk_destroy_buffer(ctx->prealloc_y);
  4638. }
  4639. ctx->prealloc_y = ggml_vk_create_buffer_device(ctx, ctx->prealloc_size_y);
  4640. }
  4641. if (ctx->prealloc_split_k == nullptr || (ctx->prealloc_size_split_k > 0 && ctx->prealloc_split_k->size < ctx->prealloc_size_split_k)) {
  4642. // Resize buffer
  4643. if (ctx->prealloc_split_k != nullptr) {
  4644. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  4645. }
  4646. ctx->prealloc_split_k = ggml_vk_create_buffer_device(ctx, ctx->prealloc_size_split_k);
  4647. }
  4648. if (ctx->staging == nullptr || (ctx->staging_size > 0 && ctx->staging->size < ctx->staging_size)) {
  4649. // Resize buffer
  4650. if (ctx->staging != nullptr) {
  4651. ggml_vk_destroy_buffer(ctx->staging);
  4652. }
  4653. ctx->staging = ggml_vk_create_buffer_check(ctx, ctx->staging_size,
  4654. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostCached,
  4655. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
  4656. }
  4657. }
  4658. static void ggml_vk_build_graph(ggml_backend_vk_context * ctx, ggml_tensor * node, bool last_node){
  4659. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) node->extra;
  4660. if (ggml_is_empty(node) || extra == nullptr) {
  4661. return;
  4662. }
  4663. #ifdef GGML_VULKAN_DEBUG
  4664. std::cerr << "ggml_vk_build_graph(" << node << ", " << ggml_op_name(node->op) << ")" << std::endl;
  4665. #endif
  4666. ctx->semaphore_idx = 0;
  4667. ctx->staging_offset = 0;
  4668. const ggml_tensor * src0 = node->src[0];
  4669. const ggml_tensor * src1 = node->src[1];
  4670. const ggml_tensor * src2 = node->src[2];
  4671. switch (node->op) {
  4672. // Return on empty ops to avoid generating a compute_ctx and setting exit_tensor
  4673. case GGML_OP_RESHAPE:
  4674. case GGML_OP_VIEW:
  4675. case GGML_OP_PERMUTE:
  4676. case GGML_OP_TRANSPOSE:
  4677. case GGML_OP_NONE:
  4678. return;
  4679. case GGML_OP_UNARY:
  4680. switch (ggml_get_unary_op(node)) {
  4681. case GGML_UNARY_OP_SILU:
  4682. case GGML_UNARY_OP_GELU:
  4683. case GGML_UNARY_OP_RELU:
  4684. break;
  4685. default:
  4686. return;
  4687. }
  4688. break;
  4689. case GGML_OP_REPEAT:
  4690. case GGML_OP_GET_ROWS:
  4691. case GGML_OP_ADD:
  4692. case GGML_OP_MUL:
  4693. case GGML_OP_DIV:
  4694. case GGML_OP_SCALE:
  4695. case GGML_OP_SQR:
  4696. case GGML_OP_CLAMP:
  4697. case GGML_OP_CPY:
  4698. case GGML_OP_CONT:
  4699. case GGML_OP_DUP:
  4700. case GGML_OP_NORM:
  4701. case GGML_OP_RMS_NORM:
  4702. case GGML_OP_DIAG_MASK_INF:
  4703. case GGML_OP_SOFT_MAX:
  4704. case GGML_OP_ROPE:
  4705. case GGML_OP_MUL_MAT:
  4706. case GGML_OP_MUL_MAT_ID:
  4707. case GGML_OP_ARGSORT:
  4708. case GGML_OP_SUM_ROWS:
  4709. break;
  4710. default:
  4711. std::cerr << "ggml_vulkan: Error: Missing op: " << ggml_op_name(node->op) << std::endl;
  4712. GGML_ASSERT(false);
  4713. return;
  4714. }
  4715. if (ctx->compute_ctx == nullptr) {
  4716. ctx->compute_ctx = ggml_vk_create_context(ctx, ctx->device->compute_queue);
  4717. ggml_vk_ctx_begin(ctx, ctx->compute_ctx);
  4718. }
  4719. switch (node->op) {
  4720. case GGML_OP_REPEAT:
  4721. ggml_vk_repeat(ctx, ctx->compute_ctx, src0, src1, node);
  4722. break;
  4723. case GGML_OP_GET_ROWS:
  4724. ggml_vk_get_rows(ctx, ctx->compute_ctx, src0, src1, node);
  4725. break;
  4726. case GGML_OP_ADD:
  4727. ggml_vk_add(ctx, ctx->compute_ctx, src0, src1, node);
  4728. break;
  4729. case GGML_OP_MUL:
  4730. ggml_vk_mul(ctx, ctx->compute_ctx, src0, src1, node);
  4731. break;
  4732. case GGML_OP_DIV:
  4733. ggml_vk_div(ctx, ctx->compute_ctx, src0, src1, node);
  4734. break;
  4735. case GGML_OP_SCALE:
  4736. ggml_vk_scale(ctx, ctx->compute_ctx, src0, node);
  4737. break;
  4738. case GGML_OP_SQR:
  4739. ggml_vk_sqr(ctx, ctx->compute_ctx, src0, node);
  4740. break;
  4741. case GGML_OP_CLAMP:
  4742. ggml_vk_clamp(ctx, ctx->compute_ctx, src0, node);
  4743. break;
  4744. case GGML_OP_CPY:
  4745. case GGML_OP_CONT:
  4746. case GGML_OP_DUP:
  4747. ggml_vk_cpy(ctx, ctx->compute_ctx, src0, node);
  4748. break;
  4749. case GGML_OP_NORM:
  4750. ggml_vk_norm(ctx, ctx->compute_ctx, src0, node);
  4751. break;
  4752. case GGML_OP_RMS_NORM:
  4753. ggml_vk_rms_norm(ctx, ctx->compute_ctx, src0, node);
  4754. break;
  4755. case GGML_OP_UNARY:
  4756. switch (ggml_get_unary_op(node)) {
  4757. case GGML_UNARY_OP_SILU:
  4758. case GGML_UNARY_OP_GELU:
  4759. case GGML_UNARY_OP_RELU:
  4760. ggml_vk_unary(ctx, ctx->compute_ctx, src0, node);
  4761. break;
  4762. default:
  4763. return;
  4764. }
  4765. break;
  4766. case GGML_OP_DIAG_MASK_INF:
  4767. ggml_vk_diag_mask_inf(ctx, ctx->compute_ctx, src0, node);
  4768. break;
  4769. case GGML_OP_SOFT_MAX:
  4770. ggml_vk_soft_max(ctx, ctx->compute_ctx, src0, src1, node);
  4771. break;
  4772. case GGML_OP_ROPE:
  4773. ggml_vk_rope(ctx, ctx->compute_ctx, src0, src1, src2, node);
  4774. break;
  4775. case GGML_OP_ARGSORT:
  4776. ggml_vk_argsort(ctx, ctx->compute_ctx, src0, node);
  4777. break;
  4778. case GGML_OP_SUM_ROWS:
  4779. ggml_vk_sum_rows(ctx, ctx->compute_ctx, src0, node);
  4780. break;
  4781. case GGML_OP_MUL_MAT:
  4782. ggml_vk_mul_mat(ctx, ctx->compute_ctx, src0, src1, node);
  4783. break;
  4784. case GGML_OP_MUL_MAT_ID:
  4785. ggml_vk_mul_mat_id(ctx, ctx->compute_ctx, src0, src1, src2, node);
  4786. break;
  4787. default:
  4788. return;
  4789. }
  4790. extra->ctx_idx = ctx->compute_ctx->idx;
  4791. #ifdef GGML_VULKAN_CHECK_RESULTS
  4792. // Force context reset on each node so that each tensor ends up in its own context
  4793. // and can be run and compared to its CPU equivalent separately
  4794. last_node = true;
  4795. #endif
  4796. if (last_node) {
  4797. ggml_vk_ctx_end(ctx->compute_ctx);
  4798. ctx->compute_ctx->exit_tensor = node;
  4799. ctx->compute_ctx = nullptr;
  4800. }
  4801. }
  4802. static bool ggml_vk_compute_forward(ggml_backend_vk_context * ctx, ggml_compute_params * params, ggml_tensor * tensor){
  4803. ggml_tensor_extra_gpu * extra = nullptr;
  4804. switch (tensor->op) {
  4805. case GGML_OP_ADD:
  4806. case GGML_OP_GET_ROWS:
  4807. case GGML_OP_MUL:
  4808. case GGML_OP_DIV:
  4809. case GGML_OP_SCALE:
  4810. case GGML_OP_SQR:
  4811. case GGML_OP_CLAMP:
  4812. case GGML_OP_CPY:
  4813. case GGML_OP_CONT:
  4814. case GGML_OP_DUP:
  4815. case GGML_OP_NORM:
  4816. case GGML_OP_RMS_NORM:
  4817. case GGML_OP_DIAG_MASK_INF:
  4818. case GGML_OP_SOFT_MAX:
  4819. case GGML_OP_ROPE:
  4820. case GGML_OP_RESHAPE:
  4821. case GGML_OP_VIEW:
  4822. case GGML_OP_PERMUTE:
  4823. case GGML_OP_TRANSPOSE:
  4824. case GGML_OP_NONE:
  4825. case GGML_OP_ARGSORT:
  4826. case GGML_OP_SUM_ROWS:
  4827. extra = (ggml_tensor_extra_gpu *) tensor->extra;
  4828. break;
  4829. case GGML_OP_UNARY:
  4830. switch (ggml_get_unary_op(tensor)) {
  4831. case GGML_UNARY_OP_SILU:
  4832. case GGML_UNARY_OP_GELU:
  4833. case GGML_UNARY_OP_RELU:
  4834. extra = (ggml_tensor_extra_gpu *) tensor->extra;
  4835. break;
  4836. default:
  4837. return false;
  4838. }
  4839. break;
  4840. case GGML_OP_MUL_MAT:
  4841. case GGML_OP_MUL_MAT_ID:
  4842. extra = (ggml_tensor_extra_gpu *) tensor->extra;
  4843. break;
  4844. default:
  4845. return false;
  4846. }
  4847. if (extra == nullptr) {
  4848. return false;
  4849. }
  4850. if (params->ith != 0) {
  4851. return true;
  4852. }
  4853. if (params->type == GGML_TASK_TYPE_INIT || params->type == GGML_TASK_TYPE_FINALIZE) {
  4854. return true;
  4855. }
  4856. #ifdef GGML_VULKAN_DEBUG
  4857. std::cerr << "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 << ")" << std::endl;
  4858. #endif
  4859. #ifdef GGML_VULKAN_CHECK_RESULTS
  4860. ggml_vk_check_results_0(ctx, params, tensor);
  4861. #endif
  4862. vk_context& subctx = ctx->gc.contexts[extra->ctx_idx];
  4863. // Only run if ctx hasn't been submitted yet
  4864. if (!subctx.seqs.empty()) {
  4865. // Do staging buffer copies
  4866. for (auto& cpy : subctx.in_memcpys) {
  4867. memcpy(cpy.dst, cpy.src, cpy.n);
  4868. }
  4869. ggml_vk_submit(&subctx, ctx->fence);
  4870. }
  4871. if (tensor == subctx.exit_tensor) {
  4872. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_compute_forward waitForFences");
  4873. ctx->device->device.resetFences({ ctx->fence });
  4874. // Do staging buffer copies
  4875. for (auto& cpy : subctx.out_memcpys) {
  4876. memcpy(cpy.dst, cpy.src, cpy.n);
  4877. }
  4878. subctx.in_memcpys.clear();
  4879. subctx.out_memcpys.clear();
  4880. }
  4881. return true;
  4882. }
  4883. // Clean up after graph processing is done
  4884. static void ggml_vk_graph_cleanup(ggml_backend_vk_context * ctx) {
  4885. #ifdef GGML_VULKAN_DEBUG
  4886. std::cerr << "ggml_vk_graph_cleanup()" << std::endl;
  4887. #endif
  4888. for (auto& buffer : ctx->gc.temp_buffers) {
  4889. ggml_vk_pool_free(ctx, buffer);
  4890. }
  4891. ctx->gc.temp_buffers.clear();
  4892. for (auto& pipeline : ctx->device->pipelines) {
  4893. if (pipeline.expired()) {
  4894. continue;
  4895. }
  4896. vk_pipeline pl = pipeline.lock();
  4897. ggml_pipeline_cleanup(pl);
  4898. }
  4899. ggml_vk_queue_cleanup(ctx, ctx->device->compute_queue);
  4900. ggml_vk_queue_cleanup(ctx, ctx->device->transfer_queue);
  4901. for (size_t i = 0; i < ctx->gc.semaphores.size(); i++) {
  4902. ctx->device->device.destroySemaphore({ ctx->gc.semaphores[i].s });
  4903. }
  4904. ctx->gc.semaphores.clear();
  4905. for (size_t i = 0; i < ctx->gc.tl_semaphores.size(); i++) {
  4906. ctx->device->device.destroySemaphore({ ctx->gc.tl_semaphores[i].s });
  4907. }
  4908. ctx->gc.tl_semaphores.clear();
  4909. ctx->semaphore_idx = 0;
  4910. ctx->event_idx = 0;
  4911. for (auto& event : ctx->gc.events) {
  4912. ctx->device->device.resetEvent(event);
  4913. }
  4914. ctx->staging_offset = 0;
  4915. ctx->compute_ctx = nullptr;
  4916. ctx->transfer_ctx = nullptr;
  4917. ctx->gc.contexts.clear();
  4918. }
  4919. // Clean up on backend free
  4920. static void ggml_vk_cleanup(ggml_backend_vk_context * ctx) {
  4921. #ifdef GGML_VULKAN_DEBUG
  4922. std::cerr << "ggml_vk_cleanup(" << ctx->idx << ")" << std::endl;
  4923. #endif
  4924. ggml_vk_graph_cleanup(ctx);
  4925. ggml_vk_destroy_buffer(ctx->prealloc_x);
  4926. ggml_vk_destroy_buffer(ctx->prealloc_y);
  4927. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  4928. ggml_vk_destroy_buffer(ctx->staging);
  4929. ggml_vk_destroy_buffer(ctx->sync_staging);
  4930. for (auto& buffer : ctx->buffer_pool) {
  4931. ggml_vk_destroy_buffer(buffer);
  4932. }
  4933. ctx->prealloc_size_x = 0;
  4934. ctx->prealloc_size_y = 0;
  4935. ctx->prealloc_size_split_k = 0;
  4936. ctx->staging_size = 0;
  4937. for (auto& event : ctx->gc.events) {
  4938. ctx->device->device.destroyEvent(event);
  4939. }
  4940. ctx->gc.events.clear();
  4941. ctx->device->device.destroyFence(ctx->fence);
  4942. }
  4943. GGML_CALL static int ggml_vk_get_device_count() {
  4944. ggml_vk_instance_init();
  4945. return vk_instance.device_indices.size();
  4946. }
  4947. GGML_CALL static void ggml_vk_get_device_description(int device, char * description, size_t description_size) {
  4948. ggml_vk_instance_init();
  4949. std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
  4950. vk::PhysicalDeviceProperties props;
  4951. devices[device].getProperties(&props);
  4952. snprintf(description, description_size, "%s", props.deviceName.data());
  4953. }
  4954. // backend interface
  4955. #define UNUSED GGML_UNUSED
  4956. // device backend
  4957. static void * const vk_ptr_base = (void *)(uintptr_t) 0x1000; // NOLINT
  4958. struct ggml_backend_vk_buffer_context {
  4959. ggml_backend_vk_context * ctx;
  4960. vk_buffer dev_buffer;
  4961. ggml_tensor_extra_gpu * temp_tensor_extras = nullptr;
  4962. size_t temp_tensor_extra_index = 0;
  4963. std::string name;
  4964. ggml_backend_vk_buffer_context(ggml_backend_vk_context * ctx, vk_buffer&& dev_buffer, std::string& name) :
  4965. ctx(ctx),
  4966. dev_buffer(dev_buffer),
  4967. name(name) {
  4968. }
  4969. ~ggml_backend_vk_buffer_context() {
  4970. ggml_vk_destroy_buffer(dev_buffer);
  4971. if (temp_tensor_extras != nullptr) {
  4972. delete[] temp_tensor_extras;
  4973. }
  4974. }
  4975. ggml_tensor_extra_gpu * ggml_vk_alloc_temp_tensor_extra() {
  4976. if (temp_tensor_extras == nullptr) {
  4977. temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_VK_MAX_NODES];
  4978. }
  4979. size_t alloc_index = temp_tensor_extra_index;
  4980. temp_tensor_extra_index = (temp_tensor_extra_index + 1) % GGML_VK_MAX_NODES;
  4981. ggml_tensor_extra_gpu * extra = &temp_tensor_extras[alloc_index];
  4982. extra->reset();
  4983. return extra;
  4984. }
  4985. };
  4986. GGML_CALL static const char * ggml_backend_vk_buffer_get_name(ggml_backend_buffer_t buffer) {
  4987. ggml_backend_vk_buffer_context * ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  4988. return ctx->name.c_str();
  4989. }
  4990. GGML_CALL static bool ggml_backend_buffer_is_vk(ggml_backend_buffer_t buffer) {
  4991. return buffer->iface.get_name == ggml_backend_vk_buffer_get_name;
  4992. }
  4993. GGML_CALL static void ggml_backend_vk_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  4994. #ifdef GGML_VULKAN_DEBUG
  4995. std::cerr << "ggml_backend_vk_buffer_free_buffer()" << std::endl;
  4996. #endif
  4997. ggml_backend_vk_buffer_context * ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  4998. ggml_vk_destroy_buffer(ctx->dev_buffer);
  4999. delete ctx;
  5000. }
  5001. GGML_CALL static void * ggml_backend_vk_buffer_get_base(ggml_backend_buffer_t buffer) {
  5002. return vk_ptr_base;
  5003. UNUSED(buffer);
  5004. }
  5005. GGML_CALL static void ggml_backend_vk_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
  5006. #ifdef GGML_VULKAN_DEBUG
  5007. std::cerr << "ggml_backend_vk_buffer_init_tensor(" << buffer << " (" << buffer->context << "), " << tensor << ")" << std::endl;
  5008. #endif
  5009. ggml_backend_vk_buffer_context * ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  5010. if (tensor->view_src != nullptr) {
  5011. GGML_ASSERT(tensor->view_src->buffer->buft == buffer->buft);
  5012. GGML_ASSERT(tensor->view_src->extra != nullptr);
  5013. tensor->extra = tensor->view_src->extra;
  5014. } else {
  5015. ggml_tensor_extra_gpu * extra = ctx->ggml_vk_alloc_temp_tensor_extra();
  5016. extra->buffer_gpu = ctx->dev_buffer;
  5017. extra->offset = (uint8_t *) tensor->data - (uint8_t *) vk_ptr_base;
  5018. tensor->extra = extra;
  5019. }
  5020. }
  5021. GGML_CALL 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) {
  5022. #ifdef GGML_VULKAN_DEBUG
  5023. std::cerr << "ggml_backend_vk_buffer_set_tensor(" << buffer << ", " << tensor << ", " << data << ", " << offset << ", " << size << ")" << std::endl;
  5024. #endif
  5025. ggml_backend_vk_buffer_context * ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  5026. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  5027. vk_buffer buf = extra->buffer_gpu.lock();
  5028. ggml_vk_buffer_write(ctx->ctx, buf, extra->offset + tensor->view_offs + offset, data, size);
  5029. }
  5030. GGML_CALL 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) {
  5031. #ifdef GGML_VULKAN_DEBUG
  5032. std::cerr << "ggml_backend_vk_buffer_get_tensor(" << buffer << ", " << tensor << ", " << data << ", " << offset << ", " << size << ")" << std::endl;
  5033. #endif
  5034. ggml_backend_vk_buffer_context * ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  5035. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  5036. vk_buffer buf = extra->buffer_gpu.lock();
  5037. ggml_vk_buffer_read(ctx->ctx, buf, extra->offset + tensor->view_offs + offset, data, size);
  5038. }
  5039. GGML_CALL static bool ggml_backend_vk_buffer_cpy_tensor(ggml_backend_buffer_t buffer, const ggml_tensor * src, ggml_tensor * dst) {
  5040. if (ggml_backend_buffer_is_vk(src->buffer)) {
  5041. ggml_tensor_extra_gpu * src_extra = (ggml_tensor_extra_gpu *) src->extra;
  5042. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  5043. vk_buffer src_buf = src_extra->buffer_gpu.lock();
  5044. vk_buffer dst_buf = dst_extra->buffer_gpu.lock();
  5045. ggml_vk_buffer_copy(dst_buf, dst_extra->offset + dst->view_offs, src_buf, src_extra->offset + src->view_offs, ggml_nbytes(src));
  5046. return true;
  5047. }
  5048. return false;
  5049. UNUSED(buffer);
  5050. }
  5051. GGML_CALL static void ggml_backend_vk_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  5052. ggml_backend_vk_buffer_context * ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  5053. ggml_vk_buffer_memset(ctx->ctx, ctx->dev_buffer, 0, value, buffer->size);
  5054. }
  5055. static ggml_backend_buffer_i ggml_backend_vk_buffer_interface = {
  5056. /* .get_name = */ ggml_backend_vk_buffer_get_name,
  5057. /* .free_buffer = */ ggml_backend_vk_buffer_free_buffer,
  5058. /* .get_base = */ ggml_backend_vk_buffer_get_base,
  5059. /* .init_tensor = */ ggml_backend_vk_buffer_init_tensor,
  5060. /* .set_tensor = */ ggml_backend_vk_buffer_set_tensor,
  5061. /* .get_tensor = */ ggml_backend_vk_buffer_get_tensor,
  5062. /* .cpy_tensor = */ ggml_backend_vk_buffer_cpy_tensor,
  5063. /* .clear = */ ggml_backend_vk_buffer_clear,
  5064. /* .reset = */ NULL,
  5065. };
  5066. // vk buffer type
  5067. struct ggml_backend_vk_buffer_type_context {
  5068. std::string name;
  5069. ggml_backend_vk_context * ctx;
  5070. };
  5071. GGML_CALL static const char * ggml_backend_vk_buffer_type_name(ggml_backend_buffer_type_t buft) {
  5072. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *)buft->context;
  5073. return ctx->name.c_str();
  5074. }
  5075. GGML_CALL static ggml_backend_buffer_t ggml_backend_vk_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  5076. #ifdef GGML_VULKAN_DEBUG
  5077. std::cerr << "ggml_backend_vk_buffer_type_alloc_buffer(" << size << ")" << std::endl;
  5078. #endif
  5079. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *) buft->context;
  5080. vk_buffer dev_buffer = nullptr;
  5081. try {
  5082. dev_buffer = ggml_vk_create_buffer_device(ctx->ctx, size);
  5083. } catch (const vk::SystemError& e) {
  5084. return nullptr;
  5085. }
  5086. ggml_backend_vk_buffer_context * bufctx = new ggml_backend_vk_buffer_context(ctx->ctx, std::move(dev_buffer), ctx->name);
  5087. return ggml_backend_buffer_init(buft, ggml_backend_vk_buffer_interface, bufctx, size);
  5088. }
  5089. GGML_CALL static size_t ggml_backend_vk_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  5090. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *) buft->context;
  5091. return ctx->ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  5092. }
  5093. GGML_CALL static size_t ggml_backend_vk_buffer_type_get_max_size(ggml_backend_buffer_type_t buft) {
  5094. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *) buft->context;
  5095. return ctx->ctx->device->max_memory_allocation_size;
  5096. }
  5097. GGML_CALL static size_t ggml_backend_vk_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  5098. return ggml_nbytes(tensor);
  5099. UNUSED(buft);
  5100. }
  5101. GGML_CALL static bool ggml_backend_vk_buffer_type_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  5102. if (!ggml_backend_is_vk(backend)) {
  5103. return false;
  5104. }
  5105. ggml_backend_vk_buffer_type_context * buft_ctx = (ggml_backend_vk_buffer_type_context *)buft->context;
  5106. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  5107. return buft_ctx->ctx->idx == ctx->idx;
  5108. }
  5109. static ggml_backend_buffer_type_i ggml_backend_vk_buffer_type_interface = {
  5110. /* .get_name = */ ggml_backend_vk_buffer_type_name,
  5111. /* .alloc_buffer = */ ggml_backend_vk_buffer_type_alloc_buffer,
  5112. /* .get_alignment = */ ggml_backend_vk_buffer_type_get_alignment,
  5113. /* .get_max_size = */ ggml_backend_vk_buffer_type_get_max_size,
  5114. /* .get_alloc_size = */ ggml_backend_vk_buffer_type_get_alloc_size,
  5115. /* .supports_backend = */ ggml_backend_vk_buffer_type_supports_backend,
  5116. /* .is_host = */ NULL,
  5117. };
  5118. GGML_CALL ggml_backend_buffer_type_t ggml_backend_vk_buffer_type(size_t dev_num) {
  5119. ggml_vk_instance_init();
  5120. #ifdef GGML_VULKAN_DEBUG
  5121. std::cerr << "ggml_backend_vk_buffer_type(" << dev_num << ")" << std::endl;
  5122. #endif
  5123. GGML_ASSERT(dev_num < vk_instance.device_indices.size());
  5124. ggml_backend_vk_init(dev_num);
  5125. return &vk_instance.buffer_types[dev_num];
  5126. }
  5127. // host buffer type
  5128. GGML_CALL static const char * ggml_backend_vk_host_buffer_type_name(ggml_backend_buffer_type_t buft) {
  5129. return GGML_VK_NAME "_Host";
  5130. UNUSED(buft);
  5131. }
  5132. GGML_CALL static const char * ggml_backend_vk_host_buffer_name(ggml_backend_buffer_t buffer) {
  5133. return GGML_VK_NAME "_Host";
  5134. UNUSED(buffer);
  5135. }
  5136. GGML_CALL static void ggml_backend_vk_host_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  5137. #ifdef GGML_VULKAN_DEBUG
  5138. std::cerr << "ggml_backend_vk_host_buffer_free_buffer()" << std::endl;
  5139. #endif
  5140. ggml_vk_host_free(&vk_instance.contexts[0], buffer->context);
  5141. }
  5142. GGML_CALL static ggml_backend_buffer_t ggml_backend_vk_host_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  5143. #ifdef GGML_VULKAN_DEBUG
  5144. std::cerr << "ggml_backend_vk_host_buffer_type_alloc_buffer(" << size << ")" << std::endl;
  5145. #endif
  5146. size += 32; // Behave like the CPU buffer type
  5147. void * ptr = nullptr;
  5148. try {
  5149. ptr = ggml_vk_host_malloc(&vk_instance.contexts[0], size);
  5150. } catch (vk::SystemError& e) {
  5151. std::cerr << "ggml_vulkan: Failed to allocate pinned memory." << std::endl;
  5152. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  5153. // fallback to cpu buffer
  5154. return ggml_backend_buft_alloc_buffer(ggml_backend_cpu_buffer_type(), size);
  5155. }
  5156. ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size);
  5157. buffer->buft = buft;
  5158. buffer->iface.get_name = ggml_backend_vk_host_buffer_name;
  5159. buffer->iface.free_buffer = ggml_backend_vk_host_buffer_free_buffer;
  5160. return buffer;
  5161. }
  5162. GGML_CALL static size_t ggml_backend_vk_host_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  5163. return vk_instance.contexts[0].device->properties.limits.minMemoryMapAlignment;
  5164. UNUSED(buft);
  5165. }
  5166. GGML_CALL ggml_backend_buffer_type_t ggml_backend_vk_host_buffer_type() {
  5167. static struct ggml_backend_buffer_type ggml_backend_vk_buffer_type_host = {
  5168. /* .iface = */ {
  5169. /* .get_name = */ ggml_backend_vk_host_buffer_type_name,
  5170. /* .alloc_buffer = */ ggml_backend_vk_host_buffer_type_alloc_buffer,
  5171. /* .get_alignment = */ ggml_backend_vk_host_buffer_type_get_alignment,
  5172. /* .get_max_size = */ NULL, // defaults to SIZE_MAX
  5173. /* .get_alloc_size = */ ggml_backend_cpu_buffer_type()->iface.get_alloc_size,
  5174. /* .supports_backend = */ ggml_backend_cpu_buffer_type()->iface.supports_backend,
  5175. /* .is_host = */ ggml_backend_cpu_buffer_type()->iface.is_host,
  5176. },
  5177. /* .context = */ nullptr,
  5178. };
  5179. if (!vk_instance.contexts[0].initialized) {
  5180. // Fall back to CPU
  5181. return ggml_backend_cpu_buffer_type();
  5182. }
  5183. return &ggml_backend_vk_buffer_type_host;
  5184. }
  5185. // backend
  5186. GGML_CALL static const char * ggml_backend_vk_name(ggml_backend_t backend) {
  5187. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  5188. return ctx->name.c_str();
  5189. }
  5190. GGML_CALL static void ggml_backend_vk_free(ggml_backend_t backend) {
  5191. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  5192. #ifdef GGML_VULKAN_DEBUG
  5193. std::cerr << "ggml_backend_vk_free(" << ctx->name << ")" << std::endl;
  5194. #endif
  5195. size_t idx = ctx->idx;
  5196. ggml_vk_cleanup(ctx);
  5197. ctx->device.reset();
  5198. ctx->initialized = false;
  5199. vk_instance.initialized[idx] = false;
  5200. vk_instance.backends[idx] = nullptr;
  5201. memset(&vk_instance.buffer_types[idx], 0, sizeof(ggml_backend_buffer_type));
  5202. delete backend;
  5203. }
  5204. GGML_CALL static ggml_backend_buffer_type_t ggml_backend_vk_get_default_buffer_type(ggml_backend_t backend) {
  5205. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  5206. GGML_ASSERT(ctx->initialized);
  5207. return ggml_backend_vk_buffer_type(ctx->idx);
  5208. }
  5209. GGML_CALL static void ggml_backend_vk_set_tensor_async(ggml_backend_t backend, ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
  5210. #ifdef GGML_VULKAN_DEBUG
  5211. std::cerr << "ggml_backend_vk_set_tensor_async(" << size << ")" << std::endl;
  5212. #endif
  5213. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  5214. GGML_ASSERT((tensor->buffer->buft == ggml_backend_vk_buffer_type(ctx->idx) || tensor->buffer->buft == ggml_backend_vk_host_buffer_type()) && "unsupported buffer type");
  5215. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  5216. if (ctx->transfer_ctx == nullptr) {
  5217. // Initialize new transfer context
  5218. ctx->transfer_ctx = ggml_vk_create_context(ctx, ctx->device->transfer_queue);
  5219. ggml_vk_ctx_begin(ctx, ctx->transfer_ctx);
  5220. }
  5221. vk_buffer buf = extra->buffer_gpu.lock();
  5222. ggml_vk_buffer_write_async(ctx, ctx->transfer_ctx, buf, extra->offset + tensor->view_offs + offset, data, size);
  5223. }
  5224. GGML_CALL static void ggml_backend_vk_get_tensor_async(ggml_backend_t backend, const ggml_tensor * tensor, void * data, size_t offset, size_t size) {
  5225. #ifdef GGML_VULKAN_DEBUG
  5226. std::cerr << "ggml_backend_vk_get_tensor_async(" << size << ")" << std::endl;
  5227. #endif
  5228. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  5229. GGML_ASSERT((tensor->buffer->buft == ggml_backend_vk_buffer_type(ctx->idx) || tensor->buffer->buft == ggml_backend_vk_host_buffer_type()) && "unsupported buffer type");
  5230. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  5231. if (ctx->transfer_ctx == nullptr) {
  5232. // Initialize new transfer context
  5233. ctx->transfer_ctx = ggml_vk_create_context(ctx, ctx->device->transfer_queue);
  5234. ggml_vk_ctx_begin(ctx, ctx->transfer_ctx);
  5235. }
  5236. vk_buffer buf = extra->buffer_gpu.lock();
  5237. ggml_vk_buffer_read_async(ctx, ctx->transfer_ctx, buf, extra->offset + tensor->view_offs + offset, data, size);
  5238. }
  5239. GGML_CALL static bool ggml_backend_vk_cpy_tensor_async(ggml_backend_t backend, const ggml_tensor * src, ggml_tensor * dst) {
  5240. #ifdef GGML_VULKAN_DEBUG
  5241. std::cerr << "ggml_backend_vk_cpy_tensor_async()" << std::endl;
  5242. #endif
  5243. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  5244. if ((dst->buffer->buft == ggml_backend_vk_buffer_type(ctx->idx) || dst->buffer->buft == ggml_backend_vk_host_buffer_type()) && ggml_backend_buffer_is_vk(src->buffer)) {
  5245. ggml_tensor_extra_gpu * src_extra = (ggml_tensor_extra_gpu *) src->extra;
  5246. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  5247. if (ctx->transfer_ctx == nullptr) {
  5248. // Initialize new transfer context
  5249. ctx->transfer_ctx = ggml_vk_create_context(ctx, ctx->device->transfer_queue);
  5250. ggml_vk_ctx_begin(ctx, ctx->transfer_ctx);
  5251. }
  5252. vk_buffer src_buf = src_extra->buffer_gpu.lock();
  5253. vk_buffer dst_buf = dst_extra->buffer_gpu.lock();
  5254. ggml_vk_buffer_copy_async(ctx->transfer_ctx, dst_buf, dst_extra->offset + dst->view_offs, src_buf, src_extra->offset + src->view_offs, ggml_nbytes(src));
  5255. return true;
  5256. }
  5257. return false;
  5258. }
  5259. GGML_CALL static void ggml_backend_vk_synchronize(ggml_backend_t backend) {
  5260. #ifdef GGML_VULKAN_DEBUG
  5261. std::cerr << "ggml_backend_vk_synchronize()" << std::endl;
  5262. #endif
  5263. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  5264. if(ctx->transfer_ctx == nullptr) {
  5265. return;
  5266. }
  5267. ggml_vk_ctx_end(ctx->transfer_ctx);
  5268. for (auto& cpy : ctx->transfer_ctx->in_memcpys) {
  5269. memcpy(cpy.dst, cpy.src, cpy.n);
  5270. }
  5271. ggml_vk_submit(ctx->transfer_ctx, ctx->fence);
  5272. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_backend_vk_synchronize waitForFences");
  5273. ctx->device->device.resetFences({ ctx->fence });
  5274. for (auto& cpy : ctx->transfer_ctx->out_memcpys) {
  5275. memcpy(cpy.dst, cpy.src, cpy.n);
  5276. }
  5277. ctx->transfer_ctx = nullptr;
  5278. }
  5279. static bool ggml_vk_is_empty(ggml_tensor * node) {
  5280. 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;
  5281. }
  5282. GGML_CALL static ggml_status ggml_backend_vk_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
  5283. #ifdef GGML_VULKAN_DEBUG
  5284. std::cerr << "ggml_backend_vk_graph_compute(" << cgraph->n_nodes << " nodes)" << std::endl;
  5285. #endif
  5286. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  5287. for (int i = 0; i < cgraph->n_nodes; i++) {
  5288. ggml_vk_preallocate_buffers_graph(ctx, cgraph->nodes[i]);
  5289. }
  5290. ggml_vk_preallocate_buffers(ctx);
  5291. int last_node = cgraph->n_nodes - 1;
  5292. // If the last op in the cgraph isn't backend GPU, the command buffer doesn't get closed properly
  5293. while (last_node > 0 && ggml_vk_is_empty(cgraph->nodes[last_node])) {
  5294. last_node -= 1;
  5295. }
  5296. for (int i = 0; i < cgraph->n_nodes; i++) {
  5297. ggml_vk_build_graph(ctx,cgraph->nodes[i], i == last_node);
  5298. }
  5299. ggml_compute_params params = {};
  5300. params.type = GGML_TASK_TYPE_COMPUTE;
  5301. params.ith = 0;
  5302. for (int i = 0; i < cgraph->n_nodes; i++) {
  5303. ggml_tensor * node = cgraph->nodes[i];
  5304. if (ggml_vk_is_empty(node)) {
  5305. continue;
  5306. }
  5307. bool ok = ggml_vk_compute_forward(ctx, &params, node);
  5308. if (!ok) {
  5309. fprintf(stderr, "%s: error: op not supported %s (%s)\n", __func__, node->name, ggml_op_name(node->op));
  5310. }
  5311. #ifdef GGML_VULKAN_CHECK_RESULTS
  5312. else {
  5313. ggml_vk_check_results_1(ctx, &params, node);
  5314. }
  5315. #endif
  5316. GGML_ASSERT(ok);
  5317. }
  5318. ggml_vk_graph_cleanup(ctx);
  5319. return GGML_STATUS_SUCCESS;
  5320. UNUSED(backend);
  5321. }
  5322. GGML_CALL static bool ggml_backend_vk_supports_op(ggml_backend_t backend, const ggml_tensor * op) {
  5323. // ggml_backend_vk_context * ctx = (ggml_backend_vk_context *) backend->context;
  5324. switch (op->op) {
  5325. case GGML_OP_UNARY:
  5326. switch (ggml_get_unary_op(op)) {
  5327. case GGML_UNARY_OP_GELU:
  5328. case GGML_UNARY_OP_SILU:
  5329. case GGML_UNARY_OP_RELU:
  5330. return true;
  5331. default:
  5332. return false;
  5333. }
  5334. break;
  5335. case GGML_OP_MUL_MAT:
  5336. case GGML_OP_MUL_MAT_ID:
  5337. {
  5338. switch (op->src[0]->type) {
  5339. case GGML_TYPE_F32:
  5340. case GGML_TYPE_F16:
  5341. case GGML_TYPE_Q4_0:
  5342. case GGML_TYPE_Q4_1:
  5343. case GGML_TYPE_Q5_0:
  5344. case GGML_TYPE_Q5_1:
  5345. case GGML_TYPE_Q8_0:
  5346. case GGML_TYPE_Q2_K:
  5347. case GGML_TYPE_Q3_K:
  5348. case GGML_TYPE_Q4_K:
  5349. case GGML_TYPE_Q5_K:
  5350. case GGML_TYPE_Q6_K:
  5351. break;
  5352. default:
  5353. return false;
  5354. }
  5355. struct ggml_tensor * a;
  5356. struct ggml_tensor * b;
  5357. if (op->op == GGML_OP_MUL_MAT) {
  5358. a = op->src[0];
  5359. b = op->src[1];
  5360. } else {
  5361. a = op->src[2];
  5362. b = op->src[1];
  5363. }
  5364. if (a->ne[3] != b->ne[3]) {
  5365. return false;
  5366. }
  5367. return true;
  5368. } break;
  5369. case GGML_OP_GET_ROWS:
  5370. {
  5371. switch (op->src[0]->type) {
  5372. case GGML_TYPE_F32:
  5373. case GGML_TYPE_F16:
  5374. case GGML_TYPE_Q4_0:
  5375. case GGML_TYPE_Q4_1:
  5376. case GGML_TYPE_Q5_0:
  5377. case GGML_TYPE_Q5_1:
  5378. case GGML_TYPE_Q8_0:
  5379. return true;
  5380. default:
  5381. return false;
  5382. }
  5383. } break;
  5384. case GGML_OP_CPY:
  5385. case GGML_OP_DUP:
  5386. {
  5387. ggml_type src0_type = op->src[0]->type;
  5388. ggml_type src1_type = op->src[1] != nullptr ? op->src[1]->type : src0_type;
  5389. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  5390. return true;
  5391. }
  5392. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F16) {
  5393. return true;
  5394. }
  5395. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  5396. return true;
  5397. }
  5398. return false;
  5399. } break;
  5400. // case GGML_OP_REPEAT:
  5401. // {
  5402. // ggml_type src0_type = op->src[0]->type;
  5403. // return src0_type != GGML_TYPE_I32 && src0_type != GGML_TYPE_I16;
  5404. // } break;
  5405. case GGML_OP_ROPE:
  5406. return ggml_is_contiguous(op->src[0]);
  5407. case GGML_OP_NONE:
  5408. case GGML_OP_RESHAPE:
  5409. case GGML_OP_VIEW:
  5410. case GGML_OP_PERMUTE:
  5411. case GGML_OP_TRANSPOSE:
  5412. case GGML_OP_NORM:
  5413. case GGML_OP_ADD:
  5414. case GGML_OP_MUL:
  5415. case GGML_OP_DIV:
  5416. case GGML_OP_RMS_NORM:
  5417. case GGML_OP_SCALE:
  5418. case GGML_OP_SQR:
  5419. case GGML_OP_CLAMP:
  5420. case GGML_OP_CONT:
  5421. case GGML_OP_DIAG_MASK_INF:
  5422. case GGML_OP_SOFT_MAX:
  5423. case GGML_OP_ARGSORT:
  5424. case GGML_OP_SUM_ROWS:
  5425. return true;
  5426. default:
  5427. return false;
  5428. }
  5429. UNUSED(backend);
  5430. }
  5431. GGML_CALL static bool ggml_backend_vk_offload_op(ggml_backend_t backend, const ggml_tensor * op) {
  5432. const int min_batch_size = 32;
  5433. return (op->ne[1] >= min_batch_size && op->op != GGML_OP_GET_ROWS) ||
  5434. (op->ne[2] >= min_batch_size && op->op == GGML_OP_MUL_MAT_ID);
  5435. UNUSED(backend);
  5436. }
  5437. // TODO: enable async and synchronize
  5438. static ggml_backend_i ggml_backend_vk_interface = {
  5439. /* .get_name = */ ggml_backend_vk_name,
  5440. /* .free = */ ggml_backend_vk_free,
  5441. /* .get_default_buffer_type = */ ggml_backend_vk_get_default_buffer_type,
  5442. /* .set_tensor_async = */ NULL, // ggml_backend_vk_set_tensor_async,
  5443. /* .get_tensor_async = */ NULL, // ggml_backend_vk_get_tensor_async,
  5444. /* .cpy_tensor_async = */ NULL, // ggml_backend_vk_cpy_tensor_async,
  5445. /* .synchronize = */ NULL, // ggml_backend_vk_synchronize,
  5446. /* .graph_plan_create = */ NULL,
  5447. /* .graph_plan_free = */ NULL,
  5448. /* .graph_plan_compute = */ NULL,
  5449. /* .graph_compute = */ ggml_backend_vk_graph_compute,
  5450. /* .supports_op = */ ggml_backend_vk_supports_op,
  5451. /* .offload_op = */ ggml_backend_vk_offload_op,
  5452. /* .event_new = */ NULL,
  5453. /* .event_free = */ NULL,
  5454. /* .event_record = */ NULL,
  5455. /* .event_wait = */ NULL,
  5456. /* .event_synchronize = */ NULL,
  5457. };
  5458. static ggml_guid_t ggml_backend_vk_guid() {
  5459. static ggml_guid guid = { 0xb8, 0xf7, 0x4f, 0x86, 0x40, 0x3c, 0xe1, 0x02, 0x91, 0xc8, 0xdd, 0xe9, 0x02, 0x3f, 0xc0, 0x2b };
  5460. return &guid;
  5461. }
  5462. GGML_CALL ggml_backend_t ggml_backend_vk_init(size_t dev_num) {
  5463. if (vk_instance.initialized[dev_num]) {
  5464. return vk_instance.backends[dev_num];
  5465. }
  5466. #ifdef GGML_VULKAN_DEBUG
  5467. std::cerr << "ggml_backend_vk_init(" << dev_num << ")" << std::endl;
  5468. #endif
  5469. ggml_backend_vk_context * ctx = &vk_instance.contexts[dev_num];
  5470. ggml_vk_init(ctx, dev_num);
  5471. ctx->name = GGML_VK_NAME + std::to_string(dev_num);
  5472. vk_instance.buffer_types[dev_num] = {
  5473. /* .iface = */ ggml_backend_vk_buffer_type_interface,
  5474. /* .context = */ new ggml_backend_vk_buffer_type_context{ ctx->name, ctx },
  5475. };
  5476. vk_instance.initialized[dev_num] = true;
  5477. ggml_backend_t vk_backend = new ggml_backend {
  5478. /* .guid = */ ggml_backend_vk_guid(),
  5479. /* .interface = */ ggml_backend_vk_interface,
  5480. /* .context = */ &vk_instance.contexts[ctx->idx],
  5481. };
  5482. vk_instance.backends[dev_num] = vk_backend;
  5483. return vk_backend;
  5484. }
  5485. GGML_CALL bool ggml_backend_is_vk(ggml_backend_t backend) {
  5486. return backend != NULL && ggml_guid_matches(backend->guid, ggml_backend_vk_guid());
  5487. }
  5488. GGML_CALL int ggml_backend_vk_get_device_count() {
  5489. return ggml_vk_get_device_count();
  5490. }
  5491. GGML_CALL void ggml_backend_vk_get_device_description(int device, char * description, size_t description_size) {
  5492. ggml_vk_get_device_description(device, description, description_size);
  5493. }
  5494. GGML_CALL void ggml_backend_vk_get_device_memory(int device, size_t * free, size_t * total) {
  5495. GGML_ASSERT(device < (int) vk_instance.device_indices.size());
  5496. vk::PhysicalDevice vkdev = vk_instance.instance.enumeratePhysicalDevices()[vk_instance.device_indices[device]];
  5497. vk::PhysicalDeviceMemoryProperties memprops = vkdev.getMemoryProperties();
  5498. for (const vk::MemoryHeap& heap : memprops.memoryHeaps) {
  5499. if (heap.flags & vk::MemoryHeapFlagBits::eDeviceLocal) {
  5500. *total = heap.size;
  5501. *free = heap.size;
  5502. break;
  5503. }
  5504. }
  5505. }
  5506. // backend registry
  5507. GGML_CALL static ggml_backend_t ggml_backend_reg_vk_init(const char * params, void * user_data) {
  5508. ggml_backend_t vk_backend = ggml_backend_vk_init((int) (intptr_t) user_data);
  5509. return vk_backend;
  5510. UNUSED(params);
  5511. }
  5512. extern "C" GGML_CALL int ggml_backend_vk_reg_devices();
  5513. GGML_CALL int ggml_backend_vk_reg_devices() {
  5514. ggml_vk_instance_init();
  5515. for (size_t i = 0; i < vk_instance.device_indices.size(); i++) {
  5516. char name[128];
  5517. snprintf(name, sizeof(name), "%s%ld", GGML_VK_NAME, i);
  5518. ggml_backend_register(name, ggml_backend_reg_vk_init, ggml_backend_vk_buffer_type(i), (void *) (intptr_t) i); // NOLINT
  5519. }
  5520. return vk_instance.device_indices.size();
  5521. }
  5522. // Extension availability
  5523. static bool ggml_vk_instance_validation_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions) {
  5524. #ifdef GGML_VULKAN_VALIDATE
  5525. bool portability_enumeration_ext = false;
  5526. // Check for portability enumeration extension for MoltenVK support
  5527. for (const auto& properties : instance_extensions) {
  5528. if (strcmp("VK_KHR_portability_enumeration", properties.extensionName) == 0) {
  5529. return true;
  5530. }
  5531. }
  5532. if (!portability_enumeration_ext) {
  5533. std::cerr << "ggml_vulkan: WARNING: Instance extension VK_KHR_portability_enumeration not found." << std::endl;
  5534. }
  5535. #endif
  5536. return false;
  5537. UNUSED(instance_extensions);
  5538. }
  5539. static bool ggml_vk_instance_portability_enumeration_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions) {
  5540. #ifdef __APPLE__
  5541. bool portability_enumeration_ext = false;
  5542. // Check for portability enumeration extension for MoltenVK support
  5543. for (const auto& properties : instance_extensions) {
  5544. if (strcmp("VK_KHR_portability_enumeration", properties.extensionName) == 0) {
  5545. return true;
  5546. }
  5547. }
  5548. if (!portability_enumeration_ext) {
  5549. std::cerr << "ggml_vulkan: WARNING: Instance extension VK_KHR_portability_enumeration not found." << std::endl;
  5550. }
  5551. #endif
  5552. return false;
  5553. UNUSED(instance_extensions);
  5554. }
  5555. // checks
  5556. #ifdef GGML_VULKAN_CHECK_RESULTS
  5557. static void ggml_vk_print_graph_origin(const ggml_tensor * tensor, std::vector<const ggml_tensor *>& done, int level = 0) {
  5558. if (std::find(done.begin(), done.end(), tensor) != done.end() || level > 10) {
  5559. return;
  5560. }
  5561. for (int j = 0; j < level; j++) {
  5562. std::cerr << " ";
  5563. }
  5564. std::cerr << ggml_op_name(tensor->op) << " gpu=" << (tensor->extra != nullptr) << std::endl;
  5565. done.push_back(tensor);
  5566. for (int i = 0; i < GGML_MAX_SRC; i++) {
  5567. if (tensor->src[i] != nullptr) {
  5568. ggml_vk_print_graph_origin(tensor->src[i], done, level + 1);
  5569. }
  5570. }
  5571. }
  5572. static void ggml_vk_print_tensor_area(const ggml_tensor * tensor, const void * data, int i0, int i1, int i2, int i3) {
  5573. if (tensor->type != GGML_TYPE_F32 && tensor->type != GGML_TYPE_F16 && tensor->type != GGML_TYPE_I32) {
  5574. return;
  5575. }
  5576. i0 = std::max(i0, 5);
  5577. i1 = std::max(i1, 5);
  5578. i2 = std::max(i2, 0);
  5579. i3 = std::max(i3, 0);
  5580. fprintf(stderr, " ");
  5581. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  5582. fprintf(stderr, "%7d ", idx1);
  5583. }
  5584. fprintf(stderr, "\n");
  5585. for (int idx0 = i0 - 5; idx0 < i0 + 5; idx0++) {
  5586. fprintf(stderr, "%7d: ", idx0);
  5587. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  5588. 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]) {
  5589. float val;
  5590. if (tensor->type == GGML_TYPE_F32) {
  5591. val = *(const float *) ((const char *) data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]);
  5592. } else if (tensor->type == GGML_TYPE_F16) {
  5593. 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]));
  5594. } else if (tensor->type == GGML_TYPE_I32) {
  5595. val = *(const int32_t *) ((const char *) data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]);
  5596. } else {
  5597. GGML_ASSERT(false);
  5598. }
  5599. fprintf(stderr, "% 7.2f ", val);
  5600. } else {
  5601. fprintf(stderr, " ");
  5602. }
  5603. }
  5604. fprintf(stderr, "\n");
  5605. }
  5606. }
  5607. static void ggml_vk_print_tensor(ggml_backend_vk_context * ctx, const ggml_tensor * tensor, const char * name) {
  5608. void * tensor_data = tensor->data;
  5609. if (ggml_backend_buffer_is_vk(tensor->buffer)) {
  5610. const size_t tensor_size = ggml_nbytes(tensor);
  5611. tensor_data = malloc(tensor_size);
  5612. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  5613. vk_buffer buffer_gpu = extra->buffer_gpu.lock();
  5614. ggml_vk_buffer_read(ctx, buffer_gpu, extra->offset + tensor->view_offs, tensor_data, tensor_size);
  5615. }
  5616. std::cerr << "TENSOR CHECK " << name << " (" << tensor->name << "): " << ggml_op_name(tensor->op) << std::endl;
  5617. 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;
  5618. if (tensor->src[0] != nullptr) {
  5619. 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;
  5620. }
  5621. if (tensor->src[1] != nullptr) {
  5622. 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;
  5623. }
  5624. std::cerr << std::endl << "Result:" << std::endl;
  5625. ggml_vk_print_tensor_area(tensor, tensor_data, 5, 5, 0, 0);
  5626. std::cerr << std::endl;
  5627. std::cerr << std::endl << "Result:" << std::endl;
  5628. ggml_vk_print_tensor_area(tensor, tensor_data, 5, 5, 1, 0);
  5629. std::cerr << std::endl;
  5630. std::vector<const ggml_tensor *> done;
  5631. ggml_vk_print_graph_origin(tensor, done);
  5632. if (ggml_backend_buffer_is_vk(tensor->buffer)) {
  5633. free(tensor_data);
  5634. }
  5635. }
  5636. void * comp_result;
  5637. size_t comp_size;
  5638. size_t comp_nb[GGML_MAX_DIMS];
  5639. size_t check_counter = 0;
  5640. static void ggml_vk_check_results_0(ggml_backend_vk_context * ctx, ggml_compute_params * params, ggml_tensor * tensor) {
  5641. if (params->ith != 0) {
  5642. return;
  5643. }
  5644. if (params->type == GGML_TASK_TYPE_INIT || params->type == GGML_TASK_TYPE_FINALIZE || tensor->op == GGML_OP_TRANSPOSE) {
  5645. return;
  5646. }
  5647. check_counter++;
  5648. if (!(vk_output_tensor > 0 && vk_output_tensor == check_counter) && check_counter <= vk_skip_checks) {
  5649. return;
  5650. }
  5651. #ifdef GGML_VULKAN_DEBUG
  5652. std::cerr << "ggml_vk_check_results_0(" << tensor->name << ")" << std::endl;
  5653. #endif
  5654. ggml_tensor * src0 = tensor->src[0];
  5655. ggml_tensor * src1 = tensor->src[1];
  5656. ggml_tensor * src2 = tensor->src[2];
  5657. struct ggml_init_params iparams = {
  5658. /*.mem_size =*/ 1024*1024*1024,
  5659. /*.mem_buffer =*/ NULL,
  5660. /*.no_alloc =*/ false,
  5661. };
  5662. struct ggml_context * ggml_ctx = ggml_init(iparams);
  5663. struct ggml_tensor * src0_clone = nullptr;
  5664. struct ggml_tensor * src1_clone = nullptr;
  5665. struct ggml_tensor * src2_clone = nullptr;
  5666. struct ggml_tensor * tensor_clone = nullptr;
  5667. size_t src0_size;
  5668. size_t src1_size;
  5669. size_t src2_size;
  5670. void * src0_buffer = nullptr;
  5671. void * src1_buffer = nullptr;
  5672. void * src2_buffer = nullptr;
  5673. if (src0 != nullptr) {
  5674. src0_clone = ggml_dup_tensor(ggml_ctx, src0);
  5675. src0_size = ggml_nbytes(src0);
  5676. src0_buffer = malloc(src0_size);
  5677. src0_clone->data = src0_buffer;
  5678. if (ggml_backend_buffer_is_host(src0->buffer)) {
  5679. memcpy(src0_clone->data, src0->data, src0_size);
  5680. memcpy(src0_clone->nb, src0->nb, sizeof(size_t) * GGML_MAX_DIMS);
  5681. } else if (ggml_backend_buffer_is_vk(src0->buffer)) {
  5682. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) src0->extra;
  5683. vk_buffer buffer_gpu = extra->buffer_gpu.lock();
  5684. uint64_t offset = extra->offset + src0->view_offs;
  5685. if (!ggml_is_contiguous(src0) && ggml_vk_dim01_contiguous(src0)) {
  5686. for (int i3 = 0; i3 < src0->ne[3]; i3++) {
  5687. for (int i2 = 0; i2 < src0->ne[2]; i2++) {
  5688. const int idx = i3*src0->ne[2] + i2;
  5689. ggml_vk_buffer_read(ctx, buffer_gpu, offset + idx * src0->nb[2], ((char *)src0_clone->data + idx * src0_clone->nb[2]), src0->ne[1] * src0->nb[1]);
  5690. }
  5691. }
  5692. src0_clone->nb[0] = src0->nb[0];
  5693. src0_clone->nb[1] = src0->nb[1];
  5694. for (int i = 2; i < GGML_MAX_DIMS; i++) {
  5695. src0_clone->nb[i] = src0_clone->nb[i - 1]*src0_clone->ne[i - 1];
  5696. }
  5697. } else {
  5698. if (offset + src0_size >= buffer_gpu->size) {
  5699. src0_size = buffer_gpu->size - offset;
  5700. }
  5701. ggml_vk_buffer_read(ctx, buffer_gpu, offset, src0_clone->data, src0_size);
  5702. memcpy(src0_clone->nb, src0->nb, sizeof(size_t) * GGML_MAX_DIMS);
  5703. }
  5704. } else {
  5705. GGML_ASSERT(false);
  5706. }
  5707. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  5708. ggml_vk_print_tensor(ctx, src0, "src0");
  5709. }
  5710. }
  5711. if (src1 != nullptr) {
  5712. src1_clone = ggml_dup_tensor(ggml_ctx, src1);
  5713. src1_size = ggml_nbytes(src1);
  5714. src1_buffer = malloc(src1_size);
  5715. src1_clone->data = src1_buffer;
  5716. if (ggml_backend_buffer_is_host(src1->buffer)) {
  5717. memcpy(src1_clone->data, src1->data, src1_size);
  5718. memcpy(src1_clone->nb, src1->nb, sizeof(size_t) * GGML_MAX_DIMS);
  5719. } else if (ggml_backend_buffer_is_vk(src1->buffer)) {
  5720. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) src1->extra;
  5721. vk_buffer buffer_gpu = extra->buffer_gpu.lock();
  5722. uint64_t offset = extra->offset + src1->view_offs;
  5723. if (!ggml_is_contiguous(src1) && ggml_vk_dim01_contiguous(src1)) {
  5724. for (int i3 = 0; i3 < src1->ne[3]; i3++) {
  5725. for (int i2 = 0; i2 < src1->ne[2]; i2++) {
  5726. const int idx = i3*src1->ne[2] + i2;
  5727. ggml_vk_buffer_read(ctx, buffer_gpu, offset + idx * src1->nb[2], ((char *)src1_clone->data + idx * src1_clone->nb[2]), src1->ne[1] * src1->nb[1]);
  5728. }
  5729. }
  5730. src1_clone->nb[0] = src1->nb[0];
  5731. src1_clone->nb[1] = src1->nb[1];
  5732. for (int i = 2; i < GGML_MAX_DIMS; i++) {
  5733. src1_clone->nb[i] = src1_clone->nb[i - 1]*src1_clone->ne[i - 1];
  5734. }
  5735. } else {
  5736. if (offset + src1_size >= buffer_gpu->size) {
  5737. src1_size = buffer_gpu->size - offset;
  5738. }
  5739. ggml_vk_buffer_read(ctx, buffer_gpu, offset, src1_clone->data, src1_size);
  5740. memcpy(src1_clone->nb, src1->nb, sizeof(size_t) * GGML_MAX_DIMS);
  5741. }
  5742. } else {
  5743. GGML_ASSERT(false);
  5744. }
  5745. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  5746. ggml_vk_print_tensor(ctx, src1, "src1");
  5747. std::cerr << "TENSOR CHECK: " << ggml_op_name(src1_clone->op) << " (check " << check_counter << ")" << std::endl;
  5748. std::cerr << "src1_clone=" << tensor << " src1_clone->type: " << ggml_type_name(src1_clone->type) << " ne0=" << src1_clone->ne[0] << " nb0=" << src1_clone->nb[0] << " ne1=" << src1_clone->ne[1] << " nb1=" << src1_clone->nb[1] << " ne2=" << src1_clone->ne[2] << " nb2=" << src1_clone->nb[2] << " ne3=" << src1_clone->ne[3] << " nb3=" << src1_clone->nb[3] << std::endl;
  5749. if (src1->src[0] != nullptr) {
  5750. std::cerr << "src1->src[0]=" << src1->src[0] << " op=" << ggml_op_name(src1->src[0]->op) << " type=" << ggml_type_name(src1->src[0]->type) << " ne0=" << src1->src[0]->ne[0] << " nb0=" << src1->src[0]->nb[0] << " ne1=" << src1->src[0]->ne[1] << " nb1=" << src1->src[0]->nb[1] << " ne2=" << src1->src[0]->ne[2] << " nb2=" << src1->src[0]->nb[2] << " ne3=" << src1->src[0]->ne[3] << " nb3=" << src1->src[0]->nb[3] << std::endl;
  5751. }
  5752. if (src1->src[1] != nullptr) {
  5753. std::cerr << "src1->src[1]=" << src1->src[1] << " op=" << ggml_op_name(src1->src[1]->op) << " type=" << ggml_type_name(src1->src[1]->type) << " ne0=" << src1->src[1]->ne[0] << " nb0=" << src1->src[1]->nb[0] << " ne1=" << src1->src[1]->ne[1] << " nb1=" << src1->src[1]->nb[1] << " ne2=" << src1->src[1]->ne[2] << " nb2=" << src1->src[1]->nb[2] << " ne3=" << src1->src[1]->ne[3] << " nb3=" << src1->src[1]->nb[3] << std::endl;
  5754. }
  5755. std::cerr << std::endl << "Result:" << std::endl;
  5756. ggml_vk_print_tensor_area(src1_clone, src1_clone->data, 5, 5, 0, 0);
  5757. std::cerr << std::endl;
  5758. std::cerr << std::endl << "Result:" << std::endl;
  5759. ggml_vk_print_tensor_area(src1_clone, src1_clone->data, 5, 5, 1, 0);
  5760. std::cerr << std::endl;
  5761. std::vector<const ggml_tensor *> done;
  5762. ggml_vk_print_graph_origin(src1_clone, done);
  5763. }
  5764. }
  5765. if (src2 != nullptr) {
  5766. src2_clone = ggml_dup_tensor(ggml_ctx, src2);
  5767. src2_size = ggml_nbytes(src2);
  5768. src2_buffer = malloc(src2_size);
  5769. src2_clone->data = src2_buffer;
  5770. if (ggml_backend_buffer_is_host(src2->buffer)) {
  5771. memcpy(src2_clone->data, src2->data, src2_size);
  5772. memcpy(src2_clone->nb, src2->nb, sizeof(size_t) * GGML_MAX_DIMS);
  5773. } else if (ggml_backend_buffer_is_vk(src2->buffer)) {
  5774. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) src2->extra;
  5775. vk_buffer buffer_gpu = extra->buffer_gpu.lock();
  5776. uint64_t offset = extra->offset + src2->view_offs;
  5777. if (!ggml_is_contiguous(src2) && ggml_vk_dim01_contiguous(src2)) {
  5778. for (int i3 = 0; i3 < src2->ne[3]; i3++) {
  5779. for (int i2 = 0; i2 < src2->ne[2]; i2++) {
  5780. const int idx = i3*src2->ne[2] + i2;
  5781. ggml_vk_buffer_read(ctx, buffer_gpu, offset + idx * src2->nb[2], ((char *)src2_clone->data + idx * src2_clone->nb[2]), src2->ne[1] * src2->nb[1]);
  5782. }
  5783. }
  5784. src2_clone->nb[0] = src2->nb[0];
  5785. src2_clone->nb[1] = src2->nb[1];
  5786. for (int i = 2; i < GGML_MAX_DIMS; i++) {
  5787. src2_clone->nb[i] = src2_clone->nb[i - 1]*src2_clone->ne[i - 1];
  5788. }
  5789. } else {
  5790. if (offset + src2_size >= buffer_gpu->size) {
  5791. src2_size = buffer_gpu->size - offset;
  5792. }
  5793. ggml_vk_buffer_read(ctx, buffer_gpu, offset, src2_clone->data, src2_size);
  5794. memcpy(src2_clone->nb, src2->nb, sizeof(size_t) * GGML_MAX_DIMS);
  5795. }
  5796. } else {
  5797. GGML_ASSERT(false);
  5798. }
  5799. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  5800. ggml_vk_print_tensor(ctx, src2, "src2");
  5801. std::cerr << "TENSOR CHECK: " << ggml_op_name(src2_clone->op) << " (check " << check_counter << ")" << std::endl;
  5802. std::cerr << "src2_clone=" << tensor << " src2_clone->type: " << ggml_type_name(src2_clone->type) << " ne0=" << src2_clone->ne[0] << " nb0=" << src2_clone->nb[0] << " ne1=" << src2_clone->ne[1] << " nb1=" << src2_clone->nb[1] << " ne2=" << src2_clone->ne[2] << " nb2=" << src2_clone->nb[2] << " ne3=" << src2_clone->ne[3] << " nb3=" << src2_clone->nb[3] << std::endl;
  5803. if (src2->src[0] != nullptr) {
  5804. std::cerr << "src2->src[0]=" << src2->src[0] << " op=" << ggml_op_name(src2->src[0]->op) << " type=" << ggml_type_name(src2->src[0]->type) << " ne0=" << src2->src[0]->ne[0] << " nb0=" << src2->src[0]->nb[0] << " ne1=" << src2->src[0]->ne[1] << " nb1=" << src2->src[0]->nb[1] << " ne2=" << src2->src[0]->ne[2] << " nb2=" << src2->src[0]->nb[2] << " ne3=" << src2->src[0]->ne[3] << " nb3=" << src2->src[0]->nb[3] << std::endl;
  5805. }
  5806. if (src2->src[1] != nullptr) {
  5807. std::cerr << "src2->src[1]=" << src2->src[1] << " op=" << ggml_op_name(src2->src[1]->op) << " type=" << ggml_type_name(src2->src[1]->type) << " ne0=" << src2->src[1]->ne[0] << " nb0=" << src2->src[1]->nb[0] << " ne1=" << src2->src[1]->ne[1] << " nb1=" << src2->src[1]->nb[1] << " ne2=" << src2->src[1]->ne[2] << " nb2=" << src2->src[1]->nb[2] << " ne3=" << src2->src[1]->ne[3] << " nb3=" << src2->src[1]->nb[3] << std::endl;
  5808. }
  5809. std::cerr << std::endl << "Result:" << std::endl;
  5810. ggml_vk_print_tensor_area(src2_clone, src2_clone->data, 5, 5, 0, 0);
  5811. std::cerr << std::endl;
  5812. std::cerr << std::endl << "Result:" << std::endl;
  5813. ggml_vk_print_tensor_area(src2_clone, src2_clone->data, 5, 5, 1, 0);
  5814. std::cerr << std::endl;
  5815. std::vector<const ggml_tensor *> done;
  5816. ggml_vk_print_graph_origin(src2_clone, done);
  5817. }
  5818. }
  5819. if (tensor->op == GGML_OP_MUL_MAT) {
  5820. tensor_clone = ggml_mul_mat(ggml_ctx, src0_clone, src1_clone);
  5821. } else if (tensor->op == GGML_OP_MUL_MAT_ID) {
  5822. tensor_clone = ggml_mul_mat_id(ggml_ctx, src0_clone, src1_clone, src2_clone);
  5823. } else if (tensor->op == GGML_OP_MUL) {
  5824. tensor_clone = ggml_mul(ggml_ctx, src0_clone, src1_clone);
  5825. } else if (tensor->op == GGML_OP_DIV) {
  5826. tensor_clone = ggml_div(ggml_ctx, src0_clone, src1_clone);
  5827. } else if (tensor->op == GGML_OP_SCALE) {
  5828. tensor_clone = ggml_scale(ggml_ctx, src0_clone, ((float *)tensor->op_params)[0]);
  5829. } else if (tensor->op == GGML_OP_SQR) {
  5830. tensor_clone = ggml_sqr(ggml_ctx, src0_clone);
  5831. } else if (tensor->op == GGML_OP_CLAMP) {
  5832. tensor_clone = ggml_clamp(ggml_ctx, src0_clone, ((float *)tensor->op_params)[0], ((float *)tensor->op_params)[1]);
  5833. } else if (tensor->op == GGML_OP_ADD) {
  5834. tensor_clone = ggml_add(ggml_ctx, src0_clone, src1_clone);
  5835. } else if (tensor->op == GGML_OP_NORM) {
  5836. tensor_clone = ggml_norm(ggml_ctx, src0_clone, *(float *)tensor->op_params);
  5837. } else if (tensor->op == GGML_OP_RMS_NORM) {
  5838. tensor_clone = ggml_rms_norm(ggml_ctx, src0_clone, *(float *)tensor->op_params);
  5839. } else if (tensor->op == GGML_OP_SOFT_MAX) {
  5840. if (src1 != nullptr) {
  5841. tensor_clone = ggml_soft_max_ext(ggml_ctx, src0_clone, src1_clone, ((float *)tensor->op_params)[0], ((float *)tensor->op_params)[1]);
  5842. } else {
  5843. tensor_clone = ggml_soft_max(ggml_ctx, src0_clone);
  5844. }
  5845. } else if (tensor->op == GGML_OP_DIAG_MASK_INF) {
  5846. tensor_clone = ggml_diag_mask_inf(ggml_ctx, src0_clone, *(int *)tensor->op_params);
  5847. } else if (tensor->op == GGML_OP_ROPE) {
  5848. const int n_dims = ((int32_t *) tensor->op_params)[1];
  5849. const int mode = ((int32_t *) tensor->op_params)[2];
  5850. //const int n_ctx_ggml = ((int32_t *) tensor->op_params)[3];
  5851. const int n_ctx_orig_ggml = ((int32_t *) tensor->op_params)[4];
  5852. float freq_base = ((float *) tensor->op_params)[5];
  5853. float freq_scale = ((float *) tensor->op_params)[6];
  5854. float ext_factor = ((float *) tensor->op_params)[7];
  5855. float attn_factor = ((float *) tensor->op_params)[8];
  5856. float beta_fast = ((float *) tensor->op_params)[9];
  5857. float beta_slow = ((float *) tensor->op_params)[10];
  5858. tensor_clone = ggml_rope_ext(ggml_ctx, src0_clone, src1_clone, src2_clone, n_dims, mode, n_ctx_orig_ggml, freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow);
  5859. } else if (tensor->op == GGML_OP_UNARY) {
  5860. switch (ggml_get_unary_op(tensor)) {
  5861. case GGML_UNARY_OP_SILU:
  5862. tensor_clone = ggml_silu(ggml_ctx, src0_clone);
  5863. break;
  5864. case GGML_UNARY_OP_GELU:
  5865. tensor_clone = ggml_gelu(ggml_ctx, src0_clone);
  5866. break;
  5867. case GGML_UNARY_OP_RELU:
  5868. tensor_clone = ggml_relu(ggml_ctx, src0_clone);
  5869. break;
  5870. default:
  5871. std::cerr << "Missing vk_check_results OP: " << ggml_op_name(tensor->op) << std::endl;
  5872. GGML_ASSERT(false);
  5873. }
  5874. } else if (tensor->op == GGML_OP_CPY || tensor->op == GGML_OP_DUP) {
  5875. if (src1 == nullptr) {
  5876. tensor_clone = ggml_dup(ggml_ctx, src0_clone);
  5877. tensor_clone->type = tensor->type;
  5878. } else {
  5879. tensor_clone = ggml_cpy(ggml_ctx, src0_clone, src1_clone);
  5880. }
  5881. } else if (tensor->op == GGML_OP_CONT) {
  5882. tensor_clone = ggml_cont_4d(ggml_ctx, src0_clone, tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3]);
  5883. } else if (tensor->op == GGML_OP_RESHAPE) {
  5884. tensor_clone = ggml_reshape_4d(ggml_ctx, src0_clone, tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3]);
  5885. } else if (tensor->op == GGML_OP_VIEW) {
  5886. tensor_clone = ggml_view_4d(ggml_ctx, src0_clone, 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]);
  5887. } else if (tensor->op == GGML_OP_PERMUTE) {
  5888. int32_t * params = (int32_t *)tensor->op_params;
  5889. tensor_clone = ggml_permute(ggml_ctx, src0_clone, params[0], params[1], params[2], params[3]);
  5890. } else if (tensor->op == GGML_OP_TRANSPOSE) {
  5891. tensor_clone = ggml_transpose(ggml_ctx, src0_clone);
  5892. } else if (tensor->op == GGML_OP_GET_ROWS) {
  5893. tensor_clone = ggml_get_rows(ggml_ctx, src0_clone, src1_clone);
  5894. } else if (tensor->op == GGML_OP_ARGSORT) {
  5895. tensor_clone = ggml_argsort(ggml_ctx, src0_clone, (ggml_sort_order) *(int *)tensor->op_params);
  5896. } else if (tensor->op == GGML_OP_SUM_ROWS) {
  5897. tensor_clone = ggml_sum_rows(ggml_ctx, src0_clone);
  5898. } else {
  5899. std::cerr << "Missing vk_check_results OP: " << ggml_op_name(tensor->op) << std::endl;
  5900. GGML_ASSERT(false);
  5901. }
  5902. ggml_cgraph * cgraph = ggml_new_graph(ggml_ctx);
  5903. ggml_build_forward_expand(cgraph, tensor_clone);
  5904. ggml_graph_compute_with_ctx(ggml_ctx, cgraph, 8);
  5905. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  5906. ggml_vk_print_tensor(ctx, tensor_clone, "tensor_clone");
  5907. }
  5908. comp_size = ggml_nbytes(tensor_clone);
  5909. comp_result = malloc(comp_size);
  5910. memcpy(comp_result, tensor_clone->data, comp_size);
  5911. memcpy(comp_nb, tensor_clone->nb, sizeof(size_t) * GGML_MAX_DIMS);
  5912. if (src0 != nullptr) {
  5913. free(src0_buffer);
  5914. }
  5915. if (src1 != nullptr) {
  5916. free(src1_buffer);
  5917. }
  5918. ggml_free(ggml_ctx);
  5919. }
  5920. static void ggml_vk_check_results_1(ggml_backend_vk_context * ctx, ggml_compute_params * params, ggml_tensor * tensor) {
  5921. if (params->ith != 0) {
  5922. return;
  5923. }
  5924. if (params->type == GGML_TASK_TYPE_INIT || params->type == GGML_TASK_TYPE_FINALIZE || tensor->op == GGML_OP_TRANSPOSE) {
  5925. return;
  5926. }
  5927. if (!(vk_output_tensor > 0 && vk_output_tensor == check_counter) && check_counter <= vk_skip_checks) {
  5928. return;
  5929. }
  5930. #ifdef GGML_VULKAN_DEBUG
  5931. std::cerr << "ggml_vk_check_results_1(" << tensor->name << ")" << std::endl;
  5932. #endif
  5933. ggml_tensor * src0 = tensor->src[0];
  5934. ggml_tensor * src1 = tensor->src[1];
  5935. ggml_tensor * src2 = tensor->src[2];
  5936. void * tensor_data = tensor->data;
  5937. if (ggml_backend_buffer_is_vk(tensor->buffer)) {
  5938. size_t tensor_size = ggml_nbytes(tensor);
  5939. tensor_data = malloc(tensor_size);
  5940. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  5941. vk_buffer buffer_gpu = extra->buffer_gpu.lock();
  5942. if (extra->offset + tensor->view_offs + tensor_size >= buffer_gpu->size) {
  5943. tensor_size = buffer_gpu->size - (extra->offset + tensor->view_offs);
  5944. }
  5945. ggml_vk_buffer_read(ctx, buffer_gpu, extra->offset + tensor->view_offs, tensor_data, tensor_size);
  5946. }
  5947. float first_error_result = -1.0f;
  5948. float first_error_correct = -1.0f;
  5949. std::array<int, 4> first_error = { -1, -1, -1, -1 };
  5950. double avg_err = 0.0;
  5951. size_t counter = 0;
  5952. for (int i3 = 0; i3 < tensor->ne[3]; i3++) {
  5953. for (int i2 = 0; i2 < tensor->ne[2]; i2++) {
  5954. for (int i1 = 0; i1 < tensor->ne[1]; i1++) {
  5955. for (int i0 = 0; i0 < tensor->ne[0]; i0++) {
  5956. const bool buffer_size_fit = i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0] < comp_size;
  5957. float correct = 0.0f;
  5958. float result = 0.0f;
  5959. if (buffer_size_fit) {
  5960. if (tensor->type == GGML_TYPE_F32) {
  5961. correct = *(float *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]);
  5962. result = *(float *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]);
  5963. } else if (tensor->type == GGML_TYPE_F16) {
  5964. 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]));
  5965. 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]));
  5966. } else if (tensor->type == GGML_TYPE_I32) {
  5967. correct = *(int32_t *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]);
  5968. result = *(int32_t *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]);
  5969. } else {
  5970. std::cerr << "Results check not implemented for type " << ggml_type_name(tensor->type) << std::endl;
  5971. }
  5972. } else {
  5973. std::cerr << "Missing debug code for type " << ggml_type_name(tensor->type) << std::endl;
  5974. GGML_ASSERT(false);
  5975. }
  5976. if ((std::isnan(correct) != std::isnan(result)) || (std::isinf(correct) != std::isinf(result)) || !buffer_size_fit) {
  5977. 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;
  5978. 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;
  5979. if (src0 != nullptr) {
  5980. 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;
  5981. }
  5982. if (src1 != nullptr) {
  5983. 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;
  5984. }
  5985. if (src2 != nullptr) {
  5986. 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;
  5987. }
  5988. 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;
  5989. std::cerr << std::endl << "Result:" << std::endl;
  5990. ggml_vk_print_tensor_area(tensor, tensor_data, i0, i1, i2, i3);
  5991. std::cerr << std::endl << "Correct:" << std::endl;
  5992. ggml_vk_print_tensor_area(tensor, comp_result, i0, i1, i2, i3);
  5993. std::cerr << std::endl;
  5994. std::vector<const ggml_tensor *> done;
  5995. ggml_vk_print_graph_origin(tensor, done);
  5996. GGML_ASSERT(false);
  5997. }
  5998. if (first_error[0] == -1 && std::fabs(correct - result) > 0.1f) {
  5999. first_error[0] = i0;
  6000. first_error[1] = i1;
  6001. first_error[2] = i2;
  6002. first_error[3] = i3;
  6003. first_error_result = result;
  6004. first_error_correct = correct;
  6005. }
  6006. // Special case, value is infinite, avoid NaN result in avg_err
  6007. // NaN also appears in results, if both are nan error is 0
  6008. if (!std::isinf(correct) && !std::isinf(result) && !std::isnan(correct) && !std::isnan(result)) {
  6009. avg_err += std::fabs(correct - result);
  6010. }
  6011. counter++;
  6012. }
  6013. }
  6014. }
  6015. }
  6016. avg_err /= counter;
  6017. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  6018. std::cerr << "TENSOR CHECK: avg_err=" << avg_err << " in " << ggml_op_name(tensor->op) << " (check " << check_counter << ")" << std::endl;
  6019. 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;
  6020. if (src0 != nullptr) {
  6021. 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;
  6022. }
  6023. if (src1 != nullptr) {
  6024. 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;
  6025. }
  6026. if (src2 != nullptr) {
  6027. 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;
  6028. }
  6029. 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;
  6030. std::cerr << std::endl << "Result:" << std::endl;
  6031. ggml_vk_print_tensor_area(tensor, tensor_data, 5, 5, 0, 0);
  6032. std::cerr << std::endl << "Correct:" << std::endl;
  6033. ggml_vk_print_tensor_area(tensor, comp_result, 5, 5, 0, 0);
  6034. std::cerr << std::endl;
  6035. std::cerr << std::endl << "Result:" << std::endl;
  6036. ggml_vk_print_tensor_area(tensor, tensor_data, 5, 5, 1, 0);
  6037. std::cerr << std::endl << "Correct:" << std::endl;
  6038. ggml_vk_print_tensor_area(tensor, comp_result, 5, 5, 1, 0);
  6039. std::cerr << std::endl;
  6040. std::vector<const ggml_tensor *> done;
  6041. ggml_vk_print_graph_origin(tensor, done);
  6042. }
  6043. if (avg_err > 0.05 || std::isnan(avg_err)) {
  6044. std::cerr << "ERROR: avg_err=" << avg_err << " in " << ggml_op_name(tensor->op) << " (check " << check_counter << ")" << std::endl;
  6045. 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;
  6046. if (src0 != nullptr) {
  6047. 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;
  6048. }
  6049. if (src1 != nullptr) {
  6050. 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;
  6051. }
  6052. if (src2 != nullptr) {
  6053. 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;
  6054. }
  6055. 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;
  6056. std::cerr << std::endl << "Result:" << std::endl;
  6057. ggml_vk_print_tensor_area(tensor, tensor_data, first_error[0], first_error[1], first_error[2], first_error[3]);
  6058. std::cerr << std::endl << "Correct:" << std::endl;
  6059. ggml_vk_print_tensor_area(tensor, comp_result, first_error[0], first_error[1], first_error[2], first_error[3]);
  6060. std::cerr << std::endl;
  6061. std::vector<const ggml_tensor *> done;
  6062. ggml_vk_print_graph_origin(tensor, done);
  6063. GGML_ASSERT(false);
  6064. } else {
  6065. std::cerr << check_counter << " " << tensor->name << " op=" << ggml_op_name(tensor->op) << " avg_err=" << avg_err << std::endl;
  6066. }
  6067. free(comp_result);
  6068. comp_result = nullptr;
  6069. comp_size = 0;
  6070. if (ggml_backend_buffer_is_vk(tensor->buffer)) {
  6071. free(tensor_data);
  6072. }
  6073. }
  6074. #endif