1
0

ggml-vulkan.cpp 377 KB

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